security/nss/lib/sqlite/sqlite3.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/sqlite/sqlite3.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137413 @@
     1.4 +/******************************************************************************
     1.5 +** This file is an amalgamation of many separate C source files from SQLite
     1.6 +** version 3.7.15.  By combining all the individual C code files into this 
     1.7 +** single large file, the entire code can be compiled as a single translation
     1.8 +** unit.  This allows many compilers to do optimizations that would not be
     1.9 +** possible if the files were compiled separately.  Performance improvements
    1.10 +** of 5% or more are commonly seen when SQLite is compiled as a single
    1.11 +** translation unit.
    1.12 +**
    1.13 +** This file is all you need to compile SQLite.  To use SQLite in other
    1.14 +** programs, you need this file and the "sqlite3.h" header file that defines
    1.15 +** the programming interface to the SQLite library.  (If you do not have 
    1.16 +** the "sqlite3.h" header file at hand, you will find a copy embedded within
    1.17 +** the text of this file.  Search for "Begin file sqlite3.h" to find the start
    1.18 +** of the embedded sqlite3.h header file.) Additional code files may be needed
    1.19 +** if you want a wrapper to interface SQLite with your choice of programming
    1.20 +** language. The code for the "sqlite3" command-line shell is also in a
    1.21 +** separate file. This file contains only code for the core SQLite library.
    1.22 +*/
    1.23 +#define SQLITE_CORE 1
    1.24 +#define SQLITE_AMALGAMATION 1
    1.25 +#ifndef SQLITE_PRIVATE
    1.26 +# define SQLITE_PRIVATE static
    1.27 +#endif
    1.28 +#ifndef SQLITE_API
    1.29 +# define SQLITE_API
    1.30 +#endif
    1.31 +/************** Begin file sqliteInt.h ***************************************/
    1.32 +/*
    1.33 +** 2001 September 15
    1.34 +**
    1.35 +** The author disclaims copyright to this source code.  In place of
    1.36 +** a legal notice, here is a blessing:
    1.37 +**
    1.38 +**    May you do good and not evil.
    1.39 +**    May you find forgiveness for yourself and forgive others.
    1.40 +**    May you share freely, never taking more than you give.
    1.41 +**
    1.42 +*************************************************************************
    1.43 +** Internal interface definitions for SQLite.
    1.44 +**
    1.45 +*/
    1.46 +#ifndef _SQLITEINT_H_
    1.47 +#define _SQLITEINT_H_
    1.48 +
    1.49 +/*
    1.50 +** These #defines should enable >2GB file support on POSIX if the
    1.51 +** underlying operating system supports it.  If the OS lacks
    1.52 +** large file support, or if the OS is windows, these should be no-ops.
    1.53 +**
    1.54 +** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
    1.55 +** system #includes.  Hence, this block of code must be the very first
    1.56 +** code in all source files.
    1.57 +**
    1.58 +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    1.59 +** on the compiler command line.  This is necessary if you are compiling
    1.60 +** on a recent machine (ex: Red Hat 7.2) but you want your code to work
    1.61 +** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
    1.62 +** without this option, LFS is enable.  But LFS does not exist in the kernel
    1.63 +** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
    1.64 +** portability you should omit LFS.
    1.65 +**
    1.66 +** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
    1.67 +*/
    1.68 +#ifndef SQLITE_DISABLE_LFS
    1.69 +# define _LARGE_FILE       1
    1.70 +# ifndef _FILE_OFFSET_BITS
    1.71 +#   define _FILE_OFFSET_BITS 64
    1.72 +# endif
    1.73 +# define _LARGEFILE_SOURCE 1
    1.74 +#endif
    1.75 +
    1.76 +/*
    1.77 +** Include the configuration header output by 'configure' if we're using the
    1.78 +** autoconf-based build
    1.79 +*/
    1.80 +#ifdef _HAVE_SQLITE_CONFIG_H
    1.81 +#include "config.h"
    1.82 +#endif
    1.83 +
    1.84 +/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
    1.85 +/************** Begin file sqliteLimit.h *************************************/
    1.86 +/*
    1.87 +** 2007 May 7
    1.88 +**
    1.89 +** The author disclaims copyright to this source code.  In place of
    1.90 +** a legal notice, here is a blessing:
    1.91 +**
    1.92 +**    May you do good and not evil.
    1.93 +**    May you find forgiveness for yourself and forgive others.
    1.94 +**    May you share freely, never taking more than you give.
    1.95 +**
    1.96 +*************************************************************************
    1.97 +** 
    1.98 +** This file defines various limits of what SQLite can process.
    1.99 +*/
   1.100 +
   1.101 +/*
   1.102 +** The maximum length of a TEXT or BLOB in bytes.   This also
   1.103 +** limits the size of a row in a table or index.
   1.104 +**
   1.105 +** The hard limit is the ability of a 32-bit signed integer
   1.106 +** to count the size: 2^31-1 or 2147483647.
   1.107 +*/
   1.108 +#ifndef SQLITE_MAX_LENGTH
   1.109 +# define SQLITE_MAX_LENGTH 1000000000
   1.110 +#endif
   1.111 +
   1.112 +/*
   1.113 +** This is the maximum number of
   1.114 +**
   1.115 +**    * Columns in a table
   1.116 +**    * Columns in an index
   1.117 +**    * Columns in a view
   1.118 +**    * Terms in the SET clause of an UPDATE statement
   1.119 +**    * Terms in the result set of a SELECT statement
   1.120 +**    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
   1.121 +**    * Terms in the VALUES clause of an INSERT statement
   1.122 +**
   1.123 +** The hard upper limit here is 32676.  Most database people will
   1.124 +** tell you that in a well-normalized database, you usually should
   1.125 +** not have more than a dozen or so columns in any table.  And if
   1.126 +** that is the case, there is no point in having more than a few
   1.127 +** dozen values in any of the other situations described above.
   1.128 +*/
   1.129 +#ifndef SQLITE_MAX_COLUMN
   1.130 +# define SQLITE_MAX_COLUMN 2000
   1.131 +#endif
   1.132 +
   1.133 +/*
   1.134 +** The maximum length of a single SQL statement in bytes.
   1.135 +**
   1.136 +** It used to be the case that setting this value to zero would
   1.137 +** turn the limit off.  That is no longer true.  It is not possible
   1.138 +** to turn this limit off.
   1.139 +*/
   1.140 +#ifndef SQLITE_MAX_SQL_LENGTH
   1.141 +# define SQLITE_MAX_SQL_LENGTH 1000000000
   1.142 +#endif
   1.143 +
   1.144 +/*
   1.145 +** The maximum depth of an expression tree. This is limited to 
   1.146 +** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might 
   1.147 +** want to place more severe limits on the complexity of an 
   1.148 +** expression.
   1.149 +**
   1.150 +** A value of 0 used to mean that the limit was not enforced.
   1.151 +** But that is no longer true.  The limit is now strictly enforced
   1.152 +** at all times.
   1.153 +*/
   1.154 +#ifndef SQLITE_MAX_EXPR_DEPTH
   1.155 +# define SQLITE_MAX_EXPR_DEPTH 1000
   1.156 +#endif
   1.157 +
   1.158 +/*
   1.159 +** The maximum number of terms in a compound SELECT statement.
   1.160 +** The code generator for compound SELECT statements does one
   1.161 +** level of recursion for each term.  A stack overflow can result
   1.162 +** if the number of terms is too large.  In practice, most SQL
   1.163 +** never has more than 3 or 4 terms.  Use a value of 0 to disable
   1.164 +** any limit on the number of terms in a compount SELECT.
   1.165 +*/
   1.166 +#ifndef SQLITE_MAX_COMPOUND_SELECT
   1.167 +# define SQLITE_MAX_COMPOUND_SELECT 500
   1.168 +#endif
   1.169 +
   1.170 +/*
   1.171 +** The maximum number of opcodes in a VDBE program.
   1.172 +** Not currently enforced.
   1.173 +*/
   1.174 +#ifndef SQLITE_MAX_VDBE_OP
   1.175 +# define SQLITE_MAX_VDBE_OP 25000
   1.176 +#endif
   1.177 +
   1.178 +/*
   1.179 +** The maximum number of arguments to an SQL function.
   1.180 +*/
   1.181 +#ifndef SQLITE_MAX_FUNCTION_ARG
   1.182 +# define SQLITE_MAX_FUNCTION_ARG 127
   1.183 +#endif
   1.184 +
   1.185 +/*
   1.186 +** The maximum number of in-memory pages to use for the main database
   1.187 +** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
   1.188 +*/
   1.189 +#ifndef SQLITE_DEFAULT_CACHE_SIZE
   1.190 +# define SQLITE_DEFAULT_CACHE_SIZE  2000
   1.191 +#endif
   1.192 +#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
   1.193 +# define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
   1.194 +#endif
   1.195 +
   1.196 +/*
   1.197 +** The default number of frames to accumulate in the log file before
   1.198 +** checkpointing the database in WAL mode.
   1.199 +*/
   1.200 +#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
   1.201 +# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
   1.202 +#endif
   1.203 +
   1.204 +/*
   1.205 +** The maximum number of attached databases.  This must be between 0
   1.206 +** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
   1.207 +** is used internally to track attached databases.
   1.208 +*/
   1.209 +#ifndef SQLITE_MAX_ATTACHED
   1.210 +# define SQLITE_MAX_ATTACHED 10
   1.211 +#endif
   1.212 +
   1.213 +
   1.214 +/*
   1.215 +** The maximum value of a ?nnn wildcard that the parser will accept.
   1.216 +*/
   1.217 +#ifndef SQLITE_MAX_VARIABLE_NUMBER
   1.218 +# define SQLITE_MAX_VARIABLE_NUMBER 999
   1.219 +#endif
   1.220 +
   1.221 +/* Maximum page size.  The upper bound on this value is 65536.  This a limit
   1.222 +** imposed by the use of 16-bit offsets within each page.
   1.223 +**
   1.224 +** Earlier versions of SQLite allowed the user to change this value at
   1.225 +** compile time. This is no longer permitted, on the grounds that it creates
   1.226 +** a library that is technically incompatible with an SQLite library 
   1.227 +** compiled with a different limit. If a process operating on a database 
   1.228 +** with a page-size of 65536 bytes crashes, then an instance of SQLite 
   1.229 +** compiled with the default page-size limit will not be able to rollback 
   1.230 +** the aborted transaction. This could lead to database corruption.
   1.231 +*/
   1.232 +#ifdef SQLITE_MAX_PAGE_SIZE
   1.233 +# undef SQLITE_MAX_PAGE_SIZE
   1.234 +#endif
   1.235 +#define SQLITE_MAX_PAGE_SIZE 65536
   1.236 +
   1.237 +
   1.238 +/*
   1.239 +** The default size of a database page.
   1.240 +*/
   1.241 +#ifndef SQLITE_DEFAULT_PAGE_SIZE
   1.242 +# define SQLITE_DEFAULT_PAGE_SIZE 1024
   1.243 +#endif
   1.244 +#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
   1.245 +# undef SQLITE_DEFAULT_PAGE_SIZE
   1.246 +# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
   1.247 +#endif
   1.248 +
   1.249 +/*
   1.250 +** Ordinarily, if no value is explicitly provided, SQLite creates databases
   1.251 +** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
   1.252 +** device characteristics (sector-size and atomic write() support),
   1.253 +** SQLite may choose a larger value. This constant is the maximum value
   1.254 +** SQLite will choose on its own.
   1.255 +*/
   1.256 +#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
   1.257 +# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
   1.258 +#endif
   1.259 +#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
   1.260 +# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
   1.261 +# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
   1.262 +#endif
   1.263 +
   1.264 +
   1.265 +/*
   1.266 +** Maximum number of pages in one database file.
   1.267 +**
   1.268 +** This is really just the default value for the max_page_count pragma.
   1.269 +** This value can be lowered (or raised) at run-time using that the
   1.270 +** max_page_count macro.
   1.271 +*/
   1.272 +#ifndef SQLITE_MAX_PAGE_COUNT
   1.273 +# define SQLITE_MAX_PAGE_COUNT 1073741823
   1.274 +#endif
   1.275 +
   1.276 +/*
   1.277 +** Maximum length (in bytes) of the pattern in a LIKE or GLOB
   1.278 +** operator.
   1.279 +*/
   1.280 +#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
   1.281 +# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
   1.282 +#endif
   1.283 +
   1.284 +/*
   1.285 +** Maximum depth of recursion for triggers.
   1.286 +**
   1.287 +** A value of 1 means that a trigger program will not be able to itself
   1.288 +** fire any triggers. A value of 0 means that no trigger programs at all 
   1.289 +** may be executed.
   1.290 +*/
   1.291 +#ifndef SQLITE_MAX_TRIGGER_DEPTH
   1.292 +# define SQLITE_MAX_TRIGGER_DEPTH 1000
   1.293 +#endif
   1.294 +
   1.295 +/************** End of sqliteLimit.h *****************************************/
   1.296 +/************** Continuing where we left off in sqliteInt.h ******************/
   1.297 +
   1.298 +/* Disable nuisance warnings on Borland compilers */
   1.299 +#if defined(__BORLANDC__)
   1.300 +#pragma warn -rch /* unreachable code */
   1.301 +#pragma warn -ccc /* Condition is always true or false */
   1.302 +#pragma warn -aus /* Assigned value is never used */
   1.303 +#pragma warn -csu /* Comparing signed and unsigned */
   1.304 +#pragma warn -spa /* Suspicious pointer arithmetic */
   1.305 +#endif
   1.306 +
   1.307 +/* Needed for various definitions... */
   1.308 +#ifndef _GNU_SOURCE
   1.309 +# define _GNU_SOURCE
   1.310 +#endif
   1.311 +
   1.312 +/*
   1.313 +** Include standard header files as necessary
   1.314 +*/
   1.315 +#ifdef HAVE_STDINT_H
   1.316 +#include <stdint.h>
   1.317 +#endif
   1.318 +#ifdef HAVE_INTTYPES_H
   1.319 +#include <inttypes.h>
   1.320 +#endif
   1.321 +
   1.322 +/*
   1.323 +** The following macros are used to cast pointers to integers and
   1.324 +** integers to pointers.  The way you do this varies from one compiler
   1.325 +** to the next, so we have developed the following set of #if statements
   1.326 +** to generate appropriate macros for a wide range of compilers.
   1.327 +**
   1.328 +** The correct "ANSI" way to do this is to use the intptr_t type. 
   1.329 +** Unfortunately, that typedef is not available on all compilers, or
   1.330 +** if it is available, it requires an #include of specific headers
   1.331 +** that vary from one machine to the next.
   1.332 +**
   1.333 +** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
   1.334 +** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
   1.335 +** So we have to define the macros in different ways depending on the
   1.336 +** compiler.
   1.337 +*/
   1.338 +#if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
   1.339 +# define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
   1.340 +# define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
   1.341 +#elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
   1.342 +# define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
   1.343 +# define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
   1.344 +#elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
   1.345 +# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
   1.346 +# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
   1.347 +#else                          /* Generates a warning - but it always works */
   1.348 +# define SQLITE_INT_TO_PTR(X)  ((void*)(X))
   1.349 +# define SQLITE_PTR_TO_INT(X)  ((int)(X))
   1.350 +#endif
   1.351 +
   1.352 +/*
   1.353 +** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
   1.354 +** 0 means mutexes are permanently disable and the library is never
   1.355 +** threadsafe.  1 means the library is serialized which is the highest
   1.356 +** level of threadsafety.  2 means the libary is multithreaded - multiple
   1.357 +** threads can use SQLite as long as no two threads try to use the same
   1.358 +** database connection at the same time.
   1.359 +**
   1.360 +** Older versions of SQLite used an optional THREADSAFE macro.
   1.361 +** We support that for legacy.
   1.362 +*/
   1.363 +#if !defined(SQLITE_THREADSAFE)
   1.364 +#if defined(THREADSAFE)
   1.365 +# define SQLITE_THREADSAFE THREADSAFE
   1.366 +#else
   1.367 +# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
   1.368 +#endif
   1.369 +#endif
   1.370 +
   1.371 +/*
   1.372 +** Powersafe overwrite is on by default.  But can be turned off using
   1.373 +** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
   1.374 +*/
   1.375 +#ifndef SQLITE_POWERSAFE_OVERWRITE
   1.376 +# define SQLITE_POWERSAFE_OVERWRITE 1
   1.377 +#endif
   1.378 +
   1.379 +/*
   1.380 +** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
   1.381 +** It determines whether or not the features related to 
   1.382 +** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
   1.383 +** be overridden at runtime using the sqlite3_config() API.
   1.384 +*/
   1.385 +#if !defined(SQLITE_DEFAULT_MEMSTATUS)
   1.386 +# define SQLITE_DEFAULT_MEMSTATUS 1
   1.387 +#endif
   1.388 +
   1.389 +/*
   1.390 +** Exactly one of the following macros must be defined in order to
   1.391 +** specify which memory allocation subsystem to use.
   1.392 +**
   1.393 +**     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
   1.394 +**     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
   1.395 +**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
   1.396 +**     SQLITE_MEMDEBUG               // Debugging version of system malloc()
   1.397 +**
   1.398 +** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
   1.399 +** assert() macro is enabled, each call into the Win32 native heap subsystem
   1.400 +** will cause HeapValidate to be called.  If heap validation should fail, an
   1.401 +** assertion will be triggered.
   1.402 +**
   1.403 +** (Historical note:  There used to be several other options, but we've
   1.404 +** pared it down to just these three.)
   1.405 +**
   1.406 +** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
   1.407 +** the default.
   1.408 +*/
   1.409 +#if defined(SQLITE_SYSTEM_MALLOC) \
   1.410 +  + defined(SQLITE_WIN32_MALLOC) \
   1.411 +  + defined(SQLITE_ZERO_MALLOC) \
   1.412 +  + defined(SQLITE_MEMDEBUG)>1
   1.413 +# error "Two or more of the following compile-time configuration options\
   1.414 + are defined but at most one is allowed:\
   1.415 + SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
   1.416 + SQLITE_ZERO_MALLOC"
   1.417 +#endif
   1.418 +#if defined(SQLITE_SYSTEM_MALLOC) \
   1.419 +  + defined(SQLITE_WIN32_MALLOC) \
   1.420 +  + defined(SQLITE_ZERO_MALLOC) \
   1.421 +  + defined(SQLITE_MEMDEBUG)==0
   1.422 +# define SQLITE_SYSTEM_MALLOC 1
   1.423 +#endif
   1.424 +
   1.425 +/*
   1.426 +** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
   1.427 +** sizes of memory allocations below this value where possible.
   1.428 +*/
   1.429 +#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
   1.430 +# define SQLITE_MALLOC_SOFT_LIMIT 1024
   1.431 +#endif
   1.432 +
   1.433 +/*
   1.434 +** We need to define _XOPEN_SOURCE as follows in order to enable
   1.435 +** recursive mutexes on most Unix systems.  But Mac OS X is different.
   1.436 +** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
   1.437 +** so it is omitted there.  See ticket #2673.
   1.438 +**
   1.439 +** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
   1.440 +** implemented on some systems.  So we avoid defining it at all
   1.441 +** if it is already defined or if it is unneeded because we are
   1.442 +** not doing a threadsafe build.  Ticket #2681.
   1.443 +**
   1.444 +** See also ticket #2741.
   1.445 +*/
   1.446 +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
   1.447 +#  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
   1.448 +#endif
   1.449 +
   1.450 +/*
   1.451 +** The TCL headers are only needed when compiling the TCL bindings.
   1.452 +*/
   1.453 +#if defined(SQLITE_TCL) || defined(TCLSH)
   1.454 +# include <tcl.h>
   1.455 +#endif
   1.456 +
   1.457 +/*
   1.458 +** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
   1.459 +** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
   1.460 +** make it true by defining or undefining NDEBUG.
   1.461 +**
   1.462 +** Setting NDEBUG makes the code smaller and run faster by disabling the
   1.463 +** number assert() statements in the code.  So we want the default action
   1.464 +** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
   1.465 +** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
   1.466 +** feature.
   1.467 +*/
   1.468 +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
   1.469 +# define NDEBUG 1
   1.470 +#endif
   1.471 +#if defined(NDEBUG) && defined(SQLITE_DEBUG)
   1.472 +# undef NDEBUG
   1.473 +#endif
   1.474 +
   1.475 +/*
   1.476 +** The testcase() macro is used to aid in coverage testing.  When 
   1.477 +** doing coverage testing, the condition inside the argument to
   1.478 +** testcase() must be evaluated both true and false in order to
   1.479 +** get full branch coverage.  The testcase() macro is inserted
   1.480 +** to help ensure adequate test coverage in places where simple
   1.481 +** condition/decision coverage is inadequate.  For example, testcase()
   1.482 +** can be used to make sure boundary values are tested.  For
   1.483 +** bitmask tests, testcase() can be used to make sure each bit
   1.484 +** is significant and used at least once.  On switch statements
   1.485 +** where multiple cases go to the same block of code, testcase()
   1.486 +** can insure that all cases are evaluated.
   1.487 +**
   1.488 +*/
   1.489 +#ifdef SQLITE_COVERAGE_TEST
   1.490 +SQLITE_PRIVATE   void sqlite3Coverage(int);
   1.491 +# define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
   1.492 +#else
   1.493 +# define testcase(X)
   1.494 +#endif
   1.495 +
   1.496 +/*
   1.497 +** The TESTONLY macro is used to enclose variable declarations or
   1.498 +** other bits of code that are needed to support the arguments
   1.499 +** within testcase() and assert() macros.
   1.500 +*/
   1.501 +#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
   1.502 +# define TESTONLY(X)  X
   1.503 +#else
   1.504 +# define TESTONLY(X)
   1.505 +#endif
   1.506 +
   1.507 +/*
   1.508 +** Sometimes we need a small amount of code such as a variable initialization
   1.509 +** to setup for a later assert() statement.  We do not want this code to
   1.510 +** appear when assert() is disabled.  The following macro is therefore
   1.511 +** used to contain that setup code.  The "VVA" acronym stands for
   1.512 +** "Verification, Validation, and Accreditation".  In other words, the
   1.513 +** code within VVA_ONLY() will only run during verification processes.
   1.514 +*/
   1.515 +#ifndef NDEBUG
   1.516 +# define VVA_ONLY(X)  X
   1.517 +#else
   1.518 +# define VVA_ONLY(X)
   1.519 +#endif
   1.520 +
   1.521 +/*
   1.522 +** The ALWAYS and NEVER macros surround boolean expressions which 
   1.523 +** are intended to always be true or false, respectively.  Such
   1.524 +** expressions could be omitted from the code completely.  But they
   1.525 +** are included in a few cases in order to enhance the resilience
   1.526 +** of SQLite to unexpected behavior - to make the code "self-healing"
   1.527 +** or "ductile" rather than being "brittle" and crashing at the first
   1.528 +** hint of unplanned behavior.
   1.529 +**
   1.530 +** In other words, ALWAYS and NEVER are added for defensive code.
   1.531 +**
   1.532 +** When doing coverage testing ALWAYS and NEVER are hard-coded to
   1.533 +** be true and false so that the unreachable code then specify will
   1.534 +** not be counted as untested code.
   1.535 +*/
   1.536 +#if defined(SQLITE_COVERAGE_TEST)
   1.537 +# define ALWAYS(X)      (1)
   1.538 +# define NEVER(X)       (0)
   1.539 +#elif !defined(NDEBUG)
   1.540 +# define ALWAYS(X)      ((X)?1:(assert(0),0))
   1.541 +# define NEVER(X)       ((X)?(assert(0),1):0)
   1.542 +#else
   1.543 +# define ALWAYS(X)      (X)
   1.544 +# define NEVER(X)       (X)
   1.545 +#endif
   1.546 +
   1.547 +/*
   1.548 +** Return true (non-zero) if the input is a integer that is too large
   1.549 +** to fit in 32-bits.  This macro is used inside of various testcase()
   1.550 +** macros to verify that we have tested SQLite for large-file support.
   1.551 +*/
   1.552 +#define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
   1.553 +
   1.554 +/*
   1.555 +** The macro unlikely() is a hint that surrounds a boolean
   1.556 +** expression that is usually false.  Macro likely() surrounds
   1.557 +** a boolean expression that is usually true.  GCC is able to
   1.558 +** use these hints to generate better code, sometimes.
   1.559 +*/
   1.560 +#if defined(__GNUC__) && 0
   1.561 +# define likely(X)    __builtin_expect((X),1)
   1.562 +# define unlikely(X)  __builtin_expect((X),0)
   1.563 +#else
   1.564 +# define likely(X)    !!(X)
   1.565 +# define unlikely(X)  !!(X)
   1.566 +#endif
   1.567 +
   1.568 +/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
   1.569 +/************** Begin file sqlite3.h *****************************************/
   1.570 +/*
   1.571 +** 2001 September 15
   1.572 +**
   1.573 +** The author disclaims copyright to this source code.  In place of
   1.574 +** a legal notice, here is a blessing:
   1.575 +**
   1.576 +**    May you do good and not evil.
   1.577 +**    May you find forgiveness for yourself and forgive others.
   1.578 +**    May you share freely, never taking more than you give.
   1.579 +**
   1.580 +*************************************************************************
   1.581 +** This header file defines the interface that the SQLite library
   1.582 +** presents to client programs.  If a C-function, structure, datatype,
   1.583 +** or constant definition does not appear in this file, then it is
   1.584 +** not a published API of SQLite, is subject to change without
   1.585 +** notice, and should not be referenced by programs that use SQLite.
   1.586 +**
   1.587 +** Some of the definitions that are in this file are marked as
   1.588 +** "experimental".  Experimental interfaces are normally new
   1.589 +** features recently added to SQLite.  We do not anticipate changes
   1.590 +** to experimental interfaces but reserve the right to make minor changes
   1.591 +** if experience from use "in the wild" suggest such changes are prudent.
   1.592 +**
   1.593 +** The official C-language API documentation for SQLite is derived
   1.594 +** from comments in this file.  This file is the authoritative source
   1.595 +** on how SQLite interfaces are suppose to operate.
   1.596 +**
   1.597 +** The name of this file under configuration management is "sqlite.h.in".
   1.598 +** The makefile makes some minor changes to this file (such as inserting
   1.599 +** the version number) and changes its name to "sqlite3.h" as
   1.600 +** part of the build process.
   1.601 +*/
   1.602 +#ifndef _SQLITE3_H_
   1.603 +#define _SQLITE3_H_
   1.604 +#include <stdarg.h>     /* Needed for the definition of va_list */
   1.605 +
   1.606 +/*
   1.607 +** Make sure we can call this stuff from C++.
   1.608 +*/
   1.609 +#if 0
   1.610 +extern "C" {
   1.611 +#endif
   1.612 +
   1.613 +
   1.614 +/*
   1.615 +** Add the ability to override 'extern'
   1.616 +*/
   1.617 +#ifndef SQLITE_EXTERN
   1.618 +# define SQLITE_EXTERN extern
   1.619 +#endif
   1.620 +
   1.621 +#ifndef SQLITE_API
   1.622 +# define SQLITE_API
   1.623 +#endif
   1.624 +
   1.625 +
   1.626 +/*
   1.627 +** These no-op macros are used in front of interfaces to mark those
   1.628 +** interfaces as either deprecated or experimental.  New applications
   1.629 +** should not use deprecated interfaces - they are support for backwards
   1.630 +** compatibility only.  Application writers should be aware that
   1.631 +** experimental interfaces are subject to change in point releases.
   1.632 +**
   1.633 +** These macros used to resolve to various kinds of compiler magic that
   1.634 +** would generate warning messages when they were used.  But that
   1.635 +** compiler magic ended up generating such a flurry of bug reports
   1.636 +** that we have taken it all out and gone back to using simple
   1.637 +** noop macros.
   1.638 +*/
   1.639 +#define SQLITE_DEPRECATED
   1.640 +#define SQLITE_EXPERIMENTAL
   1.641 +
   1.642 +/*
   1.643 +** Ensure these symbols were not defined by some previous header file.
   1.644 +*/
   1.645 +#ifdef SQLITE_VERSION
   1.646 +# undef SQLITE_VERSION
   1.647 +#endif
   1.648 +#ifdef SQLITE_VERSION_NUMBER
   1.649 +# undef SQLITE_VERSION_NUMBER
   1.650 +#endif
   1.651 +
   1.652 +/*
   1.653 +** CAPI3REF: Compile-Time Library Version Numbers
   1.654 +**
   1.655 +** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
   1.656 +** evaluates to a string literal that is the SQLite version in the
   1.657 +** format "X.Y.Z" where X is the major version number (always 3 for
   1.658 +** SQLite3) and Y is the minor version number and Z is the release number.)^
   1.659 +** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
   1.660 +** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
   1.661 +** numbers used in [SQLITE_VERSION].)^
   1.662 +** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
   1.663 +** be larger than the release from which it is derived.  Either Y will
   1.664 +** be held constant and Z will be incremented or else Y will be incremented
   1.665 +** and Z will be reset to zero.
   1.666 +**
   1.667 +** Since version 3.6.18, SQLite source code has been stored in the
   1.668 +** <a href="http://www.fossil-scm.org/">Fossil configuration management
   1.669 +** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   1.670 +** a string which identifies a particular check-in of SQLite
   1.671 +** within its configuration management system.  ^The SQLITE_SOURCE_ID
   1.672 +** string contains the date and time of the check-in (UTC) and an SHA1
   1.673 +** hash of the entire source tree.
   1.674 +**
   1.675 +** See also: [sqlite3_libversion()],
   1.676 +** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   1.677 +** [sqlite_version()] and [sqlite_source_id()].
   1.678 +*/
   1.679 +#define SQLITE_VERSION        "3.7.15"
   1.680 +#define SQLITE_VERSION_NUMBER 3007015
   1.681 +#define SQLITE_SOURCE_ID      "2012-12-12 13:36:53 cd0b37c52658bfdf992b1e3dc467bae1835a94ae"
   1.682 +
   1.683 +/*
   1.684 +** CAPI3REF: Run-Time Library Version Numbers
   1.685 +** KEYWORDS: sqlite3_version, sqlite3_sourceid
   1.686 +**
   1.687 +** These interfaces provide the same information as the [SQLITE_VERSION],
   1.688 +** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
   1.689 +** but are associated with the library instead of the header file.  ^(Cautious
   1.690 +** programmers might include assert() statements in their application to
   1.691 +** verify that values returned by these interfaces match the macros in
   1.692 +** the header, and thus insure that the application is
   1.693 +** compiled with matching library and header files.
   1.694 +**
   1.695 +** <blockquote><pre>
   1.696 +** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
   1.697 +** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
   1.698 +** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
   1.699 +** </pre></blockquote>)^
   1.700 +**
   1.701 +** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
   1.702 +** macro.  ^The sqlite3_libversion() function returns a pointer to the
   1.703 +** to the sqlite3_version[] string constant.  The sqlite3_libversion()
   1.704 +** function is provided for use in DLLs since DLL users usually do not have
   1.705 +** direct access to string constants within the DLL.  ^The
   1.706 +** sqlite3_libversion_number() function returns an integer equal to
   1.707 +** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
   1.708 +** a pointer to a string constant whose value is the same as the 
   1.709 +** [SQLITE_SOURCE_ID] C preprocessor macro.
   1.710 +**
   1.711 +** See also: [sqlite_version()] and [sqlite_source_id()].
   1.712 +*/
   1.713 +SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
   1.714 +SQLITE_API const char *sqlite3_libversion(void);
   1.715 +SQLITE_API const char *sqlite3_sourceid(void);
   1.716 +SQLITE_API int sqlite3_libversion_number(void);
   1.717 +
   1.718 +/*
   1.719 +** CAPI3REF: Run-Time Library Compilation Options Diagnostics
   1.720 +**
   1.721 +** ^The sqlite3_compileoption_used() function returns 0 or 1 
   1.722 +** indicating whether the specified option was defined at 
   1.723 +** compile time.  ^The SQLITE_ prefix may be omitted from the 
   1.724 +** option name passed to sqlite3_compileoption_used().  
   1.725 +**
   1.726 +** ^The sqlite3_compileoption_get() function allows iterating
   1.727 +** over the list of options that were defined at compile time by
   1.728 +** returning the N-th compile time option string.  ^If N is out of range,
   1.729 +** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_ 
   1.730 +** prefix is omitted from any strings returned by 
   1.731 +** sqlite3_compileoption_get().
   1.732 +**
   1.733 +** ^Support for the diagnostic functions sqlite3_compileoption_used()
   1.734 +** and sqlite3_compileoption_get() may be omitted by specifying the 
   1.735 +** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
   1.736 +**
   1.737 +** See also: SQL functions [sqlite_compileoption_used()] and
   1.738 +** [sqlite_compileoption_get()] and the [compile_options pragma].
   1.739 +*/
   1.740 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   1.741 +SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
   1.742 +SQLITE_API const char *sqlite3_compileoption_get(int N);
   1.743 +#endif
   1.744 +
   1.745 +/*
   1.746 +** CAPI3REF: Test To See If The Library Is Threadsafe
   1.747 +**
   1.748 +** ^The sqlite3_threadsafe() function returns zero if and only if
   1.749 +** SQLite was compiled with mutexing code omitted due to the
   1.750 +** [SQLITE_THREADSAFE] compile-time option being set to 0.
   1.751 +**
   1.752 +** SQLite can be compiled with or without mutexes.  When
   1.753 +** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
   1.754 +** are enabled and SQLite is threadsafe.  When the
   1.755 +** [SQLITE_THREADSAFE] macro is 0, 
   1.756 +** the mutexes are omitted.  Without the mutexes, it is not safe
   1.757 +** to use SQLite concurrently from more than one thread.
   1.758 +**
   1.759 +** Enabling mutexes incurs a measurable performance penalty.
   1.760 +** So if speed is of utmost importance, it makes sense to disable
   1.761 +** the mutexes.  But for maximum safety, mutexes should be enabled.
   1.762 +** ^The default behavior is for mutexes to be enabled.
   1.763 +**
   1.764 +** This interface can be used by an application to make sure that the
   1.765 +** version of SQLite that it is linking against was compiled with
   1.766 +** the desired setting of the [SQLITE_THREADSAFE] macro.
   1.767 +**
   1.768 +** This interface only reports on the compile-time mutex setting
   1.769 +** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
   1.770 +** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
   1.771 +** can be fully or partially disabled using a call to [sqlite3_config()]
   1.772 +** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
   1.773 +** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
   1.774 +** sqlite3_threadsafe() function shows only the compile-time setting of
   1.775 +** thread safety, not any run-time changes to that setting made by
   1.776 +** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
   1.777 +** is unchanged by calls to sqlite3_config().)^
   1.778 +**
   1.779 +** See the [threading mode] documentation for additional information.
   1.780 +*/
   1.781 +SQLITE_API int sqlite3_threadsafe(void);
   1.782 +
   1.783 +/*
   1.784 +** CAPI3REF: Database Connection Handle
   1.785 +** KEYWORDS: {database connection} {database connections}
   1.786 +**
   1.787 +** Each open SQLite database is represented by a pointer to an instance of
   1.788 +** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
   1.789 +** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
   1.790 +** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
   1.791 +** and [sqlite3_close_v2()] are its destructors.  There are many other
   1.792 +** interfaces (such as
   1.793 +** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
   1.794 +** [sqlite3_busy_timeout()] to name but three) that are methods on an
   1.795 +** sqlite3 object.
   1.796 +*/
   1.797 +typedef struct sqlite3 sqlite3;
   1.798 +
   1.799 +/*
   1.800 +** CAPI3REF: 64-Bit Integer Types
   1.801 +** KEYWORDS: sqlite_int64 sqlite_uint64
   1.802 +**
   1.803 +** Because there is no cross-platform way to specify 64-bit integer types
   1.804 +** SQLite includes typedefs for 64-bit signed and unsigned integers.
   1.805 +**
   1.806 +** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
   1.807 +** The sqlite_int64 and sqlite_uint64 types are supported for backwards
   1.808 +** compatibility only.
   1.809 +**
   1.810 +** ^The sqlite3_int64 and sqlite_int64 types can store integer values
   1.811 +** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
   1.812 +** sqlite3_uint64 and sqlite_uint64 types can store integer values 
   1.813 +** between 0 and +18446744073709551615 inclusive.
   1.814 +*/
   1.815 +#ifdef SQLITE_INT64_TYPE
   1.816 +  typedef SQLITE_INT64_TYPE sqlite_int64;
   1.817 +  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
   1.818 +#elif defined(_MSC_VER) || defined(__BORLANDC__)
   1.819 +  typedef __int64 sqlite_int64;
   1.820 +  typedef unsigned __int64 sqlite_uint64;
   1.821 +#else
   1.822 +  typedef long long int sqlite_int64;
   1.823 +  typedef unsigned long long int sqlite_uint64;
   1.824 +#endif
   1.825 +typedef sqlite_int64 sqlite3_int64;
   1.826 +typedef sqlite_uint64 sqlite3_uint64;
   1.827 +
   1.828 +/*
   1.829 +** If compiling for a processor that lacks floating point support,
   1.830 +** substitute integer for floating-point.
   1.831 +*/
   1.832 +#ifdef SQLITE_OMIT_FLOATING_POINT
   1.833 +# define double sqlite3_int64
   1.834 +#endif
   1.835 +
   1.836 +/*
   1.837 +** CAPI3REF: Closing A Database Connection
   1.838 +**
   1.839 +** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   1.840 +** for the [sqlite3] object.
   1.841 +** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
   1.842 +** the [sqlite3] object is successfully destroyed and all associated
   1.843 +** resources are deallocated.
   1.844 +**
   1.845 +** ^If the database connection is associated with unfinalized prepared
   1.846 +** statements or unfinished sqlite3_backup objects then sqlite3_close()
   1.847 +** will leave the database connection open and return [SQLITE_BUSY].
   1.848 +** ^If sqlite3_close_v2() is called with unfinalized prepared statements
   1.849 +** and unfinished sqlite3_backups, then the database connection becomes
   1.850 +** an unusable "zombie" which will automatically be deallocated when the
   1.851 +** last prepared statement is finalized or the last sqlite3_backup is
   1.852 +** finished.  The sqlite3_close_v2() interface is intended for use with
   1.853 +** host languages that are garbage collected, and where the order in which
   1.854 +** destructors are called is arbitrary.
   1.855 +**
   1.856 +** Applications should [sqlite3_finalize | finalize] all [prepared statements],
   1.857 +** [sqlite3_blob_close | close] all [BLOB handles], and 
   1.858 +** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
   1.859 +** with the [sqlite3] object prior to attempting to close the object.  ^If
   1.860 +** sqlite3_close() is called on a [database connection] that still has
   1.861 +** outstanding [prepared statements], [BLOB handles], and/or
   1.862 +** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
   1.863 +** of resources is deferred until all [prepared statements], [BLOB handles],
   1.864 +** and [sqlite3_backup] objects are also destroyed.
   1.865 +**
   1.866 +** ^If an [sqlite3] object is destroyed while a transaction is open,
   1.867 +** the transaction is automatically rolled back.
   1.868 +**
   1.869 +** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
   1.870 +** must be either a NULL
   1.871 +** pointer or an [sqlite3] object pointer obtained
   1.872 +** from [sqlite3_open()], [sqlite3_open16()], or
   1.873 +** [sqlite3_open_v2()], and not previously closed.
   1.874 +** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
   1.875 +** argument is a harmless no-op.
   1.876 +*/
   1.877 +SQLITE_API int sqlite3_close(sqlite3*);
   1.878 +SQLITE_API int sqlite3_close_v2(sqlite3*);
   1.879 +
   1.880 +/*
   1.881 +** The type for a callback function.
   1.882 +** This is legacy and deprecated.  It is included for historical
   1.883 +** compatibility and is not documented.
   1.884 +*/
   1.885 +typedef int (*sqlite3_callback)(void*,int,char**, char**);
   1.886 +
   1.887 +/*
   1.888 +** CAPI3REF: One-Step Query Execution Interface
   1.889 +**
   1.890 +** The sqlite3_exec() interface is a convenience wrapper around
   1.891 +** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
   1.892 +** that allows an application to run multiple statements of SQL
   1.893 +** without having to use a lot of C code. 
   1.894 +**
   1.895 +** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
   1.896 +** semicolon-separate SQL statements passed into its 2nd argument,
   1.897 +** in the context of the [database connection] passed in as its 1st
   1.898 +** argument.  ^If the callback function of the 3rd argument to
   1.899 +** sqlite3_exec() is not NULL, then it is invoked for each result row
   1.900 +** coming out of the evaluated SQL statements.  ^The 4th argument to
   1.901 +** sqlite3_exec() is relayed through to the 1st argument of each
   1.902 +** callback invocation.  ^If the callback pointer to sqlite3_exec()
   1.903 +** is NULL, then no callback is ever invoked and result rows are
   1.904 +** ignored.
   1.905 +**
   1.906 +** ^If an error occurs while evaluating the SQL statements passed into
   1.907 +** sqlite3_exec(), then execution of the current statement stops and
   1.908 +** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
   1.909 +** is not NULL then any error message is written into memory obtained
   1.910 +** from [sqlite3_malloc()] and passed back through the 5th parameter.
   1.911 +** To avoid memory leaks, the application should invoke [sqlite3_free()]
   1.912 +** on error message strings returned through the 5th parameter of
   1.913 +** of sqlite3_exec() after the error message string is no longer needed.
   1.914 +** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
   1.915 +** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
   1.916 +** NULL before returning.
   1.917 +**
   1.918 +** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
   1.919 +** routine returns SQLITE_ABORT without invoking the callback again and
   1.920 +** without running any subsequent SQL statements.
   1.921 +**
   1.922 +** ^The 2nd argument to the sqlite3_exec() callback function is the
   1.923 +** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
   1.924 +** callback is an array of pointers to strings obtained as if from
   1.925 +** [sqlite3_column_text()], one for each column.  ^If an element of a
   1.926 +** result row is NULL then the corresponding string pointer for the
   1.927 +** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
   1.928 +** sqlite3_exec() callback is an array of pointers to strings where each
   1.929 +** entry represents the name of corresponding result column as obtained
   1.930 +** from [sqlite3_column_name()].
   1.931 +**
   1.932 +** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
   1.933 +** to an empty string, or a pointer that contains only whitespace and/or 
   1.934 +** SQL comments, then no SQL statements are evaluated and the database
   1.935 +** is not changed.
   1.936 +**
   1.937 +** Restrictions:
   1.938 +**
   1.939 +** <ul>
   1.940 +** <li> The application must insure that the 1st parameter to sqlite3_exec()
   1.941 +**      is a valid and open [database connection].
   1.942 +** <li> The application must not close [database connection] specified by
   1.943 +**      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
   1.944 +** <li> The application must not modify the SQL statement text passed into
   1.945 +**      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
   1.946 +** </ul>
   1.947 +*/
   1.948 +SQLITE_API int sqlite3_exec(
   1.949 +  sqlite3*,                                  /* An open database */
   1.950 +  const char *sql,                           /* SQL to be evaluated */
   1.951 +  int (*callback)(void*,int,char**,char**),  /* Callback function */
   1.952 +  void *,                                    /* 1st argument to callback */
   1.953 +  char **errmsg                              /* Error msg written here */
   1.954 +);
   1.955 +
   1.956 +/*
   1.957 +** CAPI3REF: Result Codes
   1.958 +** KEYWORDS: SQLITE_OK {error code} {error codes}
   1.959 +** KEYWORDS: {result code} {result codes}
   1.960 +**
   1.961 +** Many SQLite functions return an integer result code from the set shown
   1.962 +** here in order to indicate success or failure.
   1.963 +**
   1.964 +** New error codes may be added in future versions of SQLite.
   1.965 +**
   1.966 +** See also: [SQLITE_IOERR_READ | extended result codes],
   1.967 +** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
   1.968 +*/
   1.969 +#define SQLITE_OK           0   /* Successful result */
   1.970 +/* beginning-of-error-codes */
   1.971 +#define SQLITE_ERROR        1   /* SQL error or missing database */
   1.972 +#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
   1.973 +#define SQLITE_PERM         3   /* Access permission denied */
   1.974 +#define SQLITE_ABORT        4   /* Callback routine requested an abort */
   1.975 +#define SQLITE_BUSY         5   /* The database file is locked */
   1.976 +#define SQLITE_LOCKED       6   /* A table in the database is locked */
   1.977 +#define SQLITE_NOMEM        7   /* A malloc() failed */
   1.978 +#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
   1.979 +#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
   1.980 +#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
   1.981 +#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
   1.982 +#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
   1.983 +#define SQLITE_FULL        13   /* Insertion failed because database is full */
   1.984 +#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
   1.985 +#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
   1.986 +#define SQLITE_EMPTY       16   /* Database is empty */
   1.987 +#define SQLITE_SCHEMA      17   /* The database schema changed */
   1.988 +#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
   1.989 +#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
   1.990 +#define SQLITE_MISMATCH    20   /* Data type mismatch */
   1.991 +#define SQLITE_MISUSE      21   /* Library used incorrectly */
   1.992 +#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
   1.993 +#define SQLITE_AUTH        23   /* Authorization denied */
   1.994 +#define SQLITE_FORMAT      24   /* Auxiliary database format error */
   1.995 +#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
   1.996 +#define SQLITE_NOTADB      26   /* File opened that is not a database file */
   1.997 +#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
   1.998 +#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
   1.999 +/* end-of-error-codes */
  1.1000 +
  1.1001 +/*
  1.1002 +** CAPI3REF: Extended Result Codes
  1.1003 +** KEYWORDS: {extended error code} {extended error codes}
  1.1004 +** KEYWORDS: {extended result code} {extended result codes}
  1.1005 +**
  1.1006 +** In its default configuration, SQLite API routines return one of 26 integer
  1.1007 +** [SQLITE_OK | result codes].  However, experience has shown that many of
  1.1008 +** these result codes are too coarse-grained.  They do not provide as
  1.1009 +** much information about problems as programmers might like.  In an effort to
  1.1010 +** address this, newer versions of SQLite (version 3.3.8 and later) include
  1.1011 +** support for additional result codes that provide more detailed information
  1.1012 +** about errors. The extended result codes are enabled or disabled
  1.1013 +** on a per database connection basis using the
  1.1014 +** [sqlite3_extended_result_codes()] API.
  1.1015 +**
  1.1016 +** Some of the available extended result codes are listed here.
  1.1017 +** One may expect the number of extended result codes will be expand
  1.1018 +** over time.  Software that uses extended result codes should expect
  1.1019 +** to see new result codes in future releases of SQLite.
  1.1020 +**
  1.1021 +** The SQLITE_OK result code will never be extended.  It will always
  1.1022 +** be exactly zero.
  1.1023 +*/
  1.1024 +#define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
  1.1025 +#define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
  1.1026 +#define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
  1.1027 +#define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
  1.1028 +#define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
  1.1029 +#define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
  1.1030 +#define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
  1.1031 +#define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
  1.1032 +#define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
  1.1033 +#define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
  1.1034 +#define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
  1.1035 +#define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
  1.1036 +#define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
  1.1037 +#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
  1.1038 +#define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
  1.1039 +#define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
  1.1040 +#define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
  1.1041 +#define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
  1.1042 +#define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
  1.1043 +#define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
  1.1044 +#define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
  1.1045 +#define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
  1.1046 +#define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
  1.1047 +#define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
  1.1048 +#define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
  1.1049 +#define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
  1.1050 +#define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
  1.1051 +#define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
  1.1052 +#define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
  1.1053 +#define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
  1.1054 +#define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
  1.1055 +#define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
  1.1056 +
  1.1057 +/*
  1.1058 +** CAPI3REF: Flags For File Open Operations
  1.1059 +**
  1.1060 +** These bit values are intended for use in the
  1.1061 +** 3rd parameter to the [sqlite3_open_v2()] interface and
  1.1062 +** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
  1.1063 +*/
  1.1064 +#define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
  1.1065 +#define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
  1.1066 +#define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
  1.1067 +#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
  1.1068 +#define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
  1.1069 +#define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
  1.1070 +#define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
  1.1071 +#define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
  1.1072 +#define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
  1.1073 +#define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
  1.1074 +#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
  1.1075 +#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
  1.1076 +#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
  1.1077 +#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
  1.1078 +#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
  1.1079 +#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
  1.1080 +#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
  1.1081 +#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
  1.1082 +#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
  1.1083 +#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
  1.1084 +
  1.1085 +/* Reserved:                         0x00F00000 */
  1.1086 +
  1.1087 +/*
  1.1088 +** CAPI3REF: Device Characteristics
  1.1089 +**
  1.1090 +** The xDeviceCharacteristics method of the [sqlite3_io_methods]
  1.1091 +** object returns an integer which is a vector of these
  1.1092 +** bit values expressing I/O characteristics of the mass storage
  1.1093 +** device that holds the file that the [sqlite3_io_methods]
  1.1094 +** refers to.
  1.1095 +**
  1.1096 +** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1.1097 +** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
  1.1098 +** mean that writes of blocks that are nnn bytes in size and
  1.1099 +** are aligned to an address which is an integer multiple of
  1.1100 +** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
  1.1101 +** that when data is appended to a file, the data is appended
  1.1102 +** first then the size of the file is extended, never the other
  1.1103 +** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
  1.1104 +** information is written to disk in the same order as calls
  1.1105 +** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
  1.1106 +** after reboot following a crash or power loss, the only bytes in a
  1.1107 +** file that were written at the application level might have changed
  1.1108 +** and that adjacent bytes, even bytes within the same sector are
  1.1109 +** guaranteed to be unchanged.
  1.1110 +*/
  1.1111 +#define SQLITE_IOCAP_ATOMIC                 0x00000001
  1.1112 +#define SQLITE_IOCAP_ATOMIC512              0x00000002
  1.1113 +#define SQLITE_IOCAP_ATOMIC1K               0x00000004
  1.1114 +#define SQLITE_IOCAP_ATOMIC2K               0x00000008
  1.1115 +#define SQLITE_IOCAP_ATOMIC4K               0x00000010
  1.1116 +#define SQLITE_IOCAP_ATOMIC8K               0x00000020
  1.1117 +#define SQLITE_IOCAP_ATOMIC16K              0x00000040
  1.1118 +#define SQLITE_IOCAP_ATOMIC32K              0x00000080
  1.1119 +#define SQLITE_IOCAP_ATOMIC64K              0x00000100
  1.1120 +#define SQLITE_IOCAP_SAFE_APPEND            0x00000200
  1.1121 +#define SQLITE_IOCAP_SEQUENTIAL             0x00000400
  1.1122 +#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
  1.1123 +#define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
  1.1124 +
  1.1125 +/*
  1.1126 +** CAPI3REF: File Locking Levels
  1.1127 +**
  1.1128 +** SQLite uses one of these integer values as the second
  1.1129 +** argument to calls it makes to the xLock() and xUnlock() methods
  1.1130 +** of an [sqlite3_io_methods] object.
  1.1131 +*/
  1.1132 +#define SQLITE_LOCK_NONE          0
  1.1133 +#define SQLITE_LOCK_SHARED        1
  1.1134 +#define SQLITE_LOCK_RESERVED      2
  1.1135 +#define SQLITE_LOCK_PENDING       3
  1.1136 +#define SQLITE_LOCK_EXCLUSIVE     4
  1.1137 +
  1.1138 +/*
  1.1139 +** CAPI3REF: Synchronization Type Flags
  1.1140 +**
  1.1141 +** When SQLite invokes the xSync() method of an
  1.1142 +** [sqlite3_io_methods] object it uses a combination of
  1.1143 +** these integer values as the second argument.
  1.1144 +**
  1.1145 +** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
  1.1146 +** sync operation only needs to flush data to mass storage.  Inode
  1.1147 +** information need not be flushed. If the lower four bits of the flag
  1.1148 +** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
  1.1149 +** If the lower four bits equal SQLITE_SYNC_FULL, that means
  1.1150 +** to use Mac OS X style fullsync instead of fsync().
  1.1151 +**
  1.1152 +** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
  1.1153 +** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
  1.1154 +** settings.  The [synchronous pragma] determines when calls to the
  1.1155 +** xSync VFS method occur and applies uniformly across all platforms.
  1.1156 +** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
  1.1157 +** energetic or rigorous or forceful the sync operations are and
  1.1158 +** only make a difference on Mac OSX for the default SQLite code.
  1.1159 +** (Third-party VFS implementations might also make the distinction
  1.1160 +** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
  1.1161 +** operating systems natively supported by SQLite, only Mac OSX
  1.1162 +** cares about the difference.)
  1.1163 +*/
  1.1164 +#define SQLITE_SYNC_NORMAL        0x00002
  1.1165 +#define SQLITE_SYNC_FULL          0x00003
  1.1166 +#define SQLITE_SYNC_DATAONLY      0x00010
  1.1167 +
  1.1168 +/*
  1.1169 +** CAPI3REF: OS Interface Open File Handle
  1.1170 +**
  1.1171 +** An [sqlite3_file] object represents an open file in the 
  1.1172 +** [sqlite3_vfs | OS interface layer].  Individual OS interface
  1.1173 +** implementations will
  1.1174 +** want to subclass this object by appending additional fields
  1.1175 +** for their own use.  The pMethods entry is a pointer to an
  1.1176 +** [sqlite3_io_methods] object that defines methods for performing
  1.1177 +** I/O operations on the open file.
  1.1178 +*/
  1.1179 +typedef struct sqlite3_file sqlite3_file;
  1.1180 +struct sqlite3_file {
  1.1181 +  const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
  1.1182 +};
  1.1183 +
  1.1184 +/*
  1.1185 +** CAPI3REF: OS Interface File Virtual Methods Object
  1.1186 +**
  1.1187 +** Every file opened by the [sqlite3_vfs.xOpen] method populates an
  1.1188 +** [sqlite3_file] object (or, more commonly, a subclass of the
  1.1189 +** [sqlite3_file] object) with a pointer to an instance of this object.
  1.1190 +** This object defines the methods used to perform various operations
  1.1191 +** against the open file represented by the [sqlite3_file] object.
  1.1192 +**
  1.1193 +** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element 
  1.1194 +** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
  1.1195 +** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
  1.1196 +** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
  1.1197 +** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
  1.1198 +** to NULL.
  1.1199 +**
  1.1200 +** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
  1.1201 +** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
  1.1202 +** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
  1.1203 +** flag may be ORed in to indicate that only the data of the file
  1.1204 +** and not its inode needs to be synced.
  1.1205 +**
  1.1206 +** The integer values to xLock() and xUnlock() are one of
  1.1207 +** <ul>
  1.1208 +** <li> [SQLITE_LOCK_NONE],
  1.1209 +** <li> [SQLITE_LOCK_SHARED],
  1.1210 +** <li> [SQLITE_LOCK_RESERVED],
  1.1211 +** <li> [SQLITE_LOCK_PENDING], or
  1.1212 +** <li> [SQLITE_LOCK_EXCLUSIVE].
  1.1213 +** </ul>
  1.1214 +** xLock() increases the lock. xUnlock() decreases the lock.
  1.1215 +** The xCheckReservedLock() method checks whether any database connection,
  1.1216 +** either in this process or in some other process, is holding a RESERVED,
  1.1217 +** PENDING, or EXCLUSIVE lock on the file.  It returns true
  1.1218 +** if such a lock exists and false otherwise.
  1.1219 +**
  1.1220 +** The xFileControl() method is a generic interface that allows custom
  1.1221 +** VFS implementations to directly control an open file using the
  1.1222 +** [sqlite3_file_control()] interface.  The second "op" argument is an
  1.1223 +** integer opcode.  The third argument is a generic pointer intended to
  1.1224 +** point to a structure that may contain arguments or space in which to
  1.1225 +** write return values.  Potential uses for xFileControl() might be
  1.1226 +** functions to enable blocking locks with timeouts, to change the
  1.1227 +** locking strategy (for example to use dot-file locks), to inquire
  1.1228 +** about the status of a lock, or to break stale locks.  The SQLite
  1.1229 +** core reserves all opcodes less than 100 for its own use.
  1.1230 +** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
  1.1231 +** Applications that define a custom xFileControl method should use opcodes
  1.1232 +** greater than 100 to avoid conflicts.  VFS implementations should
  1.1233 +** return [SQLITE_NOTFOUND] for file control opcodes that they do not
  1.1234 +** recognize.
  1.1235 +**
  1.1236 +** The xSectorSize() method returns the sector size of the
  1.1237 +** device that underlies the file.  The sector size is the
  1.1238 +** minimum write that can be performed without disturbing
  1.1239 +** other bytes in the file.  The xDeviceCharacteristics()
  1.1240 +** method returns a bit vector describing behaviors of the
  1.1241 +** underlying device:
  1.1242 +**
  1.1243 +** <ul>
  1.1244 +** <li> [SQLITE_IOCAP_ATOMIC]
  1.1245 +** <li> [SQLITE_IOCAP_ATOMIC512]
  1.1246 +** <li> [SQLITE_IOCAP_ATOMIC1K]
  1.1247 +** <li> [SQLITE_IOCAP_ATOMIC2K]
  1.1248 +** <li> [SQLITE_IOCAP_ATOMIC4K]
  1.1249 +** <li> [SQLITE_IOCAP_ATOMIC8K]
  1.1250 +** <li> [SQLITE_IOCAP_ATOMIC16K]
  1.1251 +** <li> [SQLITE_IOCAP_ATOMIC32K]
  1.1252 +** <li> [SQLITE_IOCAP_ATOMIC64K]
  1.1253 +** <li> [SQLITE_IOCAP_SAFE_APPEND]
  1.1254 +** <li> [SQLITE_IOCAP_SEQUENTIAL]
  1.1255 +** </ul>
  1.1256 +**
  1.1257 +** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1.1258 +** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
  1.1259 +** mean that writes of blocks that are nnn bytes in size and
  1.1260 +** are aligned to an address which is an integer multiple of
  1.1261 +** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
  1.1262 +** that when data is appended to a file, the data is appended
  1.1263 +** first then the size of the file is extended, never the other
  1.1264 +** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
  1.1265 +** information is written to disk in the same order as calls
  1.1266 +** to xWrite().
  1.1267 +**
  1.1268 +** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
  1.1269 +** in the unread portions of the buffer with zeros.  A VFS that
  1.1270 +** fails to zero-fill short reads might seem to work.  However,
  1.1271 +** failure to zero-fill short reads will eventually lead to
  1.1272 +** database corruption.
  1.1273 +*/
  1.1274 +typedef struct sqlite3_io_methods sqlite3_io_methods;
  1.1275 +struct sqlite3_io_methods {
  1.1276 +  int iVersion;
  1.1277 +  int (*xClose)(sqlite3_file*);
  1.1278 +  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  1.1279 +  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
  1.1280 +  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
  1.1281 +  int (*xSync)(sqlite3_file*, int flags);
  1.1282 +  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
  1.1283 +  int (*xLock)(sqlite3_file*, int);
  1.1284 +  int (*xUnlock)(sqlite3_file*, int);
  1.1285 +  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
  1.1286 +  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
  1.1287 +  int (*xSectorSize)(sqlite3_file*);
  1.1288 +  int (*xDeviceCharacteristics)(sqlite3_file*);
  1.1289 +  /* Methods above are valid for version 1 */
  1.1290 +  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
  1.1291 +  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
  1.1292 +  void (*xShmBarrier)(sqlite3_file*);
  1.1293 +  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
  1.1294 +  /* Methods above are valid for version 2 */
  1.1295 +  /* Additional methods may be added in future releases */
  1.1296 +};
  1.1297 +
  1.1298 +/*
  1.1299 +** CAPI3REF: Standard File Control Opcodes
  1.1300 +**
  1.1301 +** These integer constants are opcodes for the xFileControl method
  1.1302 +** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
  1.1303 +** interface.
  1.1304 +**
  1.1305 +** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
  1.1306 +** opcode causes the xFileControl method to write the current state of
  1.1307 +** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
  1.1308 +** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
  1.1309 +** into an integer that the pArg argument points to. This capability
  1.1310 +** is used during testing and only needs to be supported when SQLITE_TEST
  1.1311 +** is defined.
  1.1312 +** <ul>
  1.1313 +** <li>[[SQLITE_FCNTL_SIZE_HINT]]
  1.1314 +** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
  1.1315 +** layer a hint of how large the database file will grow to be during the
  1.1316 +** current transaction.  This hint is not guaranteed to be accurate but it
  1.1317 +** is often close.  The underlying VFS might choose to preallocate database
  1.1318 +** file space based on this hint in order to help writes to the database
  1.1319 +** file run faster.
  1.1320 +**
  1.1321 +** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
  1.1322 +** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
  1.1323 +** extends and truncates the database file in chunks of a size specified
  1.1324 +** by the user. The fourth argument to [sqlite3_file_control()] should 
  1.1325 +** point to an integer (type int) containing the new chunk-size to use
  1.1326 +** for the nominated database. Allocating database file space in large
  1.1327 +** chunks (say 1MB at a time), may reduce file-system fragmentation and
  1.1328 +** improve performance on some systems.
  1.1329 +**
  1.1330 +** <li>[[SQLITE_FCNTL_FILE_POINTER]]
  1.1331 +** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
  1.1332 +** to the [sqlite3_file] object associated with a particular database
  1.1333 +** connection.  See the [sqlite3_file_control()] documentation for
  1.1334 +** additional information.
  1.1335 +**
  1.1336 +** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
  1.1337 +** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
  1.1338 +** SQLite and sent to all VFSes in place of a call to the xSync method
  1.1339 +** when the database connection has [PRAGMA synchronous] set to OFF.)^
  1.1340 +** Some specialized VFSes need this signal in order to operate correctly
  1.1341 +** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
  1.1342 +** VFSes do not need this signal and should silently ignore this opcode.
  1.1343 +** Applications should not call [sqlite3_file_control()] with this
  1.1344 +** opcode as doing so may disrupt the operation of the specialized VFSes
  1.1345 +** that do require it.  
  1.1346 +**
  1.1347 +** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
  1.1348 +** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
  1.1349 +** retry counts and intervals for certain disk I/O operations for the
  1.1350 +** windows [VFS] in order to provide robustness in the presence of
  1.1351 +** anti-virus programs.  By default, the windows VFS will retry file read,
  1.1352 +** file write, and file delete operations up to 10 times, with a delay
  1.1353 +** of 25 milliseconds before the first retry and with the delay increasing
  1.1354 +** by an additional 25 milliseconds with each subsequent retry.  This
  1.1355 +** opcode allows these two values (10 retries and 25 milliseconds of delay)
  1.1356 +** to be adjusted.  The values are changed for all database connections
  1.1357 +** within the same process.  The argument is a pointer to an array of two
  1.1358 +** integers where the first integer i the new retry count and the second
  1.1359 +** integer is the delay.  If either integer is negative, then the setting
  1.1360 +** is not changed but instead the prior value of that setting is written
  1.1361 +** into the array entry, allowing the current retry settings to be
  1.1362 +** interrogated.  The zDbName parameter is ignored.
  1.1363 +**
  1.1364 +** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
  1.1365 +** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
  1.1366 +** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
  1.1367 +** write ahead log and shared memory files used for transaction control
  1.1368 +** are automatically deleted when the latest connection to the database
  1.1369 +** closes.  Setting persistent WAL mode causes those files to persist after
  1.1370 +** close.  Persisting the files is useful when other processes that do not
  1.1371 +** have write permission on the directory containing the database file want
  1.1372 +** to read the database file, as the WAL and shared memory files must exist
  1.1373 +** in order for the database to be readable.  The fourth parameter to
  1.1374 +** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
  1.1375 +** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
  1.1376 +** WAL mode.  If the integer is -1, then it is overwritten with the current
  1.1377 +** WAL persistence setting.
  1.1378 +**
  1.1379 +** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
  1.1380 +** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
  1.1381 +** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
  1.1382 +** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
  1.1383 +** xDeviceCharacteristics methods. The fourth parameter to
  1.1384 +** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
  1.1385 +** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
  1.1386 +** mode.  If the integer is -1, then it is overwritten with the current
  1.1387 +** zero-damage mode setting.
  1.1388 +**
  1.1389 +** <li>[[SQLITE_FCNTL_OVERWRITE]]
  1.1390 +** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
  1.1391 +** a write transaction to indicate that, unless it is rolled back for some
  1.1392 +** reason, the entire database file will be overwritten by the current 
  1.1393 +** transaction. This is used by VACUUM operations.
  1.1394 +**
  1.1395 +** <li>[[SQLITE_FCNTL_VFSNAME]]
  1.1396 +** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
  1.1397 +** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
  1.1398 +** final bottom-level VFS are written into memory obtained from 
  1.1399 +** [sqlite3_malloc()] and the result is stored in the char* variable
  1.1400 +** that the fourth parameter of [sqlite3_file_control()] points to.
  1.1401 +** The caller is responsible for freeing the memory when done.  As with
  1.1402 +** all file-control actions, there is no guarantee that this will actually
  1.1403 +** do anything.  Callers should initialize the char* variable to a NULL
  1.1404 +** pointer in case this file-control is not implemented.  This file-control
  1.1405 +** is intended for diagnostic use only.
  1.1406 +**
  1.1407 +** <li>[[SQLITE_FCNTL_PRAGMA]]
  1.1408 +** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] 
  1.1409 +** file control is sent to the open [sqlite3_file] object corresponding
  1.1410 +** to the database file to which the pragma statement refers. ^The argument
  1.1411 +** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
  1.1412 +** pointers to strings (char**) in which the second element of the array
  1.1413 +** is the name of the pragma and the third element is the argument to the
  1.1414 +** pragma or NULL if the pragma has no argument.  ^The handler for an
  1.1415 +** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
  1.1416 +** of the char** argument point to a string obtained from [sqlite3_mprintf()]
  1.1417 +** or the equivalent and that string will become the result of the pragma or
  1.1418 +** the error message if the pragma fails. ^If the
  1.1419 +** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal 
  1.1420 +** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
  1.1421 +** file control returns [SQLITE_OK], then the parser assumes that the
  1.1422 +** VFS has handled the PRAGMA itself and the parser generates a no-op
  1.1423 +** prepared statement.  ^If the [SQLITE_FCNTL_PRAGMA] file control returns
  1.1424 +** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
  1.1425 +** that the VFS encountered an error while handling the [PRAGMA] and the
  1.1426 +** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
  1.1427 +** file control occurs at the beginning of pragma statement analysis and so
  1.1428 +** it is able to override built-in [PRAGMA] statements.
  1.1429 +**
  1.1430 +** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
  1.1431 +** ^This file-control may be invoked by SQLite on the database file handle
  1.1432 +** shortly after it is opened in order to provide a custom VFS with access
  1.1433 +** to the connections busy-handler callback. The argument is of type (void **)
  1.1434 +** - an array of two (void *) values. The first (void *) actually points
  1.1435 +** to a function of type (int (*)(void *)). In order to invoke the connections
  1.1436 +** busy-handler, this function should be invoked with the second (void *) in
  1.1437 +** the array as the only argument. If it returns non-zero, then the operation
  1.1438 +** should be retried. If it returns zero, the custom VFS should abandon the
  1.1439 +** current operation.
  1.1440 +**
  1.1441 +** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
  1.1442 +** ^Application can invoke this file-control to have SQLite generate a
  1.1443 +** temporary filename using the same algorithm that is followed to generate
  1.1444 +** temporary filenames for TEMP tables and other internal uses.  The
  1.1445 +** argument should be a char** which will be filled with the filename
  1.1446 +** written into memory obtained from [sqlite3_malloc()].  The caller should
  1.1447 +** invoke [sqlite3_free()] on the result to avoid a memory leak.
  1.1448 +**
  1.1449 +** </ul>
  1.1450 +*/
  1.1451 +#define SQLITE_FCNTL_LOCKSTATE               1
  1.1452 +#define SQLITE_GET_LOCKPROXYFILE             2
  1.1453 +#define SQLITE_SET_LOCKPROXYFILE             3
  1.1454 +#define SQLITE_LAST_ERRNO                    4
  1.1455 +#define SQLITE_FCNTL_SIZE_HINT               5
  1.1456 +#define SQLITE_FCNTL_CHUNK_SIZE              6
  1.1457 +#define SQLITE_FCNTL_FILE_POINTER            7
  1.1458 +#define SQLITE_FCNTL_SYNC_OMITTED            8
  1.1459 +#define SQLITE_FCNTL_WIN32_AV_RETRY          9
  1.1460 +#define SQLITE_FCNTL_PERSIST_WAL            10
  1.1461 +#define SQLITE_FCNTL_OVERWRITE              11
  1.1462 +#define SQLITE_FCNTL_VFSNAME                12
  1.1463 +#define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
  1.1464 +#define SQLITE_FCNTL_PRAGMA                 14
  1.1465 +#define SQLITE_FCNTL_BUSYHANDLER            15
  1.1466 +#define SQLITE_FCNTL_TEMPFILENAME           16
  1.1467 +
  1.1468 +/*
  1.1469 +** CAPI3REF: Mutex Handle
  1.1470 +**
  1.1471 +** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1.1472 +** abstract type for a mutex object.  The SQLite core never looks
  1.1473 +** at the internal representation of an [sqlite3_mutex].  It only
  1.1474 +** deals with pointers to the [sqlite3_mutex] object.
  1.1475 +**
  1.1476 +** Mutexes are created using [sqlite3_mutex_alloc()].
  1.1477 +*/
  1.1478 +typedef struct sqlite3_mutex sqlite3_mutex;
  1.1479 +
  1.1480 +/*
  1.1481 +** CAPI3REF: OS Interface Object
  1.1482 +**
  1.1483 +** An instance of the sqlite3_vfs object defines the interface between
  1.1484 +** the SQLite core and the underlying operating system.  The "vfs"
  1.1485 +** in the name of the object stands for "virtual file system".  See
  1.1486 +** the [VFS | VFS documentation] for further information.
  1.1487 +**
  1.1488 +** The value of the iVersion field is initially 1 but may be larger in
  1.1489 +** future versions of SQLite.  Additional fields may be appended to this
  1.1490 +** object when the iVersion value is increased.  Note that the structure
  1.1491 +** of the sqlite3_vfs object changes in the transaction between
  1.1492 +** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
  1.1493 +** modified.
  1.1494 +**
  1.1495 +** The szOsFile field is the size of the subclassed [sqlite3_file]
  1.1496 +** structure used by this VFS.  mxPathname is the maximum length of
  1.1497 +** a pathname in this VFS.
  1.1498 +**
  1.1499 +** Registered sqlite3_vfs objects are kept on a linked list formed by
  1.1500 +** the pNext pointer.  The [sqlite3_vfs_register()]
  1.1501 +** and [sqlite3_vfs_unregister()] interfaces manage this list
  1.1502 +** in a thread-safe way.  The [sqlite3_vfs_find()] interface
  1.1503 +** searches the list.  Neither the application code nor the VFS
  1.1504 +** implementation should use the pNext pointer.
  1.1505 +**
  1.1506 +** The pNext field is the only field in the sqlite3_vfs
  1.1507 +** structure that SQLite will ever modify.  SQLite will only access
  1.1508 +** or modify this field while holding a particular static mutex.
  1.1509 +** The application should never modify anything within the sqlite3_vfs
  1.1510 +** object once the object has been registered.
  1.1511 +**
  1.1512 +** The zName field holds the name of the VFS module.  The name must
  1.1513 +** be unique across all VFS modules.
  1.1514 +**
  1.1515 +** [[sqlite3_vfs.xOpen]]
  1.1516 +** ^SQLite guarantees that the zFilename parameter to xOpen
  1.1517 +** is either a NULL pointer or string obtained
  1.1518 +** from xFullPathname() with an optional suffix added.
  1.1519 +** ^If a suffix is added to the zFilename parameter, it will
  1.1520 +** consist of a single "-" character followed by no more than
  1.1521 +** 11 alphanumeric and/or "-" characters.
  1.1522 +** ^SQLite further guarantees that
  1.1523 +** the string will be valid and unchanged until xClose() is
  1.1524 +** called. Because of the previous sentence,
  1.1525 +** the [sqlite3_file] can safely store a pointer to the
  1.1526 +** filename if it needs to remember the filename for some reason.
  1.1527 +** If the zFilename parameter to xOpen is a NULL pointer then xOpen
  1.1528 +** must invent its own temporary name for the file.  ^Whenever the 
  1.1529 +** xFilename parameter is NULL it will also be the case that the
  1.1530 +** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
  1.1531 +**
  1.1532 +** The flags argument to xOpen() includes all bits set in
  1.1533 +** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
  1.1534 +** or [sqlite3_open16()] is used, then flags includes at least
  1.1535 +** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. 
  1.1536 +** If xOpen() opens a file read-only then it sets *pOutFlags to
  1.1537 +** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
  1.1538 +**
  1.1539 +** ^(SQLite will also add one of the following flags to the xOpen()
  1.1540 +** call, depending on the object being opened:
  1.1541 +**
  1.1542 +** <ul>
  1.1543 +** <li>  [SQLITE_OPEN_MAIN_DB]
  1.1544 +** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
  1.1545 +** <li>  [SQLITE_OPEN_TEMP_DB]
  1.1546 +** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
  1.1547 +** <li>  [SQLITE_OPEN_TRANSIENT_DB]
  1.1548 +** <li>  [SQLITE_OPEN_SUBJOURNAL]
  1.1549 +** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
  1.1550 +** <li>  [SQLITE_OPEN_WAL]
  1.1551 +** </ul>)^
  1.1552 +**
  1.1553 +** The file I/O implementation can use the object type flags to
  1.1554 +** change the way it deals with files.  For example, an application
  1.1555 +** that does not care about crash recovery or rollback might make
  1.1556 +** the open of a journal file a no-op.  Writes to this journal would
  1.1557 +** also be no-ops, and any attempt to read the journal would return
  1.1558 +** SQLITE_IOERR.  Or the implementation might recognize that a database
  1.1559 +** file will be doing page-aligned sector reads and writes in a random
  1.1560 +** order and set up its I/O subsystem accordingly.
  1.1561 +**
  1.1562 +** SQLite might also add one of the following flags to the xOpen method:
  1.1563 +**
  1.1564 +** <ul>
  1.1565 +** <li> [SQLITE_OPEN_DELETEONCLOSE]
  1.1566 +** <li> [SQLITE_OPEN_EXCLUSIVE]
  1.1567 +** </ul>
  1.1568 +**
  1.1569 +** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
  1.1570 +** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
  1.1571 +** will be set for TEMP databases and their journals, transient
  1.1572 +** databases, and subjournals.
  1.1573 +**
  1.1574 +** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
  1.1575 +** with the [SQLITE_OPEN_CREATE] flag, which are both directly
  1.1576 +** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
  1.1577 +** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the 
  1.1578 +** SQLITE_OPEN_CREATE, is used to indicate that file should always
  1.1579 +** be created, and that it is an error if it already exists.
  1.1580 +** It is <i>not</i> used to indicate the file should be opened 
  1.1581 +** for exclusive access.
  1.1582 +**
  1.1583 +** ^At least szOsFile bytes of memory are allocated by SQLite
  1.1584 +** to hold the  [sqlite3_file] structure passed as the third
  1.1585 +** argument to xOpen.  The xOpen method does not have to
  1.1586 +** allocate the structure; it should just fill it in.  Note that
  1.1587 +** the xOpen method must set the sqlite3_file.pMethods to either
  1.1588 +** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
  1.1589 +** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
  1.1590 +** element will be valid after xOpen returns regardless of the success
  1.1591 +** or failure of the xOpen call.
  1.1592 +**
  1.1593 +** [[sqlite3_vfs.xAccess]]
  1.1594 +** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
  1.1595 +** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
  1.1596 +** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
  1.1597 +** to test whether a file is at least readable.   The file can be a
  1.1598 +** directory.
  1.1599 +**
  1.1600 +** ^SQLite will always allocate at least mxPathname+1 bytes for the
  1.1601 +** output buffer xFullPathname.  The exact size of the output buffer
  1.1602 +** is also passed as a parameter to both  methods. If the output buffer
  1.1603 +** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
  1.1604 +** handled as a fatal error by SQLite, vfs implementations should endeavor
  1.1605 +** to prevent this by setting mxPathname to a sufficiently large value.
  1.1606 +**
  1.1607 +** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
  1.1608 +** interfaces are not strictly a part of the filesystem, but they are
  1.1609 +** included in the VFS structure for completeness.
  1.1610 +** The xRandomness() function attempts to return nBytes bytes
  1.1611 +** of good-quality randomness into zOut.  The return value is
  1.1612 +** the actual number of bytes of randomness obtained.
  1.1613 +** The xSleep() method causes the calling thread to sleep for at
  1.1614 +** least the number of microseconds given.  ^The xCurrentTime()
  1.1615 +** method returns a Julian Day Number for the current date and time as
  1.1616 +** a floating point value.
  1.1617 +** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
  1.1618 +** Day Number multiplied by 86400000 (the number of milliseconds in 
  1.1619 +** a 24-hour day).  
  1.1620 +** ^SQLite will use the xCurrentTimeInt64() method to get the current
  1.1621 +** date and time if that method is available (if iVersion is 2 or 
  1.1622 +** greater and the function pointer is not NULL) and will fall back
  1.1623 +** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
  1.1624 +**
  1.1625 +** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
  1.1626 +** are not used by the SQLite core.  These optional interfaces are provided
  1.1627 +** by some VFSes to facilitate testing of the VFS code. By overriding 
  1.1628 +** system calls with functions under its control, a test program can
  1.1629 +** simulate faults and error conditions that would otherwise be difficult
  1.1630 +** or impossible to induce.  The set of system calls that can be overridden
  1.1631 +** varies from one VFS to another, and from one version of the same VFS to the
  1.1632 +** next.  Applications that use these interfaces must be prepared for any
  1.1633 +** or all of these interfaces to be NULL or for their behavior to change
  1.1634 +** from one release to the next.  Applications must not attempt to access
  1.1635 +** any of these methods if the iVersion of the VFS is less than 3.
  1.1636 +*/
  1.1637 +typedef struct sqlite3_vfs sqlite3_vfs;
  1.1638 +typedef void (*sqlite3_syscall_ptr)(void);
  1.1639 +struct sqlite3_vfs {
  1.1640 +  int iVersion;            /* Structure version number (currently 3) */
  1.1641 +  int szOsFile;            /* Size of subclassed sqlite3_file */
  1.1642 +  int mxPathname;          /* Maximum file pathname length */
  1.1643 +  sqlite3_vfs *pNext;      /* Next registered VFS */
  1.1644 +  const char *zName;       /* Name of this virtual file system */
  1.1645 +  void *pAppData;          /* Pointer to application-specific data */
  1.1646 +  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
  1.1647 +               int flags, int *pOutFlags);
  1.1648 +  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
  1.1649 +  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
  1.1650 +  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
  1.1651 +  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
  1.1652 +  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
  1.1653 +  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
  1.1654 +  void (*xDlClose)(sqlite3_vfs*, void*);
  1.1655 +  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
  1.1656 +  int (*xSleep)(sqlite3_vfs*, int microseconds);
  1.1657 +  int (*xCurrentTime)(sqlite3_vfs*, double*);
  1.1658 +  int (*xGetLastError)(sqlite3_vfs*, int, char *);
  1.1659 +  /*
  1.1660 +  ** The methods above are in version 1 of the sqlite_vfs object
  1.1661 +  ** definition.  Those that follow are added in version 2 or later
  1.1662 +  */
  1.1663 +  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
  1.1664 +  /*
  1.1665 +  ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
  1.1666 +  ** Those below are for version 3 and greater.
  1.1667 +  */
  1.1668 +  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
  1.1669 +  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
  1.1670 +  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
  1.1671 +  /*
  1.1672 +  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
  1.1673 +  ** New fields may be appended in figure versions.  The iVersion
  1.1674 +  ** value will increment whenever this happens. 
  1.1675 +  */
  1.1676 +};
  1.1677 +
  1.1678 +/*
  1.1679 +** CAPI3REF: Flags for the xAccess VFS method
  1.1680 +**
  1.1681 +** These integer constants can be used as the third parameter to
  1.1682 +** the xAccess method of an [sqlite3_vfs] object.  They determine
  1.1683 +** what kind of permissions the xAccess method is looking for.
  1.1684 +** With SQLITE_ACCESS_EXISTS, the xAccess method
  1.1685 +** simply checks whether the file exists.
  1.1686 +** With SQLITE_ACCESS_READWRITE, the xAccess method
  1.1687 +** checks whether the named directory is both readable and writable
  1.1688 +** (in other words, if files can be added, removed, and renamed within
  1.1689 +** the directory).
  1.1690 +** The SQLITE_ACCESS_READWRITE constant is currently used only by the
  1.1691 +** [temp_store_directory pragma], though this could change in a future
  1.1692 +** release of SQLite.
  1.1693 +** With SQLITE_ACCESS_READ, the xAccess method
  1.1694 +** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
  1.1695 +** currently unused, though it might be used in a future release of
  1.1696 +** SQLite.
  1.1697 +*/
  1.1698 +#define SQLITE_ACCESS_EXISTS    0
  1.1699 +#define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
  1.1700 +#define SQLITE_ACCESS_READ      2   /* Unused */
  1.1701 +
  1.1702 +/*
  1.1703 +** CAPI3REF: Flags for the xShmLock VFS method
  1.1704 +**
  1.1705 +** These integer constants define the various locking operations
  1.1706 +** allowed by the xShmLock method of [sqlite3_io_methods].  The
  1.1707 +** following are the only legal combinations of flags to the
  1.1708 +** xShmLock method:
  1.1709 +**
  1.1710 +** <ul>
  1.1711 +** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
  1.1712 +** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
  1.1713 +** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
  1.1714 +** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
  1.1715 +** </ul>
  1.1716 +**
  1.1717 +** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
  1.1718 +** was given no the corresponding lock.  
  1.1719 +**
  1.1720 +** The xShmLock method can transition between unlocked and SHARED or
  1.1721 +** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
  1.1722 +** and EXCLUSIVE.
  1.1723 +*/
  1.1724 +#define SQLITE_SHM_UNLOCK       1
  1.1725 +#define SQLITE_SHM_LOCK         2
  1.1726 +#define SQLITE_SHM_SHARED       4
  1.1727 +#define SQLITE_SHM_EXCLUSIVE    8
  1.1728 +
  1.1729 +/*
  1.1730 +** CAPI3REF: Maximum xShmLock index
  1.1731 +**
  1.1732 +** The xShmLock method on [sqlite3_io_methods] may use values
  1.1733 +** between 0 and this upper bound as its "offset" argument.
  1.1734 +** The SQLite core will never attempt to acquire or release a
  1.1735 +** lock outside of this range
  1.1736 +*/
  1.1737 +#define SQLITE_SHM_NLOCK        8
  1.1738 +
  1.1739 +
  1.1740 +/*
  1.1741 +** CAPI3REF: Initialize The SQLite Library
  1.1742 +**
  1.1743 +** ^The sqlite3_initialize() routine initializes the
  1.1744 +** SQLite library.  ^The sqlite3_shutdown() routine
  1.1745 +** deallocates any resources that were allocated by sqlite3_initialize().
  1.1746 +** These routines are designed to aid in process initialization and
  1.1747 +** shutdown on embedded systems.  Workstation applications using
  1.1748 +** SQLite normally do not need to invoke either of these routines.
  1.1749 +**
  1.1750 +** A call to sqlite3_initialize() is an "effective" call if it is
  1.1751 +** the first time sqlite3_initialize() is invoked during the lifetime of
  1.1752 +** the process, or if it is the first time sqlite3_initialize() is invoked
  1.1753 +** following a call to sqlite3_shutdown().  ^(Only an effective call
  1.1754 +** of sqlite3_initialize() does any initialization.  All other calls
  1.1755 +** are harmless no-ops.)^
  1.1756 +**
  1.1757 +** A call to sqlite3_shutdown() is an "effective" call if it is the first
  1.1758 +** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
  1.1759 +** an effective call to sqlite3_shutdown() does any deinitialization.
  1.1760 +** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
  1.1761 +**
  1.1762 +** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
  1.1763 +** is not.  The sqlite3_shutdown() interface must only be called from a
  1.1764 +** single thread.  All open [database connections] must be closed and all
  1.1765 +** other SQLite resources must be deallocated prior to invoking
  1.1766 +** sqlite3_shutdown().
  1.1767 +**
  1.1768 +** Among other things, ^sqlite3_initialize() will invoke
  1.1769 +** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
  1.1770 +** will invoke sqlite3_os_end().
  1.1771 +**
  1.1772 +** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
  1.1773 +** ^If for some reason, sqlite3_initialize() is unable to initialize
  1.1774 +** the library (perhaps it is unable to allocate a needed resource such
  1.1775 +** as a mutex) it returns an [error code] other than [SQLITE_OK].
  1.1776 +**
  1.1777 +** ^The sqlite3_initialize() routine is called internally by many other
  1.1778 +** SQLite interfaces so that an application usually does not need to
  1.1779 +** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
  1.1780 +** calls sqlite3_initialize() so the SQLite library will be automatically
  1.1781 +** initialized when [sqlite3_open()] is called if it has not be initialized
  1.1782 +** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
  1.1783 +** compile-time option, then the automatic calls to sqlite3_initialize()
  1.1784 +** are omitted and the application must call sqlite3_initialize() directly
  1.1785 +** prior to using any other SQLite interface.  For maximum portability,
  1.1786 +** it is recommended that applications always invoke sqlite3_initialize()
  1.1787 +** directly prior to using any other SQLite interface.  Future releases
  1.1788 +** of SQLite may require this.  In other words, the behavior exhibited
  1.1789 +** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
  1.1790 +** default behavior in some future release of SQLite.
  1.1791 +**
  1.1792 +** The sqlite3_os_init() routine does operating-system specific
  1.1793 +** initialization of the SQLite library.  The sqlite3_os_end()
  1.1794 +** routine undoes the effect of sqlite3_os_init().  Typical tasks
  1.1795 +** performed by these routines include allocation or deallocation
  1.1796 +** of static resources, initialization of global variables,
  1.1797 +** setting up a default [sqlite3_vfs] module, or setting up
  1.1798 +** a default configuration using [sqlite3_config()].
  1.1799 +**
  1.1800 +** The application should never invoke either sqlite3_os_init()
  1.1801 +** or sqlite3_os_end() directly.  The application should only invoke
  1.1802 +** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
  1.1803 +** interface is called automatically by sqlite3_initialize() and
  1.1804 +** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
  1.1805 +** implementations for sqlite3_os_init() and sqlite3_os_end()
  1.1806 +** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
  1.1807 +** When [custom builds | built for other platforms]
  1.1808 +** (using the [SQLITE_OS_OTHER=1] compile-time
  1.1809 +** option) the application must supply a suitable implementation for
  1.1810 +** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
  1.1811 +** implementation of sqlite3_os_init() or sqlite3_os_end()
  1.1812 +** must return [SQLITE_OK] on success and some other [error code] upon
  1.1813 +** failure.
  1.1814 +*/
  1.1815 +SQLITE_API int sqlite3_initialize(void);
  1.1816 +SQLITE_API int sqlite3_shutdown(void);
  1.1817 +SQLITE_API int sqlite3_os_init(void);
  1.1818 +SQLITE_API int sqlite3_os_end(void);
  1.1819 +
  1.1820 +/*
  1.1821 +** CAPI3REF: Configuring The SQLite Library
  1.1822 +**
  1.1823 +** The sqlite3_config() interface is used to make global configuration
  1.1824 +** changes to SQLite in order to tune SQLite to the specific needs of
  1.1825 +** the application.  The default configuration is recommended for most
  1.1826 +** applications and so this routine is usually not necessary.  It is
  1.1827 +** provided to support rare applications with unusual needs.
  1.1828 +**
  1.1829 +** The sqlite3_config() interface is not threadsafe.  The application
  1.1830 +** must insure that no other SQLite interfaces are invoked by other
  1.1831 +** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
  1.1832 +** may only be invoked prior to library initialization using
  1.1833 +** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
  1.1834 +** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
  1.1835 +** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
  1.1836 +** Note, however, that ^sqlite3_config() can be called as part of the
  1.1837 +** implementation of an application-defined [sqlite3_os_init()].
  1.1838 +**
  1.1839 +** The first argument to sqlite3_config() is an integer
  1.1840 +** [configuration option] that determines
  1.1841 +** what property of SQLite is to be configured.  Subsequent arguments
  1.1842 +** vary depending on the [configuration option]
  1.1843 +** in the first argument.
  1.1844 +**
  1.1845 +** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
  1.1846 +** ^If the option is unknown or SQLite is unable to set the option
  1.1847 +** then this routine returns a non-zero [error code].
  1.1848 +*/
  1.1849 +SQLITE_API int sqlite3_config(int, ...);
  1.1850 +
  1.1851 +/*
  1.1852 +** CAPI3REF: Configure database connections
  1.1853 +**
  1.1854 +** The sqlite3_db_config() interface is used to make configuration
  1.1855 +** changes to a [database connection].  The interface is similar to
  1.1856 +** [sqlite3_config()] except that the changes apply to a single
  1.1857 +** [database connection] (specified in the first argument).
  1.1858 +**
  1.1859 +** The second argument to sqlite3_db_config(D,V,...)  is the
  1.1860 +** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code 
  1.1861 +** that indicates what aspect of the [database connection] is being configured.
  1.1862 +** Subsequent arguments vary depending on the configuration verb.
  1.1863 +**
  1.1864 +** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
  1.1865 +** the call is considered successful.
  1.1866 +*/
  1.1867 +SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
  1.1868 +
  1.1869 +/*
  1.1870 +** CAPI3REF: Memory Allocation Routines
  1.1871 +**
  1.1872 +** An instance of this object defines the interface between SQLite
  1.1873 +** and low-level memory allocation routines.
  1.1874 +**
  1.1875 +** This object is used in only one place in the SQLite interface.
  1.1876 +** A pointer to an instance of this object is the argument to
  1.1877 +** [sqlite3_config()] when the configuration option is
  1.1878 +** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].  
  1.1879 +** By creating an instance of this object
  1.1880 +** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
  1.1881 +** during configuration, an application can specify an alternative
  1.1882 +** memory allocation subsystem for SQLite to use for all of its
  1.1883 +** dynamic memory needs.
  1.1884 +**
  1.1885 +** Note that SQLite comes with several [built-in memory allocators]
  1.1886 +** that are perfectly adequate for the overwhelming majority of applications
  1.1887 +** and that this object is only useful to a tiny minority of applications
  1.1888 +** with specialized memory allocation requirements.  This object is
  1.1889 +** also used during testing of SQLite in order to specify an alternative
  1.1890 +** memory allocator that simulates memory out-of-memory conditions in
  1.1891 +** order to verify that SQLite recovers gracefully from such
  1.1892 +** conditions.
  1.1893 +**
  1.1894 +** The xMalloc, xRealloc, and xFree methods must work like the
  1.1895 +** malloc(), realloc() and free() functions from the standard C library.
  1.1896 +** ^SQLite guarantees that the second argument to
  1.1897 +** xRealloc is always a value returned by a prior call to xRoundup.
  1.1898 +**
  1.1899 +** xSize should return the allocated size of a memory allocation
  1.1900 +** previously obtained from xMalloc or xRealloc.  The allocated size
  1.1901 +** is always at least as big as the requested size but may be larger.
  1.1902 +**
  1.1903 +** The xRoundup method returns what would be the allocated size of
  1.1904 +** a memory allocation given a particular requested size.  Most memory
  1.1905 +** allocators round up memory allocations at least to the next multiple
  1.1906 +** of 8.  Some allocators round up to a larger multiple or to a power of 2.
  1.1907 +** Every memory allocation request coming in through [sqlite3_malloc()]
  1.1908 +** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0, 
  1.1909 +** that causes the corresponding memory allocation to fail.
  1.1910 +**
  1.1911 +** The xInit method initializes the memory allocator.  (For example,
  1.1912 +** it might allocate any require mutexes or initialize internal data
  1.1913 +** structures.  The xShutdown method is invoked (indirectly) by
  1.1914 +** [sqlite3_shutdown()] and should deallocate any resources acquired
  1.1915 +** by xInit.  The pAppData pointer is used as the only parameter to
  1.1916 +** xInit and xShutdown.
  1.1917 +**
  1.1918 +** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
  1.1919 +** the xInit method, so the xInit method need not be threadsafe.  The
  1.1920 +** xShutdown method is only called from [sqlite3_shutdown()] so it does
  1.1921 +** not need to be threadsafe either.  For all other methods, SQLite
  1.1922 +** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
  1.1923 +** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
  1.1924 +** it is by default) and so the methods are automatically serialized.
  1.1925 +** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
  1.1926 +** methods must be threadsafe or else make their own arrangements for
  1.1927 +** serialization.
  1.1928 +**
  1.1929 +** SQLite will never invoke xInit() more than once without an intervening
  1.1930 +** call to xShutdown().
  1.1931 +*/
  1.1932 +typedef struct sqlite3_mem_methods sqlite3_mem_methods;
  1.1933 +struct sqlite3_mem_methods {
  1.1934 +  void *(*xMalloc)(int);         /* Memory allocation function */
  1.1935 +  void (*xFree)(void*);          /* Free a prior allocation */
  1.1936 +  void *(*xRealloc)(void*,int);  /* Resize an allocation */
  1.1937 +  int (*xSize)(void*);           /* Return the size of an allocation */
  1.1938 +  int (*xRoundup)(int);          /* Round up request size to allocation size */
  1.1939 +  int (*xInit)(void*);           /* Initialize the memory allocator */
  1.1940 +  void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
  1.1941 +  void *pAppData;                /* Argument to xInit() and xShutdown() */
  1.1942 +};
  1.1943 +
  1.1944 +/*
  1.1945 +** CAPI3REF: Configuration Options
  1.1946 +** KEYWORDS: {configuration option}
  1.1947 +**
  1.1948 +** These constants are the available integer configuration options that
  1.1949 +** can be passed as the first argument to the [sqlite3_config()] interface.
  1.1950 +**
  1.1951 +** New configuration options may be added in future releases of SQLite.
  1.1952 +** Existing configuration options might be discontinued.  Applications
  1.1953 +** should check the return code from [sqlite3_config()] to make sure that
  1.1954 +** the call worked.  The [sqlite3_config()] interface will return a
  1.1955 +** non-zero [error code] if a discontinued or unsupported configuration option
  1.1956 +** is invoked.
  1.1957 +**
  1.1958 +** <dl>
  1.1959 +** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
  1.1960 +** <dd>There are no arguments to this option.  ^This option sets the
  1.1961 +** [threading mode] to Single-thread.  In other words, it disables
  1.1962 +** all mutexing and puts SQLite into a mode where it can only be used
  1.1963 +** by a single thread.   ^If SQLite is compiled with
  1.1964 +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1.1965 +** it is not possible to change the [threading mode] from its default
  1.1966 +** value of Single-thread and so [sqlite3_config()] will return 
  1.1967 +** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
  1.1968 +** configuration option.</dd>
  1.1969 +**
  1.1970 +** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
  1.1971 +** <dd>There are no arguments to this option.  ^This option sets the
  1.1972 +** [threading mode] to Multi-thread.  In other words, it disables
  1.1973 +** mutexing on [database connection] and [prepared statement] objects.
  1.1974 +** The application is responsible for serializing access to
  1.1975 +** [database connections] and [prepared statements].  But other mutexes
  1.1976 +** are enabled so that SQLite will be safe to use in a multi-threaded
  1.1977 +** environment as long as no two threads attempt to use the same
  1.1978 +** [database connection] at the same time.  ^If SQLite is compiled with
  1.1979 +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1.1980 +** it is not possible to set the Multi-thread [threading mode] and
  1.1981 +** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1.1982 +** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
  1.1983 +**
  1.1984 +** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
  1.1985 +** <dd>There are no arguments to this option.  ^This option sets the
  1.1986 +** [threading mode] to Serialized. In other words, this option enables
  1.1987 +** all mutexes including the recursive
  1.1988 +** mutexes on [database connection] and [prepared statement] objects.
  1.1989 +** In this mode (which is the default when SQLite is compiled with
  1.1990 +** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
  1.1991 +** to [database connections] and [prepared statements] so that the
  1.1992 +** application is free to use the same [database connection] or the
  1.1993 +** same [prepared statement] in different threads at the same time.
  1.1994 +** ^If SQLite is compiled with
  1.1995 +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1.1996 +** it is not possible to set the Serialized [threading mode] and
  1.1997 +** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1.1998 +** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
  1.1999 +**
  1.2000 +** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
  1.2001 +** <dd> ^(This option takes a single argument which is a pointer to an
  1.2002 +** instance of the [sqlite3_mem_methods] structure.  The argument specifies
  1.2003 +** alternative low-level memory allocation routines to be used in place of
  1.2004 +** the memory allocation routines built into SQLite.)^ ^SQLite makes
  1.2005 +** its own private copy of the content of the [sqlite3_mem_methods] structure
  1.2006 +** before the [sqlite3_config()] call returns.</dd>
  1.2007 +**
  1.2008 +** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
  1.2009 +** <dd> ^(This option takes a single argument which is a pointer to an
  1.2010 +** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
  1.2011 +** structure is filled with the currently defined memory allocation routines.)^
  1.2012 +** This option can be used to overload the default memory allocation
  1.2013 +** routines with a wrapper that simulations memory allocation failure or
  1.2014 +** tracks memory usage, for example. </dd>
  1.2015 +**
  1.2016 +** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  1.2017 +** <dd> ^This option takes single argument of type int, interpreted as a 
  1.2018 +** boolean, which enables or disables the collection of memory allocation 
  1.2019 +** statistics. ^(When memory allocation statistics are disabled, the 
  1.2020 +** following SQLite interfaces become non-operational:
  1.2021 +**   <ul>
  1.2022 +**   <li> [sqlite3_memory_used()]
  1.2023 +**   <li> [sqlite3_memory_highwater()]
  1.2024 +**   <li> [sqlite3_soft_heap_limit64()]
  1.2025 +**   <li> [sqlite3_status()]
  1.2026 +**   </ul>)^
  1.2027 +** ^Memory allocation statistics are enabled by default unless SQLite is
  1.2028 +** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  1.2029 +** allocation statistics are disabled by default.
  1.2030 +** </dd>
  1.2031 +**
  1.2032 +** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
  1.2033 +** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1.2034 +** scratch memory.  There are three arguments:  A pointer an 8-byte
  1.2035 +** aligned memory buffer from which the scratch allocations will be
  1.2036 +** drawn, the size of each scratch allocation (sz),
  1.2037 +** and the maximum number of scratch allocations (N).  The sz
  1.2038 +** argument must be a multiple of 16.
  1.2039 +** The first argument must be a pointer to an 8-byte aligned buffer
  1.2040 +** of at least sz*N bytes of memory.
  1.2041 +** ^SQLite will use no more than two scratch buffers per thread.  So
  1.2042 +** N should be set to twice the expected maximum number of threads.
  1.2043 +** ^SQLite will never require a scratch buffer that is more than 6
  1.2044 +** times the database page size. ^If SQLite needs needs additional
  1.2045 +** scratch memory beyond what is provided by this configuration option, then 
  1.2046 +** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
  1.2047 +**
  1.2048 +** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
  1.2049 +** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1.2050 +** the database page cache with the default page cache implementation.  
  1.2051 +** This configuration should not be used if an application-define page
  1.2052 +** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
  1.2053 +** There are three arguments to this option: A pointer to 8-byte aligned
  1.2054 +** memory, the size of each page buffer (sz), and the number of pages (N).
  1.2055 +** The sz argument should be the size of the largest database page
  1.2056 +** (a power of two between 512 and 32768) plus a little extra for each
  1.2057 +** page header.  ^The page header size is 20 to 40 bytes depending on
  1.2058 +** the host architecture.  ^It is harmless, apart from the wasted memory,
  1.2059 +** to make sz a little too large.  The first
  1.2060 +** argument should point to an allocation of at least sz*N bytes of memory.
  1.2061 +** ^SQLite will use the memory provided by the first argument to satisfy its
  1.2062 +** memory needs for the first N pages that it adds to cache.  ^If additional
  1.2063 +** page cache memory is needed beyond what is provided by this option, then
  1.2064 +** SQLite goes to [sqlite3_malloc()] for the additional storage space.
  1.2065 +** The pointer in the first argument must
  1.2066 +** be aligned to an 8-byte boundary or subsequent behavior of SQLite
  1.2067 +** will be undefined.</dd>
  1.2068 +**
  1.2069 +** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
  1.2070 +** <dd> ^This option specifies a static memory buffer that SQLite will use
  1.2071 +** for all of its dynamic memory allocation needs beyond those provided
  1.2072 +** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
  1.2073 +** There are three arguments: An 8-byte aligned pointer to the memory,
  1.2074 +** the number of bytes in the memory buffer, and the minimum allocation size.
  1.2075 +** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
  1.2076 +** to using its default memory allocator (the system malloc() implementation),
  1.2077 +** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
  1.2078 +** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1.2079 +** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1.2080 +** allocator is engaged to handle all of SQLites memory allocation needs.
  1.2081 +** The first pointer (the memory pointer) must be aligned to an 8-byte
  1.2082 +** boundary or subsequent behavior of SQLite will be undefined.
  1.2083 +** The minimum allocation size is capped at 2**12. Reasonable values
  1.2084 +** for the minimum allocation size are 2**5 through 2**8.</dd>
  1.2085 +**
  1.2086 +** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
  1.2087 +** <dd> ^(This option takes a single argument which is a pointer to an
  1.2088 +** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
  1.2089 +** alternative low-level mutex routines to be used in place
  1.2090 +** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  1.2091 +** content of the [sqlite3_mutex_methods] structure before the call to
  1.2092 +** [sqlite3_config()] returns. ^If SQLite is compiled with
  1.2093 +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1.2094 +** the entire mutexing subsystem is omitted from the build and hence calls to
  1.2095 +** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
  1.2096 +** return [SQLITE_ERROR].</dd>
  1.2097 +**
  1.2098 +** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
  1.2099 +** <dd> ^(This option takes a single argument which is a pointer to an
  1.2100 +** instance of the [sqlite3_mutex_methods] structure.  The
  1.2101 +** [sqlite3_mutex_methods]
  1.2102 +** structure is filled with the currently defined mutex routines.)^
  1.2103 +** This option can be used to overload the default mutex allocation
  1.2104 +** routines with a wrapper used to track mutex usage for performance
  1.2105 +** profiling or testing, for example.   ^If SQLite is compiled with
  1.2106 +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1.2107 +** the entire mutexing subsystem is omitted from the build and hence calls to
  1.2108 +** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
  1.2109 +** return [SQLITE_ERROR].</dd>
  1.2110 +**
  1.2111 +** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
  1.2112 +** <dd> ^(This option takes two arguments that determine the default
  1.2113 +** memory allocation for the lookaside memory allocator on each
  1.2114 +** [database connection].  The first argument is the
  1.2115 +** size of each lookaside buffer slot and the second is the number of
  1.2116 +** slots allocated to each database connection.)^  ^(This option sets the
  1.2117 +** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
  1.2118 +** verb to [sqlite3_db_config()] can be used to change the lookaside
  1.2119 +** configuration on individual connections.)^ </dd>
  1.2120 +**
  1.2121 +** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
  1.2122 +** <dd> ^(This option takes a single argument which is a pointer to
  1.2123 +** an [sqlite3_pcache_methods2] object.  This object specifies the interface
  1.2124 +** to a custom page cache implementation.)^  ^SQLite makes a copy of the
  1.2125 +** object and uses it for page cache memory allocations.</dd>
  1.2126 +**
  1.2127 +** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
  1.2128 +** <dd> ^(This option takes a single argument which is a pointer to an
  1.2129 +** [sqlite3_pcache_methods2] object.  SQLite copies of the current
  1.2130 +** page cache implementation into that object.)^ </dd>
  1.2131 +**
  1.2132 +** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
  1.2133 +** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
  1.2134 +** function with a call signature of void(*)(void*,int,const char*), 
  1.2135 +** and a pointer to void. ^If the function pointer is not NULL, it is
  1.2136 +** invoked by [sqlite3_log()] to process each logging event.  ^If the
  1.2137 +** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
  1.2138 +** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
  1.2139 +** passed through as the first parameter to the application-defined logger
  1.2140 +** function whenever that function is invoked.  ^The second parameter to
  1.2141 +** the logger function is a copy of the first parameter to the corresponding
  1.2142 +** [sqlite3_log()] call and is intended to be a [result code] or an
  1.2143 +** [extended result code].  ^The third parameter passed to the logger is
  1.2144 +** log message after formatting via [sqlite3_snprintf()].
  1.2145 +** The SQLite logging interface is not reentrant; the logger function
  1.2146 +** supplied by the application must not invoke any SQLite interface.
  1.2147 +** In a multi-threaded application, the application-defined logger
  1.2148 +** function must be threadsafe. </dd>
  1.2149 +**
  1.2150 +** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
  1.2151 +** <dd> This option takes a single argument of type int. If non-zero, then
  1.2152 +** URI handling is globally enabled. If the parameter is zero, then URI handling
  1.2153 +** is globally disabled. If URI handling is globally enabled, all filenames
  1.2154 +** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
  1.2155 +** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  1.2156 +** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  1.2157 +** connection is opened. If it is globally disabled, filenames are
  1.2158 +** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1.2159 +** database connection is opened. By default, URI handling is globally
  1.2160 +** disabled. The default value may be changed by compiling with the
  1.2161 +** [SQLITE_USE_URI] symbol defined.
  1.2162 +**
  1.2163 +** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
  1.2164 +** <dd> This option takes a single integer argument which is interpreted as
  1.2165 +** a boolean in order to enable or disable the use of covering indices for
  1.2166 +** full table scans in the query optimizer.  The default setting is determined
  1.2167 +** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
  1.2168 +** if that compile-time option is omitted.
  1.2169 +** The ability to disable the use of covering indices for full table scans
  1.2170 +** is because some incorrectly coded legacy applications might malfunction
  1.2171 +** malfunction when the optimization is enabled.  Providing the ability to
  1.2172 +** disable the optimization allows the older, buggy application code to work
  1.2173 +** without change even with newer versions of SQLite.
  1.2174 +**
  1.2175 +** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  1.2176 +** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
  1.2177 +** <dd> These options are obsolete and should not be used by new code.
  1.2178 +** They are retained for backwards compatibility but are now no-ops.
  1.2179 +** </dl>
  1.2180 +**
  1.2181 +** [[SQLITE_CONFIG_SQLLOG]]
  1.2182 +** <dt>SQLITE_CONFIG_SQLLOG
  1.2183 +** <dd>This option is only available if sqlite is compiled with the
  1.2184 +** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
  1.2185 +** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
  1.2186 +** The second should be of type (void*). The callback is invoked by the library
  1.2187 +** in three separate circumstances, identified by the value passed as the
  1.2188 +** fourth parameter. If the fourth parameter is 0, then the database connection
  1.2189 +** passed as the second argument has just been opened. The third argument
  1.2190 +** points to a buffer containing the name of the main database file. If the
  1.2191 +** fourth parameter is 1, then the SQL statement that the third parameter
  1.2192 +** points to has just been executed. Or, if the fourth parameter is 2, then
  1.2193 +** the connection being passed as the second parameter is being closed. The
  1.2194 +** third parameter is passed NULL In this case.
  1.2195 +** </dl>
  1.2196 +*/
  1.2197 +#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  1.2198 +#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  1.2199 +#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  1.2200 +#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  1.2201 +#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
  1.2202 +#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
  1.2203 +#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
  1.2204 +#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
  1.2205 +#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
  1.2206 +#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
  1.2207 +#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
  1.2208 +/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
  1.2209 +#define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  1.2210 +#define SQLITE_CONFIG_PCACHE       14  /* no-op */
  1.2211 +#define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  1.2212 +#define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1.2213 +#define SQLITE_CONFIG_URI          17  /* int */
  1.2214 +#define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1.2215 +#define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
  1.2216 +#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
  1.2217 +#define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
  1.2218 +
  1.2219 +/*
  1.2220 +** CAPI3REF: Database Connection Configuration Options
  1.2221 +**
  1.2222 +** These constants are the available integer configuration options that
  1.2223 +** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1.2224 +**
  1.2225 +** New configuration options may be added in future releases of SQLite.
  1.2226 +** Existing configuration options might be discontinued.  Applications
  1.2227 +** should check the return code from [sqlite3_db_config()] to make sure that
  1.2228 +** the call worked.  ^The [sqlite3_db_config()] interface will return a
  1.2229 +** non-zero [error code] if a discontinued or unsupported configuration option
  1.2230 +** is invoked.
  1.2231 +**
  1.2232 +** <dl>
  1.2233 +** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  1.2234 +** <dd> ^This option takes three additional arguments that determine the 
  1.2235 +** [lookaside memory allocator] configuration for the [database connection].
  1.2236 +** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  1.2237 +** pointer to a memory buffer to use for lookaside memory.
  1.2238 +** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  1.2239 +** may be NULL in which case SQLite will allocate the
  1.2240 +** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
  1.2241 +** size of each lookaside buffer slot.  ^The third argument is the number of
  1.2242 +** slots.  The size of the buffer in the first argument must be greater than
  1.2243 +** or equal to the product of the second and third arguments.  The buffer
  1.2244 +** must be aligned to an 8-byte boundary.  ^If the second argument to
  1.2245 +** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
  1.2246 +** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
  1.2247 +** configuration for a database connection can only be changed when that
  1.2248 +** connection is not currently using lookaside memory, or in other words
  1.2249 +** when the "current value" returned by
  1.2250 +** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  1.2251 +** Any attempt to change the lookaside memory configuration when lookaside
  1.2252 +** memory is in use leaves the configuration unchanged and returns 
  1.2253 +** [SQLITE_BUSY].)^</dd>
  1.2254 +**
  1.2255 +** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  1.2256 +** <dd> ^This option is used to enable or disable the enforcement of
  1.2257 +** [foreign key constraints].  There should be two additional arguments.
  1.2258 +** The first argument is an integer which is 0 to disable FK enforcement,
  1.2259 +** positive to enable FK enforcement or negative to leave FK enforcement
  1.2260 +** unchanged.  The second parameter is a pointer to an integer into which
  1.2261 +** is written 0 or 1 to indicate whether FK enforcement is off or on
  1.2262 +** following this call.  The second parameter may be a NULL pointer, in
  1.2263 +** which case the FK enforcement setting is not reported back. </dd>
  1.2264 +**
  1.2265 +** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  1.2266 +** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  1.2267 +** There should be two additional arguments.
  1.2268 +** The first argument is an integer which is 0 to disable triggers,
  1.2269 +** positive to enable triggers or negative to leave the setting unchanged.
  1.2270 +** The second parameter is a pointer to an integer into which
  1.2271 +** is written 0 or 1 to indicate whether triggers are disabled or enabled
  1.2272 +** following this call.  The second parameter may be a NULL pointer, in
  1.2273 +** which case the trigger setting is not reported back. </dd>
  1.2274 +**
  1.2275 +** </dl>
  1.2276 +*/
  1.2277 +#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
  1.2278 +#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
  1.2279 +#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
  1.2280 +
  1.2281 +
  1.2282 +/*
  1.2283 +** CAPI3REF: Enable Or Disable Extended Result Codes
  1.2284 +**
  1.2285 +** ^The sqlite3_extended_result_codes() routine enables or disables the
  1.2286 +** [extended result codes] feature of SQLite. ^The extended result
  1.2287 +** codes are disabled by default for historical compatibility.
  1.2288 +*/
  1.2289 +SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
  1.2290 +
  1.2291 +/*
  1.2292 +** CAPI3REF: Last Insert Rowid
  1.2293 +**
  1.2294 +** ^Each entry in an SQLite table has a unique 64-bit signed
  1.2295 +** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  1.2296 +** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  1.2297 +** names are not also used by explicitly declared columns. ^If
  1.2298 +** the table has a column of type [INTEGER PRIMARY KEY] then that column
  1.2299 +** is another alias for the rowid.
  1.2300 +**
  1.2301 +** ^This routine returns the [rowid] of the most recent
  1.2302 +** successful [INSERT] into the database from the [database connection]
  1.2303 +** in the first argument.  ^As of SQLite version 3.7.7, this routines
  1.2304 +** records the last insert rowid of both ordinary tables and [virtual tables].
  1.2305 +** ^If no successful [INSERT]s
  1.2306 +** have ever occurred on that database connection, zero is returned.
  1.2307 +**
  1.2308 +** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
  1.2309 +** method, then this routine will return the [rowid] of the inserted
  1.2310 +** row as long as the trigger or virtual table method is running.
  1.2311 +** But once the trigger or virtual table method ends, the value returned 
  1.2312 +** by this routine reverts to what it was before the trigger or virtual
  1.2313 +** table method began.)^
  1.2314 +**
  1.2315 +** ^An [INSERT] that fails due to a constraint violation is not a
  1.2316 +** successful [INSERT] and does not change the value returned by this
  1.2317 +** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
  1.2318 +** and INSERT OR ABORT make no changes to the return value of this
  1.2319 +** routine when their insertion fails.  ^(When INSERT OR REPLACE
  1.2320 +** encounters a constraint violation, it does not fail.  The
  1.2321 +** INSERT continues to completion after deleting rows that caused
  1.2322 +** the constraint problem so INSERT OR REPLACE will always change
  1.2323 +** the return value of this interface.)^
  1.2324 +**
  1.2325 +** ^For the purposes of this routine, an [INSERT] is considered to
  1.2326 +** be successful even if it is subsequently rolled back.
  1.2327 +**
  1.2328 +** This function is accessible to SQL statements via the
  1.2329 +** [last_insert_rowid() SQL function].
  1.2330 +**
  1.2331 +** If a separate thread performs a new [INSERT] on the same
  1.2332 +** database connection while the [sqlite3_last_insert_rowid()]
  1.2333 +** function is running and thus changes the last insert [rowid],
  1.2334 +** then the value returned by [sqlite3_last_insert_rowid()] is
  1.2335 +** unpredictable and might not equal either the old or the new
  1.2336 +** last insert [rowid].
  1.2337 +*/
  1.2338 +SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  1.2339 +
  1.2340 +/*
  1.2341 +** CAPI3REF: Count The Number Of Rows Modified
  1.2342 +**
  1.2343 +** ^This function returns the number of database rows that were changed
  1.2344 +** or inserted or deleted by the most recently completed SQL statement
  1.2345 +** on the [database connection] specified by the first parameter.
  1.2346 +** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
  1.2347 +** or [DELETE] statement are counted.  Auxiliary changes caused by
  1.2348 +** triggers or [foreign key actions] are not counted.)^ Use the
  1.2349 +** [sqlite3_total_changes()] function to find the total number of changes
  1.2350 +** including changes caused by triggers and foreign key actions.
  1.2351 +**
  1.2352 +** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
  1.2353 +** are not counted.  Only real table changes are counted.
  1.2354 +**
  1.2355 +** ^(A "row change" is a change to a single row of a single table
  1.2356 +** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
  1.2357 +** are changed as side effects of [REPLACE] constraint resolution,
  1.2358 +** rollback, ABORT processing, [DROP TABLE], or by any other
  1.2359 +** mechanisms do not count as direct row changes.)^
  1.2360 +**
  1.2361 +** A "trigger context" is a scope of execution that begins and
  1.2362 +** ends with the script of a [CREATE TRIGGER | trigger]. 
  1.2363 +** Most SQL statements are
  1.2364 +** evaluated outside of any trigger.  This is the "top level"
  1.2365 +** trigger context.  If a trigger fires from the top level, a
  1.2366 +** new trigger context is entered for the duration of that one
  1.2367 +** trigger.  Subtriggers create subcontexts for their duration.
  1.2368 +**
  1.2369 +** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
  1.2370 +** not create a new trigger context.
  1.2371 +**
  1.2372 +** ^This function returns the number of direct row changes in the
  1.2373 +** most recent INSERT, UPDATE, or DELETE statement within the same
  1.2374 +** trigger context.
  1.2375 +**
  1.2376 +** ^Thus, when called from the top level, this function returns the
  1.2377 +** number of changes in the most recent INSERT, UPDATE, or DELETE
  1.2378 +** that also occurred at the top level.  ^(Within the body of a trigger,
  1.2379 +** the sqlite3_changes() interface can be called to find the number of
  1.2380 +** changes in the most recently completed INSERT, UPDATE, or DELETE
  1.2381 +** statement within the body of the same trigger.
  1.2382 +** However, the number returned does not include changes
  1.2383 +** caused by subtriggers since those have their own context.)^
  1.2384 +**
  1.2385 +** See also the [sqlite3_total_changes()] interface, the
  1.2386 +** [count_changes pragma], and the [changes() SQL function].
  1.2387 +**
  1.2388 +** If a separate thread makes changes on the same database connection
  1.2389 +** while [sqlite3_changes()] is running then the value returned
  1.2390 +** is unpredictable and not meaningful.
  1.2391 +*/
  1.2392 +SQLITE_API int sqlite3_changes(sqlite3*);
  1.2393 +
  1.2394 +/*
  1.2395 +** CAPI3REF: Total Number Of Rows Modified
  1.2396 +**
  1.2397 +** ^This function returns the number of row changes caused by [INSERT],
  1.2398 +** [UPDATE] or [DELETE] statements since the [database connection] was opened.
  1.2399 +** ^(The count returned by sqlite3_total_changes() includes all changes
  1.2400 +** from all [CREATE TRIGGER | trigger] contexts and changes made by
  1.2401 +** [foreign key actions]. However,
  1.2402 +** the count does not include changes used to implement [REPLACE] constraints,
  1.2403 +** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
  1.2404 +** count does not include rows of views that fire an [INSTEAD OF trigger],
  1.2405 +** though if the INSTEAD OF trigger makes changes of its own, those changes 
  1.2406 +** are counted.)^
  1.2407 +** ^The sqlite3_total_changes() function counts the changes as soon as
  1.2408 +** the statement that makes them is completed (when the statement handle
  1.2409 +** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
  1.2410 +**
  1.2411 +** See also the [sqlite3_changes()] interface, the
  1.2412 +** [count_changes pragma], and the [total_changes() SQL function].
  1.2413 +**
  1.2414 +** If a separate thread makes changes on the same database connection
  1.2415 +** while [sqlite3_total_changes()] is running then the value
  1.2416 +** returned is unpredictable and not meaningful.
  1.2417 +*/
  1.2418 +SQLITE_API int sqlite3_total_changes(sqlite3*);
  1.2419 +
  1.2420 +/*
  1.2421 +** CAPI3REF: Interrupt A Long-Running Query
  1.2422 +**
  1.2423 +** ^This function causes any pending database operation to abort and
  1.2424 +** return at its earliest opportunity. This routine is typically
  1.2425 +** called in response to a user action such as pressing "Cancel"
  1.2426 +** or Ctrl-C where the user wants a long query operation to halt
  1.2427 +** immediately.
  1.2428 +**
  1.2429 +** ^It is safe to call this routine from a thread different from the
  1.2430 +** thread that is currently running the database operation.  But it
  1.2431 +** is not safe to call this routine with a [database connection] that
  1.2432 +** is closed or might close before sqlite3_interrupt() returns.
  1.2433 +**
  1.2434 +** ^If an SQL operation is very nearly finished at the time when
  1.2435 +** sqlite3_interrupt() is called, then it might not have an opportunity
  1.2436 +** to be interrupted and might continue to completion.
  1.2437 +**
  1.2438 +** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
  1.2439 +** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
  1.2440 +** that is inside an explicit transaction, then the entire transaction
  1.2441 +** will be rolled back automatically.
  1.2442 +**
  1.2443 +** ^The sqlite3_interrupt(D) call is in effect until all currently running
  1.2444 +** SQL statements on [database connection] D complete.  ^Any new SQL statements
  1.2445 +** that are started after the sqlite3_interrupt() call and before the 
  1.2446 +** running statements reaches zero are interrupted as if they had been
  1.2447 +** running prior to the sqlite3_interrupt() call.  ^New SQL statements
  1.2448 +** that are started after the running statement count reaches zero are
  1.2449 +** not effected by the sqlite3_interrupt().
  1.2450 +** ^A call to sqlite3_interrupt(D) that occurs when there are no running
  1.2451 +** SQL statements is a no-op and has no effect on SQL statements
  1.2452 +** that are started after the sqlite3_interrupt() call returns.
  1.2453 +**
  1.2454 +** If the database connection closes while [sqlite3_interrupt()]
  1.2455 +** is running then bad things will likely happen.
  1.2456 +*/
  1.2457 +SQLITE_API void sqlite3_interrupt(sqlite3*);
  1.2458 +
  1.2459 +/*
  1.2460 +** CAPI3REF: Determine If An SQL Statement Is Complete
  1.2461 +**
  1.2462 +** These routines are useful during command-line input to determine if the
  1.2463 +** currently entered text seems to form a complete SQL statement or
  1.2464 +** if additional input is needed before sending the text into
  1.2465 +** SQLite for parsing.  ^These routines return 1 if the input string
  1.2466 +** appears to be a complete SQL statement.  ^A statement is judged to be
  1.2467 +** complete if it ends with a semicolon token and is not a prefix of a
  1.2468 +** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
  1.2469 +** string literals or quoted identifier names or comments are not
  1.2470 +** independent tokens (they are part of the token in which they are
  1.2471 +** embedded) and thus do not count as a statement terminator.  ^Whitespace
  1.2472 +** and comments that follow the final semicolon are ignored.
  1.2473 +**
  1.2474 +** ^These routines return 0 if the statement is incomplete.  ^If a
  1.2475 +** memory allocation fails, then SQLITE_NOMEM is returned.
  1.2476 +**
  1.2477 +** ^These routines do not parse the SQL statements thus
  1.2478 +** will not detect syntactically incorrect SQL.
  1.2479 +**
  1.2480 +** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior 
  1.2481 +** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
  1.2482 +** automatically by sqlite3_complete16().  If that initialization fails,
  1.2483 +** then the return value from sqlite3_complete16() will be non-zero
  1.2484 +** regardless of whether or not the input SQL is complete.)^
  1.2485 +**
  1.2486 +** The input to [sqlite3_complete()] must be a zero-terminated
  1.2487 +** UTF-8 string.
  1.2488 +**
  1.2489 +** The input to [sqlite3_complete16()] must be a zero-terminated
  1.2490 +** UTF-16 string in native byte order.
  1.2491 +*/
  1.2492 +SQLITE_API int sqlite3_complete(const char *sql);
  1.2493 +SQLITE_API int sqlite3_complete16(const void *sql);
  1.2494 +
  1.2495 +/*
  1.2496 +** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  1.2497 +**
  1.2498 +** ^This routine sets a callback function that might be invoked whenever
  1.2499 +** an attempt is made to open a database table that another thread
  1.2500 +** or process has locked.
  1.2501 +**
  1.2502 +** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
  1.2503 +** is returned immediately upon encountering the lock.  ^If the busy callback
  1.2504 +** is not NULL, then the callback might be invoked with two arguments.
  1.2505 +**
  1.2506 +** ^The first argument to the busy handler is a copy of the void* pointer which
  1.2507 +** is the third argument to sqlite3_busy_handler().  ^The second argument to
  1.2508 +** the busy handler callback is the number of times that the busy handler has
  1.2509 +** been invoked for this locking event.  ^If the
  1.2510 +** busy callback returns 0, then no additional attempts are made to
  1.2511 +** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
  1.2512 +** ^If the callback returns non-zero, then another attempt
  1.2513 +** is made to open the database for reading and the cycle repeats.
  1.2514 +**
  1.2515 +** The presence of a busy handler does not guarantee that it will be invoked
  1.2516 +** when there is lock contention. ^If SQLite determines that invoking the busy
  1.2517 +** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
  1.2518 +** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
  1.2519 +** Consider a scenario where one process is holding a read lock that
  1.2520 +** it is trying to promote to a reserved lock and
  1.2521 +** a second process is holding a reserved lock that it is trying
  1.2522 +** to promote to an exclusive lock.  The first process cannot proceed
  1.2523 +** because it is blocked by the second and the second process cannot
  1.2524 +** proceed because it is blocked by the first.  If both processes
  1.2525 +** invoke the busy handlers, neither will make any progress.  Therefore,
  1.2526 +** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
  1.2527 +** will induce the first process to release its read lock and allow
  1.2528 +** the second process to proceed.
  1.2529 +**
  1.2530 +** ^The default busy callback is NULL.
  1.2531 +**
  1.2532 +** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
  1.2533 +** when SQLite is in the middle of a large transaction where all the
  1.2534 +** changes will not fit into the in-memory cache.  SQLite will
  1.2535 +** already hold a RESERVED lock on the database file, but it needs
  1.2536 +** to promote this lock to EXCLUSIVE so that it can spill cache
  1.2537 +** pages into the database file without harm to concurrent
  1.2538 +** readers.  ^If it is unable to promote the lock, then the in-memory
  1.2539 +** cache will be left in an inconsistent state and so the error
  1.2540 +** code is promoted from the relatively benign [SQLITE_BUSY] to
  1.2541 +** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
  1.2542 +** forces an automatic rollback of the changes.  See the
  1.2543 +** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
  1.2544 +** CorruptionFollowingBusyError</a> wiki page for a discussion of why
  1.2545 +** this is important.
  1.2546 +**
  1.2547 +** ^(There can only be a single busy handler defined for each
  1.2548 +** [database connection].  Setting a new busy handler clears any
  1.2549 +** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
  1.2550 +** will also set or clear the busy handler.
  1.2551 +**
  1.2552 +** The busy callback should not take any actions which modify the
  1.2553 +** database connection that invoked the busy handler.  Any such actions
  1.2554 +** result in undefined behavior.
  1.2555 +** 
  1.2556 +** A busy handler must not close the database connection
  1.2557 +** or [prepared statement] that invoked the busy handler.
  1.2558 +*/
  1.2559 +SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  1.2560 +
  1.2561 +/*
  1.2562 +** CAPI3REF: Set A Busy Timeout
  1.2563 +**
  1.2564 +** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  1.2565 +** for a specified amount of time when a table is locked.  ^The handler
  1.2566 +** will sleep multiple times until at least "ms" milliseconds of sleeping
  1.2567 +** have accumulated.  ^After at least "ms" milliseconds of sleeping,
  1.2568 +** the handler returns 0 which causes [sqlite3_step()] to return
  1.2569 +** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
  1.2570 +**
  1.2571 +** ^Calling this routine with an argument less than or equal to zero
  1.2572 +** turns off all busy handlers.
  1.2573 +**
  1.2574 +** ^(There can only be a single busy handler for a particular
  1.2575 +** [database connection] any any given moment.  If another busy handler
  1.2576 +** was defined  (using [sqlite3_busy_handler()]) prior to calling
  1.2577 +** this routine, that other busy handler is cleared.)^
  1.2578 +*/
  1.2579 +SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
  1.2580 +
  1.2581 +/*
  1.2582 +** CAPI3REF: Convenience Routines For Running Queries
  1.2583 +**
  1.2584 +** This is a legacy interface that is preserved for backwards compatibility.
  1.2585 +** Use of this interface is not recommended.
  1.2586 +**
  1.2587 +** Definition: A <b>result table</b> is memory data structure created by the
  1.2588 +** [sqlite3_get_table()] interface.  A result table records the
  1.2589 +** complete query results from one or more queries.
  1.2590 +**
  1.2591 +** The table conceptually has a number of rows and columns.  But
  1.2592 +** these numbers are not part of the result table itself.  These
  1.2593 +** numbers are obtained separately.  Let N be the number of rows
  1.2594 +** and M be the number of columns.
  1.2595 +**
  1.2596 +** A result table is an array of pointers to zero-terminated UTF-8 strings.
  1.2597 +** There are (N+1)*M elements in the array.  The first M pointers point
  1.2598 +** to zero-terminated strings that  contain the names of the columns.
  1.2599 +** The remaining entries all point to query results.  NULL values result
  1.2600 +** in NULL pointers.  All other values are in their UTF-8 zero-terminated
  1.2601 +** string representation as returned by [sqlite3_column_text()].
  1.2602 +**
  1.2603 +** A result table might consist of one or more memory allocations.
  1.2604 +** It is not safe to pass a result table directly to [sqlite3_free()].
  1.2605 +** A result table should be deallocated using [sqlite3_free_table()].
  1.2606 +**
  1.2607 +** ^(As an example of the result table format, suppose a query result
  1.2608 +** is as follows:
  1.2609 +**
  1.2610 +** <blockquote><pre>
  1.2611 +**        Name        | Age
  1.2612 +**        -----------------------
  1.2613 +**        Alice       | 43
  1.2614 +**        Bob         | 28
  1.2615 +**        Cindy       | 21
  1.2616 +** </pre></blockquote>
  1.2617 +**
  1.2618 +** There are two column (M==2) and three rows (N==3).  Thus the
  1.2619 +** result table has 8 entries.  Suppose the result table is stored
  1.2620 +** in an array names azResult.  Then azResult holds this content:
  1.2621 +**
  1.2622 +** <blockquote><pre>
  1.2623 +**        azResult&#91;0] = "Name";
  1.2624 +**        azResult&#91;1] = "Age";
  1.2625 +**        azResult&#91;2] = "Alice";
  1.2626 +**        azResult&#91;3] = "43";
  1.2627 +**        azResult&#91;4] = "Bob";
  1.2628 +**        azResult&#91;5] = "28";
  1.2629 +**        azResult&#91;6] = "Cindy";
  1.2630 +**        azResult&#91;7] = "21";
  1.2631 +** </pre></blockquote>)^
  1.2632 +**
  1.2633 +** ^The sqlite3_get_table() function evaluates one or more
  1.2634 +** semicolon-separated SQL statements in the zero-terminated UTF-8
  1.2635 +** string of its 2nd parameter and returns a result table to the
  1.2636 +** pointer given in its 3rd parameter.
  1.2637 +**
  1.2638 +** After the application has finished with the result from sqlite3_get_table(),
  1.2639 +** it must pass the result table pointer to sqlite3_free_table() in order to
  1.2640 +** release the memory that was malloced.  Because of the way the
  1.2641 +** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
  1.2642 +** function must not try to call [sqlite3_free()] directly.  Only
  1.2643 +** [sqlite3_free_table()] is able to release the memory properly and safely.
  1.2644 +**
  1.2645 +** The sqlite3_get_table() interface is implemented as a wrapper around
  1.2646 +** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
  1.2647 +** to any internal data structures of SQLite.  It uses only the public
  1.2648 +** interface defined here.  As a consequence, errors that occur in the
  1.2649 +** wrapper layer outside of the internal [sqlite3_exec()] call are not
  1.2650 +** reflected in subsequent calls to [sqlite3_errcode()] or
  1.2651 +** [sqlite3_errmsg()].
  1.2652 +*/
  1.2653 +SQLITE_API int sqlite3_get_table(
  1.2654 +  sqlite3 *db,          /* An open database */
  1.2655 +  const char *zSql,     /* SQL to be evaluated */
  1.2656 +  char ***pazResult,    /* Results of the query */
  1.2657 +  int *pnRow,           /* Number of result rows written here */
  1.2658 +  int *pnColumn,        /* Number of result columns written here */
  1.2659 +  char **pzErrmsg       /* Error msg written here */
  1.2660 +);
  1.2661 +SQLITE_API void sqlite3_free_table(char **result);
  1.2662 +
  1.2663 +/*
  1.2664 +** CAPI3REF: Formatted String Printing Functions
  1.2665 +**
  1.2666 +** These routines are work-alikes of the "printf()" family of functions
  1.2667 +** from the standard C library.
  1.2668 +**
  1.2669 +** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  1.2670 +** results into memory obtained from [sqlite3_malloc()].
  1.2671 +** The strings returned by these two routines should be
  1.2672 +** released by [sqlite3_free()].  ^Both routines return a
  1.2673 +** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
  1.2674 +** memory to hold the resulting string.
  1.2675 +**
  1.2676 +** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  1.2677 +** the standard C library.  The result is written into the
  1.2678 +** buffer supplied as the second parameter whose size is given by
  1.2679 +** the first parameter. Note that the order of the
  1.2680 +** first two parameters is reversed from snprintf().)^  This is an
  1.2681 +** historical accident that cannot be fixed without breaking
  1.2682 +** backwards compatibility.  ^(Note also that sqlite3_snprintf()
  1.2683 +** returns a pointer to its buffer instead of the number of
  1.2684 +** characters actually written into the buffer.)^  We admit that
  1.2685 +** the number of characters written would be a more useful return
  1.2686 +** value but we cannot change the implementation of sqlite3_snprintf()
  1.2687 +** now without breaking compatibility.
  1.2688 +**
  1.2689 +** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
  1.2690 +** guarantees that the buffer is always zero-terminated.  ^The first
  1.2691 +** parameter "n" is the total size of the buffer, including space for
  1.2692 +** the zero terminator.  So the longest string that can be completely
  1.2693 +** written will be n-1 characters.
  1.2694 +**
  1.2695 +** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  1.2696 +**
  1.2697 +** These routines all implement some additional formatting
  1.2698 +** options that are useful for constructing SQL statements.
  1.2699 +** All of the usual printf() formatting options apply.  In addition, there
  1.2700 +** is are "%q", "%Q", and "%z" options.
  1.2701 +**
  1.2702 +** ^(The %q option works like %s in that it substitutes a nul-terminated
  1.2703 +** string from the argument list.  But %q also doubles every '\'' character.
  1.2704 +** %q is designed for use inside a string literal.)^  By doubling each '\''
  1.2705 +** character it escapes that character and allows it to be inserted into
  1.2706 +** the string.
  1.2707 +**
  1.2708 +** For example, assume the string variable zText contains text as follows:
  1.2709 +**
  1.2710 +** <blockquote><pre>
  1.2711 +**  char *zText = "It's a happy day!";
  1.2712 +** </pre></blockquote>
  1.2713 +**
  1.2714 +** One can use this text in an SQL statement as follows:
  1.2715 +**
  1.2716 +** <blockquote><pre>
  1.2717 +**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  1.2718 +**  sqlite3_exec(db, zSQL, 0, 0, 0);
  1.2719 +**  sqlite3_free(zSQL);
  1.2720 +** </pre></blockquote>
  1.2721 +**
  1.2722 +** Because the %q format string is used, the '\'' character in zText
  1.2723 +** is escaped and the SQL generated is as follows:
  1.2724 +**
  1.2725 +** <blockquote><pre>
  1.2726 +**  INSERT INTO table1 VALUES('It''s a happy day!')
  1.2727 +** </pre></blockquote>
  1.2728 +**
  1.2729 +** This is correct.  Had we used %s instead of %q, the generated SQL
  1.2730 +** would have looked like this:
  1.2731 +**
  1.2732 +** <blockquote><pre>
  1.2733 +**  INSERT INTO table1 VALUES('It's a happy day!');
  1.2734 +** </pre></blockquote>
  1.2735 +**
  1.2736 +** This second example is an SQL syntax error.  As a general rule you should
  1.2737 +** always use %q instead of %s when inserting text into a string literal.
  1.2738 +**
  1.2739 +** ^(The %Q option works like %q except it also adds single quotes around
  1.2740 +** the outside of the total string.  Additionally, if the parameter in the
  1.2741 +** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  1.2742 +** single quotes).)^  So, for example, one could say:
  1.2743 +**
  1.2744 +** <blockquote><pre>
  1.2745 +**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  1.2746 +**  sqlite3_exec(db, zSQL, 0, 0, 0);
  1.2747 +**  sqlite3_free(zSQL);
  1.2748 +** </pre></blockquote>
  1.2749 +**
  1.2750 +** The code above will render a correct SQL statement in the zSQL
  1.2751 +** variable even if the zText variable is a NULL pointer.
  1.2752 +**
  1.2753 +** ^(The "%z" formatting option works like "%s" but with the
  1.2754 +** addition that after the string has been read and copied into
  1.2755 +** the result, [sqlite3_free()] is called on the input string.)^
  1.2756 +*/
  1.2757 +SQLITE_API char *sqlite3_mprintf(const char*,...);
  1.2758 +SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  1.2759 +SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  1.2760 +SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  1.2761 +
  1.2762 +/*
  1.2763 +** CAPI3REF: Memory Allocation Subsystem
  1.2764 +**
  1.2765 +** The SQLite core uses these three routines for all of its own
  1.2766 +** internal memory allocation needs. "Core" in the previous sentence
  1.2767 +** does not include operating-system specific VFS implementation.  The
  1.2768 +** Windows VFS uses native malloc() and free() for some operations.
  1.2769 +**
  1.2770 +** ^The sqlite3_malloc() routine returns a pointer to a block
  1.2771 +** of memory at least N bytes in length, where N is the parameter.
  1.2772 +** ^If sqlite3_malloc() is unable to obtain sufficient free
  1.2773 +** memory, it returns a NULL pointer.  ^If the parameter N to
  1.2774 +** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
  1.2775 +** a NULL pointer.
  1.2776 +**
  1.2777 +** ^Calling sqlite3_free() with a pointer previously returned
  1.2778 +** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
  1.2779 +** that it might be reused.  ^The sqlite3_free() routine is
  1.2780 +** a no-op if is called with a NULL pointer.  Passing a NULL pointer
  1.2781 +** to sqlite3_free() is harmless.  After being freed, memory
  1.2782 +** should neither be read nor written.  Even reading previously freed
  1.2783 +** memory might result in a segmentation fault or other severe error.
  1.2784 +** Memory corruption, a segmentation fault, or other severe error
  1.2785 +** might result if sqlite3_free() is called with a non-NULL pointer that
  1.2786 +** was not obtained from sqlite3_malloc() or sqlite3_realloc().
  1.2787 +**
  1.2788 +** ^(The sqlite3_realloc() interface attempts to resize a
  1.2789 +** prior memory allocation to be at least N bytes, where N is the
  1.2790 +** second parameter.  The memory allocation to be resized is the first
  1.2791 +** parameter.)^ ^ If the first parameter to sqlite3_realloc()
  1.2792 +** is a NULL pointer then its behavior is identical to calling
  1.2793 +** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
  1.2794 +** ^If the second parameter to sqlite3_realloc() is zero or
  1.2795 +** negative then the behavior is exactly the same as calling
  1.2796 +** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
  1.2797 +** ^sqlite3_realloc() returns a pointer to a memory allocation
  1.2798 +** of at least N bytes in size or NULL if sufficient memory is unavailable.
  1.2799 +** ^If M is the size of the prior allocation, then min(N,M) bytes
  1.2800 +** of the prior allocation are copied into the beginning of buffer returned
  1.2801 +** by sqlite3_realloc() and the prior allocation is freed.
  1.2802 +** ^If sqlite3_realloc() returns NULL, then the prior allocation
  1.2803 +** is not freed.
  1.2804 +**
  1.2805 +** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
  1.2806 +** is always aligned to at least an 8 byte boundary, or to a
  1.2807 +** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
  1.2808 +** option is used.
  1.2809 +**
  1.2810 +** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  1.2811 +** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  1.2812 +** implementation of these routines to be omitted.  That capability
  1.2813 +** is no longer provided.  Only built-in memory allocators can be used.
  1.2814 +**
  1.2815 +** Prior to SQLite version 3.7.10, the Windows OS interface layer called
  1.2816 +** the system malloc() and free() directly when converting
  1.2817 +** filenames between the UTF-8 encoding used by SQLite
  1.2818 +** and whatever filename encoding is used by the particular Windows
  1.2819 +** installation.  Memory allocation errors were detected, but
  1.2820 +** they were reported back as [SQLITE_CANTOPEN] or
  1.2821 +** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
  1.2822 +**
  1.2823 +** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
  1.2824 +** must be either NULL or else pointers obtained from a prior
  1.2825 +** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
  1.2826 +** not yet been released.
  1.2827 +**
  1.2828 +** The application must not read or write any part of
  1.2829 +** a block of memory after it has been released using
  1.2830 +** [sqlite3_free()] or [sqlite3_realloc()].
  1.2831 +*/
  1.2832 +SQLITE_API void *sqlite3_malloc(int);
  1.2833 +SQLITE_API void *sqlite3_realloc(void*, int);
  1.2834 +SQLITE_API void sqlite3_free(void*);
  1.2835 +
  1.2836 +/*
  1.2837 +** CAPI3REF: Memory Allocator Statistics
  1.2838 +**
  1.2839 +** SQLite provides these two interfaces for reporting on the status
  1.2840 +** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
  1.2841 +** routines, which form the built-in memory allocation subsystem.
  1.2842 +**
  1.2843 +** ^The [sqlite3_memory_used()] routine returns the number of bytes
  1.2844 +** of memory currently outstanding (malloced but not freed).
  1.2845 +** ^The [sqlite3_memory_highwater()] routine returns the maximum
  1.2846 +** value of [sqlite3_memory_used()] since the high-water mark
  1.2847 +** was last reset.  ^The values returned by [sqlite3_memory_used()] and
  1.2848 +** [sqlite3_memory_highwater()] include any overhead
  1.2849 +** added by SQLite in its implementation of [sqlite3_malloc()],
  1.2850 +** but not overhead added by the any underlying system library
  1.2851 +** routines that [sqlite3_malloc()] may call.
  1.2852 +**
  1.2853 +** ^The memory high-water mark is reset to the current value of
  1.2854 +** [sqlite3_memory_used()] if and only if the parameter to
  1.2855 +** [sqlite3_memory_highwater()] is true.  ^The value returned
  1.2856 +** by [sqlite3_memory_highwater(1)] is the high-water mark
  1.2857 +** prior to the reset.
  1.2858 +*/
  1.2859 +SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
  1.2860 +SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
  1.2861 +
  1.2862 +/*
  1.2863 +** CAPI3REF: Pseudo-Random Number Generator
  1.2864 +**
  1.2865 +** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
  1.2866 +** select random [ROWID | ROWIDs] when inserting new records into a table that
  1.2867 +** already uses the largest possible [ROWID].  The PRNG is also used for
  1.2868 +** the build-in random() and randomblob() SQL functions.  This interface allows
  1.2869 +** applications to access the same PRNG for other purposes.
  1.2870 +**
  1.2871 +** ^A call to this routine stores N bytes of randomness into buffer P.
  1.2872 +**
  1.2873 +** ^The first time this routine is invoked (either internally or by
  1.2874 +** the application) the PRNG is seeded using randomness obtained
  1.2875 +** from the xRandomness method of the default [sqlite3_vfs] object.
  1.2876 +** ^On all subsequent invocations, the pseudo-randomness is generated
  1.2877 +** internally and without recourse to the [sqlite3_vfs] xRandomness
  1.2878 +** method.
  1.2879 +*/
  1.2880 +SQLITE_API void sqlite3_randomness(int N, void *P);
  1.2881 +
  1.2882 +/*
  1.2883 +** CAPI3REF: Compile-Time Authorization Callbacks
  1.2884 +**
  1.2885 +** ^This routine registers an authorizer callback with a particular
  1.2886 +** [database connection], supplied in the first argument.
  1.2887 +** ^The authorizer callback is invoked as SQL statements are being compiled
  1.2888 +** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  1.2889 +** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
  1.2890 +** points during the compilation process, as logic is being created
  1.2891 +** to perform various actions, the authorizer callback is invoked to
  1.2892 +** see if those actions are allowed.  ^The authorizer callback should
  1.2893 +** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
  1.2894 +** specific action but allow the SQL statement to continue to be
  1.2895 +** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
  1.2896 +** rejected with an error.  ^If the authorizer callback returns
  1.2897 +** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
  1.2898 +** then the [sqlite3_prepare_v2()] or equivalent call that triggered
  1.2899 +** the authorizer will fail with an error message.
  1.2900 +**
  1.2901 +** When the callback returns [SQLITE_OK], that means the operation
  1.2902 +** requested is ok.  ^When the callback returns [SQLITE_DENY], the
  1.2903 +** [sqlite3_prepare_v2()] or equivalent call that triggered the
  1.2904 +** authorizer will fail with an error message explaining that
  1.2905 +** access is denied. 
  1.2906 +**
  1.2907 +** ^The first parameter to the authorizer callback is a copy of the third
  1.2908 +** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
  1.2909 +** to the callback is an integer [SQLITE_COPY | action code] that specifies
  1.2910 +** the particular action to be authorized. ^The third through sixth parameters
  1.2911 +** to the callback are zero-terminated strings that contain additional
  1.2912 +** details about the action to be authorized.
  1.2913 +**
  1.2914 +** ^If the action code is [SQLITE_READ]
  1.2915 +** and the callback returns [SQLITE_IGNORE] then the
  1.2916 +** [prepared statement] statement is constructed to substitute
  1.2917 +** a NULL value in place of the table column that would have
  1.2918 +** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
  1.2919 +** return can be used to deny an untrusted user access to individual
  1.2920 +** columns of a table.
  1.2921 +** ^If the action code is [SQLITE_DELETE] and the callback returns
  1.2922 +** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
  1.2923 +** [truncate optimization] is disabled and all rows are deleted individually.
  1.2924 +**
  1.2925 +** An authorizer is used when [sqlite3_prepare | preparing]
  1.2926 +** SQL statements from an untrusted source, to ensure that the SQL statements
  1.2927 +** do not try to access data they are not allowed to see, or that they do not
  1.2928 +** try to execute malicious statements that damage the database.  For
  1.2929 +** example, an application may allow a user to enter arbitrary
  1.2930 +** SQL queries for evaluation by a database.  But the application does
  1.2931 +** not want the user to be able to make arbitrary changes to the
  1.2932 +** database.  An authorizer could then be put in place while the
  1.2933 +** user-entered SQL is being [sqlite3_prepare | prepared] that
  1.2934 +** disallows everything except [SELECT] statements.
  1.2935 +**
  1.2936 +** Applications that need to process SQL from untrusted sources
  1.2937 +** might also consider lowering resource limits using [sqlite3_limit()]
  1.2938 +** and limiting database size using the [max_page_count] [PRAGMA]
  1.2939 +** in addition to using an authorizer.
  1.2940 +**
  1.2941 +** ^(Only a single authorizer can be in place on a database connection
  1.2942 +** at a time.  Each call to sqlite3_set_authorizer overrides the
  1.2943 +** previous call.)^  ^Disable the authorizer by installing a NULL callback.
  1.2944 +** The authorizer is disabled by default.
  1.2945 +**
  1.2946 +** The authorizer callback must not do anything that will modify
  1.2947 +** the database connection that invoked the authorizer callback.
  1.2948 +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  1.2949 +** database connections for the meaning of "modify" in this paragraph.
  1.2950 +**
  1.2951 +** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
  1.2952 +** statement might be re-prepared during [sqlite3_step()] due to a 
  1.2953 +** schema change.  Hence, the application should ensure that the
  1.2954 +** correct authorizer callback remains in place during the [sqlite3_step()].
  1.2955 +**
  1.2956 +** ^Note that the authorizer callback is invoked only during
  1.2957 +** [sqlite3_prepare()] or its variants.  Authorization is not
  1.2958 +** performed during statement evaluation in [sqlite3_step()], unless
  1.2959 +** as stated in the previous paragraph, sqlite3_step() invokes
  1.2960 +** sqlite3_prepare_v2() to reprepare a statement after a schema change.
  1.2961 +*/
  1.2962 +SQLITE_API int sqlite3_set_authorizer(
  1.2963 +  sqlite3*,
  1.2964 +  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
  1.2965 +  void *pUserData
  1.2966 +);
  1.2967 +
  1.2968 +/*
  1.2969 +** CAPI3REF: Authorizer Return Codes
  1.2970 +**
  1.2971 +** The [sqlite3_set_authorizer | authorizer callback function] must
  1.2972 +** return either [SQLITE_OK] or one of these two constants in order
  1.2973 +** to signal SQLite whether or not the action is permitted.  See the
  1.2974 +** [sqlite3_set_authorizer | authorizer documentation] for additional
  1.2975 +** information.
  1.2976 +**
  1.2977 +** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
  1.2978 +** from the [sqlite3_vtab_on_conflict()] interface.
  1.2979 +*/
  1.2980 +#define SQLITE_DENY   1   /* Abort the SQL statement with an error */
  1.2981 +#define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
  1.2982 +
  1.2983 +/*
  1.2984 +** CAPI3REF: Authorizer Action Codes
  1.2985 +**
  1.2986 +** The [sqlite3_set_authorizer()] interface registers a callback function
  1.2987 +** that is invoked to authorize certain SQL statement actions.  The
  1.2988 +** second parameter to the callback is an integer code that specifies
  1.2989 +** what action is being authorized.  These are the integer action codes that
  1.2990 +** the authorizer callback may be passed.
  1.2991 +**
  1.2992 +** These action code values signify what kind of operation is to be
  1.2993 +** authorized.  The 3rd and 4th parameters to the authorization
  1.2994 +** callback function will be parameters or NULL depending on which of these
  1.2995 +** codes is used as the second parameter.  ^(The 5th parameter to the
  1.2996 +** authorizer callback is the name of the database ("main", "temp",
  1.2997 +** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
  1.2998 +** is the name of the inner-most trigger or view that is responsible for
  1.2999 +** the access attempt or NULL if this access attempt is directly from
  1.3000 +** top-level SQL code.
  1.3001 +*/
  1.3002 +/******************************************* 3rd ************ 4th ***********/
  1.3003 +#define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
  1.3004 +#define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
  1.3005 +#define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
  1.3006 +#define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
  1.3007 +#define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
  1.3008 +#define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
  1.3009 +#define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
  1.3010 +#define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
  1.3011 +#define SQLITE_DELETE                9   /* Table Name      NULL            */
  1.3012 +#define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
  1.3013 +#define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
  1.3014 +#define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
  1.3015 +#define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
  1.3016 +#define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
  1.3017 +#define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
  1.3018 +#define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
  1.3019 +#define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
  1.3020 +#define SQLITE_INSERT               18   /* Table Name      NULL            */
  1.3021 +#define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
  1.3022 +#define SQLITE_READ                 20   /* Table Name      Column Name     */
  1.3023 +#define SQLITE_SELECT               21   /* NULL            NULL            */
  1.3024 +#define SQLITE_TRANSACTION          22   /* Operation       NULL            */
  1.3025 +#define SQLITE_UPDATE               23   /* Table Name      Column Name     */
  1.3026 +#define SQLITE_ATTACH               24   /* Filename        NULL            */
  1.3027 +#define SQLITE_DETACH               25   /* Database Name   NULL            */
  1.3028 +#define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
  1.3029 +#define SQLITE_REINDEX              27   /* Index Name      NULL            */
  1.3030 +#define SQLITE_ANALYZE              28   /* Table Name      NULL            */
  1.3031 +#define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
  1.3032 +#define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
  1.3033 +#define SQLITE_FUNCTION             31   /* NULL            Function Name   */
  1.3034 +#define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  1.3035 +#define SQLITE_COPY                  0   /* No longer used */
  1.3036 +
  1.3037 +/*
  1.3038 +** CAPI3REF: Tracing And Profiling Functions
  1.3039 +**
  1.3040 +** These routines register callback functions that can be used for
  1.3041 +** tracing and profiling the execution of SQL statements.
  1.3042 +**
  1.3043 +** ^The callback function registered by sqlite3_trace() is invoked at
  1.3044 +** various times when an SQL statement is being run by [sqlite3_step()].
  1.3045 +** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
  1.3046 +** SQL statement text as the statement first begins executing.
  1.3047 +** ^(Additional sqlite3_trace() callbacks might occur
  1.3048 +** as each triggered subprogram is entered.  The callbacks for triggers
  1.3049 +** contain a UTF-8 SQL comment that identifies the trigger.)^
  1.3050 +**
  1.3051 +** ^The callback function registered by sqlite3_profile() is invoked
  1.3052 +** as each SQL statement finishes.  ^The profile callback contains
  1.3053 +** the original statement text and an estimate of wall-clock time
  1.3054 +** of how long that statement took to run.  ^The profile callback
  1.3055 +** time is in units of nanoseconds, however the current implementation
  1.3056 +** is only capable of millisecond resolution so the six least significant
  1.3057 +** digits in the time are meaningless.  Future versions of SQLite
  1.3058 +** might provide greater resolution on the profiler callback.  The
  1.3059 +** sqlite3_profile() function is considered experimental and is
  1.3060 +** subject to change in future versions of SQLite.
  1.3061 +*/
  1.3062 +SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  1.3063 +SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
  1.3064 +   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  1.3065 +
  1.3066 +/*
  1.3067 +** CAPI3REF: Query Progress Callbacks
  1.3068 +**
  1.3069 +** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  1.3070 +** function X to be invoked periodically during long running calls to
  1.3071 +** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  1.3072 +** database connection D.  An example use for this
  1.3073 +** interface is to keep a GUI updated during a large query.
  1.3074 +**
  1.3075 +** ^The parameter P is passed through as the only parameter to the 
  1.3076 +** callback function X.  ^The parameter N is the number of 
  1.3077 +** [virtual machine instructions] that are evaluated between successive
  1.3078 +** invocations of the callback X.
  1.3079 +**
  1.3080 +** ^Only a single progress handler may be defined at one time per
  1.3081 +** [database connection]; setting a new progress handler cancels the
  1.3082 +** old one.  ^Setting parameter X to NULL disables the progress handler.
  1.3083 +** ^The progress handler is also disabled by setting N to a value less
  1.3084 +** than 1.
  1.3085 +**
  1.3086 +** ^If the progress callback returns non-zero, the operation is
  1.3087 +** interrupted.  This feature can be used to implement a
  1.3088 +** "Cancel" button on a GUI progress dialog box.
  1.3089 +**
  1.3090 +** The progress handler callback must not do anything that will modify
  1.3091 +** the database connection that invoked the progress handler.
  1.3092 +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  1.3093 +** database connections for the meaning of "modify" in this paragraph.
  1.3094 +**
  1.3095 +*/
  1.3096 +SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  1.3097 +
  1.3098 +/*
  1.3099 +** CAPI3REF: Opening A New Database Connection
  1.3100 +**
  1.3101 +** ^These routines open an SQLite database file as specified by the 
  1.3102 +** filename argument. ^The filename argument is interpreted as UTF-8 for
  1.3103 +** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  1.3104 +** order for sqlite3_open16(). ^(A [database connection] handle is usually
  1.3105 +** returned in *ppDb, even if an error occurs.  The only exception is that
  1.3106 +** if SQLite is unable to allocate memory to hold the [sqlite3] object,
  1.3107 +** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
  1.3108 +** object.)^ ^(If the database is opened (and/or created) successfully, then
  1.3109 +** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
  1.3110 +** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
  1.3111 +** an English language description of the error following a failure of any
  1.3112 +** of the sqlite3_open() routines.
  1.3113 +**
  1.3114 +** ^The default encoding for the database will be UTF-8 if
  1.3115 +** sqlite3_open() or sqlite3_open_v2() is called and
  1.3116 +** UTF-16 in the native byte order if sqlite3_open16() is used.
  1.3117 +**
  1.3118 +** Whether or not an error occurs when it is opened, resources
  1.3119 +** associated with the [database connection] handle should be released by
  1.3120 +** passing it to [sqlite3_close()] when it is no longer required.
  1.3121 +**
  1.3122 +** The sqlite3_open_v2() interface works like sqlite3_open()
  1.3123 +** except that it accepts two additional parameters for additional control
  1.3124 +** over the new database connection.  ^(The flags parameter to
  1.3125 +** sqlite3_open_v2() can take one of
  1.3126 +** the following three values, optionally combined with the 
  1.3127 +** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
  1.3128 +** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
  1.3129 +**
  1.3130 +** <dl>
  1.3131 +** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
  1.3132 +** <dd>The database is opened in read-only mode.  If the database does not
  1.3133 +** already exist, an error is returned.</dd>)^
  1.3134 +**
  1.3135 +** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
  1.3136 +** <dd>The database is opened for reading and writing if possible, or reading
  1.3137 +** only if the file is write protected by the operating system.  In either
  1.3138 +** case the database must already exist, otherwise an error is returned.</dd>)^
  1.3139 +**
  1.3140 +** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
  1.3141 +** <dd>The database is opened for reading and writing, and is created if
  1.3142 +** it does not already exist. This is the behavior that is always used for
  1.3143 +** sqlite3_open() and sqlite3_open16().</dd>)^
  1.3144 +** </dl>
  1.3145 +**
  1.3146 +** If the 3rd parameter to sqlite3_open_v2() is not one of the
  1.3147 +** combinations shown above optionally combined with other
  1.3148 +** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
  1.3149 +** then the behavior is undefined.
  1.3150 +**
  1.3151 +** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
  1.3152 +** opens in the multi-thread [threading mode] as long as the single-thread
  1.3153 +** mode has not been set at compile-time or start-time.  ^If the
  1.3154 +** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
  1.3155 +** in the serialized [threading mode] unless single-thread was
  1.3156 +** previously selected at compile-time or start-time.
  1.3157 +** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
  1.3158 +** eligible to use [shared cache mode], regardless of whether or not shared
  1.3159 +** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
  1.3160 +** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
  1.3161 +** participate in [shared cache mode] even if it is enabled.
  1.3162 +**
  1.3163 +** ^The fourth parameter to sqlite3_open_v2() is the name of the
  1.3164 +** [sqlite3_vfs] object that defines the operating system interface that
  1.3165 +** the new database connection should use.  ^If the fourth parameter is
  1.3166 +** a NULL pointer then the default [sqlite3_vfs] object is used.
  1.3167 +**
  1.3168 +** ^If the filename is ":memory:", then a private, temporary in-memory database
  1.3169 +** is created for the connection.  ^This in-memory database will vanish when
  1.3170 +** the database connection is closed.  Future versions of SQLite might
  1.3171 +** make use of additional special filenames that begin with the ":" character.
  1.3172 +** It is recommended that when a database filename actually does begin with
  1.3173 +** a ":" character you should prefix the filename with a pathname such as
  1.3174 +** "./" to avoid ambiguity.
  1.3175 +**
  1.3176 +** ^If the filename is an empty string, then a private, temporary
  1.3177 +** on-disk database will be created.  ^This private database will be
  1.3178 +** automatically deleted as soon as the database connection is closed.
  1.3179 +**
  1.3180 +** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
  1.3181 +**
  1.3182 +** ^If [URI filename] interpretation is enabled, and the filename argument
  1.3183 +** begins with "file:", then the filename is interpreted as a URI. ^URI
  1.3184 +** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
  1.3185 +** set in the fourth argument to sqlite3_open_v2(), or if it has
  1.3186 +** been enabled globally using the [SQLITE_CONFIG_URI] option with the
  1.3187 +** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
  1.3188 +** As of SQLite version 3.7.7, URI filename interpretation is turned off
  1.3189 +** by default, but future releases of SQLite might enable URI filename
  1.3190 +** interpretation by default.  See "[URI filenames]" for additional
  1.3191 +** information.
  1.3192 +**
  1.3193 +** URI filenames are parsed according to RFC 3986. ^If the URI contains an
  1.3194 +** authority, then it must be either an empty string or the string 
  1.3195 +** "localhost". ^If the authority is not an empty string or "localhost", an 
  1.3196 +** error is returned to the caller. ^The fragment component of a URI, if 
  1.3197 +** present, is ignored.
  1.3198 +**
  1.3199 +** ^SQLite uses the path component of the URI as the name of the disk file
  1.3200 +** which contains the database. ^If the path begins with a '/' character, 
  1.3201 +** then it is interpreted as an absolute path. ^If the path does not begin 
  1.3202 +** with a '/' (meaning that the authority section is omitted from the URI)
  1.3203 +** then the path is interpreted as a relative path. 
  1.3204 +** ^On windows, the first component of an absolute path 
  1.3205 +** is a drive specification (e.g. "C:").
  1.3206 +**
  1.3207 +** [[core URI query parameters]]
  1.3208 +** The query component of a URI may contain parameters that are interpreted
  1.3209 +** either by SQLite itself, or by a [VFS | custom VFS implementation].
  1.3210 +** SQLite interprets the following three query parameters:
  1.3211 +**
  1.3212 +** <ul>
  1.3213 +**   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
  1.3214 +**     a VFS object that provides the operating system interface that should
  1.3215 +**     be used to access the database file on disk. ^If this option is set to
  1.3216 +**     an empty string the default VFS object is used. ^Specifying an unknown
  1.3217 +**     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
  1.3218 +**     present, then the VFS specified by the option takes precedence over
  1.3219 +**     the value passed as the fourth parameter to sqlite3_open_v2().
  1.3220 +**
  1.3221 +**   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
  1.3222 +**     "rwc", or "memory". Attempting to set it to any other value is
  1.3223 +**     an error)^. 
  1.3224 +**     ^If "ro" is specified, then the database is opened for read-only 
  1.3225 +**     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 
  1.3226 +**     third argument to sqlite3_open_v2(). ^If the mode option is set to 
  1.3227 +**     "rw", then the database is opened for read-write (but not create) 
  1.3228 +**     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 
  1.3229 +**     been set. ^Value "rwc" is equivalent to setting both 
  1.3230 +**     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
  1.3231 +**     set to "memory" then a pure [in-memory database] that never reads
  1.3232 +**     or writes from disk is used. ^It is an error to specify a value for
  1.3233 +**     the mode parameter that is less restrictive than that specified by
  1.3234 +**     the flags passed in the third parameter to sqlite3_open_v2().
  1.3235 +**
  1.3236 +**   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
  1.3237 +**     "private". ^Setting it to "shared" is equivalent to setting the
  1.3238 +**     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
  1.3239 +**     sqlite3_open_v2(). ^Setting the cache parameter to "private" is 
  1.3240 +**     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
  1.3241 +**     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
  1.3242 +**     a URI filename, its value overrides any behaviour requested by setting
  1.3243 +**     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
  1.3244 +** </ul>
  1.3245 +**
  1.3246 +** ^Specifying an unknown parameter in the query component of a URI is not an
  1.3247 +** error.  Future versions of SQLite might understand additional query
  1.3248 +** parameters.  See "[query parameters with special meaning to SQLite]" for
  1.3249 +** additional information.
  1.3250 +**
  1.3251 +** [[URI filename examples]] <h3>URI filename examples</h3>
  1.3252 +**
  1.3253 +** <table border="1" align=center cellpadding=5>
  1.3254 +** <tr><th> URI filenames <th> Results
  1.3255 +** <tr><td> file:data.db <td> 
  1.3256 +**          Open the file "data.db" in the current directory.
  1.3257 +** <tr><td> file:/home/fred/data.db<br>
  1.3258 +**          file:///home/fred/data.db <br> 
  1.3259 +**          file://localhost/home/fred/data.db <br> <td> 
  1.3260 +**          Open the database file "/home/fred/data.db".
  1.3261 +** <tr><td> file://darkstar/home/fred/data.db <td> 
  1.3262 +**          An error. "darkstar" is not a recognized authority.
  1.3263 +** <tr><td style="white-space:nowrap"> 
  1.3264 +**          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
  1.3265 +**     <td> Windows only: Open the file "data.db" on fred's desktop on drive
  1.3266 +**          C:. Note that the %20 escaping in this example is not strictly 
  1.3267 +**          necessary - space characters can be used literally
  1.3268 +**          in URI filenames.
  1.3269 +** <tr><td> file:data.db?mode=ro&cache=private <td> 
  1.3270 +**          Open file "data.db" in the current directory for read-only access.
  1.3271 +**          Regardless of whether or not shared-cache mode is enabled by
  1.3272 +**          default, use a private cache.
  1.3273 +** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
  1.3274 +**          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
  1.3275 +** <tr><td> file:data.db?mode=readonly <td> 
  1.3276 +**          An error. "readonly" is not a valid option for the "mode" parameter.
  1.3277 +** </table>
  1.3278 +**
  1.3279 +** ^URI hexadecimal escape sequences (%HH) are supported within the path and
  1.3280 +** query components of a URI. A hexadecimal escape sequence consists of a
  1.3281 +** percent sign - "%" - followed by exactly two hexadecimal digits 
  1.3282 +** specifying an octet value. ^Before the path or query components of a
  1.3283 +** URI filename are interpreted, they are encoded using UTF-8 and all 
  1.3284 +** hexadecimal escape sequences replaced by a single byte containing the
  1.3285 +** corresponding octet. If this process generates an invalid UTF-8 encoding,
  1.3286 +** the results are undefined.
  1.3287 +**
  1.3288 +** <b>Note to Windows users:</b>  The encoding used for the filename argument
  1.3289 +** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
  1.3290 +** codepage is currently defined.  Filenames containing international
  1.3291 +** characters must be converted to UTF-8 prior to passing them into
  1.3292 +** sqlite3_open() or sqlite3_open_v2().
  1.3293 +**
  1.3294 +** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
  1.3295 +** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
  1.3296 +** features that require the use of temporary files may fail.
  1.3297 +**
  1.3298 +** See also: [sqlite3_temp_directory]
  1.3299 +*/
  1.3300 +SQLITE_API int sqlite3_open(
  1.3301 +  const char *filename,   /* Database filename (UTF-8) */
  1.3302 +  sqlite3 **ppDb          /* OUT: SQLite db handle */
  1.3303 +);
  1.3304 +SQLITE_API int sqlite3_open16(
  1.3305 +  const void *filename,   /* Database filename (UTF-16) */
  1.3306 +  sqlite3 **ppDb          /* OUT: SQLite db handle */
  1.3307 +);
  1.3308 +SQLITE_API int sqlite3_open_v2(
  1.3309 +  const char *filename,   /* Database filename (UTF-8) */
  1.3310 +  sqlite3 **ppDb,         /* OUT: SQLite db handle */
  1.3311 +  int flags,              /* Flags */
  1.3312 +  const char *zVfs        /* Name of VFS module to use */
  1.3313 +);
  1.3314 +
  1.3315 +/*
  1.3316 +** CAPI3REF: Obtain Values For URI Parameters
  1.3317 +**
  1.3318 +** These are utility routines, useful to VFS implementations, that check
  1.3319 +** to see if a database file was a URI that contained a specific query 
  1.3320 +** parameter, and if so obtains the value of that query parameter.
  1.3321 +**
  1.3322 +** If F is the database filename pointer passed into the xOpen() method of 
  1.3323 +** a VFS implementation when the flags parameter to xOpen() has one or 
  1.3324 +** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
  1.3325 +** P is the name of the query parameter, then
  1.3326 +** sqlite3_uri_parameter(F,P) returns the value of the P
  1.3327 +** parameter if it exists or a NULL pointer if P does not appear as a 
  1.3328 +** query parameter on F.  If P is a query parameter of F
  1.3329 +** has no explicit value, then sqlite3_uri_parameter(F,P) returns
  1.3330 +** a pointer to an empty string.
  1.3331 +**
  1.3332 +** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
  1.3333 +** parameter and returns true (1) or false (0) according to the value
  1.3334 +** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
  1.3335 +** value of query parameter P is one of "yes", "true", or "on" in any
  1.3336 +** case or if the value begins with a non-zero number.  The 
  1.3337 +** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
  1.3338 +** query parameter P is one of "no", "false", or "off" in any case or
  1.3339 +** if the value begins with a numeric zero.  If P is not a query
  1.3340 +** parameter on F or if the value of P is does not match any of the
  1.3341 +** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
  1.3342 +**
  1.3343 +** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
  1.3344 +** 64-bit signed integer and returns that integer, or D if P does not
  1.3345 +** exist.  If the value of P is something other than an integer, then
  1.3346 +** zero is returned.
  1.3347 +** 
  1.3348 +** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
  1.3349 +** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
  1.3350 +** is not a database file pathname pointer that SQLite passed into the xOpen
  1.3351 +** VFS method, then the behavior of this routine is undefined and probably
  1.3352 +** undesirable.
  1.3353 +*/
  1.3354 +SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  1.3355 +SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  1.3356 +SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  1.3357 +
  1.3358 +
  1.3359 +/*
  1.3360 +** CAPI3REF: Error Codes And Messages
  1.3361 +**
  1.3362 +** ^The sqlite3_errcode() interface returns the numeric [result code] or
  1.3363 +** [extended result code] for the most recent failed sqlite3_* API call
  1.3364 +** associated with a [database connection]. If a prior API call failed
  1.3365 +** but the most recent API call succeeded, the return value from
  1.3366 +** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
  1.3367 +** interface is the same except that it always returns the 
  1.3368 +** [extended result code] even when extended result codes are
  1.3369 +** disabled.
  1.3370 +**
  1.3371 +** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
  1.3372 +** text that describes the error, as either UTF-8 or UTF-16 respectively.
  1.3373 +** ^(Memory to hold the error message string is managed internally.
  1.3374 +** The application does not need to worry about freeing the result.
  1.3375 +** However, the error string might be overwritten or deallocated by
  1.3376 +** subsequent calls to other SQLite interface functions.)^
  1.3377 +**
  1.3378 +** ^The sqlite3_errstr() interface returns the English-language text
  1.3379 +** that describes the [result code], as UTF-8.
  1.3380 +** ^(Memory to hold the error message string is managed internally
  1.3381 +** and must not be freed by the application)^.
  1.3382 +**
  1.3383 +** When the serialized [threading mode] is in use, it might be the
  1.3384 +** case that a second error occurs on a separate thread in between
  1.3385 +** the time of the first error and the call to these interfaces.
  1.3386 +** When that happens, the second error will be reported since these
  1.3387 +** interfaces always report the most recent result.  To avoid
  1.3388 +** this, each thread can obtain exclusive use of the [database connection] D
  1.3389 +** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
  1.3390 +** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
  1.3391 +** all calls to the interfaces listed here are completed.
  1.3392 +**
  1.3393 +** If an interface fails with SQLITE_MISUSE, that means the interface
  1.3394 +** was invoked incorrectly by the application.  In that case, the
  1.3395 +** error code and message may or may not be set.
  1.3396 +*/
  1.3397 +SQLITE_API int sqlite3_errcode(sqlite3 *db);
  1.3398 +SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
  1.3399 +SQLITE_API const char *sqlite3_errmsg(sqlite3*);
  1.3400 +SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
  1.3401 +SQLITE_API const char *sqlite3_errstr(int);
  1.3402 +
  1.3403 +/*
  1.3404 +** CAPI3REF: SQL Statement Object
  1.3405 +** KEYWORDS: {prepared statement} {prepared statements}
  1.3406 +**
  1.3407 +** An instance of this object represents a single SQL statement.
  1.3408 +** This object is variously known as a "prepared statement" or a
  1.3409 +** "compiled SQL statement" or simply as a "statement".
  1.3410 +**
  1.3411 +** The life of a statement object goes something like this:
  1.3412 +**
  1.3413 +** <ol>
  1.3414 +** <li> Create the object using [sqlite3_prepare_v2()] or a related
  1.3415 +**      function.
  1.3416 +** <li> Bind values to [host parameters] using the sqlite3_bind_*()
  1.3417 +**      interfaces.
  1.3418 +** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  1.3419 +** <li> Reset the statement using [sqlite3_reset()] then go back
  1.3420 +**      to step 2.  Do this zero or more times.
  1.3421 +** <li> Destroy the object using [sqlite3_finalize()].
  1.3422 +** </ol>
  1.3423 +**
  1.3424 +** Refer to documentation on individual methods above for additional
  1.3425 +** information.
  1.3426 +*/
  1.3427 +typedef struct sqlite3_stmt sqlite3_stmt;
  1.3428 +
  1.3429 +/*
  1.3430 +** CAPI3REF: Run-time Limits
  1.3431 +**
  1.3432 +** ^(This interface allows the size of various constructs to be limited
  1.3433 +** on a connection by connection basis.  The first parameter is the
  1.3434 +** [database connection] whose limit is to be set or queried.  The
  1.3435 +** second parameter is one of the [limit categories] that define a
  1.3436 +** class of constructs to be size limited.  The third parameter is the
  1.3437 +** new limit for that construct.)^
  1.3438 +**
  1.3439 +** ^If the new limit is a negative number, the limit is unchanged.
  1.3440 +** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a 
  1.3441 +** [limits | hard upper bound]
  1.3442 +** set at compile-time by a C preprocessor macro called
  1.3443 +** [limits | SQLITE_MAX_<i>NAME</i>].
  1.3444 +** (The "_LIMIT_" in the name is changed to "_MAX_".))^
  1.3445 +** ^Attempts to increase a limit above its hard upper bound are
  1.3446 +** silently truncated to the hard upper bound.
  1.3447 +**
  1.3448 +** ^Regardless of whether or not the limit was changed, the 
  1.3449 +** [sqlite3_limit()] interface returns the prior value of the limit.
  1.3450 +** ^Hence, to find the current value of a limit without changing it,
  1.3451 +** simply invoke this interface with the third parameter set to -1.
  1.3452 +**
  1.3453 +** Run-time limits are intended for use in applications that manage
  1.3454 +** both their own internal database and also databases that are controlled
  1.3455 +** by untrusted external sources.  An example application might be a
  1.3456 +** web browser that has its own databases for storing history and
  1.3457 +** separate databases controlled by JavaScript applications downloaded
  1.3458 +** off the Internet.  The internal databases can be given the
  1.3459 +** large, default limits.  Databases managed by external sources can
  1.3460 +** be given much smaller limits designed to prevent a denial of service
  1.3461 +** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
  1.3462 +** interface to further control untrusted SQL.  The size of the database
  1.3463 +** created by an untrusted script can be contained using the
  1.3464 +** [max_page_count] [PRAGMA].
  1.3465 +**
  1.3466 +** New run-time limit categories may be added in future releases.
  1.3467 +*/
  1.3468 +SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
  1.3469 +
  1.3470 +/*
  1.3471 +** CAPI3REF: Run-Time Limit Categories
  1.3472 +** KEYWORDS: {limit category} {*limit categories}
  1.3473 +**
  1.3474 +** These constants define various performance limits
  1.3475 +** that can be lowered at run-time using [sqlite3_limit()].
  1.3476 +** The synopsis of the meanings of the various limits is shown below.
  1.3477 +** Additional information is available at [limits | Limits in SQLite].
  1.3478 +**
  1.3479 +** <dl>
  1.3480 +** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
  1.3481 +** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
  1.3482 +**
  1.3483 +** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
  1.3484 +** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
  1.3485 +**
  1.3486 +** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
  1.3487 +** <dd>The maximum number of columns in a table definition or in the
  1.3488 +** result set of a [SELECT] or the maximum number of columns in an index
  1.3489 +** or in an ORDER BY or GROUP BY clause.</dd>)^
  1.3490 +**
  1.3491 +** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
  1.3492 +** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  1.3493 +**
  1.3494 +** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  1.3495 +** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  1.3496 +**
  1.3497 +** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  1.3498 +** <dd>The maximum number of instructions in a virtual machine program
  1.3499 +** used to implement an SQL statement.  This limit is not currently
  1.3500 +** enforced, though that might be added in some future release of
  1.3501 +** SQLite.</dd>)^
  1.3502 +**
  1.3503 +** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  1.3504 +** <dd>The maximum number of arguments on a function.</dd>)^
  1.3505 +**
  1.3506 +** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  1.3507 +** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  1.3508 +**
  1.3509 +** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
  1.3510 +** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
  1.3511 +** <dd>The maximum length of the pattern argument to the [LIKE] or
  1.3512 +** [GLOB] operators.</dd>)^
  1.3513 +**
  1.3514 +** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
  1.3515 +** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
  1.3516 +** <dd>The maximum index number of any [parameter] in an SQL statement.)^
  1.3517 +**
  1.3518 +** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
  1.3519 +** <dd>The maximum depth of recursion for triggers.</dd>)^
  1.3520 +** </dl>
  1.3521 +*/
  1.3522 +#define SQLITE_LIMIT_LENGTH                    0
  1.3523 +#define SQLITE_LIMIT_SQL_LENGTH                1
  1.3524 +#define SQLITE_LIMIT_COLUMN                    2
  1.3525 +#define SQLITE_LIMIT_EXPR_DEPTH                3
  1.3526 +#define SQLITE_LIMIT_COMPOUND_SELECT           4
  1.3527 +#define SQLITE_LIMIT_VDBE_OP                   5
  1.3528 +#define SQLITE_LIMIT_FUNCTION_ARG              6
  1.3529 +#define SQLITE_LIMIT_ATTACHED                  7
  1.3530 +#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
  1.3531 +#define SQLITE_LIMIT_VARIABLE_NUMBER           9
  1.3532 +#define SQLITE_LIMIT_TRIGGER_DEPTH            10
  1.3533 +
  1.3534 +/*
  1.3535 +** CAPI3REF: Compiling An SQL Statement
  1.3536 +** KEYWORDS: {SQL statement compiler}
  1.3537 +**
  1.3538 +** To execute an SQL query, it must first be compiled into a byte-code
  1.3539 +** program using one of these routines.
  1.3540 +**
  1.3541 +** The first argument, "db", is a [database connection] obtained from a
  1.3542 +** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  1.3543 +** [sqlite3_open16()].  The database connection must not have been closed.
  1.3544 +**
  1.3545 +** The second argument, "zSql", is the statement to be compiled, encoded
  1.3546 +** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
  1.3547 +** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
  1.3548 +** use UTF-16.
  1.3549 +**
  1.3550 +** ^If the nByte argument is less than zero, then zSql is read up to the
  1.3551 +** first zero terminator. ^If nByte is non-negative, then it is the maximum
  1.3552 +** number of  bytes read from zSql.  ^When nByte is non-negative, the
  1.3553 +** zSql string ends at either the first '\000' or '\u0000' character or
  1.3554 +** the nByte-th byte, whichever comes first. If the caller knows
  1.3555 +** that the supplied string is nul-terminated, then there is a small
  1.3556 +** performance advantage to be gained by passing an nByte parameter that
  1.3557 +** is equal to the number of bytes in the input string <i>including</i>
  1.3558 +** the nul-terminator bytes as this saves SQLite from having to
  1.3559 +** make a copy of the input string.
  1.3560 +**
  1.3561 +** ^If pzTail is not NULL then *pzTail is made to point to the first byte
  1.3562 +** past the end of the first SQL statement in zSql.  These routines only
  1.3563 +** compile the first statement in zSql, so *pzTail is left pointing to
  1.3564 +** what remains uncompiled.
  1.3565 +**
  1.3566 +** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
  1.3567 +** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
  1.3568 +** to NULL.  ^If the input text contains no SQL (if the input is an empty
  1.3569 +** string or a comment) then *ppStmt is set to NULL.
  1.3570 +** The calling procedure is responsible for deleting the compiled
  1.3571 +** SQL statement using [sqlite3_finalize()] after it has finished with it.
  1.3572 +** ppStmt may not be NULL.
  1.3573 +**
  1.3574 +** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
  1.3575 +** otherwise an [error code] is returned.
  1.3576 +**
  1.3577 +** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
  1.3578 +** recommended for all new programs. The two older interfaces are retained
  1.3579 +** for backwards compatibility, but their use is discouraged.
  1.3580 +** ^In the "v2" interfaces, the prepared statement
  1.3581 +** that is returned (the [sqlite3_stmt] object) contains a copy of the
  1.3582 +** original SQL text. This causes the [sqlite3_step()] interface to
  1.3583 +** behave differently in three ways:
  1.3584 +**
  1.3585 +** <ol>
  1.3586 +** <li>
  1.3587 +** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
  1.3588 +** always used to do, [sqlite3_step()] will automatically recompile the SQL
  1.3589 +** statement and try to run it again.
  1.3590 +** </li>
  1.3591 +**
  1.3592 +** <li>
  1.3593 +** ^When an error occurs, [sqlite3_step()] will return one of the detailed
  1.3594 +** [error codes] or [extended error codes].  ^The legacy behavior was that
  1.3595 +** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
  1.3596 +** and the application would have to make a second call to [sqlite3_reset()]
  1.3597 +** in order to find the underlying cause of the problem. With the "v2" prepare
  1.3598 +** interfaces, the underlying reason for the error is returned immediately.
  1.3599 +** </li>
  1.3600 +**
  1.3601 +** <li>
  1.3602 +** ^If the specific value bound to [parameter | host parameter] in the 
  1.3603 +** WHERE clause might influence the choice of query plan for a statement,
  1.3604 +** then the statement will be automatically recompiled, as if there had been 
  1.3605 +** a schema change, on the first  [sqlite3_step()] call following any change
  1.3606 +** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  1.3607 +** ^The specific value of WHERE-clause [parameter] might influence the 
  1.3608 +** choice of query plan if the parameter is the left-hand side of a [LIKE]
  1.3609 +** or [GLOB] operator or if the parameter is compared to an indexed column
  1.3610 +** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  1.3611 +** the 
  1.3612 +** </li>
  1.3613 +** </ol>
  1.3614 +*/
  1.3615 +SQLITE_API int sqlite3_prepare(
  1.3616 +  sqlite3 *db,            /* Database handle */
  1.3617 +  const char *zSql,       /* SQL statement, UTF-8 encoded */
  1.3618 +  int nByte,              /* Maximum length of zSql in bytes. */
  1.3619 +  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  1.3620 +  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
  1.3621 +);
  1.3622 +SQLITE_API int sqlite3_prepare_v2(
  1.3623 +  sqlite3 *db,            /* Database handle */
  1.3624 +  const char *zSql,       /* SQL statement, UTF-8 encoded */
  1.3625 +  int nByte,              /* Maximum length of zSql in bytes. */
  1.3626 +  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  1.3627 +  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
  1.3628 +);
  1.3629 +SQLITE_API int sqlite3_prepare16(
  1.3630 +  sqlite3 *db,            /* Database handle */
  1.3631 +  const void *zSql,       /* SQL statement, UTF-16 encoded */
  1.3632 +  int nByte,              /* Maximum length of zSql in bytes. */
  1.3633 +  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  1.3634 +  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  1.3635 +);
  1.3636 +SQLITE_API int sqlite3_prepare16_v2(
  1.3637 +  sqlite3 *db,            /* Database handle */
  1.3638 +  const void *zSql,       /* SQL statement, UTF-16 encoded */
  1.3639 +  int nByte,              /* Maximum length of zSql in bytes. */
  1.3640 +  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  1.3641 +  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  1.3642 +);
  1.3643 +
  1.3644 +/*
  1.3645 +** CAPI3REF: Retrieving Statement SQL
  1.3646 +**
  1.3647 +** ^This interface can be used to retrieve a saved copy of the original
  1.3648 +** SQL text used to create a [prepared statement] if that statement was
  1.3649 +** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  1.3650 +*/
  1.3651 +SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  1.3652 +
  1.3653 +/*
  1.3654 +** CAPI3REF: Determine If An SQL Statement Writes The Database
  1.3655 +**
  1.3656 +** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  1.3657 +** and only if the [prepared statement] X makes no direct changes to
  1.3658 +** the content of the database file.
  1.3659 +**
  1.3660 +** Note that [application-defined SQL functions] or
  1.3661 +** [virtual tables] might change the database indirectly as a side effect.  
  1.3662 +** ^(For example, if an application defines a function "eval()" that 
  1.3663 +** calls [sqlite3_exec()], then the following SQL statement would
  1.3664 +** change the database file through side-effects:
  1.3665 +**
  1.3666 +** <blockquote><pre>
  1.3667 +**    SELECT eval('DELETE FROM t1') FROM t2;
  1.3668 +** </pre></blockquote>
  1.3669 +**
  1.3670 +** But because the [SELECT] statement does not change the database file
  1.3671 +** directly, sqlite3_stmt_readonly() would still return true.)^
  1.3672 +**
  1.3673 +** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
  1.3674 +** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
  1.3675 +** since the statements themselves do not actually modify the database but
  1.3676 +** rather they control the timing of when other statements modify the 
  1.3677 +** database.  ^The [ATTACH] and [DETACH] statements also cause
  1.3678 +** sqlite3_stmt_readonly() to return true since, while those statements
  1.3679 +** change the configuration of a database connection, they do not make 
  1.3680 +** changes to the content of the database files on disk.
  1.3681 +*/
  1.3682 +SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  1.3683 +
  1.3684 +/*
  1.3685 +** CAPI3REF: Determine If A Prepared Statement Has Been Reset
  1.3686 +**
  1.3687 +** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  1.3688 +** [prepared statement] S has been stepped at least once using 
  1.3689 +** [sqlite3_step(S)] but has not run to completion and/or has not 
  1.3690 +** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
  1.3691 +** interface returns false if S is a NULL pointer.  If S is not a 
  1.3692 +** NULL pointer and is not a pointer to a valid [prepared statement]
  1.3693 +** object, then the behavior is undefined and probably undesirable.
  1.3694 +**
  1.3695 +** This interface can be used in combination [sqlite3_next_stmt()]
  1.3696 +** to locate all prepared statements associated with a database 
  1.3697 +** connection that are in need of being reset.  This can be used,
  1.3698 +** for example, in diagnostic routines to search for prepared 
  1.3699 +** statements that are holding a transaction open.
  1.3700 +*/
  1.3701 +SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
  1.3702 +
  1.3703 +/*
  1.3704 +** CAPI3REF: Dynamically Typed Value Object
  1.3705 +** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
  1.3706 +**
  1.3707 +** SQLite uses the sqlite3_value object to represent all values
  1.3708 +** that can be stored in a database table. SQLite uses dynamic typing
  1.3709 +** for the values it stores.  ^Values stored in sqlite3_value objects
  1.3710 +** can be integers, floating point values, strings, BLOBs, or NULL.
  1.3711 +**
  1.3712 +** An sqlite3_value object may be either "protected" or "unprotected".
  1.3713 +** Some interfaces require a protected sqlite3_value.  Other interfaces
  1.3714 +** will accept either a protected or an unprotected sqlite3_value.
  1.3715 +** Every interface that accepts sqlite3_value arguments specifies
  1.3716 +** whether or not it requires a protected sqlite3_value.
  1.3717 +**
  1.3718 +** The terms "protected" and "unprotected" refer to whether or not
  1.3719 +** a mutex is held.  An internal mutex is held for a protected
  1.3720 +** sqlite3_value object but no mutex is held for an unprotected
  1.3721 +** sqlite3_value object.  If SQLite is compiled to be single-threaded
  1.3722 +** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
  1.3723 +** or if SQLite is run in one of reduced mutex modes 
  1.3724 +** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
  1.3725 +** then there is no distinction between protected and unprotected
  1.3726 +** sqlite3_value objects and they can be used interchangeably.  However,
  1.3727 +** for maximum code portability it is recommended that applications
  1.3728 +** still make the distinction between protected and unprotected
  1.3729 +** sqlite3_value objects even when not strictly required.
  1.3730 +**
  1.3731 +** ^The sqlite3_value objects that are passed as parameters into the
  1.3732 +** implementation of [application-defined SQL functions] are protected.
  1.3733 +** ^The sqlite3_value object returned by
  1.3734 +** [sqlite3_column_value()] is unprotected.
  1.3735 +** Unprotected sqlite3_value objects may only be used with
  1.3736 +** [sqlite3_result_value()] and [sqlite3_bind_value()].
  1.3737 +** The [sqlite3_value_blob | sqlite3_value_type()] family of
  1.3738 +** interfaces require protected sqlite3_value objects.
  1.3739 +*/
  1.3740 +typedef struct Mem sqlite3_value;
  1.3741 +
  1.3742 +/*
  1.3743 +** CAPI3REF: SQL Function Context Object
  1.3744 +**
  1.3745 +** The context in which an SQL function executes is stored in an
  1.3746 +** sqlite3_context object.  ^A pointer to an sqlite3_context object
  1.3747 +** is always first parameter to [application-defined SQL functions].
  1.3748 +** The application-defined SQL function implementation will pass this
  1.3749 +** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
  1.3750 +** [sqlite3_aggregate_context()], [sqlite3_user_data()],
  1.3751 +** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
  1.3752 +** and/or [sqlite3_set_auxdata()].
  1.3753 +*/
  1.3754 +typedef struct sqlite3_context sqlite3_context;
  1.3755 +
  1.3756 +/*
  1.3757 +** CAPI3REF: Binding Values To Prepared Statements
  1.3758 +** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  1.3759 +** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
  1.3760 +**
  1.3761 +** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  1.3762 +** literals may be replaced by a [parameter] that matches one of following
  1.3763 +** templates:
  1.3764 +**
  1.3765 +** <ul>
  1.3766 +** <li>  ?
  1.3767 +** <li>  ?NNN
  1.3768 +** <li>  :VVV
  1.3769 +** <li>  @VVV
  1.3770 +** <li>  $VVV
  1.3771 +** </ul>
  1.3772 +**
  1.3773 +** In the templates above, NNN represents an integer literal,
  1.3774 +** and VVV represents an alphanumeric identifier.)^  ^The values of these
  1.3775 +** parameters (also called "host parameter names" or "SQL parameters")
  1.3776 +** can be set using the sqlite3_bind_*() routines defined here.
  1.3777 +**
  1.3778 +** ^The first argument to the sqlite3_bind_*() routines is always
  1.3779 +** a pointer to the [sqlite3_stmt] object returned from
  1.3780 +** [sqlite3_prepare_v2()] or its variants.
  1.3781 +**
  1.3782 +** ^The second argument is the index of the SQL parameter to be set.
  1.3783 +** ^The leftmost SQL parameter has an index of 1.  ^When the same named
  1.3784 +** SQL parameter is used more than once, second and subsequent
  1.3785 +** occurrences have the same index as the first occurrence.
  1.3786 +** ^The index for named parameters can be looked up using the
  1.3787 +** [sqlite3_bind_parameter_index()] API if desired.  ^The index
  1.3788 +** for "?NNN" parameters is the value of NNN.
  1.3789 +** ^The NNN value must be between 1 and the [sqlite3_limit()]
  1.3790 +** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
  1.3791 +**
  1.3792 +** ^The third argument is the value to bind to the parameter.
  1.3793 +**
  1.3794 +** ^(In those routines that have a fourth argument, its value is the
  1.3795 +** number of bytes in the parameter.  To be clear: the value is the
  1.3796 +** number of <u>bytes</u> in the value, not the number of characters.)^
  1.3797 +** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
  1.3798 +** is negative, then the length of the string is
  1.3799 +** the number of bytes up to the first zero terminator.
  1.3800 +** If the fourth parameter to sqlite3_bind_blob() is negative, then
  1.3801 +** the behavior is undefined.
  1.3802 +** If a non-negative fourth parameter is provided to sqlite3_bind_text()
  1.3803 +** or sqlite3_bind_text16() then that parameter must be the byte offset
  1.3804 +** where the NUL terminator would occur assuming the string were NUL
  1.3805 +** terminated.  If any NUL characters occur at byte offsets less than 
  1.3806 +** the value of the fourth parameter then the resulting string value will
  1.3807 +** contain embedded NULs.  The result of expressions involving strings
  1.3808 +** with embedded NULs is undefined.
  1.3809 +**
  1.3810 +** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  1.3811 +** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  1.3812 +** string after SQLite has finished with it.  ^The destructor is called
  1.3813 +** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
  1.3814 +** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
  1.3815 +** ^If the fifth argument is
  1.3816 +** the special value [SQLITE_STATIC], then SQLite assumes that the
  1.3817 +** information is in static, unmanaged space and does not need to be freed.
  1.3818 +** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  1.3819 +** SQLite makes its own private copy of the data immediately, before
  1.3820 +** the sqlite3_bind_*() routine returns.
  1.3821 +**
  1.3822 +** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  1.3823 +** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
  1.3824 +** (just an integer to hold its size) while it is being processed.
  1.3825 +** Zeroblobs are intended to serve as placeholders for BLOBs whose
  1.3826 +** content is later written using
  1.3827 +** [sqlite3_blob_open | incremental BLOB I/O] routines.
  1.3828 +** ^A negative value for the zeroblob results in a zero-length BLOB.
  1.3829 +**
  1.3830 +** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
  1.3831 +** for the [prepared statement] or with a prepared statement for which
  1.3832 +** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
  1.3833 +** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
  1.3834 +** routine is passed a [prepared statement] that has been finalized, the
  1.3835 +** result is undefined and probably harmful.
  1.3836 +**
  1.3837 +** ^Bindings are not cleared by the [sqlite3_reset()] routine.
  1.3838 +** ^Unbound parameters are interpreted as NULL.
  1.3839 +**
  1.3840 +** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
  1.3841 +** [error code] if anything goes wrong.
  1.3842 +** ^[SQLITE_RANGE] is returned if the parameter
  1.3843 +** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
  1.3844 +**
  1.3845 +** See also: [sqlite3_bind_parameter_count()],
  1.3846 +** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
  1.3847 +*/
  1.3848 +SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
  1.3849 +SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
  1.3850 +SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
  1.3851 +SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  1.3852 +SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
  1.3853 +SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
  1.3854 +SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
  1.3855 +SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  1.3856 +SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  1.3857 +
  1.3858 +/*
  1.3859 +** CAPI3REF: Number Of SQL Parameters
  1.3860 +**
  1.3861 +** ^This routine can be used to find the number of [SQL parameters]
  1.3862 +** in a [prepared statement].  SQL parameters are tokens of the
  1.3863 +** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  1.3864 +** placeholders for values that are [sqlite3_bind_blob | bound]
  1.3865 +** to the parameters at a later time.
  1.3866 +**
  1.3867 +** ^(This routine actually returns the index of the largest (rightmost)
  1.3868 +** parameter. For all forms except ?NNN, this will correspond to the
  1.3869 +** number of unique parameters.  If parameters of the ?NNN form are used,
  1.3870 +** there may be gaps in the list.)^
  1.3871 +**
  1.3872 +** See also: [sqlite3_bind_blob|sqlite3_bind()],
  1.3873 +** [sqlite3_bind_parameter_name()], and
  1.3874 +** [sqlite3_bind_parameter_index()].
  1.3875 +*/
  1.3876 +SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
  1.3877 +
  1.3878 +/*
  1.3879 +** CAPI3REF: Name Of A Host Parameter
  1.3880 +**
  1.3881 +** ^The sqlite3_bind_parameter_name(P,N) interface returns
  1.3882 +** the name of the N-th [SQL parameter] in the [prepared statement] P.
  1.3883 +** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  1.3884 +** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  1.3885 +** respectively.
  1.3886 +** In other words, the initial ":" or "$" or "@" or "?"
  1.3887 +** is included as part of the name.)^
  1.3888 +** ^Parameters of the form "?" without a following integer have no name
  1.3889 +** and are referred to as "nameless" or "anonymous parameters".
  1.3890 +**
  1.3891 +** ^The first host parameter has an index of 1, not 0.
  1.3892 +**
  1.3893 +** ^If the value N is out of range or if the N-th parameter is
  1.3894 +** nameless, then NULL is returned.  ^The returned string is
  1.3895 +** always in UTF-8 encoding even if the named parameter was
  1.3896 +** originally specified as UTF-16 in [sqlite3_prepare16()] or
  1.3897 +** [sqlite3_prepare16_v2()].
  1.3898 +**
  1.3899 +** See also: [sqlite3_bind_blob|sqlite3_bind()],
  1.3900 +** [sqlite3_bind_parameter_count()], and
  1.3901 +** [sqlite3_bind_parameter_index()].
  1.3902 +*/
  1.3903 +SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  1.3904 +
  1.3905 +/*
  1.3906 +** CAPI3REF: Index Of A Parameter With A Given Name
  1.3907 +**
  1.3908 +** ^Return the index of an SQL parameter given its name.  ^The
  1.3909 +** index value returned is suitable for use as the second
  1.3910 +** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
  1.3911 +** is returned if no matching parameter is found.  ^The parameter
  1.3912 +** name must be given in UTF-8 even if the original statement
  1.3913 +** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
  1.3914 +**
  1.3915 +** See also: [sqlite3_bind_blob|sqlite3_bind()],
  1.3916 +** [sqlite3_bind_parameter_count()], and
  1.3917 +** [sqlite3_bind_parameter_index()].
  1.3918 +*/
  1.3919 +SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  1.3920 +
  1.3921 +/*
  1.3922 +** CAPI3REF: Reset All Bindings On A Prepared Statement
  1.3923 +**
  1.3924 +** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  1.3925 +** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  1.3926 +** ^Use this routine to reset all host parameters to NULL.
  1.3927 +*/
  1.3928 +SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
  1.3929 +
  1.3930 +/*
  1.3931 +** CAPI3REF: Number Of Columns In A Result Set
  1.3932 +**
  1.3933 +** ^Return the number of columns in the result set returned by the
  1.3934 +** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  1.3935 +** statement that does not return data (for example an [UPDATE]).
  1.3936 +**
  1.3937 +** See also: [sqlite3_data_count()]
  1.3938 +*/
  1.3939 +SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
  1.3940 +
  1.3941 +/*
  1.3942 +** CAPI3REF: Column Names In A Result Set
  1.3943 +**
  1.3944 +** ^These routines return the name assigned to a particular column
  1.3945 +** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
  1.3946 +** interface returns a pointer to a zero-terminated UTF-8 string
  1.3947 +** and sqlite3_column_name16() returns a pointer to a zero-terminated
  1.3948 +** UTF-16 string.  ^The first parameter is the [prepared statement]
  1.3949 +** that implements the [SELECT] statement. ^The second parameter is the
  1.3950 +** column number.  ^The leftmost column is number 0.
  1.3951 +**
  1.3952 +** ^The returned string pointer is valid until either the [prepared statement]
  1.3953 +** is destroyed by [sqlite3_finalize()] or until the statement is automatically
  1.3954 +** reprepared by the first call to [sqlite3_step()] for a particular run
  1.3955 +** or until the next call to
  1.3956 +** sqlite3_column_name() or sqlite3_column_name16() on the same column.
  1.3957 +**
  1.3958 +** ^If sqlite3_malloc() fails during the processing of either routine
  1.3959 +** (for example during a conversion from UTF-8 to UTF-16) then a
  1.3960 +** NULL pointer is returned.
  1.3961 +**
  1.3962 +** ^The name of a result column is the value of the "AS" clause for
  1.3963 +** that column, if there is an AS clause.  If there is no AS clause
  1.3964 +** then the name of the column is unspecified and may change from
  1.3965 +** one release of SQLite to the next.
  1.3966 +*/
  1.3967 +SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
  1.3968 +SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
  1.3969 +
  1.3970 +/*
  1.3971 +** CAPI3REF: Source Of Data In A Query Result
  1.3972 +**
  1.3973 +** ^These routines provide a means to determine the database, table, and
  1.3974 +** table column that is the origin of a particular result column in
  1.3975 +** [SELECT] statement.
  1.3976 +** ^The name of the database or table or column can be returned as
  1.3977 +** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
  1.3978 +** the database name, the _table_ routines return the table name, and
  1.3979 +** the origin_ routines return the column name.
  1.3980 +** ^The returned string is valid until the [prepared statement] is destroyed
  1.3981 +** using [sqlite3_finalize()] or until the statement is automatically
  1.3982 +** reprepared by the first call to [sqlite3_step()] for a particular run
  1.3983 +** or until the same information is requested
  1.3984 +** again in a different encoding.
  1.3985 +**
  1.3986 +** ^The names returned are the original un-aliased names of the
  1.3987 +** database, table, and column.
  1.3988 +**
  1.3989 +** ^The first argument to these interfaces is a [prepared statement].
  1.3990 +** ^These functions return information about the Nth result column returned by
  1.3991 +** the statement, where N is the second function argument.
  1.3992 +** ^The left-most column is column 0 for these routines.
  1.3993 +**
  1.3994 +** ^If the Nth column returned by the statement is an expression or
  1.3995 +** subquery and is not a column value, then all of these functions return
  1.3996 +** NULL.  ^These routine might also return NULL if a memory allocation error
  1.3997 +** occurs.  ^Otherwise, they return the name of the attached database, table,
  1.3998 +** or column that query result column was extracted from.
  1.3999 +**
  1.4000 +** ^As with all other SQLite APIs, those whose names end with "16" return
  1.4001 +** UTF-16 encoded strings and the other functions return UTF-8.
  1.4002 +**
  1.4003 +** ^These APIs are only available if the library was compiled with the
  1.4004 +** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
  1.4005 +**
  1.4006 +** If two or more threads call one or more of these routines against the same
  1.4007 +** prepared statement and column at the same time then the results are
  1.4008 +** undefined.
  1.4009 +**
  1.4010 +** If two or more threads call one or more
  1.4011 +** [sqlite3_column_database_name | column metadata interfaces]
  1.4012 +** for the same [prepared statement] and result column
  1.4013 +** at the same time then the results are undefined.
  1.4014 +*/
  1.4015 +SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
  1.4016 +SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
  1.4017 +SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
  1.4018 +SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
  1.4019 +SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
  1.4020 +SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
  1.4021 +
  1.4022 +/*
  1.4023 +** CAPI3REF: Declared Datatype Of A Query Result
  1.4024 +**
  1.4025 +** ^(The first parameter is a [prepared statement].
  1.4026 +** If this statement is a [SELECT] statement and the Nth column of the
  1.4027 +** returned result set of that [SELECT] is a table column (not an
  1.4028 +** expression or subquery) then the declared type of the table
  1.4029 +** column is returned.)^  ^If the Nth column of the result set is an
  1.4030 +** expression or subquery, then a NULL pointer is returned.
  1.4031 +** ^The returned string is always UTF-8 encoded.
  1.4032 +**
  1.4033 +** ^(For example, given the database schema:
  1.4034 +**
  1.4035 +** CREATE TABLE t1(c1 VARIANT);
  1.4036 +**
  1.4037 +** and the following statement to be compiled:
  1.4038 +**
  1.4039 +** SELECT c1 + 1, c1 FROM t1;
  1.4040 +**
  1.4041 +** this routine would return the string "VARIANT" for the second result
  1.4042 +** column (i==1), and a NULL pointer for the first result column (i==0).)^
  1.4043 +**
  1.4044 +** ^SQLite uses dynamic run-time typing.  ^So just because a column
  1.4045 +** is declared to contain a particular type does not mean that the
  1.4046 +** data stored in that column is of the declared type.  SQLite is
  1.4047 +** strongly typed, but the typing is dynamic not static.  ^Type
  1.4048 +** is associated with individual values, not with the containers
  1.4049 +** used to hold those values.
  1.4050 +*/
  1.4051 +SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
  1.4052 +SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
  1.4053 +
  1.4054 +/*
  1.4055 +** CAPI3REF: Evaluate An SQL Statement
  1.4056 +**
  1.4057 +** After a [prepared statement] has been prepared using either
  1.4058 +** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  1.4059 +** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  1.4060 +** must be called one or more times to evaluate the statement.
  1.4061 +**
  1.4062 +** The details of the behavior of the sqlite3_step() interface depend
  1.4063 +** on whether the statement was prepared using the newer "v2" interface
  1.4064 +** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
  1.4065 +** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
  1.4066 +** new "v2" interface is recommended for new applications but the legacy
  1.4067 +** interface will continue to be supported.
  1.4068 +**
  1.4069 +** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
  1.4070 +** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
  1.4071 +** ^With the "v2" interface, any of the other [result codes] or
  1.4072 +** [extended result codes] might be returned as well.
  1.4073 +**
  1.4074 +** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
  1.4075 +** database locks it needs to do its job.  ^If the statement is a [COMMIT]
  1.4076 +** or occurs outside of an explicit transaction, then you can retry the
  1.4077 +** statement.  If the statement is not a [COMMIT] and occurs within an
  1.4078 +** explicit transaction then you should rollback the transaction before
  1.4079 +** continuing.
  1.4080 +**
  1.4081 +** ^[SQLITE_DONE] means that the statement has finished executing
  1.4082 +** successfully.  sqlite3_step() should not be called again on this virtual
  1.4083 +** machine without first calling [sqlite3_reset()] to reset the virtual
  1.4084 +** machine back to its initial state.
  1.4085 +**
  1.4086 +** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
  1.4087 +** is returned each time a new row of data is ready for processing by the
  1.4088 +** caller. The values may be accessed using the [column access functions].
  1.4089 +** sqlite3_step() is called again to retrieve the next row of data.
  1.4090 +**
  1.4091 +** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
  1.4092 +** violation) has occurred.  sqlite3_step() should not be called again on
  1.4093 +** the VM. More information may be found by calling [sqlite3_errmsg()].
  1.4094 +** ^With the legacy interface, a more specific error code (for example,
  1.4095 +** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
  1.4096 +** can be obtained by calling [sqlite3_reset()] on the
  1.4097 +** [prepared statement].  ^In the "v2" interface,
  1.4098 +** the more specific error code is returned directly by sqlite3_step().
  1.4099 +**
  1.4100 +** [SQLITE_MISUSE] means that the this routine was called inappropriately.
  1.4101 +** Perhaps it was called on a [prepared statement] that has
  1.4102 +** already been [sqlite3_finalize | finalized] or on one that had
  1.4103 +** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
  1.4104 +** be the case that the same database connection is being used by two or
  1.4105 +** more threads at the same moment in time.
  1.4106 +**
  1.4107 +** For all versions of SQLite up to and including 3.6.23.1, a call to
  1.4108 +** [sqlite3_reset()] was required after sqlite3_step() returned anything
  1.4109 +** other than [SQLITE_ROW] before any subsequent invocation of
  1.4110 +** sqlite3_step().  Failure to reset the prepared statement using 
  1.4111 +** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
  1.4112 +** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
  1.4113 +** calling [sqlite3_reset()] automatically in this circumstance rather
  1.4114 +** than returning [SQLITE_MISUSE].  This is not considered a compatibility
  1.4115 +** break because any application that ever receives an SQLITE_MISUSE error
  1.4116 +** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
  1.4117 +** can be used to restore the legacy behavior.
  1.4118 +**
  1.4119 +** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
  1.4120 +** API always returns a generic error code, [SQLITE_ERROR], following any
  1.4121 +** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
  1.4122 +** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
  1.4123 +** specific [error codes] that better describes the error.
  1.4124 +** We admit that this is a goofy design.  The problem has been fixed
  1.4125 +** with the "v2" interface.  If you prepare all of your SQL statements
  1.4126 +** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
  1.4127 +** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
  1.4128 +** then the more specific [error codes] are returned directly
  1.4129 +** by sqlite3_step().  The use of the "v2" interface is recommended.
  1.4130 +*/
  1.4131 +SQLITE_API int sqlite3_step(sqlite3_stmt*);
  1.4132 +
  1.4133 +/*
  1.4134 +** CAPI3REF: Number of columns in a result set
  1.4135 +**
  1.4136 +** ^The sqlite3_data_count(P) interface returns the number of columns in the
  1.4137 +** current row of the result set of [prepared statement] P.
  1.4138 +** ^If prepared statement P does not have results ready to return
  1.4139 +** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  1.4140 +** interfaces) then sqlite3_data_count(P) returns 0.
  1.4141 +** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
  1.4142 +** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
  1.4143 +** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
  1.4144 +** will return non-zero if previous call to [sqlite3_step](P) returned
  1.4145 +** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
  1.4146 +** where it always returns zero since each step of that multi-step
  1.4147 +** pragma returns 0 columns of data.
  1.4148 +**
  1.4149 +** See also: [sqlite3_column_count()]
  1.4150 +*/
  1.4151 +SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
  1.4152 +
  1.4153 +/*
  1.4154 +** CAPI3REF: Fundamental Datatypes
  1.4155 +** KEYWORDS: SQLITE_TEXT
  1.4156 +**
  1.4157 +** ^(Every value in SQLite has one of five fundamental datatypes:
  1.4158 +**
  1.4159 +** <ul>
  1.4160 +** <li> 64-bit signed integer
  1.4161 +** <li> 64-bit IEEE floating point number
  1.4162 +** <li> string
  1.4163 +** <li> BLOB
  1.4164 +** <li> NULL
  1.4165 +** </ul>)^
  1.4166 +**
  1.4167 +** These constants are codes for each of those types.
  1.4168 +**
  1.4169 +** Note that the SQLITE_TEXT constant was also used in SQLite version 2
  1.4170 +** for a completely different meaning.  Software that links against both
  1.4171 +** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
  1.4172 +** SQLITE_TEXT.
  1.4173 +*/
  1.4174 +#define SQLITE_INTEGER  1
  1.4175 +#define SQLITE_FLOAT    2
  1.4176 +#define SQLITE_BLOB     4
  1.4177 +#define SQLITE_NULL     5
  1.4178 +#ifdef SQLITE_TEXT
  1.4179 +# undef SQLITE_TEXT
  1.4180 +#else
  1.4181 +# define SQLITE_TEXT     3
  1.4182 +#endif
  1.4183 +#define SQLITE3_TEXT     3
  1.4184 +
  1.4185 +/*
  1.4186 +** CAPI3REF: Result Values From A Query
  1.4187 +** KEYWORDS: {column access functions}
  1.4188 +**
  1.4189 +** These routines form the "result set" interface.
  1.4190 +**
  1.4191 +** ^These routines return information about a single column of the current
  1.4192 +** result row of a query.  ^In every case the first argument is a pointer
  1.4193 +** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  1.4194 +** that was returned from [sqlite3_prepare_v2()] or one of its variants)
  1.4195 +** and the second argument is the index of the column for which information
  1.4196 +** should be returned. ^The leftmost column of the result set has the index 0.
  1.4197 +** ^The number of columns in the result can be determined using
  1.4198 +** [sqlite3_column_count()].
  1.4199 +**
  1.4200 +** If the SQL statement does not currently point to a valid row, or if the
  1.4201 +** column index is out of range, the result is undefined.
  1.4202 +** These routines may only be called when the most recent call to
  1.4203 +** [sqlite3_step()] has returned [SQLITE_ROW] and neither
  1.4204 +** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
  1.4205 +** If any of these routines are called after [sqlite3_reset()] or
  1.4206 +** [sqlite3_finalize()] or after [sqlite3_step()] has returned
  1.4207 +** something other than [SQLITE_ROW], the results are undefined.
  1.4208 +** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
  1.4209 +** are called from a different thread while any of these routines
  1.4210 +** are pending, then the results are undefined.
  1.4211 +**
  1.4212 +** ^The sqlite3_column_type() routine returns the
  1.4213 +** [SQLITE_INTEGER | datatype code] for the initial data type
  1.4214 +** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
  1.4215 +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
  1.4216 +** returned by sqlite3_column_type() is only meaningful if no type
  1.4217 +** conversions have occurred as described below.  After a type conversion,
  1.4218 +** the value returned by sqlite3_column_type() is undefined.  Future
  1.4219 +** versions of SQLite may change the behavior of sqlite3_column_type()
  1.4220 +** following a type conversion.
  1.4221 +**
  1.4222 +** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
  1.4223 +** routine returns the number of bytes in that BLOB or string.
  1.4224 +** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
  1.4225 +** the string to UTF-8 and then returns the number of bytes.
  1.4226 +** ^If the result is a numeric value then sqlite3_column_bytes() uses
  1.4227 +** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
  1.4228 +** the number of bytes in that string.
  1.4229 +** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
  1.4230 +**
  1.4231 +** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
  1.4232 +** routine returns the number of bytes in that BLOB or string.
  1.4233 +** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
  1.4234 +** the string to UTF-16 and then returns the number of bytes.
  1.4235 +** ^If the result is a numeric value then sqlite3_column_bytes16() uses
  1.4236 +** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
  1.4237 +** the number of bytes in that string.
  1.4238 +** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
  1.4239 +**
  1.4240 +** ^The values returned by [sqlite3_column_bytes()] and 
  1.4241 +** [sqlite3_column_bytes16()] do not include the zero terminators at the end
  1.4242 +** of the string.  ^For clarity: the values returned by
  1.4243 +** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
  1.4244 +** bytes in the string, not the number of characters.
  1.4245 +**
  1.4246 +** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
  1.4247 +** even empty strings, are always zero-terminated.  ^The return
  1.4248 +** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
  1.4249 +**
  1.4250 +** ^The object returned by [sqlite3_column_value()] is an
  1.4251 +** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
  1.4252 +** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
  1.4253 +** If the [unprotected sqlite3_value] object returned by
  1.4254 +** [sqlite3_column_value()] is used in any other way, including calls
  1.4255 +** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
  1.4256 +** or [sqlite3_value_bytes()], then the behavior is undefined.
  1.4257 +**
  1.4258 +** These routines attempt to convert the value where appropriate.  ^For
  1.4259 +** example, if the internal representation is FLOAT and a text result
  1.4260 +** is requested, [sqlite3_snprintf()] is used internally to perform the
  1.4261 +** conversion automatically.  ^(The following table details the conversions
  1.4262 +** that are applied:
  1.4263 +**
  1.4264 +** <blockquote>
  1.4265 +** <table border="1">
  1.4266 +** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
  1.4267 +**
  1.4268 +** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
  1.4269 +** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
  1.4270 +** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
  1.4271 +** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
  1.4272 +** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
  1.4273 +** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
  1.4274 +** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
  1.4275 +** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
  1.4276 +** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
  1.4277 +** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
  1.4278 +** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
  1.4279 +** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
  1.4280 +** <tr><td>  TEXT    <td>   BLOB    <td> No change
  1.4281 +** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
  1.4282 +** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
  1.4283 +** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
  1.4284 +** </table>
  1.4285 +** </blockquote>)^
  1.4286 +**
  1.4287 +** The table above makes reference to standard C library functions atoi()
  1.4288 +** and atof().  SQLite does not really use these functions.  It has its
  1.4289 +** own equivalent internal routines.  The atoi() and atof() names are
  1.4290 +** used in the table for brevity and because they are familiar to most
  1.4291 +** C programmers.
  1.4292 +**
  1.4293 +** Note that when type conversions occur, pointers returned by prior
  1.4294 +** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
  1.4295 +** sqlite3_column_text16() may be invalidated.
  1.4296 +** Type conversions and pointer invalidations might occur
  1.4297 +** in the following cases:
  1.4298 +**
  1.4299 +** <ul>
  1.4300 +** <li> The initial content is a BLOB and sqlite3_column_text() or
  1.4301 +**      sqlite3_column_text16() is called.  A zero-terminator might
  1.4302 +**      need to be added to the string.</li>
  1.4303 +** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
  1.4304 +**      sqlite3_column_text16() is called.  The content must be converted
  1.4305 +**      to UTF-16.</li>
  1.4306 +** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
  1.4307 +**      sqlite3_column_text() is called.  The content must be converted
  1.4308 +**      to UTF-8.</li>
  1.4309 +** </ul>
  1.4310 +**
  1.4311 +** ^Conversions between UTF-16be and UTF-16le are always done in place and do
  1.4312 +** not invalidate a prior pointer, though of course the content of the buffer
  1.4313 +** that the prior pointer references will have been modified.  Other kinds
  1.4314 +** of conversion are done in place when it is possible, but sometimes they
  1.4315 +** are not possible and in those cases prior pointers are invalidated.
  1.4316 +**
  1.4317 +** The safest and easiest to remember policy is to invoke these routines
  1.4318 +** in one of the following ways:
  1.4319 +**
  1.4320 +** <ul>
  1.4321 +**  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
  1.4322 +**  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
  1.4323 +**  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
  1.4324 +** </ul>
  1.4325 +**
  1.4326 +** In other words, you should call sqlite3_column_text(),
  1.4327 +** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
  1.4328 +** into the desired format, then invoke sqlite3_column_bytes() or
  1.4329 +** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
  1.4330 +** to sqlite3_column_text() or sqlite3_column_blob() with calls to
  1.4331 +** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
  1.4332 +** with calls to sqlite3_column_bytes().
  1.4333 +**
  1.4334 +** ^The pointers returned are valid until a type conversion occurs as
  1.4335 +** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
  1.4336 +** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
  1.4337 +** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
  1.4338 +** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
  1.4339 +** [sqlite3_free()].
  1.4340 +**
  1.4341 +** ^(If a memory allocation error occurs during the evaluation of any
  1.4342 +** of these routines, a default value is returned.  The default value
  1.4343 +** is either the integer 0, the floating point number 0.0, or a NULL
  1.4344 +** pointer.  Subsequent calls to [sqlite3_errcode()] will return
  1.4345 +** [SQLITE_NOMEM].)^
  1.4346 +*/
  1.4347 +SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
  1.4348 +SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
  1.4349 +SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
  1.4350 +SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
  1.4351 +SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
  1.4352 +SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
  1.4353 +SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  1.4354 +SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
  1.4355 +SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
  1.4356 +SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
  1.4357 +
  1.4358 +/*
  1.4359 +** CAPI3REF: Destroy A Prepared Statement Object
  1.4360 +**
  1.4361 +** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  1.4362 +** ^If the most recent evaluation of the statement encountered no errors
  1.4363 +** or if the statement is never been evaluated, then sqlite3_finalize() returns
  1.4364 +** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
  1.4365 +** sqlite3_finalize(S) returns the appropriate [error code] or
  1.4366 +** [extended error code].
  1.4367 +**
  1.4368 +** ^The sqlite3_finalize(S) routine can be called at any point during
  1.4369 +** the life cycle of [prepared statement] S:
  1.4370 +** before statement S is ever evaluated, after
  1.4371 +** one or more calls to [sqlite3_reset()], or after any call
  1.4372 +** to [sqlite3_step()] regardless of whether or not the statement has
  1.4373 +** completed execution.
  1.4374 +**
  1.4375 +** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
  1.4376 +**
  1.4377 +** The application must finalize every [prepared statement] in order to avoid
  1.4378 +** resource leaks.  It is a grievous error for the application to try to use
  1.4379 +** a prepared statement after it has been finalized.  Any use of a prepared
  1.4380 +** statement after it has been finalized can result in undefined and
  1.4381 +** undesirable behavior such as segfaults and heap corruption.
  1.4382 +*/
  1.4383 +SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
  1.4384 +
  1.4385 +/*
  1.4386 +** CAPI3REF: Reset A Prepared Statement Object
  1.4387 +**
  1.4388 +** The sqlite3_reset() function is called to reset a [prepared statement]
  1.4389 +** object back to its initial state, ready to be re-executed.
  1.4390 +** ^Any SQL statement variables that had values bound to them using
  1.4391 +** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  1.4392 +** Use [sqlite3_clear_bindings()] to reset the bindings.
  1.4393 +**
  1.4394 +** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
  1.4395 +** back to the beginning of its program.
  1.4396 +**
  1.4397 +** ^If the most recent call to [sqlite3_step(S)] for the
  1.4398 +** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
  1.4399 +** or if [sqlite3_step(S)] has never before been called on S,
  1.4400 +** then [sqlite3_reset(S)] returns [SQLITE_OK].
  1.4401 +**
  1.4402 +** ^If the most recent call to [sqlite3_step(S)] for the
  1.4403 +** [prepared statement] S indicated an error, then
  1.4404 +** [sqlite3_reset(S)] returns an appropriate [error code].
  1.4405 +**
  1.4406 +** ^The [sqlite3_reset(S)] interface does not change the values
  1.4407 +** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
  1.4408 +*/
  1.4409 +SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
  1.4410 +
  1.4411 +/*
  1.4412 +** CAPI3REF: Create Or Redefine SQL Functions
  1.4413 +** KEYWORDS: {function creation routines}
  1.4414 +** KEYWORDS: {application-defined SQL function}
  1.4415 +** KEYWORDS: {application-defined SQL functions}
  1.4416 +**
  1.4417 +** ^These functions (collectively known as "function creation routines")
  1.4418 +** are used to add SQL functions or aggregates or to redefine the behavior
  1.4419 +** of existing SQL functions or aggregates.  The only differences between
  1.4420 +** these routines are the text encoding expected for
  1.4421 +** the second parameter (the name of the function being created)
  1.4422 +** and the presence or absence of a destructor callback for
  1.4423 +** the application data pointer.
  1.4424 +**
  1.4425 +** ^The first parameter is the [database connection] to which the SQL
  1.4426 +** function is to be added.  ^If an application uses more than one database
  1.4427 +** connection then application-defined SQL functions must be added
  1.4428 +** to each database connection separately.
  1.4429 +**
  1.4430 +** ^The second parameter is the name of the SQL function to be created or
  1.4431 +** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
  1.4432 +** representation, exclusive of the zero-terminator.  ^Note that the name
  1.4433 +** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.  
  1.4434 +** ^Any attempt to create a function with a longer name
  1.4435 +** will result in [SQLITE_MISUSE] being returned.
  1.4436 +**
  1.4437 +** ^The third parameter (nArg)
  1.4438 +** is the number of arguments that the SQL function or
  1.4439 +** aggregate takes. ^If this parameter is -1, then the SQL function or
  1.4440 +** aggregate may take any number of arguments between 0 and the limit
  1.4441 +** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
  1.4442 +** parameter is less than -1 or greater than 127 then the behavior is
  1.4443 +** undefined.
  1.4444 +**
  1.4445 +** ^The fourth parameter, eTextRep, specifies what
  1.4446 +** [SQLITE_UTF8 | text encoding] this SQL function prefers for
  1.4447 +** its parameters.  Every SQL function implementation must be able to work
  1.4448 +** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
  1.4449 +** more efficient with one encoding than another.  ^An application may
  1.4450 +** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
  1.4451 +** times with the same function but with different values of eTextRep.
  1.4452 +** ^When multiple implementations of the same function are available, SQLite
  1.4453 +** will pick the one that involves the least amount of data conversion.
  1.4454 +** If there is only a single implementation which does not care what text
  1.4455 +** encoding is used, then the fourth argument should be [SQLITE_ANY].
  1.4456 +**
  1.4457 +** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
  1.4458 +** function can gain access to this pointer using [sqlite3_user_data()].)^
  1.4459 +**
  1.4460 +** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
  1.4461 +** pointers to C-language functions that implement the SQL function or
  1.4462 +** aggregate. ^A scalar SQL function requires an implementation of the xFunc
  1.4463 +** callback only; NULL pointers must be passed as the xStep and xFinal
  1.4464 +** parameters. ^An aggregate SQL function requires an implementation of xStep
  1.4465 +** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  1.4466 +** SQL function or aggregate, pass NULL pointers for all three function
  1.4467 +** callbacks.
  1.4468 +**
  1.4469 +** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
  1.4470 +** then it is destructor for the application data pointer. 
  1.4471 +** The destructor is invoked when the function is deleted, either by being
  1.4472 +** overloaded or when the database connection closes.)^
  1.4473 +** ^The destructor is also invoked if the call to
  1.4474 +** sqlite3_create_function_v2() fails.
  1.4475 +** ^When the destructor callback of the tenth parameter is invoked, it
  1.4476 +** is passed a single argument which is a copy of the application data 
  1.4477 +** pointer which was the fifth parameter to sqlite3_create_function_v2().
  1.4478 +**
  1.4479 +** ^It is permitted to register multiple implementations of the same
  1.4480 +** functions with the same name but with either differing numbers of
  1.4481 +** arguments or differing preferred text encodings.  ^SQLite will use
  1.4482 +** the implementation that most closely matches the way in which the
  1.4483 +** SQL function is used.  ^A function implementation with a non-negative
  1.4484 +** nArg parameter is a better match than a function implementation with
  1.4485 +** a negative nArg.  ^A function where the preferred text encoding
  1.4486 +** matches the database encoding is a better
  1.4487 +** match than a function where the encoding is different.  
  1.4488 +** ^A function where the encoding difference is between UTF16le and UTF16be
  1.4489 +** is a closer match than a function where the encoding difference is
  1.4490 +** between UTF8 and UTF16.
  1.4491 +**
  1.4492 +** ^Built-in functions may be overloaded by new application-defined functions.
  1.4493 +**
  1.4494 +** ^An application-defined function is permitted to call other
  1.4495 +** SQLite interfaces.  However, such calls must not
  1.4496 +** close the database connection nor finalize or reset the prepared
  1.4497 +** statement in which the function is running.
  1.4498 +*/
  1.4499 +SQLITE_API int sqlite3_create_function(
  1.4500 +  sqlite3 *db,
  1.4501 +  const char *zFunctionName,
  1.4502 +  int nArg,
  1.4503 +  int eTextRep,
  1.4504 +  void *pApp,
  1.4505 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  1.4506 +  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  1.4507 +  void (*xFinal)(sqlite3_context*)
  1.4508 +);
  1.4509 +SQLITE_API int sqlite3_create_function16(
  1.4510 +  sqlite3 *db,
  1.4511 +  const void *zFunctionName,
  1.4512 +  int nArg,
  1.4513 +  int eTextRep,
  1.4514 +  void *pApp,
  1.4515 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  1.4516 +  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  1.4517 +  void (*xFinal)(sqlite3_context*)
  1.4518 +);
  1.4519 +SQLITE_API int sqlite3_create_function_v2(
  1.4520 +  sqlite3 *db,
  1.4521 +  const char *zFunctionName,
  1.4522 +  int nArg,
  1.4523 +  int eTextRep,
  1.4524 +  void *pApp,
  1.4525 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  1.4526 +  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  1.4527 +  void (*xFinal)(sqlite3_context*),
  1.4528 +  void(*xDestroy)(void*)
  1.4529 +);
  1.4530 +
  1.4531 +/*
  1.4532 +** CAPI3REF: Text Encodings
  1.4533 +**
  1.4534 +** These constant define integer codes that represent the various
  1.4535 +** text encodings supported by SQLite.
  1.4536 +*/
  1.4537 +#define SQLITE_UTF8           1
  1.4538 +#define SQLITE_UTF16LE        2
  1.4539 +#define SQLITE_UTF16BE        3
  1.4540 +#define SQLITE_UTF16          4    /* Use native byte order */
  1.4541 +#define SQLITE_ANY            5    /* sqlite3_create_function only */
  1.4542 +#define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
  1.4543 +
  1.4544 +/*
  1.4545 +** CAPI3REF: Deprecated Functions
  1.4546 +** DEPRECATED
  1.4547 +**
  1.4548 +** These functions are [deprecated].  In order to maintain
  1.4549 +** backwards compatibility with older code, these functions continue 
  1.4550 +** to be supported.  However, new applications should avoid
  1.4551 +** the use of these functions.  To help encourage people to avoid
  1.4552 +** using these functions, we are not going to tell you what they do.
  1.4553 +*/
  1.4554 +#ifndef SQLITE_OMIT_DEPRECATED
  1.4555 +SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
  1.4556 +SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
  1.4557 +SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
  1.4558 +SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
  1.4559 +SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
  1.4560 +SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
  1.4561 +#endif
  1.4562 +
  1.4563 +/*
  1.4564 +** CAPI3REF: Obtaining SQL Function Parameter Values
  1.4565 +**
  1.4566 +** The C-language implementation of SQL functions and aggregates uses
  1.4567 +** this set of interface routines to access the parameter values on
  1.4568 +** the function or aggregate.
  1.4569 +**
  1.4570 +** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  1.4571 +** to [sqlite3_create_function()] and [sqlite3_create_function16()]
  1.4572 +** define callbacks that implement the SQL functions and aggregates.
  1.4573 +** The 3rd parameter to these callbacks is an array of pointers to
  1.4574 +** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
  1.4575 +** each parameter to the SQL function.  These routines are used to
  1.4576 +** extract values from the [sqlite3_value] objects.
  1.4577 +**
  1.4578 +** These routines work only with [protected sqlite3_value] objects.
  1.4579 +** Any attempt to use these routines on an [unprotected sqlite3_value]
  1.4580 +** object results in undefined behavior.
  1.4581 +**
  1.4582 +** ^These routines work just like the corresponding [column access functions]
  1.4583 +** except that  these routines take a single [protected sqlite3_value] object
  1.4584 +** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
  1.4585 +**
  1.4586 +** ^The sqlite3_value_text16() interface extracts a UTF-16 string
  1.4587 +** in the native byte-order of the host machine.  ^The
  1.4588 +** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
  1.4589 +** extract UTF-16 strings as big-endian and little-endian respectively.
  1.4590 +**
  1.4591 +** ^(The sqlite3_value_numeric_type() interface attempts to apply
  1.4592 +** numeric affinity to the value.  This means that an attempt is
  1.4593 +** made to convert the value to an integer or floating point.  If
  1.4594 +** such a conversion is possible without loss of information (in other
  1.4595 +** words, if the value is a string that looks like a number)
  1.4596 +** then the conversion is performed.  Otherwise no conversion occurs.
  1.4597 +** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
  1.4598 +**
  1.4599 +** Please pay particular attention to the fact that the pointer returned
  1.4600 +** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  1.4601 +** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  1.4602 +** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  1.4603 +** or [sqlite3_value_text16()].
  1.4604 +**
  1.4605 +** These routines must be called from the same thread as
  1.4606 +** the SQL function that supplied the [sqlite3_value*] parameters.
  1.4607 +*/
  1.4608 +SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
  1.4609 +SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  1.4610 +SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  1.4611 +SQLITE_API double sqlite3_value_double(sqlite3_value*);
  1.4612 +SQLITE_API int sqlite3_value_int(sqlite3_value*);
  1.4613 +SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
  1.4614 +SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
  1.4615 +SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
  1.4616 +SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  1.4617 +SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  1.4618 +SQLITE_API int sqlite3_value_type(sqlite3_value*);
  1.4619 +SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  1.4620 +
  1.4621 +/*
  1.4622 +** CAPI3REF: Obtain Aggregate Function Context
  1.4623 +**
  1.4624 +** Implementations of aggregate SQL functions use this
  1.4625 +** routine to allocate memory for storing their state.
  1.4626 +**
  1.4627 +** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
  1.4628 +** for a particular aggregate function, SQLite
  1.4629 +** allocates N of memory, zeroes out that memory, and returns a pointer
  1.4630 +** to the new memory. ^On second and subsequent calls to
  1.4631 +** sqlite3_aggregate_context() for the same aggregate function instance,
  1.4632 +** the same buffer is returned.  Sqlite3_aggregate_context() is normally
  1.4633 +** called once for each invocation of the xStep callback and then one
  1.4634 +** last time when the xFinal callback is invoked.  ^(When no rows match
  1.4635 +** an aggregate query, the xStep() callback of the aggregate function
  1.4636 +** implementation is never called and xFinal() is called exactly once.
  1.4637 +** In those cases, sqlite3_aggregate_context() might be called for the
  1.4638 +** first time from within xFinal().)^
  1.4639 +**
  1.4640 +** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
  1.4641 +** less than or equal to zero or if a memory allocate error occurs.
  1.4642 +**
  1.4643 +** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
  1.4644 +** determined by the N parameter on first successful call.  Changing the
  1.4645 +** value of N in subsequent call to sqlite3_aggregate_context() within
  1.4646 +** the same aggregate function instance will not resize the memory
  1.4647 +** allocation.)^
  1.4648 +**
  1.4649 +** ^SQLite automatically frees the memory allocated by 
  1.4650 +** sqlite3_aggregate_context() when the aggregate query concludes.
  1.4651 +**
  1.4652 +** The first parameter must be a copy of the
  1.4653 +** [sqlite3_context | SQL function context] that is the first parameter
  1.4654 +** to the xStep or xFinal callback routine that implements the aggregate
  1.4655 +** function.
  1.4656 +**
  1.4657 +** This routine must be called from the same thread in which
  1.4658 +** the aggregate SQL function is running.
  1.4659 +*/
  1.4660 +SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  1.4661 +
  1.4662 +/*
  1.4663 +** CAPI3REF: User Data For Functions
  1.4664 +**
  1.4665 +** ^The sqlite3_user_data() interface returns a copy of
  1.4666 +** the pointer that was the pUserData parameter (the 5th parameter)
  1.4667 +** of the [sqlite3_create_function()]
  1.4668 +** and [sqlite3_create_function16()] routines that originally
  1.4669 +** registered the application defined function.
  1.4670 +**
  1.4671 +** This routine must be called from the same thread in which
  1.4672 +** the application-defined function is running.
  1.4673 +*/
  1.4674 +SQLITE_API void *sqlite3_user_data(sqlite3_context*);
  1.4675 +
  1.4676 +/*
  1.4677 +** CAPI3REF: Database Connection For Functions
  1.4678 +**
  1.4679 +** ^The sqlite3_context_db_handle() interface returns a copy of
  1.4680 +** the pointer to the [database connection] (the 1st parameter)
  1.4681 +** of the [sqlite3_create_function()]
  1.4682 +** and [sqlite3_create_function16()] routines that originally
  1.4683 +** registered the application defined function.
  1.4684 +*/
  1.4685 +SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
  1.4686 +
  1.4687 +/*
  1.4688 +** CAPI3REF: Function Auxiliary Data
  1.4689 +**
  1.4690 +** The following two functions may be used by scalar SQL functions to
  1.4691 +** associate metadata with argument values. If the same value is passed to
  1.4692 +** multiple invocations of the same SQL function during query execution, under
  1.4693 +** some circumstances the associated metadata may be preserved. This may
  1.4694 +** be used, for example, to add a regular-expression matching scalar
  1.4695 +** function. The compiled version of the regular expression is stored as
  1.4696 +** metadata associated with the SQL value passed as the regular expression
  1.4697 +** pattern.  The compiled regular expression can be reused on multiple
  1.4698 +** invocations of the same function so that the original pattern string
  1.4699 +** does not need to be recompiled on each invocation.
  1.4700 +**
  1.4701 +** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
  1.4702 +** associated by the sqlite3_set_auxdata() function with the Nth argument
  1.4703 +** value to the application-defined function. ^If no metadata has been ever
  1.4704 +** been set for the Nth argument of the function, or if the corresponding
  1.4705 +** function parameter has changed since the meta-data was set,
  1.4706 +** then sqlite3_get_auxdata() returns a NULL pointer.
  1.4707 +**
  1.4708 +** ^The sqlite3_set_auxdata() interface saves the metadata
  1.4709 +** pointed to by its 3rd parameter as the metadata for the N-th
  1.4710 +** argument of the application-defined function.  Subsequent
  1.4711 +** calls to sqlite3_get_auxdata() might return this data, if it has
  1.4712 +** not been destroyed.
  1.4713 +** ^If it is not NULL, SQLite will invoke the destructor
  1.4714 +** function given by the 4th parameter to sqlite3_set_auxdata() on
  1.4715 +** the metadata when the corresponding function parameter changes
  1.4716 +** or when the SQL statement completes, whichever comes first.
  1.4717 +**
  1.4718 +** SQLite is free to call the destructor and drop metadata on any
  1.4719 +** parameter of any function at any time.  ^The only guarantee is that
  1.4720 +** the destructor will be called before the metadata is dropped.
  1.4721 +**
  1.4722 +** ^(In practice, metadata is preserved between function calls for
  1.4723 +** expressions that are constant at compile time. This includes literal
  1.4724 +** values and [parameters].)^
  1.4725 +**
  1.4726 +** These routines must be called from the same thread in which
  1.4727 +** the SQL function is running.
  1.4728 +*/
  1.4729 +SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
  1.4730 +SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
  1.4731 +
  1.4732 +
  1.4733 +/*
  1.4734 +** CAPI3REF: Constants Defining Special Destructor Behavior
  1.4735 +**
  1.4736 +** These are special values for the destructor that is passed in as the
  1.4737 +** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
  1.4738 +** argument is SQLITE_STATIC, it means that the content pointer is constant
  1.4739 +** and will never change.  It does not need to be destroyed.  ^The
  1.4740 +** SQLITE_TRANSIENT value means that the content will likely change in
  1.4741 +** the near future and that SQLite should make its own private copy of
  1.4742 +** the content before returning.
  1.4743 +**
  1.4744 +** The typedef is necessary to work around problems in certain
  1.4745 +** C++ compilers.  See ticket #2191.
  1.4746 +*/
  1.4747 +typedef void (*sqlite3_destructor_type)(void*);
  1.4748 +#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
  1.4749 +#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
  1.4750 +
  1.4751 +/*
  1.4752 +** CAPI3REF: Setting The Result Of An SQL Function
  1.4753 +**
  1.4754 +** These routines are used by the xFunc or xFinal callbacks that
  1.4755 +** implement SQL functions and aggregates.  See
  1.4756 +** [sqlite3_create_function()] and [sqlite3_create_function16()]
  1.4757 +** for additional information.
  1.4758 +**
  1.4759 +** These functions work very much like the [parameter binding] family of
  1.4760 +** functions used to bind values to host parameters in prepared statements.
  1.4761 +** Refer to the [SQL parameter] documentation for additional information.
  1.4762 +**
  1.4763 +** ^The sqlite3_result_blob() interface sets the result from
  1.4764 +** an application-defined function to be the BLOB whose content is pointed
  1.4765 +** to by the second parameter and which is N bytes long where N is the
  1.4766 +** third parameter.
  1.4767 +**
  1.4768 +** ^The sqlite3_result_zeroblob() interfaces set the result of
  1.4769 +** the application-defined function to be a BLOB containing all zero
  1.4770 +** bytes and N bytes in size, where N is the value of the 2nd parameter.
  1.4771 +**
  1.4772 +** ^The sqlite3_result_double() interface sets the result from
  1.4773 +** an application-defined function to be a floating point value specified
  1.4774 +** by its 2nd argument.
  1.4775 +**
  1.4776 +** ^The sqlite3_result_error() and sqlite3_result_error16() functions
  1.4777 +** cause the implemented SQL function to throw an exception.
  1.4778 +** ^SQLite uses the string pointed to by the
  1.4779 +** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
  1.4780 +** as the text of an error message.  ^SQLite interprets the error
  1.4781 +** message string from sqlite3_result_error() as UTF-8. ^SQLite
  1.4782 +** interprets the string from sqlite3_result_error16() as UTF-16 in native
  1.4783 +** byte order.  ^If the third parameter to sqlite3_result_error()
  1.4784 +** or sqlite3_result_error16() is negative then SQLite takes as the error
  1.4785 +** message all text up through the first zero character.
  1.4786 +** ^If the third parameter to sqlite3_result_error() or
  1.4787 +** sqlite3_result_error16() is non-negative then SQLite takes that many
  1.4788 +** bytes (not characters) from the 2nd parameter as the error message.
  1.4789 +** ^The sqlite3_result_error() and sqlite3_result_error16()
  1.4790 +** routines make a private copy of the error message text before
  1.4791 +** they return.  Hence, the calling function can deallocate or
  1.4792 +** modify the text after they return without harm.
  1.4793 +** ^The sqlite3_result_error_code() function changes the error code
  1.4794 +** returned by SQLite as a result of an error in a function.  ^By default,
  1.4795 +** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
  1.4796 +** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  1.4797 +**
  1.4798 +** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
  1.4799 +** error indicating that a string or BLOB is too long to represent.
  1.4800 +**
  1.4801 +** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
  1.4802 +** error indicating that a memory allocation failed.
  1.4803 +**
  1.4804 +** ^The sqlite3_result_int() interface sets the return value
  1.4805 +** of the application-defined function to be the 32-bit signed integer
  1.4806 +** value given in the 2nd argument.
  1.4807 +** ^The sqlite3_result_int64() interface sets the return value
  1.4808 +** of the application-defined function to be the 64-bit signed integer
  1.4809 +** value given in the 2nd argument.
  1.4810 +**
  1.4811 +** ^The sqlite3_result_null() interface sets the return value
  1.4812 +** of the application-defined function to be NULL.
  1.4813 +**
  1.4814 +** ^The sqlite3_result_text(), sqlite3_result_text16(),
  1.4815 +** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
  1.4816 +** set the return value of the application-defined function to be
  1.4817 +** a text string which is represented as UTF-8, UTF-16 native byte order,
  1.4818 +** UTF-16 little endian, or UTF-16 big endian, respectively.
  1.4819 +** ^SQLite takes the text result from the application from
  1.4820 +** the 2nd parameter of the sqlite3_result_text* interfaces.
  1.4821 +** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  1.4822 +** is negative, then SQLite takes result text from the 2nd parameter
  1.4823 +** through the first zero character.
  1.4824 +** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  1.4825 +** is non-negative, then as many bytes (not characters) of the text
  1.4826 +** pointed to by the 2nd parameter are taken as the application-defined
  1.4827 +** function result.  If the 3rd parameter is non-negative, then it
  1.4828 +** must be the byte offset into the string where the NUL terminator would
  1.4829 +** appear if the string where NUL terminated.  If any NUL characters occur
  1.4830 +** in the string at a byte offset that is less than the value of the 3rd
  1.4831 +** parameter, then the resulting string will contain embedded NULs and the
  1.4832 +** result of expressions operating on strings with embedded NULs is undefined.
  1.4833 +** ^If the 4th parameter to the sqlite3_result_text* interfaces
  1.4834 +** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
  1.4835 +** function as the destructor on the text or BLOB result when it has
  1.4836 +** finished using that result.
  1.4837 +** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
  1.4838 +** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
  1.4839 +** assumes that the text or BLOB result is in constant space and does not
  1.4840 +** copy the content of the parameter nor call a destructor on the content
  1.4841 +** when it has finished using that result.
  1.4842 +** ^If the 4th parameter to the sqlite3_result_text* interfaces
  1.4843 +** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
  1.4844 +** then SQLite makes a copy of the result into space obtained from
  1.4845 +** from [sqlite3_malloc()] before it returns.
  1.4846 +**
  1.4847 +** ^The sqlite3_result_value() interface sets the result of
  1.4848 +** the application-defined function to be a copy the
  1.4849 +** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
  1.4850 +** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
  1.4851 +** so that the [sqlite3_value] specified in the parameter may change or
  1.4852 +** be deallocated after sqlite3_result_value() returns without harm.
  1.4853 +** ^A [protected sqlite3_value] object may always be used where an
  1.4854 +** [unprotected sqlite3_value] object is required, so either
  1.4855 +** kind of [sqlite3_value] object can be used with this interface.
  1.4856 +**
  1.4857 +** If these routines are called from within the different thread
  1.4858 +** than the one containing the application-defined function that received
  1.4859 +** the [sqlite3_context] pointer, the results are undefined.
  1.4860 +*/
  1.4861 +SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
  1.4862 +SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
  1.4863 +SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
  1.4864 +SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
  1.4865 +SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
  1.4866 +SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
  1.4867 +SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
  1.4868 +SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
  1.4869 +SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
  1.4870 +SQLITE_API void sqlite3_result_null(sqlite3_context*);
  1.4871 +SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
  1.4872 +SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  1.4873 +SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  1.4874 +SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  1.4875 +SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  1.4876 +SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
  1.4877 +
  1.4878 +/*
  1.4879 +** CAPI3REF: Define New Collating Sequences
  1.4880 +**
  1.4881 +** ^These functions add, remove, or modify a [collation] associated
  1.4882 +** with the [database connection] specified as the first argument.
  1.4883 +**
  1.4884 +** ^The name of the collation is a UTF-8 string
  1.4885 +** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  1.4886 +** and a UTF-16 string in native byte order for sqlite3_create_collation16().
  1.4887 +** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
  1.4888 +** considered to be the same name.
  1.4889 +**
  1.4890 +** ^(The third argument (eTextRep) must be one of the constants:
  1.4891 +** <ul>
  1.4892 +** <li> [SQLITE_UTF8],
  1.4893 +** <li> [SQLITE_UTF16LE],
  1.4894 +** <li> [SQLITE_UTF16BE],
  1.4895 +** <li> [SQLITE_UTF16], or
  1.4896 +** <li> [SQLITE_UTF16_ALIGNED].
  1.4897 +** </ul>)^
  1.4898 +** ^The eTextRep argument determines the encoding of strings passed
  1.4899 +** to the collating function callback, xCallback.
  1.4900 +** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
  1.4901 +** force strings to be UTF16 with native byte order.
  1.4902 +** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
  1.4903 +** on an even byte address.
  1.4904 +**
  1.4905 +** ^The fourth argument, pArg, is an application data pointer that is passed
  1.4906 +** through as the first argument to the collating function callback.
  1.4907 +**
  1.4908 +** ^The fifth argument, xCallback, is a pointer to the collating function.
  1.4909 +** ^Multiple collating functions can be registered using the same name but
  1.4910 +** with different eTextRep parameters and SQLite will use whichever
  1.4911 +** function requires the least amount of data transformation.
  1.4912 +** ^If the xCallback argument is NULL then the collating function is
  1.4913 +** deleted.  ^When all collating functions having the same name are deleted,
  1.4914 +** that collation is no longer usable.
  1.4915 +**
  1.4916 +** ^The collating function callback is invoked with a copy of the pArg 
  1.4917 +** application data pointer and with two strings in the encoding specified
  1.4918 +** by the eTextRep argument.  The collating function must return an
  1.4919 +** integer that is negative, zero, or positive
  1.4920 +** if the first string is less than, equal to, or greater than the second,
  1.4921 +** respectively.  A collating function must always return the same answer
  1.4922 +** given the same inputs.  If two or more collating functions are registered
  1.4923 +** to the same collation name (using different eTextRep values) then all
  1.4924 +** must give an equivalent answer when invoked with equivalent strings.
  1.4925 +** The collating function must obey the following properties for all
  1.4926 +** strings A, B, and C:
  1.4927 +**
  1.4928 +** <ol>
  1.4929 +** <li> If A==B then B==A.
  1.4930 +** <li> If A==B and B==C then A==C.
  1.4931 +** <li> If A&lt;B THEN B&gt;A.
  1.4932 +** <li> If A&lt;B and B&lt;C then A&lt;C.
  1.4933 +** </ol>
  1.4934 +**
  1.4935 +** If a collating function fails any of the above constraints and that
  1.4936 +** collating function is  registered and used, then the behavior of SQLite
  1.4937 +** is undefined.
  1.4938 +**
  1.4939 +** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
  1.4940 +** with the addition that the xDestroy callback is invoked on pArg when
  1.4941 +** the collating function is deleted.
  1.4942 +** ^Collating functions are deleted when they are overridden by later
  1.4943 +** calls to the collation creation functions or when the
  1.4944 +** [database connection] is closed using [sqlite3_close()].
  1.4945 +**
  1.4946 +** ^The xDestroy callback is <u>not</u> called if the 
  1.4947 +** sqlite3_create_collation_v2() function fails.  Applications that invoke
  1.4948 +** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should 
  1.4949 +** check the return code and dispose of the application data pointer
  1.4950 +** themselves rather than expecting SQLite to deal with it for them.
  1.4951 +** This is different from every other SQLite interface.  The inconsistency 
  1.4952 +** is unfortunate but cannot be changed without breaking backwards 
  1.4953 +** compatibility.
  1.4954 +**
  1.4955 +** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
  1.4956 +*/
  1.4957 +SQLITE_API int sqlite3_create_collation(
  1.4958 +  sqlite3*, 
  1.4959 +  const char *zName, 
  1.4960 +  int eTextRep, 
  1.4961 +  void *pArg,
  1.4962 +  int(*xCompare)(void*,int,const void*,int,const void*)
  1.4963 +);
  1.4964 +SQLITE_API int sqlite3_create_collation_v2(
  1.4965 +  sqlite3*, 
  1.4966 +  const char *zName, 
  1.4967 +  int eTextRep, 
  1.4968 +  void *pArg,
  1.4969 +  int(*xCompare)(void*,int,const void*,int,const void*),
  1.4970 +  void(*xDestroy)(void*)
  1.4971 +);
  1.4972 +SQLITE_API int sqlite3_create_collation16(
  1.4973 +  sqlite3*, 
  1.4974 +  const void *zName,
  1.4975 +  int eTextRep, 
  1.4976 +  void *pArg,
  1.4977 +  int(*xCompare)(void*,int,const void*,int,const void*)
  1.4978 +);
  1.4979 +
  1.4980 +/*
  1.4981 +** CAPI3REF: Collation Needed Callbacks
  1.4982 +**
  1.4983 +** ^To avoid having to register all collation sequences before a database
  1.4984 +** can be used, a single callback function may be registered with the
  1.4985 +** [database connection] to be invoked whenever an undefined collation
  1.4986 +** sequence is required.
  1.4987 +**
  1.4988 +** ^If the function is registered using the sqlite3_collation_needed() API,
  1.4989 +** then it is passed the names of undefined collation sequences as strings
  1.4990 +** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
  1.4991 +** the names are passed as UTF-16 in machine native byte order.
  1.4992 +** ^A call to either function replaces the existing collation-needed callback.
  1.4993 +**
  1.4994 +** ^(When the callback is invoked, the first argument passed is a copy
  1.4995 +** of the second argument to sqlite3_collation_needed() or
  1.4996 +** sqlite3_collation_needed16().  The second argument is the database
  1.4997 +** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
  1.4998 +** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
  1.4999 +** sequence function required.  The fourth parameter is the name of the
  1.5000 +** required collation sequence.)^
  1.5001 +**
  1.5002 +** The callback function should register the desired collation using
  1.5003 +** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
  1.5004 +** [sqlite3_create_collation_v2()].
  1.5005 +*/
  1.5006 +SQLITE_API int sqlite3_collation_needed(
  1.5007 +  sqlite3*, 
  1.5008 +  void*, 
  1.5009 +  void(*)(void*,sqlite3*,int eTextRep,const char*)
  1.5010 +);
  1.5011 +SQLITE_API int sqlite3_collation_needed16(
  1.5012 +  sqlite3*, 
  1.5013 +  void*,
  1.5014 +  void(*)(void*,sqlite3*,int eTextRep,const void*)
  1.5015 +);
  1.5016 +
  1.5017 +#ifdef SQLITE_HAS_CODEC
  1.5018 +/*
  1.5019 +** Specify the key for an encrypted database.  This routine should be
  1.5020 +** called right after sqlite3_open().
  1.5021 +**
  1.5022 +** The code to implement this API is not available in the public release
  1.5023 +** of SQLite.
  1.5024 +*/
  1.5025 +SQLITE_API int sqlite3_key(
  1.5026 +  sqlite3 *db,                   /* Database to be rekeyed */
  1.5027 +  const void *pKey, int nKey     /* The key */
  1.5028 +);
  1.5029 +
  1.5030 +/*
  1.5031 +** Change the key on an open database.  If the current database is not
  1.5032 +** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
  1.5033 +** database is decrypted.
  1.5034 +**
  1.5035 +** The code to implement this API is not available in the public release
  1.5036 +** of SQLite.
  1.5037 +*/
  1.5038 +SQLITE_API int sqlite3_rekey(
  1.5039 +  sqlite3 *db,                   /* Database to be rekeyed */
  1.5040 +  const void *pKey, int nKey     /* The new key */
  1.5041 +);
  1.5042 +
  1.5043 +/*
  1.5044 +** Specify the activation key for a SEE database.  Unless 
  1.5045 +** activated, none of the SEE routines will work.
  1.5046 +*/
  1.5047 +SQLITE_API void sqlite3_activate_see(
  1.5048 +  const char *zPassPhrase        /* Activation phrase */
  1.5049 +);
  1.5050 +#endif
  1.5051 +
  1.5052 +#ifdef SQLITE_ENABLE_CEROD
  1.5053 +/*
  1.5054 +** Specify the activation key for a CEROD database.  Unless 
  1.5055 +** activated, none of the CEROD routines will work.
  1.5056 +*/
  1.5057 +SQLITE_API void sqlite3_activate_cerod(
  1.5058 +  const char *zPassPhrase        /* Activation phrase */
  1.5059 +);
  1.5060 +#endif
  1.5061 +
  1.5062 +/*
  1.5063 +** CAPI3REF: Suspend Execution For A Short Time
  1.5064 +**
  1.5065 +** The sqlite3_sleep() function causes the current thread to suspend execution
  1.5066 +** for at least a number of milliseconds specified in its parameter.
  1.5067 +**
  1.5068 +** If the operating system does not support sleep requests with
  1.5069 +** millisecond time resolution, then the time will be rounded up to
  1.5070 +** the nearest second. The number of milliseconds of sleep actually
  1.5071 +** requested from the operating system is returned.
  1.5072 +**
  1.5073 +** ^SQLite implements this interface by calling the xSleep()
  1.5074 +** method of the default [sqlite3_vfs] object.  If the xSleep() method
  1.5075 +** of the default VFS is not implemented correctly, or not implemented at
  1.5076 +** all, then the behavior of sqlite3_sleep() may deviate from the description
  1.5077 +** in the previous paragraphs.
  1.5078 +*/
  1.5079 +SQLITE_API int sqlite3_sleep(int);
  1.5080 +
  1.5081 +/*
  1.5082 +** CAPI3REF: Name Of The Folder Holding Temporary Files
  1.5083 +**
  1.5084 +** ^(If this global variable is made to point to a string which is
  1.5085 +** the name of a folder (a.k.a. directory), then all temporary files
  1.5086 +** created by SQLite when using a built-in [sqlite3_vfs | VFS]
  1.5087 +** will be placed in that directory.)^  ^If this variable
  1.5088 +** is a NULL pointer, then SQLite performs a search for an appropriate
  1.5089 +** temporary file directory.
  1.5090 +**
  1.5091 +** It is not safe to read or modify this variable in more than one
  1.5092 +** thread at a time.  It is not safe to read or modify this variable
  1.5093 +** if a [database connection] is being used at the same time in a separate
  1.5094 +** thread.
  1.5095 +** It is intended that this variable be set once
  1.5096 +** as part of process initialization and before any SQLite interface
  1.5097 +** routines have been called and that this variable remain unchanged
  1.5098 +** thereafter.
  1.5099 +**
  1.5100 +** ^The [temp_store_directory pragma] may modify this variable and cause
  1.5101 +** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
  1.5102 +** the [temp_store_directory pragma] always assumes that any string
  1.5103 +** that this variable points to is held in memory obtained from 
  1.5104 +** [sqlite3_malloc] and the pragma may attempt to free that memory
  1.5105 +** using [sqlite3_free].
  1.5106 +** Hence, if this variable is modified directly, either it should be
  1.5107 +** made NULL or made to point to memory obtained from [sqlite3_malloc]
  1.5108 +** or else the use of the [temp_store_directory pragma] should be avoided.
  1.5109 +**
  1.5110 +** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
  1.5111 +** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
  1.5112 +** features that require the use of temporary files may fail.  Here is an
  1.5113 +** example of how to do this using C++ with the Windows Runtime:
  1.5114 +**
  1.5115 +** <blockquote><pre>
  1.5116 +** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
  1.5117 +** &nbsp;     TemporaryFolder->Path->Data();
  1.5118 +** char zPathBuf&#91;MAX_PATH + 1&#93;;
  1.5119 +** memset(zPathBuf, 0, sizeof(zPathBuf));
  1.5120 +** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
  1.5121 +** &nbsp;     NULL, NULL);
  1.5122 +** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
  1.5123 +** </pre></blockquote>
  1.5124 +*/
  1.5125 +SQLITE_API char *sqlite3_temp_directory;
  1.5126 +
  1.5127 +/*
  1.5128 +** CAPI3REF: Name Of The Folder Holding Database Files
  1.5129 +**
  1.5130 +** ^(If this global variable is made to point to a string which is
  1.5131 +** the name of a folder (a.k.a. directory), then all database files
  1.5132 +** specified with a relative pathname and created or accessed by
  1.5133 +** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
  1.5134 +** to be relative to that directory.)^ ^If this variable is a NULL
  1.5135 +** pointer, then SQLite assumes that all database files specified
  1.5136 +** with a relative pathname are relative to the current directory
  1.5137 +** for the process.  Only the windows VFS makes use of this global
  1.5138 +** variable; it is ignored by the unix VFS.
  1.5139 +**
  1.5140 +** Changing the value of this variable while a database connection is
  1.5141 +** open can result in a corrupt database.
  1.5142 +**
  1.5143 +** It is not safe to read or modify this variable in more than one
  1.5144 +** thread at a time.  It is not safe to read or modify this variable
  1.5145 +** if a [database connection] is being used at the same time in a separate
  1.5146 +** thread.
  1.5147 +** It is intended that this variable be set once
  1.5148 +** as part of process initialization and before any SQLite interface
  1.5149 +** routines have been called and that this variable remain unchanged
  1.5150 +** thereafter.
  1.5151 +**
  1.5152 +** ^The [data_store_directory pragma] may modify this variable and cause
  1.5153 +** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
  1.5154 +** the [data_store_directory pragma] always assumes that any string
  1.5155 +** that this variable points to is held in memory obtained from 
  1.5156 +** [sqlite3_malloc] and the pragma may attempt to free that memory
  1.5157 +** using [sqlite3_free].
  1.5158 +** Hence, if this variable is modified directly, either it should be
  1.5159 +** made NULL or made to point to memory obtained from [sqlite3_malloc]
  1.5160 +** or else the use of the [data_store_directory pragma] should be avoided.
  1.5161 +*/
  1.5162 +SQLITE_API char *sqlite3_data_directory;
  1.5163 +
  1.5164 +/*
  1.5165 +** CAPI3REF: Test For Auto-Commit Mode
  1.5166 +** KEYWORDS: {autocommit mode}
  1.5167 +**
  1.5168 +** ^The sqlite3_get_autocommit() interface returns non-zero or
  1.5169 +** zero if the given database connection is or is not in autocommit mode,
  1.5170 +** respectively.  ^Autocommit mode is on by default.
  1.5171 +** ^Autocommit mode is disabled by a [BEGIN] statement.
  1.5172 +** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  1.5173 +**
  1.5174 +** If certain kinds of errors occur on a statement within a multi-statement
  1.5175 +** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
  1.5176 +** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
  1.5177 +** transaction might be rolled back automatically.  The only way to
  1.5178 +** find out whether SQLite automatically rolled back the transaction after
  1.5179 +** an error is to use this function.
  1.5180 +**
  1.5181 +** If another thread changes the autocommit status of the database
  1.5182 +** connection while this routine is running, then the return value
  1.5183 +** is undefined.
  1.5184 +*/
  1.5185 +SQLITE_API int sqlite3_get_autocommit(sqlite3*);
  1.5186 +
  1.5187 +/*
  1.5188 +** CAPI3REF: Find The Database Handle Of A Prepared Statement
  1.5189 +**
  1.5190 +** ^The sqlite3_db_handle interface returns the [database connection] handle
  1.5191 +** to which a [prepared statement] belongs.  ^The [database connection]
  1.5192 +** returned by sqlite3_db_handle is the same [database connection]
  1.5193 +** that was the first argument
  1.5194 +** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  1.5195 +** create the statement in the first place.
  1.5196 +*/
  1.5197 +SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  1.5198 +
  1.5199 +/*
  1.5200 +** CAPI3REF: Return The Filename For A Database Connection
  1.5201 +**
  1.5202 +** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  1.5203 +** associated with database N of connection D.  ^The main database file
  1.5204 +** has the name "main".  If there is no attached database N on the database
  1.5205 +** connection D, or if database N is a temporary or in-memory database, then
  1.5206 +** a NULL pointer is returned.
  1.5207 +**
  1.5208 +** ^The filename returned by this function is the output of the
  1.5209 +** xFullPathname method of the [VFS].  ^In other words, the filename
  1.5210 +** will be an absolute pathname, even if the filename used
  1.5211 +** to open the database originally was a URI or relative pathname.
  1.5212 +*/
  1.5213 +SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  1.5214 +
  1.5215 +/*
  1.5216 +** CAPI3REF: Determine if a database is read-only
  1.5217 +**
  1.5218 +** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
  1.5219 +** of connection D is read-only, 0 if it is read/write, or -1 if N is not
  1.5220 +** the name of a database on connection D.
  1.5221 +*/
  1.5222 +SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
  1.5223 +
  1.5224 +/*
  1.5225 +** CAPI3REF: Find the next prepared statement
  1.5226 +**
  1.5227 +** ^This interface returns a pointer to the next [prepared statement] after
  1.5228 +** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
  1.5229 +** then this interface returns a pointer to the first prepared statement
  1.5230 +** associated with the database connection pDb.  ^If no prepared statement
  1.5231 +** satisfies the conditions of this routine, it returns NULL.
  1.5232 +**
  1.5233 +** The [database connection] pointer D in a call to
  1.5234 +** [sqlite3_next_stmt(D,S)] must refer to an open database
  1.5235 +** connection and in particular must not be a NULL pointer.
  1.5236 +*/
  1.5237 +SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  1.5238 +
  1.5239 +/*
  1.5240 +** CAPI3REF: Commit And Rollback Notification Callbacks
  1.5241 +**
  1.5242 +** ^The sqlite3_commit_hook() interface registers a callback
  1.5243 +** function to be invoked whenever a transaction is [COMMIT | committed].
  1.5244 +** ^Any callback set by a previous call to sqlite3_commit_hook()
  1.5245 +** for the same database connection is overridden.
  1.5246 +** ^The sqlite3_rollback_hook() interface registers a callback
  1.5247 +** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
  1.5248 +** ^Any callback set by a previous call to sqlite3_rollback_hook()
  1.5249 +** for the same database connection is overridden.
  1.5250 +** ^The pArg argument is passed through to the callback.
  1.5251 +** ^If the callback on a commit hook function returns non-zero,
  1.5252 +** then the commit is converted into a rollback.
  1.5253 +**
  1.5254 +** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
  1.5255 +** return the P argument from the previous call of the same function
  1.5256 +** on the same [database connection] D, or NULL for
  1.5257 +** the first call for each function on D.
  1.5258 +**
  1.5259 +** The commit and rollback hook callbacks are not reentrant.
  1.5260 +** The callback implementation must not do anything that will modify
  1.5261 +** the database connection that invoked the callback.  Any actions
  1.5262 +** to modify the database connection must be deferred until after the
  1.5263 +** completion of the [sqlite3_step()] call that triggered the commit
  1.5264 +** or rollback hook in the first place.
  1.5265 +** Note that running any other SQL statements, including SELECT statements,
  1.5266 +** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
  1.5267 +** the database connections for the meaning of "modify" in this paragraph.
  1.5268 +**
  1.5269 +** ^Registering a NULL function disables the callback.
  1.5270 +**
  1.5271 +** ^When the commit hook callback routine returns zero, the [COMMIT]
  1.5272 +** operation is allowed to continue normally.  ^If the commit hook
  1.5273 +** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
  1.5274 +** ^The rollback hook is invoked on a rollback that results from a commit
  1.5275 +** hook returning non-zero, just as it would be with any other rollback.
  1.5276 +**
  1.5277 +** ^For the purposes of this API, a transaction is said to have been
  1.5278 +** rolled back if an explicit "ROLLBACK" statement is executed, or
  1.5279 +** an error or constraint causes an implicit rollback to occur.
  1.5280 +** ^The rollback callback is not invoked if a transaction is
  1.5281 +** automatically rolled back because the database connection is closed.
  1.5282 +**
  1.5283 +** See also the [sqlite3_update_hook()] interface.
  1.5284 +*/
  1.5285 +SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  1.5286 +SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  1.5287 +
  1.5288 +/*
  1.5289 +** CAPI3REF: Data Change Notification Callbacks
  1.5290 +**
  1.5291 +** ^The sqlite3_update_hook() interface registers a callback function
  1.5292 +** with the [database connection] identified by the first argument
  1.5293 +** to be invoked whenever a row is updated, inserted or deleted.
  1.5294 +** ^Any callback set by a previous call to this function
  1.5295 +** for the same database connection is overridden.
  1.5296 +**
  1.5297 +** ^The second argument is a pointer to the function to invoke when a
  1.5298 +** row is updated, inserted or deleted.
  1.5299 +** ^The first argument to the callback is a copy of the third argument
  1.5300 +** to sqlite3_update_hook().
  1.5301 +** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
  1.5302 +** or [SQLITE_UPDATE], depending on the operation that caused the callback
  1.5303 +** to be invoked.
  1.5304 +** ^The third and fourth arguments to the callback contain pointers to the
  1.5305 +** database and table name containing the affected row.
  1.5306 +** ^The final callback parameter is the [rowid] of the row.
  1.5307 +** ^In the case of an update, this is the [rowid] after the update takes place.
  1.5308 +**
  1.5309 +** ^(The update hook is not invoked when internal system tables are
  1.5310 +** modified (i.e. sqlite_master and sqlite_sequence).)^
  1.5311 +**
  1.5312 +** ^In the current implementation, the update hook
  1.5313 +** is not invoked when duplication rows are deleted because of an
  1.5314 +** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  1.5315 +** invoked when rows are deleted using the [truncate optimization].
  1.5316 +** The exceptions defined in this paragraph might change in a future
  1.5317 +** release of SQLite.
  1.5318 +**
  1.5319 +** The update hook implementation must not do anything that will modify
  1.5320 +** the database connection that invoked the update hook.  Any actions
  1.5321 +** to modify the database connection must be deferred until after the
  1.5322 +** completion of the [sqlite3_step()] call that triggered the update hook.
  1.5323 +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  1.5324 +** database connections for the meaning of "modify" in this paragraph.
  1.5325 +**
  1.5326 +** ^The sqlite3_update_hook(D,C,P) function
  1.5327 +** returns the P argument from the previous call
  1.5328 +** on the same [database connection] D, or NULL for
  1.5329 +** the first call on D.
  1.5330 +**
  1.5331 +** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
  1.5332 +** interfaces.
  1.5333 +*/
  1.5334 +SQLITE_API void *sqlite3_update_hook(
  1.5335 +  sqlite3*, 
  1.5336 +  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
  1.5337 +  void*
  1.5338 +);
  1.5339 +
  1.5340 +/*
  1.5341 +** CAPI3REF: Enable Or Disable Shared Pager Cache
  1.5342 +**
  1.5343 +** ^(This routine enables or disables the sharing of the database cache
  1.5344 +** and schema data structures between [database connection | connections]
  1.5345 +** to the same database. Sharing is enabled if the argument is true
  1.5346 +** and disabled if the argument is false.)^
  1.5347 +**
  1.5348 +** ^Cache sharing is enabled and disabled for an entire process.
  1.5349 +** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
  1.5350 +** sharing was enabled or disabled for each thread separately.
  1.5351 +**
  1.5352 +** ^(The cache sharing mode set by this interface effects all subsequent
  1.5353 +** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
  1.5354 +** Existing database connections continue use the sharing mode
  1.5355 +** that was in effect at the time they were opened.)^
  1.5356 +**
  1.5357 +** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
  1.5358 +** successfully.  An [error code] is returned otherwise.)^
  1.5359 +**
  1.5360 +** ^Shared cache is disabled by default. But this might change in
  1.5361 +** future releases of SQLite.  Applications that care about shared
  1.5362 +** cache setting should set it explicitly.
  1.5363 +**
  1.5364 +** This interface is threadsafe on processors where writing a
  1.5365 +** 32-bit integer is atomic.
  1.5366 +**
  1.5367 +** See Also:  [SQLite Shared-Cache Mode]
  1.5368 +*/
  1.5369 +SQLITE_API int sqlite3_enable_shared_cache(int);
  1.5370 +
  1.5371 +/*
  1.5372 +** CAPI3REF: Attempt To Free Heap Memory
  1.5373 +**
  1.5374 +** ^The sqlite3_release_memory() interface attempts to free N bytes
  1.5375 +** of heap memory by deallocating non-essential memory allocations
  1.5376 +** held by the database library.   Memory used to cache database
  1.5377 +** pages to improve performance is an example of non-essential memory.
  1.5378 +** ^sqlite3_release_memory() returns the number of bytes actually freed,
  1.5379 +** which might be more or less than the amount requested.
  1.5380 +** ^The sqlite3_release_memory() routine is a no-op returning zero
  1.5381 +** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  1.5382 +**
  1.5383 +** See also: [sqlite3_db_release_memory()]
  1.5384 +*/
  1.5385 +SQLITE_API int sqlite3_release_memory(int);
  1.5386 +
  1.5387 +/*
  1.5388 +** CAPI3REF: Free Memory Used By A Database Connection
  1.5389 +**
  1.5390 +** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  1.5391 +** memory as possible from database connection D. Unlike the
  1.5392 +** [sqlite3_release_memory()] interface, this interface is effect even
  1.5393 +** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  1.5394 +** omitted.
  1.5395 +**
  1.5396 +** See also: [sqlite3_release_memory()]
  1.5397 +*/
  1.5398 +SQLITE_API int sqlite3_db_release_memory(sqlite3*);
  1.5399 +
  1.5400 +/*
  1.5401 +** CAPI3REF: Impose A Limit On Heap Size
  1.5402 +**
  1.5403 +** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
  1.5404 +** soft limit on the amount of heap memory that may be allocated by SQLite.
  1.5405 +** ^SQLite strives to keep heap memory utilization below the soft heap
  1.5406 +** limit by reducing the number of pages held in the page cache
  1.5407 +** as heap memory usages approaches the limit.
  1.5408 +** ^The soft heap limit is "soft" because even though SQLite strives to stay
  1.5409 +** below the limit, it will exceed the limit rather than generate
  1.5410 +** an [SQLITE_NOMEM] error.  In other words, the soft heap limit 
  1.5411 +** is advisory only.
  1.5412 +**
  1.5413 +** ^The return value from sqlite3_soft_heap_limit64() is the size of
  1.5414 +** the soft heap limit prior to the call, or negative in the case of an
  1.5415 +** error.  ^If the argument N is negative
  1.5416 +** then no change is made to the soft heap limit.  Hence, the current
  1.5417 +** size of the soft heap limit can be determined by invoking
  1.5418 +** sqlite3_soft_heap_limit64() with a negative argument.
  1.5419 +**
  1.5420 +** ^If the argument N is zero then the soft heap limit is disabled.
  1.5421 +**
  1.5422 +** ^(The soft heap limit is not enforced in the current implementation
  1.5423 +** if one or more of following conditions are true:
  1.5424 +**
  1.5425 +** <ul>
  1.5426 +** <li> The soft heap limit is set to zero.
  1.5427 +** <li> Memory accounting is disabled using a combination of the
  1.5428 +**      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
  1.5429 +**      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
  1.5430 +** <li> An alternative page cache implementation is specified using
  1.5431 +**      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
  1.5432 +** <li> The page cache allocates from its own memory pool supplied
  1.5433 +**      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
  1.5434 +**      from the heap.
  1.5435 +** </ul>)^
  1.5436 +**
  1.5437 +** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
  1.5438 +** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
  1.5439 +** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
  1.5440 +** the soft heap limit is enforced on every memory allocation.  Without
  1.5441 +** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
  1.5442 +** when memory is allocated by the page cache.  Testing suggests that because
  1.5443 +** the page cache is the predominate memory user in SQLite, most
  1.5444 +** applications will achieve adequate soft heap limit enforcement without
  1.5445 +** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  1.5446 +**
  1.5447 +** The circumstances under which SQLite will enforce the soft heap limit may
  1.5448 +** changes in future releases of SQLite.
  1.5449 +*/
  1.5450 +SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
  1.5451 +
  1.5452 +/*
  1.5453 +** CAPI3REF: Deprecated Soft Heap Limit Interface
  1.5454 +** DEPRECATED
  1.5455 +**
  1.5456 +** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
  1.5457 +** interface.  This routine is provided for historical compatibility
  1.5458 +** only.  All new applications should use the
  1.5459 +** [sqlite3_soft_heap_limit64()] interface rather than this one.
  1.5460 +*/
  1.5461 +SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
  1.5462 +
  1.5463 +
  1.5464 +/*
  1.5465 +** CAPI3REF: Extract Metadata About A Column Of A Table
  1.5466 +**
  1.5467 +** ^This routine returns metadata about a specific column of a specific
  1.5468 +** database table accessible using the [database connection] handle
  1.5469 +** passed as the first function argument.
  1.5470 +**
  1.5471 +** ^The column is identified by the second, third and fourth parameters to
  1.5472 +** this function. ^The second parameter is either the name of the database
  1.5473 +** (i.e. "main", "temp", or an attached database) containing the specified
  1.5474 +** table or NULL. ^If it is NULL, then all attached databases are searched
  1.5475 +** for the table using the same algorithm used by the database engine to
  1.5476 +** resolve unqualified table references.
  1.5477 +**
  1.5478 +** ^The third and fourth parameters to this function are the table and column
  1.5479 +** name of the desired column, respectively. Neither of these parameters
  1.5480 +** may be NULL.
  1.5481 +**
  1.5482 +** ^Metadata is returned by writing to the memory locations passed as the 5th
  1.5483 +** and subsequent parameters to this function. ^Any of these arguments may be
  1.5484 +** NULL, in which case the corresponding element of metadata is omitted.
  1.5485 +**
  1.5486 +** ^(<blockquote>
  1.5487 +** <table border="1">
  1.5488 +** <tr><th> Parameter <th> Output<br>Type <th>  Description
  1.5489 +**
  1.5490 +** <tr><td> 5th <td> const char* <td> Data type
  1.5491 +** <tr><td> 6th <td> const char* <td> Name of default collation sequence
  1.5492 +** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
  1.5493 +** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
  1.5494 +** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
  1.5495 +** </table>
  1.5496 +** </blockquote>)^
  1.5497 +**
  1.5498 +** ^The memory pointed to by the character pointers returned for the
  1.5499 +** declaration type and collation sequence is valid only until the next
  1.5500 +** call to any SQLite API function.
  1.5501 +**
  1.5502 +** ^If the specified table is actually a view, an [error code] is returned.
  1.5503 +**
  1.5504 +** ^If the specified column is "rowid", "oid" or "_rowid_" and an
  1.5505 +** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
  1.5506 +** parameters are set for the explicitly declared column. ^(If there is no
  1.5507 +** explicitly declared [INTEGER PRIMARY KEY] column, then the output
  1.5508 +** parameters are set as follows:
  1.5509 +**
  1.5510 +** <pre>
  1.5511 +**     data type: "INTEGER"
  1.5512 +**     collation sequence: "BINARY"
  1.5513 +**     not null: 0
  1.5514 +**     primary key: 1
  1.5515 +**     auto increment: 0
  1.5516 +** </pre>)^
  1.5517 +**
  1.5518 +** ^(This function may load one or more schemas from database files. If an
  1.5519 +** error occurs during this process, or if the requested table or column
  1.5520 +** cannot be found, an [error code] is returned and an error message left
  1.5521 +** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
  1.5522 +**
  1.5523 +** ^This API is only available if the library was compiled with the
  1.5524 +** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
  1.5525 +*/
  1.5526 +SQLITE_API int sqlite3_table_column_metadata(
  1.5527 +  sqlite3 *db,                /* Connection handle */
  1.5528 +  const char *zDbName,        /* Database name or NULL */
  1.5529 +  const char *zTableName,     /* Table name */
  1.5530 +  const char *zColumnName,    /* Column name */
  1.5531 +  char const **pzDataType,    /* OUTPUT: Declared data type */
  1.5532 +  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
  1.5533 +  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  1.5534 +  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  1.5535 +  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  1.5536 +);
  1.5537 +
  1.5538 +/*
  1.5539 +** CAPI3REF: Load An Extension
  1.5540 +**
  1.5541 +** ^This interface loads an SQLite extension library from the named file.
  1.5542 +**
  1.5543 +** ^The sqlite3_load_extension() interface attempts to load an
  1.5544 +** SQLite extension library contained in the file zFile.
  1.5545 +**
  1.5546 +** ^The entry point is zProc.
  1.5547 +** ^zProc may be 0, in which case the name of the entry point
  1.5548 +** defaults to "sqlite3_extension_init".
  1.5549 +** ^The sqlite3_load_extension() interface returns
  1.5550 +** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
  1.5551 +** ^If an error occurs and pzErrMsg is not 0, then the
  1.5552 +** [sqlite3_load_extension()] interface shall attempt to
  1.5553 +** fill *pzErrMsg with error message text stored in memory
  1.5554 +** obtained from [sqlite3_malloc()]. The calling function
  1.5555 +** should free this memory by calling [sqlite3_free()].
  1.5556 +**
  1.5557 +** ^Extension loading must be enabled using
  1.5558 +** [sqlite3_enable_load_extension()] prior to calling this API,
  1.5559 +** otherwise an error will be returned.
  1.5560 +**
  1.5561 +** See also the [load_extension() SQL function].
  1.5562 +*/
  1.5563 +SQLITE_API int sqlite3_load_extension(
  1.5564 +  sqlite3 *db,          /* Load the extension into this database connection */
  1.5565 +  const char *zFile,    /* Name of the shared library containing extension */
  1.5566 +  const char *zProc,    /* Entry point.  Derived from zFile if 0 */
  1.5567 +  char **pzErrMsg       /* Put error message here if not 0 */
  1.5568 +);
  1.5569 +
  1.5570 +/*
  1.5571 +** CAPI3REF: Enable Or Disable Extension Loading
  1.5572 +**
  1.5573 +** ^So as not to open security holes in older applications that are
  1.5574 +** unprepared to deal with extension loading, and as a means of disabling
  1.5575 +** extension loading while evaluating user-entered SQL, the following API
  1.5576 +** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  1.5577 +**
  1.5578 +** ^Extension loading is off by default. See ticket #1863.
  1.5579 +** ^Call the sqlite3_enable_load_extension() routine with onoff==1
  1.5580 +** to turn extension loading on and call it with onoff==0 to turn
  1.5581 +** it back off again.
  1.5582 +*/
  1.5583 +SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
  1.5584 +
  1.5585 +/*
  1.5586 +** CAPI3REF: Automatically Load Statically Linked Extensions
  1.5587 +**
  1.5588 +** ^This interface causes the xEntryPoint() function to be invoked for
  1.5589 +** each new [database connection] that is created.  The idea here is that
  1.5590 +** xEntryPoint() is the entry point for a statically linked SQLite extension
  1.5591 +** that is to be automatically loaded into all new database connections.
  1.5592 +**
  1.5593 +** ^(Even though the function prototype shows that xEntryPoint() takes
  1.5594 +** no arguments and returns void, SQLite invokes xEntryPoint() with three
  1.5595 +** arguments and expects and integer result as if the signature of the
  1.5596 +** entry point where as follows:
  1.5597 +**
  1.5598 +** <blockquote><pre>
  1.5599 +** &nbsp;  int xEntryPoint(
  1.5600 +** &nbsp;    sqlite3 *db,
  1.5601 +** &nbsp;    const char **pzErrMsg,
  1.5602 +** &nbsp;    const struct sqlite3_api_routines *pThunk
  1.5603 +** &nbsp;  );
  1.5604 +** </pre></blockquote>)^
  1.5605 +**
  1.5606 +** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
  1.5607 +** point to an appropriate error message (obtained from [sqlite3_mprintf()])
  1.5608 +** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
  1.5609 +** is NULL before calling the xEntryPoint().  ^SQLite will invoke
  1.5610 +** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
  1.5611 +** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
  1.5612 +** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
  1.5613 +**
  1.5614 +** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
  1.5615 +** on the list of automatic extensions is a harmless no-op. ^No entry point
  1.5616 +** will be called more than once for each database connection that is opened.
  1.5617 +**
  1.5618 +** See also: [sqlite3_reset_auto_extension()].
  1.5619 +*/
  1.5620 +SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
  1.5621 +
  1.5622 +/*
  1.5623 +** CAPI3REF: Reset Automatic Extension Loading
  1.5624 +**
  1.5625 +** ^This interface disables all automatic extensions previously
  1.5626 +** registered using [sqlite3_auto_extension()].
  1.5627 +*/
  1.5628 +SQLITE_API void sqlite3_reset_auto_extension(void);
  1.5629 +
  1.5630 +/*
  1.5631 +** The interface to the virtual-table mechanism is currently considered
  1.5632 +** to be experimental.  The interface might change in incompatible ways.
  1.5633 +** If this is a problem for you, do not use the interface at this time.
  1.5634 +**
  1.5635 +** When the virtual-table mechanism stabilizes, we will declare the
  1.5636 +** interface fixed, support it indefinitely, and remove this comment.
  1.5637 +*/
  1.5638 +
  1.5639 +/*
  1.5640 +** Structures used by the virtual table interface
  1.5641 +*/
  1.5642 +typedef struct sqlite3_vtab sqlite3_vtab;
  1.5643 +typedef struct sqlite3_index_info sqlite3_index_info;
  1.5644 +typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
  1.5645 +typedef struct sqlite3_module sqlite3_module;
  1.5646 +
  1.5647 +/*
  1.5648 +** CAPI3REF: Virtual Table Object
  1.5649 +** KEYWORDS: sqlite3_module {virtual table module}
  1.5650 +**
  1.5651 +** This structure, sometimes called a "virtual table module", 
  1.5652 +** defines the implementation of a [virtual tables].  
  1.5653 +** This structure consists mostly of methods for the module.
  1.5654 +**
  1.5655 +** ^A virtual table module is created by filling in a persistent
  1.5656 +** instance of this structure and passing a pointer to that instance
  1.5657 +** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
  1.5658 +** ^The registration remains valid until it is replaced by a different
  1.5659 +** module or until the [database connection] closes.  The content
  1.5660 +** of this structure must not change while it is registered with
  1.5661 +** any database connection.
  1.5662 +*/
  1.5663 +struct sqlite3_module {
  1.5664 +  int iVersion;
  1.5665 +  int (*xCreate)(sqlite3*, void *pAux,
  1.5666 +               int argc, const char *const*argv,
  1.5667 +               sqlite3_vtab **ppVTab, char**);
  1.5668 +  int (*xConnect)(sqlite3*, void *pAux,
  1.5669 +               int argc, const char *const*argv,
  1.5670 +               sqlite3_vtab **ppVTab, char**);
  1.5671 +  int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
  1.5672 +  int (*xDisconnect)(sqlite3_vtab *pVTab);
  1.5673 +  int (*xDestroy)(sqlite3_vtab *pVTab);
  1.5674 +  int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
  1.5675 +  int (*xClose)(sqlite3_vtab_cursor*);
  1.5676 +  int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
  1.5677 +                int argc, sqlite3_value **argv);
  1.5678 +  int (*xNext)(sqlite3_vtab_cursor*);
  1.5679 +  int (*xEof)(sqlite3_vtab_cursor*);
  1.5680 +  int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
  1.5681 +  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
  1.5682 +  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
  1.5683 +  int (*xBegin)(sqlite3_vtab *pVTab);
  1.5684 +  int (*xSync)(sqlite3_vtab *pVTab);
  1.5685 +  int (*xCommit)(sqlite3_vtab *pVTab);
  1.5686 +  int (*xRollback)(sqlite3_vtab *pVTab);
  1.5687 +  int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
  1.5688 +                       void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
  1.5689 +                       void **ppArg);
  1.5690 +  int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  1.5691 +  /* The methods above are in version 1 of the sqlite_module object. Those 
  1.5692 +  ** below are for version 2 and greater. */
  1.5693 +  int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  1.5694 +  int (*xRelease)(sqlite3_vtab *pVTab, int);
  1.5695 +  int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
  1.5696 +};
  1.5697 +
  1.5698 +/*
  1.5699 +** CAPI3REF: Virtual Table Indexing Information
  1.5700 +** KEYWORDS: sqlite3_index_info
  1.5701 +**
  1.5702 +** The sqlite3_index_info structure and its substructures is used as part
  1.5703 +** of the [virtual table] interface to
  1.5704 +** pass information into and receive the reply from the [xBestIndex]
  1.5705 +** method of a [virtual table module].  The fields under **Inputs** are the
  1.5706 +** inputs to xBestIndex and are read-only.  xBestIndex inserts its
  1.5707 +** results into the **Outputs** fields.
  1.5708 +**
  1.5709 +** ^(The aConstraint[] array records WHERE clause constraints of the form:
  1.5710 +**
  1.5711 +** <blockquote>column OP expr</blockquote>
  1.5712 +**
  1.5713 +** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
  1.5714 +** stored in aConstraint[].op using one of the
  1.5715 +** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
  1.5716 +** ^(The index of the column is stored in
  1.5717 +** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
  1.5718 +** expr on the right-hand side can be evaluated (and thus the constraint
  1.5719 +** is usable) and false if it cannot.)^
  1.5720 +**
  1.5721 +** ^The optimizer automatically inverts terms of the form "expr OP column"
  1.5722 +** and makes other simplifications to the WHERE clause in an attempt to
  1.5723 +** get as many WHERE clause terms into the form shown above as possible.
  1.5724 +** ^The aConstraint[] array only reports WHERE clause terms that are
  1.5725 +** relevant to the particular virtual table being queried.
  1.5726 +**
  1.5727 +** ^Information about the ORDER BY clause is stored in aOrderBy[].
  1.5728 +** ^Each term of aOrderBy records a column of the ORDER BY clause.
  1.5729 +**
  1.5730 +** The [xBestIndex] method must fill aConstraintUsage[] with information
  1.5731 +** about what parameters to pass to xFilter.  ^If argvIndex>0 then
  1.5732 +** the right-hand side of the corresponding aConstraint[] is evaluated
  1.5733 +** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
  1.5734 +** is true, then the constraint is assumed to be fully handled by the
  1.5735 +** virtual table and is not checked again by SQLite.)^
  1.5736 +**
  1.5737 +** ^The idxNum and idxPtr values are recorded and passed into the
  1.5738 +** [xFilter] method.
  1.5739 +** ^[sqlite3_free()] is used to free idxPtr if and only if
  1.5740 +** needToFreeIdxPtr is true.
  1.5741 +**
  1.5742 +** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
  1.5743 +** the correct order to satisfy the ORDER BY clause so that no separate
  1.5744 +** sorting step is required.
  1.5745 +**
  1.5746 +** ^The estimatedCost value is an estimate of the cost of doing the
  1.5747 +** particular lookup.  A full scan of a table with N entries should have
  1.5748 +** a cost of N.  A binary search of a table of N entries should have a
  1.5749 +** cost of approximately log(N).
  1.5750 +*/
  1.5751 +struct sqlite3_index_info {
  1.5752 +  /* Inputs */
  1.5753 +  int nConstraint;           /* Number of entries in aConstraint */
  1.5754 +  struct sqlite3_index_constraint {
  1.5755 +     int iColumn;              /* Column on left-hand side of constraint */
  1.5756 +     unsigned char op;         /* Constraint operator */
  1.5757 +     unsigned char usable;     /* True if this constraint is usable */
  1.5758 +     int iTermOffset;          /* Used internally - xBestIndex should ignore */
  1.5759 +  } *aConstraint;            /* Table of WHERE clause constraints */
  1.5760 +  int nOrderBy;              /* Number of terms in the ORDER BY clause */
  1.5761 +  struct sqlite3_index_orderby {
  1.5762 +     int iColumn;              /* Column number */
  1.5763 +     unsigned char desc;       /* True for DESC.  False for ASC. */
  1.5764 +  } *aOrderBy;               /* The ORDER BY clause */
  1.5765 +  /* Outputs */
  1.5766 +  struct sqlite3_index_constraint_usage {
  1.5767 +    int argvIndex;           /* if >0, constraint is part of argv to xFilter */
  1.5768 +    unsigned char omit;      /* Do not code a test for this constraint */
  1.5769 +  } *aConstraintUsage;
  1.5770 +  int idxNum;                /* Number used to identify the index */
  1.5771 +  char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
  1.5772 +  int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
  1.5773 +  int orderByConsumed;       /* True if output is already ordered */
  1.5774 +  double estimatedCost;      /* Estimated cost of using this index */
  1.5775 +};
  1.5776 +
  1.5777 +/*
  1.5778 +** CAPI3REF: Virtual Table Constraint Operator Codes
  1.5779 +**
  1.5780 +** These macros defined the allowed values for the
  1.5781 +** [sqlite3_index_info].aConstraint[].op field.  Each value represents
  1.5782 +** an operator that is part of a constraint term in the wHERE clause of
  1.5783 +** a query that uses a [virtual table].
  1.5784 +*/
  1.5785 +#define SQLITE_INDEX_CONSTRAINT_EQ    2
  1.5786 +#define SQLITE_INDEX_CONSTRAINT_GT    4
  1.5787 +#define SQLITE_INDEX_CONSTRAINT_LE    8
  1.5788 +#define SQLITE_INDEX_CONSTRAINT_LT    16
  1.5789 +#define SQLITE_INDEX_CONSTRAINT_GE    32
  1.5790 +#define SQLITE_INDEX_CONSTRAINT_MATCH 64
  1.5791 +
  1.5792 +/*
  1.5793 +** CAPI3REF: Register A Virtual Table Implementation
  1.5794 +**
  1.5795 +** ^These routines are used to register a new [virtual table module] name.
  1.5796 +** ^Module names must be registered before
  1.5797 +** creating a new [virtual table] using the module and before using a
  1.5798 +** preexisting [virtual table] for the module.
  1.5799 +**
  1.5800 +** ^The module name is registered on the [database connection] specified
  1.5801 +** by the first parameter.  ^The name of the module is given by the 
  1.5802 +** second parameter.  ^The third parameter is a pointer to
  1.5803 +** the implementation of the [virtual table module].   ^The fourth
  1.5804 +** parameter is an arbitrary client data pointer that is passed through
  1.5805 +** into the [xCreate] and [xConnect] methods of the virtual table module
  1.5806 +** when a new virtual table is be being created or reinitialized.
  1.5807 +**
  1.5808 +** ^The sqlite3_create_module_v2() interface has a fifth parameter which
  1.5809 +** is a pointer to a destructor for the pClientData.  ^SQLite will
  1.5810 +** invoke the destructor function (if it is not NULL) when SQLite
  1.5811 +** no longer needs the pClientData pointer.  ^The destructor will also
  1.5812 +** be invoked if the call to sqlite3_create_module_v2() fails.
  1.5813 +** ^The sqlite3_create_module()
  1.5814 +** interface is equivalent to sqlite3_create_module_v2() with a NULL
  1.5815 +** destructor.
  1.5816 +*/
  1.5817 +SQLITE_API int sqlite3_create_module(
  1.5818 +  sqlite3 *db,               /* SQLite connection to register module with */
  1.5819 +  const char *zName,         /* Name of the module */
  1.5820 +  const sqlite3_module *p,   /* Methods for the module */
  1.5821 +  void *pClientData          /* Client data for xCreate/xConnect */
  1.5822 +);
  1.5823 +SQLITE_API int sqlite3_create_module_v2(
  1.5824 +  sqlite3 *db,               /* SQLite connection to register module with */
  1.5825 +  const char *zName,         /* Name of the module */
  1.5826 +  const sqlite3_module *p,   /* Methods for the module */
  1.5827 +  void *pClientData,         /* Client data for xCreate/xConnect */
  1.5828 +  void(*xDestroy)(void*)     /* Module destructor function */
  1.5829 +);
  1.5830 +
  1.5831 +/*
  1.5832 +** CAPI3REF: Virtual Table Instance Object
  1.5833 +** KEYWORDS: sqlite3_vtab
  1.5834 +**
  1.5835 +** Every [virtual table module] implementation uses a subclass
  1.5836 +** of this object to describe a particular instance
  1.5837 +** of the [virtual table].  Each subclass will
  1.5838 +** be tailored to the specific needs of the module implementation.
  1.5839 +** The purpose of this superclass is to define certain fields that are
  1.5840 +** common to all module implementations.
  1.5841 +**
  1.5842 +** ^Virtual tables methods can set an error message by assigning a
  1.5843 +** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
  1.5844 +** take care that any prior string is freed by a call to [sqlite3_free()]
  1.5845 +** prior to assigning a new string to zErrMsg.  ^After the error message
  1.5846 +** is delivered up to the client application, the string will be automatically
  1.5847 +** freed by sqlite3_free() and the zErrMsg field will be zeroed.
  1.5848 +*/
  1.5849 +struct sqlite3_vtab {
  1.5850 +  const sqlite3_module *pModule;  /* The module for this virtual table */
  1.5851 +  int nRef;                       /* NO LONGER USED */
  1.5852 +  char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
  1.5853 +  /* Virtual table implementations will typically add additional fields */
  1.5854 +};
  1.5855 +
  1.5856 +/*
  1.5857 +** CAPI3REF: Virtual Table Cursor Object
  1.5858 +** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
  1.5859 +**
  1.5860 +** Every [virtual table module] implementation uses a subclass of the
  1.5861 +** following structure to describe cursors that point into the
  1.5862 +** [virtual table] and are used
  1.5863 +** to loop through the virtual table.  Cursors are created using the
  1.5864 +** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
  1.5865 +** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
  1.5866 +** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
  1.5867 +** of the module.  Each module implementation will define
  1.5868 +** the content of a cursor structure to suit its own needs.
  1.5869 +**
  1.5870 +** This superclass exists in order to define fields of the cursor that
  1.5871 +** are common to all implementations.
  1.5872 +*/
  1.5873 +struct sqlite3_vtab_cursor {
  1.5874 +  sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
  1.5875 +  /* Virtual table implementations will typically add additional fields */
  1.5876 +};
  1.5877 +
  1.5878 +/*
  1.5879 +** CAPI3REF: Declare The Schema Of A Virtual Table
  1.5880 +**
  1.5881 +** ^The [xCreate] and [xConnect] methods of a
  1.5882 +** [virtual table module] call this interface
  1.5883 +** to declare the format (the names and datatypes of the columns) of
  1.5884 +** the virtual tables they implement.
  1.5885 +*/
  1.5886 +SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  1.5887 +
  1.5888 +/*
  1.5889 +** CAPI3REF: Overload A Function For A Virtual Table
  1.5890 +**
  1.5891 +** ^(Virtual tables can provide alternative implementations of functions
  1.5892 +** using the [xFindFunction] method of the [virtual table module].  
  1.5893 +** But global versions of those functions
  1.5894 +** must exist in order to be overloaded.)^
  1.5895 +**
  1.5896 +** ^(This API makes sure a global version of a function with a particular
  1.5897 +** name and number of parameters exists.  If no such function exists
  1.5898 +** before this API is called, a new function is created.)^  ^The implementation
  1.5899 +** of the new function always causes an exception to be thrown.  So
  1.5900 +** the new function is not good for anything by itself.  Its only
  1.5901 +** purpose is to be a placeholder function that can be overloaded
  1.5902 +** by a [virtual table].
  1.5903 +*/
  1.5904 +SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
  1.5905 +
  1.5906 +/*
  1.5907 +** The interface to the virtual-table mechanism defined above (back up
  1.5908 +** to a comment remarkably similar to this one) is currently considered
  1.5909 +** to be experimental.  The interface might change in incompatible ways.
  1.5910 +** If this is a problem for you, do not use the interface at this time.
  1.5911 +**
  1.5912 +** When the virtual-table mechanism stabilizes, we will declare the
  1.5913 +** interface fixed, support it indefinitely, and remove this comment.
  1.5914 +*/
  1.5915 +
  1.5916 +/*
  1.5917 +** CAPI3REF: A Handle To An Open BLOB
  1.5918 +** KEYWORDS: {BLOB handle} {BLOB handles}
  1.5919 +**
  1.5920 +** An instance of this object represents an open BLOB on which
  1.5921 +** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
  1.5922 +** ^Objects of this type are created by [sqlite3_blob_open()]
  1.5923 +** and destroyed by [sqlite3_blob_close()].
  1.5924 +** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
  1.5925 +** can be used to read or write small subsections of the BLOB.
  1.5926 +** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  1.5927 +*/
  1.5928 +typedef struct sqlite3_blob sqlite3_blob;
  1.5929 +
  1.5930 +/*
  1.5931 +** CAPI3REF: Open A BLOB For Incremental I/O
  1.5932 +**
  1.5933 +** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  1.5934 +** in row iRow, column zColumn, table zTable in database zDb;
  1.5935 +** in other words, the same BLOB that would be selected by:
  1.5936 +**
  1.5937 +** <pre>
  1.5938 +**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
  1.5939 +** </pre>)^
  1.5940 +**
  1.5941 +** ^If the flags parameter is non-zero, then the BLOB is opened for read
  1.5942 +** and write access. ^If it is zero, the BLOB is opened for read access.
  1.5943 +** ^It is not possible to open a column that is part of an index or primary 
  1.5944 +** key for writing. ^If [foreign key constraints] are enabled, it is 
  1.5945 +** not possible to open a column that is part of a [child key] for writing.
  1.5946 +**
  1.5947 +** ^Note that the database name is not the filename that contains
  1.5948 +** the database but rather the symbolic name of the database that
  1.5949 +** appears after the AS keyword when the database is connected using [ATTACH].
  1.5950 +** ^For the main database file, the database name is "main".
  1.5951 +** ^For TEMP tables, the database name is "temp".
  1.5952 +**
  1.5953 +** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
  1.5954 +** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
  1.5955 +** to be a null pointer.)^
  1.5956 +** ^This function sets the [database connection] error code and message
  1.5957 +** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
  1.5958 +** functions. ^Note that the *ppBlob variable is always initialized in a
  1.5959 +** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
  1.5960 +** regardless of the success or failure of this routine.
  1.5961 +**
  1.5962 +** ^(If the row that a BLOB handle points to is modified by an
  1.5963 +** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  1.5964 +** then the BLOB handle is marked as "expired".
  1.5965 +** This is true if any column of the row is changed, even a column
  1.5966 +** other than the one the BLOB handle is open on.)^
  1.5967 +** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
  1.5968 +** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
  1.5969 +** ^(Changes written into a BLOB prior to the BLOB expiring are not
  1.5970 +** rolled back by the expiration of the BLOB.  Such changes will eventually
  1.5971 +** commit if the transaction continues to completion.)^
  1.5972 +**
  1.5973 +** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
  1.5974 +** the opened blob.  ^The size of a blob may not be changed by this
  1.5975 +** interface.  Use the [UPDATE] SQL command to change the size of a
  1.5976 +** blob.
  1.5977 +**
  1.5978 +** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  1.5979 +** and the built-in [zeroblob] SQL function can be used, if desired,
  1.5980 +** to create an empty, zero-filled blob in which to read or write using
  1.5981 +** this interface.
  1.5982 +**
  1.5983 +** To avoid a resource leak, every open [BLOB handle] should eventually
  1.5984 +** be released by a call to [sqlite3_blob_close()].
  1.5985 +*/
  1.5986 +SQLITE_API int sqlite3_blob_open(
  1.5987 +  sqlite3*,
  1.5988 +  const char *zDb,
  1.5989 +  const char *zTable,
  1.5990 +  const char *zColumn,
  1.5991 +  sqlite3_int64 iRow,
  1.5992 +  int flags,
  1.5993 +  sqlite3_blob **ppBlob
  1.5994 +);
  1.5995 +
  1.5996 +/*
  1.5997 +** CAPI3REF: Move a BLOB Handle to a New Row
  1.5998 +**
  1.5999 +** ^This function is used to move an existing blob handle so that it points
  1.6000 +** to a different row of the same database table. ^The new row is identified
  1.6001 +** by the rowid value passed as the second argument. Only the row can be
  1.6002 +** changed. ^The database, table and column on which the blob handle is open
  1.6003 +** remain the same. Moving an existing blob handle to a new row can be
  1.6004 +** faster than closing the existing handle and opening a new one.
  1.6005 +**
  1.6006 +** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  1.6007 +** it must exist and there must be either a blob or text value stored in
  1.6008 +** the nominated column.)^ ^If the new row is not present in the table, or if
  1.6009 +** it does not contain a blob or text value, or if another error occurs, an
  1.6010 +** SQLite error code is returned and the blob handle is considered aborted.
  1.6011 +** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
  1.6012 +** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
  1.6013 +** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
  1.6014 +** always returns zero.
  1.6015 +**
  1.6016 +** ^This function sets the database handle error code and message.
  1.6017 +*/
  1.6018 +SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  1.6019 +
  1.6020 +/*
  1.6021 +** CAPI3REF: Close A BLOB Handle
  1.6022 +**
  1.6023 +** ^Closes an open [BLOB handle].
  1.6024 +**
  1.6025 +** ^Closing a BLOB shall cause the current transaction to commit
  1.6026 +** if there are no other BLOBs, no pending prepared statements, and the
  1.6027 +** database connection is in [autocommit mode].
  1.6028 +** ^If any writes were made to the BLOB, they might be held in cache
  1.6029 +** until the close operation if they will fit.
  1.6030 +**
  1.6031 +** ^(Closing the BLOB often forces the changes
  1.6032 +** out to disk and so if any I/O errors occur, they will likely occur
  1.6033 +** at the time when the BLOB is closed.  Any errors that occur during
  1.6034 +** closing are reported as a non-zero return value.)^
  1.6035 +**
  1.6036 +** ^(The BLOB is closed unconditionally.  Even if this routine returns
  1.6037 +** an error code, the BLOB is still closed.)^
  1.6038 +**
  1.6039 +** ^Calling this routine with a null pointer (such as would be returned
  1.6040 +** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
  1.6041 +*/
  1.6042 +SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
  1.6043 +
  1.6044 +/*
  1.6045 +** CAPI3REF: Return The Size Of An Open BLOB
  1.6046 +**
  1.6047 +** ^Returns the size in bytes of the BLOB accessible via the 
  1.6048 +** successfully opened [BLOB handle] in its only argument.  ^The
  1.6049 +** incremental blob I/O routines can only read or overwriting existing
  1.6050 +** blob content; they cannot change the size of a blob.
  1.6051 +**
  1.6052 +** This routine only works on a [BLOB handle] which has been created
  1.6053 +** by a prior successful call to [sqlite3_blob_open()] and which has not
  1.6054 +** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  1.6055 +** to this routine results in undefined and probably undesirable behavior.
  1.6056 +*/
  1.6057 +SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
  1.6058 +
  1.6059 +/*
  1.6060 +** CAPI3REF: Read Data From A BLOB Incrementally
  1.6061 +**
  1.6062 +** ^(This function is used to read data from an open [BLOB handle] into a
  1.6063 +** caller-supplied buffer. N bytes of data are copied into buffer Z
  1.6064 +** from the open BLOB, starting at offset iOffset.)^
  1.6065 +**
  1.6066 +** ^If offset iOffset is less than N bytes from the end of the BLOB,
  1.6067 +** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
  1.6068 +** less than zero, [SQLITE_ERROR] is returned and no data is read.
  1.6069 +** ^The size of the blob (and hence the maximum value of N+iOffset)
  1.6070 +** can be determined using the [sqlite3_blob_bytes()] interface.
  1.6071 +**
  1.6072 +** ^An attempt to read from an expired [BLOB handle] fails with an
  1.6073 +** error code of [SQLITE_ABORT].
  1.6074 +**
  1.6075 +** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
  1.6076 +** Otherwise, an [error code] or an [extended error code] is returned.)^
  1.6077 +**
  1.6078 +** This routine only works on a [BLOB handle] which has been created
  1.6079 +** by a prior successful call to [sqlite3_blob_open()] and which has not
  1.6080 +** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  1.6081 +** to this routine results in undefined and probably undesirable behavior.
  1.6082 +**
  1.6083 +** See also: [sqlite3_blob_write()].
  1.6084 +*/
  1.6085 +SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  1.6086 +
  1.6087 +/*
  1.6088 +** CAPI3REF: Write Data Into A BLOB Incrementally
  1.6089 +**
  1.6090 +** ^This function is used to write data into an open [BLOB handle] from a
  1.6091 +** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
  1.6092 +** into the open BLOB, starting at offset iOffset.
  1.6093 +**
  1.6094 +** ^If the [BLOB handle] passed as the first argument was not opened for
  1.6095 +** writing (the flags parameter to [sqlite3_blob_open()] was zero),
  1.6096 +** this function returns [SQLITE_READONLY].
  1.6097 +**
  1.6098 +** ^This function may only modify the contents of the BLOB; it is
  1.6099 +** not possible to increase the size of a BLOB using this API.
  1.6100 +** ^If offset iOffset is less than N bytes from the end of the BLOB,
  1.6101 +** [SQLITE_ERROR] is returned and no data is written.  ^If N is
  1.6102 +** less than zero [SQLITE_ERROR] is returned and no data is written.
  1.6103 +** The size of the BLOB (and hence the maximum value of N+iOffset)
  1.6104 +** can be determined using the [sqlite3_blob_bytes()] interface.
  1.6105 +**
  1.6106 +** ^An attempt to write to an expired [BLOB handle] fails with an
  1.6107 +** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
  1.6108 +** before the [BLOB handle] expired are not rolled back by the
  1.6109 +** expiration of the handle, though of course those changes might
  1.6110 +** have been overwritten by the statement that expired the BLOB handle
  1.6111 +** or by other independent statements.
  1.6112 +**
  1.6113 +** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  1.6114 +** Otherwise, an  [error code] or an [extended error code] is returned.)^
  1.6115 +**
  1.6116 +** This routine only works on a [BLOB handle] which has been created
  1.6117 +** by a prior successful call to [sqlite3_blob_open()] and which has not
  1.6118 +** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  1.6119 +** to this routine results in undefined and probably undesirable behavior.
  1.6120 +**
  1.6121 +** See also: [sqlite3_blob_read()].
  1.6122 +*/
  1.6123 +SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
  1.6124 +
  1.6125 +/*
  1.6126 +** CAPI3REF: Virtual File System Objects
  1.6127 +**
  1.6128 +** A virtual filesystem (VFS) is an [sqlite3_vfs] object
  1.6129 +** that SQLite uses to interact
  1.6130 +** with the underlying operating system.  Most SQLite builds come with a
  1.6131 +** single default VFS that is appropriate for the host computer.
  1.6132 +** New VFSes can be registered and existing VFSes can be unregistered.
  1.6133 +** The following interfaces are provided.
  1.6134 +**
  1.6135 +** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
  1.6136 +** ^Names are case sensitive.
  1.6137 +** ^Names are zero-terminated UTF-8 strings.
  1.6138 +** ^If there is no match, a NULL pointer is returned.
  1.6139 +** ^If zVfsName is NULL then the default VFS is returned.
  1.6140 +**
  1.6141 +** ^New VFSes are registered with sqlite3_vfs_register().
  1.6142 +** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
  1.6143 +** ^The same VFS can be registered multiple times without injury.
  1.6144 +** ^To make an existing VFS into the default VFS, register it again
  1.6145 +** with the makeDflt flag set.  If two different VFSes with the
  1.6146 +** same name are registered, the behavior is undefined.  If a
  1.6147 +** VFS is registered with a name that is NULL or an empty string,
  1.6148 +** then the behavior is undefined.
  1.6149 +**
  1.6150 +** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
  1.6151 +** ^(If the default VFS is unregistered, another VFS is chosen as
  1.6152 +** the default.  The choice for the new VFS is arbitrary.)^
  1.6153 +*/
  1.6154 +SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
  1.6155 +SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
  1.6156 +SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
  1.6157 +
  1.6158 +/*
  1.6159 +** CAPI3REF: Mutexes
  1.6160 +**
  1.6161 +** The SQLite core uses these routines for thread
  1.6162 +** synchronization. Though they are intended for internal
  1.6163 +** use by SQLite, code that links against SQLite is
  1.6164 +** permitted to use any of these routines.
  1.6165 +**
  1.6166 +** The SQLite source code contains multiple implementations
  1.6167 +** of these mutex routines.  An appropriate implementation
  1.6168 +** is selected automatically at compile-time.  ^(The following
  1.6169 +** implementations are available in the SQLite core:
  1.6170 +**
  1.6171 +** <ul>
  1.6172 +** <li>   SQLITE_MUTEX_PTHREADS
  1.6173 +** <li>   SQLITE_MUTEX_W32
  1.6174 +** <li>   SQLITE_MUTEX_NOOP
  1.6175 +** </ul>)^
  1.6176 +**
  1.6177 +** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  1.6178 +** that does no real locking and is appropriate for use in
  1.6179 +** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
  1.6180 +** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
  1.6181 +** and Windows.
  1.6182 +**
  1.6183 +** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  1.6184 +** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  1.6185 +** implementation is included with the library. In this case the
  1.6186 +** application must supply a custom mutex implementation using the
  1.6187 +** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
  1.6188 +** before calling sqlite3_initialize() or any other public sqlite3_
  1.6189 +** function that calls sqlite3_initialize().)^
  1.6190 +**
  1.6191 +** ^The sqlite3_mutex_alloc() routine allocates a new
  1.6192 +** mutex and returns a pointer to it. ^If it returns NULL
  1.6193 +** that means that a mutex could not be allocated.  ^SQLite
  1.6194 +** will unwind its stack and return an error.  ^(The argument
  1.6195 +** to sqlite3_mutex_alloc() is one of these integer constants:
  1.6196 +**
  1.6197 +** <ul>
  1.6198 +** <li>  SQLITE_MUTEX_FAST
  1.6199 +** <li>  SQLITE_MUTEX_RECURSIVE
  1.6200 +** <li>  SQLITE_MUTEX_STATIC_MASTER
  1.6201 +** <li>  SQLITE_MUTEX_STATIC_MEM
  1.6202 +** <li>  SQLITE_MUTEX_STATIC_MEM2
  1.6203 +** <li>  SQLITE_MUTEX_STATIC_PRNG
  1.6204 +** <li>  SQLITE_MUTEX_STATIC_LRU
  1.6205 +** <li>  SQLITE_MUTEX_STATIC_LRU2
  1.6206 +** </ul>)^
  1.6207 +**
  1.6208 +** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
  1.6209 +** cause sqlite3_mutex_alloc() to create
  1.6210 +** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  1.6211 +** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  1.6212 +** The mutex implementation does not need to make a distinction
  1.6213 +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  1.6214 +** not want to.  ^SQLite will only request a recursive mutex in
  1.6215 +** cases where it really needs one.  ^If a faster non-recursive mutex
  1.6216 +** implementation is available on the host platform, the mutex subsystem
  1.6217 +** might return such a mutex in response to SQLITE_MUTEX_FAST.
  1.6218 +**
  1.6219 +** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
  1.6220 +** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
  1.6221 +** a pointer to a static preexisting mutex.  ^Six static mutexes are
  1.6222 +** used by the current version of SQLite.  Future versions of SQLite
  1.6223 +** may add additional static mutexes.  Static mutexes are for internal
  1.6224 +** use by SQLite only.  Applications that use SQLite mutexes should
  1.6225 +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  1.6226 +** SQLITE_MUTEX_RECURSIVE.
  1.6227 +**
  1.6228 +** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  1.6229 +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  1.6230 +** returns a different mutex on every call.  ^But for the static
  1.6231 +** mutex types, the same mutex is returned on every call that has
  1.6232 +** the same type number.
  1.6233 +**
  1.6234 +** ^The sqlite3_mutex_free() routine deallocates a previously
  1.6235 +** allocated dynamic mutex.  ^SQLite is careful to deallocate every
  1.6236 +** dynamic mutex that it allocates.  The dynamic mutexes must not be in
  1.6237 +** use when they are deallocated.  Attempting to deallocate a static
  1.6238 +** mutex results in undefined behavior.  ^SQLite never deallocates
  1.6239 +** a static mutex.
  1.6240 +**
  1.6241 +** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  1.6242 +** to enter a mutex.  ^If another thread is already within the mutex,
  1.6243 +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  1.6244 +** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
  1.6245 +** upon successful entry.  ^(Mutexes created using
  1.6246 +** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
  1.6247 +** In such cases the,
  1.6248 +** mutex must be exited an equal number of times before another thread
  1.6249 +** can enter.)^  ^(If the same thread tries to enter any other
  1.6250 +** kind of mutex more than once, the behavior is undefined.
  1.6251 +** SQLite will never exhibit
  1.6252 +** such behavior in its own use of mutexes.)^
  1.6253 +**
  1.6254 +** ^(Some systems (for example, Windows 95) do not support the operation
  1.6255 +** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
  1.6256 +** will always return SQLITE_BUSY.  The SQLite core only ever uses
  1.6257 +** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
  1.6258 +**
  1.6259 +** ^The sqlite3_mutex_leave() routine exits a mutex that was
  1.6260 +** previously entered by the same thread.   ^(The behavior
  1.6261 +** is undefined if the mutex is not currently entered by the
  1.6262 +** calling thread or is not currently allocated.  SQLite will
  1.6263 +** never do either.)^
  1.6264 +**
  1.6265 +** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
  1.6266 +** sqlite3_mutex_leave() is a NULL pointer, then all three routines
  1.6267 +** behave as no-ops.
  1.6268 +**
  1.6269 +** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
  1.6270 +*/
  1.6271 +SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
  1.6272 +SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
  1.6273 +SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
  1.6274 +SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
  1.6275 +SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
  1.6276 +
  1.6277 +/*
  1.6278 +** CAPI3REF: Mutex Methods Object
  1.6279 +**
  1.6280 +** An instance of this structure defines the low-level routines
  1.6281 +** used to allocate and use mutexes.
  1.6282 +**
  1.6283 +** Usually, the default mutex implementations provided by SQLite are
  1.6284 +** sufficient, however the user has the option of substituting a custom
  1.6285 +** implementation for specialized deployments or systems for which SQLite
  1.6286 +** does not provide a suitable implementation. In this case, the user
  1.6287 +** creates and populates an instance of this structure to pass
  1.6288 +** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
  1.6289 +** Additionally, an instance of this structure can be used as an
  1.6290 +** output variable when querying the system for the current mutex
  1.6291 +** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
  1.6292 +**
  1.6293 +** ^The xMutexInit method defined by this structure is invoked as
  1.6294 +** part of system initialization by the sqlite3_initialize() function.
  1.6295 +** ^The xMutexInit routine is called by SQLite exactly once for each
  1.6296 +** effective call to [sqlite3_initialize()].
  1.6297 +**
  1.6298 +** ^The xMutexEnd method defined by this structure is invoked as
  1.6299 +** part of system shutdown by the sqlite3_shutdown() function. The
  1.6300 +** implementation of this method is expected to release all outstanding
  1.6301 +** resources obtained by the mutex methods implementation, especially
  1.6302 +** those obtained by the xMutexInit method.  ^The xMutexEnd()
  1.6303 +** interface is invoked exactly once for each call to [sqlite3_shutdown()].
  1.6304 +**
  1.6305 +** ^(The remaining seven methods defined by this structure (xMutexAlloc,
  1.6306 +** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
  1.6307 +** xMutexNotheld) implement the following interfaces (respectively):
  1.6308 +**
  1.6309 +** <ul>
  1.6310 +**   <li>  [sqlite3_mutex_alloc()] </li>
  1.6311 +**   <li>  [sqlite3_mutex_free()] </li>
  1.6312 +**   <li>  [sqlite3_mutex_enter()] </li>
  1.6313 +**   <li>  [sqlite3_mutex_try()] </li>
  1.6314 +**   <li>  [sqlite3_mutex_leave()] </li>
  1.6315 +**   <li>  [sqlite3_mutex_held()] </li>
  1.6316 +**   <li>  [sqlite3_mutex_notheld()] </li>
  1.6317 +** </ul>)^
  1.6318 +**
  1.6319 +** The only difference is that the public sqlite3_XXX functions enumerated
  1.6320 +** above silently ignore any invocations that pass a NULL pointer instead
  1.6321 +** of a valid mutex handle. The implementations of the methods defined
  1.6322 +** by this structure are not required to handle this case, the results
  1.6323 +** of passing a NULL pointer instead of a valid mutex handle are undefined
  1.6324 +** (i.e. it is acceptable to provide an implementation that segfaults if
  1.6325 +** it is passed a NULL pointer).
  1.6326 +**
  1.6327 +** The xMutexInit() method must be threadsafe.  ^It must be harmless to
  1.6328 +** invoke xMutexInit() multiple times within the same process and without
  1.6329 +** intervening calls to xMutexEnd().  Second and subsequent calls to
  1.6330 +** xMutexInit() must be no-ops.
  1.6331 +**
  1.6332 +** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
  1.6333 +** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
  1.6334 +** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
  1.6335 +** memory allocation for a fast or recursive mutex.
  1.6336 +**
  1.6337 +** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
  1.6338 +** called, but only if the prior call to xMutexInit returned SQLITE_OK.
  1.6339 +** If xMutexInit fails in any way, it is expected to clean up after itself
  1.6340 +** prior to returning.
  1.6341 +*/
  1.6342 +typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
  1.6343 +struct sqlite3_mutex_methods {
  1.6344 +  int (*xMutexInit)(void);
  1.6345 +  int (*xMutexEnd)(void);
  1.6346 +  sqlite3_mutex *(*xMutexAlloc)(int);
  1.6347 +  void (*xMutexFree)(sqlite3_mutex *);
  1.6348 +  void (*xMutexEnter)(sqlite3_mutex *);
  1.6349 +  int (*xMutexTry)(sqlite3_mutex *);
  1.6350 +  void (*xMutexLeave)(sqlite3_mutex *);
  1.6351 +  int (*xMutexHeld)(sqlite3_mutex *);
  1.6352 +  int (*xMutexNotheld)(sqlite3_mutex *);
  1.6353 +};
  1.6354 +
  1.6355 +/*
  1.6356 +** CAPI3REF: Mutex Verification Routines
  1.6357 +**
  1.6358 +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
  1.6359 +** are intended for use inside assert() statements.  ^The SQLite core
  1.6360 +** never uses these routines except inside an assert() and applications
  1.6361 +** are advised to follow the lead of the core.  ^The SQLite core only
  1.6362 +** provides implementations for these routines when it is compiled
  1.6363 +** with the SQLITE_DEBUG flag.  ^External mutex implementations
  1.6364 +** are only required to provide these routines if SQLITE_DEBUG is
  1.6365 +** defined and if NDEBUG is not defined.
  1.6366 +**
  1.6367 +** ^These routines should return true if the mutex in their argument
  1.6368 +** is held or not held, respectively, by the calling thread.
  1.6369 +**
  1.6370 +** ^The implementation is not required to provide versions of these
  1.6371 +** routines that actually work. If the implementation does not provide working
  1.6372 +** versions of these routines, it should at least provide stubs that always
  1.6373 +** return true so that one does not get spurious assertion failures.
  1.6374 +**
  1.6375 +** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
  1.6376 +** the routine should return 1.   This seems counter-intuitive since
  1.6377 +** clearly the mutex cannot be held if it does not exist.  But
  1.6378 +** the reason the mutex does not exist is because the build is not
  1.6379 +** using mutexes.  And we do not want the assert() containing the
  1.6380 +** call to sqlite3_mutex_held() to fail, so a non-zero return is
  1.6381 +** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
  1.6382 +** interface should also return 1 when given a NULL pointer.
  1.6383 +*/
  1.6384 +#ifndef NDEBUG
  1.6385 +SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
  1.6386 +SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
  1.6387 +#endif
  1.6388 +
  1.6389 +/*
  1.6390 +** CAPI3REF: Mutex Types
  1.6391 +**
  1.6392 +** The [sqlite3_mutex_alloc()] interface takes a single argument
  1.6393 +** which is one of these integer constants.
  1.6394 +**
  1.6395 +** The set of static mutexes may change from one SQLite release to the
  1.6396 +** next.  Applications that override the built-in mutex logic must be
  1.6397 +** prepared to accommodate additional static mutexes.
  1.6398 +*/
  1.6399 +#define SQLITE_MUTEX_FAST             0
  1.6400 +#define SQLITE_MUTEX_RECURSIVE        1
  1.6401 +#define SQLITE_MUTEX_STATIC_MASTER    2
  1.6402 +#define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  1.6403 +#define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  1.6404 +#define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  1.6405 +#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
  1.6406 +#define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  1.6407 +#define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
  1.6408 +#define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  1.6409 +
  1.6410 +/*
  1.6411 +** CAPI3REF: Retrieve the mutex for a database connection
  1.6412 +**
  1.6413 +** ^This interface returns a pointer the [sqlite3_mutex] object that 
  1.6414 +** serializes access to the [database connection] given in the argument
  1.6415 +** when the [threading mode] is Serialized.
  1.6416 +** ^If the [threading mode] is Single-thread or Multi-thread then this
  1.6417 +** routine returns a NULL pointer.
  1.6418 +*/
  1.6419 +SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
  1.6420 +
  1.6421 +/*
  1.6422 +** CAPI3REF: Low-Level Control Of Database Files
  1.6423 +**
  1.6424 +** ^The [sqlite3_file_control()] interface makes a direct call to the
  1.6425 +** xFileControl method for the [sqlite3_io_methods] object associated
  1.6426 +** with a particular database identified by the second argument. ^The
  1.6427 +** name of the database is "main" for the main database or "temp" for the
  1.6428 +** TEMP database, or the name that appears after the AS keyword for
  1.6429 +** databases that are added using the [ATTACH] SQL command.
  1.6430 +** ^A NULL pointer can be used in place of "main" to refer to the
  1.6431 +** main database file.
  1.6432 +** ^The third and fourth parameters to this routine
  1.6433 +** are passed directly through to the second and third parameters of
  1.6434 +** the xFileControl method.  ^The return value of the xFileControl
  1.6435 +** method becomes the return value of this routine.
  1.6436 +**
  1.6437 +** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
  1.6438 +** a pointer to the underlying [sqlite3_file] object to be written into
  1.6439 +** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
  1.6440 +** case is a short-circuit path which does not actually invoke the
  1.6441 +** underlying sqlite3_io_methods.xFileControl method.
  1.6442 +**
  1.6443 +** ^If the second parameter (zDbName) does not match the name of any
  1.6444 +** open database file, then SQLITE_ERROR is returned.  ^This error
  1.6445 +** code is not remembered and will not be recalled by [sqlite3_errcode()]
  1.6446 +** or [sqlite3_errmsg()].  The underlying xFileControl method might
  1.6447 +** also return SQLITE_ERROR.  There is no way to distinguish between
  1.6448 +** an incorrect zDbName and an SQLITE_ERROR return from the underlying
  1.6449 +** xFileControl method.
  1.6450 +**
  1.6451 +** See also: [SQLITE_FCNTL_LOCKSTATE]
  1.6452 +*/
  1.6453 +SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
  1.6454 +
  1.6455 +/*
  1.6456 +** CAPI3REF: Testing Interface
  1.6457 +**
  1.6458 +** ^The sqlite3_test_control() interface is used to read out internal
  1.6459 +** state of SQLite and to inject faults into SQLite for testing
  1.6460 +** purposes.  ^The first parameter is an operation code that determines
  1.6461 +** the number, meaning, and operation of all subsequent parameters.
  1.6462 +**
  1.6463 +** This interface is not for use by applications.  It exists solely
  1.6464 +** for verifying the correct operation of the SQLite library.  Depending
  1.6465 +** on how the SQLite library is compiled, this interface might not exist.
  1.6466 +**
  1.6467 +** The details of the operation codes, their meanings, the parameters
  1.6468 +** they take, and what they do are all subject to change without notice.
  1.6469 +** Unlike most of the SQLite API, this function is not guaranteed to
  1.6470 +** operate consistently from one release to the next.
  1.6471 +*/
  1.6472 +SQLITE_API int sqlite3_test_control(int op, ...);
  1.6473 +
  1.6474 +/*
  1.6475 +** CAPI3REF: Testing Interface Operation Codes
  1.6476 +**
  1.6477 +** These constants are the valid operation code parameters used
  1.6478 +** as the first argument to [sqlite3_test_control()].
  1.6479 +**
  1.6480 +** These parameters and their meanings are subject to change
  1.6481 +** without notice.  These values are for testing purposes only.
  1.6482 +** Applications should not use any of these parameters or the
  1.6483 +** [sqlite3_test_control()] interface.
  1.6484 +*/
  1.6485 +#define SQLITE_TESTCTRL_FIRST                    5
  1.6486 +#define SQLITE_TESTCTRL_PRNG_SAVE                5
  1.6487 +#define SQLITE_TESTCTRL_PRNG_RESTORE             6
  1.6488 +#define SQLITE_TESTCTRL_PRNG_RESET               7
  1.6489 +#define SQLITE_TESTCTRL_BITVEC_TEST              8
  1.6490 +#define SQLITE_TESTCTRL_FAULT_INSTALL            9
  1.6491 +#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  1.6492 +#define SQLITE_TESTCTRL_PENDING_BYTE            11
  1.6493 +#define SQLITE_TESTCTRL_ASSERT                  12
  1.6494 +#define SQLITE_TESTCTRL_ALWAYS                  13
  1.6495 +#define SQLITE_TESTCTRL_RESERVE                 14
  1.6496 +#define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  1.6497 +#define SQLITE_TESTCTRL_ISKEYWORD               16
  1.6498 +#define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  1.6499 +#define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  1.6500 +#define SQLITE_TESTCTRL_EXPLAIN_STMT            19
  1.6501 +#define SQLITE_TESTCTRL_LAST                    19
  1.6502 +
  1.6503 +/*
  1.6504 +** CAPI3REF: SQLite Runtime Status
  1.6505 +**
  1.6506 +** ^This interface is used to retrieve runtime status information
  1.6507 +** about the performance of SQLite, and optionally to reset various
  1.6508 +** highwater marks.  ^The first argument is an integer code for
  1.6509 +** the specific parameter to measure.  ^(Recognized integer codes
  1.6510 +** are of the form [status parameters | SQLITE_STATUS_...].)^
  1.6511 +** ^The current value of the parameter is returned into *pCurrent.
  1.6512 +** ^The highest recorded value is returned in *pHighwater.  ^If the
  1.6513 +** resetFlag is true, then the highest record value is reset after
  1.6514 +** *pHighwater is written.  ^(Some parameters do not record the highest
  1.6515 +** value.  For those parameters
  1.6516 +** nothing is written into *pHighwater and the resetFlag is ignored.)^
  1.6517 +** ^(Other parameters record only the highwater mark and not the current
  1.6518 +** value.  For these latter parameters nothing is written into *pCurrent.)^
  1.6519 +**
  1.6520 +** ^The sqlite3_status() routine returns SQLITE_OK on success and a
  1.6521 +** non-zero [error code] on failure.
  1.6522 +**
  1.6523 +** This routine is threadsafe but is not atomic.  This routine can be
  1.6524 +** called while other threads are running the same or different SQLite
  1.6525 +** interfaces.  However the values returned in *pCurrent and
  1.6526 +** *pHighwater reflect the status of SQLite at different points in time
  1.6527 +** and it is possible that another thread might change the parameter
  1.6528 +** in between the times when *pCurrent and *pHighwater are written.
  1.6529 +**
  1.6530 +** See also: [sqlite3_db_status()]
  1.6531 +*/
  1.6532 +SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
  1.6533 +
  1.6534 +
  1.6535 +/*
  1.6536 +** CAPI3REF: Status Parameters
  1.6537 +** KEYWORDS: {status parameters}
  1.6538 +**
  1.6539 +** These integer constants designate various run-time status parameters
  1.6540 +** that can be returned by [sqlite3_status()].
  1.6541 +**
  1.6542 +** <dl>
  1.6543 +** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
  1.6544 +** <dd>This parameter is the current amount of memory checked out
  1.6545 +** using [sqlite3_malloc()], either directly or indirectly.  The
  1.6546 +** figure includes calls made to [sqlite3_malloc()] by the application
  1.6547 +** and internal memory usage by the SQLite library.  Scratch memory
  1.6548 +** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
  1.6549 +** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
  1.6550 +** this parameter.  The amount returned is the sum of the allocation
  1.6551 +** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
  1.6552 +**
  1.6553 +** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
  1.6554 +** <dd>This parameter records the largest memory allocation request
  1.6555 +** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
  1.6556 +** internal equivalents).  Only the value returned in the
  1.6557 +** *pHighwater parameter to [sqlite3_status()] is of interest.  
  1.6558 +** The value written into the *pCurrent parameter is undefined.</dd>)^
  1.6559 +**
  1.6560 +** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
  1.6561 +** <dd>This parameter records the number of separate memory allocations
  1.6562 +** currently checked out.</dd>)^
  1.6563 +**
  1.6564 +** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
  1.6565 +** <dd>This parameter returns the number of pages used out of the
  1.6566 +** [pagecache memory allocator] that was configured using 
  1.6567 +** [SQLITE_CONFIG_PAGECACHE].  The
  1.6568 +** value returned is in pages, not in bytes.</dd>)^
  1.6569 +**
  1.6570 +** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] 
  1.6571 +** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
  1.6572 +** <dd>This parameter returns the number of bytes of page cache
  1.6573 +** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
  1.6574 +** buffer and where forced to overflow to [sqlite3_malloc()].  The
  1.6575 +** returned value includes allocations that overflowed because they
  1.6576 +** where too large (they were larger than the "sz" parameter to
  1.6577 +** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
  1.6578 +** no space was left in the page cache.</dd>)^
  1.6579 +**
  1.6580 +** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
  1.6581 +** <dd>This parameter records the largest memory allocation request
  1.6582 +** handed to [pagecache memory allocator].  Only the value returned in the
  1.6583 +** *pHighwater parameter to [sqlite3_status()] is of interest.  
  1.6584 +** The value written into the *pCurrent parameter is undefined.</dd>)^
  1.6585 +**
  1.6586 +** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
  1.6587 +** <dd>This parameter returns the number of allocations used out of the
  1.6588 +** [scratch memory allocator] configured using
  1.6589 +** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
  1.6590 +** in bytes.  Since a single thread may only have one scratch allocation
  1.6591 +** outstanding at time, this parameter also reports the number of threads
  1.6592 +** using scratch memory at the same time.</dd>)^
  1.6593 +**
  1.6594 +** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
  1.6595 +** <dd>This parameter returns the number of bytes of scratch memory
  1.6596 +** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
  1.6597 +** buffer and where forced to overflow to [sqlite3_malloc()].  The values
  1.6598 +** returned include overflows because the requested allocation was too
  1.6599 +** larger (that is, because the requested allocation was larger than the
  1.6600 +** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
  1.6601 +** slots were available.
  1.6602 +** </dd>)^
  1.6603 +**
  1.6604 +** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
  1.6605 +** <dd>This parameter records the largest memory allocation request
  1.6606 +** handed to [scratch memory allocator].  Only the value returned in the
  1.6607 +** *pHighwater parameter to [sqlite3_status()] is of interest.  
  1.6608 +** The value written into the *pCurrent parameter is undefined.</dd>)^
  1.6609 +**
  1.6610 +** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
  1.6611 +** <dd>This parameter records the deepest parser stack.  It is only
  1.6612 +** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
  1.6613 +** </dl>
  1.6614 +**
  1.6615 +** New status parameters may be added from time to time.
  1.6616 +*/
  1.6617 +#define SQLITE_STATUS_MEMORY_USED          0
  1.6618 +#define SQLITE_STATUS_PAGECACHE_USED       1
  1.6619 +#define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
  1.6620 +#define SQLITE_STATUS_SCRATCH_USED         3
  1.6621 +#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
  1.6622 +#define SQLITE_STATUS_MALLOC_SIZE          5
  1.6623 +#define SQLITE_STATUS_PARSER_STACK         6
  1.6624 +#define SQLITE_STATUS_PAGECACHE_SIZE       7
  1.6625 +#define SQLITE_STATUS_SCRATCH_SIZE         8
  1.6626 +#define SQLITE_STATUS_MALLOC_COUNT         9
  1.6627 +
  1.6628 +/*
  1.6629 +** CAPI3REF: Database Connection Status
  1.6630 +**
  1.6631 +** ^This interface is used to retrieve runtime status information 
  1.6632 +** about a single [database connection].  ^The first argument is the
  1.6633 +** database connection object to be interrogated.  ^The second argument
  1.6634 +** is an integer constant, taken from the set of
  1.6635 +** [SQLITE_DBSTATUS options], that
  1.6636 +** determines the parameter to interrogate.  The set of 
  1.6637 +** [SQLITE_DBSTATUS options] is likely
  1.6638 +** to grow in future releases of SQLite.
  1.6639 +**
  1.6640 +** ^The current value of the requested parameter is written into *pCur
  1.6641 +** and the highest instantaneous value is written into *pHiwtr.  ^If
  1.6642 +** the resetFlg is true, then the highest instantaneous value is
  1.6643 +** reset back down to the current value.
  1.6644 +**
  1.6645 +** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
  1.6646 +** non-zero [error code] on failure.
  1.6647 +**
  1.6648 +** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
  1.6649 +*/
  1.6650 +SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
  1.6651 +
  1.6652 +/*
  1.6653 +** CAPI3REF: Status Parameters for database connections
  1.6654 +** KEYWORDS: {SQLITE_DBSTATUS options}
  1.6655 +**
  1.6656 +** These constants are the available integer "verbs" that can be passed as
  1.6657 +** the second argument to the [sqlite3_db_status()] interface.
  1.6658 +**
  1.6659 +** New verbs may be added in future releases of SQLite. Existing verbs
  1.6660 +** might be discontinued. Applications should check the return code from
  1.6661 +** [sqlite3_db_status()] to make sure that the call worked.
  1.6662 +** The [sqlite3_db_status()] interface will return a non-zero error code
  1.6663 +** if a discontinued or unsupported verb is invoked.
  1.6664 +**
  1.6665 +** <dl>
  1.6666 +** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
  1.6667 +** <dd>This parameter returns the number of lookaside memory slots currently
  1.6668 +** checked out.</dd>)^
  1.6669 +**
  1.6670 +** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
  1.6671 +** <dd>This parameter returns the number malloc attempts that were 
  1.6672 +** satisfied using lookaside memory. Only the high-water value is meaningful;
  1.6673 +** the current value is always zero.)^
  1.6674 +**
  1.6675 +** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
  1.6676 +** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
  1.6677 +** <dd>This parameter returns the number malloc attempts that might have
  1.6678 +** been satisfied using lookaside memory but failed due to the amount of
  1.6679 +** memory requested being larger than the lookaside slot size.
  1.6680 +** Only the high-water value is meaningful;
  1.6681 +** the current value is always zero.)^
  1.6682 +**
  1.6683 +** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
  1.6684 +** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
  1.6685 +** <dd>This parameter returns the number malloc attempts that might have
  1.6686 +** been satisfied using lookaside memory but failed due to all lookaside
  1.6687 +** memory already being in use.
  1.6688 +** Only the high-water value is meaningful;
  1.6689 +** the current value is always zero.)^
  1.6690 +**
  1.6691 +** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  1.6692 +** <dd>This parameter returns the approximate number of of bytes of heap
  1.6693 +** memory used by all pager caches associated with the database connection.)^
  1.6694 +** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  1.6695 +**
  1.6696 +** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
  1.6697 +** <dd>This parameter returns the approximate number of of bytes of heap
  1.6698 +** memory used to store the schema for all databases associated
  1.6699 +** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 
  1.6700 +** ^The full amount of memory used by the schemas is reported, even if the
  1.6701 +** schema memory is shared with other database connections due to
  1.6702 +** [shared cache mode] being enabled.
  1.6703 +** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
  1.6704 +**
  1.6705 +** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  1.6706 +** <dd>This parameter returns the approximate number of of bytes of heap
  1.6707 +** and lookaside memory used by all prepared statements associated with
  1.6708 +** the database connection.)^
  1.6709 +** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  1.6710 +** </dd>
  1.6711 +**
  1.6712 +** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
  1.6713 +** <dd>This parameter returns the number of pager cache hits that have
  1.6714 +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 
  1.6715 +** is always 0.
  1.6716 +** </dd>
  1.6717 +**
  1.6718 +** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
  1.6719 +** <dd>This parameter returns the number of pager cache misses that have
  1.6720 +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
  1.6721 +** is always 0.
  1.6722 +** </dd>
  1.6723 +**
  1.6724 +** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
  1.6725 +** <dd>This parameter returns the number of dirty cache entries that have
  1.6726 +** been written to disk. Specifically, the number of pages written to the
  1.6727 +** wal file in wal mode databases, or the number of pages written to the
  1.6728 +** database file in rollback mode databases. Any pages written as part of
  1.6729 +** transaction rollback or database recovery operations are not included.
  1.6730 +** If an IO or other error occurs while writing a page to disk, the effect
  1.6731 +** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  1.6732 +** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  1.6733 +** </dd>
  1.6734 +** </dl>
  1.6735 +*/
  1.6736 +#define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  1.6737 +#define SQLITE_DBSTATUS_CACHE_USED           1
  1.6738 +#define SQLITE_DBSTATUS_SCHEMA_USED          2
  1.6739 +#define SQLITE_DBSTATUS_STMT_USED            3
  1.6740 +#define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  1.6741 +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  1.6742 +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  1.6743 +#define SQLITE_DBSTATUS_CACHE_HIT            7
  1.6744 +#define SQLITE_DBSTATUS_CACHE_MISS           8
  1.6745 +#define SQLITE_DBSTATUS_CACHE_WRITE          9
  1.6746 +#define SQLITE_DBSTATUS_MAX                  9   /* Largest defined DBSTATUS */
  1.6747 +
  1.6748 +
  1.6749 +/*
  1.6750 +** CAPI3REF: Prepared Statement Status
  1.6751 +**
  1.6752 +** ^(Each prepared statement maintains various
  1.6753 +** [SQLITE_STMTSTATUS counters] that measure the number
  1.6754 +** of times it has performed specific operations.)^  These counters can
  1.6755 +** be used to monitor the performance characteristics of the prepared
  1.6756 +** statements.  For example, if the number of table steps greatly exceeds
  1.6757 +** the number of table searches or result rows, that would tend to indicate
  1.6758 +** that the prepared statement is using a full table scan rather than
  1.6759 +** an index.  
  1.6760 +**
  1.6761 +** ^(This interface is used to retrieve and reset counter values from
  1.6762 +** a [prepared statement].  The first argument is the prepared statement
  1.6763 +** object to be interrogated.  The second argument
  1.6764 +** is an integer code for a specific [SQLITE_STMTSTATUS counter]
  1.6765 +** to be interrogated.)^
  1.6766 +** ^The current value of the requested counter is returned.
  1.6767 +** ^If the resetFlg is true, then the counter is reset to zero after this
  1.6768 +** interface call returns.
  1.6769 +**
  1.6770 +** See also: [sqlite3_status()] and [sqlite3_db_status()].
  1.6771 +*/
  1.6772 +SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
  1.6773 +
  1.6774 +/*
  1.6775 +** CAPI3REF: Status Parameters for prepared statements
  1.6776 +** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
  1.6777 +**
  1.6778 +** These preprocessor macros define integer codes that name counter
  1.6779 +** values associated with the [sqlite3_stmt_status()] interface.
  1.6780 +** The meanings of the various counters are as follows:
  1.6781 +**
  1.6782 +** <dl>
  1.6783 +** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
  1.6784 +** <dd>^This is the number of times that SQLite has stepped forward in
  1.6785 +** a table as part of a full table scan.  Large numbers for this counter
  1.6786 +** may indicate opportunities for performance improvement through 
  1.6787 +** careful use of indices.</dd>
  1.6788 +**
  1.6789 +** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
  1.6790 +** <dd>^This is the number of sort operations that have occurred.
  1.6791 +** A non-zero value in this counter may indicate an opportunity to
  1.6792 +** improvement performance through careful use of indices.</dd>
  1.6793 +**
  1.6794 +** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
  1.6795 +** <dd>^This is the number of rows inserted into transient indices that
  1.6796 +** were created automatically in order to help joins run faster.
  1.6797 +** A non-zero value in this counter may indicate an opportunity to
  1.6798 +** improvement performance by adding permanent indices that do not
  1.6799 +** need to be reinitialized each time the statement is run.</dd>
  1.6800 +** </dl>
  1.6801 +*/
  1.6802 +#define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
  1.6803 +#define SQLITE_STMTSTATUS_SORT              2
  1.6804 +#define SQLITE_STMTSTATUS_AUTOINDEX         3
  1.6805 +
  1.6806 +/*
  1.6807 +** CAPI3REF: Custom Page Cache Object
  1.6808 +**
  1.6809 +** The sqlite3_pcache type is opaque.  It is implemented by
  1.6810 +** the pluggable module.  The SQLite core has no knowledge of
  1.6811 +** its size or internal structure and never deals with the
  1.6812 +** sqlite3_pcache object except by holding and passing pointers
  1.6813 +** to the object.
  1.6814 +**
  1.6815 +** See [sqlite3_pcache_methods2] for additional information.
  1.6816 +*/
  1.6817 +typedef struct sqlite3_pcache sqlite3_pcache;
  1.6818 +
  1.6819 +/*
  1.6820 +** CAPI3REF: Custom Page Cache Object
  1.6821 +**
  1.6822 +** The sqlite3_pcache_page object represents a single page in the
  1.6823 +** page cache.  The page cache will allocate instances of this
  1.6824 +** object.  Various methods of the page cache use pointers to instances
  1.6825 +** of this object as parameters or as their return value.
  1.6826 +**
  1.6827 +** See [sqlite3_pcache_methods2] for additional information.
  1.6828 +*/
  1.6829 +typedef struct sqlite3_pcache_page sqlite3_pcache_page;
  1.6830 +struct sqlite3_pcache_page {
  1.6831 +  void *pBuf;        /* The content of the page */
  1.6832 +  void *pExtra;      /* Extra information associated with the page */
  1.6833 +};
  1.6834 +
  1.6835 +/*
  1.6836 +** CAPI3REF: Application Defined Page Cache.
  1.6837 +** KEYWORDS: {page cache}
  1.6838 +**
  1.6839 +** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
  1.6840 +** register an alternative page cache implementation by passing in an 
  1.6841 +** instance of the sqlite3_pcache_methods2 structure.)^
  1.6842 +** In many applications, most of the heap memory allocated by 
  1.6843 +** SQLite is used for the page cache.
  1.6844 +** By implementing a 
  1.6845 +** custom page cache using this API, an application can better control
  1.6846 +** the amount of memory consumed by SQLite, the way in which 
  1.6847 +** that memory is allocated and released, and the policies used to 
  1.6848 +** determine exactly which parts of a database file are cached and for 
  1.6849 +** how long.
  1.6850 +**
  1.6851 +** The alternative page cache mechanism is an
  1.6852 +** extreme measure that is only needed by the most demanding applications.
  1.6853 +** The built-in page cache is recommended for most uses.
  1.6854 +**
  1.6855 +** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
  1.6856 +** internal buffer by SQLite within the call to [sqlite3_config].  Hence
  1.6857 +** the application may discard the parameter after the call to
  1.6858 +** [sqlite3_config()] returns.)^
  1.6859 +**
  1.6860 +** [[the xInit() page cache method]]
  1.6861 +** ^(The xInit() method is called once for each effective 
  1.6862 +** call to [sqlite3_initialize()])^
  1.6863 +** (usually only once during the lifetime of the process). ^(The xInit()
  1.6864 +** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
  1.6865 +** The intent of the xInit() method is to set up global data structures 
  1.6866 +** required by the custom page cache implementation. 
  1.6867 +** ^(If the xInit() method is NULL, then the 
  1.6868 +** built-in default page cache is used instead of the application defined
  1.6869 +** page cache.)^
  1.6870 +**
  1.6871 +** [[the xShutdown() page cache method]]
  1.6872 +** ^The xShutdown() method is called by [sqlite3_shutdown()].
  1.6873 +** It can be used to clean up 
  1.6874 +** any outstanding resources before process shutdown, if required.
  1.6875 +** ^The xShutdown() method may be NULL.
  1.6876 +**
  1.6877 +** ^SQLite automatically serializes calls to the xInit method,
  1.6878 +** so the xInit method need not be threadsafe.  ^The
  1.6879 +** xShutdown method is only called from [sqlite3_shutdown()] so it does
  1.6880 +** not need to be threadsafe either.  All other methods must be threadsafe
  1.6881 +** in multithreaded applications.
  1.6882 +**
  1.6883 +** ^SQLite will never invoke xInit() more than once without an intervening
  1.6884 +** call to xShutdown().
  1.6885 +**
  1.6886 +** [[the xCreate() page cache methods]]
  1.6887 +** ^SQLite invokes the xCreate() method to construct a new cache instance.
  1.6888 +** SQLite will typically create one cache instance for each open database file,
  1.6889 +** though this is not guaranteed. ^The
  1.6890 +** first parameter, szPage, is the size in bytes of the pages that must
  1.6891 +** be allocated by the cache.  ^szPage will always a power of two.  ^The
  1.6892 +** second parameter szExtra is a number of bytes of extra storage 
  1.6893 +** associated with each page cache entry.  ^The szExtra parameter will
  1.6894 +** a number less than 250.  SQLite will use the
  1.6895 +** extra szExtra bytes on each page to store metadata about the underlying
  1.6896 +** database page on disk.  The value passed into szExtra depends
  1.6897 +** on the SQLite version, the target platform, and how SQLite was compiled.
  1.6898 +** ^The third argument to xCreate(), bPurgeable, is true if the cache being
  1.6899 +** created will be used to cache database pages of a file stored on disk, or
  1.6900 +** false if it is used for an in-memory database. The cache implementation
  1.6901 +** does not have to do anything special based with the value of bPurgeable;
  1.6902 +** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
  1.6903 +** never invoke xUnpin() except to deliberately delete a page.
  1.6904 +** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
  1.6905 +** false will always have the "discard" flag set to true.  
  1.6906 +** ^Hence, a cache created with bPurgeable false will
  1.6907 +** never contain any unpinned pages.
  1.6908 +**
  1.6909 +** [[the xCachesize() page cache method]]
  1.6910 +** ^(The xCachesize() method may be called at any time by SQLite to set the
  1.6911 +** suggested maximum cache-size (number of pages stored by) the cache
  1.6912 +** instance passed as the first argument. This is the value configured using
  1.6913 +** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
  1.6914 +** parameter, the implementation is not required to do anything with this
  1.6915 +** value; it is advisory only.
  1.6916 +**
  1.6917 +** [[the xPagecount() page cache methods]]
  1.6918 +** The xPagecount() method must return the number of pages currently
  1.6919 +** stored in the cache, both pinned and unpinned.
  1.6920 +** 
  1.6921 +** [[the xFetch() page cache methods]]
  1.6922 +** The xFetch() method locates a page in the cache and returns a pointer to 
  1.6923 +** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
  1.6924 +** The pBuf element of the returned sqlite3_pcache_page object will be a
  1.6925 +** pointer to a buffer of szPage bytes used to store the content of a 
  1.6926 +** single database page.  The pExtra element of sqlite3_pcache_page will be
  1.6927 +** a pointer to the szExtra bytes of extra storage that SQLite has requested
  1.6928 +** for each entry in the page cache.
  1.6929 +**
  1.6930 +** The page to be fetched is determined by the key. ^The minimum key value
  1.6931 +** is 1.  After it has been retrieved using xFetch, the page is considered
  1.6932 +** to be "pinned".
  1.6933 +**
  1.6934 +** If the requested page is already in the page cache, then the page cache
  1.6935 +** implementation must return a pointer to the page buffer with its content
  1.6936 +** intact.  If the requested page is not already in the cache, then the
  1.6937 +** cache implementation should use the value of the createFlag
  1.6938 +** parameter to help it determined what action to take:
  1.6939 +**
  1.6940 +** <table border=1 width=85% align=center>
  1.6941 +** <tr><th> createFlag <th> Behaviour when page is not already in cache
  1.6942 +** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
  1.6943 +** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
  1.6944 +**                 Otherwise return NULL.
  1.6945 +** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
  1.6946 +**                 NULL if allocating a new page is effectively impossible.
  1.6947 +** </table>
  1.6948 +**
  1.6949 +** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
  1.6950 +** will only use a createFlag of 2 after a prior call with a createFlag of 1
  1.6951 +** failed.)^  In between the to xFetch() calls, SQLite may
  1.6952 +** attempt to unpin one or more cache pages by spilling the content of
  1.6953 +** pinned pages to disk and synching the operating system disk cache.
  1.6954 +**
  1.6955 +** [[the xUnpin() page cache method]]
  1.6956 +** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
  1.6957 +** as its second argument.  If the third parameter, discard, is non-zero,
  1.6958 +** then the page must be evicted from the cache.
  1.6959 +** ^If the discard parameter is
  1.6960 +** zero, then the page may be discarded or retained at the discretion of
  1.6961 +** page cache implementation. ^The page cache implementation
  1.6962 +** may choose to evict unpinned pages at any time.
  1.6963 +**
  1.6964 +** The cache must not perform any reference counting. A single 
  1.6965 +** call to xUnpin() unpins the page regardless of the number of prior calls 
  1.6966 +** to xFetch().
  1.6967 +**
  1.6968 +** [[the xRekey() page cache methods]]
  1.6969 +** The xRekey() method is used to change the key value associated with the
  1.6970 +** page passed as the second argument. If the cache
  1.6971 +** previously contains an entry associated with newKey, it must be
  1.6972 +** discarded. ^Any prior cache entry associated with newKey is guaranteed not
  1.6973 +** to be pinned.
  1.6974 +**
  1.6975 +** When SQLite calls the xTruncate() method, the cache must discard all
  1.6976 +** existing cache entries with page numbers (keys) greater than or equal
  1.6977 +** to the value of the iLimit parameter passed to xTruncate(). If any
  1.6978 +** of these pages are pinned, they are implicitly unpinned, meaning that
  1.6979 +** they can be safely discarded.
  1.6980 +**
  1.6981 +** [[the xDestroy() page cache method]]
  1.6982 +** ^The xDestroy() method is used to delete a cache allocated by xCreate().
  1.6983 +** All resources associated with the specified cache should be freed. ^After
  1.6984 +** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
  1.6985 +** handle invalid, and will not use it with any other sqlite3_pcache_methods2
  1.6986 +** functions.
  1.6987 +**
  1.6988 +** [[the xShrink() page cache method]]
  1.6989 +** ^SQLite invokes the xShrink() method when it wants the page cache to
  1.6990 +** free up as much of heap memory as possible.  The page cache implementation
  1.6991 +** is not obligated to free any memory, but well-behaved implementations should
  1.6992 +** do their best.
  1.6993 +*/
  1.6994 +typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
  1.6995 +struct sqlite3_pcache_methods2 {
  1.6996 +  int iVersion;
  1.6997 +  void *pArg;
  1.6998 +  int (*xInit)(void*);
  1.6999 +  void (*xShutdown)(void*);
  1.7000 +  sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
  1.7001 +  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  1.7002 +  int (*xPagecount)(sqlite3_pcache*);
  1.7003 +  sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  1.7004 +  void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
  1.7005 +  void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, 
  1.7006 +      unsigned oldKey, unsigned newKey);
  1.7007 +  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  1.7008 +  void (*xDestroy)(sqlite3_pcache*);
  1.7009 +  void (*xShrink)(sqlite3_pcache*);
  1.7010 +};
  1.7011 +
  1.7012 +/*
  1.7013 +** This is the obsolete pcache_methods object that has now been replaced
  1.7014 +** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
  1.7015 +** retained in the header file for backwards compatibility only.
  1.7016 +*/
  1.7017 +typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
  1.7018 +struct sqlite3_pcache_methods {
  1.7019 +  void *pArg;
  1.7020 +  int (*xInit)(void*);
  1.7021 +  void (*xShutdown)(void*);
  1.7022 +  sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
  1.7023 +  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  1.7024 +  int (*xPagecount)(sqlite3_pcache*);
  1.7025 +  void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  1.7026 +  void (*xUnpin)(sqlite3_pcache*, void*, int discard);
  1.7027 +  void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
  1.7028 +  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  1.7029 +  void (*xDestroy)(sqlite3_pcache*);
  1.7030 +};
  1.7031 +
  1.7032 +
  1.7033 +/*
  1.7034 +** CAPI3REF: Online Backup Object
  1.7035 +**
  1.7036 +** The sqlite3_backup object records state information about an ongoing
  1.7037 +** online backup operation.  ^The sqlite3_backup object is created by
  1.7038 +** a call to [sqlite3_backup_init()] and is destroyed by a call to
  1.7039 +** [sqlite3_backup_finish()].
  1.7040 +**
  1.7041 +** See Also: [Using the SQLite Online Backup API]
  1.7042 +*/
  1.7043 +typedef struct sqlite3_backup sqlite3_backup;
  1.7044 +
  1.7045 +/*
  1.7046 +** CAPI3REF: Online Backup API.
  1.7047 +**
  1.7048 +** The backup API copies the content of one database into another.
  1.7049 +** It is useful either for creating backups of databases or
  1.7050 +** for copying in-memory databases to or from persistent files. 
  1.7051 +**
  1.7052 +** See Also: [Using the SQLite Online Backup API]
  1.7053 +**
  1.7054 +** ^SQLite holds a write transaction open on the destination database file
  1.7055 +** for the duration of the backup operation.
  1.7056 +** ^The source database is read-locked only while it is being read;
  1.7057 +** it is not locked continuously for the entire backup operation.
  1.7058 +** ^Thus, the backup may be performed on a live source database without
  1.7059 +** preventing other database connections from
  1.7060 +** reading or writing to the source database while the backup is underway.
  1.7061 +** 
  1.7062 +** ^(To perform a backup operation: 
  1.7063 +**   <ol>
  1.7064 +**     <li><b>sqlite3_backup_init()</b> is called once to initialize the
  1.7065 +**         backup, 
  1.7066 +**     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 
  1.7067 +**         the data between the two databases, and finally
  1.7068 +**     <li><b>sqlite3_backup_finish()</b> is called to release all resources 
  1.7069 +**         associated with the backup operation. 
  1.7070 +**   </ol>)^
  1.7071 +** There should be exactly one call to sqlite3_backup_finish() for each
  1.7072 +** successful call to sqlite3_backup_init().
  1.7073 +**
  1.7074 +** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
  1.7075 +**
  1.7076 +** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the 
  1.7077 +** [database connection] associated with the destination database 
  1.7078 +** and the database name, respectively.
  1.7079 +** ^The database name is "main" for the main database, "temp" for the
  1.7080 +** temporary database, or the name specified after the AS keyword in
  1.7081 +** an [ATTACH] statement for an attached database.
  1.7082 +** ^The S and M arguments passed to 
  1.7083 +** sqlite3_backup_init(D,N,S,M) identify the [database connection]
  1.7084 +** and database name of the source database, respectively.
  1.7085 +** ^The source and destination [database connections] (parameters S and D)
  1.7086 +** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
  1.7087 +** an error.
  1.7088 +**
  1.7089 +** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
  1.7090 +** returned and an error code and error message are stored in the
  1.7091 +** destination [database connection] D.
  1.7092 +** ^The error code and message for the failed call to sqlite3_backup_init()
  1.7093 +** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
  1.7094 +** [sqlite3_errmsg16()] functions.
  1.7095 +** ^A successful call to sqlite3_backup_init() returns a pointer to an
  1.7096 +** [sqlite3_backup] object.
  1.7097 +** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
  1.7098 +** sqlite3_backup_finish() functions to perform the specified backup 
  1.7099 +** operation.
  1.7100 +**
  1.7101 +** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
  1.7102 +**
  1.7103 +** ^Function sqlite3_backup_step(B,N) will copy up to N pages between 
  1.7104 +** the source and destination databases specified by [sqlite3_backup] object B.
  1.7105 +** ^If N is negative, all remaining source pages are copied. 
  1.7106 +** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
  1.7107 +** are still more pages to be copied, then the function returns [SQLITE_OK].
  1.7108 +** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
  1.7109 +** from source to destination, then it returns [SQLITE_DONE].
  1.7110 +** ^If an error occurs while running sqlite3_backup_step(B,N),
  1.7111 +** then an [error code] is returned. ^As well as [SQLITE_OK] and
  1.7112 +** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
  1.7113 +** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
  1.7114 +** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
  1.7115 +**
  1.7116 +** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
  1.7117 +** <ol>
  1.7118 +** <li> the destination database was opened read-only, or
  1.7119 +** <li> the destination database is using write-ahead-log journaling
  1.7120 +** and the destination and source page sizes differ, or
  1.7121 +** <li> the destination database is an in-memory database and the
  1.7122 +** destination and source page sizes differ.
  1.7123 +** </ol>)^
  1.7124 +**
  1.7125 +** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
  1.7126 +** the [sqlite3_busy_handler | busy-handler function]
  1.7127 +** is invoked (if one is specified). ^If the 
  1.7128 +** busy-handler returns non-zero before the lock is available, then 
  1.7129 +** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
  1.7130 +** sqlite3_backup_step() can be retried later. ^If the source
  1.7131 +** [database connection]
  1.7132 +** is being used to write to the source database when sqlite3_backup_step()
  1.7133 +** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
  1.7134 +** case the call to sqlite3_backup_step() can be retried later on. ^(If
  1.7135 +** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
  1.7136 +** [SQLITE_READONLY] is returned, then 
  1.7137 +** there is no point in retrying the call to sqlite3_backup_step(). These 
  1.7138 +** errors are considered fatal.)^  The application must accept 
  1.7139 +** that the backup operation has failed and pass the backup operation handle 
  1.7140 +** to the sqlite3_backup_finish() to release associated resources.
  1.7141 +**
  1.7142 +** ^The first call to sqlite3_backup_step() obtains an exclusive lock
  1.7143 +** on the destination file. ^The exclusive lock is not released until either 
  1.7144 +** sqlite3_backup_finish() is called or the backup operation is complete 
  1.7145 +** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
  1.7146 +** sqlite3_backup_step() obtains a [shared lock] on the source database that
  1.7147 +** lasts for the duration of the sqlite3_backup_step() call.
  1.7148 +** ^Because the source database is not locked between calls to
  1.7149 +** sqlite3_backup_step(), the source database may be modified mid-way
  1.7150 +** through the backup process.  ^If the source database is modified by an
  1.7151 +** external process or via a database connection other than the one being
  1.7152 +** used by the backup operation, then the backup will be automatically
  1.7153 +** restarted by the next call to sqlite3_backup_step(). ^If the source 
  1.7154 +** database is modified by the using the same database connection as is used
  1.7155 +** by the backup operation, then the backup database is automatically
  1.7156 +** updated at the same time.
  1.7157 +**
  1.7158 +** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
  1.7159 +**
  1.7160 +** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the 
  1.7161 +** application wishes to abandon the backup operation, the application
  1.7162 +** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
  1.7163 +** ^The sqlite3_backup_finish() interfaces releases all
  1.7164 +** resources associated with the [sqlite3_backup] object. 
  1.7165 +** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
  1.7166 +** active write-transaction on the destination database is rolled back.
  1.7167 +** The [sqlite3_backup] object is invalid
  1.7168 +** and may not be used following a call to sqlite3_backup_finish().
  1.7169 +**
  1.7170 +** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
  1.7171 +** sqlite3_backup_step() errors occurred, regardless or whether or not
  1.7172 +** sqlite3_backup_step() completed.
  1.7173 +** ^If an out-of-memory condition or IO error occurred during any prior
  1.7174 +** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
  1.7175 +** sqlite3_backup_finish() returns the corresponding [error code].
  1.7176 +**
  1.7177 +** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
  1.7178 +** is not a permanent error and does not affect the return value of
  1.7179 +** sqlite3_backup_finish().
  1.7180 +**
  1.7181 +** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
  1.7182 +** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
  1.7183 +**
  1.7184 +** ^Each call to sqlite3_backup_step() sets two values inside
  1.7185 +** the [sqlite3_backup] object: the number of pages still to be backed
  1.7186 +** up and the total number of pages in the source database file.
  1.7187 +** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
  1.7188 +** retrieve these two values, respectively.
  1.7189 +**
  1.7190 +** ^The values returned by these functions are only updated by
  1.7191 +** sqlite3_backup_step(). ^If the source database is modified during a backup
  1.7192 +** operation, then the values are not updated to account for any extra
  1.7193 +** pages that need to be updated or the size of the source database file
  1.7194 +** changing.
  1.7195 +**
  1.7196 +** <b>Concurrent Usage of Database Handles</b>
  1.7197 +**
  1.7198 +** ^The source [database connection] may be used by the application for other
  1.7199 +** purposes while a backup operation is underway or being initialized.
  1.7200 +** ^If SQLite is compiled and configured to support threadsafe database
  1.7201 +** connections, then the source database connection may be used concurrently
  1.7202 +** from within other threads.
  1.7203 +**
  1.7204 +** However, the application must guarantee that the destination 
  1.7205 +** [database connection] is not passed to any other API (by any thread) after 
  1.7206 +** sqlite3_backup_init() is called and before the corresponding call to
  1.7207 +** sqlite3_backup_finish().  SQLite does not currently check to see
  1.7208 +** if the application incorrectly accesses the destination [database connection]
  1.7209 +** and so no error code is reported, but the operations may malfunction
  1.7210 +** nevertheless.  Use of the destination database connection while a
  1.7211 +** backup is in progress might also also cause a mutex deadlock.
  1.7212 +**
  1.7213 +** If running in [shared cache mode], the application must
  1.7214 +** guarantee that the shared cache used by the destination database
  1.7215 +** is not accessed while the backup is running. In practice this means
  1.7216 +** that the application must guarantee that the disk file being 
  1.7217 +** backed up to is not accessed by any connection within the process,
  1.7218 +** not just the specific connection that was passed to sqlite3_backup_init().
  1.7219 +**
  1.7220 +** The [sqlite3_backup] object itself is partially threadsafe. Multiple 
  1.7221 +** threads may safely make multiple concurrent calls to sqlite3_backup_step().
  1.7222 +** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
  1.7223 +** APIs are not strictly speaking threadsafe. If they are invoked at the
  1.7224 +** same time as another thread is invoking sqlite3_backup_step() it is
  1.7225 +** possible that they return invalid values.
  1.7226 +*/
  1.7227 +SQLITE_API sqlite3_backup *sqlite3_backup_init(
  1.7228 +  sqlite3 *pDest,                        /* Destination database handle */
  1.7229 +  const char *zDestName,                 /* Destination database name */
  1.7230 +  sqlite3 *pSource,                      /* Source database handle */
  1.7231 +  const char *zSourceName                /* Source database name */
  1.7232 +);
  1.7233 +SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
  1.7234 +SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
  1.7235 +SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
  1.7236 +SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
  1.7237 +
  1.7238 +/*
  1.7239 +** CAPI3REF: Unlock Notification
  1.7240 +**
  1.7241 +** ^When running in shared-cache mode, a database operation may fail with
  1.7242 +** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  1.7243 +** individual tables within the shared-cache cannot be obtained. See
  1.7244 +** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
  1.7245 +** ^This API may be used to register a callback that SQLite will invoke 
  1.7246 +** when the connection currently holding the required lock relinquishes it.
  1.7247 +** ^This API is only available if the library was compiled with the
  1.7248 +** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
  1.7249 +**
  1.7250 +** See Also: [Using the SQLite Unlock Notification Feature].
  1.7251 +**
  1.7252 +** ^Shared-cache locks are released when a database connection concludes
  1.7253 +** its current transaction, either by committing it or rolling it back. 
  1.7254 +**
  1.7255 +** ^When a connection (known as the blocked connection) fails to obtain a
  1.7256 +** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
  1.7257 +** identity of the database connection (the blocking connection) that
  1.7258 +** has locked the required resource is stored internally. ^After an 
  1.7259 +** application receives an SQLITE_LOCKED error, it may call the
  1.7260 +** sqlite3_unlock_notify() method with the blocked connection handle as 
  1.7261 +** the first argument to register for a callback that will be invoked
  1.7262 +** when the blocking connections current transaction is concluded. ^The
  1.7263 +** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
  1.7264 +** call that concludes the blocking connections transaction.
  1.7265 +**
  1.7266 +** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
  1.7267 +** there is a chance that the blocking connection will have already
  1.7268 +** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
  1.7269 +** If this happens, then the specified callback is invoked immediately,
  1.7270 +** from within the call to sqlite3_unlock_notify().)^
  1.7271 +**
  1.7272 +** ^If the blocked connection is attempting to obtain a write-lock on a
  1.7273 +** shared-cache table, and more than one other connection currently holds
  1.7274 +** a read-lock on the same table, then SQLite arbitrarily selects one of 
  1.7275 +** the other connections to use as the blocking connection.
  1.7276 +**
  1.7277 +** ^(There may be at most one unlock-notify callback registered by a 
  1.7278 +** blocked connection. If sqlite3_unlock_notify() is called when the
  1.7279 +** blocked connection already has a registered unlock-notify callback,
  1.7280 +** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
  1.7281 +** called with a NULL pointer as its second argument, then any existing
  1.7282 +** unlock-notify callback is canceled. ^The blocked connections 
  1.7283 +** unlock-notify callback may also be canceled by closing the blocked
  1.7284 +** connection using [sqlite3_close()].
  1.7285 +**
  1.7286 +** The unlock-notify callback is not reentrant. If an application invokes
  1.7287 +** any sqlite3_xxx API functions from within an unlock-notify callback, a
  1.7288 +** crash or deadlock may be the result.
  1.7289 +**
  1.7290 +** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
  1.7291 +** returns SQLITE_OK.
  1.7292 +**
  1.7293 +** <b>Callback Invocation Details</b>
  1.7294 +**
  1.7295 +** When an unlock-notify callback is registered, the application provides a 
  1.7296 +** single void* pointer that is passed to the callback when it is invoked.
  1.7297 +** However, the signature of the callback function allows SQLite to pass
  1.7298 +** it an array of void* context pointers. The first argument passed to
  1.7299 +** an unlock-notify callback is a pointer to an array of void* pointers,
  1.7300 +** and the second is the number of entries in the array.
  1.7301 +**
  1.7302 +** When a blocking connections transaction is concluded, there may be
  1.7303 +** more than one blocked connection that has registered for an unlock-notify
  1.7304 +** callback. ^If two or more such blocked connections have specified the
  1.7305 +** same callback function, then instead of invoking the callback function
  1.7306 +** multiple times, it is invoked once with the set of void* context pointers
  1.7307 +** specified by the blocked connections bundled together into an array.
  1.7308 +** This gives the application an opportunity to prioritize any actions 
  1.7309 +** related to the set of unblocked database connections.
  1.7310 +**
  1.7311 +** <b>Deadlock Detection</b>
  1.7312 +**
  1.7313 +** Assuming that after registering for an unlock-notify callback a 
  1.7314 +** database waits for the callback to be issued before taking any further
  1.7315 +** action (a reasonable assumption), then using this API may cause the
  1.7316 +** application to deadlock. For example, if connection X is waiting for
  1.7317 +** connection Y's transaction to be concluded, and similarly connection
  1.7318 +** Y is waiting on connection X's transaction, then neither connection
  1.7319 +** will proceed and the system may remain deadlocked indefinitely.
  1.7320 +**
  1.7321 +** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
  1.7322 +** detection. ^If a given call to sqlite3_unlock_notify() would put the
  1.7323 +** system in a deadlocked state, then SQLITE_LOCKED is returned and no
  1.7324 +** unlock-notify callback is registered. The system is said to be in
  1.7325 +** a deadlocked state if connection A has registered for an unlock-notify
  1.7326 +** callback on the conclusion of connection B's transaction, and connection
  1.7327 +** B has itself registered for an unlock-notify callback when connection
  1.7328 +** A's transaction is concluded. ^Indirect deadlock is also detected, so
  1.7329 +** the system is also considered to be deadlocked if connection B has
  1.7330 +** registered for an unlock-notify callback on the conclusion of connection
  1.7331 +** C's transaction, where connection C is waiting on connection A. ^Any
  1.7332 +** number of levels of indirection are allowed.
  1.7333 +**
  1.7334 +** <b>The "DROP TABLE" Exception</b>
  1.7335 +**
  1.7336 +** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost 
  1.7337 +** always appropriate to call sqlite3_unlock_notify(). There is however,
  1.7338 +** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
  1.7339 +** SQLite checks if there are any currently executing SELECT statements
  1.7340 +** that belong to the same connection. If there are, SQLITE_LOCKED is
  1.7341 +** returned. In this case there is no "blocking connection", so invoking
  1.7342 +** sqlite3_unlock_notify() results in the unlock-notify callback being
  1.7343 +** invoked immediately. If the application then re-attempts the "DROP TABLE"
  1.7344 +** or "DROP INDEX" query, an infinite loop might be the result.
  1.7345 +**
  1.7346 +** One way around this problem is to check the extended error code returned
  1.7347 +** by an sqlite3_step() call. ^(If there is a blocking connection, then the
  1.7348 +** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
  1.7349 +** the special "DROP TABLE/INDEX" case, the extended error code is just 
  1.7350 +** SQLITE_LOCKED.)^
  1.7351 +*/
  1.7352 +SQLITE_API int sqlite3_unlock_notify(
  1.7353 +  sqlite3 *pBlocked,                          /* Waiting connection */
  1.7354 +  void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
  1.7355 +  void *pNotifyArg                            /* Argument to pass to xNotify */
  1.7356 +);
  1.7357 +
  1.7358 +
  1.7359 +/*
  1.7360 +** CAPI3REF: String Comparison
  1.7361 +**
  1.7362 +** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
  1.7363 +** and extensions to compare the contents of two buffers containing UTF-8
  1.7364 +** strings in a case-independent fashion, using the same definition of "case
  1.7365 +** independence" that SQLite uses internally when comparing identifiers.
  1.7366 +*/
  1.7367 +SQLITE_API int sqlite3_stricmp(const char *, const char *);
  1.7368 +SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
  1.7369 +
  1.7370 +/*
  1.7371 +** CAPI3REF: Error Logging Interface
  1.7372 +**
  1.7373 +** ^The [sqlite3_log()] interface writes a message into the error log
  1.7374 +** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
  1.7375 +** ^If logging is enabled, the zFormat string and subsequent arguments are
  1.7376 +** used with [sqlite3_snprintf()] to generate the final output string.
  1.7377 +**
  1.7378 +** The sqlite3_log() interface is intended for use by extensions such as
  1.7379 +** virtual tables, collating functions, and SQL functions.  While there is
  1.7380 +** nothing to prevent an application from calling sqlite3_log(), doing so
  1.7381 +** is considered bad form.
  1.7382 +**
  1.7383 +** The zFormat string must not be NULL.
  1.7384 +**
  1.7385 +** To avoid deadlocks and other threading problems, the sqlite3_log() routine
  1.7386 +** will not use dynamically allocated memory.  The log message is stored in
  1.7387 +** a fixed-length buffer on the stack.  If the log message is longer than
  1.7388 +** a few hundred characters, it will be truncated to the length of the
  1.7389 +** buffer.
  1.7390 +*/
  1.7391 +SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
  1.7392 +
  1.7393 +/*
  1.7394 +** CAPI3REF: Write-Ahead Log Commit Hook
  1.7395 +**
  1.7396 +** ^The [sqlite3_wal_hook()] function is used to register a callback that
  1.7397 +** will be invoked each time a database connection commits data to a
  1.7398 +** [write-ahead log] (i.e. whenever a transaction is committed in
  1.7399 +** [journal_mode | journal_mode=WAL mode]). 
  1.7400 +**
  1.7401 +** ^The callback is invoked by SQLite after the commit has taken place and 
  1.7402 +** the associated write-lock on the database released, so the implementation 
  1.7403 +** may read, write or [checkpoint] the database as required.
  1.7404 +**
  1.7405 +** ^The first parameter passed to the callback function when it is invoked
  1.7406 +** is a copy of the third parameter passed to sqlite3_wal_hook() when
  1.7407 +** registering the callback. ^The second is a copy of the database handle.
  1.7408 +** ^The third parameter is the name of the database that was written to -
  1.7409 +** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
  1.7410 +** is the number of pages currently in the write-ahead log file,
  1.7411 +** including those that were just committed.
  1.7412 +**
  1.7413 +** The callback function should normally return [SQLITE_OK].  ^If an error
  1.7414 +** code is returned, that error will propagate back up through the
  1.7415 +** SQLite code base to cause the statement that provoked the callback
  1.7416 +** to report an error, though the commit will have still occurred. If the
  1.7417 +** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
  1.7418 +** that does not correspond to any valid SQLite error code, the results
  1.7419 +** are undefined.
  1.7420 +**
  1.7421 +** A single database handle may have at most a single write-ahead log callback 
  1.7422 +** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
  1.7423 +** previously registered write-ahead log callback. ^Note that the
  1.7424 +** [sqlite3_wal_autocheckpoint()] interface and the
  1.7425 +** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
  1.7426 +** those overwrite any prior [sqlite3_wal_hook()] settings.
  1.7427 +*/
  1.7428 +SQLITE_API void *sqlite3_wal_hook(
  1.7429 +  sqlite3*, 
  1.7430 +  int(*)(void *,sqlite3*,const char*,int),
  1.7431 +  void*
  1.7432 +);
  1.7433 +
  1.7434 +/*
  1.7435 +** CAPI3REF: Configure an auto-checkpoint
  1.7436 +**
  1.7437 +** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
  1.7438 +** [sqlite3_wal_hook()] that causes any database on [database connection] D
  1.7439 +** to automatically [checkpoint]
  1.7440 +** after committing a transaction if there are N or
  1.7441 +** more frames in the [write-ahead log] file.  ^Passing zero or 
  1.7442 +** a negative value as the nFrame parameter disables automatic
  1.7443 +** checkpoints entirely.
  1.7444 +**
  1.7445 +** ^The callback registered by this function replaces any existing callback
  1.7446 +** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
  1.7447 +** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
  1.7448 +** configured by this function.
  1.7449 +**
  1.7450 +** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
  1.7451 +** from SQL.
  1.7452 +**
  1.7453 +** ^Every new [database connection] defaults to having the auto-checkpoint
  1.7454 +** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
  1.7455 +** pages.  The use of this interface
  1.7456 +** is only necessary if the default setting is found to be suboptimal
  1.7457 +** for a particular application.
  1.7458 +*/
  1.7459 +SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  1.7460 +
  1.7461 +/*
  1.7462 +** CAPI3REF: Checkpoint a database
  1.7463 +**
  1.7464 +** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
  1.7465 +** on [database connection] D to be [checkpointed].  ^If X is NULL or an
  1.7466 +** empty string, then a checkpoint is run on all databases of
  1.7467 +** connection D.  ^If the database connection D is not in
  1.7468 +** [WAL | write-ahead log mode] then this interface is a harmless no-op.
  1.7469 +**
  1.7470 +** ^The [wal_checkpoint pragma] can be used to invoke this interface
  1.7471 +** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
  1.7472 +** [wal_autocheckpoint pragma] can be used to cause this interface to be
  1.7473 +** run whenever the WAL reaches a certain size threshold.
  1.7474 +**
  1.7475 +** See also: [sqlite3_wal_checkpoint_v2()]
  1.7476 +*/
  1.7477 +SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  1.7478 +
  1.7479 +/*
  1.7480 +** CAPI3REF: Checkpoint a database
  1.7481 +**
  1.7482 +** Run a checkpoint operation on WAL database zDb attached to database 
  1.7483 +** handle db. The specific operation is determined by the value of the 
  1.7484 +** eMode parameter:
  1.7485 +**
  1.7486 +** <dl>
  1.7487 +** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
  1.7488 +**   Checkpoint as many frames as possible without waiting for any database 
  1.7489 +**   readers or writers to finish. Sync the db file if all frames in the log
  1.7490 +**   are checkpointed. This mode is the same as calling 
  1.7491 +**   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
  1.7492 +**
  1.7493 +** <dt>SQLITE_CHECKPOINT_FULL<dd>
  1.7494 +**   This mode blocks (calls the busy-handler callback) until there is no
  1.7495 +**   database writer and all readers are reading from the most recent database
  1.7496 +**   snapshot. It then checkpoints all frames in the log file and syncs the
  1.7497 +**   database file. This call blocks database writers while it is running,
  1.7498 +**   but not database readers.
  1.7499 +**
  1.7500 +** <dt>SQLITE_CHECKPOINT_RESTART<dd>
  1.7501 +**   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
  1.7502 +**   checkpointing the log file it blocks (calls the busy-handler callback)
  1.7503 +**   until all readers are reading from the database file only. This ensures 
  1.7504 +**   that the next client to write to the database file restarts the log file 
  1.7505 +**   from the beginning. This call blocks database writers while it is running,
  1.7506 +**   but not database readers.
  1.7507 +** </dl>
  1.7508 +**
  1.7509 +** If pnLog is not NULL, then *pnLog is set to the total number of frames in
  1.7510 +** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
  1.7511 +** the total number of checkpointed frames (including any that were already
  1.7512 +** checkpointed when this function is called). *pnLog and *pnCkpt may be
  1.7513 +** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
  1.7514 +** If no values are available because of an error, they are both set to -1
  1.7515 +** before returning to communicate this to the caller.
  1.7516 +**
  1.7517 +** All calls obtain an exclusive "checkpoint" lock on the database file. If
  1.7518 +** any other process is running a checkpoint operation at the same time, the 
  1.7519 +** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a 
  1.7520 +** busy-handler configured, it will not be invoked in this case.
  1.7521 +**
  1.7522 +** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive 
  1.7523 +** "writer" lock on the database file. If the writer lock cannot be obtained
  1.7524 +** immediately, and a busy-handler is configured, it is invoked and the writer
  1.7525 +** lock retried until either the busy-handler returns 0 or the lock is
  1.7526 +** successfully obtained. The busy-handler is also invoked while waiting for
  1.7527 +** database readers as described above. If the busy-handler returns 0 before
  1.7528 +** the writer lock is obtained or while waiting for database readers, the
  1.7529 +** checkpoint operation proceeds from that point in the same way as 
  1.7530 +** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 
  1.7531 +** without blocking any further. SQLITE_BUSY is returned in this case.
  1.7532 +**
  1.7533 +** If parameter zDb is NULL or points to a zero length string, then the
  1.7534 +** specified operation is attempted on all WAL databases. In this case the
  1.7535 +** values written to output parameters *pnLog and *pnCkpt are undefined. If 
  1.7536 +** an SQLITE_BUSY error is encountered when processing one or more of the 
  1.7537 +** attached WAL databases, the operation is still attempted on any remaining 
  1.7538 +** attached databases and SQLITE_BUSY is returned to the caller. If any other 
  1.7539 +** error occurs while processing an attached database, processing is abandoned 
  1.7540 +** and the error code returned to the caller immediately. If no error 
  1.7541 +** (SQLITE_BUSY or otherwise) is encountered while processing the attached 
  1.7542 +** databases, SQLITE_OK is returned.
  1.7543 +**
  1.7544 +** If database zDb is the name of an attached database that is not in WAL
  1.7545 +** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
  1.7546 +** zDb is not NULL (or a zero length string) and is not the name of any
  1.7547 +** attached database, SQLITE_ERROR is returned to the caller.
  1.7548 +*/
  1.7549 +SQLITE_API int sqlite3_wal_checkpoint_v2(
  1.7550 +  sqlite3 *db,                    /* Database handle */
  1.7551 +  const char *zDb,                /* Name of attached database (or NULL) */
  1.7552 +  int eMode,                      /* SQLITE_CHECKPOINT_* value */
  1.7553 +  int *pnLog,                     /* OUT: Size of WAL log in frames */
  1.7554 +  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
  1.7555 +);
  1.7556 +
  1.7557 +/*
  1.7558 +** CAPI3REF: Checkpoint operation parameters
  1.7559 +**
  1.7560 +** These constants can be used as the 3rd parameter to
  1.7561 +** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
  1.7562 +** documentation for additional information about the meaning and use of
  1.7563 +** each of these values.
  1.7564 +*/
  1.7565 +#define SQLITE_CHECKPOINT_PASSIVE 0
  1.7566 +#define SQLITE_CHECKPOINT_FULL    1
  1.7567 +#define SQLITE_CHECKPOINT_RESTART 2
  1.7568 +
  1.7569 +/*
  1.7570 +** CAPI3REF: Virtual Table Interface Configuration
  1.7571 +**
  1.7572 +** This function may be called by either the [xConnect] or [xCreate] method
  1.7573 +** of a [virtual table] implementation to configure
  1.7574 +** various facets of the virtual table interface.
  1.7575 +**
  1.7576 +** If this interface is invoked outside the context of an xConnect or
  1.7577 +** xCreate virtual table method then the behavior is undefined.
  1.7578 +**
  1.7579 +** At present, there is only one option that may be configured using
  1.7580 +** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
  1.7581 +** may be added in the future.
  1.7582 +*/
  1.7583 +SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
  1.7584 +
  1.7585 +/*
  1.7586 +** CAPI3REF: Virtual Table Configuration Options
  1.7587 +**
  1.7588 +** These macros define the various options to the
  1.7589 +** [sqlite3_vtab_config()] interface that [virtual table] implementations
  1.7590 +** can use to customize and optimize their behavior.
  1.7591 +**
  1.7592 +** <dl>
  1.7593 +** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  1.7594 +** <dd>Calls of the form
  1.7595 +** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  1.7596 +** where X is an integer.  If X is zero, then the [virtual table] whose
  1.7597 +** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  1.7598 +** support constraints.  In this configuration (which is the default) if
  1.7599 +** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
  1.7600 +** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
  1.7601 +** specified as part of the users SQL statement, regardless of the actual
  1.7602 +** ON CONFLICT mode specified.
  1.7603 +**
  1.7604 +** If X is non-zero, then the virtual table implementation guarantees
  1.7605 +** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
  1.7606 +** any modifications to internal or persistent data structures have been made.
  1.7607 +** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite 
  1.7608 +** is able to roll back a statement or database transaction, and abandon
  1.7609 +** or continue processing the current SQL statement as appropriate. 
  1.7610 +** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
  1.7611 +** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
  1.7612 +** had been ABORT.
  1.7613 +**
  1.7614 +** Virtual table implementations that are required to handle OR REPLACE
  1.7615 +** must do so within the [xUpdate] method. If a call to the 
  1.7616 +** [sqlite3_vtab_on_conflict()] function indicates that the current ON 
  1.7617 +** CONFLICT policy is REPLACE, the virtual table implementation should 
  1.7618 +** silently replace the appropriate rows within the xUpdate callback and
  1.7619 +** return SQLITE_OK. Or, if this is not possible, it may return
  1.7620 +** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT 
  1.7621 +** constraint handling.
  1.7622 +** </dl>
  1.7623 +*/
  1.7624 +#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
  1.7625 +
  1.7626 +/*
  1.7627 +** CAPI3REF: Determine The Virtual Table Conflict Policy
  1.7628 +**
  1.7629 +** This function may only be called from within a call to the [xUpdate] method
  1.7630 +** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
  1.7631 +** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
  1.7632 +** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
  1.7633 +** of the SQL statement that triggered the call to the [xUpdate] method of the
  1.7634 +** [virtual table].
  1.7635 +*/
  1.7636 +SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
  1.7637 +
  1.7638 +/*
  1.7639 +** CAPI3REF: Conflict resolution modes
  1.7640 +**
  1.7641 +** These constants are returned by [sqlite3_vtab_on_conflict()] to
  1.7642 +** inform a [virtual table] implementation what the [ON CONFLICT] mode
  1.7643 +** is for the SQL statement being evaluated.
  1.7644 +**
  1.7645 +** Note that the [SQLITE_IGNORE] constant is also used as a potential
  1.7646 +** return value from the [sqlite3_set_authorizer()] callback and that
  1.7647 +** [SQLITE_ABORT] is also a [result code].
  1.7648 +*/
  1.7649 +#define SQLITE_ROLLBACK 1
  1.7650 +/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
  1.7651 +#define SQLITE_FAIL     3
  1.7652 +/* #define SQLITE_ABORT 4  // Also an error code */
  1.7653 +#define SQLITE_REPLACE  5
  1.7654 +
  1.7655 +
  1.7656 +
  1.7657 +/*
  1.7658 +** Undo the hack that converts floating point types to integer for
  1.7659 +** builds on processors without floating point support.
  1.7660 +*/
  1.7661 +#ifdef SQLITE_OMIT_FLOATING_POINT
  1.7662 +# undef double
  1.7663 +#endif
  1.7664 +
  1.7665 +#if 0
  1.7666 +}  /* End of the 'extern "C"' block */
  1.7667 +#endif
  1.7668 +#endif
  1.7669 +
  1.7670 +/*
  1.7671 +** 2010 August 30
  1.7672 +**
  1.7673 +** The author disclaims copyright to this source code.  In place of
  1.7674 +** a legal notice, here is a blessing:
  1.7675 +**
  1.7676 +**    May you do good and not evil.
  1.7677 +**    May you find forgiveness for yourself and forgive others.
  1.7678 +**    May you share freely, never taking more than you give.
  1.7679 +**
  1.7680 +*************************************************************************
  1.7681 +*/
  1.7682 +
  1.7683 +#ifndef _SQLITE3RTREE_H_
  1.7684 +#define _SQLITE3RTREE_H_
  1.7685 +
  1.7686 +
  1.7687 +#if 0
  1.7688 +extern "C" {
  1.7689 +#endif
  1.7690 +
  1.7691 +typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
  1.7692 +
  1.7693 +/*
  1.7694 +** Register a geometry callback named zGeom that can be used as part of an
  1.7695 +** R-Tree geometry query as follows:
  1.7696 +**
  1.7697 +**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
  1.7698 +*/
  1.7699 +SQLITE_API int sqlite3_rtree_geometry_callback(
  1.7700 +  sqlite3 *db,
  1.7701 +  const char *zGeom,
  1.7702 +#ifdef SQLITE_RTREE_INT_ONLY
  1.7703 +  int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
  1.7704 +#else
  1.7705 +  int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
  1.7706 +#endif
  1.7707 +  void *pContext
  1.7708 +);
  1.7709 +
  1.7710 +
  1.7711 +/*
  1.7712 +** A pointer to a structure of the following type is passed as the first
  1.7713 +** argument to callbacks registered using rtree_geometry_callback().
  1.7714 +*/
  1.7715 +struct sqlite3_rtree_geometry {
  1.7716 +  void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
  1.7717 +  int nParam;                     /* Size of array aParam[] */
  1.7718 +  double *aParam;                 /* Parameters passed to SQL geom function */
  1.7719 +  void *pUser;                    /* Callback implementation user data */
  1.7720 +  void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
  1.7721 +};
  1.7722 +
  1.7723 +
  1.7724 +#if 0
  1.7725 +}  /* end of the 'extern "C"' block */
  1.7726 +#endif
  1.7727 +
  1.7728 +#endif  /* ifndef _SQLITE3RTREE_H_ */
  1.7729 +
  1.7730 +
  1.7731 +/************** End of sqlite3.h *********************************************/
  1.7732 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.7733 +/************** Include hash.h in the middle of sqliteInt.h ******************/
  1.7734 +/************** Begin file hash.h ********************************************/
  1.7735 +/*
  1.7736 +** 2001 September 22
  1.7737 +**
  1.7738 +** The author disclaims copyright to this source code.  In place of
  1.7739 +** a legal notice, here is a blessing:
  1.7740 +**
  1.7741 +**    May you do good and not evil.
  1.7742 +**    May you find forgiveness for yourself and forgive others.
  1.7743 +**    May you share freely, never taking more than you give.
  1.7744 +**
  1.7745 +*************************************************************************
  1.7746 +** This is the header file for the generic hash-table implemenation
  1.7747 +** used in SQLite.
  1.7748 +*/
  1.7749 +#ifndef _SQLITE_HASH_H_
  1.7750 +#define _SQLITE_HASH_H_
  1.7751 +
  1.7752 +/* Forward declarations of structures. */
  1.7753 +typedef struct Hash Hash;
  1.7754 +typedef struct HashElem HashElem;
  1.7755 +
  1.7756 +/* A complete hash table is an instance of the following structure.
  1.7757 +** The internals of this structure are intended to be opaque -- client
  1.7758 +** code should not attempt to access or modify the fields of this structure
  1.7759 +** directly.  Change this structure only by using the routines below.
  1.7760 +** However, some of the "procedures" and "functions" for modifying and
  1.7761 +** accessing this structure are really macros, so we can't really make
  1.7762 +** this structure opaque.
  1.7763 +**
  1.7764 +** All elements of the hash table are on a single doubly-linked list.
  1.7765 +** Hash.first points to the head of this list.
  1.7766 +**
  1.7767 +** There are Hash.htsize buckets.  Each bucket points to a spot in
  1.7768 +** the global doubly-linked list.  The contents of the bucket are the
  1.7769 +** element pointed to plus the next _ht.count-1 elements in the list.
  1.7770 +**
  1.7771 +** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
  1.7772 +** by a linear search of the global list.  For small tables, the 
  1.7773 +** Hash.ht table is never allocated because if there are few elements
  1.7774 +** in the table, it is faster to do a linear search than to manage
  1.7775 +** the hash table.
  1.7776 +*/
  1.7777 +struct Hash {
  1.7778 +  unsigned int htsize;      /* Number of buckets in the hash table */
  1.7779 +  unsigned int count;       /* Number of entries in this table */
  1.7780 +  HashElem *first;          /* The first element of the array */
  1.7781 +  struct _ht {              /* the hash table */
  1.7782 +    int count;                 /* Number of entries with this hash */
  1.7783 +    HashElem *chain;           /* Pointer to first entry with this hash */
  1.7784 +  } *ht;
  1.7785 +};
  1.7786 +
  1.7787 +/* Each element in the hash table is an instance of the following 
  1.7788 +** structure.  All elements are stored on a single doubly-linked list.
  1.7789 +**
  1.7790 +** Again, this structure is intended to be opaque, but it can't really
  1.7791 +** be opaque because it is used by macros.
  1.7792 +*/
  1.7793 +struct HashElem {
  1.7794 +  HashElem *next, *prev;       /* Next and previous elements in the table */
  1.7795 +  void *data;                  /* Data associated with this element */
  1.7796 +  const char *pKey; int nKey;  /* Key associated with this element */
  1.7797 +};
  1.7798 +
  1.7799 +/*
  1.7800 +** Access routines.  To delete, insert a NULL pointer.
  1.7801 +*/
  1.7802 +SQLITE_PRIVATE void sqlite3HashInit(Hash*);
  1.7803 +SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
  1.7804 +SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
  1.7805 +SQLITE_PRIVATE void sqlite3HashClear(Hash*);
  1.7806 +
  1.7807 +/*
  1.7808 +** Macros for looping over all elements of a hash table.  The idiom is
  1.7809 +** like this:
  1.7810 +**
  1.7811 +**   Hash h;
  1.7812 +**   HashElem *p;
  1.7813 +**   ...
  1.7814 +**   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
  1.7815 +**     SomeStructure *pData = sqliteHashData(p);
  1.7816 +**     // do something with pData
  1.7817 +**   }
  1.7818 +*/
  1.7819 +#define sqliteHashFirst(H)  ((H)->first)
  1.7820 +#define sqliteHashNext(E)   ((E)->next)
  1.7821 +#define sqliteHashData(E)   ((E)->data)
  1.7822 +/* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
  1.7823 +/* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
  1.7824 +
  1.7825 +/*
  1.7826 +** Number of entries in a hash table
  1.7827 +*/
  1.7828 +/* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
  1.7829 +
  1.7830 +#endif /* _SQLITE_HASH_H_ */
  1.7831 +
  1.7832 +/************** End of hash.h ************************************************/
  1.7833 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.7834 +/************** Include parse.h in the middle of sqliteInt.h *****************/
  1.7835 +/************** Begin file parse.h *******************************************/
  1.7836 +#define TK_SEMI                            1
  1.7837 +#define TK_EXPLAIN                         2
  1.7838 +#define TK_QUERY                           3
  1.7839 +#define TK_PLAN                            4
  1.7840 +#define TK_BEGIN                           5
  1.7841 +#define TK_TRANSACTION                     6
  1.7842 +#define TK_DEFERRED                        7
  1.7843 +#define TK_IMMEDIATE                       8
  1.7844 +#define TK_EXCLUSIVE                       9
  1.7845 +#define TK_COMMIT                         10
  1.7846 +#define TK_END                            11
  1.7847 +#define TK_ROLLBACK                       12
  1.7848 +#define TK_SAVEPOINT                      13
  1.7849 +#define TK_RELEASE                        14
  1.7850 +#define TK_TO                             15
  1.7851 +#define TK_TABLE                          16
  1.7852 +#define TK_CREATE                         17
  1.7853 +#define TK_IF                             18
  1.7854 +#define TK_NOT                            19
  1.7855 +#define TK_EXISTS                         20
  1.7856 +#define TK_TEMP                           21
  1.7857 +#define TK_LP                             22
  1.7858 +#define TK_RP                             23
  1.7859 +#define TK_AS                             24
  1.7860 +#define TK_COMMA                          25
  1.7861 +#define TK_ID                             26
  1.7862 +#define TK_INDEXED                        27
  1.7863 +#define TK_ABORT                          28
  1.7864 +#define TK_ACTION                         29
  1.7865 +#define TK_AFTER                          30
  1.7866 +#define TK_ANALYZE                        31
  1.7867 +#define TK_ASC                            32
  1.7868 +#define TK_ATTACH                         33
  1.7869 +#define TK_BEFORE                         34
  1.7870 +#define TK_BY                             35
  1.7871 +#define TK_CASCADE                        36
  1.7872 +#define TK_CAST                           37
  1.7873 +#define TK_COLUMNKW                       38
  1.7874 +#define TK_CONFLICT                       39
  1.7875 +#define TK_DATABASE                       40
  1.7876 +#define TK_DESC                           41
  1.7877 +#define TK_DETACH                         42
  1.7878 +#define TK_EACH                           43
  1.7879 +#define TK_FAIL                           44
  1.7880 +#define TK_FOR                            45
  1.7881 +#define TK_IGNORE                         46
  1.7882 +#define TK_INITIALLY                      47
  1.7883 +#define TK_INSTEAD                        48
  1.7884 +#define TK_LIKE_KW                        49
  1.7885 +#define TK_MATCH                          50
  1.7886 +#define TK_NO                             51
  1.7887 +#define TK_KEY                            52
  1.7888 +#define TK_OF                             53
  1.7889 +#define TK_OFFSET                         54
  1.7890 +#define TK_PRAGMA                         55
  1.7891 +#define TK_RAISE                          56
  1.7892 +#define TK_REPLACE                        57
  1.7893 +#define TK_RESTRICT                       58
  1.7894 +#define TK_ROW                            59
  1.7895 +#define TK_TRIGGER                        60
  1.7896 +#define TK_VACUUM                         61
  1.7897 +#define TK_VIEW                           62
  1.7898 +#define TK_VIRTUAL                        63
  1.7899 +#define TK_REINDEX                        64
  1.7900 +#define TK_RENAME                         65
  1.7901 +#define TK_CTIME_KW                       66
  1.7902 +#define TK_ANY                            67
  1.7903 +#define TK_OR                             68
  1.7904 +#define TK_AND                            69
  1.7905 +#define TK_IS                             70
  1.7906 +#define TK_BETWEEN                        71
  1.7907 +#define TK_IN                             72
  1.7908 +#define TK_ISNULL                         73
  1.7909 +#define TK_NOTNULL                        74
  1.7910 +#define TK_NE                             75
  1.7911 +#define TK_EQ                             76
  1.7912 +#define TK_GT                             77
  1.7913 +#define TK_LE                             78
  1.7914 +#define TK_LT                             79
  1.7915 +#define TK_GE                             80
  1.7916 +#define TK_ESCAPE                         81
  1.7917 +#define TK_BITAND                         82
  1.7918 +#define TK_BITOR                          83
  1.7919 +#define TK_LSHIFT                         84
  1.7920 +#define TK_RSHIFT                         85
  1.7921 +#define TK_PLUS                           86
  1.7922 +#define TK_MINUS                          87
  1.7923 +#define TK_STAR                           88
  1.7924 +#define TK_SLASH                          89
  1.7925 +#define TK_REM                            90
  1.7926 +#define TK_CONCAT                         91
  1.7927 +#define TK_COLLATE                        92
  1.7928 +#define TK_BITNOT                         93
  1.7929 +#define TK_STRING                         94
  1.7930 +#define TK_JOIN_KW                        95
  1.7931 +#define TK_CONSTRAINT                     96
  1.7932 +#define TK_DEFAULT                        97
  1.7933 +#define TK_NULL                           98
  1.7934 +#define TK_PRIMARY                        99
  1.7935 +#define TK_UNIQUE                         100
  1.7936 +#define TK_CHECK                          101
  1.7937 +#define TK_REFERENCES                     102
  1.7938 +#define TK_AUTOINCR                       103
  1.7939 +#define TK_ON                             104
  1.7940 +#define TK_INSERT                         105
  1.7941 +#define TK_DELETE                         106
  1.7942 +#define TK_UPDATE                         107
  1.7943 +#define TK_SET                            108
  1.7944 +#define TK_DEFERRABLE                     109
  1.7945 +#define TK_FOREIGN                        110
  1.7946 +#define TK_DROP                           111
  1.7947 +#define TK_UNION                          112
  1.7948 +#define TK_ALL                            113
  1.7949 +#define TK_EXCEPT                         114
  1.7950 +#define TK_INTERSECT                      115
  1.7951 +#define TK_SELECT                         116
  1.7952 +#define TK_DISTINCT                       117
  1.7953 +#define TK_DOT                            118
  1.7954 +#define TK_FROM                           119
  1.7955 +#define TK_JOIN                           120
  1.7956 +#define TK_USING                          121
  1.7957 +#define TK_ORDER                          122
  1.7958 +#define TK_GROUP                          123
  1.7959 +#define TK_HAVING                         124
  1.7960 +#define TK_LIMIT                          125
  1.7961 +#define TK_WHERE                          126
  1.7962 +#define TK_INTO                           127
  1.7963 +#define TK_VALUES                         128
  1.7964 +#define TK_INTEGER                        129
  1.7965 +#define TK_FLOAT                          130
  1.7966 +#define TK_BLOB                           131
  1.7967 +#define TK_REGISTER                       132
  1.7968 +#define TK_VARIABLE                       133
  1.7969 +#define TK_CASE                           134
  1.7970 +#define TK_WHEN                           135
  1.7971 +#define TK_THEN                           136
  1.7972 +#define TK_ELSE                           137
  1.7973 +#define TK_INDEX                          138
  1.7974 +#define TK_ALTER                          139
  1.7975 +#define TK_ADD                            140
  1.7976 +#define TK_TO_TEXT                        141
  1.7977 +#define TK_TO_BLOB                        142
  1.7978 +#define TK_TO_NUMERIC                     143
  1.7979 +#define TK_TO_INT                         144
  1.7980 +#define TK_TO_REAL                        145
  1.7981 +#define TK_ISNOT                          146
  1.7982 +#define TK_END_OF_FILE                    147
  1.7983 +#define TK_ILLEGAL                        148
  1.7984 +#define TK_SPACE                          149
  1.7985 +#define TK_UNCLOSED_STRING                150
  1.7986 +#define TK_FUNCTION                       151
  1.7987 +#define TK_COLUMN                         152
  1.7988 +#define TK_AGG_FUNCTION                   153
  1.7989 +#define TK_AGG_COLUMN                     154
  1.7990 +#define TK_CONST_FUNC                     155
  1.7991 +#define TK_UMINUS                         156
  1.7992 +#define TK_UPLUS                          157
  1.7993 +
  1.7994 +/************** End of parse.h ***********************************************/
  1.7995 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.7996 +#include <stdio.h>
  1.7997 +#include <stdlib.h>
  1.7998 +#include <string.h>
  1.7999 +#include <assert.h>
  1.8000 +#include <stddef.h>
  1.8001 +
  1.8002 +/*
  1.8003 +** If compiling for a processor that lacks floating point support,
  1.8004 +** substitute integer for floating-point
  1.8005 +*/
  1.8006 +#ifdef SQLITE_OMIT_FLOATING_POINT
  1.8007 +# define double sqlite_int64
  1.8008 +# define float sqlite_int64
  1.8009 +# define LONGDOUBLE_TYPE sqlite_int64
  1.8010 +# ifndef SQLITE_BIG_DBL
  1.8011 +#   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
  1.8012 +# endif
  1.8013 +# define SQLITE_OMIT_DATETIME_FUNCS 1
  1.8014 +# define SQLITE_OMIT_TRACE 1
  1.8015 +# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  1.8016 +# undef SQLITE_HAVE_ISNAN
  1.8017 +#endif
  1.8018 +#ifndef SQLITE_BIG_DBL
  1.8019 +# define SQLITE_BIG_DBL (1e99)
  1.8020 +#endif
  1.8021 +
  1.8022 +/*
  1.8023 +** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  1.8024 +** afterward. Having this macro allows us to cause the C compiler 
  1.8025 +** to omit code used by TEMP tables without messy #ifndef statements.
  1.8026 +*/
  1.8027 +#ifdef SQLITE_OMIT_TEMPDB
  1.8028 +#define OMIT_TEMPDB 1
  1.8029 +#else
  1.8030 +#define OMIT_TEMPDB 0
  1.8031 +#endif
  1.8032 +
  1.8033 +/*
  1.8034 +** The "file format" number is an integer that is incremented whenever
  1.8035 +** the VDBE-level file format changes.  The following macros define the
  1.8036 +** the default file format for new databases and the maximum file format
  1.8037 +** that the library can read.
  1.8038 +*/
  1.8039 +#define SQLITE_MAX_FILE_FORMAT 4
  1.8040 +#ifndef SQLITE_DEFAULT_FILE_FORMAT
  1.8041 +# define SQLITE_DEFAULT_FILE_FORMAT 4
  1.8042 +#endif
  1.8043 +
  1.8044 +/*
  1.8045 +** Determine whether triggers are recursive by default.  This can be
  1.8046 +** changed at run-time using a pragma.
  1.8047 +*/
  1.8048 +#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  1.8049 +# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
  1.8050 +#endif
  1.8051 +
  1.8052 +/*
  1.8053 +** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
  1.8054 +** on the command-line
  1.8055 +*/
  1.8056 +#ifndef SQLITE_TEMP_STORE
  1.8057 +# define SQLITE_TEMP_STORE 1
  1.8058 +#endif
  1.8059 +
  1.8060 +/*
  1.8061 +** GCC does not define the offsetof() macro so we'll have to do it
  1.8062 +** ourselves.
  1.8063 +*/
  1.8064 +#ifndef offsetof
  1.8065 +#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
  1.8066 +#endif
  1.8067 +
  1.8068 +/*
  1.8069 +** Check to see if this machine uses EBCDIC.  (Yes, believe it or
  1.8070 +** not, there are still machines out there that use EBCDIC.)
  1.8071 +*/
  1.8072 +#if 'A' == '\301'
  1.8073 +# define SQLITE_EBCDIC 1
  1.8074 +#else
  1.8075 +# define SQLITE_ASCII 1
  1.8076 +#endif
  1.8077 +
  1.8078 +/*
  1.8079 +** Integers of known sizes.  These typedefs might change for architectures
  1.8080 +** where the sizes very.  Preprocessor macros are available so that the
  1.8081 +** types can be conveniently redefined at compile-type.  Like this:
  1.8082 +**
  1.8083 +**         cc '-DUINTPTR_TYPE=long long int' ...
  1.8084 +*/
  1.8085 +#ifndef UINT32_TYPE
  1.8086 +# ifdef HAVE_UINT32_T
  1.8087 +#  define UINT32_TYPE uint32_t
  1.8088 +# else
  1.8089 +#  define UINT32_TYPE unsigned int
  1.8090 +# endif
  1.8091 +#endif
  1.8092 +#ifndef UINT16_TYPE
  1.8093 +# ifdef HAVE_UINT16_T
  1.8094 +#  define UINT16_TYPE uint16_t
  1.8095 +# else
  1.8096 +#  define UINT16_TYPE unsigned short int
  1.8097 +# endif
  1.8098 +#endif
  1.8099 +#ifndef INT16_TYPE
  1.8100 +# ifdef HAVE_INT16_T
  1.8101 +#  define INT16_TYPE int16_t
  1.8102 +# else
  1.8103 +#  define INT16_TYPE short int
  1.8104 +# endif
  1.8105 +#endif
  1.8106 +#ifndef UINT8_TYPE
  1.8107 +# ifdef HAVE_UINT8_T
  1.8108 +#  define UINT8_TYPE uint8_t
  1.8109 +# else
  1.8110 +#  define UINT8_TYPE unsigned char
  1.8111 +# endif
  1.8112 +#endif
  1.8113 +#ifndef INT8_TYPE
  1.8114 +# ifdef HAVE_INT8_T
  1.8115 +#  define INT8_TYPE int8_t
  1.8116 +# else
  1.8117 +#  define INT8_TYPE signed char
  1.8118 +# endif
  1.8119 +#endif
  1.8120 +#ifndef LONGDOUBLE_TYPE
  1.8121 +# define LONGDOUBLE_TYPE long double
  1.8122 +#endif
  1.8123 +typedef sqlite_int64 i64;          /* 8-byte signed integer */
  1.8124 +typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
  1.8125 +typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
  1.8126 +typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
  1.8127 +typedef INT16_TYPE i16;            /* 2-byte signed integer */
  1.8128 +typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
  1.8129 +typedef INT8_TYPE i8;              /* 1-byte signed integer */
  1.8130 +
  1.8131 +/*
  1.8132 +** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  1.8133 +** that can be stored in a u32 without loss of data.  The value
  1.8134 +** is 0x00000000ffffffff.  But because of quirks of some compilers, we
  1.8135 +** have to specify the value in the less intuitive manner shown:
  1.8136 +*/
  1.8137 +#define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
  1.8138 +
  1.8139 +/*
  1.8140 +** The datatype used to store estimates of the number of rows in a
  1.8141 +** table or index.  This is an unsigned integer type.  For 99.9% of
  1.8142 +** the world, a 32-bit integer is sufficient.  But a 64-bit integer
  1.8143 +** can be used at compile-time if desired.
  1.8144 +*/
  1.8145 +#ifdef SQLITE_64BIT_STATS
  1.8146 + typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
  1.8147 +#else
  1.8148 + typedef u32 tRowcnt;    /* 32-bit is the default */
  1.8149 +#endif
  1.8150 +
  1.8151 +/*
  1.8152 +** Macros to determine whether the machine is big or little endian,
  1.8153 +** evaluated at runtime.
  1.8154 +*/
  1.8155 +#ifdef SQLITE_AMALGAMATION
  1.8156 +SQLITE_PRIVATE const int sqlite3one = 1;
  1.8157 +#else
  1.8158 +SQLITE_PRIVATE const int sqlite3one;
  1.8159 +#endif
  1.8160 +#if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  1.8161 +                             || defined(__x86_64) || defined(__x86_64__)
  1.8162 +# define SQLITE_BIGENDIAN    0
  1.8163 +# define SQLITE_LITTLEENDIAN 1
  1.8164 +# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
  1.8165 +#else
  1.8166 +# define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
  1.8167 +# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  1.8168 +# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  1.8169 +#endif
  1.8170 +
  1.8171 +/*
  1.8172 +** Constants for the largest and smallest possible 64-bit signed integers.
  1.8173 +** These macros are designed to work correctly on both 32-bit and 64-bit
  1.8174 +** compilers.
  1.8175 +*/
  1.8176 +#define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
  1.8177 +#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  1.8178 +
  1.8179 +/* 
  1.8180 +** Round up a number to the next larger multiple of 8.  This is used
  1.8181 +** to force 8-byte alignment on 64-bit architectures.
  1.8182 +*/
  1.8183 +#define ROUND8(x)     (((x)+7)&~7)
  1.8184 +
  1.8185 +/*
  1.8186 +** Round down to the nearest multiple of 8
  1.8187 +*/
  1.8188 +#define ROUNDDOWN8(x) ((x)&~7)
  1.8189 +
  1.8190 +/*
  1.8191 +** Assert that the pointer X is aligned to an 8-byte boundary.  This
  1.8192 +** macro is used only within assert() to verify that the code gets
  1.8193 +** all alignment restrictions correct.
  1.8194 +**
  1.8195 +** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
  1.8196 +** underlying malloc() implemention might return us 4-byte aligned
  1.8197 +** pointers.  In that case, only verify 4-byte alignment.
  1.8198 +*/
  1.8199 +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
  1.8200 +# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
  1.8201 +#else
  1.8202 +# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
  1.8203 +#endif
  1.8204 +
  1.8205 +
  1.8206 +/*
  1.8207 +** An instance of the following structure is used to store the busy-handler
  1.8208 +** callback for a given sqlite handle. 
  1.8209 +**
  1.8210 +** The sqlite.busyHandler member of the sqlite struct contains the busy
  1.8211 +** callback for the database handle. Each pager opened via the sqlite
  1.8212 +** handle is passed a pointer to sqlite.busyHandler. The busy-handler
  1.8213 +** callback is currently invoked only from within pager.c.
  1.8214 +*/
  1.8215 +typedef struct BusyHandler BusyHandler;
  1.8216 +struct BusyHandler {
  1.8217 +  int (*xFunc)(void *,int);  /* The busy callback */
  1.8218 +  void *pArg;                /* First arg to busy callback */
  1.8219 +  int nBusy;                 /* Incremented with each busy call */
  1.8220 +};
  1.8221 +
  1.8222 +/*
  1.8223 +** Name of the master database table.  The master database table
  1.8224 +** is a special table that holds the names and attributes of all
  1.8225 +** user tables and indices.
  1.8226 +*/
  1.8227 +#define MASTER_NAME       "sqlite_master"
  1.8228 +#define TEMP_MASTER_NAME  "sqlite_temp_master"
  1.8229 +
  1.8230 +/*
  1.8231 +** The root-page of the master database table.
  1.8232 +*/
  1.8233 +#define MASTER_ROOT       1
  1.8234 +
  1.8235 +/*
  1.8236 +** The name of the schema table.
  1.8237 +*/
  1.8238 +#define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
  1.8239 +
  1.8240 +/*
  1.8241 +** A convenience macro that returns the number of elements in
  1.8242 +** an array.
  1.8243 +*/
  1.8244 +#define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
  1.8245 +
  1.8246 +/*
  1.8247 +** The following value as a destructor means to use sqlite3DbFree().
  1.8248 +** The sqlite3DbFree() routine requires two parameters instead of the 
  1.8249 +** one parameter that destructors normally want.  So we have to introduce 
  1.8250 +** this magic value that the code knows to handle differently.  Any 
  1.8251 +** pointer will work here as long as it is distinct from SQLITE_STATIC
  1.8252 +** and SQLITE_TRANSIENT.
  1.8253 +*/
  1.8254 +#define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3MallocSize)
  1.8255 +
  1.8256 +/*
  1.8257 +** When SQLITE_OMIT_WSD is defined, it means that the target platform does
  1.8258 +** not support Writable Static Data (WSD) such as global and static variables.
  1.8259 +** All variables must either be on the stack or dynamically allocated from
  1.8260 +** the heap.  When WSD is unsupported, the variable declarations scattered
  1.8261 +** throughout the SQLite code must become constants instead.  The SQLITE_WSD
  1.8262 +** macro is used for this purpose.  And instead of referencing the variable
  1.8263 +** directly, we use its constant as a key to lookup the run-time allocated
  1.8264 +** buffer that holds real variable.  The constant is also the initializer
  1.8265 +** for the run-time allocated buffer.
  1.8266 +**
  1.8267 +** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
  1.8268 +** macros become no-ops and have zero performance impact.
  1.8269 +*/
  1.8270 +#ifdef SQLITE_OMIT_WSD
  1.8271 +  #define SQLITE_WSD const
  1.8272 +  #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
  1.8273 +  #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
  1.8274 +SQLITE_API   int sqlite3_wsd_init(int N, int J);
  1.8275 +SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
  1.8276 +#else
  1.8277 +  #define SQLITE_WSD 
  1.8278 +  #define GLOBAL(t,v) v
  1.8279 +  #define sqlite3GlobalConfig sqlite3Config
  1.8280 +#endif
  1.8281 +
  1.8282 +/*
  1.8283 +** The following macros are used to suppress compiler warnings and to
  1.8284 +** make it clear to human readers when a function parameter is deliberately 
  1.8285 +** left unused within the body of a function. This usually happens when
  1.8286 +** a function is called via a function pointer. For example the 
  1.8287 +** implementation of an SQL aggregate step callback may not use the
  1.8288 +** parameter indicating the number of arguments passed to the aggregate,
  1.8289 +** if it knows that this is enforced elsewhere.
  1.8290 +**
  1.8291 +** When a function parameter is not used at all within the body of a function,
  1.8292 +** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
  1.8293 +** However, these macros may also be used to suppress warnings related to
  1.8294 +** parameters that may or may not be used depending on compilation options.
  1.8295 +** For example those parameters only used in assert() statements. In these
  1.8296 +** cases the parameters are named as per the usual conventions.
  1.8297 +*/
  1.8298 +#define UNUSED_PARAMETER(x) (void)(x)
  1.8299 +#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
  1.8300 +
  1.8301 +/*
  1.8302 +** Forward references to structures
  1.8303 +*/
  1.8304 +typedef struct AggInfo AggInfo;
  1.8305 +typedef struct AuthContext AuthContext;
  1.8306 +typedef struct AutoincInfo AutoincInfo;
  1.8307 +typedef struct Bitvec Bitvec;
  1.8308 +typedef struct CollSeq CollSeq;
  1.8309 +typedef struct Column Column;
  1.8310 +typedef struct Db Db;
  1.8311 +typedef struct Schema Schema;
  1.8312 +typedef struct Expr Expr;
  1.8313 +typedef struct ExprList ExprList;
  1.8314 +typedef struct ExprSpan ExprSpan;
  1.8315 +typedef struct FKey FKey;
  1.8316 +typedef struct FuncDestructor FuncDestructor;
  1.8317 +typedef struct FuncDef FuncDef;
  1.8318 +typedef struct FuncDefHash FuncDefHash;
  1.8319 +typedef struct IdList IdList;
  1.8320 +typedef struct Index Index;
  1.8321 +typedef struct IndexSample IndexSample;
  1.8322 +typedef struct KeyClass KeyClass;
  1.8323 +typedef struct KeyInfo KeyInfo;
  1.8324 +typedef struct Lookaside Lookaside;
  1.8325 +typedef struct LookasideSlot LookasideSlot;
  1.8326 +typedef struct Module Module;
  1.8327 +typedef struct NameContext NameContext;
  1.8328 +typedef struct Parse Parse;
  1.8329 +typedef struct RowSet RowSet;
  1.8330 +typedef struct Savepoint Savepoint;
  1.8331 +typedef struct Select Select;
  1.8332 +typedef struct SelectDest SelectDest;
  1.8333 +typedef struct SrcList SrcList;
  1.8334 +typedef struct StrAccum StrAccum;
  1.8335 +typedef struct Table Table;
  1.8336 +typedef struct TableLock TableLock;
  1.8337 +typedef struct Token Token;
  1.8338 +typedef struct Trigger Trigger;
  1.8339 +typedef struct TriggerPrg TriggerPrg;
  1.8340 +typedef struct TriggerStep TriggerStep;
  1.8341 +typedef struct UnpackedRecord UnpackedRecord;
  1.8342 +typedef struct VTable VTable;
  1.8343 +typedef struct VtabCtx VtabCtx;
  1.8344 +typedef struct Walker Walker;
  1.8345 +typedef struct WherePlan WherePlan;
  1.8346 +typedef struct WhereInfo WhereInfo;
  1.8347 +typedef struct WhereLevel WhereLevel;
  1.8348 +
  1.8349 +/*
  1.8350 +** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  1.8351 +** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  1.8352 +** pointer types (i.e. FuncDef) defined above.
  1.8353 +*/
  1.8354 +/************** Include btree.h in the middle of sqliteInt.h *****************/
  1.8355 +/************** Begin file btree.h *******************************************/
  1.8356 +/*
  1.8357 +** 2001 September 15
  1.8358 +**
  1.8359 +** The author disclaims copyright to this source code.  In place of
  1.8360 +** a legal notice, here is a blessing:
  1.8361 +**
  1.8362 +**    May you do good and not evil.
  1.8363 +**    May you find forgiveness for yourself and forgive others.
  1.8364 +**    May you share freely, never taking more than you give.
  1.8365 +**
  1.8366 +*************************************************************************
  1.8367 +** This header file defines the interface that the sqlite B-Tree file
  1.8368 +** subsystem.  See comments in the source code for a detailed description
  1.8369 +** of what each interface routine does.
  1.8370 +*/
  1.8371 +#ifndef _BTREE_H_
  1.8372 +#define _BTREE_H_
  1.8373 +
  1.8374 +/* TODO: This definition is just included so other modules compile. It
  1.8375 +** needs to be revisited.
  1.8376 +*/
  1.8377 +#define SQLITE_N_BTREE_META 10
  1.8378 +
  1.8379 +/*
  1.8380 +** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
  1.8381 +** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
  1.8382 +*/
  1.8383 +#ifndef SQLITE_DEFAULT_AUTOVACUUM
  1.8384 +  #define SQLITE_DEFAULT_AUTOVACUUM 0
  1.8385 +#endif
  1.8386 +
  1.8387 +#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
  1.8388 +#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
  1.8389 +#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
  1.8390 +
  1.8391 +/*
  1.8392 +** Forward declarations of structure
  1.8393 +*/
  1.8394 +typedef struct Btree Btree;
  1.8395 +typedef struct BtCursor BtCursor;
  1.8396 +typedef struct BtShared BtShared;
  1.8397 +
  1.8398 +
  1.8399 +SQLITE_PRIVATE int sqlite3BtreeOpen(
  1.8400 +  sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
  1.8401 +  const char *zFilename,   /* Name of database file to open */
  1.8402 +  sqlite3 *db,             /* Associated database connection */
  1.8403 +  Btree **ppBtree,         /* Return open Btree* here */
  1.8404 +  int flags,               /* Flags */
  1.8405 +  int vfsFlags             /* Flags passed through to VFS open */
  1.8406 +);
  1.8407 +
  1.8408 +/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
  1.8409 +** following values.
  1.8410 +**
  1.8411 +** NOTE:  These values must match the corresponding PAGER_ values in
  1.8412 +** pager.h.
  1.8413 +*/
  1.8414 +#define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
  1.8415 +#define BTREE_MEMORY        2  /* This is an in-memory DB */
  1.8416 +#define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
  1.8417 +#define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
  1.8418 +
  1.8419 +SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  1.8420 +SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  1.8421 +SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
  1.8422 +SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  1.8423 +SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  1.8424 +SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  1.8425 +SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  1.8426 +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
  1.8427 +SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
  1.8428 +SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
  1.8429 +#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
  1.8430 +SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
  1.8431 +#endif
  1.8432 +SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
  1.8433 +SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
  1.8434 +SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
  1.8435 +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
  1.8436 +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
  1.8437 +SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
  1.8438 +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int);
  1.8439 +SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
  1.8440 +SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
  1.8441 +SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
  1.8442 +SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
  1.8443 +SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
  1.8444 +SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
  1.8445 +SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
  1.8446 +SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
  1.8447 +SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
  1.8448 +
  1.8449 +SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
  1.8450 +SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
  1.8451 +SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
  1.8452 +
  1.8453 +SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
  1.8454 +
  1.8455 +/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
  1.8456 +** of the flags shown below.
  1.8457 +**
  1.8458 +** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
  1.8459 +** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
  1.8460 +** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
  1.8461 +** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
  1.8462 +** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
  1.8463 +** indices.)
  1.8464 +*/
  1.8465 +#define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
  1.8466 +#define BTREE_BLOBKEY    2    /* Table has keys only - no data */
  1.8467 +
  1.8468 +SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  1.8469 +SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
  1.8470 +SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  1.8471 +
  1.8472 +SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  1.8473 +SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
  1.8474 +
  1.8475 +SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
  1.8476 +
  1.8477 +/*
  1.8478 +** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
  1.8479 +** should be one of the following values. The integer values are assigned 
  1.8480 +** to constants so that the offset of the corresponding field in an
  1.8481 +** SQLite database header may be found using the following formula:
  1.8482 +**
  1.8483 +**   offset = 36 + (idx * 4)
  1.8484 +**
  1.8485 +** For example, the free-page-count field is located at byte offset 36 of
  1.8486 +** the database file header. The incr-vacuum-flag field is located at
  1.8487 +** byte offset 64 (== 36+4*7).
  1.8488 +*/
  1.8489 +#define BTREE_FREE_PAGE_COUNT     0
  1.8490 +#define BTREE_SCHEMA_VERSION      1
  1.8491 +#define BTREE_FILE_FORMAT         2
  1.8492 +#define BTREE_DEFAULT_CACHE_SIZE  3
  1.8493 +#define BTREE_LARGEST_ROOT_PAGE   4
  1.8494 +#define BTREE_TEXT_ENCODING       5
  1.8495 +#define BTREE_USER_VERSION        6
  1.8496 +#define BTREE_INCR_VACUUM         7
  1.8497 +
  1.8498 +/*
  1.8499 +** Values that may be OR'd together to form the second argument of an
  1.8500 +** sqlite3BtreeCursorHints() call.
  1.8501 +*/
  1.8502 +#define BTREE_BULKLOAD 0x00000001
  1.8503 +
  1.8504 +SQLITE_PRIVATE int sqlite3BtreeCursor(
  1.8505 +  Btree*,                              /* BTree containing table to open */
  1.8506 +  int iTable,                          /* Index of root page */
  1.8507 +  int wrFlag,                          /* 1 for writing.  0 for read-only */
  1.8508 +  struct KeyInfo*,                     /* First argument to compare function */
  1.8509 +  BtCursor *pCursor                    /* Space to write cursor structure */
  1.8510 +);
  1.8511 +SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
  1.8512 +SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
  1.8513 +
  1.8514 +SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
  1.8515 +SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
  1.8516 +  BtCursor*,
  1.8517 +  UnpackedRecord *pUnKey,
  1.8518 +  i64 intKey,
  1.8519 +  int bias,
  1.8520 +  int *pRes
  1.8521 +);
  1.8522 +SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
  1.8523 +SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
  1.8524 +SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
  1.8525 +                                  const void *pData, int nData,
  1.8526 +                                  int nZero, int bias, int seekResult);
  1.8527 +SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
  1.8528 +SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
  1.8529 +SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
  1.8530 +SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
  1.8531 +SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
  1.8532 +SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
  1.8533 +SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
  1.8534 +SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
  1.8535 +SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
  1.8536 +SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  1.8537 +SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  1.8538 +SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  1.8539 +SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  1.8540 +
  1.8541 +SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  1.8542 +SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  1.8543 +
  1.8544 +SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  1.8545 +SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  1.8546 +SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  1.8547 +SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  1.8548 +SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
  1.8549 +
  1.8550 +#ifndef NDEBUG
  1.8551 +SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  1.8552 +#endif
  1.8553 +
  1.8554 +#ifndef SQLITE_OMIT_BTREECOUNT
  1.8555 +SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
  1.8556 +#endif
  1.8557 +
  1.8558 +#ifdef SQLITE_TEST
  1.8559 +SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
  1.8560 +SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
  1.8561 +#endif
  1.8562 +
  1.8563 +#ifndef SQLITE_OMIT_WAL
  1.8564 +SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
  1.8565 +#endif
  1.8566 +
  1.8567 +/*
  1.8568 +** If we are not using shared cache, then there is no need to
  1.8569 +** use mutexes to access the BtShared structures.  So make the
  1.8570 +** Enter and Leave procedures no-ops.
  1.8571 +*/
  1.8572 +#ifndef SQLITE_OMIT_SHARED_CACHE
  1.8573 +SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
  1.8574 +SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
  1.8575 +#else
  1.8576 +# define sqlite3BtreeEnter(X) 
  1.8577 +# define sqlite3BtreeEnterAll(X)
  1.8578 +#endif
  1.8579 +
  1.8580 +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  1.8581 +SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
  1.8582 +SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
  1.8583 +SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
  1.8584 +SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
  1.8585 +SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
  1.8586 +#ifndef NDEBUG
  1.8587 +  /* These routines are used inside assert() statements only. */
  1.8588 +SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
  1.8589 +SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
  1.8590 +SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
  1.8591 +#endif
  1.8592 +#else
  1.8593 +
  1.8594 +# define sqlite3BtreeSharable(X) 0
  1.8595 +# define sqlite3BtreeLeave(X)
  1.8596 +# define sqlite3BtreeEnterCursor(X)
  1.8597 +# define sqlite3BtreeLeaveCursor(X)
  1.8598 +# define sqlite3BtreeLeaveAll(X)
  1.8599 +
  1.8600 +# define sqlite3BtreeHoldsMutex(X) 1
  1.8601 +# define sqlite3BtreeHoldsAllMutexes(X) 1
  1.8602 +# define sqlite3SchemaMutexHeld(X,Y,Z) 1
  1.8603 +#endif
  1.8604 +
  1.8605 +
  1.8606 +#endif /* _BTREE_H_ */
  1.8607 +
  1.8608 +/************** End of btree.h ***********************************************/
  1.8609 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.8610 +/************** Include vdbe.h in the middle of sqliteInt.h ******************/
  1.8611 +/************** Begin file vdbe.h ********************************************/
  1.8612 +/*
  1.8613 +** 2001 September 15
  1.8614 +**
  1.8615 +** The author disclaims copyright to this source code.  In place of
  1.8616 +** a legal notice, here is a blessing:
  1.8617 +**
  1.8618 +**    May you do good and not evil.
  1.8619 +**    May you find forgiveness for yourself and forgive others.
  1.8620 +**    May you share freely, never taking more than you give.
  1.8621 +**
  1.8622 +*************************************************************************
  1.8623 +** Header file for the Virtual DataBase Engine (VDBE)
  1.8624 +**
  1.8625 +** This header defines the interface to the virtual database engine
  1.8626 +** or VDBE.  The VDBE implements an abstract machine that runs a
  1.8627 +** simple program to access and modify the underlying database.
  1.8628 +*/
  1.8629 +#ifndef _SQLITE_VDBE_H_
  1.8630 +#define _SQLITE_VDBE_H_
  1.8631 +/* #include <stdio.h> */
  1.8632 +
  1.8633 +/*
  1.8634 +** A single VDBE is an opaque structure named "Vdbe".  Only routines
  1.8635 +** in the source file sqliteVdbe.c are allowed to see the insides
  1.8636 +** of this structure.
  1.8637 +*/
  1.8638 +typedef struct Vdbe Vdbe;
  1.8639 +
  1.8640 +/*
  1.8641 +** The names of the following types declared in vdbeInt.h are required
  1.8642 +** for the VdbeOp definition.
  1.8643 +*/
  1.8644 +typedef struct VdbeFunc VdbeFunc;
  1.8645 +typedef struct Mem Mem;
  1.8646 +typedef struct SubProgram SubProgram;
  1.8647 +
  1.8648 +/*
  1.8649 +** A single instruction of the virtual machine has an opcode
  1.8650 +** and as many as three operands.  The instruction is recorded
  1.8651 +** as an instance of the following structure:
  1.8652 +*/
  1.8653 +struct VdbeOp {
  1.8654 +  u8 opcode;          /* What operation to perform */
  1.8655 +  signed char p4type; /* One of the P4_xxx constants for p4 */
  1.8656 +  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
  1.8657 +  u8 p5;              /* Fifth parameter is an unsigned character */
  1.8658 +  int p1;             /* First operand */
  1.8659 +  int p2;             /* Second parameter (often the jump destination) */
  1.8660 +  int p3;             /* The third parameter */
  1.8661 +  union {             /* fourth parameter */
  1.8662 +    int i;                 /* Integer value if p4type==P4_INT32 */
  1.8663 +    void *p;               /* Generic pointer */
  1.8664 +    char *z;               /* Pointer to data for string (char array) types */
  1.8665 +    i64 *pI64;             /* Used when p4type is P4_INT64 */
  1.8666 +    double *pReal;         /* Used when p4type is P4_REAL */
  1.8667 +    FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
  1.8668 +    VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
  1.8669 +    CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
  1.8670 +    Mem *pMem;             /* Used when p4type is P4_MEM */
  1.8671 +    VTable *pVtab;         /* Used when p4type is P4_VTAB */
  1.8672 +    KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
  1.8673 +    int *ai;               /* Used when p4type is P4_INTARRAY */
  1.8674 +    SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
  1.8675 +    int (*xAdvance)(BtCursor *, int *);
  1.8676 +  } p4;
  1.8677 +#ifdef SQLITE_DEBUG
  1.8678 +  char *zComment;          /* Comment to improve readability */
  1.8679 +#endif
  1.8680 +#ifdef VDBE_PROFILE
  1.8681 +  int cnt;                 /* Number of times this instruction was executed */
  1.8682 +  u64 cycles;              /* Total time spent executing this instruction */
  1.8683 +#endif
  1.8684 +};
  1.8685 +typedef struct VdbeOp VdbeOp;
  1.8686 +
  1.8687 +
  1.8688 +/*
  1.8689 +** A sub-routine used to implement a trigger program.
  1.8690 +*/
  1.8691 +struct SubProgram {
  1.8692 +  VdbeOp *aOp;                  /* Array of opcodes for sub-program */
  1.8693 +  int nOp;                      /* Elements in aOp[] */
  1.8694 +  int nMem;                     /* Number of memory cells required */
  1.8695 +  int nCsr;                     /* Number of cursors required */
  1.8696 +  int nOnce;                    /* Number of OP_Once instructions */
  1.8697 +  void *token;                  /* id that may be used to recursive triggers */
  1.8698 +  SubProgram *pNext;            /* Next sub-program already visited */
  1.8699 +};
  1.8700 +
  1.8701 +/*
  1.8702 +** A smaller version of VdbeOp used for the VdbeAddOpList() function because
  1.8703 +** it takes up less space.
  1.8704 +*/
  1.8705 +struct VdbeOpList {
  1.8706 +  u8 opcode;          /* What operation to perform */
  1.8707 +  signed char p1;     /* First operand */
  1.8708 +  signed char p2;     /* Second parameter (often the jump destination) */
  1.8709 +  signed char p3;     /* Third parameter */
  1.8710 +};
  1.8711 +typedef struct VdbeOpList VdbeOpList;
  1.8712 +
  1.8713 +/*
  1.8714 +** Allowed values of VdbeOp.p4type
  1.8715 +*/
  1.8716 +#define P4_NOTUSED    0   /* The P4 parameter is not used */
  1.8717 +#define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
  1.8718 +#define P4_STATIC   (-2)  /* Pointer to a static string */
  1.8719 +#define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
  1.8720 +#define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
  1.8721 +#define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
  1.8722 +#define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
  1.8723 +#define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
  1.8724 +#define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
  1.8725 +#define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
  1.8726 +#define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
  1.8727 +#define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
  1.8728 +#define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
  1.8729 +#define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
  1.8730 +#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  1.8731 +#define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
  1.8732 +#define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
  1.8733 +
  1.8734 +/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
  1.8735 +** is made.  That copy is freed when the Vdbe is finalized.  But if the
  1.8736 +** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
  1.8737 +** gets freed when the Vdbe is finalized so it still should be obtained
  1.8738 +** from a single sqliteMalloc().  But no copy is made and the calling
  1.8739 +** function should *not* try to free the KeyInfo.
  1.8740 +*/
  1.8741 +#define P4_KEYINFO_HANDOFF (-16)
  1.8742 +#define P4_KEYINFO_STATIC  (-17)
  1.8743 +
  1.8744 +/*
  1.8745 +** The Vdbe.aColName array contains 5n Mem structures, where n is the 
  1.8746 +** number of columns of data returned by the statement.
  1.8747 +*/
  1.8748 +#define COLNAME_NAME     0
  1.8749 +#define COLNAME_DECLTYPE 1
  1.8750 +#define COLNAME_DATABASE 2
  1.8751 +#define COLNAME_TABLE    3
  1.8752 +#define COLNAME_COLUMN   4
  1.8753 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1.8754 +# define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
  1.8755 +#else
  1.8756 +# ifdef SQLITE_OMIT_DECLTYPE
  1.8757 +#   define COLNAME_N      1      /* Store only the name */
  1.8758 +# else
  1.8759 +#   define COLNAME_N      2      /* Store the name and decltype */
  1.8760 +# endif
  1.8761 +#endif
  1.8762 +
  1.8763 +/*
  1.8764 +** The following macro converts a relative address in the p2 field
  1.8765 +** of a VdbeOp structure into a negative number so that 
  1.8766 +** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
  1.8767 +** the macro again restores the address.
  1.8768 +*/
  1.8769 +#define ADDR(X)  (-1-(X))
  1.8770 +
  1.8771 +/*
  1.8772 +** The makefile scans the vdbe.c source file and creates the "opcodes.h"
  1.8773 +** header file that defines a number for each opcode used by the VDBE.
  1.8774 +*/
  1.8775 +/************** Include opcodes.h in the middle of vdbe.h ********************/
  1.8776 +/************** Begin file opcodes.h *****************************************/
  1.8777 +/* Automatically generated.  Do not edit */
  1.8778 +/* See the mkopcodeh.awk script for details */
  1.8779 +#define OP_Goto                                 1
  1.8780 +#define OP_Gosub                                2
  1.8781 +#define OP_Return                               3
  1.8782 +#define OP_Yield                                4
  1.8783 +#define OP_HaltIfNull                           5
  1.8784 +#define OP_Halt                                 6
  1.8785 +#define OP_Integer                              7
  1.8786 +#define OP_Int64                                8
  1.8787 +#define OP_Real                               130   /* same as TK_FLOAT    */
  1.8788 +#define OP_String8                             94   /* same as TK_STRING   */
  1.8789 +#define OP_String                               9
  1.8790 +#define OP_Null                                10
  1.8791 +#define OP_Blob                                11
  1.8792 +#define OP_Variable                            12
  1.8793 +#define OP_Move                                13
  1.8794 +#define OP_Copy                                14
  1.8795 +#define OP_SCopy                               15
  1.8796 +#define OP_ResultRow                           16
  1.8797 +#define OP_Concat                              91   /* same as TK_CONCAT   */
  1.8798 +#define OP_Add                                 86   /* same as TK_PLUS     */
  1.8799 +#define OP_Subtract                            87   /* same as TK_MINUS    */
  1.8800 +#define OP_Multiply                            88   /* same as TK_STAR     */
  1.8801 +#define OP_Divide                              89   /* same as TK_SLASH    */
  1.8802 +#define OP_Remainder                           90   /* same as TK_REM      */
  1.8803 +#define OP_CollSeq                             17
  1.8804 +#define OP_Function                            18
  1.8805 +#define OP_BitAnd                              82   /* same as TK_BITAND   */
  1.8806 +#define OP_BitOr                               83   /* same as TK_BITOR    */
  1.8807 +#define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
  1.8808 +#define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
  1.8809 +#define OP_AddImm                              20
  1.8810 +#define OP_MustBeInt                           21
  1.8811 +#define OP_RealAffinity                        22
  1.8812 +#define OP_ToText                             141   /* same as TK_TO_TEXT  */
  1.8813 +#define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
  1.8814 +#define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
  1.8815 +#define OP_ToInt                              144   /* same as TK_TO_INT   */
  1.8816 +#define OP_ToReal                             145   /* same as TK_TO_REAL  */
  1.8817 +#define OP_Eq                                  76   /* same as TK_EQ       */
  1.8818 +#define OP_Ne                                  75   /* same as TK_NE       */
  1.8819 +#define OP_Lt                                  79   /* same as TK_LT       */
  1.8820 +#define OP_Le                                  78   /* same as TK_LE       */
  1.8821 +#define OP_Gt                                  77   /* same as TK_GT       */
  1.8822 +#define OP_Ge                                  80   /* same as TK_GE       */
  1.8823 +#define OP_Permutation                         23
  1.8824 +#define OP_Compare                             24
  1.8825 +#define OP_Jump                                25
  1.8826 +#define OP_And                                 69   /* same as TK_AND      */
  1.8827 +#define OP_Or                                  68   /* same as TK_OR       */
  1.8828 +#define OP_Not                                 19   /* same as TK_NOT      */
  1.8829 +#define OP_BitNot                              93   /* same as TK_BITNOT   */
  1.8830 +#define OP_Once                                26
  1.8831 +#define OP_If                                  27
  1.8832 +#define OP_IfNot                               28
  1.8833 +#define OP_IsNull                              73   /* same as TK_ISNULL   */
  1.8834 +#define OP_NotNull                             74   /* same as TK_NOTNULL  */
  1.8835 +#define OP_Column                              29
  1.8836 +#define OP_Affinity                            30
  1.8837 +#define OP_MakeRecord                          31
  1.8838 +#define OP_Count                               32
  1.8839 +#define OP_Savepoint                           33
  1.8840 +#define OP_AutoCommit                          34
  1.8841 +#define OP_Transaction                         35
  1.8842 +#define OP_ReadCookie                          36
  1.8843 +#define OP_SetCookie                           37
  1.8844 +#define OP_VerifyCookie                        38
  1.8845 +#define OP_OpenRead                            39
  1.8846 +#define OP_OpenWrite                           40
  1.8847 +#define OP_OpenAutoindex                       41
  1.8848 +#define OP_OpenEphemeral                       42
  1.8849 +#define OP_SorterOpen                          43
  1.8850 +#define OP_OpenPseudo                          44
  1.8851 +#define OP_Close                               45
  1.8852 +#define OP_SeekLt                              46
  1.8853 +#define OP_SeekLe                              47
  1.8854 +#define OP_SeekGe                              48
  1.8855 +#define OP_SeekGt                              49
  1.8856 +#define OP_Seek                                50
  1.8857 +#define OP_NotFound                            51
  1.8858 +#define OP_Found                               52
  1.8859 +#define OP_IsUnique                            53
  1.8860 +#define OP_NotExists                           54
  1.8861 +#define OP_Sequence                            55
  1.8862 +#define OP_NewRowid                            56
  1.8863 +#define OP_Insert                              57
  1.8864 +#define OP_InsertInt                           58
  1.8865 +#define OP_Delete                              59
  1.8866 +#define OP_ResetCount                          60
  1.8867 +#define OP_SorterCompare                       61
  1.8868 +#define OP_SorterData                          62
  1.8869 +#define OP_RowKey                              63
  1.8870 +#define OP_RowData                             64
  1.8871 +#define OP_Rowid                               65
  1.8872 +#define OP_NullRow                             66
  1.8873 +#define OP_Last                                67
  1.8874 +#define OP_SorterSort                          70
  1.8875 +#define OP_Sort                                71
  1.8876 +#define OP_Rewind                              72
  1.8877 +#define OP_SorterNext                          81
  1.8878 +#define OP_Prev                                92
  1.8879 +#define OP_Next                                95
  1.8880 +#define OP_SorterInsert                        96
  1.8881 +#define OP_IdxInsert                           97
  1.8882 +#define OP_IdxDelete                           98
  1.8883 +#define OP_IdxRowid                            99
  1.8884 +#define OP_IdxLT                              100
  1.8885 +#define OP_IdxGE                              101
  1.8886 +#define OP_Destroy                            102
  1.8887 +#define OP_Clear                              103
  1.8888 +#define OP_CreateIndex                        104
  1.8889 +#define OP_CreateTable                        105
  1.8890 +#define OP_ParseSchema                        106
  1.8891 +#define OP_LoadAnalysis                       107
  1.8892 +#define OP_DropTable                          108
  1.8893 +#define OP_DropIndex                          109
  1.8894 +#define OP_DropTrigger                        110
  1.8895 +#define OP_IntegrityCk                        111
  1.8896 +#define OP_RowSetAdd                          112
  1.8897 +#define OP_RowSetRead                         113
  1.8898 +#define OP_RowSetTest                         114
  1.8899 +#define OP_Program                            115
  1.8900 +#define OP_Param                              116
  1.8901 +#define OP_FkCounter                          117
  1.8902 +#define OP_FkIfZero                           118
  1.8903 +#define OP_MemMax                             119
  1.8904 +#define OP_IfPos                              120
  1.8905 +#define OP_IfNeg                              121
  1.8906 +#define OP_IfZero                             122
  1.8907 +#define OP_AggStep                            123
  1.8908 +#define OP_AggFinal                           124
  1.8909 +#define OP_Checkpoint                         125
  1.8910 +#define OP_JournalMode                        126
  1.8911 +#define OP_Vacuum                             127
  1.8912 +#define OP_IncrVacuum                         128
  1.8913 +#define OP_Expire                             129
  1.8914 +#define OP_TableLock                          131
  1.8915 +#define OP_VBegin                             132
  1.8916 +#define OP_VCreate                            133
  1.8917 +#define OP_VDestroy                           134
  1.8918 +#define OP_VOpen                              135
  1.8919 +#define OP_VFilter                            136
  1.8920 +#define OP_VColumn                            137
  1.8921 +#define OP_VNext                              138
  1.8922 +#define OP_VRename                            139
  1.8923 +#define OP_VUpdate                            140
  1.8924 +#define OP_Pagecount                          146
  1.8925 +#define OP_MaxPgcnt                           147
  1.8926 +#define OP_Trace                              148
  1.8927 +#define OP_Noop                               149
  1.8928 +#define OP_Explain                            150
  1.8929 +
  1.8930 +
  1.8931 +/* Properties such as "out2" or "jump" that are specified in
  1.8932 +** comments following the "case" for each opcode in the vdbe.c
  1.8933 +** are encoded into bitvectors as follows:
  1.8934 +*/
  1.8935 +#define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
  1.8936 +#define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
  1.8937 +#define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
  1.8938 +#define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
  1.8939 +#define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
  1.8940 +#define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
  1.8941 +#define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
  1.8942 +#define OPFLG_INITIALIZER {\
  1.8943 +/*   0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
  1.8944 +/*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24,\
  1.8945 +/*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
  1.8946 +/*  24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
  1.8947 +/*  32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
  1.8948 +/*  40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
  1.8949 +/*  48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
  1.8950 +/*  56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.8951 +/*  64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
  1.8952 +/*  72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
  1.8953 +/*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
  1.8954 +/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
  1.8955 +/*  96 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
  1.8956 +/* 104 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.8957 +/* 112 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
  1.8958 +/* 120 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00,\
  1.8959 +/* 128 */ 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
  1.8960 +/* 136 */ 0x01, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x04,\
  1.8961 +/* 144 */ 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00,}
  1.8962 +
  1.8963 +/************** End of opcodes.h *********************************************/
  1.8964 +/************** Continuing where we left off in vdbe.h ***********************/
  1.8965 +
  1.8966 +/*
  1.8967 +** Prototypes for the VDBE interface.  See comments on the implementation
  1.8968 +** for a description of what each of these routines does.
  1.8969 +*/
  1.8970 +SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
  1.8971 +SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  1.8972 +SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  1.8973 +SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  1.8974 +SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  1.8975 +SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  1.8976 +SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  1.8977 +SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  1.8978 +SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  1.8979 +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  1.8980 +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  1.8981 +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
  1.8982 +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  1.8983 +SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  1.8984 +SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  1.8985 +SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  1.8986 +SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  1.8987 +SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  1.8988 +SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  1.8989 +SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
  1.8990 +SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  1.8991 +SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
  1.8992 +SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
  1.8993 +SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
  1.8994 +SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
  1.8995 +SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
  1.8996 +#ifdef SQLITE_DEBUG
  1.8997 +SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
  1.8998 +SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
  1.8999 +#endif
  1.9000 +SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
  1.9001 +SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
  1.9002 +SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
  1.9003 +SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
  1.9004 +SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
  1.9005 +SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
  1.9006 +SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
  1.9007 +SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
  1.9008 +SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
  1.9009 +SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
  1.9010 +SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
  1.9011 +SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  1.9012 +#ifndef SQLITE_OMIT_TRACE
  1.9013 +SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  1.9014 +#endif
  1.9015 +
  1.9016 +SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  1.9017 +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
  1.9018 +SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  1.9019 +
  1.9020 +#ifndef SQLITE_OMIT_TRIGGER
  1.9021 +SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  1.9022 +#endif
  1.9023 +
  1.9024 +
  1.9025 +#ifndef NDEBUG
  1.9026 +SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
  1.9027 +# define VdbeComment(X)  sqlite3VdbeComment X
  1.9028 +SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
  1.9029 +# define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
  1.9030 +#else
  1.9031 +# define VdbeComment(X)
  1.9032 +# define VdbeNoopComment(X)
  1.9033 +#endif
  1.9034 +
  1.9035 +#endif
  1.9036 +
  1.9037 +/************** End of vdbe.h ************************************************/
  1.9038 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.9039 +/************** Include pager.h in the middle of sqliteInt.h *****************/
  1.9040 +/************** Begin file pager.h *******************************************/
  1.9041 +/*
  1.9042 +** 2001 September 15
  1.9043 +**
  1.9044 +** The author disclaims copyright to this source code.  In place of
  1.9045 +** a legal notice, here is a blessing:
  1.9046 +**
  1.9047 +**    May you do good and not evil.
  1.9048 +**    May you find forgiveness for yourself and forgive others.
  1.9049 +**    May you share freely, never taking more than you give.
  1.9050 +**
  1.9051 +*************************************************************************
  1.9052 +** This header file defines the interface that the sqlite page cache
  1.9053 +** subsystem.  The page cache subsystem reads and writes a file a page
  1.9054 +** at a time and provides a journal for rollback.
  1.9055 +*/
  1.9056 +
  1.9057 +#ifndef _PAGER_H_
  1.9058 +#define _PAGER_H_
  1.9059 +
  1.9060 +/*
  1.9061 +** Default maximum size for persistent journal files. A negative 
  1.9062 +** value means no limit. This value may be overridden using the 
  1.9063 +** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
  1.9064 +*/
  1.9065 +#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
  1.9066 +  #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
  1.9067 +#endif
  1.9068 +
  1.9069 +/*
  1.9070 +** The type used to represent a page number.  The first page in a file
  1.9071 +** is called page 1.  0 is used to represent "not a page".
  1.9072 +*/
  1.9073 +typedef u32 Pgno;
  1.9074 +
  1.9075 +/*
  1.9076 +** Each open file is managed by a separate instance of the "Pager" structure.
  1.9077 +*/
  1.9078 +typedef struct Pager Pager;
  1.9079 +
  1.9080 +/*
  1.9081 +** Handle type for pages.
  1.9082 +*/
  1.9083 +typedef struct PgHdr DbPage;
  1.9084 +
  1.9085 +/*
  1.9086 +** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
  1.9087 +** reserved for working around a windows/posix incompatibility). It is
  1.9088 +** used in the journal to signify that the remainder of the journal file 
  1.9089 +** is devoted to storing a master journal name - there are no more pages to
  1.9090 +** roll back. See comments for function writeMasterJournal() in pager.c 
  1.9091 +** for details.
  1.9092 +*/
  1.9093 +#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
  1.9094 +
  1.9095 +/*
  1.9096 +** Allowed values for the flags parameter to sqlite3PagerOpen().
  1.9097 +**
  1.9098 +** NOTE: These values must match the corresponding BTREE_ values in btree.h.
  1.9099 +*/
  1.9100 +#define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
  1.9101 +#define PAGER_MEMORY        0x0002    /* In-memory database */
  1.9102 +
  1.9103 +/*
  1.9104 +** Valid values for the second argument to sqlite3PagerLockingMode().
  1.9105 +*/
  1.9106 +#define PAGER_LOCKINGMODE_QUERY      -1
  1.9107 +#define PAGER_LOCKINGMODE_NORMAL      0
  1.9108 +#define PAGER_LOCKINGMODE_EXCLUSIVE   1
  1.9109 +
  1.9110 +/*
  1.9111 +** Numeric constants that encode the journalmode.  
  1.9112 +*/
  1.9113 +#define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
  1.9114 +#define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
  1.9115 +#define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
  1.9116 +#define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
  1.9117 +#define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
  1.9118 +#define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
  1.9119 +#define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
  1.9120 +
  1.9121 +/*
  1.9122 +** The remainder of this file contains the declarations of the functions
  1.9123 +** that make up the Pager sub-system API. See source code comments for 
  1.9124 +** a detailed description of each routine.
  1.9125 +*/
  1.9126 +
  1.9127 +/* Open and close a Pager connection. */ 
  1.9128 +SQLITE_PRIVATE int sqlite3PagerOpen(
  1.9129 +  sqlite3_vfs*,
  1.9130 +  Pager **ppPager,
  1.9131 +  const char*,
  1.9132 +  int,
  1.9133 +  int,
  1.9134 +  int,
  1.9135 +  void(*)(DbPage*)
  1.9136 +);
  1.9137 +SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
  1.9138 +SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
  1.9139 +
  1.9140 +/* Functions used to configure a Pager object. */
  1.9141 +SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
  1.9142 +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
  1.9143 +SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
  1.9144 +SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
  1.9145 +SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
  1.9146 +SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
  1.9147 +SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
  1.9148 +SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
  1.9149 +SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
  1.9150 +SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
  1.9151 +SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
  1.9152 +SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
  1.9153 +
  1.9154 +/* Functions used to obtain and release page references. */ 
  1.9155 +SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
  1.9156 +#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
  1.9157 +SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
  1.9158 +SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
  1.9159 +SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
  1.9160 +
  1.9161 +/* Operations on page references. */
  1.9162 +SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
  1.9163 +SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
  1.9164 +SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
  1.9165 +SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
  1.9166 +SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); 
  1.9167 +SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); 
  1.9168 +
  1.9169 +/* Functions used to manage pager transactions and savepoints. */
  1.9170 +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
  1.9171 +SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
  1.9172 +SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
  1.9173 +SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
  1.9174 +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
  1.9175 +SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  1.9176 +SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  1.9177 +SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  1.9178 +SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
  1.9179 +SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
  1.9180 +
  1.9181 +#ifndef SQLITE_OMIT_WAL
  1.9182 +SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
  1.9183 +SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
  1.9184 +SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
  1.9185 +SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
  1.9186 +SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
  1.9187 +#endif
  1.9188 +
  1.9189 +#ifdef SQLITE_ENABLE_ZIPVFS
  1.9190 +SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
  1.9191 +#endif
  1.9192 +
  1.9193 +/* Functions used to query pager state and configuration. */
  1.9194 +SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
  1.9195 +SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
  1.9196 +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
  1.9197 +SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
  1.9198 +SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
  1.9199 +SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  1.9200 +SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  1.9201 +SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  1.9202 +SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  1.9203 +SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  1.9204 +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
  1.9205 +SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
  1.9206 +SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
  1.9207 +
  1.9208 +/* Functions used to truncate the database file. */
  1.9209 +SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
  1.9210 +
  1.9211 +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
  1.9212 +SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
  1.9213 +#endif
  1.9214 +
  1.9215 +/* Functions to support testing and debugging. */
  1.9216 +#if !defined(NDEBUG) || defined(SQLITE_TEST)
  1.9217 +SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
  1.9218 +SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
  1.9219 +#endif
  1.9220 +#ifdef SQLITE_TEST
  1.9221 +SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
  1.9222 +SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
  1.9223 +  void disable_simulated_io_errors(void);
  1.9224 +  void enable_simulated_io_errors(void);
  1.9225 +#else
  1.9226 +# define disable_simulated_io_errors()
  1.9227 +# define enable_simulated_io_errors()
  1.9228 +#endif
  1.9229 +
  1.9230 +#endif /* _PAGER_H_ */
  1.9231 +
  1.9232 +/************** End of pager.h ***********************************************/
  1.9233 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.9234 +/************** Include pcache.h in the middle of sqliteInt.h ****************/
  1.9235 +/************** Begin file pcache.h ******************************************/
  1.9236 +/*
  1.9237 +** 2008 August 05
  1.9238 +**
  1.9239 +** The author disclaims copyright to this source code.  In place of
  1.9240 +** a legal notice, here is a blessing:
  1.9241 +**
  1.9242 +**    May you do good and not evil.
  1.9243 +**    May you find forgiveness for yourself and forgive others.
  1.9244 +**    May you share freely, never taking more than you give.
  1.9245 +**
  1.9246 +*************************************************************************
  1.9247 +** This header file defines the interface that the sqlite page cache
  1.9248 +** subsystem. 
  1.9249 +*/
  1.9250 +
  1.9251 +#ifndef _PCACHE_H_
  1.9252 +
  1.9253 +typedef struct PgHdr PgHdr;
  1.9254 +typedef struct PCache PCache;
  1.9255 +
  1.9256 +/*
  1.9257 +** Every page in the cache is controlled by an instance of the following
  1.9258 +** structure.
  1.9259 +*/
  1.9260 +struct PgHdr {
  1.9261 +  sqlite3_pcache_page *pPage;    /* Pcache object page handle */
  1.9262 +  void *pData;                   /* Page data */
  1.9263 +  void *pExtra;                  /* Extra content */
  1.9264 +  PgHdr *pDirty;                 /* Transient list of dirty pages */
  1.9265 +  Pager *pPager;                 /* The pager this page is part of */
  1.9266 +  Pgno pgno;                     /* Page number for this page */
  1.9267 +#ifdef SQLITE_CHECK_PAGES
  1.9268 +  u32 pageHash;                  /* Hash of page content */
  1.9269 +#endif
  1.9270 +  u16 flags;                     /* PGHDR flags defined below */
  1.9271 +
  1.9272 +  /**********************************************************************
  1.9273 +  ** Elements above are public.  All that follows is private to pcache.c
  1.9274 +  ** and should not be accessed by other modules.
  1.9275 +  */
  1.9276 +  i16 nRef;                      /* Number of users of this page */
  1.9277 +  PCache *pCache;                /* Cache that owns this page */
  1.9278 +
  1.9279 +  PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
  1.9280 +  PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
  1.9281 +};
  1.9282 +
  1.9283 +/* Bit values for PgHdr.flags */
  1.9284 +#define PGHDR_DIRTY             0x002  /* Page has changed */
  1.9285 +#define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
  1.9286 +                                       ** writing this page to the database */
  1.9287 +#define PGHDR_NEED_READ         0x008  /* Content is unread */
  1.9288 +#define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
  1.9289 +#define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
  1.9290 +
  1.9291 +/* Initialize and shutdown the page cache subsystem */
  1.9292 +SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
  1.9293 +SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
  1.9294 +
  1.9295 +/* Page cache buffer management:
  1.9296 +** These routines implement SQLITE_CONFIG_PAGECACHE.
  1.9297 +*/
  1.9298 +SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
  1.9299 +
  1.9300 +/* Create a new pager cache.
  1.9301 +** Under memory stress, invoke xStress to try to make pages clean.
  1.9302 +** Only clean and unpinned pages can be reclaimed.
  1.9303 +*/
  1.9304 +SQLITE_PRIVATE void sqlite3PcacheOpen(
  1.9305 +  int szPage,                    /* Size of every page */
  1.9306 +  int szExtra,                   /* Extra space associated with each page */
  1.9307 +  int bPurgeable,                /* True if pages are on backing store */
  1.9308 +  int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
  1.9309 +  void *pStress,                 /* Argument to xStress */
  1.9310 +  PCache *pToInit                /* Preallocated space for the PCache */
  1.9311 +);
  1.9312 +
  1.9313 +/* Modify the page-size after the cache has been created. */
  1.9314 +SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
  1.9315 +
  1.9316 +/* Return the size in bytes of a PCache object.  Used to preallocate
  1.9317 +** storage space.
  1.9318 +*/
  1.9319 +SQLITE_PRIVATE int sqlite3PcacheSize(void);
  1.9320 +
  1.9321 +/* One release per successful fetch.  Page is pinned until released.
  1.9322 +** Reference counted. 
  1.9323 +*/
  1.9324 +SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
  1.9325 +SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
  1.9326 +
  1.9327 +SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
  1.9328 +SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
  1.9329 +SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
  1.9330 +SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
  1.9331 +
  1.9332 +/* Change a page number.  Used by incr-vacuum. */
  1.9333 +SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
  1.9334 +
  1.9335 +/* Remove all pages with pgno>x.  Reset the cache if x==0 */
  1.9336 +SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
  1.9337 +
  1.9338 +/* Get a list of all dirty pages in the cache, sorted by page number */
  1.9339 +SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
  1.9340 +
  1.9341 +/* Reset and close the cache object */
  1.9342 +SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
  1.9343 +
  1.9344 +/* Clear flags from pages of the page cache */
  1.9345 +SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
  1.9346 +
  1.9347 +/* Discard the contents of the cache */
  1.9348 +SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
  1.9349 +
  1.9350 +/* Return the total number of outstanding page references */
  1.9351 +SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
  1.9352 +
  1.9353 +/* Increment the reference count of an existing page */
  1.9354 +SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
  1.9355 +
  1.9356 +SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
  1.9357 +
  1.9358 +/* Return the total number of pages stored in the cache */
  1.9359 +SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
  1.9360 +
  1.9361 +#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  1.9362 +/* Iterate through all dirty pages currently stored in the cache. This
  1.9363 +** interface is only available if SQLITE_CHECK_PAGES is defined when the 
  1.9364 +** library is built.
  1.9365 +*/
  1.9366 +SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  1.9367 +#endif
  1.9368 +
  1.9369 +/* Set and get the suggested cache-size for the specified pager-cache.
  1.9370 +**
  1.9371 +** If no global maximum is configured, then the system attempts to limit
  1.9372 +** the total number of pages cached by purgeable pager-caches to the sum
  1.9373 +** of the suggested cache-sizes.
  1.9374 +*/
  1.9375 +SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
  1.9376 +#ifdef SQLITE_TEST
  1.9377 +SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
  1.9378 +#endif
  1.9379 +
  1.9380 +/* Free up as much memory as possible from the page cache */
  1.9381 +SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
  1.9382 +
  1.9383 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  1.9384 +/* Try to return memory used by the pcache module to the main memory heap */
  1.9385 +SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
  1.9386 +#endif
  1.9387 +
  1.9388 +#ifdef SQLITE_TEST
  1.9389 +SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
  1.9390 +#endif
  1.9391 +
  1.9392 +SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
  1.9393 +
  1.9394 +#endif /* _PCACHE_H_ */
  1.9395 +
  1.9396 +/************** End of pcache.h **********************************************/
  1.9397 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.9398 +
  1.9399 +/************** Include os.h in the middle of sqliteInt.h ********************/
  1.9400 +/************** Begin file os.h **********************************************/
  1.9401 +/*
  1.9402 +** 2001 September 16
  1.9403 +**
  1.9404 +** The author disclaims copyright to this source code.  In place of
  1.9405 +** a legal notice, here is a blessing:
  1.9406 +**
  1.9407 +**    May you do good and not evil.
  1.9408 +**    May you find forgiveness for yourself and forgive others.
  1.9409 +**    May you share freely, never taking more than you give.
  1.9410 +**
  1.9411 +******************************************************************************
  1.9412 +**
  1.9413 +** This header file (together with is companion C source-code file
  1.9414 +** "os.c") attempt to abstract the underlying operating system so that
  1.9415 +** the SQLite library will work on both POSIX and windows systems.
  1.9416 +**
  1.9417 +** This header file is #include-ed by sqliteInt.h and thus ends up
  1.9418 +** being included by every source file.
  1.9419 +*/
  1.9420 +#ifndef _SQLITE_OS_H_
  1.9421 +#define _SQLITE_OS_H_
  1.9422 +
  1.9423 +/*
  1.9424 +** Figure out if we are dealing with Unix, Windows, or some other
  1.9425 +** operating system.  After the following block of preprocess macros,
  1.9426 +** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
  1.9427 +** will defined to either 1 or 0.  One of the four will be 1.  The other 
  1.9428 +** three will be 0.
  1.9429 +*/
  1.9430 +#if defined(SQLITE_OS_OTHER)
  1.9431 +# if SQLITE_OS_OTHER==1
  1.9432 +#   undef SQLITE_OS_UNIX
  1.9433 +#   define SQLITE_OS_UNIX 0
  1.9434 +#   undef SQLITE_OS_WIN
  1.9435 +#   define SQLITE_OS_WIN 0
  1.9436 +# else
  1.9437 +#   undef SQLITE_OS_OTHER
  1.9438 +# endif
  1.9439 +#endif
  1.9440 +#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  1.9441 +# define SQLITE_OS_OTHER 0
  1.9442 +# ifndef SQLITE_OS_WIN
  1.9443 +#   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  1.9444 +#     define SQLITE_OS_WIN 1
  1.9445 +#     define SQLITE_OS_UNIX 0
  1.9446 +#   else
  1.9447 +#     define SQLITE_OS_WIN 0
  1.9448 +#     define SQLITE_OS_UNIX 1
  1.9449 +#  endif
  1.9450 +# else
  1.9451 +#  define SQLITE_OS_UNIX 0
  1.9452 +# endif
  1.9453 +#else
  1.9454 +# ifndef SQLITE_OS_WIN
  1.9455 +#  define SQLITE_OS_WIN 0
  1.9456 +# endif
  1.9457 +#endif
  1.9458 +
  1.9459 +#if SQLITE_OS_WIN
  1.9460 +# include <windows.h>
  1.9461 +#endif
  1.9462 +
  1.9463 +/*
  1.9464 +** Determine if we are dealing with Windows NT.
  1.9465 +**
  1.9466 +** We ought to be able to determine if we are compiling for win98 or winNT
  1.9467 +** using the _WIN32_WINNT macro as follows:
  1.9468 +**
  1.9469 +** #if defined(_WIN32_WINNT)
  1.9470 +** # define SQLITE_OS_WINNT 1
  1.9471 +** #else
  1.9472 +** # define SQLITE_OS_WINNT 0
  1.9473 +** #endif
  1.9474 +**
  1.9475 +** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
  1.9476 +** so the above test does not work.  We'll just assume that everything is
  1.9477 +** winNT unless the programmer explicitly says otherwise by setting
  1.9478 +** SQLITE_OS_WINNT to 0.
  1.9479 +*/
  1.9480 +#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  1.9481 +# define SQLITE_OS_WINNT 1
  1.9482 +#endif
  1.9483 +
  1.9484 +/*
  1.9485 +** Determine if we are dealing with WindowsCE - which has a much
  1.9486 +** reduced API.
  1.9487 +*/
  1.9488 +#if defined(_WIN32_WCE)
  1.9489 +# define SQLITE_OS_WINCE 1
  1.9490 +#else
  1.9491 +# define SQLITE_OS_WINCE 0
  1.9492 +#endif
  1.9493 +
  1.9494 +/*
  1.9495 +** Determine if we are dealing with WinRT, which provides only a subset of
  1.9496 +** the full Win32 API.
  1.9497 +*/
  1.9498 +#if !defined(SQLITE_OS_WINRT)
  1.9499 +# define SQLITE_OS_WINRT 0
  1.9500 +#endif
  1.9501 +
  1.9502 +/*
  1.9503 +** When compiled for WinCE or WinRT, there is no concept of the current
  1.9504 +** directory.
  1.9505 + */
  1.9506 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
  1.9507 +# define SQLITE_CURDIR 1
  1.9508 +#endif
  1.9509 +
  1.9510 +/* If the SET_FULLSYNC macro is not defined above, then make it
  1.9511 +** a no-op
  1.9512 +*/
  1.9513 +#ifndef SET_FULLSYNC
  1.9514 +# define SET_FULLSYNC(x,y)
  1.9515 +#endif
  1.9516 +
  1.9517 +/*
  1.9518 +** The default size of a disk sector
  1.9519 +*/
  1.9520 +#ifndef SQLITE_DEFAULT_SECTOR_SIZE
  1.9521 +# define SQLITE_DEFAULT_SECTOR_SIZE 4096
  1.9522 +#endif
  1.9523 +
  1.9524 +/*
  1.9525 +** Temporary files are named starting with this prefix followed by 16 random
  1.9526 +** alphanumeric characters, and no file extension. They are stored in the
  1.9527 +** OS's standard temporary file directory, and are deleted prior to exit.
  1.9528 +** If sqlite is being embedded in another program, you may wish to change the
  1.9529 +** prefix to reflect your program's name, so that if your program exits
  1.9530 +** prematurely, old temporary files can be easily identified. This can be done
  1.9531 +** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
  1.9532 +**
  1.9533 +** 2006-10-31:  The default prefix used to be "sqlite_".  But then
  1.9534 +** Mcafee started using SQLite in their anti-virus product and it
  1.9535 +** started putting files with the "sqlite" name in the c:/temp folder.
  1.9536 +** This annoyed many windows users.  Those users would then do a 
  1.9537 +** Google search for "sqlite", find the telephone numbers of the
  1.9538 +** developers and call to wake them up at night and complain.
  1.9539 +** For this reason, the default name prefix is changed to be "sqlite" 
  1.9540 +** spelled backwards.  So the temp files are still identified, but
  1.9541 +** anybody smart enough to figure out the code is also likely smart
  1.9542 +** enough to know that calling the developer will not help get rid
  1.9543 +** of the file.
  1.9544 +*/
  1.9545 +#ifndef SQLITE_TEMP_FILE_PREFIX
  1.9546 +# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
  1.9547 +#endif
  1.9548 +
  1.9549 +/*
  1.9550 +** The following values may be passed as the second argument to
  1.9551 +** sqlite3OsLock(). The various locks exhibit the following semantics:
  1.9552 +**
  1.9553 +** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
  1.9554 +** RESERVED:  A single process may hold a RESERVED lock on a file at
  1.9555 +**            any time. Other processes may hold and obtain new SHARED locks.
  1.9556 +** PENDING:   A single process may hold a PENDING lock on a file at
  1.9557 +**            any one time. Existing SHARED locks may persist, but no new
  1.9558 +**            SHARED locks may be obtained by other processes.
  1.9559 +** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
  1.9560 +**
  1.9561 +** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
  1.9562 +** process that requests an EXCLUSIVE lock may actually obtain a PENDING
  1.9563 +** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
  1.9564 +** sqlite3OsLock().
  1.9565 +*/
  1.9566 +#define NO_LOCK         0
  1.9567 +#define SHARED_LOCK     1
  1.9568 +#define RESERVED_LOCK   2
  1.9569 +#define PENDING_LOCK    3
  1.9570 +#define EXCLUSIVE_LOCK  4
  1.9571 +
  1.9572 +/*
  1.9573 +** File Locking Notes:  (Mostly about windows but also some info for Unix)
  1.9574 +**
  1.9575 +** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
  1.9576 +** those functions are not available.  So we use only LockFile() and
  1.9577 +** UnlockFile().
  1.9578 +**
  1.9579 +** LockFile() prevents not just writing but also reading by other processes.
  1.9580 +** A SHARED_LOCK is obtained by locking a single randomly-chosen 
  1.9581 +** byte out of a specific range of bytes. The lock byte is obtained at 
  1.9582 +** random so two separate readers can probably access the file at the 
  1.9583 +** same time, unless they are unlucky and choose the same lock byte.
  1.9584 +** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
  1.9585 +** There can only be one writer.  A RESERVED_LOCK is obtained by locking
  1.9586 +** a single byte of the file that is designated as the reserved lock byte.
  1.9587 +** A PENDING_LOCK is obtained by locking a designated byte different from
  1.9588 +** the RESERVED_LOCK byte.
  1.9589 +**
  1.9590 +** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
  1.9591 +** which means we can use reader/writer locks.  When reader/writer locks
  1.9592 +** are used, the lock is placed on the same range of bytes that is used
  1.9593 +** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
  1.9594 +** will support two or more Win95 readers or two or more WinNT readers.
  1.9595 +** But a single Win95 reader will lock out all WinNT readers and a single
  1.9596 +** WinNT reader will lock out all other Win95 readers.
  1.9597 +**
  1.9598 +** The following #defines specify the range of bytes used for locking.
  1.9599 +** SHARED_SIZE is the number of bytes available in the pool from which
  1.9600 +** a random byte is selected for a shared lock.  The pool of bytes for
  1.9601 +** shared locks begins at SHARED_FIRST. 
  1.9602 +**
  1.9603 +** The same locking strategy and
  1.9604 +** byte ranges are used for Unix.  This leaves open the possiblity of having
  1.9605 +** clients on win95, winNT, and unix all talking to the same shared file
  1.9606 +** and all locking correctly.  To do so would require that samba (or whatever
  1.9607 +** tool is being used for file sharing) implements locks correctly between
  1.9608 +** windows and unix.  I'm guessing that isn't likely to happen, but by
  1.9609 +** using the same locking range we are at least open to the possibility.
  1.9610 +**
  1.9611 +** Locking in windows is manditory.  For this reason, we cannot store
  1.9612 +** actual data in the bytes used for locking.  The pager never allocates
  1.9613 +** the pages involved in locking therefore.  SHARED_SIZE is selected so
  1.9614 +** that all locks will fit on a single page even at the minimum page size.
  1.9615 +** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
  1.9616 +** is set high so that we don't have to allocate an unused page except
  1.9617 +** for very large databases.  But one should test the page skipping logic 
  1.9618 +** by setting PENDING_BYTE low and running the entire regression suite.
  1.9619 +**
  1.9620 +** Changing the value of PENDING_BYTE results in a subtly incompatible
  1.9621 +** file format.  Depending on how it is changed, you might not notice
  1.9622 +** the incompatibility right away, even running a full regression test.
  1.9623 +** The default location of PENDING_BYTE is the first byte past the
  1.9624 +** 1GB boundary.
  1.9625 +**
  1.9626 +*/
  1.9627 +#ifdef SQLITE_OMIT_WSD
  1.9628 +# define PENDING_BYTE     (0x40000000)
  1.9629 +#else
  1.9630 +# define PENDING_BYTE      sqlite3PendingByte
  1.9631 +#endif
  1.9632 +#define RESERVED_BYTE     (PENDING_BYTE+1)
  1.9633 +#define SHARED_FIRST      (PENDING_BYTE+2)
  1.9634 +#define SHARED_SIZE       510
  1.9635 +
  1.9636 +/*
  1.9637 +** Wrapper around OS specific sqlite3_os_init() function.
  1.9638 +*/
  1.9639 +SQLITE_PRIVATE int sqlite3OsInit(void);
  1.9640 +
  1.9641 +/* 
  1.9642 +** Functions for accessing sqlite3_file methods 
  1.9643 +*/
  1.9644 +SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
  1.9645 +SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
  1.9646 +SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
  1.9647 +SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
  1.9648 +SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
  1.9649 +SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
  1.9650 +SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
  1.9651 +SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
  1.9652 +SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
  1.9653 +SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
  1.9654 +SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
  1.9655 +#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
  1.9656 +SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
  1.9657 +SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
  1.9658 +SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
  1.9659 +SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
  1.9660 +SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
  1.9661 +SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
  1.9662 +
  1.9663 +
  1.9664 +/* 
  1.9665 +** Functions for accessing sqlite3_vfs methods 
  1.9666 +*/
  1.9667 +SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
  1.9668 +SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
  1.9669 +SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
  1.9670 +SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
  1.9671 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
  1.9672 +SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
  1.9673 +SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
  1.9674 +SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
  1.9675 +SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
  1.9676 +#endif /* SQLITE_OMIT_LOAD_EXTENSION */
  1.9677 +SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
  1.9678 +SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
  1.9679 +SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
  1.9680 +
  1.9681 +/*
  1.9682 +** Convenience functions for opening and closing files using 
  1.9683 +** sqlite3_malloc() to obtain space for the file-handle structure.
  1.9684 +*/
  1.9685 +SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
  1.9686 +SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
  1.9687 +
  1.9688 +#endif /* _SQLITE_OS_H_ */
  1.9689 +
  1.9690 +/************** End of os.h **************************************************/
  1.9691 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.9692 +/************** Include mutex.h in the middle of sqliteInt.h *****************/
  1.9693 +/************** Begin file mutex.h *******************************************/
  1.9694 +/*
  1.9695 +** 2007 August 28
  1.9696 +**
  1.9697 +** The author disclaims copyright to this source code.  In place of
  1.9698 +** a legal notice, here is a blessing:
  1.9699 +**
  1.9700 +**    May you do good and not evil.
  1.9701 +**    May you find forgiveness for yourself and forgive others.
  1.9702 +**    May you share freely, never taking more than you give.
  1.9703 +**
  1.9704 +*************************************************************************
  1.9705 +**
  1.9706 +** This file contains the common header for all mutex implementations.
  1.9707 +** The sqliteInt.h header #includes this file so that it is available
  1.9708 +** to all source files.  We break it out in an effort to keep the code
  1.9709 +** better organized.
  1.9710 +**
  1.9711 +** NOTE:  source files should *not* #include this header file directly.
  1.9712 +** Source files should #include the sqliteInt.h file and let that file
  1.9713 +** include this one indirectly.
  1.9714 +*/
  1.9715 +
  1.9716 +
  1.9717 +/*
  1.9718 +** Figure out what version of the code to use.  The choices are
  1.9719 +**
  1.9720 +**   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
  1.9721 +**                             mutexes implemention cannot be overridden
  1.9722 +**                             at start-time.
  1.9723 +**
  1.9724 +**   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
  1.9725 +**                             mutual exclusion is provided.  But this
  1.9726 +**                             implementation can be overridden at
  1.9727 +**                             start-time.
  1.9728 +**
  1.9729 +**   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
  1.9730 +**
  1.9731 +**   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
  1.9732 +*/
  1.9733 +#if !SQLITE_THREADSAFE
  1.9734 +# define SQLITE_MUTEX_OMIT
  1.9735 +#endif
  1.9736 +#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
  1.9737 +#  if SQLITE_OS_UNIX
  1.9738 +#    define SQLITE_MUTEX_PTHREADS
  1.9739 +#  elif SQLITE_OS_WIN
  1.9740 +#    define SQLITE_MUTEX_W32
  1.9741 +#  else
  1.9742 +#    define SQLITE_MUTEX_NOOP
  1.9743 +#  endif
  1.9744 +#endif
  1.9745 +
  1.9746 +#ifdef SQLITE_MUTEX_OMIT
  1.9747 +/*
  1.9748 +** If this is a no-op implementation, implement everything as macros.
  1.9749 +*/
  1.9750 +#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
  1.9751 +#define sqlite3_mutex_free(X)
  1.9752 +#define sqlite3_mutex_enter(X)    
  1.9753 +#define sqlite3_mutex_try(X)      SQLITE_OK
  1.9754 +#define sqlite3_mutex_leave(X)    
  1.9755 +#define sqlite3_mutex_held(X)     ((void)(X),1)
  1.9756 +#define sqlite3_mutex_notheld(X)  ((void)(X),1)
  1.9757 +#define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
  1.9758 +#define sqlite3MutexInit()        SQLITE_OK
  1.9759 +#define sqlite3MutexEnd()
  1.9760 +#define MUTEX_LOGIC(X)
  1.9761 +#else
  1.9762 +#define MUTEX_LOGIC(X)            X
  1.9763 +#endif /* defined(SQLITE_MUTEX_OMIT) */
  1.9764 +
  1.9765 +/************** End of mutex.h ***********************************************/
  1.9766 +/************** Continuing where we left off in sqliteInt.h ******************/
  1.9767 +
  1.9768 +
  1.9769 +/*
  1.9770 +** Each database file to be accessed by the system is an instance
  1.9771 +** of the following structure.  There are normally two of these structures
  1.9772 +** in the sqlite.aDb[] array.  aDb[0] is the main database file and
  1.9773 +** aDb[1] is the database file used to hold temporary tables.  Additional
  1.9774 +** databases may be attached.
  1.9775 +*/
  1.9776 +struct Db {
  1.9777 +  char *zName;         /* Name of this database */
  1.9778 +  Btree *pBt;          /* The B*Tree structure for this database file */
  1.9779 +  u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
  1.9780 +  u8 safety_level;     /* How aggressive at syncing data to disk */
  1.9781 +  Schema *pSchema;     /* Pointer to database schema (possibly shared) */
  1.9782 +};
  1.9783 +
  1.9784 +/*
  1.9785 +** An instance of the following structure stores a database schema.
  1.9786 +**
  1.9787 +** Most Schema objects are associated with a Btree.  The exception is
  1.9788 +** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
  1.9789 +** In shared cache mode, a single Schema object can be shared by multiple
  1.9790 +** Btrees that refer to the same underlying BtShared object.
  1.9791 +** 
  1.9792 +** Schema objects are automatically deallocated when the last Btree that
  1.9793 +** references them is destroyed.   The TEMP Schema is manually freed by
  1.9794 +** sqlite3_close().
  1.9795 +*
  1.9796 +** A thread must be holding a mutex on the corresponding Btree in order
  1.9797 +** to access Schema content.  This implies that the thread must also be
  1.9798 +** holding a mutex on the sqlite3 connection pointer that owns the Btree.
  1.9799 +** For a TEMP Schema, only the connection mutex is required.
  1.9800 +*/
  1.9801 +struct Schema {
  1.9802 +  int schema_cookie;   /* Database schema version number for this file */
  1.9803 +  int iGeneration;     /* Generation counter.  Incremented with each change */
  1.9804 +  Hash tblHash;        /* All tables indexed by name */
  1.9805 +  Hash idxHash;        /* All (named) indices indexed by name */
  1.9806 +  Hash trigHash;       /* All triggers indexed by name */
  1.9807 +  Hash fkeyHash;       /* All foreign keys by referenced table name */
  1.9808 +  Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  1.9809 +  u8 file_format;      /* Schema format version for this file */
  1.9810 +  u8 enc;              /* Text encoding used by this database */
  1.9811 +  u16 flags;           /* Flags associated with this schema */
  1.9812 +  int cache_size;      /* Number of pages to use in the cache */
  1.9813 +};
  1.9814 +
  1.9815 +/*
  1.9816 +** These macros can be used to test, set, or clear bits in the 
  1.9817 +** Db.pSchema->flags field.
  1.9818 +*/
  1.9819 +#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
  1.9820 +#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
  1.9821 +#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
  1.9822 +#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
  1.9823 +
  1.9824 +/*
  1.9825 +** Allowed values for the DB.pSchema->flags field.
  1.9826 +**
  1.9827 +** The DB_SchemaLoaded flag is set after the database schema has been
  1.9828 +** read into internal hash tables.
  1.9829 +**
  1.9830 +** DB_UnresetViews means that one or more views have column names that
  1.9831 +** have been filled out.  If the schema changes, these column names might
  1.9832 +** changes and so the view will need to be reset.
  1.9833 +*/
  1.9834 +#define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
  1.9835 +#define DB_UnresetViews    0x0002  /* Some views have defined column names */
  1.9836 +#define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
  1.9837 +
  1.9838 +/*
  1.9839 +** The number of different kinds of things that can be limited
  1.9840 +** using the sqlite3_limit() interface.
  1.9841 +*/
  1.9842 +#define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
  1.9843 +
  1.9844 +/*
  1.9845 +** Lookaside malloc is a set of fixed-size buffers that can be used
  1.9846 +** to satisfy small transient memory allocation requests for objects
  1.9847 +** associated with a particular database connection.  The use of
  1.9848 +** lookaside malloc provides a significant performance enhancement
  1.9849 +** (approx 10%) by avoiding numerous malloc/free requests while parsing
  1.9850 +** SQL statements.
  1.9851 +**
  1.9852 +** The Lookaside structure holds configuration information about the
  1.9853 +** lookaside malloc subsystem.  Each available memory allocation in
  1.9854 +** the lookaside subsystem is stored on a linked list of LookasideSlot
  1.9855 +** objects.
  1.9856 +**
  1.9857 +** Lookaside allocations are only allowed for objects that are associated
  1.9858 +** with a particular database connection.  Hence, schema information cannot
  1.9859 +** be stored in lookaside because in shared cache mode the schema information
  1.9860 +** is shared by multiple database connections.  Therefore, while parsing
  1.9861 +** schema information, the Lookaside.bEnabled flag is cleared so that
  1.9862 +** lookaside allocations are not used to construct the schema objects.
  1.9863 +*/
  1.9864 +struct Lookaside {
  1.9865 +  u16 sz;                 /* Size of each buffer in bytes */
  1.9866 +  u8 bEnabled;            /* False to disable new lookaside allocations */
  1.9867 +  u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
  1.9868 +  int nOut;               /* Number of buffers currently checked out */
  1.9869 +  int mxOut;              /* Highwater mark for nOut */
  1.9870 +  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
  1.9871 +  LookasideSlot *pFree;   /* List of available buffers */
  1.9872 +  void *pStart;           /* First byte of available memory space */
  1.9873 +  void *pEnd;             /* First byte past end of available space */
  1.9874 +};
  1.9875 +struct LookasideSlot {
  1.9876 +  LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
  1.9877 +};
  1.9878 +
  1.9879 +/*
  1.9880 +** A hash table for function definitions.
  1.9881 +**
  1.9882 +** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  1.9883 +** Collisions are on the FuncDef.pHash chain.
  1.9884 +*/
  1.9885 +struct FuncDefHash {
  1.9886 +  FuncDef *a[23];       /* Hash table for functions */
  1.9887 +};
  1.9888 +
  1.9889 +/*
  1.9890 +** Each database connection is an instance of the following structure.
  1.9891 +*/
  1.9892 +struct sqlite3 {
  1.9893 +  sqlite3_vfs *pVfs;            /* OS Interface */
  1.9894 +  struct Vdbe *pVdbe;           /* List of active virtual machines */
  1.9895 +  CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
  1.9896 +  sqlite3_mutex *mutex;         /* Connection mutex */
  1.9897 +  Db *aDb;                      /* All backends */
  1.9898 +  int nDb;                      /* Number of backends currently in use */
  1.9899 +  int flags;                    /* Miscellaneous flags. See below */
  1.9900 +  i64 lastRowid;                /* ROWID of most recent insert (see above) */
  1.9901 +  unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
  1.9902 +  int errCode;                  /* Most recent error code (SQLITE_*) */
  1.9903 +  int errMask;                  /* & result codes with this before returning */
  1.9904 +  u16 dbOptFlags;               /* Flags to enable/disable optimizations */
  1.9905 +  u8 autoCommit;                /* The auto-commit flag. */
  1.9906 +  u8 temp_store;                /* 1: file 2: memory 0: default */
  1.9907 +  u8 mallocFailed;              /* True if we have seen a malloc failure */
  1.9908 +  u8 dfltLockMode;              /* Default locking-mode for attached dbs */
  1.9909 +  signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
  1.9910 +  u8 suppressErr;               /* Do not issue error messages if true */
  1.9911 +  u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
  1.9912 +  u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
  1.9913 +  int nextPagesize;             /* Pagesize after VACUUM if >0 */
  1.9914 +  u32 magic;                    /* Magic number for detect library misuse */
  1.9915 +  int nChange;                  /* Value returned by sqlite3_changes() */
  1.9916 +  int nTotalChange;             /* Value returned by sqlite3_total_changes() */
  1.9917 +  int aLimit[SQLITE_N_LIMIT];   /* Limits */
  1.9918 +  struct sqlite3InitInfo {      /* Information used during initialization */
  1.9919 +    int newTnum;                /* Rootpage of table being initialized */
  1.9920 +    u8 iDb;                     /* Which db file is being initialized */
  1.9921 +    u8 busy;                    /* TRUE if currently initializing */
  1.9922 +    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
  1.9923 +  } init;
  1.9924 +  int activeVdbeCnt;            /* Number of VDBEs currently executing */
  1.9925 +  int writeVdbeCnt;             /* Number of active VDBEs that are writing */
  1.9926 +  int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
  1.9927 +  int nExtension;               /* Number of loaded extensions */
  1.9928 +  void **aExtension;            /* Array of shared library handles */
  1.9929 +  void (*xTrace)(void*,const char*);        /* Trace function */
  1.9930 +  void *pTraceArg;                          /* Argument to the trace function */
  1.9931 +  void (*xProfile)(void*,const char*,u64);  /* Profiling function */
  1.9932 +  void *pProfileArg;                        /* Argument to profile function */
  1.9933 +  void *pCommitArg;                 /* Argument to xCommitCallback() */   
  1.9934 +  int (*xCommitCallback)(void*);    /* Invoked at every commit. */
  1.9935 +  void *pRollbackArg;               /* Argument to xRollbackCallback() */   
  1.9936 +  void (*xRollbackCallback)(void*); /* Invoked at every commit. */
  1.9937 +  void *pUpdateArg;
  1.9938 +  void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
  1.9939 +#ifndef SQLITE_OMIT_WAL
  1.9940 +  int (*xWalCallback)(void *, sqlite3 *, const char *, int);
  1.9941 +  void *pWalArg;
  1.9942 +#endif
  1.9943 +  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
  1.9944 +  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
  1.9945 +  void *pCollNeededArg;
  1.9946 +  sqlite3_value *pErr;          /* Most recent error message */
  1.9947 +  char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
  1.9948 +  char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
  1.9949 +  union {
  1.9950 +    volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
  1.9951 +    double notUsed1;            /* Spacer */
  1.9952 +  } u1;
  1.9953 +  Lookaside lookaside;          /* Lookaside malloc configuration */
  1.9954 +#ifndef SQLITE_OMIT_AUTHORIZATION
  1.9955 +  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
  1.9956 +                                /* Access authorization function */
  1.9957 +  void *pAuthArg;               /* 1st argument to the access auth function */
  1.9958 +#endif
  1.9959 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  1.9960 +  int (*xProgress)(void *);     /* The progress callback */
  1.9961 +  void *pProgressArg;           /* Argument to the progress callback */
  1.9962 +  int nProgressOps;             /* Number of opcodes for progress callback */
  1.9963 +#endif
  1.9964 +#ifndef SQLITE_OMIT_VIRTUALTABLE
  1.9965 +  int nVTrans;                  /* Allocated size of aVTrans */
  1.9966 +  Hash aModule;                 /* populated by sqlite3_create_module() */
  1.9967 +  VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
  1.9968 +  VTable **aVTrans;             /* Virtual tables with open transactions */
  1.9969 +  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
  1.9970 +#endif
  1.9971 +  FuncDefHash aFunc;            /* Hash table of connection functions */
  1.9972 +  Hash aCollSeq;                /* All collating sequences */
  1.9973 +  BusyHandler busyHandler;      /* Busy callback */
  1.9974 +  Db aDbStatic[2];              /* Static space for the 2 default backends */
  1.9975 +  Savepoint *pSavepoint;        /* List of active savepoints */
  1.9976 +  int busyTimeout;              /* Busy handler timeout, in msec */
  1.9977 +  int nSavepoint;               /* Number of non-transaction savepoints */
  1.9978 +  int nStatement;               /* Number of nested statement-transactions  */
  1.9979 +  i64 nDeferredCons;            /* Net deferred constraints this transaction. */
  1.9980 +  int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
  1.9981 +
  1.9982 +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  1.9983 +  /* The following variables are all protected by the STATIC_MASTER 
  1.9984 +  ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
  1.9985 +  **
  1.9986 +  ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  1.9987 +  ** unlock so that it can proceed.
  1.9988 +  **
  1.9989 +  ** When X.pBlockingConnection==Y, that means that something that X tried
  1.9990 +  ** tried to do recently failed with an SQLITE_LOCKED error due to locks
  1.9991 +  ** held by Y.
  1.9992 +  */
  1.9993 +  sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
  1.9994 +  sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
  1.9995 +  void *pUnlockArg;                     /* Argument to xUnlockNotify */
  1.9996 +  void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
  1.9997 +  sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
  1.9998 +#endif
  1.9999 +};
 1.10000 +
 1.10001 +/*
 1.10002 +** A macro to discover the encoding of a database.
 1.10003 +*/
 1.10004 +#define ENC(db) ((db)->aDb[0].pSchema->enc)
 1.10005 +
 1.10006 +/*
 1.10007 +** Possible values for the sqlite3.flags.
 1.10008 +*/
 1.10009 +#define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
 1.10010 +#define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
 1.10011 +#define SQLITE_FullColNames   0x00000004  /* Show full column names on SELECT */
 1.10012 +#define SQLITE_ShortColNames  0x00000008  /* Show short columns names */
 1.10013 +#define SQLITE_CountRows      0x00000010  /* Count rows changed by INSERT, */
 1.10014 +                                          /*   DELETE, or UPDATE and return */
 1.10015 +                                          /*   the count using a callback. */
 1.10016 +#define SQLITE_NullCallback   0x00000020  /* Invoke the callback once if the */
 1.10017 +                                          /*   result set is empty */
 1.10018 +#define SQLITE_SqlTrace       0x00000040  /* Debug print SQL as it executes */
 1.10019 +#define SQLITE_VdbeListing    0x00000080  /* Debug listings of VDBE programs */
 1.10020 +#define SQLITE_WriteSchema    0x00000100  /* OK to update SQLITE_MASTER */
 1.10021 +                         /*   0x00000200  Unused */
 1.10022 +#define SQLITE_IgnoreChecks   0x00000400  /* Do not enforce check constraints */
 1.10023 +#define SQLITE_ReadUncommitted 0x0000800  /* For shared-cache mode */
 1.10024 +#define SQLITE_LegacyFileFmt  0x00001000  /* Create new databases in format 1 */
 1.10025 +#define SQLITE_FullFSync      0x00002000  /* Use full fsync on the backend */
 1.10026 +#define SQLITE_CkptFullFSync  0x00004000  /* Use full fsync for checkpoint */
 1.10027 +#define SQLITE_RecoveryMode   0x00008000  /* Ignore schema errors */
 1.10028 +#define SQLITE_ReverseOrder   0x00010000  /* Reverse unordered SELECTs */
 1.10029 +#define SQLITE_RecTriggers    0x00020000  /* Enable recursive triggers */
 1.10030 +#define SQLITE_ForeignKeys    0x00040000  /* Enforce foreign key constraints  */
 1.10031 +#define SQLITE_AutoIndex      0x00080000  /* Enable automatic indexes */
 1.10032 +#define SQLITE_PreferBuiltin  0x00100000  /* Preference to built-in funcs */
 1.10033 +#define SQLITE_LoadExtension  0x00200000  /* Enable load_extension */
 1.10034 +#define SQLITE_EnableTrigger  0x00400000  /* True to enable triggers */
 1.10035 +
 1.10036 +/*
 1.10037 +** Bits of the sqlite3.dbOptFlags field that are used by the
 1.10038 +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 1.10039 +** selectively disable various optimizations.
 1.10040 +*/
 1.10041 +#define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 1.10042 +#define SQLITE_ColumnCache    0x0002   /* Column cache */
 1.10043 +#define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 1.10044 +#define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 1.10045 +#define SQLITE_IdxRealAsInt   0x0010   /* Store REAL as INT in indices */
 1.10046 +#define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
 1.10047 +#define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
 1.10048 +#define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
 1.10049 +#define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
 1.10050 +#define SQLITE_AllOpts        0xffff   /* All optimizations */
 1.10051 +
 1.10052 +/*
 1.10053 +** Macros for testing whether or not optimizations are enabled or disabled.
 1.10054 +*/
 1.10055 +#ifndef SQLITE_OMIT_BUILTIN_TEST
 1.10056 +#define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 1.10057 +#define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
 1.10058 +#else
 1.10059 +#define OptimizationDisabled(db, mask)  0
 1.10060 +#define OptimizationEnabled(db, mask)   1
 1.10061 +#endif
 1.10062 +
 1.10063 +/*
 1.10064 +** Possible values for the sqlite.magic field.
 1.10065 +** The numbers are obtained at random and have no special meaning, other
 1.10066 +** than being distinct from one another.
 1.10067 +*/
 1.10068 +#define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
 1.10069 +#define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
 1.10070 +#define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
 1.10071 +#define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
 1.10072 +#define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
 1.10073 +#define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
 1.10074 +
 1.10075 +/*
 1.10076 +** Each SQL function is defined by an instance of the following
 1.10077 +** structure.  A pointer to this structure is stored in the sqlite.aFunc
 1.10078 +** hash table.  When multiple functions have the same name, the hash table
 1.10079 +** points to a linked list of these structures.
 1.10080 +*/
 1.10081 +struct FuncDef {
 1.10082 +  i16 nArg;            /* Number of arguments.  -1 means unlimited */
 1.10083 +  u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
 1.10084 +  u8 flags;            /* Some combination of SQLITE_FUNC_* */
 1.10085 +  void *pUserData;     /* User data parameter */
 1.10086 +  FuncDef *pNext;      /* Next function with same name */
 1.10087 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
 1.10088 +  void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
 1.10089 +  void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
 1.10090 +  char *zName;         /* SQL name of the function. */
 1.10091 +  FuncDef *pHash;      /* Next with a different name but the same hash */
 1.10092 +  FuncDestructor *pDestructor;   /* Reference counted destructor function */
 1.10093 +};
 1.10094 +
 1.10095 +/*
 1.10096 +** This structure encapsulates a user-function destructor callback (as
 1.10097 +** configured using create_function_v2()) and a reference counter. When
 1.10098 +** create_function_v2() is called to create a function with a destructor,
 1.10099 +** a single object of this type is allocated. FuncDestructor.nRef is set to 
 1.10100 +** the number of FuncDef objects created (either 1 or 3, depending on whether
 1.10101 +** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
 1.10102 +** member of each of the new FuncDef objects is set to point to the allocated
 1.10103 +** FuncDestructor.
 1.10104 +**
 1.10105 +** Thereafter, when one of the FuncDef objects is deleted, the reference
 1.10106 +** count on this object is decremented. When it reaches 0, the destructor
 1.10107 +** is invoked and the FuncDestructor structure freed.
 1.10108 +*/
 1.10109 +struct FuncDestructor {
 1.10110 +  int nRef;
 1.10111 +  void (*xDestroy)(void *);
 1.10112 +  void *pUserData;
 1.10113 +};
 1.10114 +
 1.10115 +/*
 1.10116 +** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
 1.10117 +** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
 1.10118 +** are assert() statements in the code to verify this.
 1.10119 +*/
 1.10120 +#define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
 1.10121 +#define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
 1.10122 +#define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
 1.10123 +#define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
 1.10124 +#define SQLITE_FUNC_COUNT    0x10 /* Built-in count(*) aggregate */
 1.10125 +#define SQLITE_FUNC_COALESCE 0x20 /* Built-in coalesce() or ifnull() function */
 1.10126 +#define SQLITE_FUNC_LENGTH   0x40 /* Built-in length() function */
 1.10127 +#define SQLITE_FUNC_TYPEOF   0x80 /* Built-in typeof() function */
 1.10128 +
 1.10129 +/*
 1.10130 +** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 1.10131 +** used to create the initializers for the FuncDef structures.
 1.10132 +**
 1.10133 +**   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 1.10134 +**     Used to create a scalar function definition of a function zName 
 1.10135 +**     implemented by C function xFunc that accepts nArg arguments. The
 1.10136 +**     value passed as iArg is cast to a (void*) and made available
 1.10137 +**     as the user-data (sqlite3_user_data()) for the function. If 
 1.10138 +**     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
 1.10139 +**
 1.10140 +**   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
 1.10141 +**     Used to create an aggregate function definition implemented by
 1.10142 +**     the C functions xStep and xFinal. The first four parameters
 1.10143 +**     are interpreted in the same way as the first 4 parameters to
 1.10144 +**     FUNCTION().
 1.10145 +**
 1.10146 +**   LIKEFUNC(zName, nArg, pArg, flags)
 1.10147 +**     Used to create a scalar function definition of a function zName 
 1.10148 +**     that accepts nArg arguments and is implemented by a call to C 
 1.10149 +**     function likeFunc. Argument pArg is cast to a (void *) and made
 1.10150 +**     available as the function user-data (sqlite3_user_data()). The
 1.10151 +**     FuncDef.flags variable is set to the value passed as the flags
 1.10152 +**     parameter.
 1.10153 +*/
 1.10154 +#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
 1.10155 +  {nArg, SQLITE_UTF8, (bNC*SQLITE_FUNC_NEEDCOLL), \
 1.10156 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
 1.10157 +#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
 1.10158 +  {nArg, SQLITE_UTF8, (bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
 1.10159 +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
 1.10160 +#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
 1.10161 +  {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
 1.10162 +   pArg, 0, xFunc, 0, 0, #zName, 0, 0}
 1.10163 +#define LIKEFUNC(zName, nArg, arg, flags) \
 1.10164 +  {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
 1.10165 +#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
 1.10166 +  {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
 1.10167 +   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
 1.10168 +
 1.10169 +/*
 1.10170 +** All current savepoints are stored in a linked list starting at
 1.10171 +** sqlite3.pSavepoint. The first element in the list is the most recently
 1.10172 +** opened savepoint. Savepoints are added to the list by the vdbe
 1.10173 +** OP_Savepoint instruction.
 1.10174 +*/
 1.10175 +struct Savepoint {
 1.10176 +  char *zName;                        /* Savepoint name (nul-terminated) */
 1.10177 +  i64 nDeferredCons;                  /* Number of deferred fk violations */
 1.10178 +  Savepoint *pNext;                   /* Parent savepoint (if any) */
 1.10179 +};
 1.10180 +
 1.10181 +/*
 1.10182 +** The following are used as the second parameter to sqlite3Savepoint(),
 1.10183 +** and as the P1 argument to the OP_Savepoint instruction.
 1.10184 +*/
 1.10185 +#define SAVEPOINT_BEGIN      0
 1.10186 +#define SAVEPOINT_RELEASE    1
 1.10187 +#define SAVEPOINT_ROLLBACK   2
 1.10188 +
 1.10189 +
 1.10190 +/*
 1.10191 +** Each SQLite module (virtual table definition) is defined by an
 1.10192 +** instance of the following structure, stored in the sqlite3.aModule
 1.10193 +** hash table.
 1.10194 +*/
 1.10195 +struct Module {
 1.10196 +  const sqlite3_module *pModule;       /* Callback pointers */
 1.10197 +  const char *zName;                   /* Name passed to create_module() */
 1.10198 +  void *pAux;                          /* pAux passed to create_module() */
 1.10199 +  void (*xDestroy)(void *);            /* Module destructor function */
 1.10200 +};
 1.10201 +
 1.10202 +/*
 1.10203 +** information about each column of an SQL table is held in an instance
 1.10204 +** of this structure.
 1.10205 +*/
 1.10206 +struct Column {
 1.10207 +  char *zName;     /* Name of this column */
 1.10208 +  Expr *pDflt;     /* Default value of this column */
 1.10209 +  char *zDflt;     /* Original text of the default value */
 1.10210 +  char *zType;     /* Data type for this column */
 1.10211 +  char *zColl;     /* Collating sequence.  If NULL, use the default */
 1.10212 +  u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
 1.10213 +  char affinity;   /* One of the SQLITE_AFF_... values */
 1.10214 +  u16 colFlags;    /* Boolean properties.  See COLFLAG_ defines below */
 1.10215 +};
 1.10216 +
 1.10217 +/* Allowed values for Column.colFlags:
 1.10218 +*/
 1.10219 +#define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 1.10220 +#define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 1.10221 +
 1.10222 +/*
 1.10223 +** A "Collating Sequence" is defined by an instance of the following
 1.10224 +** structure. Conceptually, a collating sequence consists of a name and
 1.10225 +** a comparison routine that defines the order of that sequence.
 1.10226 +**
 1.10227 +** If CollSeq.xCmp is NULL, it means that the
 1.10228 +** collating sequence is undefined.  Indices built on an undefined
 1.10229 +** collating sequence may not be read or written.
 1.10230 +*/
 1.10231 +struct CollSeq {
 1.10232 +  char *zName;          /* Name of the collating sequence, UTF-8 encoded */
 1.10233 +  u8 enc;               /* Text encoding handled by xCmp() */
 1.10234 +  void *pUser;          /* First argument to xCmp() */
 1.10235 +  int (*xCmp)(void*,int, const void*, int, const void*);
 1.10236 +  void (*xDel)(void*);  /* Destructor for pUser */
 1.10237 +};
 1.10238 +
 1.10239 +/*
 1.10240 +** A sort order can be either ASC or DESC.
 1.10241 +*/
 1.10242 +#define SQLITE_SO_ASC       0  /* Sort in ascending order */
 1.10243 +#define SQLITE_SO_DESC      1  /* Sort in ascending order */
 1.10244 +
 1.10245 +/*
 1.10246 +** Column affinity types.
 1.10247 +**
 1.10248 +** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
 1.10249 +** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
 1.10250 +** the speed a little by numbering the values consecutively.  
 1.10251 +**
 1.10252 +** But rather than start with 0 or 1, we begin with 'a'.  That way,
 1.10253 +** when multiple affinity types are concatenated into a string and
 1.10254 +** used as the P4 operand, they will be more readable.
 1.10255 +**
 1.10256 +** Note also that the numeric types are grouped together so that testing
 1.10257 +** for a numeric type is a single comparison.
 1.10258 +*/
 1.10259 +#define SQLITE_AFF_TEXT     'a'
 1.10260 +#define SQLITE_AFF_NONE     'b'
 1.10261 +#define SQLITE_AFF_NUMERIC  'c'
 1.10262 +#define SQLITE_AFF_INTEGER  'd'
 1.10263 +#define SQLITE_AFF_REAL     'e'
 1.10264 +
 1.10265 +#define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
 1.10266 +
 1.10267 +/*
 1.10268 +** The SQLITE_AFF_MASK values masks off the significant bits of an
 1.10269 +** affinity value. 
 1.10270 +*/
 1.10271 +#define SQLITE_AFF_MASK     0x67
 1.10272 +
 1.10273 +/*
 1.10274 +** Additional bit values that can be ORed with an affinity without
 1.10275 +** changing the affinity.
 1.10276 +*/
 1.10277 +#define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
 1.10278 +#define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
 1.10279 +#define SQLITE_NULLEQ       0x80  /* NULL=NULL */
 1.10280 +
 1.10281 +/*
 1.10282 +** An object of this type is created for each virtual table present in
 1.10283 +** the database schema. 
 1.10284 +**
 1.10285 +** If the database schema is shared, then there is one instance of this
 1.10286 +** structure for each database connection (sqlite3*) that uses the shared
 1.10287 +** schema. This is because each database connection requires its own unique
 1.10288 +** instance of the sqlite3_vtab* handle used to access the virtual table 
 1.10289 +** implementation. sqlite3_vtab* handles can not be shared between 
 1.10290 +** database connections, even when the rest of the in-memory database 
 1.10291 +** schema is shared, as the implementation often stores the database
 1.10292 +** connection handle passed to it via the xConnect() or xCreate() method
 1.10293 +** during initialization internally. This database connection handle may
 1.10294 +** then be used by the virtual table implementation to access real tables 
 1.10295 +** within the database. So that they appear as part of the callers 
 1.10296 +** transaction, these accesses need to be made via the same database 
 1.10297 +** connection as that used to execute SQL operations on the virtual table.
 1.10298 +**
 1.10299 +** All VTable objects that correspond to a single table in a shared
 1.10300 +** database schema are initially stored in a linked-list pointed to by
 1.10301 +** the Table.pVTable member variable of the corresponding Table object.
 1.10302 +** When an sqlite3_prepare() operation is required to access the virtual
 1.10303 +** table, it searches the list for the VTable that corresponds to the
 1.10304 +** database connection doing the preparing so as to use the correct
 1.10305 +** sqlite3_vtab* handle in the compiled query.
 1.10306 +**
 1.10307 +** When an in-memory Table object is deleted (for example when the
 1.10308 +** schema is being reloaded for some reason), the VTable objects are not 
 1.10309 +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
 1.10310 +** immediately. Instead, they are moved from the Table.pVTable list to
 1.10311 +** another linked list headed by the sqlite3.pDisconnect member of the
 1.10312 +** corresponding sqlite3 structure. They are then deleted/xDisconnected 
 1.10313 +** next time a statement is prepared using said sqlite3*. This is done
 1.10314 +** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
 1.10315 +** Refer to comments above function sqlite3VtabUnlockList() for an
 1.10316 +** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
 1.10317 +** list without holding the corresponding sqlite3.mutex mutex.
 1.10318 +**
 1.10319 +** The memory for objects of this type is always allocated by 
 1.10320 +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
 1.10321 +** the first argument.
 1.10322 +*/
 1.10323 +struct VTable {
 1.10324 +  sqlite3 *db;              /* Database connection associated with this table */
 1.10325 +  Module *pMod;             /* Pointer to module implementation */
 1.10326 +  sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
 1.10327 +  int nRef;                 /* Number of pointers to this structure */
 1.10328 +  u8 bConstraint;           /* True if constraints are supported */
 1.10329 +  int iSavepoint;           /* Depth of the SAVEPOINT stack */
 1.10330 +  VTable *pNext;            /* Next in linked list (see above) */
 1.10331 +};
 1.10332 +
 1.10333 +/*
 1.10334 +** Each SQL table is represented in memory by an instance of the
 1.10335 +** following structure.
 1.10336 +**
 1.10337 +** Table.zName is the name of the table.  The case of the original
 1.10338 +** CREATE TABLE statement is stored, but case is not significant for
 1.10339 +** comparisons.
 1.10340 +**
 1.10341 +** Table.nCol is the number of columns in this table.  Table.aCol is a
 1.10342 +** pointer to an array of Column structures, one for each column.
 1.10343 +**
 1.10344 +** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
 1.10345 +** the column that is that key.   Otherwise Table.iPKey is negative.  Note
 1.10346 +** that the datatype of the PRIMARY KEY must be INTEGER for this field to
 1.10347 +** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
 1.10348 +** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
 1.10349 +** is generated for each row of the table.  TF_HasPrimaryKey is set if
 1.10350 +** the table has any PRIMARY KEY, INTEGER or otherwise.
 1.10351 +**
 1.10352 +** Table.tnum is the page number for the root BTree page of the table in the
 1.10353 +** database file.  If Table.iDb is the index of the database table backend
 1.10354 +** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
 1.10355 +** holds temporary tables and indices.  If TF_Ephemeral is set
 1.10356 +** then the table is stored in a file that is automatically deleted
 1.10357 +** when the VDBE cursor to the table is closed.  In this case Table.tnum 
 1.10358 +** refers VDBE cursor number that holds the table open, not to the root
 1.10359 +** page number.  Transient tables are used to hold the results of a
 1.10360 +** sub-query that appears instead of a real table name in the FROM clause 
 1.10361 +** of a SELECT statement.
 1.10362 +*/
 1.10363 +struct Table {
 1.10364 +  char *zName;         /* Name of the table or view */
 1.10365 +  Column *aCol;        /* Information about each column */
 1.10366 +  Index *pIndex;       /* List of SQL indexes on this table. */
 1.10367 +  Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 1.10368 +  FKey *pFKey;         /* Linked list of all foreign keys in this table */
 1.10369 +  char *zColAff;       /* String defining the affinity of each column */
 1.10370 +#ifndef SQLITE_OMIT_CHECK
 1.10371 +  ExprList *pCheck;    /* All CHECK constraints */
 1.10372 +#endif
 1.10373 +  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
 1.10374 +  int tnum;            /* Root BTree node for this table (see note above) */
 1.10375 +  i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
 1.10376 +  i16 nCol;            /* Number of columns in this table */
 1.10377 +  u16 nRef;            /* Number of pointers to this Table */
 1.10378 +  u8 tabFlags;         /* Mask of TF_* values */
 1.10379 +  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
 1.10380 +#ifndef SQLITE_OMIT_ALTERTABLE
 1.10381 +  int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
 1.10382 +#endif
 1.10383 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.10384 +  int nModuleArg;      /* Number of arguments to the module */
 1.10385 +  char **azModuleArg;  /* Text of all module args. [0] is module name */
 1.10386 +  VTable *pVTable;     /* List of VTable objects. */
 1.10387 +#endif
 1.10388 +  Trigger *pTrigger;   /* List of triggers stored in pSchema */
 1.10389 +  Schema *pSchema;     /* Schema that contains this table */
 1.10390 +  Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
 1.10391 +};
 1.10392 +
 1.10393 +/*
 1.10394 +** Allowed values for Tabe.tabFlags.
 1.10395 +*/
 1.10396 +#define TF_Readonly        0x01    /* Read-only system table */
 1.10397 +#define TF_Ephemeral       0x02    /* An ephemeral table */
 1.10398 +#define TF_HasPrimaryKey   0x04    /* Table has a primary key */
 1.10399 +#define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
 1.10400 +#define TF_Virtual         0x10    /* Is a virtual table */
 1.10401 +
 1.10402 +
 1.10403 +/*
 1.10404 +** Test to see whether or not a table is a virtual table.  This is
 1.10405 +** done as a macro so that it will be optimized out when virtual
 1.10406 +** table support is omitted from the build.
 1.10407 +*/
 1.10408 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.10409 +#  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
 1.10410 +#  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
 1.10411 +#else
 1.10412 +#  define IsVirtual(X)      0
 1.10413 +#  define IsHiddenColumn(X) 0
 1.10414 +#endif
 1.10415 +
 1.10416 +/*
 1.10417 +** Each foreign key constraint is an instance of the following structure.
 1.10418 +**
 1.10419 +** A foreign key is associated with two tables.  The "from" table is
 1.10420 +** the table that contains the REFERENCES clause that creates the foreign
 1.10421 +** key.  The "to" table is the table that is named in the REFERENCES clause.
 1.10422 +** Consider this example:
 1.10423 +**
 1.10424 +**     CREATE TABLE ex1(
 1.10425 +**       a INTEGER PRIMARY KEY,
 1.10426 +**       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
 1.10427 +**     );
 1.10428 +**
 1.10429 +** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
 1.10430 +**
 1.10431 +** Each REFERENCES clause generates an instance of the following structure
 1.10432 +** which is attached to the from-table.  The to-table need not exist when
 1.10433 +** the from-table is created.  The existence of the to-table is not checked.
 1.10434 +*/
 1.10435 +struct FKey {
 1.10436 +  Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
 1.10437 +  FKey *pNextFrom;  /* Next foreign key in pFrom */
 1.10438 +  char *zTo;        /* Name of table that the key points to (aka: Parent) */
 1.10439 +  FKey *pNextTo;    /* Next foreign key on table named zTo */
 1.10440 +  FKey *pPrevTo;    /* Previous foreign key on table named zTo */
 1.10441 +  int nCol;         /* Number of columns in this key */
 1.10442 +  /* EV: R-30323-21917 */
 1.10443 +  u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
 1.10444 +  u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
 1.10445 +  Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
 1.10446 +  struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
 1.10447 +    int iFrom;         /* Index of column in pFrom */
 1.10448 +    char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
 1.10449 +  } aCol[1];        /* One entry for each of nCol column s */
 1.10450 +};
 1.10451 +
 1.10452 +/*
 1.10453 +** SQLite supports many different ways to resolve a constraint
 1.10454 +** error.  ROLLBACK processing means that a constraint violation
 1.10455 +** causes the operation in process to fail and for the current transaction
 1.10456 +** to be rolled back.  ABORT processing means the operation in process
 1.10457 +** fails and any prior changes from that one operation are backed out,
 1.10458 +** but the transaction is not rolled back.  FAIL processing means that
 1.10459 +** the operation in progress stops and returns an error code.  But prior
 1.10460 +** changes due to the same operation are not backed out and no rollback
 1.10461 +** occurs.  IGNORE means that the particular row that caused the constraint
 1.10462 +** error is not inserted or updated.  Processing continues and no error
 1.10463 +** is returned.  REPLACE means that preexisting database rows that caused
 1.10464 +** a UNIQUE constraint violation are removed so that the new insert or
 1.10465 +** update can proceed.  Processing continues and no error is reported.
 1.10466 +**
 1.10467 +** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
 1.10468 +** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
 1.10469 +** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
 1.10470 +** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
 1.10471 +** referenced table row is propagated into the row that holds the
 1.10472 +** foreign key.
 1.10473 +** 
 1.10474 +** The following symbolic values are used to record which type
 1.10475 +** of action to take.
 1.10476 +*/
 1.10477 +#define OE_None     0   /* There is no constraint to check */
 1.10478 +#define OE_Rollback 1   /* Fail the operation and rollback the transaction */
 1.10479 +#define OE_Abort    2   /* Back out changes but do no rollback transaction */
 1.10480 +#define OE_Fail     3   /* Stop the operation but leave all prior changes */
 1.10481 +#define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
 1.10482 +#define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
 1.10483 +
 1.10484 +#define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
 1.10485 +#define OE_SetNull  7   /* Set the foreign key value to NULL */
 1.10486 +#define OE_SetDflt  8   /* Set the foreign key value to its default */
 1.10487 +#define OE_Cascade  9   /* Cascade the changes */
 1.10488 +
 1.10489 +#define OE_Default  99  /* Do whatever the default action is */
 1.10490 +
 1.10491 +
 1.10492 +/*
 1.10493 +** An instance of the following structure is passed as the first
 1.10494 +** argument to sqlite3VdbeKeyCompare and is used to control the 
 1.10495 +** comparison of the two index keys.
 1.10496 +*/
 1.10497 +struct KeyInfo {
 1.10498 +  sqlite3 *db;        /* The database connection */
 1.10499 +  u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
 1.10500 +  u16 nField;         /* Number of entries in aColl[] */
 1.10501 +  u8 *aSortOrder;     /* Sort order for each column.  May be NULL */
 1.10502 +  CollSeq *aColl[1];  /* Collating sequence for each term of the key */
 1.10503 +};
 1.10504 +
 1.10505 +/*
 1.10506 +** An instance of the following structure holds information about a
 1.10507 +** single index record that has already been parsed out into individual
 1.10508 +** values.
 1.10509 +**
 1.10510 +** A record is an object that contains one or more fields of data.
 1.10511 +** Records are used to store the content of a table row and to store
 1.10512 +** the key of an index.  A blob encoding of a record is created by
 1.10513 +** the OP_MakeRecord opcode of the VDBE and is disassembled by the
 1.10514 +** OP_Column opcode.
 1.10515 +**
 1.10516 +** This structure holds a record that has already been disassembled
 1.10517 +** into its constituent fields.
 1.10518 +*/
 1.10519 +struct UnpackedRecord {
 1.10520 +  KeyInfo *pKeyInfo;  /* Collation and sort-order information */
 1.10521 +  u16 nField;         /* Number of entries in apMem[] */
 1.10522 +  u8 flags;           /* Boolean settings.  UNPACKED_... below */
 1.10523 +  i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
 1.10524 +  Mem *aMem;          /* Values */
 1.10525 +};
 1.10526 +
 1.10527 +/*
 1.10528 +** Allowed values of UnpackedRecord.flags
 1.10529 +*/
 1.10530 +#define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
 1.10531 +#define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
 1.10532 +#define UNPACKED_PREFIX_SEARCH 0x04  /* Ignore final (rowid) field */
 1.10533 +
 1.10534 +/*
 1.10535 +** Each SQL index is represented in memory by an
 1.10536 +** instance of the following structure.
 1.10537 +**
 1.10538 +** The columns of the table that are to be indexed are described
 1.10539 +** by the aiColumn[] field of this structure.  For example, suppose
 1.10540 +** we have the following table and index:
 1.10541 +**
 1.10542 +**     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
 1.10543 +**     CREATE INDEX Ex2 ON Ex1(c3,c1);
 1.10544 +**
 1.10545 +** In the Table structure describing Ex1, nCol==3 because there are
 1.10546 +** three columns in the table.  In the Index structure describing
 1.10547 +** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
 1.10548 +** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
 1.10549 +** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
 1.10550 +** The second column to be indexed (c1) has an index of 0 in
 1.10551 +** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
 1.10552 +**
 1.10553 +** The Index.onError field determines whether or not the indexed columns
 1.10554 +** must be unique and what to do if they are not.  When Index.onError=OE_None,
 1.10555 +** it means this is not a unique index.  Otherwise it is a unique index
 1.10556 +** and the value of Index.onError indicate the which conflict resolution 
 1.10557 +** algorithm to employ whenever an attempt is made to insert a non-unique
 1.10558 +** element.
 1.10559 +*/
 1.10560 +struct Index {
 1.10561 +  char *zName;     /* Name of this index */
 1.10562 +  int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
 1.10563 +  tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
 1.10564 +  Table *pTable;   /* The SQL table being indexed */
 1.10565 +  char *zColAff;   /* String defining the affinity of each column */
 1.10566 +  Index *pNext;    /* The next index associated with the same table */
 1.10567 +  Schema *pSchema; /* Schema containing this index */
 1.10568 +  u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
 1.10569 +  char **azColl;   /* Array of collation sequence names for index */
 1.10570 +  int nColumn;     /* Number of columns in the table used by this index */
 1.10571 +  int tnum;        /* Page containing root of this index in database file */
 1.10572 +  u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 1.10573 +  u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
 1.10574 +  u8 bUnordered;   /* Use this index for == or IN queries only */
 1.10575 +#ifdef SQLITE_ENABLE_STAT3
 1.10576 +  int nSample;             /* Number of elements in aSample[] */
 1.10577 +  tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
 1.10578 +  IndexSample *aSample;    /* Samples of the left-most key */
 1.10579 +#endif
 1.10580 +};
 1.10581 +
 1.10582 +/*
 1.10583 +** Each sample stored in the sqlite_stat3 table is represented in memory 
 1.10584 +** using a structure of this type.  See documentation at the top of the
 1.10585 +** analyze.c source file for additional information.
 1.10586 +*/
 1.10587 +struct IndexSample {
 1.10588 +  union {
 1.10589 +    char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
 1.10590 +    double r;       /* Value if eType is SQLITE_FLOAT */
 1.10591 +    i64 i;          /* Value if eType is SQLITE_INTEGER */
 1.10592 +  } u;
 1.10593 +  u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
 1.10594 +  int nByte;        /* Size in byte of text or blob. */
 1.10595 +  tRowcnt nEq;      /* Est. number of rows where the key equals this sample */
 1.10596 +  tRowcnt nLt;      /* Est. number of rows where key is less than this sample */
 1.10597 +  tRowcnt nDLt;     /* Est. number of distinct keys less than this sample */
 1.10598 +};
 1.10599 +
 1.10600 +/*
 1.10601 +** Each token coming out of the lexer is an instance of
 1.10602 +** this structure.  Tokens are also used as part of an expression.
 1.10603 +**
 1.10604 +** Note if Token.z==0 then Token.dyn and Token.n are undefined and
 1.10605 +** may contain random values.  Do not make any assumptions about Token.dyn
 1.10606 +** and Token.n when Token.z==0.
 1.10607 +*/
 1.10608 +struct Token {
 1.10609 +  const char *z;     /* Text of the token.  Not NULL-terminated! */
 1.10610 +  unsigned int n;    /* Number of characters in this token */
 1.10611 +};
 1.10612 +
 1.10613 +/*
 1.10614 +** An instance of this structure contains information needed to generate
 1.10615 +** code for a SELECT that contains aggregate functions.
 1.10616 +**
 1.10617 +** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
 1.10618 +** pointer to this structure.  The Expr.iColumn field is the index in
 1.10619 +** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
 1.10620 +** code for that node.
 1.10621 +**
 1.10622 +** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
 1.10623 +** original Select structure that describes the SELECT statement.  These
 1.10624 +** fields do not need to be freed when deallocating the AggInfo structure.
 1.10625 +*/
 1.10626 +struct AggInfo {
 1.10627 +  u8 directMode;          /* Direct rendering mode means take data directly
 1.10628 +                          ** from source tables rather than from accumulators */
 1.10629 +  u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
 1.10630 +                          ** than the source table */
 1.10631 +  int sortingIdx;         /* Cursor number of the sorting index */
 1.10632 +  int sortingIdxPTab;     /* Cursor number of pseudo-table */
 1.10633 +  int nSortingColumn;     /* Number of columns in the sorting index */
 1.10634 +  ExprList *pGroupBy;     /* The group by clause */
 1.10635 +  struct AggInfo_col {    /* For each column used in source tables */
 1.10636 +    Table *pTab;             /* Source table */
 1.10637 +    int iTable;              /* Cursor number of the source table */
 1.10638 +    int iColumn;             /* Column number within the source table */
 1.10639 +    int iSorterColumn;       /* Column number in the sorting index */
 1.10640 +    int iMem;                /* Memory location that acts as accumulator */
 1.10641 +    Expr *pExpr;             /* The original expression */
 1.10642 +  } *aCol;
 1.10643 +  int nColumn;            /* Number of used entries in aCol[] */
 1.10644 +  int nAccumulator;       /* Number of columns that show through to the output.
 1.10645 +                          ** Additional columns are used only as parameters to
 1.10646 +                          ** aggregate functions */
 1.10647 +  struct AggInfo_func {   /* For each aggregate function */
 1.10648 +    Expr *pExpr;             /* Expression encoding the function */
 1.10649 +    FuncDef *pFunc;          /* The aggregate function implementation */
 1.10650 +    int iMem;                /* Memory location that acts as accumulator */
 1.10651 +    int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
 1.10652 +  } *aFunc;
 1.10653 +  int nFunc;              /* Number of entries in aFunc[] */
 1.10654 +};
 1.10655 +
 1.10656 +/*
 1.10657 +** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
 1.10658 +** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
 1.10659 +** than 32767 we have to make it 32-bit.  16-bit is preferred because
 1.10660 +** it uses less memory in the Expr object, which is a big memory user
 1.10661 +** in systems with lots of prepared statements.  And few applications
 1.10662 +** need more than about 10 or 20 variables.  But some extreme users want
 1.10663 +** to have prepared statements with over 32767 variables, and for them
 1.10664 +** the option is available (at compile-time).
 1.10665 +*/
 1.10666 +#if SQLITE_MAX_VARIABLE_NUMBER<=32767
 1.10667 +typedef i16 ynVar;
 1.10668 +#else
 1.10669 +typedef int ynVar;
 1.10670 +#endif
 1.10671 +
 1.10672 +/*
 1.10673 +** Each node of an expression in the parse tree is an instance
 1.10674 +** of this structure.
 1.10675 +**
 1.10676 +** Expr.op is the opcode. The integer parser token codes are reused
 1.10677 +** as opcodes here. For example, the parser defines TK_GE to be an integer
 1.10678 +** code representing the ">=" operator. This same integer code is reused
 1.10679 +** to represent the greater-than-or-equal-to operator in the expression
 1.10680 +** tree.
 1.10681 +**
 1.10682 +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
 1.10683 +** or TK_STRING), then Expr.token contains the text of the SQL literal. If
 1.10684 +** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
 1.10685 +** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
 1.10686 +** then Expr.token contains the name of the function.
 1.10687 +**
 1.10688 +** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
 1.10689 +** binary operator. Either or both may be NULL.
 1.10690 +**
 1.10691 +** Expr.x.pList is a list of arguments if the expression is an SQL function,
 1.10692 +** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
 1.10693 +** Expr.x.pSelect is used if the expression is a sub-select or an expression of
 1.10694 +** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
 1.10695 +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
 1.10696 +** valid.
 1.10697 +**
 1.10698 +** An expression of the form ID or ID.ID refers to a column in a table.
 1.10699 +** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
 1.10700 +** the integer cursor number of a VDBE cursor pointing to that table and
 1.10701 +** Expr.iColumn is the column number for the specific column.  If the
 1.10702 +** expression is used as a result in an aggregate SELECT, then the
 1.10703 +** value is also stored in the Expr.iAgg column in the aggregate so that
 1.10704 +** it can be accessed after all aggregates are computed.
 1.10705 +**
 1.10706 +** If the expression is an unbound variable marker (a question mark 
 1.10707 +** character '?' in the original SQL) then the Expr.iTable holds the index 
 1.10708 +** number for that variable.
 1.10709 +**
 1.10710 +** If the expression is a subquery then Expr.iColumn holds an integer
 1.10711 +** register number containing the result of the subquery.  If the
 1.10712 +** subquery gives a constant result, then iTable is -1.  If the subquery
 1.10713 +** gives a different answer at different times during statement processing
 1.10714 +** then iTable is the address of a subroutine that computes the subquery.
 1.10715 +**
 1.10716 +** If the Expr is of type OP_Column, and the table it is selecting from
 1.10717 +** is a disk table or the "old.*" pseudo-table, then pTab points to the
 1.10718 +** corresponding table definition.
 1.10719 +**
 1.10720 +** ALLOCATION NOTES:
 1.10721 +**
 1.10722 +** Expr objects can use a lot of memory space in database schema.  To
 1.10723 +** help reduce memory requirements, sometimes an Expr object will be
 1.10724 +** truncated.  And to reduce the number of memory allocations, sometimes
 1.10725 +** two or more Expr objects will be stored in a single memory allocation,
 1.10726 +** together with Expr.zToken strings.
 1.10727 +**
 1.10728 +** If the EP_Reduced and EP_TokenOnly flags are set when
 1.10729 +** an Expr object is truncated.  When EP_Reduced is set, then all
 1.10730 +** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
 1.10731 +** are contained within the same memory allocation.  Note, however, that
 1.10732 +** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
 1.10733 +** allocated, regardless of whether or not EP_Reduced is set.
 1.10734 +*/
 1.10735 +struct Expr {
 1.10736 +  u8 op;                 /* Operation performed by this node */
 1.10737 +  char affinity;         /* The affinity of the column or 0 if not a column */
 1.10738 +  u16 flags;             /* Various flags.  EP_* See below */
 1.10739 +  union {
 1.10740 +    char *zToken;          /* Token value. Zero terminated and dequoted */
 1.10741 +    int iValue;            /* Non-negative integer value if EP_IntValue */
 1.10742 +  } u;
 1.10743 +
 1.10744 +  /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
 1.10745 +  ** space is allocated for the fields below this point. An attempt to
 1.10746 +  ** access them will result in a segfault or malfunction. 
 1.10747 +  *********************************************************************/
 1.10748 +
 1.10749 +  Expr *pLeft;           /* Left subnode */
 1.10750 +  Expr *pRight;          /* Right subnode */
 1.10751 +  union {
 1.10752 +    ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
 1.10753 +    Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
 1.10754 +  } x;
 1.10755 +
 1.10756 +  /* If the EP_Reduced flag is set in the Expr.flags mask, then no
 1.10757 +  ** space is allocated for the fields below this point. An attempt to
 1.10758 +  ** access them will result in a segfault or malfunction.
 1.10759 +  *********************************************************************/
 1.10760 +
 1.10761 +#if SQLITE_MAX_EXPR_DEPTH>0
 1.10762 +  int nHeight;           /* Height of the tree headed by this node */
 1.10763 +#endif
 1.10764 +  int iTable;            /* TK_COLUMN: cursor number of table holding column
 1.10765 +                         ** TK_REGISTER: register number
 1.10766 +                         ** TK_TRIGGER: 1 -> new, 0 -> old */
 1.10767 +  ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
 1.10768 +                         ** TK_VARIABLE: variable number (always >= 1). */
 1.10769 +  i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 1.10770 +  i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 1.10771 +  u8 flags2;             /* Second set of flags.  EP2_... */
 1.10772 +  u8 op2;                /* TK_REGISTER: original value of Expr.op
 1.10773 +                         ** TK_COLUMN: the value of p5 for OP_Column
 1.10774 +                         ** TK_AGG_FUNCTION: nesting depth */
 1.10775 +  AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 1.10776 +  Table *pTab;           /* Table for TK_COLUMN expressions. */
 1.10777 +};
 1.10778 +
 1.10779 +/*
 1.10780 +** The following are the meanings of bits in the Expr.flags field.
 1.10781 +*/
 1.10782 +#define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
 1.10783 +#define EP_Agg        0x0002  /* Contains one or more aggregate functions */
 1.10784 +#define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
 1.10785 +#define EP_Error      0x0008  /* Expression contains one or more errors */
 1.10786 +#define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
 1.10787 +#define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
 1.10788 +#define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
 1.10789 +#define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
 1.10790 +#define EP_Collate    0x0100  /* Tree contains a TK_COLLATE opeartor */
 1.10791 +#define EP_FixedDest  0x0200  /* Result needed in a specific register */
 1.10792 +#define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
 1.10793 +#define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
 1.10794 +#define EP_Hint       0x1000  /* Not used */
 1.10795 +#define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
 1.10796 +#define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
 1.10797 +#define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
 1.10798 +
 1.10799 +/*
 1.10800 +** The following are the meanings of bits in the Expr.flags2 field.
 1.10801 +*/
 1.10802 +#define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
 1.10803 +#define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
 1.10804 +
 1.10805 +/*
 1.10806 +** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
 1.10807 +** flag on an expression structure.  This flag is used for VV&A only.  The
 1.10808 +** routine is implemented as a macro that only works when in debugging mode,
 1.10809 +** so as not to burden production code.
 1.10810 +*/
 1.10811 +#ifdef SQLITE_DEBUG
 1.10812 +# define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
 1.10813 +#else
 1.10814 +# define ExprSetIrreducible(X)
 1.10815 +#endif
 1.10816 +
 1.10817 +/*
 1.10818 +** These macros can be used to test, set, or clear bits in the 
 1.10819 +** Expr.flags field.
 1.10820 +*/
 1.10821 +#define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
 1.10822 +#define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
 1.10823 +#define ExprSetProperty(E,P)     (E)->flags|=(P)
 1.10824 +#define ExprClearProperty(E,P)   (E)->flags&=~(P)
 1.10825 +
 1.10826 +/*
 1.10827 +** Macros to determine the number of bytes required by a normal Expr 
 1.10828 +** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
 1.10829 +** and an Expr struct with the EP_TokenOnly flag set.
 1.10830 +*/
 1.10831 +#define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
 1.10832 +#define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
 1.10833 +#define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
 1.10834 +
 1.10835 +/*
 1.10836 +** Flags passed to the sqlite3ExprDup() function. See the header comment 
 1.10837 +** above sqlite3ExprDup() for details.
 1.10838 +*/
 1.10839 +#define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
 1.10840 +
 1.10841 +/*
 1.10842 +** A list of expressions.  Each expression may optionally have a
 1.10843 +** name.  An expr/name combination can be used in several ways, such
 1.10844 +** as the list of "expr AS ID" fields following a "SELECT" or in the
 1.10845 +** list of "ID = expr" items in an UPDATE.  A list of expressions can
 1.10846 +** also be used as the argument to a function, in which case the a.zName
 1.10847 +** field is not used.
 1.10848 +*/
 1.10849 +struct ExprList {
 1.10850 +  int nExpr;             /* Number of expressions on the list */
 1.10851 +  int iECursor;          /* VDBE Cursor associated with this ExprList */
 1.10852 +  struct ExprList_item { /* For each expression in the list */
 1.10853 +    Expr *pExpr;           /* The list of expressions */
 1.10854 +    char *zName;           /* Token associated with this expression */
 1.10855 +    char *zSpan;           /* Original text of the expression */
 1.10856 +    u8 sortOrder;          /* 1 for DESC or 0 for ASC */
 1.10857 +    u8 done;               /* A flag to indicate when processing is finished */
 1.10858 +    u16 iOrderByCol;       /* For ORDER BY, column number in result set */
 1.10859 +    u16 iAlias;            /* Index into Parse.aAlias[] for zName */
 1.10860 +  } *a;                  /* Alloc a power of two greater or equal to nExpr */
 1.10861 +};
 1.10862 +
 1.10863 +/*
 1.10864 +** An instance of this structure is used by the parser to record both
 1.10865 +** the parse tree for an expression and the span of input text for an
 1.10866 +** expression.
 1.10867 +*/
 1.10868 +struct ExprSpan {
 1.10869 +  Expr *pExpr;          /* The expression parse tree */
 1.10870 +  const char *zStart;   /* First character of input text */
 1.10871 +  const char *zEnd;     /* One character past the end of input text */
 1.10872 +};
 1.10873 +
 1.10874 +/*
 1.10875 +** An instance of this structure can hold a simple list of identifiers,
 1.10876 +** such as the list "a,b,c" in the following statements:
 1.10877 +**
 1.10878 +**      INSERT INTO t(a,b,c) VALUES ...;
 1.10879 +**      CREATE INDEX idx ON t(a,b,c);
 1.10880 +**      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
 1.10881 +**
 1.10882 +** The IdList.a.idx field is used when the IdList represents the list of
 1.10883 +** column names after a table name in an INSERT statement.  In the statement
 1.10884 +**
 1.10885 +**     INSERT INTO t(a,b,c) ...
 1.10886 +**
 1.10887 +** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
 1.10888 +*/
 1.10889 +struct IdList {
 1.10890 +  struct IdList_item {
 1.10891 +    char *zName;      /* Name of the identifier */
 1.10892 +    int idx;          /* Index in some Table.aCol[] of a column named zName */
 1.10893 +  } *a;
 1.10894 +  int nId;         /* Number of identifiers on the list */
 1.10895 +};
 1.10896 +
 1.10897 +/*
 1.10898 +** The bitmask datatype defined below is used for various optimizations.
 1.10899 +**
 1.10900 +** Changing this from a 64-bit to a 32-bit type limits the number of
 1.10901 +** tables in a join to 32 instead of 64.  But it also reduces the size
 1.10902 +** of the library by 738 bytes on ix86.
 1.10903 +*/
 1.10904 +typedef u64 Bitmask;
 1.10905 +
 1.10906 +/*
 1.10907 +** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
 1.10908 +*/
 1.10909 +#define BMS  ((int)(sizeof(Bitmask)*8))
 1.10910 +
 1.10911 +/*
 1.10912 +** The following structure describes the FROM clause of a SELECT statement.
 1.10913 +** Each table or subquery in the FROM clause is a separate element of
 1.10914 +** the SrcList.a[] array.
 1.10915 +**
 1.10916 +** With the addition of multiple database support, the following structure
 1.10917 +** can also be used to describe a particular table such as the table that
 1.10918 +** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
 1.10919 +** such a table must be a simple name: ID.  But in SQLite, the table can
 1.10920 +** now be identified by a database name, a dot, then the table name: ID.ID.
 1.10921 +**
 1.10922 +** The jointype starts out showing the join type between the current table
 1.10923 +** and the next table on the list.  The parser builds the list this way.
 1.10924 +** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
 1.10925 +** jointype expresses the join between the table and the previous table.
 1.10926 +**
 1.10927 +** In the colUsed field, the high-order bit (bit 63) is set if the table
 1.10928 +** contains more than 63 columns and the 64-th or later column is used.
 1.10929 +*/
 1.10930 +struct SrcList {
 1.10931 +  i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
 1.10932 +  i16 nAlloc;      /* Number of entries allocated in a[] below */
 1.10933 +  struct SrcList_item {
 1.10934 +    Schema *pSchema;  /* Schema to which this item is fixed */
 1.10935 +    char *zDatabase;  /* Name of database holding this table */
 1.10936 +    char *zName;      /* Name of the table */
 1.10937 +    char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
 1.10938 +    Table *pTab;      /* An SQL table corresponding to zName */
 1.10939 +    Select *pSelect;  /* A SELECT statement used in place of a table name */
 1.10940 +    int addrFillSub;  /* Address of subroutine to manifest a subquery */
 1.10941 +    int regReturn;    /* Register holding return address of addrFillSub */
 1.10942 +    u8 jointype;      /* Type of join between this able and the previous */
 1.10943 +    unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
 1.10944 +    unsigned isCorrelated :1;  /* True if sub-query is correlated */
 1.10945 +    unsigned viaCoroutine :1;  /* Implemented as a co-routine */
 1.10946 +#ifndef SQLITE_OMIT_EXPLAIN
 1.10947 +    u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
 1.10948 +#endif
 1.10949 +    int iCursor;      /* The VDBE cursor number used to access this table */
 1.10950 +    Expr *pOn;        /* The ON clause of a join */
 1.10951 +    IdList *pUsing;   /* The USING clause of a join */
 1.10952 +    Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
 1.10953 +    char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
 1.10954 +    Index *pIndex;    /* Index structure corresponding to zIndex, if any */
 1.10955 +  } a[1];             /* One entry for each identifier on the list */
 1.10956 +};
 1.10957 +
 1.10958 +/*
 1.10959 +** Permitted values of the SrcList.a.jointype field
 1.10960 +*/
 1.10961 +#define JT_INNER     0x0001    /* Any kind of inner or cross join */
 1.10962 +#define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
 1.10963 +#define JT_NATURAL   0x0004    /* True for a "natural" join */
 1.10964 +#define JT_LEFT      0x0008    /* Left outer join */
 1.10965 +#define JT_RIGHT     0x0010    /* Right outer join */
 1.10966 +#define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
 1.10967 +#define JT_ERROR     0x0040    /* unknown or unsupported join type */
 1.10968 +
 1.10969 +
 1.10970 +/*
 1.10971 +** A WherePlan object holds information that describes a lookup
 1.10972 +** strategy.
 1.10973 +**
 1.10974 +** This object is intended to be opaque outside of the where.c module.
 1.10975 +** It is included here only so that that compiler will know how big it
 1.10976 +** is.  None of the fields in this object should be used outside of
 1.10977 +** the where.c module.
 1.10978 +**
 1.10979 +** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
 1.10980 +** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
 1.10981 +** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
 1.10982 +** case that more than one of these conditions is true.
 1.10983 +*/
 1.10984 +struct WherePlan {
 1.10985 +  u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
 1.10986 +  u16 nEq;                       /* Number of == constraints */
 1.10987 +  u16 nOBSat;                    /* Number of ORDER BY terms satisfied */
 1.10988 +  double nRow;                   /* Estimated number of rows (for EQP) */
 1.10989 +  union {
 1.10990 +    Index *pIdx;                   /* Index when WHERE_INDEXED is true */
 1.10991 +    struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
 1.10992 +    sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
 1.10993 +  } u;
 1.10994 +};
 1.10995 +
 1.10996 +/*
 1.10997 +** For each nested loop in a WHERE clause implementation, the WhereInfo
 1.10998 +** structure contains a single instance of this structure.  This structure
 1.10999 +** is intended to be private to the where.c module and should not be
 1.11000 +** access or modified by other modules.
 1.11001 +**
 1.11002 +** The pIdxInfo field is used to help pick the best index on a
 1.11003 +** virtual table.  The pIdxInfo pointer contains indexing
 1.11004 +** information for the i-th table in the FROM clause before reordering.
 1.11005 +** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
 1.11006 +** All other information in the i-th WhereLevel object for the i-th table
 1.11007 +** after FROM clause ordering.
 1.11008 +*/
 1.11009 +struct WhereLevel {
 1.11010 +  WherePlan plan;       /* query plan for this element of the FROM clause */
 1.11011 +  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
 1.11012 +  int iTabCur;          /* The VDBE cursor used to access the table */
 1.11013 +  int iIdxCur;          /* The VDBE cursor used to access pIdx */
 1.11014 +  int addrBrk;          /* Jump here to break out of the loop */
 1.11015 +  int addrNxt;          /* Jump here to start the next IN combination */
 1.11016 +  int addrCont;         /* Jump here to continue with the next loop cycle */
 1.11017 +  int addrFirst;        /* First instruction of interior of the loop */
 1.11018 +  u8 iFrom;             /* Which entry in the FROM clause */
 1.11019 +  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
 1.11020 +  int p1, p2;           /* Operands of the opcode used to ends the loop */
 1.11021 +  union {               /* Information that depends on plan.wsFlags */
 1.11022 +    struct {
 1.11023 +      int nIn;              /* Number of entries in aInLoop[] */
 1.11024 +      struct InLoop {
 1.11025 +        int iCur;              /* The VDBE cursor used by this IN operator */
 1.11026 +        int addrInTop;         /* Top of the IN loop */
 1.11027 +      } *aInLoop;           /* Information about each nested IN operator */
 1.11028 +    } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
 1.11029 +    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
 1.11030 +  } u;
 1.11031 +  double rOptCost;      /* "Optimal" cost for this level */
 1.11032 +
 1.11033 +  /* The following field is really not part of the current level.  But
 1.11034 +  ** we need a place to cache virtual table index information for each
 1.11035 +  ** virtual table in the FROM clause and the WhereLevel structure is
 1.11036 +  ** a convenient place since there is one WhereLevel for each FROM clause
 1.11037 +  ** element.
 1.11038 +  */
 1.11039 +  sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
 1.11040 +};
 1.11041 +
 1.11042 +/*
 1.11043 +** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
 1.11044 +** and the WhereInfo.wctrlFlags member.
 1.11045 +*/
 1.11046 +#define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
 1.11047 +#define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
 1.11048 +#define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
 1.11049 +#define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
 1.11050 +#define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
 1.11051 +#define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
 1.11052 +#define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
 1.11053 +#define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
 1.11054 +#define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
 1.11055 +
 1.11056 +/*
 1.11057 +** The WHERE clause processing routine has two halves.  The
 1.11058 +** first part does the start of the WHERE loop and the second
 1.11059 +** half does the tail of the WHERE loop.  An instance of
 1.11060 +** this structure is returned by the first half and passed
 1.11061 +** into the second half to give some continuity.
 1.11062 +*/
 1.11063 +struct WhereInfo {
 1.11064 +  Parse *pParse;            /* Parsing and code generating context */
 1.11065 +  SrcList *pTabList;        /* List of tables in the join */
 1.11066 +  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
 1.11067 +  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
 1.11068 +  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
 1.11069 +  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
 1.11070 +  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
 1.11071 +  int iTop;                 /* The very beginning of the WHERE loop */
 1.11072 +  int iContinue;            /* Jump here to continue with next record */
 1.11073 +  int iBreak;               /* Jump here to break out of the loop */
 1.11074 +  int nLevel;               /* Number of nested loop */
 1.11075 +  struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
 1.11076 +  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
 1.11077 +  double nRowOut;           /* Estimated number of output rows */
 1.11078 +  WhereLevel a[1];          /* Information about each nest loop in WHERE */
 1.11079 +};
 1.11080 +
 1.11081 +/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
 1.11082 +#define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
 1.11083 +#define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
 1.11084 +#define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
 1.11085 +#define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
 1.11086 +
 1.11087 +/*
 1.11088 +** A NameContext defines a context in which to resolve table and column
 1.11089 +** names.  The context consists of a list of tables (the pSrcList) field and
 1.11090 +** a list of named expression (pEList).  The named expression list may
 1.11091 +** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
 1.11092 +** to the table being operated on by INSERT, UPDATE, or DELETE.  The
 1.11093 +** pEList corresponds to the result set of a SELECT and is NULL for
 1.11094 +** other statements.
 1.11095 +**
 1.11096 +** NameContexts can be nested.  When resolving names, the inner-most 
 1.11097 +** context is searched first.  If no match is found, the next outer
 1.11098 +** context is checked.  If there is still no match, the next context
 1.11099 +** is checked.  This process continues until either a match is found
 1.11100 +** or all contexts are check.  When a match is found, the nRef member of
 1.11101 +** the context containing the match is incremented. 
 1.11102 +**
 1.11103 +** Each subquery gets a new NameContext.  The pNext field points to the
 1.11104 +** NameContext in the parent query.  Thus the process of scanning the
 1.11105 +** NameContext list corresponds to searching through successively outer
 1.11106 +** subqueries looking for a match.
 1.11107 +*/
 1.11108 +struct NameContext {
 1.11109 +  Parse *pParse;       /* The parser */
 1.11110 +  SrcList *pSrcList;   /* One or more tables used to resolve names */
 1.11111 +  ExprList *pEList;    /* Optional list of named expressions */
 1.11112 +  AggInfo *pAggInfo;   /* Information about aggregates at this level */
 1.11113 +  NameContext *pNext;  /* Next outer name context.  NULL for outermost */
 1.11114 +  int nRef;            /* Number of names resolved by this context */
 1.11115 +  int nErr;            /* Number of errors encountered while resolving names */
 1.11116 +  u8 ncFlags;          /* Zero or more NC_* flags defined below */
 1.11117 +};
 1.11118 +
 1.11119 +/*
 1.11120 +** Allowed values for the NameContext, ncFlags field.
 1.11121 +*/
 1.11122 +#define NC_AllowAgg  0x01    /* Aggregate functions are allowed here */
 1.11123 +#define NC_HasAgg    0x02    /* One or more aggregate functions seen */
 1.11124 +#define NC_IsCheck   0x04    /* True if resolving names in a CHECK constraint */
 1.11125 +#define NC_InAggFunc 0x08    /* True if analyzing arguments to an agg func */
 1.11126 +
 1.11127 +/*
 1.11128 +** An instance of the following structure contains all information
 1.11129 +** needed to generate code for a single SELECT statement.
 1.11130 +**
 1.11131 +** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
 1.11132 +** If there is a LIMIT clause, the parser sets nLimit to the value of the
 1.11133 +** limit and nOffset to the value of the offset (or 0 if there is not
 1.11134 +** offset).  But later on, nLimit and nOffset become the memory locations
 1.11135 +** in the VDBE that record the limit and offset counters.
 1.11136 +**
 1.11137 +** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
 1.11138 +** These addresses must be stored so that we can go back and fill in
 1.11139 +** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
 1.11140 +** the number of columns in P2 can be computed at the same time
 1.11141 +** as the OP_OpenEphm instruction is coded because not
 1.11142 +** enough information about the compound query is known at that point.
 1.11143 +** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
 1.11144 +** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
 1.11145 +** sequences for the ORDER BY clause.
 1.11146 +*/
 1.11147 +struct Select {
 1.11148 +  ExprList *pEList;      /* The fields of the result */
 1.11149 +  u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
 1.11150 +  u16 selFlags;          /* Various SF_* values */
 1.11151 +  int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
 1.11152 +  int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
 1.11153 +  double nSelectRow;     /* Estimated number of result rows */
 1.11154 +  SrcList *pSrc;         /* The FROM clause */
 1.11155 +  Expr *pWhere;          /* The WHERE clause */
 1.11156 +  ExprList *pGroupBy;    /* The GROUP BY clause */
 1.11157 +  Expr *pHaving;         /* The HAVING clause */
 1.11158 +  ExprList *pOrderBy;    /* The ORDER BY clause */
 1.11159 +  Select *pPrior;        /* Prior select in a compound select statement */
 1.11160 +  Select *pNext;         /* Next select to the left in a compound */
 1.11161 +  Select *pRightmost;    /* Right-most select in a compound select statement */
 1.11162 +  Expr *pLimit;          /* LIMIT expression. NULL means not used. */
 1.11163 +  Expr *pOffset;         /* OFFSET expression. NULL means not used. */
 1.11164 +};
 1.11165 +
 1.11166 +/*
 1.11167 +** Allowed values for Select.selFlags.  The "SF" prefix stands for
 1.11168 +** "Select Flag".
 1.11169 +*/
 1.11170 +#define SF_Distinct        0x0001  /* Output should be DISTINCT */
 1.11171 +#define SF_Resolved        0x0002  /* Identifiers have been resolved */
 1.11172 +#define SF_Aggregate       0x0004  /* Contains aggregate functions */
 1.11173 +#define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 1.11174 +#define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 1.11175 +#define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
 1.11176 +#define SF_UseSorter       0x0040  /* Sort using a sorter */
 1.11177 +#define SF_Values          0x0080  /* Synthesized from VALUES clause */
 1.11178 +#define SF_Materialize     0x0100  /* Force materialization of views */
 1.11179 +
 1.11180 +
 1.11181 +/*
 1.11182 +** The results of a select can be distributed in several ways.  The
 1.11183 +** "SRT" prefix means "SELECT Result Type".
 1.11184 +*/
 1.11185 +#define SRT_Union        1  /* Store result as keys in an index */
 1.11186 +#define SRT_Except       2  /* Remove result from a UNION index */
 1.11187 +#define SRT_Exists       3  /* Store 1 if the result is not empty */
 1.11188 +#define SRT_Discard      4  /* Do not save the results anywhere */
 1.11189 +
 1.11190 +/* The ORDER BY clause is ignored for all of the above */
 1.11191 +#define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
 1.11192 +
 1.11193 +#define SRT_Output       5  /* Output each row of result */
 1.11194 +#define SRT_Mem          6  /* Store result in a memory cell */
 1.11195 +#define SRT_Set          7  /* Store results as keys in an index */
 1.11196 +#define SRT_Table        8  /* Store result as data with an automatic rowid */
 1.11197 +#define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
 1.11198 +#define SRT_Coroutine   10  /* Generate a single row of result */
 1.11199 +
 1.11200 +/*
 1.11201 +** An instance of this object describes where to put of the results of
 1.11202 +** a SELECT statement.
 1.11203 +*/
 1.11204 +struct SelectDest {
 1.11205 +  u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
 1.11206 +  char affSdst;     /* Affinity used when eDest==SRT_Set */
 1.11207 +  int iSDParm;      /* A parameter used by the eDest disposal method */
 1.11208 +  int iSdst;        /* Base register where results are written */
 1.11209 +  int nSdst;        /* Number of registers allocated */
 1.11210 +};
 1.11211 +
 1.11212 +/*
 1.11213 +** During code generation of statements that do inserts into AUTOINCREMENT 
 1.11214 +** tables, the following information is attached to the Table.u.autoInc.p
 1.11215 +** pointer of each autoincrement table to record some side information that
 1.11216 +** the code generator needs.  We have to keep per-table autoincrement
 1.11217 +** information in case inserts are down within triggers.  Triggers do not
 1.11218 +** normally coordinate their activities, but we do need to coordinate the
 1.11219 +** loading and saving of autoincrement information.
 1.11220 +*/
 1.11221 +struct AutoincInfo {
 1.11222 +  AutoincInfo *pNext;   /* Next info block in a list of them all */
 1.11223 +  Table *pTab;          /* Table this info block refers to */
 1.11224 +  int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
 1.11225 +  int regCtr;           /* Memory register holding the rowid counter */
 1.11226 +};
 1.11227 +
 1.11228 +/*
 1.11229 +** Size of the column cache
 1.11230 +*/
 1.11231 +#ifndef SQLITE_N_COLCACHE
 1.11232 +# define SQLITE_N_COLCACHE 10
 1.11233 +#endif
 1.11234 +
 1.11235 +/*
 1.11236 +** At least one instance of the following structure is created for each 
 1.11237 +** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
 1.11238 +** statement. All such objects are stored in the linked list headed at
 1.11239 +** Parse.pTriggerPrg and deleted once statement compilation has been
 1.11240 +** completed.
 1.11241 +**
 1.11242 +** A Vdbe sub-program that implements the body and WHEN clause of trigger
 1.11243 +** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
 1.11244 +** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
 1.11245 +** The Parse.pTriggerPrg list never contains two entries with the same
 1.11246 +** values for both pTrigger and orconf.
 1.11247 +**
 1.11248 +** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
 1.11249 +** accessed (or set to 0 for triggers fired as a result of INSERT 
 1.11250 +** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
 1.11251 +** a mask of new.* columns used by the program.
 1.11252 +*/
 1.11253 +struct TriggerPrg {
 1.11254 +  Trigger *pTrigger;      /* Trigger this program was coded from */
 1.11255 +  TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
 1.11256 +  SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
 1.11257 +  int orconf;             /* Default ON CONFLICT policy */
 1.11258 +  u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
 1.11259 +};
 1.11260 +
 1.11261 +/*
 1.11262 +** The yDbMask datatype for the bitmask of all attached databases.
 1.11263 +*/
 1.11264 +#if SQLITE_MAX_ATTACHED>30
 1.11265 +  typedef sqlite3_uint64 yDbMask;
 1.11266 +#else
 1.11267 +  typedef unsigned int yDbMask;
 1.11268 +#endif
 1.11269 +
 1.11270 +/*
 1.11271 +** An SQL parser context.  A copy of this structure is passed through
 1.11272 +** the parser and down into all the parser action routine in order to
 1.11273 +** carry around information that is global to the entire parse.
 1.11274 +**
 1.11275 +** The structure is divided into two parts.  When the parser and code
 1.11276 +** generate call themselves recursively, the first part of the structure
 1.11277 +** is constant but the second part is reset at the beginning and end of
 1.11278 +** each recursion.
 1.11279 +**
 1.11280 +** The nTableLock and aTableLock variables are only used if the shared-cache 
 1.11281 +** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
 1.11282 +** used to store the set of table-locks required by the statement being
 1.11283 +** compiled. Function sqlite3TableLock() is used to add entries to the
 1.11284 +** list.
 1.11285 +*/
 1.11286 +struct Parse {
 1.11287 +  sqlite3 *db;         /* The main database structure */
 1.11288 +  char *zErrMsg;       /* An error message */
 1.11289 +  Vdbe *pVdbe;         /* An engine for executing database bytecode */
 1.11290 +  int rc;              /* Return code from execution */
 1.11291 +  u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
 1.11292 +  u8 checkSchema;      /* Causes schema cookie check after an error */
 1.11293 +  u8 nested;           /* Number of nested calls to the parser/code generator */
 1.11294 +  u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
 1.11295 +  u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
 1.11296 +  u8 nColCache;        /* Number of entries in aColCache[] */
 1.11297 +  u8 iColCache;        /* Next entry in aColCache[] to replace */
 1.11298 +  u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
 1.11299 +  u8 mayAbort;         /* True if statement may throw an ABORT exception */
 1.11300 +  int aTempReg[8];     /* Holding area for temporary registers */
 1.11301 +  int nRangeReg;       /* Size of the temporary register block */
 1.11302 +  int iRangeReg;       /* First register in temporary register block */
 1.11303 +  int nErr;            /* Number of errors seen */
 1.11304 +  int nTab;            /* Number of previously allocated VDBE cursors */
 1.11305 +  int nMem;            /* Number of memory cells used so far */
 1.11306 +  int nSet;            /* Number of sets used so far */
 1.11307 +  int nOnce;           /* Number of OP_Once instructions so far */
 1.11308 +  int ckBase;          /* Base register of data during check constraints */
 1.11309 +  int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
 1.11310 +  int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
 1.11311 +  struct yColCache {
 1.11312 +    int iTable;           /* Table cursor number */
 1.11313 +    int iColumn;          /* Table column number */
 1.11314 +    u8 tempReg;           /* iReg is a temp register that needs to be freed */
 1.11315 +    int iLevel;           /* Nesting level */
 1.11316 +    int iReg;             /* Reg with value of this column. 0 means none. */
 1.11317 +    int lru;              /* Least recently used entry has the smallest value */
 1.11318 +  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
 1.11319 +  yDbMask writeMask;   /* Start a write transaction on these databases */
 1.11320 +  yDbMask cookieMask;  /* Bitmask of schema verified databases */
 1.11321 +  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
 1.11322 +  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
 1.11323 +  int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 1.11324 +  int regRoot;         /* Register holding root page number for new objects */
 1.11325 +  int nMaxArg;         /* Max args passed to user function by sub-program */
 1.11326 +  Token constraintName;/* Name of the constraint currently being parsed */
 1.11327 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.11328 +  int nTableLock;        /* Number of locks in aTableLock */
 1.11329 +  TableLock *aTableLock; /* Required table locks for shared-cache mode */
 1.11330 +#endif
 1.11331 +  AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 1.11332 +
 1.11333 +  /* Information used while coding trigger programs. */
 1.11334 +  Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 1.11335 +  Table *pTriggerTab;  /* Table triggers are being coded for */
 1.11336 +  double nQueryLoop;   /* Estimated number of iterations of a query */
 1.11337 +  u32 oldmask;         /* Mask of old.* columns referenced */
 1.11338 +  u32 newmask;         /* Mask of new.* columns referenced */
 1.11339 +  u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 1.11340 +  u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 1.11341 +  u8 disableTriggers;  /* True to disable triggers */
 1.11342 +
 1.11343 +  /* Above is constant between recursions.  Below is reset before and after
 1.11344 +  ** each recursion */
 1.11345 +
 1.11346 +  int nVar;                 /* Number of '?' variables seen in the SQL so far */
 1.11347 +  int nzVar;                /* Number of available slots in azVar[] */
 1.11348 +  u8 explain;               /* True if the EXPLAIN flag is found on the query */
 1.11349 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.11350 +  u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
 1.11351 +  int nVtabLock;            /* Number of virtual tables to lock */
 1.11352 +#endif
 1.11353 +  int nAlias;               /* Number of aliased result set columns */
 1.11354 +  int nHeight;              /* Expression tree height of current sub-select */
 1.11355 +#ifndef SQLITE_OMIT_EXPLAIN
 1.11356 +  int iSelectId;            /* ID of current select for EXPLAIN output */
 1.11357 +  int iNextSelectId;        /* Next available select ID for EXPLAIN output */
 1.11358 +#endif
 1.11359 +  char **azVar;             /* Pointers to names of parameters */
 1.11360 +  Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
 1.11361 +  int *aAlias;              /* Register used to hold aliased result */
 1.11362 +  const char *zTail;        /* All SQL text past the last semicolon parsed */
 1.11363 +  Table *pNewTable;         /* A table being constructed by CREATE TABLE */
 1.11364 +  Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
 1.11365 +  const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
 1.11366 +  Token sNameToken;         /* Token with unqualified schema object name */
 1.11367 +  Token sLastToken;         /* The last token parsed */
 1.11368 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.11369 +  Token sArg;               /* Complete text of a module argument */
 1.11370 +  Table **apVtabLock;       /* Pointer to virtual tables needing locking */
 1.11371 +#endif
 1.11372 +  Table *pZombieTab;        /* List of Table objects to delete after code gen */
 1.11373 +  TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
 1.11374 +};
 1.11375 +
 1.11376 +/*
 1.11377 +** Return true if currently inside an sqlite3_declare_vtab() call.
 1.11378 +*/
 1.11379 +#ifdef SQLITE_OMIT_VIRTUALTABLE
 1.11380 +  #define IN_DECLARE_VTAB 0
 1.11381 +#else
 1.11382 +  #define IN_DECLARE_VTAB (pParse->declareVtab)
 1.11383 +#endif
 1.11384 +
 1.11385 +/*
 1.11386 +** An instance of the following structure can be declared on a stack and used
 1.11387 +** to save the Parse.zAuthContext value so that it can be restored later.
 1.11388 +*/
 1.11389 +struct AuthContext {
 1.11390 +  const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
 1.11391 +  Parse *pParse;              /* The Parse structure */
 1.11392 +};
 1.11393 +
 1.11394 +/*
 1.11395 +** Bitfield flags for P5 value in various opcodes.
 1.11396 +*/
 1.11397 +#define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
 1.11398 +#define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
 1.11399 +#define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 1.11400 +#define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 1.11401 +#define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 1.11402 +#define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
 1.11403 +#define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 1.11404 +#define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 1.11405 +#define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 1.11406 +#define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
 1.11407 +#define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
 1.11408 +
 1.11409 +/*
 1.11410 + * Each trigger present in the database schema is stored as an instance of
 1.11411 + * struct Trigger. 
 1.11412 + *
 1.11413 + * Pointers to instances of struct Trigger are stored in two ways.
 1.11414 + * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
 1.11415 + *    database). This allows Trigger structures to be retrieved by name.
 1.11416 + * 2. All triggers associated with a single table form a linked list, using the
 1.11417 + *    pNext member of struct Trigger. A pointer to the first element of the
 1.11418 + *    linked list is stored as the "pTrigger" member of the associated
 1.11419 + *    struct Table.
 1.11420 + *
 1.11421 + * The "step_list" member points to the first element of a linked list
 1.11422 + * containing the SQL statements specified as the trigger program.
 1.11423 + */
 1.11424 +struct Trigger {
 1.11425 +  char *zName;            /* The name of the trigger                        */
 1.11426 +  char *table;            /* The table or view to which the trigger applies */
 1.11427 +  u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
 1.11428 +  u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
 1.11429 +  Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
 1.11430 +  IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
 1.11431 +                             the <column-list> is stored here */
 1.11432 +  Schema *pSchema;        /* Schema containing the trigger */
 1.11433 +  Schema *pTabSchema;     /* Schema containing the table */
 1.11434 +  TriggerStep *step_list; /* Link list of trigger program steps             */
 1.11435 +  Trigger *pNext;         /* Next trigger associated with the table */
 1.11436 +};
 1.11437 +
 1.11438 +/*
 1.11439 +** A trigger is either a BEFORE or an AFTER trigger.  The following constants
 1.11440 +** determine which. 
 1.11441 +**
 1.11442 +** If there are multiple triggers, you might of some BEFORE and some AFTER.
 1.11443 +** In that cases, the constants below can be ORed together.
 1.11444 +*/
 1.11445 +#define TRIGGER_BEFORE  1
 1.11446 +#define TRIGGER_AFTER   2
 1.11447 +
 1.11448 +/*
 1.11449 + * An instance of struct TriggerStep is used to store a single SQL statement
 1.11450 + * that is a part of a trigger-program. 
 1.11451 + *
 1.11452 + * Instances of struct TriggerStep are stored in a singly linked list (linked
 1.11453 + * using the "pNext" member) referenced by the "step_list" member of the 
 1.11454 + * associated struct Trigger instance. The first element of the linked list is
 1.11455 + * the first step of the trigger-program.
 1.11456 + * 
 1.11457 + * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
 1.11458 + * "SELECT" statement. The meanings of the other members is determined by the 
 1.11459 + * value of "op" as follows:
 1.11460 + *
 1.11461 + * (op == TK_INSERT)
 1.11462 + * orconf    -> stores the ON CONFLICT algorithm
 1.11463 + * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
 1.11464 + *              this stores a pointer to the SELECT statement. Otherwise NULL.
 1.11465 + * target    -> A token holding the quoted name of the table to insert into.
 1.11466 + * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
 1.11467 + *              this stores values to be inserted. Otherwise NULL.
 1.11468 + * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
 1.11469 + *              statement, then this stores the column-names to be
 1.11470 + *              inserted into.
 1.11471 + *
 1.11472 + * (op == TK_DELETE)
 1.11473 + * target    -> A token holding the quoted name of the table to delete from.
 1.11474 + * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
 1.11475 + *              Otherwise NULL.
 1.11476 + * 
 1.11477 + * (op == TK_UPDATE)
 1.11478 + * target    -> A token holding the quoted name of the table to update rows of.
 1.11479 + * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
 1.11480 + *              Otherwise NULL.
 1.11481 + * pExprList -> A list of the columns to update and the expressions to update
 1.11482 + *              them to. See sqlite3Update() documentation of "pChanges"
 1.11483 + *              argument.
 1.11484 + * 
 1.11485 + */
 1.11486 +struct TriggerStep {
 1.11487 +  u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
 1.11488 +  u8 orconf;           /* OE_Rollback etc. */
 1.11489 +  Trigger *pTrig;      /* The trigger that this step is a part of */
 1.11490 +  Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
 1.11491 +  Token target;        /* Target table for DELETE, UPDATE, INSERT */
 1.11492 +  Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
 1.11493 +  ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
 1.11494 +  IdList *pIdList;     /* Column names for INSERT */
 1.11495 +  TriggerStep *pNext;  /* Next in the link-list */
 1.11496 +  TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 1.11497 +};
 1.11498 +
 1.11499 +/*
 1.11500 +** The following structure contains information used by the sqliteFix...
 1.11501 +** routines as they walk the parse tree to make database references
 1.11502 +** explicit.  
 1.11503 +*/
 1.11504 +typedef struct DbFixer DbFixer;
 1.11505 +struct DbFixer {
 1.11506 +  Parse *pParse;      /* The parsing context.  Error messages written here */
 1.11507 +  Schema *pSchema;    /* Fix items to this schema */
 1.11508 +  const char *zDb;    /* Make sure all objects are contained in this database */
 1.11509 +  const char *zType;  /* Type of the container - used for error messages */
 1.11510 +  const Token *pName; /* Name of the container - used for error messages */
 1.11511 +};
 1.11512 +
 1.11513 +/*
 1.11514 +** An objected used to accumulate the text of a string where we
 1.11515 +** do not necessarily know how big the string will be in the end.
 1.11516 +*/
 1.11517 +struct StrAccum {
 1.11518 +  sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
 1.11519 +  char *zBase;         /* A base allocation.  Not from malloc. */
 1.11520 +  char *zText;         /* The string collected so far */
 1.11521 +  int  nChar;          /* Length of the string so far */
 1.11522 +  int  nAlloc;         /* Amount of space allocated in zText */
 1.11523 +  int  mxAlloc;        /* Maximum allowed string length */
 1.11524 +  u8   mallocFailed;   /* Becomes true if any memory allocation fails */
 1.11525 +  u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
 1.11526 +  u8   tooBig;         /* Becomes true if string size exceeds limits */
 1.11527 +};
 1.11528 +
 1.11529 +/*
 1.11530 +** A pointer to this structure is used to communicate information
 1.11531 +** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
 1.11532 +*/
 1.11533 +typedef struct {
 1.11534 +  sqlite3 *db;        /* The database being initialized */
 1.11535 +  char **pzErrMsg;    /* Error message stored here */
 1.11536 +  int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
 1.11537 +  int rc;             /* Result code stored here */
 1.11538 +} InitData;
 1.11539 +
 1.11540 +/*
 1.11541 +** Structure containing global configuration data for the SQLite library.
 1.11542 +**
 1.11543 +** This structure also contains some state information.
 1.11544 +*/
 1.11545 +struct Sqlite3Config {
 1.11546 +  int bMemstat;                     /* True to enable memory status */
 1.11547 +  int bCoreMutex;                   /* True to enable core mutexing */
 1.11548 +  int bFullMutex;                   /* True to enable full mutexing */
 1.11549 +  int bOpenUri;                     /* True to interpret filenames as URIs */
 1.11550 +  int bUseCis;                      /* Use covering indices for full-scans */
 1.11551 +  int mxStrlen;                     /* Maximum string length */
 1.11552 +  int szLookaside;                  /* Default lookaside buffer size */
 1.11553 +  int nLookaside;                   /* Default lookaside buffer count */
 1.11554 +  sqlite3_mem_methods m;            /* Low-level memory allocation interface */
 1.11555 +  sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
 1.11556 +  sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
 1.11557 +  void *pHeap;                      /* Heap storage space */
 1.11558 +  int nHeap;                        /* Size of pHeap[] */
 1.11559 +  int mnReq, mxReq;                 /* Min and max heap requests sizes */
 1.11560 +  void *pScratch;                   /* Scratch memory */
 1.11561 +  int szScratch;                    /* Size of each scratch buffer */
 1.11562 +  int nScratch;                     /* Number of scratch buffers */
 1.11563 +  void *pPage;                      /* Page cache memory */
 1.11564 +  int szPage;                       /* Size of each page in pPage[] */
 1.11565 +  int nPage;                        /* Number of pages in pPage[] */
 1.11566 +  int mxParserStack;                /* maximum depth of the parser stack */
 1.11567 +  int sharedCacheEnabled;           /* true if shared-cache mode enabled */
 1.11568 +  /* The above might be initialized to non-zero.  The following need to always
 1.11569 +  ** initially be zero, however. */
 1.11570 +  int isInit;                       /* True after initialization has finished */
 1.11571 +  int inProgress;                   /* True while initialization in progress */
 1.11572 +  int isMutexInit;                  /* True after mutexes are initialized */
 1.11573 +  int isMallocInit;                 /* True after malloc is initialized */
 1.11574 +  int isPCacheInit;                 /* True after malloc is initialized */
 1.11575 +  sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
 1.11576 +  int nRefInitMutex;                /* Number of users of pInitMutex */
 1.11577 +  void (*xLog)(void*,int,const char*); /* Function for logging */
 1.11578 +  void *pLogArg;                       /* First argument to xLog() */
 1.11579 +  int bLocaltimeFault;              /* True to fail localtime() calls */
 1.11580 +#ifdef SQLITE_ENABLE_SQLLOG
 1.11581 +  void(*xSqllog)(void*,sqlite3*,const char*, int);
 1.11582 +  void *pSqllogArg;
 1.11583 +#endif
 1.11584 +};
 1.11585 +
 1.11586 +/*
 1.11587 +** Context pointer passed down through the tree-walk.
 1.11588 +*/
 1.11589 +struct Walker {
 1.11590 +  int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
 1.11591 +  int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
 1.11592 +  Parse *pParse;                            /* Parser context.  */
 1.11593 +  int walkerDepth;                          /* Number of subqueries */
 1.11594 +  union {                                   /* Extra data for callback */
 1.11595 +    NameContext *pNC;                          /* Naming context */
 1.11596 +    int i;                                     /* Integer value */
 1.11597 +    SrcList *pSrcList;                         /* FROM clause */
 1.11598 +    struct SrcCount *pSrcCount;                /* Counting column references */
 1.11599 +  } u;
 1.11600 +};
 1.11601 +
 1.11602 +/* Forward declarations */
 1.11603 +SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 1.11604 +SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 1.11605 +SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
 1.11606 +SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
 1.11607 +SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
 1.11608 +
 1.11609 +/*
 1.11610 +** Return code from the parse-tree walking primitives and their
 1.11611 +** callbacks.
 1.11612 +*/
 1.11613 +#define WRC_Continue    0   /* Continue down into children */
 1.11614 +#define WRC_Prune       1   /* Omit children but continue walking siblings */
 1.11615 +#define WRC_Abort       2   /* Abandon the tree walk */
 1.11616 +
 1.11617 +/*
 1.11618 +** Assuming zIn points to the first byte of a UTF-8 character,
 1.11619 +** advance zIn to point to the first byte of the next UTF-8 character.
 1.11620 +*/
 1.11621 +#define SQLITE_SKIP_UTF8(zIn) {                        \
 1.11622 +  if( (*(zIn++))>=0xc0 ){                              \
 1.11623 +    while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
 1.11624 +  }                                                    \
 1.11625 +}
 1.11626 +
 1.11627 +/*
 1.11628 +** The SQLITE_*_BKPT macros are substitutes for the error codes with
 1.11629 +** the same name but without the _BKPT suffix.  These macros invoke
 1.11630 +** routines that report the line-number on which the error originated
 1.11631 +** using sqlite3_log().  The routines also provide a convenient place
 1.11632 +** to set a debugger breakpoint.
 1.11633 +*/
 1.11634 +SQLITE_PRIVATE int sqlite3CorruptError(int);
 1.11635 +SQLITE_PRIVATE int sqlite3MisuseError(int);
 1.11636 +SQLITE_PRIVATE int sqlite3CantopenError(int);
 1.11637 +#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
 1.11638 +#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
 1.11639 +#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
 1.11640 +
 1.11641 +
 1.11642 +/*
 1.11643 +** FTS4 is really an extension for FTS3.  It is enabled using the
 1.11644 +** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
 1.11645 +** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
 1.11646 +*/
 1.11647 +#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
 1.11648 +# define SQLITE_ENABLE_FTS3
 1.11649 +#endif
 1.11650 +
 1.11651 +/*
 1.11652 +** The ctype.h header is needed for non-ASCII systems.  It is also
 1.11653 +** needed by FTS3 when FTS3 is included in the amalgamation.
 1.11654 +*/
 1.11655 +#if !defined(SQLITE_ASCII) || \
 1.11656 +    (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
 1.11657 +# include <ctype.h>
 1.11658 +#endif
 1.11659 +
 1.11660 +/*
 1.11661 +** The following macros mimic the standard library functions toupper(),
 1.11662 +** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
 1.11663 +** sqlite versions only work for ASCII characters, regardless of locale.
 1.11664 +*/
 1.11665 +#ifdef SQLITE_ASCII
 1.11666 +# define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
 1.11667 +# define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
 1.11668 +# define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
 1.11669 +# define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
 1.11670 +# define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
 1.11671 +# define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
 1.11672 +# define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
 1.11673 +#else
 1.11674 +# define sqlite3Toupper(x)   toupper((unsigned char)(x))
 1.11675 +# define sqlite3Isspace(x)   isspace((unsigned char)(x))
 1.11676 +# define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
 1.11677 +# define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
 1.11678 +# define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
 1.11679 +# define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
 1.11680 +# define sqlite3Tolower(x)   tolower((unsigned char)(x))
 1.11681 +#endif
 1.11682 +
 1.11683 +/*
 1.11684 +** Internal function prototypes
 1.11685 +*/
 1.11686 +#define sqlite3StrICmp sqlite3_stricmp
 1.11687 +SQLITE_PRIVATE int sqlite3Strlen30(const char*);
 1.11688 +#define sqlite3StrNICmp sqlite3_strnicmp
 1.11689 +
 1.11690 +SQLITE_PRIVATE int sqlite3MallocInit(void);
 1.11691 +SQLITE_PRIVATE void sqlite3MallocEnd(void);
 1.11692 +SQLITE_PRIVATE void *sqlite3Malloc(int);
 1.11693 +SQLITE_PRIVATE void *sqlite3MallocZero(int);
 1.11694 +SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
 1.11695 +SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
 1.11696 +SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
 1.11697 +SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
 1.11698 +SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
 1.11699 +SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
 1.11700 +SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
 1.11701 +SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
 1.11702 +SQLITE_PRIVATE int sqlite3MallocSize(void*);
 1.11703 +SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
 1.11704 +SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
 1.11705 +SQLITE_PRIVATE void sqlite3ScratchFree(void*);
 1.11706 +SQLITE_PRIVATE void *sqlite3PageMalloc(int);
 1.11707 +SQLITE_PRIVATE void sqlite3PageFree(void*);
 1.11708 +SQLITE_PRIVATE void sqlite3MemSetDefault(void);
 1.11709 +SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 1.11710 +SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
 1.11711 +
 1.11712 +/*
 1.11713 +** On systems with ample stack space and that support alloca(), make
 1.11714 +** use of alloca() to obtain space for large automatic objects.  By default,
 1.11715 +** obtain space from malloc().
 1.11716 +**
 1.11717 +** The alloca() routine never returns NULL.  This will cause code paths
 1.11718 +** that deal with sqlite3StackAlloc() failures to be unreachable.
 1.11719 +*/
 1.11720 +#ifdef SQLITE_USE_ALLOCA
 1.11721 +# define sqlite3StackAllocRaw(D,N)   alloca(N)
 1.11722 +# define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
 1.11723 +# define sqlite3StackFree(D,P)       
 1.11724 +#else
 1.11725 +# define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
 1.11726 +# define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
 1.11727 +# define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
 1.11728 +#endif
 1.11729 +
 1.11730 +#ifdef SQLITE_ENABLE_MEMSYS3
 1.11731 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
 1.11732 +#endif
 1.11733 +#ifdef SQLITE_ENABLE_MEMSYS5
 1.11734 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
 1.11735 +#endif
 1.11736 +
 1.11737 +
 1.11738 +#ifndef SQLITE_MUTEX_OMIT
 1.11739 +SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
 1.11740 +SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
 1.11741 +SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
 1.11742 +SQLITE_PRIVATE   int sqlite3MutexInit(void);
 1.11743 +SQLITE_PRIVATE   int sqlite3MutexEnd(void);
 1.11744 +#endif
 1.11745 +
 1.11746 +SQLITE_PRIVATE int sqlite3StatusValue(int);
 1.11747 +SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
 1.11748 +SQLITE_PRIVATE void sqlite3StatusSet(int, int);
 1.11749 +
 1.11750 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.11751 +SQLITE_PRIVATE   int sqlite3IsNaN(double);
 1.11752 +#else
 1.11753 +# define sqlite3IsNaN(X)  0
 1.11754 +#endif
 1.11755 +
 1.11756 +SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
 1.11757 +#ifndef SQLITE_OMIT_TRACE
 1.11758 +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
 1.11759 +#endif
 1.11760 +SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
 1.11761 +SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 1.11762 +SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
 1.11763 +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
 1.11764 +SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 1.11765 +#endif
 1.11766 +#if defined(SQLITE_TEST)
 1.11767 +SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 1.11768 +#endif
 1.11769 +
 1.11770 +/* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
 1.11771 +#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 1.11772 +SQLITE_PRIVATE   void sqlite3ExplainBegin(Vdbe*);
 1.11773 +SQLITE_PRIVATE   void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
 1.11774 +SQLITE_PRIVATE   void sqlite3ExplainNL(Vdbe*);
 1.11775 +SQLITE_PRIVATE   void sqlite3ExplainPush(Vdbe*);
 1.11776 +SQLITE_PRIVATE   void sqlite3ExplainPop(Vdbe*);
 1.11777 +SQLITE_PRIVATE   void sqlite3ExplainFinish(Vdbe*);
 1.11778 +SQLITE_PRIVATE   void sqlite3ExplainSelect(Vdbe*, Select*);
 1.11779 +SQLITE_PRIVATE   void sqlite3ExplainExpr(Vdbe*, Expr*);
 1.11780 +SQLITE_PRIVATE   void sqlite3ExplainExprList(Vdbe*, ExprList*);
 1.11781 +SQLITE_PRIVATE   const char *sqlite3VdbeExplanation(Vdbe*);
 1.11782 +#else
 1.11783 +# define sqlite3ExplainBegin(X)
 1.11784 +# define sqlite3ExplainSelect(A,B)
 1.11785 +# define sqlite3ExplainExpr(A,B)
 1.11786 +# define sqlite3ExplainExprList(A,B)
 1.11787 +# define sqlite3ExplainFinish(X)
 1.11788 +# define sqlite3VdbeExplanation(X) 0
 1.11789 +#endif
 1.11790 +
 1.11791 +
 1.11792 +SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
 1.11793 +SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
 1.11794 +SQLITE_PRIVATE int sqlite3Dequote(char*);
 1.11795 +SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
 1.11796 +SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
 1.11797 +SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
 1.11798 +SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
 1.11799 +SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
 1.11800 +SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
 1.11801 +SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
 1.11802 +SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
 1.11803 +SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
 1.11804 +SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
 1.11805 +SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
 1.11806 +SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
 1.11807 +SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
 1.11808 +SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
 1.11809 +SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
 1.11810 +SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
 1.11811 +SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
 1.11812 +SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
 1.11813 +SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
 1.11814 +SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
 1.11815 +SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
 1.11816 +SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
 1.11817 +SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
 1.11818 +SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
 1.11819 +SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
 1.11820 +SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
 1.11821 +SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
 1.11822 +SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
 1.11823 +SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
 1.11824 +SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
 1.11825 +SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
 1.11826 +SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
 1.11827 +SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
 1.11828 +SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
 1.11829 +SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
 1.11830 +SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
 1.11831 +SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
 1.11832 +SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
 1.11833 +SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
 1.11834 +SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
 1.11835 +                    sqlite3_vfs**,char**,char **);
 1.11836 +SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
 1.11837 +SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
 1.11838 +
 1.11839 +SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
 1.11840 +SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
 1.11841 +SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
 1.11842 +SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
 1.11843 +SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
 1.11844 +SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
 1.11845 +SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
 1.11846 +
 1.11847 +SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
 1.11848 +SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
 1.11849 +SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
 1.11850 +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
 1.11851 +SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
 1.11852 +
 1.11853 +SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
 1.11854 +
 1.11855 +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 1.11856 +SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
 1.11857 +#else
 1.11858 +# define sqlite3ViewGetColumnNames(A,B) 0
 1.11859 +#endif
 1.11860 +
 1.11861 +SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 1.11862 +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
 1.11863 +SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
 1.11864 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.11865 +SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
 1.11866 +SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
 1.11867 +#else
 1.11868 +# define sqlite3AutoincrementBegin(X)
 1.11869 +# define sqlite3AutoincrementEnd(X)
 1.11870 +#endif
 1.11871 +SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
 1.11872 +SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
 1.11873 +SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
 1.11874 +SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
 1.11875 +SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
 1.11876 +SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
 1.11877 +SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
 1.11878 +SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
 1.11879 +                                      Token*, Select*, Expr*, IdList*);
 1.11880 +SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
 1.11881 +SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
 1.11882 +SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
 1.11883 +SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
 1.11884 +SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
 1.11885 +SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 1.11886 +SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 1.11887 +                        Token*, int, int);
 1.11888 +SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 1.11889 +SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 1.11890 +SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 1.11891 +                         Expr*,ExprList*,int,Expr*,Expr*);
 1.11892 +SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 1.11893 +SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 1.11894 +SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 1.11895 +SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 1.11896 +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 1.11897 +SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
 1.11898 +#endif
 1.11899 +SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 1.11900 +SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
 1.11901 +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 1.11902 +SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 1.11903 +SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
 1.11904 +SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
 1.11905 +SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 1.11906 +SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
 1.11907 +SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
 1.11908 +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
 1.11909 +SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
 1.11910 +SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
 1.11911 +SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
 1.11912 +SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
 1.11913 +SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
 1.11914 +SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
 1.11915 +SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
 1.11916 +SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
 1.11917 +SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
 1.11918 +SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
 1.11919 +SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
 1.11920 +SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
 1.11921 +SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
 1.11922 +SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
 1.11923 +SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
 1.11924 +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 1.11925 +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 1.11926 +SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
 1.11927 +SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
 1.11928 +SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 1.11929 +SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
 1.11930 +SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
 1.11931 +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 1.11932 +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 1.11933 +SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 1.11934 +SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 1.11935 +SQLITE_PRIVATE void sqlite3PrngSaveState(void);
 1.11936 +SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
 1.11937 +SQLITE_PRIVATE void sqlite3PrngResetState(void);
 1.11938 +SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
 1.11939 +SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 1.11940 +SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 1.11941 +SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 1.11942 +SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
 1.11943 +SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
 1.11944 +SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 1.11945 +SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 1.11946 +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
 1.11947 +SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 1.11948 +SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 1.11949 +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
 1.11950 +SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 1.11951 +SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 1.11952 +SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
 1.11953 +SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 1.11954 +SQLITE_PRIVATE int sqlite3IsRowid(const char*);
 1.11955 +SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
 1.11956 +SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
 1.11957 +SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
 1.11958 +SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
 1.11959 +                                     int*,int,int,int,int,int*);
 1.11960 +SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
 1.11961 +SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
 1.11962 +SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
 1.11963 +SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
 1.11964 +SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
 1.11965 +SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
 1.11966 +SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 1.11967 +SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 1.11968 +SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 1.11969 +SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 1.11970 +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
 1.11971 +SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
 1.11972 +SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
 1.11973 +SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
 1.11974 +SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 1.11975 +SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
 1.11976 +SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 1.11977 +SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
 1.11978 +SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
 1.11979 +
 1.11980 +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 1.11981 +SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
 1.11982 +#endif
 1.11983 +
 1.11984 +#ifndef SQLITE_OMIT_TRIGGER
 1.11985 +SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
 1.11986 +                           Expr*,int, int);
 1.11987 +SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
 1.11988 +SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
 1.11989 +SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
 1.11990 +SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
 1.11991 +SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
 1.11992 +SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
 1.11993 +                            int, int, int);
 1.11994 +SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
 1.11995 +  void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 1.11996 +SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
 1.11997 +SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
 1.11998 +SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
 1.11999 +                                        ExprList*,Select*,u8);
 1.12000 +SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
 1.12001 +SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
 1.12002 +SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 1.12003 +SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 1.12004 +SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
 1.12005 +# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
 1.12006 +#else
 1.12007 +# define sqlite3TriggersExist(B,C,D,E,F) 0
 1.12008 +# define sqlite3DeleteTrigger(A,B)
 1.12009 +# define sqlite3DropTriggerPtr(A,B)
 1.12010 +# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
 1.12011 +# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
 1.12012 +# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
 1.12013 +# define sqlite3TriggerList(X, Y) 0
 1.12014 +# define sqlite3ParseToplevel(p) p
 1.12015 +# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
 1.12016 +#endif
 1.12017 +
 1.12018 +SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
 1.12019 +SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
 1.12020 +SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
 1.12021 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.12022 +SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
 1.12023 +SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
 1.12024 +SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
 1.12025 +SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
 1.12026 +SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
 1.12027 +#else
 1.12028 +# define sqlite3AuthRead(a,b,c,d)
 1.12029 +# define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
 1.12030 +# define sqlite3AuthContextPush(a,b,c)
 1.12031 +# define sqlite3AuthContextPop(a)  ((void)(a))
 1.12032 +#endif
 1.12033 +SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
 1.12034 +SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
 1.12035 +SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
 1.12036 +SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
 1.12037 +SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
 1.12038 +SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
 1.12039 +SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
 1.12040 +SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
 1.12041 +SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
 1.12042 +SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
 1.12043 +SQLITE_PRIVATE int sqlite3Atoi(const char*);
 1.12044 +SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
 1.12045 +SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
 1.12046 +SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
 1.12047 +
 1.12048 +/*
 1.12049 +** Routines to read and write variable-length integers.  These used to
 1.12050 +** be defined locally, but now we use the varint routines in the util.c
 1.12051 +** file.  Code should use the MACRO forms below, as the Varint32 versions
 1.12052 +** are coded to assume the single byte case is already handled (which 
 1.12053 +** the MACRO form does).
 1.12054 +*/
 1.12055 +SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
 1.12056 +SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
 1.12057 +SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
 1.12058 +SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
 1.12059 +SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
 1.12060 +
 1.12061 +/*
 1.12062 +** The header of a record consists of a sequence variable-length integers.
 1.12063 +** These integers are almost always small and are encoded as a single byte.
 1.12064 +** The following macros take advantage this fact to provide a fast encode
 1.12065 +** and decode of the integers in a record header.  It is faster for the common
 1.12066 +** case where the integer is a single byte.  It is a little slower when the
 1.12067 +** integer is two or more bytes.  But overall it is faster.
 1.12068 +**
 1.12069 +** The following expressions are equivalent:
 1.12070 +**
 1.12071 +**     x = sqlite3GetVarint32( A, &B );
 1.12072 +**     x = sqlite3PutVarint32( A, B );
 1.12073 +**
 1.12074 +**     x = getVarint32( A, B );
 1.12075 +**     x = putVarint32( A, B );
 1.12076 +**
 1.12077 +*/
 1.12078 +#define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
 1.12079 +#define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
 1.12080 +#define getVarint    sqlite3GetVarint
 1.12081 +#define putVarint    sqlite3PutVarint
 1.12082 +
 1.12083 +
 1.12084 +SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
 1.12085 +SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
 1.12086 +SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
 1.12087 +SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
 1.12088 +SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
 1.12089 +SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
 1.12090 +SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
 1.12091 +SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 1.12092 +SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 1.12093 +SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 1.12094 +SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 1.12095 +SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 1.12096 +SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 1.12097 +SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 1.12098 +SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 1.12099 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
 1.12100 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 1.12101 +SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 1.12102 +SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 1.12103 +SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 1.12104 +SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 1.12105 +SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 1.12106 +SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
 1.12107 +SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
 1.12108 +SQLITE_PRIVATE int sqlite3AbsInt32(int);
 1.12109 +#ifdef SQLITE_ENABLE_8_3_NAMES
 1.12110 +SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
 1.12111 +#else
 1.12112 +# define sqlite3FileSuffix3(X,Y)
 1.12113 +#endif
 1.12114 +SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
 1.12115 +
 1.12116 +SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
 1.12117 +SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
 1.12118 +SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
 1.12119 +                        void(*)(void*));
 1.12120 +SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
 1.12121 +SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
 1.12122 +SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
 1.12123 +#ifdef SQLITE_ENABLE_STAT3
 1.12124 +SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
 1.12125 +#endif
 1.12126 +SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
 1.12127 +SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
 1.12128 +#ifndef SQLITE_AMALGAMATION
 1.12129 +SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
 1.12130 +SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
 1.12131 +SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
 1.12132 +SQLITE_PRIVATE const Token sqlite3IntTokens[];
 1.12133 +SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 1.12134 +SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 1.12135 +#ifndef SQLITE_OMIT_WSD
 1.12136 +SQLITE_PRIVATE int sqlite3PendingByte;
 1.12137 +#endif
 1.12138 +#endif
 1.12139 +SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 1.12140 +SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 1.12141 +SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 1.12142 +SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 1.12143 +SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 1.12144 +SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 1.12145 +SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
 1.12146 +SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
 1.12147 +SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 1.12148 +SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 1.12149 +SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 1.12150 +SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 1.12151 +SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 1.12152 +SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 1.12153 +SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 1.12154 +SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 1.12155 +SQLITE_PRIVATE char sqlite3AffinityType(const char*);
 1.12156 +SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 1.12157 +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
 1.12158 +SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 1.12159 +SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 1.12160 +SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 1.12161 +SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 1.12162 +SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 1.12163 +SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 1.12164 +SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
 1.12165 +SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
 1.12166 +SQLITE_PRIVATE void sqlite3SchemaClear(void *);
 1.12167 +SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
 1.12168 +SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
 1.12169 +SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
 1.12170 +SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
 1.12171 +  void (*)(sqlite3_context*,int,sqlite3_value **),
 1.12172 +  void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
 1.12173 +  FuncDestructor *pDestructor
 1.12174 +);
 1.12175 +SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
 1.12176 +SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
 1.12177 +
 1.12178 +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
 1.12179 +SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 1.12180 +SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
 1.12181 +SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 1.12182 +SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 1.12183 +SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 1.12184 +SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 1.12185 +
 1.12186 +SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
 1.12187 +SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
 1.12188 +
 1.12189 +/*
 1.12190 +** The interface to the LEMON-generated parser
 1.12191 +*/
 1.12192 +SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
 1.12193 +SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
 1.12194 +SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
 1.12195 +#ifdef YYTRACKMAXSTACKDEPTH
 1.12196 +SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
 1.12197 +#endif
 1.12198 +
 1.12199 +SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
 1.12200 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
 1.12201 +SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
 1.12202 +#else
 1.12203 +# define sqlite3CloseExtensions(X)
 1.12204 +#endif
 1.12205 +
 1.12206 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.12207 +SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
 1.12208 +#else
 1.12209 +  #define sqlite3TableLock(v,w,x,y,z)
 1.12210 +#endif
 1.12211 +
 1.12212 +#ifdef SQLITE_TEST
 1.12213 +SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
 1.12214 +#endif
 1.12215 +
 1.12216 +#ifdef SQLITE_OMIT_VIRTUALTABLE
 1.12217 +#  define sqlite3VtabClear(Y)
 1.12218 +#  define sqlite3VtabSync(X,Y) SQLITE_OK
 1.12219 +#  define sqlite3VtabRollback(X)
 1.12220 +#  define sqlite3VtabCommit(X)
 1.12221 +#  define sqlite3VtabInSync(db) 0
 1.12222 +#  define sqlite3VtabLock(X) 
 1.12223 +#  define sqlite3VtabUnlock(X)
 1.12224 +#  define sqlite3VtabUnlockList(X)
 1.12225 +#  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
 1.12226 +#  define sqlite3GetVTable(X,Y)  ((VTable*)0)
 1.12227 +#else
 1.12228 +SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
 1.12229 +SQLITE_PRIVATE    void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
 1.12230 +SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
 1.12231 +SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
 1.12232 +SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
 1.12233 +SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
 1.12234 +SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
 1.12235 +SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
 1.12236 +SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
 1.12237 +SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
 1.12238 +#  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
 1.12239 +#endif
 1.12240 +SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
 1.12241 +SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
 1.12242 +SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
 1.12243 +SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
 1.12244 +SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
 1.12245 +SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
 1.12246 +SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
 1.12247 +SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 1.12248 +SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 1.12249 +SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 1.12250 +SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
 1.12251 +SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 1.12252 +SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 1.12253 +SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 1.12254 +SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 1.12255 +SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 1.12256 +SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 1.12257 +SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 1.12258 +#ifndef SQLITE_OMIT_WAL
 1.12259 +SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
 1.12260 +SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
 1.12261 +#endif
 1.12262 +
 1.12263 +/* Declarations for functions in fkey.c. All of these are replaced by
 1.12264 +** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
 1.12265 +** key functionality is available. If OMIT_TRIGGER is defined but
 1.12266 +** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
 1.12267 +** this case foreign keys are parsed, but no other functionality is 
 1.12268 +** provided (enforcement of FK constraints requires the triggers sub-system).
 1.12269 +*/
 1.12270 +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 1.12271 +SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
 1.12272 +SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
 1.12273 +SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
 1.12274 +SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
 1.12275 +SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
 1.12276 +SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
 1.12277 +#else
 1.12278 +  #define sqlite3FkActions(a,b,c,d)
 1.12279 +  #define sqlite3FkCheck(a,b,c,d)
 1.12280 +  #define sqlite3FkDropTable(a,b,c)
 1.12281 +  #define sqlite3FkOldmask(a,b)      0
 1.12282 +  #define sqlite3FkRequired(a,b,c,d) 0
 1.12283 +#endif
 1.12284 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.12285 +SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
 1.12286 +#else
 1.12287 +  #define sqlite3FkDelete(a,b)
 1.12288 +#endif
 1.12289 +
 1.12290 +
 1.12291 +/*
 1.12292 +** Available fault injectors.  Should be numbered beginning with 0.
 1.12293 +*/
 1.12294 +#define SQLITE_FAULTINJECTOR_MALLOC     0
 1.12295 +#define SQLITE_FAULTINJECTOR_COUNT      1
 1.12296 +
 1.12297 +/*
 1.12298 +** The interface to the code in fault.c used for identifying "benign"
 1.12299 +** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
 1.12300 +** is not defined.
 1.12301 +*/
 1.12302 +#ifndef SQLITE_OMIT_BUILTIN_TEST
 1.12303 +SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
 1.12304 +SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
 1.12305 +#else
 1.12306 +  #define sqlite3BeginBenignMalloc()
 1.12307 +  #define sqlite3EndBenignMalloc()
 1.12308 +#endif
 1.12309 +
 1.12310 +#define IN_INDEX_ROWID           1
 1.12311 +#define IN_INDEX_EPH             2
 1.12312 +#define IN_INDEX_INDEX           3
 1.12313 +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
 1.12314 +
 1.12315 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
 1.12316 +SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
 1.12317 +SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
 1.12318 +SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
 1.12319 +SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
 1.12320 +#else
 1.12321 +  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
 1.12322 +  #define sqlite3JournalExists(p) 1
 1.12323 +#endif
 1.12324 +
 1.12325 +SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
 1.12326 +SQLITE_PRIVATE int sqlite3MemJournalSize(void);
 1.12327 +SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
 1.12328 +
 1.12329 +#if SQLITE_MAX_EXPR_DEPTH>0
 1.12330 +SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
 1.12331 +SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
 1.12332 +SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
 1.12333 +#else
 1.12334 +  #define sqlite3ExprSetHeight(x,y)
 1.12335 +  #define sqlite3SelectExprHeight(x) 0
 1.12336 +  #define sqlite3ExprCheckHeight(x,y)
 1.12337 +#endif
 1.12338 +
 1.12339 +SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
 1.12340 +SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
 1.12341 +
 1.12342 +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 1.12343 +SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
 1.12344 +SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
 1.12345 +SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
 1.12346 +#else
 1.12347 +  #define sqlite3ConnectionBlocked(x,y)
 1.12348 +  #define sqlite3ConnectionUnlocked(x)
 1.12349 +  #define sqlite3ConnectionClosed(x)
 1.12350 +#endif
 1.12351 +
 1.12352 +#ifdef SQLITE_DEBUG
 1.12353 +SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
 1.12354 +#endif
 1.12355 +
 1.12356 +/*
 1.12357 +** If the SQLITE_ENABLE IOTRACE exists then the global variable
 1.12358 +** sqlite3IoTrace is a pointer to a printf-like routine used to
 1.12359 +** print I/O tracing messages. 
 1.12360 +*/
 1.12361 +#ifdef SQLITE_ENABLE_IOTRACE
 1.12362 +# define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
 1.12363 +SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
 1.12364 +SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
 1.12365 +#else
 1.12366 +# define IOTRACE(A)
 1.12367 +# define sqlite3VdbeIOTraceSql(X)
 1.12368 +#endif
 1.12369 +
 1.12370 +/*
 1.12371 +** These routines are available for the mem2.c debugging memory allocator
 1.12372 +** only.  They are used to verify that different "types" of memory
 1.12373 +** allocations are properly tracked by the system.
 1.12374 +**
 1.12375 +** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
 1.12376 +** the MEMTYPE_* macros defined below.  The type must be a bitmask with
 1.12377 +** a single bit set.
 1.12378 +**
 1.12379 +** sqlite3MemdebugHasType() returns true if any of the bits in its second
 1.12380 +** argument match the type set by the previous sqlite3MemdebugSetType().
 1.12381 +** sqlite3MemdebugHasType() is intended for use inside assert() statements.
 1.12382 +**
 1.12383 +** sqlite3MemdebugNoType() returns true if none of the bits in its second
 1.12384 +** argument match the type set by the previous sqlite3MemdebugSetType().
 1.12385 +**
 1.12386 +** Perhaps the most important point is the difference between MEMTYPE_HEAP
 1.12387 +** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
 1.12388 +** it might have been allocated by lookaside, except the allocation was
 1.12389 +** too large or lookaside was already full.  It is important to verify
 1.12390 +** that allocations that might have been satisfied by lookaside are not
 1.12391 +** passed back to non-lookaside free() routines.  Asserts such as the
 1.12392 +** example above are placed on the non-lookaside free() routines to verify
 1.12393 +** this constraint. 
 1.12394 +**
 1.12395 +** All of this is no-op for a production build.  It only comes into
 1.12396 +** play when the SQLITE_MEMDEBUG compile-time option is used.
 1.12397 +*/
 1.12398 +#ifdef SQLITE_MEMDEBUG
 1.12399 +SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
 1.12400 +SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
 1.12401 +SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
 1.12402 +#else
 1.12403 +# define sqlite3MemdebugSetType(X,Y)  /* no-op */
 1.12404 +# define sqlite3MemdebugHasType(X,Y)  1
 1.12405 +# define sqlite3MemdebugNoType(X,Y)   1
 1.12406 +#endif
 1.12407 +#define MEMTYPE_HEAP       0x01  /* General heap allocations */
 1.12408 +#define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
 1.12409 +#define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
 1.12410 +#define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
 1.12411 +#define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
 1.12412 +
 1.12413 +#endif /* _SQLITEINT_H_ */
 1.12414 +
 1.12415 +/************** End of sqliteInt.h *******************************************/
 1.12416 +/************** Begin file global.c ******************************************/
 1.12417 +/*
 1.12418 +** 2008 June 13
 1.12419 +**
 1.12420 +** The author disclaims copyright to this source code.  In place of
 1.12421 +** a legal notice, here is a blessing:
 1.12422 +**
 1.12423 +**    May you do good and not evil.
 1.12424 +**    May you find forgiveness for yourself and forgive others.
 1.12425 +**    May you share freely, never taking more than you give.
 1.12426 +**
 1.12427 +*************************************************************************
 1.12428 +**
 1.12429 +** This file contains definitions of global variables and contants.
 1.12430 +*/
 1.12431 +
 1.12432 +/* An array to map all upper-case characters into their corresponding
 1.12433 +** lower-case character. 
 1.12434 +**
 1.12435 +** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
 1.12436 +** handle case conversions for the UTF character set since the tables
 1.12437 +** involved are nearly as big or bigger than SQLite itself.
 1.12438 +*/
 1.12439 +SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
 1.12440 +#ifdef SQLITE_ASCII
 1.12441 +      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
 1.12442 +     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
 1.12443 +     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
 1.12444 +     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
 1.12445 +    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
 1.12446 +    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
 1.12447 +    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
 1.12448 +    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 1.12449 +    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
 1.12450 +    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
 1.12451 +    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
 1.12452 +    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
 1.12453 +    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
 1.12454 +    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
 1.12455 +    252,253,254,255
 1.12456 +#endif
 1.12457 +#ifdef SQLITE_EBCDIC
 1.12458 +      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
 1.12459 +     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
 1.12460 +     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
 1.12461 +     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
 1.12462 +     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
 1.12463 +     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
 1.12464 +     96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
 1.12465 +    112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
 1.12466 +    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
 1.12467 +    144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
 1.12468 +    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
 1.12469 +    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
 1.12470 +    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
 1.12471 +    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
 1.12472 +    224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
 1.12473 +    239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
 1.12474 +#endif
 1.12475 +};
 1.12476 +
 1.12477 +/*
 1.12478 +** The following 256 byte lookup table is used to support SQLites built-in
 1.12479 +** equivalents to the following standard library functions:
 1.12480 +**
 1.12481 +**   isspace()                        0x01
 1.12482 +**   isalpha()                        0x02
 1.12483 +**   isdigit()                        0x04
 1.12484 +**   isalnum()                        0x06
 1.12485 +**   isxdigit()                       0x08
 1.12486 +**   toupper()                        0x20
 1.12487 +**   SQLite identifier character      0x40
 1.12488 +**
 1.12489 +** Bit 0x20 is set if the mapped character requires translation to upper
 1.12490 +** case. i.e. if the character is a lower-case ASCII character.
 1.12491 +** If x is a lower-case ASCII character, then its upper-case equivalent
 1.12492 +** is (x - 0x20). Therefore toupper() can be implemented as:
 1.12493 +**
 1.12494 +**   (x & ~(map[x]&0x20))
 1.12495 +**
 1.12496 +** Standard function tolower() is implemented using the sqlite3UpperToLower[]
 1.12497 +** array. tolower() is used more often than toupper() by SQLite.
 1.12498 +**
 1.12499 +** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
 1.12500 +** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
 1.12501 +** non-ASCII UTF character. Hence the test for whether or not a character is
 1.12502 +** part of an identifier is 0x46.
 1.12503 +**
 1.12504 +** SQLite's versions are identical to the standard versions assuming a
 1.12505 +** locale of "C". They are implemented as macros in sqliteInt.h.
 1.12506 +*/
 1.12507 +#ifdef SQLITE_ASCII
 1.12508 +SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
 1.12509 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
 1.12510 +  0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
 1.12511 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
 1.12512 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
 1.12513 +  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
 1.12514 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
 1.12515 +  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
 1.12516 +  0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
 1.12517 +
 1.12518 +  0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
 1.12519 +  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
 1.12520 +  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
 1.12521 +  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
 1.12522 +  0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
 1.12523 +  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
 1.12524 +  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
 1.12525 +  0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
 1.12526 +
 1.12527 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
 1.12528 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
 1.12529 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
 1.12530 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
 1.12531 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
 1.12532 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
 1.12533 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
 1.12534 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
 1.12535 +
 1.12536 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
 1.12537 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
 1.12538 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
 1.12539 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
 1.12540 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
 1.12541 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
 1.12542 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
 1.12543 +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
 1.12544 +};
 1.12545 +#endif
 1.12546 +
 1.12547 +#ifndef SQLITE_USE_URI
 1.12548 +# define  SQLITE_USE_URI 0
 1.12549 +#endif
 1.12550 +
 1.12551 +#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
 1.12552 +# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
 1.12553 +#endif
 1.12554 +
 1.12555 +/*
 1.12556 +** The following singleton contains the global configuration for
 1.12557 +** the SQLite library.
 1.12558 +*/
 1.12559 +SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
 1.12560 +   SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
 1.12561 +   1,                         /* bCoreMutex */
 1.12562 +   SQLITE_THREADSAFE==1,      /* bFullMutex */
 1.12563 +   SQLITE_USE_URI,            /* bOpenUri */
 1.12564 +   SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
 1.12565 +   0x7ffffffe,                /* mxStrlen */
 1.12566 +   128,                       /* szLookaside */
 1.12567 +   500,                       /* nLookaside */
 1.12568 +   {0,0,0,0,0,0,0,0},         /* m */
 1.12569 +   {0,0,0,0,0,0,0,0,0},       /* mutex */
 1.12570 +   {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
 1.12571 +   (void*)0,                  /* pHeap */
 1.12572 +   0,                         /* nHeap */
 1.12573 +   0, 0,                      /* mnHeap, mxHeap */
 1.12574 +   (void*)0,                  /* pScratch */
 1.12575 +   0,                         /* szScratch */
 1.12576 +   0,                         /* nScratch */
 1.12577 +   (void*)0,                  /* pPage */
 1.12578 +   0,                         /* szPage */
 1.12579 +   0,                         /* nPage */
 1.12580 +   0,                         /* mxParserStack */
 1.12581 +   0,                         /* sharedCacheEnabled */
 1.12582 +   /* All the rest should always be initialized to zero */
 1.12583 +   0,                         /* isInit */
 1.12584 +   0,                         /* inProgress */
 1.12585 +   0,                         /* isMutexInit */
 1.12586 +   0,                         /* isMallocInit */
 1.12587 +   0,                         /* isPCacheInit */
 1.12588 +   0,                         /* pInitMutex */
 1.12589 +   0,                         /* nRefInitMutex */
 1.12590 +   0,                         /* xLog */
 1.12591 +   0,                         /* pLogArg */
 1.12592 +   0,                         /* bLocaltimeFault */
 1.12593 +#ifdef SQLITE_ENABLE_SQLLOG
 1.12594 +   0,                         /* xSqllog */
 1.12595 +   0                          /* pSqllogArg */
 1.12596 +#endif
 1.12597 +};
 1.12598 +
 1.12599 +
 1.12600 +/*
 1.12601 +** Hash table for global functions - functions common to all
 1.12602 +** database connections.  After initialization, this table is
 1.12603 +** read-only.
 1.12604 +*/
 1.12605 +SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 1.12606 +
 1.12607 +/*
 1.12608 +** Constant tokens for values 0 and 1.
 1.12609 +*/
 1.12610 +SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
 1.12611 +   { "0", 1 },
 1.12612 +   { "1", 1 }
 1.12613 +};
 1.12614 +
 1.12615 +
 1.12616 +/*
 1.12617 +** The value of the "pending" byte must be 0x40000000 (1 byte past the
 1.12618 +** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 1.12619 +** the database page that contains the pending byte.  It never attempts
 1.12620 +** to read or write that page.  The pending byte page is set assign
 1.12621 +** for use by the VFS layers as space for managing file locks.
 1.12622 +**
 1.12623 +** During testing, it is often desirable to move the pending byte to
 1.12624 +** a different position in the file.  This allows code that has to
 1.12625 +** deal with the pending byte to run on files that are much smaller
 1.12626 +** than 1 GiB.  The sqlite3_test_control() interface can be used to
 1.12627 +** move the pending byte.
 1.12628 +**
 1.12629 +** IMPORTANT:  Changing the pending byte to any value other than
 1.12630 +** 0x40000000 results in an incompatible database file format!
 1.12631 +** Changing the pending byte during operating results in undefined
 1.12632 +** and dileterious behavior.
 1.12633 +*/
 1.12634 +#ifndef SQLITE_OMIT_WSD
 1.12635 +SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
 1.12636 +#endif
 1.12637 +
 1.12638 +/*
 1.12639 +** Properties of opcodes.  The OPFLG_INITIALIZER macro is
 1.12640 +** created by mkopcodeh.awk during compilation.  Data is obtained
 1.12641 +** from the comments following the "case OP_xxxx:" statements in
 1.12642 +** the vdbe.c file.  
 1.12643 +*/
 1.12644 +SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
 1.12645 +
 1.12646 +/************** End of global.c **********************************************/
 1.12647 +/************** Begin file ctime.c *******************************************/
 1.12648 +/*
 1.12649 +** 2010 February 23
 1.12650 +**
 1.12651 +** The author disclaims copyright to this source code.  In place of
 1.12652 +** a legal notice, here is a blessing:
 1.12653 +**
 1.12654 +**    May you do good and not evil.
 1.12655 +**    May you find forgiveness for yourself and forgive others.
 1.12656 +**    May you share freely, never taking more than you give.
 1.12657 +**
 1.12658 +*************************************************************************
 1.12659 +**
 1.12660 +** This file implements routines used to report what compile-time options
 1.12661 +** SQLite was built with.
 1.12662 +*/
 1.12663 +
 1.12664 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 1.12665 +
 1.12666 +
 1.12667 +/*
 1.12668 +** An array of names of all compile-time options.  This array should 
 1.12669 +** be sorted A-Z.
 1.12670 +**
 1.12671 +** This array looks large, but in a typical installation actually uses
 1.12672 +** only a handful of compile-time options, so most times this array is usually
 1.12673 +** rather short and uses little memory space.
 1.12674 +*/
 1.12675 +static const char * const azCompileOpt[] = {
 1.12676 +
 1.12677 +/* These macros are provided to "stringify" the value of the define
 1.12678 +** for those options in which the value is meaningful. */
 1.12679 +#define CTIMEOPT_VAL_(opt) #opt
 1.12680 +#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
 1.12681 +
 1.12682 +#ifdef SQLITE_32BIT_ROWID
 1.12683 +  "32BIT_ROWID",
 1.12684 +#endif
 1.12685 +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
 1.12686 +  "4_BYTE_ALIGNED_MALLOC",
 1.12687 +#endif
 1.12688 +#ifdef SQLITE_CASE_SENSITIVE_LIKE
 1.12689 +  "CASE_SENSITIVE_LIKE",
 1.12690 +#endif
 1.12691 +#ifdef SQLITE_CHECK_PAGES
 1.12692 +  "CHECK_PAGES",
 1.12693 +#endif
 1.12694 +#ifdef SQLITE_COVERAGE_TEST
 1.12695 +  "COVERAGE_TEST",
 1.12696 +#endif
 1.12697 +#ifdef SQLITE_CURDIR
 1.12698 +  "CURDIR",
 1.12699 +#endif
 1.12700 +#ifdef SQLITE_DEBUG
 1.12701 +  "DEBUG",
 1.12702 +#endif
 1.12703 +#ifdef SQLITE_DEFAULT_LOCKING_MODE
 1.12704 +  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
 1.12705 +#endif
 1.12706 +#ifdef SQLITE_DISABLE_DIRSYNC
 1.12707 +  "DISABLE_DIRSYNC",
 1.12708 +#endif
 1.12709 +#ifdef SQLITE_DISABLE_LFS
 1.12710 +  "DISABLE_LFS",
 1.12711 +#endif
 1.12712 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
 1.12713 +  "ENABLE_ATOMIC_WRITE",
 1.12714 +#endif
 1.12715 +#ifdef SQLITE_ENABLE_CEROD
 1.12716 +  "ENABLE_CEROD",
 1.12717 +#endif
 1.12718 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
 1.12719 +  "ENABLE_COLUMN_METADATA",
 1.12720 +#endif
 1.12721 +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 1.12722 +  "ENABLE_EXPENSIVE_ASSERT",
 1.12723 +#endif
 1.12724 +#ifdef SQLITE_ENABLE_FTS1
 1.12725 +  "ENABLE_FTS1",
 1.12726 +#endif
 1.12727 +#ifdef SQLITE_ENABLE_FTS2
 1.12728 +  "ENABLE_FTS2",
 1.12729 +#endif
 1.12730 +#ifdef SQLITE_ENABLE_FTS3
 1.12731 +  "ENABLE_FTS3",
 1.12732 +#endif
 1.12733 +#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
 1.12734 +  "ENABLE_FTS3_PARENTHESIS",
 1.12735 +#endif
 1.12736 +#ifdef SQLITE_ENABLE_FTS4
 1.12737 +  "ENABLE_FTS4",
 1.12738 +#endif
 1.12739 +#ifdef SQLITE_ENABLE_ICU
 1.12740 +  "ENABLE_ICU",
 1.12741 +#endif
 1.12742 +#ifdef SQLITE_ENABLE_IOTRACE
 1.12743 +  "ENABLE_IOTRACE",
 1.12744 +#endif
 1.12745 +#ifdef SQLITE_ENABLE_LOAD_EXTENSION
 1.12746 +  "ENABLE_LOAD_EXTENSION",
 1.12747 +#endif
 1.12748 +#ifdef SQLITE_ENABLE_LOCKING_STYLE
 1.12749 +  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
 1.12750 +#endif
 1.12751 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 1.12752 +  "ENABLE_MEMORY_MANAGEMENT",
 1.12753 +#endif
 1.12754 +#ifdef SQLITE_ENABLE_MEMSYS3
 1.12755 +  "ENABLE_MEMSYS3",
 1.12756 +#endif
 1.12757 +#ifdef SQLITE_ENABLE_MEMSYS5
 1.12758 +  "ENABLE_MEMSYS5",
 1.12759 +#endif
 1.12760 +#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
 1.12761 +  "ENABLE_OVERSIZE_CELL_CHECK",
 1.12762 +#endif
 1.12763 +#ifdef SQLITE_ENABLE_RTREE
 1.12764 +  "ENABLE_RTREE",
 1.12765 +#endif
 1.12766 +#ifdef SQLITE_ENABLE_STAT3
 1.12767 +  "ENABLE_STAT3",
 1.12768 +#endif
 1.12769 +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 1.12770 +  "ENABLE_UNLOCK_NOTIFY",
 1.12771 +#endif
 1.12772 +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
 1.12773 +  "ENABLE_UPDATE_DELETE_LIMIT",
 1.12774 +#endif
 1.12775 +#ifdef SQLITE_HAS_CODEC
 1.12776 +  "HAS_CODEC",
 1.12777 +#endif
 1.12778 +#ifdef SQLITE_HAVE_ISNAN
 1.12779 +  "HAVE_ISNAN",
 1.12780 +#endif
 1.12781 +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 1.12782 +  "HOMEGROWN_RECURSIVE_MUTEX",
 1.12783 +#endif
 1.12784 +#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
 1.12785 +  "IGNORE_AFP_LOCK_ERRORS",
 1.12786 +#endif
 1.12787 +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 1.12788 +  "IGNORE_FLOCK_LOCK_ERRORS",
 1.12789 +#endif
 1.12790 +#ifdef SQLITE_INT64_TYPE
 1.12791 +  "INT64_TYPE",
 1.12792 +#endif
 1.12793 +#ifdef SQLITE_LOCK_TRACE
 1.12794 +  "LOCK_TRACE",
 1.12795 +#endif
 1.12796 +#ifdef SQLITE_MAX_SCHEMA_RETRY
 1.12797 +  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
 1.12798 +#endif
 1.12799 +#ifdef SQLITE_MEMDEBUG
 1.12800 +  "MEMDEBUG",
 1.12801 +#endif
 1.12802 +#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 1.12803 +  "MIXED_ENDIAN_64BIT_FLOAT",
 1.12804 +#endif
 1.12805 +#ifdef SQLITE_NO_SYNC
 1.12806 +  "NO_SYNC",
 1.12807 +#endif
 1.12808 +#ifdef SQLITE_OMIT_ALTERTABLE
 1.12809 +  "OMIT_ALTERTABLE",
 1.12810 +#endif
 1.12811 +#ifdef SQLITE_OMIT_ANALYZE
 1.12812 +  "OMIT_ANALYZE",
 1.12813 +#endif
 1.12814 +#ifdef SQLITE_OMIT_ATTACH
 1.12815 +  "OMIT_ATTACH",
 1.12816 +#endif
 1.12817 +#ifdef SQLITE_OMIT_AUTHORIZATION
 1.12818 +  "OMIT_AUTHORIZATION",
 1.12819 +#endif
 1.12820 +#ifdef SQLITE_OMIT_AUTOINCREMENT
 1.12821 +  "OMIT_AUTOINCREMENT",
 1.12822 +#endif
 1.12823 +#ifdef SQLITE_OMIT_AUTOINIT
 1.12824 +  "OMIT_AUTOINIT",
 1.12825 +#endif
 1.12826 +#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
 1.12827 +  "OMIT_AUTOMATIC_INDEX",
 1.12828 +#endif
 1.12829 +#ifdef SQLITE_OMIT_AUTORESET
 1.12830 +  "OMIT_AUTORESET",
 1.12831 +#endif
 1.12832 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.12833 +  "OMIT_AUTOVACUUM",
 1.12834 +#endif
 1.12835 +#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
 1.12836 +  "OMIT_BETWEEN_OPTIMIZATION",
 1.12837 +#endif
 1.12838 +#ifdef SQLITE_OMIT_BLOB_LITERAL
 1.12839 +  "OMIT_BLOB_LITERAL",
 1.12840 +#endif
 1.12841 +#ifdef SQLITE_OMIT_BTREECOUNT
 1.12842 +  "OMIT_BTREECOUNT",
 1.12843 +#endif
 1.12844 +#ifdef SQLITE_OMIT_BUILTIN_TEST
 1.12845 +  "OMIT_BUILTIN_TEST",
 1.12846 +#endif
 1.12847 +#ifdef SQLITE_OMIT_CAST
 1.12848 +  "OMIT_CAST",
 1.12849 +#endif
 1.12850 +#ifdef SQLITE_OMIT_CHECK
 1.12851 +  "OMIT_CHECK",
 1.12852 +#endif
 1.12853 +/* // redundant
 1.12854 +** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
 1.12855 +**   "OMIT_COMPILEOPTION_DIAGS",
 1.12856 +** #endif
 1.12857 +*/
 1.12858 +#ifdef SQLITE_OMIT_COMPLETE
 1.12859 +  "OMIT_COMPLETE",
 1.12860 +#endif
 1.12861 +#ifdef SQLITE_OMIT_COMPOUND_SELECT
 1.12862 +  "OMIT_COMPOUND_SELECT",
 1.12863 +#endif
 1.12864 +#ifdef SQLITE_OMIT_DATETIME_FUNCS
 1.12865 +  "OMIT_DATETIME_FUNCS",
 1.12866 +#endif
 1.12867 +#ifdef SQLITE_OMIT_DECLTYPE
 1.12868 +  "OMIT_DECLTYPE",
 1.12869 +#endif
 1.12870 +#ifdef SQLITE_OMIT_DEPRECATED
 1.12871 +  "OMIT_DEPRECATED",
 1.12872 +#endif
 1.12873 +#ifdef SQLITE_OMIT_DISKIO
 1.12874 +  "OMIT_DISKIO",
 1.12875 +#endif
 1.12876 +#ifdef SQLITE_OMIT_EXPLAIN
 1.12877 +  "OMIT_EXPLAIN",
 1.12878 +#endif
 1.12879 +#ifdef SQLITE_OMIT_FLAG_PRAGMAS
 1.12880 +  "OMIT_FLAG_PRAGMAS",
 1.12881 +#endif
 1.12882 +#ifdef SQLITE_OMIT_FLOATING_POINT
 1.12883 +  "OMIT_FLOATING_POINT",
 1.12884 +#endif
 1.12885 +#ifdef SQLITE_OMIT_FOREIGN_KEY
 1.12886 +  "OMIT_FOREIGN_KEY",
 1.12887 +#endif
 1.12888 +#ifdef SQLITE_OMIT_GET_TABLE
 1.12889 +  "OMIT_GET_TABLE",
 1.12890 +#endif
 1.12891 +#ifdef SQLITE_OMIT_INCRBLOB
 1.12892 +  "OMIT_INCRBLOB",
 1.12893 +#endif
 1.12894 +#ifdef SQLITE_OMIT_INTEGRITY_CHECK
 1.12895 +  "OMIT_INTEGRITY_CHECK",
 1.12896 +#endif
 1.12897 +#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
 1.12898 +  "OMIT_LIKE_OPTIMIZATION",
 1.12899 +#endif
 1.12900 +#ifdef SQLITE_OMIT_LOAD_EXTENSION
 1.12901 +  "OMIT_LOAD_EXTENSION",
 1.12902 +#endif
 1.12903 +#ifdef SQLITE_OMIT_LOCALTIME
 1.12904 +  "OMIT_LOCALTIME",
 1.12905 +#endif
 1.12906 +#ifdef SQLITE_OMIT_LOOKASIDE
 1.12907 +  "OMIT_LOOKASIDE",
 1.12908 +#endif
 1.12909 +#ifdef SQLITE_OMIT_MEMORYDB
 1.12910 +  "OMIT_MEMORYDB",
 1.12911 +#endif
 1.12912 +#ifdef SQLITE_OMIT_MERGE_SORT
 1.12913 +  "OMIT_MERGE_SORT",
 1.12914 +#endif
 1.12915 +#ifdef SQLITE_OMIT_OR_OPTIMIZATION
 1.12916 +  "OMIT_OR_OPTIMIZATION",
 1.12917 +#endif
 1.12918 +#ifdef SQLITE_OMIT_PAGER_PRAGMAS
 1.12919 +  "OMIT_PAGER_PRAGMAS",
 1.12920 +#endif
 1.12921 +#ifdef SQLITE_OMIT_PRAGMA
 1.12922 +  "OMIT_PRAGMA",
 1.12923 +#endif
 1.12924 +#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
 1.12925 +  "OMIT_PROGRESS_CALLBACK",
 1.12926 +#endif
 1.12927 +#ifdef SQLITE_OMIT_QUICKBALANCE
 1.12928 +  "OMIT_QUICKBALANCE",
 1.12929 +#endif
 1.12930 +#ifdef SQLITE_OMIT_REINDEX
 1.12931 +  "OMIT_REINDEX",
 1.12932 +#endif
 1.12933 +#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
 1.12934 +  "OMIT_SCHEMA_PRAGMAS",
 1.12935 +#endif
 1.12936 +#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
 1.12937 +  "OMIT_SCHEMA_VERSION_PRAGMAS",
 1.12938 +#endif
 1.12939 +#ifdef SQLITE_OMIT_SHARED_CACHE
 1.12940 +  "OMIT_SHARED_CACHE",
 1.12941 +#endif
 1.12942 +#ifdef SQLITE_OMIT_SUBQUERY
 1.12943 +  "OMIT_SUBQUERY",
 1.12944 +#endif
 1.12945 +#ifdef SQLITE_OMIT_TCL_VARIABLE
 1.12946 +  "OMIT_TCL_VARIABLE",
 1.12947 +#endif
 1.12948 +#ifdef SQLITE_OMIT_TEMPDB
 1.12949 +  "OMIT_TEMPDB",
 1.12950 +#endif
 1.12951 +#ifdef SQLITE_OMIT_TRACE
 1.12952 +  "OMIT_TRACE",
 1.12953 +#endif
 1.12954 +#ifdef SQLITE_OMIT_TRIGGER
 1.12955 +  "OMIT_TRIGGER",
 1.12956 +#endif
 1.12957 +#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 1.12958 +  "OMIT_TRUNCATE_OPTIMIZATION",
 1.12959 +#endif
 1.12960 +#ifdef SQLITE_OMIT_UTF16
 1.12961 +  "OMIT_UTF16",
 1.12962 +#endif
 1.12963 +#ifdef SQLITE_OMIT_VACUUM
 1.12964 +  "OMIT_VACUUM",
 1.12965 +#endif
 1.12966 +#ifdef SQLITE_OMIT_VIEW
 1.12967 +  "OMIT_VIEW",
 1.12968 +#endif
 1.12969 +#ifdef SQLITE_OMIT_VIRTUALTABLE
 1.12970 +  "OMIT_VIRTUALTABLE",
 1.12971 +#endif
 1.12972 +#ifdef SQLITE_OMIT_WAL
 1.12973 +  "OMIT_WAL",
 1.12974 +#endif
 1.12975 +#ifdef SQLITE_OMIT_WSD
 1.12976 +  "OMIT_WSD",
 1.12977 +#endif
 1.12978 +#ifdef SQLITE_OMIT_XFER_OPT
 1.12979 +  "OMIT_XFER_OPT",
 1.12980 +#endif
 1.12981 +#ifdef SQLITE_PERFORMANCE_TRACE
 1.12982 +  "PERFORMANCE_TRACE",
 1.12983 +#endif
 1.12984 +#ifdef SQLITE_PROXY_DEBUG
 1.12985 +  "PROXY_DEBUG",
 1.12986 +#endif
 1.12987 +#ifdef SQLITE_RTREE_INT_ONLY
 1.12988 +  "RTREE_INT_ONLY",
 1.12989 +#endif
 1.12990 +#ifdef SQLITE_SECURE_DELETE
 1.12991 +  "SECURE_DELETE",
 1.12992 +#endif
 1.12993 +#ifdef SQLITE_SMALL_STACK
 1.12994 +  "SMALL_STACK",
 1.12995 +#endif
 1.12996 +#ifdef SQLITE_SOUNDEX
 1.12997 +  "SOUNDEX",
 1.12998 +#endif
 1.12999 +#ifdef SQLITE_TCL
 1.13000 +  "TCL",
 1.13001 +#endif
 1.13002 +#ifdef SQLITE_TEMP_STORE
 1.13003 +  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
 1.13004 +#endif
 1.13005 +#ifdef SQLITE_TEST
 1.13006 +  "TEST",
 1.13007 +#endif
 1.13008 +#ifdef SQLITE_THREADSAFE
 1.13009 +  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
 1.13010 +#endif
 1.13011 +#ifdef SQLITE_USE_ALLOCA
 1.13012 +  "USE_ALLOCA",
 1.13013 +#endif
 1.13014 +#ifdef SQLITE_ZERO_MALLOC
 1.13015 +  "ZERO_MALLOC"
 1.13016 +#endif
 1.13017 +};
 1.13018 +
 1.13019 +/*
 1.13020 +** Given the name of a compile-time option, return true if that option
 1.13021 +** was used and false if not.
 1.13022 +**
 1.13023 +** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
 1.13024 +** is not required for a match.
 1.13025 +*/
 1.13026 +SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
 1.13027 +  int i, n;
 1.13028 +  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
 1.13029 +  n = sqlite3Strlen30(zOptName);
 1.13030 +
 1.13031 +  /* Since ArraySize(azCompileOpt) is normally in single digits, a
 1.13032 +  ** linear search is adequate.  No need for a binary search. */
 1.13033 +  for(i=0; i<ArraySize(azCompileOpt); i++){
 1.13034 +    if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
 1.13035 +       && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
 1.13036 +  }
 1.13037 +  return 0;
 1.13038 +}
 1.13039 +
 1.13040 +/*
 1.13041 +** Return the N-th compile-time option string.  If N is out of range,
 1.13042 +** return a NULL pointer.
 1.13043 +*/
 1.13044 +SQLITE_API const char *sqlite3_compileoption_get(int N){
 1.13045 +  if( N>=0 && N<ArraySize(azCompileOpt) ){
 1.13046 +    return azCompileOpt[N];
 1.13047 +  }
 1.13048 +  return 0;
 1.13049 +}
 1.13050 +
 1.13051 +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 1.13052 +
 1.13053 +/************** End of ctime.c ***********************************************/
 1.13054 +/************** Begin file status.c ******************************************/
 1.13055 +/*
 1.13056 +** 2008 June 18
 1.13057 +**
 1.13058 +** The author disclaims copyright to this source code.  In place of
 1.13059 +** a legal notice, here is a blessing:
 1.13060 +**
 1.13061 +**    May you do good and not evil.
 1.13062 +**    May you find forgiveness for yourself and forgive others.
 1.13063 +**    May you share freely, never taking more than you give.
 1.13064 +**
 1.13065 +*************************************************************************
 1.13066 +**
 1.13067 +** This module implements the sqlite3_status() interface and related
 1.13068 +** functionality.
 1.13069 +*/
 1.13070 +/************** Include vdbeInt.h in the middle of status.c ******************/
 1.13071 +/************** Begin file vdbeInt.h *****************************************/
 1.13072 +/*
 1.13073 +** 2003 September 6
 1.13074 +**
 1.13075 +** The author disclaims copyright to this source code.  In place of
 1.13076 +** a legal notice, here is a blessing:
 1.13077 +**
 1.13078 +**    May you do good and not evil.
 1.13079 +**    May you find forgiveness for yourself and forgive others.
 1.13080 +**    May you share freely, never taking more than you give.
 1.13081 +**
 1.13082 +*************************************************************************
 1.13083 +** This is the header file for information that is private to the
 1.13084 +** VDBE.  This information used to all be at the top of the single
 1.13085 +** source code file "vdbe.c".  When that file became too big (over
 1.13086 +** 6000 lines long) it was split up into several smaller files and
 1.13087 +** this header information was factored out.
 1.13088 +*/
 1.13089 +#ifndef _VDBEINT_H_
 1.13090 +#define _VDBEINT_H_
 1.13091 +
 1.13092 +/*
 1.13093 +** SQL is translated into a sequence of instructions to be
 1.13094 +** executed by a virtual machine.  Each instruction is an instance
 1.13095 +** of the following structure.
 1.13096 +*/
 1.13097 +typedef struct VdbeOp Op;
 1.13098 +
 1.13099 +/*
 1.13100 +** Boolean values
 1.13101 +*/
 1.13102 +typedef unsigned char Bool;
 1.13103 +
 1.13104 +/* Opaque type used by code in vdbesort.c */
 1.13105 +typedef struct VdbeSorter VdbeSorter;
 1.13106 +
 1.13107 +/* Opaque type used by the explainer */
 1.13108 +typedef struct Explain Explain;
 1.13109 +
 1.13110 +/*
 1.13111 +** A cursor is a pointer into a single BTree within a database file.
 1.13112 +** The cursor can seek to a BTree entry with a particular key, or
 1.13113 +** loop over all entries of the Btree.  You can also insert new BTree
 1.13114 +** entries or retrieve the key or data from the entry that the cursor
 1.13115 +** is currently pointing to.
 1.13116 +** 
 1.13117 +** Every cursor that the virtual machine has open is represented by an
 1.13118 +** instance of the following structure.
 1.13119 +*/
 1.13120 +struct VdbeCursor {
 1.13121 +  BtCursor *pCursor;    /* The cursor structure of the backend */
 1.13122 +  Btree *pBt;           /* Separate file holding temporary table */
 1.13123 +  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
 1.13124 +  int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
 1.13125 +  int pseudoTableReg;   /* Register holding pseudotable content. */
 1.13126 +  int nField;           /* Number of fields in the header */
 1.13127 +  Bool zeroed;          /* True if zeroed out and ready for reuse */
 1.13128 +  Bool rowidIsValid;    /* True if lastRowid is valid */
 1.13129 +  Bool atFirst;         /* True if pointing to first entry */
 1.13130 +  Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
 1.13131 +  Bool nullRow;         /* True if pointing to a row with no data */
 1.13132 +  Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
 1.13133 +  Bool isTable;         /* True if a table requiring integer keys */
 1.13134 +  Bool isIndex;         /* True if an index containing keys only - no data */
 1.13135 +  Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
 1.13136 +  Bool isSorter;        /* True if a new-style sorter */
 1.13137 +  Bool multiPseudo;     /* Multi-register pseudo-cursor */
 1.13138 +  sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
 1.13139 +  const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
 1.13140 +  i64 seqCount;         /* Sequence counter */
 1.13141 +  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
 1.13142 +  i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
 1.13143 +  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
 1.13144 +
 1.13145 +  /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
 1.13146 +  ** OP_IsUnique opcode on this cursor. */
 1.13147 +  int seekResult;
 1.13148 +
 1.13149 +  /* Cached information about the header for the data record that the
 1.13150 +  ** cursor is currently pointing to.  Only valid if cacheStatus matches
 1.13151 +  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
 1.13152 +  ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
 1.13153 +  ** the cache is out of date.
 1.13154 +  **
 1.13155 +  ** aRow might point to (ephemeral) data for the current row, or it might
 1.13156 +  ** be NULL.
 1.13157 +  */
 1.13158 +  u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
 1.13159 +  int payloadSize;      /* Total number of bytes in the record */
 1.13160 +  u32 *aType;           /* Type values for all entries in the record */
 1.13161 +  u32 *aOffset;         /* Cached offsets to the start of each columns data */
 1.13162 +  u8 *aRow;             /* Data for the current row, if all on one page */
 1.13163 +};
 1.13164 +typedef struct VdbeCursor VdbeCursor;
 1.13165 +
 1.13166 +/*
 1.13167 +** When a sub-program is executed (OP_Program), a structure of this type
 1.13168 +** is allocated to store the current value of the program counter, as
 1.13169 +** well as the current memory cell array and various other frame specific
 1.13170 +** values stored in the Vdbe struct. When the sub-program is finished, 
 1.13171 +** these values are copied back to the Vdbe from the VdbeFrame structure,
 1.13172 +** restoring the state of the VM to as it was before the sub-program
 1.13173 +** began executing.
 1.13174 +**
 1.13175 +** The memory for a VdbeFrame object is allocated and managed by a memory
 1.13176 +** cell in the parent (calling) frame. When the memory cell is deleted or
 1.13177 +** overwritten, the VdbeFrame object is not freed immediately. Instead, it
 1.13178 +** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
 1.13179 +** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
 1.13180 +** this instead of deleting the VdbeFrame immediately is to avoid recursive
 1.13181 +** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
 1.13182 +** child frame are released.
 1.13183 +**
 1.13184 +** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
 1.13185 +** set to NULL if the currently executing frame is the main program.
 1.13186 +*/
 1.13187 +typedef struct VdbeFrame VdbeFrame;
 1.13188 +struct VdbeFrame {
 1.13189 +  Vdbe *v;                /* VM this frame belongs to */
 1.13190 +  VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
 1.13191 +  Op *aOp;                /* Program instructions for parent frame */
 1.13192 +  Mem *aMem;              /* Array of memory cells for parent frame */
 1.13193 +  u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
 1.13194 +  VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
 1.13195 +  void *token;            /* Copy of SubProgram.token */
 1.13196 +  i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
 1.13197 +  u16 nCursor;            /* Number of entries in apCsr */
 1.13198 +  int pc;                 /* Program Counter in parent (calling) frame */
 1.13199 +  int nOp;                /* Size of aOp array */
 1.13200 +  int nMem;               /* Number of entries in aMem */
 1.13201 +  int nOnceFlag;          /* Number of entries in aOnceFlag */
 1.13202 +  int nChildMem;          /* Number of memory cells for child frame */
 1.13203 +  int nChildCsr;          /* Number of cursors for child frame */
 1.13204 +  int nChange;            /* Statement changes (Vdbe.nChanges)     */
 1.13205 +};
 1.13206 +
 1.13207 +#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
 1.13208 +
 1.13209 +/*
 1.13210 +** A value for VdbeCursor.cacheValid that means the cache is always invalid.
 1.13211 +*/
 1.13212 +#define CACHE_STALE 0
 1.13213 +
 1.13214 +/*
 1.13215 +** Internally, the vdbe manipulates nearly all SQL values as Mem
 1.13216 +** structures. Each Mem struct may cache multiple representations (string,
 1.13217 +** integer etc.) of the same value.
 1.13218 +*/
 1.13219 +struct Mem {
 1.13220 +  sqlite3 *db;        /* The associated database connection */
 1.13221 +  char *z;            /* String or BLOB value */
 1.13222 +  double r;           /* Real value */
 1.13223 +  union {
 1.13224 +    i64 i;              /* Integer value used when MEM_Int is set in flags */
 1.13225 +    int nZero;          /* Used when bit MEM_Zero is set in flags */
 1.13226 +    FuncDef *pDef;      /* Used only when flags==MEM_Agg */
 1.13227 +    RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
 1.13228 +    VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
 1.13229 +  } u;
 1.13230 +  int n;              /* Number of characters in string value, excluding '\0' */
 1.13231 +  u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
 1.13232 +  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
 1.13233 +  u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
 1.13234 +#ifdef SQLITE_DEBUG
 1.13235 +  Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
 1.13236 +  void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
 1.13237 +#endif
 1.13238 +  void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
 1.13239 +  char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
 1.13240 +};
 1.13241 +
 1.13242 +/* One or more of the following flags are set to indicate the validOK
 1.13243 +** representations of the value stored in the Mem struct.
 1.13244 +**
 1.13245 +** If the MEM_Null flag is set, then the value is an SQL NULL value.
 1.13246 +** No other flags may be set in this case.
 1.13247 +**
 1.13248 +** If the MEM_Str flag is set then Mem.z points at a string representation.
 1.13249 +** Usually this is encoded in the same unicode encoding as the main
 1.13250 +** database (see below for exceptions). If the MEM_Term flag is also
 1.13251 +** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 1.13252 +** flags may coexist with the MEM_Str flag.
 1.13253 +*/
 1.13254 +#define MEM_Null      0x0001   /* Value is NULL */
 1.13255 +#define MEM_Str       0x0002   /* Value is a string */
 1.13256 +#define MEM_Int       0x0004   /* Value is an integer */
 1.13257 +#define MEM_Real      0x0008   /* Value is a real number */
 1.13258 +#define MEM_Blob      0x0010   /* Value is a BLOB */
 1.13259 +#define MEM_RowSet    0x0020   /* Value is a RowSet object */
 1.13260 +#define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
 1.13261 +#define MEM_Invalid   0x0080   /* Value is undefined */
 1.13262 +#define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
 1.13263 +#define MEM_TypeMask  0x01ff   /* Mask of type bits */
 1.13264 +
 1.13265 +
 1.13266 +/* Whenever Mem contains a valid string or blob representation, one of
 1.13267 +** the following flags must be set to determine the memory management
 1.13268 +** policy for Mem.z.  The MEM_Term flag tells us whether or not the
 1.13269 +** string is \000 or \u0000 terminated
 1.13270 +*/
 1.13271 +#define MEM_Term      0x0200   /* String rep is nul terminated */
 1.13272 +#define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
 1.13273 +#define MEM_Static    0x0800   /* Mem.z points to a static string */
 1.13274 +#define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
 1.13275 +#define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
 1.13276 +#define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
 1.13277 +#ifdef SQLITE_OMIT_INCRBLOB
 1.13278 +  #undef MEM_Zero
 1.13279 +  #define MEM_Zero 0x0000
 1.13280 +#endif
 1.13281 +
 1.13282 +/*
 1.13283 +** Clear any existing type flags from a Mem and replace them with f
 1.13284 +*/
 1.13285 +#define MemSetTypeFlag(p, f) \
 1.13286 +   ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
 1.13287 +
 1.13288 +/*
 1.13289 +** Return true if a memory cell is not marked as invalid.  This macro
 1.13290 +** is for use inside assert() statements only.
 1.13291 +*/
 1.13292 +#ifdef SQLITE_DEBUG
 1.13293 +#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
 1.13294 +#endif
 1.13295 +
 1.13296 +
 1.13297 +/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
 1.13298 +** additional information about auxiliary information bound to arguments
 1.13299 +** of the function.  This is used to implement the sqlite3_get_auxdata()
 1.13300 +** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
 1.13301 +** that can be associated with a constant argument to a function.  This
 1.13302 +** allows functions such as "regexp" to compile their constant regular
 1.13303 +** expression argument once and reused the compiled code for multiple
 1.13304 +** invocations.
 1.13305 +*/
 1.13306 +struct VdbeFunc {
 1.13307 +  FuncDef *pFunc;               /* The definition of the function */
 1.13308 +  int nAux;                     /* Number of entries allocated for apAux[] */
 1.13309 +  struct AuxData {
 1.13310 +    void *pAux;                   /* Aux data for the i-th argument */
 1.13311 +    void (*xDelete)(void *);      /* Destructor for the aux data */
 1.13312 +  } apAux[1];                   /* One slot for each function argument */
 1.13313 +};
 1.13314 +
 1.13315 +/*
 1.13316 +** The "context" argument for a installable function.  A pointer to an
 1.13317 +** instance of this structure is the first argument to the routines used
 1.13318 +** implement the SQL functions.
 1.13319 +**
 1.13320 +** There is a typedef for this structure in sqlite.h.  So all routines,
 1.13321 +** even the public interface to SQLite, can use a pointer to this structure.
 1.13322 +** But this file is the only place where the internal details of this
 1.13323 +** structure are known.
 1.13324 +**
 1.13325 +** This structure is defined inside of vdbeInt.h because it uses substructures
 1.13326 +** (Mem) which are only defined there.
 1.13327 +*/
 1.13328 +struct sqlite3_context {
 1.13329 +  FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
 1.13330 +  VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
 1.13331 +  Mem s;                /* The return value is stored here */
 1.13332 +  Mem *pMem;            /* Memory cell used to store aggregate context */
 1.13333 +  CollSeq *pColl;       /* Collating sequence */
 1.13334 +  int isError;          /* Error code returned by the function. */
 1.13335 +  int skipFlag;         /* Skip skip accumulator loading if true */
 1.13336 +};
 1.13337 +
 1.13338 +/*
 1.13339 +** An Explain object accumulates indented output which is helpful
 1.13340 +** in describing recursive data structures.
 1.13341 +*/
 1.13342 +struct Explain {
 1.13343 +  Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
 1.13344 +  StrAccum str;      /* The string being accumulated */
 1.13345 +  int nIndent;       /* Number of elements in aIndent */
 1.13346 +  u16 aIndent[100];  /* Levels of indentation */
 1.13347 +  char zBase[100];   /* Initial space */
 1.13348 +};
 1.13349 +
 1.13350 +/* A bitfield type for use inside of structures.  Always follow with :N where
 1.13351 +** N is the number of bits.
 1.13352 +*/
 1.13353 +typedef unsigned bft;  /* Bit Field Type */
 1.13354 +
 1.13355 +/*
 1.13356 +** An instance of the virtual machine.  This structure contains the complete
 1.13357 +** state of the virtual machine.
 1.13358 +**
 1.13359 +** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
 1.13360 +** is really a pointer to an instance of this structure.
 1.13361 +**
 1.13362 +** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
 1.13363 +** any virtual table method invocations made by the vdbe program. It is
 1.13364 +** set to 2 for xDestroy method calls and 1 for all other methods. This
 1.13365 +** variable is used for two purposes: to allow xDestroy methods to execute
 1.13366 +** "DROP TABLE" statements and to prevent some nasty side effects of
 1.13367 +** malloc failure when SQLite is invoked recursively by a virtual table 
 1.13368 +** method function.
 1.13369 +*/
 1.13370 +struct Vdbe {
 1.13371 +  sqlite3 *db;            /* The database connection that owns this statement */
 1.13372 +  Op *aOp;                /* Space to hold the virtual machine's program */
 1.13373 +  Mem *aMem;              /* The memory locations */
 1.13374 +  Mem **apArg;            /* Arguments to currently executing user function */
 1.13375 +  Mem *aColName;          /* Column names to return */
 1.13376 +  Mem *pResultSet;        /* Pointer to an array of results */
 1.13377 +  int nMem;               /* Number of memory locations currently allocated */
 1.13378 +  int nOp;                /* Number of instructions in the program */
 1.13379 +  int nOpAlloc;           /* Number of slots allocated for aOp[] */
 1.13380 +  int nLabel;             /* Number of labels used */
 1.13381 +  int *aLabel;            /* Space to hold the labels */
 1.13382 +  u16 nResColumn;         /* Number of columns in one row of the result set */
 1.13383 +  u16 nCursor;            /* Number of slots in apCsr[] */
 1.13384 +  u32 magic;              /* Magic number for sanity checking */
 1.13385 +  char *zErrMsg;          /* Error message written here */
 1.13386 +  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
 1.13387 +  VdbeCursor **apCsr;     /* One element of this array for each open cursor */
 1.13388 +  Mem *aVar;              /* Values for the OP_Variable opcode. */
 1.13389 +  char **azVar;           /* Name of variables */
 1.13390 +  ynVar nVar;             /* Number of entries in aVar[] */
 1.13391 +  ynVar nzVar;            /* Number of entries in azVar[] */
 1.13392 +  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
 1.13393 +  int pc;                 /* The program counter */
 1.13394 +  int rc;                 /* Value to return */
 1.13395 +  u8 errorAction;         /* Recovery action to do in case of an error */
 1.13396 +  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 1.13397 +  bft explain:2;          /* True if EXPLAIN present on SQL command */
 1.13398 +  bft inVtabMethod:2;     /* See comments above */
 1.13399 +  bft changeCntOn:1;      /* True to update the change-counter */
 1.13400 +  bft expired:1;          /* True if the VM needs to be recompiled */
 1.13401 +  bft runOnlyOnce:1;      /* Automatically expire on reset */
 1.13402 +  bft usesStmtJournal:1;  /* True if uses a statement journal */
 1.13403 +  bft readOnly:1;         /* True for read-only statements */
 1.13404 +  bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
 1.13405 +  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
 1.13406 +  int nChange;            /* Number of db changes made since last reset */
 1.13407 +  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
 1.13408 +  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 1.13409 +  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
 1.13410 +  int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
 1.13411 +#ifndef SQLITE_OMIT_TRACE
 1.13412 +  i64 startTime;          /* Time when query started - used for profiling */
 1.13413 +#endif
 1.13414 +  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
 1.13415 +  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
 1.13416 +  char *zSql;             /* Text of the SQL statement that generated this */
 1.13417 +  void *pFree;            /* Free this when deleting the vdbe */
 1.13418 +#ifdef SQLITE_DEBUG
 1.13419 +  FILE *trace;            /* Write an execution trace here, if not NULL */
 1.13420 +#endif
 1.13421 +#ifdef SQLITE_ENABLE_TREE_EXPLAIN
 1.13422 +  Explain *pExplain;      /* The explainer */
 1.13423 +  char *zExplain;         /* Explanation of data structures */
 1.13424 +#endif
 1.13425 +  VdbeFrame *pFrame;      /* Parent frame */
 1.13426 +  VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 1.13427 +  int nFrame;             /* Number of frames in pFrame list */
 1.13428 +  u32 expmask;            /* Binding to these vars invalidates VM */
 1.13429 +  SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 1.13430 +  int nOnceFlag;          /* Size of array aOnceFlag[] */
 1.13431 +  u8 *aOnceFlag;          /* Flags for OP_Once */
 1.13432 +};
 1.13433 +
 1.13434 +/*
 1.13435 +** The following are allowed values for Vdbe.magic
 1.13436 +*/
 1.13437 +#define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
 1.13438 +#define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
 1.13439 +#define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
 1.13440 +#define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
 1.13441 +
 1.13442 +/*
 1.13443 +** Function prototypes
 1.13444 +*/
 1.13445 +SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
 1.13446 +void sqliteVdbePopStack(Vdbe*,int);
 1.13447 +SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
 1.13448 +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 1.13449 +SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
 1.13450 +#endif
 1.13451 +SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
 1.13452 +SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
 1.13453 +SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
 1.13454 +SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 1.13455 +SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
 1.13456 +
 1.13457 +int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 1.13458 +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
 1.13459 +SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
 1.13460 +SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
 1.13461 +SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
 1.13462 +SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
 1.13463 +SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 1.13464 +SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 1.13465 +SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
 1.13466 +SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
 1.13467 +SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
 1.13468 +SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
 1.13469 +SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
 1.13470 +SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
 1.13471 +SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
 1.13472 +#ifdef SQLITE_OMIT_FLOATING_POINT
 1.13473 +# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
 1.13474 +#else
 1.13475 +SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
 1.13476 +#endif
 1.13477 +SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
 1.13478 +SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
 1.13479 +SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
 1.13480 +SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
 1.13481 +SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
 1.13482 +SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
 1.13483 +SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
 1.13484 +SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
 1.13485 +SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 1.13486 +SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 1.13487 +SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 1.13488 +SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
 1.13489 +SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 1.13490 +SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
 1.13491 +#define VdbeMemRelease(X)  \
 1.13492 +  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
 1.13493 +    sqlite3VdbeMemReleaseExternal(X);
 1.13494 +SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 1.13495 +SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
 1.13496 +SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 1.13497 +SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 1.13498 +SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 1.13499 +SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 1.13500 +SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
 1.13501 +SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
 1.13502 +
 1.13503 +#ifdef SQLITE_OMIT_MERGE_SORT
 1.13504 +# define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
 1.13505 +# define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
 1.13506 +# define sqlite3VdbeSorterClose(Y,Z)
 1.13507 +# define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
 1.13508 +# define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
 1.13509 +# define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
 1.13510 +# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
 1.13511 +#else
 1.13512 +SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
 1.13513 +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 1.13514 +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
 1.13515 +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
 1.13516 +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
 1.13517 +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
 1.13518 +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int *);
 1.13519 +#endif
 1.13520 +
 1.13521 +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 1.13522 +SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
 1.13523 +SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
 1.13524 +#else
 1.13525 +# define sqlite3VdbeEnter(X)
 1.13526 +# define sqlite3VdbeLeave(X)
 1.13527 +#endif
 1.13528 +
 1.13529 +#ifdef SQLITE_DEBUG
 1.13530 +SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
 1.13531 +#endif
 1.13532 +
 1.13533 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.13534 +SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
 1.13535 +#else
 1.13536 +# define sqlite3VdbeCheckFk(p,i) 0
 1.13537 +#endif
 1.13538 +
 1.13539 +SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
 1.13540 +#ifdef SQLITE_DEBUG
 1.13541 +SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
 1.13542 +SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
 1.13543 +#endif
 1.13544 +SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
 1.13545 +
 1.13546 +#ifndef SQLITE_OMIT_INCRBLOB
 1.13547 +SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
 1.13548 +  #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
 1.13549 +#else
 1.13550 +  #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
 1.13551 +  #define ExpandBlob(P) SQLITE_OK
 1.13552 +#endif
 1.13553 +
 1.13554 +#endif /* !defined(_VDBEINT_H_) */
 1.13555 +
 1.13556 +/************** End of vdbeInt.h *********************************************/
 1.13557 +/************** Continuing where we left off in status.c *********************/
 1.13558 +
 1.13559 +/*
 1.13560 +** Variables in which to record status information.
 1.13561 +*/
 1.13562 +typedef struct sqlite3StatType sqlite3StatType;
 1.13563 +static SQLITE_WSD struct sqlite3StatType {
 1.13564 +  int nowValue[10];         /* Current value */
 1.13565 +  int mxValue[10];          /* Maximum value */
 1.13566 +} sqlite3Stat = { {0,}, {0,} };
 1.13567 +
 1.13568 +
 1.13569 +/* The "wsdStat" macro will resolve to the status information
 1.13570 +** state vector.  If writable static data is unsupported on the target,
 1.13571 +** we have to locate the state vector at run-time.  In the more common
 1.13572 +** case where writable static data is supported, wsdStat can refer directly
 1.13573 +** to the "sqlite3Stat" state vector declared above.
 1.13574 +*/
 1.13575 +#ifdef SQLITE_OMIT_WSD
 1.13576 +# define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
 1.13577 +# define wsdStat x[0]
 1.13578 +#else
 1.13579 +# define wsdStatInit
 1.13580 +# define wsdStat sqlite3Stat
 1.13581 +#endif
 1.13582 +
 1.13583 +/*
 1.13584 +** Return the current value of a status parameter.
 1.13585 +*/
 1.13586 +SQLITE_PRIVATE int sqlite3StatusValue(int op){
 1.13587 +  wsdStatInit;
 1.13588 +  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 1.13589 +  return wsdStat.nowValue[op];
 1.13590 +}
 1.13591 +
 1.13592 +/*
 1.13593 +** Add N to the value of a status record.  It is assumed that the
 1.13594 +** caller holds appropriate locks.
 1.13595 +*/
 1.13596 +SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
 1.13597 +  wsdStatInit;
 1.13598 +  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 1.13599 +  wsdStat.nowValue[op] += N;
 1.13600 +  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
 1.13601 +    wsdStat.mxValue[op] = wsdStat.nowValue[op];
 1.13602 +  }
 1.13603 +}
 1.13604 +
 1.13605 +/*
 1.13606 +** Set the value of a status to X.
 1.13607 +*/
 1.13608 +SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
 1.13609 +  wsdStatInit;
 1.13610 +  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 1.13611 +  wsdStat.nowValue[op] = X;
 1.13612 +  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
 1.13613 +    wsdStat.mxValue[op] = wsdStat.nowValue[op];
 1.13614 +  }
 1.13615 +}
 1.13616 +
 1.13617 +/*
 1.13618 +** Query status information.
 1.13619 +**
 1.13620 +** This implementation assumes that reading or writing an aligned
 1.13621 +** 32-bit integer is an atomic operation.  If that assumption is not true,
 1.13622 +** then this routine is not threadsafe.
 1.13623 +*/
 1.13624 +SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
 1.13625 +  wsdStatInit;
 1.13626 +  if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
 1.13627 +    return SQLITE_MISUSE_BKPT;
 1.13628 +  }
 1.13629 +  *pCurrent = wsdStat.nowValue[op];
 1.13630 +  *pHighwater = wsdStat.mxValue[op];
 1.13631 +  if( resetFlag ){
 1.13632 +    wsdStat.mxValue[op] = wsdStat.nowValue[op];
 1.13633 +  }
 1.13634 +  return SQLITE_OK;
 1.13635 +}
 1.13636 +
 1.13637 +/*
 1.13638 +** Query status information for a single database connection
 1.13639 +*/
 1.13640 +SQLITE_API int sqlite3_db_status(
 1.13641 +  sqlite3 *db,          /* The database connection whose status is desired */
 1.13642 +  int op,               /* Status verb */
 1.13643 +  int *pCurrent,        /* Write current value here */
 1.13644 +  int *pHighwater,      /* Write high-water mark here */
 1.13645 +  int resetFlag         /* Reset high-water mark if true */
 1.13646 +){
 1.13647 +  int rc = SQLITE_OK;   /* Return code */
 1.13648 +  sqlite3_mutex_enter(db->mutex);
 1.13649 +  switch( op ){
 1.13650 +    case SQLITE_DBSTATUS_LOOKASIDE_USED: {
 1.13651 +      *pCurrent = db->lookaside.nOut;
 1.13652 +      *pHighwater = db->lookaside.mxOut;
 1.13653 +      if( resetFlag ){
 1.13654 +        db->lookaside.mxOut = db->lookaside.nOut;
 1.13655 +      }
 1.13656 +      break;
 1.13657 +    }
 1.13658 +
 1.13659 +    case SQLITE_DBSTATUS_LOOKASIDE_HIT:
 1.13660 +    case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
 1.13661 +    case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
 1.13662 +      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
 1.13663 +      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
 1.13664 +      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
 1.13665 +      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
 1.13666 +      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
 1.13667 +      *pCurrent = 0;
 1.13668 +      *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
 1.13669 +      if( resetFlag ){
 1.13670 +        db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
 1.13671 +      }
 1.13672 +      break;
 1.13673 +    }
 1.13674 +
 1.13675 +    /* 
 1.13676 +    ** Return an approximation for the amount of memory currently used
 1.13677 +    ** by all pagers associated with the given database connection.  The
 1.13678 +    ** highwater mark is meaningless and is returned as zero.
 1.13679 +    */
 1.13680 +    case SQLITE_DBSTATUS_CACHE_USED: {
 1.13681 +      int totalUsed = 0;
 1.13682 +      int i;
 1.13683 +      sqlite3BtreeEnterAll(db);
 1.13684 +      for(i=0; i<db->nDb; i++){
 1.13685 +        Btree *pBt = db->aDb[i].pBt;
 1.13686 +        if( pBt ){
 1.13687 +          Pager *pPager = sqlite3BtreePager(pBt);
 1.13688 +          totalUsed += sqlite3PagerMemUsed(pPager);
 1.13689 +        }
 1.13690 +      }
 1.13691 +      sqlite3BtreeLeaveAll(db);
 1.13692 +      *pCurrent = totalUsed;
 1.13693 +      *pHighwater = 0;
 1.13694 +      break;
 1.13695 +    }
 1.13696 +
 1.13697 +    /*
 1.13698 +    ** *pCurrent gets an accurate estimate of the amount of memory used
 1.13699 +    ** to store the schema for all databases (main, temp, and any ATTACHed
 1.13700 +    ** databases.  *pHighwater is set to zero.
 1.13701 +    */
 1.13702 +    case SQLITE_DBSTATUS_SCHEMA_USED: {
 1.13703 +      int i;                      /* Used to iterate through schemas */
 1.13704 +      int nByte = 0;              /* Used to accumulate return value */
 1.13705 +
 1.13706 +      sqlite3BtreeEnterAll(db);
 1.13707 +      db->pnBytesFreed = &nByte;
 1.13708 +      for(i=0; i<db->nDb; i++){
 1.13709 +        Schema *pSchema = db->aDb[i].pSchema;
 1.13710 +        if( ALWAYS(pSchema!=0) ){
 1.13711 +          HashElem *p;
 1.13712 +
 1.13713 +          nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
 1.13714 +              pSchema->tblHash.count 
 1.13715 +            + pSchema->trigHash.count
 1.13716 +            + pSchema->idxHash.count
 1.13717 +            + pSchema->fkeyHash.count
 1.13718 +          );
 1.13719 +          nByte += sqlite3MallocSize(pSchema->tblHash.ht);
 1.13720 +          nByte += sqlite3MallocSize(pSchema->trigHash.ht);
 1.13721 +          nByte += sqlite3MallocSize(pSchema->idxHash.ht);
 1.13722 +          nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
 1.13723 +
 1.13724 +          for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
 1.13725 +            sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
 1.13726 +          }
 1.13727 +          for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
 1.13728 +            sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
 1.13729 +          }
 1.13730 +        }
 1.13731 +      }
 1.13732 +      db->pnBytesFreed = 0;
 1.13733 +      sqlite3BtreeLeaveAll(db);
 1.13734 +
 1.13735 +      *pHighwater = 0;
 1.13736 +      *pCurrent = nByte;
 1.13737 +      break;
 1.13738 +    }
 1.13739 +
 1.13740 +    /*
 1.13741 +    ** *pCurrent gets an accurate estimate of the amount of memory used
 1.13742 +    ** to store all prepared statements.
 1.13743 +    ** *pHighwater is set to zero.
 1.13744 +    */
 1.13745 +    case SQLITE_DBSTATUS_STMT_USED: {
 1.13746 +      struct Vdbe *pVdbe;         /* Used to iterate through VMs */
 1.13747 +      int nByte = 0;              /* Used to accumulate return value */
 1.13748 +
 1.13749 +      db->pnBytesFreed = &nByte;
 1.13750 +      for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
 1.13751 +        sqlite3VdbeClearObject(db, pVdbe);
 1.13752 +        sqlite3DbFree(db, pVdbe);
 1.13753 +      }
 1.13754 +      db->pnBytesFreed = 0;
 1.13755 +
 1.13756 +      *pHighwater = 0;
 1.13757 +      *pCurrent = nByte;
 1.13758 +
 1.13759 +      break;
 1.13760 +    }
 1.13761 +
 1.13762 +    /*
 1.13763 +    ** Set *pCurrent to the total cache hits or misses encountered by all
 1.13764 +    ** pagers the database handle is connected to. *pHighwater is always set 
 1.13765 +    ** to zero.
 1.13766 +    */
 1.13767 +    case SQLITE_DBSTATUS_CACHE_HIT:
 1.13768 +    case SQLITE_DBSTATUS_CACHE_MISS:
 1.13769 +    case SQLITE_DBSTATUS_CACHE_WRITE:{
 1.13770 +      int i;
 1.13771 +      int nRet = 0;
 1.13772 +      assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 1.13773 +      assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
 1.13774 +
 1.13775 +      for(i=0; i<db->nDb; i++){
 1.13776 +        if( db->aDb[i].pBt ){
 1.13777 +          Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
 1.13778 +          sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
 1.13779 +        }
 1.13780 +      }
 1.13781 +      *pHighwater = 0;
 1.13782 +      *pCurrent = nRet;
 1.13783 +      break;
 1.13784 +    }
 1.13785 +
 1.13786 +    default: {
 1.13787 +      rc = SQLITE_ERROR;
 1.13788 +    }
 1.13789 +  }
 1.13790 +  sqlite3_mutex_leave(db->mutex);
 1.13791 +  return rc;
 1.13792 +}
 1.13793 +
 1.13794 +/************** End of status.c **********************************************/
 1.13795 +/************** Begin file date.c ********************************************/
 1.13796 +/*
 1.13797 +** 2003 October 31
 1.13798 +**
 1.13799 +** The author disclaims copyright to this source code.  In place of
 1.13800 +** a legal notice, here is a blessing:
 1.13801 +**
 1.13802 +**    May you do good and not evil.
 1.13803 +**    May you find forgiveness for yourself and forgive others.
 1.13804 +**    May you share freely, never taking more than you give.
 1.13805 +**
 1.13806 +*************************************************************************
 1.13807 +** This file contains the C functions that implement date and time
 1.13808 +** functions for SQLite.  
 1.13809 +**
 1.13810 +** There is only one exported symbol in this file - the function
 1.13811 +** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
 1.13812 +** All other code has file scope.
 1.13813 +**
 1.13814 +** SQLite processes all times and dates as Julian Day numbers.  The
 1.13815 +** dates and times are stored as the number of days since noon
 1.13816 +** in Greenwich on November 24, 4714 B.C. according to the Gregorian
 1.13817 +** calendar system. 
 1.13818 +**
 1.13819 +** 1970-01-01 00:00:00 is JD 2440587.5
 1.13820 +** 2000-01-01 00:00:00 is JD 2451544.5
 1.13821 +**
 1.13822 +** This implemention requires years to be expressed as a 4-digit number
 1.13823 +** which means that only dates between 0000-01-01 and 9999-12-31 can
 1.13824 +** be represented, even though julian day numbers allow a much wider
 1.13825 +** range of dates.
 1.13826 +**
 1.13827 +** The Gregorian calendar system is used for all dates and times,
 1.13828 +** even those that predate the Gregorian calendar.  Historians usually
 1.13829 +** use the Julian calendar for dates prior to 1582-10-15 and for some
 1.13830 +** dates afterwards, depending on locale.  Beware of this difference.
 1.13831 +**
 1.13832 +** The conversion algorithms are implemented based on descriptions
 1.13833 +** in the following text:
 1.13834 +**
 1.13835 +**      Jean Meeus
 1.13836 +**      Astronomical Algorithms, 2nd Edition, 1998
 1.13837 +**      ISBM 0-943396-61-1
 1.13838 +**      Willmann-Bell, Inc
 1.13839 +**      Richmond, Virginia (USA)
 1.13840 +*/
 1.13841 +/* #include <stdlib.h> */
 1.13842 +/* #include <assert.h> */
 1.13843 +#include <time.h>
 1.13844 +
 1.13845 +#ifndef SQLITE_OMIT_DATETIME_FUNCS
 1.13846 +
 1.13847 +
 1.13848 +/*
 1.13849 +** A structure for holding a single date and time.
 1.13850 +*/
 1.13851 +typedef struct DateTime DateTime;
 1.13852 +struct DateTime {
 1.13853 +  sqlite3_int64 iJD; /* The julian day number times 86400000 */
 1.13854 +  int Y, M, D;       /* Year, month, and day */
 1.13855 +  int h, m;          /* Hour and minutes */
 1.13856 +  int tz;            /* Timezone offset in minutes */
 1.13857 +  double s;          /* Seconds */
 1.13858 +  char validYMD;     /* True (1) if Y,M,D are valid */
 1.13859 +  char validHMS;     /* True (1) if h,m,s are valid */
 1.13860 +  char validJD;      /* True (1) if iJD is valid */
 1.13861 +  char validTZ;      /* True (1) if tz is valid */
 1.13862 +};
 1.13863 +
 1.13864 +
 1.13865 +/*
 1.13866 +** Convert zDate into one or more integers.  Additional arguments
 1.13867 +** come in groups of 5 as follows:
 1.13868 +**
 1.13869 +**       N       number of digits in the integer
 1.13870 +**       min     minimum allowed value of the integer
 1.13871 +**       max     maximum allowed value of the integer
 1.13872 +**       nextC   first character after the integer
 1.13873 +**       pVal    where to write the integers value.
 1.13874 +**
 1.13875 +** Conversions continue until one with nextC==0 is encountered.
 1.13876 +** The function returns the number of successful conversions.
 1.13877 +*/
 1.13878 +static int getDigits(const char *zDate, ...){
 1.13879 +  va_list ap;
 1.13880 +  int val;
 1.13881 +  int N;
 1.13882 +  int min;
 1.13883 +  int max;
 1.13884 +  int nextC;
 1.13885 +  int *pVal;
 1.13886 +  int cnt = 0;
 1.13887 +  va_start(ap, zDate);
 1.13888 +  do{
 1.13889 +    N = va_arg(ap, int);
 1.13890 +    min = va_arg(ap, int);
 1.13891 +    max = va_arg(ap, int);
 1.13892 +    nextC = va_arg(ap, int);
 1.13893 +    pVal = va_arg(ap, int*);
 1.13894 +    val = 0;
 1.13895 +    while( N-- ){
 1.13896 +      if( !sqlite3Isdigit(*zDate) ){
 1.13897 +        goto end_getDigits;
 1.13898 +      }
 1.13899 +      val = val*10 + *zDate - '0';
 1.13900 +      zDate++;
 1.13901 +    }
 1.13902 +    if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
 1.13903 +      goto end_getDigits;
 1.13904 +    }
 1.13905 +    *pVal = val;
 1.13906 +    zDate++;
 1.13907 +    cnt++;
 1.13908 +  }while( nextC );
 1.13909 +end_getDigits:
 1.13910 +  va_end(ap);
 1.13911 +  return cnt;
 1.13912 +}
 1.13913 +
 1.13914 +/*
 1.13915 +** Parse a timezone extension on the end of a date-time.
 1.13916 +** The extension is of the form:
 1.13917 +**
 1.13918 +**        (+/-)HH:MM
 1.13919 +**
 1.13920 +** Or the "zulu" notation:
 1.13921 +**
 1.13922 +**        Z
 1.13923 +**
 1.13924 +** If the parse is successful, write the number of minutes
 1.13925 +** of change in p->tz and return 0.  If a parser error occurs,
 1.13926 +** return non-zero.
 1.13927 +**
 1.13928 +** A missing specifier is not considered an error.
 1.13929 +*/
 1.13930 +static int parseTimezone(const char *zDate, DateTime *p){
 1.13931 +  int sgn = 0;
 1.13932 +  int nHr, nMn;
 1.13933 +  int c;
 1.13934 +  while( sqlite3Isspace(*zDate) ){ zDate++; }
 1.13935 +  p->tz = 0;
 1.13936 +  c = *zDate;
 1.13937 +  if( c=='-' ){
 1.13938 +    sgn = -1;
 1.13939 +  }else if( c=='+' ){
 1.13940 +    sgn = +1;
 1.13941 +  }else if( c=='Z' || c=='z' ){
 1.13942 +    zDate++;
 1.13943 +    goto zulu_time;
 1.13944 +  }else{
 1.13945 +    return c!=0;
 1.13946 +  }
 1.13947 +  zDate++;
 1.13948 +  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
 1.13949 +    return 1;
 1.13950 +  }
 1.13951 +  zDate += 5;
 1.13952 +  p->tz = sgn*(nMn + nHr*60);
 1.13953 +zulu_time:
 1.13954 +  while( sqlite3Isspace(*zDate) ){ zDate++; }
 1.13955 +  return *zDate!=0;
 1.13956 +}
 1.13957 +
 1.13958 +/*
 1.13959 +** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
 1.13960 +** The HH, MM, and SS must each be exactly 2 digits.  The
 1.13961 +** fractional seconds FFFF can be one or more digits.
 1.13962 +**
 1.13963 +** Return 1 if there is a parsing error and 0 on success.
 1.13964 +*/
 1.13965 +static int parseHhMmSs(const char *zDate, DateTime *p){
 1.13966 +  int h, m, s;
 1.13967 +  double ms = 0.0;
 1.13968 +  if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
 1.13969 +    return 1;
 1.13970 +  }
 1.13971 +  zDate += 5;
 1.13972 +  if( *zDate==':' ){
 1.13973 +    zDate++;
 1.13974 +    if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
 1.13975 +      return 1;
 1.13976 +    }
 1.13977 +    zDate += 2;
 1.13978 +    if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
 1.13979 +      double rScale = 1.0;
 1.13980 +      zDate++;
 1.13981 +      while( sqlite3Isdigit(*zDate) ){
 1.13982 +        ms = ms*10.0 + *zDate - '0';
 1.13983 +        rScale *= 10.0;
 1.13984 +        zDate++;
 1.13985 +      }
 1.13986 +      ms /= rScale;
 1.13987 +    }
 1.13988 +  }else{
 1.13989 +    s = 0;
 1.13990 +  }
 1.13991 +  p->validJD = 0;
 1.13992 +  p->validHMS = 1;
 1.13993 +  p->h = h;
 1.13994 +  p->m = m;
 1.13995 +  p->s = s + ms;
 1.13996 +  if( parseTimezone(zDate, p) ) return 1;
 1.13997 +  p->validTZ = (p->tz!=0)?1:0;
 1.13998 +  return 0;
 1.13999 +}
 1.14000 +
 1.14001 +/*
 1.14002 +** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
 1.14003 +** that the YYYY-MM-DD is according to the Gregorian calendar.
 1.14004 +**
 1.14005 +** Reference:  Meeus page 61
 1.14006 +*/
 1.14007 +static void computeJD(DateTime *p){
 1.14008 +  int Y, M, D, A, B, X1, X2;
 1.14009 +
 1.14010 +  if( p->validJD ) return;
 1.14011 +  if( p->validYMD ){
 1.14012 +    Y = p->Y;
 1.14013 +    M = p->M;
 1.14014 +    D = p->D;
 1.14015 +  }else{
 1.14016 +    Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
 1.14017 +    M = 1;
 1.14018 +    D = 1;
 1.14019 +  }
 1.14020 +  if( M<=2 ){
 1.14021 +    Y--;
 1.14022 +    M += 12;
 1.14023 +  }
 1.14024 +  A = Y/100;
 1.14025 +  B = 2 - A + (A/4);
 1.14026 +  X1 = 36525*(Y+4716)/100;
 1.14027 +  X2 = 306001*(M+1)/10000;
 1.14028 +  p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
 1.14029 +  p->validJD = 1;
 1.14030 +  if( p->validHMS ){
 1.14031 +    p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
 1.14032 +    if( p->validTZ ){
 1.14033 +      p->iJD -= p->tz*60000;
 1.14034 +      p->validYMD = 0;
 1.14035 +      p->validHMS = 0;
 1.14036 +      p->validTZ = 0;
 1.14037 +    }
 1.14038 +  }
 1.14039 +}
 1.14040 +
 1.14041 +/*
 1.14042 +** Parse dates of the form
 1.14043 +**
 1.14044 +**     YYYY-MM-DD HH:MM:SS.FFF
 1.14045 +**     YYYY-MM-DD HH:MM:SS
 1.14046 +**     YYYY-MM-DD HH:MM
 1.14047 +**     YYYY-MM-DD
 1.14048 +**
 1.14049 +** Write the result into the DateTime structure and return 0
 1.14050 +** on success and 1 if the input string is not a well-formed
 1.14051 +** date.
 1.14052 +*/
 1.14053 +static int parseYyyyMmDd(const char *zDate, DateTime *p){
 1.14054 +  int Y, M, D, neg;
 1.14055 +
 1.14056 +  if( zDate[0]=='-' ){
 1.14057 +    zDate++;
 1.14058 +    neg = 1;
 1.14059 +  }else{
 1.14060 +    neg = 0;
 1.14061 +  }
 1.14062 +  if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
 1.14063 +    return 1;
 1.14064 +  }
 1.14065 +  zDate += 10;
 1.14066 +  while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
 1.14067 +  if( parseHhMmSs(zDate, p)==0 ){
 1.14068 +    /* We got the time */
 1.14069 +  }else if( *zDate==0 ){
 1.14070 +    p->validHMS = 0;
 1.14071 +  }else{
 1.14072 +    return 1;
 1.14073 +  }
 1.14074 +  p->validJD = 0;
 1.14075 +  p->validYMD = 1;
 1.14076 +  p->Y = neg ? -Y : Y;
 1.14077 +  p->M = M;
 1.14078 +  p->D = D;
 1.14079 +  if( p->validTZ ){
 1.14080 +    computeJD(p);
 1.14081 +  }
 1.14082 +  return 0;
 1.14083 +}
 1.14084 +
 1.14085 +/*
 1.14086 +** Set the time to the current time reported by the VFS.
 1.14087 +**
 1.14088 +** Return the number of errors.
 1.14089 +*/
 1.14090 +static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
 1.14091 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.14092 +  if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){
 1.14093 +    p->validJD = 1;
 1.14094 +    return 0;
 1.14095 +  }else{
 1.14096 +    return 1;
 1.14097 +  }
 1.14098 +}
 1.14099 +
 1.14100 +/*
 1.14101 +** Attempt to parse the given string into a Julian Day Number.  Return
 1.14102 +** the number of errors.
 1.14103 +**
 1.14104 +** The following are acceptable forms for the input string:
 1.14105 +**
 1.14106 +**      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
 1.14107 +**      DDDD.DD 
 1.14108 +**      now
 1.14109 +**
 1.14110 +** In the first form, the +/-HH:MM is always optional.  The fractional
 1.14111 +** seconds extension (the ".FFF") is optional.  The seconds portion
 1.14112 +** (":SS.FFF") is option.  The year and date can be omitted as long
 1.14113 +** as there is a time string.  The time string can be omitted as long
 1.14114 +** as there is a year and date.
 1.14115 +*/
 1.14116 +static int parseDateOrTime(
 1.14117 +  sqlite3_context *context, 
 1.14118 +  const char *zDate, 
 1.14119 +  DateTime *p
 1.14120 +){
 1.14121 +  double r;
 1.14122 +  if( parseYyyyMmDd(zDate,p)==0 ){
 1.14123 +    return 0;
 1.14124 +  }else if( parseHhMmSs(zDate, p)==0 ){
 1.14125 +    return 0;
 1.14126 +  }else if( sqlite3StrICmp(zDate,"now")==0){
 1.14127 +    return setDateTimeToCurrent(context, p);
 1.14128 +  }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
 1.14129 +    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
 1.14130 +    p->validJD = 1;
 1.14131 +    return 0;
 1.14132 +  }
 1.14133 +  return 1;
 1.14134 +}
 1.14135 +
 1.14136 +/*
 1.14137 +** Compute the Year, Month, and Day from the julian day number.
 1.14138 +*/
 1.14139 +static void computeYMD(DateTime *p){
 1.14140 +  int Z, A, B, C, D, E, X1;
 1.14141 +  if( p->validYMD ) return;
 1.14142 +  if( !p->validJD ){
 1.14143 +    p->Y = 2000;
 1.14144 +    p->M = 1;
 1.14145 +    p->D = 1;
 1.14146 +  }else{
 1.14147 +    Z = (int)((p->iJD + 43200000)/86400000);
 1.14148 +    A = (int)((Z - 1867216.25)/36524.25);
 1.14149 +    A = Z + 1 + A - (A/4);
 1.14150 +    B = A + 1524;
 1.14151 +    C = (int)((B - 122.1)/365.25);
 1.14152 +    D = (36525*C)/100;
 1.14153 +    E = (int)((B-D)/30.6001);
 1.14154 +    X1 = (int)(30.6001*E);
 1.14155 +    p->D = B - D - X1;
 1.14156 +    p->M = E<14 ? E-1 : E-13;
 1.14157 +    p->Y = p->M>2 ? C - 4716 : C - 4715;
 1.14158 +  }
 1.14159 +  p->validYMD = 1;
 1.14160 +}
 1.14161 +
 1.14162 +/*
 1.14163 +** Compute the Hour, Minute, and Seconds from the julian day number.
 1.14164 +*/
 1.14165 +static void computeHMS(DateTime *p){
 1.14166 +  int s;
 1.14167 +  if( p->validHMS ) return;
 1.14168 +  computeJD(p);
 1.14169 +  s = (int)((p->iJD + 43200000) % 86400000);
 1.14170 +  p->s = s/1000.0;
 1.14171 +  s = (int)p->s;
 1.14172 +  p->s -= s;
 1.14173 +  p->h = s/3600;
 1.14174 +  s -= p->h*3600;
 1.14175 +  p->m = s/60;
 1.14176 +  p->s += s - p->m*60;
 1.14177 +  p->validHMS = 1;
 1.14178 +}
 1.14179 +
 1.14180 +/*
 1.14181 +** Compute both YMD and HMS
 1.14182 +*/
 1.14183 +static void computeYMD_HMS(DateTime *p){
 1.14184 +  computeYMD(p);
 1.14185 +  computeHMS(p);
 1.14186 +}
 1.14187 +
 1.14188 +/*
 1.14189 +** Clear the YMD and HMS and the TZ
 1.14190 +*/
 1.14191 +static void clearYMD_HMS_TZ(DateTime *p){
 1.14192 +  p->validYMD = 0;
 1.14193 +  p->validHMS = 0;
 1.14194 +  p->validTZ = 0;
 1.14195 +}
 1.14196 +
 1.14197 +/*
 1.14198 +** On recent Windows platforms, the localtime_s() function is available
 1.14199 +** as part of the "Secure CRT". It is essentially equivalent to 
 1.14200 +** localtime_r() available under most POSIX platforms, except that the 
 1.14201 +** order of the parameters is reversed.
 1.14202 +**
 1.14203 +** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
 1.14204 +**
 1.14205 +** If the user has not indicated to use localtime_r() or localtime_s()
 1.14206 +** already, check for an MSVC build environment that provides 
 1.14207 +** localtime_s().
 1.14208 +*/
 1.14209 +#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
 1.14210 +     defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
 1.14211 +#define HAVE_LOCALTIME_S 1
 1.14212 +#endif
 1.14213 +
 1.14214 +#ifndef SQLITE_OMIT_LOCALTIME
 1.14215 +/*
 1.14216 +** The following routine implements the rough equivalent of localtime_r()
 1.14217 +** using whatever operating-system specific localtime facility that
 1.14218 +** is available.  This routine returns 0 on success and
 1.14219 +** non-zero on any kind of error.
 1.14220 +**
 1.14221 +** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
 1.14222 +** routine will always fail.
 1.14223 +*/
 1.14224 +static int osLocaltime(time_t *t, struct tm *pTm){
 1.14225 +  int rc;
 1.14226 +#if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
 1.14227 +      && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
 1.14228 +  struct tm *pX;
 1.14229 +#if SQLITE_THREADSAFE>0
 1.14230 +  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 1.14231 +#endif
 1.14232 +  sqlite3_mutex_enter(mutex);
 1.14233 +  pX = localtime(t);
 1.14234 +#ifndef SQLITE_OMIT_BUILTIN_TEST
 1.14235 +  if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
 1.14236 +#endif
 1.14237 +  if( pX ) *pTm = *pX;
 1.14238 +  sqlite3_mutex_leave(mutex);
 1.14239 +  rc = pX==0;
 1.14240 +#else
 1.14241 +#ifndef SQLITE_OMIT_BUILTIN_TEST
 1.14242 +  if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
 1.14243 +#endif
 1.14244 +#if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
 1.14245 +  rc = localtime_r(t, pTm)==0;
 1.14246 +#else
 1.14247 +  rc = localtime_s(pTm, t);
 1.14248 +#endif /* HAVE_LOCALTIME_R */
 1.14249 +#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
 1.14250 +  return rc;
 1.14251 +}
 1.14252 +#endif /* SQLITE_OMIT_LOCALTIME */
 1.14253 +
 1.14254 +
 1.14255 +#ifndef SQLITE_OMIT_LOCALTIME
 1.14256 +/*
 1.14257 +** Compute the difference (in milliseconds) between localtime and UTC
 1.14258 +** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
 1.14259 +** return this value and set *pRc to SQLITE_OK. 
 1.14260 +**
 1.14261 +** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
 1.14262 +** is undefined in this case.
 1.14263 +*/
 1.14264 +static sqlite3_int64 localtimeOffset(
 1.14265 +  DateTime *p,                    /* Date at which to calculate offset */
 1.14266 +  sqlite3_context *pCtx,          /* Write error here if one occurs */
 1.14267 +  int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
 1.14268 +){
 1.14269 +  DateTime x, y;
 1.14270 +  time_t t;
 1.14271 +  struct tm sLocal;
 1.14272 +
 1.14273 +  /* Initialize the contents of sLocal to avoid a compiler warning. */
 1.14274 +  memset(&sLocal, 0, sizeof(sLocal));
 1.14275 +
 1.14276 +  x = *p;
 1.14277 +  computeYMD_HMS(&x);
 1.14278 +  if( x.Y<1971 || x.Y>=2038 ){
 1.14279 +    x.Y = 2000;
 1.14280 +    x.M = 1;
 1.14281 +    x.D = 1;
 1.14282 +    x.h = 0;
 1.14283 +    x.m = 0;
 1.14284 +    x.s = 0.0;
 1.14285 +  } else {
 1.14286 +    int s = (int)(x.s + 0.5);
 1.14287 +    x.s = s;
 1.14288 +  }
 1.14289 +  x.tz = 0;
 1.14290 +  x.validJD = 0;
 1.14291 +  computeJD(&x);
 1.14292 +  t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
 1.14293 +  if( osLocaltime(&t, &sLocal) ){
 1.14294 +    sqlite3_result_error(pCtx, "local time unavailable", -1);
 1.14295 +    *pRc = SQLITE_ERROR;
 1.14296 +    return 0;
 1.14297 +  }
 1.14298 +  y.Y = sLocal.tm_year + 1900;
 1.14299 +  y.M = sLocal.tm_mon + 1;
 1.14300 +  y.D = sLocal.tm_mday;
 1.14301 +  y.h = sLocal.tm_hour;
 1.14302 +  y.m = sLocal.tm_min;
 1.14303 +  y.s = sLocal.tm_sec;
 1.14304 +  y.validYMD = 1;
 1.14305 +  y.validHMS = 1;
 1.14306 +  y.validJD = 0;
 1.14307 +  y.validTZ = 0;
 1.14308 +  computeJD(&y);
 1.14309 +  *pRc = SQLITE_OK;
 1.14310 +  return y.iJD - x.iJD;
 1.14311 +}
 1.14312 +#endif /* SQLITE_OMIT_LOCALTIME */
 1.14313 +
 1.14314 +/*
 1.14315 +** Process a modifier to a date-time stamp.  The modifiers are
 1.14316 +** as follows:
 1.14317 +**
 1.14318 +**     NNN days
 1.14319 +**     NNN hours
 1.14320 +**     NNN minutes
 1.14321 +**     NNN.NNNN seconds
 1.14322 +**     NNN months
 1.14323 +**     NNN years
 1.14324 +**     start of month
 1.14325 +**     start of year
 1.14326 +**     start of week
 1.14327 +**     start of day
 1.14328 +**     weekday N
 1.14329 +**     unixepoch
 1.14330 +**     localtime
 1.14331 +**     utc
 1.14332 +**
 1.14333 +** Return 0 on success and 1 if there is any kind of error. If the error
 1.14334 +** is in a system call (i.e. localtime()), then an error message is written
 1.14335 +** to context pCtx. If the error is an unrecognized modifier, no error is
 1.14336 +** written to pCtx.
 1.14337 +*/
 1.14338 +static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
 1.14339 +  int rc = 1;
 1.14340 +  int n;
 1.14341 +  double r;
 1.14342 +  char *z, zBuf[30];
 1.14343 +  z = zBuf;
 1.14344 +  for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
 1.14345 +    z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
 1.14346 +  }
 1.14347 +  z[n] = 0;
 1.14348 +  switch( z[0] ){
 1.14349 +#ifndef SQLITE_OMIT_LOCALTIME
 1.14350 +    case 'l': {
 1.14351 +      /*    localtime
 1.14352 +      **
 1.14353 +      ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
 1.14354 +      ** show local time.
 1.14355 +      */
 1.14356 +      if( strcmp(z, "localtime")==0 ){
 1.14357 +        computeJD(p);
 1.14358 +        p->iJD += localtimeOffset(p, pCtx, &rc);
 1.14359 +        clearYMD_HMS_TZ(p);
 1.14360 +      }
 1.14361 +      break;
 1.14362 +    }
 1.14363 +#endif
 1.14364 +    case 'u': {
 1.14365 +      /*
 1.14366 +      **    unixepoch
 1.14367 +      **
 1.14368 +      ** Treat the current value of p->iJD as the number of
 1.14369 +      ** seconds since 1970.  Convert to a real julian day number.
 1.14370 +      */
 1.14371 +      if( strcmp(z, "unixepoch")==0 && p->validJD ){
 1.14372 +        p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
 1.14373 +        clearYMD_HMS_TZ(p);
 1.14374 +        rc = 0;
 1.14375 +      }
 1.14376 +#ifndef SQLITE_OMIT_LOCALTIME
 1.14377 +      else if( strcmp(z, "utc")==0 ){
 1.14378 +        sqlite3_int64 c1;
 1.14379 +        computeJD(p);
 1.14380 +        c1 = localtimeOffset(p, pCtx, &rc);
 1.14381 +        if( rc==SQLITE_OK ){
 1.14382 +          p->iJD -= c1;
 1.14383 +          clearYMD_HMS_TZ(p);
 1.14384 +          p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
 1.14385 +        }
 1.14386 +      }
 1.14387 +#endif
 1.14388 +      break;
 1.14389 +    }
 1.14390 +    case 'w': {
 1.14391 +      /*
 1.14392 +      **    weekday N
 1.14393 +      **
 1.14394 +      ** Move the date to the same time on the next occurrence of
 1.14395 +      ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
 1.14396 +      ** date is already on the appropriate weekday, this is a no-op.
 1.14397 +      */
 1.14398 +      if( strncmp(z, "weekday ", 8)==0
 1.14399 +               && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
 1.14400 +               && (n=(int)r)==r && n>=0 && r<7 ){
 1.14401 +        sqlite3_int64 Z;
 1.14402 +        computeYMD_HMS(p);
 1.14403 +        p->validTZ = 0;
 1.14404 +        p->validJD = 0;
 1.14405 +        computeJD(p);
 1.14406 +        Z = ((p->iJD + 129600000)/86400000) % 7;
 1.14407 +        if( Z>n ) Z -= 7;
 1.14408 +        p->iJD += (n - Z)*86400000;
 1.14409 +        clearYMD_HMS_TZ(p);
 1.14410 +        rc = 0;
 1.14411 +      }
 1.14412 +      break;
 1.14413 +    }
 1.14414 +    case 's': {
 1.14415 +      /*
 1.14416 +      **    start of TTTTT
 1.14417 +      **
 1.14418 +      ** Move the date backwards to the beginning of the current day,
 1.14419 +      ** or month or year.
 1.14420 +      */
 1.14421 +      if( strncmp(z, "start of ", 9)!=0 ) break;
 1.14422 +      z += 9;
 1.14423 +      computeYMD(p);
 1.14424 +      p->validHMS = 1;
 1.14425 +      p->h = p->m = 0;
 1.14426 +      p->s = 0.0;
 1.14427 +      p->validTZ = 0;
 1.14428 +      p->validJD = 0;
 1.14429 +      if( strcmp(z,"month")==0 ){
 1.14430 +        p->D = 1;
 1.14431 +        rc = 0;
 1.14432 +      }else if( strcmp(z,"year")==0 ){
 1.14433 +        computeYMD(p);
 1.14434 +        p->M = 1;
 1.14435 +        p->D = 1;
 1.14436 +        rc = 0;
 1.14437 +      }else if( strcmp(z,"day")==0 ){
 1.14438 +        rc = 0;
 1.14439 +      }
 1.14440 +      break;
 1.14441 +    }
 1.14442 +    case '+':
 1.14443 +    case '-':
 1.14444 +    case '0':
 1.14445 +    case '1':
 1.14446 +    case '2':
 1.14447 +    case '3':
 1.14448 +    case '4':
 1.14449 +    case '5':
 1.14450 +    case '6':
 1.14451 +    case '7':
 1.14452 +    case '8':
 1.14453 +    case '9': {
 1.14454 +      double rRounder;
 1.14455 +      for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
 1.14456 +      if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
 1.14457 +        rc = 1;
 1.14458 +        break;
 1.14459 +      }
 1.14460 +      if( z[n]==':' ){
 1.14461 +        /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
 1.14462 +        ** specified number of hours, minutes, seconds, and fractional seconds
 1.14463 +        ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
 1.14464 +        ** omitted.
 1.14465 +        */
 1.14466 +        const char *z2 = z;
 1.14467 +        DateTime tx;
 1.14468 +        sqlite3_int64 day;
 1.14469 +        if( !sqlite3Isdigit(*z2) ) z2++;
 1.14470 +        memset(&tx, 0, sizeof(tx));
 1.14471 +        if( parseHhMmSs(z2, &tx) ) break;
 1.14472 +        computeJD(&tx);
 1.14473 +        tx.iJD -= 43200000;
 1.14474 +        day = tx.iJD/86400000;
 1.14475 +        tx.iJD -= day*86400000;
 1.14476 +        if( z[0]=='-' ) tx.iJD = -tx.iJD;
 1.14477 +        computeJD(p);
 1.14478 +        clearYMD_HMS_TZ(p);
 1.14479 +        p->iJD += tx.iJD;
 1.14480 +        rc = 0;
 1.14481 +        break;
 1.14482 +      }
 1.14483 +      z += n;
 1.14484 +      while( sqlite3Isspace(*z) ) z++;
 1.14485 +      n = sqlite3Strlen30(z);
 1.14486 +      if( n>10 || n<3 ) break;
 1.14487 +      if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
 1.14488 +      computeJD(p);
 1.14489 +      rc = 0;
 1.14490 +      rRounder = r<0 ? -0.5 : +0.5;
 1.14491 +      if( n==3 && strcmp(z,"day")==0 ){
 1.14492 +        p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
 1.14493 +      }else if( n==4 && strcmp(z,"hour")==0 ){
 1.14494 +        p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
 1.14495 +      }else if( n==6 && strcmp(z,"minute")==0 ){
 1.14496 +        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
 1.14497 +      }else if( n==6 && strcmp(z,"second")==0 ){
 1.14498 +        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
 1.14499 +      }else if( n==5 && strcmp(z,"month")==0 ){
 1.14500 +        int x, y;
 1.14501 +        computeYMD_HMS(p);
 1.14502 +        p->M += (int)r;
 1.14503 +        x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
 1.14504 +        p->Y += x;
 1.14505 +        p->M -= x*12;
 1.14506 +        p->validJD = 0;
 1.14507 +        computeJD(p);
 1.14508 +        y = (int)r;
 1.14509 +        if( y!=r ){
 1.14510 +          p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
 1.14511 +        }
 1.14512 +      }else if( n==4 && strcmp(z,"year")==0 ){
 1.14513 +        int y = (int)r;
 1.14514 +        computeYMD_HMS(p);
 1.14515 +        p->Y += y;
 1.14516 +        p->validJD = 0;
 1.14517 +        computeJD(p);
 1.14518 +        if( y!=r ){
 1.14519 +          p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
 1.14520 +        }
 1.14521 +      }else{
 1.14522 +        rc = 1;
 1.14523 +      }
 1.14524 +      clearYMD_HMS_TZ(p);
 1.14525 +      break;
 1.14526 +    }
 1.14527 +    default: {
 1.14528 +      break;
 1.14529 +    }
 1.14530 +  }
 1.14531 +  return rc;
 1.14532 +}
 1.14533 +
 1.14534 +/*
 1.14535 +** Process time function arguments.  argv[0] is a date-time stamp.
 1.14536 +** argv[1] and following are modifiers.  Parse them all and write
 1.14537 +** the resulting time into the DateTime structure p.  Return 0
 1.14538 +** on success and 1 if there are any errors.
 1.14539 +**
 1.14540 +** If there are zero parameters (if even argv[0] is undefined)
 1.14541 +** then assume a default value of "now" for argv[0].
 1.14542 +*/
 1.14543 +static int isDate(
 1.14544 +  sqlite3_context *context, 
 1.14545 +  int argc, 
 1.14546 +  sqlite3_value **argv, 
 1.14547 +  DateTime *p
 1.14548 +){
 1.14549 +  int i;
 1.14550 +  const unsigned char *z;
 1.14551 +  int eType;
 1.14552 +  memset(p, 0, sizeof(*p));
 1.14553 +  if( argc==0 ){
 1.14554 +    return setDateTimeToCurrent(context, p);
 1.14555 +  }
 1.14556 +  if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
 1.14557 +                   || eType==SQLITE_INTEGER ){
 1.14558 +    p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
 1.14559 +    p->validJD = 1;
 1.14560 +  }else{
 1.14561 +    z = sqlite3_value_text(argv[0]);
 1.14562 +    if( !z || parseDateOrTime(context, (char*)z, p) ){
 1.14563 +      return 1;
 1.14564 +    }
 1.14565 +  }
 1.14566 +  for(i=1; i<argc; i++){
 1.14567 +    z = sqlite3_value_text(argv[i]);
 1.14568 +    if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
 1.14569 +  }
 1.14570 +  return 0;
 1.14571 +}
 1.14572 +
 1.14573 +
 1.14574 +/*
 1.14575 +** The following routines implement the various date and time functions
 1.14576 +** of SQLite.
 1.14577 +*/
 1.14578 +
 1.14579 +/*
 1.14580 +**    julianday( TIMESTRING, MOD, MOD, ...)
 1.14581 +**
 1.14582 +** Return the julian day number of the date specified in the arguments
 1.14583 +*/
 1.14584 +static void juliandayFunc(
 1.14585 +  sqlite3_context *context,
 1.14586 +  int argc,
 1.14587 +  sqlite3_value **argv
 1.14588 +){
 1.14589 +  DateTime x;
 1.14590 +  if( isDate(context, argc, argv, &x)==0 ){
 1.14591 +    computeJD(&x);
 1.14592 +    sqlite3_result_double(context, x.iJD/86400000.0);
 1.14593 +  }
 1.14594 +}
 1.14595 +
 1.14596 +/*
 1.14597 +**    datetime( TIMESTRING, MOD, MOD, ...)
 1.14598 +**
 1.14599 +** Return YYYY-MM-DD HH:MM:SS
 1.14600 +*/
 1.14601 +static void datetimeFunc(
 1.14602 +  sqlite3_context *context,
 1.14603 +  int argc,
 1.14604 +  sqlite3_value **argv
 1.14605 +){
 1.14606 +  DateTime x;
 1.14607 +  if( isDate(context, argc, argv, &x)==0 ){
 1.14608 +    char zBuf[100];
 1.14609 +    computeYMD_HMS(&x);
 1.14610 +    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
 1.14611 +                     x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
 1.14612 +    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 1.14613 +  }
 1.14614 +}
 1.14615 +
 1.14616 +/*
 1.14617 +**    time( TIMESTRING, MOD, MOD, ...)
 1.14618 +**
 1.14619 +** Return HH:MM:SS
 1.14620 +*/
 1.14621 +static void timeFunc(
 1.14622 +  sqlite3_context *context,
 1.14623 +  int argc,
 1.14624 +  sqlite3_value **argv
 1.14625 +){
 1.14626 +  DateTime x;
 1.14627 +  if( isDate(context, argc, argv, &x)==0 ){
 1.14628 +    char zBuf[100];
 1.14629 +    computeHMS(&x);
 1.14630 +    sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
 1.14631 +    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 1.14632 +  }
 1.14633 +}
 1.14634 +
 1.14635 +/*
 1.14636 +**    date( TIMESTRING, MOD, MOD, ...)
 1.14637 +**
 1.14638 +** Return YYYY-MM-DD
 1.14639 +*/
 1.14640 +static void dateFunc(
 1.14641 +  sqlite3_context *context,
 1.14642 +  int argc,
 1.14643 +  sqlite3_value **argv
 1.14644 +){
 1.14645 +  DateTime x;
 1.14646 +  if( isDate(context, argc, argv, &x)==0 ){
 1.14647 +    char zBuf[100];
 1.14648 +    computeYMD(&x);
 1.14649 +    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
 1.14650 +    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 1.14651 +  }
 1.14652 +}
 1.14653 +
 1.14654 +/*
 1.14655 +**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
 1.14656 +**
 1.14657 +** Return a string described by FORMAT.  Conversions as follows:
 1.14658 +**
 1.14659 +**   %d  day of month
 1.14660 +**   %f  ** fractional seconds  SS.SSS
 1.14661 +**   %H  hour 00-24
 1.14662 +**   %j  day of year 000-366
 1.14663 +**   %J  ** Julian day number
 1.14664 +**   %m  month 01-12
 1.14665 +**   %M  minute 00-59
 1.14666 +**   %s  seconds since 1970-01-01
 1.14667 +**   %S  seconds 00-59
 1.14668 +**   %w  day of week 0-6  sunday==0
 1.14669 +**   %W  week of year 00-53
 1.14670 +**   %Y  year 0000-9999
 1.14671 +**   %%  %
 1.14672 +*/
 1.14673 +static void strftimeFunc(
 1.14674 +  sqlite3_context *context,
 1.14675 +  int argc,
 1.14676 +  sqlite3_value **argv
 1.14677 +){
 1.14678 +  DateTime x;
 1.14679 +  u64 n;
 1.14680 +  size_t i,j;
 1.14681 +  char *z;
 1.14682 +  sqlite3 *db;
 1.14683 +  const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
 1.14684 +  char zBuf[100];
 1.14685 +  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
 1.14686 +  db = sqlite3_context_db_handle(context);
 1.14687 +  for(i=0, n=1; zFmt[i]; i++, n++){
 1.14688 +    if( zFmt[i]=='%' ){
 1.14689 +      switch( zFmt[i+1] ){
 1.14690 +        case 'd':
 1.14691 +        case 'H':
 1.14692 +        case 'm':
 1.14693 +        case 'M':
 1.14694 +        case 'S':
 1.14695 +        case 'W':
 1.14696 +          n++;
 1.14697 +          /* fall thru */
 1.14698 +        case 'w':
 1.14699 +        case '%':
 1.14700 +          break;
 1.14701 +        case 'f':
 1.14702 +          n += 8;
 1.14703 +          break;
 1.14704 +        case 'j':
 1.14705 +          n += 3;
 1.14706 +          break;
 1.14707 +        case 'Y':
 1.14708 +          n += 8;
 1.14709 +          break;
 1.14710 +        case 's':
 1.14711 +        case 'J':
 1.14712 +          n += 50;
 1.14713 +          break;
 1.14714 +        default:
 1.14715 +          return;  /* ERROR.  return a NULL */
 1.14716 +      }
 1.14717 +      i++;
 1.14718 +    }
 1.14719 +  }
 1.14720 +  testcase( n==sizeof(zBuf)-1 );
 1.14721 +  testcase( n==sizeof(zBuf) );
 1.14722 +  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 1.14723 +  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
 1.14724 +  if( n<sizeof(zBuf) ){
 1.14725 +    z = zBuf;
 1.14726 +  }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.14727 +    sqlite3_result_error_toobig(context);
 1.14728 +    return;
 1.14729 +  }else{
 1.14730 +    z = sqlite3DbMallocRaw(db, (int)n);
 1.14731 +    if( z==0 ){
 1.14732 +      sqlite3_result_error_nomem(context);
 1.14733 +      return;
 1.14734 +    }
 1.14735 +  }
 1.14736 +  computeJD(&x);
 1.14737 +  computeYMD_HMS(&x);
 1.14738 +  for(i=j=0; zFmt[i]; i++){
 1.14739 +    if( zFmt[i]!='%' ){
 1.14740 +      z[j++] = zFmt[i];
 1.14741 +    }else{
 1.14742 +      i++;
 1.14743 +      switch( zFmt[i] ){
 1.14744 +        case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
 1.14745 +        case 'f': {
 1.14746 +          double s = x.s;
 1.14747 +          if( s>59.999 ) s = 59.999;
 1.14748 +          sqlite3_snprintf(7, &z[j],"%06.3f", s);
 1.14749 +          j += sqlite3Strlen30(&z[j]);
 1.14750 +          break;
 1.14751 +        }
 1.14752 +        case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
 1.14753 +        case 'W': /* Fall thru */
 1.14754 +        case 'j': {
 1.14755 +          int nDay;             /* Number of days since 1st day of year */
 1.14756 +          DateTime y = x;
 1.14757 +          y.validJD = 0;
 1.14758 +          y.M = 1;
 1.14759 +          y.D = 1;
 1.14760 +          computeJD(&y);
 1.14761 +          nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
 1.14762 +          if( zFmt[i]=='W' ){
 1.14763 +            int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
 1.14764 +            wd = (int)(((x.iJD+43200000)/86400000)%7);
 1.14765 +            sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
 1.14766 +            j += 2;
 1.14767 +          }else{
 1.14768 +            sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
 1.14769 +            j += 3;
 1.14770 +          }
 1.14771 +          break;
 1.14772 +        }
 1.14773 +        case 'J': {
 1.14774 +          sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
 1.14775 +          j+=sqlite3Strlen30(&z[j]);
 1.14776 +          break;
 1.14777 +        }
 1.14778 +        case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
 1.14779 +        case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
 1.14780 +        case 's': {
 1.14781 +          sqlite3_snprintf(30,&z[j],"%lld",
 1.14782 +                           (i64)(x.iJD/1000 - 21086676*(i64)10000));
 1.14783 +          j += sqlite3Strlen30(&z[j]);
 1.14784 +          break;
 1.14785 +        }
 1.14786 +        case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
 1.14787 +        case 'w': {
 1.14788 +          z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
 1.14789 +          break;
 1.14790 +        }
 1.14791 +        case 'Y': {
 1.14792 +          sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
 1.14793 +          break;
 1.14794 +        }
 1.14795 +        default:   z[j++] = '%'; break;
 1.14796 +      }
 1.14797 +    }
 1.14798 +  }
 1.14799 +  z[j] = 0;
 1.14800 +  sqlite3_result_text(context, z, -1,
 1.14801 +                      z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
 1.14802 +}
 1.14803 +
 1.14804 +/*
 1.14805 +** current_time()
 1.14806 +**
 1.14807 +** This function returns the same value as time('now').
 1.14808 +*/
 1.14809 +static void ctimeFunc(
 1.14810 +  sqlite3_context *context,
 1.14811 +  int NotUsed,
 1.14812 +  sqlite3_value **NotUsed2
 1.14813 +){
 1.14814 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.14815 +  timeFunc(context, 0, 0);
 1.14816 +}
 1.14817 +
 1.14818 +/*
 1.14819 +** current_date()
 1.14820 +**
 1.14821 +** This function returns the same value as date('now').
 1.14822 +*/
 1.14823 +static void cdateFunc(
 1.14824 +  sqlite3_context *context,
 1.14825 +  int NotUsed,
 1.14826 +  sqlite3_value **NotUsed2
 1.14827 +){
 1.14828 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.14829 +  dateFunc(context, 0, 0);
 1.14830 +}
 1.14831 +
 1.14832 +/*
 1.14833 +** current_timestamp()
 1.14834 +**
 1.14835 +** This function returns the same value as datetime('now').
 1.14836 +*/
 1.14837 +static void ctimestampFunc(
 1.14838 +  sqlite3_context *context,
 1.14839 +  int NotUsed,
 1.14840 +  sqlite3_value **NotUsed2
 1.14841 +){
 1.14842 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.14843 +  datetimeFunc(context, 0, 0);
 1.14844 +}
 1.14845 +#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
 1.14846 +
 1.14847 +#ifdef SQLITE_OMIT_DATETIME_FUNCS
 1.14848 +/*
 1.14849 +** If the library is compiled to omit the full-scale date and time
 1.14850 +** handling (to get a smaller binary), the following minimal version
 1.14851 +** of the functions current_time(), current_date() and current_timestamp()
 1.14852 +** are included instead. This is to support column declarations that
 1.14853 +** include "DEFAULT CURRENT_TIME" etc.
 1.14854 +**
 1.14855 +** This function uses the C-library functions time(), gmtime()
 1.14856 +** and strftime(). The format string to pass to strftime() is supplied
 1.14857 +** as the user-data for the function.
 1.14858 +*/
 1.14859 +static void currentTimeFunc(
 1.14860 +  sqlite3_context *context,
 1.14861 +  int argc,
 1.14862 +  sqlite3_value **argv
 1.14863 +){
 1.14864 +  time_t t;
 1.14865 +  char *zFormat = (char *)sqlite3_user_data(context);
 1.14866 +  sqlite3 *db;
 1.14867 +  sqlite3_int64 iT;
 1.14868 +  struct tm *pTm;
 1.14869 +  struct tm sNow;
 1.14870 +  char zBuf[20];
 1.14871 +
 1.14872 +  UNUSED_PARAMETER(argc);
 1.14873 +  UNUSED_PARAMETER(argv);
 1.14874 +
 1.14875 +  db = sqlite3_context_db_handle(context);
 1.14876 +  if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return;
 1.14877 +  t = iT/1000 - 10000*(sqlite3_int64)21086676;
 1.14878 +#ifdef HAVE_GMTIME_R
 1.14879 +  pTm = gmtime_r(&t, &sNow);
 1.14880 +#else
 1.14881 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.14882 +  pTm = gmtime(&t);
 1.14883 +  if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
 1.14884 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.14885 +#endif
 1.14886 +  if( pTm ){
 1.14887 +    strftime(zBuf, 20, zFormat, &sNow);
 1.14888 +    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 1.14889 +  }
 1.14890 +}
 1.14891 +#endif
 1.14892 +
 1.14893 +/*
 1.14894 +** This function registered all of the above C functions as SQL
 1.14895 +** functions.  This should be the only routine in this file with
 1.14896 +** external linkage.
 1.14897 +*/
 1.14898 +SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
 1.14899 +  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
 1.14900 +#ifndef SQLITE_OMIT_DATETIME_FUNCS
 1.14901 +    FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
 1.14902 +    FUNCTION(date,             -1, 0, 0, dateFunc      ),
 1.14903 +    FUNCTION(time,             -1, 0, 0, timeFunc      ),
 1.14904 +    FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
 1.14905 +    FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
 1.14906 +    FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
 1.14907 +    FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
 1.14908 +    FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
 1.14909 +#else
 1.14910 +    STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
 1.14911 +    STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
 1.14912 +    STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
 1.14913 +#endif
 1.14914 +  };
 1.14915 +  int i;
 1.14916 +  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 1.14917 +  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
 1.14918 +
 1.14919 +  for(i=0; i<ArraySize(aDateTimeFuncs); i++){
 1.14920 +    sqlite3FuncDefInsert(pHash, &aFunc[i]);
 1.14921 +  }
 1.14922 +}
 1.14923 +
 1.14924 +/************** End of date.c ************************************************/
 1.14925 +/************** Begin file os.c **********************************************/
 1.14926 +/*
 1.14927 +** 2005 November 29
 1.14928 +**
 1.14929 +** The author disclaims copyright to this source code.  In place of
 1.14930 +** a legal notice, here is a blessing:
 1.14931 +**
 1.14932 +**    May you do good and not evil.
 1.14933 +**    May you find forgiveness for yourself and forgive others.
 1.14934 +**    May you share freely, never taking more than you give.
 1.14935 +**
 1.14936 +******************************************************************************
 1.14937 +**
 1.14938 +** This file contains OS interface code that is common to all
 1.14939 +** architectures.
 1.14940 +*/
 1.14941 +#define _SQLITE_OS_C_ 1
 1.14942 +#undef _SQLITE_OS_C_
 1.14943 +
 1.14944 +/*
 1.14945 +** The default SQLite sqlite3_vfs implementations do not allocate
 1.14946 +** memory (actually, os_unix.c allocates a small amount of memory
 1.14947 +** from within OsOpen()), but some third-party implementations may.
 1.14948 +** So we test the effects of a malloc() failing and the sqlite3OsXXX()
 1.14949 +** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
 1.14950 +**
 1.14951 +** The following functions are instrumented for malloc() failure 
 1.14952 +** testing:
 1.14953 +**
 1.14954 +**     sqlite3OsRead()
 1.14955 +**     sqlite3OsWrite()
 1.14956 +**     sqlite3OsSync()
 1.14957 +**     sqlite3OsFileSize()
 1.14958 +**     sqlite3OsLock()
 1.14959 +**     sqlite3OsCheckReservedLock()
 1.14960 +**     sqlite3OsFileControl()
 1.14961 +**     sqlite3OsShmMap()
 1.14962 +**     sqlite3OsOpen()
 1.14963 +**     sqlite3OsDelete()
 1.14964 +**     sqlite3OsAccess()
 1.14965 +**     sqlite3OsFullPathname()
 1.14966 +**
 1.14967 +*/
 1.14968 +#if defined(SQLITE_TEST)
 1.14969 +SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
 1.14970 +  #define DO_OS_MALLOC_TEST(x)                                       \
 1.14971 +  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
 1.14972 +    void *pTstAlloc = sqlite3Malloc(10);                             \
 1.14973 +    if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
 1.14974 +    sqlite3_free(pTstAlloc);                                         \
 1.14975 +  }
 1.14976 +#else
 1.14977 +  #define DO_OS_MALLOC_TEST(x)
 1.14978 +#endif
 1.14979 +
 1.14980 +/*
 1.14981 +** The following routines are convenience wrappers around methods
 1.14982 +** of the sqlite3_file object.  This is mostly just syntactic sugar. All
 1.14983 +** of this would be completely automatic if SQLite were coded using
 1.14984 +** C++ instead of plain old C.
 1.14985 +*/
 1.14986 +SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
 1.14987 +  int rc = SQLITE_OK;
 1.14988 +  if( pId->pMethods ){
 1.14989 +    rc = pId->pMethods->xClose(pId);
 1.14990 +    pId->pMethods = 0;
 1.14991 +  }
 1.14992 +  return rc;
 1.14993 +}
 1.14994 +SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
 1.14995 +  DO_OS_MALLOC_TEST(id);
 1.14996 +  return id->pMethods->xRead(id, pBuf, amt, offset);
 1.14997 +}
 1.14998 +SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
 1.14999 +  DO_OS_MALLOC_TEST(id);
 1.15000 +  return id->pMethods->xWrite(id, pBuf, amt, offset);
 1.15001 +}
 1.15002 +SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
 1.15003 +  return id->pMethods->xTruncate(id, size);
 1.15004 +}
 1.15005 +SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
 1.15006 +  DO_OS_MALLOC_TEST(id);
 1.15007 +  return id->pMethods->xSync(id, flags);
 1.15008 +}
 1.15009 +SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
 1.15010 +  DO_OS_MALLOC_TEST(id);
 1.15011 +  return id->pMethods->xFileSize(id, pSize);
 1.15012 +}
 1.15013 +SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
 1.15014 +  DO_OS_MALLOC_TEST(id);
 1.15015 +  return id->pMethods->xLock(id, lockType);
 1.15016 +}
 1.15017 +SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
 1.15018 +  return id->pMethods->xUnlock(id, lockType);
 1.15019 +}
 1.15020 +SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
 1.15021 +  DO_OS_MALLOC_TEST(id);
 1.15022 +  return id->pMethods->xCheckReservedLock(id, pResOut);
 1.15023 +}
 1.15024 +
 1.15025 +/*
 1.15026 +** Use sqlite3OsFileControl() when we are doing something that might fail
 1.15027 +** and we need to know about the failures.  Use sqlite3OsFileControlHint()
 1.15028 +** when simply tossing information over the wall to the VFS and we do not
 1.15029 +** really care if the VFS receives and understands the information since it
 1.15030 +** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
 1.15031 +** routine has no return value since the return value would be meaningless.
 1.15032 +*/
 1.15033 +SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
 1.15034 +  DO_OS_MALLOC_TEST(id);
 1.15035 +  return id->pMethods->xFileControl(id, op, pArg);
 1.15036 +}
 1.15037 +SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
 1.15038 +  (void)id->pMethods->xFileControl(id, op, pArg);
 1.15039 +}
 1.15040 +
 1.15041 +SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
 1.15042 +  int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
 1.15043 +  return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
 1.15044 +}
 1.15045 +SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
 1.15046 +  return id->pMethods->xDeviceCharacteristics(id);
 1.15047 +}
 1.15048 +SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
 1.15049 +  return id->pMethods->xShmLock(id, offset, n, flags);
 1.15050 +}
 1.15051 +SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
 1.15052 +  id->pMethods->xShmBarrier(id);
 1.15053 +}
 1.15054 +SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
 1.15055 +  return id->pMethods->xShmUnmap(id, deleteFlag);
 1.15056 +}
 1.15057 +SQLITE_PRIVATE int sqlite3OsShmMap(
 1.15058 +  sqlite3_file *id,               /* Database file handle */
 1.15059 +  int iPage,
 1.15060 +  int pgsz,
 1.15061 +  int bExtend,                    /* True to extend file if necessary */
 1.15062 +  void volatile **pp              /* OUT: Pointer to mapping */
 1.15063 +){
 1.15064 +  DO_OS_MALLOC_TEST(id);
 1.15065 +  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
 1.15066 +}
 1.15067 +
 1.15068 +/*
 1.15069 +** The next group of routines are convenience wrappers around the
 1.15070 +** VFS methods.
 1.15071 +*/
 1.15072 +SQLITE_PRIVATE int sqlite3OsOpen(
 1.15073 +  sqlite3_vfs *pVfs, 
 1.15074 +  const char *zPath, 
 1.15075 +  sqlite3_file *pFile, 
 1.15076 +  int flags, 
 1.15077 +  int *pFlagsOut
 1.15078 +){
 1.15079 +  int rc;
 1.15080 +  DO_OS_MALLOC_TEST(0);
 1.15081 +  /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
 1.15082 +  ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
 1.15083 +  ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
 1.15084 +  ** reaching the VFS. */
 1.15085 +  rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
 1.15086 +  assert( rc==SQLITE_OK || pFile->pMethods==0 );
 1.15087 +  return rc;
 1.15088 +}
 1.15089 +SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
 1.15090 +  DO_OS_MALLOC_TEST(0);
 1.15091 +  assert( dirSync==0 || dirSync==1 );
 1.15092 +  return pVfs->xDelete(pVfs, zPath, dirSync);
 1.15093 +}
 1.15094 +SQLITE_PRIVATE int sqlite3OsAccess(
 1.15095 +  sqlite3_vfs *pVfs, 
 1.15096 +  const char *zPath, 
 1.15097 +  int flags, 
 1.15098 +  int *pResOut
 1.15099 +){
 1.15100 +  DO_OS_MALLOC_TEST(0);
 1.15101 +  return pVfs->xAccess(pVfs, zPath, flags, pResOut);
 1.15102 +}
 1.15103 +SQLITE_PRIVATE int sqlite3OsFullPathname(
 1.15104 +  sqlite3_vfs *pVfs, 
 1.15105 +  const char *zPath, 
 1.15106 +  int nPathOut, 
 1.15107 +  char *zPathOut
 1.15108 +){
 1.15109 +  DO_OS_MALLOC_TEST(0);
 1.15110 +  zPathOut[0] = 0;
 1.15111 +  return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
 1.15112 +}
 1.15113 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
 1.15114 +SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
 1.15115 +  return pVfs->xDlOpen(pVfs, zPath);
 1.15116 +}
 1.15117 +SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
 1.15118 +  pVfs->xDlError(pVfs, nByte, zBufOut);
 1.15119 +}
 1.15120 +SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
 1.15121 +  return pVfs->xDlSym(pVfs, pHdle, zSym);
 1.15122 +}
 1.15123 +SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
 1.15124 +  pVfs->xDlClose(pVfs, pHandle);
 1.15125 +}
 1.15126 +#endif /* SQLITE_OMIT_LOAD_EXTENSION */
 1.15127 +SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
 1.15128 +  return pVfs->xRandomness(pVfs, nByte, zBufOut);
 1.15129 +}
 1.15130 +SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
 1.15131 +  return pVfs->xSleep(pVfs, nMicro);
 1.15132 +}
 1.15133 +SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
 1.15134 +  int rc;
 1.15135 +  /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
 1.15136 +  ** method to get the current date and time if that method is available
 1.15137 +  ** (if iVersion is 2 or greater and the function pointer is not NULL) and
 1.15138 +  ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
 1.15139 +  ** unavailable.
 1.15140 +  */
 1.15141 +  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
 1.15142 +    rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
 1.15143 +  }else{
 1.15144 +    double r;
 1.15145 +    rc = pVfs->xCurrentTime(pVfs, &r);
 1.15146 +    *pTimeOut = (sqlite3_int64)(r*86400000.0);
 1.15147 +  }
 1.15148 +  return rc;
 1.15149 +}
 1.15150 +
 1.15151 +SQLITE_PRIVATE int sqlite3OsOpenMalloc(
 1.15152 +  sqlite3_vfs *pVfs, 
 1.15153 +  const char *zFile, 
 1.15154 +  sqlite3_file **ppFile, 
 1.15155 +  int flags,
 1.15156 +  int *pOutFlags
 1.15157 +){
 1.15158 +  int rc = SQLITE_NOMEM;
 1.15159 +  sqlite3_file *pFile;
 1.15160 +  pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
 1.15161 +  if( pFile ){
 1.15162 +    rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
 1.15163 +    if( rc!=SQLITE_OK ){
 1.15164 +      sqlite3_free(pFile);
 1.15165 +    }else{
 1.15166 +      *ppFile = pFile;
 1.15167 +    }
 1.15168 +  }
 1.15169 +  return rc;
 1.15170 +}
 1.15171 +SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
 1.15172 +  int rc = SQLITE_OK;
 1.15173 +  assert( pFile );
 1.15174 +  rc = sqlite3OsClose(pFile);
 1.15175 +  sqlite3_free(pFile);
 1.15176 +  return rc;
 1.15177 +}
 1.15178 +
 1.15179 +/*
 1.15180 +** This function is a wrapper around the OS specific implementation of
 1.15181 +** sqlite3_os_init(). The purpose of the wrapper is to provide the
 1.15182 +** ability to simulate a malloc failure, so that the handling of an
 1.15183 +** error in sqlite3_os_init() by the upper layers can be tested.
 1.15184 +*/
 1.15185 +SQLITE_PRIVATE int sqlite3OsInit(void){
 1.15186 +  void *p = sqlite3_malloc(10);
 1.15187 +  if( p==0 ) return SQLITE_NOMEM;
 1.15188 +  sqlite3_free(p);
 1.15189 +  return sqlite3_os_init();
 1.15190 +}
 1.15191 +
 1.15192 +/*
 1.15193 +** The list of all registered VFS implementations.
 1.15194 +*/
 1.15195 +static sqlite3_vfs * SQLITE_WSD vfsList = 0;
 1.15196 +#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
 1.15197 +
 1.15198 +/*
 1.15199 +** Locate a VFS by name.  If no name is given, simply return the
 1.15200 +** first VFS on the list.
 1.15201 +*/
 1.15202 +SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
 1.15203 +  sqlite3_vfs *pVfs = 0;
 1.15204 +#if SQLITE_THREADSAFE
 1.15205 +  sqlite3_mutex *mutex;
 1.15206 +#endif
 1.15207 +#ifndef SQLITE_OMIT_AUTOINIT
 1.15208 +  int rc = sqlite3_initialize();
 1.15209 +  if( rc ) return 0;
 1.15210 +#endif
 1.15211 +#if SQLITE_THREADSAFE
 1.15212 +  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 1.15213 +#endif
 1.15214 +  sqlite3_mutex_enter(mutex);
 1.15215 +  for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
 1.15216 +    if( zVfs==0 ) break;
 1.15217 +    if( strcmp(zVfs, pVfs->zName)==0 ) break;
 1.15218 +  }
 1.15219 +  sqlite3_mutex_leave(mutex);
 1.15220 +  return pVfs;
 1.15221 +}
 1.15222 +
 1.15223 +/*
 1.15224 +** Unlink a VFS from the linked list
 1.15225 +*/
 1.15226 +static void vfsUnlink(sqlite3_vfs *pVfs){
 1.15227 +  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
 1.15228 +  if( pVfs==0 ){
 1.15229 +    /* No-op */
 1.15230 +  }else if( vfsList==pVfs ){
 1.15231 +    vfsList = pVfs->pNext;
 1.15232 +  }else if( vfsList ){
 1.15233 +    sqlite3_vfs *p = vfsList;
 1.15234 +    while( p->pNext && p->pNext!=pVfs ){
 1.15235 +      p = p->pNext;
 1.15236 +    }
 1.15237 +    if( p->pNext==pVfs ){
 1.15238 +      p->pNext = pVfs->pNext;
 1.15239 +    }
 1.15240 +  }
 1.15241 +}
 1.15242 +
 1.15243 +/*
 1.15244 +** Register a VFS with the system.  It is harmless to register the same
 1.15245 +** VFS multiple times.  The new VFS becomes the default if makeDflt is
 1.15246 +** true.
 1.15247 +*/
 1.15248 +SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
 1.15249 +  MUTEX_LOGIC(sqlite3_mutex *mutex;)
 1.15250 +#ifndef SQLITE_OMIT_AUTOINIT
 1.15251 +  int rc = sqlite3_initialize();
 1.15252 +  if( rc ) return rc;
 1.15253 +#endif
 1.15254 +  MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 1.15255 +  sqlite3_mutex_enter(mutex);
 1.15256 +  vfsUnlink(pVfs);
 1.15257 +  if( makeDflt || vfsList==0 ){
 1.15258 +    pVfs->pNext = vfsList;
 1.15259 +    vfsList = pVfs;
 1.15260 +  }else{
 1.15261 +    pVfs->pNext = vfsList->pNext;
 1.15262 +    vfsList->pNext = pVfs;
 1.15263 +  }
 1.15264 +  assert(vfsList);
 1.15265 +  sqlite3_mutex_leave(mutex);
 1.15266 +  return SQLITE_OK;
 1.15267 +}
 1.15268 +
 1.15269 +/*
 1.15270 +** Unregister a VFS so that it is no longer accessible.
 1.15271 +*/
 1.15272 +SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
 1.15273 +#if SQLITE_THREADSAFE
 1.15274 +  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 1.15275 +#endif
 1.15276 +  sqlite3_mutex_enter(mutex);
 1.15277 +  vfsUnlink(pVfs);
 1.15278 +  sqlite3_mutex_leave(mutex);
 1.15279 +  return SQLITE_OK;
 1.15280 +}
 1.15281 +
 1.15282 +/************** End of os.c **************************************************/
 1.15283 +/************** Begin file fault.c *******************************************/
 1.15284 +/*
 1.15285 +** 2008 Jan 22
 1.15286 +**
 1.15287 +** The author disclaims copyright to this source code.  In place of
 1.15288 +** a legal notice, here is a blessing:
 1.15289 +**
 1.15290 +**    May you do good and not evil.
 1.15291 +**    May you find forgiveness for yourself and forgive others.
 1.15292 +**    May you share freely, never taking more than you give.
 1.15293 +**
 1.15294 +*************************************************************************
 1.15295 +**
 1.15296 +** This file contains code to support the concept of "benign" 
 1.15297 +** malloc failures (when the xMalloc() or xRealloc() method of the
 1.15298 +** sqlite3_mem_methods structure fails to allocate a block of memory
 1.15299 +** and returns 0). 
 1.15300 +**
 1.15301 +** Most malloc failures are non-benign. After they occur, SQLite
 1.15302 +** abandons the current operation and returns an error code (usually
 1.15303 +** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
 1.15304 +** fatal. For example, if a malloc fails while resizing a hash table, this 
 1.15305 +** is completely recoverable simply by not carrying out the resize. The 
 1.15306 +** hash table will continue to function normally.  So a malloc failure 
 1.15307 +** during a hash table resize is a benign fault.
 1.15308 +*/
 1.15309 +
 1.15310 +
 1.15311 +#ifndef SQLITE_OMIT_BUILTIN_TEST
 1.15312 +
 1.15313 +/*
 1.15314 +** Global variables.
 1.15315 +*/
 1.15316 +typedef struct BenignMallocHooks BenignMallocHooks;
 1.15317 +static SQLITE_WSD struct BenignMallocHooks {
 1.15318 +  void (*xBenignBegin)(void);
 1.15319 +  void (*xBenignEnd)(void);
 1.15320 +} sqlite3Hooks = { 0, 0 };
 1.15321 +
 1.15322 +/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
 1.15323 +** structure.  If writable static data is unsupported on the target,
 1.15324 +** we have to locate the state vector at run-time.  In the more common
 1.15325 +** case where writable static data is supported, wsdHooks can refer directly
 1.15326 +** to the "sqlite3Hooks" state vector declared above.
 1.15327 +*/
 1.15328 +#ifdef SQLITE_OMIT_WSD
 1.15329 +# define wsdHooksInit \
 1.15330 +  BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
 1.15331 +# define wsdHooks x[0]
 1.15332 +#else
 1.15333 +# define wsdHooksInit
 1.15334 +# define wsdHooks sqlite3Hooks
 1.15335 +#endif
 1.15336 +
 1.15337 +
 1.15338 +/*
 1.15339 +** Register hooks to call when sqlite3BeginBenignMalloc() and
 1.15340 +** sqlite3EndBenignMalloc() are called, respectively.
 1.15341 +*/
 1.15342 +SQLITE_PRIVATE void sqlite3BenignMallocHooks(
 1.15343 +  void (*xBenignBegin)(void),
 1.15344 +  void (*xBenignEnd)(void)
 1.15345 +){
 1.15346 +  wsdHooksInit;
 1.15347 +  wsdHooks.xBenignBegin = xBenignBegin;
 1.15348 +  wsdHooks.xBenignEnd = xBenignEnd;
 1.15349 +}
 1.15350 +
 1.15351 +/*
 1.15352 +** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
 1.15353 +** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
 1.15354 +** indicates that subsequent malloc failures are non-benign.
 1.15355 +*/
 1.15356 +SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
 1.15357 +  wsdHooksInit;
 1.15358 +  if( wsdHooks.xBenignBegin ){
 1.15359 +    wsdHooks.xBenignBegin();
 1.15360 +  }
 1.15361 +}
 1.15362 +SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
 1.15363 +  wsdHooksInit;
 1.15364 +  if( wsdHooks.xBenignEnd ){
 1.15365 +    wsdHooks.xBenignEnd();
 1.15366 +  }
 1.15367 +}
 1.15368 +
 1.15369 +#endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
 1.15370 +
 1.15371 +/************** End of fault.c ***********************************************/
 1.15372 +/************** Begin file mem0.c ********************************************/
 1.15373 +/*
 1.15374 +** 2008 October 28
 1.15375 +**
 1.15376 +** The author disclaims copyright to this source code.  In place of
 1.15377 +** a legal notice, here is a blessing:
 1.15378 +**
 1.15379 +**    May you do good and not evil.
 1.15380 +**    May you find forgiveness for yourself and forgive others.
 1.15381 +**    May you share freely, never taking more than you give.
 1.15382 +**
 1.15383 +*************************************************************************
 1.15384 +**
 1.15385 +** This file contains a no-op memory allocation drivers for use when
 1.15386 +** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
 1.15387 +** here always fail.  SQLite will not operate with these drivers.  These
 1.15388 +** are merely placeholders.  Real drivers must be substituted using
 1.15389 +** sqlite3_config() before SQLite will operate.
 1.15390 +*/
 1.15391 +
 1.15392 +/*
 1.15393 +** This version of the memory allocator is the default.  It is
 1.15394 +** used when no other memory allocator is specified using compile-time
 1.15395 +** macros.
 1.15396 +*/
 1.15397 +#ifdef SQLITE_ZERO_MALLOC
 1.15398 +
 1.15399 +/*
 1.15400 +** No-op versions of all memory allocation routines
 1.15401 +*/
 1.15402 +static void *sqlite3MemMalloc(int nByte){ return 0; }
 1.15403 +static void sqlite3MemFree(void *pPrior){ return; }
 1.15404 +static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
 1.15405 +static int sqlite3MemSize(void *pPrior){ return 0; }
 1.15406 +static int sqlite3MemRoundup(int n){ return n; }
 1.15407 +static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
 1.15408 +static void sqlite3MemShutdown(void *NotUsed){ return; }
 1.15409 +
 1.15410 +/*
 1.15411 +** This routine is the only routine in this file with external linkage.
 1.15412 +**
 1.15413 +** Populate the low-level memory allocation function pointers in
 1.15414 +** sqlite3GlobalConfig.m with pointers to the routines in this file.
 1.15415 +*/
 1.15416 +SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 1.15417 +  static const sqlite3_mem_methods defaultMethods = {
 1.15418 +     sqlite3MemMalloc,
 1.15419 +     sqlite3MemFree,
 1.15420 +     sqlite3MemRealloc,
 1.15421 +     sqlite3MemSize,
 1.15422 +     sqlite3MemRoundup,
 1.15423 +     sqlite3MemInit,
 1.15424 +     sqlite3MemShutdown,
 1.15425 +     0
 1.15426 +  };
 1.15427 +  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 1.15428 +}
 1.15429 +
 1.15430 +#endif /* SQLITE_ZERO_MALLOC */
 1.15431 +
 1.15432 +/************** End of mem0.c ************************************************/
 1.15433 +/************** Begin file mem1.c ********************************************/
 1.15434 +/*
 1.15435 +** 2007 August 14
 1.15436 +**
 1.15437 +** The author disclaims copyright to this source code.  In place of
 1.15438 +** a legal notice, here is a blessing:
 1.15439 +**
 1.15440 +**    May you do good and not evil.
 1.15441 +**    May you find forgiveness for yourself and forgive others.
 1.15442 +**    May you share freely, never taking more than you give.
 1.15443 +**
 1.15444 +*************************************************************************
 1.15445 +**
 1.15446 +** This file contains low-level memory allocation drivers for when
 1.15447 +** SQLite will use the standard C-library malloc/realloc/free interface
 1.15448 +** to obtain the memory it needs.
 1.15449 +**
 1.15450 +** This file contains implementations of the low-level memory allocation
 1.15451 +** routines specified in the sqlite3_mem_methods object.  The content of
 1.15452 +** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
 1.15453 +** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
 1.15454 +** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
 1.15455 +** default configuration is to use memory allocation routines in this
 1.15456 +** file.
 1.15457 +**
 1.15458 +** C-preprocessor macro summary:
 1.15459 +**
 1.15460 +**    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
 1.15461 +**                                the malloc_usable_size() interface exists
 1.15462 +**                                on the target platform.  Or, this symbol
 1.15463 +**                                can be set manually, if desired.
 1.15464 +**                                If an equivalent interface exists by
 1.15465 +**                                a different name, using a separate -D
 1.15466 +**                                option to rename it.
 1.15467 +**
 1.15468 +**    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
 1.15469 +**                                memory allocator.  Set this symbol to enable
 1.15470 +**                                building on older macs.
 1.15471 +**
 1.15472 +**    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
 1.15473 +**                                _msize() on windows systems.  This might
 1.15474 +**                                be necessary when compiling for Delphi,
 1.15475 +**                                for example.
 1.15476 +*/
 1.15477 +
 1.15478 +/*
 1.15479 +** This version of the memory allocator is the default.  It is
 1.15480 +** used when no other memory allocator is specified using compile-time
 1.15481 +** macros.
 1.15482 +*/
 1.15483 +#ifdef SQLITE_SYSTEM_MALLOC
 1.15484 +
 1.15485 +/*
 1.15486 +** The MSVCRT has malloc_usable_size() but it is called _msize().
 1.15487 +** The use of _msize() is automatic, but can be disabled by compiling
 1.15488 +** with -DSQLITE_WITHOUT_MSIZE
 1.15489 +*/
 1.15490 +#if defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
 1.15491 +# define SQLITE_MALLOCSIZE _msize
 1.15492 +#endif
 1.15493 +
 1.15494 +#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
 1.15495 +
 1.15496 +/*
 1.15497 +** Use the zone allocator available on apple products unless the
 1.15498 +** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
 1.15499 +*/
 1.15500 +#include <sys/sysctl.h>
 1.15501 +#include <malloc/malloc.h>
 1.15502 +#include <libkern/OSAtomic.h>
 1.15503 +static malloc_zone_t* _sqliteZone_;
 1.15504 +#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
 1.15505 +#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
 1.15506 +#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
 1.15507 +#define SQLITE_MALLOCSIZE(x) \
 1.15508 +        (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
 1.15509 +
 1.15510 +#else /* if not __APPLE__ */
 1.15511 +
 1.15512 +/*
 1.15513 +** Use standard C library malloc and free on non-Apple systems.  
 1.15514 +** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
 1.15515 +*/
 1.15516 +#define SQLITE_MALLOC(x)    malloc(x)
 1.15517 +#define SQLITE_FREE(x)      free(x)
 1.15518 +#define SQLITE_REALLOC(x,y) realloc((x),(y))
 1.15519 +
 1.15520 +#if (defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)) \
 1.15521 +      || (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE))
 1.15522 +# include <malloc.h>    /* Needed for malloc_usable_size on linux */
 1.15523 +#endif
 1.15524 +#ifdef HAVE_MALLOC_USABLE_SIZE
 1.15525 +# ifndef SQLITE_MALLOCSIZE
 1.15526 +#  define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
 1.15527 +# endif
 1.15528 +#else
 1.15529 +# undef SQLITE_MALLOCSIZE
 1.15530 +#endif
 1.15531 +
 1.15532 +#endif /* __APPLE__ or not __APPLE__ */
 1.15533 +
 1.15534 +/*
 1.15535 +** Like malloc(), but remember the size of the allocation
 1.15536 +** so that we can find it later using sqlite3MemSize().
 1.15537 +**
 1.15538 +** For this low-level routine, we are guaranteed that nByte>0 because
 1.15539 +** cases of nByte<=0 will be intercepted and dealt with by higher level
 1.15540 +** routines.
 1.15541 +*/
 1.15542 +static void *sqlite3MemMalloc(int nByte){
 1.15543 +#ifdef SQLITE_MALLOCSIZE
 1.15544 +  void *p = SQLITE_MALLOC( nByte );
 1.15545 +  if( p==0 ){
 1.15546 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.15547 +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 1.15548 +  }
 1.15549 +  return p;
 1.15550 +#else
 1.15551 +  sqlite3_int64 *p;
 1.15552 +  assert( nByte>0 );
 1.15553 +  nByte = ROUND8(nByte);
 1.15554 +  p = SQLITE_MALLOC( nByte+8 );
 1.15555 +  if( p ){
 1.15556 +    p[0] = nByte;
 1.15557 +    p++;
 1.15558 +  }else{
 1.15559 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.15560 +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 1.15561 +  }
 1.15562 +  return (void *)p;
 1.15563 +#endif
 1.15564 +}
 1.15565 +
 1.15566 +/*
 1.15567 +** Like free() but works for allocations obtained from sqlite3MemMalloc()
 1.15568 +** or sqlite3MemRealloc().
 1.15569 +**
 1.15570 +** For this low-level routine, we already know that pPrior!=0 since
 1.15571 +** cases where pPrior==0 will have been intecepted and dealt with
 1.15572 +** by higher-level routines.
 1.15573 +*/
 1.15574 +static void sqlite3MemFree(void *pPrior){
 1.15575 +#ifdef SQLITE_MALLOCSIZE
 1.15576 +  SQLITE_FREE(pPrior);
 1.15577 +#else
 1.15578 +  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
 1.15579 +  assert( pPrior!=0 );
 1.15580 +  p--;
 1.15581 +  SQLITE_FREE(p);
 1.15582 +#endif
 1.15583 +}
 1.15584 +
 1.15585 +/*
 1.15586 +** Report the allocated size of a prior return from xMalloc()
 1.15587 +** or xRealloc().
 1.15588 +*/
 1.15589 +static int sqlite3MemSize(void *pPrior){
 1.15590 +#ifdef SQLITE_MALLOCSIZE
 1.15591 +  return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
 1.15592 +#else
 1.15593 +  sqlite3_int64 *p;
 1.15594 +  if( pPrior==0 ) return 0;
 1.15595 +  p = (sqlite3_int64*)pPrior;
 1.15596 +  p--;
 1.15597 +  return (int)p[0];
 1.15598 +#endif
 1.15599 +}
 1.15600 +
 1.15601 +/*
 1.15602 +** Like realloc().  Resize an allocation previously obtained from
 1.15603 +** sqlite3MemMalloc().
 1.15604 +**
 1.15605 +** For this low-level interface, we know that pPrior!=0.  Cases where
 1.15606 +** pPrior==0 while have been intercepted by higher-level routine and
 1.15607 +** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
 1.15608 +** cases where nByte<=0 will have been intercepted by higher-level
 1.15609 +** routines and redirected to xFree.
 1.15610 +*/
 1.15611 +static void *sqlite3MemRealloc(void *pPrior, int nByte){
 1.15612 +#ifdef SQLITE_MALLOCSIZE
 1.15613 +  void *p = SQLITE_REALLOC(pPrior, nByte);
 1.15614 +  if( p==0 ){
 1.15615 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.15616 +    sqlite3_log(SQLITE_NOMEM,
 1.15617 +      "failed memory resize %u to %u bytes",
 1.15618 +      SQLITE_MALLOCSIZE(pPrior), nByte);
 1.15619 +  }
 1.15620 +  return p;
 1.15621 +#else
 1.15622 +  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
 1.15623 +  assert( pPrior!=0 && nByte>0 );
 1.15624 +  assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
 1.15625 +  p--;
 1.15626 +  p = SQLITE_REALLOC(p, nByte+8 );
 1.15627 +  if( p ){
 1.15628 +    p[0] = nByte;
 1.15629 +    p++;
 1.15630 +  }else{
 1.15631 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.15632 +    sqlite3_log(SQLITE_NOMEM,
 1.15633 +      "failed memory resize %u to %u bytes",
 1.15634 +      sqlite3MemSize(pPrior), nByte);
 1.15635 +  }
 1.15636 +  return (void*)p;
 1.15637 +#endif
 1.15638 +}
 1.15639 +
 1.15640 +/*
 1.15641 +** Round up a request size to the next valid allocation size.
 1.15642 +*/
 1.15643 +static int sqlite3MemRoundup(int n){
 1.15644 +  return ROUND8(n);
 1.15645 +}
 1.15646 +
 1.15647 +/*
 1.15648 +** Initialize this module.
 1.15649 +*/
 1.15650 +static int sqlite3MemInit(void *NotUsed){
 1.15651 +#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
 1.15652 +  int cpuCount;
 1.15653 +  size_t len;
 1.15654 +  if( _sqliteZone_ ){
 1.15655 +    return SQLITE_OK;
 1.15656 +  }
 1.15657 +  len = sizeof(cpuCount);
 1.15658 +  /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
 1.15659 +  sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
 1.15660 +  if( cpuCount>1 ){
 1.15661 +    /* defer MT decisions to system malloc */
 1.15662 +    _sqliteZone_ = malloc_default_zone();
 1.15663 +  }else{
 1.15664 +    /* only 1 core, use our own zone to contention over global locks, 
 1.15665 +    ** e.g. we have our own dedicated locks */
 1.15666 +    bool success;
 1.15667 +    malloc_zone_t* newzone = malloc_create_zone(4096, 0);
 1.15668 +    malloc_set_zone_name(newzone, "Sqlite_Heap");
 1.15669 +    do{
 1.15670 +      success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
 1.15671 +                                 (void * volatile *)&_sqliteZone_);
 1.15672 +    }while(!_sqliteZone_);
 1.15673 +    if( !success ){
 1.15674 +      /* somebody registered a zone first */
 1.15675 +      malloc_destroy_zone(newzone);
 1.15676 +    }
 1.15677 +  }
 1.15678 +#endif
 1.15679 +  UNUSED_PARAMETER(NotUsed);
 1.15680 +  return SQLITE_OK;
 1.15681 +}
 1.15682 +
 1.15683 +/*
 1.15684 +** Deinitialize this module.
 1.15685 +*/
 1.15686 +static void sqlite3MemShutdown(void *NotUsed){
 1.15687 +  UNUSED_PARAMETER(NotUsed);
 1.15688 +  return;
 1.15689 +}
 1.15690 +
 1.15691 +/*
 1.15692 +** This routine is the only routine in this file with external linkage.
 1.15693 +**
 1.15694 +** Populate the low-level memory allocation function pointers in
 1.15695 +** sqlite3GlobalConfig.m with pointers to the routines in this file.
 1.15696 +*/
 1.15697 +SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 1.15698 +  static const sqlite3_mem_methods defaultMethods = {
 1.15699 +     sqlite3MemMalloc,
 1.15700 +     sqlite3MemFree,
 1.15701 +     sqlite3MemRealloc,
 1.15702 +     sqlite3MemSize,
 1.15703 +     sqlite3MemRoundup,
 1.15704 +     sqlite3MemInit,
 1.15705 +     sqlite3MemShutdown,
 1.15706 +     0
 1.15707 +  };
 1.15708 +  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 1.15709 +}
 1.15710 +
 1.15711 +#endif /* SQLITE_SYSTEM_MALLOC */
 1.15712 +
 1.15713 +/************** End of mem1.c ************************************************/
 1.15714 +/************** Begin file mem2.c ********************************************/
 1.15715 +/*
 1.15716 +** 2007 August 15
 1.15717 +**
 1.15718 +** The author disclaims copyright to this source code.  In place of
 1.15719 +** a legal notice, here is a blessing:
 1.15720 +**
 1.15721 +**    May you do good and not evil.
 1.15722 +**    May you find forgiveness for yourself and forgive others.
 1.15723 +**    May you share freely, never taking more than you give.
 1.15724 +**
 1.15725 +*************************************************************************
 1.15726 +**
 1.15727 +** This file contains low-level memory allocation drivers for when
 1.15728 +** SQLite will use the standard C-library malloc/realloc/free interface
 1.15729 +** to obtain the memory it needs while adding lots of additional debugging
 1.15730 +** information to each allocation in order to help detect and fix memory
 1.15731 +** leaks and memory usage errors.
 1.15732 +**
 1.15733 +** This file contains implementations of the low-level memory allocation
 1.15734 +** routines specified in the sqlite3_mem_methods object.
 1.15735 +*/
 1.15736 +
 1.15737 +/*
 1.15738 +** This version of the memory allocator is used only if the
 1.15739 +** SQLITE_MEMDEBUG macro is defined
 1.15740 +*/
 1.15741 +#ifdef SQLITE_MEMDEBUG
 1.15742 +
 1.15743 +/*
 1.15744 +** The backtrace functionality is only available with GLIBC
 1.15745 +*/
 1.15746 +#ifdef __GLIBC__
 1.15747 +  extern int backtrace(void**,int);
 1.15748 +  extern void backtrace_symbols_fd(void*const*,int,int);
 1.15749 +#else
 1.15750 +# define backtrace(A,B) 1
 1.15751 +# define backtrace_symbols_fd(A,B,C)
 1.15752 +#endif
 1.15753 +/* #include <stdio.h> */
 1.15754 +
 1.15755 +/*
 1.15756 +** Each memory allocation looks like this:
 1.15757 +**
 1.15758 +**  ------------------------------------------------------------------------
 1.15759 +**  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
 1.15760 +**  ------------------------------------------------------------------------
 1.15761 +**
 1.15762 +** The application code sees only a pointer to the allocation.  We have
 1.15763 +** to back up from the allocation pointer to find the MemBlockHdr.  The
 1.15764 +** MemBlockHdr tells us the size of the allocation and the number of
 1.15765 +** backtrace pointers.  There is also a guard word at the end of the
 1.15766 +** MemBlockHdr.
 1.15767 +*/
 1.15768 +struct MemBlockHdr {
 1.15769 +  i64 iSize;                          /* Size of this allocation */
 1.15770 +  struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
 1.15771 +  char nBacktrace;                    /* Number of backtraces on this alloc */
 1.15772 +  char nBacktraceSlots;               /* Available backtrace slots */
 1.15773 +  u8 nTitle;                          /* Bytes of title; includes '\0' */
 1.15774 +  u8 eType;                           /* Allocation type code */
 1.15775 +  int iForeGuard;                     /* Guard word for sanity */
 1.15776 +};
 1.15777 +
 1.15778 +/*
 1.15779 +** Guard words
 1.15780 +*/
 1.15781 +#define FOREGUARD 0x80F5E153
 1.15782 +#define REARGUARD 0xE4676B53
 1.15783 +
 1.15784 +/*
 1.15785 +** Number of malloc size increments to track.
 1.15786 +*/
 1.15787 +#define NCSIZE  1000
 1.15788 +
 1.15789 +/*
 1.15790 +** All of the static variables used by this module are collected
 1.15791 +** into a single structure named "mem".  This is to keep the
 1.15792 +** static variables organized and to reduce namespace pollution
 1.15793 +** when this module is combined with other in the amalgamation.
 1.15794 +*/
 1.15795 +static struct {
 1.15796 +  
 1.15797 +  /*
 1.15798 +  ** Mutex to control access to the memory allocation subsystem.
 1.15799 +  */
 1.15800 +  sqlite3_mutex *mutex;
 1.15801 +
 1.15802 +  /*
 1.15803 +  ** Head and tail of a linked list of all outstanding allocations
 1.15804 +  */
 1.15805 +  struct MemBlockHdr *pFirst;
 1.15806 +  struct MemBlockHdr *pLast;
 1.15807 +  
 1.15808 +  /*
 1.15809 +  ** The number of levels of backtrace to save in new allocations.
 1.15810 +  */
 1.15811 +  int nBacktrace;
 1.15812 +  void (*xBacktrace)(int, int, void **);
 1.15813 +
 1.15814 +  /*
 1.15815 +  ** Title text to insert in front of each block
 1.15816 +  */
 1.15817 +  int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
 1.15818 +  char zTitle[100];  /* The title text */
 1.15819 +
 1.15820 +  /* 
 1.15821 +  ** sqlite3MallocDisallow() increments the following counter.
 1.15822 +  ** sqlite3MallocAllow() decrements it.
 1.15823 +  */
 1.15824 +  int disallow; /* Do not allow memory allocation */
 1.15825 +
 1.15826 +  /*
 1.15827 +  ** Gather statistics on the sizes of memory allocations.
 1.15828 +  ** nAlloc[i] is the number of allocation attempts of i*8
 1.15829 +  ** bytes.  i==NCSIZE is the number of allocation attempts for
 1.15830 +  ** sizes more than NCSIZE*8 bytes.
 1.15831 +  */
 1.15832 +  int nAlloc[NCSIZE];      /* Total number of allocations */
 1.15833 +  int nCurrent[NCSIZE];    /* Current number of allocations */
 1.15834 +  int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
 1.15835 +
 1.15836 +} mem;
 1.15837 +
 1.15838 +
 1.15839 +/*
 1.15840 +** Adjust memory usage statistics
 1.15841 +*/
 1.15842 +static void adjustStats(int iSize, int increment){
 1.15843 +  int i = ROUND8(iSize)/8;
 1.15844 +  if( i>NCSIZE-1 ){
 1.15845 +    i = NCSIZE - 1;
 1.15846 +  }
 1.15847 +  if( increment>0 ){
 1.15848 +    mem.nAlloc[i]++;
 1.15849 +    mem.nCurrent[i]++;
 1.15850 +    if( mem.nCurrent[i]>mem.mxCurrent[i] ){
 1.15851 +      mem.mxCurrent[i] = mem.nCurrent[i];
 1.15852 +    }
 1.15853 +  }else{
 1.15854 +    mem.nCurrent[i]--;
 1.15855 +    assert( mem.nCurrent[i]>=0 );
 1.15856 +  }
 1.15857 +}
 1.15858 +
 1.15859 +/*
 1.15860 +** Given an allocation, find the MemBlockHdr for that allocation.
 1.15861 +**
 1.15862 +** This routine checks the guards at either end of the allocation and
 1.15863 +** if they are incorrect it asserts.
 1.15864 +*/
 1.15865 +static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
 1.15866 +  struct MemBlockHdr *p;
 1.15867 +  int *pInt;
 1.15868 +  u8 *pU8;
 1.15869 +  int nReserve;
 1.15870 +
 1.15871 +  p = (struct MemBlockHdr*)pAllocation;
 1.15872 +  p--;
 1.15873 +  assert( p->iForeGuard==(int)FOREGUARD );
 1.15874 +  nReserve = ROUND8(p->iSize);
 1.15875 +  pInt = (int*)pAllocation;
 1.15876 +  pU8 = (u8*)pAllocation;
 1.15877 +  assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
 1.15878 +  /* This checks any of the "extra" bytes allocated due
 1.15879 +  ** to rounding up to an 8 byte boundary to ensure 
 1.15880 +  ** they haven't been overwritten.
 1.15881 +  */
 1.15882 +  while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
 1.15883 +  return p;
 1.15884 +}
 1.15885 +
 1.15886 +/*
 1.15887 +** Return the number of bytes currently allocated at address p.
 1.15888 +*/
 1.15889 +static int sqlite3MemSize(void *p){
 1.15890 +  struct MemBlockHdr *pHdr;
 1.15891 +  if( !p ){
 1.15892 +    return 0;
 1.15893 +  }
 1.15894 +  pHdr = sqlite3MemsysGetHeader(p);
 1.15895 +  return pHdr->iSize;
 1.15896 +}
 1.15897 +
 1.15898 +/*
 1.15899 +** Initialize the memory allocation subsystem.
 1.15900 +*/
 1.15901 +static int sqlite3MemInit(void *NotUsed){
 1.15902 +  UNUSED_PARAMETER(NotUsed);
 1.15903 +  assert( (sizeof(struct MemBlockHdr)&7) == 0 );
 1.15904 +  if( !sqlite3GlobalConfig.bMemstat ){
 1.15905 +    /* If memory status is enabled, then the malloc.c wrapper will already
 1.15906 +    ** hold the STATIC_MEM mutex when the routines here are invoked. */
 1.15907 +    mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 1.15908 +  }
 1.15909 +  return SQLITE_OK;
 1.15910 +}
 1.15911 +
 1.15912 +/*
 1.15913 +** Deinitialize the memory allocation subsystem.
 1.15914 +*/
 1.15915 +static void sqlite3MemShutdown(void *NotUsed){
 1.15916 +  UNUSED_PARAMETER(NotUsed);
 1.15917 +  mem.mutex = 0;
 1.15918 +}
 1.15919 +
 1.15920 +/*
 1.15921 +** Round up a request size to the next valid allocation size.
 1.15922 +*/
 1.15923 +static int sqlite3MemRoundup(int n){
 1.15924 +  return ROUND8(n);
 1.15925 +}
 1.15926 +
 1.15927 +/*
 1.15928 +** Fill a buffer with pseudo-random bytes.  This is used to preset
 1.15929 +** the content of a new memory allocation to unpredictable values and
 1.15930 +** to clear the content of a freed allocation to unpredictable values.
 1.15931 +*/
 1.15932 +static void randomFill(char *pBuf, int nByte){
 1.15933 +  unsigned int x, y, r;
 1.15934 +  x = SQLITE_PTR_TO_INT(pBuf);
 1.15935 +  y = nByte | 1;
 1.15936 +  while( nByte >= 4 ){
 1.15937 +    x = (x>>1) ^ (-(x&1) & 0xd0000001);
 1.15938 +    y = y*1103515245 + 12345;
 1.15939 +    r = x ^ y;
 1.15940 +    *(int*)pBuf = r;
 1.15941 +    pBuf += 4;
 1.15942 +    nByte -= 4;
 1.15943 +  }
 1.15944 +  while( nByte-- > 0 ){
 1.15945 +    x = (x>>1) ^ (-(x&1) & 0xd0000001);
 1.15946 +    y = y*1103515245 + 12345;
 1.15947 +    r = x ^ y;
 1.15948 +    *(pBuf++) = r & 0xff;
 1.15949 +  }
 1.15950 +}
 1.15951 +
 1.15952 +/*
 1.15953 +** Allocate nByte bytes of memory.
 1.15954 +*/
 1.15955 +static void *sqlite3MemMalloc(int nByte){
 1.15956 +  struct MemBlockHdr *pHdr;
 1.15957 +  void **pBt;
 1.15958 +  char *z;
 1.15959 +  int *pInt;
 1.15960 +  void *p = 0;
 1.15961 +  int totalSize;
 1.15962 +  int nReserve;
 1.15963 +  sqlite3_mutex_enter(mem.mutex);
 1.15964 +  assert( mem.disallow==0 );
 1.15965 +  nReserve = ROUND8(nByte);
 1.15966 +  totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
 1.15967 +               mem.nBacktrace*sizeof(void*) + mem.nTitle;
 1.15968 +  p = malloc(totalSize);
 1.15969 +  if( p ){
 1.15970 +    z = p;
 1.15971 +    pBt = (void**)&z[mem.nTitle];
 1.15972 +    pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
 1.15973 +    pHdr->pNext = 0;
 1.15974 +    pHdr->pPrev = mem.pLast;
 1.15975 +    if( mem.pLast ){
 1.15976 +      mem.pLast->pNext = pHdr;
 1.15977 +    }else{
 1.15978 +      mem.pFirst = pHdr;
 1.15979 +    }
 1.15980 +    mem.pLast = pHdr;
 1.15981 +    pHdr->iForeGuard = FOREGUARD;
 1.15982 +    pHdr->eType = MEMTYPE_HEAP;
 1.15983 +    pHdr->nBacktraceSlots = mem.nBacktrace;
 1.15984 +    pHdr->nTitle = mem.nTitle;
 1.15985 +    if( mem.nBacktrace ){
 1.15986 +      void *aAddr[40];
 1.15987 +      pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
 1.15988 +      memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
 1.15989 +      assert(pBt[0]);
 1.15990 +      if( mem.xBacktrace ){
 1.15991 +        mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
 1.15992 +      }
 1.15993 +    }else{
 1.15994 +      pHdr->nBacktrace = 0;
 1.15995 +    }
 1.15996 +    if( mem.nTitle ){
 1.15997 +      memcpy(z, mem.zTitle, mem.nTitle);
 1.15998 +    }
 1.15999 +    pHdr->iSize = nByte;
 1.16000 +    adjustStats(nByte, +1);
 1.16001 +    pInt = (int*)&pHdr[1];
 1.16002 +    pInt[nReserve/sizeof(int)] = REARGUARD;
 1.16003 +    randomFill((char*)pInt, nByte);
 1.16004 +    memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
 1.16005 +    p = (void*)pInt;
 1.16006 +  }
 1.16007 +  sqlite3_mutex_leave(mem.mutex);
 1.16008 +  return p; 
 1.16009 +}
 1.16010 +
 1.16011 +/*
 1.16012 +** Free memory.
 1.16013 +*/
 1.16014 +static void sqlite3MemFree(void *pPrior){
 1.16015 +  struct MemBlockHdr *pHdr;
 1.16016 +  void **pBt;
 1.16017 +  char *z;
 1.16018 +  assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 
 1.16019 +       || mem.mutex!=0 );
 1.16020 +  pHdr = sqlite3MemsysGetHeader(pPrior);
 1.16021 +  pBt = (void**)pHdr;
 1.16022 +  pBt -= pHdr->nBacktraceSlots;
 1.16023 +  sqlite3_mutex_enter(mem.mutex);
 1.16024 +  if( pHdr->pPrev ){
 1.16025 +    assert( pHdr->pPrev->pNext==pHdr );
 1.16026 +    pHdr->pPrev->pNext = pHdr->pNext;
 1.16027 +  }else{
 1.16028 +    assert( mem.pFirst==pHdr );
 1.16029 +    mem.pFirst = pHdr->pNext;
 1.16030 +  }
 1.16031 +  if( pHdr->pNext ){
 1.16032 +    assert( pHdr->pNext->pPrev==pHdr );
 1.16033 +    pHdr->pNext->pPrev = pHdr->pPrev;
 1.16034 +  }else{
 1.16035 +    assert( mem.pLast==pHdr );
 1.16036 +    mem.pLast = pHdr->pPrev;
 1.16037 +  }
 1.16038 +  z = (char*)pBt;
 1.16039 +  z -= pHdr->nTitle;
 1.16040 +  adjustStats(pHdr->iSize, -1);
 1.16041 +  randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
 1.16042 +                pHdr->iSize + sizeof(int) + pHdr->nTitle);
 1.16043 +  free(z);
 1.16044 +  sqlite3_mutex_leave(mem.mutex);  
 1.16045 +}
 1.16046 +
 1.16047 +/*
 1.16048 +** Change the size of an existing memory allocation.
 1.16049 +**
 1.16050 +** For this debugging implementation, we *always* make a copy of the
 1.16051 +** allocation into a new place in memory.  In this way, if the 
 1.16052 +** higher level code is using pointer to the old allocation, it is 
 1.16053 +** much more likely to break and we are much more liking to find
 1.16054 +** the error.
 1.16055 +*/
 1.16056 +static void *sqlite3MemRealloc(void *pPrior, int nByte){
 1.16057 +  struct MemBlockHdr *pOldHdr;
 1.16058 +  void *pNew;
 1.16059 +  assert( mem.disallow==0 );
 1.16060 +  assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
 1.16061 +  pOldHdr = sqlite3MemsysGetHeader(pPrior);
 1.16062 +  pNew = sqlite3MemMalloc(nByte);
 1.16063 +  if( pNew ){
 1.16064 +    memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
 1.16065 +    if( nByte>pOldHdr->iSize ){
 1.16066 +      randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
 1.16067 +    }
 1.16068 +    sqlite3MemFree(pPrior);
 1.16069 +  }
 1.16070 +  return pNew;
 1.16071 +}
 1.16072 +
 1.16073 +/*
 1.16074 +** Populate the low-level memory allocation function pointers in
 1.16075 +** sqlite3GlobalConfig.m with pointers to the routines in this file.
 1.16076 +*/
 1.16077 +SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 1.16078 +  static const sqlite3_mem_methods defaultMethods = {
 1.16079 +     sqlite3MemMalloc,
 1.16080 +     sqlite3MemFree,
 1.16081 +     sqlite3MemRealloc,
 1.16082 +     sqlite3MemSize,
 1.16083 +     sqlite3MemRoundup,
 1.16084 +     sqlite3MemInit,
 1.16085 +     sqlite3MemShutdown,
 1.16086 +     0
 1.16087 +  };
 1.16088 +  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 1.16089 +}
 1.16090 +
 1.16091 +/*
 1.16092 +** Set the "type" of an allocation.
 1.16093 +*/
 1.16094 +SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
 1.16095 +  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 1.16096 +    struct MemBlockHdr *pHdr;
 1.16097 +    pHdr = sqlite3MemsysGetHeader(p);
 1.16098 +    assert( pHdr->iForeGuard==FOREGUARD );
 1.16099 +    pHdr->eType = eType;
 1.16100 +  }
 1.16101 +}
 1.16102 +
 1.16103 +/*
 1.16104 +** Return TRUE if the mask of type in eType matches the type of the
 1.16105 +** allocation p.  Also return true if p==NULL.
 1.16106 +**
 1.16107 +** This routine is designed for use within an assert() statement, to
 1.16108 +** verify the type of an allocation.  For example:
 1.16109 +**
 1.16110 +**     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 1.16111 +*/
 1.16112 +SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
 1.16113 +  int rc = 1;
 1.16114 +  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 1.16115 +    struct MemBlockHdr *pHdr;
 1.16116 +    pHdr = sqlite3MemsysGetHeader(p);
 1.16117 +    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
 1.16118 +    if( (pHdr->eType&eType)==0 ){
 1.16119 +      rc = 0;
 1.16120 +    }
 1.16121 +  }
 1.16122 +  return rc;
 1.16123 +}
 1.16124 +
 1.16125 +/*
 1.16126 +** Return TRUE if the mask of type in eType matches no bits of the type of the
 1.16127 +** allocation p.  Also return true if p==NULL.
 1.16128 +**
 1.16129 +** This routine is designed for use within an assert() statement, to
 1.16130 +** verify the type of an allocation.  For example:
 1.16131 +**
 1.16132 +**     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 1.16133 +*/
 1.16134 +SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
 1.16135 +  int rc = 1;
 1.16136 +  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 1.16137 +    struct MemBlockHdr *pHdr;
 1.16138 +    pHdr = sqlite3MemsysGetHeader(p);
 1.16139 +    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
 1.16140 +    if( (pHdr->eType&eType)!=0 ){
 1.16141 +      rc = 0;
 1.16142 +    }
 1.16143 +  }
 1.16144 +  return rc;
 1.16145 +}
 1.16146 +
 1.16147 +/*
 1.16148 +** Set the number of backtrace levels kept for each allocation.
 1.16149 +** A value of zero turns off backtracing.  The number is always rounded
 1.16150 +** up to a multiple of 2.
 1.16151 +*/
 1.16152 +SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
 1.16153 +  if( depth<0 ){ depth = 0; }
 1.16154 +  if( depth>20 ){ depth = 20; }
 1.16155 +  depth = (depth+1)&0xfe;
 1.16156 +  mem.nBacktrace = depth;
 1.16157 +}
 1.16158 +
 1.16159 +SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
 1.16160 +  mem.xBacktrace = xBacktrace;
 1.16161 +}
 1.16162 +
 1.16163 +/*
 1.16164 +** Set the title string for subsequent allocations.
 1.16165 +*/
 1.16166 +SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
 1.16167 +  unsigned int n = sqlite3Strlen30(zTitle) + 1;
 1.16168 +  sqlite3_mutex_enter(mem.mutex);
 1.16169 +  if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
 1.16170 +  memcpy(mem.zTitle, zTitle, n);
 1.16171 +  mem.zTitle[n] = 0;
 1.16172 +  mem.nTitle = ROUND8(n);
 1.16173 +  sqlite3_mutex_leave(mem.mutex);
 1.16174 +}
 1.16175 +
 1.16176 +SQLITE_PRIVATE void sqlite3MemdebugSync(){
 1.16177 +  struct MemBlockHdr *pHdr;
 1.16178 +  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
 1.16179 +    void **pBt = (void**)pHdr;
 1.16180 +    pBt -= pHdr->nBacktraceSlots;
 1.16181 +    mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
 1.16182 +  }
 1.16183 +}
 1.16184 +
 1.16185 +/*
 1.16186 +** Open the file indicated and write a log of all unfreed memory 
 1.16187 +** allocations into that log.
 1.16188 +*/
 1.16189 +SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
 1.16190 +  FILE *out;
 1.16191 +  struct MemBlockHdr *pHdr;
 1.16192 +  void **pBt;
 1.16193 +  int i;
 1.16194 +  out = fopen(zFilename, "w");
 1.16195 +  if( out==0 ){
 1.16196 +    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 1.16197 +                    zFilename);
 1.16198 +    return;
 1.16199 +  }
 1.16200 +  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
 1.16201 +    char *z = (char*)pHdr;
 1.16202 +    z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
 1.16203 +    fprintf(out, "**** %lld bytes at %p from %s ****\n", 
 1.16204 +            pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
 1.16205 +    if( pHdr->nBacktrace ){
 1.16206 +      fflush(out);
 1.16207 +      pBt = (void**)pHdr;
 1.16208 +      pBt -= pHdr->nBacktraceSlots;
 1.16209 +      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
 1.16210 +      fprintf(out, "\n");
 1.16211 +    }
 1.16212 +  }
 1.16213 +  fprintf(out, "COUNTS:\n");
 1.16214 +  for(i=0; i<NCSIZE-1; i++){
 1.16215 +    if( mem.nAlloc[i] ){
 1.16216 +      fprintf(out, "   %5d: %10d %10d %10d\n", 
 1.16217 +            i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
 1.16218 +    }
 1.16219 +  }
 1.16220 +  if( mem.nAlloc[NCSIZE-1] ){
 1.16221 +    fprintf(out, "   %5d: %10d %10d %10d\n",
 1.16222 +             NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
 1.16223 +             mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
 1.16224 +  }
 1.16225 +  fclose(out);
 1.16226 +}
 1.16227 +
 1.16228 +/*
 1.16229 +** Return the number of times sqlite3MemMalloc() has been called.
 1.16230 +*/
 1.16231 +SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
 1.16232 +  int i;
 1.16233 +  int nTotal = 0;
 1.16234 +  for(i=0; i<NCSIZE; i++){
 1.16235 +    nTotal += mem.nAlloc[i];
 1.16236 +  }
 1.16237 +  return nTotal;
 1.16238 +}
 1.16239 +
 1.16240 +
 1.16241 +#endif /* SQLITE_MEMDEBUG */
 1.16242 +
 1.16243 +/************** End of mem2.c ************************************************/
 1.16244 +/************** Begin file mem3.c ********************************************/
 1.16245 +/*
 1.16246 +** 2007 October 14
 1.16247 +**
 1.16248 +** The author disclaims copyright to this source code.  In place of
 1.16249 +** a legal notice, here is a blessing:
 1.16250 +**
 1.16251 +**    May you do good and not evil.
 1.16252 +**    May you find forgiveness for yourself and forgive others.
 1.16253 +**    May you share freely, never taking more than you give.
 1.16254 +**
 1.16255 +*************************************************************************
 1.16256 +** This file contains the C functions that implement a memory
 1.16257 +** allocation subsystem for use by SQLite. 
 1.16258 +**
 1.16259 +** This version of the memory allocation subsystem omits all
 1.16260 +** use of malloc(). The SQLite user supplies a block of memory
 1.16261 +** before calling sqlite3_initialize() from which allocations
 1.16262 +** are made and returned by the xMalloc() and xRealloc() 
 1.16263 +** implementations. Once sqlite3_initialize() has been called,
 1.16264 +** the amount of memory available to SQLite is fixed and cannot
 1.16265 +** be changed.
 1.16266 +**
 1.16267 +** This version of the memory allocation subsystem is included
 1.16268 +** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
 1.16269 +*/
 1.16270 +
 1.16271 +/*
 1.16272 +** This version of the memory allocator is only built into the library
 1.16273 +** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
 1.16274 +** mean that the library will use a memory-pool by default, just that
 1.16275 +** it is available. The mempool allocator is activated by calling
 1.16276 +** sqlite3_config().
 1.16277 +*/
 1.16278 +#ifdef SQLITE_ENABLE_MEMSYS3
 1.16279 +
 1.16280 +/*
 1.16281 +** Maximum size (in Mem3Blocks) of a "small" chunk.
 1.16282 +*/
 1.16283 +#define MX_SMALL 10
 1.16284 +
 1.16285 +
 1.16286 +/*
 1.16287 +** Number of freelist hash slots
 1.16288 +*/
 1.16289 +#define N_HASH  61
 1.16290 +
 1.16291 +/*
 1.16292 +** A memory allocation (also called a "chunk") consists of two or 
 1.16293 +** more blocks where each block is 8 bytes.  The first 8 bytes are 
 1.16294 +** a header that is not returned to the user.
 1.16295 +**
 1.16296 +** A chunk is two or more blocks that is either checked out or
 1.16297 +** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
 1.16298 +** size of the allocation in blocks if the allocation is free.
 1.16299 +** The u.hdr.size4x&1 bit is true if the chunk is checked out and
 1.16300 +** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
 1.16301 +** is true if the previous chunk is checked out and false if the
 1.16302 +** previous chunk is free.  The u.hdr.prevSize field is the size of
 1.16303 +** the previous chunk in blocks if the previous chunk is on the
 1.16304 +** freelist. If the previous chunk is checked out, then
 1.16305 +** u.hdr.prevSize can be part of the data for that chunk and should
 1.16306 +** not be read or written.
 1.16307 +**
 1.16308 +** We often identify a chunk by its index in mem3.aPool[].  When
 1.16309 +** this is done, the chunk index refers to the second block of
 1.16310 +** the chunk.  In this way, the first chunk has an index of 1.
 1.16311 +** A chunk index of 0 means "no such chunk" and is the equivalent
 1.16312 +** of a NULL pointer.
 1.16313 +**
 1.16314 +** The second block of free chunks is of the form u.list.  The
 1.16315 +** two fields form a double-linked list of chunks of related sizes.
 1.16316 +** Pointers to the head of the list are stored in mem3.aiSmall[] 
 1.16317 +** for smaller chunks and mem3.aiHash[] for larger chunks.
 1.16318 +**
 1.16319 +** The second block of a chunk is user data if the chunk is checked 
 1.16320 +** out.  If a chunk is checked out, the user data may extend into
 1.16321 +** the u.hdr.prevSize value of the following chunk.
 1.16322 +*/
 1.16323 +typedef struct Mem3Block Mem3Block;
 1.16324 +struct Mem3Block {
 1.16325 +  union {
 1.16326 +    struct {
 1.16327 +      u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
 1.16328 +      u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
 1.16329 +    } hdr;
 1.16330 +    struct {
 1.16331 +      u32 next;       /* Index in mem3.aPool[] of next free chunk */
 1.16332 +      u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
 1.16333 +    } list;
 1.16334 +  } u;
 1.16335 +};
 1.16336 +
 1.16337 +/*
 1.16338 +** All of the static variables used by this module are collected
 1.16339 +** into a single structure named "mem3".  This is to keep the
 1.16340 +** static variables organized and to reduce namespace pollution
 1.16341 +** when this module is combined with other in the amalgamation.
 1.16342 +*/
 1.16343 +static SQLITE_WSD struct Mem3Global {
 1.16344 +  /*
 1.16345 +  ** Memory available for allocation. nPool is the size of the array
 1.16346 +  ** (in Mem3Blocks) pointed to by aPool less 2.
 1.16347 +  */
 1.16348 +  u32 nPool;
 1.16349 +  Mem3Block *aPool;
 1.16350 +
 1.16351 +  /*
 1.16352 +  ** True if we are evaluating an out-of-memory callback.
 1.16353 +  */
 1.16354 +  int alarmBusy;
 1.16355 +  
 1.16356 +  /*
 1.16357 +  ** Mutex to control access to the memory allocation subsystem.
 1.16358 +  */
 1.16359 +  sqlite3_mutex *mutex;
 1.16360 +  
 1.16361 +  /*
 1.16362 +  ** The minimum amount of free space that we have seen.
 1.16363 +  */
 1.16364 +  u32 mnMaster;
 1.16365 +
 1.16366 +  /*
 1.16367 +  ** iMaster is the index of the master chunk.  Most new allocations
 1.16368 +  ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
 1.16369 +  ** of the current master.  iMaster is 0 if there is not master chunk.
 1.16370 +  ** The master chunk is not in either the aiHash[] or aiSmall[].
 1.16371 +  */
 1.16372 +  u32 iMaster;
 1.16373 +  u32 szMaster;
 1.16374 +
 1.16375 +  /*
 1.16376 +  ** Array of lists of free blocks according to the block size 
 1.16377 +  ** for smaller chunks, or a hash on the block size for larger
 1.16378 +  ** chunks.
 1.16379 +  */
 1.16380 +  u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
 1.16381 +  u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
 1.16382 +} mem3 = { 97535575 };
 1.16383 +
 1.16384 +#define mem3 GLOBAL(struct Mem3Global, mem3)
 1.16385 +
 1.16386 +/*
 1.16387 +** Unlink the chunk at mem3.aPool[i] from list it is currently
 1.16388 +** on.  *pRoot is the list that i is a member of.
 1.16389 +*/
 1.16390 +static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
 1.16391 +  u32 next = mem3.aPool[i].u.list.next;
 1.16392 +  u32 prev = mem3.aPool[i].u.list.prev;
 1.16393 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16394 +  if( prev==0 ){
 1.16395 +    *pRoot = next;
 1.16396 +  }else{
 1.16397 +    mem3.aPool[prev].u.list.next = next;
 1.16398 +  }
 1.16399 +  if( next ){
 1.16400 +    mem3.aPool[next].u.list.prev = prev;
 1.16401 +  }
 1.16402 +  mem3.aPool[i].u.list.next = 0;
 1.16403 +  mem3.aPool[i].u.list.prev = 0;
 1.16404 +}
 1.16405 +
 1.16406 +/*
 1.16407 +** Unlink the chunk at index i from 
 1.16408 +** whatever list is currently a member of.
 1.16409 +*/
 1.16410 +static void memsys3Unlink(u32 i){
 1.16411 +  u32 size, hash;
 1.16412 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16413 +  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
 1.16414 +  assert( i>=1 );
 1.16415 +  size = mem3.aPool[i-1].u.hdr.size4x/4;
 1.16416 +  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
 1.16417 +  assert( size>=2 );
 1.16418 +  if( size <= MX_SMALL ){
 1.16419 +    memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
 1.16420 +  }else{
 1.16421 +    hash = size % N_HASH;
 1.16422 +    memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
 1.16423 +  }
 1.16424 +}
 1.16425 +
 1.16426 +/*
 1.16427 +** Link the chunk at mem3.aPool[i] so that is on the list rooted
 1.16428 +** at *pRoot.
 1.16429 +*/
 1.16430 +static void memsys3LinkIntoList(u32 i, u32 *pRoot){
 1.16431 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16432 +  mem3.aPool[i].u.list.next = *pRoot;
 1.16433 +  mem3.aPool[i].u.list.prev = 0;
 1.16434 +  if( *pRoot ){
 1.16435 +    mem3.aPool[*pRoot].u.list.prev = i;
 1.16436 +  }
 1.16437 +  *pRoot = i;
 1.16438 +}
 1.16439 +
 1.16440 +/*
 1.16441 +** Link the chunk at index i into either the appropriate
 1.16442 +** small chunk list, or into the large chunk hash table.
 1.16443 +*/
 1.16444 +static void memsys3Link(u32 i){
 1.16445 +  u32 size, hash;
 1.16446 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16447 +  assert( i>=1 );
 1.16448 +  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
 1.16449 +  size = mem3.aPool[i-1].u.hdr.size4x/4;
 1.16450 +  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
 1.16451 +  assert( size>=2 );
 1.16452 +  if( size <= MX_SMALL ){
 1.16453 +    memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
 1.16454 +  }else{
 1.16455 +    hash = size % N_HASH;
 1.16456 +    memsys3LinkIntoList(i, &mem3.aiHash[hash]);
 1.16457 +  }
 1.16458 +}
 1.16459 +
 1.16460 +/*
 1.16461 +** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
 1.16462 +** will already be held (obtained by code in malloc.c) if
 1.16463 +** sqlite3GlobalConfig.bMemStat is true.
 1.16464 +*/
 1.16465 +static void memsys3Enter(void){
 1.16466 +  if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
 1.16467 +    mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 1.16468 +  }
 1.16469 +  sqlite3_mutex_enter(mem3.mutex);
 1.16470 +}
 1.16471 +static void memsys3Leave(void){
 1.16472 +  sqlite3_mutex_leave(mem3.mutex);
 1.16473 +}
 1.16474 +
 1.16475 +/*
 1.16476 +** Called when we are unable to satisfy an allocation of nBytes.
 1.16477 +*/
 1.16478 +static void memsys3OutOfMemory(int nByte){
 1.16479 +  if( !mem3.alarmBusy ){
 1.16480 +    mem3.alarmBusy = 1;
 1.16481 +    assert( sqlite3_mutex_held(mem3.mutex) );
 1.16482 +    sqlite3_mutex_leave(mem3.mutex);
 1.16483 +    sqlite3_release_memory(nByte);
 1.16484 +    sqlite3_mutex_enter(mem3.mutex);
 1.16485 +    mem3.alarmBusy = 0;
 1.16486 +  }
 1.16487 +}
 1.16488 +
 1.16489 +
 1.16490 +/*
 1.16491 +** Chunk i is a free chunk that has been unlinked.  Adjust its 
 1.16492 +** size parameters for check-out and return a pointer to the 
 1.16493 +** user portion of the chunk.
 1.16494 +*/
 1.16495 +static void *memsys3Checkout(u32 i, u32 nBlock){
 1.16496 +  u32 x;
 1.16497 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16498 +  assert( i>=1 );
 1.16499 +  assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
 1.16500 +  assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
 1.16501 +  x = mem3.aPool[i-1].u.hdr.size4x;
 1.16502 +  mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
 1.16503 +  mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
 1.16504 +  mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
 1.16505 +  return &mem3.aPool[i];
 1.16506 +}
 1.16507 +
 1.16508 +/*
 1.16509 +** Carve a piece off of the end of the mem3.iMaster free chunk.
 1.16510 +** Return a pointer to the new allocation.  Or, if the master chunk
 1.16511 +** is not large enough, return 0.
 1.16512 +*/
 1.16513 +static void *memsys3FromMaster(u32 nBlock){
 1.16514 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16515 +  assert( mem3.szMaster>=nBlock );
 1.16516 +  if( nBlock>=mem3.szMaster-1 ){
 1.16517 +    /* Use the entire master */
 1.16518 +    void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
 1.16519 +    mem3.iMaster = 0;
 1.16520 +    mem3.szMaster = 0;
 1.16521 +    mem3.mnMaster = 0;
 1.16522 +    return p;
 1.16523 +  }else{
 1.16524 +    /* Split the master block.  Return the tail. */
 1.16525 +    u32 newi, x;
 1.16526 +    newi = mem3.iMaster + mem3.szMaster - nBlock;
 1.16527 +    assert( newi > mem3.iMaster+1 );
 1.16528 +    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
 1.16529 +    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
 1.16530 +    mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
 1.16531 +    mem3.szMaster -= nBlock;
 1.16532 +    mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
 1.16533 +    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 1.16534 +    mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 1.16535 +    if( mem3.szMaster < mem3.mnMaster ){
 1.16536 +      mem3.mnMaster = mem3.szMaster;
 1.16537 +    }
 1.16538 +    return (void*)&mem3.aPool[newi];
 1.16539 +  }
 1.16540 +}
 1.16541 +
 1.16542 +/*
 1.16543 +** *pRoot is the head of a list of free chunks of the same size
 1.16544 +** or same size hash.  In other words, *pRoot is an entry in either
 1.16545 +** mem3.aiSmall[] or mem3.aiHash[].  
 1.16546 +**
 1.16547 +** This routine examines all entries on the given list and tries
 1.16548 +** to coalesce each entries with adjacent free chunks.  
 1.16549 +**
 1.16550 +** If it sees a chunk that is larger than mem3.iMaster, it replaces 
 1.16551 +** the current mem3.iMaster with the new larger chunk.  In order for
 1.16552 +** this mem3.iMaster replacement to work, the master chunk must be
 1.16553 +** linked into the hash tables.  That is not the normal state of
 1.16554 +** affairs, of course.  The calling routine must link the master
 1.16555 +** chunk before invoking this routine, then must unlink the (possibly
 1.16556 +** changed) master chunk once this routine has finished.
 1.16557 +*/
 1.16558 +static void memsys3Merge(u32 *pRoot){
 1.16559 +  u32 iNext, prev, size, i, x;
 1.16560 +
 1.16561 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16562 +  for(i=*pRoot; i>0; i=iNext){
 1.16563 +    iNext = mem3.aPool[i].u.list.next;
 1.16564 +    size = mem3.aPool[i-1].u.hdr.size4x;
 1.16565 +    assert( (size&1)==0 );
 1.16566 +    if( (size&2)==0 ){
 1.16567 +      memsys3UnlinkFromList(i, pRoot);
 1.16568 +      assert( i > mem3.aPool[i-1].u.hdr.prevSize );
 1.16569 +      prev = i - mem3.aPool[i-1].u.hdr.prevSize;
 1.16570 +      if( prev==iNext ){
 1.16571 +        iNext = mem3.aPool[prev].u.list.next;
 1.16572 +      }
 1.16573 +      memsys3Unlink(prev);
 1.16574 +      size = i + size/4 - prev;
 1.16575 +      x = mem3.aPool[prev-1].u.hdr.size4x & 2;
 1.16576 +      mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
 1.16577 +      mem3.aPool[prev+size-1].u.hdr.prevSize = size;
 1.16578 +      memsys3Link(prev);
 1.16579 +      i = prev;
 1.16580 +    }else{
 1.16581 +      size /= 4;
 1.16582 +    }
 1.16583 +    if( size>mem3.szMaster ){
 1.16584 +      mem3.iMaster = i;
 1.16585 +      mem3.szMaster = size;
 1.16586 +    }
 1.16587 +  }
 1.16588 +}
 1.16589 +
 1.16590 +/*
 1.16591 +** Return a block of memory of at least nBytes in size.
 1.16592 +** Return NULL if unable.
 1.16593 +**
 1.16594 +** This function assumes that the necessary mutexes, if any, are
 1.16595 +** already held by the caller. Hence "Unsafe".
 1.16596 +*/
 1.16597 +static void *memsys3MallocUnsafe(int nByte){
 1.16598 +  u32 i;
 1.16599 +  u32 nBlock;
 1.16600 +  u32 toFree;
 1.16601 +
 1.16602 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16603 +  assert( sizeof(Mem3Block)==8 );
 1.16604 +  if( nByte<=12 ){
 1.16605 +    nBlock = 2;
 1.16606 +  }else{
 1.16607 +    nBlock = (nByte + 11)/8;
 1.16608 +  }
 1.16609 +  assert( nBlock>=2 );
 1.16610 +
 1.16611 +  /* STEP 1:
 1.16612 +  ** Look for an entry of the correct size in either the small
 1.16613 +  ** chunk table or in the large chunk hash table.  This is
 1.16614 +  ** successful most of the time (about 9 times out of 10).
 1.16615 +  */
 1.16616 +  if( nBlock <= MX_SMALL ){
 1.16617 +    i = mem3.aiSmall[nBlock-2];
 1.16618 +    if( i>0 ){
 1.16619 +      memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
 1.16620 +      return memsys3Checkout(i, nBlock);
 1.16621 +    }
 1.16622 +  }else{
 1.16623 +    int hash = nBlock % N_HASH;
 1.16624 +    for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
 1.16625 +      if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
 1.16626 +        memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
 1.16627 +        return memsys3Checkout(i, nBlock);
 1.16628 +      }
 1.16629 +    }
 1.16630 +  }
 1.16631 +
 1.16632 +  /* STEP 2:
 1.16633 +  ** Try to satisfy the allocation by carving a piece off of the end
 1.16634 +  ** of the master chunk.  This step usually works if step 1 fails.
 1.16635 +  */
 1.16636 +  if( mem3.szMaster>=nBlock ){
 1.16637 +    return memsys3FromMaster(nBlock);
 1.16638 +  }
 1.16639 +
 1.16640 +
 1.16641 +  /* STEP 3:  
 1.16642 +  ** Loop through the entire memory pool.  Coalesce adjacent free
 1.16643 +  ** chunks.  Recompute the master chunk as the largest free chunk.
 1.16644 +  ** Then try again to satisfy the allocation by carving a piece off
 1.16645 +  ** of the end of the master chunk.  This step happens very
 1.16646 +  ** rarely (we hope!)
 1.16647 +  */
 1.16648 +  for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
 1.16649 +    memsys3OutOfMemory(toFree);
 1.16650 +    if( mem3.iMaster ){
 1.16651 +      memsys3Link(mem3.iMaster);
 1.16652 +      mem3.iMaster = 0;
 1.16653 +      mem3.szMaster = 0;
 1.16654 +    }
 1.16655 +    for(i=0; i<N_HASH; i++){
 1.16656 +      memsys3Merge(&mem3.aiHash[i]);
 1.16657 +    }
 1.16658 +    for(i=0; i<MX_SMALL-1; i++){
 1.16659 +      memsys3Merge(&mem3.aiSmall[i]);
 1.16660 +    }
 1.16661 +    if( mem3.szMaster ){
 1.16662 +      memsys3Unlink(mem3.iMaster);
 1.16663 +      if( mem3.szMaster>=nBlock ){
 1.16664 +        return memsys3FromMaster(nBlock);
 1.16665 +      }
 1.16666 +    }
 1.16667 +  }
 1.16668 +
 1.16669 +  /* If none of the above worked, then we fail. */
 1.16670 +  return 0;
 1.16671 +}
 1.16672 +
 1.16673 +/*
 1.16674 +** Free an outstanding memory allocation.
 1.16675 +**
 1.16676 +** This function assumes that the necessary mutexes, if any, are
 1.16677 +** already held by the caller. Hence "Unsafe".
 1.16678 +*/
 1.16679 +static void memsys3FreeUnsafe(void *pOld){
 1.16680 +  Mem3Block *p = (Mem3Block*)pOld;
 1.16681 +  int i;
 1.16682 +  u32 size, x;
 1.16683 +  assert( sqlite3_mutex_held(mem3.mutex) );
 1.16684 +  assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
 1.16685 +  i = p - mem3.aPool;
 1.16686 +  assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
 1.16687 +  size = mem3.aPool[i-1].u.hdr.size4x/4;
 1.16688 +  assert( i+size<=mem3.nPool+1 );
 1.16689 +  mem3.aPool[i-1].u.hdr.size4x &= ~1;
 1.16690 +  mem3.aPool[i+size-1].u.hdr.prevSize = size;
 1.16691 +  mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
 1.16692 +  memsys3Link(i);
 1.16693 +
 1.16694 +  /* Try to expand the master using the newly freed chunk */
 1.16695 +  if( mem3.iMaster ){
 1.16696 +    while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
 1.16697 +      size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
 1.16698 +      mem3.iMaster -= size;
 1.16699 +      mem3.szMaster += size;
 1.16700 +      memsys3Unlink(mem3.iMaster);
 1.16701 +      x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 1.16702 +      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 1.16703 +      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
 1.16704 +    }
 1.16705 +    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 1.16706 +    while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
 1.16707 +      memsys3Unlink(mem3.iMaster+mem3.szMaster);
 1.16708 +      mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
 1.16709 +      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 1.16710 +      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
 1.16711 +    }
 1.16712 +  }
 1.16713 +}
 1.16714 +
 1.16715 +/*
 1.16716 +** Return the size of an outstanding allocation, in bytes.  The
 1.16717 +** size returned omits the 8-byte header overhead.  This only
 1.16718 +** works for chunks that are currently checked out.
 1.16719 +*/
 1.16720 +static int memsys3Size(void *p){
 1.16721 +  Mem3Block *pBlock;
 1.16722 +  if( p==0 ) return 0;
 1.16723 +  pBlock = (Mem3Block*)p;
 1.16724 +  assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
 1.16725 +  return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
 1.16726 +}
 1.16727 +
 1.16728 +/*
 1.16729 +** Round up a request size to the next valid allocation size.
 1.16730 +*/
 1.16731 +static int memsys3Roundup(int n){
 1.16732 +  if( n<=12 ){
 1.16733 +    return 12;
 1.16734 +  }else{
 1.16735 +    return ((n+11)&~7) - 4;
 1.16736 +  }
 1.16737 +}
 1.16738 +
 1.16739 +/*
 1.16740 +** Allocate nBytes of memory.
 1.16741 +*/
 1.16742 +static void *memsys3Malloc(int nBytes){
 1.16743 +  sqlite3_int64 *p;
 1.16744 +  assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
 1.16745 +  memsys3Enter();
 1.16746 +  p = memsys3MallocUnsafe(nBytes);
 1.16747 +  memsys3Leave();
 1.16748 +  return (void*)p; 
 1.16749 +}
 1.16750 +
 1.16751 +/*
 1.16752 +** Free memory.
 1.16753 +*/
 1.16754 +static void memsys3Free(void *pPrior){
 1.16755 +  assert( pPrior );
 1.16756 +  memsys3Enter();
 1.16757 +  memsys3FreeUnsafe(pPrior);
 1.16758 +  memsys3Leave();
 1.16759 +}
 1.16760 +
 1.16761 +/*
 1.16762 +** Change the size of an existing memory allocation
 1.16763 +*/
 1.16764 +static void *memsys3Realloc(void *pPrior, int nBytes){
 1.16765 +  int nOld;
 1.16766 +  void *p;
 1.16767 +  if( pPrior==0 ){
 1.16768 +    return sqlite3_malloc(nBytes);
 1.16769 +  }
 1.16770 +  if( nBytes<=0 ){
 1.16771 +    sqlite3_free(pPrior);
 1.16772 +    return 0;
 1.16773 +  }
 1.16774 +  nOld = memsys3Size(pPrior);
 1.16775 +  if( nBytes<=nOld && nBytes>=nOld-128 ){
 1.16776 +    return pPrior;
 1.16777 +  }
 1.16778 +  memsys3Enter();
 1.16779 +  p = memsys3MallocUnsafe(nBytes);
 1.16780 +  if( p ){
 1.16781 +    if( nOld<nBytes ){
 1.16782 +      memcpy(p, pPrior, nOld);
 1.16783 +    }else{
 1.16784 +      memcpy(p, pPrior, nBytes);
 1.16785 +    }
 1.16786 +    memsys3FreeUnsafe(pPrior);
 1.16787 +  }
 1.16788 +  memsys3Leave();
 1.16789 +  return p;
 1.16790 +}
 1.16791 +
 1.16792 +/*
 1.16793 +** Initialize this module.
 1.16794 +*/
 1.16795 +static int memsys3Init(void *NotUsed){
 1.16796 +  UNUSED_PARAMETER(NotUsed);
 1.16797 +  if( !sqlite3GlobalConfig.pHeap ){
 1.16798 +    return SQLITE_ERROR;
 1.16799 +  }
 1.16800 +
 1.16801 +  /* Store a pointer to the memory block in global structure mem3. */
 1.16802 +  assert( sizeof(Mem3Block)==8 );
 1.16803 +  mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
 1.16804 +  mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
 1.16805 +
 1.16806 +  /* Initialize the master block. */
 1.16807 +  mem3.szMaster = mem3.nPool;
 1.16808 +  mem3.mnMaster = mem3.szMaster;
 1.16809 +  mem3.iMaster = 1;
 1.16810 +  mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
 1.16811 +  mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
 1.16812 +  mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
 1.16813 +
 1.16814 +  return SQLITE_OK;
 1.16815 +}
 1.16816 +
 1.16817 +/*
 1.16818 +** Deinitialize this module.
 1.16819 +*/
 1.16820 +static void memsys3Shutdown(void *NotUsed){
 1.16821 +  UNUSED_PARAMETER(NotUsed);
 1.16822 +  mem3.mutex = 0;
 1.16823 +  return;
 1.16824 +}
 1.16825 +
 1.16826 +
 1.16827 +
 1.16828 +/*
 1.16829 +** Open the file indicated and write a log of all unfreed memory 
 1.16830 +** allocations into that log.
 1.16831 +*/
 1.16832 +SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
 1.16833 +#ifdef SQLITE_DEBUG
 1.16834 +  FILE *out;
 1.16835 +  u32 i, j;
 1.16836 +  u32 size;
 1.16837 +  if( zFilename==0 || zFilename[0]==0 ){
 1.16838 +    out = stdout;
 1.16839 +  }else{
 1.16840 +    out = fopen(zFilename, "w");
 1.16841 +    if( out==0 ){
 1.16842 +      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 1.16843 +                      zFilename);
 1.16844 +      return;
 1.16845 +    }
 1.16846 +  }
 1.16847 +  memsys3Enter();
 1.16848 +  fprintf(out, "CHUNKS:\n");
 1.16849 +  for(i=1; i<=mem3.nPool; i+=size/4){
 1.16850 +    size = mem3.aPool[i-1].u.hdr.size4x;
 1.16851 +    if( size/4<=1 ){
 1.16852 +      fprintf(out, "%p size error\n", &mem3.aPool[i]);
 1.16853 +      assert( 0 );
 1.16854 +      break;
 1.16855 +    }
 1.16856 +    if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
 1.16857 +      fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
 1.16858 +      assert( 0 );
 1.16859 +      break;
 1.16860 +    }
 1.16861 +    if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
 1.16862 +      fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
 1.16863 +      assert( 0 );
 1.16864 +      break;
 1.16865 +    }
 1.16866 +    if( size&1 ){
 1.16867 +      fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
 1.16868 +    }else{
 1.16869 +      fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
 1.16870 +                  i==mem3.iMaster ? " **master**" : "");
 1.16871 +    }
 1.16872 +  }
 1.16873 +  for(i=0; i<MX_SMALL-1; i++){
 1.16874 +    if( mem3.aiSmall[i]==0 ) continue;
 1.16875 +    fprintf(out, "small(%2d):", i);
 1.16876 +    for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
 1.16877 +      fprintf(out, " %p(%d)", &mem3.aPool[j],
 1.16878 +              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
 1.16879 +    }
 1.16880 +    fprintf(out, "\n"); 
 1.16881 +  }
 1.16882 +  for(i=0; i<N_HASH; i++){
 1.16883 +    if( mem3.aiHash[i]==0 ) continue;
 1.16884 +    fprintf(out, "hash(%2d):", i);
 1.16885 +    for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
 1.16886 +      fprintf(out, " %p(%d)", &mem3.aPool[j],
 1.16887 +              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
 1.16888 +    }
 1.16889 +    fprintf(out, "\n"); 
 1.16890 +  }
 1.16891 +  fprintf(out, "master=%d\n", mem3.iMaster);
 1.16892 +  fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
 1.16893 +  fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
 1.16894 +  sqlite3_mutex_leave(mem3.mutex);
 1.16895 +  if( out==stdout ){
 1.16896 +    fflush(stdout);
 1.16897 +  }else{
 1.16898 +    fclose(out);
 1.16899 +  }
 1.16900 +#else
 1.16901 +  UNUSED_PARAMETER(zFilename);
 1.16902 +#endif
 1.16903 +}
 1.16904 +
 1.16905 +/*
 1.16906 +** This routine is the only routine in this file with external 
 1.16907 +** linkage.
 1.16908 +**
 1.16909 +** Populate the low-level memory allocation function pointers in
 1.16910 +** sqlite3GlobalConfig.m with pointers to the routines in this file. The
 1.16911 +** arguments specify the block of memory to manage.
 1.16912 +**
 1.16913 +** This routine is only called by sqlite3_config(), and therefore
 1.16914 +** is not required to be threadsafe (it is not).
 1.16915 +*/
 1.16916 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
 1.16917 +  static const sqlite3_mem_methods mempoolMethods = {
 1.16918 +     memsys3Malloc,
 1.16919 +     memsys3Free,
 1.16920 +     memsys3Realloc,
 1.16921 +     memsys3Size,
 1.16922 +     memsys3Roundup,
 1.16923 +     memsys3Init,
 1.16924 +     memsys3Shutdown,
 1.16925 +     0
 1.16926 +  };
 1.16927 +  return &mempoolMethods;
 1.16928 +}
 1.16929 +
 1.16930 +#endif /* SQLITE_ENABLE_MEMSYS3 */
 1.16931 +
 1.16932 +/************** End of mem3.c ************************************************/
 1.16933 +/************** Begin file mem5.c ********************************************/
 1.16934 +/*
 1.16935 +** 2007 October 14
 1.16936 +**
 1.16937 +** The author disclaims copyright to this source code.  In place of
 1.16938 +** a legal notice, here is a blessing:
 1.16939 +**
 1.16940 +**    May you do good and not evil.
 1.16941 +**    May you find forgiveness for yourself and forgive others.
 1.16942 +**    May you share freely, never taking more than you give.
 1.16943 +**
 1.16944 +*************************************************************************
 1.16945 +** This file contains the C functions that implement a memory
 1.16946 +** allocation subsystem for use by SQLite. 
 1.16947 +**
 1.16948 +** This version of the memory allocation subsystem omits all
 1.16949 +** use of malloc(). The application gives SQLite a block of memory
 1.16950 +** before calling sqlite3_initialize() from which allocations
 1.16951 +** are made and returned by the xMalloc() and xRealloc() 
 1.16952 +** implementations. Once sqlite3_initialize() has been called,
 1.16953 +** the amount of memory available to SQLite is fixed and cannot
 1.16954 +** be changed.
 1.16955 +**
 1.16956 +** This version of the memory allocation subsystem is included
 1.16957 +** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
 1.16958 +**
 1.16959 +** This memory allocator uses the following algorithm:
 1.16960 +**
 1.16961 +**   1.  All memory allocations sizes are rounded up to a power of 2.
 1.16962 +**
 1.16963 +**   2.  If two adjacent free blocks are the halves of a larger block,
 1.16964 +**       then the two blocks are coalesed into the single larger block.
 1.16965 +**
 1.16966 +**   3.  New memory is allocated from the first available free block.
 1.16967 +**
 1.16968 +** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
 1.16969 +** Concerning Dynamic Storage Allocation". Journal of the Association for
 1.16970 +** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
 1.16971 +** 
 1.16972 +** Let n be the size of the largest allocation divided by the minimum
 1.16973 +** allocation size (after rounding all sizes up to a power of 2.)  Let M
 1.16974 +** be the maximum amount of memory ever outstanding at one time.  Let
 1.16975 +** N be the total amount of memory available for allocation.  Robson
 1.16976 +** proved that this memory allocator will never breakdown due to 
 1.16977 +** fragmentation as long as the following constraint holds:
 1.16978 +**
 1.16979 +**      N >=  M*(1 + log2(n)/2) - n + 1
 1.16980 +**
 1.16981 +** The sqlite3_status() logic tracks the maximum values of n and M so
 1.16982 +** that an application can, at any time, verify this constraint.
 1.16983 +*/
 1.16984 +
 1.16985 +/*
 1.16986 +** This version of the memory allocator is used only when 
 1.16987 +** SQLITE_ENABLE_MEMSYS5 is defined.
 1.16988 +*/
 1.16989 +#ifdef SQLITE_ENABLE_MEMSYS5
 1.16990 +
 1.16991 +/*
 1.16992 +** A minimum allocation is an instance of the following structure.
 1.16993 +** Larger allocations are an array of these structures where the
 1.16994 +** size of the array is a power of 2.
 1.16995 +**
 1.16996 +** The size of this object must be a power of two.  That fact is
 1.16997 +** verified in memsys5Init().
 1.16998 +*/
 1.16999 +typedef struct Mem5Link Mem5Link;
 1.17000 +struct Mem5Link {
 1.17001 +  int next;       /* Index of next free chunk */
 1.17002 +  int prev;       /* Index of previous free chunk */
 1.17003 +};
 1.17004 +
 1.17005 +/*
 1.17006 +** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
 1.17007 +** mem5.szAtom is always at least 8 and 32-bit integers are used,
 1.17008 +** it is not actually possible to reach this limit.
 1.17009 +*/
 1.17010 +#define LOGMAX 30
 1.17011 +
 1.17012 +/*
 1.17013 +** Masks used for mem5.aCtrl[] elements.
 1.17014 +*/
 1.17015 +#define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
 1.17016 +#define CTRL_FREE     0x20    /* True if not checked out */
 1.17017 +
 1.17018 +/*
 1.17019 +** All of the static variables used by this module are collected
 1.17020 +** into a single structure named "mem5".  This is to keep the
 1.17021 +** static variables organized and to reduce namespace pollution
 1.17022 +** when this module is combined with other in the amalgamation.
 1.17023 +*/
 1.17024 +static SQLITE_WSD struct Mem5Global {
 1.17025 +  /*
 1.17026 +  ** Memory available for allocation
 1.17027 +  */
 1.17028 +  int szAtom;      /* Smallest possible allocation in bytes */
 1.17029 +  int nBlock;      /* Number of szAtom sized blocks in zPool */
 1.17030 +  u8 *zPool;       /* Memory available to be allocated */
 1.17031 +  
 1.17032 +  /*
 1.17033 +  ** Mutex to control access to the memory allocation subsystem.
 1.17034 +  */
 1.17035 +  sqlite3_mutex *mutex;
 1.17036 +
 1.17037 +  /*
 1.17038 +  ** Performance statistics
 1.17039 +  */
 1.17040 +  u64 nAlloc;         /* Total number of calls to malloc */
 1.17041 +  u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
 1.17042 +  u64 totalExcess;    /* Total internal fragmentation */
 1.17043 +  u32 currentOut;     /* Current checkout, including internal fragmentation */
 1.17044 +  u32 currentCount;   /* Current number of distinct checkouts */
 1.17045 +  u32 maxOut;         /* Maximum instantaneous currentOut */
 1.17046 +  u32 maxCount;       /* Maximum instantaneous currentCount */
 1.17047 +  u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
 1.17048 +  
 1.17049 +  /*
 1.17050 +  ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
 1.17051 +  ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
 1.17052 +  ** and so forth.
 1.17053 +  */
 1.17054 +  int aiFreelist[LOGMAX+1];
 1.17055 +
 1.17056 +  /*
 1.17057 +  ** Space for tracking which blocks are checked out and the size
 1.17058 +  ** of each block.  One byte per block.
 1.17059 +  */
 1.17060 +  u8 *aCtrl;
 1.17061 +
 1.17062 +} mem5;
 1.17063 +
 1.17064 +/*
 1.17065 +** Access the static variable through a macro for SQLITE_OMIT_WSD
 1.17066 +*/
 1.17067 +#define mem5 GLOBAL(struct Mem5Global, mem5)
 1.17068 +
 1.17069 +/*
 1.17070 +** Assuming mem5.zPool is divided up into an array of Mem5Link
 1.17071 +** structures, return a pointer to the idx-th such lik.
 1.17072 +*/
 1.17073 +#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
 1.17074 +
 1.17075 +/*
 1.17076 +** Unlink the chunk at mem5.aPool[i] from list it is currently
 1.17077 +** on.  It should be found on mem5.aiFreelist[iLogsize].
 1.17078 +*/
 1.17079 +static void memsys5Unlink(int i, int iLogsize){
 1.17080 +  int next, prev;
 1.17081 +  assert( i>=0 && i<mem5.nBlock );
 1.17082 +  assert( iLogsize>=0 && iLogsize<=LOGMAX );
 1.17083 +  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 1.17084 +
 1.17085 +  next = MEM5LINK(i)->next;
 1.17086 +  prev = MEM5LINK(i)->prev;
 1.17087 +  if( prev<0 ){
 1.17088 +    mem5.aiFreelist[iLogsize] = next;
 1.17089 +  }else{
 1.17090 +    MEM5LINK(prev)->next = next;
 1.17091 +  }
 1.17092 +  if( next>=0 ){
 1.17093 +    MEM5LINK(next)->prev = prev;
 1.17094 +  }
 1.17095 +}
 1.17096 +
 1.17097 +/*
 1.17098 +** Link the chunk at mem5.aPool[i] so that is on the iLogsize
 1.17099 +** free list.
 1.17100 +*/
 1.17101 +static void memsys5Link(int i, int iLogsize){
 1.17102 +  int x;
 1.17103 +  assert( sqlite3_mutex_held(mem5.mutex) );
 1.17104 +  assert( i>=0 && i<mem5.nBlock );
 1.17105 +  assert( iLogsize>=0 && iLogsize<=LOGMAX );
 1.17106 +  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 1.17107 +
 1.17108 +  x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
 1.17109 +  MEM5LINK(i)->prev = -1;
 1.17110 +  if( x>=0 ){
 1.17111 +    assert( x<mem5.nBlock );
 1.17112 +    MEM5LINK(x)->prev = i;
 1.17113 +  }
 1.17114 +  mem5.aiFreelist[iLogsize] = i;
 1.17115 +}
 1.17116 +
 1.17117 +/*
 1.17118 +** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
 1.17119 +** will already be held (obtained by code in malloc.c) if
 1.17120 +** sqlite3GlobalConfig.bMemStat is true.
 1.17121 +*/
 1.17122 +static void memsys5Enter(void){
 1.17123 +  sqlite3_mutex_enter(mem5.mutex);
 1.17124 +}
 1.17125 +static void memsys5Leave(void){
 1.17126 +  sqlite3_mutex_leave(mem5.mutex);
 1.17127 +}
 1.17128 +
 1.17129 +/*
 1.17130 +** Return the size of an outstanding allocation, in bytes.  The
 1.17131 +** size returned omits the 8-byte header overhead.  This only
 1.17132 +** works for chunks that are currently checked out.
 1.17133 +*/
 1.17134 +static int memsys5Size(void *p){
 1.17135 +  int iSize = 0;
 1.17136 +  if( p ){
 1.17137 +    int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
 1.17138 +    assert( i>=0 && i<mem5.nBlock );
 1.17139 +    iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
 1.17140 +  }
 1.17141 +  return iSize;
 1.17142 +}
 1.17143 +
 1.17144 +/*
 1.17145 +** Find the first entry on the freelist iLogsize.  Unlink that
 1.17146 +** entry and return its index. 
 1.17147 +*/
 1.17148 +static int memsys5UnlinkFirst(int iLogsize){
 1.17149 +  int i;
 1.17150 +  int iFirst;
 1.17151 +
 1.17152 +  assert( iLogsize>=0 && iLogsize<=LOGMAX );
 1.17153 +  i = iFirst = mem5.aiFreelist[iLogsize];
 1.17154 +  assert( iFirst>=0 );
 1.17155 +  while( i>0 ){
 1.17156 +    if( i<iFirst ) iFirst = i;
 1.17157 +    i = MEM5LINK(i)->next;
 1.17158 +  }
 1.17159 +  memsys5Unlink(iFirst, iLogsize);
 1.17160 +  return iFirst;
 1.17161 +}
 1.17162 +
 1.17163 +/*
 1.17164 +** Return a block of memory of at least nBytes in size.
 1.17165 +** Return NULL if unable.  Return NULL if nBytes==0.
 1.17166 +**
 1.17167 +** The caller guarantees that nByte positive.
 1.17168 +**
 1.17169 +** The caller has obtained a mutex prior to invoking this
 1.17170 +** routine so there is never any chance that two or more
 1.17171 +** threads can be in this routine at the same time.
 1.17172 +*/
 1.17173 +static void *memsys5MallocUnsafe(int nByte){
 1.17174 +  int i;           /* Index of a mem5.aPool[] slot */
 1.17175 +  int iBin;        /* Index into mem5.aiFreelist[] */
 1.17176 +  int iFullSz;     /* Size of allocation rounded up to power of 2 */
 1.17177 +  int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
 1.17178 +
 1.17179 +  /* nByte must be a positive */
 1.17180 +  assert( nByte>0 );
 1.17181 +
 1.17182 +  /* Keep track of the maximum allocation request.  Even unfulfilled
 1.17183 +  ** requests are counted */
 1.17184 +  if( (u32)nByte>mem5.maxRequest ){
 1.17185 +    mem5.maxRequest = nByte;
 1.17186 +  }
 1.17187 +
 1.17188 +  /* Abort if the requested allocation size is larger than the largest
 1.17189 +  ** power of two that we can represent using 32-bit signed integers.
 1.17190 +  */
 1.17191 +  if( nByte > 0x40000000 ){
 1.17192 +    return 0;
 1.17193 +  }
 1.17194 +
 1.17195 +  /* Round nByte up to the next valid power of two */
 1.17196 +  for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
 1.17197 +
 1.17198 +  /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
 1.17199 +  ** block.  If not, then split a block of the next larger power of
 1.17200 +  ** two in order to create a new free block of size iLogsize.
 1.17201 +  */
 1.17202 +  for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
 1.17203 +  if( iBin>LOGMAX ){
 1.17204 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.17205 +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
 1.17206 +    return 0;
 1.17207 +  }
 1.17208 +  i = memsys5UnlinkFirst(iBin);
 1.17209 +  while( iBin>iLogsize ){
 1.17210 +    int newSize;
 1.17211 +
 1.17212 +    iBin--;
 1.17213 +    newSize = 1 << iBin;
 1.17214 +    mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
 1.17215 +    memsys5Link(i+newSize, iBin);
 1.17216 +  }
 1.17217 +  mem5.aCtrl[i] = iLogsize;
 1.17218 +
 1.17219 +  /* Update allocator performance statistics. */
 1.17220 +  mem5.nAlloc++;
 1.17221 +  mem5.totalAlloc += iFullSz;
 1.17222 +  mem5.totalExcess += iFullSz - nByte;
 1.17223 +  mem5.currentCount++;
 1.17224 +  mem5.currentOut += iFullSz;
 1.17225 +  if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
 1.17226 +  if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
 1.17227 +
 1.17228 +  /* Return a pointer to the allocated memory. */
 1.17229 +  return (void*)&mem5.zPool[i*mem5.szAtom];
 1.17230 +}
 1.17231 +
 1.17232 +/*
 1.17233 +** Free an outstanding memory allocation.
 1.17234 +*/
 1.17235 +static void memsys5FreeUnsafe(void *pOld){
 1.17236 +  u32 size, iLogsize;
 1.17237 +  int iBlock;
 1.17238 +
 1.17239 +  /* Set iBlock to the index of the block pointed to by pOld in 
 1.17240 +  ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
 1.17241 +  */
 1.17242 +  iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
 1.17243 +
 1.17244 +  /* Check that the pointer pOld points to a valid, non-free block. */
 1.17245 +  assert( iBlock>=0 && iBlock<mem5.nBlock );
 1.17246 +  assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
 1.17247 +  assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
 1.17248 +
 1.17249 +  iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
 1.17250 +  size = 1<<iLogsize;
 1.17251 +  assert( iBlock+size-1<(u32)mem5.nBlock );
 1.17252 +
 1.17253 +  mem5.aCtrl[iBlock] |= CTRL_FREE;
 1.17254 +  mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
 1.17255 +  assert( mem5.currentCount>0 );
 1.17256 +  assert( mem5.currentOut>=(size*mem5.szAtom) );
 1.17257 +  mem5.currentCount--;
 1.17258 +  mem5.currentOut -= size*mem5.szAtom;
 1.17259 +  assert( mem5.currentOut>0 || mem5.currentCount==0 );
 1.17260 +  assert( mem5.currentCount>0 || mem5.currentOut==0 );
 1.17261 +
 1.17262 +  mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
 1.17263 +  while( ALWAYS(iLogsize<LOGMAX) ){
 1.17264 +    int iBuddy;
 1.17265 +    if( (iBlock>>iLogsize) & 1 ){
 1.17266 +      iBuddy = iBlock - size;
 1.17267 +    }else{
 1.17268 +      iBuddy = iBlock + size;
 1.17269 +    }
 1.17270 +    assert( iBuddy>=0 );
 1.17271 +    if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
 1.17272 +    if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
 1.17273 +    memsys5Unlink(iBuddy, iLogsize);
 1.17274 +    iLogsize++;
 1.17275 +    if( iBuddy<iBlock ){
 1.17276 +      mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
 1.17277 +      mem5.aCtrl[iBlock] = 0;
 1.17278 +      iBlock = iBuddy;
 1.17279 +    }else{
 1.17280 +      mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
 1.17281 +      mem5.aCtrl[iBuddy] = 0;
 1.17282 +    }
 1.17283 +    size *= 2;
 1.17284 +  }
 1.17285 +  memsys5Link(iBlock, iLogsize);
 1.17286 +}
 1.17287 +
 1.17288 +/*
 1.17289 +** Allocate nBytes of memory
 1.17290 +*/
 1.17291 +static void *memsys5Malloc(int nBytes){
 1.17292 +  sqlite3_int64 *p = 0;
 1.17293 +  if( nBytes>0 ){
 1.17294 +    memsys5Enter();
 1.17295 +    p = memsys5MallocUnsafe(nBytes);
 1.17296 +    memsys5Leave();
 1.17297 +  }
 1.17298 +  return (void*)p; 
 1.17299 +}
 1.17300 +
 1.17301 +/*
 1.17302 +** Free memory.
 1.17303 +**
 1.17304 +** The outer layer memory allocator prevents this routine from
 1.17305 +** being called with pPrior==0.
 1.17306 +*/
 1.17307 +static void memsys5Free(void *pPrior){
 1.17308 +  assert( pPrior!=0 );
 1.17309 +  memsys5Enter();
 1.17310 +  memsys5FreeUnsafe(pPrior);
 1.17311 +  memsys5Leave();  
 1.17312 +}
 1.17313 +
 1.17314 +/*
 1.17315 +** Change the size of an existing memory allocation.
 1.17316 +**
 1.17317 +** The outer layer memory allocator prevents this routine from
 1.17318 +** being called with pPrior==0.  
 1.17319 +**
 1.17320 +** nBytes is always a value obtained from a prior call to
 1.17321 +** memsys5Round().  Hence nBytes is always a non-negative power
 1.17322 +** of two.  If nBytes==0 that means that an oversize allocation
 1.17323 +** (an allocation larger than 0x40000000) was requested and this
 1.17324 +** routine should return 0 without freeing pPrior.
 1.17325 +*/
 1.17326 +static void *memsys5Realloc(void *pPrior, int nBytes){
 1.17327 +  int nOld;
 1.17328 +  void *p;
 1.17329 +  assert( pPrior!=0 );
 1.17330 +  assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
 1.17331 +  assert( nBytes>=0 );
 1.17332 +  if( nBytes==0 ){
 1.17333 +    return 0;
 1.17334 +  }
 1.17335 +  nOld = memsys5Size(pPrior);
 1.17336 +  if( nBytes<=nOld ){
 1.17337 +    return pPrior;
 1.17338 +  }
 1.17339 +  memsys5Enter();
 1.17340 +  p = memsys5MallocUnsafe(nBytes);
 1.17341 +  if( p ){
 1.17342 +    memcpy(p, pPrior, nOld);
 1.17343 +    memsys5FreeUnsafe(pPrior);
 1.17344 +  }
 1.17345 +  memsys5Leave();
 1.17346 +  return p;
 1.17347 +}
 1.17348 +
 1.17349 +/*
 1.17350 +** Round up a request size to the next valid allocation size.  If
 1.17351 +** the allocation is too large to be handled by this allocation system,
 1.17352 +** return 0.
 1.17353 +**
 1.17354 +** All allocations must be a power of two and must be expressed by a
 1.17355 +** 32-bit signed integer.  Hence the largest allocation is 0x40000000
 1.17356 +** or 1073741824 bytes.
 1.17357 +*/
 1.17358 +static int memsys5Roundup(int n){
 1.17359 +  int iFullSz;
 1.17360 +  if( n > 0x40000000 ) return 0;
 1.17361 +  for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
 1.17362 +  return iFullSz;
 1.17363 +}
 1.17364 +
 1.17365 +/*
 1.17366 +** Return the ceiling of the logarithm base 2 of iValue.
 1.17367 +**
 1.17368 +** Examples:   memsys5Log(1) -> 0
 1.17369 +**             memsys5Log(2) -> 1
 1.17370 +**             memsys5Log(4) -> 2
 1.17371 +**             memsys5Log(5) -> 3
 1.17372 +**             memsys5Log(8) -> 3
 1.17373 +**             memsys5Log(9) -> 4
 1.17374 +*/
 1.17375 +static int memsys5Log(int iValue){
 1.17376 +  int iLog;
 1.17377 +  for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
 1.17378 +  return iLog;
 1.17379 +}
 1.17380 +
 1.17381 +/*
 1.17382 +** Initialize the memory allocator.
 1.17383 +**
 1.17384 +** This routine is not threadsafe.  The caller must be holding a mutex
 1.17385 +** to prevent multiple threads from entering at the same time.
 1.17386 +*/
 1.17387 +static int memsys5Init(void *NotUsed){
 1.17388 +  int ii;            /* Loop counter */
 1.17389 +  int nByte;         /* Number of bytes of memory available to this allocator */
 1.17390 +  u8 *zByte;         /* Memory usable by this allocator */
 1.17391 +  int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
 1.17392 +  int iOffset;       /* An offset into mem5.aCtrl[] */
 1.17393 +
 1.17394 +  UNUSED_PARAMETER(NotUsed);
 1.17395 +
 1.17396 +  /* For the purposes of this routine, disable the mutex */
 1.17397 +  mem5.mutex = 0;
 1.17398 +
 1.17399 +  /* The size of a Mem5Link object must be a power of two.  Verify that
 1.17400 +  ** this is case.
 1.17401 +  */
 1.17402 +  assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
 1.17403 +
 1.17404 +  nByte = sqlite3GlobalConfig.nHeap;
 1.17405 +  zByte = (u8*)sqlite3GlobalConfig.pHeap;
 1.17406 +  assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
 1.17407 +
 1.17408 +  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
 1.17409 +  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
 1.17410 +  mem5.szAtom = (1<<nMinLog);
 1.17411 +  while( (int)sizeof(Mem5Link)>mem5.szAtom ){
 1.17412 +    mem5.szAtom = mem5.szAtom << 1;
 1.17413 +  }
 1.17414 +
 1.17415 +  mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
 1.17416 +  mem5.zPool = zByte;
 1.17417 +  mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
 1.17418 +
 1.17419 +  for(ii=0; ii<=LOGMAX; ii++){
 1.17420 +    mem5.aiFreelist[ii] = -1;
 1.17421 +  }
 1.17422 +
 1.17423 +  iOffset = 0;
 1.17424 +  for(ii=LOGMAX; ii>=0; ii--){
 1.17425 +    int nAlloc = (1<<ii);
 1.17426 +    if( (iOffset+nAlloc)<=mem5.nBlock ){
 1.17427 +      mem5.aCtrl[iOffset] = ii | CTRL_FREE;
 1.17428 +      memsys5Link(iOffset, ii);
 1.17429 +      iOffset += nAlloc;
 1.17430 +    }
 1.17431 +    assert((iOffset+nAlloc)>mem5.nBlock);
 1.17432 +  }
 1.17433 +
 1.17434 +  /* If a mutex is required for normal operation, allocate one */
 1.17435 +  if( sqlite3GlobalConfig.bMemstat==0 ){
 1.17436 +    mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 1.17437 +  }
 1.17438 +
 1.17439 +  return SQLITE_OK;
 1.17440 +}
 1.17441 +
 1.17442 +/*
 1.17443 +** Deinitialize this module.
 1.17444 +*/
 1.17445 +static void memsys5Shutdown(void *NotUsed){
 1.17446 +  UNUSED_PARAMETER(NotUsed);
 1.17447 +  mem5.mutex = 0;
 1.17448 +  return;
 1.17449 +}
 1.17450 +
 1.17451 +#ifdef SQLITE_TEST
 1.17452 +/*
 1.17453 +** Open the file indicated and write a log of all unfreed memory 
 1.17454 +** allocations into that log.
 1.17455 +*/
 1.17456 +SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
 1.17457 +  FILE *out;
 1.17458 +  int i, j, n;
 1.17459 +  int nMinLog;
 1.17460 +
 1.17461 +  if( zFilename==0 || zFilename[0]==0 ){
 1.17462 +    out = stdout;
 1.17463 +  }else{
 1.17464 +    out = fopen(zFilename, "w");
 1.17465 +    if( out==0 ){
 1.17466 +      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 1.17467 +                      zFilename);
 1.17468 +      return;
 1.17469 +    }
 1.17470 +  }
 1.17471 +  memsys5Enter();
 1.17472 +  nMinLog = memsys5Log(mem5.szAtom);
 1.17473 +  for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
 1.17474 +    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
 1.17475 +    fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
 1.17476 +  }
 1.17477 +  fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
 1.17478 +  fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
 1.17479 +  fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
 1.17480 +  fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
 1.17481 +  fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
 1.17482 +  fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
 1.17483 +  fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
 1.17484 +  fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
 1.17485 +  memsys5Leave();
 1.17486 +  if( out==stdout ){
 1.17487 +    fflush(stdout);
 1.17488 +  }else{
 1.17489 +    fclose(out);
 1.17490 +  }
 1.17491 +}
 1.17492 +#endif
 1.17493 +
 1.17494 +/*
 1.17495 +** This routine is the only routine in this file with external 
 1.17496 +** linkage. It returns a pointer to a static sqlite3_mem_methods
 1.17497 +** struct populated with the memsys5 methods.
 1.17498 +*/
 1.17499 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
 1.17500 +  static const sqlite3_mem_methods memsys5Methods = {
 1.17501 +     memsys5Malloc,
 1.17502 +     memsys5Free,
 1.17503 +     memsys5Realloc,
 1.17504 +     memsys5Size,
 1.17505 +     memsys5Roundup,
 1.17506 +     memsys5Init,
 1.17507 +     memsys5Shutdown,
 1.17508 +     0
 1.17509 +  };
 1.17510 +  return &memsys5Methods;
 1.17511 +}
 1.17512 +
 1.17513 +#endif /* SQLITE_ENABLE_MEMSYS5 */
 1.17514 +
 1.17515 +/************** End of mem5.c ************************************************/
 1.17516 +/************** Begin file mutex.c *******************************************/
 1.17517 +/*
 1.17518 +** 2007 August 14
 1.17519 +**
 1.17520 +** The author disclaims copyright to this source code.  In place of
 1.17521 +** a legal notice, here is a blessing:
 1.17522 +**
 1.17523 +**    May you do good and not evil.
 1.17524 +**    May you find forgiveness for yourself and forgive others.
 1.17525 +**    May you share freely, never taking more than you give.
 1.17526 +**
 1.17527 +*************************************************************************
 1.17528 +** This file contains the C functions that implement mutexes.
 1.17529 +**
 1.17530 +** This file contains code that is common across all mutex implementations.
 1.17531 +*/
 1.17532 +
 1.17533 +#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
 1.17534 +/*
 1.17535 +** For debugging purposes, record when the mutex subsystem is initialized
 1.17536 +** and uninitialized so that we can assert() if there is an attempt to
 1.17537 +** allocate a mutex while the system is uninitialized.
 1.17538 +*/
 1.17539 +static SQLITE_WSD int mutexIsInit = 0;
 1.17540 +#endif /* SQLITE_DEBUG */
 1.17541 +
 1.17542 +
 1.17543 +#ifndef SQLITE_MUTEX_OMIT
 1.17544 +/*
 1.17545 +** Initialize the mutex system.
 1.17546 +*/
 1.17547 +SQLITE_PRIVATE int sqlite3MutexInit(void){ 
 1.17548 +  int rc = SQLITE_OK;
 1.17549 +  if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
 1.17550 +    /* If the xMutexAlloc method has not been set, then the user did not
 1.17551 +    ** install a mutex implementation via sqlite3_config() prior to 
 1.17552 +    ** sqlite3_initialize() being called. This block copies pointers to
 1.17553 +    ** the default implementation into the sqlite3GlobalConfig structure.
 1.17554 +    */
 1.17555 +    sqlite3_mutex_methods const *pFrom;
 1.17556 +    sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
 1.17557 +
 1.17558 +    if( sqlite3GlobalConfig.bCoreMutex ){
 1.17559 +      pFrom = sqlite3DefaultMutex();
 1.17560 +    }else{
 1.17561 +      pFrom = sqlite3NoopMutex();
 1.17562 +    }
 1.17563 +    memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
 1.17564 +    memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
 1.17565 +           sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
 1.17566 +    pTo->xMutexAlloc = pFrom->xMutexAlloc;
 1.17567 +  }
 1.17568 +  rc = sqlite3GlobalConfig.mutex.xMutexInit();
 1.17569 +
 1.17570 +#ifdef SQLITE_DEBUG
 1.17571 +  GLOBAL(int, mutexIsInit) = 1;
 1.17572 +#endif
 1.17573 +
 1.17574 +  return rc;
 1.17575 +}
 1.17576 +
 1.17577 +/*
 1.17578 +** Shutdown the mutex system. This call frees resources allocated by
 1.17579 +** sqlite3MutexInit().
 1.17580 +*/
 1.17581 +SQLITE_PRIVATE int sqlite3MutexEnd(void){
 1.17582 +  int rc = SQLITE_OK;
 1.17583 +  if( sqlite3GlobalConfig.mutex.xMutexEnd ){
 1.17584 +    rc = sqlite3GlobalConfig.mutex.xMutexEnd();
 1.17585 +  }
 1.17586 +
 1.17587 +#ifdef SQLITE_DEBUG
 1.17588 +  GLOBAL(int, mutexIsInit) = 0;
 1.17589 +#endif
 1.17590 +
 1.17591 +  return rc;
 1.17592 +}
 1.17593 +
 1.17594 +/*
 1.17595 +** Retrieve a pointer to a static mutex or allocate a new dynamic one.
 1.17596 +*/
 1.17597 +SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
 1.17598 +#ifndef SQLITE_OMIT_AUTOINIT
 1.17599 +  if( sqlite3_initialize() ) return 0;
 1.17600 +#endif
 1.17601 +  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
 1.17602 +}
 1.17603 +
 1.17604 +SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
 1.17605 +  if( !sqlite3GlobalConfig.bCoreMutex ){
 1.17606 +    return 0;
 1.17607 +  }
 1.17608 +  assert( GLOBAL(int, mutexIsInit) );
 1.17609 +  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
 1.17610 +}
 1.17611 +
 1.17612 +/*
 1.17613 +** Free a dynamic mutex.
 1.17614 +*/
 1.17615 +SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
 1.17616 +  if( p ){
 1.17617 +    sqlite3GlobalConfig.mutex.xMutexFree(p);
 1.17618 +  }
 1.17619 +}
 1.17620 +
 1.17621 +/*
 1.17622 +** Obtain the mutex p. If some other thread already has the mutex, block
 1.17623 +** until it can be obtained.
 1.17624 +*/
 1.17625 +SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
 1.17626 +  if( p ){
 1.17627 +    sqlite3GlobalConfig.mutex.xMutexEnter(p);
 1.17628 +  }
 1.17629 +}
 1.17630 +
 1.17631 +/*
 1.17632 +** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
 1.17633 +** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
 1.17634 +*/
 1.17635 +SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
 1.17636 +  int rc = SQLITE_OK;
 1.17637 +  if( p ){
 1.17638 +    return sqlite3GlobalConfig.mutex.xMutexTry(p);
 1.17639 +  }
 1.17640 +  return rc;
 1.17641 +}
 1.17642 +
 1.17643 +/*
 1.17644 +** The sqlite3_mutex_leave() routine exits a mutex that was previously
 1.17645 +** entered by the same thread.  The behavior is undefined if the mutex 
 1.17646 +** is not currently entered. If a NULL pointer is passed as an argument
 1.17647 +** this function is a no-op.
 1.17648 +*/
 1.17649 +SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
 1.17650 +  if( p ){
 1.17651 +    sqlite3GlobalConfig.mutex.xMutexLeave(p);
 1.17652 +  }
 1.17653 +}
 1.17654 +
 1.17655 +#ifndef NDEBUG
 1.17656 +/*
 1.17657 +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 1.17658 +** intended for use inside assert() statements.
 1.17659 +*/
 1.17660 +SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
 1.17661 +  return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
 1.17662 +}
 1.17663 +SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
 1.17664 +  return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 1.17665 +}
 1.17666 +#endif
 1.17667 +
 1.17668 +#endif /* !defined(SQLITE_MUTEX_OMIT) */
 1.17669 +
 1.17670 +/************** End of mutex.c ***********************************************/
 1.17671 +/************** Begin file mutex_noop.c **************************************/
 1.17672 +/*
 1.17673 +** 2008 October 07
 1.17674 +**
 1.17675 +** The author disclaims copyright to this source code.  In place of
 1.17676 +** a legal notice, here is a blessing:
 1.17677 +**
 1.17678 +**    May you do good and not evil.
 1.17679 +**    May you find forgiveness for yourself and forgive others.
 1.17680 +**    May you share freely, never taking more than you give.
 1.17681 +**
 1.17682 +*************************************************************************
 1.17683 +** This file contains the C functions that implement mutexes.
 1.17684 +**
 1.17685 +** This implementation in this file does not provide any mutual
 1.17686 +** exclusion and is thus suitable for use only in applications
 1.17687 +** that use SQLite in a single thread.  The routines defined
 1.17688 +** here are place-holders.  Applications can substitute working
 1.17689 +** mutex routines at start-time using the
 1.17690 +**
 1.17691 +**     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
 1.17692 +**
 1.17693 +** interface.
 1.17694 +**
 1.17695 +** If compiled with SQLITE_DEBUG, then additional logic is inserted
 1.17696 +** that does error checking on mutexes to make sure they are being
 1.17697 +** called correctly.
 1.17698 +*/
 1.17699 +
 1.17700 +#ifndef SQLITE_MUTEX_OMIT
 1.17701 +
 1.17702 +#ifndef SQLITE_DEBUG
 1.17703 +/*
 1.17704 +** Stub routines for all mutex methods.
 1.17705 +**
 1.17706 +** This routines provide no mutual exclusion or error checking.
 1.17707 +*/
 1.17708 +static int noopMutexInit(void){ return SQLITE_OK; }
 1.17709 +static int noopMutexEnd(void){ return SQLITE_OK; }
 1.17710 +static sqlite3_mutex *noopMutexAlloc(int id){ 
 1.17711 +  UNUSED_PARAMETER(id);
 1.17712 +  return (sqlite3_mutex*)8; 
 1.17713 +}
 1.17714 +static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 1.17715 +static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 1.17716 +static int noopMutexTry(sqlite3_mutex *p){
 1.17717 +  UNUSED_PARAMETER(p);
 1.17718 +  return SQLITE_OK;
 1.17719 +}
 1.17720 +static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 1.17721 +
 1.17722 +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
 1.17723 +  static const sqlite3_mutex_methods sMutex = {
 1.17724 +    noopMutexInit,
 1.17725 +    noopMutexEnd,
 1.17726 +    noopMutexAlloc,
 1.17727 +    noopMutexFree,
 1.17728 +    noopMutexEnter,
 1.17729 +    noopMutexTry,
 1.17730 +    noopMutexLeave,
 1.17731 +
 1.17732 +    0,
 1.17733 +    0,
 1.17734 +  };
 1.17735 +
 1.17736 +  return &sMutex;
 1.17737 +}
 1.17738 +#endif /* !SQLITE_DEBUG */
 1.17739 +
 1.17740 +#ifdef SQLITE_DEBUG
 1.17741 +/*
 1.17742 +** In this implementation, error checking is provided for testing
 1.17743 +** and debugging purposes.  The mutexes still do not provide any
 1.17744 +** mutual exclusion.
 1.17745 +*/
 1.17746 +
 1.17747 +/*
 1.17748 +** The mutex object
 1.17749 +*/
 1.17750 +typedef struct sqlite3_debug_mutex {
 1.17751 +  int id;     /* The mutex type */
 1.17752 +  int cnt;    /* Number of entries without a matching leave */
 1.17753 +} sqlite3_debug_mutex;
 1.17754 +
 1.17755 +/*
 1.17756 +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 1.17757 +** intended for use inside assert() statements.
 1.17758 +*/
 1.17759 +static int debugMutexHeld(sqlite3_mutex *pX){
 1.17760 +  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 1.17761 +  return p==0 || p->cnt>0;
 1.17762 +}
 1.17763 +static int debugMutexNotheld(sqlite3_mutex *pX){
 1.17764 +  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 1.17765 +  return p==0 || p->cnt==0;
 1.17766 +}
 1.17767 +
 1.17768 +/*
 1.17769 +** Initialize and deinitialize the mutex subsystem.
 1.17770 +*/
 1.17771 +static int debugMutexInit(void){ return SQLITE_OK; }
 1.17772 +static int debugMutexEnd(void){ return SQLITE_OK; }
 1.17773 +
 1.17774 +/*
 1.17775 +** The sqlite3_mutex_alloc() routine allocates a new
 1.17776 +** mutex and returns a pointer to it.  If it returns NULL
 1.17777 +** that means that a mutex could not be allocated. 
 1.17778 +*/
 1.17779 +static sqlite3_mutex *debugMutexAlloc(int id){
 1.17780 +  static sqlite3_debug_mutex aStatic[6];
 1.17781 +  sqlite3_debug_mutex *pNew = 0;
 1.17782 +  switch( id ){
 1.17783 +    case SQLITE_MUTEX_FAST:
 1.17784 +    case SQLITE_MUTEX_RECURSIVE: {
 1.17785 +      pNew = sqlite3Malloc(sizeof(*pNew));
 1.17786 +      if( pNew ){
 1.17787 +        pNew->id = id;
 1.17788 +        pNew->cnt = 0;
 1.17789 +      }
 1.17790 +      break;
 1.17791 +    }
 1.17792 +    default: {
 1.17793 +      assert( id-2 >= 0 );
 1.17794 +      assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
 1.17795 +      pNew = &aStatic[id-2];
 1.17796 +      pNew->id = id;
 1.17797 +      break;
 1.17798 +    }
 1.17799 +  }
 1.17800 +  return (sqlite3_mutex*)pNew;
 1.17801 +}
 1.17802 +
 1.17803 +/*
 1.17804 +** This routine deallocates a previously allocated mutex.
 1.17805 +*/
 1.17806 +static void debugMutexFree(sqlite3_mutex *pX){
 1.17807 +  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 1.17808 +  assert( p->cnt==0 );
 1.17809 +  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 1.17810 +  sqlite3_free(p);
 1.17811 +}
 1.17812 +
 1.17813 +/*
 1.17814 +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 1.17815 +** to enter a mutex.  If another thread is already within the mutex,
 1.17816 +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 1.17817 +** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 1.17818 +** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 1.17819 +** be entered multiple times by the same thread.  In such cases the,
 1.17820 +** mutex must be exited an equal number of times before another thread
 1.17821 +** can enter.  If the same thread tries to enter any other kind of mutex
 1.17822 +** more than once, the behavior is undefined.
 1.17823 +*/
 1.17824 +static void debugMutexEnter(sqlite3_mutex *pX){
 1.17825 +  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 1.17826 +  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 1.17827 +  p->cnt++;
 1.17828 +}
 1.17829 +static int debugMutexTry(sqlite3_mutex *pX){
 1.17830 +  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 1.17831 +  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 1.17832 +  p->cnt++;
 1.17833 +  return SQLITE_OK;
 1.17834 +}
 1.17835 +
 1.17836 +/*
 1.17837 +** The sqlite3_mutex_leave() routine exits a mutex that was
 1.17838 +** previously entered by the same thread.  The behavior
 1.17839 +** is undefined if the mutex is not currently entered or
 1.17840 +** is not currently allocated.  SQLite will never do either.
 1.17841 +*/
 1.17842 +static void debugMutexLeave(sqlite3_mutex *pX){
 1.17843 +  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 1.17844 +  assert( debugMutexHeld(pX) );
 1.17845 +  p->cnt--;
 1.17846 +  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 1.17847 +}
 1.17848 +
 1.17849 +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
 1.17850 +  static const sqlite3_mutex_methods sMutex = {
 1.17851 +    debugMutexInit,
 1.17852 +    debugMutexEnd,
 1.17853 +    debugMutexAlloc,
 1.17854 +    debugMutexFree,
 1.17855 +    debugMutexEnter,
 1.17856 +    debugMutexTry,
 1.17857 +    debugMutexLeave,
 1.17858 +
 1.17859 +    debugMutexHeld,
 1.17860 +    debugMutexNotheld
 1.17861 +  };
 1.17862 +
 1.17863 +  return &sMutex;
 1.17864 +}
 1.17865 +#endif /* SQLITE_DEBUG */
 1.17866 +
 1.17867 +/*
 1.17868 +** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
 1.17869 +** is used regardless of the run-time threadsafety setting.
 1.17870 +*/
 1.17871 +#ifdef SQLITE_MUTEX_NOOP
 1.17872 +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 1.17873 +  return sqlite3NoopMutex();
 1.17874 +}
 1.17875 +#endif /* defined(SQLITE_MUTEX_NOOP) */
 1.17876 +#endif /* !defined(SQLITE_MUTEX_OMIT) */
 1.17877 +
 1.17878 +/************** End of mutex_noop.c ******************************************/
 1.17879 +/************** Begin file mutex_unix.c **************************************/
 1.17880 +/*
 1.17881 +** 2007 August 28
 1.17882 +**
 1.17883 +** The author disclaims copyright to this source code.  In place of
 1.17884 +** a legal notice, here is a blessing:
 1.17885 +**
 1.17886 +**    May you do good and not evil.
 1.17887 +**    May you find forgiveness for yourself and forgive others.
 1.17888 +**    May you share freely, never taking more than you give.
 1.17889 +**
 1.17890 +*************************************************************************
 1.17891 +** This file contains the C functions that implement mutexes for pthreads
 1.17892 +*/
 1.17893 +
 1.17894 +/*
 1.17895 +** The code in this file is only used if we are compiling threadsafe
 1.17896 +** under unix with pthreads.
 1.17897 +**
 1.17898 +** Note that this implementation requires a version of pthreads that
 1.17899 +** supports recursive mutexes.
 1.17900 +*/
 1.17901 +#ifdef SQLITE_MUTEX_PTHREADS
 1.17902 +
 1.17903 +#include <pthread.h>
 1.17904 +
 1.17905 +/*
 1.17906 +** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
 1.17907 +** are necessary under two condidtions:  (1) Debug builds and (2) using
 1.17908 +** home-grown mutexes.  Encapsulate these conditions into a single #define.
 1.17909 +*/
 1.17910 +#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
 1.17911 +# define SQLITE_MUTEX_NREF 1
 1.17912 +#else
 1.17913 +# define SQLITE_MUTEX_NREF 0
 1.17914 +#endif
 1.17915 +
 1.17916 +/*
 1.17917 +** Each recursive mutex is an instance of the following structure.
 1.17918 +*/
 1.17919 +struct sqlite3_mutex {
 1.17920 +  pthread_mutex_t mutex;     /* Mutex controlling the lock */
 1.17921 +#if SQLITE_MUTEX_NREF
 1.17922 +  int id;                    /* Mutex type */
 1.17923 +  volatile int nRef;         /* Number of entrances */
 1.17924 +  volatile pthread_t owner;  /* Thread that is within this mutex */
 1.17925 +  int trace;                 /* True to trace changes */
 1.17926 +#endif
 1.17927 +};
 1.17928 +#if SQLITE_MUTEX_NREF
 1.17929 +#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
 1.17930 +#else
 1.17931 +#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
 1.17932 +#endif
 1.17933 +
 1.17934 +/*
 1.17935 +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 1.17936 +** intended for use only inside assert() statements.  On some platforms,
 1.17937 +** there might be race conditions that can cause these routines to
 1.17938 +** deliver incorrect results.  In particular, if pthread_equal() is
 1.17939 +** not an atomic operation, then these routines might delivery
 1.17940 +** incorrect results.  On most platforms, pthread_equal() is a 
 1.17941 +** comparison of two integers and is therefore atomic.  But we are
 1.17942 +** told that HPUX is not such a platform.  If so, then these routines
 1.17943 +** will not always work correctly on HPUX.
 1.17944 +**
 1.17945 +** On those platforms where pthread_equal() is not atomic, SQLite
 1.17946 +** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
 1.17947 +** make sure no assert() statements are evaluated and hence these
 1.17948 +** routines are never called.
 1.17949 +*/
 1.17950 +#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
 1.17951 +static int pthreadMutexHeld(sqlite3_mutex *p){
 1.17952 +  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
 1.17953 +}
 1.17954 +static int pthreadMutexNotheld(sqlite3_mutex *p){
 1.17955 +  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
 1.17956 +}
 1.17957 +#endif
 1.17958 +
 1.17959 +/*
 1.17960 +** Initialize and deinitialize the mutex subsystem.
 1.17961 +*/
 1.17962 +static int pthreadMutexInit(void){ return SQLITE_OK; }
 1.17963 +static int pthreadMutexEnd(void){ return SQLITE_OK; }
 1.17964 +
 1.17965 +/*
 1.17966 +** The sqlite3_mutex_alloc() routine allocates a new
 1.17967 +** mutex and returns a pointer to it.  If it returns NULL
 1.17968 +** that means that a mutex could not be allocated.  SQLite
 1.17969 +** will unwind its stack and return an error.  The argument
 1.17970 +** to sqlite3_mutex_alloc() is one of these integer constants:
 1.17971 +**
 1.17972 +** <ul>
 1.17973 +** <li>  SQLITE_MUTEX_FAST
 1.17974 +** <li>  SQLITE_MUTEX_RECURSIVE
 1.17975 +** <li>  SQLITE_MUTEX_STATIC_MASTER
 1.17976 +** <li>  SQLITE_MUTEX_STATIC_MEM
 1.17977 +** <li>  SQLITE_MUTEX_STATIC_MEM2
 1.17978 +** <li>  SQLITE_MUTEX_STATIC_PRNG
 1.17979 +** <li>  SQLITE_MUTEX_STATIC_LRU
 1.17980 +** <li>  SQLITE_MUTEX_STATIC_PMEM
 1.17981 +** </ul>
 1.17982 +**
 1.17983 +** The first two constants cause sqlite3_mutex_alloc() to create
 1.17984 +** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 1.17985 +** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 1.17986 +** The mutex implementation does not need to make a distinction
 1.17987 +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 1.17988 +** not want to.  But SQLite will only request a recursive mutex in
 1.17989 +** cases where it really needs one.  If a faster non-recursive mutex
 1.17990 +** implementation is available on the host platform, the mutex subsystem
 1.17991 +** might return such a mutex in response to SQLITE_MUTEX_FAST.
 1.17992 +**
 1.17993 +** The other allowed parameters to sqlite3_mutex_alloc() each return
 1.17994 +** a pointer to a static preexisting mutex.  Six static mutexes are
 1.17995 +** used by the current version of SQLite.  Future versions of SQLite
 1.17996 +** may add additional static mutexes.  Static mutexes are for internal
 1.17997 +** use by SQLite only.  Applications that use SQLite mutexes should
 1.17998 +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 1.17999 +** SQLITE_MUTEX_RECURSIVE.
 1.18000 +**
 1.18001 +** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
 1.18002 +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 1.18003 +** returns a different mutex on every call.  But for the static 
 1.18004 +** mutex types, the same mutex is returned on every call that has
 1.18005 +** the same type number.
 1.18006 +*/
 1.18007 +static sqlite3_mutex *pthreadMutexAlloc(int iType){
 1.18008 +  static sqlite3_mutex staticMutexes[] = {
 1.18009 +    SQLITE3_MUTEX_INITIALIZER,
 1.18010 +    SQLITE3_MUTEX_INITIALIZER,
 1.18011 +    SQLITE3_MUTEX_INITIALIZER,
 1.18012 +    SQLITE3_MUTEX_INITIALIZER,
 1.18013 +    SQLITE3_MUTEX_INITIALIZER,
 1.18014 +    SQLITE3_MUTEX_INITIALIZER
 1.18015 +  };
 1.18016 +  sqlite3_mutex *p;
 1.18017 +  switch( iType ){
 1.18018 +    case SQLITE_MUTEX_RECURSIVE: {
 1.18019 +      p = sqlite3MallocZero( sizeof(*p) );
 1.18020 +      if( p ){
 1.18021 +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 1.18022 +        /* If recursive mutexes are not available, we will have to
 1.18023 +        ** build our own.  See below. */
 1.18024 +        pthread_mutex_init(&p->mutex, 0);
 1.18025 +#else
 1.18026 +        /* Use a recursive mutex if it is available */
 1.18027 +        pthread_mutexattr_t recursiveAttr;
 1.18028 +        pthread_mutexattr_init(&recursiveAttr);
 1.18029 +        pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
 1.18030 +        pthread_mutex_init(&p->mutex, &recursiveAttr);
 1.18031 +        pthread_mutexattr_destroy(&recursiveAttr);
 1.18032 +#endif
 1.18033 +#if SQLITE_MUTEX_NREF
 1.18034 +        p->id = iType;
 1.18035 +#endif
 1.18036 +      }
 1.18037 +      break;
 1.18038 +    }
 1.18039 +    case SQLITE_MUTEX_FAST: {
 1.18040 +      p = sqlite3MallocZero( sizeof(*p) );
 1.18041 +      if( p ){
 1.18042 +#if SQLITE_MUTEX_NREF
 1.18043 +        p->id = iType;
 1.18044 +#endif
 1.18045 +        pthread_mutex_init(&p->mutex, 0);
 1.18046 +      }
 1.18047 +      break;
 1.18048 +    }
 1.18049 +    default: {
 1.18050 +      assert( iType-2 >= 0 );
 1.18051 +      assert( iType-2 < ArraySize(staticMutexes) );
 1.18052 +      p = &staticMutexes[iType-2];
 1.18053 +#if SQLITE_MUTEX_NREF
 1.18054 +      p->id = iType;
 1.18055 +#endif
 1.18056 +      break;
 1.18057 +    }
 1.18058 +  }
 1.18059 +  return p;
 1.18060 +}
 1.18061 +
 1.18062 +
 1.18063 +/*
 1.18064 +** This routine deallocates a previously
 1.18065 +** allocated mutex.  SQLite is careful to deallocate every
 1.18066 +** mutex that it allocates.
 1.18067 +*/
 1.18068 +static void pthreadMutexFree(sqlite3_mutex *p){
 1.18069 +  assert( p->nRef==0 );
 1.18070 +  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 1.18071 +  pthread_mutex_destroy(&p->mutex);
 1.18072 +  sqlite3_free(p);
 1.18073 +}
 1.18074 +
 1.18075 +/*
 1.18076 +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 1.18077 +** to enter a mutex.  If another thread is already within the mutex,
 1.18078 +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 1.18079 +** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 1.18080 +** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 1.18081 +** be entered multiple times by the same thread.  In such cases the,
 1.18082 +** mutex must be exited an equal number of times before another thread
 1.18083 +** can enter.  If the same thread tries to enter any other kind of mutex
 1.18084 +** more than once, the behavior is undefined.
 1.18085 +*/
 1.18086 +static void pthreadMutexEnter(sqlite3_mutex *p){
 1.18087 +  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 1.18088 +
 1.18089 +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 1.18090 +  /* If recursive mutexes are not available, then we have to grow
 1.18091 +  ** our own.  This implementation assumes that pthread_equal()
 1.18092 +  ** is atomic - that it cannot be deceived into thinking self
 1.18093 +  ** and p->owner are equal if p->owner changes between two values
 1.18094 +  ** that are not equal to self while the comparison is taking place.
 1.18095 +  ** This implementation also assumes a coherent cache - that 
 1.18096 +  ** separate processes cannot read different values from the same
 1.18097 +  ** address at the same time.  If either of these two conditions
 1.18098 +  ** are not met, then the mutexes will fail and problems will result.
 1.18099 +  */
 1.18100 +  {
 1.18101 +    pthread_t self = pthread_self();
 1.18102 +    if( p->nRef>0 && pthread_equal(p->owner, self) ){
 1.18103 +      p->nRef++;
 1.18104 +    }else{
 1.18105 +      pthread_mutex_lock(&p->mutex);
 1.18106 +      assert( p->nRef==0 );
 1.18107 +      p->owner = self;
 1.18108 +      p->nRef = 1;
 1.18109 +    }
 1.18110 +  }
 1.18111 +#else
 1.18112 +  /* Use the built-in recursive mutexes if they are available.
 1.18113 +  */
 1.18114 +  pthread_mutex_lock(&p->mutex);
 1.18115 +#if SQLITE_MUTEX_NREF
 1.18116 +  assert( p->nRef>0 || p->owner==0 );
 1.18117 +  p->owner = pthread_self();
 1.18118 +  p->nRef++;
 1.18119 +#endif
 1.18120 +#endif
 1.18121 +
 1.18122 +#ifdef SQLITE_DEBUG
 1.18123 +  if( p->trace ){
 1.18124 +    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 1.18125 +  }
 1.18126 +#endif
 1.18127 +}
 1.18128 +static int pthreadMutexTry(sqlite3_mutex *p){
 1.18129 +  int rc;
 1.18130 +  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 1.18131 +
 1.18132 +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 1.18133 +  /* If recursive mutexes are not available, then we have to grow
 1.18134 +  ** our own.  This implementation assumes that pthread_equal()
 1.18135 +  ** is atomic - that it cannot be deceived into thinking self
 1.18136 +  ** and p->owner are equal if p->owner changes between two values
 1.18137 +  ** that are not equal to self while the comparison is taking place.
 1.18138 +  ** This implementation also assumes a coherent cache - that 
 1.18139 +  ** separate processes cannot read different values from the same
 1.18140 +  ** address at the same time.  If either of these two conditions
 1.18141 +  ** are not met, then the mutexes will fail and problems will result.
 1.18142 +  */
 1.18143 +  {
 1.18144 +    pthread_t self = pthread_self();
 1.18145 +    if( p->nRef>0 && pthread_equal(p->owner, self) ){
 1.18146 +      p->nRef++;
 1.18147 +      rc = SQLITE_OK;
 1.18148 +    }else if( pthread_mutex_trylock(&p->mutex)==0 ){
 1.18149 +      assert( p->nRef==0 );
 1.18150 +      p->owner = self;
 1.18151 +      p->nRef = 1;
 1.18152 +      rc = SQLITE_OK;
 1.18153 +    }else{
 1.18154 +      rc = SQLITE_BUSY;
 1.18155 +    }
 1.18156 +  }
 1.18157 +#else
 1.18158 +  /* Use the built-in recursive mutexes if they are available.
 1.18159 +  */
 1.18160 +  if( pthread_mutex_trylock(&p->mutex)==0 ){
 1.18161 +#if SQLITE_MUTEX_NREF
 1.18162 +    p->owner = pthread_self();
 1.18163 +    p->nRef++;
 1.18164 +#endif
 1.18165 +    rc = SQLITE_OK;
 1.18166 +  }else{
 1.18167 +    rc = SQLITE_BUSY;
 1.18168 +  }
 1.18169 +#endif
 1.18170 +
 1.18171 +#ifdef SQLITE_DEBUG
 1.18172 +  if( rc==SQLITE_OK && p->trace ){
 1.18173 +    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 1.18174 +  }
 1.18175 +#endif
 1.18176 +  return rc;
 1.18177 +}
 1.18178 +
 1.18179 +/*
 1.18180 +** The sqlite3_mutex_leave() routine exits a mutex that was
 1.18181 +** previously entered by the same thread.  The behavior
 1.18182 +** is undefined if the mutex is not currently entered or
 1.18183 +** is not currently allocated.  SQLite will never do either.
 1.18184 +*/
 1.18185 +static void pthreadMutexLeave(sqlite3_mutex *p){
 1.18186 +  assert( pthreadMutexHeld(p) );
 1.18187 +#if SQLITE_MUTEX_NREF
 1.18188 +  p->nRef--;
 1.18189 +  if( p->nRef==0 ) p->owner = 0;
 1.18190 +#endif
 1.18191 +  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 1.18192 +
 1.18193 +#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 1.18194 +  if( p->nRef==0 ){
 1.18195 +    pthread_mutex_unlock(&p->mutex);
 1.18196 +  }
 1.18197 +#else
 1.18198 +  pthread_mutex_unlock(&p->mutex);
 1.18199 +#endif
 1.18200 +
 1.18201 +#ifdef SQLITE_DEBUG
 1.18202 +  if( p->trace ){
 1.18203 +    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 1.18204 +  }
 1.18205 +#endif
 1.18206 +}
 1.18207 +
 1.18208 +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 1.18209 +  static const sqlite3_mutex_methods sMutex = {
 1.18210 +    pthreadMutexInit,
 1.18211 +    pthreadMutexEnd,
 1.18212 +    pthreadMutexAlloc,
 1.18213 +    pthreadMutexFree,
 1.18214 +    pthreadMutexEnter,
 1.18215 +    pthreadMutexTry,
 1.18216 +    pthreadMutexLeave,
 1.18217 +#ifdef SQLITE_DEBUG
 1.18218 +    pthreadMutexHeld,
 1.18219 +    pthreadMutexNotheld
 1.18220 +#else
 1.18221 +    0,
 1.18222 +    0
 1.18223 +#endif
 1.18224 +  };
 1.18225 +
 1.18226 +  return &sMutex;
 1.18227 +}
 1.18228 +
 1.18229 +#endif /* SQLITE_MUTEX_PTHREADS */
 1.18230 +
 1.18231 +/************** End of mutex_unix.c ******************************************/
 1.18232 +/************** Begin file mutex_w32.c ***************************************/
 1.18233 +/*
 1.18234 +** 2007 August 14
 1.18235 +**
 1.18236 +** The author disclaims copyright to this source code.  In place of
 1.18237 +** a legal notice, here is a blessing:
 1.18238 +**
 1.18239 +**    May you do good and not evil.
 1.18240 +**    May you find forgiveness for yourself and forgive others.
 1.18241 +**    May you share freely, never taking more than you give.
 1.18242 +**
 1.18243 +*************************************************************************
 1.18244 +** This file contains the C functions that implement mutexes for win32
 1.18245 +*/
 1.18246 +
 1.18247 +/*
 1.18248 +** The code in this file is only used if we are compiling multithreaded
 1.18249 +** on a win32 system.
 1.18250 +*/
 1.18251 +#ifdef SQLITE_MUTEX_W32
 1.18252 +
 1.18253 +/*
 1.18254 +** Each recursive mutex is an instance of the following structure.
 1.18255 +*/
 1.18256 +struct sqlite3_mutex {
 1.18257 +  CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
 1.18258 +  int id;                    /* Mutex type */
 1.18259 +#ifdef SQLITE_DEBUG
 1.18260 +  volatile int nRef;         /* Number of enterances */
 1.18261 +  volatile DWORD owner;      /* Thread holding this mutex */
 1.18262 +  int trace;                 /* True to trace changes */
 1.18263 +#endif
 1.18264 +};
 1.18265 +#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 1.18266 +#ifdef SQLITE_DEBUG
 1.18267 +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
 1.18268 +#else
 1.18269 +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
 1.18270 +#endif
 1.18271 +
 1.18272 +/*
 1.18273 +** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
 1.18274 +** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
 1.18275 +**
 1.18276 +** Here is an interesting observation:  Win95, Win98, and WinME lack
 1.18277 +** the LockFileEx() API.  But we can still statically link against that
 1.18278 +** API as long as we don't call it win running Win95/98/ME.  A call to
 1.18279 +** this routine is used to determine if the host is Win95/98/ME or
 1.18280 +** WinNT/2K/XP so that we will know whether or not we can safely call
 1.18281 +** the LockFileEx() API.
 1.18282 +**
 1.18283 +** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
 1.18284 +** which is only available if your application was compiled with 
 1.18285 +** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
 1.18286 +** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
 1.18287 +** this out as well.
 1.18288 +*/
 1.18289 +#if 0
 1.18290 +#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 1.18291 +# define mutexIsNT()  (1)
 1.18292 +#else
 1.18293 +  static int mutexIsNT(void){
 1.18294 +    static int osType = 0;
 1.18295 +    if( osType==0 ){
 1.18296 +      OSVERSIONINFO sInfo;
 1.18297 +      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
 1.18298 +      GetVersionEx(&sInfo);
 1.18299 +      osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
 1.18300 +    }
 1.18301 +    return osType==2;
 1.18302 +  }
 1.18303 +#endif /* SQLITE_OS_WINCE */
 1.18304 +#endif
 1.18305 +
 1.18306 +#ifdef SQLITE_DEBUG
 1.18307 +/*
 1.18308 +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 1.18309 +** intended for use only inside assert() statements.
 1.18310 +*/
 1.18311 +static int winMutexHeld(sqlite3_mutex *p){
 1.18312 +  return p->nRef!=0 && p->owner==GetCurrentThreadId();
 1.18313 +}
 1.18314 +static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
 1.18315 +  return p->nRef==0 || p->owner!=tid;
 1.18316 +}
 1.18317 +static int winMutexNotheld(sqlite3_mutex *p){
 1.18318 +  DWORD tid = GetCurrentThreadId(); 
 1.18319 +  return winMutexNotheld2(p, tid);
 1.18320 +}
 1.18321 +#endif
 1.18322 +
 1.18323 +
 1.18324 +/*
 1.18325 +** Initialize and deinitialize the mutex subsystem.
 1.18326 +*/
 1.18327 +static sqlite3_mutex winMutex_staticMutexes[6] = {
 1.18328 +  SQLITE3_MUTEX_INITIALIZER,
 1.18329 +  SQLITE3_MUTEX_INITIALIZER,
 1.18330 +  SQLITE3_MUTEX_INITIALIZER,
 1.18331 +  SQLITE3_MUTEX_INITIALIZER,
 1.18332 +  SQLITE3_MUTEX_INITIALIZER,
 1.18333 +  SQLITE3_MUTEX_INITIALIZER
 1.18334 +};
 1.18335 +static int winMutex_isInit = 0;
 1.18336 +/* As winMutexInit() and winMutexEnd() are called as part
 1.18337 +** of the sqlite3_initialize and sqlite3_shutdown()
 1.18338 +** processing, the "interlocked" magic is probably not
 1.18339 +** strictly necessary.
 1.18340 +*/
 1.18341 +static long winMutex_lock = 0;
 1.18342 +
 1.18343 +SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
 1.18344 +
 1.18345 +static int winMutexInit(void){ 
 1.18346 +  /* The first to increment to 1 does actual initialization */
 1.18347 +  if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
 1.18348 +    int i;
 1.18349 +    for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
 1.18350 +#if SQLITE_OS_WINRT
 1.18351 +      InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
 1.18352 +#else
 1.18353 +      InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
 1.18354 +#endif
 1.18355 +    }
 1.18356 +    winMutex_isInit = 1;
 1.18357 +  }else{
 1.18358 +    /* Someone else is in the process of initing the static mutexes */
 1.18359 +    while( !winMutex_isInit ){
 1.18360 +      sqlite3_win32_sleep(1);
 1.18361 +    }
 1.18362 +  }
 1.18363 +  return SQLITE_OK; 
 1.18364 +}
 1.18365 +
 1.18366 +static int winMutexEnd(void){ 
 1.18367 +  /* The first to decrement to 0 does actual shutdown 
 1.18368 +  ** (which should be the last to shutdown.) */
 1.18369 +  if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
 1.18370 +    if( winMutex_isInit==1 ){
 1.18371 +      int i;
 1.18372 +      for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
 1.18373 +        DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
 1.18374 +      }
 1.18375 +      winMutex_isInit = 0;
 1.18376 +    }
 1.18377 +  }
 1.18378 +  return SQLITE_OK; 
 1.18379 +}
 1.18380 +
 1.18381 +/*
 1.18382 +** The sqlite3_mutex_alloc() routine allocates a new
 1.18383 +** mutex and returns a pointer to it.  If it returns NULL
 1.18384 +** that means that a mutex could not be allocated.  SQLite
 1.18385 +** will unwind its stack and return an error.  The argument
 1.18386 +** to sqlite3_mutex_alloc() is one of these integer constants:
 1.18387 +**
 1.18388 +** <ul>
 1.18389 +** <li>  SQLITE_MUTEX_FAST
 1.18390 +** <li>  SQLITE_MUTEX_RECURSIVE
 1.18391 +** <li>  SQLITE_MUTEX_STATIC_MASTER
 1.18392 +** <li>  SQLITE_MUTEX_STATIC_MEM
 1.18393 +** <li>  SQLITE_MUTEX_STATIC_MEM2
 1.18394 +** <li>  SQLITE_MUTEX_STATIC_PRNG
 1.18395 +** <li>  SQLITE_MUTEX_STATIC_LRU
 1.18396 +** <li>  SQLITE_MUTEX_STATIC_PMEM
 1.18397 +** </ul>
 1.18398 +**
 1.18399 +** The first two constants cause sqlite3_mutex_alloc() to create
 1.18400 +** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 1.18401 +** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 1.18402 +** The mutex implementation does not need to make a distinction
 1.18403 +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 1.18404 +** not want to.  But SQLite will only request a recursive mutex in
 1.18405 +** cases where it really needs one.  If a faster non-recursive mutex
 1.18406 +** implementation is available on the host platform, the mutex subsystem
 1.18407 +** might return such a mutex in response to SQLITE_MUTEX_FAST.
 1.18408 +**
 1.18409 +** The other allowed parameters to sqlite3_mutex_alloc() each return
 1.18410 +** a pointer to a static preexisting mutex.  Six static mutexes are
 1.18411 +** used by the current version of SQLite.  Future versions of SQLite
 1.18412 +** may add additional static mutexes.  Static mutexes are for internal
 1.18413 +** use by SQLite only.  Applications that use SQLite mutexes should
 1.18414 +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 1.18415 +** SQLITE_MUTEX_RECURSIVE.
 1.18416 +**
 1.18417 +** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
 1.18418 +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 1.18419 +** returns a different mutex on every call.  But for the static 
 1.18420 +** mutex types, the same mutex is returned on every call that has
 1.18421 +** the same type number.
 1.18422 +*/
 1.18423 +static sqlite3_mutex *winMutexAlloc(int iType){
 1.18424 +  sqlite3_mutex *p;
 1.18425 +
 1.18426 +  switch( iType ){
 1.18427 +    case SQLITE_MUTEX_FAST:
 1.18428 +    case SQLITE_MUTEX_RECURSIVE: {
 1.18429 +      p = sqlite3MallocZero( sizeof(*p) );
 1.18430 +      if( p ){  
 1.18431 +#ifdef SQLITE_DEBUG
 1.18432 +        p->id = iType;
 1.18433 +#endif
 1.18434 +#if SQLITE_OS_WINRT
 1.18435 +        InitializeCriticalSectionEx(&p->mutex, 0, 0);
 1.18436 +#else
 1.18437 +        InitializeCriticalSection(&p->mutex);
 1.18438 +#endif
 1.18439 +      }
 1.18440 +      break;
 1.18441 +    }
 1.18442 +    default: {
 1.18443 +      assert( winMutex_isInit==1 );
 1.18444 +      assert( iType-2 >= 0 );
 1.18445 +      assert( iType-2 < ArraySize(winMutex_staticMutexes) );
 1.18446 +      p = &winMutex_staticMutexes[iType-2];
 1.18447 +#ifdef SQLITE_DEBUG
 1.18448 +      p->id = iType;
 1.18449 +#endif
 1.18450 +      break;
 1.18451 +    }
 1.18452 +  }
 1.18453 +  return p;
 1.18454 +}
 1.18455 +
 1.18456 +
 1.18457 +/*
 1.18458 +** This routine deallocates a previously
 1.18459 +** allocated mutex.  SQLite is careful to deallocate every
 1.18460 +** mutex that it allocates.
 1.18461 +*/
 1.18462 +static void winMutexFree(sqlite3_mutex *p){
 1.18463 +  assert( p );
 1.18464 +  assert( p->nRef==0 && p->owner==0 );
 1.18465 +  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 1.18466 +  DeleteCriticalSection(&p->mutex);
 1.18467 +  sqlite3_free(p);
 1.18468 +}
 1.18469 +
 1.18470 +/*
 1.18471 +** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 1.18472 +** to enter a mutex.  If another thread is already within the mutex,
 1.18473 +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 1.18474 +** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 1.18475 +** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 1.18476 +** be entered multiple times by the same thread.  In such cases the,
 1.18477 +** mutex must be exited an equal number of times before another thread
 1.18478 +** can enter.  If the same thread tries to enter any other kind of mutex
 1.18479 +** more than once, the behavior is undefined.
 1.18480 +*/
 1.18481 +static void winMutexEnter(sqlite3_mutex *p){
 1.18482 +#ifdef SQLITE_DEBUG
 1.18483 +  DWORD tid = GetCurrentThreadId(); 
 1.18484 +  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 1.18485 +#endif
 1.18486 +  EnterCriticalSection(&p->mutex);
 1.18487 +#ifdef SQLITE_DEBUG
 1.18488 +  assert( p->nRef>0 || p->owner==0 );
 1.18489 +  p->owner = tid; 
 1.18490 +  p->nRef++;
 1.18491 +  if( p->trace ){
 1.18492 +    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 1.18493 +  }
 1.18494 +#endif
 1.18495 +}
 1.18496 +static int winMutexTry(sqlite3_mutex *p){
 1.18497 +#ifndef NDEBUG
 1.18498 +  DWORD tid = GetCurrentThreadId(); 
 1.18499 +#endif
 1.18500 +  int rc = SQLITE_BUSY;
 1.18501 +  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 1.18502 +  /*
 1.18503 +  ** The sqlite3_mutex_try() routine is very rarely used, and when it
 1.18504 +  ** is used it is merely an optimization.  So it is OK for it to always
 1.18505 +  ** fail.  
 1.18506 +  **
 1.18507 +  ** The TryEnterCriticalSection() interface is only available on WinNT.
 1.18508 +  ** And some windows compilers complain if you try to use it without
 1.18509 +  ** first doing some #defines that prevent SQLite from building on Win98.
 1.18510 +  ** For that reason, we will omit this optimization for now.  See
 1.18511 +  ** ticket #2685.
 1.18512 +  */
 1.18513 +#if 0
 1.18514 +  if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
 1.18515 +    p->owner = tid;
 1.18516 +    p->nRef++;
 1.18517 +    rc = SQLITE_OK;
 1.18518 +  }
 1.18519 +#else
 1.18520 +  UNUSED_PARAMETER(p);
 1.18521 +#endif
 1.18522 +#ifdef SQLITE_DEBUG
 1.18523 +  if( rc==SQLITE_OK && p->trace ){
 1.18524 +    printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 1.18525 +  }
 1.18526 +#endif
 1.18527 +  return rc;
 1.18528 +}
 1.18529 +
 1.18530 +/*
 1.18531 +** The sqlite3_mutex_leave() routine exits a mutex that was
 1.18532 +** previously entered by the same thread.  The behavior
 1.18533 +** is undefined if the mutex is not currently entered or
 1.18534 +** is not currently allocated.  SQLite will never do either.
 1.18535 +*/
 1.18536 +static void winMutexLeave(sqlite3_mutex *p){
 1.18537 +#ifndef NDEBUG
 1.18538 +  DWORD tid = GetCurrentThreadId();
 1.18539 +  assert( p->nRef>0 );
 1.18540 +  assert( p->owner==tid );
 1.18541 +  p->nRef--;
 1.18542 +  if( p->nRef==0 ) p->owner = 0;
 1.18543 +  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 1.18544 +#endif
 1.18545 +  LeaveCriticalSection(&p->mutex);
 1.18546 +#ifdef SQLITE_DEBUG
 1.18547 +  if( p->trace ){
 1.18548 +    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 1.18549 +  }
 1.18550 +#endif
 1.18551 +}
 1.18552 +
 1.18553 +SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 1.18554 +  static const sqlite3_mutex_methods sMutex = {
 1.18555 +    winMutexInit,
 1.18556 +    winMutexEnd,
 1.18557 +    winMutexAlloc,
 1.18558 +    winMutexFree,
 1.18559 +    winMutexEnter,
 1.18560 +    winMutexTry,
 1.18561 +    winMutexLeave,
 1.18562 +#ifdef SQLITE_DEBUG
 1.18563 +    winMutexHeld,
 1.18564 +    winMutexNotheld
 1.18565 +#else
 1.18566 +    0,
 1.18567 +    0
 1.18568 +#endif
 1.18569 +  };
 1.18570 +
 1.18571 +  return &sMutex;
 1.18572 +}
 1.18573 +#endif /* SQLITE_MUTEX_W32 */
 1.18574 +
 1.18575 +/************** End of mutex_w32.c *******************************************/
 1.18576 +/************** Begin file malloc.c ******************************************/
 1.18577 +/*
 1.18578 +** 2001 September 15
 1.18579 +**
 1.18580 +** The author disclaims copyright to this source code.  In place of
 1.18581 +** a legal notice, here is a blessing:
 1.18582 +**
 1.18583 +**    May you do good and not evil.
 1.18584 +**    May you find forgiveness for yourself and forgive others.
 1.18585 +**    May you share freely, never taking more than you give.
 1.18586 +**
 1.18587 +*************************************************************************
 1.18588 +**
 1.18589 +** Memory allocation functions used throughout sqlite.
 1.18590 +*/
 1.18591 +/* #include <stdarg.h> */
 1.18592 +
 1.18593 +/*
 1.18594 +** Attempt to release up to n bytes of non-essential memory currently
 1.18595 +** held by SQLite. An example of non-essential memory is memory used to
 1.18596 +** cache database pages that are not currently in use.
 1.18597 +*/
 1.18598 +SQLITE_API int sqlite3_release_memory(int n){
 1.18599 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 1.18600 +  return sqlite3PcacheReleaseMemory(n);
 1.18601 +#else
 1.18602 +  /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
 1.18603 +  ** is a no-op returning zero if SQLite is not compiled with
 1.18604 +  ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
 1.18605 +  UNUSED_PARAMETER(n);
 1.18606 +  return 0;
 1.18607 +#endif
 1.18608 +}
 1.18609 +
 1.18610 +/*
 1.18611 +** An instance of the following object records the location of
 1.18612 +** each unused scratch buffer.
 1.18613 +*/
 1.18614 +typedef struct ScratchFreeslot {
 1.18615 +  struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
 1.18616 +} ScratchFreeslot;
 1.18617 +
 1.18618 +/*
 1.18619 +** State information local to the memory allocation subsystem.
 1.18620 +*/
 1.18621 +static SQLITE_WSD struct Mem0Global {
 1.18622 +  sqlite3_mutex *mutex;         /* Mutex to serialize access */
 1.18623 +
 1.18624 +  /*
 1.18625 +  ** The alarm callback and its arguments.  The mem0.mutex lock will
 1.18626 +  ** be held while the callback is running.  Recursive calls into
 1.18627 +  ** the memory subsystem are allowed, but no new callbacks will be
 1.18628 +  ** issued.
 1.18629 +  */
 1.18630 +  sqlite3_int64 alarmThreshold;
 1.18631 +  void (*alarmCallback)(void*, sqlite3_int64,int);
 1.18632 +  void *alarmArg;
 1.18633 +
 1.18634 +  /*
 1.18635 +  ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
 1.18636 +  ** (so that a range test can be used to determine if an allocation
 1.18637 +  ** being freed came from pScratch) and a pointer to the list of
 1.18638 +  ** unused scratch allocations.
 1.18639 +  */
 1.18640 +  void *pScratchEnd;
 1.18641 +  ScratchFreeslot *pScratchFree;
 1.18642 +  u32 nScratchFree;
 1.18643 +
 1.18644 +  /*
 1.18645 +  ** True if heap is nearly "full" where "full" is defined by the
 1.18646 +  ** sqlite3_soft_heap_limit() setting.
 1.18647 +  */
 1.18648 +  int nearlyFull;
 1.18649 +} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
 1.18650 +
 1.18651 +#define mem0 GLOBAL(struct Mem0Global, mem0)
 1.18652 +
 1.18653 +/*
 1.18654 +** This routine runs when the memory allocator sees that the
 1.18655 +** total memory allocation is about to exceed the soft heap
 1.18656 +** limit.
 1.18657 +*/
 1.18658 +static void softHeapLimitEnforcer(
 1.18659 +  void *NotUsed, 
 1.18660 +  sqlite3_int64 NotUsed2,
 1.18661 +  int allocSize
 1.18662 +){
 1.18663 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.18664 +  sqlite3_release_memory(allocSize);
 1.18665 +}
 1.18666 +
 1.18667 +/*
 1.18668 +** Change the alarm callback
 1.18669 +*/
 1.18670 +static int sqlite3MemoryAlarm(
 1.18671 +  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
 1.18672 +  void *pArg,
 1.18673 +  sqlite3_int64 iThreshold
 1.18674 +){
 1.18675 +  int nUsed;
 1.18676 +  sqlite3_mutex_enter(mem0.mutex);
 1.18677 +  mem0.alarmCallback = xCallback;
 1.18678 +  mem0.alarmArg = pArg;
 1.18679 +  mem0.alarmThreshold = iThreshold;
 1.18680 +  nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 1.18681 +  mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
 1.18682 +  sqlite3_mutex_leave(mem0.mutex);
 1.18683 +  return SQLITE_OK;
 1.18684 +}
 1.18685 +
 1.18686 +#ifndef SQLITE_OMIT_DEPRECATED
 1.18687 +/*
 1.18688 +** Deprecated external interface.  Internal/core SQLite code
 1.18689 +** should call sqlite3MemoryAlarm.
 1.18690 +*/
 1.18691 +SQLITE_API int sqlite3_memory_alarm(
 1.18692 +  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
 1.18693 +  void *pArg,
 1.18694 +  sqlite3_int64 iThreshold
 1.18695 +){
 1.18696 +  return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
 1.18697 +}
 1.18698 +#endif
 1.18699 +
 1.18700 +/*
 1.18701 +** Set the soft heap-size limit for the library. Passing a zero or 
 1.18702 +** negative value indicates no limit.
 1.18703 +*/
 1.18704 +SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
 1.18705 +  sqlite3_int64 priorLimit;
 1.18706 +  sqlite3_int64 excess;
 1.18707 +#ifndef SQLITE_OMIT_AUTOINIT
 1.18708 +  int rc = sqlite3_initialize();
 1.18709 +  if( rc ) return -1;
 1.18710 +#endif
 1.18711 +  sqlite3_mutex_enter(mem0.mutex);
 1.18712 +  priorLimit = mem0.alarmThreshold;
 1.18713 +  sqlite3_mutex_leave(mem0.mutex);
 1.18714 +  if( n<0 ) return priorLimit;
 1.18715 +  if( n>0 ){
 1.18716 +    sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
 1.18717 +  }else{
 1.18718 +    sqlite3MemoryAlarm(0, 0, 0);
 1.18719 +  }
 1.18720 +  excess = sqlite3_memory_used() - n;
 1.18721 +  if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
 1.18722 +  return priorLimit;
 1.18723 +}
 1.18724 +SQLITE_API void sqlite3_soft_heap_limit(int n){
 1.18725 +  if( n<0 ) n = 0;
 1.18726 +  sqlite3_soft_heap_limit64(n);
 1.18727 +}
 1.18728 +
 1.18729 +/*
 1.18730 +** Initialize the memory allocation subsystem.
 1.18731 +*/
 1.18732 +SQLITE_PRIVATE int sqlite3MallocInit(void){
 1.18733 +  if( sqlite3GlobalConfig.m.xMalloc==0 ){
 1.18734 +    sqlite3MemSetDefault();
 1.18735 +  }
 1.18736 +  memset(&mem0, 0, sizeof(mem0));
 1.18737 +  if( sqlite3GlobalConfig.bCoreMutex ){
 1.18738 +    mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 1.18739 +  }
 1.18740 +  if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
 1.18741 +      && sqlite3GlobalConfig.nScratch>0 ){
 1.18742 +    int i, n, sz;
 1.18743 +    ScratchFreeslot *pSlot;
 1.18744 +    sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
 1.18745 +    sqlite3GlobalConfig.szScratch = sz;
 1.18746 +    pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
 1.18747 +    n = sqlite3GlobalConfig.nScratch;
 1.18748 +    mem0.pScratchFree = pSlot;
 1.18749 +    mem0.nScratchFree = n;
 1.18750 +    for(i=0; i<n-1; i++){
 1.18751 +      pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
 1.18752 +      pSlot = pSlot->pNext;
 1.18753 +    }
 1.18754 +    pSlot->pNext = 0;
 1.18755 +    mem0.pScratchEnd = (void*)&pSlot[1];
 1.18756 +  }else{
 1.18757 +    mem0.pScratchEnd = 0;
 1.18758 +    sqlite3GlobalConfig.pScratch = 0;
 1.18759 +    sqlite3GlobalConfig.szScratch = 0;
 1.18760 +    sqlite3GlobalConfig.nScratch = 0;
 1.18761 +  }
 1.18762 +  if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
 1.18763 +      || sqlite3GlobalConfig.nPage<1 ){
 1.18764 +    sqlite3GlobalConfig.pPage = 0;
 1.18765 +    sqlite3GlobalConfig.szPage = 0;
 1.18766 +    sqlite3GlobalConfig.nPage = 0;
 1.18767 +  }
 1.18768 +  return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
 1.18769 +}
 1.18770 +
 1.18771 +/*
 1.18772 +** Return true if the heap is currently under memory pressure - in other
 1.18773 +** words if the amount of heap used is close to the limit set by
 1.18774 +** sqlite3_soft_heap_limit().
 1.18775 +*/
 1.18776 +SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
 1.18777 +  return mem0.nearlyFull;
 1.18778 +}
 1.18779 +
 1.18780 +/*
 1.18781 +** Deinitialize the memory allocation subsystem.
 1.18782 +*/
 1.18783 +SQLITE_PRIVATE void sqlite3MallocEnd(void){
 1.18784 +  if( sqlite3GlobalConfig.m.xShutdown ){
 1.18785 +    sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
 1.18786 +  }
 1.18787 +  memset(&mem0, 0, sizeof(mem0));
 1.18788 +}
 1.18789 +
 1.18790 +/*
 1.18791 +** Return the amount of memory currently checked out.
 1.18792 +*/
 1.18793 +SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
 1.18794 +  int n, mx;
 1.18795 +  sqlite3_int64 res;
 1.18796 +  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
 1.18797 +  res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
 1.18798 +  return res;
 1.18799 +}
 1.18800 +
 1.18801 +/*
 1.18802 +** Return the maximum amount of memory that has ever been
 1.18803 +** checked out since either the beginning of this process
 1.18804 +** or since the most recent reset.
 1.18805 +*/
 1.18806 +SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
 1.18807 +  int n, mx;
 1.18808 +  sqlite3_int64 res;
 1.18809 +  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
 1.18810 +  res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
 1.18811 +  return res;
 1.18812 +}
 1.18813 +
 1.18814 +/*
 1.18815 +** Trigger the alarm 
 1.18816 +*/
 1.18817 +static void sqlite3MallocAlarm(int nByte){
 1.18818 +  void (*xCallback)(void*,sqlite3_int64,int);
 1.18819 +  sqlite3_int64 nowUsed;
 1.18820 +  void *pArg;
 1.18821 +  if( mem0.alarmCallback==0 ) return;
 1.18822 +  xCallback = mem0.alarmCallback;
 1.18823 +  nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 1.18824 +  pArg = mem0.alarmArg;
 1.18825 +  mem0.alarmCallback = 0;
 1.18826 +  sqlite3_mutex_leave(mem0.mutex);
 1.18827 +  xCallback(pArg, nowUsed, nByte);
 1.18828 +  sqlite3_mutex_enter(mem0.mutex);
 1.18829 +  mem0.alarmCallback = xCallback;
 1.18830 +  mem0.alarmArg = pArg;
 1.18831 +}
 1.18832 +
 1.18833 +/*
 1.18834 +** Do a memory allocation with statistics and alarms.  Assume the
 1.18835 +** lock is already held.
 1.18836 +*/
 1.18837 +static int mallocWithAlarm(int n, void **pp){
 1.18838 +  int nFull;
 1.18839 +  void *p;
 1.18840 +  assert( sqlite3_mutex_held(mem0.mutex) );
 1.18841 +  nFull = sqlite3GlobalConfig.m.xRoundup(n);
 1.18842 +  sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
 1.18843 +  if( mem0.alarmCallback!=0 ){
 1.18844 +    int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 1.18845 +    if( nUsed >= mem0.alarmThreshold - nFull ){
 1.18846 +      mem0.nearlyFull = 1;
 1.18847 +      sqlite3MallocAlarm(nFull);
 1.18848 +    }else{
 1.18849 +      mem0.nearlyFull = 0;
 1.18850 +    }
 1.18851 +  }
 1.18852 +  p = sqlite3GlobalConfig.m.xMalloc(nFull);
 1.18853 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 1.18854 +  if( p==0 && mem0.alarmCallback ){
 1.18855 +    sqlite3MallocAlarm(nFull);
 1.18856 +    p = sqlite3GlobalConfig.m.xMalloc(nFull);
 1.18857 +  }
 1.18858 +#endif
 1.18859 +  if( p ){
 1.18860 +    nFull = sqlite3MallocSize(p);
 1.18861 +    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
 1.18862 +    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
 1.18863 +  }
 1.18864 +  *pp = p;
 1.18865 +  return nFull;
 1.18866 +}
 1.18867 +
 1.18868 +/*
 1.18869 +** Allocate memory.  This routine is like sqlite3_malloc() except that it
 1.18870 +** assumes the memory subsystem has already been initialized.
 1.18871 +*/
 1.18872 +SQLITE_PRIVATE void *sqlite3Malloc(int n){
 1.18873 +  void *p;
 1.18874 +  if( n<=0               /* IMP: R-65312-04917 */ 
 1.18875 +   || n>=0x7fffff00
 1.18876 +  ){
 1.18877 +    /* A memory allocation of a number of bytes which is near the maximum
 1.18878 +    ** signed integer value might cause an integer overflow inside of the
 1.18879 +    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
 1.18880 +    ** 255 bytes of overhead.  SQLite itself will never use anything near
 1.18881 +    ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
 1.18882 +    p = 0;
 1.18883 +  }else if( sqlite3GlobalConfig.bMemstat ){
 1.18884 +    sqlite3_mutex_enter(mem0.mutex);
 1.18885 +    mallocWithAlarm(n, &p);
 1.18886 +    sqlite3_mutex_leave(mem0.mutex);
 1.18887 +  }else{
 1.18888 +    p = sqlite3GlobalConfig.m.xMalloc(n);
 1.18889 +  }
 1.18890 +  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
 1.18891 +  return p;
 1.18892 +}
 1.18893 +
 1.18894 +/*
 1.18895 +** This version of the memory allocation is for use by the application.
 1.18896 +** First make sure the memory subsystem is initialized, then do the
 1.18897 +** allocation.
 1.18898 +*/
 1.18899 +SQLITE_API void *sqlite3_malloc(int n){
 1.18900 +#ifndef SQLITE_OMIT_AUTOINIT
 1.18901 +  if( sqlite3_initialize() ) return 0;
 1.18902 +#endif
 1.18903 +  return sqlite3Malloc(n);
 1.18904 +}
 1.18905 +
 1.18906 +/*
 1.18907 +** Each thread may only have a single outstanding allocation from
 1.18908 +** xScratchMalloc().  We verify this constraint in the single-threaded
 1.18909 +** case by setting scratchAllocOut to 1 when an allocation
 1.18910 +** is outstanding clearing it when the allocation is freed.
 1.18911 +*/
 1.18912 +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 1.18913 +static int scratchAllocOut = 0;
 1.18914 +#endif
 1.18915 +
 1.18916 +
 1.18917 +/*
 1.18918 +** Allocate memory that is to be used and released right away.
 1.18919 +** This routine is similar to alloca() in that it is not intended
 1.18920 +** for situations where the memory might be held long-term.  This
 1.18921 +** routine is intended to get memory to old large transient data
 1.18922 +** structures that would not normally fit on the stack of an
 1.18923 +** embedded processor.
 1.18924 +*/
 1.18925 +SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
 1.18926 +  void *p;
 1.18927 +  assert( n>0 );
 1.18928 +
 1.18929 +  sqlite3_mutex_enter(mem0.mutex);
 1.18930 +  if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
 1.18931 +    p = mem0.pScratchFree;
 1.18932 +    mem0.pScratchFree = mem0.pScratchFree->pNext;
 1.18933 +    mem0.nScratchFree--;
 1.18934 +    sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
 1.18935 +    sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
 1.18936 +    sqlite3_mutex_leave(mem0.mutex);
 1.18937 +  }else{
 1.18938 +    if( sqlite3GlobalConfig.bMemstat ){
 1.18939 +      sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
 1.18940 +      n = mallocWithAlarm(n, &p);
 1.18941 +      if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
 1.18942 +      sqlite3_mutex_leave(mem0.mutex);
 1.18943 +    }else{
 1.18944 +      sqlite3_mutex_leave(mem0.mutex);
 1.18945 +      p = sqlite3GlobalConfig.m.xMalloc(n);
 1.18946 +    }
 1.18947 +    sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
 1.18948 +  }
 1.18949 +  assert( sqlite3_mutex_notheld(mem0.mutex) );
 1.18950 +
 1.18951 +
 1.18952 +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 1.18953 +  /* Verify that no more than two scratch allocations per thread
 1.18954 +  ** are outstanding at one time.  (This is only checked in the
 1.18955 +  ** single-threaded case since checking in the multi-threaded case
 1.18956 +  ** would be much more complicated.) */
 1.18957 +  assert( scratchAllocOut<=1 );
 1.18958 +  if( p ) scratchAllocOut++;
 1.18959 +#endif
 1.18960 +
 1.18961 +  return p;
 1.18962 +}
 1.18963 +SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
 1.18964 +  if( p ){
 1.18965 +
 1.18966 +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 1.18967 +    /* Verify that no more than two scratch allocation per thread
 1.18968 +    ** is outstanding at one time.  (This is only checked in the
 1.18969 +    ** single-threaded case since checking in the multi-threaded case
 1.18970 +    ** would be much more complicated.) */
 1.18971 +    assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
 1.18972 +    scratchAllocOut--;
 1.18973 +#endif
 1.18974 +
 1.18975 +    if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
 1.18976 +      /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
 1.18977 +      ScratchFreeslot *pSlot;
 1.18978 +      pSlot = (ScratchFreeslot*)p;
 1.18979 +      sqlite3_mutex_enter(mem0.mutex);
 1.18980 +      pSlot->pNext = mem0.pScratchFree;
 1.18981 +      mem0.pScratchFree = pSlot;
 1.18982 +      mem0.nScratchFree++;
 1.18983 +      assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
 1.18984 +      sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
 1.18985 +      sqlite3_mutex_leave(mem0.mutex);
 1.18986 +    }else{
 1.18987 +      /* Release memory back to the heap */
 1.18988 +      assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
 1.18989 +      assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
 1.18990 +      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 1.18991 +      if( sqlite3GlobalConfig.bMemstat ){
 1.18992 +        int iSize = sqlite3MallocSize(p);
 1.18993 +        sqlite3_mutex_enter(mem0.mutex);
 1.18994 +        sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
 1.18995 +        sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
 1.18996 +        sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
 1.18997 +        sqlite3GlobalConfig.m.xFree(p);
 1.18998 +        sqlite3_mutex_leave(mem0.mutex);
 1.18999 +      }else{
 1.19000 +        sqlite3GlobalConfig.m.xFree(p);
 1.19001 +      }
 1.19002 +    }
 1.19003 +  }
 1.19004 +}
 1.19005 +
 1.19006 +/*
 1.19007 +** TRUE if p is a lookaside memory allocation from db
 1.19008 +*/
 1.19009 +#ifndef SQLITE_OMIT_LOOKASIDE
 1.19010 +static int isLookaside(sqlite3 *db, void *p){
 1.19011 +  return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
 1.19012 +}
 1.19013 +#else
 1.19014 +#define isLookaside(A,B) 0
 1.19015 +#endif
 1.19016 +
 1.19017 +/*
 1.19018 +** Return the size of a memory allocation previously obtained from
 1.19019 +** sqlite3Malloc() or sqlite3_malloc().
 1.19020 +*/
 1.19021 +SQLITE_PRIVATE int sqlite3MallocSize(void *p){
 1.19022 +  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 1.19023 +  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 1.19024 +  return sqlite3GlobalConfig.m.xSize(p);
 1.19025 +}
 1.19026 +SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
 1.19027 +  assert( db==0 || sqlite3_mutex_held(db->mutex) );
 1.19028 +  if( db && isLookaside(db, p) ){
 1.19029 +    return db->lookaside.sz;
 1.19030 +  }else{
 1.19031 +    assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 1.19032 +    assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 1.19033 +    assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
 1.19034 +    return sqlite3GlobalConfig.m.xSize(p);
 1.19035 +  }
 1.19036 +}
 1.19037 +
 1.19038 +/*
 1.19039 +** Free memory previously obtained from sqlite3Malloc().
 1.19040 +*/
 1.19041 +SQLITE_API void sqlite3_free(void *p){
 1.19042 +  if( p==0 ) return;  /* IMP: R-49053-54554 */
 1.19043 +  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 1.19044 +  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 1.19045 +  if( sqlite3GlobalConfig.bMemstat ){
 1.19046 +    sqlite3_mutex_enter(mem0.mutex);
 1.19047 +    sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
 1.19048 +    sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
 1.19049 +    sqlite3GlobalConfig.m.xFree(p);
 1.19050 +    sqlite3_mutex_leave(mem0.mutex);
 1.19051 +  }else{
 1.19052 +    sqlite3GlobalConfig.m.xFree(p);
 1.19053 +  }
 1.19054 +}
 1.19055 +
 1.19056 +/*
 1.19057 +** Free memory that might be associated with a particular database
 1.19058 +** connection.
 1.19059 +*/
 1.19060 +SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
 1.19061 +  assert( db==0 || sqlite3_mutex_held(db->mutex) );
 1.19062 +  if( db ){
 1.19063 +    if( db->pnBytesFreed ){
 1.19064 +      *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
 1.19065 +      return;
 1.19066 +    }
 1.19067 +    if( isLookaside(db, p) ){
 1.19068 +      LookasideSlot *pBuf = (LookasideSlot*)p;
 1.19069 +#if SQLITE_DEBUG
 1.19070 +      /* Trash all content in the buffer being freed */
 1.19071 +      memset(p, 0xaa, db->lookaside.sz);
 1.19072 +#endif
 1.19073 +      pBuf->pNext = db->lookaside.pFree;
 1.19074 +      db->lookaside.pFree = pBuf;
 1.19075 +      db->lookaside.nOut--;
 1.19076 +      return;
 1.19077 +    }
 1.19078 +  }
 1.19079 +  assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 1.19080 +  assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 1.19081 +  assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
 1.19082 +  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 1.19083 +  sqlite3_free(p);
 1.19084 +}
 1.19085 +
 1.19086 +/*
 1.19087 +** Change the size of an existing memory allocation
 1.19088 +*/
 1.19089 +SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
 1.19090 +  int nOld, nNew, nDiff;
 1.19091 +  void *pNew;
 1.19092 +  if( pOld==0 ){
 1.19093 +    return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
 1.19094 +  }
 1.19095 +  if( nBytes<=0 ){
 1.19096 +    sqlite3_free(pOld); /* IMP: R-31593-10574 */
 1.19097 +    return 0;
 1.19098 +  }
 1.19099 +  if( nBytes>=0x7fffff00 ){
 1.19100 +    /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
 1.19101 +    return 0;
 1.19102 +  }
 1.19103 +  nOld = sqlite3MallocSize(pOld);
 1.19104 +  /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
 1.19105 +  ** argument to xRealloc is always a value returned by a prior call to
 1.19106 +  ** xRoundup. */
 1.19107 +  nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
 1.19108 +  if( nOld==nNew ){
 1.19109 +    pNew = pOld;
 1.19110 +  }else if( sqlite3GlobalConfig.bMemstat ){
 1.19111 +    sqlite3_mutex_enter(mem0.mutex);
 1.19112 +    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
 1.19113 +    nDiff = nNew - nOld;
 1.19114 +    if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
 1.19115 +          mem0.alarmThreshold-nDiff ){
 1.19116 +      sqlite3MallocAlarm(nDiff);
 1.19117 +    }
 1.19118 +    assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
 1.19119 +    assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
 1.19120 +    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 1.19121 +    if( pNew==0 && mem0.alarmCallback ){
 1.19122 +      sqlite3MallocAlarm(nBytes);
 1.19123 +      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 1.19124 +    }
 1.19125 +    if( pNew ){
 1.19126 +      nNew = sqlite3MallocSize(pNew);
 1.19127 +      sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
 1.19128 +    }
 1.19129 +    sqlite3_mutex_leave(mem0.mutex);
 1.19130 +  }else{
 1.19131 +    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 1.19132 +  }
 1.19133 +  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
 1.19134 +  return pNew;
 1.19135 +}
 1.19136 +
 1.19137 +/*
 1.19138 +** The public interface to sqlite3Realloc.  Make sure that the memory
 1.19139 +** subsystem is initialized prior to invoking sqliteRealloc.
 1.19140 +*/
 1.19141 +SQLITE_API void *sqlite3_realloc(void *pOld, int n){
 1.19142 +#ifndef SQLITE_OMIT_AUTOINIT
 1.19143 +  if( sqlite3_initialize() ) return 0;
 1.19144 +#endif
 1.19145 +  return sqlite3Realloc(pOld, n);
 1.19146 +}
 1.19147 +
 1.19148 +
 1.19149 +/*
 1.19150 +** Allocate and zero memory.
 1.19151 +*/ 
 1.19152 +SQLITE_PRIVATE void *sqlite3MallocZero(int n){
 1.19153 +  void *p = sqlite3Malloc(n);
 1.19154 +  if( p ){
 1.19155 +    memset(p, 0, n);
 1.19156 +  }
 1.19157 +  return p;
 1.19158 +}
 1.19159 +
 1.19160 +/*
 1.19161 +** Allocate and zero memory.  If the allocation fails, make
 1.19162 +** the mallocFailed flag in the connection pointer.
 1.19163 +*/
 1.19164 +SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
 1.19165 +  void *p = sqlite3DbMallocRaw(db, n);
 1.19166 +  if( p ){
 1.19167 +    memset(p, 0, n);
 1.19168 +  }
 1.19169 +  return p;
 1.19170 +}
 1.19171 +
 1.19172 +/*
 1.19173 +** Allocate and zero memory.  If the allocation fails, make
 1.19174 +** the mallocFailed flag in the connection pointer.
 1.19175 +**
 1.19176 +** If db!=0 and db->mallocFailed is true (indicating a prior malloc
 1.19177 +** failure on the same database connection) then always return 0.
 1.19178 +** Hence for a particular database connection, once malloc starts
 1.19179 +** failing, it fails consistently until mallocFailed is reset.
 1.19180 +** This is an important assumption.  There are many places in the
 1.19181 +** code that do things like this:
 1.19182 +**
 1.19183 +**         int *a = (int*)sqlite3DbMallocRaw(db, 100);
 1.19184 +**         int *b = (int*)sqlite3DbMallocRaw(db, 200);
 1.19185 +**         if( b ) a[10] = 9;
 1.19186 +**
 1.19187 +** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
 1.19188 +** that all prior mallocs (ex: "a") worked too.
 1.19189 +*/
 1.19190 +SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
 1.19191 +  void *p;
 1.19192 +  assert( db==0 || sqlite3_mutex_held(db->mutex) );
 1.19193 +  assert( db==0 || db->pnBytesFreed==0 );
 1.19194 +#ifndef SQLITE_OMIT_LOOKASIDE
 1.19195 +  if( db ){
 1.19196 +    LookasideSlot *pBuf;
 1.19197 +    if( db->mallocFailed ){
 1.19198 +      return 0;
 1.19199 +    }
 1.19200 +    if( db->lookaside.bEnabled ){
 1.19201 +      if( n>db->lookaside.sz ){
 1.19202 +        db->lookaside.anStat[1]++;
 1.19203 +      }else if( (pBuf = db->lookaside.pFree)==0 ){
 1.19204 +        db->lookaside.anStat[2]++;
 1.19205 +      }else{
 1.19206 +        db->lookaside.pFree = pBuf->pNext;
 1.19207 +        db->lookaside.nOut++;
 1.19208 +        db->lookaside.anStat[0]++;
 1.19209 +        if( db->lookaside.nOut>db->lookaside.mxOut ){
 1.19210 +          db->lookaside.mxOut = db->lookaside.nOut;
 1.19211 +        }
 1.19212 +        return (void*)pBuf;
 1.19213 +      }
 1.19214 +    }
 1.19215 +  }
 1.19216 +#else
 1.19217 +  if( db && db->mallocFailed ){
 1.19218 +    return 0;
 1.19219 +  }
 1.19220 +#endif
 1.19221 +  p = sqlite3Malloc(n);
 1.19222 +  if( !p && db ){
 1.19223 +    db->mallocFailed = 1;
 1.19224 +  }
 1.19225 +  sqlite3MemdebugSetType(p, MEMTYPE_DB |
 1.19226 +         ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
 1.19227 +  return p;
 1.19228 +}
 1.19229 +
 1.19230 +/*
 1.19231 +** Resize the block of memory pointed to by p to n bytes. If the
 1.19232 +** resize fails, set the mallocFailed flag in the connection object.
 1.19233 +*/
 1.19234 +SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
 1.19235 +  void *pNew = 0;
 1.19236 +  assert( db!=0 );
 1.19237 +  assert( sqlite3_mutex_held(db->mutex) );
 1.19238 +  if( db->mallocFailed==0 ){
 1.19239 +    if( p==0 ){
 1.19240 +      return sqlite3DbMallocRaw(db, n);
 1.19241 +    }
 1.19242 +    if( isLookaside(db, p) ){
 1.19243 +      if( n<=db->lookaside.sz ){
 1.19244 +        return p;
 1.19245 +      }
 1.19246 +      pNew = sqlite3DbMallocRaw(db, n);
 1.19247 +      if( pNew ){
 1.19248 +        memcpy(pNew, p, db->lookaside.sz);
 1.19249 +        sqlite3DbFree(db, p);
 1.19250 +      }
 1.19251 +    }else{
 1.19252 +      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 1.19253 +      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 1.19254 +      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 1.19255 +      pNew = sqlite3_realloc(p, n);
 1.19256 +      if( !pNew ){
 1.19257 +        sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
 1.19258 +        db->mallocFailed = 1;
 1.19259 +      }
 1.19260 +      sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
 1.19261 +            (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
 1.19262 +    }
 1.19263 +  }
 1.19264 +  return pNew;
 1.19265 +}
 1.19266 +
 1.19267 +/*
 1.19268 +** Attempt to reallocate p.  If the reallocation fails, then free p
 1.19269 +** and set the mallocFailed flag in the database connection.
 1.19270 +*/
 1.19271 +SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
 1.19272 +  void *pNew;
 1.19273 +  pNew = sqlite3DbRealloc(db, p, n);
 1.19274 +  if( !pNew ){
 1.19275 +    sqlite3DbFree(db, p);
 1.19276 +  }
 1.19277 +  return pNew;
 1.19278 +}
 1.19279 +
 1.19280 +/*
 1.19281 +** Make a copy of a string in memory obtained from sqliteMalloc(). These 
 1.19282 +** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
 1.19283 +** is because when memory debugging is turned on, these two functions are 
 1.19284 +** called via macros that record the current file and line number in the
 1.19285 +** ThreadData structure.
 1.19286 +*/
 1.19287 +SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
 1.19288 +  char *zNew;
 1.19289 +  size_t n;
 1.19290 +  if( z==0 ){
 1.19291 +    return 0;
 1.19292 +  }
 1.19293 +  n = sqlite3Strlen30(z) + 1;
 1.19294 +  assert( (n&0x7fffffff)==n );
 1.19295 +  zNew = sqlite3DbMallocRaw(db, (int)n);
 1.19296 +  if( zNew ){
 1.19297 +    memcpy(zNew, z, n);
 1.19298 +  }
 1.19299 +  return zNew;
 1.19300 +}
 1.19301 +SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
 1.19302 +  char *zNew;
 1.19303 +  if( z==0 ){
 1.19304 +    return 0;
 1.19305 +  }
 1.19306 +  assert( (n&0x7fffffff)==n );
 1.19307 +  zNew = sqlite3DbMallocRaw(db, n+1);
 1.19308 +  if( zNew ){
 1.19309 +    memcpy(zNew, z, n);
 1.19310 +    zNew[n] = 0;
 1.19311 +  }
 1.19312 +  return zNew;
 1.19313 +}
 1.19314 +
 1.19315 +/*
 1.19316 +** Create a string from the zFromat argument and the va_list that follows.
 1.19317 +** Store the string in memory obtained from sqliteMalloc() and make *pz
 1.19318 +** point to that string.
 1.19319 +*/
 1.19320 +SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
 1.19321 +  va_list ap;
 1.19322 +  char *z;
 1.19323 +
 1.19324 +  va_start(ap, zFormat);
 1.19325 +  z = sqlite3VMPrintf(db, zFormat, ap);
 1.19326 +  va_end(ap);
 1.19327 +  sqlite3DbFree(db, *pz);
 1.19328 +  *pz = z;
 1.19329 +}
 1.19330 +
 1.19331 +
 1.19332 +/*
 1.19333 +** This function must be called before exiting any API function (i.e. 
 1.19334 +** returning control to the user) that has called sqlite3_malloc or
 1.19335 +** sqlite3_realloc.
 1.19336 +**
 1.19337 +** The returned value is normally a copy of the second argument to this
 1.19338 +** function. However, if a malloc() failure has occurred since the previous
 1.19339 +** invocation SQLITE_NOMEM is returned instead. 
 1.19340 +**
 1.19341 +** If the first argument, db, is not NULL and a malloc() error has occurred,
 1.19342 +** then the connection error-code (the value returned by sqlite3_errcode())
 1.19343 +** is set to SQLITE_NOMEM.
 1.19344 +*/
 1.19345 +SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
 1.19346 +  /* If the db handle is not NULL, then we must hold the connection handle
 1.19347 +  ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
 1.19348 +  ** is unsafe, as is the call to sqlite3Error().
 1.19349 +  */
 1.19350 +  assert( !db || sqlite3_mutex_held(db->mutex) );
 1.19351 +  if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
 1.19352 +    sqlite3Error(db, SQLITE_NOMEM, 0);
 1.19353 +    db->mallocFailed = 0;
 1.19354 +    rc = SQLITE_NOMEM;
 1.19355 +  }
 1.19356 +  return rc & (db ? db->errMask : 0xff);
 1.19357 +}
 1.19358 +
 1.19359 +/************** End of malloc.c **********************************************/
 1.19360 +/************** Begin file printf.c ******************************************/
 1.19361 +/*
 1.19362 +** The "printf" code that follows dates from the 1980's.  It is in
 1.19363 +** the public domain.  The original comments are included here for
 1.19364 +** completeness.  They are very out-of-date but might be useful as
 1.19365 +** an historical reference.  Most of the "enhancements" have been backed
 1.19366 +** out so that the functionality is now the same as standard printf().
 1.19367 +**
 1.19368 +**************************************************************************
 1.19369 +**
 1.19370 +** This file contains code for a set of "printf"-like routines.  These
 1.19371 +** routines format strings much like the printf() from the standard C
 1.19372 +** library, though the implementation here has enhancements to support
 1.19373 +** SQLlite.
 1.19374 +*/
 1.19375 +
 1.19376 +/*
 1.19377 +** Conversion types fall into various categories as defined by the
 1.19378 +** following enumeration.
 1.19379 +*/
 1.19380 +#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
 1.19381 +#define etFLOAT       2 /* Floating point.  %f */
 1.19382 +#define etEXP         3 /* Exponentional notation. %e and %E */
 1.19383 +#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
 1.19384 +#define etSIZE        5 /* Return number of characters processed so far. %n */
 1.19385 +#define etSTRING      6 /* Strings. %s */
 1.19386 +#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
 1.19387 +#define etPERCENT     8 /* Percent symbol. %% */
 1.19388 +#define etCHARX       9 /* Characters. %c */
 1.19389 +/* The rest are extensions, not normally found in printf() */
 1.19390 +#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
 1.19391 +#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
 1.19392 +                          NULL pointers replaced by SQL NULL.  %Q */
 1.19393 +#define etTOKEN      12 /* a pointer to a Token structure */
 1.19394 +#define etSRCLIST    13 /* a pointer to a SrcList */
 1.19395 +#define etPOINTER    14 /* The %p conversion */
 1.19396 +#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
 1.19397 +#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
 1.19398 +
 1.19399 +#define etINVALID     0 /* Any unrecognized conversion type */
 1.19400 +
 1.19401 +
 1.19402 +/*
 1.19403 +** An "etByte" is an 8-bit unsigned value.
 1.19404 +*/
 1.19405 +typedef unsigned char etByte;
 1.19406 +
 1.19407 +/*
 1.19408 +** Each builtin conversion character (ex: the 'd' in "%d") is described
 1.19409 +** by an instance of the following structure
 1.19410 +*/
 1.19411 +typedef struct et_info {   /* Information about each format field */
 1.19412 +  char fmttype;            /* The format field code letter */
 1.19413 +  etByte base;             /* The base for radix conversion */
 1.19414 +  etByte flags;            /* One or more of FLAG_ constants below */
 1.19415 +  etByte type;             /* Conversion paradigm */
 1.19416 +  etByte charset;          /* Offset into aDigits[] of the digits string */
 1.19417 +  etByte prefix;           /* Offset into aPrefix[] of the prefix string */
 1.19418 +} et_info;
 1.19419 +
 1.19420 +/*
 1.19421 +** Allowed values for et_info.flags
 1.19422 +*/
 1.19423 +#define FLAG_SIGNED  1     /* True if the value to convert is signed */
 1.19424 +#define FLAG_INTERN  2     /* True if for internal use only */
 1.19425 +#define FLAG_STRING  4     /* Allow infinity precision */
 1.19426 +
 1.19427 +
 1.19428 +/*
 1.19429 +** The following table is searched linearly, so it is good to put the
 1.19430 +** most frequently used conversion types first.
 1.19431 +*/
 1.19432 +static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
 1.19433 +static const char aPrefix[] = "-x0\000X0";
 1.19434 +static const et_info fmtinfo[] = {
 1.19435 +  {  'd', 10, 1, etRADIX,      0,  0 },
 1.19436 +  {  's',  0, 4, etSTRING,     0,  0 },
 1.19437 +  {  'g',  0, 1, etGENERIC,    30, 0 },
 1.19438 +  {  'z',  0, 4, etDYNSTRING,  0,  0 },
 1.19439 +  {  'q',  0, 4, etSQLESCAPE,  0,  0 },
 1.19440 +  {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
 1.19441 +  {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
 1.19442 +  {  'c',  0, 0, etCHARX,      0,  0 },
 1.19443 +  {  'o',  8, 0, etRADIX,      0,  2 },
 1.19444 +  {  'u', 10, 0, etRADIX,      0,  0 },
 1.19445 +  {  'x', 16, 0, etRADIX,      16, 1 },
 1.19446 +  {  'X', 16, 0, etRADIX,      0,  4 },
 1.19447 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.19448 +  {  'f',  0, 1, etFLOAT,      0,  0 },
 1.19449 +  {  'e',  0, 1, etEXP,        30, 0 },
 1.19450 +  {  'E',  0, 1, etEXP,        14, 0 },
 1.19451 +  {  'G',  0, 1, etGENERIC,    14, 0 },
 1.19452 +#endif
 1.19453 +  {  'i', 10, 1, etRADIX,      0,  0 },
 1.19454 +  {  'n',  0, 0, etSIZE,       0,  0 },
 1.19455 +  {  '%',  0, 0, etPERCENT,    0,  0 },
 1.19456 +  {  'p', 16, 0, etPOINTER,    0,  1 },
 1.19457 +
 1.19458 +/* All the rest have the FLAG_INTERN bit set and are thus for internal
 1.19459 +** use only */
 1.19460 +  {  'T',  0, 2, etTOKEN,      0,  0 },
 1.19461 +  {  'S',  0, 2, etSRCLIST,    0,  0 },
 1.19462 +  {  'r', 10, 3, etORDINAL,    0,  0 },
 1.19463 +};
 1.19464 +
 1.19465 +/*
 1.19466 +** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
 1.19467 +** conversions will work.
 1.19468 +*/
 1.19469 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.19470 +/*
 1.19471 +** "*val" is a double such that 0.1 <= *val < 10.0
 1.19472 +** Return the ascii code for the leading digit of *val, then
 1.19473 +** multiply "*val" by 10.0 to renormalize.
 1.19474 +**
 1.19475 +** Example:
 1.19476 +**     input:     *val = 3.14159
 1.19477 +**     output:    *val = 1.4159    function return = '3'
 1.19478 +**
 1.19479 +** The counter *cnt is incremented each time.  After counter exceeds
 1.19480 +** 16 (the number of significant digits in a 64-bit float) '0' is
 1.19481 +** always returned.
 1.19482 +*/
 1.19483 +static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
 1.19484 +  int digit;
 1.19485 +  LONGDOUBLE_TYPE d;
 1.19486 +  if( (*cnt)<=0 ) return '0';
 1.19487 +  (*cnt)--;
 1.19488 +  digit = (int)*val;
 1.19489 +  d = digit;
 1.19490 +  digit += '0';
 1.19491 +  *val = (*val - d)*10.0;
 1.19492 +  return (char)digit;
 1.19493 +}
 1.19494 +#endif /* SQLITE_OMIT_FLOATING_POINT */
 1.19495 +
 1.19496 +/*
 1.19497 +** Append N space characters to the given string buffer.
 1.19498 +*/
 1.19499 +SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
 1.19500 +  static const char zSpaces[] = "                             ";
 1.19501 +  while( N>=(int)sizeof(zSpaces)-1 ){
 1.19502 +    sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
 1.19503 +    N -= sizeof(zSpaces)-1;
 1.19504 +  }
 1.19505 +  if( N>0 ){
 1.19506 +    sqlite3StrAccumAppend(pAccum, zSpaces, N);
 1.19507 +  }
 1.19508 +}
 1.19509 +
 1.19510 +/*
 1.19511 +** On machines with a small stack size, you can redefine the
 1.19512 +** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
 1.19513 +*/
 1.19514 +#ifndef SQLITE_PRINT_BUF_SIZE
 1.19515 +# define SQLITE_PRINT_BUF_SIZE 70
 1.19516 +#endif
 1.19517 +#define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
 1.19518 +
 1.19519 +/*
 1.19520 +** Render a string given by "fmt" into the StrAccum object.
 1.19521 +*/
 1.19522 +SQLITE_PRIVATE void sqlite3VXPrintf(
 1.19523 +  StrAccum *pAccum,                  /* Accumulate results here */
 1.19524 +  int useExtended,                   /* Allow extended %-conversions */
 1.19525 +  const char *fmt,                   /* Format string */
 1.19526 +  va_list ap                         /* arguments */
 1.19527 +){
 1.19528 +  int c;                     /* Next character in the format string */
 1.19529 +  char *bufpt;               /* Pointer to the conversion buffer */
 1.19530 +  int precision;             /* Precision of the current field */
 1.19531 +  int length;                /* Length of the field */
 1.19532 +  int idx;                   /* A general purpose loop counter */
 1.19533 +  int width;                 /* Width of the current field */
 1.19534 +  etByte flag_leftjustify;   /* True if "-" flag is present */
 1.19535 +  etByte flag_plussign;      /* True if "+" flag is present */
 1.19536 +  etByte flag_blanksign;     /* True if " " flag is present */
 1.19537 +  etByte flag_alternateform; /* True if "#" flag is present */
 1.19538 +  etByte flag_altform2;      /* True if "!" flag is present */
 1.19539 +  etByte flag_zeropad;       /* True if field width constant starts with zero */
 1.19540 +  etByte flag_long;          /* True if "l" flag is present */
 1.19541 +  etByte flag_longlong;      /* True if the "ll" flag is present */
 1.19542 +  etByte done;               /* Loop termination flag */
 1.19543 +  etByte xtype = 0;          /* Conversion paradigm */
 1.19544 +  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
 1.19545 +  sqlite_uint64 longvalue;   /* Value for integer types */
 1.19546 +  LONGDOUBLE_TYPE realvalue; /* Value for real types */
 1.19547 +  const et_info *infop;      /* Pointer to the appropriate info structure */
 1.19548 +  char *zOut;                /* Rendering buffer */
 1.19549 +  int nOut;                  /* Size of the rendering buffer */
 1.19550 +  char *zExtra;              /* Malloced memory used by some conversion */
 1.19551 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.19552 +  int  exp, e2;              /* exponent of real numbers */
 1.19553 +  int nsd;                   /* Number of significant digits returned */
 1.19554 +  double rounder;            /* Used for rounding floating point values */
 1.19555 +  etByte flag_dp;            /* True if decimal point should be shown */
 1.19556 +  etByte flag_rtz;           /* True if trailing zeros should be removed */
 1.19557 +#endif
 1.19558 +  char buf[etBUFSIZE];       /* Conversion buffer */
 1.19559 +
 1.19560 +  bufpt = 0;
 1.19561 +  for(; (c=(*fmt))!=0; ++fmt){
 1.19562 +    if( c!='%' ){
 1.19563 +      int amt;
 1.19564 +      bufpt = (char *)fmt;
 1.19565 +      amt = 1;
 1.19566 +      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
 1.19567 +      sqlite3StrAccumAppend(pAccum, bufpt, amt);
 1.19568 +      if( c==0 ) break;
 1.19569 +    }
 1.19570 +    if( (c=(*++fmt))==0 ){
 1.19571 +      sqlite3StrAccumAppend(pAccum, "%", 1);
 1.19572 +      break;
 1.19573 +    }
 1.19574 +    /* Find out what flags are present */
 1.19575 +    flag_leftjustify = flag_plussign = flag_blanksign = 
 1.19576 +     flag_alternateform = flag_altform2 = flag_zeropad = 0;
 1.19577 +    done = 0;
 1.19578 +    do{
 1.19579 +      switch( c ){
 1.19580 +        case '-':   flag_leftjustify = 1;     break;
 1.19581 +        case '+':   flag_plussign = 1;        break;
 1.19582 +        case ' ':   flag_blanksign = 1;       break;
 1.19583 +        case '#':   flag_alternateform = 1;   break;
 1.19584 +        case '!':   flag_altform2 = 1;        break;
 1.19585 +        case '0':   flag_zeropad = 1;         break;
 1.19586 +        default:    done = 1;                 break;
 1.19587 +      }
 1.19588 +    }while( !done && (c=(*++fmt))!=0 );
 1.19589 +    /* Get the field width */
 1.19590 +    width = 0;
 1.19591 +    if( c=='*' ){
 1.19592 +      width = va_arg(ap,int);
 1.19593 +      if( width<0 ){
 1.19594 +        flag_leftjustify = 1;
 1.19595 +        width = -width;
 1.19596 +      }
 1.19597 +      c = *++fmt;
 1.19598 +    }else{
 1.19599 +      while( c>='0' && c<='9' ){
 1.19600 +        width = width*10 + c - '0';
 1.19601 +        c = *++fmt;
 1.19602 +      }
 1.19603 +    }
 1.19604 +    /* Get the precision */
 1.19605 +    if( c=='.' ){
 1.19606 +      precision = 0;
 1.19607 +      c = *++fmt;
 1.19608 +      if( c=='*' ){
 1.19609 +        precision = va_arg(ap,int);
 1.19610 +        if( precision<0 ) precision = -precision;
 1.19611 +        c = *++fmt;
 1.19612 +      }else{
 1.19613 +        while( c>='0' && c<='9' ){
 1.19614 +          precision = precision*10 + c - '0';
 1.19615 +          c = *++fmt;
 1.19616 +        }
 1.19617 +      }
 1.19618 +    }else{
 1.19619 +      precision = -1;
 1.19620 +    }
 1.19621 +    /* Get the conversion type modifier */
 1.19622 +    if( c=='l' ){
 1.19623 +      flag_long = 1;
 1.19624 +      c = *++fmt;
 1.19625 +      if( c=='l' ){
 1.19626 +        flag_longlong = 1;
 1.19627 +        c = *++fmt;
 1.19628 +      }else{
 1.19629 +        flag_longlong = 0;
 1.19630 +      }
 1.19631 +    }else{
 1.19632 +      flag_long = flag_longlong = 0;
 1.19633 +    }
 1.19634 +    /* Fetch the info entry for the field */
 1.19635 +    infop = &fmtinfo[0];
 1.19636 +    xtype = etINVALID;
 1.19637 +    for(idx=0; idx<ArraySize(fmtinfo); idx++){
 1.19638 +      if( c==fmtinfo[idx].fmttype ){
 1.19639 +        infop = &fmtinfo[idx];
 1.19640 +        if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
 1.19641 +          xtype = infop->type;
 1.19642 +        }else{
 1.19643 +          return;
 1.19644 +        }
 1.19645 +        break;
 1.19646 +      }
 1.19647 +    }
 1.19648 +    zExtra = 0;
 1.19649 +
 1.19650 +    /*
 1.19651 +    ** At this point, variables are initialized as follows:
 1.19652 +    **
 1.19653 +    **   flag_alternateform          TRUE if a '#' is present.
 1.19654 +    **   flag_altform2               TRUE if a '!' is present.
 1.19655 +    **   flag_plussign               TRUE if a '+' is present.
 1.19656 +    **   flag_leftjustify            TRUE if a '-' is present or if the
 1.19657 +    **                               field width was negative.
 1.19658 +    **   flag_zeropad                TRUE if the width began with 0.
 1.19659 +    **   flag_long                   TRUE if the letter 'l' (ell) prefixed
 1.19660 +    **                               the conversion character.
 1.19661 +    **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
 1.19662 +    **                               the conversion character.
 1.19663 +    **   flag_blanksign              TRUE if a ' ' is present.
 1.19664 +    **   width                       The specified field width.  This is
 1.19665 +    **                               always non-negative.  Zero is the default.
 1.19666 +    **   precision                   The specified precision.  The default
 1.19667 +    **                               is -1.
 1.19668 +    **   xtype                       The class of the conversion.
 1.19669 +    **   infop                       Pointer to the appropriate info struct.
 1.19670 +    */
 1.19671 +    switch( xtype ){
 1.19672 +      case etPOINTER:
 1.19673 +        flag_longlong = sizeof(char*)==sizeof(i64);
 1.19674 +        flag_long = sizeof(char*)==sizeof(long int);
 1.19675 +        /* Fall through into the next case */
 1.19676 +      case etORDINAL:
 1.19677 +      case etRADIX:
 1.19678 +        if( infop->flags & FLAG_SIGNED ){
 1.19679 +          i64 v;
 1.19680 +          if( flag_longlong ){
 1.19681 +            v = va_arg(ap,i64);
 1.19682 +          }else if( flag_long ){
 1.19683 +            v = va_arg(ap,long int);
 1.19684 +          }else{
 1.19685 +            v = va_arg(ap,int);
 1.19686 +          }
 1.19687 +          if( v<0 ){
 1.19688 +            if( v==SMALLEST_INT64 ){
 1.19689 +              longvalue = ((u64)1)<<63;
 1.19690 +            }else{
 1.19691 +              longvalue = -v;
 1.19692 +            }
 1.19693 +            prefix = '-';
 1.19694 +          }else{
 1.19695 +            longvalue = v;
 1.19696 +            if( flag_plussign )        prefix = '+';
 1.19697 +            else if( flag_blanksign )  prefix = ' ';
 1.19698 +            else                       prefix = 0;
 1.19699 +          }
 1.19700 +        }else{
 1.19701 +          if( flag_longlong ){
 1.19702 +            longvalue = va_arg(ap,u64);
 1.19703 +          }else if( flag_long ){
 1.19704 +            longvalue = va_arg(ap,unsigned long int);
 1.19705 +          }else{
 1.19706 +            longvalue = va_arg(ap,unsigned int);
 1.19707 +          }
 1.19708 +          prefix = 0;
 1.19709 +        }
 1.19710 +        if( longvalue==0 ) flag_alternateform = 0;
 1.19711 +        if( flag_zeropad && precision<width-(prefix!=0) ){
 1.19712 +          precision = width-(prefix!=0);
 1.19713 +        }
 1.19714 +        if( precision<etBUFSIZE-10 ){
 1.19715 +          nOut = etBUFSIZE;
 1.19716 +          zOut = buf;
 1.19717 +        }else{
 1.19718 +          nOut = precision + 10;
 1.19719 +          zOut = zExtra = sqlite3Malloc( nOut );
 1.19720 +          if( zOut==0 ){
 1.19721 +            pAccum->mallocFailed = 1;
 1.19722 +            return;
 1.19723 +          }
 1.19724 +        }
 1.19725 +        bufpt = &zOut[nOut-1];
 1.19726 +        if( xtype==etORDINAL ){
 1.19727 +          static const char zOrd[] = "thstndrd";
 1.19728 +          int x = (int)(longvalue % 10);
 1.19729 +          if( x>=4 || (longvalue/10)%10==1 ){
 1.19730 +            x = 0;
 1.19731 +          }
 1.19732 +          *(--bufpt) = zOrd[x*2+1];
 1.19733 +          *(--bufpt) = zOrd[x*2];
 1.19734 +        }
 1.19735 +        {
 1.19736 +          register const char *cset;      /* Use registers for speed */
 1.19737 +          register int base;
 1.19738 +          cset = &aDigits[infop->charset];
 1.19739 +          base = infop->base;
 1.19740 +          do{                                           /* Convert to ascii */
 1.19741 +            *(--bufpt) = cset[longvalue%base];
 1.19742 +            longvalue = longvalue/base;
 1.19743 +          }while( longvalue>0 );
 1.19744 +        }
 1.19745 +        length = (int)(&zOut[nOut-1]-bufpt);
 1.19746 +        for(idx=precision-length; idx>0; idx--){
 1.19747 +          *(--bufpt) = '0';                             /* Zero pad */
 1.19748 +        }
 1.19749 +        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
 1.19750 +        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
 1.19751 +          const char *pre;
 1.19752 +          char x;
 1.19753 +          pre = &aPrefix[infop->prefix];
 1.19754 +          for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
 1.19755 +        }
 1.19756 +        length = (int)(&zOut[nOut-1]-bufpt);
 1.19757 +        break;
 1.19758 +      case etFLOAT:
 1.19759 +      case etEXP:
 1.19760 +      case etGENERIC:
 1.19761 +        realvalue = va_arg(ap,double);
 1.19762 +#ifdef SQLITE_OMIT_FLOATING_POINT
 1.19763 +        length = 0;
 1.19764 +#else
 1.19765 +        if( precision<0 ) precision = 6;         /* Set default precision */
 1.19766 +        if( realvalue<0.0 ){
 1.19767 +          realvalue = -realvalue;
 1.19768 +          prefix = '-';
 1.19769 +        }else{
 1.19770 +          if( flag_plussign )          prefix = '+';
 1.19771 +          else if( flag_blanksign )    prefix = ' ';
 1.19772 +          else                         prefix = 0;
 1.19773 +        }
 1.19774 +        if( xtype==etGENERIC && precision>0 ) precision--;
 1.19775 +#if 0
 1.19776 +        /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
 1.19777 +        for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
 1.19778 +#else
 1.19779 +        /* It makes more sense to use 0.5 */
 1.19780 +        for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
 1.19781 +#endif
 1.19782 +        if( xtype==etFLOAT ) realvalue += rounder;
 1.19783 +        /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
 1.19784 +        exp = 0;
 1.19785 +        if( sqlite3IsNaN((double)realvalue) ){
 1.19786 +          bufpt = "NaN";
 1.19787 +          length = 3;
 1.19788 +          break;
 1.19789 +        }
 1.19790 +        if( realvalue>0.0 ){
 1.19791 +          LONGDOUBLE_TYPE scale = 1.0;
 1.19792 +          while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
 1.19793 +          while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
 1.19794 +          while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
 1.19795 +          while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
 1.19796 +          realvalue /= scale;
 1.19797 +          while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
 1.19798 +          while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
 1.19799 +          if( exp>350 ){
 1.19800 +            if( prefix=='-' ){
 1.19801 +              bufpt = "-Inf";
 1.19802 +            }else if( prefix=='+' ){
 1.19803 +              bufpt = "+Inf";
 1.19804 +            }else{
 1.19805 +              bufpt = "Inf";
 1.19806 +            }
 1.19807 +            length = sqlite3Strlen30(bufpt);
 1.19808 +            break;
 1.19809 +          }
 1.19810 +        }
 1.19811 +        bufpt = buf;
 1.19812 +        /*
 1.19813 +        ** If the field type is etGENERIC, then convert to either etEXP
 1.19814 +        ** or etFLOAT, as appropriate.
 1.19815 +        */
 1.19816 +        if( xtype!=etFLOAT ){
 1.19817 +          realvalue += rounder;
 1.19818 +          if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
 1.19819 +        }
 1.19820 +        if( xtype==etGENERIC ){
 1.19821 +          flag_rtz = !flag_alternateform;
 1.19822 +          if( exp<-4 || exp>precision ){
 1.19823 +            xtype = etEXP;
 1.19824 +          }else{
 1.19825 +            precision = precision - exp;
 1.19826 +            xtype = etFLOAT;
 1.19827 +          }
 1.19828 +        }else{
 1.19829 +          flag_rtz = flag_altform2;
 1.19830 +        }
 1.19831 +        if( xtype==etEXP ){
 1.19832 +          e2 = 0;
 1.19833 +        }else{
 1.19834 +          e2 = exp;
 1.19835 +        }
 1.19836 +        if( e2+precision+width > etBUFSIZE - 15 ){
 1.19837 +          bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
 1.19838 +          if( bufpt==0 ){
 1.19839 +            pAccum->mallocFailed = 1;
 1.19840 +            return;
 1.19841 +          }
 1.19842 +        }
 1.19843 +        zOut = bufpt;
 1.19844 +        nsd = 16 + flag_altform2*10;
 1.19845 +        flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
 1.19846 +        /* The sign in front of the number */
 1.19847 +        if( prefix ){
 1.19848 +          *(bufpt++) = prefix;
 1.19849 +        }
 1.19850 +        /* Digits prior to the decimal point */
 1.19851 +        if( e2<0 ){
 1.19852 +          *(bufpt++) = '0';
 1.19853 +        }else{
 1.19854 +          for(; e2>=0; e2--){
 1.19855 +            *(bufpt++) = et_getdigit(&realvalue,&nsd);
 1.19856 +          }
 1.19857 +        }
 1.19858 +        /* The decimal point */
 1.19859 +        if( flag_dp ){
 1.19860 +          *(bufpt++) = '.';
 1.19861 +        }
 1.19862 +        /* "0" digits after the decimal point but before the first
 1.19863 +        ** significant digit of the number */
 1.19864 +        for(e2++; e2<0; precision--, e2++){
 1.19865 +          assert( precision>0 );
 1.19866 +          *(bufpt++) = '0';
 1.19867 +        }
 1.19868 +        /* Significant digits after the decimal point */
 1.19869 +        while( (precision--)>0 ){
 1.19870 +          *(bufpt++) = et_getdigit(&realvalue,&nsd);
 1.19871 +        }
 1.19872 +        /* Remove trailing zeros and the "." if no digits follow the "." */
 1.19873 +        if( flag_rtz && flag_dp ){
 1.19874 +          while( bufpt[-1]=='0' ) *(--bufpt) = 0;
 1.19875 +          assert( bufpt>zOut );
 1.19876 +          if( bufpt[-1]=='.' ){
 1.19877 +            if( flag_altform2 ){
 1.19878 +              *(bufpt++) = '0';
 1.19879 +            }else{
 1.19880 +              *(--bufpt) = 0;
 1.19881 +            }
 1.19882 +          }
 1.19883 +        }
 1.19884 +        /* Add the "eNNN" suffix */
 1.19885 +        if( xtype==etEXP ){
 1.19886 +          *(bufpt++) = aDigits[infop->charset];
 1.19887 +          if( exp<0 ){
 1.19888 +            *(bufpt++) = '-'; exp = -exp;
 1.19889 +          }else{
 1.19890 +            *(bufpt++) = '+';
 1.19891 +          }
 1.19892 +          if( exp>=100 ){
 1.19893 +            *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
 1.19894 +            exp %= 100;
 1.19895 +          }
 1.19896 +          *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
 1.19897 +          *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
 1.19898 +        }
 1.19899 +        *bufpt = 0;
 1.19900 +
 1.19901 +        /* The converted number is in buf[] and zero terminated. Output it.
 1.19902 +        ** Note that the number is in the usual order, not reversed as with
 1.19903 +        ** integer conversions. */
 1.19904 +        length = (int)(bufpt-zOut);
 1.19905 +        bufpt = zOut;
 1.19906 +
 1.19907 +        /* Special case:  Add leading zeros if the flag_zeropad flag is
 1.19908 +        ** set and we are not left justified */
 1.19909 +        if( flag_zeropad && !flag_leftjustify && length < width){
 1.19910 +          int i;
 1.19911 +          int nPad = width - length;
 1.19912 +          for(i=width; i>=nPad; i--){
 1.19913 +            bufpt[i] = bufpt[i-nPad];
 1.19914 +          }
 1.19915 +          i = prefix!=0;
 1.19916 +          while( nPad-- ) bufpt[i++] = '0';
 1.19917 +          length = width;
 1.19918 +        }
 1.19919 +#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
 1.19920 +        break;
 1.19921 +      case etSIZE:
 1.19922 +        *(va_arg(ap,int*)) = pAccum->nChar;
 1.19923 +        length = width = 0;
 1.19924 +        break;
 1.19925 +      case etPERCENT:
 1.19926 +        buf[0] = '%';
 1.19927 +        bufpt = buf;
 1.19928 +        length = 1;
 1.19929 +        break;
 1.19930 +      case etCHARX:
 1.19931 +        c = va_arg(ap,int);
 1.19932 +        buf[0] = (char)c;
 1.19933 +        if( precision>=0 ){
 1.19934 +          for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
 1.19935 +          length = precision;
 1.19936 +        }else{
 1.19937 +          length =1;
 1.19938 +        }
 1.19939 +        bufpt = buf;
 1.19940 +        break;
 1.19941 +      case etSTRING:
 1.19942 +      case etDYNSTRING:
 1.19943 +        bufpt = va_arg(ap,char*);
 1.19944 +        if( bufpt==0 ){
 1.19945 +          bufpt = "";
 1.19946 +        }else if( xtype==etDYNSTRING ){
 1.19947 +          zExtra = bufpt;
 1.19948 +        }
 1.19949 +        if( precision>=0 ){
 1.19950 +          for(length=0; length<precision && bufpt[length]; length++){}
 1.19951 +        }else{
 1.19952 +          length = sqlite3Strlen30(bufpt);
 1.19953 +        }
 1.19954 +        break;
 1.19955 +      case etSQLESCAPE:
 1.19956 +      case etSQLESCAPE2:
 1.19957 +      case etSQLESCAPE3: {
 1.19958 +        int i, j, k, n, isnull;
 1.19959 +        int needQuote;
 1.19960 +        char ch;
 1.19961 +        char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
 1.19962 +        char *escarg = va_arg(ap,char*);
 1.19963 +        isnull = escarg==0;
 1.19964 +        if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
 1.19965 +        k = precision;
 1.19966 +        for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
 1.19967 +          if( ch==q )  n++;
 1.19968 +        }
 1.19969 +        needQuote = !isnull && xtype==etSQLESCAPE2;
 1.19970 +        n += i + 1 + needQuote*2;
 1.19971 +        if( n>etBUFSIZE ){
 1.19972 +          bufpt = zExtra = sqlite3Malloc( n );
 1.19973 +          if( bufpt==0 ){
 1.19974 +            pAccum->mallocFailed = 1;
 1.19975 +            return;
 1.19976 +          }
 1.19977 +        }else{
 1.19978 +          bufpt = buf;
 1.19979 +        }
 1.19980 +        j = 0;
 1.19981 +        if( needQuote ) bufpt[j++] = q;
 1.19982 +        k = i;
 1.19983 +        for(i=0; i<k; i++){
 1.19984 +          bufpt[j++] = ch = escarg[i];
 1.19985 +          if( ch==q ) bufpt[j++] = ch;
 1.19986 +        }
 1.19987 +        if( needQuote ) bufpt[j++] = q;
 1.19988 +        bufpt[j] = 0;
 1.19989 +        length = j;
 1.19990 +        /* The precision in %q and %Q means how many input characters to
 1.19991 +        ** consume, not the length of the output...
 1.19992 +        ** if( precision>=0 && precision<length ) length = precision; */
 1.19993 +        break;
 1.19994 +      }
 1.19995 +      case etTOKEN: {
 1.19996 +        Token *pToken = va_arg(ap, Token*);
 1.19997 +        if( pToken ){
 1.19998 +          sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
 1.19999 +        }
 1.20000 +        length = width = 0;
 1.20001 +        break;
 1.20002 +      }
 1.20003 +      case etSRCLIST: {
 1.20004 +        SrcList *pSrc = va_arg(ap, SrcList*);
 1.20005 +        int k = va_arg(ap, int);
 1.20006 +        struct SrcList_item *pItem = &pSrc->a[k];
 1.20007 +        assert( k>=0 && k<pSrc->nSrc );
 1.20008 +        if( pItem->zDatabase ){
 1.20009 +          sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
 1.20010 +          sqlite3StrAccumAppend(pAccum, ".", 1);
 1.20011 +        }
 1.20012 +        sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
 1.20013 +        length = width = 0;
 1.20014 +        break;
 1.20015 +      }
 1.20016 +      default: {
 1.20017 +        assert( xtype==etINVALID );
 1.20018 +        return;
 1.20019 +      }
 1.20020 +    }/* End switch over the format type */
 1.20021 +    /*
 1.20022 +    ** The text of the conversion is pointed to by "bufpt" and is
 1.20023 +    ** "length" characters long.  The field width is "width".  Do
 1.20024 +    ** the output.
 1.20025 +    */
 1.20026 +    if( !flag_leftjustify ){
 1.20027 +      register int nspace;
 1.20028 +      nspace = width-length;
 1.20029 +      if( nspace>0 ){
 1.20030 +        sqlite3AppendSpace(pAccum, nspace);
 1.20031 +      }
 1.20032 +    }
 1.20033 +    if( length>0 ){
 1.20034 +      sqlite3StrAccumAppend(pAccum, bufpt, length);
 1.20035 +    }
 1.20036 +    if( flag_leftjustify ){
 1.20037 +      register int nspace;
 1.20038 +      nspace = width-length;
 1.20039 +      if( nspace>0 ){
 1.20040 +        sqlite3AppendSpace(pAccum, nspace);
 1.20041 +      }
 1.20042 +    }
 1.20043 +    sqlite3_free(zExtra);
 1.20044 +  }/* End for loop over the format string */
 1.20045 +} /* End of function */
 1.20046 +
 1.20047 +/*
 1.20048 +** Append N bytes of text from z to the StrAccum object.
 1.20049 +*/
 1.20050 +SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
 1.20051 +  assert( z!=0 || N==0 );
 1.20052 +  if( p->tooBig | p->mallocFailed ){
 1.20053 +    testcase(p->tooBig);
 1.20054 +    testcase(p->mallocFailed);
 1.20055 +    return;
 1.20056 +  }
 1.20057 +  assert( p->zText!=0 || p->nChar==0 );
 1.20058 +  if( N<0 ){
 1.20059 +    N = sqlite3Strlen30(z);
 1.20060 +  }
 1.20061 +  if( N==0 || NEVER(z==0) ){
 1.20062 +    return;
 1.20063 +  }
 1.20064 +  if( p->nChar+N >= p->nAlloc ){
 1.20065 +    char *zNew;
 1.20066 +    if( !p->useMalloc ){
 1.20067 +      p->tooBig = 1;
 1.20068 +      N = p->nAlloc - p->nChar - 1;
 1.20069 +      if( N<=0 ){
 1.20070 +        return;
 1.20071 +      }
 1.20072 +    }else{
 1.20073 +      char *zOld = (p->zText==p->zBase ? 0 : p->zText);
 1.20074 +      i64 szNew = p->nChar;
 1.20075 +      szNew += N + 1;
 1.20076 +      if( szNew > p->mxAlloc ){
 1.20077 +        sqlite3StrAccumReset(p);
 1.20078 +        p->tooBig = 1;
 1.20079 +        return;
 1.20080 +      }else{
 1.20081 +        p->nAlloc = (int)szNew;
 1.20082 +      }
 1.20083 +      if( p->useMalloc==1 ){
 1.20084 +        zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 1.20085 +      }else{
 1.20086 +        zNew = sqlite3_realloc(zOld, p->nAlloc);
 1.20087 +      }
 1.20088 +      if( zNew ){
 1.20089 +        if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 1.20090 +        p->zText = zNew;
 1.20091 +      }else{
 1.20092 +        p->mallocFailed = 1;
 1.20093 +        sqlite3StrAccumReset(p);
 1.20094 +        return;
 1.20095 +      }
 1.20096 +    }
 1.20097 +  }
 1.20098 +  assert( p->zText );
 1.20099 +  memcpy(&p->zText[p->nChar], z, N);
 1.20100 +  p->nChar += N;
 1.20101 +}
 1.20102 +
 1.20103 +/*
 1.20104 +** Finish off a string by making sure it is zero-terminated.
 1.20105 +** Return a pointer to the resulting string.  Return a NULL
 1.20106 +** pointer if any kind of error was encountered.
 1.20107 +*/
 1.20108 +SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
 1.20109 +  if( p->zText ){
 1.20110 +    p->zText[p->nChar] = 0;
 1.20111 +    if( p->useMalloc && p->zText==p->zBase ){
 1.20112 +      if( p->useMalloc==1 ){
 1.20113 +        p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 1.20114 +      }else{
 1.20115 +        p->zText = sqlite3_malloc(p->nChar+1);
 1.20116 +      }
 1.20117 +      if( p->zText ){
 1.20118 +        memcpy(p->zText, p->zBase, p->nChar+1);
 1.20119 +      }else{
 1.20120 +        p->mallocFailed = 1;
 1.20121 +      }
 1.20122 +    }
 1.20123 +  }
 1.20124 +  return p->zText;
 1.20125 +}
 1.20126 +
 1.20127 +/*
 1.20128 +** Reset an StrAccum string.  Reclaim all malloced memory.
 1.20129 +*/
 1.20130 +SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
 1.20131 +  if( p->zText!=p->zBase ){
 1.20132 +    if( p->useMalloc==1 ){
 1.20133 +      sqlite3DbFree(p->db, p->zText);
 1.20134 +    }else{
 1.20135 +      sqlite3_free(p->zText);
 1.20136 +    }
 1.20137 +  }
 1.20138 +  p->zText = 0;
 1.20139 +}
 1.20140 +
 1.20141 +/*
 1.20142 +** Initialize a string accumulator
 1.20143 +*/
 1.20144 +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
 1.20145 +  p->zText = p->zBase = zBase;
 1.20146 +  p->db = 0;
 1.20147 +  p->nChar = 0;
 1.20148 +  p->nAlloc = n;
 1.20149 +  p->mxAlloc = mx;
 1.20150 +  p->useMalloc = 1;
 1.20151 +  p->tooBig = 0;
 1.20152 +  p->mallocFailed = 0;
 1.20153 +}
 1.20154 +
 1.20155 +/*
 1.20156 +** Print into memory obtained from sqliteMalloc().  Use the internal
 1.20157 +** %-conversion extensions.
 1.20158 +*/
 1.20159 +SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
 1.20160 +  char *z;
 1.20161 +  char zBase[SQLITE_PRINT_BUF_SIZE];
 1.20162 +  StrAccum acc;
 1.20163 +  assert( db!=0 );
 1.20164 +  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
 1.20165 +                      db->aLimit[SQLITE_LIMIT_LENGTH]);
 1.20166 +  acc.db = db;
 1.20167 +  sqlite3VXPrintf(&acc, 1, zFormat, ap);
 1.20168 +  z = sqlite3StrAccumFinish(&acc);
 1.20169 +  if( acc.mallocFailed ){
 1.20170 +    db->mallocFailed = 1;
 1.20171 +  }
 1.20172 +  return z;
 1.20173 +}
 1.20174 +
 1.20175 +/*
 1.20176 +** Print into memory obtained from sqliteMalloc().  Use the internal
 1.20177 +** %-conversion extensions.
 1.20178 +*/
 1.20179 +SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
 1.20180 +  va_list ap;
 1.20181 +  char *z;
 1.20182 +  va_start(ap, zFormat);
 1.20183 +  z = sqlite3VMPrintf(db, zFormat, ap);
 1.20184 +  va_end(ap);
 1.20185 +  return z;
 1.20186 +}
 1.20187 +
 1.20188 +/*
 1.20189 +** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
 1.20190 +** the string and before returnning.  This routine is intended to be used
 1.20191 +** to modify an existing string.  For example:
 1.20192 +**
 1.20193 +**       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
 1.20194 +**
 1.20195 +*/
 1.20196 +SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
 1.20197 +  va_list ap;
 1.20198 +  char *z;
 1.20199 +  va_start(ap, zFormat);
 1.20200 +  z = sqlite3VMPrintf(db, zFormat, ap);
 1.20201 +  va_end(ap);
 1.20202 +  sqlite3DbFree(db, zStr);
 1.20203 +  return z;
 1.20204 +}
 1.20205 +
 1.20206 +/*
 1.20207 +** Print into memory obtained from sqlite3_malloc().  Omit the internal
 1.20208 +** %-conversion extensions.
 1.20209 +*/
 1.20210 +SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
 1.20211 +  char *z;
 1.20212 +  char zBase[SQLITE_PRINT_BUF_SIZE];
 1.20213 +  StrAccum acc;
 1.20214 +#ifndef SQLITE_OMIT_AUTOINIT
 1.20215 +  if( sqlite3_initialize() ) return 0;
 1.20216 +#endif
 1.20217 +  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
 1.20218 +  acc.useMalloc = 2;
 1.20219 +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
 1.20220 +  z = sqlite3StrAccumFinish(&acc);
 1.20221 +  return z;
 1.20222 +}
 1.20223 +
 1.20224 +/*
 1.20225 +** Print into memory obtained from sqlite3_malloc()().  Omit the internal
 1.20226 +** %-conversion extensions.
 1.20227 +*/
 1.20228 +SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
 1.20229 +  va_list ap;
 1.20230 +  char *z;
 1.20231 +#ifndef SQLITE_OMIT_AUTOINIT
 1.20232 +  if( sqlite3_initialize() ) return 0;
 1.20233 +#endif
 1.20234 +  va_start(ap, zFormat);
 1.20235 +  z = sqlite3_vmprintf(zFormat, ap);
 1.20236 +  va_end(ap);
 1.20237 +  return z;
 1.20238 +}
 1.20239 +
 1.20240 +/*
 1.20241 +** sqlite3_snprintf() works like snprintf() except that it ignores the
 1.20242 +** current locale settings.  This is important for SQLite because we
 1.20243 +** are not able to use a "," as the decimal point in place of "." as
 1.20244 +** specified by some locales.
 1.20245 +**
 1.20246 +** Oops:  The first two arguments of sqlite3_snprintf() are backwards
 1.20247 +** from the snprintf() standard.  Unfortunately, it is too late to change
 1.20248 +** this without breaking compatibility, so we just have to live with the
 1.20249 +** mistake.
 1.20250 +**
 1.20251 +** sqlite3_vsnprintf() is the varargs version.
 1.20252 +*/
 1.20253 +SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
 1.20254 +  StrAccum acc;
 1.20255 +  if( n<=0 ) return zBuf;
 1.20256 +  sqlite3StrAccumInit(&acc, zBuf, n, 0);
 1.20257 +  acc.useMalloc = 0;
 1.20258 +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
 1.20259 +  return sqlite3StrAccumFinish(&acc);
 1.20260 +}
 1.20261 +SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
 1.20262 +  char *z;
 1.20263 +  va_list ap;
 1.20264 +  va_start(ap,zFormat);
 1.20265 +  z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
 1.20266 +  va_end(ap);
 1.20267 +  return z;
 1.20268 +}
 1.20269 +
 1.20270 +/*
 1.20271 +** This is the routine that actually formats the sqlite3_log() message.
 1.20272 +** We house it in a separate routine from sqlite3_log() to avoid using
 1.20273 +** stack space on small-stack systems when logging is disabled.
 1.20274 +**
 1.20275 +** sqlite3_log() must render into a static buffer.  It cannot dynamically
 1.20276 +** allocate memory because it might be called while the memory allocator
 1.20277 +** mutex is held.
 1.20278 +*/
 1.20279 +static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
 1.20280 +  StrAccum acc;                          /* String accumulator */
 1.20281 +  char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
 1.20282 +
 1.20283 +  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
 1.20284 +  acc.useMalloc = 0;
 1.20285 +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
 1.20286 +  sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
 1.20287 +                           sqlite3StrAccumFinish(&acc));
 1.20288 +}
 1.20289 +
 1.20290 +/*
 1.20291 +** Format and write a message to the log if logging is enabled.
 1.20292 +*/
 1.20293 +SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
 1.20294 +  va_list ap;                             /* Vararg list */
 1.20295 +  if( sqlite3GlobalConfig.xLog ){
 1.20296 +    va_start(ap, zFormat);
 1.20297 +    renderLogMsg(iErrCode, zFormat, ap);
 1.20298 +    va_end(ap);
 1.20299 +  }
 1.20300 +}
 1.20301 +
 1.20302 +#if defined(SQLITE_DEBUG)
 1.20303 +/*
 1.20304 +** A version of printf() that understands %lld.  Used for debugging.
 1.20305 +** The printf() built into some versions of windows does not understand %lld
 1.20306 +** and segfaults if you give it a long long int.
 1.20307 +*/
 1.20308 +SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
 1.20309 +  va_list ap;
 1.20310 +  StrAccum acc;
 1.20311 +  char zBuf[500];
 1.20312 +  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
 1.20313 +  acc.useMalloc = 0;
 1.20314 +  va_start(ap,zFormat);
 1.20315 +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
 1.20316 +  va_end(ap);
 1.20317 +  sqlite3StrAccumFinish(&acc);
 1.20318 +  fprintf(stdout,"%s", zBuf);
 1.20319 +  fflush(stdout);
 1.20320 +}
 1.20321 +#endif
 1.20322 +
 1.20323 +#ifndef SQLITE_OMIT_TRACE
 1.20324 +/*
 1.20325 +** variable-argument wrapper around sqlite3VXPrintf().
 1.20326 +*/
 1.20327 +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
 1.20328 +  va_list ap;
 1.20329 +  va_start(ap,zFormat);
 1.20330 +  sqlite3VXPrintf(p, 1, zFormat, ap);
 1.20331 +  va_end(ap);
 1.20332 +}
 1.20333 +#endif
 1.20334 +
 1.20335 +/************** End of printf.c **********************************************/
 1.20336 +/************** Begin file random.c ******************************************/
 1.20337 +/*
 1.20338 +** 2001 September 15
 1.20339 +**
 1.20340 +** The author disclaims copyright to this source code.  In place of
 1.20341 +** a legal notice, here is a blessing:
 1.20342 +**
 1.20343 +**    May you do good and not evil.
 1.20344 +**    May you find forgiveness for yourself and forgive others.
 1.20345 +**    May you share freely, never taking more than you give.
 1.20346 +**
 1.20347 +*************************************************************************
 1.20348 +** This file contains code to implement a pseudo-random number
 1.20349 +** generator (PRNG) for SQLite.
 1.20350 +**
 1.20351 +** Random numbers are used by some of the database backends in order
 1.20352 +** to generate random integer keys for tables or random filenames.
 1.20353 +*/
 1.20354 +
 1.20355 +
 1.20356 +/* All threads share a single random number generator.
 1.20357 +** This structure is the current state of the generator.
 1.20358 +*/
 1.20359 +static SQLITE_WSD struct sqlite3PrngType {
 1.20360 +  unsigned char isInit;          /* True if initialized */
 1.20361 +  unsigned char i, j;            /* State variables */
 1.20362 +  unsigned char s[256];          /* State variables */
 1.20363 +} sqlite3Prng;
 1.20364 +
 1.20365 +/*
 1.20366 +** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
 1.20367 +** must be held while executing this routine.
 1.20368 +**
 1.20369 +** Why not just use a library random generator like lrand48() for this?
 1.20370 +** Because the OP_NewRowid opcode in the VDBE depends on having a very
 1.20371 +** good source of random numbers.  The lrand48() library function may
 1.20372 +** well be good enough.  But maybe not.  Or maybe lrand48() has some
 1.20373 +** subtle problems on some systems that could cause problems.  It is hard
 1.20374 +** to know.  To minimize the risk of problems due to bad lrand48()
 1.20375 +** implementations, SQLite uses this random number generator based
 1.20376 +** on RC4, which we know works very well.
 1.20377 +**
 1.20378 +** (Later):  Actually, OP_NewRowid does not depend on a good source of
 1.20379 +** randomness any more.  But we will leave this code in all the same.
 1.20380 +*/
 1.20381 +static u8 randomByte(void){
 1.20382 +  unsigned char t;
 1.20383 +
 1.20384 +
 1.20385 +  /* The "wsdPrng" macro will resolve to the pseudo-random number generator
 1.20386 +  ** state vector.  If writable static data is unsupported on the target,
 1.20387 +  ** we have to locate the state vector at run-time.  In the more common
 1.20388 +  ** case where writable static data is supported, wsdPrng can refer directly
 1.20389 +  ** to the "sqlite3Prng" state vector declared above.
 1.20390 +  */
 1.20391 +#ifdef SQLITE_OMIT_WSD
 1.20392 +  struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
 1.20393 +# define wsdPrng p[0]
 1.20394 +#else
 1.20395 +# define wsdPrng sqlite3Prng
 1.20396 +#endif
 1.20397 +
 1.20398 +
 1.20399 +  /* Initialize the state of the random number generator once,
 1.20400 +  ** the first time this routine is called.  The seed value does
 1.20401 +  ** not need to contain a lot of randomness since we are not
 1.20402 +  ** trying to do secure encryption or anything like that...
 1.20403 +  **
 1.20404 +  ** Nothing in this file or anywhere else in SQLite does any kind of
 1.20405 +  ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
 1.20406 +  ** number generator) not as an encryption device.
 1.20407 +  */
 1.20408 +  if( !wsdPrng.isInit ){
 1.20409 +    int i;
 1.20410 +    char k[256];
 1.20411 +    wsdPrng.j = 0;
 1.20412 +    wsdPrng.i = 0;
 1.20413 +    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
 1.20414 +    for(i=0; i<256; i++){
 1.20415 +      wsdPrng.s[i] = (u8)i;
 1.20416 +    }
 1.20417 +    for(i=0; i<256; i++){
 1.20418 +      wsdPrng.j += wsdPrng.s[i] + k[i];
 1.20419 +      t = wsdPrng.s[wsdPrng.j];
 1.20420 +      wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
 1.20421 +      wsdPrng.s[i] = t;
 1.20422 +    }
 1.20423 +    wsdPrng.isInit = 1;
 1.20424 +  }
 1.20425 +
 1.20426 +  /* Generate and return single random byte
 1.20427 +  */
 1.20428 +  wsdPrng.i++;
 1.20429 +  t = wsdPrng.s[wsdPrng.i];
 1.20430 +  wsdPrng.j += t;
 1.20431 +  wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
 1.20432 +  wsdPrng.s[wsdPrng.j] = t;
 1.20433 +  t += wsdPrng.s[wsdPrng.i];
 1.20434 +  return wsdPrng.s[t];
 1.20435 +}
 1.20436 +
 1.20437 +/*
 1.20438 +** Return N random bytes.
 1.20439 +*/
 1.20440 +SQLITE_API void sqlite3_randomness(int N, void *pBuf){
 1.20441 +  unsigned char *zBuf = pBuf;
 1.20442 +#if SQLITE_THREADSAFE
 1.20443 +  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
 1.20444 +#endif
 1.20445 +  sqlite3_mutex_enter(mutex);
 1.20446 +  while( N-- ){
 1.20447 +    *(zBuf++) = randomByte();
 1.20448 +  }
 1.20449 +  sqlite3_mutex_leave(mutex);
 1.20450 +}
 1.20451 +
 1.20452 +#ifndef SQLITE_OMIT_BUILTIN_TEST
 1.20453 +/*
 1.20454 +** For testing purposes, we sometimes want to preserve the state of
 1.20455 +** PRNG and restore the PRNG to its saved state at a later time, or
 1.20456 +** to reset the PRNG to its initial state.  These routines accomplish
 1.20457 +** those tasks.
 1.20458 +**
 1.20459 +** The sqlite3_test_control() interface calls these routines to
 1.20460 +** control the PRNG.
 1.20461 +*/
 1.20462 +static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
 1.20463 +SQLITE_PRIVATE void sqlite3PrngSaveState(void){
 1.20464 +  memcpy(
 1.20465 +    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
 1.20466 +    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
 1.20467 +    sizeof(sqlite3Prng)
 1.20468 +  );
 1.20469 +}
 1.20470 +SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
 1.20471 +  memcpy(
 1.20472 +    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
 1.20473 +    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
 1.20474 +    sizeof(sqlite3Prng)
 1.20475 +  );
 1.20476 +}
 1.20477 +SQLITE_PRIVATE void sqlite3PrngResetState(void){
 1.20478 +  GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
 1.20479 +}
 1.20480 +#endif /* SQLITE_OMIT_BUILTIN_TEST */
 1.20481 +
 1.20482 +/************** End of random.c **********************************************/
 1.20483 +/************** Begin file utf.c *********************************************/
 1.20484 +/*
 1.20485 +** 2004 April 13
 1.20486 +**
 1.20487 +** The author disclaims copyright to this source code.  In place of
 1.20488 +** a legal notice, here is a blessing:
 1.20489 +**
 1.20490 +**    May you do good and not evil.
 1.20491 +**    May you find forgiveness for yourself and forgive others.
 1.20492 +**    May you share freely, never taking more than you give.
 1.20493 +**
 1.20494 +*************************************************************************
 1.20495 +** This file contains routines used to translate between UTF-8, 
 1.20496 +** UTF-16, UTF-16BE, and UTF-16LE.
 1.20497 +**
 1.20498 +** Notes on UTF-8:
 1.20499 +**
 1.20500 +**   Byte-0    Byte-1    Byte-2    Byte-3    Value
 1.20501 +**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
 1.20502 +**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
 1.20503 +**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
 1.20504 +**  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
 1.20505 +**
 1.20506 +**
 1.20507 +** Notes on UTF-16:  (with wwww+1==uuuuu)
 1.20508 +**
 1.20509 +**      Word-0               Word-1          Value
 1.20510 +**  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
 1.20511 +**  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
 1.20512 +**
 1.20513 +**
 1.20514 +** BOM or Byte Order Mark:
 1.20515 +**     0xff 0xfe   little-endian utf-16 follows
 1.20516 +**     0xfe 0xff   big-endian utf-16 follows
 1.20517 +**
 1.20518 +*/
 1.20519 +/* #include <assert.h> */
 1.20520 +
 1.20521 +#ifndef SQLITE_AMALGAMATION
 1.20522 +/*
 1.20523 +** The following constant value is used by the SQLITE_BIGENDIAN and
 1.20524 +** SQLITE_LITTLEENDIAN macros.
 1.20525 +*/
 1.20526 +SQLITE_PRIVATE const int sqlite3one = 1;
 1.20527 +#endif /* SQLITE_AMALGAMATION */
 1.20528 +
 1.20529 +/*
 1.20530 +** This lookup table is used to help decode the first byte of
 1.20531 +** a multi-byte UTF8 character.
 1.20532 +*/
 1.20533 +static const unsigned char sqlite3Utf8Trans1[] = {
 1.20534 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 1.20535 +  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 1.20536 +  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 1.20537 +  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
 1.20538 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 1.20539 +  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 1.20540 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 1.20541 +  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
 1.20542 +};
 1.20543 +
 1.20544 +
 1.20545 +#define WRITE_UTF8(zOut, c) {                          \
 1.20546 +  if( c<0x00080 ){                                     \
 1.20547 +    *zOut++ = (u8)(c&0xFF);                            \
 1.20548 +  }                                                    \
 1.20549 +  else if( c<0x00800 ){                                \
 1.20550 +    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
 1.20551 +    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 1.20552 +  }                                                    \
 1.20553 +  else if( c<0x10000 ){                                \
 1.20554 +    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
 1.20555 +    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 1.20556 +    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 1.20557 +  }else{                                               \
 1.20558 +    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
 1.20559 +    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
 1.20560 +    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 1.20561 +    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 1.20562 +  }                                                    \
 1.20563 +}
 1.20564 +
 1.20565 +#define WRITE_UTF16LE(zOut, c) {                                    \
 1.20566 +  if( c<=0xFFFF ){                                                  \
 1.20567 +    *zOut++ = (u8)(c&0x00FF);                                       \
 1.20568 +    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
 1.20569 +  }else{                                                            \
 1.20570 +    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
 1.20571 +    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
 1.20572 +    *zOut++ = (u8)(c&0x00FF);                                       \
 1.20573 +    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
 1.20574 +  }                                                                 \
 1.20575 +}
 1.20576 +
 1.20577 +#define WRITE_UTF16BE(zOut, c) {                                    \
 1.20578 +  if( c<=0xFFFF ){                                                  \
 1.20579 +    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
 1.20580 +    *zOut++ = (u8)(c&0x00FF);                                       \
 1.20581 +  }else{                                                            \
 1.20582 +    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
 1.20583 +    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
 1.20584 +    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
 1.20585 +    *zOut++ = (u8)(c&0x00FF);                                       \
 1.20586 +  }                                                                 \
 1.20587 +}
 1.20588 +
 1.20589 +#define READ_UTF16LE(zIn, TERM, c){                                   \
 1.20590 +  c = (*zIn++);                                                       \
 1.20591 +  c += ((*zIn++)<<8);                                                 \
 1.20592 +  if( c>=0xD800 && c<0xE000 && TERM ){                                \
 1.20593 +    int c2 = (*zIn++);                                                \
 1.20594 +    c2 += ((*zIn++)<<8);                                              \
 1.20595 +    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
 1.20596 +  }                                                                   \
 1.20597 +}
 1.20598 +
 1.20599 +#define READ_UTF16BE(zIn, TERM, c){                                   \
 1.20600 +  c = ((*zIn++)<<8);                                                  \
 1.20601 +  c += (*zIn++);                                                      \
 1.20602 +  if( c>=0xD800 && c<0xE000 && TERM ){                                \
 1.20603 +    int c2 = ((*zIn++)<<8);                                           \
 1.20604 +    c2 += (*zIn++);                                                   \
 1.20605 +    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
 1.20606 +  }                                                                   \
 1.20607 +}
 1.20608 +
 1.20609 +/*
 1.20610 +** Translate a single UTF-8 character.  Return the unicode value.
 1.20611 +**
 1.20612 +** During translation, assume that the byte that zTerm points
 1.20613 +** is a 0x00.
 1.20614 +**
 1.20615 +** Write a pointer to the next unread byte back into *pzNext.
 1.20616 +**
 1.20617 +** Notes On Invalid UTF-8:
 1.20618 +**
 1.20619 +**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
 1.20620 +**     be encoded as a multi-byte character.  Any multi-byte character that
 1.20621 +**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
 1.20622 +**
 1.20623 +**  *  This routine never allows a UTF16 surrogate value to be encoded.
 1.20624 +**     If a multi-byte character attempts to encode a value between
 1.20625 +**     0xd800 and 0xe000 then it is rendered as 0xfffd.
 1.20626 +**
 1.20627 +**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
 1.20628 +**     byte of a character are interpreted as single-byte characters
 1.20629 +**     and rendered as themselves even though they are technically
 1.20630 +**     invalid characters.
 1.20631 +**
 1.20632 +**  *  This routine accepts an infinite number of different UTF8 encodings
 1.20633 +**     for unicode values 0x80 and greater.  It do not change over-length
 1.20634 +**     encodings to 0xfffd as some systems recommend.
 1.20635 +*/
 1.20636 +#define READ_UTF8(zIn, zTerm, c)                           \
 1.20637 +  c = *(zIn++);                                            \
 1.20638 +  if( c>=0xc0 ){                                           \
 1.20639 +    c = sqlite3Utf8Trans1[c-0xc0];                         \
 1.20640 +    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
 1.20641 +      c = (c<<6) + (0x3f & *(zIn++));                      \
 1.20642 +    }                                                      \
 1.20643 +    if( c<0x80                                             \
 1.20644 +        || (c&0xFFFFF800)==0xD800                          \
 1.20645 +        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
 1.20646 +  }
 1.20647 +SQLITE_PRIVATE u32 sqlite3Utf8Read(
 1.20648 +  const unsigned char **pz    /* Pointer to string from which to read char */
 1.20649 +){
 1.20650 +  unsigned int c;
 1.20651 +
 1.20652 +  /* Same as READ_UTF8() above but without the zTerm parameter.
 1.20653 +  ** For this routine, we assume the UTF8 string is always zero-terminated.
 1.20654 +  */
 1.20655 +  c = *((*pz)++);
 1.20656 +  if( c>=0xc0 ){
 1.20657 +    c = sqlite3Utf8Trans1[c-0xc0];
 1.20658 +    while( (*(*pz) & 0xc0)==0x80 ){
 1.20659 +      c = (c<<6) + (0x3f & *((*pz)++));
 1.20660 +    }
 1.20661 +    if( c<0x80
 1.20662 +        || (c&0xFFFFF800)==0xD800
 1.20663 +        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
 1.20664 +  }
 1.20665 +  return c;
 1.20666 +}
 1.20667 +
 1.20668 +
 1.20669 +
 1.20670 +
 1.20671 +/*
 1.20672 +** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
 1.20673 +** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
 1.20674 +*/ 
 1.20675 +/* #define TRANSLATE_TRACE 1 */
 1.20676 +
 1.20677 +#ifndef SQLITE_OMIT_UTF16
 1.20678 +/*
 1.20679 +** This routine transforms the internal text encoding used by pMem to
 1.20680 +** desiredEnc. It is an error if the string is already of the desired
 1.20681 +** encoding, or if *pMem does not contain a string value.
 1.20682 +*/
 1.20683 +SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
 1.20684 +  int len;                    /* Maximum length of output string in bytes */
 1.20685 +  unsigned char *zOut;                  /* Output buffer */
 1.20686 +  unsigned char *zIn;                   /* Input iterator */
 1.20687 +  unsigned char *zTerm;                 /* End of input */
 1.20688 +  unsigned char *z;                     /* Output iterator */
 1.20689 +  unsigned int c;
 1.20690 +
 1.20691 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.20692 +  assert( pMem->flags&MEM_Str );
 1.20693 +  assert( pMem->enc!=desiredEnc );
 1.20694 +  assert( pMem->enc!=0 );
 1.20695 +  assert( pMem->n>=0 );
 1.20696 +
 1.20697 +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
 1.20698 +  {
 1.20699 +    char zBuf[100];
 1.20700 +    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
 1.20701 +    fprintf(stderr, "INPUT:  %s\n", zBuf);
 1.20702 +  }
 1.20703 +#endif
 1.20704 +
 1.20705 +  /* If the translation is between UTF-16 little and big endian, then 
 1.20706 +  ** all that is required is to swap the byte order. This case is handled
 1.20707 +  ** differently from the others.
 1.20708 +  */
 1.20709 +  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
 1.20710 +    u8 temp;
 1.20711 +    int rc;
 1.20712 +    rc = sqlite3VdbeMemMakeWriteable(pMem);
 1.20713 +    if( rc!=SQLITE_OK ){
 1.20714 +      assert( rc==SQLITE_NOMEM );
 1.20715 +      return SQLITE_NOMEM;
 1.20716 +    }
 1.20717 +    zIn = (u8*)pMem->z;
 1.20718 +    zTerm = &zIn[pMem->n&~1];
 1.20719 +    while( zIn<zTerm ){
 1.20720 +      temp = *zIn;
 1.20721 +      *zIn = *(zIn+1);
 1.20722 +      zIn++;
 1.20723 +      *zIn++ = temp;
 1.20724 +    }
 1.20725 +    pMem->enc = desiredEnc;
 1.20726 +    goto translate_out;
 1.20727 +  }
 1.20728 +
 1.20729 +  /* Set len to the maximum number of bytes required in the output buffer. */
 1.20730 +  if( desiredEnc==SQLITE_UTF8 ){
 1.20731 +    /* When converting from UTF-16, the maximum growth results from
 1.20732 +    ** translating a 2-byte character to a 4-byte UTF-8 character.
 1.20733 +    ** A single byte is required for the output string
 1.20734 +    ** nul-terminator.
 1.20735 +    */
 1.20736 +    pMem->n &= ~1;
 1.20737 +    len = pMem->n * 2 + 1;
 1.20738 +  }else{
 1.20739 +    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
 1.20740 +    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
 1.20741 +    ** character. Two bytes are required in the output buffer for the
 1.20742 +    ** nul-terminator.
 1.20743 +    */
 1.20744 +    len = pMem->n * 2 + 2;
 1.20745 +  }
 1.20746 +
 1.20747 +  /* Set zIn to point at the start of the input buffer and zTerm to point 1
 1.20748 +  ** byte past the end.
 1.20749 +  **
 1.20750 +  ** Variable zOut is set to point at the output buffer, space obtained
 1.20751 +  ** from sqlite3_malloc().
 1.20752 +  */
 1.20753 +  zIn = (u8*)pMem->z;
 1.20754 +  zTerm = &zIn[pMem->n];
 1.20755 +  zOut = sqlite3DbMallocRaw(pMem->db, len);
 1.20756 +  if( !zOut ){
 1.20757 +    return SQLITE_NOMEM;
 1.20758 +  }
 1.20759 +  z = zOut;
 1.20760 +
 1.20761 +  if( pMem->enc==SQLITE_UTF8 ){
 1.20762 +    if( desiredEnc==SQLITE_UTF16LE ){
 1.20763 +      /* UTF-8 -> UTF-16 Little-endian */
 1.20764 +      while( zIn<zTerm ){
 1.20765 +        READ_UTF8(zIn, zTerm, c);
 1.20766 +        WRITE_UTF16LE(z, c);
 1.20767 +      }
 1.20768 +    }else{
 1.20769 +      assert( desiredEnc==SQLITE_UTF16BE );
 1.20770 +      /* UTF-8 -> UTF-16 Big-endian */
 1.20771 +      while( zIn<zTerm ){
 1.20772 +        READ_UTF8(zIn, zTerm, c);
 1.20773 +        WRITE_UTF16BE(z, c);
 1.20774 +      }
 1.20775 +    }
 1.20776 +    pMem->n = (int)(z - zOut);
 1.20777 +    *z++ = 0;
 1.20778 +  }else{
 1.20779 +    assert( desiredEnc==SQLITE_UTF8 );
 1.20780 +    if( pMem->enc==SQLITE_UTF16LE ){
 1.20781 +      /* UTF-16 Little-endian -> UTF-8 */
 1.20782 +      while( zIn<zTerm ){
 1.20783 +        READ_UTF16LE(zIn, zIn<zTerm, c); 
 1.20784 +        WRITE_UTF8(z, c);
 1.20785 +      }
 1.20786 +    }else{
 1.20787 +      /* UTF-16 Big-endian -> UTF-8 */
 1.20788 +      while( zIn<zTerm ){
 1.20789 +        READ_UTF16BE(zIn, zIn<zTerm, c); 
 1.20790 +        WRITE_UTF8(z, c);
 1.20791 +      }
 1.20792 +    }
 1.20793 +    pMem->n = (int)(z - zOut);
 1.20794 +  }
 1.20795 +  *z = 0;
 1.20796 +  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
 1.20797 +
 1.20798 +  sqlite3VdbeMemRelease(pMem);
 1.20799 +  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
 1.20800 +  pMem->enc = desiredEnc;
 1.20801 +  pMem->flags |= (MEM_Term|MEM_Dyn);
 1.20802 +  pMem->z = (char*)zOut;
 1.20803 +  pMem->zMalloc = pMem->z;
 1.20804 +
 1.20805 +translate_out:
 1.20806 +#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
 1.20807 +  {
 1.20808 +    char zBuf[100];
 1.20809 +    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
 1.20810 +    fprintf(stderr, "OUTPUT: %s\n", zBuf);
 1.20811 +  }
 1.20812 +#endif
 1.20813 +  return SQLITE_OK;
 1.20814 +}
 1.20815 +
 1.20816 +/*
 1.20817 +** This routine checks for a byte-order mark at the beginning of the 
 1.20818 +** UTF-16 string stored in *pMem. If one is present, it is removed and
 1.20819 +** the encoding of the Mem adjusted. This routine does not do any
 1.20820 +** byte-swapping, it just sets Mem.enc appropriately.
 1.20821 +**
 1.20822 +** The allocation (static, dynamic etc.) and encoding of the Mem may be
 1.20823 +** changed by this function.
 1.20824 +*/
 1.20825 +SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
 1.20826 +  int rc = SQLITE_OK;
 1.20827 +  u8 bom = 0;
 1.20828 +
 1.20829 +  assert( pMem->n>=0 );
 1.20830 +  if( pMem->n>1 ){
 1.20831 +    u8 b1 = *(u8 *)pMem->z;
 1.20832 +    u8 b2 = *(((u8 *)pMem->z) + 1);
 1.20833 +    if( b1==0xFE && b2==0xFF ){
 1.20834 +      bom = SQLITE_UTF16BE;
 1.20835 +    }
 1.20836 +    if( b1==0xFF && b2==0xFE ){
 1.20837 +      bom = SQLITE_UTF16LE;
 1.20838 +    }
 1.20839 +  }
 1.20840 +  
 1.20841 +  if( bom ){
 1.20842 +    rc = sqlite3VdbeMemMakeWriteable(pMem);
 1.20843 +    if( rc==SQLITE_OK ){
 1.20844 +      pMem->n -= 2;
 1.20845 +      memmove(pMem->z, &pMem->z[2], pMem->n);
 1.20846 +      pMem->z[pMem->n] = '\0';
 1.20847 +      pMem->z[pMem->n+1] = '\0';
 1.20848 +      pMem->flags |= MEM_Term;
 1.20849 +      pMem->enc = bom;
 1.20850 +    }
 1.20851 +  }
 1.20852 +  return rc;
 1.20853 +}
 1.20854 +#endif /* SQLITE_OMIT_UTF16 */
 1.20855 +
 1.20856 +/*
 1.20857 +** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
 1.20858 +** return the number of unicode characters in pZ up to (but not including)
 1.20859 +** the first 0x00 byte. If nByte is not less than zero, return the
 1.20860 +** number of unicode characters in the first nByte of pZ (or up to 
 1.20861 +** the first 0x00, whichever comes first).
 1.20862 +*/
 1.20863 +SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
 1.20864 +  int r = 0;
 1.20865 +  const u8 *z = (const u8*)zIn;
 1.20866 +  const u8 *zTerm;
 1.20867 +  if( nByte>=0 ){
 1.20868 +    zTerm = &z[nByte];
 1.20869 +  }else{
 1.20870 +    zTerm = (const u8*)(-1);
 1.20871 +  }
 1.20872 +  assert( z<=zTerm );
 1.20873 +  while( *z!=0 && z<zTerm ){
 1.20874 +    SQLITE_SKIP_UTF8(z);
 1.20875 +    r++;
 1.20876 +  }
 1.20877 +  return r;
 1.20878 +}
 1.20879 +
 1.20880 +/* This test function is not currently used by the automated test-suite. 
 1.20881 +** Hence it is only available in debug builds.
 1.20882 +*/
 1.20883 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.20884 +/*
 1.20885 +** Translate UTF-8 to UTF-8.
 1.20886 +**
 1.20887 +** This has the effect of making sure that the string is well-formed
 1.20888 +** UTF-8.  Miscoded characters are removed.
 1.20889 +**
 1.20890 +** The translation is done in-place and aborted if the output
 1.20891 +** overruns the input.
 1.20892 +*/
 1.20893 +SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
 1.20894 +  unsigned char *zOut = zIn;
 1.20895 +  unsigned char *zStart = zIn;
 1.20896 +  u32 c;
 1.20897 +
 1.20898 +  while( zIn[0] && zOut<=zIn ){
 1.20899 +    c = sqlite3Utf8Read((const u8**)&zIn);
 1.20900 +    if( c!=0xfffd ){
 1.20901 +      WRITE_UTF8(zOut, c);
 1.20902 +    }
 1.20903 +  }
 1.20904 +  *zOut = 0;
 1.20905 +  return (int)(zOut - zStart);
 1.20906 +}
 1.20907 +#endif
 1.20908 +
 1.20909 +#ifndef SQLITE_OMIT_UTF16
 1.20910 +/*
 1.20911 +** Convert a UTF-16 string in the native encoding into a UTF-8 string.
 1.20912 +** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
 1.20913 +** be freed by the calling function.
 1.20914 +**
 1.20915 +** NULL is returned if there is an allocation error.
 1.20916 +*/
 1.20917 +SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
 1.20918 +  Mem m;
 1.20919 +  memset(&m, 0, sizeof(m));
 1.20920 +  m.db = db;
 1.20921 +  sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
 1.20922 +  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
 1.20923 +  if( db->mallocFailed ){
 1.20924 +    sqlite3VdbeMemRelease(&m);
 1.20925 +    m.z = 0;
 1.20926 +  }
 1.20927 +  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
 1.20928 +  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
 1.20929 +  assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
 1.20930 +  assert( m.z || db->mallocFailed );
 1.20931 +  return m.z;
 1.20932 +}
 1.20933 +
 1.20934 +/*
 1.20935 +** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
 1.20936 +** enc. A pointer to the new string is returned, and the value of *pnOut
 1.20937 +** is set to the length of the returned string in bytes. The call should
 1.20938 +** arrange to call sqlite3DbFree() on the returned pointer when it is
 1.20939 +** no longer required.
 1.20940 +** 
 1.20941 +** If a malloc failure occurs, NULL is returned and the db.mallocFailed
 1.20942 +** flag set.
 1.20943 +*/
 1.20944 +#ifdef SQLITE_ENABLE_STAT3
 1.20945 +SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
 1.20946 +  Mem m;
 1.20947 +  memset(&m, 0, sizeof(m));
 1.20948 +  m.db = db;
 1.20949 +  sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
 1.20950 +  if( sqlite3VdbeMemTranslate(&m, enc) ){
 1.20951 +    assert( db->mallocFailed );
 1.20952 +    return 0;
 1.20953 +  }
 1.20954 +  assert( m.z==m.zMalloc );
 1.20955 +  *pnOut = m.n;
 1.20956 +  return m.z;
 1.20957 +}
 1.20958 +#endif
 1.20959 +
 1.20960 +/*
 1.20961 +** zIn is a UTF-16 encoded unicode string at least nChar characters long.
 1.20962 +** Return the number of bytes in the first nChar unicode characters
 1.20963 +** in pZ.  nChar must be non-negative.
 1.20964 +*/
 1.20965 +SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
 1.20966 +  int c;
 1.20967 +  unsigned char const *z = zIn;
 1.20968 +  int n = 0;
 1.20969 +  
 1.20970 +  if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
 1.20971 +    while( n<nChar ){
 1.20972 +      READ_UTF16BE(z, 1, c);
 1.20973 +      n++;
 1.20974 +    }
 1.20975 +  }else{
 1.20976 +    while( n<nChar ){
 1.20977 +      READ_UTF16LE(z, 1, c);
 1.20978 +      n++;
 1.20979 +    }
 1.20980 +  }
 1.20981 +  return (int)(z-(unsigned char const *)zIn);
 1.20982 +}
 1.20983 +
 1.20984 +#if defined(SQLITE_TEST)
 1.20985 +/*
 1.20986 +** This routine is called from the TCL test function "translate_selftest".
 1.20987 +** It checks that the primitives for serializing and deserializing
 1.20988 +** characters in each encoding are inverses of each other.
 1.20989 +*/
 1.20990 +SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
 1.20991 +  unsigned int i, t;
 1.20992 +  unsigned char zBuf[20];
 1.20993 +  unsigned char *z;
 1.20994 +  int n;
 1.20995 +  unsigned int c;
 1.20996 +
 1.20997 +  for(i=0; i<0x00110000; i++){
 1.20998 +    z = zBuf;
 1.20999 +    WRITE_UTF8(z, i);
 1.21000 +    n = (int)(z-zBuf);
 1.21001 +    assert( n>0 && n<=4 );
 1.21002 +    z[0] = 0;
 1.21003 +    z = zBuf;
 1.21004 +    c = sqlite3Utf8Read((const u8**)&z);
 1.21005 +    t = i;
 1.21006 +    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
 1.21007 +    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
 1.21008 +    assert( c==t );
 1.21009 +    assert( (z-zBuf)==n );
 1.21010 +  }
 1.21011 +  for(i=0; i<0x00110000; i++){
 1.21012 +    if( i>=0xD800 && i<0xE000 ) continue;
 1.21013 +    z = zBuf;
 1.21014 +    WRITE_UTF16LE(z, i);
 1.21015 +    n = (int)(z-zBuf);
 1.21016 +    assert( n>0 && n<=4 );
 1.21017 +    z[0] = 0;
 1.21018 +    z = zBuf;
 1.21019 +    READ_UTF16LE(z, 1, c);
 1.21020 +    assert( c==i );
 1.21021 +    assert( (z-zBuf)==n );
 1.21022 +  }
 1.21023 +  for(i=0; i<0x00110000; i++){
 1.21024 +    if( i>=0xD800 && i<0xE000 ) continue;
 1.21025 +    z = zBuf;
 1.21026 +    WRITE_UTF16BE(z, i);
 1.21027 +    n = (int)(z-zBuf);
 1.21028 +    assert( n>0 && n<=4 );
 1.21029 +    z[0] = 0;
 1.21030 +    z = zBuf;
 1.21031 +    READ_UTF16BE(z, 1, c);
 1.21032 +    assert( c==i );
 1.21033 +    assert( (z-zBuf)==n );
 1.21034 +  }
 1.21035 +}
 1.21036 +#endif /* SQLITE_TEST */
 1.21037 +#endif /* SQLITE_OMIT_UTF16 */
 1.21038 +
 1.21039 +/************** End of utf.c *************************************************/
 1.21040 +/************** Begin file util.c ********************************************/
 1.21041 +/*
 1.21042 +** 2001 September 15
 1.21043 +**
 1.21044 +** The author disclaims copyright to this source code.  In place of
 1.21045 +** a legal notice, here is a blessing:
 1.21046 +**
 1.21047 +**    May you do good and not evil.
 1.21048 +**    May you find forgiveness for yourself and forgive others.
 1.21049 +**    May you share freely, never taking more than you give.
 1.21050 +**
 1.21051 +*************************************************************************
 1.21052 +** Utility functions used throughout sqlite.
 1.21053 +**
 1.21054 +** This file contains functions for allocating memory, comparing
 1.21055 +** strings, and stuff like that.
 1.21056 +**
 1.21057 +*/
 1.21058 +/* #include <stdarg.h> */
 1.21059 +#ifdef SQLITE_HAVE_ISNAN
 1.21060 +# include <math.h>
 1.21061 +#endif
 1.21062 +
 1.21063 +/*
 1.21064 +** Routine needed to support the testcase() macro.
 1.21065 +*/
 1.21066 +#ifdef SQLITE_COVERAGE_TEST
 1.21067 +SQLITE_PRIVATE void sqlite3Coverage(int x){
 1.21068 +  static unsigned dummy = 0;
 1.21069 +  dummy += (unsigned)x;
 1.21070 +}
 1.21071 +#endif
 1.21072 +
 1.21073 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.21074 +/*
 1.21075 +** Return true if the floating point value is Not a Number (NaN).
 1.21076 +**
 1.21077 +** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
 1.21078 +** Otherwise, we have our own implementation that works on most systems.
 1.21079 +*/
 1.21080 +SQLITE_PRIVATE int sqlite3IsNaN(double x){
 1.21081 +  int rc;   /* The value return */
 1.21082 +#if !defined(SQLITE_HAVE_ISNAN)
 1.21083 +  /*
 1.21084 +  ** Systems that support the isnan() library function should probably
 1.21085 +  ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
 1.21086 +  ** found that many systems do not have a working isnan() function so
 1.21087 +  ** this implementation is provided as an alternative.
 1.21088 +  **
 1.21089 +  ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
 1.21090 +  ** On the other hand, the use of -ffast-math comes with the following
 1.21091 +  ** warning:
 1.21092 +  **
 1.21093 +  **      This option [-ffast-math] should never be turned on by any
 1.21094 +  **      -O option since it can result in incorrect output for programs
 1.21095 +  **      which depend on an exact implementation of IEEE or ISO 
 1.21096 +  **      rules/specifications for math functions.
 1.21097 +  **
 1.21098 +  ** Under MSVC, this NaN test may fail if compiled with a floating-
 1.21099 +  ** point precision mode other than /fp:precise.  From the MSDN 
 1.21100 +  ** documentation:
 1.21101 +  **
 1.21102 +  **      The compiler [with /fp:precise] will properly handle comparisons 
 1.21103 +  **      involving NaN. For example, x != x evaluates to true if x is NaN 
 1.21104 +  **      ...
 1.21105 +  */
 1.21106 +#ifdef __FAST_MATH__
 1.21107 +# error SQLite will not work correctly with the -ffast-math option of GCC.
 1.21108 +#endif
 1.21109 +  volatile double y = x;
 1.21110 +  volatile double z = y;
 1.21111 +  rc = (y!=z);
 1.21112 +#else  /* if defined(SQLITE_HAVE_ISNAN) */
 1.21113 +  rc = isnan(x);
 1.21114 +#endif /* SQLITE_HAVE_ISNAN */
 1.21115 +  testcase( rc );
 1.21116 +  return rc;
 1.21117 +}
 1.21118 +#endif /* SQLITE_OMIT_FLOATING_POINT */
 1.21119 +
 1.21120 +/*
 1.21121 +** Compute a string length that is limited to what can be stored in
 1.21122 +** lower 30 bits of a 32-bit signed integer.
 1.21123 +**
 1.21124 +** The value returned will never be negative.  Nor will it ever be greater
 1.21125 +** than the actual length of the string.  For very long strings (greater
 1.21126 +** than 1GiB) the value returned might be less than the true string length.
 1.21127 +*/
 1.21128 +SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
 1.21129 +  const char *z2 = z;
 1.21130 +  if( z==0 ) return 0;
 1.21131 +  while( *z2 ){ z2++; }
 1.21132 +  return 0x3fffffff & (int)(z2 - z);
 1.21133 +}
 1.21134 +
 1.21135 +/*
 1.21136 +** Set the most recent error code and error string for the sqlite
 1.21137 +** handle "db". The error code is set to "err_code".
 1.21138 +**
 1.21139 +** If it is not NULL, string zFormat specifies the format of the
 1.21140 +** error string in the style of the printf functions: The following
 1.21141 +** format characters are allowed:
 1.21142 +**
 1.21143 +**      %s      Insert a string
 1.21144 +**      %z      A string that should be freed after use
 1.21145 +**      %d      Insert an integer
 1.21146 +**      %T      Insert a token
 1.21147 +**      %S      Insert the first element of a SrcList
 1.21148 +**
 1.21149 +** zFormat and any string tokens that follow it are assumed to be
 1.21150 +** encoded in UTF-8.
 1.21151 +**
 1.21152 +** To clear the most recent error for sqlite handle "db", sqlite3Error
 1.21153 +** should be called with err_code set to SQLITE_OK and zFormat set
 1.21154 +** to NULL.
 1.21155 +*/
 1.21156 +SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
 1.21157 +  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
 1.21158 +    db->errCode = err_code;
 1.21159 +    if( zFormat ){
 1.21160 +      char *z;
 1.21161 +      va_list ap;
 1.21162 +      va_start(ap, zFormat);
 1.21163 +      z = sqlite3VMPrintf(db, zFormat, ap);
 1.21164 +      va_end(ap);
 1.21165 +      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
 1.21166 +    }else{
 1.21167 +      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
 1.21168 +    }
 1.21169 +  }
 1.21170 +}
 1.21171 +
 1.21172 +/*
 1.21173 +** Add an error message to pParse->zErrMsg and increment pParse->nErr.
 1.21174 +** The following formatting characters are allowed:
 1.21175 +**
 1.21176 +**      %s      Insert a string
 1.21177 +**      %z      A string that should be freed after use
 1.21178 +**      %d      Insert an integer
 1.21179 +**      %T      Insert a token
 1.21180 +**      %S      Insert the first element of a SrcList
 1.21181 +**
 1.21182 +** This function should be used to report any error that occurs whilst
 1.21183 +** compiling an SQL statement (i.e. within sqlite3_prepare()). The
 1.21184 +** last thing the sqlite3_prepare() function does is copy the error
 1.21185 +** stored by this function into the database handle using sqlite3Error().
 1.21186 +** Function sqlite3Error() should be used during statement execution
 1.21187 +** (sqlite3_step() etc.).
 1.21188 +*/
 1.21189 +SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
 1.21190 +  char *zMsg;
 1.21191 +  va_list ap;
 1.21192 +  sqlite3 *db = pParse->db;
 1.21193 +  va_start(ap, zFormat);
 1.21194 +  zMsg = sqlite3VMPrintf(db, zFormat, ap);
 1.21195 +  va_end(ap);
 1.21196 +  if( db->suppressErr ){
 1.21197 +    sqlite3DbFree(db, zMsg);
 1.21198 +  }else{
 1.21199 +    pParse->nErr++;
 1.21200 +    sqlite3DbFree(db, pParse->zErrMsg);
 1.21201 +    pParse->zErrMsg = zMsg;
 1.21202 +    pParse->rc = SQLITE_ERROR;
 1.21203 +  }
 1.21204 +}
 1.21205 +
 1.21206 +/*
 1.21207 +** Convert an SQL-style quoted string into a normal string by removing
 1.21208 +** the quote characters.  The conversion is done in-place.  If the
 1.21209 +** input does not begin with a quote character, then this routine
 1.21210 +** is a no-op.
 1.21211 +**
 1.21212 +** The input string must be zero-terminated.  A new zero-terminator
 1.21213 +** is added to the dequoted string.
 1.21214 +**
 1.21215 +** The return value is -1 if no dequoting occurs or the length of the
 1.21216 +** dequoted string, exclusive of the zero terminator, if dequoting does
 1.21217 +** occur.
 1.21218 +**
 1.21219 +** 2002-Feb-14: This routine is extended to remove MS-Access style
 1.21220 +** brackets from around identifers.  For example:  "[a-b-c]" becomes
 1.21221 +** "a-b-c".
 1.21222 +*/
 1.21223 +SQLITE_PRIVATE int sqlite3Dequote(char *z){
 1.21224 +  char quote;
 1.21225 +  int i, j;
 1.21226 +  if( z==0 ) return -1;
 1.21227 +  quote = z[0];
 1.21228 +  switch( quote ){
 1.21229 +    case '\'':  break;
 1.21230 +    case '"':   break;
 1.21231 +    case '`':   break;                /* For MySQL compatibility */
 1.21232 +    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
 1.21233 +    default:    return -1;
 1.21234 +  }
 1.21235 +  for(i=1, j=0; ALWAYS(z[i]); i++){
 1.21236 +    if( z[i]==quote ){
 1.21237 +      if( z[i+1]==quote ){
 1.21238 +        z[j++] = quote;
 1.21239 +        i++;
 1.21240 +      }else{
 1.21241 +        break;
 1.21242 +      }
 1.21243 +    }else{
 1.21244 +      z[j++] = z[i];
 1.21245 +    }
 1.21246 +  }
 1.21247 +  z[j] = 0;
 1.21248 +  return j;
 1.21249 +}
 1.21250 +
 1.21251 +/* Convenient short-hand */
 1.21252 +#define UpperToLower sqlite3UpperToLower
 1.21253 +
 1.21254 +/*
 1.21255 +** Some systems have stricmp().  Others have strcasecmp().  Because
 1.21256 +** there is no consistency, we will define our own.
 1.21257 +**
 1.21258 +** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
 1.21259 +** sqlite3_strnicmp() APIs allow applications and extensions to compare
 1.21260 +** the contents of two buffers containing UTF-8 strings in a
 1.21261 +** case-independent fashion, using the same definition of "case
 1.21262 +** independence" that SQLite uses internally when comparing identifiers.
 1.21263 +*/
 1.21264 +SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
 1.21265 +  register unsigned char *a, *b;
 1.21266 +  a = (unsigned char *)zLeft;
 1.21267 +  b = (unsigned char *)zRight;
 1.21268 +  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 1.21269 +  return UpperToLower[*a] - UpperToLower[*b];
 1.21270 +}
 1.21271 +SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
 1.21272 +  register unsigned char *a, *b;
 1.21273 +  a = (unsigned char *)zLeft;
 1.21274 +  b = (unsigned char *)zRight;
 1.21275 +  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 1.21276 +  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
 1.21277 +}
 1.21278 +
 1.21279 +/*
 1.21280 +** The string z[] is an text representation of a real number.
 1.21281 +** Convert this string to a double and write it into *pResult.
 1.21282 +**
 1.21283 +** The string z[] is length bytes in length (bytes, not characters) and
 1.21284 +** uses the encoding enc.  The string is not necessarily zero-terminated.
 1.21285 +**
 1.21286 +** Return TRUE if the result is a valid real number (or integer) and FALSE
 1.21287 +** if the string is empty or contains extraneous text.  Valid numbers
 1.21288 +** are in one of these formats:
 1.21289 +**
 1.21290 +**    [+-]digits[E[+-]digits]
 1.21291 +**    [+-]digits.[digits][E[+-]digits]
 1.21292 +**    [+-].digits[E[+-]digits]
 1.21293 +**
 1.21294 +** Leading and trailing whitespace is ignored for the purpose of determining
 1.21295 +** validity.
 1.21296 +**
 1.21297 +** If some prefix of the input string is a valid number, this routine
 1.21298 +** returns FALSE but it still converts the prefix and writes the result
 1.21299 +** into *pResult.
 1.21300 +*/
 1.21301 +SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
 1.21302 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.21303 +  int incr = (enc==SQLITE_UTF8?1:2);
 1.21304 +  const char *zEnd = z + length;
 1.21305 +  /* sign * significand * (10 ^ (esign * exponent)) */
 1.21306 +  int sign = 1;    /* sign of significand */
 1.21307 +  i64 s = 0;       /* significand */
 1.21308 +  int d = 0;       /* adjust exponent for shifting decimal point */
 1.21309 +  int esign = 1;   /* sign of exponent */
 1.21310 +  int e = 0;       /* exponent */
 1.21311 +  int eValid = 1;  /* True exponent is either not used or is well-formed */
 1.21312 +  double result;
 1.21313 +  int nDigits = 0;
 1.21314 +
 1.21315 +  *pResult = 0.0;   /* Default return value, in case of an error */
 1.21316 +
 1.21317 +  if( enc==SQLITE_UTF16BE ) z++;
 1.21318 +
 1.21319 +  /* skip leading spaces */
 1.21320 +  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
 1.21321 +  if( z>=zEnd ) return 0;
 1.21322 +
 1.21323 +  /* get sign of significand */
 1.21324 +  if( *z=='-' ){
 1.21325 +    sign = -1;
 1.21326 +    z+=incr;
 1.21327 +  }else if( *z=='+' ){
 1.21328 +    z+=incr;
 1.21329 +  }
 1.21330 +
 1.21331 +  /* skip leading zeroes */
 1.21332 +  while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
 1.21333 +
 1.21334 +  /* copy max significant digits to significand */
 1.21335 +  while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
 1.21336 +    s = s*10 + (*z - '0');
 1.21337 +    z+=incr, nDigits++;
 1.21338 +  }
 1.21339 +
 1.21340 +  /* skip non-significant significand digits
 1.21341 +  ** (increase exponent by d to shift decimal left) */
 1.21342 +  while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
 1.21343 +  if( z>=zEnd ) goto do_atof_calc;
 1.21344 +
 1.21345 +  /* if decimal point is present */
 1.21346 +  if( *z=='.' ){
 1.21347 +    z+=incr;
 1.21348 +    /* copy digits from after decimal to significand
 1.21349 +    ** (decrease exponent by d to shift decimal right) */
 1.21350 +    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
 1.21351 +      s = s*10 + (*z - '0');
 1.21352 +      z+=incr, nDigits++, d--;
 1.21353 +    }
 1.21354 +    /* skip non-significant digits */
 1.21355 +    while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
 1.21356 +  }
 1.21357 +  if( z>=zEnd ) goto do_atof_calc;
 1.21358 +
 1.21359 +  /* if exponent is present */
 1.21360 +  if( *z=='e' || *z=='E' ){
 1.21361 +    z+=incr;
 1.21362 +    eValid = 0;
 1.21363 +    if( z>=zEnd ) goto do_atof_calc;
 1.21364 +    /* get sign of exponent */
 1.21365 +    if( *z=='-' ){
 1.21366 +      esign = -1;
 1.21367 +      z+=incr;
 1.21368 +    }else if( *z=='+' ){
 1.21369 +      z+=incr;
 1.21370 +    }
 1.21371 +    /* copy digits to exponent */
 1.21372 +    while( z<zEnd && sqlite3Isdigit(*z) ){
 1.21373 +      e = e<10000 ? (e*10 + (*z - '0')) : 10000;
 1.21374 +      z+=incr;
 1.21375 +      eValid = 1;
 1.21376 +    }
 1.21377 +  }
 1.21378 +
 1.21379 +  /* skip trailing spaces */
 1.21380 +  if( nDigits && eValid ){
 1.21381 +    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
 1.21382 +  }
 1.21383 +
 1.21384 +do_atof_calc:
 1.21385 +  /* adjust exponent by d, and update sign */
 1.21386 +  e = (e*esign) + d;
 1.21387 +  if( e<0 ) {
 1.21388 +    esign = -1;
 1.21389 +    e *= -1;
 1.21390 +  } else {
 1.21391 +    esign = 1;
 1.21392 +  }
 1.21393 +
 1.21394 +  /* if 0 significand */
 1.21395 +  if( !s ) {
 1.21396 +    /* In the IEEE 754 standard, zero is signed.
 1.21397 +    ** Add the sign if we've seen at least one digit */
 1.21398 +    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
 1.21399 +  } else {
 1.21400 +    /* attempt to reduce exponent */
 1.21401 +    if( esign>0 ){
 1.21402 +      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
 1.21403 +    }else{
 1.21404 +      while( !(s%10) && e>0 ) e--,s/=10;
 1.21405 +    }
 1.21406 +
 1.21407 +    /* adjust the sign of significand */
 1.21408 +    s = sign<0 ? -s : s;
 1.21409 +
 1.21410 +    /* if exponent, scale significand as appropriate
 1.21411 +    ** and store in result. */
 1.21412 +    if( e ){
 1.21413 +      LONGDOUBLE_TYPE scale = 1.0;
 1.21414 +      /* attempt to handle extremely small/large numbers better */
 1.21415 +      if( e>307 && e<342 ){
 1.21416 +        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
 1.21417 +        if( esign<0 ){
 1.21418 +          result = s / scale;
 1.21419 +          result /= 1.0e+308;
 1.21420 +        }else{
 1.21421 +          result = s * scale;
 1.21422 +          result *= 1.0e+308;
 1.21423 +        }
 1.21424 +      }else if( e>=342 ){
 1.21425 +        if( esign<0 ){
 1.21426 +          result = 0.0*s;
 1.21427 +        }else{
 1.21428 +          result = 1e308*1e308*s;  /* Infinity */
 1.21429 +        }
 1.21430 +      }else{
 1.21431 +        /* 1.0e+22 is the largest power of 10 than can be 
 1.21432 +        ** represented exactly. */
 1.21433 +        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
 1.21434 +        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
 1.21435 +        if( esign<0 ){
 1.21436 +          result = s / scale;
 1.21437 +        }else{
 1.21438 +          result = s * scale;
 1.21439 +        }
 1.21440 +      }
 1.21441 +    } else {
 1.21442 +      result = (double)s;
 1.21443 +    }
 1.21444 +  }
 1.21445 +
 1.21446 +  /* store the result */
 1.21447 +  *pResult = result;
 1.21448 +
 1.21449 +  /* return true if number and no extra non-whitespace chracters after */
 1.21450 +  return z>=zEnd && nDigits>0 && eValid;
 1.21451 +#else
 1.21452 +  return !sqlite3Atoi64(z, pResult, length, enc);
 1.21453 +#endif /* SQLITE_OMIT_FLOATING_POINT */
 1.21454 +}
 1.21455 +
 1.21456 +/*
 1.21457 +** Compare the 19-character string zNum against the text representation
 1.21458 +** value 2^63:  9223372036854775808.  Return negative, zero, or positive
 1.21459 +** if zNum is less than, equal to, or greater than the string.
 1.21460 +** Note that zNum must contain exactly 19 characters.
 1.21461 +**
 1.21462 +** Unlike memcmp() this routine is guaranteed to return the difference
 1.21463 +** in the values of the last digit if the only difference is in the
 1.21464 +** last digit.  So, for example,
 1.21465 +**
 1.21466 +**      compare2pow63("9223372036854775800", 1)
 1.21467 +**
 1.21468 +** will return -8.
 1.21469 +*/
 1.21470 +static int compare2pow63(const char *zNum, int incr){
 1.21471 +  int c = 0;
 1.21472 +  int i;
 1.21473 +                    /* 012345678901234567 */
 1.21474 +  const char *pow63 = "922337203685477580";
 1.21475 +  for(i=0; c==0 && i<18; i++){
 1.21476 +    c = (zNum[i*incr]-pow63[i])*10;
 1.21477 +  }
 1.21478 +  if( c==0 ){
 1.21479 +    c = zNum[18*incr] - '8';
 1.21480 +    testcase( c==(-1) );
 1.21481 +    testcase( c==0 );
 1.21482 +    testcase( c==(+1) );
 1.21483 +  }
 1.21484 +  return c;
 1.21485 +}
 1.21486 +
 1.21487 +
 1.21488 +/*
 1.21489 +** Convert zNum to a 64-bit signed integer.
 1.21490 +**
 1.21491 +** If the zNum value is representable as a 64-bit twos-complement 
 1.21492 +** integer, then write that value into *pNum and return 0.
 1.21493 +**
 1.21494 +** If zNum is exactly 9223372036854665808, return 2.  This special
 1.21495 +** case is broken out because while 9223372036854665808 cannot be a 
 1.21496 +** signed 64-bit integer, its negative -9223372036854665808 can be.
 1.21497 +**
 1.21498 +** If zNum is too big for a 64-bit integer and is not
 1.21499 +** 9223372036854665808 then return 1.
 1.21500 +**
 1.21501 +** length is the number of bytes in the string (bytes, not characters).
 1.21502 +** The string is not necessarily zero-terminated.  The encoding is
 1.21503 +** given by enc.
 1.21504 +*/
 1.21505 +SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
 1.21506 +  int incr = (enc==SQLITE_UTF8?1:2);
 1.21507 +  u64 u = 0;
 1.21508 +  int neg = 0; /* assume positive */
 1.21509 +  int i;
 1.21510 +  int c = 0;
 1.21511 +  const char *zStart;
 1.21512 +  const char *zEnd = zNum + length;
 1.21513 +  if( enc==SQLITE_UTF16BE ) zNum++;
 1.21514 +  while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
 1.21515 +  if( zNum<zEnd ){
 1.21516 +    if( *zNum=='-' ){
 1.21517 +      neg = 1;
 1.21518 +      zNum+=incr;
 1.21519 +    }else if( *zNum=='+' ){
 1.21520 +      zNum+=incr;
 1.21521 +    }
 1.21522 +  }
 1.21523 +  zStart = zNum;
 1.21524 +  while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 1.21525 +  for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 1.21526 +    u = u*10 + c - '0';
 1.21527 +  }
 1.21528 +  if( u>LARGEST_INT64 ){
 1.21529 +    *pNum = SMALLEST_INT64;
 1.21530 +  }else if( neg ){
 1.21531 +    *pNum = -(i64)u;
 1.21532 +  }else{
 1.21533 +    *pNum = (i64)u;
 1.21534 +  }
 1.21535 +  testcase( i==18 );
 1.21536 +  testcase( i==19 );
 1.21537 +  testcase( i==20 );
 1.21538 +  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
 1.21539 +    /* zNum is empty or contains non-numeric text or is longer
 1.21540 +    ** than 19 digits (thus guaranteeing that it is too large) */
 1.21541 +    return 1;
 1.21542 +  }else if( i<19*incr ){
 1.21543 +    /* Less than 19 digits, so we know that it fits in 64 bits */
 1.21544 +    assert( u<=LARGEST_INT64 );
 1.21545 +    return 0;
 1.21546 +  }else{
 1.21547 +    /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 1.21548 +    c = compare2pow63(zNum, incr);
 1.21549 +    if( c<0 ){
 1.21550 +      /* zNum is less than 9223372036854775808 so it fits */
 1.21551 +      assert( u<=LARGEST_INT64 );
 1.21552 +      return 0;
 1.21553 +    }else if( c>0 ){
 1.21554 +      /* zNum is greater than 9223372036854775808 so it overflows */
 1.21555 +      return 1;
 1.21556 +    }else{
 1.21557 +      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 1.21558 +      ** special case 2 overflow if positive */
 1.21559 +      assert( u-1==LARGEST_INT64 );
 1.21560 +      assert( (*pNum)==SMALLEST_INT64 );
 1.21561 +      return neg ? 0 : 2;
 1.21562 +    }
 1.21563 +  }
 1.21564 +}
 1.21565 +
 1.21566 +/*
 1.21567 +** If zNum represents an integer that will fit in 32-bits, then set
 1.21568 +** *pValue to that integer and return true.  Otherwise return false.
 1.21569 +**
 1.21570 +** Any non-numeric characters that following zNum are ignored.
 1.21571 +** This is different from sqlite3Atoi64() which requires the
 1.21572 +** input number to be zero-terminated.
 1.21573 +*/
 1.21574 +SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
 1.21575 +  sqlite_int64 v = 0;
 1.21576 +  int i, c;
 1.21577 +  int neg = 0;
 1.21578 +  if( zNum[0]=='-' ){
 1.21579 +    neg = 1;
 1.21580 +    zNum++;
 1.21581 +  }else if( zNum[0]=='+' ){
 1.21582 +    zNum++;
 1.21583 +  }
 1.21584 +  while( zNum[0]=='0' ) zNum++;
 1.21585 +  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
 1.21586 +    v = v*10 + c;
 1.21587 +  }
 1.21588 +
 1.21589 +  /* The longest decimal representation of a 32 bit integer is 10 digits:
 1.21590 +  **
 1.21591 +  **             1234567890
 1.21592 +  **     2^31 -> 2147483648
 1.21593 +  */
 1.21594 +  testcase( i==10 );
 1.21595 +  if( i>10 ){
 1.21596 +    return 0;
 1.21597 +  }
 1.21598 +  testcase( v-neg==2147483647 );
 1.21599 +  if( v-neg>2147483647 ){
 1.21600 +    return 0;
 1.21601 +  }
 1.21602 +  if( neg ){
 1.21603 +    v = -v;
 1.21604 +  }
 1.21605 +  *pValue = (int)v;
 1.21606 +  return 1;
 1.21607 +}
 1.21608 +
 1.21609 +/*
 1.21610 +** Return a 32-bit integer value extracted from a string.  If the
 1.21611 +** string is not an integer, just return 0.
 1.21612 +*/
 1.21613 +SQLITE_PRIVATE int sqlite3Atoi(const char *z){
 1.21614 +  int x = 0;
 1.21615 +  if( z ) sqlite3GetInt32(z, &x);
 1.21616 +  return x;
 1.21617 +}
 1.21618 +
 1.21619 +/*
 1.21620 +** The variable-length integer encoding is as follows:
 1.21621 +**
 1.21622 +** KEY:
 1.21623 +**         A = 0xxxxxxx    7 bits of data and one flag bit
 1.21624 +**         B = 1xxxxxxx    7 bits of data and one flag bit
 1.21625 +**         C = xxxxxxxx    8 bits of data
 1.21626 +**
 1.21627 +**  7 bits - A
 1.21628 +** 14 bits - BA
 1.21629 +** 21 bits - BBA
 1.21630 +** 28 bits - BBBA
 1.21631 +** 35 bits - BBBBA
 1.21632 +** 42 bits - BBBBBA
 1.21633 +** 49 bits - BBBBBBA
 1.21634 +** 56 bits - BBBBBBBA
 1.21635 +** 64 bits - BBBBBBBBC
 1.21636 +*/
 1.21637 +
 1.21638 +/*
 1.21639 +** Write a 64-bit variable-length integer to memory starting at p[0].
 1.21640 +** The length of data write will be between 1 and 9 bytes.  The number
 1.21641 +** of bytes written is returned.
 1.21642 +**
 1.21643 +** A variable-length integer consists of the lower 7 bits of each byte
 1.21644 +** for all bytes that have the 8th bit set and one byte with the 8th
 1.21645 +** bit clear.  Except, if we get to the 9th byte, it stores the full
 1.21646 +** 8 bits and is the last byte.
 1.21647 +*/
 1.21648 +SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
 1.21649 +  int i, j, n;
 1.21650 +  u8 buf[10];
 1.21651 +  if( v & (((u64)0xff000000)<<32) ){
 1.21652 +    p[8] = (u8)v;
 1.21653 +    v >>= 8;
 1.21654 +    for(i=7; i>=0; i--){
 1.21655 +      p[i] = (u8)((v & 0x7f) | 0x80);
 1.21656 +      v >>= 7;
 1.21657 +    }
 1.21658 +    return 9;
 1.21659 +  }    
 1.21660 +  n = 0;
 1.21661 +  do{
 1.21662 +    buf[n++] = (u8)((v & 0x7f) | 0x80);
 1.21663 +    v >>= 7;
 1.21664 +  }while( v!=0 );
 1.21665 +  buf[0] &= 0x7f;
 1.21666 +  assert( n<=9 );
 1.21667 +  for(i=0, j=n-1; j>=0; j--, i++){
 1.21668 +    p[i] = buf[j];
 1.21669 +  }
 1.21670 +  return n;
 1.21671 +}
 1.21672 +
 1.21673 +/*
 1.21674 +** This routine is a faster version of sqlite3PutVarint() that only
 1.21675 +** works for 32-bit positive integers and which is optimized for
 1.21676 +** the common case of small integers.  A MACRO version, putVarint32,
 1.21677 +** is provided which inlines the single-byte case.  All code should use
 1.21678 +** the MACRO version as this function assumes the single-byte case has
 1.21679 +** already been handled.
 1.21680 +*/
 1.21681 +SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
 1.21682 +#ifndef putVarint32
 1.21683 +  if( (v & ~0x7f)==0 ){
 1.21684 +    p[0] = v;
 1.21685 +    return 1;
 1.21686 +  }
 1.21687 +#endif
 1.21688 +  if( (v & ~0x3fff)==0 ){
 1.21689 +    p[0] = (u8)((v>>7) | 0x80);
 1.21690 +    p[1] = (u8)(v & 0x7f);
 1.21691 +    return 2;
 1.21692 +  }
 1.21693 +  return sqlite3PutVarint(p, v);
 1.21694 +}
 1.21695 +
 1.21696 +/*
 1.21697 +** Bitmasks used by sqlite3GetVarint().  These precomputed constants
 1.21698 +** are defined here rather than simply putting the constant expressions
 1.21699 +** inline in order to work around bugs in the RVT compiler.
 1.21700 +**
 1.21701 +** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
 1.21702 +**
 1.21703 +** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
 1.21704 +*/
 1.21705 +#define SLOT_2_0     0x001fc07f
 1.21706 +#define SLOT_4_2_0   0xf01fc07f
 1.21707 +
 1.21708 +
 1.21709 +/*
 1.21710 +** Read a 64-bit variable-length integer from memory starting at p[0].
 1.21711 +** Return the number of bytes read.  The value is stored in *v.
 1.21712 +*/
 1.21713 +SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
 1.21714 +  u32 a,b,s;
 1.21715 +
 1.21716 +  a = *p;
 1.21717 +  /* a: p0 (unmasked) */
 1.21718 +  if (!(a&0x80))
 1.21719 +  {
 1.21720 +    *v = a;
 1.21721 +    return 1;
 1.21722 +  }
 1.21723 +
 1.21724 +  p++;
 1.21725 +  b = *p;
 1.21726 +  /* b: p1 (unmasked) */
 1.21727 +  if (!(b&0x80))
 1.21728 +  {
 1.21729 +    a &= 0x7f;
 1.21730 +    a = a<<7;
 1.21731 +    a |= b;
 1.21732 +    *v = a;
 1.21733 +    return 2;
 1.21734 +  }
 1.21735 +
 1.21736 +  /* Verify that constants are precomputed correctly */
 1.21737 +  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
 1.21738 +  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
 1.21739 +
 1.21740 +  p++;
 1.21741 +  a = a<<14;
 1.21742 +  a |= *p;
 1.21743 +  /* a: p0<<14 | p2 (unmasked) */
 1.21744 +  if (!(a&0x80))
 1.21745 +  {
 1.21746 +    a &= SLOT_2_0;
 1.21747 +    b &= 0x7f;
 1.21748 +    b = b<<7;
 1.21749 +    a |= b;
 1.21750 +    *v = a;
 1.21751 +    return 3;
 1.21752 +  }
 1.21753 +
 1.21754 +  /* CSE1 from below */
 1.21755 +  a &= SLOT_2_0;
 1.21756 +  p++;
 1.21757 +  b = b<<14;
 1.21758 +  b |= *p;
 1.21759 +  /* b: p1<<14 | p3 (unmasked) */
 1.21760 +  if (!(b&0x80))
 1.21761 +  {
 1.21762 +    b &= SLOT_2_0;
 1.21763 +    /* moved CSE1 up */
 1.21764 +    /* a &= (0x7f<<14)|(0x7f); */
 1.21765 +    a = a<<7;
 1.21766 +    a |= b;
 1.21767 +    *v = a;
 1.21768 +    return 4;
 1.21769 +  }
 1.21770 +
 1.21771 +  /* a: p0<<14 | p2 (masked) */
 1.21772 +  /* b: p1<<14 | p3 (unmasked) */
 1.21773 +  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 1.21774 +  /* moved CSE1 up */
 1.21775 +  /* a &= (0x7f<<14)|(0x7f); */
 1.21776 +  b &= SLOT_2_0;
 1.21777 +  s = a;
 1.21778 +  /* s: p0<<14 | p2 (masked) */
 1.21779 +
 1.21780 +  p++;
 1.21781 +  a = a<<14;
 1.21782 +  a |= *p;
 1.21783 +  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
 1.21784 +  if (!(a&0x80))
 1.21785 +  {
 1.21786 +    /* we can skip these cause they were (effectively) done above in calc'ing s */
 1.21787 +    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
 1.21788 +    /* b &= (0x7f<<14)|(0x7f); */
 1.21789 +    b = b<<7;
 1.21790 +    a |= b;
 1.21791 +    s = s>>18;
 1.21792 +    *v = ((u64)s)<<32 | a;
 1.21793 +    return 5;
 1.21794 +  }
 1.21795 +
 1.21796 +  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 1.21797 +  s = s<<7;
 1.21798 +  s |= b;
 1.21799 +  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 1.21800 +
 1.21801 +  p++;
 1.21802 +  b = b<<14;
 1.21803 +  b |= *p;
 1.21804 +  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
 1.21805 +  if (!(b&0x80))
 1.21806 +  {
 1.21807 +    /* we can skip this cause it was (effectively) done above in calc'ing s */
 1.21808 +    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
 1.21809 +    a &= SLOT_2_0;
 1.21810 +    a = a<<7;
 1.21811 +    a |= b;
 1.21812 +    s = s>>18;
 1.21813 +    *v = ((u64)s)<<32 | a;
 1.21814 +    return 6;
 1.21815 +  }
 1.21816 +
 1.21817 +  p++;
 1.21818 +  a = a<<14;
 1.21819 +  a |= *p;
 1.21820 +  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
 1.21821 +  if (!(a&0x80))
 1.21822 +  {
 1.21823 +    a &= SLOT_4_2_0;
 1.21824 +    b &= SLOT_2_0;
 1.21825 +    b = b<<7;
 1.21826 +    a |= b;
 1.21827 +    s = s>>11;
 1.21828 +    *v = ((u64)s)<<32 | a;
 1.21829 +    return 7;
 1.21830 +  }
 1.21831 +
 1.21832 +  /* CSE2 from below */
 1.21833 +  a &= SLOT_2_0;
 1.21834 +  p++;
 1.21835 +  b = b<<14;
 1.21836 +  b |= *p;
 1.21837 +  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
 1.21838 +  if (!(b&0x80))
 1.21839 +  {
 1.21840 +    b &= SLOT_4_2_0;
 1.21841 +    /* moved CSE2 up */
 1.21842 +    /* a &= (0x7f<<14)|(0x7f); */
 1.21843 +    a = a<<7;
 1.21844 +    a |= b;
 1.21845 +    s = s>>4;
 1.21846 +    *v = ((u64)s)<<32 | a;
 1.21847 +    return 8;
 1.21848 +  }
 1.21849 +
 1.21850 +  p++;
 1.21851 +  a = a<<15;
 1.21852 +  a |= *p;
 1.21853 +  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
 1.21854 +
 1.21855 +  /* moved CSE2 up */
 1.21856 +  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
 1.21857 +  b &= SLOT_2_0;
 1.21858 +  b = b<<8;
 1.21859 +  a |= b;
 1.21860 +
 1.21861 +  s = s<<4;
 1.21862 +  b = p[-4];
 1.21863 +  b &= 0x7f;
 1.21864 +  b = b>>3;
 1.21865 +  s |= b;
 1.21866 +
 1.21867 +  *v = ((u64)s)<<32 | a;
 1.21868 +
 1.21869 +  return 9;
 1.21870 +}
 1.21871 +
 1.21872 +/*
 1.21873 +** Read a 32-bit variable-length integer from memory starting at p[0].
 1.21874 +** Return the number of bytes read.  The value is stored in *v.
 1.21875 +**
 1.21876 +** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
 1.21877 +** integer, then set *v to 0xffffffff.
 1.21878 +**
 1.21879 +** A MACRO version, getVarint32, is provided which inlines the 
 1.21880 +** single-byte case.  All code should use the MACRO version as 
 1.21881 +** this function assumes the single-byte case has already been handled.
 1.21882 +*/
 1.21883 +SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
 1.21884 +  u32 a,b;
 1.21885 +
 1.21886 +  /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
 1.21887 +  ** by the getVarin32() macro */
 1.21888 +  a = *p;
 1.21889 +  /* a: p0 (unmasked) */
 1.21890 +#ifndef getVarint32
 1.21891 +  if (!(a&0x80))
 1.21892 +  {
 1.21893 +    /* Values between 0 and 127 */
 1.21894 +    *v = a;
 1.21895 +    return 1;
 1.21896 +  }
 1.21897 +#endif
 1.21898 +
 1.21899 +  /* The 2-byte case */
 1.21900 +  p++;
 1.21901 +  b = *p;
 1.21902 +  /* b: p1 (unmasked) */
 1.21903 +  if (!(b&0x80))
 1.21904 +  {
 1.21905 +    /* Values between 128 and 16383 */
 1.21906 +    a &= 0x7f;
 1.21907 +    a = a<<7;
 1.21908 +    *v = a | b;
 1.21909 +    return 2;
 1.21910 +  }
 1.21911 +
 1.21912 +  /* The 3-byte case */
 1.21913 +  p++;
 1.21914 +  a = a<<14;
 1.21915 +  a |= *p;
 1.21916 +  /* a: p0<<14 | p2 (unmasked) */
 1.21917 +  if (!(a&0x80))
 1.21918 +  {
 1.21919 +    /* Values between 16384 and 2097151 */
 1.21920 +    a &= (0x7f<<14)|(0x7f);
 1.21921 +    b &= 0x7f;
 1.21922 +    b = b<<7;
 1.21923 +    *v = a | b;
 1.21924 +    return 3;
 1.21925 +  }
 1.21926 +
 1.21927 +  /* A 32-bit varint is used to store size information in btrees.
 1.21928 +  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
 1.21929 +  ** A 3-byte varint is sufficient, for example, to record the size
 1.21930 +  ** of a 1048569-byte BLOB or string.
 1.21931 +  **
 1.21932 +  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
 1.21933 +  ** rare larger cases can be handled by the slower 64-bit varint
 1.21934 +  ** routine.
 1.21935 +  */
 1.21936 +#if 1
 1.21937 +  {
 1.21938 +    u64 v64;
 1.21939 +    u8 n;
 1.21940 +
 1.21941 +    p -= 2;
 1.21942 +    n = sqlite3GetVarint(p, &v64);
 1.21943 +    assert( n>3 && n<=9 );
 1.21944 +    if( (v64 & SQLITE_MAX_U32)!=v64 ){
 1.21945 +      *v = 0xffffffff;
 1.21946 +    }else{
 1.21947 +      *v = (u32)v64;
 1.21948 +    }
 1.21949 +    return n;
 1.21950 +  }
 1.21951 +
 1.21952 +#else
 1.21953 +  /* For following code (kept for historical record only) shows an
 1.21954 +  ** unrolling for the 3- and 4-byte varint cases.  This code is
 1.21955 +  ** slightly faster, but it is also larger and much harder to test.
 1.21956 +  */
 1.21957 +  p++;
 1.21958 +  b = b<<14;
 1.21959 +  b |= *p;
 1.21960 +  /* b: p1<<14 | p3 (unmasked) */
 1.21961 +  if (!(b&0x80))
 1.21962 +  {
 1.21963 +    /* Values between 2097152 and 268435455 */
 1.21964 +    b &= (0x7f<<14)|(0x7f);
 1.21965 +    a &= (0x7f<<14)|(0x7f);
 1.21966 +    a = a<<7;
 1.21967 +    *v = a | b;
 1.21968 +    return 4;
 1.21969 +  }
 1.21970 +
 1.21971 +  p++;
 1.21972 +  a = a<<14;
 1.21973 +  a |= *p;
 1.21974 +  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
 1.21975 +  if (!(a&0x80))
 1.21976 +  {
 1.21977 +    /* Values  between 268435456 and 34359738367 */
 1.21978 +    a &= SLOT_4_2_0;
 1.21979 +    b &= SLOT_4_2_0;
 1.21980 +    b = b<<7;
 1.21981 +    *v = a | b;
 1.21982 +    return 5;
 1.21983 +  }
 1.21984 +
 1.21985 +  /* We can only reach this point when reading a corrupt database
 1.21986 +  ** file.  In that case we are not in any hurry.  Use the (relatively
 1.21987 +  ** slow) general-purpose sqlite3GetVarint() routine to extract the
 1.21988 +  ** value. */
 1.21989 +  {
 1.21990 +    u64 v64;
 1.21991 +    u8 n;
 1.21992 +
 1.21993 +    p -= 4;
 1.21994 +    n = sqlite3GetVarint(p, &v64);
 1.21995 +    assert( n>5 && n<=9 );
 1.21996 +    *v = (u32)v64;
 1.21997 +    return n;
 1.21998 +  }
 1.21999 +#endif
 1.22000 +}
 1.22001 +
 1.22002 +/*
 1.22003 +** Return the number of bytes that will be needed to store the given
 1.22004 +** 64-bit integer.
 1.22005 +*/
 1.22006 +SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
 1.22007 +  int i = 0;
 1.22008 +  do{
 1.22009 +    i++;
 1.22010 +    v >>= 7;
 1.22011 +  }while( v!=0 && ALWAYS(i<9) );
 1.22012 +  return i;
 1.22013 +}
 1.22014 +
 1.22015 +
 1.22016 +/*
 1.22017 +** Read or write a four-byte big-endian integer value.
 1.22018 +*/
 1.22019 +SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
 1.22020 +  return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 1.22021 +}
 1.22022 +SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
 1.22023 +  p[0] = (u8)(v>>24);
 1.22024 +  p[1] = (u8)(v>>16);
 1.22025 +  p[2] = (u8)(v>>8);
 1.22026 +  p[3] = (u8)v;
 1.22027 +}
 1.22028 +
 1.22029 +
 1.22030 +
 1.22031 +/*
 1.22032 +** Translate a single byte of Hex into an integer.
 1.22033 +** This routine only works if h really is a valid hexadecimal
 1.22034 +** character:  0..9a..fA..F
 1.22035 +*/
 1.22036 +SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
 1.22037 +  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
 1.22038 +#ifdef SQLITE_ASCII
 1.22039 +  h += 9*(1&(h>>6));
 1.22040 +#endif
 1.22041 +#ifdef SQLITE_EBCDIC
 1.22042 +  h += 9*(1&~(h>>4));
 1.22043 +#endif
 1.22044 +  return (u8)(h & 0xf);
 1.22045 +}
 1.22046 +
 1.22047 +#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
 1.22048 +/*
 1.22049 +** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
 1.22050 +** value.  Return a pointer to its binary value.  Space to hold the
 1.22051 +** binary value has been obtained from malloc and must be freed by
 1.22052 +** the calling routine.
 1.22053 +*/
 1.22054 +SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
 1.22055 +  char *zBlob;
 1.22056 +  int i;
 1.22057 +
 1.22058 +  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
 1.22059 +  n--;
 1.22060 +  if( zBlob ){
 1.22061 +    for(i=0; i<n; i+=2){
 1.22062 +      zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
 1.22063 +    }
 1.22064 +    zBlob[i/2] = 0;
 1.22065 +  }
 1.22066 +  return zBlob;
 1.22067 +}
 1.22068 +#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
 1.22069 +
 1.22070 +/*
 1.22071 +** Log an error that is an API call on a connection pointer that should
 1.22072 +** not have been used.  The "type" of connection pointer is given as the
 1.22073 +** argument.  The zType is a word like "NULL" or "closed" or "invalid".
 1.22074 +*/
 1.22075 +static void logBadConnection(const char *zType){
 1.22076 +  sqlite3_log(SQLITE_MISUSE, 
 1.22077 +     "API call with %s database connection pointer",
 1.22078 +     zType
 1.22079 +  );
 1.22080 +}
 1.22081 +
 1.22082 +/*
 1.22083 +** Check to make sure we have a valid db pointer.  This test is not
 1.22084 +** foolproof but it does provide some measure of protection against
 1.22085 +** misuse of the interface such as passing in db pointers that are
 1.22086 +** NULL or which have been previously closed.  If this routine returns
 1.22087 +** 1 it means that the db pointer is valid and 0 if it should not be
 1.22088 +** dereferenced for any reason.  The calling function should invoke
 1.22089 +** SQLITE_MISUSE immediately.
 1.22090 +**
 1.22091 +** sqlite3SafetyCheckOk() requires that the db pointer be valid for
 1.22092 +** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
 1.22093 +** open properly and is not fit for general use but which can be
 1.22094 +** used as an argument to sqlite3_errmsg() or sqlite3_close().
 1.22095 +*/
 1.22096 +SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
 1.22097 +  u32 magic;
 1.22098 +  if( db==0 ){
 1.22099 +    logBadConnection("NULL");
 1.22100 +    return 0;
 1.22101 +  }
 1.22102 +  magic = db->magic;
 1.22103 +  if( magic!=SQLITE_MAGIC_OPEN ){
 1.22104 +    if( sqlite3SafetyCheckSickOrOk(db) ){
 1.22105 +      testcase( sqlite3GlobalConfig.xLog!=0 );
 1.22106 +      logBadConnection("unopened");
 1.22107 +    }
 1.22108 +    return 0;
 1.22109 +  }else{
 1.22110 +    return 1;
 1.22111 +  }
 1.22112 +}
 1.22113 +SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
 1.22114 +  u32 magic;
 1.22115 +  magic = db->magic;
 1.22116 +  if( magic!=SQLITE_MAGIC_SICK &&
 1.22117 +      magic!=SQLITE_MAGIC_OPEN &&
 1.22118 +      magic!=SQLITE_MAGIC_BUSY ){
 1.22119 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.22120 +    logBadConnection("invalid");
 1.22121 +    return 0;
 1.22122 +  }else{
 1.22123 +    return 1;
 1.22124 +  }
 1.22125 +}
 1.22126 +
 1.22127 +/*
 1.22128 +** Attempt to add, substract, or multiply the 64-bit signed value iB against
 1.22129 +** the other 64-bit signed integer at *pA and store the result in *pA.
 1.22130 +** Return 0 on success.  Or if the operation would have resulted in an
 1.22131 +** overflow, leave *pA unchanged and return 1.
 1.22132 +*/
 1.22133 +SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
 1.22134 +  i64 iA = *pA;
 1.22135 +  testcase( iA==0 ); testcase( iA==1 );
 1.22136 +  testcase( iB==-1 ); testcase( iB==0 );
 1.22137 +  if( iB>=0 ){
 1.22138 +    testcase( iA>0 && LARGEST_INT64 - iA == iB );
 1.22139 +    testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
 1.22140 +    if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
 1.22141 +    *pA += iB;
 1.22142 +  }else{
 1.22143 +    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
 1.22144 +    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
 1.22145 +    if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 1.22146 +    *pA += iB;
 1.22147 +  }
 1.22148 +  return 0; 
 1.22149 +}
 1.22150 +SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 1.22151 +  testcase( iB==SMALLEST_INT64+1 );
 1.22152 +  if( iB==SMALLEST_INT64 ){
 1.22153 +    testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 1.22154 +    if( (*pA)>=0 ) return 1;
 1.22155 +    *pA -= iB;
 1.22156 +    return 0;
 1.22157 +  }else{
 1.22158 +    return sqlite3AddInt64(pA, -iB);
 1.22159 +  }
 1.22160 +}
 1.22161 +#define TWOPOWER32 (((i64)1)<<32)
 1.22162 +#define TWOPOWER31 (((i64)1)<<31)
 1.22163 +SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 1.22164 +  i64 iA = *pA;
 1.22165 +  i64 iA1, iA0, iB1, iB0, r;
 1.22166 +
 1.22167 +  iA1 = iA/TWOPOWER32;
 1.22168 +  iA0 = iA % TWOPOWER32;
 1.22169 +  iB1 = iB/TWOPOWER32;
 1.22170 +  iB0 = iB % TWOPOWER32;
 1.22171 +  if( iA1*iB1 != 0 ) return 1;
 1.22172 +  assert( iA1*iB0==0 || iA0*iB1==0 );
 1.22173 +  r = iA1*iB0 + iA0*iB1;
 1.22174 +  testcase( r==(-TWOPOWER31)-1 );
 1.22175 +  testcase( r==(-TWOPOWER31) );
 1.22176 +  testcase( r==TWOPOWER31 );
 1.22177 +  testcase( r==TWOPOWER31-1 );
 1.22178 +  if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
 1.22179 +  r *= TWOPOWER32;
 1.22180 +  if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
 1.22181 +  *pA = r;
 1.22182 +  return 0;
 1.22183 +}
 1.22184 +
 1.22185 +/*
 1.22186 +** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
 1.22187 +** if the integer has a value of -2147483648, return +2147483647
 1.22188 +*/
 1.22189 +SQLITE_PRIVATE int sqlite3AbsInt32(int x){
 1.22190 +  if( x>=0 ) return x;
 1.22191 +  if( x==(int)0x80000000 ) return 0x7fffffff;
 1.22192 +  return -x;
 1.22193 +}
 1.22194 +
 1.22195 +#ifdef SQLITE_ENABLE_8_3_NAMES
 1.22196 +/*
 1.22197 +** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
 1.22198 +** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
 1.22199 +** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
 1.22200 +** three characters, then shorten the suffix on z[] to be the last three
 1.22201 +** characters of the original suffix.
 1.22202 +**
 1.22203 +** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
 1.22204 +** do the suffix shortening regardless of URI parameter.
 1.22205 +**
 1.22206 +** Examples:
 1.22207 +**
 1.22208 +**     test.db-journal    =>   test.nal
 1.22209 +**     test.db-wal        =>   test.wal
 1.22210 +**     test.db-shm        =>   test.shm
 1.22211 +**     test.db-mj7f3319fa =>   test.9fa
 1.22212 +*/
 1.22213 +SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
 1.22214 +#if SQLITE_ENABLE_8_3_NAMES<2
 1.22215 +  if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
 1.22216 +#endif
 1.22217 +  {
 1.22218 +    int i, sz;
 1.22219 +    sz = sqlite3Strlen30(z);
 1.22220 +    for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
 1.22221 +    if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
 1.22222 +  }
 1.22223 +}
 1.22224 +#endif
 1.22225 +
 1.22226 +/************** End of util.c ************************************************/
 1.22227 +/************** Begin file hash.c ********************************************/
 1.22228 +/*
 1.22229 +** 2001 September 22
 1.22230 +**
 1.22231 +** The author disclaims copyright to this source code.  In place of
 1.22232 +** a legal notice, here is a blessing:
 1.22233 +**
 1.22234 +**    May you do good and not evil.
 1.22235 +**    May you find forgiveness for yourself and forgive others.
 1.22236 +**    May you share freely, never taking more than you give.
 1.22237 +**
 1.22238 +*************************************************************************
 1.22239 +** This is the implementation of generic hash-tables
 1.22240 +** used in SQLite.
 1.22241 +*/
 1.22242 +/* #include <assert.h> */
 1.22243 +
 1.22244 +/* Turn bulk memory into a hash table object by initializing the
 1.22245 +** fields of the Hash structure.
 1.22246 +**
 1.22247 +** "pNew" is a pointer to the hash table that is to be initialized.
 1.22248 +*/
 1.22249 +SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
 1.22250 +  assert( pNew!=0 );
 1.22251 +  pNew->first = 0;
 1.22252 +  pNew->count = 0;
 1.22253 +  pNew->htsize = 0;
 1.22254 +  pNew->ht = 0;
 1.22255 +}
 1.22256 +
 1.22257 +/* Remove all entries from a hash table.  Reclaim all memory.
 1.22258 +** Call this routine to delete a hash table or to reset a hash table
 1.22259 +** to the empty state.
 1.22260 +*/
 1.22261 +SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
 1.22262 +  HashElem *elem;         /* For looping over all elements of the table */
 1.22263 +
 1.22264 +  assert( pH!=0 );
 1.22265 +  elem = pH->first;
 1.22266 +  pH->first = 0;
 1.22267 +  sqlite3_free(pH->ht);
 1.22268 +  pH->ht = 0;
 1.22269 +  pH->htsize = 0;
 1.22270 +  while( elem ){
 1.22271 +    HashElem *next_elem = elem->next;
 1.22272 +    sqlite3_free(elem);
 1.22273 +    elem = next_elem;
 1.22274 +  }
 1.22275 +  pH->count = 0;
 1.22276 +}
 1.22277 +
 1.22278 +/*
 1.22279 +** The hashing function.
 1.22280 +*/
 1.22281 +static unsigned int strHash(const char *z, int nKey){
 1.22282 +  int h = 0;
 1.22283 +  assert( nKey>=0 );
 1.22284 +  while( nKey > 0  ){
 1.22285 +    h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
 1.22286 +    nKey--;
 1.22287 +  }
 1.22288 +  return h;
 1.22289 +}
 1.22290 +
 1.22291 +
 1.22292 +/* Link pNew element into the hash table pH.  If pEntry!=0 then also
 1.22293 +** insert pNew into the pEntry hash bucket.
 1.22294 +*/
 1.22295 +static void insertElement(
 1.22296 +  Hash *pH,              /* The complete hash table */
 1.22297 +  struct _ht *pEntry,    /* The entry into which pNew is inserted */
 1.22298 +  HashElem *pNew         /* The element to be inserted */
 1.22299 +){
 1.22300 +  HashElem *pHead;       /* First element already in pEntry */
 1.22301 +  if( pEntry ){
 1.22302 +    pHead = pEntry->count ? pEntry->chain : 0;
 1.22303 +    pEntry->count++;
 1.22304 +    pEntry->chain = pNew;
 1.22305 +  }else{
 1.22306 +    pHead = 0;
 1.22307 +  }
 1.22308 +  if( pHead ){
 1.22309 +    pNew->next = pHead;
 1.22310 +    pNew->prev = pHead->prev;
 1.22311 +    if( pHead->prev ){ pHead->prev->next = pNew; }
 1.22312 +    else             { pH->first = pNew; }
 1.22313 +    pHead->prev = pNew;
 1.22314 +  }else{
 1.22315 +    pNew->next = pH->first;
 1.22316 +    if( pH->first ){ pH->first->prev = pNew; }
 1.22317 +    pNew->prev = 0;
 1.22318 +    pH->first = pNew;
 1.22319 +  }
 1.22320 +}
 1.22321 +
 1.22322 +
 1.22323 +/* Resize the hash table so that it cantains "new_size" buckets.
 1.22324 +**
 1.22325 +** The hash table might fail to resize if sqlite3_malloc() fails or
 1.22326 +** if the new size is the same as the prior size.
 1.22327 +** Return TRUE if the resize occurs and false if not.
 1.22328 +*/
 1.22329 +static int rehash(Hash *pH, unsigned int new_size){
 1.22330 +  struct _ht *new_ht;            /* The new hash table */
 1.22331 +  HashElem *elem, *next_elem;    /* For looping over existing elements */
 1.22332 +
 1.22333 +#if SQLITE_MALLOC_SOFT_LIMIT>0
 1.22334 +  if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
 1.22335 +    new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
 1.22336 +  }
 1.22337 +  if( new_size==pH->htsize ) return 0;
 1.22338 +#endif
 1.22339 +
 1.22340 +  /* The inability to allocates space for a larger hash table is
 1.22341 +  ** a performance hit but it is not a fatal error.  So mark the
 1.22342 +  ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of 
 1.22343 +  ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
 1.22344 +  ** only zeroes the requested number of bytes whereas this module will
 1.22345 +  ** use the actual amount of space allocated for the hash table (which
 1.22346 +  ** may be larger than the requested amount).
 1.22347 +  */
 1.22348 +  sqlite3BeginBenignMalloc();
 1.22349 +  new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
 1.22350 +  sqlite3EndBenignMalloc();
 1.22351 +
 1.22352 +  if( new_ht==0 ) return 0;
 1.22353 +  sqlite3_free(pH->ht);
 1.22354 +  pH->ht = new_ht;
 1.22355 +  pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
 1.22356 +  memset(new_ht, 0, new_size*sizeof(struct _ht));
 1.22357 +  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
 1.22358 +    unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
 1.22359 +    next_elem = elem->next;
 1.22360 +    insertElement(pH, &new_ht[h], elem);
 1.22361 +  }
 1.22362 +  return 1;
 1.22363 +}
 1.22364 +
 1.22365 +/* This function (for internal use only) locates an element in an
 1.22366 +** hash table that matches the given key.  The hash for this key has
 1.22367 +** already been computed and is passed as the 4th parameter.
 1.22368 +*/
 1.22369 +static HashElem *findElementGivenHash(
 1.22370 +  const Hash *pH,     /* The pH to be searched */
 1.22371 +  const char *pKey,   /* The key we are searching for */
 1.22372 +  int nKey,           /* Bytes in key (not counting zero terminator) */
 1.22373 +  unsigned int h      /* The hash for this key. */
 1.22374 +){
 1.22375 +  HashElem *elem;                /* Used to loop thru the element list */
 1.22376 +  int count;                     /* Number of elements left to test */
 1.22377 +
 1.22378 +  if( pH->ht ){
 1.22379 +    struct _ht *pEntry = &pH->ht[h];
 1.22380 +    elem = pEntry->chain;
 1.22381 +    count = pEntry->count;
 1.22382 +  }else{
 1.22383 +    elem = pH->first;
 1.22384 +    count = pH->count;
 1.22385 +  }
 1.22386 +  while( count-- && ALWAYS(elem) ){
 1.22387 +    if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
 1.22388 +      return elem;
 1.22389 +    }
 1.22390 +    elem = elem->next;
 1.22391 +  }
 1.22392 +  return 0;
 1.22393 +}
 1.22394 +
 1.22395 +/* Remove a single entry from the hash table given a pointer to that
 1.22396 +** element and a hash on the element's key.
 1.22397 +*/
 1.22398 +static void removeElementGivenHash(
 1.22399 +  Hash *pH,         /* The pH containing "elem" */
 1.22400 +  HashElem* elem,   /* The element to be removed from the pH */
 1.22401 +  unsigned int h    /* Hash value for the element */
 1.22402 +){
 1.22403 +  struct _ht *pEntry;
 1.22404 +  if( elem->prev ){
 1.22405 +    elem->prev->next = elem->next; 
 1.22406 +  }else{
 1.22407 +    pH->first = elem->next;
 1.22408 +  }
 1.22409 +  if( elem->next ){
 1.22410 +    elem->next->prev = elem->prev;
 1.22411 +  }
 1.22412 +  if( pH->ht ){
 1.22413 +    pEntry = &pH->ht[h];
 1.22414 +    if( pEntry->chain==elem ){
 1.22415 +      pEntry->chain = elem->next;
 1.22416 +    }
 1.22417 +    pEntry->count--;
 1.22418 +    assert( pEntry->count>=0 );
 1.22419 +  }
 1.22420 +  sqlite3_free( elem );
 1.22421 +  pH->count--;
 1.22422 +  if( pH->count==0 ){
 1.22423 +    assert( pH->first==0 );
 1.22424 +    assert( pH->count==0 );
 1.22425 +    sqlite3HashClear(pH);
 1.22426 +  }
 1.22427 +}
 1.22428 +
 1.22429 +/* Attempt to locate an element of the hash table pH with a key
 1.22430 +** that matches pKey,nKey.  Return the data for this element if it is
 1.22431 +** found, or NULL if there is no match.
 1.22432 +*/
 1.22433 +SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
 1.22434 +  HashElem *elem;    /* The element that matches key */
 1.22435 +  unsigned int h;    /* A hash on key */
 1.22436 +
 1.22437 +  assert( pH!=0 );
 1.22438 +  assert( pKey!=0 );
 1.22439 +  assert( nKey>=0 );
 1.22440 +  if( pH->ht ){
 1.22441 +    h = strHash(pKey, nKey) % pH->htsize;
 1.22442 +  }else{
 1.22443 +    h = 0;
 1.22444 +  }
 1.22445 +  elem = findElementGivenHash(pH, pKey, nKey, h);
 1.22446 +  return elem ? elem->data : 0;
 1.22447 +}
 1.22448 +
 1.22449 +/* Insert an element into the hash table pH.  The key is pKey,nKey
 1.22450 +** and the data is "data".
 1.22451 +**
 1.22452 +** If no element exists with a matching key, then a new
 1.22453 +** element is created and NULL is returned.
 1.22454 +**
 1.22455 +** If another element already exists with the same key, then the
 1.22456 +** new data replaces the old data and the old data is returned.
 1.22457 +** The key is not copied in this instance.  If a malloc fails, then
 1.22458 +** the new data is returned and the hash table is unchanged.
 1.22459 +**
 1.22460 +** If the "data" parameter to this function is NULL, then the
 1.22461 +** element corresponding to "key" is removed from the hash table.
 1.22462 +*/
 1.22463 +SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
 1.22464 +  unsigned int h;       /* the hash of the key modulo hash table size */
 1.22465 +  HashElem *elem;       /* Used to loop thru the element list */
 1.22466 +  HashElem *new_elem;   /* New element added to the pH */
 1.22467 +
 1.22468 +  assert( pH!=0 );
 1.22469 +  assert( pKey!=0 );
 1.22470 +  assert( nKey>=0 );
 1.22471 +  if( pH->htsize ){
 1.22472 +    h = strHash(pKey, nKey) % pH->htsize;
 1.22473 +  }else{
 1.22474 +    h = 0;
 1.22475 +  }
 1.22476 +  elem = findElementGivenHash(pH,pKey,nKey,h);
 1.22477 +  if( elem ){
 1.22478 +    void *old_data = elem->data;
 1.22479 +    if( data==0 ){
 1.22480 +      removeElementGivenHash(pH,elem,h);
 1.22481 +    }else{
 1.22482 +      elem->data = data;
 1.22483 +      elem->pKey = pKey;
 1.22484 +      assert(nKey==elem->nKey);
 1.22485 +    }
 1.22486 +    return old_data;
 1.22487 +  }
 1.22488 +  if( data==0 ) return 0;
 1.22489 +  new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
 1.22490 +  if( new_elem==0 ) return data;
 1.22491 +  new_elem->pKey = pKey;
 1.22492 +  new_elem->nKey = nKey;
 1.22493 +  new_elem->data = data;
 1.22494 +  pH->count++;
 1.22495 +  if( pH->count>=10 && pH->count > 2*pH->htsize ){
 1.22496 +    if( rehash(pH, pH->count*2) ){
 1.22497 +      assert( pH->htsize>0 );
 1.22498 +      h = strHash(pKey, nKey) % pH->htsize;
 1.22499 +    }
 1.22500 +  }
 1.22501 +  if( pH->ht ){
 1.22502 +    insertElement(pH, &pH->ht[h], new_elem);
 1.22503 +  }else{
 1.22504 +    insertElement(pH, 0, new_elem);
 1.22505 +  }
 1.22506 +  return 0;
 1.22507 +}
 1.22508 +
 1.22509 +/************** End of hash.c ************************************************/
 1.22510 +/************** Begin file opcodes.c *****************************************/
 1.22511 +/* Automatically generated.  Do not edit */
 1.22512 +/* See the mkopcodec.awk script for details. */
 1.22513 +#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 1.22514 +SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
 1.22515 + static const char *const azName[] = { "?",
 1.22516 +     /*   1 */ "Goto",
 1.22517 +     /*   2 */ "Gosub",
 1.22518 +     /*   3 */ "Return",
 1.22519 +     /*   4 */ "Yield",
 1.22520 +     /*   5 */ "HaltIfNull",
 1.22521 +     /*   6 */ "Halt",
 1.22522 +     /*   7 */ "Integer",
 1.22523 +     /*   8 */ "Int64",
 1.22524 +     /*   9 */ "String",
 1.22525 +     /*  10 */ "Null",
 1.22526 +     /*  11 */ "Blob",
 1.22527 +     /*  12 */ "Variable",
 1.22528 +     /*  13 */ "Move",
 1.22529 +     /*  14 */ "Copy",
 1.22530 +     /*  15 */ "SCopy",
 1.22531 +     /*  16 */ "ResultRow",
 1.22532 +     /*  17 */ "CollSeq",
 1.22533 +     /*  18 */ "Function",
 1.22534 +     /*  19 */ "Not",
 1.22535 +     /*  20 */ "AddImm",
 1.22536 +     /*  21 */ "MustBeInt",
 1.22537 +     /*  22 */ "RealAffinity",
 1.22538 +     /*  23 */ "Permutation",
 1.22539 +     /*  24 */ "Compare",
 1.22540 +     /*  25 */ "Jump",
 1.22541 +     /*  26 */ "Once",
 1.22542 +     /*  27 */ "If",
 1.22543 +     /*  28 */ "IfNot",
 1.22544 +     /*  29 */ "Column",
 1.22545 +     /*  30 */ "Affinity",
 1.22546 +     /*  31 */ "MakeRecord",
 1.22547 +     /*  32 */ "Count",
 1.22548 +     /*  33 */ "Savepoint",
 1.22549 +     /*  34 */ "AutoCommit",
 1.22550 +     /*  35 */ "Transaction",
 1.22551 +     /*  36 */ "ReadCookie",
 1.22552 +     /*  37 */ "SetCookie",
 1.22553 +     /*  38 */ "VerifyCookie",
 1.22554 +     /*  39 */ "OpenRead",
 1.22555 +     /*  40 */ "OpenWrite",
 1.22556 +     /*  41 */ "OpenAutoindex",
 1.22557 +     /*  42 */ "OpenEphemeral",
 1.22558 +     /*  43 */ "SorterOpen",
 1.22559 +     /*  44 */ "OpenPseudo",
 1.22560 +     /*  45 */ "Close",
 1.22561 +     /*  46 */ "SeekLt",
 1.22562 +     /*  47 */ "SeekLe",
 1.22563 +     /*  48 */ "SeekGe",
 1.22564 +     /*  49 */ "SeekGt",
 1.22565 +     /*  50 */ "Seek",
 1.22566 +     /*  51 */ "NotFound",
 1.22567 +     /*  52 */ "Found",
 1.22568 +     /*  53 */ "IsUnique",
 1.22569 +     /*  54 */ "NotExists",
 1.22570 +     /*  55 */ "Sequence",
 1.22571 +     /*  56 */ "NewRowid",
 1.22572 +     /*  57 */ "Insert",
 1.22573 +     /*  58 */ "InsertInt",
 1.22574 +     /*  59 */ "Delete",
 1.22575 +     /*  60 */ "ResetCount",
 1.22576 +     /*  61 */ "SorterCompare",
 1.22577 +     /*  62 */ "SorterData",
 1.22578 +     /*  63 */ "RowKey",
 1.22579 +     /*  64 */ "RowData",
 1.22580 +     /*  65 */ "Rowid",
 1.22581 +     /*  66 */ "NullRow",
 1.22582 +     /*  67 */ "Last",
 1.22583 +     /*  68 */ "Or",
 1.22584 +     /*  69 */ "And",
 1.22585 +     /*  70 */ "SorterSort",
 1.22586 +     /*  71 */ "Sort",
 1.22587 +     /*  72 */ "Rewind",
 1.22588 +     /*  73 */ "IsNull",
 1.22589 +     /*  74 */ "NotNull",
 1.22590 +     /*  75 */ "Ne",
 1.22591 +     /*  76 */ "Eq",
 1.22592 +     /*  77 */ "Gt",
 1.22593 +     /*  78 */ "Le",
 1.22594 +     /*  79 */ "Lt",
 1.22595 +     /*  80 */ "Ge",
 1.22596 +     /*  81 */ "SorterNext",
 1.22597 +     /*  82 */ "BitAnd",
 1.22598 +     /*  83 */ "BitOr",
 1.22599 +     /*  84 */ "ShiftLeft",
 1.22600 +     /*  85 */ "ShiftRight",
 1.22601 +     /*  86 */ "Add",
 1.22602 +     /*  87 */ "Subtract",
 1.22603 +     /*  88 */ "Multiply",
 1.22604 +     /*  89 */ "Divide",
 1.22605 +     /*  90 */ "Remainder",
 1.22606 +     /*  91 */ "Concat",
 1.22607 +     /*  92 */ "Prev",
 1.22608 +     /*  93 */ "BitNot",
 1.22609 +     /*  94 */ "String8",
 1.22610 +     /*  95 */ "Next",
 1.22611 +     /*  96 */ "SorterInsert",
 1.22612 +     /*  97 */ "IdxInsert",
 1.22613 +     /*  98 */ "IdxDelete",
 1.22614 +     /*  99 */ "IdxRowid",
 1.22615 +     /* 100 */ "IdxLT",
 1.22616 +     /* 101 */ "IdxGE",
 1.22617 +     /* 102 */ "Destroy",
 1.22618 +     /* 103 */ "Clear",
 1.22619 +     /* 104 */ "CreateIndex",
 1.22620 +     /* 105 */ "CreateTable",
 1.22621 +     /* 106 */ "ParseSchema",
 1.22622 +     /* 107 */ "LoadAnalysis",
 1.22623 +     /* 108 */ "DropTable",
 1.22624 +     /* 109 */ "DropIndex",
 1.22625 +     /* 110 */ "DropTrigger",
 1.22626 +     /* 111 */ "IntegrityCk",
 1.22627 +     /* 112 */ "RowSetAdd",
 1.22628 +     /* 113 */ "RowSetRead",
 1.22629 +     /* 114 */ "RowSetTest",
 1.22630 +     /* 115 */ "Program",
 1.22631 +     /* 116 */ "Param",
 1.22632 +     /* 117 */ "FkCounter",
 1.22633 +     /* 118 */ "FkIfZero",
 1.22634 +     /* 119 */ "MemMax",
 1.22635 +     /* 120 */ "IfPos",
 1.22636 +     /* 121 */ "IfNeg",
 1.22637 +     /* 122 */ "IfZero",
 1.22638 +     /* 123 */ "AggStep",
 1.22639 +     /* 124 */ "AggFinal",
 1.22640 +     /* 125 */ "Checkpoint",
 1.22641 +     /* 126 */ "JournalMode",
 1.22642 +     /* 127 */ "Vacuum",
 1.22643 +     /* 128 */ "IncrVacuum",
 1.22644 +     /* 129 */ "Expire",
 1.22645 +     /* 130 */ "Real",
 1.22646 +     /* 131 */ "TableLock",
 1.22647 +     /* 132 */ "VBegin",
 1.22648 +     /* 133 */ "VCreate",
 1.22649 +     /* 134 */ "VDestroy",
 1.22650 +     /* 135 */ "VOpen",
 1.22651 +     /* 136 */ "VFilter",
 1.22652 +     /* 137 */ "VColumn",
 1.22653 +     /* 138 */ "VNext",
 1.22654 +     /* 139 */ "VRename",
 1.22655 +     /* 140 */ "VUpdate",
 1.22656 +     /* 141 */ "ToText",
 1.22657 +     /* 142 */ "ToBlob",
 1.22658 +     /* 143 */ "ToNumeric",
 1.22659 +     /* 144 */ "ToInt",
 1.22660 +     /* 145 */ "ToReal",
 1.22661 +     /* 146 */ "Pagecount",
 1.22662 +     /* 147 */ "MaxPgcnt",
 1.22663 +     /* 148 */ "Trace",
 1.22664 +     /* 149 */ "Noop",
 1.22665 +     /* 150 */ "Explain",
 1.22666 +  };
 1.22667 +  return azName[i];
 1.22668 +}
 1.22669 +#endif
 1.22670 +
 1.22671 +/************** End of opcodes.c *********************************************/
 1.22672 +/************** Begin file os_unix.c *****************************************/
 1.22673 +/*
 1.22674 +** 2004 May 22
 1.22675 +**
 1.22676 +** The author disclaims copyright to this source code.  In place of
 1.22677 +** a legal notice, here is a blessing:
 1.22678 +**
 1.22679 +**    May you do good and not evil.
 1.22680 +**    May you find forgiveness for yourself and forgive others.
 1.22681 +**    May you share freely, never taking more than you give.
 1.22682 +**
 1.22683 +******************************************************************************
 1.22684 +**
 1.22685 +** This file contains the VFS implementation for unix-like operating systems
 1.22686 +** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
 1.22687 +**
 1.22688 +** There are actually several different VFS implementations in this file.
 1.22689 +** The differences are in the way that file locking is done.  The default
 1.22690 +** implementation uses Posix Advisory Locks.  Alternative implementations
 1.22691 +** use flock(), dot-files, various proprietary locking schemas, or simply
 1.22692 +** skip locking all together.
 1.22693 +**
 1.22694 +** This source file is organized into divisions where the logic for various
 1.22695 +** subfunctions is contained within the appropriate division.  PLEASE
 1.22696 +** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
 1.22697 +** in the correct division and should be clearly labeled.
 1.22698 +**
 1.22699 +** The layout of divisions is as follows:
 1.22700 +**
 1.22701 +**   *  General-purpose declarations and utility functions.
 1.22702 +**   *  Unique file ID logic used by VxWorks.
 1.22703 +**   *  Various locking primitive implementations (all except proxy locking):
 1.22704 +**      + for Posix Advisory Locks
 1.22705 +**      + for no-op locks
 1.22706 +**      + for dot-file locks
 1.22707 +**      + for flock() locking
 1.22708 +**      + for named semaphore locks (VxWorks only)
 1.22709 +**      + for AFP filesystem locks (MacOSX only)
 1.22710 +**   *  sqlite3_file methods not associated with locking.
 1.22711 +**   *  Definitions of sqlite3_io_methods objects for all locking
 1.22712 +**      methods plus "finder" functions for each locking method.
 1.22713 +**   *  sqlite3_vfs method implementations.
 1.22714 +**   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
 1.22715 +**   *  Definitions of sqlite3_vfs objects for all locking methods
 1.22716 +**      plus implementations of sqlite3_os_init() and sqlite3_os_end().
 1.22717 +*/
 1.22718 +#if SQLITE_OS_UNIX              /* This file is used on unix only */
 1.22719 +
 1.22720 +/* Use posix_fallocate() if it is available
 1.22721 +*/
 1.22722 +#if !defined(HAVE_POSIX_FALLOCATE) \
 1.22723 +      && (_XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L)
 1.22724 +# define HAVE_POSIX_FALLOCATE 1
 1.22725 +#endif
 1.22726 +
 1.22727 +/*
 1.22728 +** There are various methods for file locking used for concurrency
 1.22729 +** control:
 1.22730 +**
 1.22731 +**   1. POSIX locking (the default),
 1.22732 +**   2. No locking,
 1.22733 +**   3. Dot-file locking,
 1.22734 +**   4. flock() locking,
 1.22735 +**   5. AFP locking (OSX only),
 1.22736 +**   6. Named POSIX semaphores (VXWorks only),
 1.22737 +**   7. proxy locking. (OSX only)
 1.22738 +**
 1.22739 +** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
 1.22740 +** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
 1.22741 +** selection of the appropriate locking style based on the filesystem
 1.22742 +** where the database is located.  
 1.22743 +*/
 1.22744 +#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
 1.22745 +#  if defined(__APPLE__)
 1.22746 +#    define SQLITE_ENABLE_LOCKING_STYLE 1
 1.22747 +#  else
 1.22748 +#    define SQLITE_ENABLE_LOCKING_STYLE 0
 1.22749 +#  endif
 1.22750 +#endif
 1.22751 +
 1.22752 +/*
 1.22753 +** Define the OS_VXWORKS pre-processor macro to 1 if building on 
 1.22754 +** vxworks, or 0 otherwise.
 1.22755 +*/
 1.22756 +#ifndef OS_VXWORKS
 1.22757 +#  if defined(__RTP__) || defined(_WRS_KERNEL)
 1.22758 +#    define OS_VXWORKS 1
 1.22759 +#  else
 1.22760 +#    define OS_VXWORKS 0
 1.22761 +#  endif
 1.22762 +#endif
 1.22763 +
 1.22764 +/*
 1.22765 +** These #defines should enable >2GB file support on Posix if the
 1.22766 +** underlying operating system supports it.  If the OS lacks
 1.22767 +** large file support, these should be no-ops.
 1.22768 +**
 1.22769 +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
 1.22770 +** on the compiler command line.  This is necessary if you are compiling
 1.22771 +** on a recent machine (ex: RedHat 7.2) but you want your code to work
 1.22772 +** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
 1.22773 +** without this option, LFS is enable.  But LFS does not exist in the kernel
 1.22774 +** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
 1.22775 +** portability you should omit LFS.
 1.22776 +**
 1.22777 +** The previous paragraph was written in 2005.  (This paragraph is written
 1.22778 +** on 2008-11-28.) These days, all Linux kernels support large files, so
 1.22779 +** you should probably leave LFS enabled.  But some embedded platforms might
 1.22780 +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
 1.22781 +*/
 1.22782 +#ifndef SQLITE_DISABLE_LFS
 1.22783 +# define _LARGE_FILE       1
 1.22784 +# ifndef _FILE_OFFSET_BITS
 1.22785 +#   define _FILE_OFFSET_BITS 64
 1.22786 +# endif
 1.22787 +# define _LARGEFILE_SOURCE 1
 1.22788 +#endif
 1.22789 +
 1.22790 +/*
 1.22791 +** standard include files.
 1.22792 +*/
 1.22793 +#include <sys/types.h>
 1.22794 +#include <sys/stat.h>
 1.22795 +#include <fcntl.h>
 1.22796 +#include <unistd.h>
 1.22797 +/* #include <time.h> */
 1.22798 +#include <sys/time.h>
 1.22799 +#include <errno.h>
 1.22800 +#ifndef SQLITE_OMIT_WAL
 1.22801 +#include <sys/mman.h>
 1.22802 +#endif
 1.22803 +
 1.22804 +
 1.22805 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.22806 +# include <sys/ioctl.h>
 1.22807 +# if OS_VXWORKS
 1.22808 +#  include <semaphore.h>
 1.22809 +#  include <limits.h>
 1.22810 +# else
 1.22811 +#  include <sys/file.h>
 1.22812 +#  include <sys/param.h>
 1.22813 +# endif
 1.22814 +#endif /* SQLITE_ENABLE_LOCKING_STYLE */
 1.22815 +
 1.22816 +#if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
 1.22817 +# include <sys/mount.h>
 1.22818 +#endif
 1.22819 +
 1.22820 +#ifdef HAVE_UTIME
 1.22821 +# include <utime.h>
 1.22822 +#endif
 1.22823 +
 1.22824 +/*
 1.22825 +** Allowed values of unixFile.fsFlags
 1.22826 +*/
 1.22827 +#define SQLITE_FSFLAGS_IS_MSDOS     0x1
 1.22828 +
 1.22829 +/*
 1.22830 +** If we are to be thread-safe, include the pthreads header and define
 1.22831 +** the SQLITE_UNIX_THREADS macro.
 1.22832 +*/
 1.22833 +#if SQLITE_THREADSAFE
 1.22834 +/* # include <pthread.h> */
 1.22835 +# define SQLITE_UNIX_THREADS 1
 1.22836 +#endif
 1.22837 +
 1.22838 +/*
 1.22839 +** Default permissions when creating a new file
 1.22840 +*/
 1.22841 +#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 1.22842 +# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
 1.22843 +#endif
 1.22844 +
 1.22845 +/*
 1.22846 +** Default permissions when creating auto proxy dir
 1.22847 +*/
 1.22848 +#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 1.22849 +# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
 1.22850 +#endif
 1.22851 +
 1.22852 +/*
 1.22853 +** Maximum supported path-length.
 1.22854 +*/
 1.22855 +#define MAX_PATHNAME 512
 1.22856 +
 1.22857 +/*
 1.22858 +** Only set the lastErrno if the error code is a real error and not 
 1.22859 +** a normal expected return code of SQLITE_BUSY or SQLITE_OK
 1.22860 +*/
 1.22861 +#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
 1.22862 +
 1.22863 +/* Forward references */
 1.22864 +typedef struct unixShm unixShm;               /* Connection shared memory */
 1.22865 +typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
 1.22866 +typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
 1.22867 +typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
 1.22868 +
 1.22869 +/*
 1.22870 +** Sometimes, after a file handle is closed by SQLite, the file descriptor
 1.22871 +** cannot be closed immediately. In these cases, instances of the following
 1.22872 +** structure are used to store the file descriptor while waiting for an
 1.22873 +** opportunity to either close or reuse it.
 1.22874 +*/
 1.22875 +struct UnixUnusedFd {
 1.22876 +  int fd;                   /* File descriptor to close */
 1.22877 +  int flags;                /* Flags this file descriptor was opened with */
 1.22878 +  UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
 1.22879 +};
 1.22880 +
 1.22881 +/*
 1.22882 +** The unixFile structure is subclass of sqlite3_file specific to the unix
 1.22883 +** VFS implementations.
 1.22884 +*/
 1.22885 +typedef struct unixFile unixFile;
 1.22886 +struct unixFile {
 1.22887 +  sqlite3_io_methods const *pMethod;  /* Always the first entry */
 1.22888 +  sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
 1.22889 +  unixInodeInfo *pInode;              /* Info about locks on this inode */
 1.22890 +  int h;                              /* The file descriptor */
 1.22891 +  unsigned char eFileLock;            /* The type of lock held on this fd */
 1.22892 +  unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
 1.22893 +  int lastErrno;                      /* The unix errno from last I/O error */
 1.22894 +  void *lockingContext;               /* Locking style specific state */
 1.22895 +  UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
 1.22896 +  const char *zPath;                  /* Name of the file */
 1.22897 +  unixShm *pShm;                      /* Shared memory segment information */
 1.22898 +  int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
 1.22899 +#ifdef __QNXNTO__
 1.22900 +  int sectorSize;                     /* Device sector size */
 1.22901 +  int deviceCharacteristics;          /* Precomputed device characteristics */
 1.22902 +#endif
 1.22903 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.22904 +  int openFlags;                      /* The flags specified at open() */
 1.22905 +#endif
 1.22906 +#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 1.22907 +  unsigned fsFlags;                   /* cached details from statfs() */
 1.22908 +#endif
 1.22909 +#if OS_VXWORKS
 1.22910 +  struct vxworksFileId *pId;          /* Unique file ID */
 1.22911 +#endif
 1.22912 +#ifdef SQLITE_DEBUG
 1.22913 +  /* The next group of variables are used to track whether or not the
 1.22914 +  ** transaction counter in bytes 24-27 of database files are updated
 1.22915 +  ** whenever any part of the database changes.  An assertion fault will
 1.22916 +  ** occur if a file is updated without also updating the transaction
 1.22917 +  ** counter.  This test is made to avoid new problems similar to the
 1.22918 +  ** one described by ticket #3584. 
 1.22919 +  */
 1.22920 +  unsigned char transCntrChng;   /* True if the transaction counter changed */
 1.22921 +  unsigned char dbUpdate;        /* True if any part of database file changed */
 1.22922 +  unsigned char inNormalWrite;   /* True if in a normal write operation */
 1.22923 +#endif
 1.22924 +#ifdef SQLITE_TEST
 1.22925 +  /* In test mode, increase the size of this structure a bit so that 
 1.22926 +  ** it is larger than the struct CrashFile defined in test6.c.
 1.22927 +  */
 1.22928 +  char aPadding[32];
 1.22929 +#endif
 1.22930 +};
 1.22931 +
 1.22932 +/*
 1.22933 +** Allowed values for the unixFile.ctrlFlags bitmask:
 1.22934 +*/
 1.22935 +#define UNIXFILE_EXCL        0x01     /* Connections from one process only */
 1.22936 +#define UNIXFILE_RDONLY      0x02     /* Connection is read only */
 1.22937 +#define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
 1.22938 +#ifndef SQLITE_DISABLE_DIRSYNC
 1.22939 +# define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
 1.22940 +#else
 1.22941 +# define UNIXFILE_DIRSYNC    0x00
 1.22942 +#endif
 1.22943 +#define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
 1.22944 +#define UNIXFILE_DELETE      0x20     /* Delete on close */
 1.22945 +#define UNIXFILE_URI         0x40     /* Filename might have query parameters */
 1.22946 +#define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
 1.22947 +
 1.22948 +/*
 1.22949 +** Include code that is common to all os_*.c files
 1.22950 +*/
 1.22951 +/************** Include os_common.h in the middle of os_unix.c ***************/
 1.22952 +/************** Begin file os_common.h ***************************************/
 1.22953 +/*
 1.22954 +** 2004 May 22
 1.22955 +**
 1.22956 +** The author disclaims copyright to this source code.  In place of
 1.22957 +** a legal notice, here is a blessing:
 1.22958 +**
 1.22959 +**    May you do good and not evil.
 1.22960 +**    May you find forgiveness for yourself and forgive others.
 1.22961 +**    May you share freely, never taking more than you give.
 1.22962 +**
 1.22963 +******************************************************************************
 1.22964 +**
 1.22965 +** This file contains macros and a little bit of code that is common to
 1.22966 +** all of the platform-specific files (os_*.c) and is #included into those
 1.22967 +** files.
 1.22968 +**
 1.22969 +** This file should be #included by the os_*.c files only.  It is not a
 1.22970 +** general purpose header file.
 1.22971 +*/
 1.22972 +#ifndef _OS_COMMON_H_
 1.22973 +#define _OS_COMMON_H_
 1.22974 +
 1.22975 +/*
 1.22976 +** At least two bugs have slipped in because we changed the MEMORY_DEBUG
 1.22977 +** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 1.22978 +** switch.  The following code should catch this problem at compile-time.
 1.22979 +*/
 1.22980 +#ifdef MEMORY_DEBUG
 1.22981 +# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 1.22982 +#endif
 1.22983 +
 1.22984 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.22985 +# ifndef SQLITE_DEBUG_OS_TRACE
 1.22986 +#   define SQLITE_DEBUG_OS_TRACE 0
 1.22987 +# endif
 1.22988 +  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 1.22989 +# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 1.22990 +#else
 1.22991 +# define OSTRACE(X)
 1.22992 +#endif
 1.22993 +
 1.22994 +/*
 1.22995 +** Macros for performance tracing.  Normally turned off.  Only works
 1.22996 +** on i486 hardware.
 1.22997 +*/
 1.22998 +#ifdef SQLITE_PERFORMANCE_TRACE
 1.22999 +
 1.23000 +/* 
 1.23001 +** hwtime.h contains inline assembler code for implementing 
 1.23002 +** high-performance timing routines.
 1.23003 +*/
 1.23004 +/************** Include hwtime.h in the middle of os_common.h ****************/
 1.23005 +/************** Begin file hwtime.h ******************************************/
 1.23006 +/*
 1.23007 +** 2008 May 27
 1.23008 +**
 1.23009 +** The author disclaims copyright to this source code.  In place of
 1.23010 +** a legal notice, here is a blessing:
 1.23011 +**
 1.23012 +**    May you do good and not evil.
 1.23013 +**    May you find forgiveness for yourself and forgive others.
 1.23014 +**    May you share freely, never taking more than you give.
 1.23015 +**
 1.23016 +******************************************************************************
 1.23017 +**
 1.23018 +** This file contains inline asm code for retrieving "high-performance"
 1.23019 +** counters for x86 class CPUs.
 1.23020 +*/
 1.23021 +#ifndef _HWTIME_H_
 1.23022 +#define _HWTIME_H_
 1.23023 +
 1.23024 +/*
 1.23025 +** The following routine only works on pentium-class (or newer) processors.
 1.23026 +** It uses the RDTSC opcode to read the cycle count value out of the
 1.23027 +** processor and returns that value.  This can be used for high-res
 1.23028 +** profiling.
 1.23029 +*/
 1.23030 +#if (defined(__GNUC__) || defined(_MSC_VER)) && \
 1.23031 +      (defined(i386) || defined(__i386__) || defined(_M_IX86))
 1.23032 +
 1.23033 +  #if defined(__GNUC__)
 1.23034 +
 1.23035 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.23036 +     unsigned int lo, hi;
 1.23037 +     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 1.23038 +     return (sqlite_uint64)hi << 32 | lo;
 1.23039 +  }
 1.23040 +
 1.23041 +  #elif defined(_MSC_VER)
 1.23042 +
 1.23043 +  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 1.23044 +     __asm {
 1.23045 +        rdtsc
 1.23046 +        ret       ; return value at EDX:EAX
 1.23047 +     }
 1.23048 +  }
 1.23049 +
 1.23050 +  #endif
 1.23051 +
 1.23052 +#elif (defined(__GNUC__) && defined(__x86_64__))
 1.23053 +
 1.23054 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.23055 +      unsigned long val;
 1.23056 +      __asm__ __volatile__ ("rdtsc" : "=A" (val));
 1.23057 +      return val;
 1.23058 +  }
 1.23059 + 
 1.23060 +#elif (defined(__GNUC__) && defined(__ppc__))
 1.23061 +
 1.23062 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.23063 +      unsigned long long retval;
 1.23064 +      unsigned long junk;
 1.23065 +      __asm__ __volatile__ ("\n\
 1.23066 +          1:      mftbu   %1\n\
 1.23067 +                  mftb    %L0\n\
 1.23068 +                  mftbu   %0\n\
 1.23069 +                  cmpw    %0,%1\n\
 1.23070 +                  bne     1b"
 1.23071 +                  : "=r" (retval), "=r" (junk));
 1.23072 +      return retval;
 1.23073 +  }
 1.23074 +
 1.23075 +#else
 1.23076 +
 1.23077 +  #error Need implementation of sqlite3Hwtime() for your platform.
 1.23078 +
 1.23079 +  /*
 1.23080 +  ** To compile without implementing sqlite3Hwtime() for your platform,
 1.23081 +  ** you can remove the above #error and use the following
 1.23082 +  ** stub function.  You will lose timing support for many
 1.23083 +  ** of the debugging and testing utilities, but it should at
 1.23084 +  ** least compile and run.
 1.23085 +  */
 1.23086 +SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 1.23087 +
 1.23088 +#endif
 1.23089 +
 1.23090 +#endif /* !defined(_HWTIME_H_) */
 1.23091 +
 1.23092 +/************** End of hwtime.h **********************************************/
 1.23093 +/************** Continuing where we left off in os_common.h ******************/
 1.23094 +
 1.23095 +static sqlite_uint64 g_start;
 1.23096 +static sqlite_uint64 g_elapsed;
 1.23097 +#define TIMER_START       g_start=sqlite3Hwtime()
 1.23098 +#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
 1.23099 +#define TIMER_ELAPSED     g_elapsed
 1.23100 +#else
 1.23101 +#define TIMER_START
 1.23102 +#define TIMER_END
 1.23103 +#define TIMER_ELAPSED     ((sqlite_uint64)0)
 1.23104 +#endif
 1.23105 +
 1.23106 +/*
 1.23107 +** If we compile with the SQLITE_TEST macro set, then the following block
 1.23108 +** of code will give us the ability to simulate a disk I/O error.  This
 1.23109 +** is used for testing the I/O recovery logic.
 1.23110 +*/
 1.23111 +#ifdef SQLITE_TEST
 1.23112 +SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
 1.23113 +SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
 1.23114 +SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
 1.23115 +SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
 1.23116 +SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
 1.23117 +SQLITE_API int sqlite3_diskfull_pending = 0;
 1.23118 +SQLITE_API int sqlite3_diskfull = 0;
 1.23119 +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
 1.23120 +#define SimulateIOError(CODE)  \
 1.23121 +  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
 1.23122 +       || sqlite3_io_error_pending-- == 1 )  \
 1.23123 +              { local_ioerr(); CODE; }
 1.23124 +static void local_ioerr(){
 1.23125 +  IOTRACE(("IOERR\n"));
 1.23126 +  sqlite3_io_error_hit++;
 1.23127 +  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
 1.23128 +}
 1.23129 +#define SimulateDiskfullError(CODE) \
 1.23130 +   if( sqlite3_diskfull_pending ){ \
 1.23131 +     if( sqlite3_diskfull_pending == 1 ){ \
 1.23132 +       local_ioerr(); \
 1.23133 +       sqlite3_diskfull = 1; \
 1.23134 +       sqlite3_io_error_hit = 1; \
 1.23135 +       CODE; \
 1.23136 +     }else{ \
 1.23137 +       sqlite3_diskfull_pending--; \
 1.23138 +     } \
 1.23139 +   }
 1.23140 +#else
 1.23141 +#define SimulateIOErrorBenign(X)
 1.23142 +#define SimulateIOError(A)
 1.23143 +#define SimulateDiskfullError(A)
 1.23144 +#endif
 1.23145 +
 1.23146 +/*
 1.23147 +** When testing, keep a count of the number of open files.
 1.23148 +*/
 1.23149 +#ifdef SQLITE_TEST
 1.23150 +SQLITE_API int sqlite3_open_file_count = 0;
 1.23151 +#define OpenCounter(X)  sqlite3_open_file_count+=(X)
 1.23152 +#else
 1.23153 +#define OpenCounter(X)
 1.23154 +#endif
 1.23155 +
 1.23156 +#endif /* !defined(_OS_COMMON_H_) */
 1.23157 +
 1.23158 +/************** End of os_common.h *******************************************/
 1.23159 +/************** Continuing where we left off in os_unix.c ********************/
 1.23160 +
 1.23161 +/*
 1.23162 +** Define various macros that are missing from some systems.
 1.23163 +*/
 1.23164 +#ifndef O_LARGEFILE
 1.23165 +# define O_LARGEFILE 0
 1.23166 +#endif
 1.23167 +#ifdef SQLITE_DISABLE_LFS
 1.23168 +# undef O_LARGEFILE
 1.23169 +# define O_LARGEFILE 0
 1.23170 +#endif
 1.23171 +#ifndef O_NOFOLLOW
 1.23172 +# define O_NOFOLLOW 0
 1.23173 +#endif
 1.23174 +#ifndef O_BINARY
 1.23175 +# define O_BINARY 0
 1.23176 +#endif
 1.23177 +
 1.23178 +/*
 1.23179 +** The threadid macro resolves to the thread-id or to 0.  Used for
 1.23180 +** testing and debugging only.
 1.23181 +*/
 1.23182 +#if SQLITE_THREADSAFE
 1.23183 +#define threadid pthread_self()
 1.23184 +#else
 1.23185 +#define threadid 0
 1.23186 +#endif
 1.23187 +
 1.23188 +/*
 1.23189 +** Different Unix systems declare open() in different ways.  Same use
 1.23190 +** open(const char*,int,mode_t).  Others use open(const char*,int,...).
 1.23191 +** The difference is important when using a pointer to the function.
 1.23192 +**
 1.23193 +** The safest way to deal with the problem is to always use this wrapper
 1.23194 +** which always has the same well-defined interface.
 1.23195 +*/
 1.23196 +static int posixOpen(const char *zFile, int flags, int mode){
 1.23197 +  return open(zFile, flags, mode);
 1.23198 +}
 1.23199 +
 1.23200 +/*
 1.23201 +** On some systems, calls to fchown() will trigger a message in a security
 1.23202 +** log if they come from non-root processes.  So avoid calling fchown() if
 1.23203 +** we are not running as root.
 1.23204 +*/
 1.23205 +static int posixFchown(int fd, uid_t uid, gid_t gid){
 1.23206 +  return geteuid() ? 0 : fchown(fd,uid,gid);
 1.23207 +}
 1.23208 +
 1.23209 +/* Forward reference */
 1.23210 +static int openDirectory(const char*, int*);
 1.23211 +
 1.23212 +/*
 1.23213 +** Many system calls are accessed through pointer-to-functions so that
 1.23214 +** they may be overridden at runtime to facilitate fault injection during
 1.23215 +** testing and sandboxing.  The following array holds the names and pointers
 1.23216 +** to all overrideable system calls.
 1.23217 +*/
 1.23218 +static struct unix_syscall {
 1.23219 +  const char *zName;            /* Name of the sytem call */
 1.23220 +  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
 1.23221 +  sqlite3_syscall_ptr pDefault; /* Default value */
 1.23222 +} aSyscall[] = {
 1.23223 +  { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
 1.23224 +#define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
 1.23225 +
 1.23226 +  { "close",        (sqlite3_syscall_ptr)close,      0  },
 1.23227 +#define osClose     ((int(*)(int))aSyscall[1].pCurrent)
 1.23228 +
 1.23229 +  { "access",       (sqlite3_syscall_ptr)access,     0  },
 1.23230 +#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
 1.23231 +
 1.23232 +  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
 1.23233 +#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
 1.23234 +
 1.23235 +  { "stat",         (sqlite3_syscall_ptr)stat,       0  },
 1.23236 +#define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
 1.23237 +
 1.23238 +/*
 1.23239 +** The DJGPP compiler environment looks mostly like Unix, but it
 1.23240 +** lacks the fcntl() system call.  So redefine fcntl() to be something
 1.23241 +** that always succeeds.  This means that locking does not occur under
 1.23242 +** DJGPP.  But it is DOS - what did you expect?
 1.23243 +*/
 1.23244 +#ifdef __DJGPP__
 1.23245 +  { "fstat",        0,                 0  },
 1.23246 +#define osFstat(a,b,c)    0
 1.23247 +#else     
 1.23248 +  { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
 1.23249 +#define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
 1.23250 +#endif
 1.23251 +
 1.23252 +  { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
 1.23253 +#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
 1.23254 +
 1.23255 +  { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
 1.23256 +#define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
 1.23257 +
 1.23258 +  { "read",         (sqlite3_syscall_ptr)read,       0  },
 1.23259 +#define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
 1.23260 +
 1.23261 +#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
 1.23262 +  { "pread",        (sqlite3_syscall_ptr)pread,      0  },
 1.23263 +#else
 1.23264 +  { "pread",        (sqlite3_syscall_ptr)0,          0  },
 1.23265 +#endif
 1.23266 +#define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
 1.23267 +
 1.23268 +#if defined(USE_PREAD64)
 1.23269 +  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
 1.23270 +#else
 1.23271 +  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
 1.23272 +#endif
 1.23273 +#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
 1.23274 +
 1.23275 +  { "write",        (sqlite3_syscall_ptr)write,      0  },
 1.23276 +#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
 1.23277 +
 1.23278 +#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
 1.23279 +  { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
 1.23280 +#else
 1.23281 +  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
 1.23282 +#endif
 1.23283 +#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
 1.23284 +                    aSyscall[12].pCurrent)
 1.23285 +
 1.23286 +#if defined(USE_PREAD64)
 1.23287 +  { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
 1.23288 +#else
 1.23289 +  { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
 1.23290 +#endif
 1.23291 +#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
 1.23292 +                    aSyscall[13].pCurrent)
 1.23293 +
 1.23294 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.23295 +  { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
 1.23296 +#else
 1.23297 +  { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
 1.23298 +#endif
 1.23299 +#define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
 1.23300 +
 1.23301 +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 1.23302 +  { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
 1.23303 +#else
 1.23304 +  { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
 1.23305 +#endif
 1.23306 +#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
 1.23307 +
 1.23308 +  { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
 1.23309 +#define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
 1.23310 +
 1.23311 +  { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
 1.23312 +#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
 1.23313 +
 1.23314 +  { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
 1.23315 +#define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
 1.23316 +
 1.23317 +  { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
 1.23318 +#define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
 1.23319 +
 1.23320 +  { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
 1.23321 +#define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 1.23322 +
 1.23323 +  { "umask",        (sqlite3_syscall_ptr)umask,           0 },
 1.23324 +#define osUmask     ((mode_t(*)(mode_t))aSyscall[21].pCurrent)
 1.23325 +
 1.23326 +}; /* End of the overrideable system calls */
 1.23327 +
 1.23328 +/*
 1.23329 +** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 1.23330 +** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
 1.23331 +** system call pointer, or SQLITE_NOTFOUND if there is no configurable
 1.23332 +** system call named zName.
 1.23333 +*/
 1.23334 +static int unixSetSystemCall(
 1.23335 +  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
 1.23336 +  const char *zName,            /* Name of system call to override */
 1.23337 +  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
 1.23338 +){
 1.23339 +  unsigned int i;
 1.23340 +  int rc = SQLITE_NOTFOUND;
 1.23341 +
 1.23342 +  UNUSED_PARAMETER(pNotUsed);
 1.23343 +  if( zName==0 ){
 1.23344 +    /* If no zName is given, restore all system calls to their default
 1.23345 +    ** settings and return NULL
 1.23346 +    */
 1.23347 +    rc = SQLITE_OK;
 1.23348 +    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 1.23349 +      if( aSyscall[i].pDefault ){
 1.23350 +        aSyscall[i].pCurrent = aSyscall[i].pDefault;
 1.23351 +      }
 1.23352 +    }
 1.23353 +  }else{
 1.23354 +    /* If zName is specified, operate on only the one system call
 1.23355 +    ** specified.
 1.23356 +    */
 1.23357 +    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 1.23358 +      if( strcmp(zName, aSyscall[i].zName)==0 ){
 1.23359 +        if( aSyscall[i].pDefault==0 ){
 1.23360 +          aSyscall[i].pDefault = aSyscall[i].pCurrent;
 1.23361 +        }
 1.23362 +        rc = SQLITE_OK;
 1.23363 +        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
 1.23364 +        aSyscall[i].pCurrent = pNewFunc;
 1.23365 +        break;
 1.23366 +      }
 1.23367 +    }
 1.23368 +  }
 1.23369 +  return rc;
 1.23370 +}
 1.23371 +
 1.23372 +/*
 1.23373 +** Return the value of a system call.  Return NULL if zName is not a
 1.23374 +** recognized system call name.  NULL is also returned if the system call
 1.23375 +** is currently undefined.
 1.23376 +*/
 1.23377 +static sqlite3_syscall_ptr unixGetSystemCall(
 1.23378 +  sqlite3_vfs *pNotUsed,
 1.23379 +  const char *zName
 1.23380 +){
 1.23381 +  unsigned int i;
 1.23382 +
 1.23383 +  UNUSED_PARAMETER(pNotUsed);
 1.23384 +  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 1.23385 +    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
 1.23386 +  }
 1.23387 +  return 0;
 1.23388 +}
 1.23389 +
 1.23390 +/*
 1.23391 +** Return the name of the first system call after zName.  If zName==NULL
 1.23392 +** then return the name of the first system call.  Return NULL if zName
 1.23393 +** is the last system call or if zName is not the name of a valid
 1.23394 +** system call.
 1.23395 +*/
 1.23396 +static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
 1.23397 +  int i = -1;
 1.23398 +
 1.23399 +  UNUSED_PARAMETER(p);
 1.23400 +  if( zName ){
 1.23401 +    for(i=0; i<ArraySize(aSyscall)-1; i++){
 1.23402 +      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
 1.23403 +    }
 1.23404 +  }
 1.23405 +  for(i++; i<ArraySize(aSyscall); i++){
 1.23406 +    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
 1.23407 +  }
 1.23408 +  return 0;
 1.23409 +}
 1.23410 +
 1.23411 +/*
 1.23412 +** Invoke open().  Do so multiple times, until it either succeeds or
 1.23413 +** fails for some reason other than EINTR.
 1.23414 +**
 1.23415 +** If the file creation mode "m" is 0 then set it to the default for
 1.23416 +** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
 1.23417 +** 0644) as modified by the system umask.  If m is not 0, then
 1.23418 +** make the file creation mode be exactly m ignoring the umask.
 1.23419 +**
 1.23420 +** The m parameter will be non-zero only when creating -wal, -journal,
 1.23421 +** and -shm files.  We want those files to have *exactly* the same
 1.23422 +** permissions as their original database, unadulterated by the umask.
 1.23423 +** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
 1.23424 +** transaction crashes and leaves behind hot journals, then any
 1.23425 +** process that is able to write to the database will also be able to
 1.23426 +** recover the hot journals.
 1.23427 +*/
 1.23428 +static int robust_open(const char *z, int f, mode_t m){
 1.23429 +  int fd;
 1.23430 +  mode_t m2;
 1.23431 +  mode_t origM = 0;
 1.23432 +  if( m==0 ){
 1.23433 +    m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
 1.23434 +  }else{
 1.23435 +    m2 = m;
 1.23436 +    origM = osUmask(0);
 1.23437 +  }
 1.23438 +  do{
 1.23439 +#if defined(O_CLOEXEC)
 1.23440 +    fd = osOpen(z,f|O_CLOEXEC,m2);
 1.23441 +#else
 1.23442 +    fd = osOpen(z,f,m2);
 1.23443 +#endif
 1.23444 +  }while( fd<0 && errno==EINTR );
 1.23445 +  if( m ){
 1.23446 +    osUmask(origM);
 1.23447 +  }
 1.23448 +#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
 1.23449 +  if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 1.23450 +#endif
 1.23451 +  return fd;
 1.23452 +}
 1.23453 +
 1.23454 +/*
 1.23455 +** Helper functions to obtain and relinquish the global mutex. The
 1.23456 +** global mutex is used to protect the unixInodeInfo and
 1.23457 +** vxworksFileId objects used by this file, all of which may be 
 1.23458 +** shared by multiple threads.
 1.23459 +**
 1.23460 +** Function unixMutexHeld() is used to assert() that the global mutex 
 1.23461 +** is held when required. This function is only used as part of assert() 
 1.23462 +** statements. e.g.
 1.23463 +**
 1.23464 +**   unixEnterMutex()
 1.23465 +**     assert( unixMutexHeld() );
 1.23466 +**   unixEnterLeave()
 1.23467 +*/
 1.23468 +static void unixEnterMutex(void){
 1.23469 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.23470 +}
 1.23471 +static void unixLeaveMutex(void){
 1.23472 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.23473 +}
 1.23474 +#ifdef SQLITE_DEBUG
 1.23475 +static int unixMutexHeld(void) {
 1.23476 +  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.23477 +}
 1.23478 +#endif
 1.23479 +
 1.23480 +
 1.23481 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.23482 +/*
 1.23483 +** Helper function for printing out trace information from debugging
 1.23484 +** binaries. This returns the string represetation of the supplied
 1.23485 +** integer lock-type.
 1.23486 +*/
 1.23487 +static const char *azFileLock(int eFileLock){
 1.23488 +  switch( eFileLock ){
 1.23489 +    case NO_LOCK: return "NONE";
 1.23490 +    case SHARED_LOCK: return "SHARED";
 1.23491 +    case RESERVED_LOCK: return "RESERVED";
 1.23492 +    case PENDING_LOCK: return "PENDING";
 1.23493 +    case EXCLUSIVE_LOCK: return "EXCLUSIVE";
 1.23494 +  }
 1.23495 +  return "ERROR";
 1.23496 +}
 1.23497 +#endif
 1.23498 +
 1.23499 +#ifdef SQLITE_LOCK_TRACE
 1.23500 +/*
 1.23501 +** Print out information about all locking operations.
 1.23502 +**
 1.23503 +** This routine is used for troubleshooting locks on multithreaded
 1.23504 +** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
 1.23505 +** command-line option on the compiler.  This code is normally
 1.23506 +** turned off.
 1.23507 +*/
 1.23508 +static int lockTrace(int fd, int op, struct flock *p){
 1.23509 +  char *zOpName, *zType;
 1.23510 +  int s;
 1.23511 +  int savedErrno;
 1.23512 +  if( op==F_GETLK ){
 1.23513 +    zOpName = "GETLK";
 1.23514 +  }else if( op==F_SETLK ){
 1.23515 +    zOpName = "SETLK";
 1.23516 +  }else{
 1.23517 +    s = osFcntl(fd, op, p);
 1.23518 +    sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
 1.23519 +    return s;
 1.23520 +  }
 1.23521 +  if( p->l_type==F_RDLCK ){
 1.23522 +    zType = "RDLCK";
 1.23523 +  }else if( p->l_type==F_WRLCK ){
 1.23524 +    zType = "WRLCK";
 1.23525 +  }else if( p->l_type==F_UNLCK ){
 1.23526 +    zType = "UNLCK";
 1.23527 +  }else{
 1.23528 +    assert( 0 );
 1.23529 +  }
 1.23530 +  assert( p->l_whence==SEEK_SET );
 1.23531 +  s = osFcntl(fd, op, p);
 1.23532 +  savedErrno = errno;
 1.23533 +  sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
 1.23534 +     threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
 1.23535 +     (int)p->l_pid, s);
 1.23536 +  if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
 1.23537 +    struct flock l2;
 1.23538 +    l2 = *p;
 1.23539 +    osFcntl(fd, F_GETLK, &l2);
 1.23540 +    if( l2.l_type==F_RDLCK ){
 1.23541 +      zType = "RDLCK";
 1.23542 +    }else if( l2.l_type==F_WRLCK ){
 1.23543 +      zType = "WRLCK";
 1.23544 +    }else if( l2.l_type==F_UNLCK ){
 1.23545 +      zType = "UNLCK";
 1.23546 +    }else{
 1.23547 +      assert( 0 );
 1.23548 +    }
 1.23549 +    sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
 1.23550 +       zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
 1.23551 +  }
 1.23552 +  errno = savedErrno;
 1.23553 +  return s;
 1.23554 +}
 1.23555 +#undef osFcntl
 1.23556 +#define osFcntl lockTrace
 1.23557 +#endif /* SQLITE_LOCK_TRACE */
 1.23558 +
 1.23559 +/*
 1.23560 +** Retry ftruncate() calls that fail due to EINTR
 1.23561 +*/
 1.23562 +static int robust_ftruncate(int h, sqlite3_int64 sz){
 1.23563 +  int rc;
 1.23564 +  do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
 1.23565 +  return rc;
 1.23566 +}
 1.23567 +
 1.23568 +/*
 1.23569 +** This routine translates a standard POSIX errno code into something
 1.23570 +** useful to the clients of the sqlite3 functions.  Specifically, it is
 1.23571 +** intended to translate a variety of "try again" errors into SQLITE_BUSY
 1.23572 +** and a variety of "please close the file descriptor NOW" errors into 
 1.23573 +** SQLITE_IOERR
 1.23574 +** 
 1.23575 +** Errors during initialization of locks, or file system support for locks,
 1.23576 +** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
 1.23577 +*/
 1.23578 +static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
 1.23579 +  switch (posixError) {
 1.23580 +#if 0
 1.23581 +  /* At one point this code was not commented out. In theory, this branch
 1.23582 +  ** should never be hit, as this function should only be called after
 1.23583 +  ** a locking-related function (i.e. fcntl()) has returned non-zero with
 1.23584 +  ** the value of errno as the first argument. Since a system call has failed,
 1.23585 +  ** errno should be non-zero.
 1.23586 +  **
 1.23587 +  ** Despite this, if errno really is zero, we still don't want to return
 1.23588 +  ** SQLITE_OK. The system call failed, and *some* SQLite error should be
 1.23589 +  ** propagated back to the caller. Commenting this branch out means errno==0
 1.23590 +  ** will be handled by the "default:" case below.
 1.23591 +  */
 1.23592 +  case 0: 
 1.23593 +    return SQLITE_OK;
 1.23594 +#endif
 1.23595 +
 1.23596 +  case EAGAIN:
 1.23597 +  case ETIMEDOUT:
 1.23598 +  case EBUSY:
 1.23599 +  case EINTR:
 1.23600 +  case ENOLCK:  
 1.23601 +    /* random NFS retry error, unless during file system support 
 1.23602 +     * introspection, in which it actually means what it says */
 1.23603 +    return SQLITE_BUSY;
 1.23604 +    
 1.23605 +  case EACCES: 
 1.23606 +    /* EACCES is like EAGAIN during locking operations, but not any other time*/
 1.23607 +    if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
 1.23608 +        (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
 1.23609 +        (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
 1.23610 +        (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
 1.23611 +      return SQLITE_BUSY;
 1.23612 +    }
 1.23613 +    /* else fall through */
 1.23614 +  case EPERM: 
 1.23615 +    return SQLITE_PERM;
 1.23616 +    
 1.23617 +  /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
 1.23618 +  ** this module never makes such a call. And the code in SQLite itself 
 1.23619 +  ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
 1.23620 +  ** this case is also commented out. If the system does set errno to EDEADLK,
 1.23621 +  ** the default SQLITE_IOERR_XXX code will be returned. */
 1.23622 +#if 0
 1.23623 +  case EDEADLK:
 1.23624 +    return SQLITE_IOERR_BLOCKED;
 1.23625 +#endif
 1.23626 +    
 1.23627 +#if EOPNOTSUPP!=ENOTSUP
 1.23628 +  case EOPNOTSUPP: 
 1.23629 +    /* something went terribly awry, unless during file system support 
 1.23630 +     * introspection, in which it actually means what it says */
 1.23631 +#endif
 1.23632 +#ifdef ENOTSUP
 1.23633 +  case ENOTSUP: 
 1.23634 +    /* invalid fd, unless during file system support introspection, in which 
 1.23635 +     * it actually means what it says */
 1.23636 +#endif
 1.23637 +  case EIO:
 1.23638 +  case EBADF:
 1.23639 +  case EINVAL:
 1.23640 +  case ENOTCONN:
 1.23641 +  case ENODEV:
 1.23642 +  case ENXIO:
 1.23643 +  case ENOENT:
 1.23644 +#ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
 1.23645 +  case ESTALE:
 1.23646 +#endif
 1.23647 +  case ENOSYS:
 1.23648 +    /* these should force the client to close the file and reconnect */
 1.23649 +    
 1.23650 +  default: 
 1.23651 +    return sqliteIOErr;
 1.23652 +  }
 1.23653 +}
 1.23654 +
 1.23655 +
 1.23656 +
 1.23657 +/******************************************************************************
 1.23658 +****************** Begin Unique File ID Utility Used By VxWorks ***************
 1.23659 +**
 1.23660 +** On most versions of unix, we can get a unique ID for a file by concatenating
 1.23661 +** the device number and the inode number.  But this does not work on VxWorks.
 1.23662 +** On VxWorks, a unique file id must be based on the canonical filename.
 1.23663 +**
 1.23664 +** A pointer to an instance of the following structure can be used as a
 1.23665 +** unique file ID in VxWorks.  Each instance of this structure contains
 1.23666 +** a copy of the canonical filename.  There is also a reference count.  
 1.23667 +** The structure is reclaimed when the number of pointers to it drops to
 1.23668 +** zero.
 1.23669 +**
 1.23670 +** There are never very many files open at one time and lookups are not
 1.23671 +** a performance-critical path, so it is sufficient to put these
 1.23672 +** structures on a linked list.
 1.23673 +*/
 1.23674 +struct vxworksFileId {
 1.23675 +  struct vxworksFileId *pNext;  /* Next in a list of them all */
 1.23676 +  int nRef;                     /* Number of references to this one */
 1.23677 +  int nName;                    /* Length of the zCanonicalName[] string */
 1.23678 +  char *zCanonicalName;         /* Canonical filename */
 1.23679 +};
 1.23680 +
 1.23681 +#if OS_VXWORKS
 1.23682 +/* 
 1.23683 +** All unique filenames are held on a linked list headed by this
 1.23684 +** variable:
 1.23685 +*/
 1.23686 +static struct vxworksFileId *vxworksFileList = 0;
 1.23687 +
 1.23688 +/*
 1.23689 +** Simplify a filename into its canonical form
 1.23690 +** by making the following changes:
 1.23691 +**
 1.23692 +**  * removing any trailing and duplicate /
 1.23693 +**  * convert /./ into just /
 1.23694 +**  * convert /A/../ where A is any simple name into just /
 1.23695 +**
 1.23696 +** Changes are made in-place.  Return the new name length.
 1.23697 +**
 1.23698 +** The original filename is in z[0..n-1].  Return the number of
 1.23699 +** characters in the simplified name.
 1.23700 +*/
 1.23701 +static int vxworksSimplifyName(char *z, int n){
 1.23702 +  int i, j;
 1.23703 +  while( n>1 && z[n-1]=='/' ){ n--; }
 1.23704 +  for(i=j=0; i<n; i++){
 1.23705 +    if( z[i]=='/' ){
 1.23706 +      if( z[i+1]=='/' ) continue;
 1.23707 +      if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
 1.23708 +        i += 1;
 1.23709 +        continue;
 1.23710 +      }
 1.23711 +      if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
 1.23712 +        while( j>0 && z[j-1]!='/' ){ j--; }
 1.23713 +        if( j>0 ){ j--; }
 1.23714 +        i += 2;
 1.23715 +        continue;
 1.23716 +      }
 1.23717 +    }
 1.23718 +    z[j++] = z[i];
 1.23719 +  }
 1.23720 +  z[j] = 0;
 1.23721 +  return j;
 1.23722 +}
 1.23723 +
 1.23724 +/*
 1.23725 +** Find a unique file ID for the given absolute pathname.  Return
 1.23726 +** a pointer to the vxworksFileId object.  This pointer is the unique
 1.23727 +** file ID.
 1.23728 +**
 1.23729 +** The nRef field of the vxworksFileId object is incremented before
 1.23730 +** the object is returned.  A new vxworksFileId object is created
 1.23731 +** and added to the global list if necessary.
 1.23732 +**
 1.23733 +** If a memory allocation error occurs, return NULL.
 1.23734 +*/
 1.23735 +static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
 1.23736 +  struct vxworksFileId *pNew;         /* search key and new file ID */
 1.23737 +  struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
 1.23738 +  int n;                              /* Length of zAbsoluteName string */
 1.23739 +
 1.23740 +  assert( zAbsoluteName[0]=='/' );
 1.23741 +  n = (int)strlen(zAbsoluteName);
 1.23742 +  pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
 1.23743 +  if( pNew==0 ) return 0;
 1.23744 +  pNew->zCanonicalName = (char*)&pNew[1];
 1.23745 +  memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
 1.23746 +  n = vxworksSimplifyName(pNew->zCanonicalName, n);
 1.23747 +
 1.23748 +  /* Search for an existing entry that matching the canonical name.
 1.23749 +  ** If found, increment the reference count and return a pointer to
 1.23750 +  ** the existing file ID.
 1.23751 +  */
 1.23752 +  unixEnterMutex();
 1.23753 +  for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
 1.23754 +    if( pCandidate->nName==n 
 1.23755 +     && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
 1.23756 +    ){
 1.23757 +       sqlite3_free(pNew);
 1.23758 +       pCandidate->nRef++;
 1.23759 +       unixLeaveMutex();
 1.23760 +       return pCandidate;
 1.23761 +    }
 1.23762 +  }
 1.23763 +
 1.23764 +  /* No match was found.  We will make a new file ID */
 1.23765 +  pNew->nRef = 1;
 1.23766 +  pNew->nName = n;
 1.23767 +  pNew->pNext = vxworksFileList;
 1.23768 +  vxworksFileList = pNew;
 1.23769 +  unixLeaveMutex();
 1.23770 +  return pNew;
 1.23771 +}
 1.23772 +
 1.23773 +/*
 1.23774 +** Decrement the reference count on a vxworksFileId object.  Free
 1.23775 +** the object when the reference count reaches zero.
 1.23776 +*/
 1.23777 +static void vxworksReleaseFileId(struct vxworksFileId *pId){
 1.23778 +  unixEnterMutex();
 1.23779 +  assert( pId->nRef>0 );
 1.23780 +  pId->nRef--;
 1.23781 +  if( pId->nRef==0 ){
 1.23782 +    struct vxworksFileId **pp;
 1.23783 +    for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
 1.23784 +    assert( *pp==pId );
 1.23785 +    *pp = pId->pNext;
 1.23786 +    sqlite3_free(pId);
 1.23787 +  }
 1.23788 +  unixLeaveMutex();
 1.23789 +}
 1.23790 +#endif /* OS_VXWORKS */
 1.23791 +/*************** End of Unique File ID Utility Used By VxWorks ****************
 1.23792 +******************************************************************************/
 1.23793 +
 1.23794 +
 1.23795 +/******************************************************************************
 1.23796 +*************************** Posix Advisory Locking ****************************
 1.23797 +**
 1.23798 +** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
 1.23799 +** section 6.5.2.2 lines 483 through 490 specify that when a process
 1.23800 +** sets or clears a lock, that operation overrides any prior locks set
 1.23801 +** by the same process.  It does not explicitly say so, but this implies
 1.23802 +** that it overrides locks set by the same process using a different
 1.23803 +** file descriptor.  Consider this test case:
 1.23804 +**
 1.23805 +**       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
 1.23806 +**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
 1.23807 +**
 1.23808 +** Suppose ./file1 and ./file2 are really the same file (because
 1.23809 +** one is a hard or symbolic link to the other) then if you set
 1.23810 +** an exclusive lock on fd1, then try to get an exclusive lock
 1.23811 +** on fd2, it works.  I would have expected the second lock to
 1.23812 +** fail since there was already a lock on the file due to fd1.
 1.23813 +** But not so.  Since both locks came from the same process, the
 1.23814 +** second overrides the first, even though they were on different
 1.23815 +** file descriptors opened on different file names.
 1.23816 +**
 1.23817 +** This means that we cannot use POSIX locks to synchronize file access
 1.23818 +** among competing threads of the same process.  POSIX locks will work fine
 1.23819 +** to synchronize access for threads in separate processes, but not
 1.23820 +** threads within the same process.
 1.23821 +**
 1.23822 +** To work around the problem, SQLite has to manage file locks internally
 1.23823 +** on its own.  Whenever a new database is opened, we have to find the
 1.23824 +** specific inode of the database file (the inode is determined by the
 1.23825 +** st_dev and st_ino fields of the stat structure that fstat() fills in)
 1.23826 +** and check for locks already existing on that inode.  When locks are
 1.23827 +** created or removed, we have to look at our own internal record of the
 1.23828 +** locks to see if another thread has previously set a lock on that same
 1.23829 +** inode.
 1.23830 +**
 1.23831 +** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
 1.23832 +** For VxWorks, we have to use the alternative unique ID system based on
 1.23833 +** canonical filename and implemented in the previous division.)
 1.23834 +**
 1.23835 +** The sqlite3_file structure for POSIX is no longer just an integer file
 1.23836 +** descriptor.  It is now a structure that holds the integer file
 1.23837 +** descriptor and a pointer to a structure that describes the internal
 1.23838 +** locks on the corresponding inode.  There is one locking structure
 1.23839 +** per inode, so if the same inode is opened twice, both unixFile structures
 1.23840 +** point to the same locking structure.  The locking structure keeps
 1.23841 +** a reference count (so we will know when to delete it) and a "cnt"
 1.23842 +** field that tells us its internal lock status.  cnt==0 means the
 1.23843 +** file is unlocked.  cnt==-1 means the file has an exclusive lock.
 1.23844 +** cnt>0 means there are cnt shared locks on the file.
 1.23845 +**
 1.23846 +** Any attempt to lock or unlock a file first checks the locking
 1.23847 +** structure.  The fcntl() system call is only invoked to set a 
 1.23848 +** POSIX lock if the internal lock structure transitions between
 1.23849 +** a locked and an unlocked state.
 1.23850 +**
 1.23851 +** But wait:  there are yet more problems with POSIX advisory locks.
 1.23852 +**
 1.23853 +** If you close a file descriptor that points to a file that has locks,
 1.23854 +** all locks on that file that are owned by the current process are
 1.23855 +** released.  To work around this problem, each unixInodeInfo object
 1.23856 +** maintains a count of the number of pending locks on tha inode.
 1.23857 +** When an attempt is made to close an unixFile, if there are
 1.23858 +** other unixFile open on the same inode that are holding locks, the call
 1.23859 +** to close() the file descriptor is deferred until all of the locks clear.
 1.23860 +** The unixInodeInfo structure keeps a list of file descriptors that need to
 1.23861 +** be closed and that list is walked (and cleared) when the last lock
 1.23862 +** clears.
 1.23863 +**
 1.23864 +** Yet another problem:  LinuxThreads do not play well with posix locks.
 1.23865 +**
 1.23866 +** Many older versions of linux use the LinuxThreads library which is
 1.23867 +** not posix compliant.  Under LinuxThreads, a lock created by thread
 1.23868 +** A cannot be modified or overridden by a different thread B.
 1.23869 +** Only thread A can modify the lock.  Locking behavior is correct
 1.23870 +** if the appliation uses the newer Native Posix Thread Library (NPTL)
 1.23871 +** on linux - with NPTL a lock created by thread A can override locks
 1.23872 +** in thread B.  But there is no way to know at compile-time which
 1.23873 +** threading library is being used.  So there is no way to know at
 1.23874 +** compile-time whether or not thread A can override locks on thread B.
 1.23875 +** One has to do a run-time check to discover the behavior of the
 1.23876 +** current process.
 1.23877 +**
 1.23878 +** SQLite used to support LinuxThreads.  But support for LinuxThreads
 1.23879 +** was dropped beginning with version 3.7.0.  SQLite will still work with
 1.23880 +** LinuxThreads provided that (1) there is no more than one connection 
 1.23881 +** per database file in the same process and (2) database connections
 1.23882 +** do not move across threads.
 1.23883 +*/
 1.23884 +
 1.23885 +/*
 1.23886 +** An instance of the following structure serves as the key used
 1.23887 +** to locate a particular unixInodeInfo object.
 1.23888 +*/
 1.23889 +struct unixFileId {
 1.23890 +  dev_t dev;                  /* Device number */
 1.23891 +#if OS_VXWORKS
 1.23892 +  struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
 1.23893 +#else
 1.23894 +  ino_t ino;                  /* Inode number */
 1.23895 +#endif
 1.23896 +};
 1.23897 +
 1.23898 +/*
 1.23899 +** An instance of the following structure is allocated for each open
 1.23900 +** inode.  Or, on LinuxThreads, there is one of these structures for
 1.23901 +** each inode opened by each thread.
 1.23902 +**
 1.23903 +** A single inode can have multiple file descriptors, so each unixFile
 1.23904 +** structure contains a pointer to an instance of this object and this
 1.23905 +** object keeps a count of the number of unixFile pointing to it.
 1.23906 +*/
 1.23907 +struct unixInodeInfo {
 1.23908 +  struct unixFileId fileId;       /* The lookup key */
 1.23909 +  int nShared;                    /* Number of SHARED locks held */
 1.23910 +  unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
 1.23911 +  unsigned char bProcessLock;     /* An exclusive process lock is held */
 1.23912 +  int nRef;                       /* Number of pointers to this structure */
 1.23913 +  unixShmNode *pShmNode;          /* Shared memory associated with this inode */
 1.23914 +  int nLock;                      /* Number of outstanding file locks */
 1.23915 +  UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
 1.23916 +  unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
 1.23917 +  unixInodeInfo *pPrev;           /*    .... doubly linked */
 1.23918 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.23919 +  unsigned long long sharedByte;  /* for AFP simulated shared lock */
 1.23920 +#endif
 1.23921 +#if OS_VXWORKS
 1.23922 +  sem_t *pSem;                    /* Named POSIX semaphore */
 1.23923 +  char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
 1.23924 +#endif
 1.23925 +};
 1.23926 +
 1.23927 +/*
 1.23928 +** A lists of all unixInodeInfo objects.
 1.23929 +*/
 1.23930 +static unixInodeInfo *inodeList = 0;
 1.23931 +
 1.23932 +/*
 1.23933 +**
 1.23934 +** This function - unixLogError_x(), is only ever called via the macro
 1.23935 +** unixLogError().
 1.23936 +**
 1.23937 +** It is invoked after an error occurs in an OS function and errno has been
 1.23938 +** set. It logs a message using sqlite3_log() containing the current value of
 1.23939 +** errno and, if possible, the human-readable equivalent from strerror() or
 1.23940 +** strerror_r().
 1.23941 +**
 1.23942 +** The first argument passed to the macro should be the error code that
 1.23943 +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
 1.23944 +** The two subsequent arguments should be the name of the OS function that
 1.23945 +** failed (e.g. "unlink", "open") and the associated file-system path,
 1.23946 +** if any.
 1.23947 +*/
 1.23948 +#define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
 1.23949 +static int unixLogErrorAtLine(
 1.23950 +  int errcode,                    /* SQLite error code */
 1.23951 +  const char *zFunc,              /* Name of OS function that failed */
 1.23952 +  const char *zPath,              /* File path associated with error */
 1.23953 +  int iLine                       /* Source line number where error occurred */
 1.23954 +){
 1.23955 +  char *zErr;                     /* Message from strerror() or equivalent */
 1.23956 +  int iErrno = errno;             /* Saved syscall error number */
 1.23957 +
 1.23958 +  /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
 1.23959 +  ** the strerror() function to obtain the human-readable error message
 1.23960 +  ** equivalent to errno. Otherwise, use strerror_r().
 1.23961 +  */ 
 1.23962 +#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
 1.23963 +  char aErr[80];
 1.23964 +  memset(aErr, 0, sizeof(aErr));
 1.23965 +  zErr = aErr;
 1.23966 +
 1.23967 +  /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
 1.23968 +  ** assume that the system provides the GNU version of strerror_r() that
 1.23969 +  ** returns a pointer to a buffer containing the error message. That pointer 
 1.23970 +  ** may point to aErr[], or it may point to some static storage somewhere. 
 1.23971 +  ** Otherwise, assume that the system provides the POSIX version of 
 1.23972 +  ** strerror_r(), which always writes an error message into aErr[].
 1.23973 +  **
 1.23974 +  ** If the code incorrectly assumes that it is the POSIX version that is
 1.23975 +  ** available, the error message will often be an empty string. Not a
 1.23976 +  ** huge problem. Incorrectly concluding that the GNU version is available 
 1.23977 +  ** could lead to a segfault though.
 1.23978 +  */
 1.23979 +#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
 1.23980 +  zErr = 
 1.23981 +# endif
 1.23982 +  strerror_r(iErrno, aErr, sizeof(aErr)-1);
 1.23983 +
 1.23984 +#elif SQLITE_THREADSAFE
 1.23985 +  /* This is a threadsafe build, but strerror_r() is not available. */
 1.23986 +  zErr = "";
 1.23987 +#else
 1.23988 +  /* Non-threadsafe build, use strerror(). */
 1.23989 +  zErr = strerror(iErrno);
 1.23990 +#endif
 1.23991 +
 1.23992 +  assert( errcode!=SQLITE_OK );
 1.23993 +  if( zPath==0 ) zPath = "";
 1.23994 +  sqlite3_log(errcode,
 1.23995 +      "os_unix.c:%d: (%d) %s(%s) - %s",
 1.23996 +      iLine, iErrno, zFunc, zPath, zErr
 1.23997 +  );
 1.23998 +
 1.23999 +  return errcode;
 1.24000 +}
 1.24001 +
 1.24002 +/*
 1.24003 +** Close a file descriptor.
 1.24004 +**
 1.24005 +** We assume that close() almost always works, since it is only in a
 1.24006 +** very sick application or on a very sick platform that it might fail.
 1.24007 +** If it does fail, simply leak the file descriptor, but do log the
 1.24008 +** error.
 1.24009 +**
 1.24010 +** Note that it is not safe to retry close() after EINTR since the
 1.24011 +** file descriptor might have already been reused by another thread.
 1.24012 +** So we don't even try to recover from an EINTR.  Just log the error
 1.24013 +** and move on.
 1.24014 +*/
 1.24015 +static void robust_close(unixFile *pFile, int h, int lineno){
 1.24016 +  if( osClose(h) ){
 1.24017 +    unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
 1.24018 +                       pFile ? pFile->zPath : 0, lineno);
 1.24019 +  }
 1.24020 +}
 1.24021 +
 1.24022 +/*
 1.24023 +** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
 1.24024 +*/ 
 1.24025 +static void closePendingFds(unixFile *pFile){
 1.24026 +  unixInodeInfo *pInode = pFile->pInode;
 1.24027 +  UnixUnusedFd *p;
 1.24028 +  UnixUnusedFd *pNext;
 1.24029 +  for(p=pInode->pUnused; p; p=pNext){
 1.24030 +    pNext = p->pNext;
 1.24031 +    robust_close(pFile, p->fd, __LINE__);
 1.24032 +    sqlite3_free(p);
 1.24033 +  }
 1.24034 +  pInode->pUnused = 0;
 1.24035 +}
 1.24036 +
 1.24037 +/*
 1.24038 +** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 1.24039 +**
 1.24040 +** The mutex entered using the unixEnterMutex() function must be held
 1.24041 +** when this function is called.
 1.24042 +*/
 1.24043 +static void releaseInodeInfo(unixFile *pFile){
 1.24044 +  unixInodeInfo *pInode = pFile->pInode;
 1.24045 +  assert( unixMutexHeld() );
 1.24046 +  if( ALWAYS(pInode) ){
 1.24047 +    pInode->nRef--;
 1.24048 +    if( pInode->nRef==0 ){
 1.24049 +      assert( pInode->pShmNode==0 );
 1.24050 +      closePendingFds(pFile);
 1.24051 +      if( pInode->pPrev ){
 1.24052 +        assert( pInode->pPrev->pNext==pInode );
 1.24053 +        pInode->pPrev->pNext = pInode->pNext;
 1.24054 +      }else{
 1.24055 +        assert( inodeList==pInode );
 1.24056 +        inodeList = pInode->pNext;
 1.24057 +      }
 1.24058 +      if( pInode->pNext ){
 1.24059 +        assert( pInode->pNext->pPrev==pInode );
 1.24060 +        pInode->pNext->pPrev = pInode->pPrev;
 1.24061 +      }
 1.24062 +      sqlite3_free(pInode);
 1.24063 +    }
 1.24064 +  }
 1.24065 +}
 1.24066 +
 1.24067 +/*
 1.24068 +** Given a file descriptor, locate the unixInodeInfo object that
 1.24069 +** describes that file descriptor.  Create a new one if necessary.  The
 1.24070 +** return value might be uninitialized if an error occurs.
 1.24071 +**
 1.24072 +** The mutex entered using the unixEnterMutex() function must be held
 1.24073 +** when this function is called.
 1.24074 +**
 1.24075 +** Return an appropriate error code.
 1.24076 +*/
 1.24077 +static int findInodeInfo(
 1.24078 +  unixFile *pFile,               /* Unix file with file desc used in the key */
 1.24079 +  unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
 1.24080 +){
 1.24081 +  int rc;                        /* System call return code */
 1.24082 +  int fd;                        /* The file descriptor for pFile */
 1.24083 +  struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
 1.24084 +  struct stat statbuf;           /* Low-level file information */
 1.24085 +  unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
 1.24086 +
 1.24087 +  assert( unixMutexHeld() );
 1.24088 +
 1.24089 +  /* Get low-level information about the file that we can used to
 1.24090 +  ** create a unique name for the file.
 1.24091 +  */
 1.24092 +  fd = pFile->h;
 1.24093 +  rc = osFstat(fd, &statbuf);
 1.24094 +  if( rc!=0 ){
 1.24095 +    pFile->lastErrno = errno;
 1.24096 +#ifdef EOVERFLOW
 1.24097 +    if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
 1.24098 +#endif
 1.24099 +    return SQLITE_IOERR;
 1.24100 +  }
 1.24101 +
 1.24102 +#ifdef __APPLE__
 1.24103 +  /* On OS X on an msdos filesystem, the inode number is reported
 1.24104 +  ** incorrectly for zero-size files.  See ticket #3260.  To work
 1.24105 +  ** around this problem (we consider it a bug in OS X, not SQLite)
 1.24106 +  ** we always increase the file size to 1 by writing a single byte
 1.24107 +  ** prior to accessing the inode number.  The one byte written is
 1.24108 +  ** an ASCII 'S' character which also happens to be the first byte
 1.24109 +  ** in the header of every SQLite database.  In this way, if there
 1.24110 +  ** is a race condition such that another thread has already populated
 1.24111 +  ** the first page of the database, no damage is done.
 1.24112 +  */
 1.24113 +  if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
 1.24114 +    do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
 1.24115 +    if( rc!=1 ){
 1.24116 +      pFile->lastErrno = errno;
 1.24117 +      return SQLITE_IOERR;
 1.24118 +    }
 1.24119 +    rc = osFstat(fd, &statbuf);
 1.24120 +    if( rc!=0 ){
 1.24121 +      pFile->lastErrno = errno;
 1.24122 +      return SQLITE_IOERR;
 1.24123 +    }
 1.24124 +  }
 1.24125 +#endif
 1.24126 +
 1.24127 +  memset(&fileId, 0, sizeof(fileId));
 1.24128 +  fileId.dev = statbuf.st_dev;
 1.24129 +#if OS_VXWORKS
 1.24130 +  fileId.pId = pFile->pId;
 1.24131 +#else
 1.24132 +  fileId.ino = statbuf.st_ino;
 1.24133 +#endif
 1.24134 +  pInode = inodeList;
 1.24135 +  while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 1.24136 +    pInode = pInode->pNext;
 1.24137 +  }
 1.24138 +  if( pInode==0 ){
 1.24139 +    pInode = sqlite3_malloc( sizeof(*pInode) );
 1.24140 +    if( pInode==0 ){
 1.24141 +      return SQLITE_NOMEM;
 1.24142 +    }
 1.24143 +    memset(pInode, 0, sizeof(*pInode));
 1.24144 +    memcpy(&pInode->fileId, &fileId, sizeof(fileId));
 1.24145 +    pInode->nRef = 1;
 1.24146 +    pInode->pNext = inodeList;
 1.24147 +    pInode->pPrev = 0;
 1.24148 +    if( inodeList ) inodeList->pPrev = pInode;
 1.24149 +    inodeList = pInode;
 1.24150 +  }else{
 1.24151 +    pInode->nRef++;
 1.24152 +  }
 1.24153 +  *ppInode = pInode;
 1.24154 +  return SQLITE_OK;
 1.24155 +}
 1.24156 +
 1.24157 +
 1.24158 +/*
 1.24159 +** This routine checks if there is a RESERVED lock held on the specified
 1.24160 +** file by this or any other process. If such a lock is held, set *pResOut
 1.24161 +** to a non-zero value otherwise *pResOut is set to zero.  The return value
 1.24162 +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 1.24163 +*/
 1.24164 +static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
 1.24165 +  int rc = SQLITE_OK;
 1.24166 +  int reserved = 0;
 1.24167 +  unixFile *pFile = (unixFile*)id;
 1.24168 +
 1.24169 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 1.24170 +
 1.24171 +  assert( pFile );
 1.24172 +  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
 1.24173 +
 1.24174 +  /* Check if a thread in this process holds such a lock */
 1.24175 +  if( pFile->pInode->eFileLock>SHARED_LOCK ){
 1.24176 +    reserved = 1;
 1.24177 +  }
 1.24178 +
 1.24179 +  /* Otherwise see if some other process holds it.
 1.24180 +  */
 1.24181 +#ifndef __DJGPP__
 1.24182 +  if( !reserved && !pFile->pInode->bProcessLock ){
 1.24183 +    struct flock lock;
 1.24184 +    lock.l_whence = SEEK_SET;
 1.24185 +    lock.l_start = RESERVED_BYTE;
 1.24186 +    lock.l_len = 1;
 1.24187 +    lock.l_type = F_WRLCK;
 1.24188 +    if( osFcntl(pFile->h, F_GETLK, &lock) ){
 1.24189 +      rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
 1.24190 +      pFile->lastErrno = errno;
 1.24191 +    } else if( lock.l_type!=F_UNLCK ){
 1.24192 +      reserved = 1;
 1.24193 +    }
 1.24194 +  }
 1.24195 +#endif
 1.24196 +  
 1.24197 +  unixLeaveMutex();
 1.24198 +  OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 1.24199 +
 1.24200 +  *pResOut = reserved;
 1.24201 +  return rc;
 1.24202 +}
 1.24203 +
 1.24204 +/*
 1.24205 +** Attempt to set a system-lock on the file pFile.  The lock is 
 1.24206 +** described by pLock.
 1.24207 +**
 1.24208 +** If the pFile was opened read/write from unix-excl, then the only lock
 1.24209 +** ever obtained is an exclusive lock, and it is obtained exactly once
 1.24210 +** the first time any lock is attempted.  All subsequent system locking
 1.24211 +** operations become no-ops.  Locking operations still happen internally,
 1.24212 +** in order to coordinate access between separate database connections
 1.24213 +** within this process, but all of that is handled in memory and the
 1.24214 +** operating system does not participate.
 1.24215 +**
 1.24216 +** This function is a pass-through to fcntl(F_SETLK) if pFile is using
 1.24217 +** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
 1.24218 +** and is read-only.
 1.24219 +**
 1.24220 +** Zero is returned if the call completes successfully, or -1 if a call
 1.24221 +** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
 1.24222 +*/
 1.24223 +static int unixFileLock(unixFile *pFile, struct flock *pLock){
 1.24224 +  int rc;
 1.24225 +  unixInodeInfo *pInode = pFile->pInode;
 1.24226 +  assert( unixMutexHeld() );
 1.24227 +  assert( pInode!=0 );
 1.24228 +  if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
 1.24229 +   && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
 1.24230 +  ){
 1.24231 +    if( pInode->bProcessLock==0 ){
 1.24232 +      struct flock lock;
 1.24233 +      assert( pInode->nLock==0 );
 1.24234 +      lock.l_whence = SEEK_SET;
 1.24235 +      lock.l_start = SHARED_FIRST;
 1.24236 +      lock.l_len = SHARED_SIZE;
 1.24237 +      lock.l_type = F_WRLCK;
 1.24238 +      rc = osFcntl(pFile->h, F_SETLK, &lock);
 1.24239 +      if( rc<0 ) return rc;
 1.24240 +      pInode->bProcessLock = 1;
 1.24241 +      pInode->nLock++;
 1.24242 +    }else{
 1.24243 +      rc = 0;
 1.24244 +    }
 1.24245 +  }else{
 1.24246 +    rc = osFcntl(pFile->h, F_SETLK, pLock);
 1.24247 +  }
 1.24248 +  return rc;
 1.24249 +}
 1.24250 +
 1.24251 +/*
 1.24252 +** Lock the file with the lock specified by parameter eFileLock - one
 1.24253 +** of the following:
 1.24254 +**
 1.24255 +**     (1) SHARED_LOCK
 1.24256 +**     (2) RESERVED_LOCK
 1.24257 +**     (3) PENDING_LOCK
 1.24258 +**     (4) EXCLUSIVE_LOCK
 1.24259 +**
 1.24260 +** Sometimes when requesting one lock state, additional lock states
 1.24261 +** are inserted in between.  The locking might fail on one of the later
 1.24262 +** transitions leaving the lock state different from what it started but
 1.24263 +** still short of its goal.  The following chart shows the allowed
 1.24264 +** transitions and the inserted intermediate states:
 1.24265 +**
 1.24266 +**    UNLOCKED -> SHARED
 1.24267 +**    SHARED -> RESERVED
 1.24268 +**    SHARED -> (PENDING) -> EXCLUSIVE
 1.24269 +**    RESERVED -> (PENDING) -> EXCLUSIVE
 1.24270 +**    PENDING -> EXCLUSIVE
 1.24271 +**
 1.24272 +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 1.24273 +** routine to lower a locking level.
 1.24274 +*/
 1.24275 +static int unixLock(sqlite3_file *id, int eFileLock){
 1.24276 +  /* The following describes the implementation of the various locks and
 1.24277 +  ** lock transitions in terms of the POSIX advisory shared and exclusive
 1.24278 +  ** lock primitives (called read-locks and write-locks below, to avoid
 1.24279 +  ** confusion with SQLite lock names). The algorithms are complicated
 1.24280 +  ** slightly in order to be compatible with windows systems simultaneously
 1.24281 +  ** accessing the same database file, in case that is ever required.
 1.24282 +  **
 1.24283 +  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
 1.24284 +  ** byte', each single bytes at well known offsets, and the 'shared byte
 1.24285 +  ** range', a range of 510 bytes at a well known offset.
 1.24286 +  **
 1.24287 +  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
 1.24288 +  ** byte'.  If this is successful, a random byte from the 'shared byte
 1.24289 +  ** range' is read-locked and the lock on the 'pending byte' released.
 1.24290 +  **
 1.24291 +  ** A process may only obtain a RESERVED lock after it has a SHARED lock.
 1.24292 +  ** A RESERVED lock is implemented by grabbing a write-lock on the
 1.24293 +  ** 'reserved byte'. 
 1.24294 +  **
 1.24295 +  ** A process may only obtain a PENDING lock after it has obtained a
 1.24296 +  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
 1.24297 +  ** on the 'pending byte'. This ensures that no new SHARED locks can be
 1.24298 +  ** obtained, but existing SHARED locks are allowed to persist. A process
 1.24299 +  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
 1.24300 +  ** This property is used by the algorithm for rolling back a journal file
 1.24301 +  ** after a crash.
 1.24302 +  **
 1.24303 +  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
 1.24304 +  ** implemented by obtaining a write-lock on the entire 'shared byte
 1.24305 +  ** range'. Since all other locks require a read-lock on one of the bytes
 1.24306 +  ** within this range, this ensures that no other locks are held on the
 1.24307 +  ** database. 
 1.24308 +  **
 1.24309 +  ** The reason a single byte cannot be used instead of the 'shared byte
 1.24310 +  ** range' is that some versions of windows do not support read-locks. By
 1.24311 +  ** locking a random byte from a range, concurrent SHARED locks may exist
 1.24312 +  ** even if the locking primitive used is always a write-lock.
 1.24313 +  */
 1.24314 +  int rc = SQLITE_OK;
 1.24315 +  unixFile *pFile = (unixFile*)id;
 1.24316 +  unixInodeInfo *pInode;
 1.24317 +  struct flock lock;
 1.24318 +  int tErrno = 0;
 1.24319 +
 1.24320 +  assert( pFile );
 1.24321 +  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
 1.24322 +      azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 1.24323 +      azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
 1.24324 +
 1.24325 +  /* If there is already a lock of this type or more restrictive on the
 1.24326 +  ** unixFile, do nothing. Don't use the end_lock: exit path, as
 1.24327 +  ** unixEnterMutex() hasn't been called yet.
 1.24328 +  */
 1.24329 +  if( pFile->eFileLock>=eFileLock ){
 1.24330 +    OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
 1.24331 +            azFileLock(eFileLock)));
 1.24332 +    return SQLITE_OK;
 1.24333 +  }
 1.24334 +
 1.24335 +  /* Make sure the locking sequence is correct.
 1.24336 +  **  (1) We never move from unlocked to anything higher than shared lock.
 1.24337 +  **  (2) SQLite never explicitly requests a pendig lock.
 1.24338 +  **  (3) A shared lock is always held when a reserve lock is requested.
 1.24339 +  */
 1.24340 +  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
 1.24341 +  assert( eFileLock!=PENDING_LOCK );
 1.24342 +  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
 1.24343 +
 1.24344 +  /* This mutex is needed because pFile->pInode is shared across threads
 1.24345 +  */
 1.24346 +  unixEnterMutex();
 1.24347 +  pInode = pFile->pInode;
 1.24348 +
 1.24349 +  /* If some thread using this PID has a lock via a different unixFile*
 1.24350 +  ** handle that precludes the requested lock, return BUSY.
 1.24351 +  */
 1.24352 +  if( (pFile->eFileLock!=pInode->eFileLock && 
 1.24353 +          (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
 1.24354 +  ){
 1.24355 +    rc = SQLITE_BUSY;
 1.24356 +    goto end_lock;
 1.24357 +  }
 1.24358 +
 1.24359 +  /* If a SHARED lock is requested, and some thread using this PID already
 1.24360 +  ** has a SHARED or RESERVED lock, then increment reference counts and
 1.24361 +  ** return SQLITE_OK.
 1.24362 +  */
 1.24363 +  if( eFileLock==SHARED_LOCK && 
 1.24364 +      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
 1.24365 +    assert( eFileLock==SHARED_LOCK );
 1.24366 +    assert( pFile->eFileLock==0 );
 1.24367 +    assert( pInode->nShared>0 );
 1.24368 +    pFile->eFileLock = SHARED_LOCK;
 1.24369 +    pInode->nShared++;
 1.24370 +    pInode->nLock++;
 1.24371 +    goto end_lock;
 1.24372 +  }
 1.24373 +
 1.24374 +
 1.24375 +  /* A PENDING lock is needed before acquiring a SHARED lock and before
 1.24376 +  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
 1.24377 +  ** be released.
 1.24378 +  */
 1.24379 +  lock.l_len = 1L;
 1.24380 +  lock.l_whence = SEEK_SET;
 1.24381 +  if( eFileLock==SHARED_LOCK 
 1.24382 +      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
 1.24383 +  ){
 1.24384 +    lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
 1.24385 +    lock.l_start = PENDING_BYTE;
 1.24386 +    if( unixFileLock(pFile, &lock) ){
 1.24387 +      tErrno = errno;
 1.24388 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 1.24389 +      if( rc!=SQLITE_BUSY ){
 1.24390 +        pFile->lastErrno = tErrno;
 1.24391 +      }
 1.24392 +      goto end_lock;
 1.24393 +    }
 1.24394 +  }
 1.24395 +
 1.24396 +
 1.24397 +  /* If control gets to this point, then actually go ahead and make
 1.24398 +  ** operating system calls for the specified lock.
 1.24399 +  */
 1.24400 +  if( eFileLock==SHARED_LOCK ){
 1.24401 +    assert( pInode->nShared==0 );
 1.24402 +    assert( pInode->eFileLock==0 );
 1.24403 +    assert( rc==SQLITE_OK );
 1.24404 +
 1.24405 +    /* Now get the read-lock */
 1.24406 +    lock.l_start = SHARED_FIRST;
 1.24407 +    lock.l_len = SHARED_SIZE;
 1.24408 +    if( unixFileLock(pFile, &lock) ){
 1.24409 +      tErrno = errno;
 1.24410 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 1.24411 +    }
 1.24412 +
 1.24413 +    /* Drop the temporary PENDING lock */
 1.24414 +    lock.l_start = PENDING_BYTE;
 1.24415 +    lock.l_len = 1L;
 1.24416 +    lock.l_type = F_UNLCK;
 1.24417 +    if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
 1.24418 +      /* This could happen with a network mount */
 1.24419 +      tErrno = errno;
 1.24420 +      rc = SQLITE_IOERR_UNLOCK; 
 1.24421 +    }
 1.24422 +
 1.24423 +    if( rc ){
 1.24424 +      if( rc!=SQLITE_BUSY ){
 1.24425 +        pFile->lastErrno = tErrno;
 1.24426 +      }
 1.24427 +      goto end_lock;
 1.24428 +    }else{
 1.24429 +      pFile->eFileLock = SHARED_LOCK;
 1.24430 +      pInode->nLock++;
 1.24431 +      pInode->nShared = 1;
 1.24432 +    }
 1.24433 +  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
 1.24434 +    /* We are trying for an exclusive lock but another thread in this
 1.24435 +    ** same process is still holding a shared lock. */
 1.24436 +    rc = SQLITE_BUSY;
 1.24437 +  }else{
 1.24438 +    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
 1.24439 +    ** assumed that there is a SHARED or greater lock on the file
 1.24440 +    ** already.
 1.24441 +    */
 1.24442 +    assert( 0!=pFile->eFileLock );
 1.24443 +    lock.l_type = F_WRLCK;
 1.24444 +
 1.24445 +    assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
 1.24446 +    if( eFileLock==RESERVED_LOCK ){
 1.24447 +      lock.l_start = RESERVED_BYTE;
 1.24448 +      lock.l_len = 1L;
 1.24449 +    }else{
 1.24450 +      lock.l_start = SHARED_FIRST;
 1.24451 +      lock.l_len = SHARED_SIZE;
 1.24452 +    }
 1.24453 +
 1.24454 +    if( unixFileLock(pFile, &lock) ){
 1.24455 +      tErrno = errno;
 1.24456 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 1.24457 +      if( rc!=SQLITE_BUSY ){
 1.24458 +        pFile->lastErrno = tErrno;
 1.24459 +      }
 1.24460 +    }
 1.24461 +  }
 1.24462 +  
 1.24463 +
 1.24464 +#ifdef SQLITE_DEBUG
 1.24465 +  /* Set up the transaction-counter change checking flags when
 1.24466 +  ** transitioning from a SHARED to a RESERVED lock.  The change
 1.24467 +  ** from SHARED to RESERVED marks the beginning of a normal
 1.24468 +  ** write operation (not a hot journal rollback).
 1.24469 +  */
 1.24470 +  if( rc==SQLITE_OK
 1.24471 +   && pFile->eFileLock<=SHARED_LOCK
 1.24472 +   && eFileLock==RESERVED_LOCK
 1.24473 +  ){
 1.24474 +    pFile->transCntrChng = 0;
 1.24475 +    pFile->dbUpdate = 0;
 1.24476 +    pFile->inNormalWrite = 1;
 1.24477 +  }
 1.24478 +#endif
 1.24479 +
 1.24480 +
 1.24481 +  if( rc==SQLITE_OK ){
 1.24482 +    pFile->eFileLock = eFileLock;
 1.24483 +    pInode->eFileLock = eFileLock;
 1.24484 +  }else if( eFileLock==EXCLUSIVE_LOCK ){
 1.24485 +    pFile->eFileLock = PENDING_LOCK;
 1.24486 +    pInode->eFileLock = PENDING_LOCK;
 1.24487 +  }
 1.24488 +
 1.24489 +end_lock:
 1.24490 +  unixLeaveMutex();
 1.24491 +  OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), 
 1.24492 +      rc==SQLITE_OK ? "ok" : "failed"));
 1.24493 +  return rc;
 1.24494 +}
 1.24495 +
 1.24496 +/*
 1.24497 +** Add the file descriptor used by file handle pFile to the corresponding
 1.24498 +** pUnused list.
 1.24499 +*/
 1.24500 +static void setPendingFd(unixFile *pFile){
 1.24501 +  unixInodeInfo *pInode = pFile->pInode;
 1.24502 +  UnixUnusedFd *p = pFile->pUnused;
 1.24503 +  p->pNext = pInode->pUnused;
 1.24504 +  pInode->pUnused = p;
 1.24505 +  pFile->h = -1;
 1.24506 +  pFile->pUnused = 0;
 1.24507 +}
 1.24508 +
 1.24509 +/*
 1.24510 +** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.24511 +** must be either NO_LOCK or SHARED_LOCK.
 1.24512 +**
 1.24513 +** If the locking level of the file descriptor is already at or below
 1.24514 +** the requested locking level, this routine is a no-op.
 1.24515 +** 
 1.24516 +** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
 1.24517 +** the byte range is divided into 2 parts and the first part is unlocked then
 1.24518 +** set to a read lock, then the other part is simply unlocked.  This works 
 1.24519 +** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to 
 1.24520 +** remove the write lock on a region when a read lock is set.
 1.24521 +*/
 1.24522 +static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
 1.24523 +  unixFile *pFile = (unixFile*)id;
 1.24524 +  unixInodeInfo *pInode;
 1.24525 +  struct flock lock;
 1.24526 +  int rc = SQLITE_OK;
 1.24527 +
 1.24528 +  assert( pFile );
 1.24529 +  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
 1.24530 +      pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 1.24531 +      getpid()));
 1.24532 +
 1.24533 +  assert( eFileLock<=SHARED_LOCK );
 1.24534 +  if( pFile->eFileLock<=eFileLock ){
 1.24535 +    return SQLITE_OK;
 1.24536 +  }
 1.24537 +  unixEnterMutex();
 1.24538 +  pInode = pFile->pInode;
 1.24539 +  assert( pInode->nShared!=0 );
 1.24540 +  if( pFile->eFileLock>SHARED_LOCK ){
 1.24541 +    assert( pInode->eFileLock==pFile->eFileLock );
 1.24542 +
 1.24543 +#ifdef SQLITE_DEBUG
 1.24544 +    /* When reducing a lock such that other processes can start
 1.24545 +    ** reading the database file again, make sure that the
 1.24546 +    ** transaction counter was updated if any part of the database
 1.24547 +    ** file changed.  If the transaction counter is not updated,
 1.24548 +    ** other connections to the same file might not realize that
 1.24549 +    ** the file has changed and hence might not know to flush their
 1.24550 +    ** cache.  The use of a stale cache can lead to database corruption.
 1.24551 +    */
 1.24552 +    pFile->inNormalWrite = 0;
 1.24553 +#endif
 1.24554 +
 1.24555 +    /* downgrading to a shared lock on NFS involves clearing the write lock
 1.24556 +    ** before establishing the readlock - to avoid a race condition we downgrade
 1.24557 +    ** the lock in 2 blocks, so that part of the range will be covered by a 
 1.24558 +    ** write lock until the rest is covered by a read lock:
 1.24559 +    **  1:   [WWWWW]
 1.24560 +    **  2:   [....W]
 1.24561 +    **  3:   [RRRRW]
 1.24562 +    **  4:   [RRRR.]
 1.24563 +    */
 1.24564 +    if( eFileLock==SHARED_LOCK ){
 1.24565 +
 1.24566 +#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
 1.24567 +      (void)handleNFSUnlock;
 1.24568 +      assert( handleNFSUnlock==0 );
 1.24569 +#endif
 1.24570 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.24571 +      if( handleNFSUnlock ){
 1.24572 +        int tErrno;               /* Error code from system call errors */
 1.24573 +        off_t divSize = SHARED_SIZE - 1;
 1.24574 +        
 1.24575 +        lock.l_type = F_UNLCK;
 1.24576 +        lock.l_whence = SEEK_SET;
 1.24577 +        lock.l_start = SHARED_FIRST;
 1.24578 +        lock.l_len = divSize;
 1.24579 +        if( unixFileLock(pFile, &lock)==(-1) ){
 1.24580 +          tErrno = errno;
 1.24581 +          rc = SQLITE_IOERR_UNLOCK;
 1.24582 +          if( IS_LOCK_ERROR(rc) ){
 1.24583 +            pFile->lastErrno = tErrno;
 1.24584 +          }
 1.24585 +          goto end_unlock;
 1.24586 +        }
 1.24587 +        lock.l_type = F_RDLCK;
 1.24588 +        lock.l_whence = SEEK_SET;
 1.24589 +        lock.l_start = SHARED_FIRST;
 1.24590 +        lock.l_len = divSize;
 1.24591 +        if( unixFileLock(pFile, &lock)==(-1) ){
 1.24592 +          tErrno = errno;
 1.24593 +          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
 1.24594 +          if( IS_LOCK_ERROR(rc) ){
 1.24595 +            pFile->lastErrno = tErrno;
 1.24596 +          }
 1.24597 +          goto end_unlock;
 1.24598 +        }
 1.24599 +        lock.l_type = F_UNLCK;
 1.24600 +        lock.l_whence = SEEK_SET;
 1.24601 +        lock.l_start = SHARED_FIRST+divSize;
 1.24602 +        lock.l_len = SHARED_SIZE-divSize;
 1.24603 +        if( unixFileLock(pFile, &lock)==(-1) ){
 1.24604 +          tErrno = errno;
 1.24605 +          rc = SQLITE_IOERR_UNLOCK;
 1.24606 +          if( IS_LOCK_ERROR(rc) ){
 1.24607 +            pFile->lastErrno = tErrno;
 1.24608 +          }
 1.24609 +          goto end_unlock;
 1.24610 +        }
 1.24611 +      }else
 1.24612 +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 1.24613 +      {
 1.24614 +        lock.l_type = F_RDLCK;
 1.24615 +        lock.l_whence = SEEK_SET;
 1.24616 +        lock.l_start = SHARED_FIRST;
 1.24617 +        lock.l_len = SHARED_SIZE;
 1.24618 +        if( unixFileLock(pFile, &lock) ){
 1.24619 +          /* In theory, the call to unixFileLock() cannot fail because another
 1.24620 +          ** process is holding an incompatible lock. If it does, this 
 1.24621 +          ** indicates that the other process is not following the locking
 1.24622 +          ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
 1.24623 +          ** SQLITE_BUSY would confuse the upper layer (in practice it causes 
 1.24624 +          ** an assert to fail). */ 
 1.24625 +          rc = SQLITE_IOERR_RDLOCK;
 1.24626 +          pFile->lastErrno = errno;
 1.24627 +          goto end_unlock;
 1.24628 +        }
 1.24629 +      }
 1.24630 +    }
 1.24631 +    lock.l_type = F_UNLCK;
 1.24632 +    lock.l_whence = SEEK_SET;
 1.24633 +    lock.l_start = PENDING_BYTE;
 1.24634 +    lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
 1.24635 +    if( unixFileLock(pFile, &lock)==0 ){
 1.24636 +      pInode->eFileLock = SHARED_LOCK;
 1.24637 +    }else{
 1.24638 +      rc = SQLITE_IOERR_UNLOCK;
 1.24639 +      pFile->lastErrno = errno;
 1.24640 +      goto end_unlock;
 1.24641 +    }
 1.24642 +  }
 1.24643 +  if( eFileLock==NO_LOCK ){
 1.24644 +    /* Decrement the shared lock counter.  Release the lock using an
 1.24645 +    ** OS call only when all threads in this same process have released
 1.24646 +    ** the lock.
 1.24647 +    */
 1.24648 +    pInode->nShared--;
 1.24649 +    if( pInode->nShared==0 ){
 1.24650 +      lock.l_type = F_UNLCK;
 1.24651 +      lock.l_whence = SEEK_SET;
 1.24652 +      lock.l_start = lock.l_len = 0L;
 1.24653 +      if( unixFileLock(pFile, &lock)==0 ){
 1.24654 +        pInode->eFileLock = NO_LOCK;
 1.24655 +      }else{
 1.24656 +        rc = SQLITE_IOERR_UNLOCK;
 1.24657 +        pFile->lastErrno = errno;
 1.24658 +        pInode->eFileLock = NO_LOCK;
 1.24659 +        pFile->eFileLock = NO_LOCK;
 1.24660 +      }
 1.24661 +    }
 1.24662 +
 1.24663 +    /* Decrement the count of locks against this same file.  When the
 1.24664 +    ** count reaches zero, close any other file descriptors whose close
 1.24665 +    ** was deferred because of outstanding locks.
 1.24666 +    */
 1.24667 +    pInode->nLock--;
 1.24668 +    assert( pInode->nLock>=0 );
 1.24669 +    if( pInode->nLock==0 ){
 1.24670 +      closePendingFds(pFile);
 1.24671 +    }
 1.24672 +  }
 1.24673 +
 1.24674 +end_unlock:
 1.24675 +  unixLeaveMutex();
 1.24676 +  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 1.24677 +  return rc;
 1.24678 +}
 1.24679 +
 1.24680 +/*
 1.24681 +** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.24682 +** must be either NO_LOCK or SHARED_LOCK.
 1.24683 +**
 1.24684 +** If the locking level of the file descriptor is already at or below
 1.24685 +** the requested locking level, this routine is a no-op.
 1.24686 +*/
 1.24687 +static int unixUnlock(sqlite3_file *id, int eFileLock){
 1.24688 +  return posixUnlock(id, eFileLock, 0);
 1.24689 +}
 1.24690 +
 1.24691 +/*
 1.24692 +** This function performs the parts of the "close file" operation 
 1.24693 +** common to all locking schemes. It closes the directory and file
 1.24694 +** handles, if they are valid, and sets all fields of the unixFile
 1.24695 +** structure to 0.
 1.24696 +**
 1.24697 +** It is *not* necessary to hold the mutex when this routine is called,
 1.24698 +** even on VxWorks.  A mutex will be acquired on VxWorks by the
 1.24699 +** vxworksReleaseFileId() routine.
 1.24700 +*/
 1.24701 +static int closeUnixFile(sqlite3_file *id){
 1.24702 +  unixFile *pFile = (unixFile*)id;
 1.24703 +  if( pFile->h>=0 ){
 1.24704 +    robust_close(pFile, pFile->h, __LINE__);
 1.24705 +    pFile->h = -1;
 1.24706 +  }
 1.24707 +#if OS_VXWORKS
 1.24708 +  if( pFile->pId ){
 1.24709 +    if( pFile->ctrlFlags & UNIXFILE_DELETE ){
 1.24710 +      osUnlink(pFile->pId->zCanonicalName);
 1.24711 +    }
 1.24712 +    vxworksReleaseFileId(pFile->pId);
 1.24713 +    pFile->pId = 0;
 1.24714 +  }
 1.24715 +#endif
 1.24716 +  OSTRACE(("CLOSE   %-3d\n", pFile->h));
 1.24717 +  OpenCounter(-1);
 1.24718 +  sqlite3_free(pFile->pUnused);
 1.24719 +  memset(pFile, 0, sizeof(unixFile));
 1.24720 +  return SQLITE_OK;
 1.24721 +}
 1.24722 +
 1.24723 +/*
 1.24724 +** Close a file.
 1.24725 +*/
 1.24726 +static int unixClose(sqlite3_file *id){
 1.24727 +  int rc = SQLITE_OK;
 1.24728 +  unixFile *pFile = (unixFile *)id;
 1.24729 +  unixUnlock(id, NO_LOCK);
 1.24730 +  unixEnterMutex();
 1.24731 +
 1.24732 +  /* unixFile.pInode is always valid here. Otherwise, a different close
 1.24733 +  ** routine (e.g. nolockClose()) would be called instead.
 1.24734 +  */
 1.24735 +  assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
 1.24736 +  if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
 1.24737 +    /* If there are outstanding locks, do not actually close the file just
 1.24738 +    ** yet because that would clear those locks.  Instead, add the file
 1.24739 +    ** descriptor to pInode->pUnused list.  It will be automatically closed 
 1.24740 +    ** when the last lock is cleared.
 1.24741 +    */
 1.24742 +    setPendingFd(pFile);
 1.24743 +  }
 1.24744 +  releaseInodeInfo(pFile);
 1.24745 +  rc = closeUnixFile(id);
 1.24746 +  unixLeaveMutex();
 1.24747 +  return rc;
 1.24748 +}
 1.24749 +
 1.24750 +/************** End of the posix advisory lock implementation *****************
 1.24751 +******************************************************************************/
 1.24752 +
 1.24753 +/******************************************************************************
 1.24754 +****************************** No-op Locking **********************************
 1.24755 +**
 1.24756 +** Of the various locking implementations available, this is by far the
 1.24757 +** simplest:  locking is ignored.  No attempt is made to lock the database
 1.24758 +** file for reading or writing.
 1.24759 +**
 1.24760 +** This locking mode is appropriate for use on read-only databases
 1.24761 +** (ex: databases that are burned into CD-ROM, for example.)  It can
 1.24762 +** also be used if the application employs some external mechanism to
 1.24763 +** prevent simultaneous access of the same database by two or more
 1.24764 +** database connections.  But there is a serious risk of database
 1.24765 +** corruption if this locking mode is used in situations where multiple
 1.24766 +** database connections are accessing the same database file at the same
 1.24767 +** time and one or more of those connections are writing.
 1.24768 +*/
 1.24769 +
 1.24770 +static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
 1.24771 +  UNUSED_PARAMETER(NotUsed);
 1.24772 +  *pResOut = 0;
 1.24773 +  return SQLITE_OK;
 1.24774 +}
 1.24775 +static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
 1.24776 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.24777 +  return SQLITE_OK;
 1.24778 +}
 1.24779 +static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
 1.24780 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.24781 +  return SQLITE_OK;
 1.24782 +}
 1.24783 +
 1.24784 +/*
 1.24785 +** Close the file.
 1.24786 +*/
 1.24787 +static int nolockClose(sqlite3_file *id) {
 1.24788 +  return closeUnixFile(id);
 1.24789 +}
 1.24790 +
 1.24791 +/******************* End of the no-op lock implementation *********************
 1.24792 +******************************************************************************/
 1.24793 +
 1.24794 +/******************************************************************************
 1.24795 +************************* Begin dot-file Locking ******************************
 1.24796 +**
 1.24797 +** The dotfile locking implementation uses the existance of separate lock
 1.24798 +** files (really a directory) to control access to the database.  This works
 1.24799 +** on just about every filesystem imaginable.  But there are serious downsides:
 1.24800 +**
 1.24801 +**    (1)  There is zero concurrency.  A single reader blocks all other
 1.24802 +**         connections from reading or writing the database.
 1.24803 +**
 1.24804 +**    (2)  An application crash or power loss can leave stale lock files
 1.24805 +**         sitting around that need to be cleared manually.
 1.24806 +**
 1.24807 +** Nevertheless, a dotlock is an appropriate locking mode for use if no
 1.24808 +** other locking strategy is available.
 1.24809 +**
 1.24810 +** Dotfile locking works by creating a subdirectory in the same directory as
 1.24811 +** the database and with the same name but with a ".lock" extension added.
 1.24812 +** The existance of a lock directory implies an EXCLUSIVE lock.  All other
 1.24813 +** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
 1.24814 +*/
 1.24815 +
 1.24816 +/*
 1.24817 +** The file suffix added to the data base filename in order to create the
 1.24818 +** lock directory.
 1.24819 +*/
 1.24820 +#define DOTLOCK_SUFFIX ".lock"
 1.24821 +
 1.24822 +/*
 1.24823 +** This routine checks if there is a RESERVED lock held on the specified
 1.24824 +** file by this or any other process. If such a lock is held, set *pResOut
 1.24825 +** to a non-zero value otherwise *pResOut is set to zero.  The return value
 1.24826 +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 1.24827 +**
 1.24828 +** In dotfile locking, either a lock exists or it does not.  So in this
 1.24829 +** variation of CheckReservedLock(), *pResOut is set to true if any lock
 1.24830 +** is held on the file and false if the file is unlocked.
 1.24831 +*/
 1.24832 +static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
 1.24833 +  int rc = SQLITE_OK;
 1.24834 +  int reserved = 0;
 1.24835 +  unixFile *pFile = (unixFile*)id;
 1.24836 +
 1.24837 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 1.24838 +  
 1.24839 +  assert( pFile );
 1.24840 +
 1.24841 +  /* Check if a thread in this process holds such a lock */
 1.24842 +  if( pFile->eFileLock>SHARED_LOCK ){
 1.24843 +    /* Either this connection or some other connection in the same process
 1.24844 +    ** holds a lock on the file.  No need to check further. */
 1.24845 +    reserved = 1;
 1.24846 +  }else{
 1.24847 +    /* The lock is held if and only if the lockfile exists */
 1.24848 +    const char *zLockFile = (const char*)pFile->lockingContext;
 1.24849 +    reserved = osAccess(zLockFile, 0)==0;
 1.24850 +  }
 1.24851 +  OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
 1.24852 +  *pResOut = reserved;
 1.24853 +  return rc;
 1.24854 +}
 1.24855 +
 1.24856 +/*
 1.24857 +** Lock the file with the lock specified by parameter eFileLock - one
 1.24858 +** of the following:
 1.24859 +**
 1.24860 +**     (1) SHARED_LOCK
 1.24861 +**     (2) RESERVED_LOCK
 1.24862 +**     (3) PENDING_LOCK
 1.24863 +**     (4) EXCLUSIVE_LOCK
 1.24864 +**
 1.24865 +** Sometimes when requesting one lock state, additional lock states
 1.24866 +** are inserted in between.  The locking might fail on one of the later
 1.24867 +** transitions leaving the lock state different from what it started but
 1.24868 +** still short of its goal.  The following chart shows the allowed
 1.24869 +** transitions and the inserted intermediate states:
 1.24870 +**
 1.24871 +**    UNLOCKED -> SHARED
 1.24872 +**    SHARED -> RESERVED
 1.24873 +**    SHARED -> (PENDING) -> EXCLUSIVE
 1.24874 +**    RESERVED -> (PENDING) -> EXCLUSIVE
 1.24875 +**    PENDING -> EXCLUSIVE
 1.24876 +**
 1.24877 +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 1.24878 +** routine to lower a locking level.
 1.24879 +**
 1.24880 +** With dotfile locking, we really only support state (4): EXCLUSIVE.
 1.24881 +** But we track the other locking levels internally.
 1.24882 +*/
 1.24883 +static int dotlockLock(sqlite3_file *id, int eFileLock) {
 1.24884 +  unixFile *pFile = (unixFile*)id;
 1.24885 +  char *zLockFile = (char *)pFile->lockingContext;
 1.24886 +  int rc = SQLITE_OK;
 1.24887 +
 1.24888 +
 1.24889 +  /* If we have any lock, then the lock file already exists.  All we have
 1.24890 +  ** to do is adjust our internal record of the lock level.
 1.24891 +  */
 1.24892 +  if( pFile->eFileLock > NO_LOCK ){
 1.24893 +    pFile->eFileLock = eFileLock;
 1.24894 +    /* Always update the timestamp on the old file */
 1.24895 +#ifdef HAVE_UTIME
 1.24896 +    utime(zLockFile, NULL);
 1.24897 +#else
 1.24898 +    utimes(zLockFile, NULL);
 1.24899 +#endif
 1.24900 +    return SQLITE_OK;
 1.24901 +  }
 1.24902 +  
 1.24903 +  /* grab an exclusive lock */
 1.24904 +  rc = osMkdir(zLockFile, 0777);
 1.24905 +  if( rc<0 ){
 1.24906 +    /* failed to open/create the lock directory */
 1.24907 +    int tErrno = errno;
 1.24908 +    if( EEXIST == tErrno ){
 1.24909 +      rc = SQLITE_BUSY;
 1.24910 +    } else {
 1.24911 +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 1.24912 +      if( IS_LOCK_ERROR(rc) ){
 1.24913 +        pFile->lastErrno = tErrno;
 1.24914 +      }
 1.24915 +    }
 1.24916 +    return rc;
 1.24917 +  } 
 1.24918 +  
 1.24919 +  /* got it, set the type and return ok */
 1.24920 +  pFile->eFileLock = eFileLock;
 1.24921 +  return rc;
 1.24922 +}
 1.24923 +
 1.24924 +/*
 1.24925 +** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.24926 +** must be either NO_LOCK or SHARED_LOCK.
 1.24927 +**
 1.24928 +** If the locking level of the file descriptor is already at or below
 1.24929 +** the requested locking level, this routine is a no-op.
 1.24930 +**
 1.24931 +** When the locking level reaches NO_LOCK, delete the lock file.
 1.24932 +*/
 1.24933 +static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
 1.24934 +  unixFile *pFile = (unixFile*)id;
 1.24935 +  char *zLockFile = (char *)pFile->lockingContext;
 1.24936 +  int rc;
 1.24937 +
 1.24938 +  assert( pFile );
 1.24939 +  OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
 1.24940 +           pFile->eFileLock, getpid()));
 1.24941 +  assert( eFileLock<=SHARED_LOCK );
 1.24942 +  
 1.24943 +  /* no-op if possible */
 1.24944 +  if( pFile->eFileLock==eFileLock ){
 1.24945 +    return SQLITE_OK;
 1.24946 +  }
 1.24947 +
 1.24948 +  /* To downgrade to shared, simply update our internal notion of the
 1.24949 +  ** lock state.  No need to mess with the file on disk.
 1.24950 +  */
 1.24951 +  if( eFileLock==SHARED_LOCK ){
 1.24952 +    pFile->eFileLock = SHARED_LOCK;
 1.24953 +    return SQLITE_OK;
 1.24954 +  }
 1.24955 +  
 1.24956 +  /* To fully unlock the database, delete the lock file */
 1.24957 +  assert( eFileLock==NO_LOCK );
 1.24958 +  rc = osRmdir(zLockFile);
 1.24959 +  if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
 1.24960 +  if( rc<0 ){
 1.24961 +    int tErrno = errno;
 1.24962 +    rc = 0;
 1.24963 +    if( ENOENT != tErrno ){
 1.24964 +      rc = SQLITE_IOERR_UNLOCK;
 1.24965 +    }
 1.24966 +    if( IS_LOCK_ERROR(rc) ){
 1.24967 +      pFile->lastErrno = tErrno;
 1.24968 +    }
 1.24969 +    return rc; 
 1.24970 +  }
 1.24971 +  pFile->eFileLock = NO_LOCK;
 1.24972 +  return SQLITE_OK;
 1.24973 +}
 1.24974 +
 1.24975 +/*
 1.24976 +** Close a file.  Make sure the lock has been released before closing.
 1.24977 +*/
 1.24978 +static int dotlockClose(sqlite3_file *id) {
 1.24979 +  int rc = SQLITE_OK;
 1.24980 +  if( id ){
 1.24981 +    unixFile *pFile = (unixFile*)id;
 1.24982 +    dotlockUnlock(id, NO_LOCK);
 1.24983 +    sqlite3_free(pFile->lockingContext);
 1.24984 +    rc = closeUnixFile(id);
 1.24985 +  }
 1.24986 +  return rc;
 1.24987 +}
 1.24988 +/****************** End of the dot-file lock implementation *******************
 1.24989 +******************************************************************************/
 1.24990 +
 1.24991 +/******************************************************************************
 1.24992 +************************** Begin flock Locking ********************************
 1.24993 +**
 1.24994 +** Use the flock() system call to do file locking.
 1.24995 +**
 1.24996 +** flock() locking is like dot-file locking in that the various
 1.24997 +** fine-grain locking levels supported by SQLite are collapsed into
 1.24998 +** a single exclusive lock.  In other words, SHARED, RESERVED, and
 1.24999 +** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
 1.25000 +** still works when you do this, but concurrency is reduced since
 1.25001 +** only a single process can be reading the database at a time.
 1.25002 +**
 1.25003 +** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
 1.25004 +** compiling for VXWORKS.
 1.25005 +*/
 1.25006 +#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
 1.25007 +
 1.25008 +/*
 1.25009 +** Retry flock() calls that fail with EINTR
 1.25010 +*/
 1.25011 +#ifdef EINTR
 1.25012 +static int robust_flock(int fd, int op){
 1.25013 +  int rc;
 1.25014 +  do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
 1.25015 +  return rc;
 1.25016 +}
 1.25017 +#else
 1.25018 +# define robust_flock(a,b) flock(a,b)
 1.25019 +#endif
 1.25020 +     
 1.25021 +
 1.25022 +/*
 1.25023 +** This routine checks if there is a RESERVED lock held on the specified
 1.25024 +** file by this or any other process. If such a lock is held, set *pResOut
 1.25025 +** to a non-zero value otherwise *pResOut is set to zero.  The return value
 1.25026 +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 1.25027 +*/
 1.25028 +static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
 1.25029 +  int rc = SQLITE_OK;
 1.25030 +  int reserved = 0;
 1.25031 +  unixFile *pFile = (unixFile*)id;
 1.25032 +  
 1.25033 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 1.25034 +  
 1.25035 +  assert( pFile );
 1.25036 +  
 1.25037 +  /* Check if a thread in this process holds such a lock */
 1.25038 +  if( pFile->eFileLock>SHARED_LOCK ){
 1.25039 +    reserved = 1;
 1.25040 +  }
 1.25041 +  
 1.25042 +  /* Otherwise see if some other process holds it. */
 1.25043 +  if( !reserved ){
 1.25044 +    /* attempt to get the lock */
 1.25045 +    int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
 1.25046 +    if( !lrc ){
 1.25047 +      /* got the lock, unlock it */
 1.25048 +      lrc = robust_flock(pFile->h, LOCK_UN);
 1.25049 +      if ( lrc ) {
 1.25050 +        int tErrno = errno;
 1.25051 +        /* unlock failed with an error */
 1.25052 +        lrc = SQLITE_IOERR_UNLOCK; 
 1.25053 +        if( IS_LOCK_ERROR(lrc) ){
 1.25054 +          pFile->lastErrno = tErrno;
 1.25055 +          rc = lrc;
 1.25056 +        }
 1.25057 +      }
 1.25058 +    } else {
 1.25059 +      int tErrno = errno;
 1.25060 +      reserved = 1;
 1.25061 +      /* someone else might have it reserved */
 1.25062 +      lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 
 1.25063 +      if( IS_LOCK_ERROR(lrc) ){
 1.25064 +        pFile->lastErrno = tErrno;
 1.25065 +        rc = lrc;
 1.25066 +      }
 1.25067 +    }
 1.25068 +  }
 1.25069 +  OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
 1.25070 +
 1.25071 +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 1.25072 +  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
 1.25073 +    rc = SQLITE_OK;
 1.25074 +    reserved=1;
 1.25075 +  }
 1.25076 +#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 1.25077 +  *pResOut = reserved;
 1.25078 +  return rc;
 1.25079 +}
 1.25080 +
 1.25081 +/*
 1.25082 +** Lock the file with the lock specified by parameter eFileLock - one
 1.25083 +** of the following:
 1.25084 +**
 1.25085 +**     (1) SHARED_LOCK
 1.25086 +**     (2) RESERVED_LOCK
 1.25087 +**     (3) PENDING_LOCK
 1.25088 +**     (4) EXCLUSIVE_LOCK
 1.25089 +**
 1.25090 +** Sometimes when requesting one lock state, additional lock states
 1.25091 +** are inserted in between.  The locking might fail on one of the later
 1.25092 +** transitions leaving the lock state different from what it started but
 1.25093 +** still short of its goal.  The following chart shows the allowed
 1.25094 +** transitions and the inserted intermediate states:
 1.25095 +**
 1.25096 +**    UNLOCKED -> SHARED
 1.25097 +**    SHARED -> RESERVED
 1.25098 +**    SHARED -> (PENDING) -> EXCLUSIVE
 1.25099 +**    RESERVED -> (PENDING) -> EXCLUSIVE
 1.25100 +**    PENDING -> EXCLUSIVE
 1.25101 +**
 1.25102 +** flock() only really support EXCLUSIVE locks.  We track intermediate
 1.25103 +** lock states in the sqlite3_file structure, but all locks SHARED or
 1.25104 +** above are really EXCLUSIVE locks and exclude all other processes from
 1.25105 +** access the file.
 1.25106 +**
 1.25107 +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 1.25108 +** routine to lower a locking level.
 1.25109 +*/
 1.25110 +static int flockLock(sqlite3_file *id, int eFileLock) {
 1.25111 +  int rc = SQLITE_OK;
 1.25112 +  unixFile *pFile = (unixFile*)id;
 1.25113 +
 1.25114 +  assert( pFile );
 1.25115 +
 1.25116 +  /* if we already have a lock, it is exclusive.  
 1.25117 +  ** Just adjust level and punt on outta here. */
 1.25118 +  if (pFile->eFileLock > NO_LOCK) {
 1.25119 +    pFile->eFileLock = eFileLock;
 1.25120 +    return SQLITE_OK;
 1.25121 +  }
 1.25122 +  
 1.25123 +  /* grab an exclusive lock */
 1.25124 +  
 1.25125 +  if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
 1.25126 +    int tErrno = errno;
 1.25127 +    /* didn't get, must be busy */
 1.25128 +    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 1.25129 +    if( IS_LOCK_ERROR(rc) ){
 1.25130 +      pFile->lastErrno = tErrno;
 1.25131 +    }
 1.25132 +  } else {
 1.25133 +    /* got it, set the type and return ok */
 1.25134 +    pFile->eFileLock = eFileLock;
 1.25135 +  }
 1.25136 +  OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
 1.25137 +           rc==SQLITE_OK ? "ok" : "failed"));
 1.25138 +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 1.25139 +  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
 1.25140 +    rc = SQLITE_BUSY;
 1.25141 +  }
 1.25142 +#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 1.25143 +  return rc;
 1.25144 +}
 1.25145 +
 1.25146 +
 1.25147 +/*
 1.25148 +** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.25149 +** must be either NO_LOCK or SHARED_LOCK.
 1.25150 +**
 1.25151 +** If the locking level of the file descriptor is already at or below
 1.25152 +** the requested locking level, this routine is a no-op.
 1.25153 +*/
 1.25154 +static int flockUnlock(sqlite3_file *id, int eFileLock) {
 1.25155 +  unixFile *pFile = (unixFile*)id;
 1.25156 +  
 1.25157 +  assert( pFile );
 1.25158 +  OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
 1.25159 +           pFile->eFileLock, getpid()));
 1.25160 +  assert( eFileLock<=SHARED_LOCK );
 1.25161 +  
 1.25162 +  /* no-op if possible */
 1.25163 +  if( pFile->eFileLock==eFileLock ){
 1.25164 +    return SQLITE_OK;
 1.25165 +  }
 1.25166 +  
 1.25167 +  /* shared can just be set because we always have an exclusive */
 1.25168 +  if (eFileLock==SHARED_LOCK) {
 1.25169 +    pFile->eFileLock = eFileLock;
 1.25170 +    return SQLITE_OK;
 1.25171 +  }
 1.25172 +  
 1.25173 +  /* no, really, unlock. */
 1.25174 +  if( robust_flock(pFile->h, LOCK_UN) ){
 1.25175 +#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 1.25176 +    return SQLITE_OK;
 1.25177 +#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 1.25178 +    return SQLITE_IOERR_UNLOCK;
 1.25179 +  }else{
 1.25180 +    pFile->eFileLock = NO_LOCK;
 1.25181 +    return SQLITE_OK;
 1.25182 +  }
 1.25183 +}
 1.25184 +
 1.25185 +/*
 1.25186 +** Close a file.
 1.25187 +*/
 1.25188 +static int flockClose(sqlite3_file *id) {
 1.25189 +  int rc = SQLITE_OK;
 1.25190 +  if( id ){
 1.25191 +    flockUnlock(id, NO_LOCK);
 1.25192 +    rc = closeUnixFile(id);
 1.25193 +  }
 1.25194 +  return rc;
 1.25195 +}
 1.25196 +
 1.25197 +#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
 1.25198 +
 1.25199 +/******************* End of the flock lock implementation *********************
 1.25200 +******************************************************************************/
 1.25201 +
 1.25202 +/******************************************************************************
 1.25203 +************************ Begin Named Semaphore Locking ************************
 1.25204 +**
 1.25205 +** Named semaphore locking is only supported on VxWorks.
 1.25206 +**
 1.25207 +** Semaphore locking is like dot-lock and flock in that it really only
 1.25208 +** supports EXCLUSIVE locking.  Only a single process can read or write
 1.25209 +** the database file at a time.  This reduces potential concurrency, but
 1.25210 +** makes the lock implementation much easier.
 1.25211 +*/
 1.25212 +#if OS_VXWORKS
 1.25213 +
 1.25214 +/*
 1.25215 +** This routine checks if there is a RESERVED lock held on the specified
 1.25216 +** file by this or any other process. If such a lock is held, set *pResOut
 1.25217 +** to a non-zero value otherwise *pResOut is set to zero.  The return value
 1.25218 +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 1.25219 +*/
 1.25220 +static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
 1.25221 +  int rc = SQLITE_OK;
 1.25222 +  int reserved = 0;
 1.25223 +  unixFile *pFile = (unixFile*)id;
 1.25224 +
 1.25225 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 1.25226 +  
 1.25227 +  assert( pFile );
 1.25228 +
 1.25229 +  /* Check if a thread in this process holds such a lock */
 1.25230 +  if( pFile->eFileLock>SHARED_LOCK ){
 1.25231 +    reserved = 1;
 1.25232 +  }
 1.25233 +  
 1.25234 +  /* Otherwise see if some other process holds it. */
 1.25235 +  if( !reserved ){
 1.25236 +    sem_t *pSem = pFile->pInode->pSem;
 1.25237 +    struct stat statBuf;
 1.25238 +
 1.25239 +    if( sem_trywait(pSem)==-1 ){
 1.25240 +      int tErrno = errno;
 1.25241 +      if( EAGAIN != tErrno ){
 1.25242 +        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
 1.25243 +        pFile->lastErrno = tErrno;
 1.25244 +      } else {
 1.25245 +        /* someone else has the lock when we are in NO_LOCK */
 1.25246 +        reserved = (pFile->eFileLock < SHARED_LOCK);
 1.25247 +      }
 1.25248 +    }else{
 1.25249 +      /* we could have it if we want it */
 1.25250 +      sem_post(pSem);
 1.25251 +    }
 1.25252 +  }
 1.25253 +  OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
 1.25254 +
 1.25255 +  *pResOut = reserved;
 1.25256 +  return rc;
 1.25257 +}
 1.25258 +
 1.25259 +/*
 1.25260 +** Lock the file with the lock specified by parameter eFileLock - one
 1.25261 +** of the following:
 1.25262 +**
 1.25263 +**     (1) SHARED_LOCK
 1.25264 +**     (2) RESERVED_LOCK
 1.25265 +**     (3) PENDING_LOCK
 1.25266 +**     (4) EXCLUSIVE_LOCK
 1.25267 +**
 1.25268 +** Sometimes when requesting one lock state, additional lock states
 1.25269 +** are inserted in between.  The locking might fail on one of the later
 1.25270 +** transitions leaving the lock state different from what it started but
 1.25271 +** still short of its goal.  The following chart shows the allowed
 1.25272 +** transitions and the inserted intermediate states:
 1.25273 +**
 1.25274 +**    UNLOCKED -> SHARED
 1.25275 +**    SHARED -> RESERVED
 1.25276 +**    SHARED -> (PENDING) -> EXCLUSIVE
 1.25277 +**    RESERVED -> (PENDING) -> EXCLUSIVE
 1.25278 +**    PENDING -> EXCLUSIVE
 1.25279 +**
 1.25280 +** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
 1.25281 +** lock states in the sqlite3_file structure, but all locks SHARED or
 1.25282 +** above are really EXCLUSIVE locks and exclude all other processes from
 1.25283 +** access the file.
 1.25284 +**
 1.25285 +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 1.25286 +** routine to lower a locking level.
 1.25287 +*/
 1.25288 +static int semLock(sqlite3_file *id, int eFileLock) {
 1.25289 +  unixFile *pFile = (unixFile*)id;
 1.25290 +  int fd;
 1.25291 +  sem_t *pSem = pFile->pInode->pSem;
 1.25292 +  int rc = SQLITE_OK;
 1.25293 +
 1.25294 +  /* if we already have a lock, it is exclusive.  
 1.25295 +  ** Just adjust level and punt on outta here. */
 1.25296 +  if (pFile->eFileLock > NO_LOCK) {
 1.25297 +    pFile->eFileLock = eFileLock;
 1.25298 +    rc = SQLITE_OK;
 1.25299 +    goto sem_end_lock;
 1.25300 +  }
 1.25301 +  
 1.25302 +  /* lock semaphore now but bail out when already locked. */
 1.25303 +  if( sem_trywait(pSem)==-1 ){
 1.25304 +    rc = SQLITE_BUSY;
 1.25305 +    goto sem_end_lock;
 1.25306 +  }
 1.25307 +
 1.25308 +  /* got it, set the type and return ok */
 1.25309 +  pFile->eFileLock = eFileLock;
 1.25310 +
 1.25311 + sem_end_lock:
 1.25312 +  return rc;
 1.25313 +}
 1.25314 +
 1.25315 +/*
 1.25316 +** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.25317 +** must be either NO_LOCK or SHARED_LOCK.
 1.25318 +**
 1.25319 +** If the locking level of the file descriptor is already at or below
 1.25320 +** the requested locking level, this routine is a no-op.
 1.25321 +*/
 1.25322 +static int semUnlock(sqlite3_file *id, int eFileLock) {
 1.25323 +  unixFile *pFile = (unixFile*)id;
 1.25324 +  sem_t *pSem = pFile->pInode->pSem;
 1.25325 +
 1.25326 +  assert( pFile );
 1.25327 +  assert( pSem );
 1.25328 +  OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
 1.25329 +           pFile->eFileLock, getpid()));
 1.25330 +  assert( eFileLock<=SHARED_LOCK );
 1.25331 +  
 1.25332 +  /* no-op if possible */
 1.25333 +  if( pFile->eFileLock==eFileLock ){
 1.25334 +    return SQLITE_OK;
 1.25335 +  }
 1.25336 +  
 1.25337 +  /* shared can just be set because we always have an exclusive */
 1.25338 +  if (eFileLock==SHARED_LOCK) {
 1.25339 +    pFile->eFileLock = eFileLock;
 1.25340 +    return SQLITE_OK;
 1.25341 +  }
 1.25342 +  
 1.25343 +  /* no, really unlock. */
 1.25344 +  if ( sem_post(pSem)==-1 ) {
 1.25345 +    int rc, tErrno = errno;
 1.25346 +    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
 1.25347 +    if( IS_LOCK_ERROR(rc) ){
 1.25348 +      pFile->lastErrno = tErrno;
 1.25349 +    }
 1.25350 +    return rc; 
 1.25351 +  }
 1.25352 +  pFile->eFileLock = NO_LOCK;
 1.25353 +  return SQLITE_OK;
 1.25354 +}
 1.25355 +
 1.25356 +/*
 1.25357 + ** Close a file.
 1.25358 + */
 1.25359 +static int semClose(sqlite3_file *id) {
 1.25360 +  if( id ){
 1.25361 +    unixFile *pFile = (unixFile*)id;
 1.25362 +    semUnlock(id, NO_LOCK);
 1.25363 +    assert( pFile );
 1.25364 +    unixEnterMutex();
 1.25365 +    releaseInodeInfo(pFile);
 1.25366 +    unixLeaveMutex();
 1.25367 +    closeUnixFile(id);
 1.25368 +  }
 1.25369 +  return SQLITE_OK;
 1.25370 +}
 1.25371 +
 1.25372 +#endif /* OS_VXWORKS */
 1.25373 +/*
 1.25374 +** Named semaphore locking is only available on VxWorks.
 1.25375 +**
 1.25376 +*************** End of the named semaphore lock implementation ****************
 1.25377 +******************************************************************************/
 1.25378 +
 1.25379 +
 1.25380 +/******************************************************************************
 1.25381 +*************************** Begin AFP Locking *********************************
 1.25382 +**
 1.25383 +** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
 1.25384 +** on Apple Macintosh computers - both OS9 and OSX.
 1.25385 +**
 1.25386 +** Third-party implementations of AFP are available.  But this code here
 1.25387 +** only works on OSX.
 1.25388 +*/
 1.25389 +
 1.25390 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.25391 +/*
 1.25392 +** The afpLockingContext structure contains all afp lock specific state
 1.25393 +*/
 1.25394 +typedef struct afpLockingContext afpLockingContext;
 1.25395 +struct afpLockingContext {
 1.25396 +  int reserved;
 1.25397 +  const char *dbPath;             /* Name of the open file */
 1.25398 +};
 1.25399 +
 1.25400 +struct ByteRangeLockPB2
 1.25401 +{
 1.25402 +  unsigned long long offset;        /* offset to first byte to lock */
 1.25403 +  unsigned long long length;        /* nbr of bytes to lock */
 1.25404 +  unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
 1.25405 +  unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
 1.25406 +  unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
 1.25407 +  int fd;                           /* file desc to assoc this lock with */
 1.25408 +};
 1.25409 +
 1.25410 +#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
 1.25411 +
 1.25412 +/*
 1.25413 +** This is a utility for setting or clearing a bit-range lock on an
 1.25414 +** AFP filesystem.
 1.25415 +** 
 1.25416 +** Return SQLITE_OK on success, SQLITE_BUSY on failure.
 1.25417 +*/
 1.25418 +static int afpSetLock(
 1.25419 +  const char *path,              /* Name of the file to be locked or unlocked */
 1.25420 +  unixFile *pFile,               /* Open file descriptor on path */
 1.25421 +  unsigned long long offset,     /* First byte to be locked */
 1.25422 +  unsigned long long length,     /* Number of bytes to lock */
 1.25423 +  int setLockFlag                /* True to set lock.  False to clear lock */
 1.25424 +){
 1.25425 +  struct ByteRangeLockPB2 pb;
 1.25426 +  int err;
 1.25427 +  
 1.25428 +  pb.unLockFlag = setLockFlag ? 0 : 1;
 1.25429 +  pb.startEndFlag = 0;
 1.25430 +  pb.offset = offset;
 1.25431 +  pb.length = length; 
 1.25432 +  pb.fd = pFile->h;
 1.25433 +  
 1.25434 +  OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", 
 1.25435 +    (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
 1.25436 +    offset, length));
 1.25437 +  err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
 1.25438 +  if ( err==-1 ) {
 1.25439 +    int rc;
 1.25440 +    int tErrno = errno;
 1.25441 +    OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
 1.25442 +             path, tErrno, strerror(tErrno)));
 1.25443 +#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
 1.25444 +    rc = SQLITE_BUSY;
 1.25445 +#else
 1.25446 +    rc = sqliteErrorFromPosixError(tErrno,
 1.25447 +                    setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
 1.25448 +#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
 1.25449 +    if( IS_LOCK_ERROR(rc) ){
 1.25450 +      pFile->lastErrno = tErrno;
 1.25451 +    }
 1.25452 +    return rc;
 1.25453 +  } else {
 1.25454 +    return SQLITE_OK;
 1.25455 +  }
 1.25456 +}
 1.25457 +
 1.25458 +/*
 1.25459 +** This routine checks if there is a RESERVED lock held on the specified
 1.25460 +** file by this or any other process. If such a lock is held, set *pResOut
 1.25461 +** to a non-zero value otherwise *pResOut is set to zero.  The return value
 1.25462 +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 1.25463 +*/
 1.25464 +static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
 1.25465 +  int rc = SQLITE_OK;
 1.25466 +  int reserved = 0;
 1.25467 +  unixFile *pFile = (unixFile*)id;
 1.25468 +  afpLockingContext *context;
 1.25469 +  
 1.25470 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 1.25471 +  
 1.25472 +  assert( pFile );
 1.25473 +  context = (afpLockingContext *) pFile->lockingContext;
 1.25474 +  if( context->reserved ){
 1.25475 +    *pResOut = 1;
 1.25476 +    return SQLITE_OK;
 1.25477 +  }
 1.25478 +  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
 1.25479 +  
 1.25480 +  /* Check if a thread in this process holds such a lock */
 1.25481 +  if( pFile->pInode->eFileLock>SHARED_LOCK ){
 1.25482 +    reserved = 1;
 1.25483 +  }
 1.25484 +  
 1.25485 +  /* Otherwise see if some other process holds it.
 1.25486 +   */
 1.25487 +  if( !reserved ){
 1.25488 +    /* lock the RESERVED byte */
 1.25489 +    int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
 1.25490 +    if( SQLITE_OK==lrc ){
 1.25491 +      /* if we succeeded in taking the reserved lock, unlock it to restore
 1.25492 +      ** the original state */
 1.25493 +      lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
 1.25494 +    } else {
 1.25495 +      /* if we failed to get the lock then someone else must have it */
 1.25496 +      reserved = 1;
 1.25497 +    }
 1.25498 +    if( IS_LOCK_ERROR(lrc) ){
 1.25499 +      rc=lrc;
 1.25500 +    }
 1.25501 +  }
 1.25502 +  
 1.25503 +  unixLeaveMutex();
 1.25504 +  OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
 1.25505 +  
 1.25506 +  *pResOut = reserved;
 1.25507 +  return rc;
 1.25508 +}
 1.25509 +
 1.25510 +/*
 1.25511 +** Lock the file with the lock specified by parameter eFileLock - one
 1.25512 +** of the following:
 1.25513 +**
 1.25514 +**     (1) SHARED_LOCK
 1.25515 +**     (2) RESERVED_LOCK
 1.25516 +**     (3) PENDING_LOCK
 1.25517 +**     (4) EXCLUSIVE_LOCK
 1.25518 +**
 1.25519 +** Sometimes when requesting one lock state, additional lock states
 1.25520 +** are inserted in between.  The locking might fail on one of the later
 1.25521 +** transitions leaving the lock state different from what it started but
 1.25522 +** still short of its goal.  The following chart shows the allowed
 1.25523 +** transitions and the inserted intermediate states:
 1.25524 +**
 1.25525 +**    UNLOCKED -> SHARED
 1.25526 +**    SHARED -> RESERVED
 1.25527 +**    SHARED -> (PENDING) -> EXCLUSIVE
 1.25528 +**    RESERVED -> (PENDING) -> EXCLUSIVE
 1.25529 +**    PENDING -> EXCLUSIVE
 1.25530 +**
 1.25531 +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 1.25532 +** routine to lower a locking level.
 1.25533 +*/
 1.25534 +static int afpLock(sqlite3_file *id, int eFileLock){
 1.25535 +  int rc = SQLITE_OK;
 1.25536 +  unixFile *pFile = (unixFile*)id;
 1.25537 +  unixInodeInfo *pInode = pFile->pInode;
 1.25538 +  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
 1.25539 +  
 1.25540 +  assert( pFile );
 1.25541 +  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
 1.25542 +           azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 1.25543 +           azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
 1.25544 +
 1.25545 +  /* If there is already a lock of this type or more restrictive on the
 1.25546 +  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
 1.25547 +  ** unixEnterMutex() hasn't been called yet.
 1.25548 +  */
 1.25549 +  if( pFile->eFileLock>=eFileLock ){
 1.25550 +    OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
 1.25551 +           azFileLock(eFileLock)));
 1.25552 +    return SQLITE_OK;
 1.25553 +  }
 1.25554 +
 1.25555 +  /* Make sure the locking sequence is correct
 1.25556 +  **  (1) We never move from unlocked to anything higher than shared lock.
 1.25557 +  **  (2) SQLite never explicitly requests a pendig lock.
 1.25558 +  **  (3) A shared lock is always held when a reserve lock is requested.
 1.25559 +  */
 1.25560 +  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
 1.25561 +  assert( eFileLock!=PENDING_LOCK );
 1.25562 +  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
 1.25563 +  
 1.25564 +  /* This mutex is needed because pFile->pInode is shared across threads
 1.25565 +  */
 1.25566 +  unixEnterMutex();
 1.25567 +  pInode = pFile->pInode;
 1.25568 +
 1.25569 +  /* If some thread using this PID has a lock via a different unixFile*
 1.25570 +  ** handle that precludes the requested lock, return BUSY.
 1.25571 +  */
 1.25572 +  if( (pFile->eFileLock!=pInode->eFileLock && 
 1.25573 +       (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
 1.25574 +     ){
 1.25575 +    rc = SQLITE_BUSY;
 1.25576 +    goto afp_end_lock;
 1.25577 +  }
 1.25578 +  
 1.25579 +  /* If a SHARED lock is requested, and some thread using this PID already
 1.25580 +  ** has a SHARED or RESERVED lock, then increment reference counts and
 1.25581 +  ** return SQLITE_OK.
 1.25582 +  */
 1.25583 +  if( eFileLock==SHARED_LOCK && 
 1.25584 +     (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
 1.25585 +    assert( eFileLock==SHARED_LOCK );
 1.25586 +    assert( pFile->eFileLock==0 );
 1.25587 +    assert( pInode->nShared>0 );
 1.25588 +    pFile->eFileLock = SHARED_LOCK;
 1.25589 +    pInode->nShared++;
 1.25590 +    pInode->nLock++;
 1.25591 +    goto afp_end_lock;
 1.25592 +  }
 1.25593 +    
 1.25594 +  /* A PENDING lock is needed before acquiring a SHARED lock and before
 1.25595 +  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
 1.25596 +  ** be released.
 1.25597 +  */
 1.25598 +  if( eFileLock==SHARED_LOCK 
 1.25599 +      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
 1.25600 +  ){
 1.25601 +    int failed;
 1.25602 +    failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
 1.25603 +    if (failed) {
 1.25604 +      rc = failed;
 1.25605 +      goto afp_end_lock;
 1.25606 +    }
 1.25607 +  }
 1.25608 +  
 1.25609 +  /* If control gets to this point, then actually go ahead and make
 1.25610 +  ** operating system calls for the specified lock.
 1.25611 +  */
 1.25612 +  if( eFileLock==SHARED_LOCK ){
 1.25613 +    int lrc1, lrc2, lrc1Errno = 0;
 1.25614 +    long lk, mask;
 1.25615 +    
 1.25616 +    assert( pInode->nShared==0 );
 1.25617 +    assert( pInode->eFileLock==0 );
 1.25618 +        
 1.25619 +    mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
 1.25620 +    /* Now get the read-lock SHARED_LOCK */
 1.25621 +    /* note that the quality of the randomness doesn't matter that much */
 1.25622 +    lk = random(); 
 1.25623 +    pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
 1.25624 +    lrc1 = afpSetLock(context->dbPath, pFile, 
 1.25625 +          SHARED_FIRST+pInode->sharedByte, 1, 1);
 1.25626 +    if( IS_LOCK_ERROR(lrc1) ){
 1.25627 +      lrc1Errno = pFile->lastErrno;
 1.25628 +    }
 1.25629 +    /* Drop the temporary PENDING lock */
 1.25630 +    lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
 1.25631 +    
 1.25632 +    if( IS_LOCK_ERROR(lrc1) ) {
 1.25633 +      pFile->lastErrno = lrc1Errno;
 1.25634 +      rc = lrc1;
 1.25635 +      goto afp_end_lock;
 1.25636 +    } else if( IS_LOCK_ERROR(lrc2) ){
 1.25637 +      rc = lrc2;
 1.25638 +      goto afp_end_lock;
 1.25639 +    } else if( lrc1 != SQLITE_OK ) {
 1.25640 +      rc = lrc1;
 1.25641 +    } else {
 1.25642 +      pFile->eFileLock = SHARED_LOCK;
 1.25643 +      pInode->nLock++;
 1.25644 +      pInode->nShared = 1;
 1.25645 +    }
 1.25646 +  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
 1.25647 +    /* We are trying for an exclusive lock but another thread in this
 1.25648 +     ** same process is still holding a shared lock. */
 1.25649 +    rc = SQLITE_BUSY;
 1.25650 +  }else{
 1.25651 +    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
 1.25652 +    ** assumed that there is a SHARED or greater lock on the file
 1.25653 +    ** already.
 1.25654 +    */
 1.25655 +    int failed = 0;
 1.25656 +    assert( 0!=pFile->eFileLock );
 1.25657 +    if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
 1.25658 +        /* Acquire a RESERVED lock */
 1.25659 +        failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
 1.25660 +      if( !failed ){
 1.25661 +        context->reserved = 1;
 1.25662 +      }
 1.25663 +    }
 1.25664 +    if (!failed && eFileLock == EXCLUSIVE_LOCK) {
 1.25665 +      /* Acquire an EXCLUSIVE lock */
 1.25666 +        
 1.25667 +      /* Remove the shared lock before trying the range.  we'll need to 
 1.25668 +      ** reestablish the shared lock if we can't get the  afpUnlock
 1.25669 +      */
 1.25670 +      if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
 1.25671 +                         pInode->sharedByte, 1, 0)) ){
 1.25672 +        int failed2 = SQLITE_OK;
 1.25673 +        /* now attemmpt to get the exclusive lock range */
 1.25674 +        failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
 1.25675 +                               SHARED_SIZE, 1);
 1.25676 +        if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
 1.25677 +                       SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
 1.25678 +          /* Can't reestablish the shared lock.  Sqlite can't deal, this is
 1.25679 +          ** a critical I/O error
 1.25680 +          */
 1.25681 +          rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
 1.25682 +               SQLITE_IOERR_LOCK;
 1.25683 +          goto afp_end_lock;
 1.25684 +        } 
 1.25685 +      }else{
 1.25686 +        rc = failed; 
 1.25687 +      }
 1.25688 +    }
 1.25689 +    if( failed ){
 1.25690 +      rc = failed;
 1.25691 +    }
 1.25692 +  }
 1.25693 +  
 1.25694 +  if( rc==SQLITE_OK ){
 1.25695 +    pFile->eFileLock = eFileLock;
 1.25696 +    pInode->eFileLock = eFileLock;
 1.25697 +  }else if( eFileLock==EXCLUSIVE_LOCK ){
 1.25698 +    pFile->eFileLock = PENDING_LOCK;
 1.25699 +    pInode->eFileLock = PENDING_LOCK;
 1.25700 +  }
 1.25701 +  
 1.25702 +afp_end_lock:
 1.25703 +  unixLeaveMutex();
 1.25704 +  OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), 
 1.25705 +         rc==SQLITE_OK ? "ok" : "failed"));
 1.25706 +  return rc;
 1.25707 +}
 1.25708 +
 1.25709 +/*
 1.25710 +** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.25711 +** must be either NO_LOCK or SHARED_LOCK.
 1.25712 +**
 1.25713 +** If the locking level of the file descriptor is already at or below
 1.25714 +** the requested locking level, this routine is a no-op.
 1.25715 +*/
 1.25716 +static int afpUnlock(sqlite3_file *id, int eFileLock) {
 1.25717 +  int rc = SQLITE_OK;
 1.25718 +  unixFile *pFile = (unixFile*)id;
 1.25719 +  unixInodeInfo *pInode;
 1.25720 +  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
 1.25721 +  int skipShared = 0;
 1.25722 +#ifdef SQLITE_TEST
 1.25723 +  int h = pFile->h;
 1.25724 +#endif
 1.25725 +
 1.25726 +  assert( pFile );
 1.25727 +  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
 1.25728 +           pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 1.25729 +           getpid()));
 1.25730 +
 1.25731 +  assert( eFileLock<=SHARED_LOCK );
 1.25732 +  if( pFile->eFileLock<=eFileLock ){
 1.25733 +    return SQLITE_OK;
 1.25734 +  }
 1.25735 +  unixEnterMutex();
 1.25736 +  pInode = pFile->pInode;
 1.25737 +  assert( pInode->nShared!=0 );
 1.25738 +  if( pFile->eFileLock>SHARED_LOCK ){
 1.25739 +    assert( pInode->eFileLock==pFile->eFileLock );
 1.25740 +    SimulateIOErrorBenign(1);
 1.25741 +    SimulateIOError( h=(-1) )
 1.25742 +    SimulateIOErrorBenign(0);
 1.25743 +    
 1.25744 +#ifdef SQLITE_DEBUG
 1.25745 +    /* When reducing a lock such that other processes can start
 1.25746 +    ** reading the database file again, make sure that the
 1.25747 +    ** transaction counter was updated if any part of the database
 1.25748 +    ** file changed.  If the transaction counter is not updated,
 1.25749 +    ** other connections to the same file might not realize that
 1.25750 +    ** the file has changed and hence might not know to flush their
 1.25751 +    ** cache.  The use of a stale cache can lead to database corruption.
 1.25752 +    */
 1.25753 +    assert( pFile->inNormalWrite==0
 1.25754 +           || pFile->dbUpdate==0
 1.25755 +           || pFile->transCntrChng==1 );
 1.25756 +    pFile->inNormalWrite = 0;
 1.25757 +#endif
 1.25758 +    
 1.25759 +    if( pFile->eFileLock==EXCLUSIVE_LOCK ){
 1.25760 +      rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
 1.25761 +      if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
 1.25762 +        /* only re-establish the shared lock if necessary */
 1.25763 +        int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
 1.25764 +        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
 1.25765 +      } else {
 1.25766 +        skipShared = 1;
 1.25767 +      }
 1.25768 +    }
 1.25769 +    if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
 1.25770 +      rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
 1.25771 +    } 
 1.25772 +    if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
 1.25773 +      rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
 1.25774 +      if( !rc ){ 
 1.25775 +        context->reserved = 0; 
 1.25776 +      }
 1.25777 +    }
 1.25778 +    if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
 1.25779 +      pInode->eFileLock = SHARED_LOCK;
 1.25780 +    }
 1.25781 +  }
 1.25782 +  if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
 1.25783 +
 1.25784 +    /* Decrement the shared lock counter.  Release the lock using an
 1.25785 +    ** OS call only when all threads in this same process have released
 1.25786 +    ** the lock.
 1.25787 +    */
 1.25788 +    unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
 1.25789 +    pInode->nShared--;
 1.25790 +    if( pInode->nShared==0 ){
 1.25791 +      SimulateIOErrorBenign(1);
 1.25792 +      SimulateIOError( h=(-1) )
 1.25793 +      SimulateIOErrorBenign(0);
 1.25794 +      if( !skipShared ){
 1.25795 +        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
 1.25796 +      }
 1.25797 +      if( !rc ){
 1.25798 +        pInode->eFileLock = NO_LOCK;
 1.25799 +        pFile->eFileLock = NO_LOCK;
 1.25800 +      }
 1.25801 +    }
 1.25802 +    if( rc==SQLITE_OK ){
 1.25803 +      pInode->nLock--;
 1.25804 +      assert( pInode->nLock>=0 );
 1.25805 +      if( pInode->nLock==0 ){
 1.25806 +        closePendingFds(pFile);
 1.25807 +      }
 1.25808 +    }
 1.25809 +  }
 1.25810 +  
 1.25811 +  unixLeaveMutex();
 1.25812 +  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 1.25813 +  return rc;
 1.25814 +}
 1.25815 +
 1.25816 +/*
 1.25817 +** Close a file & cleanup AFP specific locking context 
 1.25818 +*/
 1.25819 +static int afpClose(sqlite3_file *id) {
 1.25820 +  int rc = SQLITE_OK;
 1.25821 +  if( id ){
 1.25822 +    unixFile *pFile = (unixFile*)id;
 1.25823 +    afpUnlock(id, NO_LOCK);
 1.25824 +    unixEnterMutex();
 1.25825 +    if( pFile->pInode && pFile->pInode->nLock ){
 1.25826 +      /* If there are outstanding locks, do not actually close the file just
 1.25827 +      ** yet because that would clear those locks.  Instead, add the file
 1.25828 +      ** descriptor to pInode->aPending.  It will be automatically closed when
 1.25829 +      ** the last lock is cleared.
 1.25830 +      */
 1.25831 +      setPendingFd(pFile);
 1.25832 +    }
 1.25833 +    releaseInodeInfo(pFile);
 1.25834 +    sqlite3_free(pFile->lockingContext);
 1.25835 +    rc = closeUnixFile(id);
 1.25836 +    unixLeaveMutex();
 1.25837 +  }
 1.25838 +  return rc;
 1.25839 +}
 1.25840 +
 1.25841 +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 1.25842 +/*
 1.25843 +** The code above is the AFP lock implementation.  The code is specific
 1.25844 +** to MacOSX and does not work on other unix platforms.  No alternative
 1.25845 +** is available.  If you don't compile for a mac, then the "unix-afp"
 1.25846 +** VFS is not available.
 1.25847 +**
 1.25848 +********************* End of the AFP lock implementation **********************
 1.25849 +******************************************************************************/
 1.25850 +
 1.25851 +/******************************************************************************
 1.25852 +*************************** Begin NFS Locking ********************************/
 1.25853 +
 1.25854 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.25855 +/*
 1.25856 + ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.25857 + ** must be either NO_LOCK or SHARED_LOCK.
 1.25858 + **
 1.25859 + ** If the locking level of the file descriptor is already at or below
 1.25860 + ** the requested locking level, this routine is a no-op.
 1.25861 + */
 1.25862 +static int nfsUnlock(sqlite3_file *id, int eFileLock){
 1.25863 +  return posixUnlock(id, eFileLock, 1);
 1.25864 +}
 1.25865 +
 1.25866 +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 1.25867 +/*
 1.25868 +** The code above is the NFS lock implementation.  The code is specific
 1.25869 +** to MacOSX and does not work on other unix platforms.  No alternative
 1.25870 +** is available.  
 1.25871 +**
 1.25872 +********************* End of the NFS lock implementation **********************
 1.25873 +******************************************************************************/
 1.25874 +
 1.25875 +/******************************************************************************
 1.25876 +**************** Non-locking sqlite3_file methods *****************************
 1.25877 +**
 1.25878 +** The next division contains implementations for all methods of the 
 1.25879 +** sqlite3_file object other than the locking methods.  The locking
 1.25880 +** methods were defined in divisions above (one locking method per
 1.25881 +** division).  Those methods that are common to all locking modes
 1.25882 +** are gather together into this division.
 1.25883 +*/
 1.25884 +
 1.25885 +/*
 1.25886 +** Seek to the offset passed as the second argument, then read cnt 
 1.25887 +** bytes into pBuf. Return the number of bytes actually read.
 1.25888 +**
 1.25889 +** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
 1.25890 +** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
 1.25891 +** one system to another.  Since SQLite does not define USE_PREAD
 1.25892 +** any any form by default, we will not attempt to define _XOPEN_SOURCE.
 1.25893 +** See tickets #2741 and #2681.
 1.25894 +**
 1.25895 +** To avoid stomping the errno value on a failed read the lastErrno value
 1.25896 +** is set before returning.
 1.25897 +*/
 1.25898 +static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
 1.25899 +  int got;
 1.25900 +  int prior = 0;
 1.25901 +#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
 1.25902 +  i64 newOffset;
 1.25903 +#endif
 1.25904 +  TIMER_START;
 1.25905 +  assert( cnt==(cnt&0x1ffff) );
 1.25906 +  cnt &= 0x1ffff;
 1.25907 +  do{
 1.25908 +#if defined(USE_PREAD)
 1.25909 +    got = osPread(id->h, pBuf, cnt, offset);
 1.25910 +    SimulateIOError( got = -1 );
 1.25911 +#elif defined(USE_PREAD64)
 1.25912 +    got = osPread64(id->h, pBuf, cnt, offset);
 1.25913 +    SimulateIOError( got = -1 );
 1.25914 +#else
 1.25915 +    newOffset = lseek(id->h, offset, SEEK_SET);
 1.25916 +    SimulateIOError( newOffset-- );
 1.25917 +    if( newOffset!=offset ){
 1.25918 +      if( newOffset == -1 ){
 1.25919 +        ((unixFile*)id)->lastErrno = errno;
 1.25920 +      }else{
 1.25921 +        ((unixFile*)id)->lastErrno = 0;
 1.25922 +      }
 1.25923 +      return -1;
 1.25924 +    }
 1.25925 +    got = osRead(id->h, pBuf, cnt);
 1.25926 +#endif
 1.25927 +    if( got==cnt ) break;
 1.25928 +    if( got<0 ){
 1.25929 +      if( errno==EINTR ){ got = 1; continue; }
 1.25930 +      prior = 0;
 1.25931 +      ((unixFile*)id)->lastErrno = errno;
 1.25932 +      break;
 1.25933 +    }else if( got>0 ){
 1.25934 +      cnt -= got;
 1.25935 +      offset += got;
 1.25936 +      prior += got;
 1.25937 +      pBuf = (void*)(got + (char*)pBuf);
 1.25938 +    }
 1.25939 +  }while( got>0 );
 1.25940 +  TIMER_END;
 1.25941 +  OSTRACE(("READ    %-3d %5d %7lld %llu\n",
 1.25942 +            id->h, got+prior, offset-prior, TIMER_ELAPSED));
 1.25943 +  return got+prior;
 1.25944 +}
 1.25945 +
 1.25946 +/*
 1.25947 +** Read data from a file into a buffer.  Return SQLITE_OK if all
 1.25948 +** bytes were read successfully and SQLITE_IOERR if anything goes
 1.25949 +** wrong.
 1.25950 +*/
 1.25951 +static int unixRead(
 1.25952 +  sqlite3_file *id, 
 1.25953 +  void *pBuf, 
 1.25954 +  int amt,
 1.25955 +  sqlite3_int64 offset
 1.25956 +){
 1.25957 +  unixFile *pFile = (unixFile *)id;
 1.25958 +  int got;
 1.25959 +  assert( id );
 1.25960 +
 1.25961 +  /* If this is a database file (not a journal, master-journal or temp
 1.25962 +  ** file), the bytes in the locking range should never be read or written. */
 1.25963 +#if 0
 1.25964 +  assert( pFile->pUnused==0
 1.25965 +       || offset>=PENDING_BYTE+512
 1.25966 +       || offset+amt<=PENDING_BYTE 
 1.25967 +  );
 1.25968 +#endif
 1.25969 +
 1.25970 +  got = seekAndRead(pFile, offset, pBuf, amt);
 1.25971 +  if( got==amt ){
 1.25972 +    return SQLITE_OK;
 1.25973 +  }else if( got<0 ){
 1.25974 +    /* lastErrno set by seekAndRead */
 1.25975 +    return SQLITE_IOERR_READ;
 1.25976 +  }else{
 1.25977 +    pFile->lastErrno = 0; /* not a system error */
 1.25978 +    /* Unread parts of the buffer must be zero-filled */
 1.25979 +    memset(&((char*)pBuf)[got], 0, amt-got);
 1.25980 +    return SQLITE_IOERR_SHORT_READ;
 1.25981 +  }
 1.25982 +}
 1.25983 +
 1.25984 +/*
 1.25985 +** Seek to the offset in id->offset then read cnt bytes into pBuf.
 1.25986 +** Return the number of bytes actually read.  Update the offset.
 1.25987 +**
 1.25988 +** To avoid stomping the errno value on a failed write the lastErrno value
 1.25989 +** is set before returning.
 1.25990 +*/
 1.25991 +static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
 1.25992 +  int got;
 1.25993 +#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
 1.25994 +  i64 newOffset;
 1.25995 +#endif
 1.25996 +  assert( cnt==(cnt&0x1ffff) );
 1.25997 +  cnt &= 0x1ffff;
 1.25998 +  TIMER_START;
 1.25999 +#if defined(USE_PREAD)
 1.26000 +  do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
 1.26001 +#elif defined(USE_PREAD64)
 1.26002 +  do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
 1.26003 +#else
 1.26004 +  do{
 1.26005 +    newOffset = lseek(id->h, offset, SEEK_SET);
 1.26006 +    SimulateIOError( newOffset-- );
 1.26007 +    if( newOffset!=offset ){
 1.26008 +      if( newOffset == -1 ){
 1.26009 +        ((unixFile*)id)->lastErrno = errno;
 1.26010 +      }else{
 1.26011 +        ((unixFile*)id)->lastErrno = 0;
 1.26012 +      }
 1.26013 +      return -1;
 1.26014 +    }
 1.26015 +    got = osWrite(id->h, pBuf, cnt);
 1.26016 +  }while( got<0 && errno==EINTR );
 1.26017 +#endif
 1.26018 +  TIMER_END;
 1.26019 +  if( got<0 ){
 1.26020 +    ((unixFile*)id)->lastErrno = errno;
 1.26021 +  }
 1.26022 +
 1.26023 +  OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
 1.26024 +  return got;
 1.26025 +}
 1.26026 +
 1.26027 +
 1.26028 +/*
 1.26029 +** Write data from a buffer into a file.  Return SQLITE_OK on success
 1.26030 +** or some other error code on failure.
 1.26031 +*/
 1.26032 +static int unixWrite(
 1.26033 +  sqlite3_file *id, 
 1.26034 +  const void *pBuf, 
 1.26035 +  int amt,
 1.26036 +  sqlite3_int64 offset 
 1.26037 +){
 1.26038 +  unixFile *pFile = (unixFile*)id;
 1.26039 +  int wrote = 0;
 1.26040 +  assert( id );
 1.26041 +  assert( amt>0 );
 1.26042 +
 1.26043 +  /* If this is a database file (not a journal, master-journal or temp
 1.26044 +  ** file), the bytes in the locking range should never be read or written. */
 1.26045 +#if 0
 1.26046 +  assert( pFile->pUnused==0
 1.26047 +       || offset>=PENDING_BYTE+512
 1.26048 +       || offset+amt<=PENDING_BYTE 
 1.26049 +  );
 1.26050 +#endif
 1.26051 +
 1.26052 +#ifdef SQLITE_DEBUG
 1.26053 +  /* If we are doing a normal write to a database file (as opposed to
 1.26054 +  ** doing a hot-journal rollback or a write to some file other than a
 1.26055 +  ** normal database file) then record the fact that the database
 1.26056 +  ** has changed.  If the transaction counter is modified, record that
 1.26057 +  ** fact too.
 1.26058 +  */
 1.26059 +  if( pFile->inNormalWrite ){
 1.26060 +    pFile->dbUpdate = 1;  /* The database has been modified */
 1.26061 +    if( offset<=24 && offset+amt>=27 ){
 1.26062 +      int rc;
 1.26063 +      char oldCntr[4];
 1.26064 +      SimulateIOErrorBenign(1);
 1.26065 +      rc = seekAndRead(pFile, 24, oldCntr, 4);
 1.26066 +      SimulateIOErrorBenign(0);
 1.26067 +      if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
 1.26068 +        pFile->transCntrChng = 1;  /* The transaction counter has changed */
 1.26069 +      }
 1.26070 +    }
 1.26071 +  }
 1.26072 +#endif
 1.26073 +
 1.26074 +  while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
 1.26075 +    amt -= wrote;
 1.26076 +    offset += wrote;
 1.26077 +    pBuf = &((char*)pBuf)[wrote];
 1.26078 +  }
 1.26079 +  SimulateIOError(( wrote=(-1), amt=1 ));
 1.26080 +  SimulateDiskfullError(( wrote=0, amt=1 ));
 1.26081 +
 1.26082 +  if( amt>0 ){
 1.26083 +    if( wrote<0 && pFile->lastErrno!=ENOSPC ){
 1.26084 +      /* lastErrno set by seekAndWrite */
 1.26085 +      return SQLITE_IOERR_WRITE;
 1.26086 +    }else{
 1.26087 +      pFile->lastErrno = 0; /* not a system error */
 1.26088 +      return SQLITE_FULL;
 1.26089 +    }
 1.26090 +  }
 1.26091 +
 1.26092 +  return SQLITE_OK;
 1.26093 +}
 1.26094 +
 1.26095 +#ifdef SQLITE_TEST
 1.26096 +/*
 1.26097 +** Count the number of fullsyncs and normal syncs.  This is used to test
 1.26098 +** that syncs and fullsyncs are occurring at the right times.
 1.26099 +*/
 1.26100 +SQLITE_API int sqlite3_sync_count = 0;
 1.26101 +SQLITE_API int sqlite3_fullsync_count = 0;
 1.26102 +#endif
 1.26103 +
 1.26104 +/*
 1.26105 +** We do not trust systems to provide a working fdatasync().  Some do.
 1.26106 +** Others do no.  To be safe, we will stick with the (slightly slower)
 1.26107 +** fsync(). If you know that your system does support fdatasync() correctly,
 1.26108 +** then simply compile with -Dfdatasync=fdatasync
 1.26109 +*/
 1.26110 +#if !defined(fdatasync)
 1.26111 +# define fdatasync fsync
 1.26112 +#endif
 1.26113 +
 1.26114 +/*
 1.26115 +** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
 1.26116 +** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
 1.26117 +** only available on Mac OS X.  But that could change.
 1.26118 +*/
 1.26119 +#ifdef F_FULLFSYNC
 1.26120 +# define HAVE_FULLFSYNC 1
 1.26121 +#else
 1.26122 +# define HAVE_FULLFSYNC 0
 1.26123 +#endif
 1.26124 +
 1.26125 +
 1.26126 +/*
 1.26127 +** The fsync() system call does not work as advertised on many
 1.26128 +** unix systems.  The following procedure is an attempt to make
 1.26129 +** it work better.
 1.26130 +**
 1.26131 +** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
 1.26132 +** for testing when we want to run through the test suite quickly.
 1.26133 +** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
 1.26134 +** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
 1.26135 +** or power failure will likely corrupt the database file.
 1.26136 +**
 1.26137 +** SQLite sets the dataOnly flag if the size of the file is unchanged.
 1.26138 +** The idea behind dataOnly is that it should only write the file content
 1.26139 +** to disk, not the inode.  We only set dataOnly if the file size is 
 1.26140 +** unchanged since the file size is part of the inode.  However, 
 1.26141 +** Ted Ts'o tells us that fdatasync() will also write the inode if the
 1.26142 +** file size has changed.  The only real difference between fdatasync()
 1.26143 +** and fsync(), Ted tells us, is that fdatasync() will not flush the
 1.26144 +** inode if the mtime or owner or other inode attributes have changed.
 1.26145 +** We only care about the file size, not the other file attributes, so
 1.26146 +** as far as SQLite is concerned, an fdatasync() is always adequate.
 1.26147 +** So, we always use fdatasync() if it is available, regardless of
 1.26148 +** the value of the dataOnly flag.
 1.26149 +*/
 1.26150 +static int full_fsync(int fd, int fullSync, int dataOnly){
 1.26151 +  int rc;
 1.26152 +
 1.26153 +  /* The following "ifdef/elif/else/" block has the same structure as
 1.26154 +  ** the one below. It is replicated here solely to avoid cluttering 
 1.26155 +  ** up the real code with the UNUSED_PARAMETER() macros.
 1.26156 +  */
 1.26157 +#ifdef SQLITE_NO_SYNC
 1.26158 +  UNUSED_PARAMETER(fd);
 1.26159 +  UNUSED_PARAMETER(fullSync);
 1.26160 +  UNUSED_PARAMETER(dataOnly);
 1.26161 +#elif HAVE_FULLFSYNC
 1.26162 +  UNUSED_PARAMETER(dataOnly);
 1.26163 +#else
 1.26164 +  UNUSED_PARAMETER(fullSync);
 1.26165 +  UNUSED_PARAMETER(dataOnly);
 1.26166 +#endif
 1.26167 +
 1.26168 +  /* Record the number of times that we do a normal fsync() and 
 1.26169 +  ** FULLSYNC.  This is used during testing to verify that this procedure
 1.26170 +  ** gets called with the correct arguments.
 1.26171 +  */
 1.26172 +#ifdef SQLITE_TEST
 1.26173 +  if( fullSync ) sqlite3_fullsync_count++;
 1.26174 +  sqlite3_sync_count++;
 1.26175 +#endif
 1.26176 +
 1.26177 +  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 1.26178 +  ** no-op
 1.26179 +  */
 1.26180 +#ifdef SQLITE_NO_SYNC
 1.26181 +  rc = SQLITE_OK;
 1.26182 +#elif HAVE_FULLFSYNC
 1.26183 +  if( fullSync ){
 1.26184 +    rc = osFcntl(fd, F_FULLFSYNC, 0);
 1.26185 +  }else{
 1.26186 +    rc = 1;
 1.26187 +  }
 1.26188 +  /* If the FULLFSYNC failed, fall back to attempting an fsync().
 1.26189 +  ** It shouldn't be possible for fullfsync to fail on the local 
 1.26190 +  ** file system (on OSX), so failure indicates that FULLFSYNC
 1.26191 +  ** isn't supported for this file system. So, attempt an fsync 
 1.26192 +  ** and (for now) ignore the overhead of a superfluous fcntl call.  
 1.26193 +  ** It'd be better to detect fullfsync support once and avoid 
 1.26194 +  ** the fcntl call every time sync is called.
 1.26195 +  */
 1.26196 +  if( rc ) rc = fsync(fd);
 1.26197 +
 1.26198 +#elif defined(__APPLE__)
 1.26199 +  /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
 1.26200 +  ** so currently we default to the macro that redefines fdatasync to fsync
 1.26201 +  */
 1.26202 +  rc = fsync(fd);
 1.26203 +#else 
 1.26204 +  rc = fdatasync(fd);
 1.26205 +#if OS_VXWORKS
 1.26206 +  if( rc==-1 && errno==ENOTSUP ){
 1.26207 +    rc = fsync(fd);
 1.26208 +  }
 1.26209 +#endif /* OS_VXWORKS */
 1.26210 +#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
 1.26211 +
 1.26212 +  if( OS_VXWORKS && rc!= -1 ){
 1.26213 +    rc = 0;
 1.26214 +  }
 1.26215 +  return rc;
 1.26216 +}
 1.26217 +
 1.26218 +/*
 1.26219 +** Open a file descriptor to the directory containing file zFilename.
 1.26220 +** If successful, *pFd is set to the opened file descriptor and
 1.26221 +** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
 1.26222 +** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
 1.26223 +** value.
 1.26224 +**
 1.26225 +** The directory file descriptor is used for only one thing - to
 1.26226 +** fsync() a directory to make sure file creation and deletion events
 1.26227 +** are flushed to disk.  Such fsyncs are not needed on newer
 1.26228 +** journaling filesystems, but are required on older filesystems.
 1.26229 +**
 1.26230 +** This routine can be overridden using the xSetSysCall interface.
 1.26231 +** The ability to override this routine was added in support of the
 1.26232 +** chromium sandbox.  Opening a directory is a security risk (we are
 1.26233 +** told) so making it overrideable allows the chromium sandbox to
 1.26234 +** replace this routine with a harmless no-op.  To make this routine
 1.26235 +** a no-op, replace it with a stub that returns SQLITE_OK but leaves
 1.26236 +** *pFd set to a negative number.
 1.26237 +**
 1.26238 +** If SQLITE_OK is returned, the caller is responsible for closing
 1.26239 +** the file descriptor *pFd using close().
 1.26240 +*/
 1.26241 +static int openDirectory(const char *zFilename, int *pFd){
 1.26242 +  int ii;
 1.26243 +  int fd = -1;
 1.26244 +  char zDirname[MAX_PATHNAME+1];
 1.26245 +
 1.26246 +  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
 1.26247 +  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
 1.26248 +  if( ii>0 ){
 1.26249 +    zDirname[ii] = '\0';
 1.26250 +    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
 1.26251 +    if( fd>=0 ){
 1.26252 +      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
 1.26253 +    }
 1.26254 +  }
 1.26255 +  *pFd = fd;
 1.26256 +  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
 1.26257 +}
 1.26258 +
 1.26259 +/*
 1.26260 +** Make sure all writes to a particular file are committed to disk.
 1.26261 +**
 1.26262 +** If dataOnly==0 then both the file itself and its metadata (file
 1.26263 +** size, access time, etc) are synced.  If dataOnly!=0 then only the
 1.26264 +** file data is synced.
 1.26265 +**
 1.26266 +** Under Unix, also make sure that the directory entry for the file
 1.26267 +** has been created by fsync-ing the directory that contains the file.
 1.26268 +** If we do not do this and we encounter a power failure, the directory
 1.26269 +** entry for the journal might not exist after we reboot.  The next
 1.26270 +** SQLite to access the file will not know that the journal exists (because
 1.26271 +** the directory entry for the journal was never created) and the transaction
 1.26272 +** will not roll back - possibly leading to database corruption.
 1.26273 +*/
 1.26274 +static int unixSync(sqlite3_file *id, int flags){
 1.26275 +  int rc;
 1.26276 +  unixFile *pFile = (unixFile*)id;
 1.26277 +
 1.26278 +  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
 1.26279 +  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
 1.26280 +
 1.26281 +  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
 1.26282 +  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
 1.26283 +      || (flags&0x0F)==SQLITE_SYNC_FULL
 1.26284 +  );
 1.26285 +
 1.26286 +  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
 1.26287 +  ** line is to test that doing so does not cause any problems.
 1.26288 +  */
 1.26289 +  SimulateDiskfullError( return SQLITE_FULL );
 1.26290 +
 1.26291 +  assert( pFile );
 1.26292 +  OSTRACE(("SYNC    %-3d\n", pFile->h));
 1.26293 +  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
 1.26294 +  SimulateIOError( rc=1 );
 1.26295 +  if( rc ){
 1.26296 +    pFile->lastErrno = errno;
 1.26297 +    return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
 1.26298 +  }
 1.26299 +
 1.26300 +  /* Also fsync the directory containing the file if the DIRSYNC flag
 1.26301 +  ** is set.  This is a one-time occurrance.  Many systems (examples: AIX)
 1.26302 +  ** are unable to fsync a directory, so ignore errors on the fsync.
 1.26303 +  */
 1.26304 +  if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
 1.26305 +    int dirfd;
 1.26306 +    OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
 1.26307 +            HAVE_FULLFSYNC, isFullsync));
 1.26308 +    rc = osOpenDirectory(pFile->zPath, &dirfd);
 1.26309 +    if( rc==SQLITE_OK && dirfd>=0 ){
 1.26310 +      full_fsync(dirfd, 0, 0);
 1.26311 +      robust_close(pFile, dirfd, __LINE__);
 1.26312 +    }else if( rc==SQLITE_CANTOPEN ){
 1.26313 +      rc = SQLITE_OK;
 1.26314 +    }
 1.26315 +    pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
 1.26316 +  }
 1.26317 +  return rc;
 1.26318 +}
 1.26319 +
 1.26320 +/*
 1.26321 +** Truncate an open file to a specified size
 1.26322 +*/
 1.26323 +static int unixTruncate(sqlite3_file *id, i64 nByte){
 1.26324 +  unixFile *pFile = (unixFile *)id;
 1.26325 +  int rc;
 1.26326 +  assert( pFile );
 1.26327 +  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
 1.26328 +
 1.26329 +  /* If the user has configured a chunk-size for this file, truncate the
 1.26330 +  ** file so that it consists of an integer number of chunks (i.e. the
 1.26331 +  ** actual file size after the operation may be larger than the requested
 1.26332 +  ** size).
 1.26333 +  */
 1.26334 +  if( pFile->szChunk>0 ){
 1.26335 +    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 1.26336 +  }
 1.26337 +
 1.26338 +  rc = robust_ftruncate(pFile->h, (off_t)nByte);
 1.26339 +  if( rc ){
 1.26340 +    pFile->lastErrno = errno;
 1.26341 +    return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 1.26342 +  }else{
 1.26343 +#ifdef SQLITE_DEBUG
 1.26344 +    /* If we are doing a normal write to a database file (as opposed to
 1.26345 +    ** doing a hot-journal rollback or a write to some file other than a
 1.26346 +    ** normal database file) and we truncate the file to zero length,
 1.26347 +    ** that effectively updates the change counter.  This might happen
 1.26348 +    ** when restoring a database using the backup API from a zero-length
 1.26349 +    ** source.
 1.26350 +    */
 1.26351 +    if( pFile->inNormalWrite && nByte==0 ){
 1.26352 +      pFile->transCntrChng = 1;
 1.26353 +    }
 1.26354 +#endif
 1.26355 +
 1.26356 +    return SQLITE_OK;
 1.26357 +  }
 1.26358 +}
 1.26359 +
 1.26360 +/*
 1.26361 +** Determine the current size of a file in bytes
 1.26362 +*/
 1.26363 +static int unixFileSize(sqlite3_file *id, i64 *pSize){
 1.26364 +  int rc;
 1.26365 +  struct stat buf;
 1.26366 +  assert( id );
 1.26367 +  rc = osFstat(((unixFile*)id)->h, &buf);
 1.26368 +  SimulateIOError( rc=1 );
 1.26369 +  if( rc!=0 ){
 1.26370 +    ((unixFile*)id)->lastErrno = errno;
 1.26371 +    return SQLITE_IOERR_FSTAT;
 1.26372 +  }
 1.26373 +  *pSize = buf.st_size;
 1.26374 +
 1.26375 +  /* When opening a zero-size database, the findInodeInfo() procedure
 1.26376 +  ** writes a single byte into that file in order to work around a bug
 1.26377 +  ** in the OS-X msdos filesystem.  In order to avoid problems with upper
 1.26378 +  ** layers, we need to report this file size as zero even though it is
 1.26379 +  ** really 1.   Ticket #3260.
 1.26380 +  */
 1.26381 +  if( *pSize==1 ) *pSize = 0;
 1.26382 +
 1.26383 +
 1.26384 +  return SQLITE_OK;
 1.26385 +}
 1.26386 +
 1.26387 +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 1.26388 +/*
 1.26389 +** Handler for proxy-locking file-control verbs.  Defined below in the
 1.26390 +** proxying locking division.
 1.26391 +*/
 1.26392 +static int proxyFileControl(sqlite3_file*,int,void*);
 1.26393 +#endif
 1.26394 +
 1.26395 +/* 
 1.26396 +** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 
 1.26397 +** file-control operation.  Enlarge the database to nBytes in size
 1.26398 +** (rounded up to the next chunk-size).  If the database is already
 1.26399 +** nBytes or larger, this routine is a no-op.
 1.26400 +*/
 1.26401 +static int fcntlSizeHint(unixFile *pFile, i64 nByte){
 1.26402 +  if( pFile->szChunk>0 ){
 1.26403 +    i64 nSize;                    /* Required file size */
 1.26404 +    struct stat buf;              /* Used to hold return values of fstat() */
 1.26405 +   
 1.26406 +    if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
 1.26407 +
 1.26408 +    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
 1.26409 +    if( nSize>(i64)buf.st_size ){
 1.26410 +
 1.26411 +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 1.26412 +      /* The code below is handling the return value of osFallocate() 
 1.26413 +      ** correctly. posix_fallocate() is defined to "returns zero on success, 
 1.26414 +      ** or an error number on  failure". See the manpage for details. */
 1.26415 +      int err;
 1.26416 +      do{
 1.26417 +        err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
 1.26418 +      }while( err==EINTR );
 1.26419 +      if( err ) return SQLITE_IOERR_WRITE;
 1.26420 +#else
 1.26421 +      /* If the OS does not have posix_fallocate(), fake it. First use
 1.26422 +      ** ftruncate() to set the file size, then write a single byte to
 1.26423 +      ** the last byte in each block within the extended region. This
 1.26424 +      ** is the same technique used by glibc to implement posix_fallocate()
 1.26425 +      ** on systems that do not have a real fallocate() system call.
 1.26426 +      */
 1.26427 +      int nBlk = buf.st_blksize;  /* File-system block size */
 1.26428 +      i64 iWrite;                 /* Next offset to write to */
 1.26429 +
 1.26430 +      if( robust_ftruncate(pFile->h, nSize) ){
 1.26431 +        pFile->lastErrno = errno;
 1.26432 +        return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 1.26433 +      }
 1.26434 +      iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
 1.26435 +      while( iWrite<nSize ){
 1.26436 +        int nWrite = seekAndWrite(pFile, iWrite, "", 1);
 1.26437 +        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
 1.26438 +        iWrite += nBlk;
 1.26439 +      }
 1.26440 +#endif
 1.26441 +    }
 1.26442 +  }
 1.26443 +
 1.26444 +  return SQLITE_OK;
 1.26445 +}
 1.26446 +
 1.26447 +/*
 1.26448 +** If *pArg is inititially negative then this is a query.  Set *pArg to
 1.26449 +** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
 1.26450 +**
 1.26451 +** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
 1.26452 +*/
 1.26453 +static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
 1.26454 +  if( *pArg<0 ){
 1.26455 +    *pArg = (pFile->ctrlFlags & mask)!=0;
 1.26456 +  }else if( (*pArg)==0 ){
 1.26457 +    pFile->ctrlFlags &= ~mask;
 1.26458 +  }else{
 1.26459 +    pFile->ctrlFlags |= mask;
 1.26460 +  }
 1.26461 +}
 1.26462 +
 1.26463 +/* Forward declaration */
 1.26464 +static int unixGetTempname(int nBuf, char *zBuf);
 1.26465 +
 1.26466 +/*
 1.26467 +** Information and control of an open file handle.
 1.26468 +*/
 1.26469 +static int unixFileControl(sqlite3_file *id, int op, void *pArg){
 1.26470 +  unixFile *pFile = (unixFile*)id;
 1.26471 +  switch( op ){
 1.26472 +    case SQLITE_FCNTL_LOCKSTATE: {
 1.26473 +      *(int*)pArg = pFile->eFileLock;
 1.26474 +      return SQLITE_OK;
 1.26475 +    }
 1.26476 +    case SQLITE_LAST_ERRNO: {
 1.26477 +      *(int*)pArg = pFile->lastErrno;
 1.26478 +      return SQLITE_OK;
 1.26479 +    }
 1.26480 +    case SQLITE_FCNTL_CHUNK_SIZE: {
 1.26481 +      pFile->szChunk = *(int *)pArg;
 1.26482 +      return SQLITE_OK;
 1.26483 +    }
 1.26484 +    case SQLITE_FCNTL_SIZE_HINT: {
 1.26485 +      int rc;
 1.26486 +      SimulateIOErrorBenign(1);
 1.26487 +      rc = fcntlSizeHint(pFile, *(i64 *)pArg);
 1.26488 +      SimulateIOErrorBenign(0);
 1.26489 +      return rc;
 1.26490 +    }
 1.26491 +    case SQLITE_FCNTL_PERSIST_WAL: {
 1.26492 +      unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
 1.26493 +      return SQLITE_OK;
 1.26494 +    }
 1.26495 +    case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
 1.26496 +      unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
 1.26497 +      return SQLITE_OK;
 1.26498 +    }
 1.26499 +    case SQLITE_FCNTL_VFSNAME: {
 1.26500 +      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
 1.26501 +      return SQLITE_OK;
 1.26502 +    }
 1.26503 +    case SQLITE_FCNTL_TEMPFILENAME: {
 1.26504 +      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
 1.26505 +      if( zTFile ){
 1.26506 +        unixGetTempname(pFile->pVfs->mxPathname, zTFile);
 1.26507 +        *(char**)pArg = zTFile;
 1.26508 +      }
 1.26509 +      return SQLITE_OK;
 1.26510 +    }
 1.26511 +#ifdef SQLITE_DEBUG
 1.26512 +    /* The pager calls this method to signal that it has done
 1.26513 +    ** a rollback and that the database is therefore unchanged and
 1.26514 +    ** it hence it is OK for the transaction change counter to be
 1.26515 +    ** unchanged.
 1.26516 +    */
 1.26517 +    case SQLITE_FCNTL_DB_UNCHANGED: {
 1.26518 +      ((unixFile*)id)->dbUpdate = 0;
 1.26519 +      return SQLITE_OK;
 1.26520 +    }
 1.26521 +#endif
 1.26522 +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 1.26523 +    case SQLITE_SET_LOCKPROXYFILE:
 1.26524 +    case SQLITE_GET_LOCKPROXYFILE: {
 1.26525 +      return proxyFileControl(id,op,pArg);
 1.26526 +    }
 1.26527 +#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
 1.26528 +  }
 1.26529 +  return SQLITE_NOTFOUND;
 1.26530 +}
 1.26531 +
 1.26532 +/*
 1.26533 +** Return the sector size in bytes of the underlying block device for
 1.26534 +** the specified file. This is almost always 512 bytes, but may be
 1.26535 +** larger for some devices.
 1.26536 +**
 1.26537 +** SQLite code assumes this function cannot fail. It also assumes that
 1.26538 +** if two files are created in the same file-system directory (i.e.
 1.26539 +** a database and its journal file) that the sector size will be the
 1.26540 +** same for both.
 1.26541 +*/
 1.26542 +#ifndef __QNXNTO__ 
 1.26543 +static int unixSectorSize(sqlite3_file *NotUsed){
 1.26544 +  UNUSED_PARAMETER(NotUsed);
 1.26545 +  return SQLITE_DEFAULT_SECTOR_SIZE;
 1.26546 +}
 1.26547 +#endif
 1.26548 +
 1.26549 +/*
 1.26550 +** The following version of unixSectorSize() is optimized for QNX.
 1.26551 +*/
 1.26552 +#ifdef __QNXNTO__
 1.26553 +#include <sys/dcmd_blk.h>
 1.26554 +#include <sys/statvfs.h>
 1.26555 +static int unixSectorSize(sqlite3_file *id){
 1.26556 +  unixFile *pFile = (unixFile*)id;
 1.26557 +  if( pFile->sectorSize == 0 ){
 1.26558 +    struct statvfs fsInfo;
 1.26559 +       
 1.26560 +    /* Set defaults for non-supported filesystems */
 1.26561 +    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 1.26562 +    pFile->deviceCharacteristics = 0;
 1.26563 +    if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
 1.26564 +      return pFile->sectorSize;
 1.26565 +    }
 1.26566 +
 1.26567 +    if( !strcmp(fsInfo.f_basetype, "tmp") ) {
 1.26568 +      pFile->sectorSize = fsInfo.f_bsize;
 1.26569 +      pFile->deviceCharacteristics =
 1.26570 +        SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
 1.26571 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 1.26572 +                                      ** the write succeeds */
 1.26573 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 1.26574 +                                      ** so it is ordered */
 1.26575 +        0;
 1.26576 +    }else if( strstr(fsInfo.f_basetype, "etfs") ){
 1.26577 +      pFile->sectorSize = fsInfo.f_bsize;
 1.26578 +      pFile->deviceCharacteristics =
 1.26579 +        /* etfs cluster size writes are atomic */
 1.26580 +        (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
 1.26581 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 1.26582 +                                      ** the write succeeds */
 1.26583 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 1.26584 +                                      ** so it is ordered */
 1.26585 +        0;
 1.26586 +    }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
 1.26587 +      pFile->sectorSize = fsInfo.f_bsize;
 1.26588 +      pFile->deviceCharacteristics =
 1.26589 +        SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
 1.26590 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 1.26591 +                                      ** the write succeeds */
 1.26592 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 1.26593 +                                      ** so it is ordered */
 1.26594 +        0;
 1.26595 +    }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
 1.26596 +      pFile->sectorSize = fsInfo.f_bsize;
 1.26597 +      pFile->deviceCharacteristics =
 1.26598 +        /* full bitset of atomics from max sector size and smaller */
 1.26599 +        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
 1.26600 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 1.26601 +                                      ** so it is ordered */
 1.26602 +        0;
 1.26603 +    }else if( strstr(fsInfo.f_basetype, "dos") ){
 1.26604 +      pFile->sectorSize = fsInfo.f_bsize;
 1.26605 +      pFile->deviceCharacteristics =
 1.26606 +        /* full bitset of atomics from max sector size and smaller */
 1.26607 +        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
 1.26608 +        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 1.26609 +                                      ** so it is ordered */
 1.26610 +        0;
 1.26611 +    }else{
 1.26612 +      pFile->deviceCharacteristics =
 1.26613 +        SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
 1.26614 +        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 1.26615 +                                      ** the write succeeds */
 1.26616 +        0;
 1.26617 +    }
 1.26618 +  }
 1.26619 +  /* Last chance verification.  If the sector size isn't a multiple of 512
 1.26620 +  ** then it isn't valid.*/
 1.26621 +  if( pFile->sectorSize % 512 != 0 ){
 1.26622 +    pFile->deviceCharacteristics = 0;
 1.26623 +    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 1.26624 +  }
 1.26625 +  return pFile->sectorSize;
 1.26626 +}
 1.26627 +#endif /* __QNXNTO__ */
 1.26628 +
 1.26629 +/*
 1.26630 +** Return the device characteristics for the file.
 1.26631 +**
 1.26632 +** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
 1.26633 +** However, that choice is contraversial since technically the underlying
 1.26634 +** file system does not always provide powersafe overwrites.  (In other
 1.26635 +** words, after a power-loss event, parts of the file that were never
 1.26636 +** written might end up being altered.)  However, non-PSOW behavior is very,
 1.26637 +** very rare.  And asserting PSOW makes a large reduction in the amount
 1.26638 +** of required I/O for journaling, since a lot of padding is eliminated.
 1.26639 +**  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
 1.26640 +** available to turn it off and URI query parameter available to turn it off.
 1.26641 +*/
 1.26642 +static int unixDeviceCharacteristics(sqlite3_file *id){
 1.26643 +  unixFile *p = (unixFile*)id;
 1.26644 +  int rc = 0;
 1.26645 +#ifdef __QNXNTO__
 1.26646 +  if( p->sectorSize==0 ) unixSectorSize(id);
 1.26647 +  rc = p->deviceCharacteristics;
 1.26648 +#endif
 1.26649 +  if( p->ctrlFlags & UNIXFILE_PSOW ){
 1.26650 +    rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
 1.26651 +  }
 1.26652 +  return rc;
 1.26653 +}
 1.26654 +
 1.26655 +#ifndef SQLITE_OMIT_WAL
 1.26656 +
 1.26657 +
 1.26658 +/*
 1.26659 +** Object used to represent an shared memory buffer.  
 1.26660 +**
 1.26661 +** When multiple threads all reference the same wal-index, each thread
 1.26662 +** has its own unixShm object, but they all point to a single instance
 1.26663 +** of this unixShmNode object.  In other words, each wal-index is opened
 1.26664 +** only once per process.
 1.26665 +**
 1.26666 +** Each unixShmNode object is connected to a single unixInodeInfo object.
 1.26667 +** We could coalesce this object into unixInodeInfo, but that would mean
 1.26668 +** every open file that does not use shared memory (in other words, most
 1.26669 +** open files) would have to carry around this extra information.  So
 1.26670 +** the unixInodeInfo object contains a pointer to this unixShmNode object
 1.26671 +** and the unixShmNode object is created only when needed.
 1.26672 +**
 1.26673 +** unixMutexHeld() must be true when creating or destroying
 1.26674 +** this object or while reading or writing the following fields:
 1.26675 +**
 1.26676 +**      nRef
 1.26677 +**
 1.26678 +** The following fields are read-only after the object is created:
 1.26679 +** 
 1.26680 +**      fid
 1.26681 +**      zFilename
 1.26682 +**
 1.26683 +** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
 1.26684 +** unixMutexHeld() is true when reading or writing any other field
 1.26685 +** in this structure.
 1.26686 +*/
 1.26687 +struct unixShmNode {
 1.26688 +  unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
 1.26689 +  sqlite3_mutex *mutex;      /* Mutex to access this object */
 1.26690 +  char *zFilename;           /* Name of the mmapped file */
 1.26691 +  int h;                     /* Open file descriptor */
 1.26692 +  int szRegion;              /* Size of shared-memory regions */
 1.26693 +  u16 nRegion;               /* Size of array apRegion */
 1.26694 +  u8 isReadonly;             /* True if read-only */
 1.26695 +  char **apRegion;           /* Array of mapped shared-memory regions */
 1.26696 +  int nRef;                  /* Number of unixShm objects pointing to this */
 1.26697 +  unixShm *pFirst;           /* All unixShm objects pointing to this */
 1.26698 +#ifdef SQLITE_DEBUG
 1.26699 +  u8 exclMask;               /* Mask of exclusive locks held */
 1.26700 +  u8 sharedMask;             /* Mask of shared locks held */
 1.26701 +  u8 nextShmId;              /* Next available unixShm.id value */
 1.26702 +#endif
 1.26703 +};
 1.26704 +
 1.26705 +/*
 1.26706 +** Structure used internally by this VFS to record the state of an
 1.26707 +** open shared memory connection.
 1.26708 +**
 1.26709 +** The following fields are initialized when this object is created and
 1.26710 +** are read-only thereafter:
 1.26711 +**
 1.26712 +**    unixShm.pFile
 1.26713 +**    unixShm.id
 1.26714 +**
 1.26715 +** All other fields are read/write.  The unixShm.pFile->mutex must be held
 1.26716 +** while accessing any read/write fields.
 1.26717 +*/
 1.26718 +struct unixShm {
 1.26719 +  unixShmNode *pShmNode;     /* The underlying unixShmNode object */
 1.26720 +  unixShm *pNext;            /* Next unixShm with the same unixShmNode */
 1.26721 +  u8 hasMutex;               /* True if holding the unixShmNode mutex */
 1.26722 +  u8 id;                     /* Id of this connection within its unixShmNode */
 1.26723 +  u16 sharedMask;            /* Mask of shared locks held */
 1.26724 +  u16 exclMask;              /* Mask of exclusive locks held */
 1.26725 +};
 1.26726 +
 1.26727 +/*
 1.26728 +** Constants used for locking
 1.26729 +*/
 1.26730 +#define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
 1.26731 +#define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 1.26732 +
 1.26733 +/*
 1.26734 +** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
 1.26735 +**
 1.26736 +** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
 1.26737 +** otherwise.
 1.26738 +*/
 1.26739 +static int unixShmSystemLock(
 1.26740 +  unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
 1.26741 +  int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
 1.26742 +  int ofst,              /* First byte of the locking range */
 1.26743 +  int n                  /* Number of bytes to lock */
 1.26744 +){
 1.26745 +  struct flock f;       /* The posix advisory locking structure */
 1.26746 +  int rc = SQLITE_OK;   /* Result code form fcntl() */
 1.26747 +
 1.26748 +  /* Access to the unixShmNode object is serialized by the caller */
 1.26749 +  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
 1.26750 +
 1.26751 +  /* Shared locks never span more than one byte */
 1.26752 +  assert( n==1 || lockType!=F_RDLCK );
 1.26753 +
 1.26754 +  /* Locks are within range */
 1.26755 +  assert( n>=1 && n<SQLITE_SHM_NLOCK );
 1.26756 +
 1.26757 +  if( pShmNode->h>=0 ){
 1.26758 +    /* Initialize the locking parameters */
 1.26759 +    memset(&f, 0, sizeof(f));
 1.26760 +    f.l_type = lockType;
 1.26761 +    f.l_whence = SEEK_SET;
 1.26762 +    f.l_start = ofst;
 1.26763 +    f.l_len = n;
 1.26764 +
 1.26765 +    rc = osFcntl(pShmNode->h, F_SETLK, &f);
 1.26766 +    rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 1.26767 +  }
 1.26768 +
 1.26769 +  /* Update the global lock state and do debug tracing */
 1.26770 +#ifdef SQLITE_DEBUG
 1.26771 +  { u16 mask;
 1.26772 +  OSTRACE(("SHM-LOCK "));
 1.26773 +  mask = (1<<(ofst+n)) - (1<<ofst);
 1.26774 +  if( rc==SQLITE_OK ){
 1.26775 +    if( lockType==F_UNLCK ){
 1.26776 +      OSTRACE(("unlock %d ok", ofst));
 1.26777 +      pShmNode->exclMask &= ~mask;
 1.26778 +      pShmNode->sharedMask &= ~mask;
 1.26779 +    }else if( lockType==F_RDLCK ){
 1.26780 +      OSTRACE(("read-lock %d ok", ofst));
 1.26781 +      pShmNode->exclMask &= ~mask;
 1.26782 +      pShmNode->sharedMask |= mask;
 1.26783 +    }else{
 1.26784 +      assert( lockType==F_WRLCK );
 1.26785 +      OSTRACE(("write-lock %d ok", ofst));
 1.26786 +      pShmNode->exclMask |= mask;
 1.26787 +      pShmNode->sharedMask &= ~mask;
 1.26788 +    }
 1.26789 +  }else{
 1.26790 +    if( lockType==F_UNLCK ){
 1.26791 +      OSTRACE(("unlock %d failed", ofst));
 1.26792 +    }else if( lockType==F_RDLCK ){
 1.26793 +      OSTRACE(("read-lock failed"));
 1.26794 +    }else{
 1.26795 +      assert( lockType==F_WRLCK );
 1.26796 +      OSTRACE(("write-lock %d failed", ofst));
 1.26797 +    }
 1.26798 +  }
 1.26799 +  OSTRACE((" - afterwards %03x,%03x\n",
 1.26800 +           pShmNode->sharedMask, pShmNode->exclMask));
 1.26801 +  }
 1.26802 +#endif
 1.26803 +
 1.26804 +  return rc;        
 1.26805 +}
 1.26806 +
 1.26807 +
 1.26808 +/*
 1.26809 +** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
 1.26810 +**
 1.26811 +** This is not a VFS shared-memory method; it is a utility function called
 1.26812 +** by VFS shared-memory methods.
 1.26813 +*/
 1.26814 +static void unixShmPurge(unixFile *pFd){
 1.26815 +  unixShmNode *p = pFd->pInode->pShmNode;
 1.26816 +  assert( unixMutexHeld() );
 1.26817 +  if( p && p->nRef==0 ){
 1.26818 +    int i;
 1.26819 +    assert( p->pInode==pFd->pInode );
 1.26820 +    sqlite3_mutex_free(p->mutex);
 1.26821 +    for(i=0; i<p->nRegion; i++){
 1.26822 +      if( p->h>=0 ){
 1.26823 +        munmap(p->apRegion[i], p->szRegion);
 1.26824 +      }else{
 1.26825 +        sqlite3_free(p->apRegion[i]);
 1.26826 +      }
 1.26827 +    }
 1.26828 +    sqlite3_free(p->apRegion);
 1.26829 +    if( p->h>=0 ){
 1.26830 +      robust_close(pFd, p->h, __LINE__);
 1.26831 +      p->h = -1;
 1.26832 +    }
 1.26833 +    p->pInode->pShmNode = 0;
 1.26834 +    sqlite3_free(p);
 1.26835 +  }
 1.26836 +}
 1.26837 +
 1.26838 +/*
 1.26839 +** Open a shared-memory area associated with open database file pDbFd.  
 1.26840 +** This particular implementation uses mmapped files.
 1.26841 +**
 1.26842 +** The file used to implement shared-memory is in the same directory
 1.26843 +** as the open database file and has the same name as the open database
 1.26844 +** file with the "-shm" suffix added.  For example, if the database file
 1.26845 +** is "/home/user1/config.db" then the file that is created and mmapped
 1.26846 +** for shared memory will be called "/home/user1/config.db-shm".  
 1.26847 +**
 1.26848 +** Another approach to is to use files in /dev/shm or /dev/tmp or an
 1.26849 +** some other tmpfs mount. But if a file in a different directory
 1.26850 +** from the database file is used, then differing access permissions
 1.26851 +** or a chroot() might cause two different processes on the same
 1.26852 +** database to end up using different files for shared memory - 
 1.26853 +** meaning that their memory would not really be shared - resulting
 1.26854 +** in database corruption.  Nevertheless, this tmpfs file usage
 1.26855 +** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
 1.26856 +** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
 1.26857 +** option results in an incompatible build of SQLite;  builds of SQLite
 1.26858 +** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
 1.26859 +** same database file at the same time, database corruption will likely
 1.26860 +** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
 1.26861 +** "unsupported" and may go away in a future SQLite release.
 1.26862 +**
 1.26863 +** When opening a new shared-memory file, if no other instances of that
 1.26864 +** file are currently open, in this process or in other processes, then
 1.26865 +** the file must be truncated to zero length or have its header cleared.
 1.26866 +**
 1.26867 +** If the original database file (pDbFd) is using the "unix-excl" VFS
 1.26868 +** that means that an exclusive lock is held on the database file and
 1.26869 +** that no other processes are able to read or write the database.  In
 1.26870 +** that case, we do not really need shared memory.  No shared memory
 1.26871 +** file is created.  The shared memory will be simulated with heap memory.
 1.26872 +*/
 1.26873 +static int unixOpenSharedMemory(unixFile *pDbFd){
 1.26874 +  struct unixShm *p = 0;          /* The connection to be opened */
 1.26875 +  struct unixShmNode *pShmNode;   /* The underlying mmapped file */
 1.26876 +  int rc;                         /* Result code */
 1.26877 +  unixInodeInfo *pInode;          /* The inode of fd */
 1.26878 +  char *zShmFilename;             /* Name of the file used for SHM */
 1.26879 +  int nShmFilename;               /* Size of the SHM filename in bytes */
 1.26880 +
 1.26881 +  /* Allocate space for the new unixShm object. */
 1.26882 +  p = sqlite3_malloc( sizeof(*p) );
 1.26883 +  if( p==0 ) return SQLITE_NOMEM;
 1.26884 +  memset(p, 0, sizeof(*p));
 1.26885 +  assert( pDbFd->pShm==0 );
 1.26886 +
 1.26887 +  /* Check to see if a unixShmNode object already exists. Reuse an existing
 1.26888 +  ** one if present. Create a new one if necessary.
 1.26889 +  */
 1.26890 +  unixEnterMutex();
 1.26891 +  pInode = pDbFd->pInode;
 1.26892 +  pShmNode = pInode->pShmNode;
 1.26893 +  if( pShmNode==0 ){
 1.26894 +    struct stat sStat;                 /* fstat() info for database file */
 1.26895 +
 1.26896 +    /* Call fstat() to figure out the permissions on the database file. If
 1.26897 +    ** a new *-shm file is created, an attempt will be made to create it
 1.26898 +    ** with the same permissions.
 1.26899 +    */
 1.26900 +    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
 1.26901 +      rc = SQLITE_IOERR_FSTAT;
 1.26902 +      goto shm_open_err;
 1.26903 +    }
 1.26904 +
 1.26905 +#ifdef SQLITE_SHM_DIRECTORY
 1.26906 +    nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
 1.26907 +#else
 1.26908 +    nShmFilename = 6 + (int)strlen(pDbFd->zPath);
 1.26909 +#endif
 1.26910 +    pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
 1.26911 +    if( pShmNode==0 ){
 1.26912 +      rc = SQLITE_NOMEM;
 1.26913 +      goto shm_open_err;
 1.26914 +    }
 1.26915 +    memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
 1.26916 +    zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
 1.26917 +#ifdef SQLITE_SHM_DIRECTORY
 1.26918 +    sqlite3_snprintf(nShmFilename, zShmFilename, 
 1.26919 +                     SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
 1.26920 +                     (u32)sStat.st_ino, (u32)sStat.st_dev);
 1.26921 +#else
 1.26922 +    sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
 1.26923 +    sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
 1.26924 +#endif
 1.26925 +    pShmNode->h = -1;
 1.26926 +    pDbFd->pInode->pShmNode = pShmNode;
 1.26927 +    pShmNode->pInode = pDbFd->pInode;
 1.26928 +    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 1.26929 +    if( pShmNode->mutex==0 ){
 1.26930 +      rc = SQLITE_NOMEM;
 1.26931 +      goto shm_open_err;
 1.26932 +    }
 1.26933 +
 1.26934 +    if( pInode->bProcessLock==0 ){
 1.26935 +      int openFlags = O_RDWR | O_CREAT;
 1.26936 +      if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 1.26937 +        openFlags = O_RDONLY;
 1.26938 +        pShmNode->isReadonly = 1;
 1.26939 +      }
 1.26940 +      pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
 1.26941 +      if( pShmNode->h<0 ){
 1.26942 +        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
 1.26943 +        goto shm_open_err;
 1.26944 +      }
 1.26945 +
 1.26946 +      /* If this process is running as root, make sure that the SHM file
 1.26947 +      ** is owned by the same user that owns the original database.  Otherwise,
 1.26948 +      ** the original owner will not be able to connect.
 1.26949 +      */
 1.26950 +      osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
 1.26951 +  
 1.26952 +      /* Check to see if another process is holding the dead-man switch.
 1.26953 +      ** If not, truncate the file to zero length. 
 1.26954 +      */
 1.26955 +      rc = SQLITE_OK;
 1.26956 +      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
 1.26957 +        if( robust_ftruncate(pShmNode->h, 0) ){
 1.26958 +          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
 1.26959 +        }
 1.26960 +      }
 1.26961 +      if( rc==SQLITE_OK ){
 1.26962 +        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
 1.26963 +      }
 1.26964 +      if( rc ) goto shm_open_err;
 1.26965 +    }
 1.26966 +  }
 1.26967 +
 1.26968 +  /* Make the new connection a child of the unixShmNode */
 1.26969 +  p->pShmNode = pShmNode;
 1.26970 +#ifdef SQLITE_DEBUG
 1.26971 +  p->id = pShmNode->nextShmId++;
 1.26972 +#endif
 1.26973 +  pShmNode->nRef++;
 1.26974 +  pDbFd->pShm = p;
 1.26975 +  unixLeaveMutex();
 1.26976 +
 1.26977 +  /* The reference count on pShmNode has already been incremented under
 1.26978 +  ** the cover of the unixEnterMutex() mutex and the pointer from the
 1.26979 +  ** new (struct unixShm) object to the pShmNode has been set. All that is
 1.26980 +  ** left to do is to link the new object into the linked list starting
 1.26981 +  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 1.26982 +  ** mutex.
 1.26983 +  */
 1.26984 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.26985 +  p->pNext = pShmNode->pFirst;
 1.26986 +  pShmNode->pFirst = p;
 1.26987 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.26988 +  return SQLITE_OK;
 1.26989 +
 1.26990 +  /* Jump here on any error */
 1.26991 +shm_open_err:
 1.26992 +  unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
 1.26993 +  sqlite3_free(p);
 1.26994 +  unixLeaveMutex();
 1.26995 +  return rc;
 1.26996 +}
 1.26997 +
 1.26998 +/*
 1.26999 +** This function is called to obtain a pointer to region iRegion of the 
 1.27000 +** shared-memory associated with the database file fd. Shared-memory regions 
 1.27001 +** are numbered starting from zero. Each shared-memory region is szRegion 
 1.27002 +** bytes in size.
 1.27003 +**
 1.27004 +** If an error occurs, an error code is returned and *pp is set to NULL.
 1.27005 +**
 1.27006 +** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
 1.27007 +** region has not been allocated (by any client, including one running in a
 1.27008 +** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
 1.27009 +** bExtend is non-zero and the requested shared-memory region has not yet 
 1.27010 +** been allocated, it is allocated by this function.
 1.27011 +**
 1.27012 +** If the shared-memory region has already been allocated or is allocated by
 1.27013 +** this call as described above, then it is mapped into this processes 
 1.27014 +** address space (if it is not already), *pp is set to point to the mapped 
 1.27015 +** memory and SQLITE_OK returned.
 1.27016 +*/
 1.27017 +static int unixShmMap(
 1.27018 +  sqlite3_file *fd,               /* Handle open on database file */
 1.27019 +  int iRegion,                    /* Region to retrieve */
 1.27020 +  int szRegion,                   /* Size of regions */
 1.27021 +  int bExtend,                    /* True to extend file if necessary */
 1.27022 +  void volatile **pp              /* OUT: Mapped memory */
 1.27023 +){
 1.27024 +  unixFile *pDbFd = (unixFile*)fd;
 1.27025 +  unixShm *p;
 1.27026 +  unixShmNode *pShmNode;
 1.27027 +  int rc = SQLITE_OK;
 1.27028 +
 1.27029 +  /* If the shared-memory file has not yet been opened, open it now. */
 1.27030 +  if( pDbFd->pShm==0 ){
 1.27031 +    rc = unixOpenSharedMemory(pDbFd);
 1.27032 +    if( rc!=SQLITE_OK ) return rc;
 1.27033 +  }
 1.27034 +
 1.27035 +  p = pDbFd->pShm;
 1.27036 +  pShmNode = p->pShmNode;
 1.27037 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.27038 +  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 1.27039 +  assert( pShmNode->pInode==pDbFd->pInode );
 1.27040 +  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 1.27041 +  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 1.27042 +
 1.27043 +  if( pShmNode->nRegion<=iRegion ){
 1.27044 +    char **apNew;                      /* New apRegion[] array */
 1.27045 +    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 1.27046 +    struct stat sStat;                 /* Used by fstat() */
 1.27047 +
 1.27048 +    pShmNode->szRegion = szRegion;
 1.27049 +
 1.27050 +    if( pShmNode->h>=0 ){
 1.27051 +      /* The requested region is not mapped into this processes address space.
 1.27052 +      ** Check to see if it has been allocated (i.e. if the wal-index file is
 1.27053 +      ** large enough to contain the requested region).
 1.27054 +      */
 1.27055 +      if( osFstat(pShmNode->h, &sStat) ){
 1.27056 +        rc = SQLITE_IOERR_SHMSIZE;
 1.27057 +        goto shmpage_out;
 1.27058 +      }
 1.27059 +  
 1.27060 +      if( sStat.st_size<nByte ){
 1.27061 +        /* The requested memory region does not exist. If bExtend is set to
 1.27062 +        ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
 1.27063 +        **
 1.27064 +        ** Alternatively, if bExtend is true, use ftruncate() to allocate
 1.27065 +        ** the requested memory region.
 1.27066 +        */
 1.27067 +        if( !bExtend ) goto shmpage_out;
 1.27068 +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 1.27069 +        if( osFallocate(pShmNode->h, sStat.st_size, nByte)!=0 ){
 1.27070 +          rc = unixLogError(SQLITE_IOERR_SHMSIZE, "fallocate",
 1.27071 +                            pShmNode->zFilename);
 1.27072 +          goto shmpage_out;
 1.27073 +        }
 1.27074 +#else
 1.27075 +        if( robust_ftruncate(pShmNode->h, nByte) ){
 1.27076 +          rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
 1.27077 +                            pShmNode->zFilename);
 1.27078 +          goto shmpage_out;
 1.27079 +        }
 1.27080 +#endif
 1.27081 +      }
 1.27082 +    }
 1.27083 +
 1.27084 +    /* Map the requested memory region into this processes address space. */
 1.27085 +    apNew = (char **)sqlite3_realloc(
 1.27086 +        pShmNode->apRegion, (iRegion+1)*sizeof(char *)
 1.27087 +    );
 1.27088 +    if( !apNew ){
 1.27089 +      rc = SQLITE_IOERR_NOMEM;
 1.27090 +      goto shmpage_out;
 1.27091 +    }
 1.27092 +    pShmNode->apRegion = apNew;
 1.27093 +    while(pShmNode->nRegion<=iRegion){
 1.27094 +      void *pMem;
 1.27095 +      if( pShmNode->h>=0 ){
 1.27096 +        pMem = mmap(0, szRegion,
 1.27097 +            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
 1.27098 +            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
 1.27099 +        );
 1.27100 +        if( pMem==MAP_FAILED ){
 1.27101 +          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 1.27102 +          goto shmpage_out;
 1.27103 +        }
 1.27104 +      }else{
 1.27105 +        pMem = sqlite3_malloc(szRegion);
 1.27106 +        if( pMem==0 ){
 1.27107 +          rc = SQLITE_NOMEM;
 1.27108 +          goto shmpage_out;
 1.27109 +        }
 1.27110 +        memset(pMem, 0, szRegion);
 1.27111 +      }
 1.27112 +      pShmNode->apRegion[pShmNode->nRegion] = pMem;
 1.27113 +      pShmNode->nRegion++;
 1.27114 +    }
 1.27115 +  }
 1.27116 +
 1.27117 +shmpage_out:
 1.27118 +  if( pShmNode->nRegion>iRegion ){
 1.27119 +    *pp = pShmNode->apRegion[iRegion];
 1.27120 +  }else{
 1.27121 +    *pp = 0;
 1.27122 +  }
 1.27123 +  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 1.27124 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.27125 +  return rc;
 1.27126 +}
 1.27127 +
 1.27128 +/*
 1.27129 +** Change the lock state for a shared-memory segment.
 1.27130 +**
 1.27131 +** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
 1.27132 +** different here than in posix.  In xShmLock(), one can go from unlocked
 1.27133 +** to shared and back or from unlocked to exclusive and back.  But one may
 1.27134 +** not go from shared to exclusive or from exclusive to shared.
 1.27135 +*/
 1.27136 +static int unixShmLock(
 1.27137 +  sqlite3_file *fd,          /* Database file holding the shared memory */
 1.27138 +  int ofst,                  /* First lock to acquire or release */
 1.27139 +  int n,                     /* Number of locks to acquire or release */
 1.27140 +  int flags                  /* What to do with the lock */
 1.27141 +){
 1.27142 +  unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
 1.27143 +  unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
 1.27144 +  unixShm *pX;                          /* For looping over all siblings */
 1.27145 +  unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
 1.27146 +  int rc = SQLITE_OK;                   /* Result code */
 1.27147 +  u16 mask;                             /* Mask of locks to take or release */
 1.27148 +
 1.27149 +  assert( pShmNode==pDbFd->pInode->pShmNode );
 1.27150 +  assert( pShmNode->pInode==pDbFd->pInode );
 1.27151 +  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 1.27152 +  assert( n>=1 );
 1.27153 +  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 1.27154 +       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 1.27155 +       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 1.27156 +       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 1.27157 +  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 1.27158 +  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 1.27159 +  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 1.27160 +
 1.27161 +  mask = (1<<(ofst+n)) - (1<<ofst);
 1.27162 +  assert( n>1 || mask==(1<<ofst) );
 1.27163 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.27164 +  if( flags & SQLITE_SHM_UNLOCK ){
 1.27165 +    u16 allMask = 0; /* Mask of locks held by siblings */
 1.27166 +
 1.27167 +    /* See if any siblings hold this same lock */
 1.27168 +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 1.27169 +      if( pX==p ) continue;
 1.27170 +      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 1.27171 +      allMask |= pX->sharedMask;
 1.27172 +    }
 1.27173 +
 1.27174 +    /* Unlock the system-level locks */
 1.27175 +    if( (mask & allMask)==0 ){
 1.27176 +      rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
 1.27177 +    }else{
 1.27178 +      rc = SQLITE_OK;
 1.27179 +    }
 1.27180 +
 1.27181 +    /* Undo the local locks */
 1.27182 +    if( rc==SQLITE_OK ){
 1.27183 +      p->exclMask &= ~mask;
 1.27184 +      p->sharedMask &= ~mask;
 1.27185 +    } 
 1.27186 +  }else if( flags & SQLITE_SHM_SHARED ){
 1.27187 +    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
 1.27188 +
 1.27189 +    /* Find out which shared locks are already held by sibling connections.
 1.27190 +    ** If any sibling already holds an exclusive lock, go ahead and return
 1.27191 +    ** SQLITE_BUSY.
 1.27192 +    */
 1.27193 +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 1.27194 +      if( (pX->exclMask & mask)!=0 ){
 1.27195 +        rc = SQLITE_BUSY;
 1.27196 +        break;
 1.27197 +      }
 1.27198 +      allShared |= pX->sharedMask;
 1.27199 +    }
 1.27200 +
 1.27201 +    /* Get shared locks at the system level, if necessary */
 1.27202 +    if( rc==SQLITE_OK ){
 1.27203 +      if( (allShared & mask)==0 ){
 1.27204 +        rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
 1.27205 +      }else{
 1.27206 +        rc = SQLITE_OK;
 1.27207 +      }
 1.27208 +    }
 1.27209 +
 1.27210 +    /* Get the local shared locks */
 1.27211 +    if( rc==SQLITE_OK ){
 1.27212 +      p->sharedMask |= mask;
 1.27213 +    }
 1.27214 +  }else{
 1.27215 +    /* Make sure no sibling connections hold locks that will block this
 1.27216 +    ** lock.  If any do, return SQLITE_BUSY right away.
 1.27217 +    */
 1.27218 +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 1.27219 +      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
 1.27220 +        rc = SQLITE_BUSY;
 1.27221 +        break;
 1.27222 +      }
 1.27223 +    }
 1.27224 +  
 1.27225 +    /* Get the exclusive locks at the system level.  Then if successful
 1.27226 +    ** also mark the local connection as being locked.
 1.27227 +    */
 1.27228 +    if( rc==SQLITE_OK ){
 1.27229 +      rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
 1.27230 +      if( rc==SQLITE_OK ){
 1.27231 +        assert( (p->sharedMask & mask)==0 );
 1.27232 +        p->exclMask |= mask;
 1.27233 +      }
 1.27234 +    }
 1.27235 +  }
 1.27236 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.27237 +  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
 1.27238 +           p->id, getpid(), p->sharedMask, p->exclMask));
 1.27239 +  return rc;
 1.27240 +}
 1.27241 +
 1.27242 +/*
 1.27243 +** Implement a memory barrier or memory fence on shared memory.  
 1.27244 +**
 1.27245 +** All loads and stores begun before the barrier must complete before
 1.27246 +** any load or store begun after the barrier.
 1.27247 +*/
 1.27248 +static void unixShmBarrier(
 1.27249 +  sqlite3_file *fd                /* Database file holding the shared memory */
 1.27250 +){
 1.27251 +  UNUSED_PARAMETER(fd);
 1.27252 +  unixEnterMutex();
 1.27253 +  unixLeaveMutex();
 1.27254 +}
 1.27255 +
 1.27256 +/*
 1.27257 +** Close a connection to shared-memory.  Delete the underlying 
 1.27258 +** storage if deleteFlag is true.
 1.27259 +**
 1.27260 +** If there is no shared memory associated with the connection then this
 1.27261 +** routine is a harmless no-op.
 1.27262 +*/
 1.27263 +static int unixShmUnmap(
 1.27264 +  sqlite3_file *fd,               /* The underlying database file */
 1.27265 +  int deleteFlag                  /* Delete shared-memory if true */
 1.27266 +){
 1.27267 +  unixShm *p;                     /* The connection to be closed */
 1.27268 +  unixShmNode *pShmNode;          /* The underlying shared-memory file */
 1.27269 +  unixShm **pp;                   /* For looping over sibling connections */
 1.27270 +  unixFile *pDbFd;                /* The underlying database file */
 1.27271 +
 1.27272 +  pDbFd = (unixFile*)fd;
 1.27273 +  p = pDbFd->pShm;
 1.27274 +  if( p==0 ) return SQLITE_OK;
 1.27275 +  pShmNode = p->pShmNode;
 1.27276 +
 1.27277 +  assert( pShmNode==pDbFd->pInode->pShmNode );
 1.27278 +  assert( pShmNode->pInode==pDbFd->pInode );
 1.27279 +
 1.27280 +  /* Remove connection p from the set of connections associated
 1.27281 +  ** with pShmNode */
 1.27282 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.27283 +  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 1.27284 +  *pp = p->pNext;
 1.27285 +
 1.27286 +  /* Free the connection p */
 1.27287 +  sqlite3_free(p);
 1.27288 +  pDbFd->pShm = 0;
 1.27289 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.27290 +
 1.27291 +  /* If pShmNode->nRef has reached 0, then close the underlying
 1.27292 +  ** shared-memory file, too */
 1.27293 +  unixEnterMutex();
 1.27294 +  assert( pShmNode->nRef>0 );
 1.27295 +  pShmNode->nRef--;
 1.27296 +  if( pShmNode->nRef==0 ){
 1.27297 +    if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
 1.27298 +    unixShmPurge(pDbFd);
 1.27299 +  }
 1.27300 +  unixLeaveMutex();
 1.27301 +
 1.27302 +  return SQLITE_OK;
 1.27303 +}
 1.27304 +
 1.27305 +
 1.27306 +#else
 1.27307 +# define unixShmMap     0
 1.27308 +# define unixShmLock    0
 1.27309 +# define unixShmBarrier 0
 1.27310 +# define unixShmUnmap   0
 1.27311 +#endif /* #ifndef SQLITE_OMIT_WAL */
 1.27312 +
 1.27313 +/*
 1.27314 +** Here ends the implementation of all sqlite3_file methods.
 1.27315 +**
 1.27316 +********************** End sqlite3_file Methods *******************************
 1.27317 +******************************************************************************/
 1.27318 +
 1.27319 +/*
 1.27320 +** This division contains definitions of sqlite3_io_methods objects that
 1.27321 +** implement various file locking strategies.  It also contains definitions
 1.27322 +** of "finder" functions.  A finder-function is used to locate the appropriate
 1.27323 +** sqlite3_io_methods object for a particular database file.  The pAppData
 1.27324 +** field of the sqlite3_vfs VFS objects are initialized to be pointers to
 1.27325 +** the correct finder-function for that VFS.
 1.27326 +**
 1.27327 +** Most finder functions return a pointer to a fixed sqlite3_io_methods
 1.27328 +** object.  The only interesting finder-function is autolockIoFinder, which
 1.27329 +** looks at the filesystem type and tries to guess the best locking
 1.27330 +** strategy from that.
 1.27331 +**
 1.27332 +** For finder-funtion F, two objects are created:
 1.27333 +**
 1.27334 +**    (1) The real finder-function named "FImpt()".
 1.27335 +**
 1.27336 +**    (2) A constant pointer to this function named just "F".
 1.27337 +**
 1.27338 +**
 1.27339 +** A pointer to the F pointer is used as the pAppData value for VFS
 1.27340 +** objects.  We have to do this instead of letting pAppData point
 1.27341 +** directly at the finder-function since C90 rules prevent a void*
 1.27342 +** from be cast into a function pointer.
 1.27343 +**
 1.27344 +**
 1.27345 +** Each instance of this macro generates two objects:
 1.27346 +**
 1.27347 +**   *  A constant sqlite3_io_methods object call METHOD that has locking
 1.27348 +**      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
 1.27349 +**
 1.27350 +**   *  An I/O method finder function called FINDER that returns a pointer
 1.27351 +**      to the METHOD object in the previous bullet.
 1.27352 +*/
 1.27353 +#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
 1.27354 +static const sqlite3_io_methods METHOD = {                                   \
 1.27355 +   VERSION,                    /* iVersion */                                \
 1.27356 +   CLOSE,                      /* xClose */                                  \
 1.27357 +   unixRead,                   /* xRead */                                   \
 1.27358 +   unixWrite,                  /* xWrite */                                  \
 1.27359 +   unixTruncate,               /* xTruncate */                               \
 1.27360 +   unixSync,                   /* xSync */                                   \
 1.27361 +   unixFileSize,               /* xFileSize */                               \
 1.27362 +   LOCK,                       /* xLock */                                   \
 1.27363 +   UNLOCK,                     /* xUnlock */                                 \
 1.27364 +   CKLOCK,                     /* xCheckReservedLock */                      \
 1.27365 +   unixFileControl,            /* xFileControl */                            \
 1.27366 +   unixSectorSize,             /* xSectorSize */                             \
 1.27367 +   unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
 1.27368 +   unixShmMap,                 /* xShmMap */                                 \
 1.27369 +   unixShmLock,                /* xShmLock */                                \
 1.27370 +   unixShmBarrier,             /* xShmBarrier */                             \
 1.27371 +   unixShmUnmap                /* xShmUnmap */                               \
 1.27372 +};                                                                           \
 1.27373 +static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
 1.27374 +  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
 1.27375 +  return &METHOD;                                                            \
 1.27376 +}                                                                            \
 1.27377 +static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
 1.27378 +    = FINDER##Impl;
 1.27379 +
 1.27380 +/*
 1.27381 +** Here are all of the sqlite3_io_methods objects for each of the
 1.27382 +** locking strategies.  Functions that return pointers to these methods
 1.27383 +** are also created.
 1.27384 +*/
 1.27385 +IOMETHODS(
 1.27386 +  posixIoFinder,            /* Finder function name */
 1.27387 +  posixIoMethods,           /* sqlite3_io_methods object name */
 1.27388 +  2,                        /* shared memory is enabled */
 1.27389 +  unixClose,                /* xClose method */
 1.27390 +  unixLock,                 /* xLock method */
 1.27391 +  unixUnlock,               /* xUnlock method */
 1.27392 +  unixCheckReservedLock     /* xCheckReservedLock method */
 1.27393 +)
 1.27394 +IOMETHODS(
 1.27395 +  nolockIoFinder,           /* Finder function name */
 1.27396 +  nolockIoMethods,          /* sqlite3_io_methods object name */
 1.27397 +  1,                        /* shared memory is disabled */
 1.27398 +  nolockClose,              /* xClose method */
 1.27399 +  nolockLock,               /* xLock method */
 1.27400 +  nolockUnlock,             /* xUnlock method */
 1.27401 +  nolockCheckReservedLock   /* xCheckReservedLock method */
 1.27402 +)
 1.27403 +IOMETHODS(
 1.27404 +  dotlockIoFinder,          /* Finder function name */
 1.27405 +  dotlockIoMethods,         /* sqlite3_io_methods object name */
 1.27406 +  1,                        /* shared memory is disabled */
 1.27407 +  dotlockClose,             /* xClose method */
 1.27408 +  dotlockLock,              /* xLock method */
 1.27409 +  dotlockUnlock,            /* xUnlock method */
 1.27410 +  dotlockCheckReservedLock  /* xCheckReservedLock method */
 1.27411 +)
 1.27412 +
 1.27413 +#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
 1.27414 +IOMETHODS(
 1.27415 +  flockIoFinder,            /* Finder function name */
 1.27416 +  flockIoMethods,           /* sqlite3_io_methods object name */
 1.27417 +  1,                        /* shared memory is disabled */
 1.27418 +  flockClose,               /* xClose method */
 1.27419 +  flockLock,                /* xLock method */
 1.27420 +  flockUnlock,              /* xUnlock method */
 1.27421 +  flockCheckReservedLock    /* xCheckReservedLock method */
 1.27422 +)
 1.27423 +#endif
 1.27424 +
 1.27425 +#if OS_VXWORKS
 1.27426 +IOMETHODS(
 1.27427 +  semIoFinder,              /* Finder function name */
 1.27428 +  semIoMethods,             /* sqlite3_io_methods object name */
 1.27429 +  1,                        /* shared memory is disabled */
 1.27430 +  semClose,                 /* xClose method */
 1.27431 +  semLock,                  /* xLock method */
 1.27432 +  semUnlock,                /* xUnlock method */
 1.27433 +  semCheckReservedLock      /* xCheckReservedLock method */
 1.27434 +)
 1.27435 +#endif
 1.27436 +
 1.27437 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.27438 +IOMETHODS(
 1.27439 +  afpIoFinder,              /* Finder function name */
 1.27440 +  afpIoMethods,             /* sqlite3_io_methods object name */
 1.27441 +  1,                        /* shared memory is disabled */
 1.27442 +  afpClose,                 /* xClose method */
 1.27443 +  afpLock,                  /* xLock method */
 1.27444 +  afpUnlock,                /* xUnlock method */
 1.27445 +  afpCheckReservedLock      /* xCheckReservedLock method */
 1.27446 +)
 1.27447 +#endif
 1.27448 +
 1.27449 +/*
 1.27450 +** The proxy locking method is a "super-method" in the sense that it
 1.27451 +** opens secondary file descriptors for the conch and lock files and
 1.27452 +** it uses proxy, dot-file, AFP, and flock() locking methods on those
 1.27453 +** secondary files.  For this reason, the division that implements
 1.27454 +** proxy locking is located much further down in the file.  But we need
 1.27455 +** to go ahead and define the sqlite3_io_methods and finder function
 1.27456 +** for proxy locking here.  So we forward declare the I/O methods.
 1.27457 +*/
 1.27458 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.27459 +static int proxyClose(sqlite3_file*);
 1.27460 +static int proxyLock(sqlite3_file*, int);
 1.27461 +static int proxyUnlock(sqlite3_file*, int);
 1.27462 +static int proxyCheckReservedLock(sqlite3_file*, int*);
 1.27463 +IOMETHODS(
 1.27464 +  proxyIoFinder,            /* Finder function name */
 1.27465 +  proxyIoMethods,           /* sqlite3_io_methods object name */
 1.27466 +  1,                        /* shared memory is disabled */
 1.27467 +  proxyClose,               /* xClose method */
 1.27468 +  proxyLock,                /* xLock method */
 1.27469 +  proxyUnlock,              /* xUnlock method */
 1.27470 +  proxyCheckReservedLock    /* xCheckReservedLock method */
 1.27471 +)
 1.27472 +#endif
 1.27473 +
 1.27474 +/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
 1.27475 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.27476 +IOMETHODS(
 1.27477 +  nfsIoFinder,               /* Finder function name */
 1.27478 +  nfsIoMethods,              /* sqlite3_io_methods object name */
 1.27479 +  1,                         /* shared memory is disabled */
 1.27480 +  unixClose,                 /* xClose method */
 1.27481 +  unixLock,                  /* xLock method */
 1.27482 +  nfsUnlock,                 /* xUnlock method */
 1.27483 +  unixCheckReservedLock      /* xCheckReservedLock method */
 1.27484 +)
 1.27485 +#endif
 1.27486 +
 1.27487 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.27488 +/* 
 1.27489 +** This "finder" function attempts to determine the best locking strategy 
 1.27490 +** for the database file "filePath".  It then returns the sqlite3_io_methods
 1.27491 +** object that implements that strategy.
 1.27492 +**
 1.27493 +** This is for MacOSX only.
 1.27494 +*/
 1.27495 +static const sqlite3_io_methods *autolockIoFinderImpl(
 1.27496 +  const char *filePath,    /* name of the database file */
 1.27497 +  unixFile *pNew           /* open file object for the database file */
 1.27498 +){
 1.27499 +  static const struct Mapping {
 1.27500 +    const char *zFilesystem;              /* Filesystem type name */
 1.27501 +    const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
 1.27502 +  } aMap[] = {
 1.27503 +    { "hfs",    &posixIoMethods },
 1.27504 +    { "ufs",    &posixIoMethods },
 1.27505 +    { "afpfs",  &afpIoMethods },
 1.27506 +    { "smbfs",  &afpIoMethods },
 1.27507 +    { "webdav", &nolockIoMethods },
 1.27508 +    { 0, 0 }
 1.27509 +  };
 1.27510 +  int i;
 1.27511 +  struct statfs fsInfo;
 1.27512 +  struct flock lockInfo;
 1.27513 +
 1.27514 +  if( !filePath ){
 1.27515 +    /* If filePath==NULL that means we are dealing with a transient file
 1.27516 +    ** that does not need to be locked. */
 1.27517 +    return &nolockIoMethods;
 1.27518 +  }
 1.27519 +  if( statfs(filePath, &fsInfo) != -1 ){
 1.27520 +    if( fsInfo.f_flags & MNT_RDONLY ){
 1.27521 +      return &nolockIoMethods;
 1.27522 +    }
 1.27523 +    for(i=0; aMap[i].zFilesystem; i++){
 1.27524 +      if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
 1.27525 +        return aMap[i].pMethods;
 1.27526 +      }
 1.27527 +    }
 1.27528 +  }
 1.27529 +
 1.27530 +  /* Default case. Handles, amongst others, "nfs".
 1.27531 +  ** Test byte-range lock using fcntl(). If the call succeeds, 
 1.27532 +  ** assume that the file-system supports POSIX style locks. 
 1.27533 +  */
 1.27534 +  lockInfo.l_len = 1;
 1.27535 +  lockInfo.l_start = 0;
 1.27536 +  lockInfo.l_whence = SEEK_SET;
 1.27537 +  lockInfo.l_type = F_RDLCK;
 1.27538 +  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 1.27539 +    if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
 1.27540 +      return &nfsIoMethods;
 1.27541 +    } else {
 1.27542 +      return &posixIoMethods;
 1.27543 +    }
 1.27544 +  }else{
 1.27545 +    return &dotlockIoMethods;
 1.27546 +  }
 1.27547 +}
 1.27548 +static const sqlite3_io_methods 
 1.27549 +  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
 1.27550 +
 1.27551 +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 1.27552 +
 1.27553 +#if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
 1.27554 +/* 
 1.27555 +** This "finder" function attempts to determine the best locking strategy 
 1.27556 +** for the database file "filePath".  It then returns the sqlite3_io_methods
 1.27557 +** object that implements that strategy.
 1.27558 +**
 1.27559 +** This is for VXWorks only.
 1.27560 +*/
 1.27561 +static const sqlite3_io_methods *autolockIoFinderImpl(
 1.27562 +  const char *filePath,    /* name of the database file */
 1.27563 +  unixFile *pNew           /* the open file object */
 1.27564 +){
 1.27565 +  struct flock lockInfo;
 1.27566 +
 1.27567 +  if( !filePath ){
 1.27568 +    /* If filePath==NULL that means we are dealing with a transient file
 1.27569 +    ** that does not need to be locked. */
 1.27570 +    return &nolockIoMethods;
 1.27571 +  }
 1.27572 +
 1.27573 +  /* Test if fcntl() is supported and use POSIX style locks.
 1.27574 +  ** Otherwise fall back to the named semaphore method.
 1.27575 +  */
 1.27576 +  lockInfo.l_len = 1;
 1.27577 +  lockInfo.l_start = 0;
 1.27578 +  lockInfo.l_whence = SEEK_SET;
 1.27579 +  lockInfo.l_type = F_RDLCK;
 1.27580 +  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 1.27581 +    return &posixIoMethods;
 1.27582 +  }else{
 1.27583 +    return &semIoMethods;
 1.27584 +  }
 1.27585 +}
 1.27586 +static const sqlite3_io_methods 
 1.27587 +  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
 1.27588 +
 1.27589 +#endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
 1.27590 +
 1.27591 +/*
 1.27592 +** An abstract type for a pointer to a IO method finder function:
 1.27593 +*/
 1.27594 +typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
 1.27595 +
 1.27596 +
 1.27597 +/****************************************************************************
 1.27598 +**************************** sqlite3_vfs methods ****************************
 1.27599 +**
 1.27600 +** This division contains the implementation of methods on the
 1.27601 +** sqlite3_vfs object.
 1.27602 +*/
 1.27603 +
 1.27604 +/*
 1.27605 +** Initialize the contents of the unixFile structure pointed to by pId.
 1.27606 +*/
 1.27607 +static int fillInUnixFile(
 1.27608 +  sqlite3_vfs *pVfs,      /* Pointer to vfs object */
 1.27609 +  int h,                  /* Open file descriptor of file being opened */
 1.27610 +  sqlite3_file *pId,      /* Write to the unixFile structure here */
 1.27611 +  const char *zFilename,  /* Name of the file being opened */
 1.27612 +  int ctrlFlags           /* Zero or more UNIXFILE_* values */
 1.27613 +){
 1.27614 +  const sqlite3_io_methods *pLockingStyle;
 1.27615 +  unixFile *pNew = (unixFile *)pId;
 1.27616 +  int rc = SQLITE_OK;
 1.27617 +
 1.27618 +  assert( pNew->pInode==NULL );
 1.27619 +
 1.27620 +  /* Usually the path zFilename should not be a relative pathname. The
 1.27621 +  ** exception is when opening the proxy "conch" file in builds that
 1.27622 +  ** include the special Apple locking styles.
 1.27623 +  */
 1.27624 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.27625 +  assert( zFilename==0 || zFilename[0]=='/' 
 1.27626 +    || pVfs->pAppData==(void*)&autolockIoFinder );
 1.27627 +#else
 1.27628 +  assert( zFilename==0 || zFilename[0]=='/' );
 1.27629 +#endif
 1.27630 +
 1.27631 +  /* No locking occurs in temporary files */
 1.27632 +  assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
 1.27633 +
 1.27634 +  OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
 1.27635 +  pNew->h = h;
 1.27636 +  pNew->pVfs = pVfs;
 1.27637 +  pNew->zPath = zFilename;
 1.27638 +  pNew->ctrlFlags = (u8)ctrlFlags;
 1.27639 +  if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
 1.27640 +                           "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 1.27641 +    pNew->ctrlFlags |= UNIXFILE_PSOW;
 1.27642 +  }
 1.27643 +  if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
 1.27644 +    pNew->ctrlFlags |= UNIXFILE_EXCL;
 1.27645 +  }
 1.27646 +
 1.27647 +#if OS_VXWORKS
 1.27648 +  pNew->pId = vxworksFindFileId(zFilename);
 1.27649 +  if( pNew->pId==0 ){
 1.27650 +    ctrlFlags |= UNIXFILE_NOLOCK;
 1.27651 +    rc = SQLITE_NOMEM;
 1.27652 +  }
 1.27653 +#endif
 1.27654 +
 1.27655 +  if( ctrlFlags & UNIXFILE_NOLOCK ){
 1.27656 +    pLockingStyle = &nolockIoMethods;
 1.27657 +  }else{
 1.27658 +    pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
 1.27659 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.27660 +    /* Cache zFilename in the locking context (AFP and dotlock override) for
 1.27661 +    ** proxyLock activation is possible (remote proxy is based on db name)
 1.27662 +    ** zFilename remains valid until file is closed, to support */
 1.27663 +    pNew->lockingContext = (void*)zFilename;
 1.27664 +#endif
 1.27665 +  }
 1.27666 +
 1.27667 +  if( pLockingStyle == &posixIoMethods
 1.27668 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.27669 +    || pLockingStyle == &nfsIoMethods
 1.27670 +#endif
 1.27671 +  ){
 1.27672 +    unixEnterMutex();
 1.27673 +    rc = findInodeInfo(pNew, &pNew->pInode);
 1.27674 +    if( rc!=SQLITE_OK ){
 1.27675 +      /* If an error occured in findInodeInfo(), close the file descriptor
 1.27676 +      ** immediately, before releasing the mutex. findInodeInfo() may fail
 1.27677 +      ** in two scenarios:
 1.27678 +      **
 1.27679 +      **   (a) A call to fstat() failed.
 1.27680 +      **   (b) A malloc failed.
 1.27681 +      **
 1.27682 +      ** Scenario (b) may only occur if the process is holding no other
 1.27683 +      ** file descriptors open on the same file. If there were other file
 1.27684 +      ** descriptors on this file, then no malloc would be required by
 1.27685 +      ** findInodeInfo(). If this is the case, it is quite safe to close
 1.27686 +      ** handle h - as it is guaranteed that no posix locks will be released
 1.27687 +      ** by doing so.
 1.27688 +      **
 1.27689 +      ** If scenario (a) caused the error then things are not so safe. The
 1.27690 +      ** implicit assumption here is that if fstat() fails, things are in
 1.27691 +      ** such bad shape that dropping a lock or two doesn't matter much.
 1.27692 +      */
 1.27693 +      robust_close(pNew, h, __LINE__);
 1.27694 +      h = -1;
 1.27695 +    }
 1.27696 +    unixLeaveMutex();
 1.27697 +  }
 1.27698 +
 1.27699 +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 1.27700 +  else if( pLockingStyle == &afpIoMethods ){
 1.27701 +    /* AFP locking uses the file path so it needs to be included in
 1.27702 +    ** the afpLockingContext.
 1.27703 +    */
 1.27704 +    afpLockingContext *pCtx;
 1.27705 +    pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
 1.27706 +    if( pCtx==0 ){
 1.27707 +      rc = SQLITE_NOMEM;
 1.27708 +    }else{
 1.27709 +      /* NB: zFilename exists and remains valid until the file is closed
 1.27710 +      ** according to requirement F11141.  So we do not need to make a
 1.27711 +      ** copy of the filename. */
 1.27712 +      pCtx->dbPath = zFilename;
 1.27713 +      pCtx->reserved = 0;
 1.27714 +      srandomdev();
 1.27715 +      unixEnterMutex();
 1.27716 +      rc = findInodeInfo(pNew, &pNew->pInode);
 1.27717 +      if( rc!=SQLITE_OK ){
 1.27718 +        sqlite3_free(pNew->lockingContext);
 1.27719 +        robust_close(pNew, h, __LINE__);
 1.27720 +        h = -1;
 1.27721 +      }
 1.27722 +      unixLeaveMutex();        
 1.27723 +    }
 1.27724 +  }
 1.27725 +#endif
 1.27726 +
 1.27727 +  else if( pLockingStyle == &dotlockIoMethods ){
 1.27728 +    /* Dotfile locking uses the file path so it needs to be included in
 1.27729 +    ** the dotlockLockingContext 
 1.27730 +    */
 1.27731 +    char *zLockFile;
 1.27732 +    int nFilename;
 1.27733 +    assert( zFilename!=0 );
 1.27734 +    nFilename = (int)strlen(zFilename) + 6;
 1.27735 +    zLockFile = (char *)sqlite3_malloc(nFilename);
 1.27736 +    if( zLockFile==0 ){
 1.27737 +      rc = SQLITE_NOMEM;
 1.27738 +    }else{
 1.27739 +      sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
 1.27740 +    }
 1.27741 +    pNew->lockingContext = zLockFile;
 1.27742 +  }
 1.27743 +
 1.27744 +#if OS_VXWORKS
 1.27745 +  else if( pLockingStyle == &semIoMethods ){
 1.27746 +    /* Named semaphore locking uses the file path so it needs to be
 1.27747 +    ** included in the semLockingContext
 1.27748 +    */
 1.27749 +    unixEnterMutex();
 1.27750 +    rc = findInodeInfo(pNew, &pNew->pInode);
 1.27751 +    if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
 1.27752 +      char *zSemName = pNew->pInode->aSemName;
 1.27753 +      int n;
 1.27754 +      sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
 1.27755 +                       pNew->pId->zCanonicalName);
 1.27756 +      for( n=1; zSemName[n]; n++ )
 1.27757 +        if( zSemName[n]=='/' ) zSemName[n] = '_';
 1.27758 +      pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
 1.27759 +      if( pNew->pInode->pSem == SEM_FAILED ){
 1.27760 +        rc = SQLITE_NOMEM;
 1.27761 +        pNew->pInode->aSemName[0] = '\0';
 1.27762 +      }
 1.27763 +    }
 1.27764 +    unixLeaveMutex();
 1.27765 +  }
 1.27766 +#endif
 1.27767 +  
 1.27768 +  pNew->lastErrno = 0;
 1.27769 +#if OS_VXWORKS
 1.27770 +  if( rc!=SQLITE_OK ){
 1.27771 +    if( h>=0 ) robust_close(pNew, h, __LINE__);
 1.27772 +    h = -1;
 1.27773 +    osUnlink(zFilename);
 1.27774 +    isDelete = 0;
 1.27775 +  }
 1.27776 +  if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
 1.27777 +#endif
 1.27778 +  if( rc!=SQLITE_OK ){
 1.27779 +    if( h>=0 ) robust_close(pNew, h, __LINE__);
 1.27780 +  }else{
 1.27781 +    pNew->pMethod = pLockingStyle;
 1.27782 +    OpenCounter(+1);
 1.27783 +  }
 1.27784 +  return rc;
 1.27785 +}
 1.27786 +
 1.27787 +/*
 1.27788 +** Return the name of a directory in which to put temporary files.
 1.27789 +** If no suitable temporary file directory can be found, return NULL.
 1.27790 +*/
 1.27791 +static const char *unixTempFileDir(void){
 1.27792 +  static const char *azDirs[] = {
 1.27793 +     0,
 1.27794 +     0,
 1.27795 +     "/var/tmp",
 1.27796 +     "/usr/tmp",
 1.27797 +     "/tmp",
 1.27798 +     0        /* List terminator */
 1.27799 +  };
 1.27800 +  unsigned int i;
 1.27801 +  struct stat buf;
 1.27802 +  const char *zDir = 0;
 1.27803 +
 1.27804 +  azDirs[0] = sqlite3_temp_directory;
 1.27805 +  if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
 1.27806 +  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
 1.27807 +    if( zDir==0 ) continue;
 1.27808 +    if( osStat(zDir, &buf) ) continue;
 1.27809 +    if( !S_ISDIR(buf.st_mode) ) continue;
 1.27810 +    if( osAccess(zDir, 07) ) continue;
 1.27811 +    break;
 1.27812 +  }
 1.27813 +  return zDir;
 1.27814 +}
 1.27815 +
 1.27816 +/*
 1.27817 +** Create a temporary file name in zBuf.  zBuf must be allocated
 1.27818 +** by the calling process and must be big enough to hold at least
 1.27819 +** pVfs->mxPathname bytes.
 1.27820 +*/
 1.27821 +static int unixGetTempname(int nBuf, char *zBuf){
 1.27822 +  static const unsigned char zChars[] =
 1.27823 +    "abcdefghijklmnopqrstuvwxyz"
 1.27824 +    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 1.27825 +    "0123456789";
 1.27826 +  unsigned int i, j;
 1.27827 +  const char *zDir;
 1.27828 +
 1.27829 +  /* It's odd to simulate an io-error here, but really this is just
 1.27830 +  ** using the io-error infrastructure to test that SQLite handles this
 1.27831 +  ** function failing. 
 1.27832 +  */
 1.27833 +  SimulateIOError( return SQLITE_IOERR );
 1.27834 +
 1.27835 +  zDir = unixTempFileDir();
 1.27836 +  if( zDir==0 ) zDir = ".";
 1.27837 +
 1.27838 +  /* Check that the output buffer is large enough for the temporary file 
 1.27839 +  ** name. If it is not, return SQLITE_ERROR.
 1.27840 +  */
 1.27841 +  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
 1.27842 +    return SQLITE_ERROR;
 1.27843 +  }
 1.27844 +
 1.27845 +  do{
 1.27846 +    sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
 1.27847 +    j = (int)strlen(zBuf);
 1.27848 +    sqlite3_randomness(15, &zBuf[j]);
 1.27849 +    for(i=0; i<15; i++, j++){
 1.27850 +      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 1.27851 +    }
 1.27852 +    zBuf[j] = 0;
 1.27853 +    zBuf[j+1] = 0;
 1.27854 +  }while( osAccess(zBuf,0)==0 );
 1.27855 +  return SQLITE_OK;
 1.27856 +}
 1.27857 +
 1.27858 +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 1.27859 +/*
 1.27860 +** Routine to transform a unixFile into a proxy-locking unixFile.
 1.27861 +** Implementation in the proxy-lock division, but used by unixOpen()
 1.27862 +** if SQLITE_PREFER_PROXY_LOCKING is defined.
 1.27863 +*/
 1.27864 +static int proxyTransformUnixFile(unixFile*, const char*);
 1.27865 +#endif
 1.27866 +
 1.27867 +/*
 1.27868 +** Search for an unused file descriptor that was opened on the database 
 1.27869 +** file (not a journal or master-journal file) identified by pathname
 1.27870 +** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
 1.27871 +** argument to this function.
 1.27872 +**
 1.27873 +** Such a file descriptor may exist if a database connection was closed
 1.27874 +** but the associated file descriptor could not be closed because some
 1.27875 +** other file descriptor open on the same file is holding a file-lock.
 1.27876 +** Refer to comments in the unixClose() function and the lengthy comment
 1.27877 +** describing "Posix Advisory Locking" at the start of this file for 
 1.27878 +** further details. Also, ticket #4018.
 1.27879 +**
 1.27880 +** If a suitable file descriptor is found, then it is returned. If no
 1.27881 +** such file descriptor is located, -1 is returned.
 1.27882 +*/
 1.27883 +static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
 1.27884 +  UnixUnusedFd *pUnused = 0;
 1.27885 +
 1.27886 +  /* Do not search for an unused file descriptor on vxworks. Not because
 1.27887 +  ** vxworks would not benefit from the change (it might, we're not sure),
 1.27888 +  ** but because no way to test it is currently available. It is better 
 1.27889 +  ** not to risk breaking vxworks support for the sake of such an obscure 
 1.27890 +  ** feature.  */
 1.27891 +#if !OS_VXWORKS
 1.27892 +  struct stat sStat;                   /* Results of stat() call */
 1.27893 +
 1.27894 +  /* A stat() call may fail for various reasons. If this happens, it is
 1.27895 +  ** almost certain that an open() call on the same path will also fail.
 1.27896 +  ** For this reason, if an error occurs in the stat() call here, it is
 1.27897 +  ** ignored and -1 is returned. The caller will try to open a new file
 1.27898 +  ** descriptor on the same path, fail, and return an error to SQLite.
 1.27899 +  **
 1.27900 +  ** Even if a subsequent open() call does succeed, the consequences of
 1.27901 +  ** not searching for a resusable file descriptor are not dire.  */
 1.27902 +  if( 0==osStat(zPath, &sStat) ){
 1.27903 +    unixInodeInfo *pInode;
 1.27904 +
 1.27905 +    unixEnterMutex();
 1.27906 +    pInode = inodeList;
 1.27907 +    while( pInode && (pInode->fileId.dev!=sStat.st_dev
 1.27908 +                     || pInode->fileId.ino!=sStat.st_ino) ){
 1.27909 +       pInode = pInode->pNext;
 1.27910 +    }
 1.27911 +    if( pInode ){
 1.27912 +      UnixUnusedFd **pp;
 1.27913 +      for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
 1.27914 +      pUnused = *pp;
 1.27915 +      if( pUnused ){
 1.27916 +        *pp = pUnused->pNext;
 1.27917 +      }
 1.27918 +    }
 1.27919 +    unixLeaveMutex();
 1.27920 +  }
 1.27921 +#endif    /* if !OS_VXWORKS */
 1.27922 +  return pUnused;
 1.27923 +}
 1.27924 +
 1.27925 +/*
 1.27926 +** This function is called by unixOpen() to determine the unix permissions
 1.27927 +** to create new files with. If no error occurs, then SQLITE_OK is returned
 1.27928 +** and a value suitable for passing as the third argument to open(2) is
 1.27929 +** written to *pMode. If an IO error occurs, an SQLite error code is 
 1.27930 +** returned and the value of *pMode is not modified.
 1.27931 +**
 1.27932 +** In most cases cases, this routine sets *pMode to 0, which will become
 1.27933 +** an indication to robust_open() to create the file using
 1.27934 +** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
 1.27935 +** But if the file being opened is a WAL or regular journal file, then 
 1.27936 +** this function queries the file-system for the permissions on the 
 1.27937 +** corresponding database file and sets *pMode to this value. Whenever 
 1.27938 +** possible, WAL and journal files are created using the same permissions 
 1.27939 +** as the associated database file.
 1.27940 +**
 1.27941 +** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
 1.27942 +** original filename is unavailable.  But 8_3_NAMES is only used for
 1.27943 +** FAT filesystems and permissions do not matter there, so just use
 1.27944 +** the default permissions.
 1.27945 +*/
 1.27946 +static int findCreateFileMode(
 1.27947 +  const char *zPath,              /* Path of file (possibly) being created */
 1.27948 +  int flags,                      /* Flags passed as 4th argument to xOpen() */
 1.27949 +  mode_t *pMode,                  /* OUT: Permissions to open file with */
 1.27950 +  uid_t *pUid,                    /* OUT: uid to set on the file */
 1.27951 +  gid_t *pGid                     /* OUT: gid to set on the file */
 1.27952 +){
 1.27953 +  int rc = SQLITE_OK;             /* Return Code */
 1.27954 +  *pMode = 0;
 1.27955 +  *pUid = 0;
 1.27956 +  *pGid = 0;
 1.27957 +  if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 1.27958 +    char zDb[MAX_PATHNAME+1];     /* Database file path */
 1.27959 +    int nDb;                      /* Number of valid bytes in zDb */
 1.27960 +    struct stat sStat;            /* Output of stat() on database file */
 1.27961 +
 1.27962 +    /* zPath is a path to a WAL or journal file. The following block derives
 1.27963 +    ** the path to the associated database file from zPath. This block handles
 1.27964 +    ** the following naming conventions:
 1.27965 +    **
 1.27966 +    **   "<path to db>-journal"
 1.27967 +    **   "<path to db>-wal"
 1.27968 +    **   "<path to db>-journalNN"
 1.27969 +    **   "<path to db>-walNN"
 1.27970 +    **
 1.27971 +    ** where NN is a decimal number. The NN naming schemes are 
 1.27972 +    ** used by the test_multiplex.c module.
 1.27973 +    */
 1.27974 +    nDb = sqlite3Strlen30(zPath) - 1; 
 1.27975 +#ifdef SQLITE_ENABLE_8_3_NAMES
 1.27976 +    while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
 1.27977 +    if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
 1.27978 +#else
 1.27979 +    while( zPath[nDb]!='-' ){
 1.27980 +      assert( nDb>0 );
 1.27981 +      assert( zPath[nDb]!='\n' );
 1.27982 +      nDb--;
 1.27983 +    }
 1.27984 +#endif
 1.27985 +    memcpy(zDb, zPath, nDb);
 1.27986 +    zDb[nDb] = '\0';
 1.27987 +
 1.27988 +    if( 0==osStat(zDb, &sStat) ){
 1.27989 +      *pMode = sStat.st_mode & 0777;
 1.27990 +      *pUid = sStat.st_uid;
 1.27991 +      *pGid = sStat.st_gid;
 1.27992 +    }else{
 1.27993 +      rc = SQLITE_IOERR_FSTAT;
 1.27994 +    }
 1.27995 +  }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
 1.27996 +    *pMode = 0600;
 1.27997 +  }
 1.27998 +  return rc;
 1.27999 +}
 1.28000 +
 1.28001 +/*
 1.28002 +** Open the file zPath.
 1.28003 +** 
 1.28004 +** Previously, the SQLite OS layer used three functions in place of this
 1.28005 +** one:
 1.28006 +**
 1.28007 +**     sqlite3OsOpenReadWrite();
 1.28008 +**     sqlite3OsOpenReadOnly();
 1.28009 +**     sqlite3OsOpenExclusive();
 1.28010 +**
 1.28011 +** These calls correspond to the following combinations of flags:
 1.28012 +**
 1.28013 +**     ReadWrite() ->     (READWRITE | CREATE)
 1.28014 +**     ReadOnly()  ->     (READONLY) 
 1.28015 +**     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
 1.28016 +**
 1.28017 +** The old OpenExclusive() accepted a boolean argument - "delFlag". If
 1.28018 +** true, the file was configured to be automatically deleted when the
 1.28019 +** file handle closed. To achieve the same effect using this new 
 1.28020 +** interface, add the DELETEONCLOSE flag to those specified above for 
 1.28021 +** OpenExclusive().
 1.28022 +*/
 1.28023 +static int unixOpen(
 1.28024 +  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
 1.28025 +  const char *zPath,           /* Pathname of file to be opened */
 1.28026 +  sqlite3_file *pFile,         /* The file descriptor to be filled in */
 1.28027 +  int flags,                   /* Input flags to control the opening */
 1.28028 +  int *pOutFlags               /* Output flags returned to SQLite core */
 1.28029 +){
 1.28030 +  unixFile *p = (unixFile *)pFile;
 1.28031 +  int fd = -1;                   /* File descriptor returned by open() */
 1.28032 +  int openFlags = 0;             /* Flags to pass to open() */
 1.28033 +  int eType = flags&0xFFFFFF00;  /* Type of file to open */
 1.28034 +  int noLock;                    /* True to omit locking primitives */
 1.28035 +  int rc = SQLITE_OK;            /* Function Return Code */
 1.28036 +  int ctrlFlags = 0;             /* UNIXFILE_* flags */
 1.28037 +
 1.28038 +  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
 1.28039 +  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
 1.28040 +  int isCreate     = (flags & SQLITE_OPEN_CREATE);
 1.28041 +  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
 1.28042 +  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
 1.28043 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.28044 +  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
 1.28045 +#endif
 1.28046 +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 1.28047 +  struct statfs fsInfo;
 1.28048 +#endif
 1.28049 +
 1.28050 +  /* If creating a master or main-file journal, this function will open
 1.28051 +  ** a file-descriptor on the directory too. The first time unixSync()
 1.28052 +  ** is called the directory file descriptor will be fsync()ed and close()d.
 1.28053 +  */
 1.28054 +  int syncDir = (isCreate && (
 1.28055 +        eType==SQLITE_OPEN_MASTER_JOURNAL 
 1.28056 +     || eType==SQLITE_OPEN_MAIN_JOURNAL 
 1.28057 +     || eType==SQLITE_OPEN_WAL
 1.28058 +  ));
 1.28059 +
 1.28060 +  /* If argument zPath is a NULL pointer, this function is required to open
 1.28061 +  ** a temporary file. Use this buffer to store the file name in.
 1.28062 +  */
 1.28063 +  char zTmpname[MAX_PATHNAME+2];
 1.28064 +  const char *zName = zPath;
 1.28065 +
 1.28066 +  /* Check the following statements are true: 
 1.28067 +  **
 1.28068 +  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
 1.28069 +  **   (b) if CREATE is set, then READWRITE must also be set, and
 1.28070 +  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
 1.28071 +  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
 1.28072 +  */
 1.28073 +  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
 1.28074 +  assert(isCreate==0 || isReadWrite);
 1.28075 +  assert(isExclusive==0 || isCreate);
 1.28076 +  assert(isDelete==0 || isCreate);
 1.28077 +
 1.28078 +  /* The main DB, main journal, WAL file and master journal are never 
 1.28079 +  ** automatically deleted. Nor are they ever temporary files.  */
 1.28080 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
 1.28081 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
 1.28082 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
 1.28083 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
 1.28084 +
 1.28085 +  /* Assert that the upper layer has set one of the "file-type" flags. */
 1.28086 +  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
 1.28087 +       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
 1.28088 +       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
 1.28089 +       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
 1.28090 +  );
 1.28091 +
 1.28092 +  memset(p, 0, sizeof(unixFile));
 1.28093 +
 1.28094 +  if( eType==SQLITE_OPEN_MAIN_DB ){
 1.28095 +    UnixUnusedFd *pUnused;
 1.28096 +    pUnused = findReusableFd(zName, flags);
 1.28097 +    if( pUnused ){
 1.28098 +      fd = pUnused->fd;
 1.28099 +    }else{
 1.28100 +      pUnused = sqlite3_malloc(sizeof(*pUnused));
 1.28101 +      if( !pUnused ){
 1.28102 +        return SQLITE_NOMEM;
 1.28103 +      }
 1.28104 +    }
 1.28105 +    p->pUnused = pUnused;
 1.28106 +
 1.28107 +    /* Database filenames are double-zero terminated if they are not
 1.28108 +    ** URIs with parameters.  Hence, they can always be passed into
 1.28109 +    ** sqlite3_uri_parameter(). */
 1.28110 +    assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
 1.28111 +
 1.28112 +  }else if( !zName ){
 1.28113 +    /* If zName is NULL, the upper layer is requesting a temp file. */
 1.28114 +    assert(isDelete && !syncDir);
 1.28115 +    rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
 1.28116 +    if( rc!=SQLITE_OK ){
 1.28117 +      return rc;
 1.28118 +    }
 1.28119 +    zName = zTmpname;
 1.28120 +
 1.28121 +    /* Generated temporary filenames are always double-zero terminated
 1.28122 +    ** for use by sqlite3_uri_parameter(). */
 1.28123 +    assert( zName[strlen(zName)+1]==0 );
 1.28124 +  }
 1.28125 +
 1.28126 +  /* Determine the value of the flags parameter passed to POSIX function
 1.28127 +  ** open(). These must be calculated even if open() is not called, as
 1.28128 +  ** they may be stored as part of the file handle and used by the 
 1.28129 +  ** 'conch file' locking functions later on.  */
 1.28130 +  if( isReadonly )  openFlags |= O_RDONLY;
 1.28131 +  if( isReadWrite ) openFlags |= O_RDWR;
 1.28132 +  if( isCreate )    openFlags |= O_CREAT;
 1.28133 +  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
 1.28134 +  openFlags |= (O_LARGEFILE|O_BINARY);
 1.28135 +
 1.28136 +  if( fd<0 ){
 1.28137 +    mode_t openMode;              /* Permissions to create file with */
 1.28138 +    uid_t uid;                    /* Userid for the file */
 1.28139 +    gid_t gid;                    /* Groupid for the file */
 1.28140 +    rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
 1.28141 +    if( rc!=SQLITE_OK ){
 1.28142 +      assert( !p->pUnused );
 1.28143 +      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
 1.28144 +      return rc;
 1.28145 +    }
 1.28146 +    fd = robust_open(zName, openFlags, openMode);
 1.28147 +    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
 1.28148 +    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
 1.28149 +      /* Failed to open the file for read/write access. Try read-only. */
 1.28150 +      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
 1.28151 +      openFlags &= ~(O_RDWR|O_CREAT);
 1.28152 +      flags |= SQLITE_OPEN_READONLY;
 1.28153 +      openFlags |= O_RDONLY;
 1.28154 +      isReadonly = 1;
 1.28155 +      fd = robust_open(zName, openFlags, openMode);
 1.28156 +    }
 1.28157 +    if( fd<0 ){
 1.28158 +      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
 1.28159 +      goto open_finished;
 1.28160 +    }
 1.28161 +
 1.28162 +    /* If this process is running as root and if creating a new rollback
 1.28163 +    ** journal or WAL file, set the ownership of the journal or WAL to be
 1.28164 +    ** the same as the original database.
 1.28165 +    */
 1.28166 +    if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 1.28167 +      osFchown(fd, uid, gid);
 1.28168 +    }
 1.28169 +  }
 1.28170 +  assert( fd>=0 );
 1.28171 +  if( pOutFlags ){
 1.28172 +    *pOutFlags = flags;
 1.28173 +  }
 1.28174 +
 1.28175 +  if( p->pUnused ){
 1.28176 +    p->pUnused->fd = fd;
 1.28177 +    p->pUnused->flags = flags;
 1.28178 +  }
 1.28179 +
 1.28180 +  if( isDelete ){
 1.28181 +#if OS_VXWORKS
 1.28182 +    zPath = zName;
 1.28183 +#else
 1.28184 +    osUnlink(zName);
 1.28185 +#endif
 1.28186 +  }
 1.28187 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.28188 +  else{
 1.28189 +    p->openFlags = openFlags;
 1.28190 +  }
 1.28191 +#endif
 1.28192 +
 1.28193 +  noLock = eType!=SQLITE_OPEN_MAIN_DB;
 1.28194 +
 1.28195 +  
 1.28196 +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 1.28197 +  if( fstatfs(fd, &fsInfo) == -1 ){
 1.28198 +    ((unixFile*)pFile)->lastErrno = errno;
 1.28199 +    robust_close(p, fd, __LINE__);
 1.28200 +    return SQLITE_IOERR_ACCESS;
 1.28201 +  }
 1.28202 +  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
 1.28203 +    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
 1.28204 +  }
 1.28205 +#endif
 1.28206 +
 1.28207 +  /* Set up appropriate ctrlFlags */
 1.28208 +  if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
 1.28209 +  if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
 1.28210 +  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
 1.28211 +  if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
 1.28212 +  if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
 1.28213 +
 1.28214 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.28215 +#if SQLITE_PREFER_PROXY_LOCKING
 1.28216 +  isAutoProxy = 1;
 1.28217 +#endif
 1.28218 +  if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
 1.28219 +    char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
 1.28220 +    int useProxy = 0;
 1.28221 +
 1.28222 +    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
 1.28223 +    ** never use proxy, NULL means use proxy for non-local files only.  */
 1.28224 +    if( envforce!=NULL ){
 1.28225 +      useProxy = atoi(envforce)>0;
 1.28226 +    }else{
 1.28227 +      if( statfs(zPath, &fsInfo) == -1 ){
 1.28228 +        /* In theory, the close(fd) call is sub-optimal. If the file opened
 1.28229 +        ** with fd is a database file, and there are other connections open
 1.28230 +        ** on that file that are currently holding advisory locks on it,
 1.28231 +        ** then the call to close() will cancel those locks. In practice,
 1.28232 +        ** we're assuming that statfs() doesn't fail very often. At least
 1.28233 +        ** not while other file descriptors opened by the same process on
 1.28234 +        ** the same file are working.  */
 1.28235 +        p->lastErrno = errno;
 1.28236 +        robust_close(p, fd, __LINE__);
 1.28237 +        rc = SQLITE_IOERR_ACCESS;
 1.28238 +        goto open_finished;
 1.28239 +      }
 1.28240 +      useProxy = !(fsInfo.f_flags&MNT_LOCAL);
 1.28241 +    }
 1.28242 +    if( useProxy ){
 1.28243 +      rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 1.28244 +      if( rc==SQLITE_OK ){
 1.28245 +        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
 1.28246 +        if( rc!=SQLITE_OK ){
 1.28247 +          /* Use unixClose to clean up the resources added in fillInUnixFile 
 1.28248 +          ** and clear all the structure's references.  Specifically, 
 1.28249 +          ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op 
 1.28250 +          */
 1.28251 +          unixClose(pFile);
 1.28252 +          return rc;
 1.28253 +        }
 1.28254 +      }
 1.28255 +      goto open_finished;
 1.28256 +    }
 1.28257 +  }
 1.28258 +#endif
 1.28259 +  
 1.28260 +  rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 1.28261 +
 1.28262 +open_finished:
 1.28263 +  if( rc!=SQLITE_OK ){
 1.28264 +    sqlite3_free(p->pUnused);
 1.28265 +  }
 1.28266 +  return rc;
 1.28267 +}
 1.28268 +
 1.28269 +
 1.28270 +/*
 1.28271 +** Delete the file at zPath. If the dirSync argument is true, fsync()
 1.28272 +** the directory after deleting the file.
 1.28273 +*/
 1.28274 +static int unixDelete(
 1.28275 +  sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
 1.28276 +  const char *zPath,        /* Name of file to be deleted */
 1.28277 +  int dirSync               /* If true, fsync() directory after deleting file */
 1.28278 +){
 1.28279 +  int rc = SQLITE_OK;
 1.28280 +  UNUSED_PARAMETER(NotUsed);
 1.28281 +  SimulateIOError(return SQLITE_IOERR_DELETE);
 1.28282 +  if( osUnlink(zPath)==(-1) ){
 1.28283 +    if( errno==ENOENT ){
 1.28284 +      rc = SQLITE_IOERR_DELETE_NOENT;
 1.28285 +    }else{
 1.28286 +      rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
 1.28287 +    }
 1.28288 +    return rc;
 1.28289 +  }
 1.28290 +#ifndef SQLITE_DISABLE_DIRSYNC
 1.28291 +  if( (dirSync & 1)!=0 ){
 1.28292 +    int fd;
 1.28293 +    rc = osOpenDirectory(zPath, &fd);
 1.28294 +    if( rc==SQLITE_OK ){
 1.28295 +#if OS_VXWORKS
 1.28296 +      if( fsync(fd)==-1 )
 1.28297 +#else
 1.28298 +      if( fsync(fd) )
 1.28299 +#endif
 1.28300 +      {
 1.28301 +        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
 1.28302 +      }
 1.28303 +      robust_close(0, fd, __LINE__);
 1.28304 +    }else if( rc==SQLITE_CANTOPEN ){
 1.28305 +      rc = SQLITE_OK;
 1.28306 +    }
 1.28307 +  }
 1.28308 +#endif
 1.28309 +  return rc;
 1.28310 +}
 1.28311 +
 1.28312 +/*
 1.28313 +** Test the existance of or access permissions of file zPath. The
 1.28314 +** test performed depends on the value of flags:
 1.28315 +**
 1.28316 +**     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
 1.28317 +**     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
 1.28318 +**     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
 1.28319 +**
 1.28320 +** Otherwise return 0.
 1.28321 +*/
 1.28322 +static int unixAccess(
 1.28323 +  sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
 1.28324 +  const char *zPath,      /* Path of the file to examine */
 1.28325 +  int flags,              /* What do we want to learn about the zPath file? */
 1.28326 +  int *pResOut            /* Write result boolean here */
 1.28327 +){
 1.28328 +  int amode = 0;
 1.28329 +  UNUSED_PARAMETER(NotUsed);
 1.28330 +  SimulateIOError( return SQLITE_IOERR_ACCESS; );
 1.28331 +  switch( flags ){
 1.28332 +    case SQLITE_ACCESS_EXISTS:
 1.28333 +      amode = F_OK;
 1.28334 +      break;
 1.28335 +    case SQLITE_ACCESS_READWRITE:
 1.28336 +      amode = W_OK|R_OK;
 1.28337 +      break;
 1.28338 +    case SQLITE_ACCESS_READ:
 1.28339 +      amode = R_OK;
 1.28340 +      break;
 1.28341 +
 1.28342 +    default:
 1.28343 +      assert(!"Invalid flags argument");
 1.28344 +  }
 1.28345 +  *pResOut = (osAccess(zPath, amode)==0);
 1.28346 +  if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
 1.28347 +    struct stat buf;
 1.28348 +    if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
 1.28349 +      *pResOut = 0;
 1.28350 +    }
 1.28351 +  }
 1.28352 +  return SQLITE_OK;
 1.28353 +}
 1.28354 +
 1.28355 +
 1.28356 +/*
 1.28357 +** Turn a relative pathname into a full pathname. The relative path
 1.28358 +** is stored as a nul-terminated string in the buffer pointed to by
 1.28359 +** zPath. 
 1.28360 +**
 1.28361 +** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 
 1.28362 +** (in this case, MAX_PATHNAME bytes). The full-path is written to
 1.28363 +** this buffer before returning.
 1.28364 +*/
 1.28365 +static int unixFullPathname(
 1.28366 +  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
 1.28367 +  const char *zPath,            /* Possibly relative input path */
 1.28368 +  int nOut,                     /* Size of output buffer in bytes */
 1.28369 +  char *zOut                    /* Output buffer */
 1.28370 +){
 1.28371 +
 1.28372 +  /* It's odd to simulate an io-error here, but really this is just
 1.28373 +  ** using the io-error infrastructure to test that SQLite handles this
 1.28374 +  ** function failing. This function could fail if, for example, the
 1.28375 +  ** current working directory has been unlinked.
 1.28376 +  */
 1.28377 +  SimulateIOError( return SQLITE_ERROR );
 1.28378 +
 1.28379 +  assert( pVfs->mxPathname==MAX_PATHNAME );
 1.28380 +  UNUSED_PARAMETER(pVfs);
 1.28381 +
 1.28382 +  zOut[nOut-1] = '\0';
 1.28383 +  if( zPath[0]=='/' ){
 1.28384 +    sqlite3_snprintf(nOut, zOut, "%s", zPath);
 1.28385 +  }else{
 1.28386 +    int nCwd;
 1.28387 +    if( osGetcwd(zOut, nOut-1)==0 ){
 1.28388 +      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
 1.28389 +    }
 1.28390 +    nCwd = (int)strlen(zOut);
 1.28391 +    sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
 1.28392 +  }
 1.28393 +  return SQLITE_OK;
 1.28394 +}
 1.28395 +
 1.28396 +
 1.28397 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
 1.28398 +/*
 1.28399 +** Interfaces for opening a shared library, finding entry points
 1.28400 +** within the shared library, and closing the shared library.
 1.28401 +*/
 1.28402 +#include <dlfcn.h>
 1.28403 +static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
 1.28404 +  UNUSED_PARAMETER(NotUsed);
 1.28405 +  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
 1.28406 +}
 1.28407 +
 1.28408 +/*
 1.28409 +** SQLite calls this function immediately after a call to unixDlSym() or
 1.28410 +** unixDlOpen() fails (returns a null pointer). If a more detailed error
 1.28411 +** message is available, it is written to zBufOut. If no error message
 1.28412 +** is available, zBufOut is left unmodified and SQLite uses a default
 1.28413 +** error message.
 1.28414 +*/
 1.28415 +static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
 1.28416 +  const char *zErr;
 1.28417 +  UNUSED_PARAMETER(NotUsed);
 1.28418 +  unixEnterMutex();
 1.28419 +  zErr = dlerror();
 1.28420 +  if( zErr ){
 1.28421 +    sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
 1.28422 +  }
 1.28423 +  unixLeaveMutex();
 1.28424 +}
 1.28425 +static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
 1.28426 +  /* 
 1.28427 +  ** GCC with -pedantic-errors says that C90 does not allow a void* to be
 1.28428 +  ** cast into a pointer to a function.  And yet the library dlsym() routine
 1.28429 +  ** returns a void* which is really a pointer to a function.  So how do we
 1.28430 +  ** use dlsym() with -pedantic-errors?
 1.28431 +  **
 1.28432 +  ** Variable x below is defined to be a pointer to a function taking
 1.28433 +  ** parameters void* and const char* and returning a pointer to a function.
 1.28434 +  ** We initialize x by assigning it a pointer to the dlsym() function.
 1.28435 +  ** (That assignment requires a cast.)  Then we call the function that
 1.28436 +  ** x points to.  
 1.28437 +  **
 1.28438 +  ** This work-around is unlikely to work correctly on any system where
 1.28439 +  ** you really cannot cast a function pointer into void*.  But then, on the
 1.28440 +  ** other hand, dlsym() will not work on such a system either, so we have
 1.28441 +  ** not really lost anything.
 1.28442 +  */
 1.28443 +  void (*(*x)(void*,const char*))(void);
 1.28444 +  UNUSED_PARAMETER(NotUsed);
 1.28445 +  x = (void(*(*)(void*,const char*))(void))dlsym;
 1.28446 +  return (*x)(p, zSym);
 1.28447 +}
 1.28448 +static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
 1.28449 +  UNUSED_PARAMETER(NotUsed);
 1.28450 +  dlclose(pHandle);
 1.28451 +}
 1.28452 +#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 1.28453 +  #define unixDlOpen  0
 1.28454 +  #define unixDlError 0
 1.28455 +  #define unixDlSym   0
 1.28456 +  #define unixDlClose 0
 1.28457 +#endif
 1.28458 +
 1.28459 +/*
 1.28460 +** Write nBuf bytes of random data to the supplied buffer zBuf.
 1.28461 +*/
 1.28462 +static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
 1.28463 +  UNUSED_PARAMETER(NotUsed);
 1.28464 +  assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
 1.28465 +
 1.28466 +  /* We have to initialize zBuf to prevent valgrind from reporting
 1.28467 +  ** errors.  The reports issued by valgrind are incorrect - we would
 1.28468 +  ** prefer that the randomness be increased by making use of the
 1.28469 +  ** uninitialized space in zBuf - but valgrind errors tend to worry
 1.28470 +  ** some users.  Rather than argue, it seems easier just to initialize
 1.28471 +  ** the whole array and silence valgrind, even if that means less randomness
 1.28472 +  ** in the random seed.
 1.28473 +  **
 1.28474 +  ** When testing, initializing zBuf[] to zero is all we do.  That means
 1.28475 +  ** that we always use the same random number sequence.  This makes the
 1.28476 +  ** tests repeatable.
 1.28477 +  */
 1.28478 +  memset(zBuf, 0, nBuf);
 1.28479 +#if !defined(SQLITE_TEST)
 1.28480 +  {
 1.28481 +    int pid, fd, got;
 1.28482 +    fd = robust_open("/dev/urandom", O_RDONLY, 0);
 1.28483 +    if( fd<0 ){
 1.28484 +      time_t t;
 1.28485 +      time(&t);
 1.28486 +      memcpy(zBuf, &t, sizeof(t));
 1.28487 +      pid = getpid();
 1.28488 +      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
 1.28489 +      assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
 1.28490 +      nBuf = sizeof(t) + sizeof(pid);
 1.28491 +    }else{
 1.28492 +      do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
 1.28493 +      robust_close(0, fd, __LINE__);
 1.28494 +    }
 1.28495 +  }
 1.28496 +#endif
 1.28497 +  return nBuf;
 1.28498 +}
 1.28499 +
 1.28500 +
 1.28501 +/*
 1.28502 +** Sleep for a little while.  Return the amount of time slept.
 1.28503 +** The argument is the number of microseconds we want to sleep.
 1.28504 +** The return value is the number of microseconds of sleep actually
 1.28505 +** requested from the underlying operating system, a number which
 1.28506 +** might be greater than or equal to the argument, but not less
 1.28507 +** than the argument.
 1.28508 +*/
 1.28509 +static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
 1.28510 +#if OS_VXWORKS
 1.28511 +  struct timespec sp;
 1.28512 +
 1.28513 +  sp.tv_sec = microseconds / 1000000;
 1.28514 +  sp.tv_nsec = (microseconds % 1000000) * 1000;
 1.28515 +  nanosleep(&sp, NULL);
 1.28516 +  UNUSED_PARAMETER(NotUsed);
 1.28517 +  return microseconds;
 1.28518 +#elif defined(HAVE_USLEEP) && HAVE_USLEEP
 1.28519 +  usleep(microseconds);
 1.28520 +  UNUSED_PARAMETER(NotUsed);
 1.28521 +  return microseconds;
 1.28522 +#else
 1.28523 +  int seconds = (microseconds+999999)/1000000;
 1.28524 +  sleep(seconds);
 1.28525 +  UNUSED_PARAMETER(NotUsed);
 1.28526 +  return seconds*1000000;
 1.28527 +#endif
 1.28528 +}
 1.28529 +
 1.28530 +/*
 1.28531 +** The following variable, if set to a non-zero value, is interpreted as
 1.28532 +** the number of seconds since 1970 and is used to set the result of
 1.28533 +** sqlite3OsCurrentTime() during testing.
 1.28534 +*/
 1.28535 +#ifdef SQLITE_TEST
 1.28536 +SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
 1.28537 +#endif
 1.28538 +
 1.28539 +/*
 1.28540 +** Find the current time (in Universal Coordinated Time).  Write into *piNow
 1.28541 +** the current time and date as a Julian Day number times 86_400_000.  In
 1.28542 +** other words, write into *piNow the number of milliseconds since the Julian
 1.28543 +** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 1.28544 +** proleptic Gregorian calendar.
 1.28545 +**
 1.28546 +** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
 1.28547 +** cannot be found.
 1.28548 +*/
 1.28549 +static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
 1.28550 +  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 1.28551 +  int rc = SQLITE_OK;
 1.28552 +#if defined(NO_GETTOD)
 1.28553 +  time_t t;
 1.28554 +  time(&t);
 1.28555 +  *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
 1.28556 +#elif OS_VXWORKS
 1.28557 +  struct timespec sNow;
 1.28558 +  clock_gettime(CLOCK_REALTIME, &sNow);
 1.28559 +  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
 1.28560 +#else
 1.28561 +  struct timeval sNow;
 1.28562 +  if( gettimeofday(&sNow, 0)==0 ){
 1.28563 +    *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
 1.28564 +  }else{
 1.28565 +    rc = SQLITE_ERROR;
 1.28566 +  }
 1.28567 +#endif
 1.28568 +
 1.28569 +#ifdef SQLITE_TEST
 1.28570 +  if( sqlite3_current_time ){
 1.28571 +    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 1.28572 +  }
 1.28573 +#endif
 1.28574 +  UNUSED_PARAMETER(NotUsed);
 1.28575 +  return rc;
 1.28576 +}
 1.28577 +
 1.28578 +/*
 1.28579 +** Find the current time (in Universal Coordinated Time).  Write the
 1.28580 +** current time and date as a Julian Day number into *prNow and
 1.28581 +** return 0.  Return 1 if the time and date cannot be found.
 1.28582 +*/
 1.28583 +static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
 1.28584 +  sqlite3_int64 i = 0;
 1.28585 +  int rc;
 1.28586 +  UNUSED_PARAMETER(NotUsed);
 1.28587 +  rc = unixCurrentTimeInt64(0, &i);
 1.28588 +  *prNow = i/86400000.0;
 1.28589 +  return rc;
 1.28590 +}
 1.28591 +
 1.28592 +/*
 1.28593 +** We added the xGetLastError() method with the intention of providing
 1.28594 +** better low-level error messages when operating-system problems come up
 1.28595 +** during SQLite operation.  But so far, none of that has been implemented
 1.28596 +** in the core.  So this routine is never called.  For now, it is merely
 1.28597 +** a place-holder.
 1.28598 +*/
 1.28599 +static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
 1.28600 +  UNUSED_PARAMETER(NotUsed);
 1.28601 +  UNUSED_PARAMETER(NotUsed2);
 1.28602 +  UNUSED_PARAMETER(NotUsed3);
 1.28603 +  return 0;
 1.28604 +}
 1.28605 +
 1.28606 +
 1.28607 +/*
 1.28608 +************************ End of sqlite3_vfs methods ***************************
 1.28609 +******************************************************************************/
 1.28610 +
 1.28611 +/******************************************************************************
 1.28612 +************************** Begin Proxy Locking ********************************
 1.28613 +**
 1.28614 +** Proxy locking is a "uber-locking-method" in this sense:  It uses the
 1.28615 +** other locking methods on secondary lock files.  Proxy locking is a
 1.28616 +** meta-layer over top of the primitive locking implemented above.  For
 1.28617 +** this reason, the division that implements of proxy locking is deferred
 1.28618 +** until late in the file (here) after all of the other I/O methods have
 1.28619 +** been defined - so that the primitive locking methods are available
 1.28620 +** as services to help with the implementation of proxy locking.
 1.28621 +**
 1.28622 +****
 1.28623 +**
 1.28624 +** The default locking schemes in SQLite use byte-range locks on the
 1.28625 +** database file to coordinate safe, concurrent access by multiple readers
 1.28626 +** and writers [http://sqlite.org/lockingv3.html].  The five file locking
 1.28627 +** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
 1.28628 +** as POSIX read & write locks over fixed set of locations (via fsctl),
 1.28629 +** on AFP and SMB only exclusive byte-range locks are available via fsctl
 1.28630 +** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
 1.28631 +** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
 1.28632 +** address in the shared range is taken for a SHARED lock, the entire
 1.28633 +** shared range is taken for an EXCLUSIVE lock):
 1.28634 +**
 1.28635 +**      PENDING_BYTE        0x40000000
 1.28636 +**      RESERVED_BYTE       0x40000001
 1.28637 +**      SHARED_RANGE        0x40000002 -> 0x40000200
 1.28638 +**
 1.28639 +** This works well on the local file system, but shows a nearly 100x
 1.28640 +** slowdown in read performance on AFP because the AFP client disables
 1.28641 +** the read cache when byte-range locks are present.  Enabling the read
 1.28642 +** cache exposes a cache coherency problem that is present on all OS X
 1.28643 +** supported network file systems.  NFS and AFP both observe the
 1.28644 +** close-to-open semantics for ensuring cache coherency
 1.28645 +** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
 1.28646 +** address the requirements for concurrent database access by multiple
 1.28647 +** readers and writers
 1.28648 +** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
 1.28649 +**
 1.28650 +** To address the performance and cache coherency issues, proxy file locking
 1.28651 +** changes the way database access is controlled by limiting access to a
 1.28652 +** single host at a time and moving file locks off of the database file
 1.28653 +** and onto a proxy file on the local file system.  
 1.28654 +**
 1.28655 +**
 1.28656 +** Using proxy locks
 1.28657 +** -----------------
 1.28658 +**
 1.28659 +** C APIs
 1.28660 +**
 1.28661 +**  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
 1.28662 +**                       <proxy_path> | ":auto:");
 1.28663 +**  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
 1.28664 +**
 1.28665 +**
 1.28666 +** SQL pragmas
 1.28667 +**
 1.28668 +**  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
 1.28669 +**  PRAGMA [database.]lock_proxy_file
 1.28670 +**
 1.28671 +** Specifying ":auto:" means that if there is a conch file with a matching
 1.28672 +** host ID in it, the proxy path in the conch file will be used, otherwise
 1.28673 +** a proxy path based on the user's temp dir
 1.28674 +** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
 1.28675 +** actual proxy file name is generated from the name and path of the
 1.28676 +** database file.  For example:
 1.28677 +**
 1.28678 +**       For database path "/Users/me/foo.db" 
 1.28679 +**       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
 1.28680 +**
 1.28681 +** Once a lock proxy is configured for a database connection, it can not
 1.28682 +** be removed, however it may be switched to a different proxy path via
 1.28683 +** the above APIs (assuming the conch file is not being held by another
 1.28684 +** connection or process). 
 1.28685 +**
 1.28686 +**
 1.28687 +** How proxy locking works
 1.28688 +** -----------------------
 1.28689 +**
 1.28690 +** Proxy file locking relies primarily on two new supporting files: 
 1.28691 +**
 1.28692 +**   *  conch file to limit access to the database file to a single host
 1.28693 +**      at a time
 1.28694 +**
 1.28695 +**   *  proxy file to act as a proxy for the advisory locks normally
 1.28696 +**      taken on the database
 1.28697 +**
 1.28698 +** The conch file - to use a proxy file, sqlite must first "hold the conch"
 1.28699 +** by taking an sqlite-style shared lock on the conch file, reading the
 1.28700 +** contents and comparing the host's unique host ID (see below) and lock
 1.28701 +** proxy path against the values stored in the conch.  The conch file is
 1.28702 +** stored in the same directory as the database file and the file name
 1.28703 +** is patterned after the database file name as ".<databasename>-conch".
 1.28704 +** If the conch file does not exist, or it's contents do not match the
 1.28705 +** host ID and/or proxy path, then the lock is escalated to an exclusive
 1.28706 +** lock and the conch file contents is updated with the host ID and proxy
 1.28707 +** path and the lock is downgraded to a shared lock again.  If the conch
 1.28708 +** is held by another process (with a shared lock), the exclusive lock
 1.28709 +** will fail and SQLITE_BUSY is returned.
 1.28710 +**
 1.28711 +** The proxy file - a single-byte file used for all advisory file locks
 1.28712 +** normally taken on the database file.   This allows for safe sharing
 1.28713 +** of the database file for multiple readers and writers on the same
 1.28714 +** host (the conch ensures that they all use the same local lock file).
 1.28715 +**
 1.28716 +** Requesting the lock proxy does not immediately take the conch, it is
 1.28717 +** only taken when the first request to lock database file is made.  
 1.28718 +** This matches the semantics of the traditional locking behavior, where
 1.28719 +** opening a connection to a database file does not take a lock on it.
 1.28720 +** The shared lock and an open file descriptor are maintained until 
 1.28721 +** the connection to the database is closed. 
 1.28722 +**
 1.28723 +** The proxy file and the lock file are never deleted so they only need
 1.28724 +** to be created the first time they are used.
 1.28725 +**
 1.28726 +** Configuration options
 1.28727 +** ---------------------
 1.28728 +**
 1.28729 +**  SQLITE_PREFER_PROXY_LOCKING
 1.28730 +**
 1.28731 +**       Database files accessed on non-local file systems are
 1.28732 +**       automatically configured for proxy locking, lock files are
 1.28733 +**       named automatically using the same logic as
 1.28734 +**       PRAGMA lock_proxy_file=":auto:"
 1.28735 +**    
 1.28736 +**  SQLITE_PROXY_DEBUG
 1.28737 +**
 1.28738 +**       Enables the logging of error messages during host id file
 1.28739 +**       retrieval and creation
 1.28740 +**
 1.28741 +**  LOCKPROXYDIR
 1.28742 +**
 1.28743 +**       Overrides the default directory used for lock proxy files that
 1.28744 +**       are named automatically via the ":auto:" setting
 1.28745 +**
 1.28746 +**  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 1.28747 +**
 1.28748 +**       Permissions to use when creating a directory for storing the
 1.28749 +**       lock proxy files, only used when LOCKPROXYDIR is not set.
 1.28750 +**    
 1.28751 +**    
 1.28752 +** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
 1.28753 +** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
 1.28754 +** force proxy locking to be used for every database file opened, and 0
 1.28755 +** will force automatic proxy locking to be disabled for all database
 1.28756 +** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
 1.28757 +** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
 1.28758 +*/
 1.28759 +
 1.28760 +/*
 1.28761 +** Proxy locking is only available on MacOSX 
 1.28762 +*/
 1.28763 +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 1.28764 +
 1.28765 +/*
 1.28766 +** The proxyLockingContext has the path and file structures for the remote 
 1.28767 +** and local proxy files in it
 1.28768 +*/
 1.28769 +typedef struct proxyLockingContext proxyLockingContext;
 1.28770 +struct proxyLockingContext {
 1.28771 +  unixFile *conchFile;         /* Open conch file */
 1.28772 +  char *conchFilePath;         /* Name of the conch file */
 1.28773 +  unixFile *lockProxy;         /* Open proxy lock file */
 1.28774 +  char *lockProxyPath;         /* Name of the proxy lock file */
 1.28775 +  char *dbPath;                /* Name of the open file */
 1.28776 +  int conchHeld;               /* 1 if the conch is held, -1 if lockless */
 1.28777 +  void *oldLockingContext;     /* Original lockingcontext to restore on close */
 1.28778 +  sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
 1.28779 +};
 1.28780 +
 1.28781 +/* 
 1.28782 +** The proxy lock file path for the database at dbPath is written into lPath, 
 1.28783 +** which must point to valid, writable memory large enough for a maxLen length
 1.28784 +** file path. 
 1.28785 +*/
 1.28786 +static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
 1.28787 +  int len;
 1.28788 +  int dbLen;
 1.28789 +  int i;
 1.28790 +
 1.28791 +#ifdef LOCKPROXYDIR
 1.28792 +  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
 1.28793 +#else
 1.28794 +# ifdef _CS_DARWIN_USER_TEMP_DIR
 1.28795 +  {
 1.28796 +    if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
 1.28797 +      OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
 1.28798 +               lPath, errno, getpid()));
 1.28799 +      return SQLITE_IOERR_LOCK;
 1.28800 +    }
 1.28801 +    len = strlcat(lPath, "sqliteplocks", maxLen);    
 1.28802 +  }
 1.28803 +# else
 1.28804 +  len = strlcpy(lPath, "/tmp/", maxLen);
 1.28805 +# endif
 1.28806 +#endif
 1.28807 +
 1.28808 +  if( lPath[len-1]!='/' ){
 1.28809 +    len = strlcat(lPath, "/", maxLen);
 1.28810 +  }
 1.28811 +  
 1.28812 +  /* transform the db path to a unique cache name */
 1.28813 +  dbLen = (int)strlen(dbPath);
 1.28814 +  for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
 1.28815 +    char c = dbPath[i];
 1.28816 +    lPath[i+len] = (c=='/')?'_':c;
 1.28817 +  }
 1.28818 +  lPath[i+len]='\0';
 1.28819 +  strlcat(lPath, ":auto:", maxLen);
 1.28820 +  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
 1.28821 +  return SQLITE_OK;
 1.28822 +}
 1.28823 +
 1.28824 +/* 
 1.28825 + ** Creates the lock file and any missing directories in lockPath
 1.28826 + */
 1.28827 +static int proxyCreateLockPath(const char *lockPath){
 1.28828 +  int i, len;
 1.28829 +  char buf[MAXPATHLEN];
 1.28830 +  int start = 0;
 1.28831 +  
 1.28832 +  assert(lockPath!=NULL);
 1.28833 +  /* try to create all the intermediate directories */
 1.28834 +  len = (int)strlen(lockPath);
 1.28835 +  buf[0] = lockPath[0];
 1.28836 +  for( i=1; i<len; i++ ){
 1.28837 +    if( lockPath[i] == '/' && (i - start > 0) ){
 1.28838 +      /* only mkdir if leaf dir != "." or "/" or ".." */
 1.28839 +      if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') 
 1.28840 +         || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
 1.28841 +        buf[i]='\0';
 1.28842 +        if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
 1.28843 +          int err=errno;
 1.28844 +          if( err!=EEXIST ) {
 1.28845 +            OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
 1.28846 +                     "'%s' proxy lock path=%s pid=%d\n",
 1.28847 +                     buf, strerror(err), lockPath, getpid()));
 1.28848 +            return err;
 1.28849 +          }
 1.28850 +        }
 1.28851 +      }
 1.28852 +      start=i+1;
 1.28853 +    }
 1.28854 +    buf[i] = lockPath[i];
 1.28855 +  }
 1.28856 +  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
 1.28857 +  return 0;
 1.28858 +}
 1.28859 +
 1.28860 +/*
 1.28861 +** Create a new VFS file descriptor (stored in memory obtained from
 1.28862 +** sqlite3_malloc) and open the file named "path" in the file descriptor.
 1.28863 +**
 1.28864 +** The caller is responsible not only for closing the file descriptor
 1.28865 +** but also for freeing the memory associated with the file descriptor.
 1.28866 +*/
 1.28867 +static int proxyCreateUnixFile(
 1.28868 +    const char *path,        /* path for the new unixFile */
 1.28869 +    unixFile **ppFile,       /* unixFile created and returned by ref */
 1.28870 +    int islockfile           /* if non zero missing dirs will be created */
 1.28871 +) {
 1.28872 +  int fd = -1;
 1.28873 +  unixFile *pNew;
 1.28874 +  int rc = SQLITE_OK;
 1.28875 +  int openFlags = O_RDWR | O_CREAT;
 1.28876 +  sqlite3_vfs dummyVfs;
 1.28877 +  int terrno = 0;
 1.28878 +  UnixUnusedFd *pUnused = NULL;
 1.28879 +
 1.28880 +  /* 1. first try to open/create the file
 1.28881 +  ** 2. if that fails, and this is a lock file (not-conch), try creating
 1.28882 +  ** the parent directories and then try again.
 1.28883 +  ** 3. if that fails, try to open the file read-only
 1.28884 +  ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
 1.28885 +  */
 1.28886 +  pUnused = findReusableFd(path, openFlags);
 1.28887 +  if( pUnused ){
 1.28888 +    fd = pUnused->fd;
 1.28889 +  }else{
 1.28890 +    pUnused = sqlite3_malloc(sizeof(*pUnused));
 1.28891 +    if( !pUnused ){
 1.28892 +      return SQLITE_NOMEM;
 1.28893 +    }
 1.28894 +  }
 1.28895 +  if( fd<0 ){
 1.28896 +    fd = robust_open(path, openFlags, 0);
 1.28897 +    terrno = errno;
 1.28898 +    if( fd<0 && errno==ENOENT && islockfile ){
 1.28899 +      if( proxyCreateLockPath(path) == SQLITE_OK ){
 1.28900 +        fd = robust_open(path, openFlags, 0);
 1.28901 +      }
 1.28902 +    }
 1.28903 +  }
 1.28904 +  if( fd<0 ){
 1.28905 +    openFlags = O_RDONLY;
 1.28906 +    fd = robust_open(path, openFlags, 0);
 1.28907 +    terrno = errno;
 1.28908 +  }
 1.28909 +  if( fd<0 ){
 1.28910 +    if( islockfile ){
 1.28911 +      return SQLITE_BUSY;
 1.28912 +    }
 1.28913 +    switch (terrno) {
 1.28914 +      case EACCES:
 1.28915 +        return SQLITE_PERM;
 1.28916 +      case EIO: 
 1.28917 +        return SQLITE_IOERR_LOCK; /* even though it is the conch */
 1.28918 +      default:
 1.28919 +        return SQLITE_CANTOPEN_BKPT;
 1.28920 +    }
 1.28921 +  }
 1.28922 +  
 1.28923 +  pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
 1.28924 +  if( pNew==NULL ){
 1.28925 +    rc = SQLITE_NOMEM;
 1.28926 +    goto end_create_proxy;
 1.28927 +  }
 1.28928 +  memset(pNew, 0, sizeof(unixFile));
 1.28929 +  pNew->openFlags = openFlags;
 1.28930 +  memset(&dummyVfs, 0, sizeof(dummyVfs));
 1.28931 +  dummyVfs.pAppData = (void*)&autolockIoFinder;
 1.28932 +  dummyVfs.zName = "dummy";
 1.28933 +  pUnused->fd = fd;
 1.28934 +  pUnused->flags = openFlags;
 1.28935 +  pNew->pUnused = pUnused;
 1.28936 +  
 1.28937 +  rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
 1.28938 +  if( rc==SQLITE_OK ){
 1.28939 +    *ppFile = pNew;
 1.28940 +    return SQLITE_OK;
 1.28941 +  }
 1.28942 +end_create_proxy:    
 1.28943 +  robust_close(pNew, fd, __LINE__);
 1.28944 +  sqlite3_free(pNew);
 1.28945 +  sqlite3_free(pUnused);
 1.28946 +  return rc;
 1.28947 +}
 1.28948 +
 1.28949 +#ifdef SQLITE_TEST
 1.28950 +/* simulate multiple hosts by creating unique hostid file paths */
 1.28951 +SQLITE_API int sqlite3_hostid_num = 0;
 1.28952 +#endif
 1.28953 +
 1.28954 +#define PROXY_HOSTIDLEN    16  /* conch file host id length */
 1.28955 +
 1.28956 +/* Not always defined in the headers as it ought to be */
 1.28957 +extern int gethostuuid(uuid_t id, const struct timespec *wait);
 1.28958 +
 1.28959 +/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
 1.28960 +** bytes of writable memory.
 1.28961 +*/
 1.28962 +static int proxyGetHostID(unsigned char *pHostID, int *pError){
 1.28963 +  assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
 1.28964 +  memset(pHostID, 0, PROXY_HOSTIDLEN);
 1.28965 +#if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
 1.28966 +               && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
 1.28967 +  {
 1.28968 +    static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
 1.28969 +    if( gethostuuid(pHostID, &timeout) ){
 1.28970 +      int err = errno;
 1.28971 +      if( pError ){
 1.28972 +        *pError = err;
 1.28973 +      }
 1.28974 +      return SQLITE_IOERR;
 1.28975 +    }
 1.28976 +  }
 1.28977 +#else
 1.28978 +  UNUSED_PARAMETER(pError);
 1.28979 +#endif
 1.28980 +#ifdef SQLITE_TEST
 1.28981 +  /* simulate multiple hosts by creating unique hostid file paths */
 1.28982 +  if( sqlite3_hostid_num != 0){
 1.28983 +    pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
 1.28984 +  }
 1.28985 +#endif
 1.28986 +  
 1.28987 +  return SQLITE_OK;
 1.28988 +}
 1.28989 +
 1.28990 +/* The conch file contains the header, host id and lock file path
 1.28991 + */
 1.28992 +#define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
 1.28993 +#define PROXY_HEADERLEN    1   /* conch file header length */
 1.28994 +#define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
 1.28995 +#define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
 1.28996 +
 1.28997 +/* 
 1.28998 +** Takes an open conch file, copies the contents to a new path and then moves 
 1.28999 +** it back.  The newly created file's file descriptor is assigned to the
 1.29000 +** conch file structure and finally the original conch file descriptor is 
 1.29001 +** closed.  Returns zero if successful.
 1.29002 +*/
 1.29003 +static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
 1.29004 +  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 1.29005 +  unixFile *conchFile = pCtx->conchFile;
 1.29006 +  char tPath[MAXPATHLEN];
 1.29007 +  char buf[PROXY_MAXCONCHLEN];
 1.29008 +  char *cPath = pCtx->conchFilePath;
 1.29009 +  size_t readLen = 0;
 1.29010 +  size_t pathLen = 0;
 1.29011 +  char errmsg[64] = "";
 1.29012 +  int fd = -1;
 1.29013 +  int rc = -1;
 1.29014 +  UNUSED_PARAMETER(myHostID);
 1.29015 +
 1.29016 +  /* create a new path by replace the trailing '-conch' with '-break' */
 1.29017 +  pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
 1.29018 +  if( pathLen>MAXPATHLEN || pathLen<6 || 
 1.29019 +     (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
 1.29020 +    sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
 1.29021 +    goto end_breaklock;
 1.29022 +  }
 1.29023 +  /* read the conch content */
 1.29024 +  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
 1.29025 +  if( readLen<PROXY_PATHINDEX ){
 1.29026 +    sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
 1.29027 +    goto end_breaklock;
 1.29028 +  }
 1.29029 +  /* write it out to the temporary break file */
 1.29030 +  fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
 1.29031 +  if( fd<0 ){
 1.29032 +    sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
 1.29033 +    goto end_breaklock;
 1.29034 +  }
 1.29035 +  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
 1.29036 +    sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
 1.29037 +    goto end_breaklock;
 1.29038 +  }
 1.29039 +  if( rename(tPath, cPath) ){
 1.29040 +    sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
 1.29041 +    goto end_breaklock;
 1.29042 +  }
 1.29043 +  rc = 0;
 1.29044 +  fprintf(stderr, "broke stale lock on %s\n", cPath);
 1.29045 +  robust_close(pFile, conchFile->h, __LINE__);
 1.29046 +  conchFile->h = fd;
 1.29047 +  conchFile->openFlags = O_RDWR | O_CREAT;
 1.29048 +
 1.29049 +end_breaklock:
 1.29050 +  if( rc ){
 1.29051 +    if( fd>=0 ){
 1.29052 +      osUnlink(tPath);
 1.29053 +      robust_close(pFile, fd, __LINE__);
 1.29054 +    }
 1.29055 +    fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
 1.29056 +  }
 1.29057 +  return rc;
 1.29058 +}
 1.29059 +
 1.29060 +/* Take the requested lock on the conch file and break a stale lock if the 
 1.29061 +** host id matches.
 1.29062 +*/
 1.29063 +static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
 1.29064 +  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 1.29065 +  unixFile *conchFile = pCtx->conchFile;
 1.29066 +  int rc = SQLITE_OK;
 1.29067 +  int nTries = 0;
 1.29068 +  struct timespec conchModTime;
 1.29069 +  
 1.29070 +  memset(&conchModTime, 0, sizeof(conchModTime));
 1.29071 +  do {
 1.29072 +    rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
 1.29073 +    nTries ++;
 1.29074 +    if( rc==SQLITE_BUSY ){
 1.29075 +      /* If the lock failed (busy):
 1.29076 +       * 1st try: get the mod time of the conch, wait 0.5s and try again. 
 1.29077 +       * 2nd try: fail if the mod time changed or host id is different, wait 
 1.29078 +       *           10 sec and try again
 1.29079 +       * 3rd try: break the lock unless the mod time has changed.
 1.29080 +       */
 1.29081 +      struct stat buf;
 1.29082 +      if( osFstat(conchFile->h, &buf) ){
 1.29083 +        pFile->lastErrno = errno;
 1.29084 +        return SQLITE_IOERR_LOCK;
 1.29085 +      }
 1.29086 +      
 1.29087 +      if( nTries==1 ){
 1.29088 +        conchModTime = buf.st_mtimespec;
 1.29089 +        usleep(500000); /* wait 0.5 sec and try the lock again*/
 1.29090 +        continue;  
 1.29091 +      }
 1.29092 +
 1.29093 +      assert( nTries>1 );
 1.29094 +      if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
 1.29095 +         conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
 1.29096 +        return SQLITE_BUSY;
 1.29097 +      }
 1.29098 +      
 1.29099 +      if( nTries==2 ){  
 1.29100 +        char tBuf[PROXY_MAXCONCHLEN];
 1.29101 +        int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
 1.29102 +        if( len<0 ){
 1.29103 +          pFile->lastErrno = errno;
 1.29104 +          return SQLITE_IOERR_LOCK;
 1.29105 +        }
 1.29106 +        if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
 1.29107 +          /* don't break the lock if the host id doesn't match */
 1.29108 +          if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
 1.29109 +            return SQLITE_BUSY;
 1.29110 +          }
 1.29111 +        }else{
 1.29112 +          /* don't break the lock on short read or a version mismatch */
 1.29113 +          return SQLITE_BUSY;
 1.29114 +        }
 1.29115 +        usleep(10000000); /* wait 10 sec and try the lock again */
 1.29116 +        continue; 
 1.29117 +      }
 1.29118 +      
 1.29119 +      assert( nTries==3 );
 1.29120 +      if( 0==proxyBreakConchLock(pFile, myHostID) ){
 1.29121 +        rc = SQLITE_OK;
 1.29122 +        if( lockType==EXCLUSIVE_LOCK ){
 1.29123 +          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);          
 1.29124 +        }
 1.29125 +        if( !rc ){
 1.29126 +          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
 1.29127 +        }
 1.29128 +      }
 1.29129 +    }
 1.29130 +  } while( rc==SQLITE_BUSY && nTries<3 );
 1.29131 +  
 1.29132 +  return rc;
 1.29133 +}
 1.29134 +
 1.29135 +/* Takes the conch by taking a shared lock and read the contents conch, if 
 1.29136 +** lockPath is non-NULL, the host ID and lock file path must match.  A NULL 
 1.29137 +** lockPath means that the lockPath in the conch file will be used if the 
 1.29138 +** host IDs match, or a new lock path will be generated automatically 
 1.29139 +** and written to the conch file.
 1.29140 +*/
 1.29141 +static int proxyTakeConch(unixFile *pFile){
 1.29142 +  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 1.29143 +  
 1.29144 +  if( pCtx->conchHeld!=0 ){
 1.29145 +    return SQLITE_OK;
 1.29146 +  }else{
 1.29147 +    unixFile *conchFile = pCtx->conchFile;
 1.29148 +    uuid_t myHostID;
 1.29149 +    int pError = 0;
 1.29150 +    char readBuf[PROXY_MAXCONCHLEN];
 1.29151 +    char lockPath[MAXPATHLEN];
 1.29152 +    char *tempLockPath = NULL;
 1.29153 +    int rc = SQLITE_OK;
 1.29154 +    int createConch = 0;
 1.29155 +    int hostIdMatch = 0;
 1.29156 +    int readLen = 0;
 1.29157 +    int tryOldLockPath = 0;
 1.29158 +    int forceNewLockPath = 0;
 1.29159 +    
 1.29160 +    OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
 1.29161 +             (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
 1.29162 +
 1.29163 +    rc = proxyGetHostID(myHostID, &pError);
 1.29164 +    if( (rc&0xff)==SQLITE_IOERR ){
 1.29165 +      pFile->lastErrno = pError;
 1.29166 +      goto end_takeconch;
 1.29167 +    }
 1.29168 +    rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
 1.29169 +    if( rc!=SQLITE_OK ){
 1.29170 +      goto end_takeconch;
 1.29171 +    }
 1.29172 +    /* read the existing conch file */
 1.29173 +    readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
 1.29174 +    if( readLen<0 ){
 1.29175 +      /* I/O error: lastErrno set by seekAndRead */
 1.29176 +      pFile->lastErrno = conchFile->lastErrno;
 1.29177 +      rc = SQLITE_IOERR_READ;
 1.29178 +      goto end_takeconch;
 1.29179 +    }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || 
 1.29180 +             readBuf[0]!=(char)PROXY_CONCHVERSION ){
 1.29181 +      /* a short read or version format mismatch means we need to create a new 
 1.29182 +      ** conch file. 
 1.29183 +      */
 1.29184 +      createConch = 1;
 1.29185 +    }
 1.29186 +    /* if the host id matches and the lock path already exists in the conch
 1.29187 +    ** we'll try to use the path there, if we can't open that path, we'll 
 1.29188 +    ** retry with a new auto-generated path 
 1.29189 +    */
 1.29190 +    do { /* in case we need to try again for an :auto: named lock file */
 1.29191 +
 1.29192 +      if( !createConch && !forceNewLockPath ){
 1.29193 +        hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, 
 1.29194 +                                  PROXY_HOSTIDLEN);
 1.29195 +        /* if the conch has data compare the contents */
 1.29196 +        if( !pCtx->lockProxyPath ){
 1.29197 +          /* for auto-named local lock file, just check the host ID and we'll
 1.29198 +           ** use the local lock file path that's already in there
 1.29199 +           */
 1.29200 +          if( hostIdMatch ){
 1.29201 +            size_t pathLen = (readLen - PROXY_PATHINDEX);
 1.29202 +            
 1.29203 +            if( pathLen>=MAXPATHLEN ){
 1.29204 +              pathLen=MAXPATHLEN-1;
 1.29205 +            }
 1.29206 +            memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
 1.29207 +            lockPath[pathLen] = 0;
 1.29208 +            tempLockPath = lockPath;
 1.29209 +            tryOldLockPath = 1;
 1.29210 +            /* create a copy of the lock path if the conch is taken */
 1.29211 +            goto end_takeconch;
 1.29212 +          }
 1.29213 +        }else if( hostIdMatch
 1.29214 +               && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
 1.29215 +                           readLen-PROXY_PATHINDEX)
 1.29216 +        ){
 1.29217 +          /* conch host and lock path match */
 1.29218 +          goto end_takeconch; 
 1.29219 +        }
 1.29220 +      }
 1.29221 +      
 1.29222 +      /* if the conch isn't writable and doesn't match, we can't take it */
 1.29223 +      if( (conchFile->openFlags&O_RDWR) == 0 ){
 1.29224 +        rc = SQLITE_BUSY;
 1.29225 +        goto end_takeconch;
 1.29226 +      }
 1.29227 +      
 1.29228 +      /* either the conch didn't match or we need to create a new one */
 1.29229 +      if( !pCtx->lockProxyPath ){
 1.29230 +        proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
 1.29231 +        tempLockPath = lockPath;
 1.29232 +        /* create a copy of the lock path _only_ if the conch is taken */
 1.29233 +      }
 1.29234 +      
 1.29235 +      /* update conch with host and path (this will fail if other process
 1.29236 +      ** has a shared lock already), if the host id matches, use the big
 1.29237 +      ** stick.
 1.29238 +      */
 1.29239 +      futimes(conchFile->h, NULL);
 1.29240 +      if( hostIdMatch && !createConch ){
 1.29241 +        if( conchFile->pInode && conchFile->pInode->nShared>1 ){
 1.29242 +          /* We are trying for an exclusive lock but another thread in this
 1.29243 +           ** same process is still holding a shared lock. */
 1.29244 +          rc = SQLITE_BUSY;
 1.29245 +        } else {          
 1.29246 +          rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
 1.29247 +        }
 1.29248 +      }else{
 1.29249 +        rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
 1.29250 +      }
 1.29251 +      if( rc==SQLITE_OK ){
 1.29252 +        char writeBuffer[PROXY_MAXCONCHLEN];
 1.29253 +        int writeSize = 0;
 1.29254 +        
 1.29255 +        writeBuffer[0] = (char)PROXY_CONCHVERSION;
 1.29256 +        memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
 1.29257 +        if( pCtx->lockProxyPath!=NULL ){
 1.29258 +          strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
 1.29259 +        }else{
 1.29260 +          strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
 1.29261 +        }
 1.29262 +        writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
 1.29263 +        robust_ftruncate(conchFile->h, writeSize);
 1.29264 +        rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
 1.29265 +        fsync(conchFile->h);
 1.29266 +        /* If we created a new conch file (not just updated the contents of a 
 1.29267 +         ** valid conch file), try to match the permissions of the database 
 1.29268 +         */
 1.29269 +        if( rc==SQLITE_OK && createConch ){
 1.29270 +          struct stat buf;
 1.29271 +          int err = osFstat(pFile->h, &buf);
 1.29272 +          if( err==0 ){
 1.29273 +            mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
 1.29274 +                                        S_IROTH|S_IWOTH);
 1.29275 +            /* try to match the database file R/W permissions, ignore failure */
 1.29276 +#ifndef SQLITE_PROXY_DEBUG
 1.29277 +            osFchmod(conchFile->h, cmode);
 1.29278 +#else
 1.29279 +            do{
 1.29280 +              rc = osFchmod(conchFile->h, cmode);
 1.29281 +            }while( rc==(-1) && errno==EINTR );
 1.29282 +            if( rc!=0 ){
 1.29283 +              int code = errno;
 1.29284 +              fprintf(stderr, "fchmod %o FAILED with %d %s\n",
 1.29285 +                      cmode, code, strerror(code));
 1.29286 +            } else {
 1.29287 +              fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
 1.29288 +            }
 1.29289 +          }else{
 1.29290 +            int code = errno;
 1.29291 +            fprintf(stderr, "STAT FAILED[%d] with %d %s\n", 
 1.29292 +                    err, code, strerror(code));
 1.29293 +#endif
 1.29294 +          }
 1.29295 +        }
 1.29296 +      }
 1.29297 +      conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
 1.29298 +      
 1.29299 +    end_takeconch:
 1.29300 +      OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
 1.29301 +      if( rc==SQLITE_OK && pFile->openFlags ){
 1.29302 +        int fd;
 1.29303 +        if( pFile->h>=0 ){
 1.29304 +          robust_close(pFile, pFile->h, __LINE__);
 1.29305 +        }
 1.29306 +        pFile->h = -1;
 1.29307 +        fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
 1.29308 +        OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
 1.29309 +        if( fd>=0 ){
 1.29310 +          pFile->h = fd;
 1.29311 +        }else{
 1.29312 +          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
 1.29313 +           during locking */
 1.29314 +        }
 1.29315 +      }
 1.29316 +      if( rc==SQLITE_OK && !pCtx->lockProxy ){
 1.29317 +        char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
 1.29318 +        rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
 1.29319 +        if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
 1.29320 +          /* we couldn't create the proxy lock file with the old lock file path
 1.29321 +           ** so try again via auto-naming 
 1.29322 +           */
 1.29323 +          forceNewLockPath = 1;
 1.29324 +          tryOldLockPath = 0;
 1.29325 +          continue; /* go back to the do {} while start point, try again */
 1.29326 +        }
 1.29327 +      }
 1.29328 +      if( rc==SQLITE_OK ){
 1.29329 +        /* Need to make a copy of path if we extracted the value
 1.29330 +         ** from the conch file or the path was allocated on the stack
 1.29331 +         */
 1.29332 +        if( tempLockPath ){
 1.29333 +          pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
 1.29334 +          if( !pCtx->lockProxyPath ){
 1.29335 +            rc = SQLITE_NOMEM;
 1.29336 +          }
 1.29337 +        }
 1.29338 +      }
 1.29339 +      if( rc==SQLITE_OK ){
 1.29340 +        pCtx->conchHeld = 1;
 1.29341 +        
 1.29342 +        if( pCtx->lockProxy->pMethod == &afpIoMethods ){
 1.29343 +          afpLockingContext *afpCtx;
 1.29344 +          afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
 1.29345 +          afpCtx->dbPath = pCtx->lockProxyPath;
 1.29346 +        }
 1.29347 +      } else {
 1.29348 +        conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
 1.29349 +      }
 1.29350 +      OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
 1.29351 +               rc==SQLITE_OK?"ok":"failed"));
 1.29352 +      return rc;
 1.29353 +    } while (1); /* in case we need to retry the :auto: lock file - 
 1.29354 +                 ** we should never get here except via the 'continue' call. */
 1.29355 +  }
 1.29356 +}
 1.29357 +
 1.29358 +/*
 1.29359 +** If pFile holds a lock on a conch file, then release that lock.
 1.29360 +*/
 1.29361 +static int proxyReleaseConch(unixFile *pFile){
 1.29362 +  int rc = SQLITE_OK;         /* Subroutine return code */
 1.29363 +  proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
 1.29364 +  unixFile *conchFile;        /* Name of the conch file */
 1.29365 +
 1.29366 +  pCtx = (proxyLockingContext *)pFile->lockingContext;
 1.29367 +  conchFile = pCtx->conchFile;
 1.29368 +  OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
 1.29369 +           (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
 1.29370 +           getpid()));
 1.29371 +  if( pCtx->conchHeld>0 ){
 1.29372 +    rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
 1.29373 +  }
 1.29374 +  pCtx->conchHeld = 0;
 1.29375 +  OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
 1.29376 +           (rc==SQLITE_OK ? "ok" : "failed")));
 1.29377 +  return rc;
 1.29378 +}
 1.29379 +
 1.29380 +/*
 1.29381 +** Given the name of a database file, compute the name of its conch file.
 1.29382 +** Store the conch filename in memory obtained from sqlite3_malloc().
 1.29383 +** Make *pConchPath point to the new name.  Return SQLITE_OK on success
 1.29384 +** or SQLITE_NOMEM if unable to obtain memory.
 1.29385 +**
 1.29386 +** The caller is responsible for ensuring that the allocated memory
 1.29387 +** space is eventually freed.
 1.29388 +**
 1.29389 +** *pConchPath is set to NULL if a memory allocation error occurs.
 1.29390 +*/
 1.29391 +static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
 1.29392 +  int i;                        /* Loop counter */
 1.29393 +  int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
 1.29394 +  char *conchPath;              /* buffer in which to construct conch name */
 1.29395 +
 1.29396 +  /* Allocate space for the conch filename and initialize the name to
 1.29397 +  ** the name of the original database file. */  
 1.29398 +  *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
 1.29399 +  if( conchPath==0 ){
 1.29400 +    return SQLITE_NOMEM;
 1.29401 +  }
 1.29402 +  memcpy(conchPath, dbPath, len+1);
 1.29403 +  
 1.29404 +  /* now insert a "." before the last / character */
 1.29405 +  for( i=(len-1); i>=0; i-- ){
 1.29406 +    if( conchPath[i]=='/' ){
 1.29407 +      i++;
 1.29408 +      break;
 1.29409 +    }
 1.29410 +  }
 1.29411 +  conchPath[i]='.';
 1.29412 +  while ( i<len ){
 1.29413 +    conchPath[i+1]=dbPath[i];
 1.29414 +    i++;
 1.29415 +  }
 1.29416 +
 1.29417 +  /* append the "-conch" suffix to the file */
 1.29418 +  memcpy(&conchPath[i+1], "-conch", 7);
 1.29419 +  assert( (int)strlen(conchPath) == len+7 );
 1.29420 +
 1.29421 +  return SQLITE_OK;
 1.29422 +}
 1.29423 +
 1.29424 +
 1.29425 +/* Takes a fully configured proxy locking-style unix file and switches
 1.29426 +** the local lock file path 
 1.29427 +*/
 1.29428 +static int switchLockProxyPath(unixFile *pFile, const char *path) {
 1.29429 +  proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
 1.29430 +  char *oldPath = pCtx->lockProxyPath;
 1.29431 +  int rc = SQLITE_OK;
 1.29432 +
 1.29433 +  if( pFile->eFileLock!=NO_LOCK ){
 1.29434 +    return SQLITE_BUSY;
 1.29435 +  }  
 1.29436 +
 1.29437 +  /* nothing to do if the path is NULL, :auto: or matches the existing path */
 1.29438 +  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
 1.29439 +    (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
 1.29440 +    return SQLITE_OK;
 1.29441 +  }else{
 1.29442 +    unixFile *lockProxy = pCtx->lockProxy;
 1.29443 +    pCtx->lockProxy=NULL;
 1.29444 +    pCtx->conchHeld = 0;
 1.29445 +    if( lockProxy!=NULL ){
 1.29446 +      rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
 1.29447 +      if( rc ) return rc;
 1.29448 +      sqlite3_free(lockProxy);
 1.29449 +    }
 1.29450 +    sqlite3_free(oldPath);
 1.29451 +    pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
 1.29452 +  }
 1.29453 +  
 1.29454 +  return rc;
 1.29455 +}
 1.29456 +
 1.29457 +/*
 1.29458 +** pFile is a file that has been opened by a prior xOpen call.  dbPath
 1.29459 +** is a string buffer at least MAXPATHLEN+1 characters in size.
 1.29460 +**
 1.29461 +** This routine find the filename associated with pFile and writes it
 1.29462 +** int dbPath.
 1.29463 +*/
 1.29464 +static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
 1.29465 +#if defined(__APPLE__)
 1.29466 +  if( pFile->pMethod == &afpIoMethods ){
 1.29467 +    /* afp style keeps a reference to the db path in the filePath field 
 1.29468 +    ** of the struct */
 1.29469 +    assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
 1.29470 +    strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
 1.29471 +  } else
 1.29472 +#endif
 1.29473 +  if( pFile->pMethod == &dotlockIoMethods ){
 1.29474 +    /* dot lock style uses the locking context to store the dot lock
 1.29475 +    ** file path */
 1.29476 +    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
 1.29477 +    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
 1.29478 +  }else{
 1.29479 +    /* all other styles use the locking context to store the db file path */
 1.29480 +    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
 1.29481 +    strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
 1.29482 +  }
 1.29483 +  return SQLITE_OK;
 1.29484 +}
 1.29485 +
 1.29486 +/*
 1.29487 +** Takes an already filled in unix file and alters it so all file locking 
 1.29488 +** will be performed on the local proxy lock file.  The following fields
 1.29489 +** are preserved in the locking context so that they can be restored and 
 1.29490 +** the unix structure properly cleaned up at close time:
 1.29491 +**  ->lockingContext
 1.29492 +**  ->pMethod
 1.29493 +*/
 1.29494 +static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
 1.29495 +  proxyLockingContext *pCtx;
 1.29496 +  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
 1.29497 +  char *lockPath=NULL;
 1.29498 +  int rc = SQLITE_OK;
 1.29499 +  
 1.29500 +  if( pFile->eFileLock!=NO_LOCK ){
 1.29501 +    return SQLITE_BUSY;
 1.29502 +  }
 1.29503 +  proxyGetDbPathForUnixFile(pFile, dbPath);
 1.29504 +  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
 1.29505 +    lockPath=NULL;
 1.29506 +  }else{
 1.29507 +    lockPath=(char *)path;
 1.29508 +  }
 1.29509 +  
 1.29510 +  OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
 1.29511 +           (lockPath ? lockPath : ":auto:"), getpid()));
 1.29512 +
 1.29513 +  pCtx = sqlite3_malloc( sizeof(*pCtx) );
 1.29514 +  if( pCtx==0 ){
 1.29515 +    return SQLITE_NOMEM;
 1.29516 +  }
 1.29517 +  memset(pCtx, 0, sizeof(*pCtx));
 1.29518 +
 1.29519 +  rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
 1.29520 +  if( rc==SQLITE_OK ){
 1.29521 +    rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
 1.29522 +    if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
 1.29523 +      /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
 1.29524 +      ** (c) the file system is read-only, then enable no-locking access.
 1.29525 +      ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
 1.29526 +      ** that openFlags will have only one of O_RDONLY or O_RDWR.
 1.29527 +      */
 1.29528 +      struct statfs fsInfo;
 1.29529 +      struct stat conchInfo;
 1.29530 +      int goLockless = 0;
 1.29531 +
 1.29532 +      if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
 1.29533 +        int err = errno;
 1.29534 +        if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
 1.29535 +          goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
 1.29536 +        }
 1.29537 +      }
 1.29538 +      if( goLockless ){
 1.29539 +        pCtx->conchHeld = -1; /* read only FS/ lockless */
 1.29540 +        rc = SQLITE_OK;
 1.29541 +      }
 1.29542 +    }
 1.29543 +  }  
 1.29544 +  if( rc==SQLITE_OK && lockPath ){
 1.29545 +    pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
 1.29546 +  }
 1.29547 +
 1.29548 +  if( rc==SQLITE_OK ){
 1.29549 +    pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
 1.29550 +    if( pCtx->dbPath==NULL ){
 1.29551 +      rc = SQLITE_NOMEM;
 1.29552 +    }
 1.29553 +  }
 1.29554 +  if( rc==SQLITE_OK ){
 1.29555 +    /* all memory is allocated, proxys are created and assigned, 
 1.29556 +    ** switch the locking context and pMethod then return.
 1.29557 +    */
 1.29558 +    pCtx->oldLockingContext = pFile->lockingContext;
 1.29559 +    pFile->lockingContext = pCtx;
 1.29560 +    pCtx->pOldMethod = pFile->pMethod;
 1.29561 +    pFile->pMethod = &proxyIoMethods;
 1.29562 +  }else{
 1.29563 +    if( pCtx->conchFile ){ 
 1.29564 +      pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
 1.29565 +      sqlite3_free(pCtx->conchFile);
 1.29566 +    }
 1.29567 +    sqlite3DbFree(0, pCtx->lockProxyPath);
 1.29568 +    sqlite3_free(pCtx->conchFilePath); 
 1.29569 +    sqlite3_free(pCtx);
 1.29570 +  }
 1.29571 +  OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
 1.29572 +           (rc==SQLITE_OK ? "ok" : "failed")));
 1.29573 +  return rc;
 1.29574 +}
 1.29575 +
 1.29576 +
 1.29577 +/*
 1.29578 +** This routine handles sqlite3_file_control() calls that are specific
 1.29579 +** to proxy locking.
 1.29580 +*/
 1.29581 +static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
 1.29582 +  switch( op ){
 1.29583 +    case SQLITE_GET_LOCKPROXYFILE: {
 1.29584 +      unixFile *pFile = (unixFile*)id;
 1.29585 +      if( pFile->pMethod == &proxyIoMethods ){
 1.29586 +        proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
 1.29587 +        proxyTakeConch(pFile);
 1.29588 +        if( pCtx->lockProxyPath ){
 1.29589 +          *(const char **)pArg = pCtx->lockProxyPath;
 1.29590 +        }else{
 1.29591 +          *(const char **)pArg = ":auto: (not held)";
 1.29592 +        }
 1.29593 +      } else {
 1.29594 +        *(const char **)pArg = NULL;
 1.29595 +      }
 1.29596 +      return SQLITE_OK;
 1.29597 +    }
 1.29598 +    case SQLITE_SET_LOCKPROXYFILE: {
 1.29599 +      unixFile *pFile = (unixFile*)id;
 1.29600 +      int rc = SQLITE_OK;
 1.29601 +      int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
 1.29602 +      if( pArg==NULL || (const char *)pArg==0 ){
 1.29603 +        if( isProxyStyle ){
 1.29604 +          /* turn off proxy locking - not supported */
 1.29605 +          rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
 1.29606 +        }else{
 1.29607 +          /* turn off proxy locking - already off - NOOP */
 1.29608 +          rc = SQLITE_OK;
 1.29609 +        }
 1.29610 +      }else{
 1.29611 +        const char *proxyPath = (const char *)pArg;
 1.29612 +        if( isProxyStyle ){
 1.29613 +          proxyLockingContext *pCtx = 
 1.29614 +            (proxyLockingContext*)pFile->lockingContext;
 1.29615 +          if( !strcmp(pArg, ":auto:") 
 1.29616 +           || (pCtx->lockProxyPath &&
 1.29617 +               !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
 1.29618 +          ){
 1.29619 +            rc = SQLITE_OK;
 1.29620 +          }else{
 1.29621 +            rc = switchLockProxyPath(pFile, proxyPath);
 1.29622 +          }
 1.29623 +        }else{
 1.29624 +          /* turn on proxy file locking */
 1.29625 +          rc = proxyTransformUnixFile(pFile, proxyPath);
 1.29626 +        }
 1.29627 +      }
 1.29628 +      return rc;
 1.29629 +    }
 1.29630 +    default: {
 1.29631 +      assert( 0 );  /* The call assures that only valid opcodes are sent */
 1.29632 +    }
 1.29633 +  }
 1.29634 +  /*NOTREACHED*/
 1.29635 +  return SQLITE_ERROR;
 1.29636 +}
 1.29637 +
 1.29638 +/*
 1.29639 +** Within this division (the proxying locking implementation) the procedures
 1.29640 +** above this point are all utilities.  The lock-related methods of the
 1.29641 +** proxy-locking sqlite3_io_method object follow.
 1.29642 +*/
 1.29643 +
 1.29644 +
 1.29645 +/*
 1.29646 +** This routine checks if there is a RESERVED lock held on the specified
 1.29647 +** file by this or any other process. If such a lock is held, set *pResOut
 1.29648 +** to a non-zero value otherwise *pResOut is set to zero.  The return value
 1.29649 +** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 1.29650 +*/
 1.29651 +static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
 1.29652 +  unixFile *pFile = (unixFile*)id;
 1.29653 +  int rc = proxyTakeConch(pFile);
 1.29654 +  if( rc==SQLITE_OK ){
 1.29655 +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 1.29656 +    if( pCtx->conchHeld>0 ){
 1.29657 +      unixFile *proxy = pCtx->lockProxy;
 1.29658 +      return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
 1.29659 +    }else{ /* conchHeld < 0 is lockless */
 1.29660 +      pResOut=0;
 1.29661 +    }
 1.29662 +  }
 1.29663 +  return rc;
 1.29664 +}
 1.29665 +
 1.29666 +/*
 1.29667 +** Lock the file with the lock specified by parameter eFileLock - one
 1.29668 +** of the following:
 1.29669 +**
 1.29670 +**     (1) SHARED_LOCK
 1.29671 +**     (2) RESERVED_LOCK
 1.29672 +**     (3) PENDING_LOCK
 1.29673 +**     (4) EXCLUSIVE_LOCK
 1.29674 +**
 1.29675 +** Sometimes when requesting one lock state, additional lock states
 1.29676 +** are inserted in between.  The locking might fail on one of the later
 1.29677 +** transitions leaving the lock state different from what it started but
 1.29678 +** still short of its goal.  The following chart shows the allowed
 1.29679 +** transitions and the inserted intermediate states:
 1.29680 +**
 1.29681 +**    UNLOCKED -> SHARED
 1.29682 +**    SHARED -> RESERVED
 1.29683 +**    SHARED -> (PENDING) -> EXCLUSIVE
 1.29684 +**    RESERVED -> (PENDING) -> EXCLUSIVE
 1.29685 +**    PENDING -> EXCLUSIVE
 1.29686 +**
 1.29687 +** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 1.29688 +** routine to lower a locking level.
 1.29689 +*/
 1.29690 +static int proxyLock(sqlite3_file *id, int eFileLock) {
 1.29691 +  unixFile *pFile = (unixFile*)id;
 1.29692 +  int rc = proxyTakeConch(pFile);
 1.29693 +  if( rc==SQLITE_OK ){
 1.29694 +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 1.29695 +    if( pCtx->conchHeld>0 ){
 1.29696 +      unixFile *proxy = pCtx->lockProxy;
 1.29697 +      rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
 1.29698 +      pFile->eFileLock = proxy->eFileLock;
 1.29699 +    }else{
 1.29700 +      /* conchHeld < 0 is lockless */
 1.29701 +    }
 1.29702 +  }
 1.29703 +  return rc;
 1.29704 +}
 1.29705 +
 1.29706 +
 1.29707 +/*
 1.29708 +** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 1.29709 +** must be either NO_LOCK or SHARED_LOCK.
 1.29710 +**
 1.29711 +** If the locking level of the file descriptor is already at or below
 1.29712 +** the requested locking level, this routine is a no-op.
 1.29713 +*/
 1.29714 +static int proxyUnlock(sqlite3_file *id, int eFileLock) {
 1.29715 +  unixFile *pFile = (unixFile*)id;
 1.29716 +  int rc = proxyTakeConch(pFile);
 1.29717 +  if( rc==SQLITE_OK ){
 1.29718 +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 1.29719 +    if( pCtx->conchHeld>0 ){
 1.29720 +      unixFile *proxy = pCtx->lockProxy;
 1.29721 +      rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
 1.29722 +      pFile->eFileLock = proxy->eFileLock;
 1.29723 +    }else{
 1.29724 +      /* conchHeld < 0 is lockless */
 1.29725 +    }
 1.29726 +  }
 1.29727 +  return rc;
 1.29728 +}
 1.29729 +
 1.29730 +/*
 1.29731 +** Close a file that uses proxy locks.
 1.29732 +*/
 1.29733 +static int proxyClose(sqlite3_file *id) {
 1.29734 +  if( id ){
 1.29735 +    unixFile *pFile = (unixFile*)id;
 1.29736 +    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 1.29737 +    unixFile *lockProxy = pCtx->lockProxy;
 1.29738 +    unixFile *conchFile = pCtx->conchFile;
 1.29739 +    int rc = SQLITE_OK;
 1.29740 +    
 1.29741 +    if( lockProxy ){
 1.29742 +      rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
 1.29743 +      if( rc ) return rc;
 1.29744 +      rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
 1.29745 +      if( rc ) return rc;
 1.29746 +      sqlite3_free(lockProxy);
 1.29747 +      pCtx->lockProxy = 0;
 1.29748 +    }
 1.29749 +    if( conchFile ){
 1.29750 +      if( pCtx->conchHeld ){
 1.29751 +        rc = proxyReleaseConch(pFile);
 1.29752 +        if( rc ) return rc;
 1.29753 +      }
 1.29754 +      rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
 1.29755 +      if( rc ) return rc;
 1.29756 +      sqlite3_free(conchFile);
 1.29757 +    }
 1.29758 +    sqlite3DbFree(0, pCtx->lockProxyPath);
 1.29759 +    sqlite3_free(pCtx->conchFilePath);
 1.29760 +    sqlite3DbFree(0, pCtx->dbPath);
 1.29761 +    /* restore the original locking context and pMethod then close it */
 1.29762 +    pFile->lockingContext = pCtx->oldLockingContext;
 1.29763 +    pFile->pMethod = pCtx->pOldMethod;
 1.29764 +    sqlite3_free(pCtx);
 1.29765 +    return pFile->pMethod->xClose(id);
 1.29766 +  }
 1.29767 +  return SQLITE_OK;
 1.29768 +}
 1.29769 +
 1.29770 +
 1.29771 +
 1.29772 +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 1.29773 +/*
 1.29774 +** The proxy locking style is intended for use with AFP filesystems.
 1.29775 +** And since AFP is only supported on MacOSX, the proxy locking is also
 1.29776 +** restricted to MacOSX.
 1.29777 +** 
 1.29778 +**
 1.29779 +******************* End of the proxy lock implementation **********************
 1.29780 +******************************************************************************/
 1.29781 +
 1.29782 +/*
 1.29783 +** Initialize the operating system interface.
 1.29784 +**
 1.29785 +** This routine registers all VFS implementations for unix-like operating
 1.29786 +** systems.  This routine, and the sqlite3_os_end() routine that follows,
 1.29787 +** should be the only routines in this file that are visible from other
 1.29788 +** files.
 1.29789 +**
 1.29790 +** This routine is called once during SQLite initialization and by a
 1.29791 +** single thread.  The memory allocation and mutex subsystems have not
 1.29792 +** necessarily been initialized when this routine is called, and so they
 1.29793 +** should not be used.
 1.29794 +*/
 1.29795 +SQLITE_API int sqlite3_os_init(void){ 
 1.29796 +  /* 
 1.29797 +  ** The following macro defines an initializer for an sqlite3_vfs object.
 1.29798 +  ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
 1.29799 +  ** to the "finder" function.  (pAppData is a pointer to a pointer because
 1.29800 +  ** silly C90 rules prohibit a void* from being cast to a function pointer
 1.29801 +  ** and so we have to go through the intermediate pointer to avoid problems
 1.29802 +  ** when compiling with -pedantic-errors on GCC.)
 1.29803 +  **
 1.29804 +  ** The FINDER parameter to this macro is the name of the pointer to the
 1.29805 +  ** finder-function.  The finder-function returns a pointer to the
 1.29806 +  ** sqlite_io_methods object that implements the desired locking
 1.29807 +  ** behaviors.  See the division above that contains the IOMETHODS
 1.29808 +  ** macro for addition information on finder-functions.
 1.29809 +  **
 1.29810 +  ** Most finders simply return a pointer to a fixed sqlite3_io_methods
 1.29811 +  ** object.  But the "autolockIoFinder" available on MacOSX does a little
 1.29812 +  ** more than that; it looks at the filesystem type that hosts the 
 1.29813 +  ** database file and tries to choose an locking method appropriate for
 1.29814 +  ** that filesystem time.
 1.29815 +  */
 1.29816 +  #define UNIXVFS(VFSNAME, FINDER) {                        \
 1.29817 +    3,                    /* iVersion */                    \
 1.29818 +    sizeof(unixFile),     /* szOsFile */                    \
 1.29819 +    MAX_PATHNAME,         /* mxPathname */                  \
 1.29820 +    0,                    /* pNext */                       \
 1.29821 +    VFSNAME,              /* zName */                       \
 1.29822 +    (void*)&FINDER,       /* pAppData */                    \
 1.29823 +    unixOpen,             /* xOpen */                       \
 1.29824 +    unixDelete,           /* xDelete */                     \
 1.29825 +    unixAccess,           /* xAccess */                     \
 1.29826 +    unixFullPathname,     /* xFullPathname */               \
 1.29827 +    unixDlOpen,           /* xDlOpen */                     \
 1.29828 +    unixDlError,          /* xDlError */                    \
 1.29829 +    unixDlSym,            /* xDlSym */                      \
 1.29830 +    unixDlClose,          /* xDlClose */                    \
 1.29831 +    unixRandomness,       /* xRandomness */                 \
 1.29832 +    unixSleep,            /* xSleep */                      \
 1.29833 +    unixCurrentTime,      /* xCurrentTime */                \
 1.29834 +    unixGetLastError,     /* xGetLastError */               \
 1.29835 +    unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
 1.29836 +    unixSetSystemCall,    /* xSetSystemCall */              \
 1.29837 +    unixGetSystemCall,    /* xGetSystemCall */              \
 1.29838 +    unixNextSystemCall,   /* xNextSystemCall */             \
 1.29839 +  }
 1.29840 +
 1.29841 +  /*
 1.29842 +  ** All default VFSes for unix are contained in the following array.
 1.29843 +  **
 1.29844 +  ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
 1.29845 +  ** by the SQLite core when the VFS is registered.  So the following
 1.29846 +  ** array cannot be const.
 1.29847 +  */
 1.29848 +  static sqlite3_vfs aVfs[] = {
 1.29849 +#if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
 1.29850 +    UNIXVFS("unix",          autolockIoFinder ),
 1.29851 +#else
 1.29852 +    UNIXVFS("unix",          posixIoFinder ),
 1.29853 +#endif
 1.29854 +    UNIXVFS("unix-none",     nolockIoFinder ),
 1.29855 +    UNIXVFS("unix-dotfile",  dotlockIoFinder ),
 1.29856 +    UNIXVFS("unix-excl",     posixIoFinder ),
 1.29857 +#if OS_VXWORKS
 1.29858 +    UNIXVFS("unix-namedsem", semIoFinder ),
 1.29859 +#endif
 1.29860 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.29861 +    UNIXVFS("unix-posix",    posixIoFinder ),
 1.29862 +#if !OS_VXWORKS
 1.29863 +    UNIXVFS("unix-flock",    flockIoFinder ),
 1.29864 +#endif
 1.29865 +#endif
 1.29866 +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 1.29867 +    UNIXVFS("unix-afp",      afpIoFinder ),
 1.29868 +    UNIXVFS("unix-nfs",      nfsIoFinder ),
 1.29869 +    UNIXVFS("unix-proxy",    proxyIoFinder ),
 1.29870 +#endif
 1.29871 +  };
 1.29872 +  unsigned int i;          /* Loop counter */
 1.29873 +
 1.29874 +  /* Double-check that the aSyscall[] array has been constructed
 1.29875 +  ** correctly.  See ticket [bb3a86e890c8e96ab] */
 1.29876 +  assert( ArraySize(aSyscall)==22 );
 1.29877 +
 1.29878 +  /* Register all VFSes defined in the aVfs[] array */
 1.29879 +  for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 1.29880 +    sqlite3_vfs_register(&aVfs[i], i==0);
 1.29881 +  }
 1.29882 +  return SQLITE_OK; 
 1.29883 +}
 1.29884 +
 1.29885 +/*
 1.29886 +** Shutdown the operating system interface.
 1.29887 +**
 1.29888 +** Some operating systems might need to do some cleanup in this routine,
 1.29889 +** to release dynamically allocated objects.  But not on unix.
 1.29890 +** This routine is a no-op for unix.
 1.29891 +*/
 1.29892 +SQLITE_API int sqlite3_os_end(void){ 
 1.29893 +  return SQLITE_OK; 
 1.29894 +}
 1.29895 + 
 1.29896 +#endif /* SQLITE_OS_UNIX */
 1.29897 +
 1.29898 +/************** End of os_unix.c *********************************************/
 1.29899 +/************** Begin file os_win.c ******************************************/
 1.29900 +/*
 1.29901 +** 2004 May 22
 1.29902 +**
 1.29903 +** The author disclaims copyright to this source code.  In place of
 1.29904 +** a legal notice, here is a blessing:
 1.29905 +**
 1.29906 +**    May you do good and not evil.
 1.29907 +**    May you find forgiveness for yourself and forgive others.
 1.29908 +**    May you share freely, never taking more than you give.
 1.29909 +**
 1.29910 +******************************************************************************
 1.29911 +**
 1.29912 +** This file contains code that is specific to Windows.
 1.29913 +*/
 1.29914 +#if SQLITE_OS_WIN               /* This file is used for Windows only */
 1.29915 +
 1.29916 +#ifdef __CYGWIN__
 1.29917 +# include <sys/cygwin.h>
 1.29918 +#endif
 1.29919 +
 1.29920 +/*
 1.29921 +** Include code that is common to all os_*.c files
 1.29922 +*/
 1.29923 +/************** Include os_common.h in the middle of os_win.c ****************/
 1.29924 +/************** Begin file os_common.h ***************************************/
 1.29925 +/*
 1.29926 +** 2004 May 22
 1.29927 +**
 1.29928 +** The author disclaims copyright to this source code.  In place of
 1.29929 +** a legal notice, here is a blessing:
 1.29930 +**
 1.29931 +**    May you do good and not evil.
 1.29932 +**    May you find forgiveness for yourself and forgive others.
 1.29933 +**    May you share freely, never taking more than you give.
 1.29934 +**
 1.29935 +******************************************************************************
 1.29936 +**
 1.29937 +** This file contains macros and a little bit of code that is common to
 1.29938 +** all of the platform-specific files (os_*.c) and is #included into those
 1.29939 +** files.
 1.29940 +**
 1.29941 +** This file should be #included by the os_*.c files only.  It is not a
 1.29942 +** general purpose header file.
 1.29943 +*/
 1.29944 +#ifndef _OS_COMMON_H_
 1.29945 +#define _OS_COMMON_H_
 1.29946 +
 1.29947 +/*
 1.29948 +** At least two bugs have slipped in because we changed the MEMORY_DEBUG
 1.29949 +** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 1.29950 +** switch.  The following code should catch this problem at compile-time.
 1.29951 +*/
 1.29952 +#ifdef MEMORY_DEBUG
 1.29953 +# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 1.29954 +#endif
 1.29955 +
 1.29956 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.29957 +# ifndef SQLITE_DEBUG_OS_TRACE
 1.29958 +#   define SQLITE_DEBUG_OS_TRACE 0
 1.29959 +# endif
 1.29960 +  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 1.29961 +# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 1.29962 +#else
 1.29963 +# define OSTRACE(X)
 1.29964 +#endif
 1.29965 +
 1.29966 +/*
 1.29967 +** Macros for performance tracing.  Normally turned off.  Only works
 1.29968 +** on i486 hardware.
 1.29969 +*/
 1.29970 +#ifdef SQLITE_PERFORMANCE_TRACE
 1.29971 +
 1.29972 +/* 
 1.29973 +** hwtime.h contains inline assembler code for implementing 
 1.29974 +** high-performance timing routines.
 1.29975 +*/
 1.29976 +/************** Include hwtime.h in the middle of os_common.h ****************/
 1.29977 +/************** Begin file hwtime.h ******************************************/
 1.29978 +/*
 1.29979 +** 2008 May 27
 1.29980 +**
 1.29981 +** The author disclaims copyright to this source code.  In place of
 1.29982 +** a legal notice, here is a blessing:
 1.29983 +**
 1.29984 +**    May you do good and not evil.
 1.29985 +**    May you find forgiveness for yourself and forgive others.
 1.29986 +**    May you share freely, never taking more than you give.
 1.29987 +**
 1.29988 +******************************************************************************
 1.29989 +**
 1.29990 +** This file contains inline asm code for retrieving "high-performance"
 1.29991 +** counters for x86 class CPUs.
 1.29992 +*/
 1.29993 +#ifndef _HWTIME_H_
 1.29994 +#define _HWTIME_H_
 1.29995 +
 1.29996 +/*
 1.29997 +** The following routine only works on pentium-class (or newer) processors.
 1.29998 +** It uses the RDTSC opcode to read the cycle count value out of the
 1.29999 +** processor and returns that value.  This can be used for high-res
 1.30000 +** profiling.
 1.30001 +*/
 1.30002 +#if (defined(__GNUC__) || defined(_MSC_VER)) && \
 1.30003 +      (defined(i386) || defined(__i386__) || defined(_M_IX86))
 1.30004 +
 1.30005 +  #if defined(__GNUC__)
 1.30006 +
 1.30007 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.30008 +     unsigned int lo, hi;
 1.30009 +     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 1.30010 +     return (sqlite_uint64)hi << 32 | lo;
 1.30011 +  }
 1.30012 +
 1.30013 +  #elif defined(_MSC_VER)
 1.30014 +
 1.30015 +  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 1.30016 +     __asm {
 1.30017 +        rdtsc
 1.30018 +        ret       ; return value at EDX:EAX
 1.30019 +     }
 1.30020 +  }
 1.30021 +
 1.30022 +  #endif
 1.30023 +
 1.30024 +#elif (defined(__GNUC__) && defined(__x86_64__))
 1.30025 +
 1.30026 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.30027 +      unsigned long val;
 1.30028 +      __asm__ __volatile__ ("rdtsc" : "=A" (val));
 1.30029 +      return val;
 1.30030 +  }
 1.30031 + 
 1.30032 +#elif (defined(__GNUC__) && defined(__ppc__))
 1.30033 +
 1.30034 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.30035 +      unsigned long long retval;
 1.30036 +      unsigned long junk;
 1.30037 +      __asm__ __volatile__ ("\n\
 1.30038 +          1:      mftbu   %1\n\
 1.30039 +                  mftb    %L0\n\
 1.30040 +                  mftbu   %0\n\
 1.30041 +                  cmpw    %0,%1\n\
 1.30042 +                  bne     1b"
 1.30043 +                  : "=r" (retval), "=r" (junk));
 1.30044 +      return retval;
 1.30045 +  }
 1.30046 +
 1.30047 +#else
 1.30048 +
 1.30049 +  #error Need implementation of sqlite3Hwtime() for your platform.
 1.30050 +
 1.30051 +  /*
 1.30052 +  ** To compile without implementing sqlite3Hwtime() for your platform,
 1.30053 +  ** you can remove the above #error and use the following
 1.30054 +  ** stub function.  You will lose timing support for many
 1.30055 +  ** of the debugging and testing utilities, but it should at
 1.30056 +  ** least compile and run.
 1.30057 +  */
 1.30058 +SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 1.30059 +
 1.30060 +#endif
 1.30061 +
 1.30062 +#endif /* !defined(_HWTIME_H_) */
 1.30063 +
 1.30064 +/************** End of hwtime.h **********************************************/
 1.30065 +/************** Continuing where we left off in os_common.h ******************/
 1.30066 +
 1.30067 +static sqlite_uint64 g_start;
 1.30068 +static sqlite_uint64 g_elapsed;
 1.30069 +#define TIMER_START       g_start=sqlite3Hwtime()
 1.30070 +#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
 1.30071 +#define TIMER_ELAPSED     g_elapsed
 1.30072 +#else
 1.30073 +#define TIMER_START
 1.30074 +#define TIMER_END
 1.30075 +#define TIMER_ELAPSED     ((sqlite_uint64)0)
 1.30076 +#endif
 1.30077 +
 1.30078 +/*
 1.30079 +** If we compile with the SQLITE_TEST macro set, then the following block
 1.30080 +** of code will give us the ability to simulate a disk I/O error.  This
 1.30081 +** is used for testing the I/O recovery logic.
 1.30082 +*/
 1.30083 +#ifdef SQLITE_TEST
 1.30084 +SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
 1.30085 +SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
 1.30086 +SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
 1.30087 +SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
 1.30088 +SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
 1.30089 +SQLITE_API int sqlite3_diskfull_pending = 0;
 1.30090 +SQLITE_API int sqlite3_diskfull = 0;
 1.30091 +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
 1.30092 +#define SimulateIOError(CODE)  \
 1.30093 +  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
 1.30094 +       || sqlite3_io_error_pending-- == 1 )  \
 1.30095 +              { local_ioerr(); CODE; }
 1.30096 +static void local_ioerr(){
 1.30097 +  IOTRACE(("IOERR\n"));
 1.30098 +  sqlite3_io_error_hit++;
 1.30099 +  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
 1.30100 +}
 1.30101 +#define SimulateDiskfullError(CODE) \
 1.30102 +   if( sqlite3_diskfull_pending ){ \
 1.30103 +     if( sqlite3_diskfull_pending == 1 ){ \
 1.30104 +       local_ioerr(); \
 1.30105 +       sqlite3_diskfull = 1; \
 1.30106 +       sqlite3_io_error_hit = 1; \
 1.30107 +       CODE; \
 1.30108 +     }else{ \
 1.30109 +       sqlite3_diskfull_pending--; \
 1.30110 +     } \
 1.30111 +   }
 1.30112 +#else
 1.30113 +#define SimulateIOErrorBenign(X)
 1.30114 +#define SimulateIOError(A)
 1.30115 +#define SimulateDiskfullError(A)
 1.30116 +#endif
 1.30117 +
 1.30118 +/*
 1.30119 +** When testing, keep a count of the number of open files.
 1.30120 +*/
 1.30121 +#ifdef SQLITE_TEST
 1.30122 +SQLITE_API int sqlite3_open_file_count = 0;
 1.30123 +#define OpenCounter(X)  sqlite3_open_file_count+=(X)
 1.30124 +#else
 1.30125 +#define OpenCounter(X)
 1.30126 +#endif
 1.30127 +
 1.30128 +#endif /* !defined(_OS_COMMON_H_) */
 1.30129 +
 1.30130 +/************** End of os_common.h *******************************************/
 1.30131 +/************** Continuing where we left off in os_win.c *********************/
 1.30132 +
 1.30133 +/*
 1.30134 +** Compiling and using WAL mode requires several APIs that are only
 1.30135 +** available in Windows platforms based on the NT kernel.
 1.30136 +*/
 1.30137 +#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
 1.30138 +# error "WAL mode requires support from the Windows NT kernel, compile\
 1.30139 + with SQLITE_OMIT_WAL."
 1.30140 +#endif
 1.30141 +
 1.30142 +/*
 1.30143 +** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
 1.30144 +** based on the sub-platform)?
 1.30145 +*/
 1.30146 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 1.30147 +#  define SQLITE_WIN32_HAS_ANSI
 1.30148 +#endif
 1.30149 +
 1.30150 +/*
 1.30151 +** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
 1.30152 +** based on the sub-platform)?
 1.30153 +*/
 1.30154 +#if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
 1.30155 +#  define SQLITE_WIN32_HAS_WIDE
 1.30156 +#endif
 1.30157 +
 1.30158 +/*
 1.30159 +** Do we need to manually define the Win32 file mapping APIs for use with WAL
 1.30160 +** mode (e.g. these APIs are available in the Windows CE SDK; however, they
 1.30161 +** are not present in the header file)?
 1.30162 +*/
 1.30163 +#if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
 1.30164 +/*
 1.30165 +** Two of the file mapping APIs are different under WinRT.  Figure out which
 1.30166 +** set we need.
 1.30167 +*/
 1.30168 +#if SQLITE_OS_WINRT
 1.30169 +WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
 1.30170 +        LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
 1.30171 +
 1.30172 +WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
 1.30173 +#else
 1.30174 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30175 +WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
 1.30176 +        DWORD, DWORD, DWORD, LPCSTR);
 1.30177 +#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
 1.30178 +
 1.30179 +#if defined(SQLITE_WIN32_HAS_WIDE)
 1.30180 +WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
 1.30181 +        DWORD, DWORD, DWORD, LPCWSTR);
 1.30182 +#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
 1.30183 +
 1.30184 +WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
 1.30185 +#endif /* SQLITE_OS_WINRT */
 1.30186 +
 1.30187 +/*
 1.30188 +** This file mapping API is common to both Win32 and WinRT.
 1.30189 +*/
 1.30190 +WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
 1.30191 +#endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
 1.30192 +
 1.30193 +/*
 1.30194 +** Macro to find the minimum of two numeric values.
 1.30195 +*/
 1.30196 +#ifndef MIN
 1.30197 +# define MIN(x,y) ((x)<(y)?(x):(y))
 1.30198 +#endif
 1.30199 +
 1.30200 +/*
 1.30201 +** Some Microsoft compilers lack this definition.
 1.30202 +*/
 1.30203 +#ifndef INVALID_FILE_ATTRIBUTES
 1.30204 +# define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
 1.30205 +#endif
 1.30206 +
 1.30207 +#ifndef FILE_FLAG_MASK
 1.30208 +# define FILE_FLAG_MASK          (0xFF3C0000)
 1.30209 +#endif
 1.30210 +
 1.30211 +#ifndef FILE_ATTRIBUTE_MASK
 1.30212 +# define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
 1.30213 +#endif
 1.30214 +
 1.30215 +#ifndef SQLITE_OMIT_WAL
 1.30216 +/* Forward references */
 1.30217 +typedef struct winShm winShm;           /* A connection to shared-memory */
 1.30218 +typedef struct winShmNode winShmNode;   /* A region of shared-memory */
 1.30219 +#endif
 1.30220 +
 1.30221 +/*
 1.30222 +** WinCE lacks native support for file locking so we have to fake it
 1.30223 +** with some code of our own.
 1.30224 +*/
 1.30225 +#if SQLITE_OS_WINCE
 1.30226 +typedef struct winceLock {
 1.30227 +  int nReaders;       /* Number of reader locks obtained */
 1.30228 +  BOOL bPending;      /* Indicates a pending lock has been obtained */
 1.30229 +  BOOL bReserved;     /* Indicates a reserved lock has been obtained */
 1.30230 +  BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
 1.30231 +} winceLock;
 1.30232 +#endif
 1.30233 +
 1.30234 +/*
 1.30235 +** The winFile structure is a subclass of sqlite3_file* specific to the win32
 1.30236 +** portability layer.
 1.30237 +*/
 1.30238 +typedef struct winFile winFile;
 1.30239 +struct winFile {
 1.30240 +  const sqlite3_io_methods *pMethod; /*** Must be first ***/
 1.30241 +  sqlite3_vfs *pVfs;      /* The VFS used to open this file */
 1.30242 +  HANDLE h;               /* Handle for accessing the file */
 1.30243 +  u8 locktype;            /* Type of lock currently held on this file */
 1.30244 +  short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
 1.30245 +  u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
 1.30246 +  DWORD lastErrno;        /* The Windows errno from the last I/O error */
 1.30247 +#ifndef SQLITE_OMIT_WAL
 1.30248 +  winShm *pShm;           /* Instance of shared memory on this file */
 1.30249 +#endif
 1.30250 +  const char *zPath;      /* Full pathname of this file */
 1.30251 +  int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
 1.30252 +#if SQLITE_OS_WINCE
 1.30253 +  LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
 1.30254 +  HANDLE hMutex;          /* Mutex used to control access to shared lock */  
 1.30255 +  HANDLE hShared;         /* Shared memory segment used for locking */
 1.30256 +  winceLock local;        /* Locks obtained by this instance of winFile */
 1.30257 +  winceLock *shared;      /* Global shared lock memory for the file  */
 1.30258 +#endif
 1.30259 +};
 1.30260 +
 1.30261 +/*
 1.30262 +** Allowed values for winFile.ctrlFlags
 1.30263 +*/
 1.30264 +#define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
 1.30265 +#define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
 1.30266 +
 1.30267 +/*
 1.30268 + * The size of the buffer used by sqlite3_win32_write_debug().
 1.30269 + */
 1.30270 +#ifndef SQLITE_WIN32_DBG_BUF_SIZE
 1.30271 +#  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
 1.30272 +#endif
 1.30273 +
 1.30274 +/*
 1.30275 + * The value used with sqlite3_win32_set_directory() to specify that
 1.30276 + * the data directory should be changed.
 1.30277 + */
 1.30278 +#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
 1.30279 +#  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
 1.30280 +#endif
 1.30281 +
 1.30282 +/*
 1.30283 + * The value used with sqlite3_win32_set_directory() to specify that
 1.30284 + * the temporary directory should be changed.
 1.30285 + */
 1.30286 +#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 1.30287 +#  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
 1.30288 +#endif
 1.30289 +
 1.30290 +/*
 1.30291 + * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
 1.30292 + * various Win32 API heap functions instead of our own.
 1.30293 + */
 1.30294 +#ifdef SQLITE_WIN32_MALLOC
 1.30295 +
 1.30296 +/*
 1.30297 + * If this is non-zero, an isolated heap will be created by the native Win32
 1.30298 + * allocator subsystem; otherwise, the default process heap will be used.  This
 1.30299 + * setting has no effect when compiling for WinRT.  By default, this is enabled
 1.30300 + * and an isolated heap will be created to store all allocated data.
 1.30301 + *
 1.30302 + ******************************************************************************
 1.30303 + * WARNING: It is important to note that when this setting is non-zero and the
 1.30304 + *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
 1.30305 + *          function), all data that was allocated using the isolated heap will
 1.30306 + *          be freed immediately and any attempt to access any of that freed
 1.30307 + *          data will almost certainly result in an immediate access violation.
 1.30308 + ******************************************************************************
 1.30309 + */
 1.30310 +#ifndef SQLITE_WIN32_HEAP_CREATE
 1.30311 +#  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
 1.30312 +#endif
 1.30313 +
 1.30314 +/*
 1.30315 + * The initial size of the Win32-specific heap.  This value may be zero.
 1.30316 + */
 1.30317 +#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
 1.30318 +#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
 1.30319 +                                       (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
 1.30320 +#endif
 1.30321 +
 1.30322 +/*
 1.30323 + * The maximum size of the Win32-specific heap.  This value may be zero.
 1.30324 + */
 1.30325 +#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
 1.30326 +#  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
 1.30327 +#endif
 1.30328 +
 1.30329 +/*
 1.30330 + * The extra flags to use in calls to the Win32 heap APIs.  This value may be
 1.30331 + * zero for the default behavior.
 1.30332 + */
 1.30333 +#ifndef SQLITE_WIN32_HEAP_FLAGS
 1.30334 +#  define SQLITE_WIN32_HEAP_FLAGS     (0)
 1.30335 +#endif
 1.30336 +
 1.30337 +/*
 1.30338 +** The winMemData structure stores information required by the Win32-specific
 1.30339 +** sqlite3_mem_methods implementation.
 1.30340 +*/
 1.30341 +typedef struct winMemData winMemData;
 1.30342 +struct winMemData {
 1.30343 +#ifndef NDEBUG
 1.30344 +  u32 magic;    /* Magic number to detect structure corruption. */
 1.30345 +#endif
 1.30346 +  HANDLE hHeap; /* The handle to our heap. */
 1.30347 +  BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
 1.30348 +};
 1.30349 +
 1.30350 +#ifndef NDEBUG
 1.30351 +#define WINMEM_MAGIC     0x42b2830b
 1.30352 +#endif
 1.30353 +
 1.30354 +static struct winMemData win_mem_data = {
 1.30355 +#ifndef NDEBUG
 1.30356 +  WINMEM_MAGIC,
 1.30357 +#endif
 1.30358 +  NULL, FALSE
 1.30359 +};
 1.30360 +
 1.30361 +#ifndef NDEBUG
 1.30362 +#define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
 1.30363 +#else
 1.30364 +#define winMemAssertMagic()
 1.30365 +#endif
 1.30366 +
 1.30367 +#define winMemGetHeap() win_mem_data.hHeap
 1.30368 +
 1.30369 +static void *winMemMalloc(int nBytes);
 1.30370 +static void winMemFree(void *pPrior);
 1.30371 +static void *winMemRealloc(void *pPrior, int nBytes);
 1.30372 +static int winMemSize(void *p);
 1.30373 +static int winMemRoundup(int n);
 1.30374 +static int winMemInit(void *pAppData);
 1.30375 +static void winMemShutdown(void *pAppData);
 1.30376 +
 1.30377 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
 1.30378 +#endif /* SQLITE_WIN32_MALLOC */
 1.30379 +
 1.30380 +/*
 1.30381 +** The following variable is (normally) set once and never changes
 1.30382 +** thereafter.  It records whether the operating system is Win9x
 1.30383 +** or WinNT.
 1.30384 +**
 1.30385 +** 0:   Operating system unknown.
 1.30386 +** 1:   Operating system is Win9x.
 1.30387 +** 2:   Operating system is WinNT.
 1.30388 +**
 1.30389 +** In order to facilitate testing on a WinNT system, the test fixture
 1.30390 +** can manually set this value to 1 to emulate Win98 behavior.
 1.30391 +*/
 1.30392 +#ifdef SQLITE_TEST
 1.30393 +SQLITE_API int sqlite3_os_type = 0;
 1.30394 +#else
 1.30395 +static int sqlite3_os_type = 0;
 1.30396 +#endif
 1.30397 +
 1.30398 +#ifndef SYSCALL
 1.30399 +#  define SYSCALL sqlite3_syscall_ptr
 1.30400 +#endif
 1.30401 +
 1.30402 +/*
 1.30403 +** This function is not available on Windows CE or WinRT.
 1.30404 + */
 1.30405 +
 1.30406 +#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 1.30407 +#  define osAreFileApisANSI()       1
 1.30408 +#endif
 1.30409 +
 1.30410 +/*
 1.30411 +** Many system calls are accessed through pointer-to-functions so that
 1.30412 +** they may be overridden at runtime to facilitate fault injection during
 1.30413 +** testing and sandboxing.  The following array holds the names and pointers
 1.30414 +** to all overrideable system calls.
 1.30415 +*/
 1.30416 +static struct win_syscall {
 1.30417 +  const char *zName;            /* Name of the sytem call */
 1.30418 +  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
 1.30419 +  sqlite3_syscall_ptr pDefault; /* Default value */
 1.30420 +} aSyscall[] = {
 1.30421 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 1.30422 +  { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
 1.30423 +#else
 1.30424 +  { "AreFileApisANSI",         (SYSCALL)0,                       0 },
 1.30425 +#endif
 1.30426 +
 1.30427 +#ifndef osAreFileApisANSI
 1.30428 +#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
 1.30429 +#endif
 1.30430 +
 1.30431 +#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
 1.30432 +  { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
 1.30433 +#else
 1.30434 +  { "CharLowerW",              (SYSCALL)0,                       0 },
 1.30435 +#endif
 1.30436 +
 1.30437 +#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
 1.30438 +
 1.30439 +#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
 1.30440 +  { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
 1.30441 +#else
 1.30442 +  { "CharUpperW",              (SYSCALL)0,                       0 },
 1.30443 +#endif
 1.30444 +
 1.30445 +#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
 1.30446 +
 1.30447 +  { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
 1.30448 +
 1.30449 +#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
 1.30450 +
 1.30451 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30452 +  { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
 1.30453 +#else
 1.30454 +  { "CreateFileA",             (SYSCALL)0,                       0 },
 1.30455 +#endif
 1.30456 +
 1.30457 +#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
 1.30458 +        LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
 1.30459 +
 1.30460 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 1.30461 +  { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
 1.30462 +#else
 1.30463 +  { "CreateFileW",             (SYSCALL)0,                       0 },
 1.30464 +#endif
 1.30465 +
 1.30466 +#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
 1.30467 +        LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
 1.30468 +
 1.30469 +#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
 1.30470 +        !defined(SQLITE_OMIT_WAL))
 1.30471 +  { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
 1.30472 +#else
 1.30473 +  { "CreateFileMappingA",      (SYSCALL)0,                       0 },
 1.30474 +#endif
 1.30475 +
 1.30476 +#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
 1.30477 +        DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
 1.30478 +
 1.30479 +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
 1.30480 +        !defined(SQLITE_OMIT_WAL))
 1.30481 +  { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
 1.30482 +#else
 1.30483 +  { "CreateFileMappingW",      (SYSCALL)0,                       0 },
 1.30484 +#endif
 1.30485 +
 1.30486 +#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
 1.30487 +        DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
 1.30488 +
 1.30489 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 1.30490 +  { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
 1.30491 +#else
 1.30492 +  { "CreateMutexW",            (SYSCALL)0,                       0 },
 1.30493 +#endif
 1.30494 +
 1.30495 +#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
 1.30496 +        LPCWSTR))aSyscall[8].pCurrent)
 1.30497 +
 1.30498 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30499 +  { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
 1.30500 +#else
 1.30501 +  { "DeleteFileA",             (SYSCALL)0,                       0 },
 1.30502 +#endif
 1.30503 +
 1.30504 +#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
 1.30505 +
 1.30506 +#if defined(SQLITE_WIN32_HAS_WIDE)
 1.30507 +  { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
 1.30508 +#else
 1.30509 +  { "DeleteFileW",             (SYSCALL)0,                       0 },
 1.30510 +#endif
 1.30511 +
 1.30512 +#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
 1.30513 +
 1.30514 +#if SQLITE_OS_WINCE
 1.30515 +  { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
 1.30516 +#else
 1.30517 +  { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
 1.30518 +#endif
 1.30519 +
 1.30520 +#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
 1.30521 +        LPFILETIME))aSyscall[11].pCurrent)
 1.30522 +
 1.30523 +#if SQLITE_OS_WINCE
 1.30524 +  { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
 1.30525 +#else
 1.30526 +  { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
 1.30527 +#endif
 1.30528 +
 1.30529 +#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
 1.30530 +        LPSYSTEMTIME))aSyscall[12].pCurrent)
 1.30531 +
 1.30532 +  { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
 1.30533 +
 1.30534 +#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
 1.30535 +
 1.30536 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30537 +  { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
 1.30538 +#else
 1.30539 +  { "FormatMessageA",          (SYSCALL)0,                       0 },
 1.30540 +#endif
 1.30541 +
 1.30542 +#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
 1.30543 +        DWORD,va_list*))aSyscall[14].pCurrent)
 1.30544 +
 1.30545 +#if defined(SQLITE_WIN32_HAS_WIDE)
 1.30546 +  { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
 1.30547 +#else
 1.30548 +  { "FormatMessageW",          (SYSCALL)0,                       0 },
 1.30549 +#endif
 1.30550 +
 1.30551 +#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
 1.30552 +        DWORD,va_list*))aSyscall[15].pCurrent)
 1.30553 +
 1.30554 +#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
 1.30555 +  { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
 1.30556 +#else
 1.30557 +  { "FreeLibrary",             (SYSCALL)0,                       0 },
 1.30558 +#endif
 1.30559 +
 1.30560 +#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
 1.30561 +
 1.30562 +  { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
 1.30563 +
 1.30564 +#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
 1.30565 +
 1.30566 +#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
 1.30567 +  { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
 1.30568 +#else
 1.30569 +  { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
 1.30570 +#endif
 1.30571 +
 1.30572 +#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
 1.30573 +        LPDWORD))aSyscall[18].pCurrent)
 1.30574 +
 1.30575 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 1.30576 +  { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
 1.30577 +#else
 1.30578 +  { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
 1.30579 +#endif
 1.30580 +
 1.30581 +#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
 1.30582 +        LPDWORD))aSyscall[19].pCurrent)
 1.30583 +
 1.30584 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30585 +  { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
 1.30586 +#else
 1.30587 +  { "GetFileAttributesA",      (SYSCALL)0,                       0 },
 1.30588 +#endif
 1.30589 +
 1.30590 +#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
 1.30591 +
 1.30592 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 1.30593 +  { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
 1.30594 +#else
 1.30595 +  { "GetFileAttributesW",      (SYSCALL)0,                       0 },
 1.30596 +#endif
 1.30597 +
 1.30598 +#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
 1.30599 +
 1.30600 +#if defined(SQLITE_WIN32_HAS_WIDE)
 1.30601 +  { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
 1.30602 +#else
 1.30603 +  { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
 1.30604 +#endif
 1.30605 +
 1.30606 +#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
 1.30607 +        LPVOID))aSyscall[22].pCurrent)
 1.30608 +
 1.30609 +#if !SQLITE_OS_WINRT
 1.30610 +  { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
 1.30611 +#else
 1.30612 +  { "GetFileSize",             (SYSCALL)0,                       0 },
 1.30613 +#endif
 1.30614 +
 1.30615 +#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
 1.30616 +
 1.30617 +#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
 1.30618 +  { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
 1.30619 +#else
 1.30620 +  { "GetFullPathNameA",        (SYSCALL)0,                       0 },
 1.30621 +#endif
 1.30622 +
 1.30623 +#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
 1.30624 +        LPSTR*))aSyscall[24].pCurrent)
 1.30625 +
 1.30626 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 1.30627 +  { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
 1.30628 +#else
 1.30629 +  { "GetFullPathNameW",        (SYSCALL)0,                       0 },
 1.30630 +#endif
 1.30631 +
 1.30632 +#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
 1.30633 +        LPWSTR*))aSyscall[25].pCurrent)
 1.30634 +
 1.30635 +  { "GetLastError",            (SYSCALL)GetLastError,            0 },
 1.30636 +
 1.30637 +#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
 1.30638 +
 1.30639 +#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
 1.30640 +#if SQLITE_OS_WINCE
 1.30641 +  /* The GetProcAddressA() routine is only available on Windows CE. */
 1.30642 +  { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
 1.30643 +#else
 1.30644 +  /* All other Windows platforms expect GetProcAddress() to take
 1.30645 +  ** an ANSI string regardless of the _UNICODE setting */
 1.30646 +  { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
 1.30647 +#endif
 1.30648 +#else
 1.30649 +  { "GetProcAddressA",         (SYSCALL)0,                       0 },
 1.30650 +#endif
 1.30651 +
 1.30652 +#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
 1.30653 +        LPCSTR))aSyscall[27].pCurrent)
 1.30654 +
 1.30655 +#if !SQLITE_OS_WINRT
 1.30656 +  { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
 1.30657 +#else
 1.30658 +  { "GetSystemInfo",           (SYSCALL)0,                       0 },
 1.30659 +#endif
 1.30660 +
 1.30661 +#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
 1.30662 +
 1.30663 +  { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
 1.30664 +
 1.30665 +#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
 1.30666 +
 1.30667 +#if !SQLITE_OS_WINCE
 1.30668 +  { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
 1.30669 +#else
 1.30670 +  { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
 1.30671 +#endif
 1.30672 +
 1.30673 +#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
 1.30674 +        LPFILETIME))aSyscall[30].pCurrent)
 1.30675 +
 1.30676 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30677 +  { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
 1.30678 +#else
 1.30679 +  { "GetTempPathA",            (SYSCALL)0,                       0 },
 1.30680 +#endif
 1.30681 +
 1.30682 +#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
 1.30683 +
 1.30684 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 1.30685 +  { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
 1.30686 +#else
 1.30687 +  { "GetTempPathW",            (SYSCALL)0,                       0 },
 1.30688 +#endif
 1.30689 +
 1.30690 +#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
 1.30691 +
 1.30692 +#if !SQLITE_OS_WINRT
 1.30693 +  { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
 1.30694 +#else
 1.30695 +  { "GetTickCount",            (SYSCALL)0,                       0 },
 1.30696 +#endif
 1.30697 +
 1.30698 +#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
 1.30699 +
 1.30700 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30701 +  { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
 1.30702 +#else
 1.30703 +  { "GetVersionExA",           (SYSCALL)0,                       0 },
 1.30704 +#endif
 1.30705 +
 1.30706 +#define osGetVersionExA ((BOOL(WINAPI*)( \
 1.30707 +        LPOSVERSIONINFOA))aSyscall[34].pCurrent)
 1.30708 +
 1.30709 +  { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
 1.30710 +
 1.30711 +#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
 1.30712 +        SIZE_T))aSyscall[35].pCurrent)
 1.30713 +
 1.30714 +#if !SQLITE_OS_WINRT
 1.30715 +  { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
 1.30716 +#else
 1.30717 +  { "HeapCreate",              (SYSCALL)0,                       0 },
 1.30718 +#endif
 1.30719 +
 1.30720 +#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
 1.30721 +        SIZE_T))aSyscall[36].pCurrent)
 1.30722 +
 1.30723 +#if !SQLITE_OS_WINRT
 1.30724 +  { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
 1.30725 +#else
 1.30726 +  { "HeapDestroy",             (SYSCALL)0,                       0 },
 1.30727 +#endif
 1.30728 +
 1.30729 +#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
 1.30730 +
 1.30731 +  { "HeapFree",                (SYSCALL)HeapFree,                0 },
 1.30732 +
 1.30733 +#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
 1.30734 +
 1.30735 +  { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
 1.30736 +
 1.30737 +#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
 1.30738 +        SIZE_T))aSyscall[39].pCurrent)
 1.30739 +
 1.30740 +  { "HeapSize",                (SYSCALL)HeapSize,                0 },
 1.30741 +
 1.30742 +#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
 1.30743 +        LPCVOID))aSyscall[40].pCurrent)
 1.30744 +
 1.30745 +#if !SQLITE_OS_WINRT
 1.30746 +  { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
 1.30747 +#else
 1.30748 +  { "HeapValidate",            (SYSCALL)0,                       0 },
 1.30749 +#endif
 1.30750 +
 1.30751 +#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
 1.30752 +        LPCVOID))aSyscall[41].pCurrent)
 1.30753 +
 1.30754 +#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
 1.30755 +  { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
 1.30756 +#else
 1.30757 +  { "LoadLibraryA",            (SYSCALL)0,                       0 },
 1.30758 +#endif
 1.30759 +
 1.30760 +#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
 1.30761 +
 1.30762 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
 1.30763 +        !defined(SQLITE_OMIT_LOAD_EXTENSION)
 1.30764 +  { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
 1.30765 +#else
 1.30766 +  { "LoadLibraryW",            (SYSCALL)0,                       0 },
 1.30767 +#endif
 1.30768 +
 1.30769 +#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
 1.30770 +
 1.30771 +#if !SQLITE_OS_WINRT
 1.30772 +  { "LocalFree",               (SYSCALL)LocalFree,               0 },
 1.30773 +#else
 1.30774 +  { "LocalFree",               (SYSCALL)0,                       0 },
 1.30775 +#endif
 1.30776 +
 1.30777 +#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
 1.30778 +
 1.30779 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 1.30780 +  { "LockFile",                (SYSCALL)LockFile,                0 },
 1.30781 +#else
 1.30782 +  { "LockFile",                (SYSCALL)0,                       0 },
 1.30783 +#endif
 1.30784 +
 1.30785 +#ifndef osLockFile
 1.30786 +#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 1.30787 +        DWORD))aSyscall[45].pCurrent)
 1.30788 +#endif
 1.30789 +
 1.30790 +#if !SQLITE_OS_WINCE
 1.30791 +  { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
 1.30792 +#else
 1.30793 +  { "LockFileEx",              (SYSCALL)0,                       0 },
 1.30794 +#endif
 1.30795 +
 1.30796 +#ifndef osLockFileEx
 1.30797 +#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
 1.30798 +        LPOVERLAPPED))aSyscall[46].pCurrent)
 1.30799 +#endif
 1.30800 +
 1.30801 +#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
 1.30802 +  { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
 1.30803 +#else
 1.30804 +  { "MapViewOfFile",           (SYSCALL)0,                       0 },
 1.30805 +#endif
 1.30806 +
 1.30807 +#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 1.30808 +        SIZE_T))aSyscall[47].pCurrent)
 1.30809 +
 1.30810 +  { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
 1.30811 +
 1.30812 +#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
 1.30813 +        int))aSyscall[48].pCurrent)
 1.30814 +
 1.30815 +  { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
 1.30816 +
 1.30817 +#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
 1.30818 +        LARGE_INTEGER*))aSyscall[49].pCurrent)
 1.30819 +
 1.30820 +  { "ReadFile",                (SYSCALL)ReadFile,                0 },
 1.30821 +
 1.30822 +#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
 1.30823 +        LPOVERLAPPED))aSyscall[50].pCurrent)
 1.30824 +
 1.30825 +  { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
 1.30826 +
 1.30827 +#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
 1.30828 +
 1.30829 +#if !SQLITE_OS_WINRT
 1.30830 +  { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
 1.30831 +#else
 1.30832 +  { "SetFilePointer",          (SYSCALL)0,                       0 },
 1.30833 +#endif
 1.30834 +
 1.30835 +#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
 1.30836 +        DWORD))aSyscall[52].pCurrent)
 1.30837 +
 1.30838 +#if !SQLITE_OS_WINRT
 1.30839 +  { "Sleep",                   (SYSCALL)Sleep,                   0 },
 1.30840 +#else
 1.30841 +  { "Sleep",                   (SYSCALL)0,                       0 },
 1.30842 +#endif
 1.30843 +
 1.30844 +#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
 1.30845 +
 1.30846 +  { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
 1.30847 +
 1.30848 +#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
 1.30849 +        LPFILETIME))aSyscall[54].pCurrent)
 1.30850 +
 1.30851 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 1.30852 +  { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
 1.30853 +#else
 1.30854 +  { "UnlockFile",              (SYSCALL)0,                       0 },
 1.30855 +#endif
 1.30856 +
 1.30857 +#ifndef osUnlockFile
 1.30858 +#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 1.30859 +        DWORD))aSyscall[55].pCurrent)
 1.30860 +#endif
 1.30861 +
 1.30862 +#if !SQLITE_OS_WINCE
 1.30863 +  { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
 1.30864 +#else
 1.30865 +  { "UnlockFileEx",            (SYSCALL)0,                       0 },
 1.30866 +#endif
 1.30867 +
 1.30868 +#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 1.30869 +        LPOVERLAPPED))aSyscall[56].pCurrent)
 1.30870 +
 1.30871 +#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
 1.30872 +  { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
 1.30873 +#else
 1.30874 +  { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
 1.30875 +#endif
 1.30876 +
 1.30877 +#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
 1.30878 +
 1.30879 +  { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
 1.30880 +
 1.30881 +#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
 1.30882 +        LPCSTR,LPBOOL))aSyscall[58].pCurrent)
 1.30883 +
 1.30884 +  { "WriteFile",               (SYSCALL)WriteFile,               0 },
 1.30885 +
 1.30886 +#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
 1.30887 +        LPOVERLAPPED))aSyscall[59].pCurrent)
 1.30888 +
 1.30889 +#if SQLITE_OS_WINRT
 1.30890 +  { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
 1.30891 +#else
 1.30892 +  { "CreateEventExW",          (SYSCALL)0,                       0 },
 1.30893 +#endif
 1.30894 +
 1.30895 +#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
 1.30896 +        DWORD,DWORD))aSyscall[60].pCurrent)
 1.30897 +
 1.30898 +#if !SQLITE_OS_WINRT
 1.30899 +  { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
 1.30900 +#else
 1.30901 +  { "WaitForSingleObject",     (SYSCALL)0,                       0 },
 1.30902 +#endif
 1.30903 +
 1.30904 +#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
 1.30905 +        DWORD))aSyscall[61].pCurrent)
 1.30906 +
 1.30907 +#if SQLITE_OS_WINRT
 1.30908 +  { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
 1.30909 +#else
 1.30910 +  { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
 1.30911 +#endif
 1.30912 +
 1.30913 +#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
 1.30914 +        BOOL))aSyscall[62].pCurrent)
 1.30915 +
 1.30916 +#if SQLITE_OS_WINRT
 1.30917 +  { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
 1.30918 +#else
 1.30919 +  { "SetFilePointerEx",        (SYSCALL)0,                       0 },
 1.30920 +#endif
 1.30921 +
 1.30922 +#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
 1.30923 +        PLARGE_INTEGER,DWORD))aSyscall[63].pCurrent)
 1.30924 +
 1.30925 +#if SQLITE_OS_WINRT
 1.30926 +  { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
 1.30927 +#else
 1.30928 +  { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
 1.30929 +#endif
 1.30930 +
 1.30931 +#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
 1.30932 +        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[64].pCurrent)
 1.30933 +
 1.30934 +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 1.30935 +  { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
 1.30936 +#else
 1.30937 +  { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
 1.30938 +#endif
 1.30939 +
 1.30940 +#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
 1.30941 +        SIZE_T))aSyscall[65].pCurrent)
 1.30942 +
 1.30943 +#if SQLITE_OS_WINRT
 1.30944 +  { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
 1.30945 +#else
 1.30946 +  { "CreateFile2",             (SYSCALL)0,                       0 },
 1.30947 +#endif
 1.30948 +
 1.30949 +#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
 1.30950 +        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[66].pCurrent)
 1.30951 +
 1.30952 +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
 1.30953 +  { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
 1.30954 +#else
 1.30955 +  { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
 1.30956 +#endif
 1.30957 +
 1.30958 +#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
 1.30959 +        DWORD))aSyscall[67].pCurrent)
 1.30960 +
 1.30961 +#if SQLITE_OS_WINRT
 1.30962 +  { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
 1.30963 +#else
 1.30964 +  { "GetTickCount64",          (SYSCALL)0,                       0 },
 1.30965 +#endif
 1.30966 +
 1.30967 +#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[68].pCurrent)
 1.30968 +
 1.30969 +#if SQLITE_OS_WINRT
 1.30970 +  { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
 1.30971 +#else
 1.30972 +  { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
 1.30973 +#endif
 1.30974 +
 1.30975 +#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
 1.30976 +        LPSYSTEM_INFO))aSyscall[69].pCurrent)
 1.30977 +
 1.30978 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.30979 +  { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
 1.30980 +#else
 1.30981 +  { "OutputDebugStringA",      (SYSCALL)0,                       0 },
 1.30982 +#endif
 1.30983 +
 1.30984 +#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[70].pCurrent)
 1.30985 +
 1.30986 +#if defined(SQLITE_WIN32_HAS_WIDE)
 1.30987 +  { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
 1.30988 +#else
 1.30989 +  { "OutputDebugStringW",      (SYSCALL)0,                       0 },
 1.30990 +#endif
 1.30991 +
 1.30992 +#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[71].pCurrent)
 1.30993 +
 1.30994 +  { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
 1.30995 +
 1.30996 +#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[72].pCurrent)
 1.30997 +
 1.30998 +#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 1.30999 +  { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
 1.31000 +#else
 1.31001 +  { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
 1.31002 +#endif
 1.31003 +
 1.31004 +#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
 1.31005 +        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[73].pCurrent)
 1.31006 +
 1.31007 +}; /* End of the overrideable system calls */
 1.31008 +
 1.31009 +/*
 1.31010 +** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 1.31011 +** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
 1.31012 +** system call pointer, or SQLITE_NOTFOUND if there is no configurable
 1.31013 +** system call named zName.
 1.31014 +*/
 1.31015 +static int winSetSystemCall(
 1.31016 +  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
 1.31017 +  const char *zName,            /* Name of system call to override */
 1.31018 +  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
 1.31019 +){
 1.31020 +  unsigned int i;
 1.31021 +  int rc = SQLITE_NOTFOUND;
 1.31022 +
 1.31023 +  UNUSED_PARAMETER(pNotUsed);
 1.31024 +  if( zName==0 ){
 1.31025 +    /* If no zName is given, restore all system calls to their default
 1.31026 +    ** settings and return NULL
 1.31027 +    */
 1.31028 +    rc = SQLITE_OK;
 1.31029 +    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 1.31030 +      if( aSyscall[i].pDefault ){
 1.31031 +        aSyscall[i].pCurrent = aSyscall[i].pDefault;
 1.31032 +      }
 1.31033 +    }
 1.31034 +  }else{
 1.31035 +    /* If zName is specified, operate on only the one system call
 1.31036 +    ** specified.
 1.31037 +    */
 1.31038 +    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 1.31039 +      if( strcmp(zName, aSyscall[i].zName)==0 ){
 1.31040 +        if( aSyscall[i].pDefault==0 ){
 1.31041 +          aSyscall[i].pDefault = aSyscall[i].pCurrent;
 1.31042 +        }
 1.31043 +        rc = SQLITE_OK;
 1.31044 +        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
 1.31045 +        aSyscall[i].pCurrent = pNewFunc;
 1.31046 +        break;
 1.31047 +      }
 1.31048 +    }
 1.31049 +  }
 1.31050 +  return rc;
 1.31051 +}
 1.31052 +
 1.31053 +/*
 1.31054 +** Return the value of a system call.  Return NULL if zName is not a
 1.31055 +** recognized system call name.  NULL is also returned if the system call
 1.31056 +** is currently undefined.
 1.31057 +*/
 1.31058 +static sqlite3_syscall_ptr winGetSystemCall(
 1.31059 +  sqlite3_vfs *pNotUsed,
 1.31060 +  const char *zName
 1.31061 +){
 1.31062 +  unsigned int i;
 1.31063 +
 1.31064 +  UNUSED_PARAMETER(pNotUsed);
 1.31065 +  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 1.31066 +    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
 1.31067 +  }
 1.31068 +  return 0;
 1.31069 +}
 1.31070 +
 1.31071 +/*
 1.31072 +** Return the name of the first system call after zName.  If zName==NULL
 1.31073 +** then return the name of the first system call.  Return NULL if zName
 1.31074 +** is the last system call or if zName is not the name of a valid
 1.31075 +** system call.
 1.31076 +*/
 1.31077 +static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
 1.31078 +  int i = -1;
 1.31079 +
 1.31080 +  UNUSED_PARAMETER(p);
 1.31081 +  if( zName ){
 1.31082 +    for(i=0; i<ArraySize(aSyscall)-1; i++){
 1.31083 +      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
 1.31084 +    }
 1.31085 +  }
 1.31086 +  for(i++; i<ArraySize(aSyscall); i++){
 1.31087 +    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
 1.31088 +  }
 1.31089 +  return 0;
 1.31090 +}
 1.31091 +
 1.31092 +/*
 1.31093 +** This function outputs the specified (ANSI) string to the Win32 debugger
 1.31094 +** (if available).
 1.31095 +*/
 1.31096 +
 1.31097 +SQLITE_API void sqlite3_win32_write_debug(char *zBuf, int nBuf){
 1.31098 +  char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
 1.31099 +  int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
 1.31100 +  if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
 1.31101 +  assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
 1.31102 +#if defined(SQLITE_WIN32_HAS_ANSI)
 1.31103 +  if( nMin>0 ){
 1.31104 +    memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 1.31105 +    memcpy(zDbgBuf, zBuf, nMin);
 1.31106 +    osOutputDebugStringA(zDbgBuf);
 1.31107 +  }else{
 1.31108 +    osOutputDebugStringA(zBuf);
 1.31109 +  }
 1.31110 +#elif defined(SQLITE_WIN32_HAS_WIDE)
 1.31111 +  memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 1.31112 +  if ( osMultiByteToWideChar(
 1.31113 +          osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
 1.31114 +          nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
 1.31115 +    return;
 1.31116 +  }
 1.31117 +  osOutputDebugStringW((LPCWSTR)zDbgBuf);
 1.31118 +#else
 1.31119 +  if( nMin>0 ){
 1.31120 +    memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 1.31121 +    memcpy(zDbgBuf, zBuf, nMin);
 1.31122 +    fprintf(stderr, "%s", zDbgBuf);
 1.31123 +  }else{
 1.31124 +    fprintf(stderr, "%s", zBuf);
 1.31125 +  }
 1.31126 +#endif
 1.31127 +}
 1.31128 +
 1.31129 +/*
 1.31130 +** The following routine suspends the current thread for at least ms
 1.31131 +** milliseconds.  This is equivalent to the Win32 Sleep() interface.
 1.31132 +*/
 1.31133 +#if SQLITE_OS_WINRT
 1.31134 +static HANDLE sleepObj = NULL;
 1.31135 +#endif
 1.31136 +
 1.31137 +SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
 1.31138 +#if SQLITE_OS_WINRT
 1.31139 +  if ( sleepObj==NULL ){
 1.31140 +    sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
 1.31141 +                                SYNCHRONIZE);
 1.31142 +  }
 1.31143 +  assert( sleepObj!=NULL );
 1.31144 +  osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
 1.31145 +#else
 1.31146 +  osSleep(milliseconds);
 1.31147 +#endif
 1.31148 +}
 1.31149 +
 1.31150 +/*
 1.31151 +** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
 1.31152 +** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
 1.31153 +**
 1.31154 +** Here is an interesting observation:  Win95, Win98, and WinME lack
 1.31155 +** the LockFileEx() API.  But we can still statically link against that
 1.31156 +** API as long as we don't call it when running Win95/98/ME.  A call to
 1.31157 +** this routine is used to determine if the host is Win95/98/ME or
 1.31158 +** WinNT/2K/XP so that we will know whether or not we can safely call
 1.31159 +** the LockFileEx() API.
 1.31160 +*/
 1.31161 +#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 1.31162 +# define isNT()  (1)
 1.31163 +#elif !defined(SQLITE_WIN32_HAS_WIDE)
 1.31164 +# define isNT()  (0)
 1.31165 +#else
 1.31166 +  static int isNT(void){
 1.31167 +    if( sqlite3_os_type==0 ){
 1.31168 +      OSVERSIONINFOA sInfo;
 1.31169 +      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
 1.31170 +      osGetVersionExA(&sInfo);
 1.31171 +      sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
 1.31172 +    }
 1.31173 +    return sqlite3_os_type==2;
 1.31174 +  }
 1.31175 +#endif
 1.31176 +
 1.31177 +#ifdef SQLITE_WIN32_MALLOC
 1.31178 +/*
 1.31179 +** Allocate nBytes of memory.
 1.31180 +*/
 1.31181 +static void *winMemMalloc(int nBytes){
 1.31182 +  HANDLE hHeap;
 1.31183 +  void *p;
 1.31184 +
 1.31185 +  winMemAssertMagic();
 1.31186 +  hHeap = winMemGetHeap();
 1.31187 +  assert( hHeap!=0 );
 1.31188 +  assert( hHeap!=INVALID_HANDLE_VALUE );
 1.31189 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 1.31190 +  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 1.31191 +#endif
 1.31192 +  assert( nBytes>=0 );
 1.31193 +  p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
 1.31194 +  if( !p ){
 1.31195 +    sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
 1.31196 +                nBytes, osGetLastError(), (void*)hHeap);
 1.31197 +  }
 1.31198 +  return p;
 1.31199 +}
 1.31200 +
 1.31201 +/*
 1.31202 +** Free memory.
 1.31203 +*/
 1.31204 +static void winMemFree(void *pPrior){
 1.31205 +  HANDLE hHeap;
 1.31206 +
 1.31207 +  winMemAssertMagic();
 1.31208 +  hHeap = winMemGetHeap();
 1.31209 +  assert( hHeap!=0 );
 1.31210 +  assert( hHeap!=INVALID_HANDLE_VALUE );
 1.31211 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 1.31212 +  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
 1.31213 +#endif
 1.31214 +  if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
 1.31215 +  if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
 1.31216 +    sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
 1.31217 +                pPrior, osGetLastError(), (void*)hHeap);
 1.31218 +  }
 1.31219 +}
 1.31220 +
 1.31221 +/*
 1.31222 +** Change the size of an existing memory allocation
 1.31223 +*/
 1.31224 +static void *winMemRealloc(void *pPrior, int nBytes){
 1.31225 +  HANDLE hHeap;
 1.31226 +  void *p;
 1.31227 +
 1.31228 +  winMemAssertMagic();
 1.31229 +  hHeap = winMemGetHeap();
 1.31230 +  assert( hHeap!=0 );
 1.31231 +  assert( hHeap!=INVALID_HANDLE_VALUE );
 1.31232 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 1.31233 +  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
 1.31234 +#endif
 1.31235 +  assert( nBytes>=0 );
 1.31236 +  if( !pPrior ){
 1.31237 +    p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
 1.31238 +  }else{
 1.31239 +    p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
 1.31240 +  }
 1.31241 +  if( !p ){
 1.31242 +    sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
 1.31243 +                pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
 1.31244 +                (void*)hHeap);
 1.31245 +  }
 1.31246 +  return p;
 1.31247 +}
 1.31248 +
 1.31249 +/*
 1.31250 +** Return the size of an outstanding allocation, in bytes.
 1.31251 +*/
 1.31252 +static int winMemSize(void *p){
 1.31253 +  HANDLE hHeap;
 1.31254 +  SIZE_T n;
 1.31255 +
 1.31256 +  winMemAssertMagic();
 1.31257 +  hHeap = winMemGetHeap();
 1.31258 +  assert( hHeap!=0 );
 1.31259 +  assert( hHeap!=INVALID_HANDLE_VALUE );
 1.31260 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 1.31261 +  assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 1.31262 +#endif
 1.31263 +  if( !p ) return 0;
 1.31264 +  n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
 1.31265 +  if( n==(SIZE_T)-1 ){
 1.31266 +    sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
 1.31267 +                p, osGetLastError(), (void*)hHeap);
 1.31268 +    return 0;
 1.31269 +  }
 1.31270 +  return (int)n;
 1.31271 +}
 1.31272 +
 1.31273 +/*
 1.31274 +** Round up a request size to the next valid allocation size.
 1.31275 +*/
 1.31276 +static int winMemRoundup(int n){
 1.31277 +  return n;
 1.31278 +}
 1.31279 +
 1.31280 +/*
 1.31281 +** Initialize this module.
 1.31282 +*/
 1.31283 +static int winMemInit(void *pAppData){
 1.31284 +  winMemData *pWinMemData = (winMemData *)pAppData;
 1.31285 +
 1.31286 +  if( !pWinMemData ) return SQLITE_ERROR;
 1.31287 +  assert( pWinMemData->magic==WINMEM_MAGIC );
 1.31288 +
 1.31289 +#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
 1.31290 +  if( !pWinMemData->hHeap ){
 1.31291 +    pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
 1.31292 +                                      SQLITE_WIN32_HEAP_INIT_SIZE,
 1.31293 +                                      SQLITE_WIN32_HEAP_MAX_SIZE);
 1.31294 +    if( !pWinMemData->hHeap ){
 1.31295 +      sqlite3_log(SQLITE_NOMEM,
 1.31296 +          "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
 1.31297 +          osGetLastError(), SQLITE_WIN32_HEAP_FLAGS,
 1.31298 +          SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE);
 1.31299 +      return SQLITE_NOMEM;
 1.31300 +    }
 1.31301 +    pWinMemData->bOwned = TRUE;
 1.31302 +    assert( pWinMemData->bOwned );
 1.31303 +  }
 1.31304 +#else
 1.31305 +  pWinMemData->hHeap = osGetProcessHeap();
 1.31306 +  if( !pWinMemData->hHeap ){
 1.31307 +    sqlite3_log(SQLITE_NOMEM,
 1.31308 +        "failed to GetProcessHeap (%d)", osGetLastError());
 1.31309 +    return SQLITE_NOMEM;
 1.31310 +  }
 1.31311 +  pWinMemData->bOwned = FALSE;
 1.31312 +  assert( !pWinMemData->bOwned );
 1.31313 +#endif
 1.31314 +  assert( pWinMemData->hHeap!=0 );
 1.31315 +  assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
 1.31316 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 1.31317 +  assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 1.31318 +#endif
 1.31319 +  return SQLITE_OK;
 1.31320 +}
 1.31321 +
 1.31322 +/*
 1.31323 +** Deinitialize this module.
 1.31324 +*/
 1.31325 +static void winMemShutdown(void *pAppData){
 1.31326 +  winMemData *pWinMemData = (winMemData *)pAppData;
 1.31327 +
 1.31328 +  if( !pWinMemData ) return;
 1.31329 +  if( pWinMemData->hHeap ){
 1.31330 +    assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
 1.31331 +#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 1.31332 +    assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 1.31333 +#endif
 1.31334 +    if( pWinMemData->bOwned ){
 1.31335 +      if( !osHeapDestroy(pWinMemData->hHeap) ){
 1.31336 +        sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
 1.31337 +                    osGetLastError(), (void*)pWinMemData->hHeap);
 1.31338 +      }
 1.31339 +      pWinMemData->bOwned = FALSE;
 1.31340 +    }
 1.31341 +    pWinMemData->hHeap = NULL;
 1.31342 +  }
 1.31343 +}
 1.31344 +
 1.31345 +/*
 1.31346 +** Populate the low-level memory allocation function pointers in
 1.31347 +** sqlite3GlobalConfig.m with pointers to the routines in this file. The
 1.31348 +** arguments specify the block of memory to manage.
 1.31349 +**
 1.31350 +** This routine is only called by sqlite3_config(), and therefore
 1.31351 +** is not required to be threadsafe (it is not).
 1.31352 +*/
 1.31353 +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
 1.31354 +  static const sqlite3_mem_methods winMemMethods = {
 1.31355 +    winMemMalloc,
 1.31356 +    winMemFree,
 1.31357 +    winMemRealloc,
 1.31358 +    winMemSize,
 1.31359 +    winMemRoundup,
 1.31360 +    winMemInit,
 1.31361 +    winMemShutdown,
 1.31362 +    &win_mem_data
 1.31363 +  };
 1.31364 +  return &winMemMethods;
 1.31365 +}
 1.31366 +
 1.31367 +SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 1.31368 +  sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
 1.31369 +}
 1.31370 +#endif /* SQLITE_WIN32_MALLOC */
 1.31371 +
 1.31372 +/*
 1.31373 +** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
 1.31374 +**
 1.31375 +** Space to hold the returned string is obtained from malloc.
 1.31376 +*/
 1.31377 +static LPWSTR utf8ToUnicode(const char *zFilename){
 1.31378 +  int nChar;
 1.31379 +  LPWSTR zWideFilename;
 1.31380 +
 1.31381 +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
 1.31382 +  if( nChar==0 ){
 1.31383 +    return 0;
 1.31384 +  }
 1.31385 +  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
 1.31386 +  if( zWideFilename==0 ){
 1.31387 +    return 0;
 1.31388 +  }
 1.31389 +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
 1.31390 +                                nChar);
 1.31391 +  if( nChar==0 ){
 1.31392 +    sqlite3_free(zWideFilename);
 1.31393 +    zWideFilename = 0;
 1.31394 +  }
 1.31395 +  return zWideFilename;
 1.31396 +}
 1.31397 +
 1.31398 +/*
 1.31399 +** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
 1.31400 +** obtained from sqlite3_malloc().
 1.31401 +*/
 1.31402 +static char *unicodeToUtf8(LPCWSTR zWideFilename){
 1.31403 +  int nByte;
 1.31404 +  char *zFilename;
 1.31405 +
 1.31406 +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
 1.31407 +  if( nByte == 0 ){
 1.31408 +    return 0;
 1.31409 +  }
 1.31410 +  zFilename = sqlite3MallocZero( nByte );
 1.31411 +  if( zFilename==0 ){
 1.31412 +    return 0;
 1.31413 +  }
 1.31414 +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
 1.31415 +                                0, 0);
 1.31416 +  if( nByte == 0 ){
 1.31417 +    sqlite3_free(zFilename);
 1.31418 +    zFilename = 0;
 1.31419 +  }
 1.31420 +  return zFilename;
 1.31421 +}
 1.31422 +
 1.31423 +/*
 1.31424 +** Convert an ANSI string to Microsoft Unicode, based on the
 1.31425 +** current codepage settings for file apis.
 1.31426 +** 
 1.31427 +** Space to hold the returned string is obtained
 1.31428 +** from sqlite3_malloc.
 1.31429 +*/
 1.31430 +static LPWSTR mbcsToUnicode(const char *zFilename){
 1.31431 +  int nByte;
 1.31432 +  LPWSTR zMbcsFilename;
 1.31433 +  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
 1.31434 +
 1.31435 +  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
 1.31436 +                                0)*sizeof(WCHAR);
 1.31437 +  if( nByte==0 ){
 1.31438 +    return 0;
 1.31439 +  }
 1.31440 +  zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
 1.31441 +  if( zMbcsFilename==0 ){
 1.31442 +    return 0;
 1.31443 +  }
 1.31444 +  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
 1.31445 +                                nByte);
 1.31446 +  if( nByte==0 ){
 1.31447 +    sqlite3_free(zMbcsFilename);
 1.31448 +    zMbcsFilename = 0;
 1.31449 +  }
 1.31450 +  return zMbcsFilename;
 1.31451 +}
 1.31452 +
 1.31453 +/*
 1.31454 +** Convert Microsoft Unicode to multi-byte character string, based on the
 1.31455 +** user's ANSI codepage.
 1.31456 +**
 1.31457 +** Space to hold the returned string is obtained from
 1.31458 +** sqlite3_malloc().
 1.31459 +*/
 1.31460 +static char *unicodeToMbcs(LPCWSTR zWideFilename){
 1.31461 +  int nByte;
 1.31462 +  char *zFilename;
 1.31463 +  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
 1.31464 +
 1.31465 +  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
 1.31466 +  if( nByte == 0 ){
 1.31467 +    return 0;
 1.31468 +  }
 1.31469 +  zFilename = sqlite3MallocZero( nByte );
 1.31470 +  if( zFilename==0 ){
 1.31471 +    return 0;
 1.31472 +  }
 1.31473 +  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
 1.31474 +                                nByte, 0, 0);
 1.31475 +  if( nByte == 0 ){
 1.31476 +    sqlite3_free(zFilename);
 1.31477 +    zFilename = 0;
 1.31478 +  }
 1.31479 +  return zFilename;
 1.31480 +}
 1.31481 +
 1.31482 +/*
 1.31483 +** Convert multibyte character string to UTF-8.  Space to hold the
 1.31484 +** returned string is obtained from sqlite3_malloc().
 1.31485 +*/
 1.31486 +SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
 1.31487 +  char *zFilenameUtf8;
 1.31488 +  LPWSTR zTmpWide;
 1.31489 +
 1.31490 +  zTmpWide = mbcsToUnicode(zFilename);
 1.31491 +  if( zTmpWide==0 ){
 1.31492 +    return 0;
 1.31493 +  }
 1.31494 +  zFilenameUtf8 = unicodeToUtf8(zTmpWide);
 1.31495 +  sqlite3_free(zTmpWide);
 1.31496 +  return zFilenameUtf8;
 1.31497 +}
 1.31498 +
 1.31499 +/*
 1.31500 +** Convert UTF-8 to multibyte character string.  Space to hold the 
 1.31501 +** returned string is obtained from sqlite3_malloc().
 1.31502 +*/
 1.31503 +SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
 1.31504 +  char *zFilenameMbcs;
 1.31505 +  LPWSTR zTmpWide;
 1.31506 +
 1.31507 +  zTmpWide = utf8ToUnicode(zFilename);
 1.31508 +  if( zTmpWide==0 ){
 1.31509 +    return 0;
 1.31510 +  }
 1.31511 +  zFilenameMbcs = unicodeToMbcs(zTmpWide);
 1.31512 +  sqlite3_free(zTmpWide);
 1.31513 +  return zFilenameMbcs;
 1.31514 +}
 1.31515 +
 1.31516 +/*
 1.31517 +** This function sets the data directory or the temporary directory based on
 1.31518 +** the provided arguments.  The type argument must be 1 in order to set the
 1.31519 +** data directory or 2 in order to set the temporary directory.  The zValue
 1.31520 +** argument is the name of the directory to use.  The return value will be
 1.31521 +** SQLITE_OK if successful.
 1.31522 +*/
 1.31523 +SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
 1.31524 +  char **ppDirectory = 0;
 1.31525 +#ifndef SQLITE_OMIT_AUTOINIT
 1.31526 +  int rc = sqlite3_initialize();
 1.31527 +  if( rc ) return rc;
 1.31528 +#endif
 1.31529 +  if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
 1.31530 +    ppDirectory = &sqlite3_data_directory;
 1.31531 +  }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
 1.31532 +    ppDirectory = &sqlite3_temp_directory;
 1.31533 +  }
 1.31534 +  assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
 1.31535 +          || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 1.31536 +  );
 1.31537 +  assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
 1.31538 +  if( ppDirectory ){
 1.31539 +    char *zValueUtf8 = 0;
 1.31540 +    if( zValue && zValue[0] ){
 1.31541 +      zValueUtf8 = unicodeToUtf8(zValue);
 1.31542 +      if ( zValueUtf8==0 ){
 1.31543 +        return SQLITE_NOMEM;
 1.31544 +      }
 1.31545 +    }
 1.31546 +    sqlite3_free(*ppDirectory);
 1.31547 +    *ppDirectory = zValueUtf8;
 1.31548 +    return SQLITE_OK;
 1.31549 +  }
 1.31550 +  return SQLITE_ERROR;
 1.31551 +}
 1.31552 +
 1.31553 +/*
 1.31554 +** The return value of getLastErrorMsg
 1.31555 +** is zero if the error message fits in the buffer, or non-zero
 1.31556 +** otherwise (if the message was truncated).
 1.31557 +*/
 1.31558 +static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
 1.31559 +  /* FormatMessage returns 0 on failure.  Otherwise it
 1.31560 +  ** returns the number of TCHARs written to the output
 1.31561 +  ** buffer, excluding the terminating null char.
 1.31562 +  */
 1.31563 +  DWORD dwLen = 0;
 1.31564 +  char *zOut = 0;
 1.31565 +
 1.31566 +  if( isNT() ){
 1.31567 +#if SQLITE_OS_WINRT
 1.31568 +    WCHAR zTempWide[MAX_PATH+1]; /* NOTE: Somewhat arbitrary. */
 1.31569 +    dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
 1.31570 +                             FORMAT_MESSAGE_IGNORE_INSERTS,
 1.31571 +                             NULL,
 1.31572 +                             lastErrno,
 1.31573 +                             0,
 1.31574 +                             zTempWide,
 1.31575 +                             MAX_PATH,
 1.31576 +                             0);
 1.31577 +#else
 1.31578 +    LPWSTR zTempWide = NULL;
 1.31579 +    dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
 1.31580 +                             FORMAT_MESSAGE_FROM_SYSTEM |
 1.31581 +                             FORMAT_MESSAGE_IGNORE_INSERTS,
 1.31582 +                             NULL,
 1.31583 +                             lastErrno,
 1.31584 +                             0,
 1.31585 +                             (LPWSTR) &zTempWide,
 1.31586 +                             0,
 1.31587 +                             0);
 1.31588 +#endif
 1.31589 +    if( dwLen > 0 ){
 1.31590 +      /* allocate a buffer and convert to UTF8 */
 1.31591 +      sqlite3BeginBenignMalloc();
 1.31592 +      zOut = unicodeToUtf8(zTempWide);
 1.31593 +      sqlite3EndBenignMalloc();
 1.31594 +#if !SQLITE_OS_WINRT
 1.31595 +      /* free the system buffer allocated by FormatMessage */
 1.31596 +      osLocalFree(zTempWide);
 1.31597 +#endif
 1.31598 +    }
 1.31599 +  }
 1.31600 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.31601 +  else{
 1.31602 +    char *zTemp = NULL;
 1.31603 +    dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
 1.31604 +                             FORMAT_MESSAGE_FROM_SYSTEM |
 1.31605 +                             FORMAT_MESSAGE_IGNORE_INSERTS,
 1.31606 +                             NULL,
 1.31607 +                             lastErrno,
 1.31608 +                             0,
 1.31609 +                             (LPSTR) &zTemp,
 1.31610 +                             0,
 1.31611 +                             0);
 1.31612 +    if( dwLen > 0 ){
 1.31613 +      /* allocate a buffer and convert to UTF8 */
 1.31614 +      sqlite3BeginBenignMalloc();
 1.31615 +      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
 1.31616 +      sqlite3EndBenignMalloc();
 1.31617 +      /* free the system buffer allocated by FormatMessage */
 1.31618 +      osLocalFree(zTemp);
 1.31619 +    }
 1.31620 +  }
 1.31621 +#endif
 1.31622 +  if( 0 == dwLen ){
 1.31623 +    sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
 1.31624 +  }else{
 1.31625 +    /* copy a maximum of nBuf chars to output buffer */
 1.31626 +    sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
 1.31627 +    /* free the UTF8 buffer */
 1.31628 +    sqlite3_free(zOut);
 1.31629 +  }
 1.31630 +  return 0;
 1.31631 +}
 1.31632 +
 1.31633 +/*
 1.31634 +**
 1.31635 +** This function - winLogErrorAtLine() - is only ever called via the macro
 1.31636 +** winLogError().
 1.31637 +**
 1.31638 +** This routine is invoked after an error occurs in an OS function.
 1.31639 +** It logs a message using sqlite3_log() containing the current value of
 1.31640 +** error code and, if possible, the human-readable equivalent from 
 1.31641 +** FormatMessage.
 1.31642 +**
 1.31643 +** The first argument passed to the macro should be the error code that
 1.31644 +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
 1.31645 +** The two subsequent arguments should be the name of the OS function that
 1.31646 +** failed and the associated file-system path, if any.
 1.31647 +*/
 1.31648 +#define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
 1.31649 +static int winLogErrorAtLine(
 1.31650 +  int errcode,                    /* SQLite error code */
 1.31651 +  DWORD lastErrno,                /* Win32 last error */
 1.31652 +  const char *zFunc,              /* Name of OS function that failed */
 1.31653 +  const char *zPath,              /* File path associated with error */
 1.31654 +  int iLine                       /* Source line number where error occurred */
 1.31655 +){
 1.31656 +  char zMsg[500];                 /* Human readable error text */
 1.31657 +  int i;                          /* Loop counter */
 1.31658 +
 1.31659 +  zMsg[0] = 0;
 1.31660 +  getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
 1.31661 +  assert( errcode!=SQLITE_OK );
 1.31662 +  if( zPath==0 ) zPath = "";
 1.31663 +  for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
 1.31664 +  zMsg[i] = 0;
 1.31665 +  sqlite3_log(errcode,
 1.31666 +      "os_win.c:%d: (%d) %s(%s) - %s",
 1.31667 +      iLine, lastErrno, zFunc, zPath, zMsg
 1.31668 +  );
 1.31669 +
 1.31670 +  return errcode;
 1.31671 +}
 1.31672 +
 1.31673 +/*
 1.31674 +** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
 1.31675 +** will be retried following a locking error - probably caused by 
 1.31676 +** antivirus software.  Also the initial delay before the first retry.
 1.31677 +** The delay increases linearly with each retry.
 1.31678 +*/
 1.31679 +#ifndef SQLITE_WIN32_IOERR_RETRY
 1.31680 +# define SQLITE_WIN32_IOERR_RETRY 10
 1.31681 +#endif
 1.31682 +#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
 1.31683 +# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
 1.31684 +#endif
 1.31685 +static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
 1.31686 +static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
 1.31687 +
 1.31688 +/*
 1.31689 +** If a ReadFile() or WriteFile() error occurs, invoke this routine
 1.31690 +** to see if it should be retried.  Return TRUE to retry.  Return FALSE
 1.31691 +** to give up with an error.
 1.31692 +*/
 1.31693 +static int retryIoerr(int *pnRetry, DWORD *pError){
 1.31694 +  DWORD e = osGetLastError();
 1.31695 +  if( *pnRetry>=win32IoerrRetry ){
 1.31696 +    if( pError ){
 1.31697 +      *pError = e;
 1.31698 +    }
 1.31699 +    return 0;
 1.31700 +  }
 1.31701 +  if( e==ERROR_ACCESS_DENIED ||
 1.31702 +      e==ERROR_LOCK_VIOLATION ||
 1.31703 +      e==ERROR_SHARING_VIOLATION ){
 1.31704 +    sqlite3_win32_sleep(win32IoerrRetryDelay*(1+*pnRetry));
 1.31705 +    ++*pnRetry;
 1.31706 +    return 1;
 1.31707 +  }
 1.31708 +  if( pError ){
 1.31709 +    *pError = e;
 1.31710 +  }
 1.31711 +  return 0;
 1.31712 +}
 1.31713 +
 1.31714 +/*
 1.31715 +** Log a I/O error retry episode.
 1.31716 +*/
 1.31717 +static void logIoerr(int nRetry){
 1.31718 +  if( nRetry ){
 1.31719 +    sqlite3_log(SQLITE_IOERR, 
 1.31720 +      "delayed %dms for lock/sharing conflict",
 1.31721 +      win32IoerrRetryDelay*nRetry*(nRetry+1)/2
 1.31722 +    );
 1.31723 +  }
 1.31724 +}
 1.31725 +
 1.31726 +#if SQLITE_OS_WINCE
 1.31727 +/*************************************************************************
 1.31728 +** This section contains code for WinCE only.
 1.31729 +*/
 1.31730 +/*
 1.31731 +** Windows CE does not have a localtime() function.  So create a
 1.31732 +** substitute.
 1.31733 +*/
 1.31734 +/* #include <time.h> */
 1.31735 +struct tm *__cdecl localtime(const time_t *t)
 1.31736 +{
 1.31737 +  static struct tm y;
 1.31738 +  FILETIME uTm, lTm;
 1.31739 +  SYSTEMTIME pTm;
 1.31740 +  sqlite3_int64 t64;
 1.31741 +  t64 = *t;
 1.31742 +  t64 = (t64 + 11644473600)*10000000;
 1.31743 +  uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
 1.31744 +  uTm.dwHighDateTime= (DWORD)(t64 >> 32);
 1.31745 +  osFileTimeToLocalFileTime(&uTm,&lTm);
 1.31746 +  osFileTimeToSystemTime(&lTm,&pTm);
 1.31747 +  y.tm_year = pTm.wYear - 1900;
 1.31748 +  y.tm_mon = pTm.wMonth - 1;
 1.31749 +  y.tm_wday = pTm.wDayOfWeek;
 1.31750 +  y.tm_mday = pTm.wDay;
 1.31751 +  y.tm_hour = pTm.wHour;
 1.31752 +  y.tm_min = pTm.wMinute;
 1.31753 +  y.tm_sec = pTm.wSecond;
 1.31754 +  return &y;
 1.31755 +}
 1.31756 +
 1.31757 +#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
 1.31758 +
 1.31759 +/*
 1.31760 +** Acquire a lock on the handle h
 1.31761 +*/
 1.31762 +static void winceMutexAcquire(HANDLE h){
 1.31763 +   DWORD dwErr;
 1.31764 +   do {
 1.31765 +     dwErr = osWaitForSingleObject(h, INFINITE);
 1.31766 +   } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
 1.31767 +}
 1.31768 +/*
 1.31769 +** Release a lock acquired by winceMutexAcquire()
 1.31770 +*/
 1.31771 +#define winceMutexRelease(h) ReleaseMutex(h)
 1.31772 +
 1.31773 +/*
 1.31774 +** Create the mutex and shared memory used for locking in the file
 1.31775 +** descriptor pFile
 1.31776 +*/
 1.31777 +static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
 1.31778 +  LPWSTR zTok;
 1.31779 +  LPWSTR zName;
 1.31780 +  BOOL bInit = TRUE;
 1.31781 +
 1.31782 +  zName = utf8ToUnicode(zFilename);
 1.31783 +  if( zName==0 ){
 1.31784 +    /* out of memory */
 1.31785 +    return FALSE;
 1.31786 +  }
 1.31787 +
 1.31788 +  /* Initialize the local lockdata */
 1.31789 +  memset(&pFile->local, 0, sizeof(pFile->local));
 1.31790 +
 1.31791 +  /* Replace the backslashes from the filename and lowercase it
 1.31792 +  ** to derive a mutex name. */
 1.31793 +  zTok = osCharLowerW(zName);
 1.31794 +  for (;*zTok;zTok++){
 1.31795 +    if (*zTok == '\\') *zTok = '_';
 1.31796 +  }
 1.31797 +
 1.31798 +  /* Create/open the named mutex */
 1.31799 +  pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
 1.31800 +  if (!pFile->hMutex){
 1.31801 +    pFile->lastErrno = osGetLastError();
 1.31802 +    winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
 1.31803 +    sqlite3_free(zName);
 1.31804 +    return FALSE;
 1.31805 +  }
 1.31806 +
 1.31807 +  /* Acquire the mutex before continuing */
 1.31808 +  winceMutexAcquire(pFile->hMutex);
 1.31809 +  
 1.31810 +  /* Since the names of named mutexes, semaphores, file mappings etc are 
 1.31811 +  ** case-sensitive, take advantage of that by uppercasing the mutex name
 1.31812 +  ** and using that as the shared filemapping name.
 1.31813 +  */
 1.31814 +  osCharUpperW(zName);
 1.31815 +  pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
 1.31816 +                                        PAGE_READWRITE, 0, sizeof(winceLock),
 1.31817 +                                        zName);  
 1.31818 +
 1.31819 +  /* Set a flag that indicates we're the first to create the memory so it 
 1.31820 +  ** must be zero-initialized */
 1.31821 +  if (osGetLastError() == ERROR_ALREADY_EXISTS){
 1.31822 +    bInit = FALSE;
 1.31823 +  }
 1.31824 +
 1.31825 +  sqlite3_free(zName);
 1.31826 +
 1.31827 +  /* If we succeeded in making the shared memory handle, map it. */
 1.31828 +  if (pFile->hShared){
 1.31829 +    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
 1.31830 +             FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
 1.31831 +    /* If mapping failed, close the shared memory handle and erase it */
 1.31832 +    if (!pFile->shared){
 1.31833 +      pFile->lastErrno = osGetLastError();
 1.31834 +      winLogError(SQLITE_ERROR, pFile->lastErrno,
 1.31835 +               "winceCreateLock2", zFilename);
 1.31836 +      osCloseHandle(pFile->hShared);
 1.31837 +      pFile->hShared = NULL;
 1.31838 +    }
 1.31839 +  }
 1.31840 +
 1.31841 +  /* If shared memory could not be created, then close the mutex and fail */
 1.31842 +  if (pFile->hShared == NULL){
 1.31843 +    winceMutexRelease(pFile->hMutex);
 1.31844 +    osCloseHandle(pFile->hMutex);
 1.31845 +    pFile->hMutex = NULL;
 1.31846 +    return FALSE;
 1.31847 +  }
 1.31848 +  
 1.31849 +  /* Initialize the shared memory if we're supposed to */
 1.31850 +  if (bInit) {
 1.31851 +    memset(pFile->shared, 0, sizeof(winceLock));
 1.31852 +  }
 1.31853 +
 1.31854 +  winceMutexRelease(pFile->hMutex);
 1.31855 +  return TRUE;
 1.31856 +}
 1.31857 +
 1.31858 +/*
 1.31859 +** Destroy the part of winFile that deals with wince locks
 1.31860 +*/
 1.31861 +static void winceDestroyLock(winFile *pFile){
 1.31862 +  if (pFile->hMutex){
 1.31863 +    /* Acquire the mutex */
 1.31864 +    winceMutexAcquire(pFile->hMutex);
 1.31865 +
 1.31866 +    /* The following blocks should probably assert in debug mode, but they
 1.31867 +       are to cleanup in case any locks remained open */
 1.31868 +    if (pFile->local.nReaders){
 1.31869 +      pFile->shared->nReaders --;
 1.31870 +    }
 1.31871 +    if (pFile->local.bReserved){
 1.31872 +      pFile->shared->bReserved = FALSE;
 1.31873 +    }
 1.31874 +    if (pFile->local.bPending){
 1.31875 +      pFile->shared->bPending = FALSE;
 1.31876 +    }
 1.31877 +    if (pFile->local.bExclusive){
 1.31878 +      pFile->shared->bExclusive = FALSE;
 1.31879 +    }
 1.31880 +
 1.31881 +    /* De-reference and close our copy of the shared memory handle */
 1.31882 +    osUnmapViewOfFile(pFile->shared);
 1.31883 +    osCloseHandle(pFile->hShared);
 1.31884 +
 1.31885 +    /* Done with the mutex */
 1.31886 +    winceMutexRelease(pFile->hMutex);    
 1.31887 +    osCloseHandle(pFile->hMutex);
 1.31888 +    pFile->hMutex = NULL;
 1.31889 +  }
 1.31890 +}
 1.31891 +
 1.31892 +/* 
 1.31893 +** An implementation of the LockFile() API of Windows for CE
 1.31894 +*/
 1.31895 +static BOOL winceLockFile(
 1.31896 +  LPHANDLE phFile,
 1.31897 +  DWORD dwFileOffsetLow,
 1.31898 +  DWORD dwFileOffsetHigh,
 1.31899 +  DWORD nNumberOfBytesToLockLow,
 1.31900 +  DWORD nNumberOfBytesToLockHigh
 1.31901 +){
 1.31902 +  winFile *pFile = HANDLE_TO_WINFILE(phFile);
 1.31903 +  BOOL bReturn = FALSE;
 1.31904 +
 1.31905 +  UNUSED_PARAMETER(dwFileOffsetHigh);
 1.31906 +  UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
 1.31907 +
 1.31908 +  if (!pFile->hMutex) return TRUE;
 1.31909 +  winceMutexAcquire(pFile->hMutex);
 1.31910 +
 1.31911 +  /* Wanting an exclusive lock? */
 1.31912 +  if (dwFileOffsetLow == (DWORD)SHARED_FIRST
 1.31913 +       && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
 1.31914 +    if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
 1.31915 +       pFile->shared->bExclusive = TRUE;
 1.31916 +       pFile->local.bExclusive = TRUE;
 1.31917 +       bReturn = TRUE;
 1.31918 +    }
 1.31919 +  }
 1.31920 +
 1.31921 +  /* Want a read-only lock? */
 1.31922 +  else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
 1.31923 +           nNumberOfBytesToLockLow == 1){
 1.31924 +    if (pFile->shared->bExclusive == 0){
 1.31925 +      pFile->local.nReaders ++;
 1.31926 +      if (pFile->local.nReaders == 1){
 1.31927 +        pFile->shared->nReaders ++;
 1.31928 +      }
 1.31929 +      bReturn = TRUE;
 1.31930 +    }
 1.31931 +  }
 1.31932 +
 1.31933 +  /* Want a pending lock? */
 1.31934 +  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
 1.31935 +    /* If no pending lock has been acquired, then acquire it */
 1.31936 +    if (pFile->shared->bPending == 0) {
 1.31937 +      pFile->shared->bPending = TRUE;
 1.31938 +      pFile->local.bPending = TRUE;
 1.31939 +      bReturn = TRUE;
 1.31940 +    }
 1.31941 +  }
 1.31942 +
 1.31943 +  /* Want a reserved lock? */
 1.31944 +  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
 1.31945 +    if (pFile->shared->bReserved == 0) {
 1.31946 +      pFile->shared->bReserved = TRUE;
 1.31947 +      pFile->local.bReserved = TRUE;
 1.31948 +      bReturn = TRUE;
 1.31949 +    }
 1.31950 +  }
 1.31951 +
 1.31952 +  winceMutexRelease(pFile->hMutex);
 1.31953 +  return bReturn;
 1.31954 +}
 1.31955 +
 1.31956 +/*
 1.31957 +** An implementation of the UnlockFile API of Windows for CE
 1.31958 +*/
 1.31959 +static BOOL winceUnlockFile(
 1.31960 +  LPHANDLE phFile,
 1.31961 +  DWORD dwFileOffsetLow,
 1.31962 +  DWORD dwFileOffsetHigh,
 1.31963 +  DWORD nNumberOfBytesToUnlockLow,
 1.31964 +  DWORD nNumberOfBytesToUnlockHigh
 1.31965 +){
 1.31966 +  winFile *pFile = HANDLE_TO_WINFILE(phFile);
 1.31967 +  BOOL bReturn = FALSE;
 1.31968 +
 1.31969 +  UNUSED_PARAMETER(dwFileOffsetHigh);
 1.31970 +  UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
 1.31971 +
 1.31972 +  if (!pFile->hMutex) return TRUE;
 1.31973 +  winceMutexAcquire(pFile->hMutex);
 1.31974 +
 1.31975 +  /* Releasing a reader lock or an exclusive lock */
 1.31976 +  if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
 1.31977 +    /* Did we have an exclusive lock? */
 1.31978 +    if (pFile->local.bExclusive){
 1.31979 +      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
 1.31980 +      pFile->local.bExclusive = FALSE;
 1.31981 +      pFile->shared->bExclusive = FALSE;
 1.31982 +      bReturn = TRUE;
 1.31983 +    }
 1.31984 +
 1.31985 +    /* Did we just have a reader lock? */
 1.31986 +    else if (pFile->local.nReaders){
 1.31987 +      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
 1.31988 +      pFile->local.nReaders --;
 1.31989 +      if (pFile->local.nReaders == 0)
 1.31990 +      {
 1.31991 +        pFile->shared->nReaders --;
 1.31992 +      }
 1.31993 +      bReturn = TRUE;
 1.31994 +    }
 1.31995 +  }
 1.31996 +
 1.31997 +  /* Releasing a pending lock */
 1.31998 +  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
 1.31999 +    if (pFile->local.bPending){
 1.32000 +      pFile->local.bPending = FALSE;
 1.32001 +      pFile->shared->bPending = FALSE;
 1.32002 +      bReturn = TRUE;
 1.32003 +    }
 1.32004 +  }
 1.32005 +  /* Releasing a reserved lock */
 1.32006 +  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
 1.32007 +    if (pFile->local.bReserved) {
 1.32008 +      pFile->local.bReserved = FALSE;
 1.32009 +      pFile->shared->bReserved = FALSE;
 1.32010 +      bReturn = TRUE;
 1.32011 +    }
 1.32012 +  }
 1.32013 +
 1.32014 +  winceMutexRelease(pFile->hMutex);
 1.32015 +  return bReturn;
 1.32016 +}
 1.32017 +/*
 1.32018 +** End of the special code for wince
 1.32019 +*****************************************************************************/
 1.32020 +#endif /* SQLITE_OS_WINCE */
 1.32021 +
 1.32022 +/*
 1.32023 +** Lock a file region.
 1.32024 +*/
 1.32025 +static BOOL winLockFile(
 1.32026 +  LPHANDLE phFile,
 1.32027 +  DWORD flags,
 1.32028 +  DWORD offsetLow,
 1.32029 +  DWORD offsetHigh,
 1.32030 +  DWORD numBytesLow,
 1.32031 +  DWORD numBytesHigh
 1.32032 +){
 1.32033 +#if SQLITE_OS_WINCE
 1.32034 +  /*
 1.32035 +  ** NOTE: Windows CE is handled differently here due its lack of the Win32
 1.32036 +  **       API LockFile.
 1.32037 +  */
 1.32038 +  return winceLockFile(phFile, offsetLow, offsetHigh,
 1.32039 +                       numBytesLow, numBytesHigh);
 1.32040 +#else
 1.32041 +  if( isNT() ){
 1.32042 +    OVERLAPPED ovlp;
 1.32043 +    memset(&ovlp, 0, sizeof(OVERLAPPED));
 1.32044 +    ovlp.Offset = offsetLow;
 1.32045 +    ovlp.OffsetHigh = offsetHigh;
 1.32046 +    return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
 1.32047 +  }else{
 1.32048 +    return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
 1.32049 +                      numBytesHigh);
 1.32050 +  }
 1.32051 +#endif
 1.32052 +}
 1.32053 +
 1.32054 +/*
 1.32055 +** Unlock a file region.
 1.32056 + */
 1.32057 +static BOOL winUnlockFile(
 1.32058 +  LPHANDLE phFile,
 1.32059 +  DWORD offsetLow,
 1.32060 +  DWORD offsetHigh,
 1.32061 +  DWORD numBytesLow,
 1.32062 +  DWORD numBytesHigh
 1.32063 +){
 1.32064 +#if SQLITE_OS_WINCE
 1.32065 +  /*
 1.32066 +  ** NOTE: Windows CE is handled differently here due its lack of the Win32
 1.32067 +  **       API UnlockFile.
 1.32068 +  */
 1.32069 +  return winceUnlockFile(phFile, offsetLow, offsetHigh,
 1.32070 +                         numBytesLow, numBytesHigh);
 1.32071 +#else
 1.32072 +  if( isNT() ){
 1.32073 +    OVERLAPPED ovlp;
 1.32074 +    memset(&ovlp, 0, sizeof(OVERLAPPED));
 1.32075 +    ovlp.Offset = offsetLow;
 1.32076 +    ovlp.OffsetHigh = offsetHigh;
 1.32077 +    return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
 1.32078 +  }else{
 1.32079 +    return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
 1.32080 +                        numBytesHigh);
 1.32081 +  }
 1.32082 +#endif
 1.32083 +}
 1.32084 +
 1.32085 +/*****************************************************************************
 1.32086 +** The next group of routines implement the I/O methods specified
 1.32087 +** by the sqlite3_io_methods object.
 1.32088 +******************************************************************************/
 1.32089 +
 1.32090 +/*
 1.32091 +** Some Microsoft compilers lack this definition.
 1.32092 +*/
 1.32093 +#ifndef INVALID_SET_FILE_POINTER
 1.32094 +# define INVALID_SET_FILE_POINTER ((DWORD)-1)
 1.32095 +#endif
 1.32096 +
 1.32097 +/*
 1.32098 +** Move the current position of the file handle passed as the first 
 1.32099 +** argument to offset iOffset within the file. If successful, return 0. 
 1.32100 +** Otherwise, set pFile->lastErrno and return non-zero.
 1.32101 +*/
 1.32102 +static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
 1.32103 +#if !SQLITE_OS_WINRT
 1.32104 +  LONG upperBits;                 /* Most sig. 32 bits of new offset */
 1.32105 +  LONG lowerBits;                 /* Least sig. 32 bits of new offset */
 1.32106 +  DWORD dwRet;                    /* Value returned by SetFilePointer() */
 1.32107 +  DWORD lastErrno;                /* Value returned by GetLastError() */
 1.32108 +
 1.32109 +  upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
 1.32110 +  lowerBits = (LONG)(iOffset & 0xffffffff);
 1.32111 +
 1.32112 +  /* API oddity: If successful, SetFilePointer() returns a dword 
 1.32113 +  ** containing the lower 32-bits of the new file-offset. Or, if it fails,
 1.32114 +  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
 1.32115 +  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
 1.32116 +  ** whether an error has actually occured, it is also necessary to call 
 1.32117 +  ** GetLastError().
 1.32118 +  */
 1.32119 +  dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
 1.32120 +
 1.32121 +  if( (dwRet==INVALID_SET_FILE_POINTER
 1.32122 +      && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
 1.32123 +    pFile->lastErrno = lastErrno;
 1.32124 +    winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
 1.32125 +             "seekWinFile", pFile->zPath);
 1.32126 +    return 1;
 1.32127 +  }
 1.32128 +
 1.32129 +  return 0;
 1.32130 +#else
 1.32131 +  /*
 1.32132 +  ** Same as above, except that this implementation works for WinRT.
 1.32133 +  */
 1.32134 +
 1.32135 +  LARGE_INTEGER x;                /* The new offset */
 1.32136 +  BOOL bRet;                      /* Value returned by SetFilePointerEx() */
 1.32137 +
 1.32138 +  x.QuadPart = iOffset;
 1.32139 +  bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
 1.32140 +
 1.32141 +  if(!bRet){
 1.32142 +    pFile->lastErrno = osGetLastError();
 1.32143 +    winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
 1.32144 +             "seekWinFile", pFile->zPath);
 1.32145 +    return 1;
 1.32146 +  }
 1.32147 +
 1.32148 +  return 0;
 1.32149 +#endif
 1.32150 +}
 1.32151 +
 1.32152 +/*
 1.32153 +** Close a file.
 1.32154 +**
 1.32155 +** It is reported that an attempt to close a handle might sometimes
 1.32156 +** fail.  This is a very unreasonable result, but Windows is notorious
 1.32157 +** for being unreasonable so I do not doubt that it might happen.  If
 1.32158 +** the close fails, we pause for 100 milliseconds and try again.  As
 1.32159 +** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
 1.32160 +** giving up and returning an error.
 1.32161 +*/
 1.32162 +#define MX_CLOSE_ATTEMPT 3
 1.32163 +static int winClose(sqlite3_file *id){
 1.32164 +  int rc, cnt = 0;
 1.32165 +  winFile *pFile = (winFile*)id;
 1.32166 +
 1.32167 +  assert( id!=0 );
 1.32168 +#ifndef SQLITE_OMIT_WAL
 1.32169 +  assert( pFile->pShm==0 );
 1.32170 +#endif
 1.32171 +  OSTRACE(("CLOSE %d\n", pFile->h));
 1.32172 +  do{
 1.32173 +    rc = osCloseHandle(pFile->h);
 1.32174 +    /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
 1.32175 +  }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
 1.32176 +#if SQLITE_OS_WINCE
 1.32177 +#define WINCE_DELETION_ATTEMPTS 3
 1.32178 +  winceDestroyLock(pFile);
 1.32179 +  if( pFile->zDeleteOnClose ){
 1.32180 +    int cnt = 0;
 1.32181 +    while(
 1.32182 +           osDeleteFileW(pFile->zDeleteOnClose)==0
 1.32183 +        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
 1.32184 +        && cnt++ < WINCE_DELETION_ATTEMPTS
 1.32185 +    ){
 1.32186 +       sqlite3_win32_sleep(100);  /* Wait a little before trying again */
 1.32187 +    }
 1.32188 +    sqlite3_free(pFile->zDeleteOnClose);
 1.32189 +  }
 1.32190 +#endif
 1.32191 +  OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
 1.32192 +  if( rc ){
 1.32193 +    pFile->h = NULL;
 1.32194 +  }
 1.32195 +  OpenCounter(-1);
 1.32196 +  return rc ? SQLITE_OK
 1.32197 +            : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
 1.32198 +                          "winClose", pFile->zPath);
 1.32199 +}
 1.32200 +
 1.32201 +/*
 1.32202 +** Read data from a file into a buffer.  Return SQLITE_OK if all
 1.32203 +** bytes were read successfully and SQLITE_IOERR if anything goes
 1.32204 +** wrong.
 1.32205 +*/
 1.32206 +static int winRead(
 1.32207 +  sqlite3_file *id,          /* File to read from */
 1.32208 +  void *pBuf,                /* Write content into this buffer */
 1.32209 +  int amt,                   /* Number of bytes to read */
 1.32210 +  sqlite3_int64 offset       /* Begin reading at this offset */
 1.32211 +){
 1.32212 +#if !SQLITE_OS_WINCE
 1.32213 +  OVERLAPPED overlapped;          /* The offset for ReadFile. */
 1.32214 +#endif
 1.32215 +  winFile *pFile = (winFile*)id;  /* file handle */
 1.32216 +  DWORD nRead;                    /* Number of bytes actually read from file */
 1.32217 +  int nRetry = 0;                 /* Number of retrys */
 1.32218 +
 1.32219 +  assert( id!=0 );
 1.32220 +  SimulateIOError(return SQLITE_IOERR_READ);
 1.32221 +  OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
 1.32222 +
 1.32223 +#if SQLITE_OS_WINCE
 1.32224 +  if( seekWinFile(pFile, offset) ){
 1.32225 +    return SQLITE_FULL;
 1.32226 +  }
 1.32227 +  while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
 1.32228 +#else
 1.32229 +  memset(&overlapped, 0, sizeof(OVERLAPPED));
 1.32230 +  overlapped.Offset = (LONG)(offset & 0xffffffff);
 1.32231 +  overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 1.32232 +  while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
 1.32233 +         osGetLastError()!=ERROR_HANDLE_EOF ){
 1.32234 +#endif
 1.32235 +    DWORD lastErrno;
 1.32236 +    if( retryIoerr(&nRetry, &lastErrno) ) continue;
 1.32237 +    pFile->lastErrno = lastErrno;
 1.32238 +    return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
 1.32239 +             "winRead", pFile->zPath);
 1.32240 +  }
 1.32241 +  logIoerr(nRetry);
 1.32242 +  if( nRead<(DWORD)amt ){
 1.32243 +    /* Unread parts of the buffer must be zero-filled */
 1.32244 +    memset(&((char*)pBuf)[nRead], 0, amt-nRead);
 1.32245 +    return SQLITE_IOERR_SHORT_READ;
 1.32246 +  }
 1.32247 +
 1.32248 +  return SQLITE_OK;
 1.32249 +}
 1.32250 +
 1.32251 +/*
 1.32252 +** Write data from a buffer into a file.  Return SQLITE_OK on success
 1.32253 +** or some other error code on failure.
 1.32254 +*/
 1.32255 +static int winWrite(
 1.32256 +  sqlite3_file *id,               /* File to write into */
 1.32257 +  const void *pBuf,               /* The bytes to be written */
 1.32258 +  int amt,                        /* Number of bytes to write */
 1.32259 +  sqlite3_int64 offset            /* Offset into the file to begin writing at */
 1.32260 +){
 1.32261 +  int rc = 0;                     /* True if error has occured, else false */
 1.32262 +  winFile *pFile = (winFile*)id;  /* File handle */
 1.32263 +  int nRetry = 0;                 /* Number of retries */
 1.32264 +
 1.32265 +  assert( amt>0 );
 1.32266 +  assert( pFile );
 1.32267 +  SimulateIOError(return SQLITE_IOERR_WRITE);
 1.32268 +  SimulateDiskfullError(return SQLITE_FULL);
 1.32269 +
 1.32270 +  OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
 1.32271 +
 1.32272 +#if SQLITE_OS_WINCE
 1.32273 +  rc = seekWinFile(pFile, offset);
 1.32274 +  if( rc==0 ){
 1.32275 +#else
 1.32276 +  {
 1.32277 +#endif
 1.32278 +#if !SQLITE_OS_WINCE
 1.32279 +    OVERLAPPED overlapped;        /* The offset for WriteFile. */
 1.32280 +#endif
 1.32281 +    u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
 1.32282 +    int nRem = amt;               /* Number of bytes yet to be written */
 1.32283 +    DWORD nWrite;                 /* Bytes written by each WriteFile() call */
 1.32284 +    DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
 1.32285 +
 1.32286 +#if !SQLITE_OS_WINCE
 1.32287 +    memset(&overlapped, 0, sizeof(OVERLAPPED));
 1.32288 +    overlapped.Offset = (LONG)(offset & 0xffffffff);
 1.32289 +    overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 1.32290 +#endif
 1.32291 +
 1.32292 +    while( nRem>0 ){
 1.32293 +#if SQLITE_OS_WINCE
 1.32294 +      if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
 1.32295 +#else
 1.32296 +      if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
 1.32297 +#endif
 1.32298 +        if( retryIoerr(&nRetry, &lastErrno) ) continue;
 1.32299 +        break;
 1.32300 +      }
 1.32301 +      assert( nWrite==0 || nWrite<=(DWORD)nRem );
 1.32302 +      if( nWrite==0 || nWrite>(DWORD)nRem ){
 1.32303 +        lastErrno = osGetLastError();
 1.32304 +        break;
 1.32305 +      }
 1.32306 +#if !SQLITE_OS_WINCE
 1.32307 +      offset += nWrite;
 1.32308 +      overlapped.Offset = (LONG)(offset & 0xffffffff);
 1.32309 +      overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 1.32310 +#endif
 1.32311 +      aRem += nWrite;
 1.32312 +      nRem -= nWrite;
 1.32313 +    }
 1.32314 +    if( nRem>0 ){
 1.32315 +      pFile->lastErrno = lastErrno;
 1.32316 +      rc = 1;
 1.32317 +    }
 1.32318 +  }
 1.32319 +
 1.32320 +  if( rc ){
 1.32321 +    if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
 1.32322 +       || ( pFile->lastErrno==ERROR_DISK_FULL )){
 1.32323 +      return SQLITE_FULL;
 1.32324 +    }
 1.32325 +    return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
 1.32326 +             "winWrite", pFile->zPath);
 1.32327 +  }else{
 1.32328 +    logIoerr(nRetry);
 1.32329 +  }
 1.32330 +  return SQLITE_OK;
 1.32331 +}
 1.32332 +
 1.32333 +/*
 1.32334 +** Truncate an open file to a specified size
 1.32335 +*/
 1.32336 +static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
 1.32337 +  winFile *pFile = (winFile*)id;  /* File handle object */
 1.32338 +  int rc = SQLITE_OK;             /* Return code for this function */
 1.32339 +
 1.32340 +  assert( pFile );
 1.32341 +
 1.32342 +  OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
 1.32343 +  SimulateIOError(return SQLITE_IOERR_TRUNCATE);
 1.32344 +
 1.32345 +  /* If the user has configured a chunk-size for this file, truncate the
 1.32346 +  ** file so that it consists of an integer number of chunks (i.e. the
 1.32347 +  ** actual file size after the operation may be larger than the requested
 1.32348 +  ** size).
 1.32349 +  */
 1.32350 +  if( pFile->szChunk>0 ){
 1.32351 +    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 1.32352 +  }
 1.32353 +
 1.32354 +  /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
 1.32355 +  if( seekWinFile(pFile, nByte) ){
 1.32356 +    rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
 1.32357 +             "winTruncate1", pFile->zPath);
 1.32358 +  }else if( 0==osSetEndOfFile(pFile->h) ){
 1.32359 +    pFile->lastErrno = osGetLastError();
 1.32360 +    rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
 1.32361 +             "winTruncate2", pFile->zPath);
 1.32362 +  }
 1.32363 +
 1.32364 +  OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
 1.32365 +  return rc;
 1.32366 +}
 1.32367 +
 1.32368 +#ifdef SQLITE_TEST
 1.32369 +/*
 1.32370 +** Count the number of fullsyncs and normal syncs.  This is used to test
 1.32371 +** that syncs and fullsyncs are occuring at the right times.
 1.32372 +*/
 1.32373 +SQLITE_API int sqlite3_sync_count = 0;
 1.32374 +SQLITE_API int sqlite3_fullsync_count = 0;
 1.32375 +#endif
 1.32376 +
 1.32377 +/*
 1.32378 +** Make sure all writes to a particular file are committed to disk.
 1.32379 +*/
 1.32380 +static int winSync(sqlite3_file *id, int flags){
 1.32381 +#ifndef SQLITE_NO_SYNC
 1.32382 +  /*
 1.32383 +  ** Used only when SQLITE_NO_SYNC is not defined.
 1.32384 +   */
 1.32385 +  BOOL rc;
 1.32386 +#endif
 1.32387 +#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
 1.32388 +    (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
 1.32389 +  /*
 1.32390 +  ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
 1.32391 +  ** OSTRACE() macros.
 1.32392 +   */
 1.32393 +  winFile *pFile = (winFile*)id;
 1.32394 +#else
 1.32395 +  UNUSED_PARAMETER(id);
 1.32396 +#endif
 1.32397 +
 1.32398 +  assert( pFile );
 1.32399 +  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
 1.32400 +  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
 1.32401 +      || (flags&0x0F)==SQLITE_SYNC_FULL
 1.32402 +  );
 1.32403 +
 1.32404 +  OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
 1.32405 +
 1.32406 +  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
 1.32407 +  ** line is to test that doing so does not cause any problems.
 1.32408 +  */
 1.32409 +  SimulateDiskfullError( return SQLITE_FULL );
 1.32410 +
 1.32411 +#ifndef SQLITE_TEST
 1.32412 +  UNUSED_PARAMETER(flags);
 1.32413 +#else
 1.32414 +  if( (flags&0x0F)==SQLITE_SYNC_FULL ){
 1.32415 +    sqlite3_fullsync_count++;
 1.32416 +  }
 1.32417 +  sqlite3_sync_count++;
 1.32418 +#endif
 1.32419 +
 1.32420 +  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 1.32421 +  ** no-op
 1.32422 +  */
 1.32423 +#ifdef SQLITE_NO_SYNC
 1.32424 +  return SQLITE_OK;
 1.32425 +#else
 1.32426 +  rc = osFlushFileBuffers(pFile->h);
 1.32427 +  SimulateIOError( rc=FALSE );
 1.32428 +  if( rc ){
 1.32429 +    return SQLITE_OK;
 1.32430 +  }else{
 1.32431 +    pFile->lastErrno = osGetLastError();
 1.32432 +    return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
 1.32433 +             "winSync", pFile->zPath);
 1.32434 +  }
 1.32435 +#endif
 1.32436 +}
 1.32437 +
 1.32438 +/*
 1.32439 +** Determine the current size of a file in bytes
 1.32440 +*/
 1.32441 +static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
 1.32442 +  winFile *pFile = (winFile*)id;
 1.32443 +  int rc = SQLITE_OK;
 1.32444 +
 1.32445 +  assert( id!=0 );
 1.32446 +  SimulateIOError(return SQLITE_IOERR_FSTAT);
 1.32447 +#if SQLITE_OS_WINRT
 1.32448 +  {
 1.32449 +    FILE_STANDARD_INFO info;
 1.32450 +    if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
 1.32451 +                                     &info, sizeof(info)) ){
 1.32452 +      *pSize = info.EndOfFile.QuadPart;
 1.32453 +    }else{
 1.32454 +      pFile->lastErrno = osGetLastError();
 1.32455 +      rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
 1.32456 +                       "winFileSize", pFile->zPath);
 1.32457 +    }
 1.32458 +  }
 1.32459 +#else
 1.32460 +  {
 1.32461 +    DWORD upperBits;
 1.32462 +    DWORD lowerBits;
 1.32463 +    DWORD lastErrno;
 1.32464 +
 1.32465 +    lowerBits = osGetFileSize(pFile->h, &upperBits);
 1.32466 +    *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
 1.32467 +    if(   (lowerBits == INVALID_FILE_SIZE)
 1.32468 +       && ((lastErrno = osGetLastError())!=NO_ERROR) ){
 1.32469 +      pFile->lastErrno = lastErrno;
 1.32470 +      rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
 1.32471 +             "winFileSize", pFile->zPath);
 1.32472 +    }
 1.32473 +  }
 1.32474 +#endif
 1.32475 +  return rc;
 1.32476 +}
 1.32477 +
 1.32478 +/*
 1.32479 +** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
 1.32480 +*/
 1.32481 +#ifndef LOCKFILE_FAIL_IMMEDIATELY
 1.32482 +# define LOCKFILE_FAIL_IMMEDIATELY 1
 1.32483 +#endif
 1.32484 +
 1.32485 +#ifndef LOCKFILE_EXCLUSIVE_LOCK
 1.32486 +# define LOCKFILE_EXCLUSIVE_LOCK 2
 1.32487 +#endif
 1.32488 +
 1.32489 +/*
 1.32490 +** Historically, SQLite has used both the LockFile and LockFileEx functions.
 1.32491 +** When the LockFile function was used, it was always expected to fail
 1.32492 +** immediately if the lock could not be obtained.  Also, it always expected to
 1.32493 +** obtain an exclusive lock.  These flags are used with the LockFileEx function
 1.32494 +** and reflect those expectations; therefore, they should not be changed.
 1.32495 +*/
 1.32496 +#ifndef SQLITE_LOCKFILE_FLAGS
 1.32497 +# define SQLITE_LOCKFILE_FLAGS   (LOCKFILE_FAIL_IMMEDIATELY | \
 1.32498 +                                  LOCKFILE_EXCLUSIVE_LOCK)
 1.32499 +#endif
 1.32500 +
 1.32501 +/*
 1.32502 +** Currently, SQLite never calls the LockFileEx function without wanting the
 1.32503 +** call to fail immediately if the lock cannot be obtained.
 1.32504 +*/
 1.32505 +#ifndef SQLITE_LOCKFILEEX_FLAGS
 1.32506 +# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
 1.32507 +#endif
 1.32508 +
 1.32509 +/*
 1.32510 +** Acquire a reader lock.
 1.32511 +** Different API routines are called depending on whether or not this
 1.32512 +** is Win9x or WinNT.
 1.32513 +*/
 1.32514 +static int getReadLock(winFile *pFile){
 1.32515 +  int res;
 1.32516 +  if( isNT() ){
 1.32517 +#if SQLITE_OS_WINCE
 1.32518 +    /*
 1.32519 +    ** NOTE: Windows CE is handled differently here due its lack of the Win32
 1.32520 +    **       API LockFileEx.
 1.32521 +    */
 1.32522 +    res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
 1.32523 +#else
 1.32524 +    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
 1.32525 +                      SHARED_SIZE, 0);
 1.32526 +#endif
 1.32527 +  }
 1.32528 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.32529 +  else{
 1.32530 +    int lk;
 1.32531 +    sqlite3_randomness(sizeof(lk), &lk);
 1.32532 +    pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
 1.32533 +    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
 1.32534 +                      SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
 1.32535 +  }
 1.32536 +#endif
 1.32537 +  if( res == 0 ){
 1.32538 +    pFile->lastErrno = osGetLastError();
 1.32539 +    /* No need to log a failure to lock */
 1.32540 +  }
 1.32541 +  return res;
 1.32542 +}
 1.32543 +
 1.32544 +/*
 1.32545 +** Undo a readlock
 1.32546 +*/
 1.32547 +static int unlockReadLock(winFile *pFile){
 1.32548 +  int res;
 1.32549 +  DWORD lastErrno;
 1.32550 +  if( isNT() ){
 1.32551 +    res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
 1.32552 +  }
 1.32553 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.32554 +  else{
 1.32555 +    res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
 1.32556 +  }
 1.32557 +#endif
 1.32558 +  if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
 1.32559 +    pFile->lastErrno = lastErrno;
 1.32560 +    winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
 1.32561 +             "unlockReadLock", pFile->zPath);
 1.32562 +  }
 1.32563 +  return res;
 1.32564 +}
 1.32565 +
 1.32566 +/*
 1.32567 +** Lock the file with the lock specified by parameter locktype - one
 1.32568 +** of the following:
 1.32569 +**
 1.32570 +**     (1) SHARED_LOCK
 1.32571 +**     (2) RESERVED_LOCK
 1.32572 +**     (3) PENDING_LOCK
 1.32573 +**     (4) EXCLUSIVE_LOCK
 1.32574 +**
 1.32575 +** Sometimes when requesting one lock state, additional lock states
 1.32576 +** are inserted in between.  The locking might fail on one of the later
 1.32577 +** transitions leaving the lock state different from what it started but
 1.32578 +** still short of its goal.  The following chart shows the allowed
 1.32579 +** transitions and the inserted intermediate states:
 1.32580 +**
 1.32581 +**    UNLOCKED -> SHARED
 1.32582 +**    SHARED -> RESERVED
 1.32583 +**    SHARED -> (PENDING) -> EXCLUSIVE
 1.32584 +**    RESERVED -> (PENDING) -> EXCLUSIVE
 1.32585 +**    PENDING -> EXCLUSIVE
 1.32586 +**
 1.32587 +** This routine will only increase a lock.  The winUnlock() routine
 1.32588 +** erases all locks at once and returns us immediately to locking level 0.
 1.32589 +** It is not possible to lower the locking level one step at a time.  You
 1.32590 +** must go straight to locking level 0.
 1.32591 +*/
 1.32592 +static int winLock(sqlite3_file *id, int locktype){
 1.32593 +  int rc = SQLITE_OK;    /* Return code from subroutines */
 1.32594 +  int res = 1;           /* Result of a Windows lock call */
 1.32595 +  int newLocktype;       /* Set pFile->locktype to this value before exiting */
 1.32596 +  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
 1.32597 +  winFile *pFile = (winFile*)id;
 1.32598 +  DWORD lastErrno = NO_ERROR;
 1.32599 +
 1.32600 +  assert( id!=0 );
 1.32601 +  OSTRACE(("LOCK %d %d was %d(%d)\n",
 1.32602 +           pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
 1.32603 +
 1.32604 +  /* If there is already a lock of this type or more restrictive on the
 1.32605 +  ** OsFile, do nothing. Don't use the end_lock: exit path, as
 1.32606 +  ** sqlite3OsEnterMutex() hasn't been called yet.
 1.32607 +  */
 1.32608 +  if( pFile->locktype>=locktype ){
 1.32609 +    return SQLITE_OK;
 1.32610 +  }
 1.32611 +
 1.32612 +  /* Make sure the locking sequence is correct
 1.32613 +  */
 1.32614 +  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
 1.32615 +  assert( locktype!=PENDING_LOCK );
 1.32616 +  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
 1.32617 +
 1.32618 +  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
 1.32619 +  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
 1.32620 +  ** the PENDING_LOCK byte is temporary.
 1.32621 +  */
 1.32622 +  newLocktype = pFile->locktype;
 1.32623 +  if(   (pFile->locktype==NO_LOCK)
 1.32624 +     || (   (locktype==EXCLUSIVE_LOCK)
 1.32625 +         && (pFile->locktype==RESERVED_LOCK))
 1.32626 +  ){
 1.32627 +    int cnt = 3;
 1.32628 +    while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
 1.32629 +                                         PENDING_BYTE, 0, 1, 0))==0 ){
 1.32630 +      /* Try 3 times to get the pending lock.  This is needed to work
 1.32631 +      ** around problems caused by indexing and/or anti-virus software on
 1.32632 +      ** Windows systems.
 1.32633 +      ** If you are using this code as a model for alternative VFSes, do not
 1.32634 +      ** copy this retry logic.  It is a hack intended for Windows only.
 1.32635 +      */
 1.32636 +      OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
 1.32637 +      if( cnt ) sqlite3_win32_sleep(1);
 1.32638 +    }
 1.32639 +    gotPendingLock = res;
 1.32640 +    if( !res ){
 1.32641 +      lastErrno = osGetLastError();
 1.32642 +    }
 1.32643 +  }
 1.32644 +
 1.32645 +  /* Acquire a shared lock
 1.32646 +  */
 1.32647 +  if( locktype==SHARED_LOCK && res ){
 1.32648 +    assert( pFile->locktype==NO_LOCK );
 1.32649 +    res = getReadLock(pFile);
 1.32650 +    if( res ){
 1.32651 +      newLocktype = SHARED_LOCK;
 1.32652 +    }else{
 1.32653 +      lastErrno = osGetLastError();
 1.32654 +    }
 1.32655 +  }
 1.32656 +
 1.32657 +  /* Acquire a RESERVED lock
 1.32658 +  */
 1.32659 +  if( locktype==RESERVED_LOCK && res ){
 1.32660 +    assert( pFile->locktype==SHARED_LOCK );
 1.32661 +    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
 1.32662 +    if( res ){
 1.32663 +      newLocktype = RESERVED_LOCK;
 1.32664 +    }else{
 1.32665 +      lastErrno = osGetLastError();
 1.32666 +    }
 1.32667 +  }
 1.32668 +
 1.32669 +  /* Acquire a PENDING lock
 1.32670 +  */
 1.32671 +  if( locktype==EXCLUSIVE_LOCK && res ){
 1.32672 +    newLocktype = PENDING_LOCK;
 1.32673 +    gotPendingLock = 0;
 1.32674 +  }
 1.32675 +
 1.32676 +  /* Acquire an EXCLUSIVE lock
 1.32677 +  */
 1.32678 +  if( locktype==EXCLUSIVE_LOCK && res ){
 1.32679 +    assert( pFile->locktype>=SHARED_LOCK );
 1.32680 +    res = unlockReadLock(pFile);
 1.32681 +    OSTRACE(("unreadlock = %d\n", res));
 1.32682 +    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
 1.32683 +                      SHARED_SIZE, 0);
 1.32684 +    if( res ){
 1.32685 +      newLocktype = EXCLUSIVE_LOCK;
 1.32686 +    }else{
 1.32687 +      lastErrno = osGetLastError();
 1.32688 +      OSTRACE(("error-code = %d\n", lastErrno));
 1.32689 +      getReadLock(pFile);
 1.32690 +    }
 1.32691 +  }
 1.32692 +
 1.32693 +  /* If we are holding a PENDING lock that ought to be released, then
 1.32694 +  ** release it now.
 1.32695 +  */
 1.32696 +  if( gotPendingLock && locktype==SHARED_LOCK ){
 1.32697 +    winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
 1.32698 +  }
 1.32699 +
 1.32700 +  /* Update the state of the lock has held in the file descriptor then
 1.32701 +  ** return the appropriate result code.
 1.32702 +  */
 1.32703 +  if( res ){
 1.32704 +    rc = SQLITE_OK;
 1.32705 +  }else{
 1.32706 +    OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
 1.32707 +           locktype, newLocktype));
 1.32708 +    pFile->lastErrno = lastErrno;
 1.32709 +    rc = SQLITE_BUSY;
 1.32710 +  }
 1.32711 +  pFile->locktype = (u8)newLocktype;
 1.32712 +  return rc;
 1.32713 +}
 1.32714 +
 1.32715 +/*
 1.32716 +** This routine checks if there is a RESERVED lock held on the specified
 1.32717 +** file by this or any other process. If such a lock is held, return
 1.32718 +** non-zero, otherwise zero.
 1.32719 +*/
 1.32720 +static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
 1.32721 +  int rc;
 1.32722 +  winFile *pFile = (winFile*)id;
 1.32723 +
 1.32724 +  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 1.32725 +
 1.32726 +  assert( id!=0 );
 1.32727 +  if( pFile->locktype>=RESERVED_LOCK ){
 1.32728 +    rc = 1;
 1.32729 +    OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
 1.32730 +  }else{
 1.32731 +    rc = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
 1.32732 +    if( rc ){
 1.32733 +      winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
 1.32734 +    }
 1.32735 +    rc = !rc;
 1.32736 +    OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
 1.32737 +  }
 1.32738 +  *pResOut = rc;
 1.32739 +  return SQLITE_OK;
 1.32740 +}
 1.32741 +
 1.32742 +/*
 1.32743 +** Lower the locking level on file descriptor id to locktype.  locktype
 1.32744 +** must be either NO_LOCK or SHARED_LOCK.
 1.32745 +**
 1.32746 +** If the locking level of the file descriptor is already at or below
 1.32747 +** the requested locking level, this routine is a no-op.
 1.32748 +**
 1.32749 +** It is not possible for this routine to fail if the second argument
 1.32750 +** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
 1.32751 +** might return SQLITE_IOERR;
 1.32752 +*/
 1.32753 +static int winUnlock(sqlite3_file *id, int locktype){
 1.32754 +  int type;
 1.32755 +  winFile *pFile = (winFile*)id;
 1.32756 +  int rc = SQLITE_OK;
 1.32757 +  assert( pFile!=0 );
 1.32758 +  assert( locktype<=SHARED_LOCK );
 1.32759 +  OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
 1.32760 +          pFile->locktype, pFile->sharedLockByte));
 1.32761 +  type = pFile->locktype;
 1.32762 +  if( type>=EXCLUSIVE_LOCK ){
 1.32763 +    winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
 1.32764 +    if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
 1.32765 +      /* This should never happen.  We should always be able to
 1.32766 +      ** reacquire the read lock */
 1.32767 +      rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
 1.32768 +               "winUnlock", pFile->zPath);
 1.32769 +    }
 1.32770 +  }
 1.32771 +  if( type>=RESERVED_LOCK ){
 1.32772 +    winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
 1.32773 +  }
 1.32774 +  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
 1.32775 +    unlockReadLock(pFile);
 1.32776 +  }
 1.32777 +  if( type>=PENDING_LOCK ){
 1.32778 +    winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
 1.32779 +  }
 1.32780 +  pFile->locktype = (u8)locktype;
 1.32781 +  return rc;
 1.32782 +}
 1.32783 +
 1.32784 +/*
 1.32785 +** If *pArg is inititially negative then this is a query.  Set *pArg to
 1.32786 +** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
 1.32787 +**
 1.32788 +** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
 1.32789 +*/
 1.32790 +static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
 1.32791 +  if( *pArg<0 ){
 1.32792 +    *pArg = (pFile->ctrlFlags & mask)!=0;
 1.32793 +  }else if( (*pArg)==0 ){
 1.32794 +    pFile->ctrlFlags &= ~mask;
 1.32795 +  }else{
 1.32796 +    pFile->ctrlFlags |= mask;
 1.32797 +  }
 1.32798 +}
 1.32799 +
 1.32800 +/* Forward declaration */
 1.32801 +static int getTempname(int nBuf, char *zBuf);
 1.32802 +
 1.32803 +/*
 1.32804 +** Control and query of the open file handle.
 1.32805 +*/
 1.32806 +static int winFileControl(sqlite3_file *id, int op, void *pArg){
 1.32807 +  winFile *pFile = (winFile*)id;
 1.32808 +  switch( op ){
 1.32809 +    case SQLITE_FCNTL_LOCKSTATE: {
 1.32810 +      *(int*)pArg = pFile->locktype;
 1.32811 +      return SQLITE_OK;
 1.32812 +    }
 1.32813 +    case SQLITE_LAST_ERRNO: {
 1.32814 +      *(int*)pArg = (int)pFile->lastErrno;
 1.32815 +      return SQLITE_OK;
 1.32816 +    }
 1.32817 +    case SQLITE_FCNTL_CHUNK_SIZE: {
 1.32818 +      pFile->szChunk = *(int *)pArg;
 1.32819 +      return SQLITE_OK;
 1.32820 +    }
 1.32821 +    case SQLITE_FCNTL_SIZE_HINT: {
 1.32822 +      if( pFile->szChunk>0 ){
 1.32823 +        sqlite3_int64 oldSz;
 1.32824 +        int rc = winFileSize(id, &oldSz);
 1.32825 +        if( rc==SQLITE_OK ){
 1.32826 +          sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
 1.32827 +          if( newSz>oldSz ){
 1.32828 +            SimulateIOErrorBenign(1);
 1.32829 +            rc = winTruncate(id, newSz);
 1.32830 +            SimulateIOErrorBenign(0);
 1.32831 +          }
 1.32832 +        }
 1.32833 +        return rc;
 1.32834 +      }
 1.32835 +      return SQLITE_OK;
 1.32836 +    }
 1.32837 +    case SQLITE_FCNTL_PERSIST_WAL: {
 1.32838 +      winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
 1.32839 +      return SQLITE_OK;
 1.32840 +    }
 1.32841 +    case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
 1.32842 +      winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
 1.32843 +      return SQLITE_OK;
 1.32844 +    }
 1.32845 +    case SQLITE_FCNTL_VFSNAME: {
 1.32846 +      *(char**)pArg = sqlite3_mprintf("win32");
 1.32847 +      return SQLITE_OK;
 1.32848 +    }
 1.32849 +    case SQLITE_FCNTL_WIN32_AV_RETRY: {
 1.32850 +      int *a = (int*)pArg;
 1.32851 +      if( a[0]>0 ){
 1.32852 +        win32IoerrRetry = a[0];
 1.32853 +      }else{
 1.32854 +        a[0] = win32IoerrRetry;
 1.32855 +      }
 1.32856 +      if( a[1]>0 ){
 1.32857 +        win32IoerrRetryDelay = a[1];
 1.32858 +      }else{
 1.32859 +        a[1] = win32IoerrRetryDelay;
 1.32860 +      }
 1.32861 +      return SQLITE_OK;
 1.32862 +    }
 1.32863 +    case SQLITE_FCNTL_TEMPFILENAME: {
 1.32864 +      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
 1.32865 +      if( zTFile ){
 1.32866 +        getTempname(pFile->pVfs->mxPathname, zTFile);
 1.32867 +        *(char**)pArg = zTFile;
 1.32868 +      }
 1.32869 +      return SQLITE_OK;
 1.32870 +    }
 1.32871 +  }
 1.32872 +  return SQLITE_NOTFOUND;
 1.32873 +}
 1.32874 +
 1.32875 +/*
 1.32876 +** Return the sector size in bytes of the underlying block device for
 1.32877 +** the specified file. This is almost always 512 bytes, but may be
 1.32878 +** larger for some devices.
 1.32879 +**
 1.32880 +** SQLite code assumes this function cannot fail. It also assumes that
 1.32881 +** if two files are created in the same file-system directory (i.e.
 1.32882 +** a database and its journal file) that the sector size will be the
 1.32883 +** same for both.
 1.32884 +*/
 1.32885 +static int winSectorSize(sqlite3_file *id){
 1.32886 +  (void)id;
 1.32887 +  return SQLITE_DEFAULT_SECTOR_SIZE;
 1.32888 +}
 1.32889 +
 1.32890 +/*
 1.32891 +** Return a vector of device characteristics.
 1.32892 +*/
 1.32893 +static int winDeviceCharacteristics(sqlite3_file *id){
 1.32894 +  winFile *p = (winFile*)id;
 1.32895 +  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
 1.32896 +         ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
 1.32897 +}
 1.32898 +
 1.32899 +#ifndef SQLITE_OMIT_WAL
 1.32900 +
 1.32901 +/* 
 1.32902 +** Windows will only let you create file view mappings
 1.32903 +** on allocation size granularity boundaries.
 1.32904 +** During sqlite3_os_init() we do a GetSystemInfo()
 1.32905 +** to get the granularity size.
 1.32906 +*/
 1.32907 +SYSTEM_INFO winSysInfo;
 1.32908 +
 1.32909 +/*
 1.32910 +** Helper functions to obtain and relinquish the global mutex. The
 1.32911 +** global mutex is used to protect the winLockInfo objects used by 
 1.32912 +** this file, all of which may be shared by multiple threads.
 1.32913 +**
 1.32914 +** Function winShmMutexHeld() is used to assert() that the global mutex 
 1.32915 +** is held when required. This function is only used as part of assert() 
 1.32916 +** statements. e.g.
 1.32917 +**
 1.32918 +**   winShmEnterMutex()
 1.32919 +**     assert( winShmMutexHeld() );
 1.32920 +**   winShmLeaveMutex()
 1.32921 +*/
 1.32922 +static void winShmEnterMutex(void){
 1.32923 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.32924 +}
 1.32925 +static void winShmLeaveMutex(void){
 1.32926 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.32927 +}
 1.32928 +#ifdef SQLITE_DEBUG
 1.32929 +static int winShmMutexHeld(void) {
 1.32930 +  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 1.32931 +}
 1.32932 +#endif
 1.32933 +
 1.32934 +/*
 1.32935 +** Object used to represent a single file opened and mmapped to provide
 1.32936 +** shared memory.  When multiple threads all reference the same
 1.32937 +** log-summary, each thread has its own winFile object, but they all
 1.32938 +** point to a single instance of this object.  In other words, each
 1.32939 +** log-summary is opened only once per process.
 1.32940 +**
 1.32941 +** winShmMutexHeld() must be true when creating or destroying
 1.32942 +** this object or while reading or writing the following fields:
 1.32943 +**
 1.32944 +**      nRef
 1.32945 +**      pNext 
 1.32946 +**
 1.32947 +** The following fields are read-only after the object is created:
 1.32948 +** 
 1.32949 +**      fid
 1.32950 +**      zFilename
 1.32951 +**
 1.32952 +** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
 1.32953 +** winShmMutexHeld() is true when reading or writing any other field
 1.32954 +** in this structure.
 1.32955 +**
 1.32956 +*/
 1.32957 +struct winShmNode {
 1.32958 +  sqlite3_mutex *mutex;      /* Mutex to access this object */
 1.32959 +  char *zFilename;           /* Name of the file */
 1.32960 +  winFile hFile;             /* File handle from winOpen */
 1.32961 +
 1.32962 +  int szRegion;              /* Size of shared-memory regions */
 1.32963 +  int nRegion;               /* Size of array apRegion */
 1.32964 +  struct ShmRegion {
 1.32965 +    HANDLE hMap;             /* File handle from CreateFileMapping */
 1.32966 +    void *pMap;
 1.32967 +  } *aRegion;
 1.32968 +  DWORD lastErrno;           /* The Windows errno from the last I/O error */
 1.32969 +
 1.32970 +  int nRef;                  /* Number of winShm objects pointing to this */
 1.32971 +  winShm *pFirst;            /* All winShm objects pointing to this */
 1.32972 +  winShmNode *pNext;         /* Next in list of all winShmNode objects */
 1.32973 +#ifdef SQLITE_DEBUG
 1.32974 +  u8 nextShmId;              /* Next available winShm.id value */
 1.32975 +#endif
 1.32976 +};
 1.32977 +
 1.32978 +/*
 1.32979 +** A global array of all winShmNode objects.
 1.32980 +**
 1.32981 +** The winShmMutexHeld() must be true while reading or writing this list.
 1.32982 +*/
 1.32983 +static winShmNode *winShmNodeList = 0;
 1.32984 +
 1.32985 +/*
 1.32986 +** Structure used internally by this VFS to record the state of an
 1.32987 +** open shared memory connection.
 1.32988 +**
 1.32989 +** The following fields are initialized when this object is created and
 1.32990 +** are read-only thereafter:
 1.32991 +**
 1.32992 +**    winShm.pShmNode
 1.32993 +**    winShm.id
 1.32994 +**
 1.32995 +** All other fields are read/write.  The winShm.pShmNode->mutex must be held
 1.32996 +** while accessing any read/write fields.
 1.32997 +*/
 1.32998 +struct winShm {
 1.32999 +  winShmNode *pShmNode;      /* The underlying winShmNode object */
 1.33000 +  winShm *pNext;             /* Next winShm with the same winShmNode */
 1.33001 +  u8 hasMutex;               /* True if holding the winShmNode mutex */
 1.33002 +  u16 sharedMask;            /* Mask of shared locks held */
 1.33003 +  u16 exclMask;              /* Mask of exclusive locks held */
 1.33004 +#ifdef SQLITE_DEBUG
 1.33005 +  u8 id;                     /* Id of this connection with its winShmNode */
 1.33006 +#endif
 1.33007 +};
 1.33008 +
 1.33009 +/*
 1.33010 +** Constants used for locking
 1.33011 +*/
 1.33012 +#define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
 1.33013 +#define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 1.33014 +
 1.33015 +/*
 1.33016 +** Apply advisory locks for all n bytes beginning at ofst.
 1.33017 +*/
 1.33018 +#define _SHM_UNLCK  1
 1.33019 +#define _SHM_RDLCK  2
 1.33020 +#define _SHM_WRLCK  3
 1.33021 +static int winShmSystemLock(
 1.33022 +  winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
 1.33023 +  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
 1.33024 +  int ofst,             /* Offset to first byte to be locked/unlocked */
 1.33025 +  int nByte             /* Number of bytes to lock or unlock */
 1.33026 +){
 1.33027 +  int rc = 0;           /* Result code form Lock/UnlockFileEx() */
 1.33028 +
 1.33029 +  /* Access to the winShmNode object is serialized by the caller */
 1.33030 +  assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
 1.33031 +
 1.33032 +  /* Release/Acquire the system-level lock */
 1.33033 +  if( lockType==_SHM_UNLCK ){
 1.33034 +    rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
 1.33035 +  }else{
 1.33036 +    /* Initialize the locking parameters */
 1.33037 +    DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
 1.33038 +    if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
 1.33039 +    rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
 1.33040 +  }
 1.33041 +  
 1.33042 +  if( rc!= 0 ){
 1.33043 +    rc = SQLITE_OK;
 1.33044 +  }else{
 1.33045 +    pFile->lastErrno =  osGetLastError();
 1.33046 +    rc = SQLITE_BUSY;
 1.33047 +  }
 1.33048 +
 1.33049 +  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
 1.33050 +           pFile->hFile.h,
 1.33051 +           rc==SQLITE_OK ? "ok" : "failed",
 1.33052 +           lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
 1.33053 +           pFile->lastErrno));
 1.33054 +
 1.33055 +  return rc;
 1.33056 +}
 1.33057 +
 1.33058 +/* Forward references to VFS methods */
 1.33059 +static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
 1.33060 +static int winDelete(sqlite3_vfs *,const char*,int);
 1.33061 +
 1.33062 +/*
 1.33063 +** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
 1.33064 +**
 1.33065 +** This is not a VFS shared-memory method; it is a utility function called
 1.33066 +** by VFS shared-memory methods.
 1.33067 +*/
 1.33068 +static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
 1.33069 +  winShmNode **pp;
 1.33070 +  winShmNode *p;
 1.33071 +  BOOL bRc;
 1.33072 +  assert( winShmMutexHeld() );
 1.33073 +  pp = &winShmNodeList;
 1.33074 +  while( (p = *pp)!=0 ){
 1.33075 +    if( p->nRef==0 ){
 1.33076 +      int i;
 1.33077 +      if( p->mutex ) sqlite3_mutex_free(p->mutex);
 1.33078 +      for(i=0; i<p->nRegion; i++){
 1.33079 +        bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
 1.33080 +        OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
 1.33081 +                 (int)osGetCurrentProcessId(), i,
 1.33082 +                 bRc ? "ok" : "failed"));
 1.33083 +        bRc = osCloseHandle(p->aRegion[i].hMap);
 1.33084 +        OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
 1.33085 +                 (int)osGetCurrentProcessId(), i,
 1.33086 +                 bRc ? "ok" : "failed"));
 1.33087 +      }
 1.33088 +      if( p->hFile.h != INVALID_HANDLE_VALUE ){
 1.33089 +        SimulateIOErrorBenign(1);
 1.33090 +        winClose((sqlite3_file *)&p->hFile);
 1.33091 +        SimulateIOErrorBenign(0);
 1.33092 +      }
 1.33093 +      if( deleteFlag ){
 1.33094 +        SimulateIOErrorBenign(1);
 1.33095 +        sqlite3BeginBenignMalloc();
 1.33096 +        winDelete(pVfs, p->zFilename, 0);
 1.33097 +        sqlite3EndBenignMalloc();
 1.33098 +        SimulateIOErrorBenign(0);
 1.33099 +      }
 1.33100 +      *pp = p->pNext;
 1.33101 +      sqlite3_free(p->aRegion);
 1.33102 +      sqlite3_free(p);
 1.33103 +    }else{
 1.33104 +      pp = &p->pNext;
 1.33105 +    }
 1.33106 +  }
 1.33107 +}
 1.33108 +
 1.33109 +/*
 1.33110 +** Open the shared-memory area associated with database file pDbFd.
 1.33111 +**
 1.33112 +** When opening a new shared-memory file, if no other instances of that
 1.33113 +** file are currently open, in this process or in other processes, then
 1.33114 +** the file must be truncated to zero length or have its header cleared.
 1.33115 +*/
 1.33116 +static int winOpenSharedMemory(winFile *pDbFd){
 1.33117 +  struct winShm *p;                  /* The connection to be opened */
 1.33118 +  struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
 1.33119 +  int rc;                            /* Result code */
 1.33120 +  struct winShmNode *pNew;           /* Newly allocated winShmNode */
 1.33121 +  int nName;                         /* Size of zName in bytes */
 1.33122 +
 1.33123 +  assert( pDbFd->pShm==0 );    /* Not previously opened */
 1.33124 +
 1.33125 +  /* Allocate space for the new sqlite3_shm object.  Also speculatively
 1.33126 +  ** allocate space for a new winShmNode and filename.
 1.33127 +  */
 1.33128 +  p = sqlite3MallocZero( sizeof(*p) );
 1.33129 +  if( p==0 ) return SQLITE_IOERR_NOMEM;
 1.33130 +  nName = sqlite3Strlen30(pDbFd->zPath);
 1.33131 +  pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
 1.33132 +  if( pNew==0 ){
 1.33133 +    sqlite3_free(p);
 1.33134 +    return SQLITE_IOERR_NOMEM;
 1.33135 +  }
 1.33136 +  pNew->zFilename = (char*)&pNew[1];
 1.33137 +  sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
 1.33138 +  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
 1.33139 +
 1.33140 +  /* Look to see if there is an existing winShmNode that can be used.
 1.33141 +  ** If no matching winShmNode currently exists, create a new one.
 1.33142 +  */
 1.33143 +  winShmEnterMutex();
 1.33144 +  for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
 1.33145 +    /* TBD need to come up with better match here.  Perhaps
 1.33146 +    ** use FILE_ID_BOTH_DIR_INFO Structure.
 1.33147 +    */
 1.33148 +    if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
 1.33149 +  }
 1.33150 +  if( pShmNode ){
 1.33151 +    sqlite3_free(pNew);
 1.33152 +  }else{
 1.33153 +    pShmNode = pNew;
 1.33154 +    pNew = 0;
 1.33155 +    ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
 1.33156 +    pShmNode->pNext = winShmNodeList;
 1.33157 +    winShmNodeList = pShmNode;
 1.33158 +
 1.33159 +    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 1.33160 +    if( pShmNode->mutex==0 ){
 1.33161 +      rc = SQLITE_IOERR_NOMEM;
 1.33162 +      goto shm_open_err;
 1.33163 +    }
 1.33164 +
 1.33165 +    rc = winOpen(pDbFd->pVfs,
 1.33166 +                 pShmNode->zFilename,             /* Name of the file (UTF-8) */
 1.33167 +                 (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
 1.33168 +                 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
 1.33169 +                 0);
 1.33170 +    if( SQLITE_OK!=rc ){
 1.33171 +      goto shm_open_err;
 1.33172 +    }
 1.33173 +
 1.33174 +    /* Check to see if another process is holding the dead-man switch.
 1.33175 +    ** If not, truncate the file to zero length. 
 1.33176 +    */
 1.33177 +    if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
 1.33178 +      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
 1.33179 +      if( rc!=SQLITE_OK ){
 1.33180 +        rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
 1.33181 +                 "winOpenShm", pDbFd->zPath);
 1.33182 +      }
 1.33183 +    }
 1.33184 +    if( rc==SQLITE_OK ){
 1.33185 +      winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
 1.33186 +      rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
 1.33187 +    }
 1.33188 +    if( rc ) goto shm_open_err;
 1.33189 +  }
 1.33190 +
 1.33191 +  /* Make the new connection a child of the winShmNode */
 1.33192 +  p->pShmNode = pShmNode;
 1.33193 +#ifdef SQLITE_DEBUG
 1.33194 +  p->id = pShmNode->nextShmId++;
 1.33195 +#endif
 1.33196 +  pShmNode->nRef++;
 1.33197 +  pDbFd->pShm = p;
 1.33198 +  winShmLeaveMutex();
 1.33199 +
 1.33200 +  /* The reference count on pShmNode has already been incremented under
 1.33201 +  ** the cover of the winShmEnterMutex() mutex and the pointer from the
 1.33202 +  ** new (struct winShm) object to the pShmNode has been set. All that is
 1.33203 +  ** left to do is to link the new object into the linked list starting
 1.33204 +  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 1.33205 +  ** mutex.
 1.33206 +  */
 1.33207 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.33208 +  p->pNext = pShmNode->pFirst;
 1.33209 +  pShmNode->pFirst = p;
 1.33210 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.33211 +  return SQLITE_OK;
 1.33212 +
 1.33213 +  /* Jump here on any error */
 1.33214 +shm_open_err:
 1.33215 +  winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
 1.33216 +  winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
 1.33217 +  sqlite3_free(p);
 1.33218 +  sqlite3_free(pNew);
 1.33219 +  winShmLeaveMutex();
 1.33220 +  return rc;
 1.33221 +}
 1.33222 +
 1.33223 +/*
 1.33224 +** Close a connection to shared-memory.  Delete the underlying 
 1.33225 +** storage if deleteFlag is true.
 1.33226 +*/
 1.33227 +static int winShmUnmap(
 1.33228 +  sqlite3_file *fd,          /* Database holding shared memory */
 1.33229 +  int deleteFlag             /* Delete after closing if true */
 1.33230 +){
 1.33231 +  winFile *pDbFd;       /* Database holding shared-memory */
 1.33232 +  winShm *p;            /* The connection to be closed */
 1.33233 +  winShmNode *pShmNode; /* The underlying shared-memory file */
 1.33234 +  winShm **pp;          /* For looping over sibling connections */
 1.33235 +
 1.33236 +  pDbFd = (winFile*)fd;
 1.33237 +  p = pDbFd->pShm;
 1.33238 +  if( p==0 ) return SQLITE_OK;
 1.33239 +  pShmNode = p->pShmNode;
 1.33240 +
 1.33241 +  /* Remove connection p from the set of connections associated
 1.33242 +  ** with pShmNode */
 1.33243 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.33244 +  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 1.33245 +  *pp = p->pNext;
 1.33246 +
 1.33247 +  /* Free the connection p */
 1.33248 +  sqlite3_free(p);
 1.33249 +  pDbFd->pShm = 0;
 1.33250 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.33251 +
 1.33252 +  /* If pShmNode->nRef has reached 0, then close the underlying
 1.33253 +  ** shared-memory file, too */
 1.33254 +  winShmEnterMutex();
 1.33255 +  assert( pShmNode->nRef>0 );
 1.33256 +  pShmNode->nRef--;
 1.33257 +  if( pShmNode->nRef==0 ){
 1.33258 +    winShmPurge(pDbFd->pVfs, deleteFlag);
 1.33259 +  }
 1.33260 +  winShmLeaveMutex();
 1.33261 +
 1.33262 +  return SQLITE_OK;
 1.33263 +}
 1.33264 +
 1.33265 +/*
 1.33266 +** Change the lock state for a shared-memory segment.
 1.33267 +*/
 1.33268 +static int winShmLock(
 1.33269 +  sqlite3_file *fd,          /* Database file holding the shared memory */
 1.33270 +  int ofst,                  /* First lock to acquire or release */
 1.33271 +  int n,                     /* Number of locks to acquire or release */
 1.33272 +  int flags                  /* What to do with the lock */
 1.33273 +){
 1.33274 +  winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
 1.33275 +  winShm *p = pDbFd->pShm;              /* The shared memory being locked */
 1.33276 +  winShm *pX;                           /* For looping over all siblings */
 1.33277 +  winShmNode *pShmNode = p->pShmNode;
 1.33278 +  int rc = SQLITE_OK;                   /* Result code */
 1.33279 +  u16 mask;                             /* Mask of locks to take or release */
 1.33280 +
 1.33281 +  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 1.33282 +  assert( n>=1 );
 1.33283 +  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 1.33284 +       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 1.33285 +       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 1.33286 +       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 1.33287 +  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 1.33288 +
 1.33289 +  mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
 1.33290 +  assert( n>1 || mask==(1<<ofst) );
 1.33291 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.33292 +  if( flags & SQLITE_SHM_UNLOCK ){
 1.33293 +    u16 allMask = 0; /* Mask of locks held by siblings */
 1.33294 +
 1.33295 +    /* See if any siblings hold this same lock */
 1.33296 +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 1.33297 +      if( pX==p ) continue;
 1.33298 +      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 1.33299 +      allMask |= pX->sharedMask;
 1.33300 +    }
 1.33301 +
 1.33302 +    /* Unlock the system-level locks */
 1.33303 +    if( (mask & allMask)==0 ){
 1.33304 +      rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
 1.33305 +    }else{
 1.33306 +      rc = SQLITE_OK;
 1.33307 +    }
 1.33308 +
 1.33309 +    /* Undo the local locks */
 1.33310 +    if( rc==SQLITE_OK ){
 1.33311 +      p->exclMask &= ~mask;
 1.33312 +      p->sharedMask &= ~mask;
 1.33313 +    } 
 1.33314 +  }else if( flags & SQLITE_SHM_SHARED ){
 1.33315 +    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
 1.33316 +
 1.33317 +    /* Find out which shared locks are already held by sibling connections.
 1.33318 +    ** If any sibling already holds an exclusive lock, go ahead and return
 1.33319 +    ** SQLITE_BUSY.
 1.33320 +    */
 1.33321 +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 1.33322 +      if( (pX->exclMask & mask)!=0 ){
 1.33323 +        rc = SQLITE_BUSY;
 1.33324 +        break;
 1.33325 +      }
 1.33326 +      allShared |= pX->sharedMask;
 1.33327 +    }
 1.33328 +
 1.33329 +    /* Get shared locks at the system level, if necessary */
 1.33330 +    if( rc==SQLITE_OK ){
 1.33331 +      if( (allShared & mask)==0 ){
 1.33332 +        rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
 1.33333 +      }else{
 1.33334 +        rc = SQLITE_OK;
 1.33335 +      }
 1.33336 +    }
 1.33337 +
 1.33338 +    /* Get the local shared locks */
 1.33339 +    if( rc==SQLITE_OK ){
 1.33340 +      p->sharedMask |= mask;
 1.33341 +    }
 1.33342 +  }else{
 1.33343 +    /* Make sure no sibling connections hold locks that will block this
 1.33344 +    ** lock.  If any do, return SQLITE_BUSY right away.
 1.33345 +    */
 1.33346 +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 1.33347 +      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
 1.33348 +        rc = SQLITE_BUSY;
 1.33349 +        break;
 1.33350 +      }
 1.33351 +    }
 1.33352 +  
 1.33353 +    /* Get the exclusive locks at the system level.  Then if successful
 1.33354 +    ** also mark the local connection as being locked.
 1.33355 +    */
 1.33356 +    if( rc==SQLITE_OK ){
 1.33357 +      rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
 1.33358 +      if( rc==SQLITE_OK ){
 1.33359 +        assert( (p->sharedMask & mask)==0 );
 1.33360 +        p->exclMask |= mask;
 1.33361 +      }
 1.33362 +    }
 1.33363 +  }
 1.33364 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.33365 +  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
 1.33366 +           p->id, (int)osGetCurrentProcessId(), p->sharedMask, p->exclMask,
 1.33367 +           rc ? "failed" : "ok"));
 1.33368 +  return rc;
 1.33369 +}
 1.33370 +
 1.33371 +/*
 1.33372 +** Implement a memory barrier or memory fence on shared memory.  
 1.33373 +**
 1.33374 +** All loads and stores begun before the barrier must complete before
 1.33375 +** any load or store begun after the barrier.
 1.33376 +*/
 1.33377 +static void winShmBarrier(
 1.33378 +  sqlite3_file *fd          /* Database holding the shared memory */
 1.33379 +){
 1.33380 +  UNUSED_PARAMETER(fd);
 1.33381 +  /* MemoryBarrier(); // does not work -- do not know why not */
 1.33382 +  winShmEnterMutex();
 1.33383 +  winShmLeaveMutex();
 1.33384 +}
 1.33385 +
 1.33386 +/*
 1.33387 +** This function is called to obtain a pointer to region iRegion of the 
 1.33388 +** shared-memory associated with the database file fd. Shared-memory regions 
 1.33389 +** are numbered starting from zero. Each shared-memory region is szRegion 
 1.33390 +** bytes in size.
 1.33391 +**
 1.33392 +** If an error occurs, an error code is returned and *pp is set to NULL.
 1.33393 +**
 1.33394 +** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
 1.33395 +** region has not been allocated (by any client, including one running in a
 1.33396 +** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
 1.33397 +** isWrite is non-zero and the requested shared-memory region has not yet 
 1.33398 +** been allocated, it is allocated by this function.
 1.33399 +**
 1.33400 +** If the shared-memory region has already been allocated or is allocated by
 1.33401 +** this call as described above, then it is mapped into this processes 
 1.33402 +** address space (if it is not already), *pp is set to point to the mapped 
 1.33403 +** memory and SQLITE_OK returned.
 1.33404 +*/
 1.33405 +static int winShmMap(
 1.33406 +  sqlite3_file *fd,               /* Handle open on database file */
 1.33407 +  int iRegion,                    /* Region to retrieve */
 1.33408 +  int szRegion,                   /* Size of regions */
 1.33409 +  int isWrite,                    /* True to extend file if necessary */
 1.33410 +  void volatile **pp              /* OUT: Mapped memory */
 1.33411 +){
 1.33412 +  winFile *pDbFd = (winFile*)fd;
 1.33413 +  winShm *p = pDbFd->pShm;
 1.33414 +  winShmNode *pShmNode;
 1.33415 +  int rc = SQLITE_OK;
 1.33416 +
 1.33417 +  if( !p ){
 1.33418 +    rc = winOpenSharedMemory(pDbFd);
 1.33419 +    if( rc!=SQLITE_OK ) return rc;
 1.33420 +    p = pDbFd->pShm;
 1.33421 +  }
 1.33422 +  pShmNode = p->pShmNode;
 1.33423 +
 1.33424 +  sqlite3_mutex_enter(pShmNode->mutex);
 1.33425 +  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 1.33426 +
 1.33427 +  if( pShmNode->nRegion<=iRegion ){
 1.33428 +    struct ShmRegion *apNew;           /* New aRegion[] array */
 1.33429 +    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 1.33430 +    sqlite3_int64 sz;                  /* Current size of wal-index file */
 1.33431 +
 1.33432 +    pShmNode->szRegion = szRegion;
 1.33433 +
 1.33434 +    /* The requested region is not mapped into this processes address space.
 1.33435 +    ** Check to see if it has been allocated (i.e. if the wal-index file is
 1.33436 +    ** large enough to contain the requested region).
 1.33437 +    */
 1.33438 +    rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
 1.33439 +    if( rc!=SQLITE_OK ){
 1.33440 +      rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
 1.33441 +               "winShmMap1", pDbFd->zPath);
 1.33442 +      goto shmpage_out;
 1.33443 +    }
 1.33444 +
 1.33445 +    if( sz<nByte ){
 1.33446 +      /* The requested memory region does not exist. If isWrite is set to
 1.33447 +      ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
 1.33448 +      **
 1.33449 +      ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
 1.33450 +      ** the requested memory region.
 1.33451 +      */
 1.33452 +      if( !isWrite ) goto shmpage_out;
 1.33453 +      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
 1.33454 +      if( rc!=SQLITE_OK ){
 1.33455 +        rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
 1.33456 +                 "winShmMap2", pDbFd->zPath);
 1.33457 +        goto shmpage_out;
 1.33458 +      }
 1.33459 +    }
 1.33460 +
 1.33461 +    /* Map the requested memory region into this processes address space. */
 1.33462 +    apNew = (struct ShmRegion *)sqlite3_realloc(
 1.33463 +        pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
 1.33464 +    );
 1.33465 +    if( !apNew ){
 1.33466 +      rc = SQLITE_IOERR_NOMEM;
 1.33467 +      goto shmpage_out;
 1.33468 +    }
 1.33469 +    pShmNode->aRegion = apNew;
 1.33470 +
 1.33471 +    while( pShmNode->nRegion<=iRegion ){
 1.33472 +      HANDLE hMap = NULL;         /* file-mapping handle */
 1.33473 +      void *pMap = 0;             /* Mapped memory region */
 1.33474 +     
 1.33475 +#if SQLITE_OS_WINRT
 1.33476 +      hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
 1.33477 +          NULL, PAGE_READWRITE, nByte, NULL
 1.33478 +      );
 1.33479 +#elif defined(SQLITE_WIN32_HAS_WIDE)
 1.33480 +      hMap = osCreateFileMappingW(pShmNode->hFile.h, 
 1.33481 +          NULL, PAGE_READWRITE, 0, nByte, NULL
 1.33482 +      );
 1.33483 +#elif defined(SQLITE_WIN32_HAS_ANSI)
 1.33484 +      hMap = osCreateFileMappingA(pShmNode->hFile.h, 
 1.33485 +          NULL, PAGE_READWRITE, 0, nByte, NULL
 1.33486 +      );
 1.33487 +#endif
 1.33488 +      OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
 1.33489 +               (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
 1.33490 +               hMap ? "ok" : "failed"));
 1.33491 +      if( hMap ){
 1.33492 +        int iOffset = pShmNode->nRegion*szRegion;
 1.33493 +        int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 1.33494 +#if SQLITE_OS_WINRT
 1.33495 +        pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
 1.33496 +            iOffset - iOffsetShift, szRegion + iOffsetShift
 1.33497 +        );
 1.33498 +#else
 1.33499 +        pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
 1.33500 +            0, iOffset - iOffsetShift, szRegion + iOffsetShift
 1.33501 +        );
 1.33502 +#endif
 1.33503 +        OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
 1.33504 +                 (int)osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
 1.33505 +                 szRegion, pMap ? "ok" : "failed"));
 1.33506 +      }
 1.33507 +      if( !pMap ){
 1.33508 +        pShmNode->lastErrno = osGetLastError();
 1.33509 +        rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
 1.33510 +                 "winShmMap3", pDbFd->zPath);
 1.33511 +        if( hMap ) osCloseHandle(hMap);
 1.33512 +        goto shmpage_out;
 1.33513 +      }
 1.33514 +
 1.33515 +      pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
 1.33516 +      pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
 1.33517 +      pShmNode->nRegion++;
 1.33518 +    }
 1.33519 +  }
 1.33520 +
 1.33521 +shmpage_out:
 1.33522 +  if( pShmNode->nRegion>iRegion ){
 1.33523 +    int iOffset = iRegion*szRegion;
 1.33524 +    int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 1.33525 +    char *p = (char *)pShmNode->aRegion[iRegion].pMap;
 1.33526 +    *pp = (void *)&p[iOffsetShift];
 1.33527 +  }else{
 1.33528 +    *pp = 0;
 1.33529 +  }
 1.33530 +  sqlite3_mutex_leave(pShmNode->mutex);
 1.33531 +  return rc;
 1.33532 +}
 1.33533 +
 1.33534 +#else
 1.33535 +# define winShmMap     0
 1.33536 +# define winShmLock    0
 1.33537 +# define winShmBarrier 0
 1.33538 +# define winShmUnmap   0
 1.33539 +#endif /* #ifndef SQLITE_OMIT_WAL */
 1.33540 +
 1.33541 +/*
 1.33542 +** Here ends the implementation of all sqlite3_file methods.
 1.33543 +**
 1.33544 +********************** End sqlite3_file Methods *******************************
 1.33545 +******************************************************************************/
 1.33546 +
 1.33547 +/*
 1.33548 +** This vector defines all the methods that can operate on an
 1.33549 +** sqlite3_file for win32.
 1.33550 +*/
 1.33551 +static const sqlite3_io_methods winIoMethod = {
 1.33552 +  2,                              /* iVersion */
 1.33553 +  winClose,                       /* xClose */
 1.33554 +  winRead,                        /* xRead */
 1.33555 +  winWrite,                       /* xWrite */
 1.33556 +  winTruncate,                    /* xTruncate */
 1.33557 +  winSync,                        /* xSync */
 1.33558 +  winFileSize,                    /* xFileSize */
 1.33559 +  winLock,                        /* xLock */
 1.33560 +  winUnlock,                      /* xUnlock */
 1.33561 +  winCheckReservedLock,           /* xCheckReservedLock */
 1.33562 +  winFileControl,                 /* xFileControl */
 1.33563 +  winSectorSize,                  /* xSectorSize */
 1.33564 +  winDeviceCharacteristics,       /* xDeviceCharacteristics */
 1.33565 +  winShmMap,                      /* xShmMap */
 1.33566 +  winShmLock,                     /* xShmLock */
 1.33567 +  winShmBarrier,                  /* xShmBarrier */
 1.33568 +  winShmUnmap                     /* xShmUnmap */
 1.33569 +};
 1.33570 +
 1.33571 +/****************************************************************************
 1.33572 +**************************** sqlite3_vfs methods ****************************
 1.33573 +**
 1.33574 +** This division contains the implementation of methods on the
 1.33575 +** sqlite3_vfs object.
 1.33576 +*/
 1.33577 +
 1.33578 +/*
 1.33579 +** Convert a UTF-8 filename into whatever form the underlying
 1.33580 +** operating system wants filenames in.  Space to hold the result
 1.33581 +** is obtained from malloc and must be freed by the calling
 1.33582 +** function.
 1.33583 +*/
 1.33584 +static void *convertUtf8Filename(const char *zFilename){
 1.33585 +  void *zConverted = 0;
 1.33586 +  if( isNT() ){
 1.33587 +    zConverted = utf8ToUnicode(zFilename);
 1.33588 +  }
 1.33589 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.33590 +  else{
 1.33591 +    zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
 1.33592 +  }
 1.33593 +#endif
 1.33594 +  /* caller will handle out of memory */
 1.33595 +  return zConverted;
 1.33596 +}
 1.33597 +
 1.33598 +/*
 1.33599 +** Create a temporary file name in zBuf.  zBuf must be big enough to
 1.33600 +** hold at pVfs->mxPathname characters.
 1.33601 +*/
 1.33602 +static int getTempname(int nBuf, char *zBuf){
 1.33603 +  static char zChars[] =
 1.33604 +    "abcdefghijklmnopqrstuvwxyz"
 1.33605 +    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 1.33606 +    "0123456789";
 1.33607 +  size_t i, j;
 1.33608 +  int nTempPath;
 1.33609 +  char zTempPath[MAX_PATH+2];
 1.33610 +
 1.33611 +  /* It's odd to simulate an io-error here, but really this is just
 1.33612 +  ** using the io-error infrastructure to test that SQLite handles this
 1.33613 +  ** function failing. 
 1.33614 +  */
 1.33615 +  SimulateIOError( return SQLITE_IOERR );
 1.33616 +
 1.33617 +  memset(zTempPath, 0, MAX_PATH+2);
 1.33618 +
 1.33619 +  if( sqlite3_temp_directory ){
 1.33620 +    sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
 1.33621 +  }
 1.33622 +#if !SQLITE_OS_WINRT
 1.33623 +  else if( isNT() ){
 1.33624 +    char *zMulti;
 1.33625 +    WCHAR zWidePath[MAX_PATH];
 1.33626 +    osGetTempPathW(MAX_PATH-30, zWidePath);
 1.33627 +    zMulti = unicodeToUtf8(zWidePath);
 1.33628 +    if( zMulti ){
 1.33629 +      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
 1.33630 +      sqlite3_free(zMulti);
 1.33631 +    }else{
 1.33632 +      return SQLITE_IOERR_NOMEM;
 1.33633 +    }
 1.33634 +  }
 1.33635 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.33636 +  else{
 1.33637 +    char *zUtf8;
 1.33638 +    char zMbcsPath[MAX_PATH];
 1.33639 +    osGetTempPathA(MAX_PATH-30, zMbcsPath);
 1.33640 +    zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
 1.33641 +    if( zUtf8 ){
 1.33642 +      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
 1.33643 +      sqlite3_free(zUtf8);
 1.33644 +    }else{
 1.33645 +      return SQLITE_IOERR_NOMEM;
 1.33646 +    }
 1.33647 +  }
 1.33648 +#endif
 1.33649 +#endif
 1.33650 +
 1.33651 +  /* Check that the output buffer is large enough for the temporary file 
 1.33652 +  ** name. If it is not, return SQLITE_ERROR.
 1.33653 +  */
 1.33654 +  nTempPath = sqlite3Strlen30(zTempPath);
 1.33655 +
 1.33656 +  if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
 1.33657 +    return SQLITE_ERROR;
 1.33658 +  }
 1.33659 +
 1.33660 +  for(i=nTempPath; i>0 && zTempPath[i-1]=='\\'; i--){}
 1.33661 +  zTempPath[i] = 0;
 1.33662 +
 1.33663 +  sqlite3_snprintf(nBuf-18, zBuf, (nTempPath > 0) ?
 1.33664 +                       "%s\\"SQLITE_TEMP_FILE_PREFIX : SQLITE_TEMP_FILE_PREFIX,
 1.33665 +                   zTempPath);
 1.33666 +  j = sqlite3Strlen30(zBuf);
 1.33667 +  sqlite3_randomness(15, &zBuf[j]);
 1.33668 +  for(i=0; i<15; i++, j++){
 1.33669 +    zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 1.33670 +  }
 1.33671 +  zBuf[j] = 0;
 1.33672 +  zBuf[j+1] = 0;
 1.33673 +
 1.33674 +  OSTRACE(("TEMP FILENAME: %s\n", zBuf));
 1.33675 +  return SQLITE_OK; 
 1.33676 +}
 1.33677 +
 1.33678 +/*
 1.33679 +** Return TRUE if the named file is really a directory.  Return false if
 1.33680 +** it is something other than a directory, or if there is any kind of memory
 1.33681 +** allocation failure.
 1.33682 +*/
 1.33683 +static int winIsDir(const void *zConverted){
 1.33684 +  DWORD attr;
 1.33685 +  int rc = 0;
 1.33686 +  DWORD lastErrno;
 1.33687 +
 1.33688 +  if( isNT() ){
 1.33689 +    int cnt = 0;
 1.33690 +    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 1.33691 +    memset(&sAttrData, 0, sizeof(sAttrData));
 1.33692 +    while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
 1.33693 +                             GetFileExInfoStandard,
 1.33694 +                             &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
 1.33695 +    if( !rc ){
 1.33696 +      return 0; /* Invalid name? */
 1.33697 +    }
 1.33698 +    attr = sAttrData.dwFileAttributes;
 1.33699 +#if SQLITE_OS_WINCE==0
 1.33700 +  }else{
 1.33701 +    attr = osGetFileAttributesA((char*)zConverted);
 1.33702 +#endif
 1.33703 +  }
 1.33704 +  return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
 1.33705 +}
 1.33706 +
 1.33707 +/*
 1.33708 +** Open a file.
 1.33709 +*/
 1.33710 +static int winOpen(
 1.33711 +  sqlite3_vfs *pVfs,        /* Not used */
 1.33712 +  const char *zName,        /* Name of the file (UTF-8) */
 1.33713 +  sqlite3_file *id,         /* Write the SQLite file handle here */
 1.33714 +  int flags,                /* Open mode flags */
 1.33715 +  int *pOutFlags            /* Status return flags */
 1.33716 +){
 1.33717 +  HANDLE h;
 1.33718 +  DWORD lastErrno;
 1.33719 +  DWORD dwDesiredAccess;
 1.33720 +  DWORD dwShareMode;
 1.33721 +  DWORD dwCreationDisposition;
 1.33722 +  DWORD dwFlagsAndAttributes = 0;
 1.33723 +#if SQLITE_OS_WINCE
 1.33724 +  int isTemp = 0;
 1.33725 +#endif
 1.33726 +  winFile *pFile = (winFile*)id;
 1.33727 +  void *zConverted;              /* Filename in OS encoding */
 1.33728 +  const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
 1.33729 +  int cnt = 0;
 1.33730 +
 1.33731 +  /* If argument zPath is a NULL pointer, this function is required to open
 1.33732 +  ** a temporary file. Use this buffer to store the file name in.
 1.33733 +  */
 1.33734 +  char zTmpname[MAX_PATH+2];     /* Buffer used to create temp filename */
 1.33735 +
 1.33736 +  int rc = SQLITE_OK;            /* Function Return Code */
 1.33737 +#if !defined(NDEBUG) || SQLITE_OS_WINCE
 1.33738 +  int eType = flags&0xFFFFFF00;  /* Type of file to open */
 1.33739 +#endif
 1.33740 +
 1.33741 +  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
 1.33742 +  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
 1.33743 +  int isCreate     = (flags & SQLITE_OPEN_CREATE);
 1.33744 +#ifndef NDEBUG
 1.33745 +  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
 1.33746 +#endif
 1.33747 +  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
 1.33748 +
 1.33749 +#ifndef NDEBUG
 1.33750 +  int isOpenJournal = (isCreate && (
 1.33751 +        eType==SQLITE_OPEN_MASTER_JOURNAL 
 1.33752 +     || eType==SQLITE_OPEN_MAIN_JOURNAL 
 1.33753 +     || eType==SQLITE_OPEN_WAL
 1.33754 +  ));
 1.33755 +#endif
 1.33756 +
 1.33757 +  /* Check the following statements are true: 
 1.33758 +  **
 1.33759 +  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
 1.33760 +  **   (b) if CREATE is set, then READWRITE must also be set, and
 1.33761 +  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
 1.33762 +  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
 1.33763 +  */
 1.33764 +  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
 1.33765 +  assert(isCreate==0 || isReadWrite);
 1.33766 +  assert(isExclusive==0 || isCreate);
 1.33767 +  assert(isDelete==0 || isCreate);
 1.33768 +
 1.33769 +  /* The main DB, main journal, WAL file and master journal are never 
 1.33770 +  ** automatically deleted. Nor are they ever temporary files.  */
 1.33771 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
 1.33772 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
 1.33773 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
 1.33774 +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
 1.33775 +
 1.33776 +  /* Assert that the upper layer has set one of the "file-type" flags. */
 1.33777 +  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
 1.33778 +       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
 1.33779 +       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
 1.33780 +       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
 1.33781 +  );
 1.33782 +
 1.33783 +  assert( id!=0 );
 1.33784 +  UNUSED_PARAMETER(pVfs);
 1.33785 +
 1.33786 +#if SQLITE_OS_WINRT
 1.33787 +  if( !sqlite3_temp_directory ){
 1.33788 +    sqlite3_log(SQLITE_ERROR,
 1.33789 +        "sqlite3_temp_directory variable should be set for WinRT");
 1.33790 +  }
 1.33791 +#endif
 1.33792 +
 1.33793 +  pFile->h = INVALID_HANDLE_VALUE;
 1.33794 +
 1.33795 +  /* If the second argument to this function is NULL, generate a 
 1.33796 +  ** temporary file name to use 
 1.33797 +  */
 1.33798 +  if( !zUtf8Name ){
 1.33799 +    assert(isDelete && !isOpenJournal);
 1.33800 +    rc = getTempname(MAX_PATH+2, zTmpname);
 1.33801 +    if( rc!=SQLITE_OK ){
 1.33802 +      return rc;
 1.33803 +    }
 1.33804 +    zUtf8Name = zTmpname;
 1.33805 +  }
 1.33806 +
 1.33807 +  /* Database filenames are double-zero terminated if they are not
 1.33808 +  ** URIs with parameters.  Hence, they can always be passed into
 1.33809 +  ** sqlite3_uri_parameter().
 1.33810 +  */
 1.33811 +  assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
 1.33812 +        zUtf8Name[strlen(zUtf8Name)+1]==0 );
 1.33813 +
 1.33814 +  /* Convert the filename to the system encoding. */
 1.33815 +  zConverted = convertUtf8Filename(zUtf8Name);
 1.33816 +  if( zConverted==0 ){
 1.33817 +    return SQLITE_IOERR_NOMEM;
 1.33818 +  }
 1.33819 +
 1.33820 +  if( winIsDir(zConverted) ){
 1.33821 +    sqlite3_free(zConverted);
 1.33822 +    return SQLITE_CANTOPEN_ISDIR;
 1.33823 +  }
 1.33824 +
 1.33825 +  if( isReadWrite ){
 1.33826 +    dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
 1.33827 +  }else{
 1.33828 +    dwDesiredAccess = GENERIC_READ;
 1.33829 +  }
 1.33830 +
 1.33831 +  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
 1.33832 +  ** created. SQLite doesn't use it to indicate "exclusive access" 
 1.33833 +  ** as it is usually understood.
 1.33834 +  */
 1.33835 +  if( isExclusive ){
 1.33836 +    /* Creates a new file, only if it does not already exist. */
 1.33837 +    /* If the file exists, it fails. */
 1.33838 +    dwCreationDisposition = CREATE_NEW;
 1.33839 +  }else if( isCreate ){
 1.33840 +    /* Open existing file, or create if it doesn't exist */
 1.33841 +    dwCreationDisposition = OPEN_ALWAYS;
 1.33842 +  }else{
 1.33843 +    /* Opens a file, only if it exists. */
 1.33844 +    dwCreationDisposition = OPEN_EXISTING;
 1.33845 +  }
 1.33846 +
 1.33847 +  dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
 1.33848 +
 1.33849 +  if( isDelete ){
 1.33850 +#if SQLITE_OS_WINCE
 1.33851 +    dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
 1.33852 +    isTemp = 1;
 1.33853 +#else
 1.33854 +    dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
 1.33855 +                               | FILE_ATTRIBUTE_HIDDEN
 1.33856 +                               | FILE_FLAG_DELETE_ON_CLOSE;
 1.33857 +#endif
 1.33858 +  }else{
 1.33859 +    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
 1.33860 +  }
 1.33861 +  /* Reports from the internet are that performance is always
 1.33862 +  ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
 1.33863 +#if SQLITE_OS_WINCE
 1.33864 +  dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
 1.33865 +#endif
 1.33866 +
 1.33867 +  if( isNT() ){
 1.33868 +#if SQLITE_OS_WINRT
 1.33869 +    CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
 1.33870 +    extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
 1.33871 +    extendedParameters.dwFileAttributes =
 1.33872 +            dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
 1.33873 +    extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
 1.33874 +    extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
 1.33875 +    extendedParameters.lpSecurityAttributes = NULL;
 1.33876 +    extendedParameters.hTemplateFile = NULL;
 1.33877 +    while( (h = osCreateFile2((LPCWSTR)zConverted,
 1.33878 +                              dwDesiredAccess,
 1.33879 +                              dwShareMode,
 1.33880 +                              dwCreationDisposition,
 1.33881 +                              &extendedParameters))==INVALID_HANDLE_VALUE &&
 1.33882 +                              retryIoerr(&cnt, &lastErrno) ){
 1.33883 +               /* Noop */
 1.33884 +    }
 1.33885 +#else
 1.33886 +    while( (h = osCreateFileW((LPCWSTR)zConverted,
 1.33887 +                              dwDesiredAccess,
 1.33888 +                              dwShareMode, NULL,
 1.33889 +                              dwCreationDisposition,
 1.33890 +                              dwFlagsAndAttributes,
 1.33891 +                              NULL))==INVALID_HANDLE_VALUE &&
 1.33892 +                              retryIoerr(&cnt, &lastErrno) ){
 1.33893 +               /* Noop */
 1.33894 +    }
 1.33895 +#endif
 1.33896 +  }
 1.33897 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.33898 +  else{
 1.33899 +    while( (h = osCreateFileA((LPCSTR)zConverted,
 1.33900 +                              dwDesiredAccess,
 1.33901 +                              dwShareMode, NULL,
 1.33902 +                              dwCreationDisposition,
 1.33903 +                              dwFlagsAndAttributes,
 1.33904 +                              NULL))==INVALID_HANDLE_VALUE &&
 1.33905 +                              retryIoerr(&cnt, &lastErrno) ){
 1.33906 +               /* Noop */
 1.33907 +    }
 1.33908 +  }
 1.33909 +#endif
 1.33910 +  logIoerr(cnt);
 1.33911 +
 1.33912 +  OSTRACE(("OPEN %d %s 0x%lx %s\n", 
 1.33913 +           h, zName, dwDesiredAccess, 
 1.33914 +           h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
 1.33915 +
 1.33916 +  if( h==INVALID_HANDLE_VALUE ){
 1.33917 +    pFile->lastErrno = lastErrno;
 1.33918 +    winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
 1.33919 +    sqlite3_free(zConverted);
 1.33920 +    if( isReadWrite && !isExclusive ){
 1.33921 +      return winOpen(pVfs, zName, id, 
 1.33922 +             ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
 1.33923 +    }else{
 1.33924 +      return SQLITE_CANTOPEN_BKPT;
 1.33925 +    }
 1.33926 +  }
 1.33927 +
 1.33928 +  if( pOutFlags ){
 1.33929 +    if( isReadWrite ){
 1.33930 +      *pOutFlags = SQLITE_OPEN_READWRITE;
 1.33931 +    }else{
 1.33932 +      *pOutFlags = SQLITE_OPEN_READONLY;
 1.33933 +    }
 1.33934 +  }
 1.33935 +
 1.33936 +  memset(pFile, 0, sizeof(*pFile));
 1.33937 +  pFile->pMethod = &winIoMethod;
 1.33938 +  pFile->h = h;
 1.33939 +  pFile->lastErrno = NO_ERROR;
 1.33940 +  pFile->pVfs = pVfs;
 1.33941 +#ifndef SQLITE_OMIT_WAL
 1.33942 +  pFile->pShm = 0;
 1.33943 +#endif
 1.33944 +  pFile->zPath = zName;
 1.33945 +  if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 1.33946 +    pFile->ctrlFlags |= WINFILE_PSOW;
 1.33947 +  }
 1.33948 +
 1.33949 +#if SQLITE_OS_WINCE
 1.33950 +  if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
 1.33951 +       && !winceCreateLock(zName, pFile)
 1.33952 +  ){
 1.33953 +    osCloseHandle(h);
 1.33954 +    sqlite3_free(zConverted);
 1.33955 +    return SQLITE_CANTOPEN_BKPT;
 1.33956 +  }
 1.33957 +  if( isTemp ){
 1.33958 +    pFile->zDeleteOnClose = zConverted;
 1.33959 +  }else
 1.33960 +#endif
 1.33961 +  {
 1.33962 +    sqlite3_free(zConverted);
 1.33963 +  }
 1.33964 +
 1.33965 +  OpenCounter(+1);
 1.33966 +  return rc;
 1.33967 +}
 1.33968 +
 1.33969 +/*
 1.33970 +** Delete the named file.
 1.33971 +**
 1.33972 +** Note that Windows does not allow a file to be deleted if some other
 1.33973 +** process has it open.  Sometimes a virus scanner or indexing program
 1.33974 +** will open a journal file shortly after it is created in order to do
 1.33975 +** whatever it does.  While this other process is holding the
 1.33976 +** file open, we will be unable to delete it.  To work around this
 1.33977 +** problem, we delay 100 milliseconds and try to delete again.  Up
 1.33978 +** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
 1.33979 +** up and returning an error.
 1.33980 +*/
 1.33981 +static int winDelete(
 1.33982 +  sqlite3_vfs *pVfs,          /* Not used on win32 */
 1.33983 +  const char *zFilename,      /* Name of file to delete */
 1.33984 +  int syncDir                 /* Not used on win32 */
 1.33985 +){
 1.33986 +  int cnt = 0;
 1.33987 +  int rc;
 1.33988 +  DWORD attr;
 1.33989 +  DWORD lastErrno;
 1.33990 +  void *zConverted;
 1.33991 +  UNUSED_PARAMETER(pVfs);
 1.33992 +  UNUSED_PARAMETER(syncDir);
 1.33993 +
 1.33994 +  SimulateIOError(return SQLITE_IOERR_DELETE);
 1.33995 +  zConverted = convertUtf8Filename(zFilename);
 1.33996 +  if( zConverted==0 ){
 1.33997 +    return SQLITE_IOERR_NOMEM;
 1.33998 +  }
 1.33999 +  if( isNT() ){
 1.34000 +    do {
 1.34001 +#if SQLITE_OS_WINRT
 1.34002 +      WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 1.34003 +      memset(&sAttrData, 0, sizeof(sAttrData));
 1.34004 +      if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
 1.34005 +                                  &sAttrData) ){
 1.34006 +        attr = sAttrData.dwFileAttributes;
 1.34007 +      }else{
 1.34008 +        lastErrno = osGetLastError();
 1.34009 +        if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
 1.34010 +          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 1.34011 +        }else{
 1.34012 +          rc = SQLITE_ERROR;
 1.34013 +        }
 1.34014 +        break;
 1.34015 +      }
 1.34016 +#else
 1.34017 +      attr = osGetFileAttributesW(zConverted);
 1.34018 +#endif
 1.34019 +      if ( attr==INVALID_FILE_ATTRIBUTES ){
 1.34020 +        lastErrno = osGetLastError();
 1.34021 +        if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
 1.34022 +          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 1.34023 +        }else{
 1.34024 +          rc = SQLITE_ERROR;
 1.34025 +        }
 1.34026 +        break;
 1.34027 +      }
 1.34028 +      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
 1.34029 +        rc = SQLITE_ERROR; /* Files only. */
 1.34030 +        break;
 1.34031 +      }
 1.34032 +      if ( osDeleteFileW(zConverted) ){
 1.34033 +        rc = SQLITE_OK; /* Deleted OK. */
 1.34034 +        break;
 1.34035 +      }
 1.34036 +      if ( !retryIoerr(&cnt, &lastErrno) ){
 1.34037 +        rc = SQLITE_ERROR; /* No more retries. */
 1.34038 +        break;
 1.34039 +      }
 1.34040 +    } while(1);
 1.34041 +  }
 1.34042 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.34043 +  else{
 1.34044 +    do {
 1.34045 +      attr = osGetFileAttributesA(zConverted);
 1.34046 +      if ( attr==INVALID_FILE_ATTRIBUTES ){
 1.34047 +        lastErrno = osGetLastError();
 1.34048 +        if( lastErrno==ERROR_FILE_NOT_FOUND || lastErrno==ERROR_PATH_NOT_FOUND ){
 1.34049 +          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 1.34050 +        }else{
 1.34051 +          rc = SQLITE_ERROR;
 1.34052 +        }
 1.34053 +        break;
 1.34054 +      }
 1.34055 +      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
 1.34056 +        rc = SQLITE_ERROR; /* Files only. */
 1.34057 +        break;
 1.34058 +      }
 1.34059 +      if ( osDeleteFileA(zConverted) ){
 1.34060 +        rc = SQLITE_OK; /* Deleted OK. */
 1.34061 +        break;
 1.34062 +      }
 1.34063 +      if ( !retryIoerr(&cnt, &lastErrno) ){
 1.34064 +        rc = SQLITE_ERROR; /* No more retries. */
 1.34065 +        break;
 1.34066 +      }
 1.34067 +    } while(1);
 1.34068 +  }
 1.34069 +#endif
 1.34070 +  if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
 1.34071 +    rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
 1.34072 +             "winDelete", zFilename);
 1.34073 +  }else{
 1.34074 +    logIoerr(cnt);
 1.34075 +  }
 1.34076 +  sqlite3_free(zConverted);
 1.34077 +  OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
 1.34078 +  return rc;
 1.34079 +}
 1.34080 +
 1.34081 +/*
 1.34082 +** Check the existance and status of a file.
 1.34083 +*/
 1.34084 +static int winAccess(
 1.34085 +  sqlite3_vfs *pVfs,         /* Not used on win32 */
 1.34086 +  const char *zFilename,     /* Name of file to check */
 1.34087 +  int flags,                 /* Type of test to make on this file */
 1.34088 +  int *pResOut               /* OUT: Result */
 1.34089 +){
 1.34090 +  DWORD attr;
 1.34091 +  int rc = 0;
 1.34092 +  DWORD lastErrno;
 1.34093 +  void *zConverted;
 1.34094 +  UNUSED_PARAMETER(pVfs);
 1.34095 +
 1.34096 +  SimulateIOError( return SQLITE_IOERR_ACCESS; );
 1.34097 +  zConverted = convertUtf8Filename(zFilename);
 1.34098 +  if( zConverted==0 ){
 1.34099 +    return SQLITE_IOERR_NOMEM;
 1.34100 +  }
 1.34101 +  if( isNT() ){
 1.34102 +    int cnt = 0;
 1.34103 +    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 1.34104 +    memset(&sAttrData, 0, sizeof(sAttrData));
 1.34105 +    while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
 1.34106 +                             GetFileExInfoStandard, 
 1.34107 +                             &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
 1.34108 +    if( rc ){
 1.34109 +      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
 1.34110 +      ** as if it does not exist.
 1.34111 +      */
 1.34112 +      if(    flags==SQLITE_ACCESS_EXISTS
 1.34113 +          && sAttrData.nFileSizeHigh==0 
 1.34114 +          && sAttrData.nFileSizeLow==0 ){
 1.34115 +        attr = INVALID_FILE_ATTRIBUTES;
 1.34116 +      }else{
 1.34117 +        attr = sAttrData.dwFileAttributes;
 1.34118 +      }
 1.34119 +    }else{
 1.34120 +      logIoerr(cnt);
 1.34121 +      if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
 1.34122 +        winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
 1.34123 +        sqlite3_free(zConverted);
 1.34124 +        return SQLITE_IOERR_ACCESS;
 1.34125 +      }else{
 1.34126 +        attr = INVALID_FILE_ATTRIBUTES;
 1.34127 +      }
 1.34128 +    }
 1.34129 +  }
 1.34130 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.34131 +  else{
 1.34132 +    attr = osGetFileAttributesA((char*)zConverted);
 1.34133 +  }
 1.34134 +#endif
 1.34135 +  sqlite3_free(zConverted);
 1.34136 +  switch( flags ){
 1.34137 +    case SQLITE_ACCESS_READ:
 1.34138 +    case SQLITE_ACCESS_EXISTS:
 1.34139 +      rc = attr!=INVALID_FILE_ATTRIBUTES;
 1.34140 +      break;
 1.34141 +    case SQLITE_ACCESS_READWRITE:
 1.34142 +      rc = attr!=INVALID_FILE_ATTRIBUTES &&
 1.34143 +             (attr & FILE_ATTRIBUTE_READONLY)==0;
 1.34144 +      break;
 1.34145 +    default:
 1.34146 +      assert(!"Invalid flags argument");
 1.34147 +  }
 1.34148 +  *pResOut = rc;
 1.34149 +  return SQLITE_OK;
 1.34150 +}
 1.34151 +
 1.34152 +
 1.34153 +/*
 1.34154 +** Returns non-zero if the specified path name should be used verbatim.  If
 1.34155 +** non-zero is returned from this function, the calling function must simply
 1.34156 +** use the provided path name verbatim -OR- resolve it into a full path name
 1.34157 +** using the GetFullPathName Win32 API function (if available).
 1.34158 +*/
 1.34159 +static BOOL winIsVerbatimPathname(
 1.34160 +  const char *zPathname
 1.34161 +){
 1.34162 +  /*
 1.34163 +  ** If the path name starts with a forward slash or a backslash, it is either
 1.34164 +  ** a legal UNC name, a volume relative path, or an absolute path name in the
 1.34165 +  ** "Unix" format on Windows.  There is no easy way to differentiate between
 1.34166 +  ** the final two cases; therefore, we return the safer return value of TRUE
 1.34167 +  ** so that callers of this function will simply use it verbatim.
 1.34168 +  */
 1.34169 +  if ( zPathname[0]=='/' || zPathname[0]=='\\' ){
 1.34170 +    return TRUE;
 1.34171 +  }
 1.34172 +
 1.34173 +  /*
 1.34174 +  ** If the path name starts with a letter and a colon it is either a volume
 1.34175 +  ** relative path or an absolute path.  Callers of this function must not
 1.34176 +  ** attempt to treat it as a relative path name (i.e. they should simply use
 1.34177 +  ** it verbatim).
 1.34178 +  */
 1.34179 +  if ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ){
 1.34180 +    return TRUE;
 1.34181 +  }
 1.34182 +
 1.34183 +  /*
 1.34184 +  ** If we get to this point, the path name should almost certainly be a purely
 1.34185 +  ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
 1.34186 +  */
 1.34187 +  return FALSE;
 1.34188 +}
 1.34189 +
 1.34190 +/*
 1.34191 +** Turn a relative pathname into a full pathname.  Write the full
 1.34192 +** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
 1.34193 +** bytes in size.
 1.34194 +*/
 1.34195 +static int winFullPathname(
 1.34196 +  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
 1.34197 +  const char *zRelative,        /* Possibly relative input path */
 1.34198 +  int nFull,                    /* Size of output buffer in bytes */
 1.34199 +  char *zFull                   /* Output buffer */
 1.34200 +){
 1.34201 +  
 1.34202 +#if defined(__CYGWIN__)
 1.34203 +  SimulateIOError( return SQLITE_ERROR );
 1.34204 +  UNUSED_PARAMETER(nFull);
 1.34205 +  assert( pVfs->mxPathname>=MAX_PATH );
 1.34206 +  assert( nFull>=pVfs->mxPathname );
 1.34207 +  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 1.34208 +    /*
 1.34209 +    ** NOTE: We are dealing with a relative path name and the data
 1.34210 +    **       directory has been set.  Therefore, use it as the basis
 1.34211 +    **       for converting the relative path name to an absolute
 1.34212 +    **       one by prepending the data directory and a slash.
 1.34213 +    */
 1.34214 +    char zOut[MAX_PATH+1];
 1.34215 +    memset(zOut, 0, MAX_PATH+1);
 1.34216 +    cygwin_conv_to_win32_path(zRelative, zOut); /* POSIX to Win32 */
 1.34217 +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
 1.34218 +                     sqlite3_data_directory, zOut);
 1.34219 +  }else{
 1.34220 +    /*
 1.34221 +    ** NOTE: The Cygwin docs state that the maximum length needed
 1.34222 +    **       for the buffer passed to cygwin_conv_to_full_win32_path
 1.34223 +    **       is MAX_PATH.
 1.34224 +    */
 1.34225 +    cygwin_conv_to_full_win32_path(zRelative, zFull);
 1.34226 +  }
 1.34227 +  return SQLITE_OK;
 1.34228 +#endif
 1.34229 +
 1.34230 +#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
 1.34231 +  SimulateIOError( return SQLITE_ERROR );
 1.34232 +  /* WinCE has no concept of a relative pathname, or so I am told. */
 1.34233 +  /* WinRT has no way to convert a relative path to an absolute one. */
 1.34234 +  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 1.34235 +    /*
 1.34236 +    ** NOTE: We are dealing with a relative path name and the data
 1.34237 +    **       directory has been set.  Therefore, use it as the basis
 1.34238 +    **       for converting the relative path name to an absolute
 1.34239 +    **       one by prepending the data directory and a backslash.
 1.34240 +    */
 1.34241 +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
 1.34242 +                     sqlite3_data_directory, zRelative);
 1.34243 +  }else{
 1.34244 +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
 1.34245 +  }
 1.34246 +  return SQLITE_OK;
 1.34247 +#endif
 1.34248 +
 1.34249 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
 1.34250 +  DWORD nByte;
 1.34251 +  void *zConverted;
 1.34252 +  char *zOut;
 1.34253 +
 1.34254 +  /* If this path name begins with "/X:", where "X" is any alphabetic
 1.34255 +  ** character, discard the initial "/" from the pathname.
 1.34256 +  */
 1.34257 +  if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
 1.34258 +    zRelative++;
 1.34259 +  }
 1.34260 +
 1.34261 +  /* It's odd to simulate an io-error here, but really this is just
 1.34262 +  ** using the io-error infrastructure to test that SQLite handles this
 1.34263 +  ** function failing. This function could fail if, for example, the
 1.34264 +  ** current working directory has been unlinked.
 1.34265 +  */
 1.34266 +  SimulateIOError( return SQLITE_ERROR );
 1.34267 +  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 1.34268 +    /*
 1.34269 +    ** NOTE: We are dealing with a relative path name and the data
 1.34270 +    **       directory has been set.  Therefore, use it as the basis
 1.34271 +    **       for converting the relative path name to an absolute
 1.34272 +    **       one by prepending the data directory and a backslash.
 1.34273 +    */
 1.34274 +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
 1.34275 +                     sqlite3_data_directory, zRelative);
 1.34276 +    return SQLITE_OK;
 1.34277 +  }
 1.34278 +  zConverted = convertUtf8Filename(zRelative);
 1.34279 +  if( zConverted==0 ){
 1.34280 +    return SQLITE_IOERR_NOMEM;
 1.34281 +  }
 1.34282 +  if( isNT() ){
 1.34283 +    LPWSTR zTemp;
 1.34284 +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
 1.34285 +    if( nByte==0 ){
 1.34286 +      winLogError(SQLITE_ERROR, osGetLastError(),
 1.34287 +                  "GetFullPathNameW1", zConverted);
 1.34288 +      sqlite3_free(zConverted);
 1.34289 +      return SQLITE_CANTOPEN_FULLPATH;
 1.34290 +    }
 1.34291 +    nByte += 3;
 1.34292 +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
 1.34293 +    if( zTemp==0 ){
 1.34294 +      sqlite3_free(zConverted);
 1.34295 +      return SQLITE_IOERR_NOMEM;
 1.34296 +    }
 1.34297 +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
 1.34298 +    if( nByte==0 ){
 1.34299 +      winLogError(SQLITE_ERROR, osGetLastError(),
 1.34300 +                  "GetFullPathNameW2", zConverted);
 1.34301 +      sqlite3_free(zConverted);
 1.34302 +      sqlite3_free(zTemp);
 1.34303 +      return SQLITE_CANTOPEN_FULLPATH;
 1.34304 +    }
 1.34305 +    sqlite3_free(zConverted);
 1.34306 +    zOut = unicodeToUtf8(zTemp);
 1.34307 +    sqlite3_free(zTemp);
 1.34308 +  }
 1.34309 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.34310 +  else{
 1.34311 +    char *zTemp;
 1.34312 +    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
 1.34313 +    if( nByte==0 ){
 1.34314 +      winLogError(SQLITE_ERROR, osGetLastError(),
 1.34315 +                  "GetFullPathNameA1", zConverted);
 1.34316 +      sqlite3_free(zConverted);
 1.34317 +      return SQLITE_CANTOPEN_FULLPATH;
 1.34318 +    }
 1.34319 +    nByte += 3;
 1.34320 +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
 1.34321 +    if( zTemp==0 ){
 1.34322 +      sqlite3_free(zConverted);
 1.34323 +      return SQLITE_IOERR_NOMEM;
 1.34324 +    }
 1.34325 +    nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
 1.34326 +    if( nByte==0 ){
 1.34327 +      winLogError(SQLITE_ERROR, osGetLastError(),
 1.34328 +                  "GetFullPathNameA2", zConverted);
 1.34329 +      sqlite3_free(zConverted);
 1.34330 +      sqlite3_free(zTemp);
 1.34331 +      return SQLITE_CANTOPEN_FULLPATH;
 1.34332 +    }
 1.34333 +    sqlite3_free(zConverted);
 1.34334 +    zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
 1.34335 +    sqlite3_free(zTemp);
 1.34336 +  }
 1.34337 +#endif
 1.34338 +  if( zOut ){
 1.34339 +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
 1.34340 +    sqlite3_free(zOut);
 1.34341 +    return SQLITE_OK;
 1.34342 +  }else{
 1.34343 +    return SQLITE_IOERR_NOMEM;
 1.34344 +  }
 1.34345 +#endif
 1.34346 +}
 1.34347 +
 1.34348 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
 1.34349 +/*
 1.34350 +** Interfaces for opening a shared library, finding entry points
 1.34351 +** within the shared library, and closing the shared library.
 1.34352 +*/
 1.34353 +/*
 1.34354 +** Interfaces for opening a shared library, finding entry points
 1.34355 +** within the shared library, and closing the shared library.
 1.34356 +*/
 1.34357 +static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
 1.34358 +  HANDLE h;
 1.34359 +  void *zConverted = convertUtf8Filename(zFilename);
 1.34360 +  UNUSED_PARAMETER(pVfs);
 1.34361 +  if( zConverted==0 ){
 1.34362 +    return 0;
 1.34363 +  }
 1.34364 +  if( isNT() ){
 1.34365 +#if SQLITE_OS_WINRT
 1.34366 +    h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
 1.34367 +#else
 1.34368 +    h = osLoadLibraryW((LPCWSTR)zConverted);
 1.34369 +#endif
 1.34370 +  }
 1.34371 +#ifdef SQLITE_WIN32_HAS_ANSI
 1.34372 +  else{
 1.34373 +    h = osLoadLibraryA((char*)zConverted);
 1.34374 +  }
 1.34375 +#endif
 1.34376 +  sqlite3_free(zConverted);
 1.34377 +  return (void*)h;
 1.34378 +}
 1.34379 +static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
 1.34380 +  UNUSED_PARAMETER(pVfs);
 1.34381 +  getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
 1.34382 +}
 1.34383 +static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
 1.34384 +  UNUSED_PARAMETER(pVfs);
 1.34385 +  return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol);
 1.34386 +}
 1.34387 +static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
 1.34388 +  UNUSED_PARAMETER(pVfs);
 1.34389 +  osFreeLibrary((HANDLE)pHandle);
 1.34390 +}
 1.34391 +#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 1.34392 +  #define winDlOpen  0
 1.34393 +  #define winDlError 0
 1.34394 +  #define winDlSym   0
 1.34395 +  #define winDlClose 0
 1.34396 +#endif
 1.34397 +
 1.34398 +
 1.34399 +/*
 1.34400 +** Write up to nBuf bytes of randomness into zBuf.
 1.34401 +*/
 1.34402 +static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 1.34403 +  int n = 0;
 1.34404 +  UNUSED_PARAMETER(pVfs);
 1.34405 +#if defined(SQLITE_TEST)
 1.34406 +  n = nBuf;
 1.34407 +  memset(zBuf, 0, nBuf);
 1.34408 +#else
 1.34409 +  if( sizeof(SYSTEMTIME)<=nBuf-n ){
 1.34410 +    SYSTEMTIME x;
 1.34411 +    osGetSystemTime(&x);
 1.34412 +    memcpy(&zBuf[n], &x, sizeof(x));
 1.34413 +    n += sizeof(x);
 1.34414 +  }
 1.34415 +  if( sizeof(DWORD)<=nBuf-n ){
 1.34416 +    DWORD pid = osGetCurrentProcessId();
 1.34417 +    memcpy(&zBuf[n], &pid, sizeof(pid));
 1.34418 +    n += sizeof(pid);
 1.34419 +  }
 1.34420 +#if SQLITE_OS_WINRT
 1.34421 +  if( sizeof(ULONGLONG)<=nBuf-n ){
 1.34422 +    ULONGLONG cnt = osGetTickCount64();
 1.34423 +    memcpy(&zBuf[n], &cnt, sizeof(cnt));
 1.34424 +    n += sizeof(cnt);
 1.34425 +  }
 1.34426 +#else
 1.34427 +  if( sizeof(DWORD)<=nBuf-n ){
 1.34428 +    DWORD cnt = osGetTickCount();
 1.34429 +    memcpy(&zBuf[n], &cnt, sizeof(cnt));
 1.34430 +    n += sizeof(cnt);
 1.34431 +  }
 1.34432 +#endif
 1.34433 +  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
 1.34434 +    LARGE_INTEGER i;
 1.34435 +    osQueryPerformanceCounter(&i);
 1.34436 +    memcpy(&zBuf[n], &i, sizeof(i));
 1.34437 +    n += sizeof(i);
 1.34438 +  }
 1.34439 +#endif
 1.34440 +  return n;
 1.34441 +}
 1.34442 +
 1.34443 +
 1.34444 +/*
 1.34445 +** Sleep for a little while.  Return the amount of time slept.
 1.34446 +*/
 1.34447 +static int winSleep(sqlite3_vfs *pVfs, int microsec){
 1.34448 +  sqlite3_win32_sleep((microsec+999)/1000);
 1.34449 +  UNUSED_PARAMETER(pVfs);
 1.34450 +  return ((microsec+999)/1000)*1000;
 1.34451 +}
 1.34452 +
 1.34453 +/*
 1.34454 +** The following variable, if set to a non-zero value, is interpreted as
 1.34455 +** the number of seconds since 1970 and is used to set the result of
 1.34456 +** sqlite3OsCurrentTime() during testing.
 1.34457 +*/
 1.34458 +#ifdef SQLITE_TEST
 1.34459 +SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
 1.34460 +#endif
 1.34461 +
 1.34462 +/*
 1.34463 +** Find the current time (in Universal Coordinated Time).  Write into *piNow
 1.34464 +** the current time and date as a Julian Day number times 86_400_000.  In
 1.34465 +** other words, write into *piNow the number of milliseconds since the Julian
 1.34466 +** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 1.34467 +** proleptic Gregorian calendar.
 1.34468 +**
 1.34469 +** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
 1.34470 +** cannot be found.
 1.34471 +*/
 1.34472 +static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
 1.34473 +  /* FILETIME structure is a 64-bit value representing the number of 
 1.34474 +     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
 1.34475 +  */
 1.34476 +  FILETIME ft;
 1.34477 +  static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
 1.34478 +#ifdef SQLITE_TEST
 1.34479 +  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 1.34480 +#endif
 1.34481 +  /* 2^32 - to avoid use of LL and warnings in gcc */
 1.34482 +  static const sqlite3_int64 max32BitValue = 
 1.34483 +      (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
 1.34484 +
 1.34485 +#if SQLITE_OS_WINCE
 1.34486 +  SYSTEMTIME time;
 1.34487 +  osGetSystemTime(&time);
 1.34488 +  /* if SystemTimeToFileTime() fails, it returns zero. */
 1.34489 +  if (!osSystemTimeToFileTime(&time,&ft)){
 1.34490 +    return SQLITE_ERROR;
 1.34491 +  }
 1.34492 +#else
 1.34493 +  osGetSystemTimeAsFileTime( &ft );
 1.34494 +#endif
 1.34495 +
 1.34496 +  *piNow = winFiletimeEpoch +
 1.34497 +            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
 1.34498 +               (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
 1.34499 +
 1.34500 +#ifdef SQLITE_TEST
 1.34501 +  if( sqlite3_current_time ){
 1.34502 +    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 1.34503 +  }
 1.34504 +#endif
 1.34505 +  UNUSED_PARAMETER(pVfs);
 1.34506 +  return SQLITE_OK;
 1.34507 +}
 1.34508 +
 1.34509 +/*
 1.34510 +** Find the current time (in Universal Coordinated Time).  Write the
 1.34511 +** current time and date as a Julian Day number into *prNow and
 1.34512 +** return 0.  Return 1 if the time and date cannot be found.
 1.34513 +*/
 1.34514 +static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
 1.34515 +  int rc;
 1.34516 +  sqlite3_int64 i;
 1.34517 +  rc = winCurrentTimeInt64(pVfs, &i);
 1.34518 +  if( !rc ){
 1.34519 +    *prNow = i/86400000.0;
 1.34520 +  }
 1.34521 +  return rc;
 1.34522 +}
 1.34523 +
 1.34524 +/*
 1.34525 +** The idea is that this function works like a combination of
 1.34526 +** GetLastError() and FormatMessage() on Windows (or errno and
 1.34527 +** strerror_r() on Unix). After an error is returned by an OS
 1.34528 +** function, SQLite calls this function with zBuf pointing to
 1.34529 +** a buffer of nBuf bytes. The OS layer should populate the
 1.34530 +** buffer with a nul-terminated UTF-8 encoded error message
 1.34531 +** describing the last IO error to have occurred within the calling
 1.34532 +** thread.
 1.34533 +**
 1.34534 +** If the error message is too large for the supplied buffer,
 1.34535 +** it should be truncated. The return value of xGetLastError
 1.34536 +** is zero if the error message fits in the buffer, or non-zero
 1.34537 +** otherwise (if the message was truncated). If non-zero is returned,
 1.34538 +** then it is not necessary to include the nul-terminator character
 1.34539 +** in the output buffer.
 1.34540 +**
 1.34541 +** Not supplying an error message will have no adverse effect
 1.34542 +** on SQLite. It is fine to have an implementation that never
 1.34543 +** returns an error message:
 1.34544 +**
 1.34545 +**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 1.34546 +**     assert(zBuf[0]=='\0');
 1.34547 +**     return 0;
 1.34548 +**   }
 1.34549 +**
 1.34550 +** However if an error message is supplied, it will be incorporated
 1.34551 +** by sqlite into the error message available to the user using
 1.34552 +** sqlite3_errmsg(), possibly making IO errors easier to debug.
 1.34553 +*/
 1.34554 +static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 1.34555 +  UNUSED_PARAMETER(pVfs);
 1.34556 +  return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
 1.34557 +}
 1.34558 +
 1.34559 +/*
 1.34560 +** Initialize and deinitialize the operating system interface.
 1.34561 +*/
 1.34562 +SQLITE_API int sqlite3_os_init(void){
 1.34563 +  static sqlite3_vfs winVfs = {
 1.34564 +    3,                   /* iVersion */
 1.34565 +    sizeof(winFile),     /* szOsFile */
 1.34566 +    MAX_PATH,            /* mxPathname */
 1.34567 +    0,                   /* pNext */
 1.34568 +    "win32",             /* zName */
 1.34569 +    0,                   /* pAppData */
 1.34570 +    winOpen,             /* xOpen */
 1.34571 +    winDelete,           /* xDelete */
 1.34572 +    winAccess,           /* xAccess */
 1.34573 +    winFullPathname,     /* xFullPathname */
 1.34574 +    winDlOpen,           /* xDlOpen */
 1.34575 +    winDlError,          /* xDlError */
 1.34576 +    winDlSym,            /* xDlSym */
 1.34577 +    winDlClose,          /* xDlClose */
 1.34578 +    winRandomness,       /* xRandomness */
 1.34579 +    winSleep,            /* xSleep */
 1.34580 +    winCurrentTime,      /* xCurrentTime */
 1.34581 +    winGetLastError,     /* xGetLastError */
 1.34582 +    winCurrentTimeInt64, /* xCurrentTimeInt64 */
 1.34583 +    winSetSystemCall,    /* xSetSystemCall */
 1.34584 +    winGetSystemCall,    /* xGetSystemCall */
 1.34585 +    winNextSystemCall,   /* xNextSystemCall */
 1.34586 +  };
 1.34587 +
 1.34588 +  /* Double-check that the aSyscall[] array has been constructed
 1.34589 +  ** correctly.  See ticket [bb3a86e890c8e96ab] */
 1.34590 +  assert( ArraySize(aSyscall)==74 );
 1.34591 +
 1.34592 +#ifndef SQLITE_OMIT_WAL
 1.34593 +  /* get memory map allocation granularity */
 1.34594 +  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
 1.34595 +#if SQLITE_OS_WINRT
 1.34596 +  osGetNativeSystemInfo(&winSysInfo);
 1.34597 +#else
 1.34598 +  osGetSystemInfo(&winSysInfo);
 1.34599 +#endif
 1.34600 +  assert(winSysInfo.dwAllocationGranularity > 0);
 1.34601 +#endif
 1.34602 +
 1.34603 +  sqlite3_vfs_register(&winVfs, 1);
 1.34604 +  return SQLITE_OK; 
 1.34605 +}
 1.34606 +
 1.34607 +SQLITE_API int sqlite3_os_end(void){ 
 1.34608 +#if SQLITE_OS_WINRT
 1.34609 +  if( sleepObj!=NULL ){
 1.34610 +    osCloseHandle(sleepObj);
 1.34611 +    sleepObj = NULL;
 1.34612 +  }
 1.34613 +#endif
 1.34614 +  return SQLITE_OK;
 1.34615 +}
 1.34616 +
 1.34617 +#endif /* SQLITE_OS_WIN */
 1.34618 +
 1.34619 +/************** End of os_win.c **********************************************/
 1.34620 +/************** Begin file bitvec.c ******************************************/
 1.34621 +/*
 1.34622 +** 2008 February 16
 1.34623 +**
 1.34624 +** The author disclaims copyright to this source code.  In place of
 1.34625 +** a legal notice, here is a blessing:
 1.34626 +**
 1.34627 +**    May you do good and not evil.
 1.34628 +**    May you find forgiveness for yourself and forgive others.
 1.34629 +**    May you share freely, never taking more than you give.
 1.34630 +**
 1.34631 +*************************************************************************
 1.34632 +** This file implements an object that represents a fixed-length
 1.34633 +** bitmap.  Bits are numbered starting with 1.
 1.34634 +**
 1.34635 +** A bitmap is used to record which pages of a database file have been
 1.34636 +** journalled during a transaction, or which pages have the "dont-write"
 1.34637 +** property.  Usually only a few pages are meet either condition.
 1.34638 +** So the bitmap is usually sparse and has low cardinality.
 1.34639 +** But sometimes (for example when during a DROP of a large table) most
 1.34640 +** or all of the pages in a database can get journalled.  In those cases, 
 1.34641 +** the bitmap becomes dense with high cardinality.  The algorithm needs 
 1.34642 +** to handle both cases well.
 1.34643 +**
 1.34644 +** The size of the bitmap is fixed when the object is created.
 1.34645 +**
 1.34646 +** All bits are clear when the bitmap is created.  Individual bits
 1.34647 +** may be set or cleared one at a time.
 1.34648 +**
 1.34649 +** Test operations are about 100 times more common that set operations.
 1.34650 +** Clear operations are exceedingly rare.  There are usually between
 1.34651 +** 5 and 500 set operations per Bitvec object, though the number of sets can
 1.34652 +** sometimes grow into tens of thousands or larger.  The size of the
 1.34653 +** Bitvec object is the number of pages in the database file at the
 1.34654 +** start of a transaction, and is thus usually less than a few thousand,
 1.34655 +** but can be as large as 2 billion for a really big database.
 1.34656 +*/
 1.34657 +
 1.34658 +/* Size of the Bitvec structure in bytes. */
 1.34659 +#define BITVEC_SZ        512
 1.34660 +
 1.34661 +/* Round the union size down to the nearest pointer boundary, since that's how 
 1.34662 +** it will be aligned within the Bitvec struct. */
 1.34663 +#define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
 1.34664 +
 1.34665 +/* Type of the array "element" for the bitmap representation. 
 1.34666 +** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
 1.34667 +** Setting this to the "natural word" size of your CPU may improve
 1.34668 +** performance. */
 1.34669 +#define BITVEC_TELEM     u8
 1.34670 +/* Size, in bits, of the bitmap element. */
 1.34671 +#define BITVEC_SZELEM    8
 1.34672 +/* Number of elements in a bitmap array. */
 1.34673 +#define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
 1.34674 +/* Number of bits in the bitmap array. */
 1.34675 +#define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
 1.34676 +
 1.34677 +/* Number of u32 values in hash table. */
 1.34678 +#define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
 1.34679 +/* Maximum number of entries in hash table before 
 1.34680 +** sub-dividing and re-hashing. */
 1.34681 +#define BITVEC_MXHASH    (BITVEC_NINT/2)
 1.34682 +/* Hashing function for the aHash representation.
 1.34683 +** Empirical testing showed that the *37 multiplier 
 1.34684 +** (an arbitrary prime)in the hash function provided 
 1.34685 +** no fewer collisions than the no-op *1. */
 1.34686 +#define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
 1.34687 +
 1.34688 +#define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
 1.34689 +
 1.34690 +
 1.34691 +/*
 1.34692 +** A bitmap is an instance of the following structure.
 1.34693 +**
 1.34694 +** This bitmap records the existance of zero or more bits
 1.34695 +** with values between 1 and iSize, inclusive.
 1.34696 +**
 1.34697 +** There are three possible representations of the bitmap.
 1.34698 +** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
 1.34699 +** bitmap.  The least significant bit is bit 1.
 1.34700 +**
 1.34701 +** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
 1.34702 +** a hash table that will hold up to BITVEC_MXHASH distinct values.
 1.34703 +**
 1.34704 +** Otherwise, the value i is redirected into one of BITVEC_NPTR
 1.34705 +** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
 1.34706 +** handles up to iDivisor separate values of i.  apSub[0] holds
 1.34707 +** values between 1 and iDivisor.  apSub[1] holds values between
 1.34708 +** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
 1.34709 +** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
 1.34710 +** to hold deal with values between 1 and iDivisor.
 1.34711 +*/
 1.34712 +struct Bitvec {
 1.34713 +  u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
 1.34714 +  u32 nSet;       /* Number of bits that are set - only valid for aHash
 1.34715 +                  ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
 1.34716 +                  ** this would be 125. */
 1.34717 +  u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
 1.34718 +                  /* Should >=0 for apSub element. */
 1.34719 +                  /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
 1.34720 +                  /* For a BITVEC_SZ of 512, this would be 34,359,739. */
 1.34721 +  union {
 1.34722 +    BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
 1.34723 +    u32 aHash[BITVEC_NINT];      /* Hash table representation */
 1.34724 +    Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
 1.34725 +  } u;
 1.34726 +};
 1.34727 +
 1.34728 +/*
 1.34729 +** Create a new bitmap object able to handle bits between 0 and iSize,
 1.34730 +** inclusive.  Return a pointer to the new object.  Return NULL if 
 1.34731 +** malloc fails.
 1.34732 +*/
 1.34733 +SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
 1.34734 +  Bitvec *p;
 1.34735 +  assert( sizeof(*p)==BITVEC_SZ );
 1.34736 +  p = sqlite3MallocZero( sizeof(*p) );
 1.34737 +  if( p ){
 1.34738 +    p->iSize = iSize;
 1.34739 +  }
 1.34740 +  return p;
 1.34741 +}
 1.34742 +
 1.34743 +/*
 1.34744 +** Check to see if the i-th bit is set.  Return true or false.
 1.34745 +** If p is NULL (if the bitmap has not been created) or if
 1.34746 +** i is out of range, then return false.
 1.34747 +*/
 1.34748 +SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
 1.34749 +  if( p==0 ) return 0;
 1.34750 +  if( i>p->iSize || i==0 ) return 0;
 1.34751 +  i--;
 1.34752 +  while( p->iDivisor ){
 1.34753 +    u32 bin = i/p->iDivisor;
 1.34754 +    i = i%p->iDivisor;
 1.34755 +    p = p->u.apSub[bin];
 1.34756 +    if (!p) {
 1.34757 +      return 0;
 1.34758 +    }
 1.34759 +  }
 1.34760 +  if( p->iSize<=BITVEC_NBIT ){
 1.34761 +    return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
 1.34762 +  } else{
 1.34763 +    u32 h = BITVEC_HASH(i++);
 1.34764 +    while( p->u.aHash[h] ){
 1.34765 +      if( p->u.aHash[h]==i ) return 1;
 1.34766 +      h = (h+1) % BITVEC_NINT;
 1.34767 +    }
 1.34768 +    return 0;
 1.34769 +  }
 1.34770 +}
 1.34771 +
 1.34772 +/*
 1.34773 +** Set the i-th bit.  Return 0 on success and an error code if
 1.34774 +** anything goes wrong.
 1.34775 +**
 1.34776 +** This routine might cause sub-bitmaps to be allocated.  Failing
 1.34777 +** to get the memory needed to hold the sub-bitmap is the only
 1.34778 +** that can go wrong with an insert, assuming p and i are valid.
 1.34779 +**
 1.34780 +** The calling function must ensure that p is a valid Bitvec object
 1.34781 +** and that the value for "i" is within range of the Bitvec object.
 1.34782 +** Otherwise the behavior is undefined.
 1.34783 +*/
 1.34784 +SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
 1.34785 +  u32 h;
 1.34786 +  if( p==0 ) return SQLITE_OK;
 1.34787 +  assert( i>0 );
 1.34788 +  assert( i<=p->iSize );
 1.34789 +  i--;
 1.34790 +  while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
 1.34791 +    u32 bin = i/p->iDivisor;
 1.34792 +    i = i%p->iDivisor;
 1.34793 +    if( p->u.apSub[bin]==0 ){
 1.34794 +      p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
 1.34795 +      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
 1.34796 +    }
 1.34797 +    p = p->u.apSub[bin];
 1.34798 +  }
 1.34799 +  if( p->iSize<=BITVEC_NBIT ){
 1.34800 +    p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
 1.34801 +    return SQLITE_OK;
 1.34802 +  }
 1.34803 +  h = BITVEC_HASH(i++);
 1.34804 +  /* if there wasn't a hash collision, and this doesn't */
 1.34805 +  /* completely fill the hash, then just add it without */
 1.34806 +  /* worring about sub-dividing and re-hashing. */
 1.34807 +  if( !p->u.aHash[h] ){
 1.34808 +    if (p->nSet<(BITVEC_NINT-1)) {
 1.34809 +      goto bitvec_set_end;
 1.34810 +    } else {
 1.34811 +      goto bitvec_set_rehash;
 1.34812 +    }
 1.34813 +  }
 1.34814 +  /* there was a collision, check to see if it's already */
 1.34815 +  /* in hash, if not, try to find a spot for it */
 1.34816 +  do {
 1.34817 +    if( p->u.aHash[h]==i ) return SQLITE_OK;
 1.34818 +    h++;
 1.34819 +    if( h>=BITVEC_NINT ) h = 0;
 1.34820 +  } while( p->u.aHash[h] );
 1.34821 +  /* we didn't find it in the hash.  h points to the first */
 1.34822 +  /* available free spot. check to see if this is going to */
 1.34823 +  /* make our hash too "full".  */
 1.34824 +bitvec_set_rehash:
 1.34825 +  if( p->nSet>=BITVEC_MXHASH ){
 1.34826 +    unsigned int j;
 1.34827 +    int rc;
 1.34828 +    u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
 1.34829 +    if( aiValues==0 ){
 1.34830 +      return SQLITE_NOMEM;
 1.34831 +    }else{
 1.34832 +      memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
 1.34833 +      memset(p->u.apSub, 0, sizeof(p->u.apSub));
 1.34834 +      p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
 1.34835 +      rc = sqlite3BitvecSet(p, i);
 1.34836 +      for(j=0; j<BITVEC_NINT; j++){
 1.34837 +        if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
 1.34838 +      }
 1.34839 +      sqlite3StackFree(0, aiValues);
 1.34840 +      return rc;
 1.34841 +    }
 1.34842 +  }
 1.34843 +bitvec_set_end:
 1.34844 +  p->nSet++;
 1.34845 +  p->u.aHash[h] = i;
 1.34846 +  return SQLITE_OK;
 1.34847 +}
 1.34848 +
 1.34849 +/*
 1.34850 +** Clear the i-th bit.
 1.34851 +**
 1.34852 +** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
 1.34853 +** that BitvecClear can use to rebuilt its hash table.
 1.34854 +*/
 1.34855 +SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
 1.34856 +  if( p==0 ) return;
 1.34857 +  assert( i>0 );
 1.34858 +  i--;
 1.34859 +  while( p->iDivisor ){
 1.34860 +    u32 bin = i/p->iDivisor;
 1.34861 +    i = i%p->iDivisor;
 1.34862 +    p = p->u.apSub[bin];
 1.34863 +    if (!p) {
 1.34864 +      return;
 1.34865 +    }
 1.34866 +  }
 1.34867 +  if( p->iSize<=BITVEC_NBIT ){
 1.34868 +    p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
 1.34869 +  }else{
 1.34870 +    unsigned int j;
 1.34871 +    u32 *aiValues = pBuf;
 1.34872 +    memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
 1.34873 +    memset(p->u.aHash, 0, sizeof(p->u.aHash));
 1.34874 +    p->nSet = 0;
 1.34875 +    for(j=0; j<BITVEC_NINT; j++){
 1.34876 +      if( aiValues[j] && aiValues[j]!=(i+1) ){
 1.34877 +        u32 h = BITVEC_HASH(aiValues[j]-1);
 1.34878 +        p->nSet++;
 1.34879 +        while( p->u.aHash[h] ){
 1.34880 +          h++;
 1.34881 +          if( h>=BITVEC_NINT ) h = 0;
 1.34882 +        }
 1.34883 +        p->u.aHash[h] = aiValues[j];
 1.34884 +      }
 1.34885 +    }
 1.34886 +  }
 1.34887 +}
 1.34888 +
 1.34889 +/*
 1.34890 +** Destroy a bitmap object.  Reclaim all memory used.
 1.34891 +*/
 1.34892 +SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
 1.34893 +  if( p==0 ) return;
 1.34894 +  if( p->iDivisor ){
 1.34895 +    unsigned int i;
 1.34896 +    for(i=0; i<BITVEC_NPTR; i++){
 1.34897 +      sqlite3BitvecDestroy(p->u.apSub[i]);
 1.34898 +    }
 1.34899 +  }
 1.34900 +  sqlite3_free(p);
 1.34901 +}
 1.34902 +
 1.34903 +/*
 1.34904 +** Return the value of the iSize parameter specified when Bitvec *p
 1.34905 +** was created.
 1.34906 +*/
 1.34907 +SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
 1.34908 +  return p->iSize;
 1.34909 +}
 1.34910 +
 1.34911 +#ifndef SQLITE_OMIT_BUILTIN_TEST
 1.34912 +/*
 1.34913 +** Let V[] be an array of unsigned characters sufficient to hold
 1.34914 +** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
 1.34915 +** Then the following macros can be used to set, clear, or test
 1.34916 +** individual bits within V.
 1.34917 +*/
 1.34918 +#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
 1.34919 +#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
 1.34920 +#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
 1.34921 +
 1.34922 +/*
 1.34923 +** This routine runs an extensive test of the Bitvec code.
 1.34924 +**
 1.34925 +** The input is an array of integers that acts as a program
 1.34926 +** to test the Bitvec.  The integers are opcodes followed
 1.34927 +** by 0, 1, or 3 operands, depending on the opcode.  Another
 1.34928 +** opcode follows immediately after the last operand.
 1.34929 +**
 1.34930 +** There are 6 opcodes numbered from 0 through 5.  0 is the
 1.34931 +** "halt" opcode and causes the test to end.
 1.34932 +**
 1.34933 +**    0          Halt and return the number of errors
 1.34934 +**    1 N S X    Set N bits beginning with S and incrementing by X
 1.34935 +**    2 N S X    Clear N bits beginning with S and incrementing by X
 1.34936 +**    3 N        Set N randomly chosen bits
 1.34937 +**    4 N        Clear N randomly chosen bits
 1.34938 +**    5 N S X    Set N bits from S increment X in array only, not in bitvec
 1.34939 +**
 1.34940 +** The opcodes 1 through 4 perform set and clear operations are performed
 1.34941 +** on both a Bitvec object and on a linear array of bits obtained from malloc.
 1.34942 +** Opcode 5 works on the linear array only, not on the Bitvec.
 1.34943 +** Opcode 5 is used to deliberately induce a fault in order to
 1.34944 +** confirm that error detection works.
 1.34945 +**
 1.34946 +** At the conclusion of the test the linear array is compared
 1.34947 +** against the Bitvec object.  If there are any differences,
 1.34948 +** an error is returned.  If they are the same, zero is returned.
 1.34949 +**
 1.34950 +** If a memory allocation error occurs, return -1.
 1.34951 +*/
 1.34952 +SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
 1.34953 +  Bitvec *pBitvec = 0;
 1.34954 +  unsigned char *pV = 0;
 1.34955 +  int rc = -1;
 1.34956 +  int i, nx, pc, op;
 1.34957 +  void *pTmpSpace;
 1.34958 +
 1.34959 +  /* Allocate the Bitvec to be tested and a linear array of
 1.34960 +  ** bits to act as the reference */
 1.34961 +  pBitvec = sqlite3BitvecCreate( sz );
 1.34962 +  pV = sqlite3MallocZero( (sz+7)/8 + 1 );
 1.34963 +  pTmpSpace = sqlite3_malloc(BITVEC_SZ);
 1.34964 +  if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
 1.34965 +
 1.34966 +  /* NULL pBitvec tests */
 1.34967 +  sqlite3BitvecSet(0, 1);
 1.34968 +  sqlite3BitvecClear(0, 1, pTmpSpace);
 1.34969 +
 1.34970 +  /* Run the program */
 1.34971 +  pc = 0;
 1.34972 +  while( (op = aOp[pc])!=0 ){
 1.34973 +    switch( op ){
 1.34974 +      case 1:
 1.34975 +      case 2:
 1.34976 +      case 5: {
 1.34977 +        nx = 4;
 1.34978 +        i = aOp[pc+2] - 1;
 1.34979 +        aOp[pc+2] += aOp[pc+3];
 1.34980 +        break;
 1.34981 +      }
 1.34982 +      case 3:
 1.34983 +      case 4: 
 1.34984 +      default: {
 1.34985 +        nx = 2;
 1.34986 +        sqlite3_randomness(sizeof(i), &i);
 1.34987 +        break;
 1.34988 +      }
 1.34989 +    }
 1.34990 +    if( (--aOp[pc+1]) > 0 ) nx = 0;
 1.34991 +    pc += nx;
 1.34992 +    i = (i & 0x7fffffff)%sz;
 1.34993 +    if( (op & 1)!=0 ){
 1.34994 +      SETBIT(pV, (i+1));
 1.34995 +      if( op!=5 ){
 1.34996 +        if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
 1.34997 +      }
 1.34998 +    }else{
 1.34999 +      CLEARBIT(pV, (i+1));
 1.35000 +      sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
 1.35001 +    }
 1.35002 +  }
 1.35003 +
 1.35004 +  /* Test to make sure the linear array exactly matches the
 1.35005 +  ** Bitvec object.  Start with the assumption that they do
 1.35006 +  ** match (rc==0).  Change rc to non-zero if a discrepancy
 1.35007 +  ** is found.
 1.35008 +  */
 1.35009 +  rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
 1.35010 +          + sqlite3BitvecTest(pBitvec, 0)
 1.35011 +          + (sqlite3BitvecSize(pBitvec) - sz);
 1.35012 +  for(i=1; i<=sz; i++){
 1.35013 +    if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
 1.35014 +      rc = i;
 1.35015 +      break;
 1.35016 +    }
 1.35017 +  }
 1.35018 +
 1.35019 +  /* Free allocated structure */
 1.35020 +bitvec_end:
 1.35021 +  sqlite3_free(pTmpSpace);
 1.35022 +  sqlite3_free(pV);
 1.35023 +  sqlite3BitvecDestroy(pBitvec);
 1.35024 +  return rc;
 1.35025 +}
 1.35026 +#endif /* SQLITE_OMIT_BUILTIN_TEST */
 1.35027 +
 1.35028 +/************** End of bitvec.c **********************************************/
 1.35029 +/************** Begin file pcache.c ******************************************/
 1.35030 +/*
 1.35031 +** 2008 August 05
 1.35032 +**
 1.35033 +** The author disclaims copyright to this source code.  In place of
 1.35034 +** a legal notice, here is a blessing:
 1.35035 +**
 1.35036 +**    May you do good and not evil.
 1.35037 +**    May you find forgiveness for yourself and forgive others.
 1.35038 +**    May you share freely, never taking more than you give.
 1.35039 +**
 1.35040 +*************************************************************************
 1.35041 +** This file implements that page cache.
 1.35042 +*/
 1.35043 +
 1.35044 +/*
 1.35045 +** A complete page cache is an instance of this structure.
 1.35046 +*/
 1.35047 +struct PCache {
 1.35048 +  PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
 1.35049 +  PgHdr *pSynced;                     /* Last synced page in dirty page list */
 1.35050 +  int nRef;                           /* Number of referenced pages */
 1.35051 +  int szCache;                        /* Configured cache size */
 1.35052 +  int szPage;                         /* Size of every page in this cache */
 1.35053 +  int szExtra;                        /* Size of extra space for each page */
 1.35054 +  int bPurgeable;                     /* True if pages are on backing store */
 1.35055 +  int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
 1.35056 +  void *pStress;                      /* Argument to xStress */
 1.35057 +  sqlite3_pcache *pCache;             /* Pluggable cache module */
 1.35058 +  PgHdr *pPage1;                      /* Reference to page 1 */
 1.35059 +};
 1.35060 +
 1.35061 +/*
 1.35062 +** Some of the assert() macros in this code are too expensive to run
 1.35063 +** even during normal debugging.  Use them only rarely on long-running
 1.35064 +** tests.  Enable the expensive asserts using the
 1.35065 +** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
 1.35066 +*/
 1.35067 +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 1.35068 +# define expensive_assert(X)  assert(X)
 1.35069 +#else
 1.35070 +# define expensive_assert(X)
 1.35071 +#endif
 1.35072 +
 1.35073 +/********************************** Linked List Management ********************/
 1.35074 +
 1.35075 +#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
 1.35076 +/*
 1.35077 +** Check that the pCache->pSynced variable is set correctly. If it
 1.35078 +** is not, either fail an assert or return zero. Otherwise, return
 1.35079 +** non-zero. This is only used in debugging builds, as follows:
 1.35080 +**
 1.35081 +**   expensive_assert( pcacheCheckSynced(pCache) );
 1.35082 +*/
 1.35083 +static int pcacheCheckSynced(PCache *pCache){
 1.35084 +  PgHdr *p;
 1.35085 +  for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
 1.35086 +    assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
 1.35087 +  }
 1.35088 +  return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
 1.35089 +}
 1.35090 +#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
 1.35091 +
 1.35092 +/*
 1.35093 +** Remove page pPage from the list of dirty pages.
 1.35094 +*/
 1.35095 +static void pcacheRemoveFromDirtyList(PgHdr *pPage){
 1.35096 +  PCache *p = pPage->pCache;
 1.35097 +
 1.35098 +  assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
 1.35099 +  assert( pPage->pDirtyPrev || pPage==p->pDirty );
 1.35100 +
 1.35101 +  /* Update the PCache1.pSynced variable if necessary. */
 1.35102 +  if( p->pSynced==pPage ){
 1.35103 +    PgHdr *pSynced = pPage->pDirtyPrev;
 1.35104 +    while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
 1.35105 +      pSynced = pSynced->pDirtyPrev;
 1.35106 +    }
 1.35107 +    p->pSynced = pSynced;
 1.35108 +  }
 1.35109 +
 1.35110 +  if( pPage->pDirtyNext ){
 1.35111 +    pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
 1.35112 +  }else{
 1.35113 +    assert( pPage==p->pDirtyTail );
 1.35114 +    p->pDirtyTail = pPage->pDirtyPrev;
 1.35115 +  }
 1.35116 +  if( pPage->pDirtyPrev ){
 1.35117 +    pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
 1.35118 +  }else{
 1.35119 +    assert( pPage==p->pDirty );
 1.35120 +    p->pDirty = pPage->pDirtyNext;
 1.35121 +  }
 1.35122 +  pPage->pDirtyNext = 0;
 1.35123 +  pPage->pDirtyPrev = 0;
 1.35124 +
 1.35125 +  expensive_assert( pcacheCheckSynced(p) );
 1.35126 +}
 1.35127 +
 1.35128 +/*
 1.35129 +** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
 1.35130 +** pPage).
 1.35131 +*/
 1.35132 +static void pcacheAddToDirtyList(PgHdr *pPage){
 1.35133 +  PCache *p = pPage->pCache;
 1.35134 +
 1.35135 +  assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
 1.35136 +
 1.35137 +  pPage->pDirtyNext = p->pDirty;
 1.35138 +  if( pPage->pDirtyNext ){
 1.35139 +    assert( pPage->pDirtyNext->pDirtyPrev==0 );
 1.35140 +    pPage->pDirtyNext->pDirtyPrev = pPage;
 1.35141 +  }
 1.35142 +  p->pDirty = pPage;
 1.35143 +  if( !p->pDirtyTail ){
 1.35144 +    p->pDirtyTail = pPage;
 1.35145 +  }
 1.35146 +  if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
 1.35147 +    p->pSynced = pPage;
 1.35148 +  }
 1.35149 +  expensive_assert( pcacheCheckSynced(p) );
 1.35150 +}
 1.35151 +
 1.35152 +/*
 1.35153 +** Wrapper around the pluggable caches xUnpin method. If the cache is
 1.35154 +** being used for an in-memory database, this function is a no-op.
 1.35155 +*/
 1.35156 +static void pcacheUnpin(PgHdr *p){
 1.35157 +  PCache *pCache = p->pCache;
 1.35158 +  if( pCache->bPurgeable ){
 1.35159 +    if( p->pgno==1 ){
 1.35160 +      pCache->pPage1 = 0;
 1.35161 +    }
 1.35162 +    sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
 1.35163 +  }
 1.35164 +}
 1.35165 +
 1.35166 +/*************************************************** General Interfaces ******
 1.35167 +**
 1.35168 +** Initialize and shutdown the page cache subsystem. Neither of these 
 1.35169 +** functions are threadsafe.
 1.35170 +*/
 1.35171 +SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
 1.35172 +  if( sqlite3GlobalConfig.pcache2.xInit==0 ){
 1.35173 +    /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
 1.35174 +    ** built-in default page cache is used instead of the application defined
 1.35175 +    ** page cache. */
 1.35176 +    sqlite3PCacheSetDefault();
 1.35177 +  }
 1.35178 +  return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
 1.35179 +}
 1.35180 +SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
 1.35181 +  if( sqlite3GlobalConfig.pcache2.xShutdown ){
 1.35182 +    /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
 1.35183 +    sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
 1.35184 +  }
 1.35185 +}
 1.35186 +
 1.35187 +/*
 1.35188 +** Return the size in bytes of a PCache object.
 1.35189 +*/
 1.35190 +SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
 1.35191 +
 1.35192 +/*
 1.35193 +** Create a new PCache object. Storage space to hold the object
 1.35194 +** has already been allocated and is passed in as the p pointer. 
 1.35195 +** The caller discovers how much space needs to be allocated by 
 1.35196 +** calling sqlite3PcacheSize().
 1.35197 +*/
 1.35198 +SQLITE_PRIVATE void sqlite3PcacheOpen(
 1.35199 +  int szPage,                  /* Size of every page */
 1.35200 +  int szExtra,                 /* Extra space associated with each page */
 1.35201 +  int bPurgeable,              /* True if pages are on backing store */
 1.35202 +  int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
 1.35203 +  void *pStress,               /* Argument to xStress */
 1.35204 +  PCache *p                    /* Preallocated space for the PCache */
 1.35205 +){
 1.35206 +  memset(p, 0, sizeof(PCache));
 1.35207 +  p->szPage = szPage;
 1.35208 +  p->szExtra = szExtra;
 1.35209 +  p->bPurgeable = bPurgeable;
 1.35210 +  p->xStress = xStress;
 1.35211 +  p->pStress = pStress;
 1.35212 +  p->szCache = 100;
 1.35213 +}
 1.35214 +
 1.35215 +/*
 1.35216 +** Change the page size for PCache object. The caller must ensure that there
 1.35217 +** are no outstanding page references when this function is called.
 1.35218 +*/
 1.35219 +SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
 1.35220 +  assert( pCache->nRef==0 && pCache->pDirty==0 );
 1.35221 +  if( pCache->pCache ){
 1.35222 +    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
 1.35223 +    pCache->pCache = 0;
 1.35224 +    pCache->pPage1 = 0;
 1.35225 +  }
 1.35226 +  pCache->szPage = szPage;
 1.35227 +}
 1.35228 +
 1.35229 +/*
 1.35230 +** Compute the number of pages of cache requested.
 1.35231 +*/
 1.35232 +static int numberOfCachePages(PCache *p){
 1.35233 +  if( p->szCache>=0 ){
 1.35234 +    return p->szCache;
 1.35235 +  }else{
 1.35236 +    return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
 1.35237 +  }
 1.35238 +}
 1.35239 +
 1.35240 +/*
 1.35241 +** Try to obtain a page from the cache.
 1.35242 +*/
 1.35243 +SQLITE_PRIVATE int sqlite3PcacheFetch(
 1.35244 +  PCache *pCache,       /* Obtain the page from this cache */
 1.35245 +  Pgno pgno,            /* Page number to obtain */
 1.35246 +  int createFlag,       /* If true, create page if it does not exist already */
 1.35247 +  PgHdr **ppPage        /* Write the page here */
 1.35248 +){
 1.35249 +  sqlite3_pcache_page *pPage = 0;
 1.35250 +  PgHdr *pPgHdr = 0;
 1.35251 +  int eCreate;
 1.35252 +
 1.35253 +  assert( pCache!=0 );
 1.35254 +  assert( createFlag==1 || createFlag==0 );
 1.35255 +  assert( pgno>0 );
 1.35256 +
 1.35257 +  /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
 1.35258 +  ** allocate it now.
 1.35259 +  */
 1.35260 +  if( !pCache->pCache && createFlag ){
 1.35261 +    sqlite3_pcache *p;
 1.35262 +    p = sqlite3GlobalConfig.pcache2.xCreate(
 1.35263 +        pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
 1.35264 +    );
 1.35265 +    if( !p ){
 1.35266 +      return SQLITE_NOMEM;
 1.35267 +    }
 1.35268 +    sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
 1.35269 +    pCache->pCache = p;
 1.35270 +  }
 1.35271 +
 1.35272 +  eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
 1.35273 +  if( pCache->pCache ){
 1.35274 +    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
 1.35275 +  }
 1.35276 +
 1.35277 +  if( !pPage && eCreate==1 ){
 1.35278 +    PgHdr *pPg;
 1.35279 +
 1.35280 +    /* Find a dirty page to write-out and recycle. First try to find a 
 1.35281 +    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
 1.35282 +    ** cleared), but if that is not possible settle for any other 
 1.35283 +    ** unreferenced dirty page.
 1.35284 +    */
 1.35285 +    expensive_assert( pcacheCheckSynced(pCache) );
 1.35286 +    for(pPg=pCache->pSynced; 
 1.35287 +        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
 1.35288 +        pPg=pPg->pDirtyPrev
 1.35289 +    );
 1.35290 +    pCache->pSynced = pPg;
 1.35291 +    if( !pPg ){
 1.35292 +      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
 1.35293 +    }
 1.35294 +    if( pPg ){
 1.35295 +      int rc;
 1.35296 +#ifdef SQLITE_LOG_CACHE_SPILL
 1.35297 +      sqlite3_log(SQLITE_FULL, 
 1.35298 +                  "spill page %d making room for %d - cache used: %d/%d",
 1.35299 +                  pPg->pgno, pgno,
 1.35300 +                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
 1.35301 +                  numberOfCachePages(pCache));
 1.35302 +#endif
 1.35303 +      rc = pCache->xStress(pCache->pStress, pPg);
 1.35304 +      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 1.35305 +        return rc;
 1.35306 +      }
 1.35307 +    }
 1.35308 +
 1.35309 +    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
 1.35310 +  }
 1.35311 +
 1.35312 +  if( pPage ){
 1.35313 +    pPgHdr = (PgHdr *)pPage->pExtra;
 1.35314 +
 1.35315 +    if( !pPgHdr->pPage ){
 1.35316 +      memset(pPgHdr, 0, sizeof(PgHdr));
 1.35317 +      pPgHdr->pPage = pPage;
 1.35318 +      pPgHdr->pData = pPage->pBuf;
 1.35319 +      pPgHdr->pExtra = (void *)&pPgHdr[1];
 1.35320 +      memset(pPgHdr->pExtra, 0, pCache->szExtra);
 1.35321 +      pPgHdr->pCache = pCache;
 1.35322 +      pPgHdr->pgno = pgno;
 1.35323 +    }
 1.35324 +    assert( pPgHdr->pCache==pCache );
 1.35325 +    assert( pPgHdr->pgno==pgno );
 1.35326 +    assert( pPgHdr->pData==pPage->pBuf );
 1.35327 +    assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
 1.35328 +
 1.35329 +    if( 0==pPgHdr->nRef ){
 1.35330 +      pCache->nRef++;
 1.35331 +    }
 1.35332 +    pPgHdr->nRef++;
 1.35333 +    if( pgno==1 ){
 1.35334 +      pCache->pPage1 = pPgHdr;
 1.35335 +    }
 1.35336 +  }
 1.35337 +  *ppPage = pPgHdr;
 1.35338 +  return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
 1.35339 +}
 1.35340 +
 1.35341 +/*
 1.35342 +** Decrement the reference count on a page. If the page is clean and the
 1.35343 +** reference count drops to 0, then it is made elible for recycling.
 1.35344 +*/
 1.35345 +SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
 1.35346 +  assert( p->nRef>0 );
 1.35347 +  p->nRef--;
 1.35348 +  if( p->nRef==0 ){
 1.35349 +    PCache *pCache = p->pCache;
 1.35350 +    pCache->nRef--;
 1.35351 +    if( (p->flags&PGHDR_DIRTY)==0 ){
 1.35352 +      pcacheUnpin(p);
 1.35353 +    }else{
 1.35354 +      /* Move the page to the head of the dirty list. */
 1.35355 +      pcacheRemoveFromDirtyList(p);
 1.35356 +      pcacheAddToDirtyList(p);
 1.35357 +    }
 1.35358 +  }
 1.35359 +}
 1.35360 +
 1.35361 +/*
 1.35362 +** Increase the reference count of a supplied page by 1.
 1.35363 +*/
 1.35364 +SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
 1.35365 +  assert(p->nRef>0);
 1.35366 +  p->nRef++;
 1.35367 +}
 1.35368 +
 1.35369 +/*
 1.35370 +** Drop a page from the cache. There must be exactly one reference to the
 1.35371 +** page. This function deletes that reference, so after it returns the
 1.35372 +** page pointed to by p is invalid.
 1.35373 +*/
 1.35374 +SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
 1.35375 +  PCache *pCache;
 1.35376 +  assert( p->nRef==1 );
 1.35377 +  if( p->flags&PGHDR_DIRTY ){
 1.35378 +    pcacheRemoveFromDirtyList(p);
 1.35379 +  }
 1.35380 +  pCache = p->pCache;
 1.35381 +  pCache->nRef--;
 1.35382 +  if( p->pgno==1 ){
 1.35383 +    pCache->pPage1 = 0;
 1.35384 +  }
 1.35385 +  sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
 1.35386 +}
 1.35387 +
 1.35388 +/*
 1.35389 +** Make sure the page is marked as dirty. If it isn't dirty already,
 1.35390 +** make it so.
 1.35391 +*/
 1.35392 +SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
 1.35393 +  p->flags &= ~PGHDR_DONT_WRITE;
 1.35394 +  assert( p->nRef>0 );
 1.35395 +  if( 0==(p->flags & PGHDR_DIRTY) ){
 1.35396 +    p->flags |= PGHDR_DIRTY;
 1.35397 +    pcacheAddToDirtyList( p);
 1.35398 +  }
 1.35399 +}
 1.35400 +
 1.35401 +/*
 1.35402 +** Make sure the page is marked as clean. If it isn't clean already,
 1.35403 +** make it so.
 1.35404 +*/
 1.35405 +SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
 1.35406 +  if( (p->flags & PGHDR_DIRTY) ){
 1.35407 +    pcacheRemoveFromDirtyList(p);
 1.35408 +    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
 1.35409 +    if( p->nRef==0 ){
 1.35410 +      pcacheUnpin(p);
 1.35411 +    }
 1.35412 +  }
 1.35413 +}
 1.35414 +
 1.35415 +/*
 1.35416 +** Make every page in the cache clean.
 1.35417 +*/
 1.35418 +SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
 1.35419 +  PgHdr *p;
 1.35420 +  while( (p = pCache->pDirty)!=0 ){
 1.35421 +    sqlite3PcacheMakeClean(p);
 1.35422 +  }
 1.35423 +}
 1.35424 +
 1.35425 +/*
 1.35426 +** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
 1.35427 +*/
 1.35428 +SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
 1.35429 +  PgHdr *p;
 1.35430 +  for(p=pCache->pDirty; p; p=p->pDirtyNext){
 1.35431 +    p->flags &= ~PGHDR_NEED_SYNC;
 1.35432 +  }
 1.35433 +  pCache->pSynced = pCache->pDirtyTail;
 1.35434 +}
 1.35435 +
 1.35436 +/*
 1.35437 +** Change the page number of page p to newPgno. 
 1.35438 +*/
 1.35439 +SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
 1.35440 +  PCache *pCache = p->pCache;
 1.35441 +  assert( p->nRef>0 );
 1.35442 +  assert( newPgno>0 );
 1.35443 +  sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
 1.35444 +  p->pgno = newPgno;
 1.35445 +  if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
 1.35446 +    pcacheRemoveFromDirtyList(p);
 1.35447 +    pcacheAddToDirtyList(p);
 1.35448 +  }
 1.35449 +}
 1.35450 +
 1.35451 +/*
 1.35452 +** Drop every cache entry whose page number is greater than "pgno". The
 1.35453 +** caller must ensure that there are no outstanding references to any pages
 1.35454 +** other than page 1 with a page number greater than pgno.
 1.35455 +**
 1.35456 +** If there is a reference to page 1 and the pgno parameter passed to this
 1.35457 +** function is 0, then the data area associated with page 1 is zeroed, but
 1.35458 +** the page object is not dropped.
 1.35459 +*/
 1.35460 +SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
 1.35461 +  if( pCache->pCache ){
 1.35462 +    PgHdr *p;
 1.35463 +    PgHdr *pNext;
 1.35464 +    for(p=pCache->pDirty; p; p=pNext){
 1.35465 +      pNext = p->pDirtyNext;
 1.35466 +      /* This routine never gets call with a positive pgno except right
 1.35467 +      ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
 1.35468 +      ** it must be that pgno==0.
 1.35469 +      */
 1.35470 +      assert( p->pgno>0 );
 1.35471 +      if( ALWAYS(p->pgno>pgno) ){
 1.35472 +        assert( p->flags&PGHDR_DIRTY );
 1.35473 +        sqlite3PcacheMakeClean(p);
 1.35474 +      }
 1.35475 +    }
 1.35476 +    if( pgno==0 && pCache->pPage1 ){
 1.35477 +      memset(pCache->pPage1->pData, 0, pCache->szPage);
 1.35478 +      pgno = 1;
 1.35479 +    }
 1.35480 +    sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
 1.35481 +  }
 1.35482 +}
 1.35483 +
 1.35484 +/*
 1.35485 +** Close a cache.
 1.35486 +*/
 1.35487 +SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
 1.35488 +  if( pCache->pCache ){
 1.35489 +    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
 1.35490 +  }
 1.35491 +}
 1.35492 +
 1.35493 +/* 
 1.35494 +** Discard the contents of the cache.
 1.35495 +*/
 1.35496 +SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
 1.35497 +  sqlite3PcacheTruncate(pCache, 0);
 1.35498 +}
 1.35499 +
 1.35500 +/*
 1.35501 +** Merge two lists of pages connected by pDirty and in pgno order.
 1.35502 +** Do not both fixing the pDirtyPrev pointers.
 1.35503 +*/
 1.35504 +static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
 1.35505 +  PgHdr result, *pTail;
 1.35506 +  pTail = &result;
 1.35507 +  while( pA && pB ){
 1.35508 +    if( pA->pgno<pB->pgno ){
 1.35509 +      pTail->pDirty = pA;
 1.35510 +      pTail = pA;
 1.35511 +      pA = pA->pDirty;
 1.35512 +    }else{
 1.35513 +      pTail->pDirty = pB;
 1.35514 +      pTail = pB;
 1.35515 +      pB = pB->pDirty;
 1.35516 +    }
 1.35517 +  }
 1.35518 +  if( pA ){
 1.35519 +    pTail->pDirty = pA;
 1.35520 +  }else if( pB ){
 1.35521 +    pTail->pDirty = pB;
 1.35522 +  }else{
 1.35523 +    pTail->pDirty = 0;
 1.35524 +  }
 1.35525 +  return result.pDirty;
 1.35526 +}
 1.35527 +
 1.35528 +/*
 1.35529 +** Sort the list of pages in accending order by pgno.  Pages are
 1.35530 +** connected by pDirty pointers.  The pDirtyPrev pointers are
 1.35531 +** corrupted by this sort.
 1.35532 +**
 1.35533 +** Since there cannot be more than 2^31 distinct pages in a database,
 1.35534 +** there cannot be more than 31 buckets required by the merge sorter.
 1.35535 +** One extra bucket is added to catch overflow in case something
 1.35536 +** ever changes to make the previous sentence incorrect.
 1.35537 +*/
 1.35538 +#define N_SORT_BUCKET  32
 1.35539 +static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
 1.35540 +  PgHdr *a[N_SORT_BUCKET], *p;
 1.35541 +  int i;
 1.35542 +  memset(a, 0, sizeof(a));
 1.35543 +  while( pIn ){
 1.35544 +    p = pIn;
 1.35545 +    pIn = p->pDirty;
 1.35546 +    p->pDirty = 0;
 1.35547 +    for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
 1.35548 +      if( a[i]==0 ){
 1.35549 +        a[i] = p;
 1.35550 +        break;
 1.35551 +      }else{
 1.35552 +        p = pcacheMergeDirtyList(a[i], p);
 1.35553 +        a[i] = 0;
 1.35554 +      }
 1.35555 +    }
 1.35556 +    if( NEVER(i==N_SORT_BUCKET-1) ){
 1.35557 +      /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
 1.35558 +      ** the input list.  But that is impossible.
 1.35559 +      */
 1.35560 +      a[i] = pcacheMergeDirtyList(a[i], p);
 1.35561 +    }
 1.35562 +  }
 1.35563 +  p = a[0];
 1.35564 +  for(i=1; i<N_SORT_BUCKET; i++){
 1.35565 +    p = pcacheMergeDirtyList(p, a[i]);
 1.35566 +  }
 1.35567 +  return p;
 1.35568 +}
 1.35569 +
 1.35570 +/*
 1.35571 +** Return a list of all dirty pages in the cache, sorted by page number.
 1.35572 +*/
 1.35573 +SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
 1.35574 +  PgHdr *p;
 1.35575 +  for(p=pCache->pDirty; p; p=p->pDirtyNext){
 1.35576 +    p->pDirty = p->pDirtyNext;
 1.35577 +  }
 1.35578 +  return pcacheSortDirtyList(pCache->pDirty);
 1.35579 +}
 1.35580 +
 1.35581 +/* 
 1.35582 +** Return the total number of referenced pages held by the cache.
 1.35583 +*/
 1.35584 +SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
 1.35585 +  return pCache->nRef;
 1.35586 +}
 1.35587 +
 1.35588 +/*
 1.35589 +** Return the number of references to the page supplied as an argument.
 1.35590 +*/
 1.35591 +SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
 1.35592 +  return p->nRef;
 1.35593 +}
 1.35594 +
 1.35595 +/* 
 1.35596 +** Return the total number of pages in the cache.
 1.35597 +*/
 1.35598 +SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
 1.35599 +  int nPage = 0;
 1.35600 +  if( pCache->pCache ){
 1.35601 +    nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
 1.35602 +  }
 1.35603 +  return nPage;
 1.35604 +}
 1.35605 +
 1.35606 +#ifdef SQLITE_TEST
 1.35607 +/*
 1.35608 +** Get the suggested cache-size value.
 1.35609 +*/
 1.35610 +SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
 1.35611 +  return numberOfCachePages(pCache);
 1.35612 +}
 1.35613 +#endif
 1.35614 +
 1.35615 +/*
 1.35616 +** Set the suggested cache-size value.
 1.35617 +*/
 1.35618 +SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
 1.35619 +  pCache->szCache = mxPage;
 1.35620 +  if( pCache->pCache ){
 1.35621 +    sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
 1.35622 +                                           numberOfCachePages(pCache));
 1.35623 +  }
 1.35624 +}
 1.35625 +
 1.35626 +/*
 1.35627 +** Free up as much memory as possible from the page cache.
 1.35628 +*/
 1.35629 +SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
 1.35630 +  if( pCache->pCache ){
 1.35631 +    sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
 1.35632 +  }
 1.35633 +}
 1.35634 +
 1.35635 +#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
 1.35636 +/*
 1.35637 +** For all dirty pages currently in the cache, invoke the specified
 1.35638 +** callback. This is only used if the SQLITE_CHECK_PAGES macro is
 1.35639 +** defined.
 1.35640 +*/
 1.35641 +SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
 1.35642 +  PgHdr *pDirty;
 1.35643 +  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
 1.35644 +    xIter(pDirty);
 1.35645 +  }
 1.35646 +}
 1.35647 +#endif
 1.35648 +
 1.35649 +/************** End of pcache.c **********************************************/
 1.35650 +/************** Begin file pcache1.c *****************************************/
 1.35651 +/*
 1.35652 +** 2008 November 05
 1.35653 +**
 1.35654 +** The author disclaims copyright to this source code.  In place of
 1.35655 +** a legal notice, here is a blessing:
 1.35656 +**
 1.35657 +**    May you do good and not evil.
 1.35658 +**    May you find forgiveness for yourself and forgive others.
 1.35659 +**    May you share freely, never taking more than you give.
 1.35660 +**
 1.35661 +*************************************************************************
 1.35662 +**
 1.35663 +** This file implements the default page cache implementation (the
 1.35664 +** sqlite3_pcache interface). It also contains part of the implementation
 1.35665 +** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
 1.35666 +** If the default page cache implementation is overriden, then neither of
 1.35667 +** these two features are available.
 1.35668 +*/
 1.35669 +
 1.35670 +
 1.35671 +typedef struct PCache1 PCache1;
 1.35672 +typedef struct PgHdr1 PgHdr1;
 1.35673 +typedef struct PgFreeslot PgFreeslot;
 1.35674 +typedef struct PGroup PGroup;
 1.35675 +
 1.35676 +/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
 1.35677 +** of one or more PCaches that are able to recycle each others unpinned
 1.35678 +** pages when they are under memory pressure.  A PGroup is an instance of
 1.35679 +** the following object.
 1.35680 +**
 1.35681 +** This page cache implementation works in one of two modes:
 1.35682 +**
 1.35683 +**   (1)  Every PCache is the sole member of its own PGroup.  There is
 1.35684 +**        one PGroup per PCache.
 1.35685 +**
 1.35686 +**   (2)  There is a single global PGroup that all PCaches are a member
 1.35687 +**        of.
 1.35688 +**
 1.35689 +** Mode 1 uses more memory (since PCache instances are not able to rob
 1.35690 +** unused pages from other PCaches) but it also operates without a mutex,
 1.35691 +** and is therefore often faster.  Mode 2 requires a mutex in order to be
 1.35692 +** threadsafe, but recycles pages more efficiently.
 1.35693 +**
 1.35694 +** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
 1.35695 +** PGroup which is the pcache1.grp global variable and its mutex is
 1.35696 +** SQLITE_MUTEX_STATIC_LRU.
 1.35697 +*/
 1.35698 +struct PGroup {
 1.35699 +  sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
 1.35700 +  unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
 1.35701 +  unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
 1.35702 +  unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
 1.35703 +  unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
 1.35704 +  PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
 1.35705 +};
 1.35706 +
 1.35707 +/* Each page cache is an instance of the following object.  Every
 1.35708 +** open database file (including each in-memory database and each
 1.35709 +** temporary or transient database) has a single page cache which
 1.35710 +** is an instance of this object.
 1.35711 +**
 1.35712 +** Pointers to structures of this type are cast and returned as 
 1.35713 +** opaque sqlite3_pcache* handles.
 1.35714 +*/
 1.35715 +struct PCache1 {
 1.35716 +  /* Cache configuration parameters. Page size (szPage) and the purgeable
 1.35717 +  ** flag (bPurgeable) are set when the cache is created. nMax may be 
 1.35718 +  ** modified at any time by a call to the pcache1Cachesize() method.
 1.35719 +  ** The PGroup mutex must be held when accessing nMax.
 1.35720 +  */
 1.35721 +  PGroup *pGroup;                     /* PGroup this cache belongs to */
 1.35722 +  int szPage;                         /* Size of allocated pages in bytes */
 1.35723 +  int szExtra;                        /* Size of extra space in bytes */
 1.35724 +  int bPurgeable;                     /* True if cache is purgeable */
 1.35725 +  unsigned int nMin;                  /* Minimum number of pages reserved */
 1.35726 +  unsigned int nMax;                  /* Configured "cache_size" value */
 1.35727 +  unsigned int n90pct;                /* nMax*9/10 */
 1.35728 +  unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
 1.35729 +
 1.35730 +  /* Hash table of all pages. The following variables may only be accessed
 1.35731 +  ** when the accessor is holding the PGroup mutex.
 1.35732 +  */
 1.35733 +  unsigned int nRecyclable;           /* Number of pages in the LRU list */
 1.35734 +  unsigned int nPage;                 /* Total number of pages in apHash */
 1.35735 +  unsigned int nHash;                 /* Number of slots in apHash[] */
 1.35736 +  PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
 1.35737 +};
 1.35738 +
 1.35739 +/*
 1.35740 +** Each cache entry is represented by an instance of the following 
 1.35741 +** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
 1.35742 +** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
 1.35743 +** in memory.
 1.35744 +*/
 1.35745 +struct PgHdr1 {
 1.35746 +  sqlite3_pcache_page page;
 1.35747 +  unsigned int iKey;             /* Key value (page number) */
 1.35748 +  PgHdr1 *pNext;                 /* Next in hash table chain */
 1.35749 +  PCache1 *pCache;               /* Cache that currently owns this page */
 1.35750 +  PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 1.35751 +  PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 1.35752 +};
 1.35753 +
 1.35754 +/*
 1.35755 +** Free slots in the allocator used to divide up the buffer provided using
 1.35756 +** the SQLITE_CONFIG_PAGECACHE mechanism.
 1.35757 +*/
 1.35758 +struct PgFreeslot {
 1.35759 +  PgFreeslot *pNext;  /* Next free slot */
 1.35760 +};
 1.35761 +
 1.35762 +/*
 1.35763 +** Global data used by this cache.
 1.35764 +*/
 1.35765 +static SQLITE_WSD struct PCacheGlobal {
 1.35766 +  PGroup grp;                    /* The global PGroup for mode (2) */
 1.35767 +
 1.35768 +  /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
 1.35769 +  ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
 1.35770 +  ** fixed at sqlite3_initialize() time and do not require mutex protection.
 1.35771 +  ** The nFreeSlot and pFree values do require mutex protection.
 1.35772 +  */
 1.35773 +  int isInit;                    /* True if initialized */
 1.35774 +  int szSlot;                    /* Size of each free slot */
 1.35775 +  int nSlot;                     /* The number of pcache slots */
 1.35776 +  int nReserve;                  /* Try to keep nFreeSlot above this */
 1.35777 +  void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
 1.35778 +  /* Above requires no mutex.  Use mutex below for variable that follow. */
 1.35779 +  sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
 1.35780 +  PgFreeslot *pFree;             /* Free page blocks */
 1.35781 +  int nFreeSlot;                 /* Number of unused pcache slots */
 1.35782 +  /* The following value requires a mutex to change.  We skip the mutex on
 1.35783 +  ** reading because (1) most platforms read a 32-bit integer atomically and
 1.35784 +  ** (2) even if an incorrect value is read, no great harm is done since this
 1.35785 +  ** is really just an optimization. */
 1.35786 +  int bUnderPressure;            /* True if low on PAGECACHE memory */
 1.35787 +} pcache1_g;
 1.35788 +
 1.35789 +/*
 1.35790 +** All code in this file should access the global structure above via the
 1.35791 +** alias "pcache1". This ensures that the WSD emulation is used when
 1.35792 +** compiling for systems that do not support real WSD.
 1.35793 +*/
 1.35794 +#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
 1.35795 +
 1.35796 +/*
 1.35797 +** Macros to enter and leave the PCache LRU mutex.
 1.35798 +*/
 1.35799 +#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
 1.35800 +#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
 1.35801 +
 1.35802 +/******************************************************************************/
 1.35803 +/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
 1.35804 +
 1.35805 +/*
 1.35806 +** This function is called during initialization if a static buffer is 
 1.35807 +** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
 1.35808 +** verb to sqlite3_config(). Parameter pBuf points to an allocation large
 1.35809 +** enough to contain 'n' buffers of 'sz' bytes each.
 1.35810 +**
 1.35811 +** This routine is called from sqlite3_initialize() and so it is guaranteed
 1.35812 +** to be serialized already.  There is no need for further mutexing.
 1.35813 +*/
 1.35814 +SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
 1.35815 +  if( pcache1.isInit ){
 1.35816 +    PgFreeslot *p;
 1.35817 +    sz = ROUNDDOWN8(sz);
 1.35818 +    pcache1.szSlot = sz;
 1.35819 +    pcache1.nSlot = pcache1.nFreeSlot = n;
 1.35820 +    pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
 1.35821 +    pcache1.pStart = pBuf;
 1.35822 +    pcache1.pFree = 0;
 1.35823 +    pcache1.bUnderPressure = 0;
 1.35824 +    while( n-- ){
 1.35825 +      p = (PgFreeslot*)pBuf;
 1.35826 +      p->pNext = pcache1.pFree;
 1.35827 +      pcache1.pFree = p;
 1.35828 +      pBuf = (void*)&((char*)pBuf)[sz];
 1.35829 +    }
 1.35830 +    pcache1.pEnd = pBuf;
 1.35831 +  }
 1.35832 +}
 1.35833 +
 1.35834 +/*
 1.35835 +** Malloc function used within this file to allocate space from the buffer
 1.35836 +** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
 1.35837 +** such buffer exists or there is no space left in it, this function falls 
 1.35838 +** back to sqlite3Malloc().
 1.35839 +**
 1.35840 +** Multiple threads can run this routine at the same time.  Global variables
 1.35841 +** in pcache1 need to be protected via mutex.
 1.35842 +*/
 1.35843 +static void *pcache1Alloc(int nByte){
 1.35844 +  void *p = 0;
 1.35845 +  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 1.35846 +  sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
 1.35847 +  if( nByte<=pcache1.szSlot ){
 1.35848 +    sqlite3_mutex_enter(pcache1.mutex);
 1.35849 +    p = (PgHdr1 *)pcache1.pFree;
 1.35850 +    if( p ){
 1.35851 +      pcache1.pFree = pcache1.pFree->pNext;
 1.35852 +      pcache1.nFreeSlot--;
 1.35853 +      pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
 1.35854 +      assert( pcache1.nFreeSlot>=0 );
 1.35855 +      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
 1.35856 +    }
 1.35857 +    sqlite3_mutex_leave(pcache1.mutex);
 1.35858 +  }
 1.35859 +  if( p==0 ){
 1.35860 +    /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
 1.35861 +    ** it from sqlite3Malloc instead.
 1.35862 +    */
 1.35863 +    p = sqlite3Malloc(nByte);
 1.35864 +#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
 1.35865 +    if( p ){
 1.35866 +      int sz = sqlite3MallocSize(p);
 1.35867 +      sqlite3_mutex_enter(pcache1.mutex);
 1.35868 +      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
 1.35869 +      sqlite3_mutex_leave(pcache1.mutex);
 1.35870 +    }
 1.35871 +#endif
 1.35872 +    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 1.35873 +  }
 1.35874 +  return p;
 1.35875 +}
 1.35876 +
 1.35877 +/*
 1.35878 +** Free an allocated buffer obtained from pcache1Alloc().
 1.35879 +*/
 1.35880 +static int pcache1Free(void *p){
 1.35881 +  int nFreed = 0;
 1.35882 +  if( p==0 ) return 0;
 1.35883 +  if( p>=pcache1.pStart && p<pcache1.pEnd ){
 1.35884 +    PgFreeslot *pSlot;
 1.35885 +    sqlite3_mutex_enter(pcache1.mutex);
 1.35886 +    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
 1.35887 +    pSlot = (PgFreeslot*)p;
 1.35888 +    pSlot->pNext = pcache1.pFree;
 1.35889 +    pcache1.pFree = pSlot;
 1.35890 +    pcache1.nFreeSlot++;
 1.35891 +    pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
 1.35892 +    assert( pcache1.nFreeSlot<=pcache1.nSlot );
 1.35893 +    sqlite3_mutex_leave(pcache1.mutex);
 1.35894 +  }else{
 1.35895 +    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 1.35896 +    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 1.35897 +    nFreed = sqlite3MallocSize(p);
 1.35898 +#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
 1.35899 +    sqlite3_mutex_enter(pcache1.mutex);
 1.35900 +    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
 1.35901 +    sqlite3_mutex_leave(pcache1.mutex);
 1.35902 +#endif
 1.35903 +    sqlite3_free(p);
 1.35904 +  }
 1.35905 +  return nFreed;
 1.35906 +}
 1.35907 +
 1.35908 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 1.35909 +/*
 1.35910 +** Return the size of a pcache allocation
 1.35911 +*/
 1.35912 +static int pcache1MemSize(void *p){
 1.35913 +  if( p>=pcache1.pStart && p<pcache1.pEnd ){
 1.35914 +    return pcache1.szSlot;
 1.35915 +  }else{
 1.35916 +    int iSize;
 1.35917 +    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 1.35918 +    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 1.35919 +    iSize = sqlite3MallocSize(p);
 1.35920 +    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 1.35921 +    return iSize;
 1.35922 +  }
 1.35923 +}
 1.35924 +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 1.35925 +
 1.35926 +/*
 1.35927 +** Allocate a new page object initially associated with cache pCache.
 1.35928 +*/
 1.35929 +static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
 1.35930 +  PgHdr1 *p = 0;
 1.35931 +  void *pPg;
 1.35932 +
 1.35933 +  /* The group mutex must be released before pcache1Alloc() is called. This
 1.35934 +  ** is because it may call sqlite3_release_memory(), which assumes that 
 1.35935 +  ** this mutex is not held. */
 1.35936 +  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 1.35937 +  pcache1LeaveMutex(pCache->pGroup);
 1.35938 +#ifdef SQLITE_PCACHE_SEPARATE_HEADER
 1.35939 +  pPg = pcache1Alloc(pCache->szPage);
 1.35940 +  p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
 1.35941 +  if( !pPg || !p ){
 1.35942 +    pcache1Free(pPg);
 1.35943 +    sqlite3_free(p);
 1.35944 +    pPg = 0;
 1.35945 +  }
 1.35946 +#else
 1.35947 +  pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
 1.35948 +  p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
 1.35949 +#endif
 1.35950 +  pcache1EnterMutex(pCache->pGroup);
 1.35951 +
 1.35952 +  if( pPg ){
 1.35953 +    p->page.pBuf = pPg;
 1.35954 +    p->page.pExtra = &p[1];
 1.35955 +    if( pCache->bPurgeable ){
 1.35956 +      pCache->pGroup->nCurrentPage++;
 1.35957 +    }
 1.35958 +    return p;
 1.35959 +  }
 1.35960 +  return 0;
 1.35961 +}
 1.35962 +
 1.35963 +/*
 1.35964 +** Free a page object allocated by pcache1AllocPage().
 1.35965 +**
 1.35966 +** The pointer is allowed to be NULL, which is prudent.  But it turns out
 1.35967 +** that the current implementation happens to never call this routine
 1.35968 +** with a NULL pointer, so we mark the NULL test with ALWAYS().
 1.35969 +*/
 1.35970 +static void pcache1FreePage(PgHdr1 *p){
 1.35971 +  if( ALWAYS(p) ){
 1.35972 +    PCache1 *pCache = p->pCache;
 1.35973 +    assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
 1.35974 +    pcache1Free(p->page.pBuf);
 1.35975 +#ifdef SQLITE_PCACHE_SEPARATE_HEADER
 1.35976 +    sqlite3_free(p);
 1.35977 +#endif
 1.35978 +    if( pCache->bPurgeable ){
 1.35979 +      pCache->pGroup->nCurrentPage--;
 1.35980 +    }
 1.35981 +  }
 1.35982 +}
 1.35983 +
 1.35984 +/*
 1.35985 +** Malloc function used by SQLite to obtain space from the buffer configured
 1.35986 +** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
 1.35987 +** exists, this function falls back to sqlite3Malloc().
 1.35988 +*/
 1.35989 +SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
 1.35990 +  return pcache1Alloc(sz);
 1.35991 +}
 1.35992 +
 1.35993 +/*
 1.35994 +** Free an allocated buffer obtained from sqlite3PageMalloc().
 1.35995 +*/
 1.35996 +SQLITE_PRIVATE void sqlite3PageFree(void *p){
 1.35997 +  pcache1Free(p);
 1.35998 +}
 1.35999 +
 1.36000 +
 1.36001 +/*
 1.36002 +** Return true if it desirable to avoid allocating a new page cache
 1.36003 +** entry.
 1.36004 +**
 1.36005 +** If memory was allocated specifically to the page cache using
 1.36006 +** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
 1.36007 +** it is desirable to avoid allocating a new page cache entry because
 1.36008 +** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
 1.36009 +** for all page cache needs and we should not need to spill the
 1.36010 +** allocation onto the heap.
 1.36011 +**
 1.36012 +** Or, the heap is used for all page cache memory but the heap is
 1.36013 +** under memory pressure, then again it is desirable to avoid
 1.36014 +** allocating a new page cache entry in order to avoid stressing
 1.36015 +** the heap even further.
 1.36016 +*/
 1.36017 +static int pcache1UnderMemoryPressure(PCache1 *pCache){
 1.36018 +  if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
 1.36019 +    return pcache1.bUnderPressure;
 1.36020 +  }else{
 1.36021 +    return sqlite3HeapNearlyFull();
 1.36022 +  }
 1.36023 +}
 1.36024 +
 1.36025 +/******************************************************************************/
 1.36026 +/******** General Implementation Functions ************************************/
 1.36027 +
 1.36028 +/*
 1.36029 +** This function is used to resize the hash table used by the cache passed
 1.36030 +** as the first argument.
 1.36031 +**
 1.36032 +** The PCache mutex must be held when this function is called.
 1.36033 +*/
 1.36034 +static int pcache1ResizeHash(PCache1 *p){
 1.36035 +  PgHdr1 **apNew;
 1.36036 +  unsigned int nNew;
 1.36037 +  unsigned int i;
 1.36038 +
 1.36039 +  assert( sqlite3_mutex_held(p->pGroup->mutex) );
 1.36040 +
 1.36041 +  nNew = p->nHash*2;
 1.36042 +  if( nNew<256 ){
 1.36043 +    nNew = 256;
 1.36044 +  }
 1.36045 +
 1.36046 +  pcache1LeaveMutex(p->pGroup);
 1.36047 +  if( p->nHash ){ sqlite3BeginBenignMalloc(); }
 1.36048 +  apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
 1.36049 +  if( p->nHash ){ sqlite3EndBenignMalloc(); }
 1.36050 +  pcache1EnterMutex(p->pGroup);
 1.36051 +  if( apNew ){
 1.36052 +    for(i=0; i<p->nHash; i++){
 1.36053 +      PgHdr1 *pPage;
 1.36054 +      PgHdr1 *pNext = p->apHash[i];
 1.36055 +      while( (pPage = pNext)!=0 ){
 1.36056 +        unsigned int h = pPage->iKey % nNew;
 1.36057 +        pNext = pPage->pNext;
 1.36058 +        pPage->pNext = apNew[h];
 1.36059 +        apNew[h] = pPage;
 1.36060 +      }
 1.36061 +    }
 1.36062 +    sqlite3_free(p->apHash);
 1.36063 +    p->apHash = apNew;
 1.36064 +    p->nHash = nNew;
 1.36065 +  }
 1.36066 +
 1.36067 +  return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
 1.36068 +}
 1.36069 +
 1.36070 +/*
 1.36071 +** This function is used internally to remove the page pPage from the 
 1.36072 +** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
 1.36073 +** LRU list, then this function is a no-op.
 1.36074 +**
 1.36075 +** The PGroup mutex must be held when this function is called.
 1.36076 +**
 1.36077 +** If pPage is NULL then this routine is a no-op.
 1.36078 +*/
 1.36079 +static void pcache1PinPage(PgHdr1 *pPage){
 1.36080 +  PCache1 *pCache;
 1.36081 +  PGroup *pGroup;
 1.36082 +
 1.36083 +  if( pPage==0 ) return;
 1.36084 +  pCache = pPage->pCache;
 1.36085 +  pGroup = pCache->pGroup;
 1.36086 +  assert( sqlite3_mutex_held(pGroup->mutex) );
 1.36087 +  if( pPage->pLruNext || pPage==pGroup->pLruTail ){
 1.36088 +    if( pPage->pLruPrev ){
 1.36089 +      pPage->pLruPrev->pLruNext = pPage->pLruNext;
 1.36090 +    }
 1.36091 +    if( pPage->pLruNext ){
 1.36092 +      pPage->pLruNext->pLruPrev = pPage->pLruPrev;
 1.36093 +    }
 1.36094 +    if( pGroup->pLruHead==pPage ){
 1.36095 +      pGroup->pLruHead = pPage->pLruNext;
 1.36096 +    }
 1.36097 +    if( pGroup->pLruTail==pPage ){
 1.36098 +      pGroup->pLruTail = pPage->pLruPrev;
 1.36099 +    }
 1.36100 +    pPage->pLruNext = 0;
 1.36101 +    pPage->pLruPrev = 0;
 1.36102 +    pPage->pCache->nRecyclable--;
 1.36103 +  }
 1.36104 +}
 1.36105 +
 1.36106 +
 1.36107 +/*
 1.36108 +** Remove the page supplied as an argument from the hash table 
 1.36109 +** (PCache1.apHash structure) that it is currently stored in.
 1.36110 +**
 1.36111 +** The PGroup mutex must be held when this function is called.
 1.36112 +*/
 1.36113 +static void pcache1RemoveFromHash(PgHdr1 *pPage){
 1.36114 +  unsigned int h;
 1.36115 +  PCache1 *pCache = pPage->pCache;
 1.36116 +  PgHdr1 **pp;
 1.36117 +
 1.36118 +  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 1.36119 +  h = pPage->iKey % pCache->nHash;
 1.36120 +  for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
 1.36121 +  *pp = (*pp)->pNext;
 1.36122 +
 1.36123 +  pCache->nPage--;
 1.36124 +}
 1.36125 +
 1.36126 +/*
 1.36127 +** If there are currently more than nMaxPage pages allocated, try
 1.36128 +** to recycle pages to reduce the number allocated to nMaxPage.
 1.36129 +*/
 1.36130 +static void pcache1EnforceMaxPage(PGroup *pGroup){
 1.36131 +  assert( sqlite3_mutex_held(pGroup->mutex) );
 1.36132 +  while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
 1.36133 +    PgHdr1 *p = pGroup->pLruTail;
 1.36134 +    assert( p->pCache->pGroup==pGroup );
 1.36135 +    pcache1PinPage(p);
 1.36136 +    pcache1RemoveFromHash(p);
 1.36137 +    pcache1FreePage(p);
 1.36138 +  }
 1.36139 +}
 1.36140 +
 1.36141 +/*
 1.36142 +** Discard all pages from cache pCache with a page number (key value) 
 1.36143 +** greater than or equal to iLimit. Any pinned pages that meet this 
 1.36144 +** criteria are unpinned before they are discarded.
 1.36145 +**
 1.36146 +** The PCache mutex must be held when this function is called.
 1.36147 +*/
 1.36148 +static void pcache1TruncateUnsafe(
 1.36149 +  PCache1 *pCache,             /* The cache to truncate */
 1.36150 +  unsigned int iLimit          /* Drop pages with this pgno or larger */
 1.36151 +){
 1.36152 +  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
 1.36153 +  unsigned int h;
 1.36154 +  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 1.36155 +  for(h=0; h<pCache->nHash; h++){
 1.36156 +    PgHdr1 **pp = &pCache->apHash[h]; 
 1.36157 +    PgHdr1 *pPage;
 1.36158 +    while( (pPage = *pp)!=0 ){
 1.36159 +      if( pPage->iKey>=iLimit ){
 1.36160 +        pCache->nPage--;
 1.36161 +        *pp = pPage->pNext;
 1.36162 +        pcache1PinPage(pPage);
 1.36163 +        pcache1FreePage(pPage);
 1.36164 +      }else{
 1.36165 +        pp = &pPage->pNext;
 1.36166 +        TESTONLY( nPage++; )
 1.36167 +      }
 1.36168 +    }
 1.36169 +  }
 1.36170 +  assert( pCache->nPage==nPage );
 1.36171 +}
 1.36172 +
 1.36173 +/******************************************************************************/
 1.36174 +/******** sqlite3_pcache Methods **********************************************/
 1.36175 +
 1.36176 +/*
 1.36177 +** Implementation of the sqlite3_pcache.xInit method.
 1.36178 +*/
 1.36179 +static int pcache1Init(void *NotUsed){
 1.36180 +  UNUSED_PARAMETER(NotUsed);
 1.36181 +  assert( pcache1.isInit==0 );
 1.36182 +  memset(&pcache1, 0, sizeof(pcache1));
 1.36183 +  if( sqlite3GlobalConfig.bCoreMutex ){
 1.36184 +    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
 1.36185 +    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
 1.36186 +  }
 1.36187 +  pcache1.grp.mxPinned = 10;
 1.36188 +  pcache1.isInit = 1;
 1.36189 +  return SQLITE_OK;
 1.36190 +}
 1.36191 +
 1.36192 +/*
 1.36193 +** Implementation of the sqlite3_pcache.xShutdown method.
 1.36194 +** Note that the static mutex allocated in xInit does 
 1.36195 +** not need to be freed.
 1.36196 +*/
 1.36197 +static void pcache1Shutdown(void *NotUsed){
 1.36198 +  UNUSED_PARAMETER(NotUsed);
 1.36199 +  assert( pcache1.isInit!=0 );
 1.36200 +  memset(&pcache1, 0, sizeof(pcache1));
 1.36201 +}
 1.36202 +
 1.36203 +/*
 1.36204 +** Implementation of the sqlite3_pcache.xCreate method.
 1.36205 +**
 1.36206 +** Allocate a new cache.
 1.36207 +*/
 1.36208 +static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
 1.36209 +  PCache1 *pCache;      /* The newly created page cache */
 1.36210 +  PGroup *pGroup;       /* The group the new page cache will belong to */
 1.36211 +  int sz;               /* Bytes of memory required to allocate the new cache */
 1.36212 +
 1.36213 +  /*
 1.36214 +  ** The seperateCache variable is true if each PCache has its own private
 1.36215 +  ** PGroup.  In other words, separateCache is true for mode (1) where no
 1.36216 +  ** mutexing is required.
 1.36217 +  **
 1.36218 +  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
 1.36219 +  **
 1.36220 +  **   *  Always use a unified cache in single-threaded applications
 1.36221 +  **
 1.36222 +  **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
 1.36223 +  **      use separate caches (mode-1)
 1.36224 +  */
 1.36225 +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
 1.36226 +  const int separateCache = 0;
 1.36227 +#else
 1.36228 +  int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
 1.36229 +#endif
 1.36230 +
 1.36231 +  assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
 1.36232 +  assert( szExtra < 300 );
 1.36233 +
 1.36234 +  sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
 1.36235 +  pCache = (PCache1 *)sqlite3MallocZero(sz);
 1.36236 +  if( pCache ){
 1.36237 +    if( separateCache ){
 1.36238 +      pGroup = (PGroup*)&pCache[1];
 1.36239 +      pGroup->mxPinned = 10;
 1.36240 +    }else{
 1.36241 +      pGroup = &pcache1.grp;
 1.36242 +    }
 1.36243 +    pCache->pGroup = pGroup;
 1.36244 +    pCache->szPage = szPage;
 1.36245 +    pCache->szExtra = szExtra;
 1.36246 +    pCache->bPurgeable = (bPurgeable ? 1 : 0);
 1.36247 +    if( bPurgeable ){
 1.36248 +      pCache->nMin = 10;
 1.36249 +      pcache1EnterMutex(pGroup);
 1.36250 +      pGroup->nMinPage += pCache->nMin;
 1.36251 +      pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 1.36252 +      pcache1LeaveMutex(pGroup);
 1.36253 +    }
 1.36254 +  }
 1.36255 +  return (sqlite3_pcache *)pCache;
 1.36256 +}
 1.36257 +
 1.36258 +/*
 1.36259 +** Implementation of the sqlite3_pcache.xCachesize method. 
 1.36260 +**
 1.36261 +** Configure the cache_size limit for a cache.
 1.36262 +*/
 1.36263 +static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
 1.36264 +  PCache1 *pCache = (PCache1 *)p;
 1.36265 +  if( pCache->bPurgeable ){
 1.36266 +    PGroup *pGroup = pCache->pGroup;
 1.36267 +    pcache1EnterMutex(pGroup);
 1.36268 +    pGroup->nMaxPage += (nMax - pCache->nMax);
 1.36269 +    pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 1.36270 +    pCache->nMax = nMax;
 1.36271 +    pCache->n90pct = pCache->nMax*9/10;
 1.36272 +    pcache1EnforceMaxPage(pGroup);
 1.36273 +    pcache1LeaveMutex(pGroup);
 1.36274 +  }
 1.36275 +}
 1.36276 +
 1.36277 +/*
 1.36278 +** Implementation of the sqlite3_pcache.xShrink method. 
 1.36279 +**
 1.36280 +** Free up as much memory as possible.
 1.36281 +*/
 1.36282 +static void pcache1Shrink(sqlite3_pcache *p){
 1.36283 +  PCache1 *pCache = (PCache1*)p;
 1.36284 +  if( pCache->bPurgeable ){
 1.36285 +    PGroup *pGroup = pCache->pGroup;
 1.36286 +    int savedMaxPage;
 1.36287 +    pcache1EnterMutex(pGroup);
 1.36288 +    savedMaxPage = pGroup->nMaxPage;
 1.36289 +    pGroup->nMaxPage = 0;
 1.36290 +    pcache1EnforceMaxPage(pGroup);
 1.36291 +    pGroup->nMaxPage = savedMaxPage;
 1.36292 +    pcache1LeaveMutex(pGroup);
 1.36293 +  }
 1.36294 +}
 1.36295 +
 1.36296 +/*
 1.36297 +** Implementation of the sqlite3_pcache.xPagecount method. 
 1.36298 +*/
 1.36299 +static int pcache1Pagecount(sqlite3_pcache *p){
 1.36300 +  int n;
 1.36301 +  PCache1 *pCache = (PCache1*)p;
 1.36302 +  pcache1EnterMutex(pCache->pGroup);
 1.36303 +  n = pCache->nPage;
 1.36304 +  pcache1LeaveMutex(pCache->pGroup);
 1.36305 +  return n;
 1.36306 +}
 1.36307 +
 1.36308 +/*
 1.36309 +** Implementation of the sqlite3_pcache.xFetch method. 
 1.36310 +**
 1.36311 +** Fetch a page by key value.
 1.36312 +**
 1.36313 +** Whether or not a new page may be allocated by this function depends on
 1.36314 +** the value of the createFlag argument.  0 means do not allocate a new
 1.36315 +** page.  1 means allocate a new page if space is easily available.  2 
 1.36316 +** means to try really hard to allocate a new page.
 1.36317 +**
 1.36318 +** For a non-purgeable cache (a cache used as the storage for an in-memory
 1.36319 +** database) there is really no difference between createFlag 1 and 2.  So
 1.36320 +** the calling function (pcache.c) will never have a createFlag of 1 on
 1.36321 +** a non-purgeable cache.
 1.36322 +**
 1.36323 +** There are three different approaches to obtaining space for a page,
 1.36324 +** depending on the value of parameter createFlag (which may be 0, 1 or 2).
 1.36325 +**
 1.36326 +**   1. Regardless of the value of createFlag, the cache is searched for a 
 1.36327 +**      copy of the requested page. If one is found, it is returned.
 1.36328 +**
 1.36329 +**   2. If createFlag==0 and the page is not already in the cache, NULL is
 1.36330 +**      returned.
 1.36331 +**
 1.36332 +**   3. If createFlag is 1, and the page is not already in the cache, then
 1.36333 +**      return NULL (do not allocate a new page) if any of the following
 1.36334 +**      conditions are true:
 1.36335 +**
 1.36336 +**       (a) the number of pages pinned by the cache is greater than
 1.36337 +**           PCache1.nMax, or
 1.36338 +**
 1.36339 +**       (b) the number of pages pinned by the cache is greater than
 1.36340 +**           the sum of nMax for all purgeable caches, less the sum of 
 1.36341 +**           nMin for all other purgeable caches, or
 1.36342 +**
 1.36343 +**   4. If none of the first three conditions apply and the cache is marked
 1.36344 +**      as purgeable, and if one of the following is true:
 1.36345 +**
 1.36346 +**       (a) The number of pages allocated for the cache is already 
 1.36347 +**           PCache1.nMax, or
 1.36348 +**
 1.36349 +**       (b) The number of pages allocated for all purgeable caches is
 1.36350 +**           already equal to or greater than the sum of nMax for all
 1.36351 +**           purgeable caches,
 1.36352 +**
 1.36353 +**       (c) The system is under memory pressure and wants to avoid
 1.36354 +**           unnecessary pages cache entry allocations
 1.36355 +**
 1.36356 +**      then attempt to recycle a page from the LRU list. If it is the right
 1.36357 +**      size, return the recycled buffer. Otherwise, free the buffer and
 1.36358 +**      proceed to step 5. 
 1.36359 +**
 1.36360 +**   5. Otherwise, allocate and return a new page buffer.
 1.36361 +*/
 1.36362 +static sqlite3_pcache_page *pcache1Fetch(
 1.36363 +  sqlite3_pcache *p, 
 1.36364 +  unsigned int iKey, 
 1.36365 +  int createFlag
 1.36366 +){
 1.36367 +  unsigned int nPinned;
 1.36368 +  PCache1 *pCache = (PCache1 *)p;
 1.36369 +  PGroup *pGroup;
 1.36370 +  PgHdr1 *pPage = 0;
 1.36371 +
 1.36372 +  assert( pCache->bPurgeable || createFlag!=1 );
 1.36373 +  assert( pCache->bPurgeable || pCache->nMin==0 );
 1.36374 +  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
 1.36375 +  assert( pCache->nMin==0 || pCache->bPurgeable );
 1.36376 +  pcache1EnterMutex(pGroup = pCache->pGroup);
 1.36377 +
 1.36378 +  /* Step 1: Search the hash table for an existing entry. */
 1.36379 +  if( pCache->nHash>0 ){
 1.36380 +    unsigned int h = iKey % pCache->nHash;
 1.36381 +    for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
 1.36382 +  }
 1.36383 +
 1.36384 +  /* Step 2: Abort if no existing page is found and createFlag is 0 */
 1.36385 +  if( pPage || createFlag==0 ){
 1.36386 +    pcache1PinPage(pPage);
 1.36387 +    goto fetch_out;
 1.36388 +  }
 1.36389 +
 1.36390 +  /* The pGroup local variable will normally be initialized by the
 1.36391 +  ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
 1.36392 +  ** then pcache1EnterMutex() is a no-op, so we have to initialize the
 1.36393 +  ** local variable here.  Delaying the initialization of pGroup is an
 1.36394 +  ** optimization:  The common case is to exit the module before reaching
 1.36395 +  ** this point.
 1.36396 +  */
 1.36397 +#ifdef SQLITE_MUTEX_OMIT
 1.36398 +  pGroup = pCache->pGroup;
 1.36399 +#endif
 1.36400 +
 1.36401 +  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
 1.36402 +  assert( pCache->nPage >= pCache->nRecyclable );
 1.36403 +  nPinned = pCache->nPage - pCache->nRecyclable;
 1.36404 +  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
 1.36405 +  assert( pCache->n90pct == pCache->nMax*9/10 );
 1.36406 +  if( createFlag==1 && (
 1.36407 +        nPinned>=pGroup->mxPinned
 1.36408 +     || nPinned>=pCache->n90pct
 1.36409 +     || pcache1UnderMemoryPressure(pCache)
 1.36410 +  )){
 1.36411 +    goto fetch_out;
 1.36412 +  }
 1.36413 +
 1.36414 +  if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
 1.36415 +    goto fetch_out;
 1.36416 +  }
 1.36417 +
 1.36418 +  /* Step 4. Try to recycle a page. */
 1.36419 +  if( pCache->bPurgeable && pGroup->pLruTail && (
 1.36420 +         (pCache->nPage+1>=pCache->nMax)
 1.36421 +      || pGroup->nCurrentPage>=pGroup->nMaxPage
 1.36422 +      || pcache1UnderMemoryPressure(pCache)
 1.36423 +  )){
 1.36424 +    PCache1 *pOther;
 1.36425 +    pPage = pGroup->pLruTail;
 1.36426 +    pcache1RemoveFromHash(pPage);
 1.36427 +    pcache1PinPage(pPage);
 1.36428 +    pOther = pPage->pCache;
 1.36429 +
 1.36430 +    /* We want to verify that szPage and szExtra are the same for pOther
 1.36431 +    ** and pCache.  Assert that we can verify this by comparing sums. */
 1.36432 +    assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
 1.36433 +    assert( pCache->szExtra<512 );
 1.36434 +    assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
 1.36435 +    assert( pOther->szExtra<512 );
 1.36436 +
 1.36437 +    if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
 1.36438 +      pcache1FreePage(pPage);
 1.36439 +      pPage = 0;
 1.36440 +    }else{
 1.36441 +      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
 1.36442 +    }
 1.36443 +  }
 1.36444 +
 1.36445 +  /* Step 5. If a usable page buffer has still not been found, 
 1.36446 +  ** attempt to allocate a new one. 
 1.36447 +  */
 1.36448 +  if( !pPage ){
 1.36449 +    if( createFlag==1 ) sqlite3BeginBenignMalloc();
 1.36450 +    pPage = pcache1AllocPage(pCache);
 1.36451 +    if( createFlag==1 ) sqlite3EndBenignMalloc();
 1.36452 +  }
 1.36453 +
 1.36454 +  if( pPage ){
 1.36455 +    unsigned int h = iKey % pCache->nHash;
 1.36456 +    pCache->nPage++;
 1.36457 +    pPage->iKey = iKey;
 1.36458 +    pPage->pNext = pCache->apHash[h];
 1.36459 +    pPage->pCache = pCache;
 1.36460 +    pPage->pLruPrev = 0;
 1.36461 +    pPage->pLruNext = 0;
 1.36462 +    *(void **)pPage->page.pExtra = 0;
 1.36463 +    pCache->apHash[h] = pPage;
 1.36464 +  }
 1.36465 +
 1.36466 +fetch_out:
 1.36467 +  if( pPage && iKey>pCache->iMaxKey ){
 1.36468 +    pCache->iMaxKey = iKey;
 1.36469 +  }
 1.36470 +  pcache1LeaveMutex(pGroup);
 1.36471 +  return &pPage->page;
 1.36472 +}
 1.36473 +
 1.36474 +
 1.36475 +/*
 1.36476 +** Implementation of the sqlite3_pcache.xUnpin method.
 1.36477 +**
 1.36478 +** Mark a page as unpinned (eligible for asynchronous recycling).
 1.36479 +*/
 1.36480 +static void pcache1Unpin(
 1.36481 +  sqlite3_pcache *p, 
 1.36482 +  sqlite3_pcache_page *pPg, 
 1.36483 +  int reuseUnlikely
 1.36484 +){
 1.36485 +  PCache1 *pCache = (PCache1 *)p;
 1.36486 +  PgHdr1 *pPage = (PgHdr1 *)pPg;
 1.36487 +  PGroup *pGroup = pCache->pGroup;
 1.36488 + 
 1.36489 +  assert( pPage->pCache==pCache );
 1.36490 +  pcache1EnterMutex(pGroup);
 1.36491 +
 1.36492 +  /* It is an error to call this function if the page is already 
 1.36493 +  ** part of the PGroup LRU list.
 1.36494 +  */
 1.36495 +  assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
 1.36496 +  assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
 1.36497 +
 1.36498 +  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
 1.36499 +    pcache1RemoveFromHash(pPage);
 1.36500 +    pcache1FreePage(pPage);
 1.36501 +  }else{
 1.36502 +    /* Add the page to the PGroup LRU list. */
 1.36503 +    if( pGroup->pLruHead ){
 1.36504 +      pGroup->pLruHead->pLruPrev = pPage;
 1.36505 +      pPage->pLruNext = pGroup->pLruHead;
 1.36506 +      pGroup->pLruHead = pPage;
 1.36507 +    }else{
 1.36508 +      pGroup->pLruTail = pPage;
 1.36509 +      pGroup->pLruHead = pPage;
 1.36510 +    }
 1.36511 +    pCache->nRecyclable++;
 1.36512 +  }
 1.36513 +
 1.36514 +  pcache1LeaveMutex(pCache->pGroup);
 1.36515 +}
 1.36516 +
 1.36517 +/*
 1.36518 +** Implementation of the sqlite3_pcache.xRekey method. 
 1.36519 +*/
 1.36520 +static void pcache1Rekey(
 1.36521 +  sqlite3_pcache *p,
 1.36522 +  sqlite3_pcache_page *pPg,
 1.36523 +  unsigned int iOld,
 1.36524 +  unsigned int iNew
 1.36525 +){
 1.36526 +  PCache1 *pCache = (PCache1 *)p;
 1.36527 +  PgHdr1 *pPage = (PgHdr1 *)pPg;
 1.36528 +  PgHdr1 **pp;
 1.36529 +  unsigned int h; 
 1.36530 +  assert( pPage->iKey==iOld );
 1.36531 +  assert( pPage->pCache==pCache );
 1.36532 +
 1.36533 +  pcache1EnterMutex(pCache->pGroup);
 1.36534 +
 1.36535 +  h = iOld%pCache->nHash;
 1.36536 +  pp = &pCache->apHash[h];
 1.36537 +  while( (*pp)!=pPage ){
 1.36538 +    pp = &(*pp)->pNext;
 1.36539 +  }
 1.36540 +  *pp = pPage->pNext;
 1.36541 +
 1.36542 +  h = iNew%pCache->nHash;
 1.36543 +  pPage->iKey = iNew;
 1.36544 +  pPage->pNext = pCache->apHash[h];
 1.36545 +  pCache->apHash[h] = pPage;
 1.36546 +  if( iNew>pCache->iMaxKey ){
 1.36547 +    pCache->iMaxKey = iNew;
 1.36548 +  }
 1.36549 +
 1.36550 +  pcache1LeaveMutex(pCache->pGroup);
 1.36551 +}
 1.36552 +
 1.36553 +/*
 1.36554 +** Implementation of the sqlite3_pcache.xTruncate method. 
 1.36555 +**
 1.36556 +** Discard all unpinned pages in the cache with a page number equal to
 1.36557 +** or greater than parameter iLimit. Any pinned pages with a page number
 1.36558 +** equal to or greater than iLimit are implicitly unpinned.
 1.36559 +*/
 1.36560 +static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
 1.36561 +  PCache1 *pCache = (PCache1 *)p;
 1.36562 +  pcache1EnterMutex(pCache->pGroup);
 1.36563 +  if( iLimit<=pCache->iMaxKey ){
 1.36564 +    pcache1TruncateUnsafe(pCache, iLimit);
 1.36565 +    pCache->iMaxKey = iLimit-1;
 1.36566 +  }
 1.36567 +  pcache1LeaveMutex(pCache->pGroup);
 1.36568 +}
 1.36569 +
 1.36570 +/*
 1.36571 +** Implementation of the sqlite3_pcache.xDestroy method. 
 1.36572 +**
 1.36573 +** Destroy a cache allocated using pcache1Create().
 1.36574 +*/
 1.36575 +static void pcache1Destroy(sqlite3_pcache *p){
 1.36576 +  PCache1 *pCache = (PCache1 *)p;
 1.36577 +  PGroup *pGroup = pCache->pGroup;
 1.36578 +  assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
 1.36579 +  pcache1EnterMutex(pGroup);
 1.36580 +  pcache1TruncateUnsafe(pCache, 0);
 1.36581 +  assert( pGroup->nMaxPage >= pCache->nMax );
 1.36582 +  pGroup->nMaxPage -= pCache->nMax;
 1.36583 +  assert( pGroup->nMinPage >= pCache->nMin );
 1.36584 +  pGroup->nMinPage -= pCache->nMin;
 1.36585 +  pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 1.36586 +  pcache1EnforceMaxPage(pGroup);
 1.36587 +  pcache1LeaveMutex(pGroup);
 1.36588 +  sqlite3_free(pCache->apHash);
 1.36589 +  sqlite3_free(pCache);
 1.36590 +}
 1.36591 +
 1.36592 +/*
 1.36593 +** This function is called during initialization (sqlite3_initialize()) to
 1.36594 +** install the default pluggable cache module, assuming the user has not
 1.36595 +** already provided an alternative.
 1.36596 +*/
 1.36597 +SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
 1.36598 +  static const sqlite3_pcache_methods2 defaultMethods = {
 1.36599 +    1,                       /* iVersion */
 1.36600 +    0,                       /* pArg */
 1.36601 +    pcache1Init,             /* xInit */
 1.36602 +    pcache1Shutdown,         /* xShutdown */
 1.36603 +    pcache1Create,           /* xCreate */
 1.36604 +    pcache1Cachesize,        /* xCachesize */
 1.36605 +    pcache1Pagecount,        /* xPagecount */
 1.36606 +    pcache1Fetch,            /* xFetch */
 1.36607 +    pcache1Unpin,            /* xUnpin */
 1.36608 +    pcache1Rekey,            /* xRekey */
 1.36609 +    pcache1Truncate,         /* xTruncate */
 1.36610 +    pcache1Destroy,          /* xDestroy */
 1.36611 +    pcache1Shrink            /* xShrink */
 1.36612 +  };
 1.36613 +  sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
 1.36614 +}
 1.36615 +
 1.36616 +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 1.36617 +/*
 1.36618 +** This function is called to free superfluous dynamically allocated memory
 1.36619 +** held by the pager system. Memory in use by any SQLite pager allocated
 1.36620 +** by the current thread may be sqlite3_free()ed.
 1.36621 +**
 1.36622 +** nReq is the number of bytes of memory required. Once this much has
 1.36623 +** been released, the function returns. The return value is the total number 
 1.36624 +** of bytes of memory released.
 1.36625 +*/
 1.36626 +SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
 1.36627 +  int nFree = 0;
 1.36628 +  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 1.36629 +  assert( sqlite3_mutex_notheld(pcache1.mutex) );
 1.36630 +  if( pcache1.pStart==0 ){
 1.36631 +    PgHdr1 *p;
 1.36632 +    pcache1EnterMutex(&pcache1.grp);
 1.36633 +    while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
 1.36634 +      nFree += pcache1MemSize(p->page.pBuf);
 1.36635 +#ifdef SQLITE_PCACHE_SEPARATE_HEADER
 1.36636 +      nFree += sqlite3MemSize(p);
 1.36637 +#endif
 1.36638 +      pcache1PinPage(p);
 1.36639 +      pcache1RemoveFromHash(p);
 1.36640 +      pcache1FreePage(p);
 1.36641 +    }
 1.36642 +    pcache1LeaveMutex(&pcache1.grp);
 1.36643 +  }
 1.36644 +  return nFree;
 1.36645 +}
 1.36646 +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 1.36647 +
 1.36648 +#ifdef SQLITE_TEST
 1.36649 +/*
 1.36650 +** This function is used by test procedures to inspect the internal state
 1.36651 +** of the global cache.
 1.36652 +*/
 1.36653 +SQLITE_PRIVATE void sqlite3PcacheStats(
 1.36654 +  int *pnCurrent,      /* OUT: Total number of pages cached */
 1.36655 +  int *pnMax,          /* OUT: Global maximum cache size */
 1.36656 +  int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
 1.36657 +  int *pnRecyclable    /* OUT: Total number of pages available for recycling */
 1.36658 +){
 1.36659 +  PgHdr1 *p;
 1.36660 +  int nRecyclable = 0;
 1.36661 +  for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
 1.36662 +    nRecyclable++;
 1.36663 +  }
 1.36664 +  *pnCurrent = pcache1.grp.nCurrentPage;
 1.36665 +  *pnMax = (int)pcache1.grp.nMaxPage;
 1.36666 +  *pnMin = (int)pcache1.grp.nMinPage;
 1.36667 +  *pnRecyclable = nRecyclable;
 1.36668 +}
 1.36669 +#endif
 1.36670 +
 1.36671 +/************** End of pcache1.c *********************************************/
 1.36672 +/************** Begin file rowset.c ******************************************/
 1.36673 +/*
 1.36674 +** 2008 December 3
 1.36675 +**
 1.36676 +** The author disclaims copyright to this source code.  In place of
 1.36677 +** a legal notice, here is a blessing:
 1.36678 +**
 1.36679 +**    May you do good and not evil.
 1.36680 +**    May you find forgiveness for yourself and forgive others.
 1.36681 +**    May you share freely, never taking more than you give.
 1.36682 +**
 1.36683 +*************************************************************************
 1.36684 +**
 1.36685 +** This module implements an object we call a "RowSet".
 1.36686 +**
 1.36687 +** The RowSet object is a collection of rowids.  Rowids
 1.36688 +** are inserted into the RowSet in an arbitrary order.  Inserts
 1.36689 +** can be intermixed with tests to see if a given rowid has been
 1.36690 +** previously inserted into the RowSet.
 1.36691 +**
 1.36692 +** After all inserts are finished, it is possible to extract the
 1.36693 +** elements of the RowSet in sorted order.  Once this extraction
 1.36694 +** process has started, no new elements may be inserted.
 1.36695 +**
 1.36696 +** Hence, the primitive operations for a RowSet are:
 1.36697 +**
 1.36698 +**    CREATE
 1.36699 +**    INSERT
 1.36700 +**    TEST
 1.36701 +**    SMALLEST
 1.36702 +**    DESTROY
 1.36703 +**
 1.36704 +** The CREATE and DESTROY primitives are the constructor and destructor,
 1.36705 +** obviously.  The INSERT primitive adds a new element to the RowSet.
 1.36706 +** TEST checks to see if an element is already in the RowSet.  SMALLEST
 1.36707 +** extracts the least value from the RowSet.
 1.36708 +**
 1.36709 +** The INSERT primitive might allocate additional memory.  Memory is
 1.36710 +** allocated in chunks so most INSERTs do no allocation.  There is an 
 1.36711 +** upper bound on the size of allocated memory.  No memory is freed
 1.36712 +** until DESTROY.
 1.36713 +**
 1.36714 +** The TEST primitive includes a "batch" number.  The TEST primitive
 1.36715 +** will only see elements that were inserted before the last change
 1.36716 +** in the batch number.  In other words, if an INSERT occurs between
 1.36717 +** two TESTs where the TESTs have the same batch nubmer, then the
 1.36718 +** value added by the INSERT will not be visible to the second TEST.
 1.36719 +** The initial batch number is zero, so if the very first TEST contains
 1.36720 +** a non-zero batch number, it will see all prior INSERTs.
 1.36721 +**
 1.36722 +** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
 1.36723 +** that is attempted.
 1.36724 +**
 1.36725 +** The cost of an INSERT is roughly constant.  (Sometime new memory
 1.36726 +** has to be allocated on an INSERT.)  The cost of a TEST with a new
 1.36727 +** batch number is O(NlogN) where N is the number of elements in the RowSet.
 1.36728 +** The cost of a TEST using the same batch number is O(logN).  The cost
 1.36729 +** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
 1.36730 +** primitives are constant time.  The cost of DESTROY is O(N).
 1.36731 +**
 1.36732 +** There is an added cost of O(N) when switching between TEST and
 1.36733 +** SMALLEST primitives.
 1.36734 +*/
 1.36735 +
 1.36736 +
 1.36737 +/*
 1.36738 +** Target size for allocation chunks.
 1.36739 +*/
 1.36740 +#define ROWSET_ALLOCATION_SIZE 1024
 1.36741 +
 1.36742 +/*
 1.36743 +** The number of rowset entries per allocation chunk.
 1.36744 +*/
 1.36745 +#define ROWSET_ENTRY_PER_CHUNK  \
 1.36746 +                       ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
 1.36747 +
 1.36748 +/*
 1.36749 +** Each entry in a RowSet is an instance of the following object.
 1.36750 +**
 1.36751 +** This same object is reused to store a linked list of trees of RowSetEntry
 1.36752 +** objects.  In that alternative use, pRight points to the next entry
 1.36753 +** in the list, pLeft points to the tree, and v is unused.  The
 1.36754 +** RowSet.pForest value points to the head of this forest list.
 1.36755 +*/
 1.36756 +struct RowSetEntry {            
 1.36757 +  i64 v;                        /* ROWID value for this entry */
 1.36758 +  struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
 1.36759 +  struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
 1.36760 +};
 1.36761 +
 1.36762 +/*
 1.36763 +** RowSetEntry objects are allocated in large chunks (instances of the
 1.36764 +** following structure) to reduce memory allocation overhead.  The
 1.36765 +** chunks are kept on a linked list so that they can be deallocated
 1.36766 +** when the RowSet is destroyed.
 1.36767 +*/
 1.36768 +struct RowSetChunk {
 1.36769 +  struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
 1.36770 +  struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
 1.36771 +};
 1.36772 +
 1.36773 +/*
 1.36774 +** A RowSet in an instance of the following structure.
 1.36775 +**
 1.36776 +** A typedef of this structure if found in sqliteInt.h.
 1.36777 +*/
 1.36778 +struct RowSet {
 1.36779 +  struct RowSetChunk *pChunk;    /* List of all chunk allocations */
 1.36780 +  sqlite3 *db;                   /* The database connection */
 1.36781 +  struct RowSetEntry *pEntry;    /* List of entries using pRight */
 1.36782 +  struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
 1.36783 +  struct RowSetEntry *pFresh;    /* Source of new entry objects */
 1.36784 +  struct RowSetEntry *pForest;   /* List of binary trees of entries */
 1.36785 +  u16 nFresh;                    /* Number of objects on pFresh */
 1.36786 +  u8 rsFlags;                    /* Various flags */
 1.36787 +  u8 iBatch;                     /* Current insert batch */
 1.36788 +};
 1.36789 +
 1.36790 +/*
 1.36791 +** Allowed values for RowSet.rsFlags
 1.36792 +*/
 1.36793 +#define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
 1.36794 +#define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
 1.36795 +
 1.36796 +/*
 1.36797 +** Turn bulk memory into a RowSet object.  N bytes of memory
 1.36798 +** are available at pSpace.  The db pointer is used as a memory context
 1.36799 +** for any subsequent allocations that need to occur.
 1.36800 +** Return a pointer to the new RowSet object.
 1.36801 +**
 1.36802 +** It must be the case that N is sufficient to make a Rowset.  If not
 1.36803 +** an assertion fault occurs.
 1.36804 +** 
 1.36805 +** If N is larger than the minimum, use the surplus as an initial
 1.36806 +** allocation of entries available to be filled.
 1.36807 +*/
 1.36808 +SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
 1.36809 +  RowSet *p;
 1.36810 +  assert( N >= ROUND8(sizeof(*p)) );
 1.36811 +  p = pSpace;
 1.36812 +  p->pChunk = 0;
 1.36813 +  p->db = db;
 1.36814 +  p->pEntry = 0;
 1.36815 +  p->pLast = 0;
 1.36816 +  p->pForest = 0;
 1.36817 +  p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
 1.36818 +  p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
 1.36819 +  p->rsFlags = ROWSET_SORTED;
 1.36820 +  p->iBatch = 0;
 1.36821 +  return p;
 1.36822 +}
 1.36823 +
 1.36824 +/*
 1.36825 +** Deallocate all chunks from a RowSet.  This frees all memory that
 1.36826 +** the RowSet has allocated over its lifetime.  This routine is
 1.36827 +** the destructor for the RowSet.
 1.36828 +*/
 1.36829 +SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
 1.36830 +  struct RowSetChunk *pChunk, *pNextChunk;
 1.36831 +  for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
 1.36832 +    pNextChunk = pChunk->pNextChunk;
 1.36833 +    sqlite3DbFree(p->db, pChunk);
 1.36834 +  }
 1.36835 +  p->pChunk = 0;
 1.36836 +  p->nFresh = 0;
 1.36837 +  p->pEntry = 0;
 1.36838 +  p->pLast = 0;
 1.36839 +  p->pForest = 0;
 1.36840 +  p->rsFlags = ROWSET_SORTED;
 1.36841 +}
 1.36842 +
 1.36843 +/*
 1.36844 +** Allocate a new RowSetEntry object that is associated with the
 1.36845 +** given RowSet.  Return a pointer to the new and completely uninitialized
 1.36846 +** objected.
 1.36847 +**
 1.36848 +** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
 1.36849 +** routine returns NULL.
 1.36850 +*/
 1.36851 +static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
 1.36852 +  assert( p!=0 );
 1.36853 +  if( p->nFresh==0 ){
 1.36854 +    struct RowSetChunk *pNew;
 1.36855 +    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
 1.36856 +    if( pNew==0 ){
 1.36857 +      return 0;
 1.36858 +    }
 1.36859 +    pNew->pNextChunk = p->pChunk;
 1.36860 +    p->pChunk = pNew;
 1.36861 +    p->pFresh = pNew->aEntry;
 1.36862 +    p->nFresh = ROWSET_ENTRY_PER_CHUNK;
 1.36863 +  }
 1.36864 +  p->nFresh--;
 1.36865 +  return p->pFresh++;
 1.36866 +}
 1.36867 +
 1.36868 +/*
 1.36869 +** Insert a new value into a RowSet.
 1.36870 +**
 1.36871 +** The mallocFailed flag of the database connection is set if a
 1.36872 +** memory allocation fails.
 1.36873 +*/
 1.36874 +SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
 1.36875 +  struct RowSetEntry *pEntry;  /* The new entry */
 1.36876 +  struct RowSetEntry *pLast;   /* The last prior entry */
 1.36877 +
 1.36878 +  /* This routine is never called after sqlite3RowSetNext() */
 1.36879 +  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
 1.36880 +
 1.36881 +  pEntry = rowSetEntryAlloc(p);
 1.36882 +  if( pEntry==0 ) return;
 1.36883 +  pEntry->v = rowid;
 1.36884 +  pEntry->pRight = 0;
 1.36885 +  pLast = p->pLast;
 1.36886 +  if( pLast ){
 1.36887 +    if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
 1.36888 +      p->rsFlags &= ~ROWSET_SORTED;
 1.36889 +    }
 1.36890 +    pLast->pRight = pEntry;
 1.36891 +  }else{
 1.36892 +    p->pEntry = pEntry;
 1.36893 +  }
 1.36894 +  p->pLast = pEntry;
 1.36895 +}
 1.36896 +
 1.36897 +/*
 1.36898 +** Merge two lists of RowSetEntry objects.  Remove duplicates.
 1.36899 +**
 1.36900 +** The input lists are connected via pRight pointers and are 
 1.36901 +** assumed to each already be in sorted order.
 1.36902 +*/
 1.36903 +static struct RowSetEntry *rowSetEntryMerge(
 1.36904 +  struct RowSetEntry *pA,    /* First sorted list to be merged */
 1.36905 +  struct RowSetEntry *pB     /* Second sorted list to be merged */
 1.36906 +){
 1.36907 +  struct RowSetEntry head;
 1.36908 +  struct RowSetEntry *pTail;
 1.36909 +
 1.36910 +  pTail = &head;
 1.36911 +  while( pA && pB ){
 1.36912 +    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
 1.36913 +    assert( pB->pRight==0 || pB->v<=pB->pRight->v );
 1.36914 +    if( pA->v<pB->v ){
 1.36915 +      pTail->pRight = pA;
 1.36916 +      pA = pA->pRight;
 1.36917 +      pTail = pTail->pRight;
 1.36918 +    }else if( pB->v<pA->v ){
 1.36919 +      pTail->pRight = pB;
 1.36920 +      pB = pB->pRight;
 1.36921 +      pTail = pTail->pRight;
 1.36922 +    }else{
 1.36923 +      pA = pA->pRight;
 1.36924 +    }
 1.36925 +  }
 1.36926 +  if( pA ){
 1.36927 +    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
 1.36928 +    pTail->pRight = pA;
 1.36929 +  }else{
 1.36930 +    assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
 1.36931 +    pTail->pRight = pB;
 1.36932 +  }
 1.36933 +  return head.pRight;
 1.36934 +}
 1.36935 +
 1.36936 +/*
 1.36937 +** Sort all elements on the list of RowSetEntry objects into order of
 1.36938 +** increasing v.
 1.36939 +*/ 
 1.36940 +static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
 1.36941 +  unsigned int i;
 1.36942 +  struct RowSetEntry *pNext, *aBucket[40];
 1.36943 +
 1.36944 +  memset(aBucket, 0, sizeof(aBucket));
 1.36945 +  while( pIn ){
 1.36946 +    pNext = pIn->pRight;
 1.36947 +    pIn->pRight = 0;
 1.36948 +    for(i=0; aBucket[i]; i++){
 1.36949 +      pIn = rowSetEntryMerge(aBucket[i], pIn);
 1.36950 +      aBucket[i] = 0;
 1.36951 +    }
 1.36952 +    aBucket[i] = pIn;
 1.36953 +    pIn = pNext;
 1.36954 +  }
 1.36955 +  pIn = 0;
 1.36956 +  for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
 1.36957 +    pIn = rowSetEntryMerge(pIn, aBucket[i]);
 1.36958 +  }
 1.36959 +  return pIn;
 1.36960 +}
 1.36961 +
 1.36962 +
 1.36963 +/*
 1.36964 +** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
 1.36965 +** Convert this tree into a linked list connected by the pRight pointers
 1.36966 +** and return pointers to the first and last elements of the new list.
 1.36967 +*/
 1.36968 +static void rowSetTreeToList(
 1.36969 +  struct RowSetEntry *pIn,         /* Root of the input tree */
 1.36970 +  struct RowSetEntry **ppFirst,    /* Write head of the output list here */
 1.36971 +  struct RowSetEntry **ppLast      /* Write tail of the output list here */
 1.36972 +){
 1.36973 +  assert( pIn!=0 );
 1.36974 +  if( pIn->pLeft ){
 1.36975 +    struct RowSetEntry *p;
 1.36976 +    rowSetTreeToList(pIn->pLeft, ppFirst, &p);
 1.36977 +    p->pRight = pIn;
 1.36978 +  }else{
 1.36979 +    *ppFirst = pIn;
 1.36980 +  }
 1.36981 +  if( pIn->pRight ){
 1.36982 +    rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
 1.36983 +  }else{
 1.36984 +    *ppLast = pIn;
 1.36985 +  }
 1.36986 +  assert( (*ppLast)->pRight==0 );
 1.36987 +}
 1.36988 +
 1.36989 +
 1.36990 +/*
 1.36991 +** Convert a sorted list of elements (connected by pRight) into a binary
 1.36992 +** tree with depth of iDepth.  A depth of 1 means the tree contains a single
 1.36993 +** node taken from the head of *ppList.  A depth of 2 means a tree with
 1.36994 +** three nodes.  And so forth.
 1.36995 +**
 1.36996 +** Use as many entries from the input list as required and update the
 1.36997 +** *ppList to point to the unused elements of the list.  If the input
 1.36998 +** list contains too few elements, then construct an incomplete tree
 1.36999 +** and leave *ppList set to NULL.
 1.37000 +**
 1.37001 +** Return a pointer to the root of the constructed binary tree.
 1.37002 +*/
 1.37003 +static struct RowSetEntry *rowSetNDeepTree(
 1.37004 +  struct RowSetEntry **ppList,
 1.37005 +  int iDepth
 1.37006 +){
 1.37007 +  struct RowSetEntry *p;         /* Root of the new tree */
 1.37008 +  struct RowSetEntry *pLeft;     /* Left subtree */
 1.37009 +  if( *ppList==0 ){
 1.37010 +    return 0;
 1.37011 +  }
 1.37012 +  if( iDepth==1 ){
 1.37013 +    p = *ppList;
 1.37014 +    *ppList = p->pRight;
 1.37015 +    p->pLeft = p->pRight = 0;
 1.37016 +    return p;
 1.37017 +  }
 1.37018 +  pLeft = rowSetNDeepTree(ppList, iDepth-1);
 1.37019 +  p = *ppList;
 1.37020 +  if( p==0 ){
 1.37021 +    return pLeft;
 1.37022 +  }
 1.37023 +  p->pLeft = pLeft;
 1.37024 +  *ppList = p->pRight;
 1.37025 +  p->pRight = rowSetNDeepTree(ppList, iDepth-1);
 1.37026 +  return p;
 1.37027 +}
 1.37028 +
 1.37029 +/*
 1.37030 +** Convert a sorted list of elements into a binary tree. Make the tree
 1.37031 +** as deep as it needs to be in order to contain the entire list.
 1.37032 +*/
 1.37033 +static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
 1.37034 +  int iDepth;           /* Depth of the tree so far */
 1.37035 +  struct RowSetEntry *p;       /* Current tree root */
 1.37036 +  struct RowSetEntry *pLeft;   /* Left subtree */
 1.37037 +
 1.37038 +  assert( pList!=0 );
 1.37039 +  p = pList;
 1.37040 +  pList = p->pRight;
 1.37041 +  p->pLeft = p->pRight = 0;
 1.37042 +  for(iDepth=1; pList; iDepth++){
 1.37043 +    pLeft = p;
 1.37044 +    p = pList;
 1.37045 +    pList = p->pRight;
 1.37046 +    p->pLeft = pLeft;
 1.37047 +    p->pRight = rowSetNDeepTree(&pList, iDepth);
 1.37048 +  }
 1.37049 +  return p;
 1.37050 +}
 1.37051 +
 1.37052 +/*
 1.37053 +** Take all the entries on p->pEntry and on the trees in p->pForest and
 1.37054 +** sort them all together into one big ordered list on p->pEntry.
 1.37055 +**
 1.37056 +** This routine should only be called once in the life of a RowSet.
 1.37057 +*/
 1.37058 +static void rowSetToList(RowSet *p){
 1.37059 +
 1.37060 +  /* This routine is called only once */
 1.37061 +  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
 1.37062 +
 1.37063 +  if( (p->rsFlags & ROWSET_SORTED)==0 ){
 1.37064 +    p->pEntry = rowSetEntrySort(p->pEntry);
 1.37065 +  }
 1.37066 +
 1.37067 +  /* While this module could theoretically support it, sqlite3RowSetNext()
 1.37068 +  ** is never called after sqlite3RowSetText() for the same RowSet.  So
 1.37069 +  ** there is never a forest to deal with.  Should this change, simply
 1.37070 +  ** remove the assert() and the #if 0. */
 1.37071 +  assert( p->pForest==0 );
 1.37072 +#if 0
 1.37073 +  while( p->pForest ){
 1.37074 +    struct RowSetEntry *pTree = p->pForest->pLeft;
 1.37075 +    if( pTree ){
 1.37076 +      struct RowSetEntry *pHead, *pTail;
 1.37077 +      rowSetTreeToList(pTree, &pHead, &pTail);
 1.37078 +      p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
 1.37079 +    }
 1.37080 +    p->pForest = p->pForest->pRight;
 1.37081 +  }
 1.37082 +#endif
 1.37083 +  p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
 1.37084 +}
 1.37085 +
 1.37086 +/*
 1.37087 +** Extract the smallest element from the RowSet.
 1.37088 +** Write the element into *pRowid.  Return 1 on success.  Return
 1.37089 +** 0 if the RowSet is already empty.
 1.37090 +**
 1.37091 +** After this routine has been called, the sqlite3RowSetInsert()
 1.37092 +** routine may not be called again.  
 1.37093 +*/
 1.37094 +SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
 1.37095 +  assert( p!=0 );
 1.37096 +
 1.37097 +  /* Merge the forest into a single sorted list on first call */
 1.37098 +  if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
 1.37099 +
 1.37100 +  /* Return the next entry on the list */
 1.37101 +  if( p->pEntry ){
 1.37102 +    *pRowid = p->pEntry->v;
 1.37103 +    p->pEntry = p->pEntry->pRight;
 1.37104 +    if( p->pEntry==0 ){
 1.37105 +      sqlite3RowSetClear(p);
 1.37106 +    }
 1.37107 +    return 1;
 1.37108 +  }else{
 1.37109 +    return 0;
 1.37110 +  }
 1.37111 +}
 1.37112 +
 1.37113 +/*
 1.37114 +** Check to see if element iRowid was inserted into the rowset as
 1.37115 +** part of any insert batch prior to iBatch.  Return 1 or 0.
 1.37116 +**
 1.37117 +** If this is the first test of a new batch and if there exist entires
 1.37118 +** on pRowSet->pEntry, then sort those entires into the forest at
 1.37119 +** pRowSet->pForest so that they can be tested.
 1.37120 +*/
 1.37121 +SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
 1.37122 +  struct RowSetEntry *p, *pTree;
 1.37123 +
 1.37124 +  /* This routine is never called after sqlite3RowSetNext() */
 1.37125 +  assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
 1.37126 +
 1.37127 +  /* Sort entries into the forest on the first test of a new batch 
 1.37128 +  */
 1.37129 +  if( iBatch!=pRowSet->iBatch ){
 1.37130 +    p = pRowSet->pEntry;
 1.37131 +    if( p ){
 1.37132 +      struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
 1.37133 +      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
 1.37134 +        p = rowSetEntrySort(p);
 1.37135 +      }
 1.37136 +      for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
 1.37137 +        ppPrevTree = &pTree->pRight;
 1.37138 +        if( pTree->pLeft==0 ){
 1.37139 +          pTree->pLeft = rowSetListToTree(p);
 1.37140 +          break;
 1.37141 +        }else{
 1.37142 +          struct RowSetEntry *pAux, *pTail;
 1.37143 +          rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
 1.37144 +          pTree->pLeft = 0;
 1.37145 +          p = rowSetEntryMerge(pAux, p);
 1.37146 +        }
 1.37147 +      }
 1.37148 +      if( pTree==0 ){
 1.37149 +        *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
 1.37150 +        if( pTree ){
 1.37151 +          pTree->v = 0;
 1.37152 +          pTree->pRight = 0;
 1.37153 +          pTree->pLeft = rowSetListToTree(p);
 1.37154 +        }
 1.37155 +      }
 1.37156 +      pRowSet->pEntry = 0;
 1.37157 +      pRowSet->pLast = 0;
 1.37158 +      pRowSet->rsFlags |= ROWSET_SORTED;
 1.37159 +    }
 1.37160 +    pRowSet->iBatch = iBatch;
 1.37161 +  }
 1.37162 +
 1.37163 +  /* Test to see if the iRowid value appears anywhere in the forest.
 1.37164 +  ** Return 1 if it does and 0 if not.
 1.37165 +  */
 1.37166 +  for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
 1.37167 +    p = pTree->pLeft;
 1.37168 +    while( p ){
 1.37169 +      if( p->v<iRowid ){
 1.37170 +        p = p->pRight;
 1.37171 +      }else if( p->v>iRowid ){
 1.37172 +        p = p->pLeft;
 1.37173 +      }else{
 1.37174 +        return 1;
 1.37175 +      }
 1.37176 +    }
 1.37177 +  }
 1.37178 +  return 0;
 1.37179 +}
 1.37180 +
 1.37181 +/************** End of rowset.c **********************************************/
 1.37182 +/************** Begin file pager.c *******************************************/
 1.37183 +/*
 1.37184 +** 2001 September 15
 1.37185 +**
 1.37186 +** The author disclaims copyright to this source code.  In place of
 1.37187 +** a legal notice, here is a blessing:
 1.37188 +**
 1.37189 +**    May you do good and not evil.
 1.37190 +**    May you find forgiveness for yourself and forgive others.
 1.37191 +**    May you share freely, never taking more than you give.
 1.37192 +**
 1.37193 +*************************************************************************
 1.37194 +** This is the implementation of the page cache subsystem or "pager".
 1.37195 +** 
 1.37196 +** The pager is used to access a database disk file.  It implements
 1.37197 +** atomic commit and rollback through the use of a journal file that
 1.37198 +** is separate from the database file.  The pager also implements file
 1.37199 +** locking to prevent two processes from writing the same database
 1.37200 +** file simultaneously, or one process from reading the database while
 1.37201 +** another is writing.
 1.37202 +*/
 1.37203 +#ifndef SQLITE_OMIT_DISKIO
 1.37204 +/************** Include wal.h in the middle of pager.c ***********************/
 1.37205 +/************** Begin file wal.h *********************************************/
 1.37206 +/*
 1.37207 +** 2010 February 1
 1.37208 +**
 1.37209 +** The author disclaims copyright to this source code.  In place of
 1.37210 +** a legal notice, here is a blessing:
 1.37211 +**
 1.37212 +**    May you do good and not evil.
 1.37213 +**    May you find forgiveness for yourself and forgive others.
 1.37214 +**    May you share freely, never taking more than you give.
 1.37215 +**
 1.37216 +*************************************************************************
 1.37217 +** This header file defines the interface to the write-ahead logging 
 1.37218 +** system. Refer to the comments below and the header comment attached to 
 1.37219 +** the implementation of each function in log.c for further details.
 1.37220 +*/
 1.37221 +
 1.37222 +#ifndef _WAL_H_
 1.37223 +#define _WAL_H_
 1.37224 +
 1.37225 +
 1.37226 +/* Additional values that can be added to the sync_flags argument of
 1.37227 +** sqlite3WalFrames():
 1.37228 +*/
 1.37229 +#define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
 1.37230 +#define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
 1.37231 +
 1.37232 +#ifdef SQLITE_OMIT_WAL
 1.37233 +# define sqlite3WalOpen(x,y,z)                   0
 1.37234 +# define sqlite3WalLimit(x,y)
 1.37235 +# define sqlite3WalClose(w,x,y,z)                0
 1.37236 +# define sqlite3WalBeginReadTransaction(y,z)     0
 1.37237 +# define sqlite3WalEndReadTransaction(z)
 1.37238 +# define sqlite3WalRead(v,w,x,y,z)               0
 1.37239 +# define sqlite3WalDbsize(y)                     0
 1.37240 +# define sqlite3WalBeginWriteTransaction(y)      0
 1.37241 +# define sqlite3WalEndWriteTransaction(x)        0
 1.37242 +# define sqlite3WalUndo(x,y,z)                   0
 1.37243 +# define sqlite3WalSavepoint(y,z)
 1.37244 +# define sqlite3WalSavepointUndo(y,z)            0
 1.37245 +# define sqlite3WalFrames(u,v,w,x,y,z)           0
 1.37246 +# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
 1.37247 +# define sqlite3WalCallback(z)                   0
 1.37248 +# define sqlite3WalExclusiveMode(y,z)            0
 1.37249 +# define sqlite3WalHeapMemory(z)                 0
 1.37250 +# define sqlite3WalFramesize(z)                  0
 1.37251 +#else
 1.37252 +
 1.37253 +#define WAL_SAVEPOINT_NDATA 4
 1.37254 +
 1.37255 +/* Connection to a write-ahead log (WAL) file. 
 1.37256 +** There is one object of this type for each pager. 
 1.37257 +*/
 1.37258 +typedef struct Wal Wal;
 1.37259 +
 1.37260 +/* Open and close a connection to a write-ahead log. */
 1.37261 +SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
 1.37262 +SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
 1.37263 +
 1.37264 +/* Set the limiting size of a WAL file. */
 1.37265 +SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
 1.37266 +
 1.37267 +/* Used by readers to open (lock) and close (unlock) a snapshot.  A 
 1.37268 +** snapshot is like a read-transaction.  It is the state of the database
 1.37269 +** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
 1.37270 +** preserves the current state even if the other threads or processes
 1.37271 +** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
 1.37272 +** transaction and releases the lock.
 1.37273 +*/
 1.37274 +SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
 1.37275 +SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
 1.37276 +
 1.37277 +/* Read a page from the write-ahead log, if it is present. */
 1.37278 +SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
 1.37279 +
 1.37280 +/* If the WAL is not empty, return the size of the database. */
 1.37281 +SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
 1.37282 +
 1.37283 +/* Obtain or release the WRITER lock. */
 1.37284 +SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
 1.37285 +SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
 1.37286 +
 1.37287 +/* Undo any frames written (but not committed) to the log */
 1.37288 +SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
 1.37289 +
 1.37290 +/* Return an integer that records the current (uncommitted) write
 1.37291 +** position in the WAL */
 1.37292 +SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
 1.37293 +
 1.37294 +/* Move the write position of the WAL back to iFrame.  Called in
 1.37295 +** response to a ROLLBACK TO command. */
 1.37296 +SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
 1.37297 +
 1.37298 +/* Write a frame or frames to the log. */
 1.37299 +SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
 1.37300 +
 1.37301 +/* Copy pages from the log to the database file */ 
 1.37302 +SQLITE_PRIVATE int sqlite3WalCheckpoint(
 1.37303 +  Wal *pWal,                      /* Write-ahead log connection */
 1.37304 +  int eMode,                      /* One of PASSIVE, FULL and RESTART */
 1.37305 +  int (*xBusy)(void*),            /* Function to call when busy */
 1.37306 +  void *pBusyArg,                 /* Context argument for xBusyHandler */
 1.37307 +  int sync_flags,                 /* Flags to sync db file with (or 0) */
 1.37308 +  int nBuf,                       /* Size of buffer nBuf */
 1.37309 +  u8 *zBuf,                       /* Temporary buffer to use */
 1.37310 +  int *pnLog,                     /* OUT: Number of frames in WAL */
 1.37311 +  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 1.37312 +);
 1.37313 +
 1.37314 +/* Return the value to pass to a sqlite3_wal_hook callback, the
 1.37315 +** number of frames in the WAL at the point of the last commit since
 1.37316 +** sqlite3WalCallback() was called.  If no commits have occurred since
 1.37317 +** the last call, then return 0.
 1.37318 +*/
 1.37319 +SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
 1.37320 +
 1.37321 +/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
 1.37322 +** by the pager layer on the database file.
 1.37323 +*/
 1.37324 +SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
 1.37325 +
 1.37326 +/* Return true if the argument is non-NULL and the WAL module is using
 1.37327 +** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
 1.37328 +** WAL module is using shared-memory, return false. 
 1.37329 +*/
 1.37330 +SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
 1.37331 +
 1.37332 +#ifdef SQLITE_ENABLE_ZIPVFS
 1.37333 +/* If the WAL file is not empty, return the number of bytes of content
 1.37334 +** stored in each frame (i.e. the db page-size when the WAL was created).
 1.37335 +*/
 1.37336 +SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
 1.37337 +#endif
 1.37338 +
 1.37339 +#endif /* ifndef SQLITE_OMIT_WAL */
 1.37340 +#endif /* _WAL_H_ */
 1.37341 +
 1.37342 +/************** End of wal.h *************************************************/
 1.37343 +/************** Continuing where we left off in pager.c **********************/
 1.37344 +
 1.37345 +
 1.37346 +/******************* NOTES ON THE DESIGN OF THE PAGER ************************
 1.37347 +**
 1.37348 +** This comment block describes invariants that hold when using a rollback
 1.37349 +** journal.  These invariants do not apply for journal_mode=WAL,
 1.37350 +** journal_mode=MEMORY, or journal_mode=OFF.
 1.37351 +**
 1.37352 +** Within this comment block, a page is deemed to have been synced
 1.37353 +** automatically as soon as it is written when PRAGMA synchronous=OFF.
 1.37354 +** Otherwise, the page is not synced until the xSync method of the VFS
 1.37355 +** is called successfully on the file containing the page.
 1.37356 +**
 1.37357 +** Definition:  A page of the database file is said to be "overwriteable" if
 1.37358 +** one or more of the following are true about the page:
 1.37359 +** 
 1.37360 +**     (a)  The original content of the page as it was at the beginning of
 1.37361 +**          the transaction has been written into the rollback journal and
 1.37362 +**          synced.
 1.37363 +** 
 1.37364 +**     (b)  The page was a freelist leaf page at the start of the transaction.
 1.37365 +** 
 1.37366 +**     (c)  The page number is greater than the largest page that existed in
 1.37367 +**          the database file at the start of the transaction.
 1.37368 +** 
 1.37369 +** (1) A page of the database file is never overwritten unless one of the
 1.37370 +**     following are true:
 1.37371 +** 
 1.37372 +**     (a) The page and all other pages on the same sector are overwriteable.
 1.37373 +** 
 1.37374 +**     (b) The atomic page write optimization is enabled, and the entire
 1.37375 +**         transaction other than the update of the transaction sequence
 1.37376 +**         number consists of a single page change.
 1.37377 +** 
 1.37378 +** (2) The content of a page written into the rollback journal exactly matches
 1.37379 +**     both the content in the database when the rollback journal was written
 1.37380 +**     and the content in the database at the beginning of the current
 1.37381 +**     transaction.
 1.37382 +** 
 1.37383 +** (3) Writes to the database file are an integer multiple of the page size
 1.37384 +**     in length and are aligned on a page boundary.
 1.37385 +** 
 1.37386 +** (4) Reads from the database file are either aligned on a page boundary and
 1.37387 +**     an integer multiple of the page size in length or are taken from the
 1.37388 +**     first 100 bytes of the database file.
 1.37389 +** 
 1.37390 +** (5) All writes to the database file are synced prior to the rollback journal
 1.37391 +**     being deleted, truncated, or zeroed.
 1.37392 +** 
 1.37393 +** (6) If a master journal file is used, then all writes to the database file
 1.37394 +**     are synced prior to the master journal being deleted.
 1.37395 +** 
 1.37396 +** Definition: Two databases (or the same database at two points it time)
 1.37397 +** are said to be "logically equivalent" if they give the same answer to
 1.37398 +** all queries.  Note in particular the content of freelist leaf
 1.37399 +** pages can be changed arbitarily without effecting the logical equivalence
 1.37400 +** of the database.
 1.37401 +** 
 1.37402 +** (7) At any time, if any subset, including the empty set and the total set,
 1.37403 +**     of the unsynced changes to a rollback journal are removed and the 
 1.37404 +**     journal is rolled back, the resulting database file will be logical
 1.37405 +**     equivalent to the database file at the beginning of the transaction.
 1.37406 +** 
 1.37407 +** (8) When a transaction is rolled back, the xTruncate method of the VFS
 1.37408 +**     is called to restore the database file to the same size it was at
 1.37409 +**     the beginning of the transaction.  (In some VFSes, the xTruncate
 1.37410 +**     method is a no-op, but that does not change the fact the SQLite will
 1.37411 +**     invoke it.)
 1.37412 +** 
 1.37413 +** (9) Whenever the database file is modified, at least one bit in the range
 1.37414 +**     of bytes from 24 through 39 inclusive will be changed prior to releasing
 1.37415 +**     the EXCLUSIVE lock, thus signaling other connections on the same
 1.37416 +**     database to flush their caches.
 1.37417 +**
 1.37418 +** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
 1.37419 +**      than one billion transactions.
 1.37420 +**
 1.37421 +** (11) A database file is well-formed at the beginning and at the conclusion
 1.37422 +**      of every transaction.
 1.37423 +**
 1.37424 +** (12) An EXCLUSIVE lock is held on the database file when writing to
 1.37425 +**      the database file.
 1.37426 +**
 1.37427 +** (13) A SHARED lock is held on the database file while reading any
 1.37428 +**      content out of the database file.
 1.37429 +**
 1.37430 +******************************************************************************/
 1.37431 +
 1.37432 +/*
 1.37433 +** Macros for troubleshooting.  Normally turned off
 1.37434 +*/
 1.37435 +#if 0
 1.37436 +int sqlite3PagerTrace=1;  /* True to enable tracing */
 1.37437 +#define sqlite3DebugPrintf printf
 1.37438 +#define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
 1.37439 +#else
 1.37440 +#define PAGERTRACE(X)
 1.37441 +#endif
 1.37442 +
 1.37443 +/*
 1.37444 +** The following two macros are used within the PAGERTRACE() macros above
 1.37445 +** to print out file-descriptors. 
 1.37446 +**
 1.37447 +** PAGERID() takes a pointer to a Pager struct as its argument. The
 1.37448 +** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
 1.37449 +** struct as its argument.
 1.37450 +*/
 1.37451 +#define PAGERID(p) ((int)(p->fd))
 1.37452 +#define FILEHANDLEID(fd) ((int)fd)
 1.37453 +
 1.37454 +/*
 1.37455 +** The Pager.eState variable stores the current 'state' of a pager. A
 1.37456 +** pager may be in any one of the seven states shown in the following
 1.37457 +** state diagram.
 1.37458 +**
 1.37459 +**                            OPEN <------+------+
 1.37460 +**                              |         |      |
 1.37461 +**                              V         |      |
 1.37462 +**               +---------> READER-------+      |
 1.37463 +**               |              |                |
 1.37464 +**               |              V                |
 1.37465 +**               |<-------WRITER_LOCKED------> ERROR
 1.37466 +**               |              |                ^  
 1.37467 +**               |              V                |
 1.37468 +**               |<------WRITER_CACHEMOD-------->|
 1.37469 +**               |              |                |
 1.37470 +**               |              V                |
 1.37471 +**               |<-------WRITER_DBMOD---------->|
 1.37472 +**               |              |                |
 1.37473 +**               |              V                |
 1.37474 +**               +<------WRITER_FINISHED-------->+
 1.37475 +**
 1.37476 +**
 1.37477 +** List of state transitions and the C [function] that performs each:
 1.37478 +** 
 1.37479 +**   OPEN              -> READER              [sqlite3PagerSharedLock]
 1.37480 +**   READER            -> OPEN                [pager_unlock]
 1.37481 +**
 1.37482 +**   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
 1.37483 +**   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
 1.37484 +**   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
 1.37485 +**   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
 1.37486 +**   WRITER_***        -> READER              [pager_end_transaction]
 1.37487 +**
 1.37488 +**   WRITER_***        -> ERROR               [pager_error]
 1.37489 +**   ERROR             -> OPEN                [pager_unlock]
 1.37490 +** 
 1.37491 +**
 1.37492 +**  OPEN:
 1.37493 +**
 1.37494 +**    The pager starts up in this state. Nothing is guaranteed in this
 1.37495 +**    state - the file may or may not be locked and the database size is
 1.37496 +**    unknown. The database may not be read or written.
 1.37497 +**
 1.37498 +**    * No read or write transaction is active.
 1.37499 +**    * Any lock, or no lock at all, may be held on the database file.
 1.37500 +**    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
 1.37501 +**
 1.37502 +**  READER:
 1.37503 +**
 1.37504 +**    In this state all the requirements for reading the database in 
 1.37505 +**    rollback (non-WAL) mode are met. Unless the pager is (or recently
 1.37506 +**    was) in exclusive-locking mode, a user-level read transaction is 
 1.37507 +**    open. The database size is known in this state.
 1.37508 +**
 1.37509 +**    A connection running with locking_mode=normal enters this state when
 1.37510 +**    it opens a read-transaction on the database and returns to state
 1.37511 +**    OPEN after the read-transaction is completed. However a connection
 1.37512 +**    running in locking_mode=exclusive (including temp databases) remains in
 1.37513 +**    this state even after the read-transaction is closed. The only way
 1.37514 +**    a locking_mode=exclusive connection can transition from READER to OPEN
 1.37515 +**    is via the ERROR state (see below).
 1.37516 +** 
 1.37517 +**    * A read transaction may be active (but a write-transaction cannot).
 1.37518 +**    * A SHARED or greater lock is held on the database file.
 1.37519 +**    * The dbSize variable may be trusted (even if a user-level read 
 1.37520 +**      transaction is not active). The dbOrigSize and dbFileSize variables
 1.37521 +**      may not be trusted at this point.
 1.37522 +**    * If the database is a WAL database, then the WAL connection is open.
 1.37523 +**    * Even if a read-transaction is not open, it is guaranteed that 
 1.37524 +**      there is no hot-journal in the file-system.
 1.37525 +**
 1.37526 +**  WRITER_LOCKED:
 1.37527 +**
 1.37528 +**    The pager moves to this state from READER when a write-transaction
 1.37529 +**    is first opened on the database. In WRITER_LOCKED state, all locks 
 1.37530 +**    required to start a write-transaction are held, but no actual 
 1.37531 +**    modifications to the cache or database have taken place.
 1.37532 +**
 1.37533 +**    In rollback mode, a RESERVED or (if the transaction was opened with 
 1.37534 +**    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
 1.37535 +**    moving to this state, but the journal file is not written to or opened 
 1.37536 +**    to in this state. If the transaction is committed or rolled back while 
 1.37537 +**    in WRITER_LOCKED state, all that is required is to unlock the database 
 1.37538 +**    file.
 1.37539 +**
 1.37540 +**    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
 1.37541 +**    If the connection is running with locking_mode=exclusive, an attempt
 1.37542 +**    is made to obtain an EXCLUSIVE lock on the database file.
 1.37543 +**
 1.37544 +**    * A write transaction is active.
 1.37545 +**    * If the connection is open in rollback-mode, a RESERVED or greater 
 1.37546 +**      lock is held on the database file.
 1.37547 +**    * If the connection is open in WAL-mode, a WAL write transaction
 1.37548 +**      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
 1.37549 +**      called).
 1.37550 +**    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
 1.37551 +**    * The contents of the pager cache have not been modified.
 1.37552 +**    * The journal file may or may not be open.
 1.37553 +**    * Nothing (not even the first header) has been written to the journal.
 1.37554 +**
 1.37555 +**  WRITER_CACHEMOD:
 1.37556 +**
 1.37557 +**    A pager moves from WRITER_LOCKED state to this state when a page is
 1.37558 +**    first modified by the upper layer. In rollback mode the journal file
 1.37559 +**    is opened (if it is not already open) and a header written to the
 1.37560 +**    start of it. The database file on disk has not been modified.
 1.37561 +**
 1.37562 +**    * A write transaction is active.
 1.37563 +**    * A RESERVED or greater lock is held on the database file.
 1.37564 +**    * The journal file is open and the first header has been written 
 1.37565 +**      to it, but the header has not been synced to disk.
 1.37566 +**    * The contents of the page cache have been modified.
 1.37567 +**
 1.37568 +**  WRITER_DBMOD:
 1.37569 +**
 1.37570 +**    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
 1.37571 +**    when it modifies the contents of the database file. WAL connections
 1.37572 +**    never enter this state (since they do not modify the database file,
 1.37573 +**    just the log file).
 1.37574 +**
 1.37575 +**    * A write transaction is active.
 1.37576 +**    * An EXCLUSIVE or greater lock is held on the database file.
 1.37577 +**    * The journal file is open and the first header has been written 
 1.37578 +**      and synced to disk.
 1.37579 +**    * The contents of the page cache have been modified (and possibly
 1.37580 +**      written to disk).
 1.37581 +**
 1.37582 +**  WRITER_FINISHED:
 1.37583 +**
 1.37584 +**    It is not possible for a WAL connection to enter this state.
 1.37585 +**
 1.37586 +**    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
 1.37587 +**    state after the entire transaction has been successfully written into the
 1.37588 +**    database file. In this state the transaction may be committed simply
 1.37589 +**    by finalizing the journal file. Once in WRITER_FINISHED state, it is 
 1.37590 +**    not possible to modify the database further. At this point, the upper 
 1.37591 +**    layer must either commit or rollback the transaction.
 1.37592 +**
 1.37593 +**    * A write transaction is active.
 1.37594 +**    * An EXCLUSIVE or greater lock is held on the database file.
 1.37595 +**    * All writing and syncing of journal and database data has finished.
 1.37596 +**      If no error occured, all that remains is to finalize the journal to
 1.37597 +**      commit the transaction. If an error did occur, the caller will need
 1.37598 +**      to rollback the transaction. 
 1.37599 +**
 1.37600 +**  ERROR:
 1.37601 +**
 1.37602 +**    The ERROR state is entered when an IO or disk-full error (including
 1.37603 +**    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it 
 1.37604 +**    difficult to be sure that the in-memory pager state (cache contents, 
 1.37605 +**    db size etc.) are consistent with the contents of the file-system.
 1.37606 +**
 1.37607 +**    Temporary pager files may enter the ERROR state, but in-memory pagers
 1.37608 +**    cannot.
 1.37609 +**
 1.37610 +**    For example, if an IO error occurs while performing a rollback, 
 1.37611 +**    the contents of the page-cache may be left in an inconsistent state.
 1.37612 +**    At this point it would be dangerous to change back to READER state
 1.37613 +**    (as usually happens after a rollback). Any subsequent readers might
 1.37614 +**    report database corruption (due to the inconsistent cache), and if
 1.37615 +**    they upgrade to writers, they may inadvertently corrupt the database
 1.37616 +**    file. To avoid this hazard, the pager switches into the ERROR state
 1.37617 +**    instead of READER following such an error.
 1.37618 +**
 1.37619 +**    Once it has entered the ERROR state, any attempt to use the pager
 1.37620 +**    to read or write data returns an error. Eventually, once all 
 1.37621 +**    outstanding transactions have been abandoned, the pager is able to
 1.37622 +**    transition back to OPEN state, discarding the contents of the 
 1.37623 +**    page-cache and any other in-memory state at the same time. Everything
 1.37624 +**    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
 1.37625 +**    when a read-transaction is next opened on the pager (transitioning
 1.37626 +**    the pager into READER state). At that point the system has recovered 
 1.37627 +**    from the error.
 1.37628 +**
 1.37629 +**    Specifically, the pager jumps into the ERROR state if:
 1.37630 +**
 1.37631 +**      1. An error occurs while attempting a rollback. This happens in
 1.37632 +**         function sqlite3PagerRollback().
 1.37633 +**
 1.37634 +**      2. An error occurs while attempting to finalize a journal file
 1.37635 +**         following a commit in function sqlite3PagerCommitPhaseTwo().
 1.37636 +**
 1.37637 +**      3. An error occurs while attempting to write to the journal or
 1.37638 +**         database file in function pagerStress() in order to free up
 1.37639 +**         memory.
 1.37640 +**
 1.37641 +**    In other cases, the error is returned to the b-tree layer. The b-tree
 1.37642 +**    layer then attempts a rollback operation. If the error condition 
 1.37643 +**    persists, the pager enters the ERROR state via condition (1) above.
 1.37644 +**
 1.37645 +**    Condition (3) is necessary because it can be triggered by a read-only
 1.37646 +**    statement executed within a transaction. In this case, if the error
 1.37647 +**    code were simply returned to the user, the b-tree layer would not
 1.37648 +**    automatically attempt a rollback, as it assumes that an error in a
 1.37649 +**    read-only statement cannot leave the pager in an internally inconsistent 
 1.37650 +**    state.
 1.37651 +**
 1.37652 +**    * The Pager.errCode variable is set to something other than SQLITE_OK.
 1.37653 +**    * There are one or more outstanding references to pages (after the
 1.37654 +**      last reference is dropped the pager should move back to OPEN state).
 1.37655 +**    * The pager is not an in-memory pager.
 1.37656 +**    
 1.37657 +**
 1.37658 +** Notes:
 1.37659 +**
 1.37660 +**   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
 1.37661 +**     connection is open in WAL mode. A WAL connection is always in one
 1.37662 +**     of the first four states.
 1.37663 +**
 1.37664 +**   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
 1.37665 +**     state. There are two exceptions: immediately after exclusive-mode has
 1.37666 +**     been turned on (and before any read or write transactions are 
 1.37667 +**     executed), and when the pager is leaving the "error state".
 1.37668 +**
 1.37669 +**   * See also: assert_pager_state().
 1.37670 +*/
 1.37671 +#define PAGER_OPEN                  0
 1.37672 +#define PAGER_READER                1
 1.37673 +#define PAGER_WRITER_LOCKED         2
 1.37674 +#define PAGER_WRITER_CACHEMOD       3
 1.37675 +#define PAGER_WRITER_DBMOD          4
 1.37676 +#define PAGER_WRITER_FINISHED       5
 1.37677 +#define PAGER_ERROR                 6
 1.37678 +
 1.37679 +/*
 1.37680 +** The Pager.eLock variable is almost always set to one of the 
 1.37681 +** following locking-states, according to the lock currently held on
 1.37682 +** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
 1.37683 +** This variable is kept up to date as locks are taken and released by
 1.37684 +** the pagerLockDb() and pagerUnlockDb() wrappers.
 1.37685 +**
 1.37686 +** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
 1.37687 +** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
 1.37688 +** the operation was successful. In these circumstances pagerLockDb() and
 1.37689 +** pagerUnlockDb() take a conservative approach - eLock is always updated
 1.37690 +** when unlocking the file, and only updated when locking the file if the
 1.37691 +** VFS call is successful. This way, the Pager.eLock variable may be set
 1.37692 +** to a less exclusive (lower) value than the lock that is actually held
 1.37693 +** at the system level, but it is never set to a more exclusive value.
 1.37694 +**
 1.37695 +** This is usually safe. If an xUnlock fails or appears to fail, there may 
 1.37696 +** be a few redundant xLock() calls or a lock may be held for longer than
 1.37697 +** required, but nothing really goes wrong.
 1.37698 +**
 1.37699 +** The exception is when the database file is unlocked as the pager moves
 1.37700 +** from ERROR to OPEN state. At this point there may be a hot-journal file 
 1.37701 +** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
 1.37702 +** transition, by the same pager or any other). If the call to xUnlock()
 1.37703 +** fails at this point and the pager is left holding an EXCLUSIVE lock, this
 1.37704 +** can confuse the call to xCheckReservedLock() call made later as part
 1.37705 +** of hot-journal detection.
 1.37706 +**
 1.37707 +** xCheckReservedLock() is defined as returning true "if there is a RESERVED 
 1.37708 +** lock held by this process or any others". So xCheckReservedLock may 
 1.37709 +** return true because the caller itself is holding an EXCLUSIVE lock (but
 1.37710 +** doesn't know it because of a previous error in xUnlock). If this happens
 1.37711 +** a hot-journal may be mistaken for a journal being created by an active
 1.37712 +** transaction in another process, causing SQLite to read from the database
 1.37713 +** without rolling it back.
 1.37714 +**
 1.37715 +** To work around this, if a call to xUnlock() fails when unlocking the
 1.37716 +** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
 1.37717 +** is only changed back to a real locking state after a successful call
 1.37718 +** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
 1.37719 +** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK 
 1.37720 +** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
 1.37721 +** lock on the database file before attempting to roll it back. See function
 1.37722 +** PagerSharedLock() for more detail.
 1.37723 +**
 1.37724 +** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in 
 1.37725 +** PAGER_OPEN state.
 1.37726 +*/
 1.37727 +#define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
 1.37728 +
 1.37729 +/*
 1.37730 +** A macro used for invoking the codec if there is one
 1.37731 +*/
 1.37732 +#ifdef SQLITE_HAS_CODEC
 1.37733 +# define CODEC1(P,D,N,X,E) \
 1.37734 +    if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
 1.37735 +# define CODEC2(P,D,N,X,E,O) \
 1.37736 +    if( P->xCodec==0 ){ O=(char*)D; }else \
 1.37737 +    if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
 1.37738 +#else
 1.37739 +# define CODEC1(P,D,N,X,E)   /* NO-OP */
 1.37740 +# define CODEC2(P,D,N,X,E,O) O=(char*)D
 1.37741 +#endif
 1.37742 +
 1.37743 +/*
 1.37744 +** The maximum allowed sector size. 64KiB. If the xSectorsize() method 
 1.37745 +** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
 1.37746 +** This could conceivably cause corruption following a power failure on
 1.37747 +** such a system. This is currently an undocumented limit.
 1.37748 +*/
 1.37749 +#define MAX_SECTOR_SIZE 0x10000
 1.37750 +
 1.37751 +/*
 1.37752 +** An instance of the following structure is allocated for each active
 1.37753 +** savepoint and statement transaction in the system. All such structures
 1.37754 +** are stored in the Pager.aSavepoint[] array, which is allocated and
 1.37755 +** resized using sqlite3Realloc().
 1.37756 +**
 1.37757 +** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
 1.37758 +** set to 0. If a journal-header is written into the main journal while
 1.37759 +** the savepoint is active, then iHdrOffset is set to the byte offset 
 1.37760 +** immediately following the last journal record written into the main
 1.37761 +** journal before the journal-header. This is required during savepoint
 1.37762 +** rollback (see pagerPlaybackSavepoint()).
 1.37763 +*/
 1.37764 +typedef struct PagerSavepoint PagerSavepoint;
 1.37765 +struct PagerSavepoint {
 1.37766 +  i64 iOffset;                 /* Starting offset in main journal */
 1.37767 +  i64 iHdrOffset;              /* See above */
 1.37768 +  Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
 1.37769 +  Pgno nOrig;                  /* Original number of pages in file */
 1.37770 +  Pgno iSubRec;                /* Index of first record in sub-journal */
 1.37771 +#ifndef SQLITE_OMIT_WAL
 1.37772 +  u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
 1.37773 +#endif
 1.37774 +};
 1.37775 +
 1.37776 +/*
 1.37777 +** A open page cache is an instance of struct Pager. A description of
 1.37778 +** some of the more important member variables follows:
 1.37779 +**
 1.37780 +** eState
 1.37781 +**
 1.37782 +**   The current 'state' of the pager object. See the comment and state
 1.37783 +**   diagram above for a description of the pager state.
 1.37784 +**
 1.37785 +** eLock
 1.37786 +**
 1.37787 +**   For a real on-disk database, the current lock held on the database file -
 1.37788 +**   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
 1.37789 +**
 1.37790 +**   For a temporary or in-memory database (neither of which require any
 1.37791 +**   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
 1.37792 +**   databases always have Pager.exclusiveMode==1, this tricks the pager
 1.37793 +**   logic into thinking that it already has all the locks it will ever
 1.37794 +**   need (and no reason to release them).
 1.37795 +**
 1.37796 +**   In some (obscure) circumstances, this variable may also be set to
 1.37797 +**   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
 1.37798 +**   details.
 1.37799 +**
 1.37800 +** changeCountDone
 1.37801 +**
 1.37802 +**   This boolean variable is used to make sure that the change-counter 
 1.37803 +**   (the 4-byte header field at byte offset 24 of the database file) is 
 1.37804 +**   not updated more often than necessary. 
 1.37805 +**
 1.37806 +**   It is set to true when the change-counter field is updated, which 
 1.37807 +**   can only happen if an exclusive lock is held on the database file.
 1.37808 +**   It is cleared (set to false) whenever an exclusive lock is 
 1.37809 +**   relinquished on the database file. Each time a transaction is committed,
 1.37810 +**   The changeCountDone flag is inspected. If it is true, the work of
 1.37811 +**   updating the change-counter is omitted for the current transaction.
 1.37812 +**
 1.37813 +**   This mechanism means that when running in exclusive mode, a connection 
 1.37814 +**   need only update the change-counter once, for the first transaction
 1.37815 +**   committed.
 1.37816 +**
 1.37817 +** setMaster
 1.37818 +**
 1.37819 +**   When PagerCommitPhaseOne() is called to commit a transaction, it may
 1.37820 +**   (or may not) specify a master-journal name to be written into the 
 1.37821 +**   journal file before it is synced to disk.
 1.37822 +**
 1.37823 +**   Whether or not a journal file contains a master-journal pointer affects 
 1.37824 +**   the way in which the journal file is finalized after the transaction is 
 1.37825 +**   committed or rolled back when running in "journal_mode=PERSIST" mode.
 1.37826 +**   If a journal file does not contain a master-journal pointer, it is
 1.37827 +**   finalized by overwriting the first journal header with zeroes. If
 1.37828 +**   it does contain a master-journal pointer the journal file is finalized 
 1.37829 +**   by truncating it to zero bytes, just as if the connection were 
 1.37830 +**   running in "journal_mode=truncate" mode.
 1.37831 +**
 1.37832 +**   Journal files that contain master journal pointers cannot be finalized
 1.37833 +**   simply by overwriting the first journal-header with zeroes, as the
 1.37834 +**   master journal pointer could interfere with hot-journal rollback of any
 1.37835 +**   subsequently interrupted transaction that reuses the journal file.
 1.37836 +**
 1.37837 +**   The flag is cleared as soon as the journal file is finalized (either
 1.37838 +**   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
 1.37839 +**   journal file from being successfully finalized, the setMaster flag
 1.37840 +**   is cleared anyway (and the pager will move to ERROR state).
 1.37841 +**
 1.37842 +** doNotSpill, doNotSyncSpill
 1.37843 +**
 1.37844 +**   These two boolean variables control the behaviour of cache-spills
 1.37845 +**   (calls made by the pcache module to the pagerStress() routine to
 1.37846 +**   write cached data to the file-system in order to free up memory).
 1.37847 +**
 1.37848 +**   When doNotSpill is non-zero, writing to the database from pagerStress()
 1.37849 +**   is disabled altogether. This is done in a very obscure case that
 1.37850 +**   comes up during savepoint rollback that requires the pcache module
 1.37851 +**   to allocate a new page to prevent the journal file from being written
 1.37852 +**   while it is being traversed by code in pager_playback().
 1.37853 +** 
 1.37854 +**   If doNotSyncSpill is non-zero, writing to the database from pagerStress()
 1.37855 +**   is permitted, but syncing the journal file is not. This flag is set
 1.37856 +**   by sqlite3PagerWrite() when the file-system sector-size is larger than
 1.37857 +**   the database page-size in order to prevent a journal sync from happening 
 1.37858 +**   in between the journalling of two pages on the same sector. 
 1.37859 +**
 1.37860 +** subjInMemory
 1.37861 +**
 1.37862 +**   This is a boolean variable. If true, then any required sub-journal
 1.37863 +**   is opened as an in-memory journal file. If false, then in-memory
 1.37864 +**   sub-journals are only used for in-memory pager files.
 1.37865 +**
 1.37866 +**   This variable is updated by the upper layer each time a new 
 1.37867 +**   write-transaction is opened.
 1.37868 +**
 1.37869 +** dbSize, dbOrigSize, dbFileSize
 1.37870 +**
 1.37871 +**   Variable dbSize is set to the number of pages in the database file.
 1.37872 +**   It is valid in PAGER_READER and higher states (all states except for
 1.37873 +**   OPEN and ERROR). 
 1.37874 +**
 1.37875 +**   dbSize is set based on the size of the database file, which may be 
 1.37876 +**   larger than the size of the database (the value stored at offset
 1.37877 +**   28 of the database header by the btree). If the size of the file
 1.37878 +**   is not an integer multiple of the page-size, the value stored in
 1.37879 +**   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
 1.37880 +**   Except, any file that is greater than 0 bytes in size is considered
 1.37881 +**   to have at least one page. (i.e. a 1KB file with 2K page-size leads
 1.37882 +**   to dbSize==1).
 1.37883 +**
 1.37884 +**   During a write-transaction, if pages with page-numbers greater than
 1.37885 +**   dbSize are modified in the cache, dbSize is updated accordingly.
 1.37886 +**   Similarly, if the database is truncated using PagerTruncateImage(), 
 1.37887 +**   dbSize is updated.
 1.37888 +**
 1.37889 +**   Variables dbOrigSize and dbFileSize are valid in states 
 1.37890 +**   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
 1.37891 +**   variable at the start of the transaction. It is used during rollback,
 1.37892 +**   and to determine whether or not pages need to be journalled before
 1.37893 +**   being modified.
 1.37894 +**
 1.37895 +**   Throughout a write-transaction, dbFileSize contains the size of
 1.37896 +**   the file on disk in pages. It is set to a copy of dbSize when the
 1.37897 +**   write-transaction is first opened, and updated when VFS calls are made
 1.37898 +**   to write or truncate the database file on disk. 
 1.37899 +**
 1.37900 +**   The only reason the dbFileSize variable is required is to suppress 
 1.37901 +**   unnecessary calls to xTruncate() after committing a transaction. If, 
 1.37902 +**   when a transaction is committed, the dbFileSize variable indicates 
 1.37903 +**   that the database file is larger than the database image (Pager.dbSize), 
 1.37904 +**   pager_truncate() is called. The pager_truncate() call uses xFilesize()
 1.37905 +**   to measure the database file on disk, and then truncates it if required.
 1.37906 +**   dbFileSize is not used when rolling back a transaction. In this case
 1.37907 +**   pager_truncate() is called unconditionally (which means there may be
 1.37908 +**   a call to xFilesize() that is not strictly required). In either case,
 1.37909 +**   pager_truncate() may cause the file to become smaller or larger.
 1.37910 +**
 1.37911 +** dbHintSize
 1.37912 +**
 1.37913 +**   The dbHintSize variable is used to limit the number of calls made to
 1.37914 +**   the VFS xFileControl(FCNTL_SIZE_HINT) method. 
 1.37915 +**
 1.37916 +**   dbHintSize is set to a copy of the dbSize variable when a
 1.37917 +**   write-transaction is opened (at the same time as dbFileSize and
 1.37918 +**   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
 1.37919 +**   dbHintSize is increased to the number of pages that correspond to the
 1.37920 +**   size-hint passed to the method call. See pager_write_pagelist() for 
 1.37921 +**   details.
 1.37922 +**
 1.37923 +** errCode
 1.37924 +**
 1.37925 +**   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
 1.37926 +**   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 
 1.37927 +**   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 
 1.37928 +**   sub-codes.
 1.37929 +*/
 1.37930 +struct Pager {
 1.37931 +  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
 1.37932 +  u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
 1.37933 +  u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
 1.37934 +  u8 useJournal;              /* Use a rollback journal on this file */
 1.37935 +  u8 noSync;                  /* Do not sync the journal if true */
 1.37936 +  u8 fullSync;                /* Do extra syncs of the journal for robustness */
 1.37937 +  u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
 1.37938 +  u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
 1.37939 +  u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
 1.37940 +  u8 tempFile;                /* zFilename is a temporary file */
 1.37941 +  u8 readOnly;                /* True for a read-only database */
 1.37942 +  u8 memDb;                   /* True to inhibit all file I/O */
 1.37943 +
 1.37944 +  /**************************************************************************
 1.37945 +  ** The following block contains those class members that change during
 1.37946 +  ** routine opertion.  Class members not in this block are either fixed
 1.37947 +  ** when the pager is first created or else only change when there is a
 1.37948 +  ** significant mode change (such as changing the page_size, locking_mode,
 1.37949 +  ** or the journal_mode).  From another view, these class members describe
 1.37950 +  ** the "state" of the pager, while other class members describe the
 1.37951 +  ** "configuration" of the pager.
 1.37952 +  */
 1.37953 +  u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
 1.37954 +  u8 eLock;                   /* Current lock held on database file */
 1.37955 +  u8 changeCountDone;         /* Set after incrementing the change-counter */
 1.37956 +  u8 setMaster;               /* True if a m-j name has been written to jrnl */
 1.37957 +  u8 doNotSpill;              /* Do not spill the cache when non-zero */
 1.37958 +  u8 doNotSyncSpill;          /* Do not do a spill that requires jrnl sync */
 1.37959 +  u8 subjInMemory;            /* True to use in-memory sub-journals */
 1.37960 +  Pgno dbSize;                /* Number of pages in the database */
 1.37961 +  Pgno dbOrigSize;            /* dbSize before the current transaction */
 1.37962 +  Pgno dbFileSize;            /* Number of pages in the database file */
 1.37963 +  Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
 1.37964 +  int errCode;                /* One of several kinds of errors */
 1.37965 +  int nRec;                   /* Pages journalled since last j-header written */
 1.37966 +  u32 cksumInit;              /* Quasi-random value added to every checksum */
 1.37967 +  u32 nSubRec;                /* Number of records written to sub-journal */
 1.37968 +  Bitvec *pInJournal;         /* One bit for each page in the database file */
 1.37969 +  sqlite3_file *fd;           /* File descriptor for database */
 1.37970 +  sqlite3_file *jfd;          /* File descriptor for main journal */
 1.37971 +  sqlite3_file *sjfd;         /* File descriptor for sub-journal */
 1.37972 +  i64 journalOff;             /* Current write offset in the journal file */
 1.37973 +  i64 journalHdr;             /* Byte offset to previous journal header */
 1.37974 +  sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
 1.37975 +  PagerSavepoint *aSavepoint; /* Array of active savepoints */
 1.37976 +  int nSavepoint;             /* Number of elements in aSavepoint[] */
 1.37977 +  char dbFileVers[16];        /* Changes whenever database file changes */
 1.37978 +  /*
 1.37979 +  ** End of the routinely-changing class members
 1.37980 +  ***************************************************************************/
 1.37981 +
 1.37982 +  u16 nExtra;                 /* Add this many bytes to each in-memory page */
 1.37983 +  i16 nReserve;               /* Number of unused bytes at end of each page */
 1.37984 +  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
 1.37985 +  u32 sectorSize;             /* Assumed sector size during rollback */
 1.37986 +  int pageSize;               /* Number of bytes in a page */
 1.37987 +  Pgno mxPgno;                /* Maximum allowed size of the database */
 1.37988 +  i64 journalSizeLimit;       /* Size limit for persistent journal files */
 1.37989 +  char *zFilename;            /* Name of the database file */
 1.37990 +  char *zJournal;             /* Name of the journal file */
 1.37991 +  int (*xBusyHandler)(void*); /* Function to call when busy */
 1.37992 +  void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 1.37993 +  int aStat[3];               /* Total cache hits, misses and writes */
 1.37994 +#ifdef SQLITE_TEST
 1.37995 +  int nRead;                  /* Database pages read */
 1.37996 +#endif
 1.37997 +  void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 1.37998 +#ifdef SQLITE_HAS_CODEC
 1.37999 +  void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
 1.38000 +  void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
 1.38001 +  void (*xCodecFree)(void*);             /* Destructor for the codec */
 1.38002 +  void *pCodec;               /* First argument to xCodec... methods */
 1.38003 +#endif
 1.38004 +  char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
 1.38005 +  PCache *pPCache;            /* Pointer to page cache object */
 1.38006 +#ifndef SQLITE_OMIT_WAL
 1.38007 +  Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
 1.38008 +  char *zWal;                 /* File name for write-ahead log */
 1.38009 +#endif
 1.38010 +};
 1.38011 +
 1.38012 +/*
 1.38013 +** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 1.38014 +** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 1.38015 +** or CACHE_WRITE to sqlite3_db_status().
 1.38016 +*/
 1.38017 +#define PAGER_STAT_HIT   0
 1.38018 +#define PAGER_STAT_MISS  1
 1.38019 +#define PAGER_STAT_WRITE 2
 1.38020 +
 1.38021 +/*
 1.38022 +** The following global variables hold counters used for
 1.38023 +** testing purposes only.  These variables do not exist in
 1.38024 +** a non-testing build.  These variables are not thread-safe.
 1.38025 +*/
 1.38026 +#ifdef SQLITE_TEST
 1.38027 +SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
 1.38028 +SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
 1.38029 +SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
 1.38030 +# define PAGER_INCR(v)  v++
 1.38031 +#else
 1.38032 +# define PAGER_INCR(v)
 1.38033 +#endif
 1.38034 +
 1.38035 +
 1.38036 +
 1.38037 +/*
 1.38038 +** Journal files begin with the following magic string.  The data
 1.38039 +** was obtained from /dev/random.  It is used only as a sanity check.
 1.38040 +**
 1.38041 +** Since version 2.8.0, the journal format contains additional sanity
 1.38042 +** checking information.  If the power fails while the journal is being
 1.38043 +** written, semi-random garbage data might appear in the journal
 1.38044 +** file after power is restored.  If an attempt is then made
 1.38045 +** to roll the journal back, the database could be corrupted.  The additional
 1.38046 +** sanity checking data is an attempt to discover the garbage in the
 1.38047 +** journal and ignore it.
 1.38048 +**
 1.38049 +** The sanity checking information for the new journal format consists
 1.38050 +** of a 32-bit checksum on each page of data.  The checksum covers both
 1.38051 +** the page number and the pPager->pageSize bytes of data for the page.
 1.38052 +** This cksum is initialized to a 32-bit random value that appears in the
 1.38053 +** journal file right after the header.  The random initializer is important,
 1.38054 +** because garbage data that appears at the end of a journal is likely
 1.38055 +** data that was once in other files that have now been deleted.  If the
 1.38056 +** garbage data came from an obsolete journal file, the checksums might
 1.38057 +** be correct.  But by initializing the checksum to random value which
 1.38058 +** is different for every journal, we minimize that risk.
 1.38059 +*/
 1.38060 +static const unsigned char aJournalMagic[] = {
 1.38061 +  0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
 1.38062 +};
 1.38063 +
 1.38064 +/*
 1.38065 +** The size of the of each page record in the journal is given by
 1.38066 +** the following macro.
 1.38067 +*/
 1.38068 +#define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
 1.38069 +
 1.38070 +/*
 1.38071 +** The journal header size for this pager. This is usually the same 
 1.38072 +** size as a single disk sector. See also setSectorSize().
 1.38073 +*/
 1.38074 +#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
 1.38075 +
 1.38076 +/*
 1.38077 +** The macro MEMDB is true if we are dealing with an in-memory database.
 1.38078 +** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
 1.38079 +** the value of MEMDB will be a constant and the compiler will optimize
 1.38080 +** out code that would never execute.
 1.38081 +*/
 1.38082 +#ifdef SQLITE_OMIT_MEMORYDB
 1.38083 +# define MEMDB 0
 1.38084 +#else
 1.38085 +# define MEMDB pPager->memDb
 1.38086 +#endif
 1.38087 +
 1.38088 +/*
 1.38089 +** The maximum legal page number is (2^31 - 1).
 1.38090 +*/
 1.38091 +#define PAGER_MAX_PGNO 2147483647
 1.38092 +
 1.38093 +/*
 1.38094 +** The argument to this macro is a file descriptor (type sqlite3_file*).
 1.38095 +** Return 0 if it is not open, or non-zero (but not 1) if it is.
 1.38096 +**
 1.38097 +** This is so that expressions can be written as:
 1.38098 +**
 1.38099 +**   if( isOpen(pPager->jfd) ){ ...
 1.38100 +**
 1.38101 +** instead of
 1.38102 +**
 1.38103 +**   if( pPager->jfd->pMethods ){ ...
 1.38104 +*/
 1.38105 +#define isOpen(pFd) ((pFd)->pMethods)
 1.38106 +
 1.38107 +/*
 1.38108 +** Return true if this pager uses a write-ahead log instead of the usual
 1.38109 +** rollback journal. Otherwise false.
 1.38110 +*/
 1.38111 +#ifndef SQLITE_OMIT_WAL
 1.38112 +static int pagerUseWal(Pager *pPager){
 1.38113 +  return (pPager->pWal!=0);
 1.38114 +}
 1.38115 +#else
 1.38116 +# define pagerUseWal(x) 0
 1.38117 +# define pagerRollbackWal(x) 0
 1.38118 +# define pagerWalFrames(v,w,x,y) 0
 1.38119 +# define pagerOpenWalIfPresent(z) SQLITE_OK
 1.38120 +# define pagerBeginReadTransaction(z) SQLITE_OK
 1.38121 +#endif
 1.38122 +
 1.38123 +#ifndef NDEBUG 
 1.38124 +/*
 1.38125 +** Usage:
 1.38126 +**
 1.38127 +**   assert( assert_pager_state(pPager) );
 1.38128 +**
 1.38129 +** This function runs many asserts to try to find inconsistencies in
 1.38130 +** the internal state of the Pager object.
 1.38131 +*/
 1.38132 +static int assert_pager_state(Pager *p){
 1.38133 +  Pager *pPager = p;
 1.38134 +
 1.38135 +  /* State must be valid. */
 1.38136 +  assert( p->eState==PAGER_OPEN
 1.38137 +       || p->eState==PAGER_READER
 1.38138 +       || p->eState==PAGER_WRITER_LOCKED
 1.38139 +       || p->eState==PAGER_WRITER_CACHEMOD
 1.38140 +       || p->eState==PAGER_WRITER_DBMOD
 1.38141 +       || p->eState==PAGER_WRITER_FINISHED
 1.38142 +       || p->eState==PAGER_ERROR
 1.38143 +  );
 1.38144 +
 1.38145 +  /* Regardless of the current state, a temp-file connection always behaves
 1.38146 +  ** as if it has an exclusive lock on the database file. It never updates
 1.38147 +  ** the change-counter field, so the changeCountDone flag is always set.
 1.38148 +  */
 1.38149 +  assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
 1.38150 +  assert( p->tempFile==0 || pPager->changeCountDone );
 1.38151 +
 1.38152 +  /* If the useJournal flag is clear, the journal-mode must be "OFF". 
 1.38153 +  ** And if the journal-mode is "OFF", the journal file must not be open.
 1.38154 +  */
 1.38155 +  assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
 1.38156 +  assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
 1.38157 +
 1.38158 +  /* Check that MEMDB implies noSync. And an in-memory journal. Since 
 1.38159 +  ** this means an in-memory pager performs no IO at all, it cannot encounter 
 1.38160 +  ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing 
 1.38161 +  ** a journal file. (although the in-memory journal implementation may 
 1.38162 +  ** return SQLITE_IOERR_NOMEM while the journal file is being written). It 
 1.38163 +  ** is therefore not possible for an in-memory pager to enter the ERROR 
 1.38164 +  ** state.
 1.38165 +  */
 1.38166 +  if( MEMDB ){
 1.38167 +    assert( p->noSync );
 1.38168 +    assert( p->journalMode==PAGER_JOURNALMODE_OFF 
 1.38169 +         || p->journalMode==PAGER_JOURNALMODE_MEMORY 
 1.38170 +    );
 1.38171 +    assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
 1.38172 +    assert( pagerUseWal(p)==0 );
 1.38173 +  }
 1.38174 +
 1.38175 +  /* If changeCountDone is set, a RESERVED lock or greater must be held
 1.38176 +  ** on the file.
 1.38177 +  */
 1.38178 +  assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
 1.38179 +  assert( p->eLock!=PENDING_LOCK );
 1.38180 +
 1.38181 +  switch( p->eState ){
 1.38182 +    case PAGER_OPEN:
 1.38183 +      assert( !MEMDB );
 1.38184 +      assert( pPager->errCode==SQLITE_OK );
 1.38185 +      assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
 1.38186 +      break;
 1.38187 +
 1.38188 +    case PAGER_READER:
 1.38189 +      assert( pPager->errCode==SQLITE_OK );
 1.38190 +      assert( p->eLock!=UNKNOWN_LOCK );
 1.38191 +      assert( p->eLock>=SHARED_LOCK );
 1.38192 +      break;
 1.38193 +
 1.38194 +    case PAGER_WRITER_LOCKED:
 1.38195 +      assert( p->eLock!=UNKNOWN_LOCK );
 1.38196 +      assert( pPager->errCode==SQLITE_OK );
 1.38197 +      if( !pagerUseWal(pPager) ){
 1.38198 +        assert( p->eLock>=RESERVED_LOCK );
 1.38199 +      }
 1.38200 +      assert( pPager->dbSize==pPager->dbOrigSize );
 1.38201 +      assert( pPager->dbOrigSize==pPager->dbFileSize );
 1.38202 +      assert( pPager->dbOrigSize==pPager->dbHintSize );
 1.38203 +      assert( pPager->setMaster==0 );
 1.38204 +      break;
 1.38205 +
 1.38206 +    case PAGER_WRITER_CACHEMOD:
 1.38207 +      assert( p->eLock!=UNKNOWN_LOCK );
 1.38208 +      assert( pPager->errCode==SQLITE_OK );
 1.38209 +      if( !pagerUseWal(pPager) ){
 1.38210 +        /* It is possible that if journal_mode=wal here that neither the
 1.38211 +        ** journal file nor the WAL file are open. This happens during
 1.38212 +        ** a rollback transaction that switches from journal_mode=off
 1.38213 +        ** to journal_mode=wal.
 1.38214 +        */
 1.38215 +        assert( p->eLock>=RESERVED_LOCK );
 1.38216 +        assert( isOpen(p->jfd) 
 1.38217 +             || p->journalMode==PAGER_JOURNALMODE_OFF 
 1.38218 +             || p->journalMode==PAGER_JOURNALMODE_WAL 
 1.38219 +        );
 1.38220 +      }
 1.38221 +      assert( pPager->dbOrigSize==pPager->dbFileSize );
 1.38222 +      assert( pPager->dbOrigSize==pPager->dbHintSize );
 1.38223 +      break;
 1.38224 +
 1.38225 +    case PAGER_WRITER_DBMOD:
 1.38226 +      assert( p->eLock==EXCLUSIVE_LOCK );
 1.38227 +      assert( pPager->errCode==SQLITE_OK );
 1.38228 +      assert( !pagerUseWal(pPager) );
 1.38229 +      assert( p->eLock>=EXCLUSIVE_LOCK );
 1.38230 +      assert( isOpen(p->jfd) 
 1.38231 +           || p->journalMode==PAGER_JOURNALMODE_OFF 
 1.38232 +           || p->journalMode==PAGER_JOURNALMODE_WAL 
 1.38233 +      );
 1.38234 +      assert( pPager->dbOrigSize<=pPager->dbHintSize );
 1.38235 +      break;
 1.38236 +
 1.38237 +    case PAGER_WRITER_FINISHED:
 1.38238 +      assert( p->eLock==EXCLUSIVE_LOCK );
 1.38239 +      assert( pPager->errCode==SQLITE_OK );
 1.38240 +      assert( !pagerUseWal(pPager) );
 1.38241 +      assert( isOpen(p->jfd) 
 1.38242 +           || p->journalMode==PAGER_JOURNALMODE_OFF 
 1.38243 +           || p->journalMode==PAGER_JOURNALMODE_WAL 
 1.38244 +      );
 1.38245 +      break;
 1.38246 +
 1.38247 +    case PAGER_ERROR:
 1.38248 +      /* There must be at least one outstanding reference to the pager if
 1.38249 +      ** in ERROR state. Otherwise the pager should have already dropped
 1.38250 +      ** back to OPEN state.
 1.38251 +      */
 1.38252 +      assert( pPager->errCode!=SQLITE_OK );
 1.38253 +      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
 1.38254 +      break;
 1.38255 +  }
 1.38256 +
 1.38257 +  return 1;
 1.38258 +}
 1.38259 +#endif /* ifndef NDEBUG */
 1.38260 +
 1.38261 +#ifdef SQLITE_DEBUG 
 1.38262 +/*
 1.38263 +** Return a pointer to a human readable string in a static buffer
 1.38264 +** containing the state of the Pager object passed as an argument. This
 1.38265 +** is intended to be used within debuggers. For example, as an alternative
 1.38266 +** to "print *pPager" in gdb:
 1.38267 +**
 1.38268 +** (gdb) printf "%s", print_pager_state(pPager)
 1.38269 +*/
 1.38270 +static char *print_pager_state(Pager *p){
 1.38271 +  static char zRet[1024];
 1.38272 +
 1.38273 +  sqlite3_snprintf(1024, zRet,
 1.38274 +      "Filename:      %s\n"
 1.38275 +      "State:         %s errCode=%d\n"
 1.38276 +      "Lock:          %s\n"
 1.38277 +      "Locking mode:  locking_mode=%s\n"
 1.38278 +      "Journal mode:  journal_mode=%s\n"
 1.38279 +      "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
 1.38280 +      "Journal:       journalOff=%lld journalHdr=%lld\n"
 1.38281 +      "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
 1.38282 +      , p->zFilename
 1.38283 +      , p->eState==PAGER_OPEN            ? "OPEN" :
 1.38284 +        p->eState==PAGER_READER          ? "READER" :
 1.38285 +        p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
 1.38286 +        p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
 1.38287 +        p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
 1.38288 +        p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
 1.38289 +        p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
 1.38290 +      , (int)p->errCode
 1.38291 +      , p->eLock==NO_LOCK         ? "NO_LOCK" :
 1.38292 +        p->eLock==RESERVED_LOCK   ? "RESERVED" :
 1.38293 +        p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
 1.38294 +        p->eLock==SHARED_LOCK     ? "SHARED" :
 1.38295 +        p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
 1.38296 +      , p->exclusiveMode ? "exclusive" : "normal"
 1.38297 +      , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
 1.38298 +        p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
 1.38299 +        p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
 1.38300 +        p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
 1.38301 +        p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
 1.38302 +        p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
 1.38303 +      , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
 1.38304 +      , p->journalOff, p->journalHdr
 1.38305 +      , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
 1.38306 +  );
 1.38307 +
 1.38308 +  return zRet;
 1.38309 +}
 1.38310 +#endif
 1.38311 +
 1.38312 +/*
 1.38313 +** Return true if it is necessary to write page *pPg into the sub-journal.
 1.38314 +** A page needs to be written into the sub-journal if there exists one
 1.38315 +** or more open savepoints for which:
 1.38316 +**
 1.38317 +**   * The page-number is less than or equal to PagerSavepoint.nOrig, and
 1.38318 +**   * The bit corresponding to the page-number is not set in
 1.38319 +**     PagerSavepoint.pInSavepoint.
 1.38320 +*/
 1.38321 +static int subjRequiresPage(PgHdr *pPg){
 1.38322 +  Pgno pgno = pPg->pgno;
 1.38323 +  Pager *pPager = pPg->pPager;
 1.38324 +  int i;
 1.38325 +  for(i=0; i<pPager->nSavepoint; i++){
 1.38326 +    PagerSavepoint *p = &pPager->aSavepoint[i];
 1.38327 +    if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
 1.38328 +      return 1;
 1.38329 +    }
 1.38330 +  }
 1.38331 +  return 0;
 1.38332 +}
 1.38333 +
 1.38334 +/*
 1.38335 +** Return true if the page is already in the journal file.
 1.38336 +*/
 1.38337 +static int pageInJournal(PgHdr *pPg){
 1.38338 +  return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
 1.38339 +}
 1.38340 +
 1.38341 +/*
 1.38342 +** Read a 32-bit integer from the given file descriptor.  Store the integer
 1.38343 +** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
 1.38344 +** error code is something goes wrong.
 1.38345 +**
 1.38346 +** All values are stored on disk as big-endian.
 1.38347 +*/
 1.38348 +static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
 1.38349 +  unsigned char ac[4];
 1.38350 +  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
 1.38351 +  if( rc==SQLITE_OK ){
 1.38352 +    *pRes = sqlite3Get4byte(ac);
 1.38353 +  }
 1.38354 +  return rc;
 1.38355 +}
 1.38356 +
 1.38357 +/*
 1.38358 +** Write a 32-bit integer into a string buffer in big-endian byte order.
 1.38359 +*/
 1.38360 +#define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
 1.38361 +
 1.38362 +
 1.38363 +/*
 1.38364 +** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
 1.38365 +** on success or an error code is something goes wrong.
 1.38366 +*/
 1.38367 +static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
 1.38368 +  char ac[4];
 1.38369 +  put32bits(ac, val);
 1.38370 +  return sqlite3OsWrite(fd, ac, 4, offset);
 1.38371 +}
 1.38372 +
 1.38373 +/*
 1.38374 +** Unlock the database file to level eLock, which must be either NO_LOCK
 1.38375 +** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
 1.38376 +** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
 1.38377 +**
 1.38378 +** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
 1.38379 +** called, do not modify it. See the comment above the #define of 
 1.38380 +** UNKNOWN_LOCK for an explanation of this.
 1.38381 +*/
 1.38382 +static int pagerUnlockDb(Pager *pPager, int eLock){
 1.38383 +  int rc = SQLITE_OK;
 1.38384 +
 1.38385 +  assert( !pPager->exclusiveMode || pPager->eLock==eLock );
 1.38386 +  assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
 1.38387 +  assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
 1.38388 +  if( isOpen(pPager->fd) ){
 1.38389 +    assert( pPager->eLock>=eLock );
 1.38390 +    rc = sqlite3OsUnlock(pPager->fd, eLock);
 1.38391 +    if( pPager->eLock!=UNKNOWN_LOCK ){
 1.38392 +      pPager->eLock = (u8)eLock;
 1.38393 +    }
 1.38394 +    IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
 1.38395 +  }
 1.38396 +  return rc;
 1.38397 +}
 1.38398 +
 1.38399 +/*
 1.38400 +** Lock the database file to level eLock, which must be either SHARED_LOCK,
 1.38401 +** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
 1.38402 +** Pager.eLock variable to the new locking state. 
 1.38403 +**
 1.38404 +** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is 
 1.38405 +** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. 
 1.38406 +** See the comment above the #define of UNKNOWN_LOCK for an explanation 
 1.38407 +** of this.
 1.38408 +*/
 1.38409 +static int pagerLockDb(Pager *pPager, int eLock){
 1.38410 +  int rc = SQLITE_OK;
 1.38411 +
 1.38412 +  assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
 1.38413 +  if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
 1.38414 +    rc = sqlite3OsLock(pPager->fd, eLock);
 1.38415 +    if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
 1.38416 +      pPager->eLock = (u8)eLock;
 1.38417 +      IOTRACE(("LOCK %p %d\n", pPager, eLock))
 1.38418 +    }
 1.38419 +  }
 1.38420 +  return rc;
 1.38421 +}
 1.38422 +
 1.38423 +/*
 1.38424 +** This function determines whether or not the atomic-write optimization
 1.38425 +** can be used with this pager. The optimization can be used if:
 1.38426 +**
 1.38427 +**  (a) the value returned by OsDeviceCharacteristics() indicates that
 1.38428 +**      a database page may be written atomically, and
 1.38429 +**  (b) the value returned by OsSectorSize() is less than or equal
 1.38430 +**      to the page size.
 1.38431 +**
 1.38432 +** The optimization is also always enabled for temporary files. It is
 1.38433 +** an error to call this function if pPager is opened on an in-memory
 1.38434 +** database.
 1.38435 +**
 1.38436 +** If the optimization cannot be used, 0 is returned. If it can be used,
 1.38437 +** then the value returned is the size of the journal file when it
 1.38438 +** contains rollback data for exactly one page.
 1.38439 +*/
 1.38440 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
 1.38441 +static int jrnlBufferSize(Pager *pPager){
 1.38442 +  assert( !MEMDB );
 1.38443 +  if( !pPager->tempFile ){
 1.38444 +    int dc;                           /* Device characteristics */
 1.38445 +    int nSector;                      /* Sector size */
 1.38446 +    int szPage;                       /* Page size */
 1.38447 +
 1.38448 +    assert( isOpen(pPager->fd) );
 1.38449 +    dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 1.38450 +    nSector = pPager->sectorSize;
 1.38451 +    szPage = pPager->pageSize;
 1.38452 +
 1.38453 +    assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 1.38454 +    assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 1.38455 +    if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
 1.38456 +      return 0;
 1.38457 +    }
 1.38458 +  }
 1.38459 +
 1.38460 +  return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
 1.38461 +}
 1.38462 +#endif
 1.38463 +
 1.38464 +/*
 1.38465 +** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
 1.38466 +** on the cache using a hash function.  This is used for testing
 1.38467 +** and debugging only.
 1.38468 +*/
 1.38469 +#ifdef SQLITE_CHECK_PAGES
 1.38470 +/*
 1.38471 +** Return a 32-bit hash of the page data for pPage.
 1.38472 +*/
 1.38473 +static u32 pager_datahash(int nByte, unsigned char *pData){
 1.38474 +  u32 hash = 0;
 1.38475 +  int i;
 1.38476 +  for(i=0; i<nByte; i++){
 1.38477 +    hash = (hash*1039) + pData[i];
 1.38478 +  }
 1.38479 +  return hash;
 1.38480 +}
 1.38481 +static u32 pager_pagehash(PgHdr *pPage){
 1.38482 +  return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
 1.38483 +}
 1.38484 +static void pager_set_pagehash(PgHdr *pPage){
 1.38485 +  pPage->pageHash = pager_pagehash(pPage);
 1.38486 +}
 1.38487 +
 1.38488 +/*
 1.38489 +** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
 1.38490 +** is defined, and NDEBUG is not defined, an assert() statement checks
 1.38491 +** that the page is either dirty or still matches the calculated page-hash.
 1.38492 +*/
 1.38493 +#define CHECK_PAGE(x) checkPage(x)
 1.38494 +static void checkPage(PgHdr *pPg){
 1.38495 +  Pager *pPager = pPg->pPager;
 1.38496 +  assert( pPager->eState!=PAGER_ERROR );
 1.38497 +  assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
 1.38498 +}
 1.38499 +
 1.38500 +#else
 1.38501 +#define pager_datahash(X,Y)  0
 1.38502 +#define pager_pagehash(X)  0
 1.38503 +#define pager_set_pagehash(X)
 1.38504 +#define CHECK_PAGE(x)
 1.38505 +#endif  /* SQLITE_CHECK_PAGES */
 1.38506 +
 1.38507 +/*
 1.38508 +** When this is called the journal file for pager pPager must be open.
 1.38509 +** This function attempts to read a master journal file name from the 
 1.38510 +** end of the file and, if successful, copies it into memory supplied 
 1.38511 +** by the caller. See comments above writeMasterJournal() for the format
 1.38512 +** used to store a master journal file name at the end of a journal file.
 1.38513 +**
 1.38514 +** zMaster must point to a buffer of at least nMaster bytes allocated by
 1.38515 +** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
 1.38516 +** enough space to write the master journal name). If the master journal
 1.38517 +** name in the journal is longer than nMaster bytes (including a
 1.38518 +** nul-terminator), then this is handled as if no master journal name
 1.38519 +** were present in the journal.
 1.38520 +**
 1.38521 +** If a master journal file name is present at the end of the journal
 1.38522 +** file, then it is copied into the buffer pointed to by zMaster. A
 1.38523 +** nul-terminator byte is appended to the buffer following the master
 1.38524 +** journal file name.
 1.38525 +**
 1.38526 +** If it is determined that no master journal file name is present 
 1.38527 +** zMaster[0] is set to 0 and SQLITE_OK returned.
 1.38528 +**
 1.38529 +** If an error occurs while reading from the journal file, an SQLite
 1.38530 +** error code is returned.
 1.38531 +*/
 1.38532 +static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
 1.38533 +  int rc;                    /* Return code */
 1.38534 +  u32 len;                   /* Length in bytes of master journal name */
 1.38535 +  i64 szJ;                   /* Total size in bytes of journal file pJrnl */
 1.38536 +  u32 cksum;                 /* MJ checksum value read from journal */
 1.38537 +  u32 u;                     /* Unsigned loop counter */
 1.38538 +  unsigned char aMagic[8];   /* A buffer to hold the magic header */
 1.38539 +  zMaster[0] = '\0';
 1.38540 +
 1.38541 +  if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
 1.38542 +   || szJ<16
 1.38543 +   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
 1.38544 +   || len>=nMaster 
 1.38545 +   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
 1.38546 +   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
 1.38547 +   || memcmp(aMagic, aJournalMagic, 8)
 1.38548 +   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
 1.38549 +  ){
 1.38550 +    return rc;
 1.38551 +  }
 1.38552 +
 1.38553 +  /* See if the checksum matches the master journal name */
 1.38554 +  for(u=0; u<len; u++){
 1.38555 +    cksum -= zMaster[u];
 1.38556 +  }
 1.38557 +  if( cksum ){
 1.38558 +    /* If the checksum doesn't add up, then one or more of the disk sectors
 1.38559 +    ** containing the master journal filename is corrupted. This means
 1.38560 +    ** definitely roll back, so just return SQLITE_OK and report a (nul)
 1.38561 +    ** master-journal filename.
 1.38562 +    */
 1.38563 +    len = 0;
 1.38564 +  }
 1.38565 +  zMaster[len] = '\0';
 1.38566 +   
 1.38567 +  return SQLITE_OK;
 1.38568 +}
 1.38569 +
 1.38570 +/*
 1.38571 +** Return the offset of the sector boundary at or immediately 
 1.38572 +** following the value in pPager->journalOff, assuming a sector 
 1.38573 +** size of pPager->sectorSize bytes.
 1.38574 +**
 1.38575 +** i.e for a sector size of 512:
 1.38576 +**
 1.38577 +**   Pager.journalOff          Return value
 1.38578 +**   ---------------------------------------
 1.38579 +**   0                         0
 1.38580 +**   512                       512
 1.38581 +**   100                       512
 1.38582 +**   2000                      2048
 1.38583 +** 
 1.38584 +*/
 1.38585 +static i64 journalHdrOffset(Pager *pPager){
 1.38586 +  i64 offset = 0;
 1.38587 +  i64 c = pPager->journalOff;
 1.38588 +  if( c ){
 1.38589 +    offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
 1.38590 +  }
 1.38591 +  assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
 1.38592 +  assert( offset>=c );
 1.38593 +  assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
 1.38594 +  return offset;
 1.38595 +}
 1.38596 +
 1.38597 +/*
 1.38598 +** The journal file must be open when this function is called.
 1.38599 +**
 1.38600 +** This function is a no-op if the journal file has not been written to
 1.38601 +** within the current transaction (i.e. if Pager.journalOff==0).
 1.38602 +**
 1.38603 +** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
 1.38604 +** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
 1.38605 +** zero the 28-byte header at the start of the journal file. In either case, 
 1.38606 +** if the pager is not in no-sync mode, sync the journal file immediately 
 1.38607 +** after writing or truncating it.
 1.38608 +**
 1.38609 +** If Pager.journalSizeLimit is set to a positive, non-zero value, and
 1.38610 +** following the truncation or zeroing described above the size of the 
 1.38611 +** journal file in bytes is larger than this value, then truncate the
 1.38612 +** journal file to Pager.journalSizeLimit bytes. The journal file does
 1.38613 +** not need to be synced following this operation.
 1.38614 +**
 1.38615 +** If an IO error occurs, abandon processing and return the IO error code.
 1.38616 +** Otherwise, return SQLITE_OK.
 1.38617 +*/
 1.38618 +static int zeroJournalHdr(Pager *pPager, int doTruncate){
 1.38619 +  int rc = SQLITE_OK;                               /* Return code */
 1.38620 +  assert( isOpen(pPager->jfd) );
 1.38621 +  if( pPager->journalOff ){
 1.38622 +    const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
 1.38623 +
 1.38624 +    IOTRACE(("JZEROHDR %p\n", pPager))
 1.38625 +    if( doTruncate || iLimit==0 ){
 1.38626 +      rc = sqlite3OsTruncate(pPager->jfd, 0);
 1.38627 +    }else{
 1.38628 +      static const char zeroHdr[28] = {0};
 1.38629 +      rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
 1.38630 +    }
 1.38631 +    if( rc==SQLITE_OK && !pPager->noSync ){
 1.38632 +      rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
 1.38633 +    }
 1.38634 +
 1.38635 +    /* At this point the transaction is committed but the write lock 
 1.38636 +    ** is still held on the file. If there is a size limit configured for 
 1.38637 +    ** the persistent journal and the journal file currently consumes more
 1.38638 +    ** space than that limit allows for, truncate it now. There is no need
 1.38639 +    ** to sync the file following this operation.
 1.38640 +    */
 1.38641 +    if( rc==SQLITE_OK && iLimit>0 ){
 1.38642 +      i64 sz;
 1.38643 +      rc = sqlite3OsFileSize(pPager->jfd, &sz);
 1.38644 +      if( rc==SQLITE_OK && sz>iLimit ){
 1.38645 +        rc = sqlite3OsTruncate(pPager->jfd, iLimit);
 1.38646 +      }
 1.38647 +    }
 1.38648 +  }
 1.38649 +  return rc;
 1.38650 +}
 1.38651 +
 1.38652 +/*
 1.38653 +** The journal file must be open when this routine is called. A journal
 1.38654 +** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
 1.38655 +** current location.
 1.38656 +**
 1.38657 +** The format for the journal header is as follows:
 1.38658 +** - 8 bytes: Magic identifying journal format.
 1.38659 +** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
 1.38660 +** - 4 bytes: Random number used for page hash.
 1.38661 +** - 4 bytes: Initial database page count.
 1.38662 +** - 4 bytes: Sector size used by the process that wrote this journal.
 1.38663 +** - 4 bytes: Database page size.
 1.38664 +** 
 1.38665 +** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
 1.38666 +*/
 1.38667 +static int writeJournalHdr(Pager *pPager){
 1.38668 +  int rc = SQLITE_OK;                 /* Return code */
 1.38669 +  char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
 1.38670 +  u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
 1.38671 +  u32 nWrite;                         /* Bytes of header sector written */
 1.38672 +  int ii;                             /* Loop counter */
 1.38673 +
 1.38674 +  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
 1.38675 +
 1.38676 +  if( nHeader>JOURNAL_HDR_SZ(pPager) ){
 1.38677 +    nHeader = JOURNAL_HDR_SZ(pPager);
 1.38678 +  }
 1.38679 +
 1.38680 +  /* If there are active savepoints and any of them were created 
 1.38681 +  ** since the most recent journal header was written, update the 
 1.38682 +  ** PagerSavepoint.iHdrOffset fields now.
 1.38683 +  */
 1.38684 +  for(ii=0; ii<pPager->nSavepoint; ii++){
 1.38685 +    if( pPager->aSavepoint[ii].iHdrOffset==0 ){
 1.38686 +      pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
 1.38687 +    }
 1.38688 +  }
 1.38689 +
 1.38690 +  pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
 1.38691 +
 1.38692 +  /* 
 1.38693 +  ** Write the nRec Field - the number of page records that follow this
 1.38694 +  ** journal header. Normally, zero is written to this value at this time.
 1.38695 +  ** After the records are added to the journal (and the journal synced, 
 1.38696 +  ** if in full-sync mode), the zero is overwritten with the true number
 1.38697 +  ** of records (see syncJournal()).
 1.38698 +  **
 1.38699 +  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
 1.38700 +  ** reading the journal this value tells SQLite to assume that the
 1.38701 +  ** rest of the journal file contains valid page records. This assumption
 1.38702 +  ** is dangerous, as if a failure occurred whilst writing to the journal
 1.38703 +  ** file it may contain some garbage data. There are two scenarios
 1.38704 +  ** where this risk can be ignored:
 1.38705 +  **
 1.38706 +  **   * When the pager is in no-sync mode. Corruption can follow a
 1.38707 +  **     power failure in this case anyway.
 1.38708 +  **
 1.38709 +  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
 1.38710 +  **     that garbage data is never appended to the journal file.
 1.38711 +  */
 1.38712 +  assert( isOpen(pPager->fd) || pPager->noSync );
 1.38713 +  if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
 1.38714 +   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
 1.38715 +  ){
 1.38716 +    memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
 1.38717 +    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
 1.38718 +  }else{
 1.38719 +    memset(zHeader, 0, sizeof(aJournalMagic)+4);
 1.38720 +  }
 1.38721 +
 1.38722 +  /* The random check-hash initialiser */ 
 1.38723 +  sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
 1.38724 +  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
 1.38725 +  /* The initial database size */
 1.38726 +  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
 1.38727 +  /* The assumed sector size for this process */
 1.38728 +  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
 1.38729 +
 1.38730 +  /* The page size */
 1.38731 +  put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
 1.38732 +
 1.38733 +  /* Initializing the tail of the buffer is not necessary.  Everything
 1.38734 +  ** works find if the following memset() is omitted.  But initializing
 1.38735 +  ** the memory prevents valgrind from complaining, so we are willing to
 1.38736 +  ** take the performance hit.
 1.38737 +  */
 1.38738 +  memset(&zHeader[sizeof(aJournalMagic)+20], 0,
 1.38739 +         nHeader-(sizeof(aJournalMagic)+20));
 1.38740 +
 1.38741 +  /* In theory, it is only necessary to write the 28 bytes that the 
 1.38742 +  ** journal header consumes to the journal file here. Then increment the 
 1.38743 +  ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next 
 1.38744 +  ** record is written to the following sector (leaving a gap in the file
 1.38745 +  ** that will be implicitly filled in by the OS).
 1.38746 +  **
 1.38747 +  ** However it has been discovered that on some systems this pattern can 
 1.38748 +  ** be significantly slower than contiguously writing data to the file,
 1.38749 +  ** even if that means explicitly writing data to the block of 
 1.38750 +  ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
 1.38751 +  ** is done. 
 1.38752 +  **
 1.38753 +  ** The loop is required here in case the sector-size is larger than the 
 1.38754 +  ** database page size. Since the zHeader buffer is only Pager.pageSize
 1.38755 +  ** bytes in size, more than one call to sqlite3OsWrite() may be required
 1.38756 +  ** to populate the entire journal header sector.
 1.38757 +  */ 
 1.38758 +  for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
 1.38759 +    IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
 1.38760 +    rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
 1.38761 +    assert( pPager->journalHdr <= pPager->journalOff );
 1.38762 +    pPager->journalOff += nHeader;
 1.38763 +  }
 1.38764 +
 1.38765 +  return rc;
 1.38766 +}
 1.38767 +
 1.38768 +/*
 1.38769 +** The journal file must be open when this is called. A journal header file
 1.38770 +** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
 1.38771 +** file. The current location in the journal file is given by
 1.38772 +** pPager->journalOff. See comments above function writeJournalHdr() for
 1.38773 +** a description of the journal header format.
 1.38774 +**
 1.38775 +** If the header is read successfully, *pNRec is set to the number of
 1.38776 +** page records following this header and *pDbSize is set to the size of the
 1.38777 +** database before the transaction began, in pages. Also, pPager->cksumInit
 1.38778 +** is set to the value read from the journal header. SQLITE_OK is returned
 1.38779 +** in this case.
 1.38780 +**
 1.38781 +** If the journal header file appears to be corrupted, SQLITE_DONE is
 1.38782 +** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
 1.38783 +** cannot be read from the journal file an error code is returned.
 1.38784 +*/
 1.38785 +static int readJournalHdr(
 1.38786 +  Pager *pPager,               /* Pager object */
 1.38787 +  int isHot,
 1.38788 +  i64 journalSize,             /* Size of the open journal file in bytes */
 1.38789 +  u32 *pNRec,                  /* OUT: Value read from the nRec field */
 1.38790 +  u32 *pDbSize                 /* OUT: Value of original database size field */
 1.38791 +){
 1.38792 +  int rc;                      /* Return code */
 1.38793 +  unsigned char aMagic[8];     /* A buffer to hold the magic header */
 1.38794 +  i64 iHdrOff;                 /* Offset of journal header being read */
 1.38795 +
 1.38796 +  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
 1.38797 +
 1.38798 +  /* Advance Pager.journalOff to the start of the next sector. If the
 1.38799 +  ** journal file is too small for there to be a header stored at this
 1.38800 +  ** point, return SQLITE_DONE.
 1.38801 +  */
 1.38802 +  pPager->journalOff = journalHdrOffset(pPager);
 1.38803 +  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
 1.38804 +    return SQLITE_DONE;
 1.38805 +  }
 1.38806 +  iHdrOff = pPager->journalOff;
 1.38807 +
 1.38808 +  /* Read in the first 8 bytes of the journal header. If they do not match
 1.38809 +  ** the  magic string found at the start of each journal header, return
 1.38810 +  ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
 1.38811 +  ** proceed.
 1.38812 +  */
 1.38813 +  if( isHot || iHdrOff!=pPager->journalHdr ){
 1.38814 +    rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
 1.38815 +    if( rc ){
 1.38816 +      return rc;
 1.38817 +    }
 1.38818 +    if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
 1.38819 +      return SQLITE_DONE;
 1.38820 +    }
 1.38821 +  }
 1.38822 +
 1.38823 +  /* Read the first three 32-bit fields of the journal header: The nRec
 1.38824 +  ** field, the checksum-initializer and the database size at the start
 1.38825 +  ** of the transaction. Return an error code if anything goes wrong.
 1.38826 +  */
 1.38827 +  if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
 1.38828 +   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
 1.38829 +   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
 1.38830 +  ){
 1.38831 +    return rc;
 1.38832 +  }
 1.38833 +
 1.38834 +  if( pPager->journalOff==0 ){
 1.38835 +    u32 iPageSize;               /* Page-size field of journal header */
 1.38836 +    u32 iSectorSize;             /* Sector-size field of journal header */
 1.38837 +
 1.38838 +    /* Read the page-size and sector-size journal header fields. */
 1.38839 +    if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
 1.38840 +     || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
 1.38841 +    ){
 1.38842 +      return rc;
 1.38843 +    }
 1.38844 +
 1.38845 +    /* Versions of SQLite prior to 3.5.8 set the page-size field of the
 1.38846 +    ** journal header to zero. In this case, assume that the Pager.pageSize
 1.38847 +    ** variable is already set to the correct page size.
 1.38848 +    */
 1.38849 +    if( iPageSize==0 ){
 1.38850 +      iPageSize = pPager->pageSize;
 1.38851 +    }
 1.38852 +
 1.38853 +    /* Check that the values read from the page-size and sector-size fields
 1.38854 +    ** are within range. To be 'in range', both values need to be a power
 1.38855 +    ** of two greater than or equal to 512 or 32, and not greater than their 
 1.38856 +    ** respective compile time maximum limits.
 1.38857 +    */
 1.38858 +    if( iPageSize<512                  || iSectorSize<32
 1.38859 +     || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
 1.38860 +     || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0 
 1.38861 +    ){
 1.38862 +      /* If the either the page-size or sector-size in the journal-header is 
 1.38863 +      ** invalid, then the process that wrote the journal-header must have 
 1.38864 +      ** crashed before the header was synced. In this case stop reading 
 1.38865 +      ** the journal file here.
 1.38866 +      */
 1.38867 +      return SQLITE_DONE;
 1.38868 +    }
 1.38869 +
 1.38870 +    /* Update the page-size to match the value read from the journal. 
 1.38871 +    ** Use a testcase() macro to make sure that malloc failure within 
 1.38872 +    ** PagerSetPagesize() is tested.
 1.38873 +    */
 1.38874 +    rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
 1.38875 +    testcase( rc!=SQLITE_OK );
 1.38876 +
 1.38877 +    /* Update the assumed sector-size to match the value used by 
 1.38878 +    ** the process that created this journal. If this journal was
 1.38879 +    ** created by a process other than this one, then this routine
 1.38880 +    ** is being called from within pager_playback(). The local value
 1.38881 +    ** of Pager.sectorSize is restored at the end of that routine.
 1.38882 +    */
 1.38883 +    pPager->sectorSize = iSectorSize;
 1.38884 +  }
 1.38885 +
 1.38886 +  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
 1.38887 +  return rc;
 1.38888 +}
 1.38889 +
 1.38890 +
 1.38891 +/*
 1.38892 +** Write the supplied master journal name into the journal file for pager
 1.38893 +** pPager at the current location. The master journal name must be the last
 1.38894 +** thing written to a journal file. If the pager is in full-sync mode, the
 1.38895 +** journal file descriptor is advanced to the next sector boundary before
 1.38896 +** anything is written. The format is:
 1.38897 +**
 1.38898 +**   + 4 bytes: PAGER_MJ_PGNO.
 1.38899 +**   + N bytes: Master journal filename in utf-8.
 1.38900 +**   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
 1.38901 +**   + 4 bytes: Master journal name checksum.
 1.38902 +**   + 8 bytes: aJournalMagic[].
 1.38903 +**
 1.38904 +** The master journal page checksum is the sum of the bytes in the master
 1.38905 +** journal name, where each byte is interpreted as a signed 8-bit integer.
 1.38906 +**
 1.38907 +** If zMaster is a NULL pointer (occurs for a single database transaction), 
 1.38908 +** this call is a no-op.
 1.38909 +*/
 1.38910 +static int writeMasterJournal(Pager *pPager, const char *zMaster){
 1.38911 +  int rc;                          /* Return code */
 1.38912 +  int nMaster;                     /* Length of string zMaster */
 1.38913 +  i64 iHdrOff;                     /* Offset of header in journal file */
 1.38914 +  i64 jrnlSize;                    /* Size of journal file on disk */
 1.38915 +  u32 cksum = 0;                   /* Checksum of string zMaster */
 1.38916 +
 1.38917 +  assert( pPager->setMaster==0 );
 1.38918 +  assert( !pagerUseWal(pPager) );
 1.38919 +
 1.38920 +  if( !zMaster 
 1.38921 +   || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
 1.38922 +   || pPager->journalMode==PAGER_JOURNALMODE_OFF 
 1.38923 +  ){
 1.38924 +    return SQLITE_OK;
 1.38925 +  }
 1.38926 +  pPager->setMaster = 1;
 1.38927 +  assert( isOpen(pPager->jfd) );
 1.38928 +  assert( pPager->journalHdr <= pPager->journalOff );
 1.38929 +
 1.38930 +  /* Calculate the length in bytes and the checksum of zMaster */
 1.38931 +  for(nMaster=0; zMaster[nMaster]; nMaster++){
 1.38932 +    cksum += zMaster[nMaster];
 1.38933 +  }
 1.38934 +
 1.38935 +  /* If in full-sync mode, advance to the next disk sector before writing
 1.38936 +  ** the master journal name. This is in case the previous page written to
 1.38937 +  ** the journal has already been synced.
 1.38938 +  */
 1.38939 +  if( pPager->fullSync ){
 1.38940 +    pPager->journalOff = journalHdrOffset(pPager);
 1.38941 +  }
 1.38942 +  iHdrOff = pPager->journalOff;
 1.38943 +
 1.38944 +  /* Write the master journal data to the end of the journal file. If
 1.38945 +  ** an error occurs, return the error code to the caller.
 1.38946 +  */
 1.38947 +  if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
 1.38948 +   || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
 1.38949 +   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
 1.38950 +   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
 1.38951 +   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
 1.38952 +  ){
 1.38953 +    return rc;
 1.38954 +  }
 1.38955 +  pPager->journalOff += (nMaster+20);
 1.38956 +
 1.38957 +  /* If the pager is in peristent-journal mode, then the physical 
 1.38958 +  ** journal-file may extend past the end of the master-journal name
 1.38959 +  ** and 8 bytes of magic data just written to the file. This is 
 1.38960 +  ** dangerous because the code to rollback a hot-journal file
 1.38961 +  ** will not be able to find the master-journal name to determine 
 1.38962 +  ** whether or not the journal is hot. 
 1.38963 +  **
 1.38964 +  ** Easiest thing to do in this scenario is to truncate the journal 
 1.38965 +  ** file to the required size.
 1.38966 +  */ 
 1.38967 +  if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
 1.38968 +   && jrnlSize>pPager->journalOff
 1.38969 +  ){
 1.38970 +    rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
 1.38971 +  }
 1.38972 +  return rc;
 1.38973 +}
 1.38974 +
 1.38975 +/*
 1.38976 +** Find a page in the hash table given its page number. Return
 1.38977 +** a pointer to the page or NULL if the requested page is not 
 1.38978 +** already in memory.
 1.38979 +*/
 1.38980 +static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
 1.38981 +  PgHdr *p;                         /* Return value */
 1.38982 +
 1.38983 +  /* It is not possible for a call to PcacheFetch() with createFlag==0 to
 1.38984 +  ** fail, since no attempt to allocate dynamic memory will be made.
 1.38985 +  */
 1.38986 +  (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
 1.38987 +  return p;
 1.38988 +}
 1.38989 +
 1.38990 +/*
 1.38991 +** Discard the entire contents of the in-memory page-cache.
 1.38992 +*/
 1.38993 +static void pager_reset(Pager *pPager){
 1.38994 +  sqlite3BackupRestart(pPager->pBackup);
 1.38995 +  sqlite3PcacheClear(pPager->pPCache);
 1.38996 +}
 1.38997 +
 1.38998 +/*
 1.38999 +** Free all structures in the Pager.aSavepoint[] array and set both
 1.39000 +** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
 1.39001 +** if it is open and the pager is not in exclusive mode.
 1.39002 +*/
 1.39003 +static void releaseAllSavepoints(Pager *pPager){
 1.39004 +  int ii;               /* Iterator for looping through Pager.aSavepoint */
 1.39005 +  for(ii=0; ii<pPager->nSavepoint; ii++){
 1.39006 +    sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
 1.39007 +  }
 1.39008 +  if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
 1.39009 +    sqlite3OsClose(pPager->sjfd);
 1.39010 +  }
 1.39011 +  sqlite3_free(pPager->aSavepoint);
 1.39012 +  pPager->aSavepoint = 0;
 1.39013 +  pPager->nSavepoint = 0;
 1.39014 +  pPager->nSubRec = 0;
 1.39015 +}
 1.39016 +
 1.39017 +/*
 1.39018 +** Set the bit number pgno in the PagerSavepoint.pInSavepoint 
 1.39019 +** bitvecs of all open savepoints. Return SQLITE_OK if successful
 1.39020 +** or SQLITE_NOMEM if a malloc failure occurs.
 1.39021 +*/
 1.39022 +static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
 1.39023 +  int ii;                   /* Loop counter */
 1.39024 +  int rc = SQLITE_OK;       /* Result code */
 1.39025 +
 1.39026 +  for(ii=0; ii<pPager->nSavepoint; ii++){
 1.39027 +    PagerSavepoint *p = &pPager->aSavepoint[ii];
 1.39028 +    if( pgno<=p->nOrig ){
 1.39029 +      rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
 1.39030 +      testcase( rc==SQLITE_NOMEM );
 1.39031 +      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
 1.39032 +    }
 1.39033 +  }
 1.39034 +  return rc;
 1.39035 +}
 1.39036 +
 1.39037 +/*
 1.39038 +** This function is a no-op if the pager is in exclusive mode and not
 1.39039 +** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
 1.39040 +** state.
 1.39041 +**
 1.39042 +** If the pager is not in exclusive-access mode, the database file is
 1.39043 +** completely unlocked. If the file is unlocked and the file-system does
 1.39044 +** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
 1.39045 +** closed (if it is open).
 1.39046 +**
 1.39047 +** If the pager is in ERROR state when this function is called, the 
 1.39048 +** contents of the pager cache are discarded before switching back to 
 1.39049 +** the OPEN state. Regardless of whether the pager is in exclusive-mode
 1.39050 +** or not, any journal file left in the file-system will be treated
 1.39051 +** as a hot-journal and rolled back the next time a read-transaction
 1.39052 +** is opened (by this or by any other connection).
 1.39053 +*/
 1.39054 +static void pager_unlock(Pager *pPager){
 1.39055 +
 1.39056 +  assert( pPager->eState==PAGER_READER 
 1.39057 +       || pPager->eState==PAGER_OPEN 
 1.39058 +       || pPager->eState==PAGER_ERROR 
 1.39059 +  );
 1.39060 +
 1.39061 +  sqlite3BitvecDestroy(pPager->pInJournal);
 1.39062 +  pPager->pInJournal = 0;
 1.39063 +  releaseAllSavepoints(pPager);
 1.39064 +
 1.39065 +  if( pagerUseWal(pPager) ){
 1.39066 +    assert( !isOpen(pPager->jfd) );
 1.39067 +    sqlite3WalEndReadTransaction(pPager->pWal);
 1.39068 +    pPager->eState = PAGER_OPEN;
 1.39069 +  }else if( !pPager->exclusiveMode ){
 1.39070 +    int rc;                       /* Error code returned by pagerUnlockDb() */
 1.39071 +    int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
 1.39072 +
 1.39073 +    /* If the operating system support deletion of open files, then
 1.39074 +    ** close the journal file when dropping the database lock.  Otherwise
 1.39075 +    ** another connection with journal_mode=delete might delete the file
 1.39076 +    ** out from under us.
 1.39077 +    */
 1.39078 +    assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
 1.39079 +    assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
 1.39080 +    assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
 1.39081 +    assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
 1.39082 +    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
 1.39083 +    assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
 1.39084 +    if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
 1.39085 +     || 1!=(pPager->journalMode & 5)
 1.39086 +    ){
 1.39087 +      sqlite3OsClose(pPager->jfd);
 1.39088 +    }
 1.39089 +
 1.39090 +    /* If the pager is in the ERROR state and the call to unlock the database
 1.39091 +    ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
 1.39092 +    ** above the #define for UNKNOWN_LOCK for an explanation of why this
 1.39093 +    ** is necessary.
 1.39094 +    */
 1.39095 +    rc = pagerUnlockDb(pPager, NO_LOCK);
 1.39096 +    if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
 1.39097 +      pPager->eLock = UNKNOWN_LOCK;
 1.39098 +    }
 1.39099 +
 1.39100 +    /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
 1.39101 +    ** without clearing the error code. This is intentional - the error
 1.39102 +    ** code is cleared and the cache reset in the block below.
 1.39103 +    */
 1.39104 +    assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
 1.39105 +    pPager->changeCountDone = 0;
 1.39106 +    pPager->eState = PAGER_OPEN;
 1.39107 +  }
 1.39108 +
 1.39109 +  /* If Pager.errCode is set, the contents of the pager cache cannot be
 1.39110 +  ** trusted. Now that there are no outstanding references to the pager,
 1.39111 +  ** it can safely move back to PAGER_OPEN state. This happens in both
 1.39112 +  ** normal and exclusive-locking mode.
 1.39113 +  */
 1.39114 +  if( pPager->errCode ){
 1.39115 +    assert( !MEMDB );
 1.39116 +    pager_reset(pPager);
 1.39117 +    pPager->changeCountDone = pPager->tempFile;
 1.39118 +    pPager->eState = PAGER_OPEN;
 1.39119 +    pPager->errCode = SQLITE_OK;
 1.39120 +  }
 1.39121 +
 1.39122 +  pPager->journalOff = 0;
 1.39123 +  pPager->journalHdr = 0;
 1.39124 +  pPager->setMaster = 0;
 1.39125 +}
 1.39126 +
 1.39127 +/*
 1.39128 +** This function is called whenever an IOERR or FULL error that requires
 1.39129 +** the pager to transition into the ERROR state may ahve occurred.
 1.39130 +** The first argument is a pointer to the pager structure, the second 
 1.39131 +** the error-code about to be returned by a pager API function. The 
 1.39132 +** value returned is a copy of the second argument to this function. 
 1.39133 +**
 1.39134 +** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
 1.39135 +** IOERR sub-codes, the pager enters the ERROR state and the error code
 1.39136 +** is stored in Pager.errCode. While the pager remains in the ERROR state,
 1.39137 +** all major API calls on the Pager will immediately return Pager.errCode.
 1.39138 +**
 1.39139 +** The ERROR state indicates that the contents of the pager-cache 
 1.39140 +** cannot be trusted. This state can be cleared by completely discarding 
 1.39141 +** the contents of the pager-cache. If a transaction was active when
 1.39142 +** the persistent error occurred, then the rollback journal may need
 1.39143 +** to be replayed to restore the contents of the database file (as if
 1.39144 +** it were a hot-journal).
 1.39145 +*/
 1.39146 +static int pager_error(Pager *pPager, int rc){
 1.39147 +  int rc2 = rc & 0xff;
 1.39148 +  assert( rc==SQLITE_OK || !MEMDB );
 1.39149 +  assert(
 1.39150 +       pPager->errCode==SQLITE_FULL ||
 1.39151 +       pPager->errCode==SQLITE_OK ||
 1.39152 +       (pPager->errCode & 0xff)==SQLITE_IOERR
 1.39153 +  );
 1.39154 +  if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
 1.39155 +    pPager->errCode = rc;
 1.39156 +    pPager->eState = PAGER_ERROR;
 1.39157 +  }
 1.39158 +  return rc;
 1.39159 +}
 1.39160 +
 1.39161 +/*
 1.39162 +** This routine ends a transaction. A transaction is usually ended by 
 1.39163 +** either a COMMIT or a ROLLBACK operation. This routine may be called 
 1.39164 +** after rollback of a hot-journal, or if an error occurs while opening
 1.39165 +** the journal file or writing the very first journal-header of a
 1.39166 +** database transaction.
 1.39167 +** 
 1.39168 +** This routine is never called in PAGER_ERROR state. If it is called
 1.39169 +** in PAGER_NONE or PAGER_SHARED state and the lock held is less
 1.39170 +** exclusive than a RESERVED lock, it is a no-op.
 1.39171 +**
 1.39172 +** Otherwise, any active savepoints are released.
 1.39173 +**
 1.39174 +** If the journal file is open, then it is "finalized". Once a journal 
 1.39175 +** file has been finalized it is not possible to use it to roll back a 
 1.39176 +** transaction. Nor will it be considered to be a hot-journal by this
 1.39177 +** or any other database connection. Exactly how a journal is finalized
 1.39178 +** depends on whether or not the pager is running in exclusive mode and
 1.39179 +** the current journal-mode (Pager.journalMode value), as follows:
 1.39180 +**
 1.39181 +**   journalMode==MEMORY
 1.39182 +**     Journal file descriptor is simply closed. This destroys an 
 1.39183 +**     in-memory journal.
 1.39184 +**
 1.39185 +**   journalMode==TRUNCATE
 1.39186 +**     Journal file is truncated to zero bytes in size.
 1.39187 +**
 1.39188 +**   journalMode==PERSIST
 1.39189 +**     The first 28 bytes of the journal file are zeroed. This invalidates
 1.39190 +**     the first journal header in the file, and hence the entire journal
 1.39191 +**     file. An invalid journal file cannot be rolled back.
 1.39192 +**
 1.39193 +**   journalMode==DELETE
 1.39194 +**     The journal file is closed and deleted using sqlite3OsDelete().
 1.39195 +**
 1.39196 +**     If the pager is running in exclusive mode, this method of finalizing
 1.39197 +**     the journal file is never used. Instead, if the journalMode is
 1.39198 +**     DELETE and the pager is in exclusive mode, the method described under
 1.39199 +**     journalMode==PERSIST is used instead.
 1.39200 +**
 1.39201 +** After the journal is finalized, the pager moves to PAGER_READER state.
 1.39202 +** If running in non-exclusive rollback mode, the lock on the file is 
 1.39203 +** downgraded to a SHARED_LOCK.
 1.39204 +**
 1.39205 +** SQLITE_OK is returned if no error occurs. If an error occurs during
 1.39206 +** any of the IO operations to finalize the journal file or unlock the
 1.39207 +** database then the IO error code is returned to the user. If the 
 1.39208 +** operation to finalize the journal file fails, then the code still
 1.39209 +** tries to unlock the database file if not in exclusive mode. If the
 1.39210 +** unlock operation fails as well, then the first error code related
 1.39211 +** to the first error encountered (the journal finalization one) is
 1.39212 +** returned.
 1.39213 +*/
 1.39214 +static int pager_end_transaction(Pager *pPager, int hasMaster){
 1.39215 +  int rc = SQLITE_OK;      /* Error code from journal finalization operation */
 1.39216 +  int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
 1.39217 +
 1.39218 +  /* Do nothing if the pager does not have an open write transaction
 1.39219 +  ** or at least a RESERVED lock. This function may be called when there
 1.39220 +  ** is no write-transaction active but a RESERVED or greater lock is
 1.39221 +  ** held under two circumstances:
 1.39222 +  **
 1.39223 +  **   1. After a successful hot-journal rollback, it is called with
 1.39224 +  **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
 1.39225 +  **
 1.39226 +  **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE 
 1.39227 +  **      lock switches back to locking_mode=normal and then executes a
 1.39228 +  **      read-transaction, this function is called with eState==PAGER_READER 
 1.39229 +  **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
 1.39230 +  */
 1.39231 +  assert( assert_pager_state(pPager) );
 1.39232 +  assert( pPager->eState!=PAGER_ERROR );
 1.39233 +  if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
 1.39234 +    return SQLITE_OK;
 1.39235 +  }
 1.39236 +
 1.39237 +  releaseAllSavepoints(pPager);
 1.39238 +  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
 1.39239 +  if( isOpen(pPager->jfd) ){
 1.39240 +    assert( !pagerUseWal(pPager) );
 1.39241 +
 1.39242 +    /* Finalize the journal file. */
 1.39243 +    if( sqlite3IsMemJournal(pPager->jfd) ){
 1.39244 +      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
 1.39245 +      sqlite3OsClose(pPager->jfd);
 1.39246 +    }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
 1.39247 +      if( pPager->journalOff==0 ){
 1.39248 +        rc = SQLITE_OK;
 1.39249 +      }else{
 1.39250 +        rc = sqlite3OsTruncate(pPager->jfd, 0);
 1.39251 +      }
 1.39252 +      pPager->journalOff = 0;
 1.39253 +    }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
 1.39254 +      || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
 1.39255 +    ){
 1.39256 +      rc = zeroJournalHdr(pPager, hasMaster);
 1.39257 +      pPager->journalOff = 0;
 1.39258 +    }else{
 1.39259 +      /* This branch may be executed with Pager.journalMode==MEMORY if
 1.39260 +      ** a hot-journal was just rolled back. In this case the journal
 1.39261 +      ** file should be closed and deleted. If this connection writes to
 1.39262 +      ** the database file, it will do so using an in-memory journal. 
 1.39263 +      */
 1.39264 +      int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
 1.39265 +      assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
 1.39266 +           || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
 1.39267 +           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
 1.39268 +      );
 1.39269 +      sqlite3OsClose(pPager->jfd);
 1.39270 +      if( bDelete ){
 1.39271 +        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 1.39272 +      }
 1.39273 +    }
 1.39274 +  }
 1.39275 +
 1.39276 +#ifdef SQLITE_CHECK_PAGES
 1.39277 +  sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
 1.39278 +  if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
 1.39279 +    PgHdr *p = pager_lookup(pPager, 1);
 1.39280 +    if( p ){
 1.39281 +      p->pageHash = 0;
 1.39282 +      sqlite3PagerUnref(p);
 1.39283 +    }
 1.39284 +  }
 1.39285 +#endif
 1.39286 +
 1.39287 +  sqlite3BitvecDestroy(pPager->pInJournal);
 1.39288 +  pPager->pInJournal = 0;
 1.39289 +  pPager->nRec = 0;
 1.39290 +  sqlite3PcacheCleanAll(pPager->pPCache);
 1.39291 +  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
 1.39292 +
 1.39293 +  if( pagerUseWal(pPager) ){
 1.39294 +    /* Drop the WAL write-lock, if any. Also, if the connection was in 
 1.39295 +    ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 
 1.39296 +    ** lock held on the database file.
 1.39297 +    */
 1.39298 +    rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
 1.39299 +    assert( rc2==SQLITE_OK );
 1.39300 +  }
 1.39301 +  if( !pPager->exclusiveMode 
 1.39302 +   && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
 1.39303 +  ){
 1.39304 +    rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
 1.39305 +    pPager->changeCountDone = 0;
 1.39306 +  }
 1.39307 +  pPager->eState = PAGER_READER;
 1.39308 +  pPager->setMaster = 0;
 1.39309 +
 1.39310 +  return (rc==SQLITE_OK?rc2:rc);
 1.39311 +}
 1.39312 +
 1.39313 +/*
 1.39314 +** Execute a rollback if a transaction is active and unlock the 
 1.39315 +** database file. 
 1.39316 +**
 1.39317 +** If the pager has already entered the ERROR state, do not attempt 
 1.39318 +** the rollback at this time. Instead, pager_unlock() is called. The
 1.39319 +** call to pager_unlock() will discard all in-memory pages, unlock
 1.39320 +** the database file and move the pager back to OPEN state. If this 
 1.39321 +** means that there is a hot-journal left in the file-system, the next 
 1.39322 +** connection to obtain a shared lock on the pager (which may be this one) 
 1.39323 +** will roll it back.
 1.39324 +**
 1.39325 +** If the pager has not already entered the ERROR state, but an IO or
 1.39326 +** malloc error occurs during a rollback, then this will itself cause 
 1.39327 +** the pager to enter the ERROR state. Which will be cleared by the
 1.39328 +** call to pager_unlock(), as described above.
 1.39329 +*/
 1.39330 +static void pagerUnlockAndRollback(Pager *pPager){
 1.39331 +  if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
 1.39332 +    assert( assert_pager_state(pPager) );
 1.39333 +    if( pPager->eState>=PAGER_WRITER_LOCKED ){
 1.39334 +      sqlite3BeginBenignMalloc();
 1.39335 +      sqlite3PagerRollback(pPager);
 1.39336 +      sqlite3EndBenignMalloc();
 1.39337 +    }else if( !pPager->exclusiveMode ){
 1.39338 +      assert( pPager->eState==PAGER_READER );
 1.39339 +      pager_end_transaction(pPager, 0);
 1.39340 +    }
 1.39341 +  }
 1.39342 +  pager_unlock(pPager);
 1.39343 +}
 1.39344 +
 1.39345 +/*
 1.39346 +** Parameter aData must point to a buffer of pPager->pageSize bytes
 1.39347 +** of data. Compute and return a checksum based ont the contents of the 
 1.39348 +** page of data and the current value of pPager->cksumInit.
 1.39349 +**
 1.39350 +** This is not a real checksum. It is really just the sum of the 
 1.39351 +** random initial value (pPager->cksumInit) and every 200th byte
 1.39352 +** of the page data, starting with byte offset (pPager->pageSize%200).
 1.39353 +** Each byte is interpreted as an 8-bit unsigned integer.
 1.39354 +**
 1.39355 +** Changing the formula used to compute this checksum results in an
 1.39356 +** incompatible journal file format.
 1.39357 +**
 1.39358 +** If journal corruption occurs due to a power failure, the most likely 
 1.39359 +** scenario is that one end or the other of the record will be changed. 
 1.39360 +** It is much less likely that the two ends of the journal record will be
 1.39361 +** correct and the middle be corrupt.  Thus, this "checksum" scheme,
 1.39362 +** though fast and simple, catches the mostly likely kind of corruption.
 1.39363 +*/
 1.39364 +static u32 pager_cksum(Pager *pPager, const u8 *aData){
 1.39365 +  u32 cksum = pPager->cksumInit;         /* Checksum value to return */
 1.39366 +  int i = pPager->pageSize-200;          /* Loop counter */
 1.39367 +  while( i>0 ){
 1.39368 +    cksum += aData[i];
 1.39369 +    i -= 200;
 1.39370 +  }
 1.39371 +  return cksum;
 1.39372 +}
 1.39373 +
 1.39374 +/*
 1.39375 +** Report the current page size and number of reserved bytes back
 1.39376 +** to the codec.
 1.39377 +*/
 1.39378 +#ifdef SQLITE_HAS_CODEC
 1.39379 +static void pagerReportSize(Pager *pPager){
 1.39380 +  if( pPager->xCodecSizeChng ){
 1.39381 +    pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
 1.39382 +                           (int)pPager->nReserve);
 1.39383 +  }
 1.39384 +}
 1.39385 +#else
 1.39386 +# define pagerReportSize(X)     /* No-op if we do not support a codec */
 1.39387 +#endif
 1.39388 +
 1.39389 +/*
 1.39390 +** Read a single page from either the journal file (if isMainJrnl==1) or
 1.39391 +** from the sub-journal (if isMainJrnl==0) and playback that page.
 1.39392 +** The page begins at offset *pOffset into the file. The *pOffset
 1.39393 +** value is increased to the start of the next page in the journal.
 1.39394 +**
 1.39395 +** The main rollback journal uses checksums - the statement journal does 
 1.39396 +** not.
 1.39397 +**
 1.39398 +** If the page number of the page record read from the (sub-)journal file
 1.39399 +** is greater than the current value of Pager.dbSize, then playback is
 1.39400 +** skipped and SQLITE_OK is returned.
 1.39401 +**
 1.39402 +** If pDone is not NULL, then it is a record of pages that have already
 1.39403 +** been played back.  If the page at *pOffset has already been played back
 1.39404 +** (if the corresponding pDone bit is set) then skip the playback.
 1.39405 +** Make sure the pDone bit corresponding to the *pOffset page is set
 1.39406 +** prior to returning.
 1.39407 +**
 1.39408 +** If the page record is successfully read from the (sub-)journal file
 1.39409 +** and played back, then SQLITE_OK is returned. If an IO error occurs
 1.39410 +** while reading the record from the (sub-)journal file or while writing
 1.39411 +** to the database file, then the IO error code is returned. If data
 1.39412 +** is successfully read from the (sub-)journal file but appears to be
 1.39413 +** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
 1.39414 +** two circumstances:
 1.39415 +** 
 1.39416 +**   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
 1.39417 +**   * If the record is being rolled back from the main journal file
 1.39418 +**     and the checksum field does not match the record content.
 1.39419 +**
 1.39420 +** Neither of these two scenarios are possible during a savepoint rollback.
 1.39421 +**
 1.39422 +** If this is a savepoint rollback, then memory may have to be dynamically
 1.39423 +** allocated by this function. If this is the case and an allocation fails,
 1.39424 +** SQLITE_NOMEM is returned.
 1.39425 +*/
 1.39426 +static int pager_playback_one_page(
 1.39427 +  Pager *pPager,                /* The pager being played back */
 1.39428 +  i64 *pOffset,                 /* Offset of record to playback */
 1.39429 +  Bitvec *pDone,                /* Bitvec of pages already played back */
 1.39430 +  int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
 1.39431 +  int isSavepnt                 /* True for a savepoint rollback */
 1.39432 +){
 1.39433 +  int rc;
 1.39434 +  PgHdr *pPg;                   /* An existing page in the cache */
 1.39435 +  Pgno pgno;                    /* The page number of a page in journal */
 1.39436 +  u32 cksum;                    /* Checksum used for sanity checking */
 1.39437 +  char *aData;                  /* Temporary storage for the page */
 1.39438 +  sqlite3_file *jfd;            /* The file descriptor for the journal file */
 1.39439 +  int isSynced;                 /* True if journal page is synced */
 1.39440 +
 1.39441 +  assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
 1.39442 +  assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
 1.39443 +  assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
 1.39444 +  assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
 1.39445 +
 1.39446 +  aData = pPager->pTmpSpace;
 1.39447 +  assert( aData );         /* Temp storage must have already been allocated */
 1.39448 +  assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
 1.39449 +
 1.39450 +  /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction 
 1.39451 +  ** or savepoint rollback done at the request of the caller) or this is
 1.39452 +  ** a hot-journal rollback. If it is a hot-journal rollback, the pager
 1.39453 +  ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
 1.39454 +  ** only reads from the main journal, not the sub-journal.
 1.39455 +  */
 1.39456 +  assert( pPager->eState>=PAGER_WRITER_CACHEMOD
 1.39457 +       || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
 1.39458 +  );
 1.39459 +  assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
 1.39460 +
 1.39461 +  /* Read the page number and page data from the journal or sub-journal
 1.39462 +  ** file. Return an error code to the caller if an IO error occurs.
 1.39463 +  */
 1.39464 +  jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
 1.39465 +  rc = read32bits(jfd, *pOffset, &pgno);
 1.39466 +  if( rc!=SQLITE_OK ) return rc;
 1.39467 +  rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
 1.39468 +  if( rc!=SQLITE_OK ) return rc;
 1.39469 +  *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
 1.39470 +
 1.39471 +  /* Sanity checking on the page.  This is more important that I originally
 1.39472 +  ** thought.  If a power failure occurs while the journal is being written,
 1.39473 +  ** it could cause invalid data to be written into the journal.  We need to
 1.39474 +  ** detect this invalid data (with high probability) and ignore it.
 1.39475 +  */
 1.39476 +  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
 1.39477 +    assert( !isSavepnt );
 1.39478 +    return SQLITE_DONE;
 1.39479 +  }
 1.39480 +  if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
 1.39481 +    return SQLITE_OK;
 1.39482 +  }
 1.39483 +  if( isMainJrnl ){
 1.39484 +    rc = read32bits(jfd, (*pOffset)-4, &cksum);
 1.39485 +    if( rc ) return rc;
 1.39486 +    if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
 1.39487 +      return SQLITE_DONE;
 1.39488 +    }
 1.39489 +  }
 1.39490 +
 1.39491 +  /* If this page has already been played by before during the current
 1.39492 +  ** rollback, then don't bother to play it back again.
 1.39493 +  */
 1.39494 +  if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
 1.39495 +    return rc;
 1.39496 +  }
 1.39497 +
 1.39498 +  /* When playing back page 1, restore the nReserve setting
 1.39499 +  */
 1.39500 +  if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
 1.39501 +    pPager->nReserve = ((u8*)aData)[20];
 1.39502 +    pagerReportSize(pPager);
 1.39503 +  }
 1.39504 +
 1.39505 +  /* If the pager is in CACHEMOD state, then there must be a copy of this
 1.39506 +  ** page in the pager cache. In this case just update the pager cache,
 1.39507 +  ** not the database file. The page is left marked dirty in this case.
 1.39508 +  **
 1.39509 +  ** An exception to the above rule: If the database is in no-sync mode
 1.39510 +  ** and a page is moved during an incremental vacuum then the page may
 1.39511 +  ** not be in the pager cache. Later: if a malloc() or IO error occurs
 1.39512 +  ** during a Movepage() call, then the page may not be in the cache
 1.39513 +  ** either. So the condition described in the above paragraph is not
 1.39514 +  ** assert()able.
 1.39515 +  **
 1.39516 +  ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
 1.39517 +  ** pager cache if it exists and the main file. The page is then marked 
 1.39518 +  ** not dirty. Since this code is only executed in PAGER_OPEN state for
 1.39519 +  ** a hot-journal rollback, it is guaranteed that the page-cache is empty
 1.39520 +  ** if the pager is in OPEN state.
 1.39521 +  **
 1.39522 +  ** Ticket #1171:  The statement journal might contain page content that is
 1.39523 +  ** different from the page content at the start of the transaction.
 1.39524 +  ** This occurs when a page is changed prior to the start of a statement
 1.39525 +  ** then changed again within the statement.  When rolling back such a
 1.39526 +  ** statement we must not write to the original database unless we know
 1.39527 +  ** for certain that original page contents are synced into the main rollback
 1.39528 +  ** journal.  Otherwise, a power loss might leave modified data in the
 1.39529 +  ** database file without an entry in the rollback journal that can
 1.39530 +  ** restore the database to its original form.  Two conditions must be
 1.39531 +  ** met before writing to the database files. (1) the database must be
 1.39532 +  ** locked.  (2) we know that the original page content is fully synced
 1.39533 +  ** in the main journal either because the page is not in cache or else
 1.39534 +  ** the page is marked as needSync==0.
 1.39535 +  **
 1.39536 +  ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
 1.39537 +  ** is possible to fail a statement on a database that does not yet exist.
 1.39538 +  ** Do not attempt to write if database file has never been opened.
 1.39539 +  */
 1.39540 +  if( pagerUseWal(pPager) ){
 1.39541 +    pPg = 0;
 1.39542 +  }else{
 1.39543 +    pPg = pager_lookup(pPager, pgno);
 1.39544 +  }
 1.39545 +  assert( pPg || !MEMDB );
 1.39546 +  assert( pPager->eState!=PAGER_OPEN || pPg==0 );
 1.39547 +  PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
 1.39548 +           PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
 1.39549 +           (isMainJrnl?"main-journal":"sub-journal")
 1.39550 +  ));
 1.39551 +  if( isMainJrnl ){
 1.39552 +    isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
 1.39553 +  }else{
 1.39554 +    isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
 1.39555 +  }
 1.39556 +  if( isOpen(pPager->fd)
 1.39557 +   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
 1.39558 +   && isSynced
 1.39559 +  ){
 1.39560 +    i64 ofst = (pgno-1)*(i64)pPager->pageSize;
 1.39561 +    testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
 1.39562 +    assert( !pagerUseWal(pPager) );
 1.39563 +    rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
 1.39564 +    if( pgno>pPager->dbFileSize ){
 1.39565 +      pPager->dbFileSize = pgno;
 1.39566 +    }
 1.39567 +    if( pPager->pBackup ){
 1.39568 +      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
 1.39569 +      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
 1.39570 +      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
 1.39571 +    }
 1.39572 +  }else if( !isMainJrnl && pPg==0 ){
 1.39573 +    /* If this is a rollback of a savepoint and data was not written to
 1.39574 +    ** the database and the page is not in-memory, there is a potential
 1.39575 +    ** problem. When the page is next fetched by the b-tree layer, it 
 1.39576 +    ** will be read from the database file, which may or may not be 
 1.39577 +    ** current. 
 1.39578 +    **
 1.39579 +    ** There are a couple of different ways this can happen. All are quite
 1.39580 +    ** obscure. When running in synchronous mode, this can only happen 
 1.39581 +    ** if the page is on the free-list at the start of the transaction, then
 1.39582 +    ** populated, then moved using sqlite3PagerMovepage().
 1.39583 +    **
 1.39584 +    ** The solution is to add an in-memory page to the cache containing
 1.39585 +    ** the data just read from the sub-journal. Mark the page as dirty 
 1.39586 +    ** and if the pager requires a journal-sync, then mark the page as 
 1.39587 +    ** requiring a journal-sync before it is written.
 1.39588 +    */
 1.39589 +    assert( isSavepnt );
 1.39590 +    assert( pPager->doNotSpill==0 );
 1.39591 +    pPager->doNotSpill++;
 1.39592 +    rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
 1.39593 +    assert( pPager->doNotSpill==1 );
 1.39594 +    pPager->doNotSpill--;
 1.39595 +    if( rc!=SQLITE_OK ) return rc;
 1.39596 +    pPg->flags &= ~PGHDR_NEED_READ;
 1.39597 +    sqlite3PcacheMakeDirty(pPg);
 1.39598 +  }
 1.39599 +  if( pPg ){
 1.39600 +    /* No page should ever be explicitly rolled back that is in use, except
 1.39601 +    ** for page 1 which is held in use in order to keep the lock on the
 1.39602 +    ** database active. However such a page may be rolled back as a result
 1.39603 +    ** of an internal error resulting in an automatic call to
 1.39604 +    ** sqlite3PagerRollback().
 1.39605 +    */
 1.39606 +    void *pData;
 1.39607 +    pData = pPg->pData;
 1.39608 +    memcpy(pData, (u8*)aData, pPager->pageSize);
 1.39609 +    pPager->xReiniter(pPg);
 1.39610 +    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
 1.39611 +      /* If the contents of this page were just restored from the main 
 1.39612 +      ** journal file, then its content must be as they were when the 
 1.39613 +      ** transaction was first opened. In this case we can mark the page
 1.39614 +      ** as clean, since there will be no need to write it out to the
 1.39615 +      ** database.
 1.39616 +      **
 1.39617 +      ** There is one exception to this rule. If the page is being rolled
 1.39618 +      ** back as part of a savepoint (or statement) rollback from an 
 1.39619 +      ** unsynced portion of the main journal file, then it is not safe
 1.39620 +      ** to mark the page as clean. This is because marking the page as
 1.39621 +      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
 1.39622 +      ** already in the journal file (recorded in Pager.pInJournal) and
 1.39623 +      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
 1.39624 +      ** again within this transaction, it will be marked as dirty but
 1.39625 +      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
 1.39626 +      ** be written out into the database file before its journal file
 1.39627 +      ** segment is synced. If a crash occurs during or following this,
 1.39628 +      ** database corruption may ensue.
 1.39629 +      */
 1.39630 +      assert( !pagerUseWal(pPager) );
 1.39631 +      sqlite3PcacheMakeClean(pPg);
 1.39632 +    }
 1.39633 +    pager_set_pagehash(pPg);
 1.39634 +
 1.39635 +    /* If this was page 1, then restore the value of Pager.dbFileVers.
 1.39636 +    ** Do this before any decoding. */
 1.39637 +    if( pgno==1 ){
 1.39638 +      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
 1.39639 +    }
 1.39640 +
 1.39641 +    /* Decode the page just read from disk */
 1.39642 +    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
 1.39643 +    sqlite3PcacheRelease(pPg);
 1.39644 +  }
 1.39645 +  return rc;
 1.39646 +}
 1.39647 +
 1.39648 +/*
 1.39649 +** Parameter zMaster is the name of a master journal file. A single journal
 1.39650 +** file that referred to the master journal file has just been rolled back.
 1.39651 +** This routine checks if it is possible to delete the master journal file,
 1.39652 +** and does so if it is.
 1.39653 +**
 1.39654 +** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
 1.39655 +** available for use within this function.
 1.39656 +**
 1.39657 +** When a master journal file is created, it is populated with the names 
 1.39658 +** of all of its child journals, one after another, formatted as utf-8 
 1.39659 +** encoded text. The end of each child journal file is marked with a 
 1.39660 +** nul-terminator byte (0x00). i.e. the entire contents of a master journal
 1.39661 +** file for a transaction involving two databases might be:
 1.39662 +**
 1.39663 +**   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
 1.39664 +**
 1.39665 +** A master journal file may only be deleted once all of its child 
 1.39666 +** journals have been rolled back.
 1.39667 +**
 1.39668 +** This function reads the contents of the master-journal file into 
 1.39669 +** memory and loops through each of the child journal names. For
 1.39670 +** each child journal, it checks if:
 1.39671 +**
 1.39672 +**   * if the child journal exists, and if so
 1.39673 +**   * if the child journal contains a reference to master journal 
 1.39674 +**     file zMaster
 1.39675 +**
 1.39676 +** If a child journal can be found that matches both of the criteria
 1.39677 +** above, this function returns without doing anything. Otherwise, if
 1.39678 +** no such child journal can be found, file zMaster is deleted from
 1.39679 +** the file-system using sqlite3OsDelete().
 1.39680 +**
 1.39681 +** If an IO error within this function, an error code is returned. This
 1.39682 +** function allocates memory by calling sqlite3Malloc(). If an allocation
 1.39683 +** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors 
 1.39684 +** occur, SQLITE_OK is returned.
 1.39685 +**
 1.39686 +** TODO: This function allocates a single block of memory to load
 1.39687 +** the entire contents of the master journal file. This could be
 1.39688 +** a couple of kilobytes or so - potentially larger than the page 
 1.39689 +** size.
 1.39690 +*/
 1.39691 +static int pager_delmaster(Pager *pPager, const char *zMaster){
 1.39692 +  sqlite3_vfs *pVfs = pPager->pVfs;
 1.39693 +  int rc;                   /* Return code */
 1.39694 +  sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
 1.39695 +  sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
 1.39696 +  char *zMasterJournal = 0; /* Contents of master journal file */
 1.39697 +  i64 nMasterJournal;       /* Size of master journal file */
 1.39698 +  char *zJournal;           /* Pointer to one journal within MJ file */
 1.39699 +  char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
 1.39700 +  int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
 1.39701 +
 1.39702 +  /* Allocate space for both the pJournal and pMaster file descriptors.
 1.39703 +  ** If successful, open the master journal file for reading.
 1.39704 +  */
 1.39705 +  pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
 1.39706 +  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
 1.39707 +  if( !pMaster ){
 1.39708 +    rc = SQLITE_NOMEM;
 1.39709 +  }else{
 1.39710 +    const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
 1.39711 +    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
 1.39712 +  }
 1.39713 +  if( rc!=SQLITE_OK ) goto delmaster_out;
 1.39714 +
 1.39715 +  /* Load the entire master journal file into space obtained from
 1.39716 +  ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
 1.39717 +  ** sufficient space (in zMasterPtr) to hold the names of master
 1.39718 +  ** journal files extracted from regular rollback-journals.
 1.39719 +  */
 1.39720 +  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
 1.39721 +  if( rc!=SQLITE_OK ) goto delmaster_out;
 1.39722 +  nMasterPtr = pVfs->mxPathname+1;
 1.39723 +  zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
 1.39724 +  if( !zMasterJournal ){
 1.39725 +    rc = SQLITE_NOMEM;
 1.39726 +    goto delmaster_out;
 1.39727 +  }
 1.39728 +  zMasterPtr = &zMasterJournal[nMasterJournal+1];
 1.39729 +  rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
 1.39730 +  if( rc!=SQLITE_OK ) goto delmaster_out;
 1.39731 +  zMasterJournal[nMasterJournal] = 0;
 1.39732 +
 1.39733 +  zJournal = zMasterJournal;
 1.39734 +  while( (zJournal-zMasterJournal)<nMasterJournal ){
 1.39735 +    int exists;
 1.39736 +    rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
 1.39737 +    if( rc!=SQLITE_OK ){
 1.39738 +      goto delmaster_out;
 1.39739 +    }
 1.39740 +    if( exists ){
 1.39741 +      /* One of the journals pointed to by the master journal exists.
 1.39742 +      ** Open it and check if it points at the master journal. If
 1.39743 +      ** so, return without deleting the master journal file.
 1.39744 +      */
 1.39745 +      int c;
 1.39746 +      int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
 1.39747 +      rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
 1.39748 +      if( rc!=SQLITE_OK ){
 1.39749 +        goto delmaster_out;
 1.39750 +      }
 1.39751 +
 1.39752 +      rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
 1.39753 +      sqlite3OsClose(pJournal);
 1.39754 +      if( rc!=SQLITE_OK ){
 1.39755 +        goto delmaster_out;
 1.39756 +      }
 1.39757 +
 1.39758 +      c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
 1.39759 +      if( c ){
 1.39760 +        /* We have a match. Do not delete the master journal file. */
 1.39761 +        goto delmaster_out;
 1.39762 +      }
 1.39763 +    }
 1.39764 +    zJournal += (sqlite3Strlen30(zJournal)+1);
 1.39765 +  }
 1.39766 + 
 1.39767 +  sqlite3OsClose(pMaster);
 1.39768 +  rc = sqlite3OsDelete(pVfs, zMaster, 0);
 1.39769 +
 1.39770 +delmaster_out:
 1.39771 +  sqlite3_free(zMasterJournal);
 1.39772 +  if( pMaster ){
 1.39773 +    sqlite3OsClose(pMaster);
 1.39774 +    assert( !isOpen(pJournal) );
 1.39775 +    sqlite3_free(pMaster);
 1.39776 +  }
 1.39777 +  return rc;
 1.39778 +}
 1.39779 +
 1.39780 +
 1.39781 +/*
 1.39782 +** This function is used to change the actual size of the database 
 1.39783 +** file in the file-system. This only happens when committing a transaction,
 1.39784 +** or rolling back a transaction (including rolling back a hot-journal).
 1.39785 +**
 1.39786 +** If the main database file is not open, or the pager is not in either
 1.39787 +** DBMOD or OPEN state, this function is a no-op. Otherwise, the size 
 1.39788 +** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). 
 1.39789 +** If the file on disk is currently larger than nPage pages, then use the VFS
 1.39790 +** xTruncate() method to truncate it.
 1.39791 +**
 1.39792 +** Or, it might might be the case that the file on disk is smaller than 
 1.39793 +** nPage pages. Some operating system implementations can get confused if 
 1.39794 +** you try to truncate a file to some size that is larger than it 
 1.39795 +** currently is, so detect this case and write a single zero byte to 
 1.39796 +** the end of the new file instead.
 1.39797 +**
 1.39798 +** If successful, return SQLITE_OK. If an IO error occurs while modifying
 1.39799 +** the database file, return the error code to the caller.
 1.39800 +*/
 1.39801 +static int pager_truncate(Pager *pPager, Pgno nPage){
 1.39802 +  int rc = SQLITE_OK;
 1.39803 +  assert( pPager->eState!=PAGER_ERROR );
 1.39804 +  assert( pPager->eState!=PAGER_READER );
 1.39805 +  
 1.39806 +  if( isOpen(pPager->fd) 
 1.39807 +   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 
 1.39808 +  ){
 1.39809 +    i64 currentSize, newSize;
 1.39810 +    int szPage = pPager->pageSize;
 1.39811 +    assert( pPager->eLock==EXCLUSIVE_LOCK );
 1.39812 +    /* TODO: Is it safe to use Pager.dbFileSize here? */
 1.39813 +    rc = sqlite3OsFileSize(pPager->fd, &currentSize);
 1.39814 +    newSize = szPage*(i64)nPage;
 1.39815 +    if( rc==SQLITE_OK && currentSize!=newSize ){
 1.39816 +      if( currentSize>newSize ){
 1.39817 +        rc = sqlite3OsTruncate(pPager->fd, newSize);
 1.39818 +      }else if( (currentSize+szPage)<=newSize ){
 1.39819 +        char *pTmp = pPager->pTmpSpace;
 1.39820 +        memset(pTmp, 0, szPage);
 1.39821 +        testcase( (newSize-szPage) == currentSize );
 1.39822 +        testcase( (newSize-szPage) >  currentSize );
 1.39823 +        rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
 1.39824 +      }
 1.39825 +      if( rc==SQLITE_OK ){
 1.39826 +        pPager->dbFileSize = nPage;
 1.39827 +      }
 1.39828 +    }
 1.39829 +  }
 1.39830 +  return rc;
 1.39831 +}
 1.39832 +
 1.39833 +/*
 1.39834 +** Return a sanitized version of the sector-size of OS file pFile. The
 1.39835 +** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
 1.39836 +*/
 1.39837 +SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
 1.39838 +  int iRet = sqlite3OsSectorSize(pFile);
 1.39839 +  if( iRet<32 ){
 1.39840 +    iRet = 512;
 1.39841 +  }else if( iRet>MAX_SECTOR_SIZE ){
 1.39842 +    assert( MAX_SECTOR_SIZE>=512 );
 1.39843 +    iRet = MAX_SECTOR_SIZE;
 1.39844 +  }
 1.39845 +  return iRet;
 1.39846 +}
 1.39847 +
 1.39848 +/*
 1.39849 +** Set the value of the Pager.sectorSize variable for the given
 1.39850 +** pager based on the value returned by the xSectorSize method
 1.39851 +** of the open database file. The sector size will be used used 
 1.39852 +** to determine the size and alignment of journal header and 
 1.39853 +** master journal pointers within created journal files.
 1.39854 +**
 1.39855 +** For temporary files the effective sector size is always 512 bytes.
 1.39856 +**
 1.39857 +** Otherwise, for non-temporary files, the effective sector size is
 1.39858 +** the value returned by the xSectorSize() method rounded up to 32 if
 1.39859 +** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
 1.39860 +** is greater than MAX_SECTOR_SIZE.
 1.39861 +**
 1.39862 +** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
 1.39863 +** the effective sector size to its minimum value (512).  The purpose of
 1.39864 +** pPager->sectorSize is to define the "blast radius" of bytes that
 1.39865 +** might change if a crash occurs while writing to a single byte in
 1.39866 +** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
 1.39867 +** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
 1.39868 +** size.  For backwards compatibility of the rollback journal file format,
 1.39869 +** we cannot reduce the effective sector size below 512.
 1.39870 +*/
 1.39871 +static void setSectorSize(Pager *pPager){
 1.39872 +  assert( isOpen(pPager->fd) || pPager->tempFile );
 1.39873 +
 1.39874 +  if( pPager->tempFile
 1.39875 +   || (sqlite3OsDeviceCharacteristics(pPager->fd) & 
 1.39876 +              SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
 1.39877 +  ){
 1.39878 +    /* Sector size doesn't matter for temporary files. Also, the file
 1.39879 +    ** may not have been opened yet, in which case the OsSectorSize()
 1.39880 +    ** call will segfault. */
 1.39881 +    pPager->sectorSize = 512;
 1.39882 +  }else{
 1.39883 +    pPager->sectorSize = sqlite3SectorSize(pPager->fd);
 1.39884 +  }
 1.39885 +}
 1.39886 +
 1.39887 +/*
 1.39888 +** Playback the journal and thus restore the database file to
 1.39889 +** the state it was in before we started making changes.  
 1.39890 +**
 1.39891 +** The journal file format is as follows: 
 1.39892 +**
 1.39893 +**  (1)  8 byte prefix.  A copy of aJournalMagic[].
 1.39894 +**  (2)  4 byte big-endian integer which is the number of valid page records
 1.39895 +**       in the journal.  If this value is 0xffffffff, then compute the
 1.39896 +**       number of page records from the journal size.
 1.39897 +**  (3)  4 byte big-endian integer which is the initial value for the 
 1.39898 +**       sanity checksum.
 1.39899 +**  (4)  4 byte integer which is the number of pages to truncate the
 1.39900 +**       database to during a rollback.
 1.39901 +**  (5)  4 byte big-endian integer which is the sector size.  The header
 1.39902 +**       is this many bytes in size.
 1.39903 +**  (6)  4 byte big-endian integer which is the page size.
 1.39904 +**  (7)  zero padding out to the next sector size.
 1.39905 +**  (8)  Zero or more pages instances, each as follows:
 1.39906 +**        +  4 byte page number.
 1.39907 +**        +  pPager->pageSize bytes of data.
 1.39908 +**        +  4 byte checksum
 1.39909 +**
 1.39910 +** When we speak of the journal header, we mean the first 7 items above.
 1.39911 +** Each entry in the journal is an instance of the 8th item.
 1.39912 +**
 1.39913 +** Call the value from the second bullet "nRec".  nRec is the number of
 1.39914 +** valid page entries in the journal.  In most cases, you can compute the
 1.39915 +** value of nRec from the size of the journal file.  But if a power
 1.39916 +** failure occurred while the journal was being written, it could be the
 1.39917 +** case that the size of the journal file had already been increased but
 1.39918 +** the extra entries had not yet made it safely to disk.  In such a case,
 1.39919 +** the value of nRec computed from the file size would be too large.  For
 1.39920 +** that reason, we always use the nRec value in the header.
 1.39921 +**
 1.39922 +** If the nRec value is 0xffffffff it means that nRec should be computed
 1.39923 +** from the file size.  This value is used when the user selects the
 1.39924 +** no-sync option for the journal.  A power failure could lead to corruption
 1.39925 +** in this case.  But for things like temporary table (which will be
 1.39926 +** deleted when the power is restored) we don't care.  
 1.39927 +**
 1.39928 +** If the file opened as the journal file is not a well-formed
 1.39929 +** journal file then all pages up to the first corrupted page are rolled
 1.39930 +** back (or no pages if the journal header is corrupted). The journal file
 1.39931 +** is then deleted and SQLITE_OK returned, just as if no corruption had
 1.39932 +** been encountered.
 1.39933 +**
 1.39934 +** If an I/O or malloc() error occurs, the journal-file is not deleted
 1.39935 +** and an error code is returned.
 1.39936 +**
 1.39937 +** The isHot parameter indicates that we are trying to rollback a journal
 1.39938 +** that might be a hot journal.  Or, it could be that the journal is 
 1.39939 +** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
 1.39940 +** If the journal really is hot, reset the pager cache prior rolling
 1.39941 +** back any content.  If the journal is merely persistent, no reset is
 1.39942 +** needed.
 1.39943 +*/
 1.39944 +static int pager_playback(Pager *pPager, int isHot){
 1.39945 +  sqlite3_vfs *pVfs = pPager->pVfs;
 1.39946 +  i64 szJ;                 /* Size of the journal file in bytes */
 1.39947 +  u32 nRec;                /* Number of Records in the journal */
 1.39948 +  u32 u;                   /* Unsigned loop counter */
 1.39949 +  Pgno mxPg = 0;           /* Size of the original file in pages */
 1.39950 +  int rc;                  /* Result code of a subroutine */
 1.39951 +  int res = 1;             /* Value returned by sqlite3OsAccess() */
 1.39952 +  char *zMaster = 0;       /* Name of master journal file if any */
 1.39953 +  int needPagerReset;      /* True to reset page prior to first page rollback */
 1.39954 +
 1.39955 +  /* Figure out how many records are in the journal.  Abort early if
 1.39956 +  ** the journal is empty.
 1.39957 +  */
 1.39958 +  assert( isOpen(pPager->jfd) );
 1.39959 +  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
 1.39960 +  if( rc!=SQLITE_OK ){
 1.39961 +    goto end_playback;
 1.39962 +  }
 1.39963 +
 1.39964 +  /* Read the master journal name from the journal, if it is present.
 1.39965 +  ** If a master journal file name is specified, but the file is not
 1.39966 +  ** present on disk, then the journal is not hot and does not need to be
 1.39967 +  ** played back.
 1.39968 +  **
 1.39969 +  ** TODO: Technically the following is an error because it assumes that
 1.39970 +  ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
 1.39971 +  ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
 1.39972 +  **  mxPathname is 512, which is the same as the minimum allowable value
 1.39973 +  ** for pageSize.
 1.39974 +  */
 1.39975 +  zMaster = pPager->pTmpSpace;
 1.39976 +  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
 1.39977 +  if( rc==SQLITE_OK && zMaster[0] ){
 1.39978 +    rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
 1.39979 +  }
 1.39980 +  zMaster = 0;
 1.39981 +  if( rc!=SQLITE_OK || !res ){
 1.39982 +    goto end_playback;
 1.39983 +  }
 1.39984 +  pPager->journalOff = 0;
 1.39985 +  needPagerReset = isHot;
 1.39986 +
 1.39987 +  /* This loop terminates either when a readJournalHdr() or 
 1.39988 +  ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 
 1.39989 +  ** occurs. 
 1.39990 +  */
 1.39991 +  while( 1 ){
 1.39992 +    /* Read the next journal header from the journal file.  If there are
 1.39993 +    ** not enough bytes left in the journal file for a complete header, or
 1.39994 +    ** it is corrupted, then a process must have failed while writing it.
 1.39995 +    ** This indicates nothing more needs to be rolled back.
 1.39996 +    */
 1.39997 +    rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
 1.39998 +    if( rc!=SQLITE_OK ){ 
 1.39999 +      if( rc==SQLITE_DONE ){
 1.40000 +        rc = SQLITE_OK;
 1.40001 +      }
 1.40002 +      goto end_playback;
 1.40003 +    }
 1.40004 +
 1.40005 +    /* If nRec is 0xffffffff, then this journal was created by a process
 1.40006 +    ** working in no-sync mode. This means that the rest of the journal
 1.40007 +    ** file consists of pages, there are no more journal headers. Compute
 1.40008 +    ** the value of nRec based on this assumption.
 1.40009 +    */
 1.40010 +    if( nRec==0xffffffff ){
 1.40011 +      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
 1.40012 +      nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
 1.40013 +    }
 1.40014 +
 1.40015 +    /* If nRec is 0 and this rollback is of a transaction created by this
 1.40016 +    ** process and if this is the final header in the journal, then it means
 1.40017 +    ** that this part of the journal was being filled but has not yet been
 1.40018 +    ** synced to disk.  Compute the number of pages based on the remaining
 1.40019 +    ** size of the file.
 1.40020 +    **
 1.40021 +    ** The third term of the test was added to fix ticket #2565.
 1.40022 +    ** When rolling back a hot journal, nRec==0 always means that the next
 1.40023 +    ** chunk of the journal contains zero pages to be rolled back.  But
 1.40024 +    ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
 1.40025 +    ** the journal, it means that the journal might contain additional
 1.40026 +    ** pages that need to be rolled back and that the number of pages 
 1.40027 +    ** should be computed based on the journal file size.
 1.40028 +    */
 1.40029 +    if( nRec==0 && !isHot &&
 1.40030 +        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
 1.40031 +      nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
 1.40032 +    }
 1.40033 +
 1.40034 +    /* If this is the first header read from the journal, truncate the
 1.40035 +    ** database file back to its original size.
 1.40036 +    */
 1.40037 +    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
 1.40038 +      rc = pager_truncate(pPager, mxPg);
 1.40039 +      if( rc!=SQLITE_OK ){
 1.40040 +        goto end_playback;
 1.40041 +      }
 1.40042 +      pPager->dbSize = mxPg;
 1.40043 +    }
 1.40044 +
 1.40045 +    /* Copy original pages out of the journal and back into the 
 1.40046 +    ** database file and/or page cache.
 1.40047 +    */
 1.40048 +    for(u=0; u<nRec; u++){
 1.40049 +      if( needPagerReset ){
 1.40050 +        pager_reset(pPager);
 1.40051 +        needPagerReset = 0;
 1.40052 +      }
 1.40053 +      rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
 1.40054 +      if( rc!=SQLITE_OK ){
 1.40055 +        if( rc==SQLITE_DONE ){
 1.40056 +          pPager->journalOff = szJ;
 1.40057 +          break;
 1.40058 +        }else if( rc==SQLITE_IOERR_SHORT_READ ){
 1.40059 +          /* If the journal has been truncated, simply stop reading and
 1.40060 +          ** processing the journal. This might happen if the journal was
 1.40061 +          ** not completely written and synced prior to a crash.  In that
 1.40062 +          ** case, the database should have never been written in the
 1.40063 +          ** first place so it is OK to simply abandon the rollback. */
 1.40064 +          rc = SQLITE_OK;
 1.40065 +          goto end_playback;
 1.40066 +        }else{
 1.40067 +          /* If we are unable to rollback, quit and return the error
 1.40068 +          ** code.  This will cause the pager to enter the error state
 1.40069 +          ** so that no further harm will be done.  Perhaps the next
 1.40070 +          ** process to come along will be able to rollback the database.
 1.40071 +          */
 1.40072 +          goto end_playback;
 1.40073 +        }
 1.40074 +      }
 1.40075 +    }
 1.40076 +  }
 1.40077 +  /*NOTREACHED*/
 1.40078 +  assert( 0 );
 1.40079 +
 1.40080 +end_playback:
 1.40081 +  /* Following a rollback, the database file should be back in its original
 1.40082 +  ** state prior to the start of the transaction, so invoke the
 1.40083 +  ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
 1.40084 +  ** assertion that the transaction counter was modified.
 1.40085 +  */
 1.40086 +#ifdef SQLITE_DEBUG
 1.40087 +  if( pPager->fd->pMethods ){
 1.40088 +    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 1.40089 +  }
 1.40090 +#endif
 1.40091 +
 1.40092 +  /* If this playback is happening automatically as a result of an IO or 
 1.40093 +  ** malloc error that occurred after the change-counter was updated but 
 1.40094 +  ** before the transaction was committed, then the change-counter 
 1.40095 +  ** modification may just have been reverted. If this happens in exclusive 
 1.40096 +  ** mode, then subsequent transactions performed by the connection will not
 1.40097 +  ** update the change-counter at all. This may lead to cache inconsistency
 1.40098 +  ** problems for other processes at some point in the future. So, just
 1.40099 +  ** in case this has happened, clear the changeCountDone flag now.
 1.40100 +  */
 1.40101 +  pPager->changeCountDone = pPager->tempFile;
 1.40102 +
 1.40103 +  if( rc==SQLITE_OK ){
 1.40104 +    zMaster = pPager->pTmpSpace;
 1.40105 +    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
 1.40106 +    testcase( rc!=SQLITE_OK );
 1.40107 +  }
 1.40108 +  if( rc==SQLITE_OK
 1.40109 +   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
 1.40110 +  ){
 1.40111 +    rc = sqlite3PagerSync(pPager);
 1.40112 +  }
 1.40113 +  if( rc==SQLITE_OK ){
 1.40114 +    rc = pager_end_transaction(pPager, zMaster[0]!='\0');
 1.40115 +    testcase( rc!=SQLITE_OK );
 1.40116 +  }
 1.40117 +  if( rc==SQLITE_OK && zMaster[0] && res ){
 1.40118 +    /* If there was a master journal and this routine will return success,
 1.40119 +    ** see if it is possible to delete the master journal.
 1.40120 +    */
 1.40121 +    rc = pager_delmaster(pPager, zMaster);
 1.40122 +    testcase( rc!=SQLITE_OK );
 1.40123 +  }
 1.40124 +
 1.40125 +  /* The Pager.sectorSize variable may have been updated while rolling
 1.40126 +  ** back a journal created by a process with a different sector size
 1.40127 +  ** value. Reset it to the correct value for this process.
 1.40128 +  */
 1.40129 +  setSectorSize(pPager);
 1.40130 +  return rc;
 1.40131 +}
 1.40132 +
 1.40133 +
 1.40134 +/*
 1.40135 +** Read the content for page pPg out of the database file and into 
 1.40136 +** pPg->pData. A shared lock or greater must be held on the database
 1.40137 +** file before this function is called.
 1.40138 +**
 1.40139 +** If page 1 is read, then the value of Pager.dbFileVers[] is set to
 1.40140 +** the value read from the database file.
 1.40141 +**
 1.40142 +** If an IO error occurs, then the IO error is returned to the caller.
 1.40143 +** Otherwise, SQLITE_OK is returned.
 1.40144 +*/
 1.40145 +static int readDbPage(PgHdr *pPg){
 1.40146 +  Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
 1.40147 +  Pgno pgno = pPg->pgno;       /* Page number to read */
 1.40148 +  int rc = SQLITE_OK;          /* Return code */
 1.40149 +  int isInWal = 0;             /* True if page is in log file */
 1.40150 +  int pgsz = pPager->pageSize; /* Number of bytes to read */
 1.40151 +
 1.40152 +  assert( pPager->eState>=PAGER_READER && !MEMDB );
 1.40153 +  assert( isOpen(pPager->fd) );
 1.40154 +
 1.40155 +  if( NEVER(!isOpen(pPager->fd)) ){
 1.40156 +    assert( pPager->tempFile );
 1.40157 +    memset(pPg->pData, 0, pPager->pageSize);
 1.40158 +    return SQLITE_OK;
 1.40159 +  }
 1.40160 +
 1.40161 +  if( pagerUseWal(pPager) ){
 1.40162 +    /* Try to pull the page from the write-ahead log. */
 1.40163 +    rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
 1.40164 +  }
 1.40165 +  if( rc==SQLITE_OK && !isInWal ){
 1.40166 +    i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
 1.40167 +    rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
 1.40168 +    if( rc==SQLITE_IOERR_SHORT_READ ){
 1.40169 +      rc = SQLITE_OK;
 1.40170 +    }
 1.40171 +  }
 1.40172 +
 1.40173 +  if( pgno==1 ){
 1.40174 +    if( rc ){
 1.40175 +      /* If the read is unsuccessful, set the dbFileVers[] to something
 1.40176 +      ** that will never be a valid file version.  dbFileVers[] is a copy
 1.40177 +      ** of bytes 24..39 of the database.  Bytes 28..31 should always be
 1.40178 +      ** zero or the size of the database in page. Bytes 32..35 and 35..39
 1.40179 +      ** should be page numbers which are never 0xffffffff.  So filling
 1.40180 +      ** pPager->dbFileVers[] with all 0xff bytes should suffice.
 1.40181 +      **
 1.40182 +      ** For an encrypted database, the situation is more complex:  bytes
 1.40183 +      ** 24..39 of the database are white noise.  But the probability of
 1.40184 +      ** white noising equaling 16 bytes of 0xff is vanishingly small so
 1.40185 +      ** we should still be ok.
 1.40186 +      */
 1.40187 +      memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
 1.40188 +    }else{
 1.40189 +      u8 *dbFileVers = &((u8*)pPg->pData)[24];
 1.40190 +      memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
 1.40191 +    }
 1.40192 +  }
 1.40193 +  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
 1.40194 +
 1.40195 +  PAGER_INCR(sqlite3_pager_readdb_count);
 1.40196 +  PAGER_INCR(pPager->nRead);
 1.40197 +  IOTRACE(("PGIN %p %d\n", pPager, pgno));
 1.40198 +  PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
 1.40199 +               PAGERID(pPager), pgno, pager_pagehash(pPg)));
 1.40200 +
 1.40201 +  return rc;
 1.40202 +}
 1.40203 +
 1.40204 +/*
 1.40205 +** Update the value of the change-counter at offsets 24 and 92 in
 1.40206 +** the header and the sqlite version number at offset 96.
 1.40207 +**
 1.40208 +** This is an unconditional update.  See also the pager_incr_changecounter()
 1.40209 +** routine which only updates the change-counter if the update is actually
 1.40210 +** needed, as determined by the pPager->changeCountDone state variable.
 1.40211 +*/
 1.40212 +static void pager_write_changecounter(PgHdr *pPg){
 1.40213 +  u32 change_counter;
 1.40214 +
 1.40215 +  /* Increment the value just read and write it back to byte 24. */
 1.40216 +  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
 1.40217 +  put32bits(((char*)pPg->pData)+24, change_counter);
 1.40218 +
 1.40219 +  /* Also store the SQLite version number in bytes 96..99 and in
 1.40220 +  ** bytes 92..95 store the change counter for which the version number
 1.40221 +  ** is valid. */
 1.40222 +  put32bits(((char*)pPg->pData)+92, change_counter);
 1.40223 +  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
 1.40224 +}
 1.40225 +
 1.40226 +#ifndef SQLITE_OMIT_WAL
 1.40227 +/*
 1.40228 +** This function is invoked once for each page that has already been 
 1.40229 +** written into the log file when a WAL transaction is rolled back.
 1.40230 +** Parameter iPg is the page number of said page. The pCtx argument 
 1.40231 +** is actually a pointer to the Pager structure.
 1.40232 +**
 1.40233 +** If page iPg is present in the cache, and has no outstanding references,
 1.40234 +** it is discarded. Otherwise, if there are one or more outstanding
 1.40235 +** references, the page content is reloaded from the database. If the
 1.40236 +** attempt to reload content from the database is required and fails, 
 1.40237 +** return an SQLite error code. Otherwise, SQLITE_OK.
 1.40238 +*/
 1.40239 +static int pagerUndoCallback(void *pCtx, Pgno iPg){
 1.40240 +  int rc = SQLITE_OK;
 1.40241 +  Pager *pPager = (Pager *)pCtx;
 1.40242 +  PgHdr *pPg;
 1.40243 +
 1.40244 +  pPg = sqlite3PagerLookup(pPager, iPg);
 1.40245 +  if( pPg ){
 1.40246 +    if( sqlite3PcachePageRefcount(pPg)==1 ){
 1.40247 +      sqlite3PcacheDrop(pPg);
 1.40248 +    }else{
 1.40249 +      rc = readDbPage(pPg);
 1.40250 +      if( rc==SQLITE_OK ){
 1.40251 +        pPager->xReiniter(pPg);
 1.40252 +      }
 1.40253 +      sqlite3PagerUnref(pPg);
 1.40254 +    }
 1.40255 +  }
 1.40256 +
 1.40257 +  /* Normally, if a transaction is rolled back, any backup processes are
 1.40258 +  ** updated as data is copied out of the rollback journal and into the
 1.40259 +  ** database. This is not generally possible with a WAL database, as
 1.40260 +  ** rollback involves simply truncating the log file. Therefore, if one
 1.40261 +  ** or more frames have already been written to the log (and therefore 
 1.40262 +  ** also copied into the backup databases) as part of this transaction,
 1.40263 +  ** the backups must be restarted.
 1.40264 +  */
 1.40265 +  sqlite3BackupRestart(pPager->pBackup);
 1.40266 +
 1.40267 +  return rc;
 1.40268 +}
 1.40269 +
 1.40270 +/*
 1.40271 +** This function is called to rollback a transaction on a WAL database.
 1.40272 +*/
 1.40273 +static int pagerRollbackWal(Pager *pPager){
 1.40274 +  int rc;                         /* Return Code */
 1.40275 +  PgHdr *pList;                   /* List of dirty pages to revert */
 1.40276 +
 1.40277 +  /* For all pages in the cache that are currently dirty or have already
 1.40278 +  ** been written (but not committed) to the log file, do one of the 
 1.40279 +  ** following:
 1.40280 +  **
 1.40281 +  **   + Discard the cached page (if refcount==0), or
 1.40282 +  **   + Reload page content from the database (if refcount>0).
 1.40283 +  */
 1.40284 +  pPager->dbSize = pPager->dbOrigSize;
 1.40285 +  rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
 1.40286 +  pList = sqlite3PcacheDirtyList(pPager->pPCache);
 1.40287 +  while( pList && rc==SQLITE_OK ){
 1.40288 +    PgHdr *pNext = pList->pDirty;
 1.40289 +    rc = pagerUndoCallback((void *)pPager, pList->pgno);
 1.40290 +    pList = pNext;
 1.40291 +  }
 1.40292 +
 1.40293 +  return rc;
 1.40294 +}
 1.40295 +
 1.40296 +/*
 1.40297 +** This function is a wrapper around sqlite3WalFrames(). As well as logging
 1.40298 +** the contents of the list of pages headed by pList (connected by pDirty),
 1.40299 +** this function notifies any active backup processes that the pages have
 1.40300 +** changed. 
 1.40301 +**
 1.40302 +** The list of pages passed into this routine is always sorted by page number.
 1.40303 +** Hence, if page 1 appears anywhere on the list, it will be the first page.
 1.40304 +*/ 
 1.40305 +static int pagerWalFrames(
 1.40306 +  Pager *pPager,                  /* Pager object */
 1.40307 +  PgHdr *pList,                   /* List of frames to log */
 1.40308 +  Pgno nTruncate,                 /* Database size after this commit */
 1.40309 +  int isCommit                    /* True if this is a commit */
 1.40310 +){
 1.40311 +  int rc;                         /* Return code */
 1.40312 +  int nList;                      /* Number of pages in pList */
 1.40313 +#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
 1.40314 +  PgHdr *p;                       /* For looping over pages */
 1.40315 +#endif
 1.40316 +
 1.40317 +  assert( pPager->pWal );
 1.40318 +  assert( pList );
 1.40319 +#ifdef SQLITE_DEBUG
 1.40320 +  /* Verify that the page list is in accending order */
 1.40321 +  for(p=pList; p && p->pDirty; p=p->pDirty){
 1.40322 +    assert( p->pgno < p->pDirty->pgno );
 1.40323 +  }
 1.40324 +#endif
 1.40325 +
 1.40326 +  assert( pList->pDirty==0 || isCommit );
 1.40327 +  if( isCommit ){
 1.40328 +    /* If a WAL transaction is being committed, there is no point in writing
 1.40329 +    ** any pages with page numbers greater than nTruncate into the WAL file.
 1.40330 +    ** They will never be read by any client. So remove them from the pDirty
 1.40331 +    ** list here. */
 1.40332 +    PgHdr *p;
 1.40333 +    PgHdr **ppNext = &pList;
 1.40334 +    nList = 0;
 1.40335 +    for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
 1.40336 +      if( p->pgno<=nTruncate ){
 1.40337 +        ppNext = &p->pDirty;
 1.40338 +        nList++;
 1.40339 +      }
 1.40340 +    }
 1.40341 +    assert( pList );
 1.40342 +  }else{
 1.40343 +    nList = 1;
 1.40344 +  }
 1.40345 +  pPager->aStat[PAGER_STAT_WRITE] += nList;
 1.40346 +
 1.40347 +  if( pList->pgno==1 ) pager_write_changecounter(pList);
 1.40348 +  rc = sqlite3WalFrames(pPager->pWal, 
 1.40349 +      pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
 1.40350 +  );
 1.40351 +  if( rc==SQLITE_OK && pPager->pBackup ){
 1.40352 +    PgHdr *p;
 1.40353 +    for(p=pList; p; p=p->pDirty){
 1.40354 +      sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
 1.40355 +    }
 1.40356 +  }
 1.40357 +
 1.40358 +#ifdef SQLITE_CHECK_PAGES
 1.40359 +  pList = sqlite3PcacheDirtyList(pPager->pPCache);
 1.40360 +  for(p=pList; p; p=p->pDirty){
 1.40361 +    pager_set_pagehash(p);
 1.40362 +  }
 1.40363 +#endif
 1.40364 +
 1.40365 +  return rc;
 1.40366 +}
 1.40367 +
 1.40368 +/*
 1.40369 +** Begin a read transaction on the WAL.
 1.40370 +**
 1.40371 +** This routine used to be called "pagerOpenSnapshot()" because it essentially
 1.40372 +** makes a snapshot of the database at the current point in time and preserves
 1.40373 +** that snapshot for use by the reader in spite of concurrently changes by
 1.40374 +** other writers or checkpointers.
 1.40375 +*/
 1.40376 +static int pagerBeginReadTransaction(Pager *pPager){
 1.40377 +  int rc;                         /* Return code */
 1.40378 +  int changed = 0;                /* True if cache must be reset */
 1.40379 +
 1.40380 +  assert( pagerUseWal(pPager) );
 1.40381 +  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
 1.40382 +
 1.40383 +  /* sqlite3WalEndReadTransaction() was not called for the previous
 1.40384 +  ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
 1.40385 +  ** are in locking_mode=NORMAL and EndRead() was previously called,
 1.40386 +  ** the duplicate call is harmless.
 1.40387 +  */
 1.40388 +  sqlite3WalEndReadTransaction(pPager->pWal);
 1.40389 +
 1.40390 +  rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
 1.40391 +  if( rc!=SQLITE_OK || changed ){
 1.40392 +    pager_reset(pPager);
 1.40393 +  }
 1.40394 +
 1.40395 +  return rc;
 1.40396 +}
 1.40397 +#endif
 1.40398 +
 1.40399 +/*
 1.40400 +** This function is called as part of the transition from PAGER_OPEN
 1.40401 +** to PAGER_READER state to determine the size of the database file
 1.40402 +** in pages (assuming the page size currently stored in Pager.pageSize).
 1.40403 +**
 1.40404 +** If no error occurs, SQLITE_OK is returned and the size of the database
 1.40405 +** in pages is stored in *pnPage. Otherwise, an error code (perhaps
 1.40406 +** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
 1.40407 +*/
 1.40408 +static int pagerPagecount(Pager *pPager, Pgno *pnPage){
 1.40409 +  Pgno nPage;                     /* Value to return via *pnPage */
 1.40410 +
 1.40411 +  /* Query the WAL sub-system for the database size. The WalDbsize()
 1.40412 +  ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
 1.40413 +  ** if the database size is not available. The database size is not
 1.40414 +  ** available from the WAL sub-system if the log file is empty or
 1.40415 +  ** contains no valid committed transactions.
 1.40416 +  */
 1.40417 +  assert( pPager->eState==PAGER_OPEN );
 1.40418 +  assert( pPager->eLock>=SHARED_LOCK );
 1.40419 +  nPage = sqlite3WalDbsize(pPager->pWal);
 1.40420 +
 1.40421 +  /* If the database size was not available from the WAL sub-system,
 1.40422 +  ** determine it based on the size of the database file. If the size
 1.40423 +  ** of the database file is not an integer multiple of the page-size,
 1.40424 +  ** round down to the nearest page. Except, any file larger than 0
 1.40425 +  ** bytes in size is considered to contain at least one page.
 1.40426 +  */
 1.40427 +  if( nPage==0 ){
 1.40428 +    i64 n = 0;                    /* Size of db file in bytes */
 1.40429 +    assert( isOpen(pPager->fd) || pPager->tempFile );
 1.40430 +    if( isOpen(pPager->fd) ){
 1.40431 +      int rc = sqlite3OsFileSize(pPager->fd, &n);
 1.40432 +      if( rc!=SQLITE_OK ){
 1.40433 +        return rc;
 1.40434 +      }
 1.40435 +    }
 1.40436 +    nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
 1.40437 +  }
 1.40438 +
 1.40439 +  /* If the current number of pages in the file is greater than the
 1.40440 +  ** configured maximum pager number, increase the allowed limit so
 1.40441 +  ** that the file can be read.
 1.40442 +  */
 1.40443 +  if( nPage>pPager->mxPgno ){
 1.40444 +    pPager->mxPgno = (Pgno)nPage;
 1.40445 +  }
 1.40446 +
 1.40447 +  *pnPage = nPage;
 1.40448 +  return SQLITE_OK;
 1.40449 +}
 1.40450 +
 1.40451 +#ifndef SQLITE_OMIT_WAL
 1.40452 +/*
 1.40453 +** Check if the *-wal file that corresponds to the database opened by pPager
 1.40454 +** exists if the database is not empy, or verify that the *-wal file does
 1.40455 +** not exist (by deleting it) if the database file is empty.
 1.40456 +**
 1.40457 +** If the database is not empty and the *-wal file exists, open the pager
 1.40458 +** in WAL mode.  If the database is empty or if no *-wal file exists and
 1.40459 +** if no error occurs, make sure Pager.journalMode is not set to
 1.40460 +** PAGER_JOURNALMODE_WAL.
 1.40461 +**
 1.40462 +** Return SQLITE_OK or an error code.
 1.40463 +**
 1.40464 +** The caller must hold a SHARED lock on the database file to call this
 1.40465 +** function. Because an EXCLUSIVE lock on the db file is required to delete 
 1.40466 +** a WAL on a none-empty database, this ensures there is no race condition 
 1.40467 +** between the xAccess() below and an xDelete() being executed by some 
 1.40468 +** other connection.
 1.40469 +*/
 1.40470 +static int pagerOpenWalIfPresent(Pager *pPager){
 1.40471 +  int rc = SQLITE_OK;
 1.40472 +  assert( pPager->eState==PAGER_OPEN );
 1.40473 +  assert( pPager->eLock>=SHARED_LOCK );
 1.40474 +
 1.40475 +  if( !pPager->tempFile ){
 1.40476 +    int isWal;                    /* True if WAL file exists */
 1.40477 +    Pgno nPage;                   /* Size of the database file */
 1.40478 +
 1.40479 +    rc = pagerPagecount(pPager, &nPage);
 1.40480 +    if( rc ) return rc;
 1.40481 +    if( nPage==0 ){
 1.40482 +      rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
 1.40483 +      if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
 1.40484 +      isWal = 0;
 1.40485 +    }else{
 1.40486 +      rc = sqlite3OsAccess(
 1.40487 +          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
 1.40488 +      );
 1.40489 +    }
 1.40490 +    if( rc==SQLITE_OK ){
 1.40491 +      if( isWal ){
 1.40492 +        testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
 1.40493 +        rc = sqlite3PagerOpenWal(pPager, 0);
 1.40494 +      }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
 1.40495 +        pPager->journalMode = PAGER_JOURNALMODE_DELETE;
 1.40496 +      }
 1.40497 +    }
 1.40498 +  }
 1.40499 +  return rc;
 1.40500 +}
 1.40501 +#endif
 1.40502 +
 1.40503 +/*
 1.40504 +** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
 1.40505 +** the entire master journal file. The case pSavepoint==NULL occurs when 
 1.40506 +** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction 
 1.40507 +** savepoint.
 1.40508 +**
 1.40509 +** When pSavepoint is not NULL (meaning a non-transaction savepoint is 
 1.40510 +** being rolled back), then the rollback consists of up to three stages,
 1.40511 +** performed in the order specified:
 1.40512 +**
 1.40513 +**   * Pages are played back from the main journal starting at byte
 1.40514 +**     offset PagerSavepoint.iOffset and continuing to 
 1.40515 +**     PagerSavepoint.iHdrOffset, or to the end of the main journal
 1.40516 +**     file if PagerSavepoint.iHdrOffset is zero.
 1.40517 +**
 1.40518 +**   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
 1.40519 +**     back starting from the journal header immediately following 
 1.40520 +**     PagerSavepoint.iHdrOffset to the end of the main journal file.
 1.40521 +**
 1.40522 +**   * Pages are then played back from the sub-journal file, starting
 1.40523 +**     with the PagerSavepoint.iSubRec and continuing to the end of
 1.40524 +**     the journal file.
 1.40525 +**
 1.40526 +** Throughout the rollback process, each time a page is rolled back, the
 1.40527 +** corresponding bit is set in a bitvec structure (variable pDone in the
 1.40528 +** implementation below). This is used to ensure that a page is only
 1.40529 +** rolled back the first time it is encountered in either journal.
 1.40530 +**
 1.40531 +** If pSavepoint is NULL, then pages are only played back from the main
 1.40532 +** journal file. There is no need for a bitvec in this case.
 1.40533 +**
 1.40534 +** In either case, before playback commences the Pager.dbSize variable
 1.40535 +** is reset to the value that it held at the start of the savepoint 
 1.40536 +** (or transaction). No page with a page-number greater than this value
 1.40537 +** is played back. If one is encountered it is simply skipped.
 1.40538 +*/
 1.40539 +static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
 1.40540 +  i64 szJ;                 /* Effective size of the main journal */
 1.40541 +  i64 iHdrOff;             /* End of first segment of main-journal records */
 1.40542 +  int rc = SQLITE_OK;      /* Return code */
 1.40543 +  Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
 1.40544 +
 1.40545 +  assert( pPager->eState!=PAGER_ERROR );
 1.40546 +  assert( pPager->eState>=PAGER_WRITER_LOCKED );
 1.40547 +
 1.40548 +  /* Allocate a bitvec to use to store the set of pages rolled back */
 1.40549 +  if( pSavepoint ){
 1.40550 +    pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
 1.40551 +    if( !pDone ){
 1.40552 +      return SQLITE_NOMEM;
 1.40553 +    }
 1.40554 +  }
 1.40555 +
 1.40556 +  /* Set the database size back to the value it was before the savepoint 
 1.40557 +  ** being reverted was opened.
 1.40558 +  */
 1.40559 +  pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
 1.40560 +  pPager->changeCountDone = pPager->tempFile;
 1.40561 +
 1.40562 +  if( !pSavepoint && pagerUseWal(pPager) ){
 1.40563 +    return pagerRollbackWal(pPager);
 1.40564 +  }
 1.40565 +
 1.40566 +  /* Use pPager->journalOff as the effective size of the main rollback
 1.40567 +  ** journal.  The actual file might be larger than this in
 1.40568 +  ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
 1.40569 +  ** past pPager->journalOff is off-limits to us.
 1.40570 +  */
 1.40571 +  szJ = pPager->journalOff;
 1.40572 +  assert( pagerUseWal(pPager)==0 || szJ==0 );
 1.40573 +
 1.40574 +  /* Begin by rolling back records from the main journal starting at
 1.40575 +  ** PagerSavepoint.iOffset and continuing to the next journal header.
 1.40576 +  ** There might be records in the main journal that have a page number
 1.40577 +  ** greater than the current database size (pPager->dbSize) but those
 1.40578 +  ** will be skipped automatically.  Pages are added to pDone as they
 1.40579 +  ** are played back.
 1.40580 +  */
 1.40581 +  if( pSavepoint && !pagerUseWal(pPager) ){
 1.40582 +    iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
 1.40583 +    pPager->journalOff = pSavepoint->iOffset;
 1.40584 +    while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
 1.40585 +      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
 1.40586 +    }
 1.40587 +    assert( rc!=SQLITE_DONE );
 1.40588 +  }else{
 1.40589 +    pPager->journalOff = 0;
 1.40590 +  }
 1.40591 +
 1.40592 +  /* Continue rolling back records out of the main journal starting at
 1.40593 +  ** the first journal header seen and continuing until the effective end
 1.40594 +  ** of the main journal file.  Continue to skip out-of-range pages and
 1.40595 +  ** continue adding pages rolled back to pDone.
 1.40596 +  */
 1.40597 +  while( rc==SQLITE_OK && pPager->journalOff<szJ ){
 1.40598 +    u32 ii;            /* Loop counter */
 1.40599 +    u32 nJRec = 0;     /* Number of Journal Records */
 1.40600 +    u32 dummy;
 1.40601 +    rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
 1.40602 +    assert( rc!=SQLITE_DONE );
 1.40603 +
 1.40604 +    /*
 1.40605 +    ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
 1.40606 +    ** test is related to ticket #2565.  See the discussion in the
 1.40607 +    ** pager_playback() function for additional information.
 1.40608 +    */
 1.40609 +    if( nJRec==0 
 1.40610 +     && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
 1.40611 +    ){
 1.40612 +      nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
 1.40613 +    }
 1.40614 +    for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
 1.40615 +      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
 1.40616 +    }
 1.40617 +    assert( rc!=SQLITE_DONE );
 1.40618 +  }
 1.40619 +  assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
 1.40620 +
 1.40621 +  /* Finally,  rollback pages from the sub-journal.  Page that were
 1.40622 +  ** previously rolled back out of the main journal (and are hence in pDone)
 1.40623 +  ** will be skipped.  Out-of-range pages are also skipped.
 1.40624 +  */
 1.40625 +  if( pSavepoint ){
 1.40626 +    u32 ii;            /* Loop counter */
 1.40627 +    i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
 1.40628 +
 1.40629 +    if( pagerUseWal(pPager) ){
 1.40630 +      rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
 1.40631 +    }
 1.40632 +    for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
 1.40633 +      assert( offset==(i64)ii*(4+pPager->pageSize) );
 1.40634 +      rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
 1.40635 +    }
 1.40636 +    assert( rc!=SQLITE_DONE );
 1.40637 +  }
 1.40638 +
 1.40639 +  sqlite3BitvecDestroy(pDone);
 1.40640 +  if( rc==SQLITE_OK ){
 1.40641 +    pPager->journalOff = szJ;
 1.40642 +  }
 1.40643 +
 1.40644 +  return rc;
 1.40645 +}
 1.40646 +
 1.40647 +/*
 1.40648 +** Change the maximum number of in-memory pages that are allowed.
 1.40649 +*/
 1.40650 +SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
 1.40651 +  sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
 1.40652 +}
 1.40653 +
 1.40654 +/*
 1.40655 +** Free as much memory as possible from the pager.
 1.40656 +*/
 1.40657 +SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
 1.40658 +  sqlite3PcacheShrink(pPager->pPCache);
 1.40659 +}
 1.40660 +
 1.40661 +/*
 1.40662 +** Adjust the robustness of the database to damage due to OS crashes
 1.40663 +** or power failures by changing the number of syncs()s when writing
 1.40664 +** the rollback journal.  There are three levels:
 1.40665 +**
 1.40666 +**    OFF       sqlite3OsSync() is never called.  This is the default
 1.40667 +**              for temporary and transient files.
 1.40668 +**
 1.40669 +**    NORMAL    The journal is synced once before writes begin on the
 1.40670 +**              database.  This is normally adequate protection, but
 1.40671 +**              it is theoretically possible, though very unlikely,
 1.40672 +**              that an inopertune power failure could leave the journal
 1.40673 +**              in a state which would cause damage to the database
 1.40674 +**              when it is rolled back.
 1.40675 +**
 1.40676 +**    FULL      The journal is synced twice before writes begin on the
 1.40677 +**              database (with some additional information - the nRec field
 1.40678 +**              of the journal header - being written in between the two
 1.40679 +**              syncs).  If we assume that writing a
 1.40680 +**              single disk sector is atomic, then this mode provides
 1.40681 +**              assurance that the journal will not be corrupted to the
 1.40682 +**              point of causing damage to the database during rollback.
 1.40683 +**
 1.40684 +** The above is for a rollback-journal mode.  For WAL mode, OFF continues
 1.40685 +** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
 1.40686 +** prior to the start of checkpoint and that the database file is synced
 1.40687 +** at the conclusion of the checkpoint if the entire content of the WAL
 1.40688 +** was written back into the database.  But no sync operations occur for
 1.40689 +** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
 1.40690 +** file is synced following each commit operation, in addition to the
 1.40691 +** syncs associated with NORMAL.
 1.40692 +**
 1.40693 +** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
 1.40694 +** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
 1.40695 +** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
 1.40696 +** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
 1.40697 +** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
 1.40698 +** synchronous=FULL versus synchronous=NORMAL setting determines when
 1.40699 +** the xSync primitive is called and is relevant to all platforms.
 1.40700 +**
 1.40701 +** Numeric values associated with these states are OFF==1, NORMAL=2,
 1.40702 +** and FULL=3.
 1.40703 +*/
 1.40704 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
 1.40705 +SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
 1.40706 +  Pager *pPager,        /* The pager to set safety level for */
 1.40707 +  int level,            /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */  
 1.40708 +  int bFullFsync,       /* PRAGMA fullfsync */
 1.40709 +  int bCkptFullFsync    /* PRAGMA checkpoint_fullfsync */
 1.40710 +){
 1.40711 +  assert( level>=1 && level<=3 );
 1.40712 +  pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
 1.40713 +  pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
 1.40714 +  if( pPager->noSync ){
 1.40715 +    pPager->syncFlags = 0;
 1.40716 +    pPager->ckptSyncFlags = 0;
 1.40717 +  }else if( bFullFsync ){
 1.40718 +    pPager->syncFlags = SQLITE_SYNC_FULL;
 1.40719 +    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 1.40720 +  }else if( bCkptFullFsync ){
 1.40721 +    pPager->syncFlags = SQLITE_SYNC_NORMAL;
 1.40722 +    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 1.40723 +  }else{
 1.40724 +    pPager->syncFlags = SQLITE_SYNC_NORMAL;
 1.40725 +    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
 1.40726 +  }
 1.40727 +  pPager->walSyncFlags = pPager->syncFlags;
 1.40728 +  if( pPager->fullSync ){
 1.40729 +    pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
 1.40730 +  }
 1.40731 +}
 1.40732 +#endif
 1.40733 +
 1.40734 +/*
 1.40735 +** The following global variable is incremented whenever the library
 1.40736 +** attempts to open a temporary file.  This information is used for
 1.40737 +** testing and analysis only.  
 1.40738 +*/
 1.40739 +#ifdef SQLITE_TEST
 1.40740 +SQLITE_API int sqlite3_opentemp_count = 0;
 1.40741 +#endif
 1.40742 +
 1.40743 +/*
 1.40744 +** Open a temporary file.
 1.40745 +**
 1.40746 +** Write the file descriptor into *pFile. Return SQLITE_OK on success 
 1.40747 +** or some other error code if we fail. The OS will automatically 
 1.40748 +** delete the temporary file when it is closed.
 1.40749 +**
 1.40750 +** The flags passed to the VFS layer xOpen() call are those specified
 1.40751 +** by parameter vfsFlags ORed with the following:
 1.40752 +**
 1.40753 +**     SQLITE_OPEN_READWRITE
 1.40754 +**     SQLITE_OPEN_CREATE
 1.40755 +**     SQLITE_OPEN_EXCLUSIVE
 1.40756 +**     SQLITE_OPEN_DELETEONCLOSE
 1.40757 +*/
 1.40758 +static int pagerOpentemp(
 1.40759 +  Pager *pPager,        /* The pager object */
 1.40760 +  sqlite3_file *pFile,  /* Write the file descriptor here */
 1.40761 +  int vfsFlags          /* Flags passed through to the VFS */
 1.40762 +){
 1.40763 +  int rc;               /* Return code */
 1.40764 +
 1.40765 +#ifdef SQLITE_TEST
 1.40766 +  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
 1.40767 +#endif
 1.40768 +
 1.40769 +  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
 1.40770 +            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
 1.40771 +  rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
 1.40772 +  assert( rc!=SQLITE_OK || isOpen(pFile) );
 1.40773 +  return rc;
 1.40774 +}
 1.40775 +
 1.40776 +/*
 1.40777 +** Set the busy handler function.
 1.40778 +**
 1.40779 +** The pager invokes the busy-handler if sqlite3OsLock() returns 
 1.40780 +** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
 1.40781 +** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE 
 1.40782 +** lock. It does *not* invoke the busy handler when upgrading from
 1.40783 +** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
 1.40784 +** (which occurs during hot-journal rollback). Summary:
 1.40785 +**
 1.40786 +**   Transition                        | Invokes xBusyHandler
 1.40787 +**   --------------------------------------------------------
 1.40788 +**   NO_LOCK       -> SHARED_LOCK      | Yes
 1.40789 +**   SHARED_LOCK   -> RESERVED_LOCK    | No
 1.40790 +**   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
 1.40791 +**   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
 1.40792 +**
 1.40793 +** If the busy-handler callback returns non-zero, the lock is 
 1.40794 +** retried. If it returns zero, then the SQLITE_BUSY error is
 1.40795 +** returned to the caller of the pager API function.
 1.40796 +*/
 1.40797 +SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
 1.40798 +  Pager *pPager,                       /* Pager object */
 1.40799 +  int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
 1.40800 +  void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
 1.40801 +){
 1.40802 +  pPager->xBusyHandler = xBusyHandler;
 1.40803 +  pPager->pBusyHandlerArg = pBusyHandlerArg;
 1.40804 +
 1.40805 +  if( isOpen(pPager->fd) ){
 1.40806 +    void **ap = (void **)&pPager->xBusyHandler;
 1.40807 +    assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
 1.40808 +    assert( ap[1]==pBusyHandlerArg );
 1.40809 +    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 1.40810 +  }
 1.40811 +}
 1.40812 +
 1.40813 +/*
 1.40814 +** Change the page size used by the Pager object. The new page size 
 1.40815 +** is passed in *pPageSize.
 1.40816 +**
 1.40817 +** If the pager is in the error state when this function is called, it
 1.40818 +** is a no-op. The value returned is the error state error code (i.e. 
 1.40819 +** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
 1.40820 +**
 1.40821 +** Otherwise, if all of the following are true:
 1.40822 +**
 1.40823 +**   * the new page size (value of *pPageSize) is valid (a power 
 1.40824 +**     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
 1.40825 +**
 1.40826 +**   * there are no outstanding page references, and
 1.40827 +**
 1.40828 +**   * the database is either not an in-memory database or it is
 1.40829 +**     an in-memory database that currently consists of zero pages.
 1.40830 +**
 1.40831 +** then the pager object page size is set to *pPageSize.
 1.40832 +**
 1.40833 +** If the page size is changed, then this function uses sqlite3PagerMalloc() 
 1.40834 +** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt 
 1.40835 +** fails, SQLITE_NOMEM is returned and the page size remains unchanged. 
 1.40836 +** In all other cases, SQLITE_OK is returned.
 1.40837 +**
 1.40838 +** If the page size is not changed, either because one of the enumerated
 1.40839 +** conditions above is not true, the pager was in error state when this
 1.40840 +** function was called, or because the memory allocation attempt failed, 
 1.40841 +** then *pPageSize is set to the old, retained page size before returning.
 1.40842 +*/
 1.40843 +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
 1.40844 +  int rc = SQLITE_OK;
 1.40845 +
 1.40846 +  /* It is not possible to do a full assert_pager_state() here, as this
 1.40847 +  ** function may be called from within PagerOpen(), before the state
 1.40848 +  ** of the Pager object is internally consistent.
 1.40849 +  **
 1.40850 +  ** At one point this function returned an error if the pager was in 
 1.40851 +  ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
 1.40852 +  ** there is at least one outstanding page reference, this function
 1.40853 +  ** is a no-op for that case anyhow.
 1.40854 +  */
 1.40855 +
 1.40856 +  u32 pageSize = *pPageSize;
 1.40857 +  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
 1.40858 +  if( (pPager->memDb==0 || pPager->dbSize==0)
 1.40859 +   && sqlite3PcacheRefCount(pPager->pPCache)==0 
 1.40860 +   && pageSize && pageSize!=(u32)pPager->pageSize 
 1.40861 +  ){
 1.40862 +    char *pNew = NULL;             /* New temp space */
 1.40863 +    i64 nByte = 0;
 1.40864 +
 1.40865 +    if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
 1.40866 +      rc = sqlite3OsFileSize(pPager->fd, &nByte);
 1.40867 +    }
 1.40868 +    if( rc==SQLITE_OK ){
 1.40869 +      pNew = (char *)sqlite3PageMalloc(pageSize);
 1.40870 +      if( !pNew ) rc = SQLITE_NOMEM;
 1.40871 +    }
 1.40872 +
 1.40873 +    if( rc==SQLITE_OK ){
 1.40874 +      pager_reset(pPager);
 1.40875 +      pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
 1.40876 +      pPager->pageSize = pageSize;
 1.40877 +      sqlite3PageFree(pPager->pTmpSpace);
 1.40878 +      pPager->pTmpSpace = pNew;
 1.40879 +      sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
 1.40880 +    }
 1.40881 +  }
 1.40882 +
 1.40883 +  *pPageSize = pPager->pageSize;
 1.40884 +  if( rc==SQLITE_OK ){
 1.40885 +    if( nReserve<0 ) nReserve = pPager->nReserve;
 1.40886 +    assert( nReserve>=0 && nReserve<1000 );
 1.40887 +    pPager->nReserve = (i16)nReserve;
 1.40888 +    pagerReportSize(pPager);
 1.40889 +  }
 1.40890 +  return rc;
 1.40891 +}
 1.40892 +
 1.40893 +/*
 1.40894 +** Return a pointer to the "temporary page" buffer held internally
 1.40895 +** by the pager.  This is a buffer that is big enough to hold the
 1.40896 +** entire content of a database page.  This buffer is used internally
 1.40897 +** during rollback and will be overwritten whenever a rollback
 1.40898 +** occurs.  But other modules are free to use it too, as long as
 1.40899 +** no rollbacks are happening.
 1.40900 +*/
 1.40901 +SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
 1.40902 +  return pPager->pTmpSpace;
 1.40903 +}
 1.40904 +
 1.40905 +/*
 1.40906 +** Attempt to set the maximum database page count if mxPage is positive. 
 1.40907 +** Make no changes if mxPage is zero or negative.  And never reduce the
 1.40908 +** maximum page count below the current size of the database.
 1.40909 +**
 1.40910 +** Regardless of mxPage, return the current maximum page count.
 1.40911 +*/
 1.40912 +SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
 1.40913 +  if( mxPage>0 ){
 1.40914 +    pPager->mxPgno = mxPage;
 1.40915 +  }
 1.40916 +  assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
 1.40917 +  assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
 1.40918 +  return pPager->mxPgno;
 1.40919 +}
 1.40920 +
 1.40921 +/*
 1.40922 +** The following set of routines are used to disable the simulated
 1.40923 +** I/O error mechanism.  These routines are used to avoid simulated
 1.40924 +** errors in places where we do not care about errors.
 1.40925 +**
 1.40926 +** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
 1.40927 +** and generate no code.
 1.40928 +*/
 1.40929 +#ifdef SQLITE_TEST
 1.40930 +SQLITE_API extern int sqlite3_io_error_pending;
 1.40931 +SQLITE_API extern int sqlite3_io_error_hit;
 1.40932 +static int saved_cnt;
 1.40933 +void disable_simulated_io_errors(void){
 1.40934 +  saved_cnt = sqlite3_io_error_pending;
 1.40935 +  sqlite3_io_error_pending = -1;
 1.40936 +}
 1.40937 +void enable_simulated_io_errors(void){
 1.40938 +  sqlite3_io_error_pending = saved_cnt;
 1.40939 +}
 1.40940 +#else
 1.40941 +# define disable_simulated_io_errors()
 1.40942 +# define enable_simulated_io_errors()
 1.40943 +#endif
 1.40944 +
 1.40945 +/*
 1.40946 +** Read the first N bytes from the beginning of the file into memory
 1.40947 +** that pDest points to. 
 1.40948 +**
 1.40949 +** If the pager was opened on a transient file (zFilename==""), or
 1.40950 +** opened on a file less than N bytes in size, the output buffer is
 1.40951 +** zeroed and SQLITE_OK returned. The rationale for this is that this 
 1.40952 +** function is used to read database headers, and a new transient or
 1.40953 +** zero sized database has a header than consists entirely of zeroes.
 1.40954 +**
 1.40955 +** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
 1.40956 +** the error code is returned to the caller and the contents of the
 1.40957 +** output buffer undefined.
 1.40958 +*/
 1.40959 +SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
 1.40960 +  int rc = SQLITE_OK;
 1.40961 +  memset(pDest, 0, N);
 1.40962 +  assert( isOpen(pPager->fd) || pPager->tempFile );
 1.40963 +
 1.40964 +  /* This routine is only called by btree immediately after creating
 1.40965 +  ** the Pager object.  There has not been an opportunity to transition
 1.40966 +  ** to WAL mode yet.
 1.40967 +  */
 1.40968 +  assert( !pagerUseWal(pPager) );
 1.40969 +
 1.40970 +  if( isOpen(pPager->fd) ){
 1.40971 +    IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
 1.40972 +    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
 1.40973 +    if( rc==SQLITE_IOERR_SHORT_READ ){
 1.40974 +      rc = SQLITE_OK;
 1.40975 +    }
 1.40976 +  }
 1.40977 +  return rc;
 1.40978 +}
 1.40979 +
 1.40980 +/*
 1.40981 +** This function may only be called when a read-transaction is open on
 1.40982 +** the pager. It returns the total number of pages in the database.
 1.40983 +**
 1.40984 +** However, if the file is between 1 and <page-size> bytes in size, then 
 1.40985 +** this is considered a 1 page file.
 1.40986 +*/
 1.40987 +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
 1.40988 +  assert( pPager->eState>=PAGER_READER );
 1.40989 +  assert( pPager->eState!=PAGER_WRITER_FINISHED );
 1.40990 +  *pnPage = (int)pPager->dbSize;
 1.40991 +}
 1.40992 +
 1.40993 +
 1.40994 +/*
 1.40995 +** Try to obtain a lock of type locktype on the database file. If
 1.40996 +** a similar or greater lock is already held, this function is a no-op
 1.40997 +** (returning SQLITE_OK immediately).
 1.40998 +**
 1.40999 +** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke 
 1.41000 +** the busy callback if the lock is currently not available. Repeat 
 1.41001 +** until the busy callback returns false or until the attempt to 
 1.41002 +** obtain the lock succeeds.
 1.41003 +**
 1.41004 +** Return SQLITE_OK on success and an error code if we cannot obtain
 1.41005 +** the lock. If the lock is obtained successfully, set the Pager.state 
 1.41006 +** variable to locktype before returning.
 1.41007 +*/
 1.41008 +static int pager_wait_on_lock(Pager *pPager, int locktype){
 1.41009 +  int rc;                              /* Return code */
 1.41010 +
 1.41011 +  /* Check that this is either a no-op (because the requested lock is 
 1.41012 +  ** already held, or one of the transistions that the busy-handler
 1.41013 +  ** may be invoked during, according to the comment above
 1.41014 +  ** sqlite3PagerSetBusyhandler().
 1.41015 +  */
 1.41016 +  assert( (pPager->eLock>=locktype)
 1.41017 +       || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
 1.41018 +       || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
 1.41019 +  );
 1.41020 +
 1.41021 +  do {
 1.41022 +    rc = pagerLockDb(pPager, locktype);
 1.41023 +  }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
 1.41024 +  return rc;
 1.41025 +}
 1.41026 +
 1.41027 +/*
 1.41028 +** Function assertTruncateConstraint(pPager) checks that one of the 
 1.41029 +** following is true for all dirty pages currently in the page-cache:
 1.41030 +**
 1.41031 +**   a) The page number is less than or equal to the size of the 
 1.41032 +**      current database image, in pages, OR
 1.41033 +**
 1.41034 +**   b) if the page content were written at this time, it would not
 1.41035 +**      be necessary to write the current content out to the sub-journal
 1.41036 +**      (as determined by function subjRequiresPage()).
 1.41037 +**
 1.41038 +** If the condition asserted by this function were not true, and the
 1.41039 +** dirty page were to be discarded from the cache via the pagerStress()
 1.41040 +** routine, pagerStress() would not write the current page content to
 1.41041 +** the database file. If a savepoint transaction were rolled back after
 1.41042 +** this happened, the correct behaviour would be to restore the current
 1.41043 +** content of the page. However, since this content is not present in either
 1.41044 +** the database file or the portion of the rollback journal and 
 1.41045 +** sub-journal rolled back the content could not be restored and the
 1.41046 +** database image would become corrupt. It is therefore fortunate that 
 1.41047 +** this circumstance cannot arise.
 1.41048 +*/
 1.41049 +#if defined(SQLITE_DEBUG)
 1.41050 +static void assertTruncateConstraintCb(PgHdr *pPg){
 1.41051 +  assert( pPg->flags&PGHDR_DIRTY );
 1.41052 +  assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
 1.41053 +}
 1.41054 +static void assertTruncateConstraint(Pager *pPager){
 1.41055 +  sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
 1.41056 +}
 1.41057 +#else
 1.41058 +# define assertTruncateConstraint(pPager)
 1.41059 +#endif
 1.41060 +
 1.41061 +/*
 1.41062 +** Truncate the in-memory database file image to nPage pages. This 
 1.41063 +** function does not actually modify the database file on disk. It 
 1.41064 +** just sets the internal state of the pager object so that the 
 1.41065 +** truncation will be done when the current transaction is committed.
 1.41066 +*/
 1.41067 +SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
 1.41068 +  assert( pPager->dbSize>=nPage );
 1.41069 +  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
 1.41070 +  pPager->dbSize = nPage;
 1.41071 +  assertTruncateConstraint(pPager);
 1.41072 +}
 1.41073 +
 1.41074 +
 1.41075 +/*
 1.41076 +** This function is called before attempting a hot-journal rollback. It
 1.41077 +** syncs the journal file to disk, then sets pPager->journalHdr to the
 1.41078 +** size of the journal file so that the pager_playback() routine knows
 1.41079 +** that the entire journal file has been synced.
 1.41080 +**
 1.41081 +** Syncing a hot-journal to disk before attempting to roll it back ensures 
 1.41082 +** that if a power-failure occurs during the rollback, the process that
 1.41083 +** attempts rollback following system recovery sees the same journal
 1.41084 +** content as this process.
 1.41085 +**
 1.41086 +** If everything goes as planned, SQLITE_OK is returned. Otherwise, 
 1.41087 +** an SQLite error code.
 1.41088 +*/
 1.41089 +static int pagerSyncHotJournal(Pager *pPager){
 1.41090 +  int rc = SQLITE_OK;
 1.41091 +  if( !pPager->noSync ){
 1.41092 +    rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
 1.41093 +  }
 1.41094 +  if( rc==SQLITE_OK ){
 1.41095 +    rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
 1.41096 +  }
 1.41097 +  return rc;
 1.41098 +}
 1.41099 +
 1.41100 +/*
 1.41101 +** Shutdown the page cache.  Free all memory and close all files.
 1.41102 +**
 1.41103 +** If a transaction was in progress when this routine is called, that
 1.41104 +** transaction is rolled back.  All outstanding pages are invalidated
 1.41105 +** and their memory is freed.  Any attempt to use a page associated
 1.41106 +** with this page cache after this function returns will likely
 1.41107 +** result in a coredump.
 1.41108 +**
 1.41109 +** This function always succeeds. If a transaction is active an attempt
 1.41110 +** is made to roll it back. If an error occurs during the rollback 
 1.41111 +** a hot journal may be left in the filesystem but no error is returned
 1.41112 +** to the caller.
 1.41113 +*/
 1.41114 +SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
 1.41115 +  u8 *pTmp = (u8 *)pPager->pTmpSpace;
 1.41116 +
 1.41117 +  assert( assert_pager_state(pPager) );
 1.41118 +  disable_simulated_io_errors();
 1.41119 +  sqlite3BeginBenignMalloc();
 1.41120 +  /* pPager->errCode = 0; */
 1.41121 +  pPager->exclusiveMode = 0;
 1.41122 +#ifndef SQLITE_OMIT_WAL
 1.41123 +  sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
 1.41124 +  pPager->pWal = 0;
 1.41125 +#endif
 1.41126 +  pager_reset(pPager);
 1.41127 +  if( MEMDB ){
 1.41128 +    pager_unlock(pPager);
 1.41129 +  }else{
 1.41130 +    /* If it is open, sync the journal file before calling UnlockAndRollback.
 1.41131 +    ** If this is not done, then an unsynced portion of the open journal 
 1.41132 +    ** file may be played back into the database. If a power failure occurs 
 1.41133 +    ** while this is happening, the database could become corrupt.
 1.41134 +    **
 1.41135 +    ** If an error occurs while trying to sync the journal, shift the pager
 1.41136 +    ** into the ERROR state. This causes UnlockAndRollback to unlock the
 1.41137 +    ** database and close the journal file without attempting to roll it
 1.41138 +    ** back or finalize it. The next database user will have to do hot-journal
 1.41139 +    ** rollback before accessing the database file.
 1.41140 +    */
 1.41141 +    if( isOpen(pPager->jfd) ){
 1.41142 +      pager_error(pPager, pagerSyncHotJournal(pPager));
 1.41143 +    }
 1.41144 +    pagerUnlockAndRollback(pPager);
 1.41145 +  }
 1.41146 +  sqlite3EndBenignMalloc();
 1.41147 +  enable_simulated_io_errors();
 1.41148 +  PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
 1.41149 +  IOTRACE(("CLOSE %p\n", pPager))
 1.41150 +  sqlite3OsClose(pPager->jfd);
 1.41151 +  sqlite3OsClose(pPager->fd);
 1.41152 +  sqlite3PageFree(pTmp);
 1.41153 +  sqlite3PcacheClose(pPager->pPCache);
 1.41154 +
 1.41155 +#ifdef SQLITE_HAS_CODEC
 1.41156 +  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
 1.41157 +#endif
 1.41158 +
 1.41159 +  assert( !pPager->aSavepoint && !pPager->pInJournal );
 1.41160 +  assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
 1.41161 +
 1.41162 +  sqlite3_free(pPager);
 1.41163 +  return SQLITE_OK;
 1.41164 +}
 1.41165 +
 1.41166 +#if !defined(NDEBUG) || defined(SQLITE_TEST)
 1.41167 +/*
 1.41168 +** Return the page number for page pPg.
 1.41169 +*/
 1.41170 +SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
 1.41171 +  return pPg->pgno;
 1.41172 +}
 1.41173 +#endif
 1.41174 +
 1.41175 +/*
 1.41176 +** Increment the reference count for page pPg.
 1.41177 +*/
 1.41178 +SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
 1.41179 +  sqlite3PcacheRef(pPg);
 1.41180 +}
 1.41181 +
 1.41182 +/*
 1.41183 +** Sync the journal. In other words, make sure all the pages that have
 1.41184 +** been written to the journal have actually reached the surface of the
 1.41185 +** disk and can be restored in the event of a hot-journal rollback.
 1.41186 +**
 1.41187 +** If the Pager.noSync flag is set, then this function is a no-op.
 1.41188 +** Otherwise, the actions required depend on the journal-mode and the 
 1.41189 +** device characteristics of the file-system, as follows:
 1.41190 +**
 1.41191 +**   * If the journal file is an in-memory journal file, no action need
 1.41192 +**     be taken.
 1.41193 +**
 1.41194 +**   * Otherwise, if the device does not support the SAFE_APPEND property,
 1.41195 +**     then the nRec field of the most recently written journal header
 1.41196 +**     is updated to contain the number of journal records that have
 1.41197 +**     been written following it. If the pager is operating in full-sync
 1.41198 +**     mode, then the journal file is synced before this field is updated.
 1.41199 +**
 1.41200 +**   * If the device does not support the SEQUENTIAL property, then 
 1.41201 +**     journal file is synced.
 1.41202 +**
 1.41203 +** Or, in pseudo-code:
 1.41204 +**
 1.41205 +**   if( NOT <in-memory journal> ){
 1.41206 +**     if( NOT SAFE_APPEND ){
 1.41207 +**       if( <full-sync mode> ) xSync(<journal file>);
 1.41208 +**       <update nRec field>
 1.41209 +**     } 
 1.41210 +**     if( NOT SEQUENTIAL ) xSync(<journal file>);
 1.41211 +**   }
 1.41212 +**
 1.41213 +** If successful, this routine clears the PGHDR_NEED_SYNC flag of every 
 1.41214 +** page currently held in memory before returning SQLITE_OK. If an IO
 1.41215 +** error is encountered, then the IO error code is returned to the caller.
 1.41216 +*/
 1.41217 +static int syncJournal(Pager *pPager, int newHdr){
 1.41218 +  int rc;                         /* Return code */
 1.41219 +
 1.41220 +  assert( pPager->eState==PAGER_WRITER_CACHEMOD
 1.41221 +       || pPager->eState==PAGER_WRITER_DBMOD
 1.41222 +  );
 1.41223 +  assert( assert_pager_state(pPager) );
 1.41224 +  assert( !pagerUseWal(pPager) );
 1.41225 +
 1.41226 +  rc = sqlite3PagerExclusiveLock(pPager);
 1.41227 +  if( rc!=SQLITE_OK ) return rc;
 1.41228 +
 1.41229 +  if( !pPager->noSync ){
 1.41230 +    assert( !pPager->tempFile );
 1.41231 +    if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
 1.41232 +      const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
 1.41233 +      assert( isOpen(pPager->jfd) );
 1.41234 +
 1.41235 +      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
 1.41236 +        /* This block deals with an obscure problem. If the last connection
 1.41237 +        ** that wrote to this database was operating in persistent-journal
 1.41238 +        ** mode, then the journal file may at this point actually be larger
 1.41239 +        ** than Pager.journalOff bytes. If the next thing in the journal
 1.41240 +        ** file happens to be a journal-header (written as part of the
 1.41241 +        ** previous connection's transaction), and a crash or power-failure 
 1.41242 +        ** occurs after nRec is updated but before this connection writes 
 1.41243 +        ** anything else to the journal file (or commits/rolls back its 
 1.41244 +        ** transaction), then SQLite may become confused when doing the 
 1.41245 +        ** hot-journal rollback following recovery. It may roll back all
 1.41246 +        ** of this connections data, then proceed to rolling back the old,
 1.41247 +        ** out-of-date data that follows it. Database corruption.
 1.41248 +        **
 1.41249 +        ** To work around this, if the journal file does appear to contain
 1.41250 +        ** a valid header following Pager.journalOff, then write a 0x00
 1.41251 +        ** byte to the start of it to prevent it from being recognized.
 1.41252 +        **
 1.41253 +        ** Variable iNextHdrOffset is set to the offset at which this
 1.41254 +        ** problematic header will occur, if it exists. aMagic is used 
 1.41255 +        ** as a temporary buffer to inspect the first couple of bytes of
 1.41256 +        ** the potential journal header.
 1.41257 +        */
 1.41258 +        i64 iNextHdrOffset;
 1.41259 +        u8 aMagic[8];
 1.41260 +        u8 zHeader[sizeof(aJournalMagic)+4];
 1.41261 +
 1.41262 +        memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
 1.41263 +        put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
 1.41264 +
 1.41265 +        iNextHdrOffset = journalHdrOffset(pPager);
 1.41266 +        rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
 1.41267 +        if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
 1.41268 +          static const u8 zerobyte = 0;
 1.41269 +          rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
 1.41270 +        }
 1.41271 +        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
 1.41272 +          return rc;
 1.41273 +        }
 1.41274 +
 1.41275 +        /* Write the nRec value into the journal file header. If in
 1.41276 +        ** full-synchronous mode, sync the journal first. This ensures that
 1.41277 +        ** all data has really hit the disk before nRec is updated to mark
 1.41278 +        ** it as a candidate for rollback.
 1.41279 +        **
 1.41280 +        ** This is not required if the persistent media supports the
 1.41281 +        ** SAFE_APPEND property. Because in this case it is not possible 
 1.41282 +        ** for garbage data to be appended to the file, the nRec field
 1.41283 +        ** is populated with 0xFFFFFFFF when the journal header is written
 1.41284 +        ** and never needs to be updated.
 1.41285 +        */
 1.41286 +        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
 1.41287 +          PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
 1.41288 +          IOTRACE(("JSYNC %p\n", pPager))
 1.41289 +          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
 1.41290 +          if( rc!=SQLITE_OK ) return rc;
 1.41291 +        }
 1.41292 +        IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
 1.41293 +        rc = sqlite3OsWrite(
 1.41294 +            pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
 1.41295 +        );
 1.41296 +        if( rc!=SQLITE_OK ) return rc;
 1.41297 +      }
 1.41298 +      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
 1.41299 +        PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
 1.41300 +        IOTRACE(("JSYNC %p\n", pPager))
 1.41301 +        rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| 
 1.41302 +          (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
 1.41303 +        );
 1.41304 +        if( rc!=SQLITE_OK ) return rc;
 1.41305 +      }
 1.41306 +
 1.41307 +      pPager->journalHdr = pPager->journalOff;
 1.41308 +      if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
 1.41309 +        pPager->nRec = 0;
 1.41310 +        rc = writeJournalHdr(pPager);
 1.41311 +        if( rc!=SQLITE_OK ) return rc;
 1.41312 +      }
 1.41313 +    }else{
 1.41314 +      pPager->journalHdr = pPager->journalOff;
 1.41315 +    }
 1.41316 +  }
 1.41317 +
 1.41318 +  /* Unless the pager is in noSync mode, the journal file was just 
 1.41319 +  ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on 
 1.41320 +  ** all pages.
 1.41321 +  */
 1.41322 +  sqlite3PcacheClearSyncFlags(pPager->pPCache);
 1.41323 +  pPager->eState = PAGER_WRITER_DBMOD;
 1.41324 +  assert( assert_pager_state(pPager) );
 1.41325 +  return SQLITE_OK;
 1.41326 +}
 1.41327 +
 1.41328 +/*
 1.41329 +** The argument is the first in a linked list of dirty pages connected
 1.41330 +** by the PgHdr.pDirty pointer. This function writes each one of the
 1.41331 +** in-memory pages in the list to the database file. The argument may
 1.41332 +** be NULL, representing an empty list. In this case this function is
 1.41333 +** a no-op.
 1.41334 +**
 1.41335 +** The pager must hold at least a RESERVED lock when this function
 1.41336 +** is called. Before writing anything to the database file, this lock
 1.41337 +** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
 1.41338 +** SQLITE_BUSY is returned and no data is written to the database file.
 1.41339 +** 
 1.41340 +** If the pager is a temp-file pager and the actual file-system file
 1.41341 +** is not yet open, it is created and opened before any data is 
 1.41342 +** written out.
 1.41343 +**
 1.41344 +** Once the lock has been upgraded and, if necessary, the file opened,
 1.41345 +** the pages are written out to the database file in list order. Writing
 1.41346 +** a page is skipped if it meets either of the following criteria:
 1.41347 +**
 1.41348 +**   * The page number is greater than Pager.dbSize, or
 1.41349 +**   * The PGHDR_DONT_WRITE flag is set on the page.
 1.41350 +**
 1.41351 +** If writing out a page causes the database file to grow, Pager.dbFileSize
 1.41352 +** is updated accordingly. If page 1 is written out, then the value cached
 1.41353 +** in Pager.dbFileVers[] is updated to match the new value stored in
 1.41354 +** the database file.
 1.41355 +**
 1.41356 +** If everything is successful, SQLITE_OK is returned. If an IO error 
 1.41357 +** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
 1.41358 +** be obtained, SQLITE_BUSY is returned.
 1.41359 +*/
 1.41360 +static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
 1.41361 +  int rc = SQLITE_OK;                  /* Return code */
 1.41362 +
 1.41363 +  /* This function is only called for rollback pagers in WRITER_DBMOD state. */
 1.41364 +  assert( !pagerUseWal(pPager) );
 1.41365 +  assert( pPager->eState==PAGER_WRITER_DBMOD );
 1.41366 +  assert( pPager->eLock==EXCLUSIVE_LOCK );
 1.41367 +
 1.41368 +  /* If the file is a temp-file has not yet been opened, open it now. It
 1.41369 +  ** is not possible for rc to be other than SQLITE_OK if this branch
 1.41370 +  ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
 1.41371 +  */
 1.41372 +  if( !isOpen(pPager->fd) ){
 1.41373 +    assert( pPager->tempFile && rc==SQLITE_OK );
 1.41374 +    rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
 1.41375 +  }
 1.41376 +
 1.41377 +  /* Before the first write, give the VFS a hint of what the final
 1.41378 +  ** file size will be.
 1.41379 +  */
 1.41380 +  assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
 1.41381 +  if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
 1.41382 +    sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
 1.41383 +    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
 1.41384 +    pPager->dbHintSize = pPager->dbSize;
 1.41385 +  }
 1.41386 +
 1.41387 +  while( rc==SQLITE_OK && pList ){
 1.41388 +    Pgno pgno = pList->pgno;
 1.41389 +
 1.41390 +    /* If there are dirty pages in the page cache with page numbers greater
 1.41391 +    ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
 1.41392 +    ** make the file smaller (presumably by auto-vacuum code). Do not write
 1.41393 +    ** any such pages to the file.
 1.41394 +    **
 1.41395 +    ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
 1.41396 +    ** set (set by sqlite3PagerDontWrite()).
 1.41397 +    */
 1.41398 +    if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
 1.41399 +      i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
 1.41400 +      char *pData;                                   /* Data to write */    
 1.41401 +
 1.41402 +      assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
 1.41403 +      if( pList->pgno==1 ) pager_write_changecounter(pList);
 1.41404 +
 1.41405 +      /* Encode the database */
 1.41406 +      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
 1.41407 +
 1.41408 +      /* Write out the page data. */
 1.41409 +      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
 1.41410 +
 1.41411 +      /* If page 1 was just written, update Pager.dbFileVers to match
 1.41412 +      ** the value now stored in the database file. If writing this 
 1.41413 +      ** page caused the database file to grow, update dbFileSize. 
 1.41414 +      */
 1.41415 +      if( pgno==1 ){
 1.41416 +        memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
 1.41417 +      }
 1.41418 +      if( pgno>pPager->dbFileSize ){
 1.41419 +        pPager->dbFileSize = pgno;
 1.41420 +      }
 1.41421 +      pPager->aStat[PAGER_STAT_WRITE]++;
 1.41422 +
 1.41423 +      /* Update any backup objects copying the contents of this pager. */
 1.41424 +      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
 1.41425 +
 1.41426 +      PAGERTRACE(("STORE %d page %d hash(%08x)\n",
 1.41427 +                   PAGERID(pPager), pgno, pager_pagehash(pList)));
 1.41428 +      IOTRACE(("PGOUT %p %d\n", pPager, pgno));
 1.41429 +      PAGER_INCR(sqlite3_pager_writedb_count);
 1.41430 +    }else{
 1.41431 +      PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
 1.41432 +    }
 1.41433 +    pager_set_pagehash(pList);
 1.41434 +    pList = pList->pDirty;
 1.41435 +  }
 1.41436 +
 1.41437 +  return rc;
 1.41438 +}
 1.41439 +
 1.41440 +/*
 1.41441 +** Ensure that the sub-journal file is open. If it is already open, this 
 1.41442 +** function is a no-op.
 1.41443 +**
 1.41444 +** SQLITE_OK is returned if everything goes according to plan. An 
 1.41445 +** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() 
 1.41446 +** fails.
 1.41447 +*/
 1.41448 +static int openSubJournal(Pager *pPager){
 1.41449 +  int rc = SQLITE_OK;
 1.41450 +  if( !isOpen(pPager->sjfd) ){
 1.41451 +    if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
 1.41452 +      sqlite3MemJournalOpen(pPager->sjfd);
 1.41453 +    }else{
 1.41454 +      rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
 1.41455 +    }
 1.41456 +  }
 1.41457 +  return rc;
 1.41458 +}
 1.41459 +
 1.41460 +/*
 1.41461 +** Append a record of the current state of page pPg to the sub-journal. 
 1.41462 +** It is the callers responsibility to use subjRequiresPage() to check 
 1.41463 +** that it is really required before calling this function.
 1.41464 +**
 1.41465 +** If successful, set the bit corresponding to pPg->pgno in the bitvecs
 1.41466 +** for all open savepoints before returning.
 1.41467 +**
 1.41468 +** This function returns SQLITE_OK if everything is successful, an IO
 1.41469 +** error code if the attempt to write to the sub-journal fails, or 
 1.41470 +** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
 1.41471 +** bitvec.
 1.41472 +*/
 1.41473 +static int subjournalPage(PgHdr *pPg){
 1.41474 +  int rc = SQLITE_OK;
 1.41475 +  Pager *pPager = pPg->pPager;
 1.41476 +  if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
 1.41477 +
 1.41478 +    /* Open the sub-journal, if it has not already been opened */
 1.41479 +    assert( pPager->useJournal );
 1.41480 +    assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
 1.41481 +    assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
 1.41482 +    assert( pagerUseWal(pPager) 
 1.41483 +         || pageInJournal(pPg) 
 1.41484 +         || pPg->pgno>pPager->dbOrigSize 
 1.41485 +    );
 1.41486 +    rc = openSubJournal(pPager);
 1.41487 +
 1.41488 +    /* If the sub-journal was opened successfully (or was already open),
 1.41489 +    ** write the journal record into the file.  */
 1.41490 +    if( rc==SQLITE_OK ){
 1.41491 +      void *pData = pPg->pData;
 1.41492 +      i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
 1.41493 +      char *pData2;
 1.41494 +  
 1.41495 +      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
 1.41496 +      PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
 1.41497 +      rc = write32bits(pPager->sjfd, offset, pPg->pgno);
 1.41498 +      if( rc==SQLITE_OK ){
 1.41499 +        rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
 1.41500 +      }
 1.41501 +    }
 1.41502 +  }
 1.41503 +  if( rc==SQLITE_OK ){
 1.41504 +    pPager->nSubRec++;
 1.41505 +    assert( pPager->nSavepoint>0 );
 1.41506 +    rc = addToSavepointBitvecs(pPager, pPg->pgno);
 1.41507 +  }
 1.41508 +  return rc;
 1.41509 +}
 1.41510 +
 1.41511 +/*
 1.41512 +** This function is called by the pcache layer when it has reached some
 1.41513 +** soft memory limit. The first argument is a pointer to a Pager object
 1.41514 +** (cast as a void*). The pager is always 'purgeable' (not an in-memory
 1.41515 +** database). The second argument is a reference to a page that is 
 1.41516 +** currently dirty but has no outstanding references. The page
 1.41517 +** is always associated with the Pager object passed as the first 
 1.41518 +** argument.
 1.41519 +**
 1.41520 +** The job of this function is to make pPg clean by writing its contents
 1.41521 +** out to the database file, if possible. This may involve syncing the
 1.41522 +** journal file. 
 1.41523 +**
 1.41524 +** If successful, sqlite3PcacheMakeClean() is called on the page and
 1.41525 +** SQLITE_OK returned. If an IO error occurs while trying to make the
 1.41526 +** page clean, the IO error code is returned. If the page cannot be
 1.41527 +** made clean for some other reason, but no error occurs, then SQLITE_OK
 1.41528 +** is returned by sqlite3PcacheMakeClean() is not called.
 1.41529 +*/
 1.41530 +static int pagerStress(void *p, PgHdr *pPg){
 1.41531 +  Pager *pPager = (Pager *)p;
 1.41532 +  int rc = SQLITE_OK;
 1.41533 +
 1.41534 +  assert( pPg->pPager==pPager );
 1.41535 +  assert( pPg->flags&PGHDR_DIRTY );
 1.41536 +
 1.41537 +  /* The doNotSyncSpill flag is set during times when doing a sync of
 1.41538 +  ** journal (and adding a new header) is not allowed.  This occurs
 1.41539 +  ** during calls to sqlite3PagerWrite() while trying to journal multiple
 1.41540 +  ** pages belonging to the same sector.
 1.41541 +  **
 1.41542 +  ** The doNotSpill flag inhibits all cache spilling regardless of whether
 1.41543 +  ** or not a sync is required.  This is set during a rollback.
 1.41544 +  **
 1.41545 +  ** Spilling is also prohibited when in an error state since that could
 1.41546 +  ** lead to database corruption.   In the current implementaton it 
 1.41547 +  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
 1.41548 +  ** while in the error state, hence it is impossible for this routine to
 1.41549 +  ** be called in the error state.  Nevertheless, we include a NEVER()
 1.41550 +  ** test for the error state as a safeguard against future changes.
 1.41551 +  */
 1.41552 +  if( NEVER(pPager->errCode) ) return SQLITE_OK;
 1.41553 +  if( pPager->doNotSpill ) return SQLITE_OK;
 1.41554 +  if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
 1.41555 +    return SQLITE_OK;
 1.41556 +  }
 1.41557 +
 1.41558 +  pPg->pDirty = 0;
 1.41559 +  if( pagerUseWal(pPager) ){
 1.41560 +    /* Write a single frame for this page to the log. */
 1.41561 +    if( subjRequiresPage(pPg) ){ 
 1.41562 +      rc = subjournalPage(pPg); 
 1.41563 +    }
 1.41564 +    if( rc==SQLITE_OK ){
 1.41565 +      rc = pagerWalFrames(pPager, pPg, 0, 0);
 1.41566 +    }
 1.41567 +  }else{
 1.41568 +  
 1.41569 +    /* Sync the journal file if required. */
 1.41570 +    if( pPg->flags&PGHDR_NEED_SYNC 
 1.41571 +     || pPager->eState==PAGER_WRITER_CACHEMOD
 1.41572 +    ){
 1.41573 +      rc = syncJournal(pPager, 1);
 1.41574 +    }
 1.41575 +  
 1.41576 +    /* If the page number of this page is larger than the current size of
 1.41577 +    ** the database image, it may need to be written to the sub-journal.
 1.41578 +    ** This is because the call to pager_write_pagelist() below will not
 1.41579 +    ** actually write data to the file in this case.
 1.41580 +    **
 1.41581 +    ** Consider the following sequence of events:
 1.41582 +    **
 1.41583 +    **   BEGIN;
 1.41584 +    **     <journal page X>
 1.41585 +    **     <modify page X>
 1.41586 +    **     SAVEPOINT sp;
 1.41587 +    **       <shrink database file to Y pages>
 1.41588 +    **       pagerStress(page X)
 1.41589 +    **     ROLLBACK TO sp;
 1.41590 +    **
 1.41591 +    ** If (X>Y), then when pagerStress is called page X will not be written
 1.41592 +    ** out to the database file, but will be dropped from the cache. Then,
 1.41593 +    ** following the "ROLLBACK TO sp" statement, reading page X will read
 1.41594 +    ** data from the database file. This will be the copy of page X as it
 1.41595 +    ** was when the transaction started, not as it was when "SAVEPOINT sp"
 1.41596 +    ** was executed.
 1.41597 +    **
 1.41598 +    ** The solution is to write the current data for page X into the 
 1.41599 +    ** sub-journal file now (if it is not already there), so that it will
 1.41600 +    ** be restored to its current value when the "ROLLBACK TO sp" is 
 1.41601 +    ** executed.
 1.41602 +    */
 1.41603 +    if( NEVER(
 1.41604 +        rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
 1.41605 +    ) ){
 1.41606 +      rc = subjournalPage(pPg);
 1.41607 +    }
 1.41608 +  
 1.41609 +    /* Write the contents of the page out to the database file. */
 1.41610 +    if( rc==SQLITE_OK ){
 1.41611 +      assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
 1.41612 +      rc = pager_write_pagelist(pPager, pPg);
 1.41613 +    }
 1.41614 +  }
 1.41615 +
 1.41616 +  /* Mark the page as clean. */
 1.41617 +  if( rc==SQLITE_OK ){
 1.41618 +    PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
 1.41619 +    sqlite3PcacheMakeClean(pPg);
 1.41620 +  }
 1.41621 +
 1.41622 +  return pager_error(pPager, rc); 
 1.41623 +}
 1.41624 +
 1.41625 +
 1.41626 +/*
 1.41627 +** Allocate and initialize a new Pager object and put a pointer to it
 1.41628 +** in *ppPager. The pager should eventually be freed by passing it
 1.41629 +** to sqlite3PagerClose().
 1.41630 +**
 1.41631 +** The zFilename argument is the path to the database file to open.
 1.41632 +** If zFilename is NULL then a randomly-named temporary file is created
 1.41633 +** and used as the file to be cached. Temporary files are be deleted
 1.41634 +** automatically when they are closed. If zFilename is ":memory:" then 
 1.41635 +** all information is held in cache. It is never written to disk. 
 1.41636 +** This can be used to implement an in-memory database.
 1.41637 +**
 1.41638 +** The nExtra parameter specifies the number of bytes of space allocated
 1.41639 +** along with each page reference. This space is available to the user
 1.41640 +** via the sqlite3PagerGetExtra() API.
 1.41641 +**
 1.41642 +** The flags argument is used to specify properties that affect the
 1.41643 +** operation of the pager. It should be passed some bitwise combination
 1.41644 +** of the PAGER_* flags.
 1.41645 +**
 1.41646 +** The vfsFlags parameter is a bitmask to pass to the flags parameter
 1.41647 +** of the xOpen() method of the supplied VFS when opening files. 
 1.41648 +**
 1.41649 +** If the pager object is allocated and the specified file opened 
 1.41650 +** successfully, SQLITE_OK is returned and *ppPager set to point to
 1.41651 +** the new pager object. If an error occurs, *ppPager is set to NULL
 1.41652 +** and error code returned. This function may return SQLITE_NOMEM
 1.41653 +** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or 
 1.41654 +** various SQLITE_IO_XXX errors.
 1.41655 +*/
 1.41656 +SQLITE_PRIVATE int sqlite3PagerOpen(
 1.41657 +  sqlite3_vfs *pVfs,       /* The virtual file system to use */
 1.41658 +  Pager **ppPager,         /* OUT: Return the Pager structure here */
 1.41659 +  const char *zFilename,   /* Name of the database file to open */
 1.41660 +  int nExtra,              /* Extra bytes append to each in-memory page */
 1.41661 +  int flags,               /* flags controlling this file */
 1.41662 +  int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
 1.41663 +  void (*xReinit)(DbPage*) /* Function to reinitialize pages */
 1.41664 +){
 1.41665 +  u8 *pPtr;
 1.41666 +  Pager *pPager = 0;       /* Pager object to allocate and return */
 1.41667 +  int rc = SQLITE_OK;      /* Return code */
 1.41668 +  int tempFile = 0;        /* True for temp files (incl. in-memory files) */
 1.41669 +  int memDb = 0;           /* True if this is an in-memory file */
 1.41670 +  int readOnly = 0;        /* True if this is a read-only file */
 1.41671 +  int journalFileSize;     /* Bytes to allocate for each journal fd */
 1.41672 +  char *zPathname = 0;     /* Full path to database file */
 1.41673 +  int nPathname = 0;       /* Number of bytes in zPathname */
 1.41674 +  int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
 1.41675 +  int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
 1.41676 +  u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
 1.41677 +  const char *zUri = 0;    /* URI args to copy */
 1.41678 +  int nUri = 0;            /* Number of bytes of URI args at *zUri */
 1.41679 +
 1.41680 +  /* Figure out how much space is required for each journal file-handle
 1.41681 +  ** (there are two of them, the main journal and the sub-journal). This
 1.41682 +  ** is the maximum space required for an in-memory journal file handle 
 1.41683 +  ** and a regular journal file-handle. Note that a "regular journal-handle"
 1.41684 +  ** may be a wrapper capable of caching the first portion of the journal
 1.41685 +  ** file in memory to implement the atomic-write optimization (see 
 1.41686 +  ** source file journal.c).
 1.41687 +  */
 1.41688 +  if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
 1.41689 +    journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
 1.41690 +  }else{
 1.41691 +    journalFileSize = ROUND8(sqlite3MemJournalSize());
 1.41692 +  }
 1.41693 +
 1.41694 +  /* Set the output variable to NULL in case an error occurs. */
 1.41695 +  *ppPager = 0;
 1.41696 +
 1.41697 +#ifndef SQLITE_OMIT_MEMORYDB
 1.41698 +  if( flags & PAGER_MEMORY ){
 1.41699 +    memDb = 1;
 1.41700 +    if( zFilename && zFilename[0] ){
 1.41701 +      zPathname = sqlite3DbStrDup(0, zFilename);
 1.41702 +      if( zPathname==0  ) return SQLITE_NOMEM;
 1.41703 +      nPathname = sqlite3Strlen30(zPathname);
 1.41704 +      zFilename = 0;
 1.41705 +    }
 1.41706 +  }
 1.41707 +#endif
 1.41708 +
 1.41709 +  /* Compute and store the full pathname in an allocated buffer pointed
 1.41710 +  ** to by zPathname, length nPathname. Or, if this is a temporary file,
 1.41711 +  ** leave both nPathname and zPathname set to 0.
 1.41712 +  */
 1.41713 +  if( zFilename && zFilename[0] ){
 1.41714 +    const char *z;
 1.41715 +    nPathname = pVfs->mxPathname+1;
 1.41716 +    zPathname = sqlite3DbMallocRaw(0, nPathname*2);
 1.41717 +    if( zPathname==0 ){
 1.41718 +      return SQLITE_NOMEM;
 1.41719 +    }
 1.41720 +    zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
 1.41721 +    rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
 1.41722 +    nPathname = sqlite3Strlen30(zPathname);
 1.41723 +    z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
 1.41724 +    while( *z ){
 1.41725 +      z += sqlite3Strlen30(z)+1;
 1.41726 +      z += sqlite3Strlen30(z)+1;
 1.41727 +    }
 1.41728 +    nUri = (int)(&z[1] - zUri);
 1.41729 +    assert( nUri>=0 );
 1.41730 +    if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
 1.41731 +      /* This branch is taken when the journal path required by
 1.41732 +      ** the database being opened will be more than pVfs->mxPathname
 1.41733 +      ** bytes in length. This means the database cannot be opened,
 1.41734 +      ** as it will not be possible to open the journal file or even
 1.41735 +      ** check for a hot-journal before reading.
 1.41736 +      */
 1.41737 +      rc = SQLITE_CANTOPEN_BKPT;
 1.41738 +    }
 1.41739 +    if( rc!=SQLITE_OK ){
 1.41740 +      sqlite3DbFree(0, zPathname);
 1.41741 +      return rc;
 1.41742 +    }
 1.41743 +  }
 1.41744 +
 1.41745 +  /* Allocate memory for the Pager structure, PCache object, the
 1.41746 +  ** three file descriptors, the database file name and the journal 
 1.41747 +  ** file name. The layout in memory is as follows:
 1.41748 +  **
 1.41749 +  **     Pager object                    (sizeof(Pager) bytes)
 1.41750 +  **     PCache object                   (sqlite3PcacheSize() bytes)
 1.41751 +  **     Database file handle            (pVfs->szOsFile bytes)
 1.41752 +  **     Sub-journal file handle         (journalFileSize bytes)
 1.41753 +  **     Main journal file handle        (journalFileSize bytes)
 1.41754 +  **     Database file name              (nPathname+1 bytes)
 1.41755 +  **     Journal file name               (nPathname+8+1 bytes)
 1.41756 +  */
 1.41757 +  pPtr = (u8 *)sqlite3MallocZero(
 1.41758 +    ROUND8(sizeof(*pPager)) +      /* Pager structure */
 1.41759 +    ROUND8(pcacheSize) +           /* PCache object */
 1.41760 +    ROUND8(pVfs->szOsFile) +       /* The main db file */
 1.41761 +    journalFileSize * 2 +          /* The two journal files */ 
 1.41762 +    nPathname + 1 + nUri +         /* zFilename */
 1.41763 +    nPathname + 8 + 2              /* zJournal */
 1.41764 +#ifndef SQLITE_OMIT_WAL
 1.41765 +    + nPathname + 4 + 2            /* zWal */
 1.41766 +#endif
 1.41767 +  );
 1.41768 +  assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
 1.41769 +  if( !pPtr ){
 1.41770 +    sqlite3DbFree(0, zPathname);
 1.41771 +    return SQLITE_NOMEM;
 1.41772 +  }
 1.41773 +  pPager =              (Pager*)(pPtr);
 1.41774 +  pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
 1.41775 +  pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
 1.41776 +  pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
 1.41777 +  pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
 1.41778 +  pPager->zFilename =    (char*)(pPtr += journalFileSize);
 1.41779 +  assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
 1.41780 +
 1.41781 +  /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
 1.41782 +  if( zPathname ){
 1.41783 +    assert( nPathname>0 );
 1.41784 +    pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
 1.41785 +    memcpy(pPager->zFilename, zPathname, nPathname);
 1.41786 +    if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
 1.41787 +    memcpy(pPager->zJournal, zPathname, nPathname);
 1.41788 +    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
 1.41789 +    sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
 1.41790 +#ifndef SQLITE_OMIT_WAL
 1.41791 +    pPager->zWal = &pPager->zJournal[nPathname+8+1];
 1.41792 +    memcpy(pPager->zWal, zPathname, nPathname);
 1.41793 +    memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
 1.41794 +    sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
 1.41795 +#endif
 1.41796 +    sqlite3DbFree(0, zPathname);
 1.41797 +  }
 1.41798 +  pPager->pVfs = pVfs;
 1.41799 +  pPager->vfsFlags = vfsFlags;
 1.41800 +
 1.41801 +  /* Open the pager file.
 1.41802 +  */
 1.41803 +  if( zFilename && zFilename[0] ){
 1.41804 +    int fout = 0;                    /* VFS flags returned by xOpen() */
 1.41805 +    rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
 1.41806 +    assert( !memDb );
 1.41807 +    readOnly = (fout&SQLITE_OPEN_READONLY);
 1.41808 +
 1.41809 +    /* If the file was successfully opened for read/write access,
 1.41810 +    ** choose a default page size in case we have to create the
 1.41811 +    ** database file. The default page size is the maximum of:
 1.41812 +    **
 1.41813 +    **    + SQLITE_DEFAULT_PAGE_SIZE,
 1.41814 +    **    + The value returned by sqlite3OsSectorSize()
 1.41815 +    **    + The largest page size that can be written atomically.
 1.41816 +    */
 1.41817 +    if( rc==SQLITE_OK && !readOnly ){
 1.41818 +      setSectorSize(pPager);
 1.41819 +      assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
 1.41820 +      if( szPageDflt<pPager->sectorSize ){
 1.41821 +        if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
 1.41822 +          szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
 1.41823 +        }else{
 1.41824 +          szPageDflt = (u32)pPager->sectorSize;
 1.41825 +        }
 1.41826 +      }
 1.41827 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
 1.41828 +      {
 1.41829 +        int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
 1.41830 +        int ii;
 1.41831 +        assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 1.41832 +        assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 1.41833 +        assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
 1.41834 +        for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
 1.41835 +          if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
 1.41836 +            szPageDflt = ii;
 1.41837 +          }
 1.41838 +        }
 1.41839 +      }
 1.41840 +#endif
 1.41841 +    }
 1.41842 +  }else{
 1.41843 +    /* If a temporary file is requested, it is not opened immediately.
 1.41844 +    ** In this case we accept the default page size and delay actually
 1.41845 +    ** opening the file until the first call to OsWrite().
 1.41846 +    **
 1.41847 +    ** This branch is also run for an in-memory database. An in-memory
 1.41848 +    ** database is the same as a temp-file that is never written out to
 1.41849 +    ** disk and uses an in-memory rollback journal.
 1.41850 +    */ 
 1.41851 +    tempFile = 1;
 1.41852 +    pPager->eState = PAGER_READER;
 1.41853 +    pPager->eLock = EXCLUSIVE_LOCK;
 1.41854 +    readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
 1.41855 +  }
 1.41856 +
 1.41857 +  /* The following call to PagerSetPagesize() serves to set the value of 
 1.41858 +  ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
 1.41859 +  */
 1.41860 +  if( rc==SQLITE_OK ){
 1.41861 +    assert( pPager->memDb==0 );
 1.41862 +    rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
 1.41863 +    testcase( rc!=SQLITE_OK );
 1.41864 +  }
 1.41865 +
 1.41866 +  /* If an error occurred in either of the blocks above, free the 
 1.41867 +  ** Pager structure and close the file.
 1.41868 +  */
 1.41869 +  if( rc!=SQLITE_OK ){
 1.41870 +    assert( !pPager->pTmpSpace );
 1.41871 +    sqlite3OsClose(pPager->fd);
 1.41872 +    sqlite3_free(pPager);
 1.41873 +    return rc;
 1.41874 +  }
 1.41875 +
 1.41876 +  /* Initialize the PCache object. */
 1.41877 +  assert( nExtra<1000 );
 1.41878 +  nExtra = ROUND8(nExtra);
 1.41879 +  sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
 1.41880 +                    !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
 1.41881 +
 1.41882 +  PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
 1.41883 +  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
 1.41884 +
 1.41885 +  pPager->useJournal = (u8)useJournal;
 1.41886 +  /* pPager->stmtOpen = 0; */
 1.41887 +  /* pPager->stmtInUse = 0; */
 1.41888 +  /* pPager->nRef = 0; */
 1.41889 +  /* pPager->stmtSize = 0; */
 1.41890 +  /* pPager->stmtJSize = 0; */
 1.41891 +  /* pPager->nPage = 0; */
 1.41892 +  pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
 1.41893 +  /* pPager->state = PAGER_UNLOCK; */
 1.41894 +#if 0
 1.41895 +  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
 1.41896 +#endif
 1.41897 +  /* pPager->errMask = 0; */
 1.41898 +  pPager->tempFile = (u8)tempFile;
 1.41899 +  assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
 1.41900 +          || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
 1.41901 +  assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
 1.41902 +  pPager->exclusiveMode = (u8)tempFile; 
 1.41903 +  pPager->changeCountDone = pPager->tempFile;
 1.41904 +  pPager->memDb = (u8)memDb;
 1.41905 +  pPager->readOnly = (u8)readOnly;
 1.41906 +  assert( useJournal || pPager->tempFile );
 1.41907 +  pPager->noSync = pPager->tempFile;
 1.41908 +  if( pPager->noSync ){
 1.41909 +    assert( pPager->fullSync==0 );
 1.41910 +    assert( pPager->syncFlags==0 );
 1.41911 +    assert( pPager->walSyncFlags==0 );
 1.41912 +    assert( pPager->ckptSyncFlags==0 );
 1.41913 +  }else{
 1.41914 +    pPager->fullSync = 1;
 1.41915 +    pPager->syncFlags = SQLITE_SYNC_NORMAL;
 1.41916 +    pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
 1.41917 +    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
 1.41918 +  }
 1.41919 +  /* pPager->pFirst = 0; */
 1.41920 +  /* pPager->pFirstSynced = 0; */
 1.41921 +  /* pPager->pLast = 0; */
 1.41922 +  pPager->nExtra = (u16)nExtra;
 1.41923 +  pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 1.41924 +  assert( isOpen(pPager->fd) || tempFile );
 1.41925 +  setSectorSize(pPager);
 1.41926 +  if( !useJournal ){
 1.41927 +    pPager->journalMode = PAGER_JOURNALMODE_OFF;
 1.41928 +  }else if( memDb ){
 1.41929 +    pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 1.41930 +  }
 1.41931 +  /* pPager->xBusyHandler = 0; */
 1.41932 +  /* pPager->pBusyHandlerArg = 0; */
 1.41933 +  pPager->xReiniter = xReinit;
 1.41934 +  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
 1.41935 +
 1.41936 +  *ppPager = pPager;
 1.41937 +  return SQLITE_OK;
 1.41938 +}
 1.41939 +
 1.41940 +
 1.41941 +
 1.41942 +/*
 1.41943 +** This function is called after transitioning from PAGER_UNLOCK to
 1.41944 +** PAGER_SHARED state. It tests if there is a hot journal present in
 1.41945 +** the file-system for the given pager. A hot journal is one that 
 1.41946 +** needs to be played back. According to this function, a hot-journal
 1.41947 +** file exists if the following criteria are met:
 1.41948 +**
 1.41949 +**   * The journal file exists in the file system, and
 1.41950 +**   * No process holds a RESERVED or greater lock on the database file, and
 1.41951 +**   * The database file itself is greater than 0 bytes in size, and
 1.41952 +**   * The first byte of the journal file exists and is not 0x00.
 1.41953 +**
 1.41954 +** If the current size of the database file is 0 but a journal file
 1.41955 +** exists, that is probably an old journal left over from a prior
 1.41956 +** database with the same name. In this case the journal file is
 1.41957 +** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
 1.41958 +** is returned.
 1.41959 +**
 1.41960 +** This routine does not check if there is a master journal filename
 1.41961 +** at the end of the file. If there is, and that master journal file
 1.41962 +** does not exist, then the journal file is not really hot. In this
 1.41963 +** case this routine will return a false-positive. The pager_playback()
 1.41964 +** routine will discover that the journal file is not really hot and 
 1.41965 +** will not roll it back. 
 1.41966 +**
 1.41967 +** If a hot-journal file is found to exist, *pExists is set to 1 and 
 1.41968 +** SQLITE_OK returned. If no hot-journal file is present, *pExists is
 1.41969 +** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
 1.41970 +** to determine whether or not a hot-journal file exists, the IO error
 1.41971 +** code is returned and the value of *pExists is undefined.
 1.41972 +*/
 1.41973 +static int hasHotJournal(Pager *pPager, int *pExists){
 1.41974 +  sqlite3_vfs * const pVfs = pPager->pVfs;
 1.41975 +  int rc = SQLITE_OK;           /* Return code */
 1.41976 +  int exists = 1;               /* True if a journal file is present */
 1.41977 +  int jrnlOpen = !!isOpen(pPager->jfd);
 1.41978 +
 1.41979 +  assert( pPager->useJournal );
 1.41980 +  assert( isOpen(pPager->fd) );
 1.41981 +  assert( pPager->eState==PAGER_OPEN );
 1.41982 +
 1.41983 +  assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
 1.41984 +    SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
 1.41985 +  ));
 1.41986 +
 1.41987 +  *pExists = 0;
 1.41988 +  if( !jrnlOpen ){
 1.41989 +    rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
 1.41990 +  }
 1.41991 +  if( rc==SQLITE_OK && exists ){
 1.41992 +    int locked = 0;             /* True if some process holds a RESERVED lock */
 1.41993 +
 1.41994 +    /* Race condition here:  Another process might have been holding the
 1.41995 +    ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 
 1.41996 +    ** call above, but then delete the journal and drop the lock before
 1.41997 +    ** we get to the following sqlite3OsCheckReservedLock() call.  If that
 1.41998 +    ** is the case, this routine might think there is a hot journal when
 1.41999 +    ** in fact there is none.  This results in a false-positive which will
 1.42000 +    ** be dealt with by the playback routine.  Ticket #3883.
 1.42001 +    */
 1.42002 +    rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
 1.42003 +    if( rc==SQLITE_OK && !locked ){
 1.42004 +      Pgno nPage;                 /* Number of pages in database file */
 1.42005 +
 1.42006 +      /* Check the size of the database file. If it consists of 0 pages,
 1.42007 +      ** then delete the journal file. See the header comment above for 
 1.42008 +      ** the reasoning here.  Delete the obsolete journal file under
 1.42009 +      ** a RESERVED lock to avoid race conditions and to avoid violating
 1.42010 +      ** [H33020].
 1.42011 +      */
 1.42012 +      rc = pagerPagecount(pPager, &nPage);
 1.42013 +      if( rc==SQLITE_OK ){
 1.42014 +        if( nPage==0 ){
 1.42015 +          sqlite3BeginBenignMalloc();
 1.42016 +          if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
 1.42017 +            sqlite3OsDelete(pVfs, pPager->zJournal, 0);
 1.42018 +            if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
 1.42019 +          }
 1.42020 +          sqlite3EndBenignMalloc();
 1.42021 +        }else{
 1.42022 +          /* The journal file exists and no other connection has a reserved
 1.42023 +          ** or greater lock on the database file. Now check that there is
 1.42024 +          ** at least one non-zero bytes at the start of the journal file.
 1.42025 +          ** If there is, then we consider this journal to be hot. If not, 
 1.42026 +          ** it can be ignored.
 1.42027 +          */
 1.42028 +          if( !jrnlOpen ){
 1.42029 +            int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
 1.42030 +            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
 1.42031 +          }
 1.42032 +          if( rc==SQLITE_OK ){
 1.42033 +            u8 first = 0;
 1.42034 +            rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
 1.42035 +            if( rc==SQLITE_IOERR_SHORT_READ ){
 1.42036 +              rc = SQLITE_OK;
 1.42037 +            }
 1.42038 +            if( !jrnlOpen ){
 1.42039 +              sqlite3OsClose(pPager->jfd);
 1.42040 +            }
 1.42041 +            *pExists = (first!=0);
 1.42042 +          }else if( rc==SQLITE_CANTOPEN ){
 1.42043 +            /* If we cannot open the rollback journal file in order to see if
 1.42044 +            ** its has a zero header, that might be due to an I/O error, or
 1.42045 +            ** it might be due to the race condition described above and in
 1.42046 +            ** ticket #3883.  Either way, assume that the journal is hot.
 1.42047 +            ** This might be a false positive.  But if it is, then the
 1.42048 +            ** automatic journal playback and recovery mechanism will deal
 1.42049 +            ** with it under an EXCLUSIVE lock where we do not need to
 1.42050 +            ** worry so much with race conditions.
 1.42051 +            */
 1.42052 +            *pExists = 1;
 1.42053 +            rc = SQLITE_OK;
 1.42054 +          }
 1.42055 +        }
 1.42056 +      }
 1.42057 +    }
 1.42058 +  }
 1.42059 +
 1.42060 +  return rc;
 1.42061 +}
 1.42062 +
 1.42063 +/*
 1.42064 +** This function is called to obtain a shared lock on the database file.
 1.42065 +** It is illegal to call sqlite3PagerAcquire() until after this function
 1.42066 +** has been successfully called. If a shared-lock is already held when
 1.42067 +** this function is called, it is a no-op.
 1.42068 +**
 1.42069 +** The following operations are also performed by this function.
 1.42070 +**
 1.42071 +**   1) If the pager is currently in PAGER_OPEN state (no lock held
 1.42072 +**      on the database file), then an attempt is made to obtain a
 1.42073 +**      SHARED lock on the database file. Immediately after obtaining
 1.42074 +**      the SHARED lock, the file-system is checked for a hot-journal,
 1.42075 +**      which is played back if present. Following any hot-journal 
 1.42076 +**      rollback, the contents of the cache are validated by checking
 1.42077 +**      the 'change-counter' field of the database file header and
 1.42078 +**      discarded if they are found to be invalid.
 1.42079 +**
 1.42080 +**   2) If the pager is running in exclusive-mode, and there are currently
 1.42081 +**      no outstanding references to any pages, and is in the error state,
 1.42082 +**      then an attempt is made to clear the error state by discarding
 1.42083 +**      the contents of the page cache and rolling back any open journal
 1.42084 +**      file.
 1.42085 +**
 1.42086 +** If everything is successful, SQLITE_OK is returned. If an IO error 
 1.42087 +** occurs while locking the database, checking for a hot-journal file or 
 1.42088 +** rolling back a journal file, the IO error code is returned.
 1.42089 +*/
 1.42090 +SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
 1.42091 +  int rc = SQLITE_OK;                /* Return code */
 1.42092 +
 1.42093 +  /* This routine is only called from b-tree and only when there are no
 1.42094 +  ** outstanding pages. This implies that the pager state should either
 1.42095 +  ** be OPEN or READER. READER is only possible if the pager is or was in 
 1.42096 +  ** exclusive access mode.
 1.42097 +  */
 1.42098 +  assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
 1.42099 +  assert( assert_pager_state(pPager) );
 1.42100 +  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
 1.42101 +  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
 1.42102 +
 1.42103 +  if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
 1.42104 +    int bHotJournal = 1;          /* True if there exists a hot journal-file */
 1.42105 +
 1.42106 +    assert( !MEMDB );
 1.42107 +
 1.42108 +    rc = pager_wait_on_lock(pPager, SHARED_LOCK);
 1.42109 +    if( rc!=SQLITE_OK ){
 1.42110 +      assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
 1.42111 +      goto failed;
 1.42112 +    }
 1.42113 +
 1.42114 +    /* If a journal file exists, and there is no RESERVED lock on the
 1.42115 +    ** database file, then it either needs to be played back or deleted.
 1.42116 +    */
 1.42117 +    if( pPager->eLock<=SHARED_LOCK ){
 1.42118 +      rc = hasHotJournal(pPager, &bHotJournal);
 1.42119 +    }
 1.42120 +    if( rc!=SQLITE_OK ){
 1.42121 +      goto failed;
 1.42122 +    }
 1.42123 +    if( bHotJournal ){
 1.42124 +      /* Get an EXCLUSIVE lock on the database file. At this point it is
 1.42125 +      ** important that a RESERVED lock is not obtained on the way to the
 1.42126 +      ** EXCLUSIVE lock. If it were, another process might open the
 1.42127 +      ** database file, detect the RESERVED lock, and conclude that the
 1.42128 +      ** database is safe to read while this process is still rolling the 
 1.42129 +      ** hot-journal back.
 1.42130 +      ** 
 1.42131 +      ** Because the intermediate RESERVED lock is not requested, any
 1.42132 +      ** other process attempting to access the database file will get to 
 1.42133 +      ** this point in the code and fail to obtain its own EXCLUSIVE lock 
 1.42134 +      ** on the database file.
 1.42135 +      **
 1.42136 +      ** Unless the pager is in locking_mode=exclusive mode, the lock is
 1.42137 +      ** downgraded to SHARED_LOCK before this function returns.
 1.42138 +      */
 1.42139 +      rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 1.42140 +      if( rc!=SQLITE_OK ){
 1.42141 +        goto failed;
 1.42142 +      }
 1.42143 + 
 1.42144 +      /* If it is not already open and the file exists on disk, open the 
 1.42145 +      ** journal for read/write access. Write access is required because 
 1.42146 +      ** in exclusive-access mode the file descriptor will be kept open 
 1.42147 +      ** and possibly used for a transaction later on. Also, write-access 
 1.42148 +      ** is usually required to finalize the journal in journal_mode=persist 
 1.42149 +      ** mode (and also for journal_mode=truncate on some systems).
 1.42150 +      **
 1.42151 +      ** If the journal does not exist, it usually means that some 
 1.42152 +      ** other connection managed to get in and roll it back before 
 1.42153 +      ** this connection obtained the exclusive lock above. Or, it 
 1.42154 +      ** may mean that the pager was in the error-state when this
 1.42155 +      ** function was called and the journal file does not exist.
 1.42156 +      */
 1.42157 +      if( !isOpen(pPager->jfd) ){
 1.42158 +        sqlite3_vfs * const pVfs = pPager->pVfs;
 1.42159 +        int bExists;              /* True if journal file exists */
 1.42160 +        rc = sqlite3OsAccess(
 1.42161 +            pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
 1.42162 +        if( rc==SQLITE_OK && bExists ){
 1.42163 +          int fout = 0;
 1.42164 +          int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
 1.42165 +          assert( !pPager->tempFile );
 1.42166 +          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
 1.42167 +          assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
 1.42168 +          if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
 1.42169 +            rc = SQLITE_CANTOPEN_BKPT;
 1.42170 +            sqlite3OsClose(pPager->jfd);
 1.42171 +          }
 1.42172 +        }
 1.42173 +      }
 1.42174 + 
 1.42175 +      /* Playback and delete the journal.  Drop the database write
 1.42176 +      ** lock and reacquire the read lock. Purge the cache before
 1.42177 +      ** playing back the hot-journal so that we don't end up with
 1.42178 +      ** an inconsistent cache.  Sync the hot journal before playing
 1.42179 +      ** it back since the process that crashed and left the hot journal
 1.42180 +      ** probably did not sync it and we are required to always sync
 1.42181 +      ** the journal before playing it back.
 1.42182 +      */
 1.42183 +      if( isOpen(pPager->jfd) ){
 1.42184 +        assert( rc==SQLITE_OK );
 1.42185 +        rc = pagerSyncHotJournal(pPager);
 1.42186 +        if( rc==SQLITE_OK ){
 1.42187 +          rc = pager_playback(pPager, 1);
 1.42188 +          pPager->eState = PAGER_OPEN;
 1.42189 +        }
 1.42190 +      }else if( !pPager->exclusiveMode ){
 1.42191 +        pagerUnlockDb(pPager, SHARED_LOCK);
 1.42192 +      }
 1.42193 +
 1.42194 +      if( rc!=SQLITE_OK ){
 1.42195 +        /* This branch is taken if an error occurs while trying to open
 1.42196 +        ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
 1.42197 +        ** pager_unlock() routine will be called before returning to unlock
 1.42198 +        ** the file. If the unlock attempt fails, then Pager.eLock must be
 1.42199 +        ** set to UNKNOWN_LOCK (see the comment above the #define for 
 1.42200 +        ** UNKNOWN_LOCK above for an explanation). 
 1.42201 +        **
 1.42202 +        ** In order to get pager_unlock() to do this, set Pager.eState to
 1.42203 +        ** PAGER_ERROR now. This is not actually counted as a transition
 1.42204 +        ** to ERROR state in the state diagram at the top of this file,
 1.42205 +        ** since we know that the same call to pager_unlock() will very
 1.42206 +        ** shortly transition the pager object to the OPEN state. Calling
 1.42207 +        ** assert_pager_state() would fail now, as it should not be possible
 1.42208 +        ** to be in ERROR state when there are zero outstanding page 
 1.42209 +        ** references.
 1.42210 +        */
 1.42211 +        pager_error(pPager, rc);
 1.42212 +        goto failed;
 1.42213 +      }
 1.42214 +
 1.42215 +      assert( pPager->eState==PAGER_OPEN );
 1.42216 +      assert( (pPager->eLock==SHARED_LOCK)
 1.42217 +           || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
 1.42218 +      );
 1.42219 +    }
 1.42220 +
 1.42221 +    if( !pPager->tempFile 
 1.42222 +     && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0) 
 1.42223 +    ){
 1.42224 +      /* The shared-lock has just been acquired on the database file
 1.42225 +      ** and there are already pages in the cache (from a previous
 1.42226 +      ** read or write transaction).  Check to see if the database
 1.42227 +      ** has been modified.  If the database has changed, flush the
 1.42228 +      ** cache.
 1.42229 +      **
 1.42230 +      ** Database changes is detected by looking at 15 bytes beginning
 1.42231 +      ** at offset 24 into the file.  The first 4 of these 16 bytes are
 1.42232 +      ** a 32-bit counter that is incremented with each change.  The
 1.42233 +      ** other bytes change randomly with each file change when
 1.42234 +      ** a codec is in use.
 1.42235 +      ** 
 1.42236 +      ** There is a vanishingly small chance that a change will not be 
 1.42237 +      ** detected.  The chance of an undetected change is so small that
 1.42238 +      ** it can be neglected.
 1.42239 +      */
 1.42240 +      Pgno nPage = 0;
 1.42241 +      char dbFileVers[sizeof(pPager->dbFileVers)];
 1.42242 +
 1.42243 +      rc = pagerPagecount(pPager, &nPage);
 1.42244 +      if( rc ) goto failed;
 1.42245 +
 1.42246 +      if( nPage>0 ){
 1.42247 +        IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
 1.42248 +        rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
 1.42249 +        if( rc!=SQLITE_OK ){
 1.42250 +          goto failed;
 1.42251 +        }
 1.42252 +      }else{
 1.42253 +        memset(dbFileVers, 0, sizeof(dbFileVers));
 1.42254 +      }
 1.42255 +
 1.42256 +      if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
 1.42257 +        pager_reset(pPager);
 1.42258 +      }
 1.42259 +    }
 1.42260 +
 1.42261 +    /* If there is a WAL file in the file-system, open this database in WAL
 1.42262 +    ** mode. Otherwise, the following function call is a no-op.
 1.42263 +    */
 1.42264 +    rc = pagerOpenWalIfPresent(pPager);
 1.42265 +#ifndef SQLITE_OMIT_WAL
 1.42266 +    assert( pPager->pWal==0 || rc==SQLITE_OK );
 1.42267 +#endif
 1.42268 +  }
 1.42269 +
 1.42270 +  if( pagerUseWal(pPager) ){
 1.42271 +    assert( rc==SQLITE_OK );
 1.42272 +    rc = pagerBeginReadTransaction(pPager);
 1.42273 +  }
 1.42274 +
 1.42275 +  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
 1.42276 +    rc = pagerPagecount(pPager, &pPager->dbSize);
 1.42277 +  }
 1.42278 +
 1.42279 + failed:
 1.42280 +  if( rc!=SQLITE_OK ){
 1.42281 +    assert( !MEMDB );
 1.42282 +    pager_unlock(pPager);
 1.42283 +    assert( pPager->eState==PAGER_OPEN );
 1.42284 +  }else{
 1.42285 +    pPager->eState = PAGER_READER;
 1.42286 +  }
 1.42287 +  return rc;
 1.42288 +}
 1.42289 +
 1.42290 +/*
 1.42291 +** If the reference count has reached zero, rollback any active
 1.42292 +** transaction and unlock the pager.
 1.42293 +**
 1.42294 +** Except, in locking_mode=EXCLUSIVE when there is nothing to in
 1.42295 +** the rollback journal, the unlock is not performed and there is
 1.42296 +** nothing to rollback, so this routine is a no-op.
 1.42297 +*/ 
 1.42298 +static void pagerUnlockIfUnused(Pager *pPager){
 1.42299 +  if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
 1.42300 +    pagerUnlockAndRollback(pPager);
 1.42301 +  }
 1.42302 +}
 1.42303 +
 1.42304 +/*
 1.42305 +** Acquire a reference to page number pgno in pager pPager (a page
 1.42306 +** reference has type DbPage*). If the requested reference is 
 1.42307 +** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
 1.42308 +**
 1.42309 +** If the requested page is already in the cache, it is returned. 
 1.42310 +** Otherwise, a new page object is allocated and populated with data
 1.42311 +** read from the database file. In some cases, the pcache module may
 1.42312 +** choose not to allocate a new page object and may reuse an existing
 1.42313 +** object with no outstanding references.
 1.42314 +**
 1.42315 +** The extra data appended to a page is always initialized to zeros the 
 1.42316 +** first time a page is loaded into memory. If the page requested is 
 1.42317 +** already in the cache when this function is called, then the extra
 1.42318 +** data is left as it was when the page object was last used.
 1.42319 +**
 1.42320 +** If the database image is smaller than the requested page or if a 
 1.42321 +** non-zero value is passed as the noContent parameter and the 
 1.42322 +** requested page is not already stored in the cache, then no 
 1.42323 +** actual disk read occurs. In this case the memory image of the 
 1.42324 +** page is initialized to all zeros. 
 1.42325 +**
 1.42326 +** If noContent is true, it means that we do not care about the contents
 1.42327 +** of the page. This occurs in two seperate scenarios:
 1.42328 +**
 1.42329 +**   a) When reading a free-list leaf page from the database, and
 1.42330 +**
 1.42331 +**   b) When a savepoint is being rolled back and we need to load
 1.42332 +**      a new page into the cache to be filled with the data read
 1.42333 +**      from the savepoint journal.
 1.42334 +**
 1.42335 +** If noContent is true, then the data returned is zeroed instead of
 1.42336 +** being read from the database. Additionally, the bits corresponding
 1.42337 +** to pgno in Pager.pInJournal (bitvec of pages already written to the
 1.42338 +** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
 1.42339 +** savepoints are set. This means if the page is made writable at any
 1.42340 +** point in the future, using a call to sqlite3PagerWrite(), its contents
 1.42341 +** will not be journaled. This saves IO.
 1.42342 +**
 1.42343 +** The acquisition might fail for several reasons.  In all cases,
 1.42344 +** an appropriate error code is returned and *ppPage is set to NULL.
 1.42345 +**
 1.42346 +** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
 1.42347 +** to find a page in the in-memory cache first.  If the page is not already
 1.42348 +** in memory, this routine goes to disk to read it in whereas Lookup()
 1.42349 +** just returns 0.  This routine acquires a read-lock the first time it
 1.42350 +** has to go to disk, and could also playback an old journal if necessary.
 1.42351 +** Since Lookup() never goes to disk, it never has to deal with locks
 1.42352 +** or journal files.
 1.42353 +*/
 1.42354 +SQLITE_PRIVATE int sqlite3PagerAcquire(
 1.42355 +  Pager *pPager,      /* The pager open on the database file */
 1.42356 +  Pgno pgno,          /* Page number to fetch */
 1.42357 +  DbPage **ppPage,    /* Write a pointer to the page here */
 1.42358 +  int noContent       /* Do not bother reading content from disk if true */
 1.42359 +){
 1.42360 +  int rc;
 1.42361 +  PgHdr *pPg;
 1.42362 +
 1.42363 +  assert( pPager->eState>=PAGER_READER );
 1.42364 +  assert( assert_pager_state(pPager) );
 1.42365 +
 1.42366 +  if( pgno==0 ){
 1.42367 +    return SQLITE_CORRUPT_BKPT;
 1.42368 +  }
 1.42369 +
 1.42370 +  /* If the pager is in the error state, return an error immediately. 
 1.42371 +  ** Otherwise, request the page from the PCache layer. */
 1.42372 +  if( pPager->errCode!=SQLITE_OK ){
 1.42373 +    rc = pPager->errCode;
 1.42374 +  }else{
 1.42375 +    rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
 1.42376 +  }
 1.42377 +
 1.42378 +  if( rc!=SQLITE_OK ){
 1.42379 +    /* Either the call to sqlite3PcacheFetch() returned an error or the
 1.42380 +    ** pager was already in the error-state when this function was called.
 1.42381 +    ** Set pPg to 0 and jump to the exception handler.  */
 1.42382 +    pPg = 0;
 1.42383 +    goto pager_acquire_err;
 1.42384 +  }
 1.42385 +  assert( (*ppPage)->pgno==pgno );
 1.42386 +  assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
 1.42387 +
 1.42388 +  if( (*ppPage)->pPager && !noContent ){
 1.42389 +    /* In this case the pcache already contains an initialized copy of
 1.42390 +    ** the page. Return without further ado.  */
 1.42391 +    assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
 1.42392 +    pPager->aStat[PAGER_STAT_HIT]++;
 1.42393 +    return SQLITE_OK;
 1.42394 +
 1.42395 +  }else{
 1.42396 +    /* The pager cache has created a new page. Its content needs to 
 1.42397 +    ** be initialized.  */
 1.42398 +
 1.42399 +    pPg = *ppPage;
 1.42400 +    pPg->pPager = pPager;
 1.42401 +
 1.42402 +    /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
 1.42403 +    ** number greater than this, or the unused locking-page, is requested. */
 1.42404 +    if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
 1.42405 +      rc = SQLITE_CORRUPT_BKPT;
 1.42406 +      goto pager_acquire_err;
 1.42407 +    }
 1.42408 +
 1.42409 +    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
 1.42410 +      if( pgno>pPager->mxPgno ){
 1.42411 +        rc = SQLITE_FULL;
 1.42412 +        goto pager_acquire_err;
 1.42413 +      }
 1.42414 +      if( noContent ){
 1.42415 +        /* Failure to set the bits in the InJournal bit-vectors is benign.
 1.42416 +        ** It merely means that we might do some extra work to journal a 
 1.42417 +        ** page that does not need to be journaled.  Nevertheless, be sure 
 1.42418 +        ** to test the case where a malloc error occurs while trying to set 
 1.42419 +        ** a bit in a bit vector.
 1.42420 +        */
 1.42421 +        sqlite3BeginBenignMalloc();
 1.42422 +        if( pgno<=pPager->dbOrigSize ){
 1.42423 +          TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
 1.42424 +          testcase( rc==SQLITE_NOMEM );
 1.42425 +        }
 1.42426 +        TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
 1.42427 +        testcase( rc==SQLITE_NOMEM );
 1.42428 +        sqlite3EndBenignMalloc();
 1.42429 +      }
 1.42430 +      memset(pPg->pData, 0, pPager->pageSize);
 1.42431 +      IOTRACE(("ZERO %p %d\n", pPager, pgno));
 1.42432 +    }else{
 1.42433 +      assert( pPg->pPager==pPager );
 1.42434 +      pPager->aStat[PAGER_STAT_MISS]++;
 1.42435 +      rc = readDbPage(pPg);
 1.42436 +      if( rc!=SQLITE_OK ){
 1.42437 +        goto pager_acquire_err;
 1.42438 +      }
 1.42439 +    }
 1.42440 +    pager_set_pagehash(pPg);
 1.42441 +  }
 1.42442 +
 1.42443 +  return SQLITE_OK;
 1.42444 +
 1.42445 +pager_acquire_err:
 1.42446 +  assert( rc!=SQLITE_OK );
 1.42447 +  if( pPg ){
 1.42448 +    sqlite3PcacheDrop(pPg);
 1.42449 +  }
 1.42450 +  pagerUnlockIfUnused(pPager);
 1.42451 +
 1.42452 +  *ppPage = 0;
 1.42453 +  return rc;
 1.42454 +}
 1.42455 +
 1.42456 +/*
 1.42457 +** Acquire a page if it is already in the in-memory cache.  Do
 1.42458 +** not read the page from disk.  Return a pointer to the page,
 1.42459 +** or 0 if the page is not in cache. 
 1.42460 +**
 1.42461 +** See also sqlite3PagerGet().  The difference between this routine
 1.42462 +** and sqlite3PagerGet() is that _get() will go to the disk and read
 1.42463 +** in the page if the page is not already in cache.  This routine
 1.42464 +** returns NULL if the page is not in cache or if a disk I/O error 
 1.42465 +** has ever happened.
 1.42466 +*/
 1.42467 +SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
 1.42468 +  PgHdr *pPg = 0;
 1.42469 +  assert( pPager!=0 );
 1.42470 +  assert( pgno!=0 );
 1.42471 +  assert( pPager->pPCache!=0 );
 1.42472 +  assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
 1.42473 +  sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
 1.42474 +  return pPg;
 1.42475 +}
 1.42476 +
 1.42477 +/*
 1.42478 +** Release a page reference.
 1.42479 +**
 1.42480 +** If the number of references to the page drop to zero, then the
 1.42481 +** page is added to the LRU list.  When all references to all pages
 1.42482 +** are released, a rollback occurs and the lock on the database is
 1.42483 +** removed.
 1.42484 +*/
 1.42485 +SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
 1.42486 +  if( pPg ){
 1.42487 +    Pager *pPager = pPg->pPager;
 1.42488 +    sqlite3PcacheRelease(pPg);
 1.42489 +    pagerUnlockIfUnused(pPager);
 1.42490 +  }
 1.42491 +}
 1.42492 +
 1.42493 +/*
 1.42494 +** This function is called at the start of every write transaction.
 1.42495 +** There must already be a RESERVED or EXCLUSIVE lock on the database 
 1.42496 +** file when this routine is called.
 1.42497 +**
 1.42498 +** Open the journal file for pager pPager and write a journal header
 1.42499 +** to the start of it. If there are active savepoints, open the sub-journal
 1.42500 +** as well. This function is only used when the journal file is being 
 1.42501 +** opened to write a rollback log for a transaction. It is not used 
 1.42502 +** when opening a hot journal file to roll it back.
 1.42503 +**
 1.42504 +** If the journal file is already open (as it may be in exclusive mode),
 1.42505 +** then this function just writes a journal header to the start of the
 1.42506 +** already open file. 
 1.42507 +**
 1.42508 +** Whether or not the journal file is opened by this function, the
 1.42509 +** Pager.pInJournal bitvec structure is allocated.
 1.42510 +**
 1.42511 +** Return SQLITE_OK if everything is successful. Otherwise, return 
 1.42512 +** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or 
 1.42513 +** an IO error code if opening or writing the journal file fails.
 1.42514 +*/
 1.42515 +static int pager_open_journal(Pager *pPager){
 1.42516 +  int rc = SQLITE_OK;                        /* Return code */
 1.42517 +  sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
 1.42518 +
 1.42519 +  assert( pPager->eState==PAGER_WRITER_LOCKED );
 1.42520 +  assert( assert_pager_state(pPager) );
 1.42521 +  assert( pPager->pInJournal==0 );
 1.42522 +  
 1.42523 +  /* If already in the error state, this function is a no-op.  But on
 1.42524 +  ** the other hand, this routine is never called if we are already in
 1.42525 +  ** an error state. */
 1.42526 +  if( NEVER(pPager->errCode) ) return pPager->errCode;
 1.42527 +
 1.42528 +  if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
 1.42529 +    pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
 1.42530 +    if( pPager->pInJournal==0 ){
 1.42531 +      return SQLITE_NOMEM;
 1.42532 +    }
 1.42533 +  
 1.42534 +    /* Open the journal file if it is not already open. */
 1.42535 +    if( !isOpen(pPager->jfd) ){
 1.42536 +      if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
 1.42537 +        sqlite3MemJournalOpen(pPager->jfd);
 1.42538 +      }else{
 1.42539 +        const int flags =                   /* VFS flags to open journal file */
 1.42540 +          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
 1.42541 +          (pPager->tempFile ? 
 1.42542 +            (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
 1.42543 +            (SQLITE_OPEN_MAIN_JOURNAL)
 1.42544 +          );
 1.42545 +  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 1.42546 +        rc = sqlite3JournalOpen(
 1.42547 +            pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
 1.42548 +        );
 1.42549 +  #else
 1.42550 +        rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
 1.42551 +  #endif
 1.42552 +      }
 1.42553 +      assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
 1.42554 +    }
 1.42555 +  
 1.42556 +  
 1.42557 +    /* Write the first journal header to the journal file and open 
 1.42558 +    ** the sub-journal if necessary.
 1.42559 +    */
 1.42560 +    if( rc==SQLITE_OK ){
 1.42561 +      /* TODO: Check if all of these are really required. */
 1.42562 +      pPager->nRec = 0;
 1.42563 +      pPager->journalOff = 0;
 1.42564 +      pPager->setMaster = 0;
 1.42565 +      pPager->journalHdr = 0;
 1.42566 +      rc = writeJournalHdr(pPager);
 1.42567 +    }
 1.42568 +  }
 1.42569 +
 1.42570 +  if( rc!=SQLITE_OK ){
 1.42571 +    sqlite3BitvecDestroy(pPager->pInJournal);
 1.42572 +    pPager->pInJournal = 0;
 1.42573 +  }else{
 1.42574 +    assert( pPager->eState==PAGER_WRITER_LOCKED );
 1.42575 +    pPager->eState = PAGER_WRITER_CACHEMOD;
 1.42576 +  }
 1.42577 +
 1.42578 +  return rc;
 1.42579 +}
 1.42580 +
 1.42581 +/*
 1.42582 +** Begin a write-transaction on the specified pager object. If a 
 1.42583 +** write-transaction has already been opened, this function is a no-op.
 1.42584 +**
 1.42585 +** If the exFlag argument is false, then acquire at least a RESERVED
 1.42586 +** lock on the database file. If exFlag is true, then acquire at least
 1.42587 +** an EXCLUSIVE lock. If such a lock is already held, no locking 
 1.42588 +** functions need be called.
 1.42589 +**
 1.42590 +** If the subjInMemory argument is non-zero, then any sub-journal opened
 1.42591 +** within this transaction will be opened as an in-memory file. This
 1.42592 +** has no effect if the sub-journal is already opened (as it may be when
 1.42593 +** running in exclusive mode) or if the transaction does not require a
 1.42594 +** sub-journal. If the subjInMemory argument is zero, then any required
 1.42595 +** sub-journal is implemented in-memory if pPager is an in-memory database, 
 1.42596 +** or using a temporary file otherwise.
 1.42597 +*/
 1.42598 +SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
 1.42599 +  int rc = SQLITE_OK;
 1.42600 +
 1.42601 +  if( pPager->errCode ) return pPager->errCode;
 1.42602 +  assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
 1.42603 +  pPager->subjInMemory = (u8)subjInMemory;
 1.42604 +
 1.42605 +  if( ALWAYS(pPager->eState==PAGER_READER) ){
 1.42606 +    assert( pPager->pInJournal==0 );
 1.42607 +
 1.42608 +    if( pagerUseWal(pPager) ){
 1.42609 +      /* If the pager is configured to use locking_mode=exclusive, and an
 1.42610 +      ** exclusive lock on the database is not already held, obtain it now.
 1.42611 +      */
 1.42612 +      if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
 1.42613 +        rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 1.42614 +        if( rc!=SQLITE_OK ){
 1.42615 +          return rc;
 1.42616 +        }
 1.42617 +        sqlite3WalExclusiveMode(pPager->pWal, 1);
 1.42618 +      }
 1.42619 +
 1.42620 +      /* Grab the write lock on the log file. If successful, upgrade to
 1.42621 +      ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
 1.42622 +      ** The busy-handler is not invoked if another connection already
 1.42623 +      ** holds the write-lock. If possible, the upper layer will call it.
 1.42624 +      */
 1.42625 +      rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
 1.42626 +    }else{
 1.42627 +      /* Obtain a RESERVED lock on the database file. If the exFlag parameter
 1.42628 +      ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
 1.42629 +      ** busy-handler callback can be used when upgrading to the EXCLUSIVE
 1.42630 +      ** lock, but not when obtaining the RESERVED lock.
 1.42631 +      */
 1.42632 +      rc = pagerLockDb(pPager, RESERVED_LOCK);
 1.42633 +      if( rc==SQLITE_OK && exFlag ){
 1.42634 +        rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
 1.42635 +      }
 1.42636 +    }
 1.42637 +
 1.42638 +    if( rc==SQLITE_OK ){
 1.42639 +      /* Change to WRITER_LOCKED state.
 1.42640 +      **
 1.42641 +      ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
 1.42642 +      ** when it has an open transaction, but never to DBMOD or FINISHED.
 1.42643 +      ** This is because in those states the code to roll back savepoint 
 1.42644 +      ** transactions may copy data from the sub-journal into the database 
 1.42645 +      ** file as well as into the page cache. Which would be incorrect in 
 1.42646 +      ** WAL mode.
 1.42647 +      */
 1.42648 +      pPager->eState = PAGER_WRITER_LOCKED;
 1.42649 +      pPager->dbHintSize = pPager->dbSize;
 1.42650 +      pPager->dbFileSize = pPager->dbSize;
 1.42651 +      pPager->dbOrigSize = pPager->dbSize;
 1.42652 +      pPager->journalOff = 0;
 1.42653 +    }
 1.42654 +
 1.42655 +    assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
 1.42656 +    assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
 1.42657 +    assert( assert_pager_state(pPager) );
 1.42658 +  }
 1.42659 +
 1.42660 +  PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
 1.42661 +  return rc;
 1.42662 +}
 1.42663 +
 1.42664 +/*
 1.42665 +** Mark a single data page as writeable. The page is written into the 
 1.42666 +** main journal or sub-journal as required. If the page is written into
 1.42667 +** one of the journals, the corresponding bit is set in the 
 1.42668 +** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
 1.42669 +** of any open savepoints as appropriate.
 1.42670 +*/
 1.42671 +static int pager_write(PgHdr *pPg){
 1.42672 +  void *pData = pPg->pData;
 1.42673 +  Pager *pPager = pPg->pPager;
 1.42674 +  int rc = SQLITE_OK;
 1.42675 +
 1.42676 +  /* This routine is not called unless a write-transaction has already 
 1.42677 +  ** been started. The journal file may or may not be open at this point.
 1.42678 +  ** It is never called in the ERROR state.
 1.42679 +  */
 1.42680 +  assert( pPager->eState==PAGER_WRITER_LOCKED
 1.42681 +       || pPager->eState==PAGER_WRITER_CACHEMOD
 1.42682 +       || pPager->eState==PAGER_WRITER_DBMOD
 1.42683 +  );
 1.42684 +  assert( assert_pager_state(pPager) );
 1.42685 +
 1.42686 +  /* If an error has been previously detected, report the same error
 1.42687 +  ** again. This should not happen, but the check provides robustness. */
 1.42688 +  if( NEVER(pPager->errCode) )  return pPager->errCode;
 1.42689 +
 1.42690 +  /* Higher-level routines never call this function if database is not
 1.42691 +  ** writable.  But check anyway, just for robustness. */
 1.42692 +  if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
 1.42693 +
 1.42694 +  CHECK_PAGE(pPg);
 1.42695 +
 1.42696 +  /* The journal file needs to be opened. Higher level routines have already
 1.42697 +  ** obtained the necessary locks to begin the write-transaction, but the
 1.42698 +  ** rollback journal might not yet be open. Open it now if this is the case.
 1.42699 +  **
 1.42700 +  ** This is done before calling sqlite3PcacheMakeDirty() on the page. 
 1.42701 +  ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
 1.42702 +  ** an error might occur and the pager would end up in WRITER_LOCKED state
 1.42703 +  ** with pages marked as dirty in the cache.
 1.42704 +  */
 1.42705 +  if( pPager->eState==PAGER_WRITER_LOCKED ){
 1.42706 +    rc = pager_open_journal(pPager);
 1.42707 +    if( rc!=SQLITE_OK ) return rc;
 1.42708 +  }
 1.42709 +  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
 1.42710 +  assert( assert_pager_state(pPager) );
 1.42711 +
 1.42712 +  /* Mark the page as dirty.  If the page has already been written
 1.42713 +  ** to the journal then we can return right away.
 1.42714 +  */
 1.42715 +  sqlite3PcacheMakeDirty(pPg);
 1.42716 +  if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
 1.42717 +    assert( !pagerUseWal(pPager) );
 1.42718 +  }else{
 1.42719 +  
 1.42720 +    /* The transaction journal now exists and we have a RESERVED or an
 1.42721 +    ** EXCLUSIVE lock on the main database file.  Write the current page to
 1.42722 +    ** the transaction journal if it is not there already.
 1.42723 +    */
 1.42724 +    if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
 1.42725 +      assert( pagerUseWal(pPager)==0 );
 1.42726 +      if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
 1.42727 +        u32 cksum;
 1.42728 +        char *pData2;
 1.42729 +        i64 iOff = pPager->journalOff;
 1.42730 +
 1.42731 +        /* We should never write to the journal file the page that
 1.42732 +        ** contains the database locks.  The following assert verifies
 1.42733 +        ** that we do not. */
 1.42734 +        assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
 1.42735 +
 1.42736 +        assert( pPager->journalHdr<=pPager->journalOff );
 1.42737 +        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
 1.42738 +        cksum = pager_cksum(pPager, (u8*)pData2);
 1.42739 +
 1.42740 +        /* Even if an IO or diskfull error occurs while journalling the
 1.42741 +        ** page in the block above, set the need-sync flag for the page.
 1.42742 +        ** Otherwise, when the transaction is rolled back, the logic in
 1.42743 +        ** playback_one_page() will think that the page needs to be restored
 1.42744 +        ** in the database file. And if an IO error occurs while doing so,
 1.42745 +        ** then corruption may follow.
 1.42746 +        */
 1.42747 +        pPg->flags |= PGHDR_NEED_SYNC;
 1.42748 +
 1.42749 +        rc = write32bits(pPager->jfd, iOff, pPg->pgno);
 1.42750 +        if( rc!=SQLITE_OK ) return rc;
 1.42751 +        rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
 1.42752 +        if( rc!=SQLITE_OK ) return rc;
 1.42753 +        rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
 1.42754 +        if( rc!=SQLITE_OK ) return rc;
 1.42755 +
 1.42756 +        IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
 1.42757 +                 pPager->journalOff, pPager->pageSize));
 1.42758 +        PAGER_INCR(sqlite3_pager_writej_count);
 1.42759 +        PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
 1.42760 +             PAGERID(pPager), pPg->pgno, 
 1.42761 +             ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
 1.42762 +
 1.42763 +        pPager->journalOff += 8 + pPager->pageSize;
 1.42764 +        pPager->nRec++;
 1.42765 +        assert( pPager->pInJournal!=0 );
 1.42766 +        rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
 1.42767 +        testcase( rc==SQLITE_NOMEM );
 1.42768 +        assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
 1.42769 +        rc |= addToSavepointBitvecs(pPager, pPg->pgno);
 1.42770 +        if( rc!=SQLITE_OK ){
 1.42771 +          assert( rc==SQLITE_NOMEM );
 1.42772 +          return rc;
 1.42773 +        }
 1.42774 +      }else{
 1.42775 +        if( pPager->eState!=PAGER_WRITER_DBMOD ){
 1.42776 +          pPg->flags |= PGHDR_NEED_SYNC;
 1.42777 +        }
 1.42778 +        PAGERTRACE(("APPEND %d page %d needSync=%d\n",
 1.42779 +                PAGERID(pPager), pPg->pgno,
 1.42780 +               ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
 1.42781 +      }
 1.42782 +    }
 1.42783 +  
 1.42784 +    /* If the statement journal is open and the page is not in it,
 1.42785 +    ** then write the current page to the statement journal.  Note that
 1.42786 +    ** the statement journal format differs from the standard journal format
 1.42787 +    ** in that it omits the checksums and the header.
 1.42788 +    */
 1.42789 +    if( subjRequiresPage(pPg) ){
 1.42790 +      rc = subjournalPage(pPg);
 1.42791 +    }
 1.42792 +  }
 1.42793 +
 1.42794 +  /* Update the database size and return.
 1.42795 +  */
 1.42796 +  if( pPager->dbSize<pPg->pgno ){
 1.42797 +    pPager->dbSize = pPg->pgno;
 1.42798 +  }
 1.42799 +  return rc;
 1.42800 +}
 1.42801 +
 1.42802 +/*
 1.42803 +** Mark a data page as writeable. This routine must be called before 
 1.42804 +** making changes to a page. The caller must check the return value 
 1.42805 +** of this function and be careful not to change any page data unless 
 1.42806 +** this routine returns SQLITE_OK.
 1.42807 +**
 1.42808 +** The difference between this function and pager_write() is that this
 1.42809 +** function also deals with the special case where 2 or more pages
 1.42810 +** fit on a single disk sector. In this case all co-resident pages
 1.42811 +** must have been written to the journal file before returning.
 1.42812 +**
 1.42813 +** If an error occurs, SQLITE_NOMEM or an IO error code is returned
 1.42814 +** as appropriate. Otherwise, SQLITE_OK.
 1.42815 +*/
 1.42816 +SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
 1.42817 +  int rc = SQLITE_OK;
 1.42818 +
 1.42819 +  PgHdr *pPg = pDbPage;
 1.42820 +  Pager *pPager = pPg->pPager;
 1.42821 +  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
 1.42822 +
 1.42823 +  assert( pPager->eState>=PAGER_WRITER_LOCKED );
 1.42824 +  assert( pPager->eState!=PAGER_ERROR );
 1.42825 +  assert( assert_pager_state(pPager) );
 1.42826 +
 1.42827 +  if( nPagePerSector>1 ){
 1.42828 +    Pgno nPageCount;          /* Total number of pages in database file */
 1.42829 +    Pgno pg1;                 /* First page of the sector pPg is located on. */
 1.42830 +    int nPage = 0;            /* Number of pages starting at pg1 to journal */
 1.42831 +    int ii;                   /* Loop counter */
 1.42832 +    int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
 1.42833 +
 1.42834 +    /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
 1.42835 +    ** a journal header to be written between the pages journaled by
 1.42836 +    ** this function.
 1.42837 +    */
 1.42838 +    assert( !MEMDB );
 1.42839 +    assert( pPager->doNotSyncSpill==0 );
 1.42840 +    pPager->doNotSyncSpill++;
 1.42841 +
 1.42842 +    /* This trick assumes that both the page-size and sector-size are
 1.42843 +    ** an integer power of 2. It sets variable pg1 to the identifier
 1.42844 +    ** of the first page of the sector pPg is located on.
 1.42845 +    */
 1.42846 +    pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
 1.42847 +
 1.42848 +    nPageCount = pPager->dbSize;
 1.42849 +    if( pPg->pgno>nPageCount ){
 1.42850 +      nPage = (pPg->pgno - pg1)+1;
 1.42851 +    }else if( (pg1+nPagePerSector-1)>nPageCount ){
 1.42852 +      nPage = nPageCount+1-pg1;
 1.42853 +    }else{
 1.42854 +      nPage = nPagePerSector;
 1.42855 +    }
 1.42856 +    assert(nPage>0);
 1.42857 +    assert(pg1<=pPg->pgno);
 1.42858 +    assert((pg1+nPage)>pPg->pgno);
 1.42859 +
 1.42860 +    for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
 1.42861 +      Pgno pg = pg1+ii;
 1.42862 +      PgHdr *pPage;
 1.42863 +      if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
 1.42864 +        if( pg!=PAGER_MJ_PGNO(pPager) ){
 1.42865 +          rc = sqlite3PagerGet(pPager, pg, &pPage);
 1.42866 +          if( rc==SQLITE_OK ){
 1.42867 +            rc = pager_write(pPage);
 1.42868 +            if( pPage->flags&PGHDR_NEED_SYNC ){
 1.42869 +              needSync = 1;
 1.42870 +            }
 1.42871 +            sqlite3PagerUnref(pPage);
 1.42872 +          }
 1.42873 +        }
 1.42874 +      }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
 1.42875 +        if( pPage->flags&PGHDR_NEED_SYNC ){
 1.42876 +          needSync = 1;
 1.42877 +        }
 1.42878 +        sqlite3PagerUnref(pPage);
 1.42879 +      }
 1.42880 +    }
 1.42881 +
 1.42882 +    /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
 1.42883 +    ** starting at pg1, then it needs to be set for all of them. Because
 1.42884 +    ** writing to any of these nPage pages may damage the others, the
 1.42885 +    ** journal file must contain sync()ed copies of all of them
 1.42886 +    ** before any of them can be written out to the database file.
 1.42887 +    */
 1.42888 +    if( rc==SQLITE_OK && needSync ){
 1.42889 +      assert( !MEMDB );
 1.42890 +      for(ii=0; ii<nPage; ii++){
 1.42891 +        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
 1.42892 +        if( pPage ){
 1.42893 +          pPage->flags |= PGHDR_NEED_SYNC;
 1.42894 +          sqlite3PagerUnref(pPage);
 1.42895 +        }
 1.42896 +      }
 1.42897 +    }
 1.42898 +
 1.42899 +    assert( pPager->doNotSyncSpill==1 );
 1.42900 +    pPager->doNotSyncSpill--;
 1.42901 +  }else{
 1.42902 +    rc = pager_write(pDbPage);
 1.42903 +  }
 1.42904 +  return rc;
 1.42905 +}
 1.42906 +
 1.42907 +/*
 1.42908 +** Return TRUE if the page given in the argument was previously passed
 1.42909 +** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
 1.42910 +** to change the content of the page.
 1.42911 +*/
 1.42912 +#ifndef NDEBUG
 1.42913 +SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
 1.42914 +  return pPg->flags&PGHDR_DIRTY;
 1.42915 +}
 1.42916 +#endif
 1.42917 +
 1.42918 +/*
 1.42919 +** A call to this routine tells the pager that it is not necessary to
 1.42920 +** write the information on page pPg back to the disk, even though
 1.42921 +** that page might be marked as dirty.  This happens, for example, when
 1.42922 +** the page has been added as a leaf of the freelist and so its
 1.42923 +** content no longer matters.
 1.42924 +**
 1.42925 +** The overlying software layer calls this routine when all of the data
 1.42926 +** on the given page is unused. The pager marks the page as clean so
 1.42927 +** that it does not get written to disk.
 1.42928 +**
 1.42929 +** Tests show that this optimization can quadruple the speed of large 
 1.42930 +** DELETE operations.
 1.42931 +*/
 1.42932 +SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
 1.42933 +  Pager *pPager = pPg->pPager;
 1.42934 +  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
 1.42935 +    PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
 1.42936 +    IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
 1.42937 +    pPg->flags |= PGHDR_DONT_WRITE;
 1.42938 +    pager_set_pagehash(pPg);
 1.42939 +  }
 1.42940 +}
 1.42941 +
 1.42942 +/*
 1.42943 +** This routine is called to increment the value of the database file 
 1.42944 +** change-counter, stored as a 4-byte big-endian integer starting at 
 1.42945 +** byte offset 24 of the pager file.  The secondary change counter at
 1.42946 +** 92 is also updated, as is the SQLite version number at offset 96.
 1.42947 +**
 1.42948 +** But this only happens if the pPager->changeCountDone flag is false.
 1.42949 +** To avoid excess churning of page 1, the update only happens once.
 1.42950 +** See also the pager_write_changecounter() routine that does an 
 1.42951 +** unconditional update of the change counters.
 1.42952 +**
 1.42953 +** If the isDirectMode flag is zero, then this is done by calling 
 1.42954 +** sqlite3PagerWrite() on page 1, then modifying the contents of the
 1.42955 +** page data. In this case the file will be updated when the current
 1.42956 +** transaction is committed.
 1.42957 +**
 1.42958 +** The isDirectMode flag may only be non-zero if the library was compiled
 1.42959 +** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
 1.42960 +** if isDirect is non-zero, then the database file is updated directly
 1.42961 +** by writing an updated version of page 1 using a call to the 
 1.42962 +** sqlite3OsWrite() function.
 1.42963 +*/
 1.42964 +static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
 1.42965 +  int rc = SQLITE_OK;
 1.42966 +
 1.42967 +  assert( pPager->eState==PAGER_WRITER_CACHEMOD
 1.42968 +       || pPager->eState==PAGER_WRITER_DBMOD
 1.42969 +  );
 1.42970 +  assert( assert_pager_state(pPager) );
 1.42971 +
 1.42972 +  /* Declare and initialize constant integer 'isDirect'. If the
 1.42973 +  ** atomic-write optimization is enabled in this build, then isDirect
 1.42974 +  ** is initialized to the value passed as the isDirectMode parameter
 1.42975 +  ** to this function. Otherwise, it is always set to zero.
 1.42976 +  **
 1.42977 +  ** The idea is that if the atomic-write optimization is not
 1.42978 +  ** enabled at compile time, the compiler can omit the tests of
 1.42979 +  ** 'isDirect' below, as well as the block enclosed in the
 1.42980 +  ** "if( isDirect )" condition.
 1.42981 +  */
 1.42982 +#ifndef SQLITE_ENABLE_ATOMIC_WRITE
 1.42983 +# define DIRECT_MODE 0
 1.42984 +  assert( isDirectMode==0 );
 1.42985 +  UNUSED_PARAMETER(isDirectMode);
 1.42986 +#else
 1.42987 +# define DIRECT_MODE isDirectMode
 1.42988 +#endif
 1.42989 +
 1.42990 +  if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
 1.42991 +    PgHdr *pPgHdr;                /* Reference to page 1 */
 1.42992 +
 1.42993 +    assert( !pPager->tempFile && isOpen(pPager->fd) );
 1.42994 +
 1.42995 +    /* Open page 1 of the file for writing. */
 1.42996 +    rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
 1.42997 +    assert( pPgHdr==0 || rc==SQLITE_OK );
 1.42998 +
 1.42999 +    /* If page one was fetched successfully, and this function is not
 1.43000 +    ** operating in direct-mode, make page 1 writable.  When not in 
 1.43001 +    ** direct mode, page 1 is always held in cache and hence the PagerGet()
 1.43002 +    ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
 1.43003 +    */
 1.43004 +    if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
 1.43005 +      rc = sqlite3PagerWrite(pPgHdr);
 1.43006 +    }
 1.43007 +
 1.43008 +    if( rc==SQLITE_OK ){
 1.43009 +      /* Actually do the update of the change counter */
 1.43010 +      pager_write_changecounter(pPgHdr);
 1.43011 +
 1.43012 +      /* If running in direct mode, write the contents of page 1 to the file. */
 1.43013 +      if( DIRECT_MODE ){
 1.43014 +        const void *zBuf;
 1.43015 +        assert( pPager->dbFileSize>0 );
 1.43016 +        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
 1.43017 +        if( rc==SQLITE_OK ){
 1.43018 +          rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
 1.43019 +          pPager->aStat[PAGER_STAT_WRITE]++;
 1.43020 +        }
 1.43021 +        if( rc==SQLITE_OK ){
 1.43022 +          pPager->changeCountDone = 1;
 1.43023 +        }
 1.43024 +      }else{
 1.43025 +        pPager->changeCountDone = 1;
 1.43026 +      }
 1.43027 +    }
 1.43028 +
 1.43029 +    /* Release the page reference. */
 1.43030 +    sqlite3PagerUnref(pPgHdr);
 1.43031 +  }
 1.43032 +  return rc;
 1.43033 +}
 1.43034 +
 1.43035 +/*
 1.43036 +** Sync the database file to disk. This is a no-op for in-memory databases
 1.43037 +** or pages with the Pager.noSync flag set.
 1.43038 +**
 1.43039 +** If successful, or if called on a pager for which it is a no-op, this
 1.43040 +** function returns SQLITE_OK. Otherwise, an IO error code is returned.
 1.43041 +*/
 1.43042 +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
 1.43043 +  int rc = SQLITE_OK;
 1.43044 +  if( !pPager->noSync ){
 1.43045 +    assert( !MEMDB );
 1.43046 +    rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
 1.43047 +  }else if( isOpen(pPager->fd) ){
 1.43048 +    assert( !MEMDB );
 1.43049 +    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
 1.43050 +    if( rc==SQLITE_NOTFOUND ){
 1.43051 +      rc = SQLITE_OK;
 1.43052 +    }
 1.43053 +  }
 1.43054 +  return rc;
 1.43055 +}
 1.43056 +
 1.43057 +/*
 1.43058 +** This function may only be called while a write-transaction is active in
 1.43059 +** rollback. If the connection is in WAL mode, this call is a no-op. 
 1.43060 +** Otherwise, if the connection does not already have an EXCLUSIVE lock on 
 1.43061 +** the database file, an attempt is made to obtain one.
 1.43062 +**
 1.43063 +** If the EXCLUSIVE lock is already held or the attempt to obtain it is
 1.43064 +** successful, or the connection is in WAL mode, SQLITE_OK is returned.
 1.43065 +** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is 
 1.43066 +** returned.
 1.43067 +*/
 1.43068 +SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
 1.43069 +  int rc = SQLITE_OK;
 1.43070 +  assert( pPager->eState==PAGER_WRITER_CACHEMOD 
 1.43071 +       || pPager->eState==PAGER_WRITER_DBMOD 
 1.43072 +       || pPager->eState==PAGER_WRITER_LOCKED 
 1.43073 +  );
 1.43074 +  assert( assert_pager_state(pPager) );
 1.43075 +  if( 0==pagerUseWal(pPager) ){
 1.43076 +    rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
 1.43077 +  }
 1.43078 +  return rc;
 1.43079 +}
 1.43080 +
 1.43081 +/*
 1.43082 +** Sync the database file for the pager pPager. zMaster points to the name
 1.43083 +** of a master journal file that should be written into the individual
 1.43084 +** journal file. zMaster may be NULL, which is interpreted as no master
 1.43085 +** journal (a single database transaction).
 1.43086 +**
 1.43087 +** This routine ensures that:
 1.43088 +**
 1.43089 +**   * The database file change-counter is updated,
 1.43090 +**   * the journal is synced (unless the atomic-write optimization is used),
 1.43091 +**   * all dirty pages are written to the database file, 
 1.43092 +**   * the database file is truncated (if required), and
 1.43093 +**   * the database file synced. 
 1.43094 +**
 1.43095 +** The only thing that remains to commit the transaction is to finalize 
 1.43096 +** (delete, truncate or zero the first part of) the journal file (or 
 1.43097 +** delete the master journal file if specified).
 1.43098 +**
 1.43099 +** Note that if zMaster==NULL, this does not overwrite a previous value
 1.43100 +** passed to an sqlite3PagerCommitPhaseOne() call.
 1.43101 +**
 1.43102 +** If the final parameter - noSync - is true, then the database file itself
 1.43103 +** is not synced. The caller must call sqlite3PagerSync() directly to
 1.43104 +** sync the database file before calling CommitPhaseTwo() to delete the
 1.43105 +** journal file in this case.
 1.43106 +*/
 1.43107 +SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
 1.43108 +  Pager *pPager,                  /* Pager object */
 1.43109 +  const char *zMaster,            /* If not NULL, the master journal name */
 1.43110 +  int noSync                      /* True to omit the xSync on the db file */
 1.43111 +){
 1.43112 +  int rc = SQLITE_OK;             /* Return code */
 1.43113 +
 1.43114 +  assert( pPager->eState==PAGER_WRITER_LOCKED
 1.43115 +       || pPager->eState==PAGER_WRITER_CACHEMOD
 1.43116 +       || pPager->eState==PAGER_WRITER_DBMOD
 1.43117 +       || pPager->eState==PAGER_ERROR
 1.43118 +  );
 1.43119 +  assert( assert_pager_state(pPager) );
 1.43120 +
 1.43121 +  /* If a prior error occurred, report that error again. */
 1.43122 +  if( NEVER(pPager->errCode) ) return pPager->errCode;
 1.43123 +
 1.43124 +  PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
 1.43125 +      pPager->zFilename, zMaster, pPager->dbSize));
 1.43126 +
 1.43127 +  /* If no database changes have been made, return early. */
 1.43128 +  if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
 1.43129 +
 1.43130 +  if( MEMDB ){
 1.43131 +    /* If this is an in-memory db, or no pages have been written to, or this
 1.43132 +    ** function has already been called, it is mostly a no-op.  However, any
 1.43133 +    ** backup in progress needs to be restarted.
 1.43134 +    */
 1.43135 +    sqlite3BackupRestart(pPager->pBackup);
 1.43136 +  }else{
 1.43137 +    if( pagerUseWal(pPager) ){
 1.43138 +      PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
 1.43139 +      PgHdr *pPageOne = 0;
 1.43140 +      if( pList==0 ){
 1.43141 +        /* Must have at least one page for the WAL commit flag.
 1.43142 +        ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
 1.43143 +        rc = sqlite3PagerGet(pPager, 1, &pPageOne);
 1.43144 +        pList = pPageOne;
 1.43145 +        pList->pDirty = 0;
 1.43146 +      }
 1.43147 +      assert( rc==SQLITE_OK );
 1.43148 +      if( ALWAYS(pList) ){
 1.43149 +        rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
 1.43150 +      }
 1.43151 +      sqlite3PagerUnref(pPageOne);
 1.43152 +      if( rc==SQLITE_OK ){
 1.43153 +        sqlite3PcacheCleanAll(pPager->pPCache);
 1.43154 +      }
 1.43155 +    }else{
 1.43156 +      /* The following block updates the change-counter. Exactly how it
 1.43157 +      ** does this depends on whether or not the atomic-update optimization
 1.43158 +      ** was enabled at compile time, and if this transaction meets the 
 1.43159 +      ** runtime criteria to use the operation: 
 1.43160 +      **
 1.43161 +      **    * The file-system supports the atomic-write property for
 1.43162 +      **      blocks of size page-size, and 
 1.43163 +      **    * This commit is not part of a multi-file transaction, and
 1.43164 +      **    * Exactly one page has been modified and store in the journal file.
 1.43165 +      **
 1.43166 +      ** If the optimization was not enabled at compile time, then the
 1.43167 +      ** pager_incr_changecounter() function is called to update the change
 1.43168 +      ** counter in 'indirect-mode'. If the optimization is compiled in but
 1.43169 +      ** is not applicable to this transaction, call sqlite3JournalCreate()
 1.43170 +      ** to make sure the journal file has actually been created, then call
 1.43171 +      ** pager_incr_changecounter() to update the change-counter in indirect
 1.43172 +      ** mode. 
 1.43173 +      **
 1.43174 +      ** Otherwise, if the optimization is both enabled and applicable,
 1.43175 +      ** then call pager_incr_changecounter() to update the change-counter
 1.43176 +      ** in 'direct' mode. In this case the journal file will never be
 1.43177 +      ** created for this transaction.
 1.43178 +      */
 1.43179 +  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 1.43180 +      PgHdr *pPg;
 1.43181 +      assert( isOpen(pPager->jfd) 
 1.43182 +           || pPager->journalMode==PAGER_JOURNALMODE_OFF 
 1.43183 +           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
 1.43184 +      );
 1.43185 +      if( !zMaster && isOpen(pPager->jfd) 
 1.43186 +       && pPager->journalOff==jrnlBufferSize(pPager) 
 1.43187 +       && pPager->dbSize>=pPager->dbOrigSize
 1.43188 +       && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
 1.43189 +      ){
 1.43190 +        /* Update the db file change counter via the direct-write method. The 
 1.43191 +        ** following call will modify the in-memory representation of page 1 
 1.43192 +        ** to include the updated change counter and then write page 1 
 1.43193 +        ** directly to the database file. Because of the atomic-write 
 1.43194 +        ** property of the host file-system, this is safe.
 1.43195 +        */
 1.43196 +        rc = pager_incr_changecounter(pPager, 1);
 1.43197 +      }else{
 1.43198 +        rc = sqlite3JournalCreate(pPager->jfd);
 1.43199 +        if( rc==SQLITE_OK ){
 1.43200 +          rc = pager_incr_changecounter(pPager, 0);
 1.43201 +        }
 1.43202 +      }
 1.43203 +  #else
 1.43204 +      rc = pager_incr_changecounter(pPager, 0);
 1.43205 +  #endif
 1.43206 +      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 1.43207 +  
 1.43208 +      /* If this transaction has made the database smaller, then all pages
 1.43209 +      ** being discarded by the truncation must be written to the journal
 1.43210 +      ** file.
 1.43211 +      **
 1.43212 +      ** Before reading the pages with page numbers larger than the 
 1.43213 +      ** current value of Pager.dbSize, set dbSize back to the value
 1.43214 +      ** that it took at the start of the transaction. Otherwise, the
 1.43215 +      ** calls to sqlite3PagerGet() return zeroed pages instead of 
 1.43216 +      ** reading data from the database file.
 1.43217 +      */
 1.43218 +      if( pPager->dbSize<pPager->dbOrigSize 
 1.43219 +       && pPager->journalMode!=PAGER_JOURNALMODE_OFF
 1.43220 +      ){
 1.43221 +        Pgno i;                                   /* Iterator variable */
 1.43222 +        const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
 1.43223 +        const Pgno dbSize = pPager->dbSize;       /* Database image size */ 
 1.43224 +        pPager->dbSize = pPager->dbOrigSize;
 1.43225 +        for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
 1.43226 +          if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
 1.43227 +            PgHdr *pPage;             /* Page to journal */
 1.43228 +            rc = sqlite3PagerGet(pPager, i, &pPage);
 1.43229 +            if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 1.43230 +            rc = sqlite3PagerWrite(pPage);
 1.43231 +            sqlite3PagerUnref(pPage);
 1.43232 +            if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 1.43233 +          }
 1.43234 +        }
 1.43235 +        pPager->dbSize = dbSize;
 1.43236 +      } 
 1.43237 +  
 1.43238 +      /* Write the master journal name into the journal file. If a master 
 1.43239 +      ** journal file name has already been written to the journal file, 
 1.43240 +      ** or if zMaster is NULL (no master journal), then this call is a no-op.
 1.43241 +      */
 1.43242 +      rc = writeMasterJournal(pPager, zMaster);
 1.43243 +      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 1.43244 +  
 1.43245 +      /* Sync the journal file and write all dirty pages to the database.
 1.43246 +      ** If the atomic-update optimization is being used, this sync will not 
 1.43247 +      ** create the journal file or perform any real IO.
 1.43248 +      **
 1.43249 +      ** Because the change-counter page was just modified, unless the
 1.43250 +      ** atomic-update optimization is used it is almost certain that the
 1.43251 +      ** journal requires a sync here. However, in locking_mode=exclusive
 1.43252 +      ** on a system under memory pressure it is just possible that this is 
 1.43253 +      ** not the case. In this case it is likely enough that the redundant
 1.43254 +      ** xSync() call will be changed to a no-op by the OS anyhow. 
 1.43255 +      */
 1.43256 +      rc = syncJournal(pPager, 0);
 1.43257 +      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 1.43258 +  
 1.43259 +      rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
 1.43260 +      if( rc!=SQLITE_OK ){
 1.43261 +        assert( rc!=SQLITE_IOERR_BLOCKED );
 1.43262 +        goto commit_phase_one_exit;
 1.43263 +      }
 1.43264 +      sqlite3PcacheCleanAll(pPager->pPCache);
 1.43265 +  
 1.43266 +      /* If the file on disk is not the same size as the database image,
 1.43267 +      ** then use pager_truncate to grow or shrink the file here.
 1.43268 +      */
 1.43269 +      if( pPager->dbSize!=pPager->dbFileSize ){
 1.43270 +        Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
 1.43271 +        assert( pPager->eState==PAGER_WRITER_DBMOD );
 1.43272 +        rc = pager_truncate(pPager, nNew);
 1.43273 +        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 1.43274 +      }
 1.43275 +  
 1.43276 +      /* Finally, sync the database file. */
 1.43277 +      if( !noSync ){
 1.43278 +        rc = sqlite3PagerSync(pPager);
 1.43279 +      }
 1.43280 +      IOTRACE(("DBSYNC %p\n", pPager))
 1.43281 +    }
 1.43282 +  }
 1.43283 +
 1.43284 +commit_phase_one_exit:
 1.43285 +  if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
 1.43286 +    pPager->eState = PAGER_WRITER_FINISHED;
 1.43287 +  }
 1.43288 +  return rc;
 1.43289 +}
 1.43290 +
 1.43291 +
 1.43292 +/*
 1.43293 +** When this function is called, the database file has been completely
 1.43294 +** updated to reflect the changes made by the current transaction and
 1.43295 +** synced to disk. The journal file still exists in the file-system 
 1.43296 +** though, and if a failure occurs at this point it will eventually
 1.43297 +** be used as a hot-journal and the current transaction rolled back.
 1.43298 +**
 1.43299 +** This function finalizes the journal file, either by deleting, 
 1.43300 +** truncating or partially zeroing it, so that it cannot be used 
 1.43301 +** for hot-journal rollback. Once this is done the transaction is
 1.43302 +** irrevocably committed.
 1.43303 +**
 1.43304 +** If an error occurs, an IO error code is returned and the pager
 1.43305 +** moves into the error state. Otherwise, SQLITE_OK is returned.
 1.43306 +*/
 1.43307 +SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
 1.43308 +  int rc = SQLITE_OK;                  /* Return code */
 1.43309 +
 1.43310 +  /* This routine should not be called if a prior error has occurred.
 1.43311 +  ** But if (due to a coding error elsewhere in the system) it does get
 1.43312 +  ** called, just return the same error code without doing anything. */
 1.43313 +  if( NEVER(pPager->errCode) ) return pPager->errCode;
 1.43314 +
 1.43315 +  assert( pPager->eState==PAGER_WRITER_LOCKED
 1.43316 +       || pPager->eState==PAGER_WRITER_FINISHED
 1.43317 +       || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
 1.43318 +  );
 1.43319 +  assert( assert_pager_state(pPager) );
 1.43320 +
 1.43321 +  /* An optimization. If the database was not actually modified during
 1.43322 +  ** this transaction, the pager is running in exclusive-mode and is
 1.43323 +  ** using persistent journals, then this function is a no-op.
 1.43324 +  **
 1.43325 +  ** The start of the journal file currently contains a single journal 
 1.43326 +  ** header with the nRec field set to 0. If such a journal is used as
 1.43327 +  ** a hot-journal during hot-journal rollback, 0 changes will be made
 1.43328 +  ** to the database file. So there is no need to zero the journal 
 1.43329 +  ** header. Since the pager is in exclusive mode, there is no need
 1.43330 +  ** to drop any locks either.
 1.43331 +  */
 1.43332 +  if( pPager->eState==PAGER_WRITER_LOCKED 
 1.43333 +   && pPager->exclusiveMode 
 1.43334 +   && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
 1.43335 +  ){
 1.43336 +    assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
 1.43337 +    pPager->eState = PAGER_READER;
 1.43338 +    return SQLITE_OK;
 1.43339 +  }
 1.43340 +
 1.43341 +  PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
 1.43342 +  rc = pager_end_transaction(pPager, pPager->setMaster);
 1.43343 +  return pager_error(pPager, rc);
 1.43344 +}
 1.43345 +
 1.43346 +/*
 1.43347 +** If a write transaction is open, then all changes made within the 
 1.43348 +** transaction are reverted and the current write-transaction is closed.
 1.43349 +** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
 1.43350 +** state if an error occurs.
 1.43351 +**
 1.43352 +** If the pager is already in PAGER_ERROR state when this function is called,
 1.43353 +** it returns Pager.errCode immediately. No work is performed in this case.
 1.43354 +**
 1.43355 +** Otherwise, in rollback mode, this function performs two functions:
 1.43356 +**
 1.43357 +**   1) It rolls back the journal file, restoring all database file and 
 1.43358 +**      in-memory cache pages to the state they were in when the transaction
 1.43359 +**      was opened, and
 1.43360 +**
 1.43361 +**   2) It finalizes the journal file, so that it is not used for hot
 1.43362 +**      rollback at any point in the future.
 1.43363 +**
 1.43364 +** Finalization of the journal file (task 2) is only performed if the 
 1.43365 +** rollback is successful.
 1.43366 +**
 1.43367 +** In WAL mode, all cache-entries containing data modified within the
 1.43368 +** current transaction are either expelled from the cache or reverted to
 1.43369 +** their pre-transaction state by re-reading data from the database or
 1.43370 +** WAL files. The WAL transaction is then closed.
 1.43371 +*/
 1.43372 +SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
 1.43373 +  int rc = SQLITE_OK;                  /* Return code */
 1.43374 +  PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
 1.43375 +
 1.43376 +  /* PagerRollback() is a no-op if called in READER or OPEN state. If
 1.43377 +  ** the pager is already in the ERROR state, the rollback is not 
 1.43378 +  ** attempted here. Instead, the error code is returned to the caller.
 1.43379 +  */
 1.43380 +  assert( assert_pager_state(pPager) );
 1.43381 +  if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
 1.43382 +  if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
 1.43383 +
 1.43384 +  if( pagerUseWal(pPager) ){
 1.43385 +    int rc2;
 1.43386 +    rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
 1.43387 +    rc2 = pager_end_transaction(pPager, pPager->setMaster);
 1.43388 +    if( rc==SQLITE_OK ) rc = rc2;
 1.43389 +  }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
 1.43390 +    int eState = pPager->eState;
 1.43391 +    rc = pager_end_transaction(pPager, 0);
 1.43392 +    if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
 1.43393 +      /* This can happen using journal_mode=off. Move the pager to the error 
 1.43394 +      ** state to indicate that the contents of the cache may not be trusted.
 1.43395 +      ** Any active readers will get SQLITE_ABORT.
 1.43396 +      */
 1.43397 +      pPager->errCode = SQLITE_ABORT;
 1.43398 +      pPager->eState = PAGER_ERROR;
 1.43399 +      return rc;
 1.43400 +    }
 1.43401 +  }else{
 1.43402 +    rc = pager_playback(pPager, 0);
 1.43403 +  }
 1.43404 +
 1.43405 +  assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
 1.43406 +  assert( rc==SQLITE_OK || rc==SQLITE_FULL
 1.43407 +          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
 1.43408 +
 1.43409 +  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
 1.43410 +  ** cache. So call pager_error() on the way out to make any error persistent.
 1.43411 +  */
 1.43412 +  return pager_error(pPager, rc);
 1.43413 +}
 1.43414 +
 1.43415 +/*
 1.43416 +** Return TRUE if the database file is opened read-only.  Return FALSE
 1.43417 +** if the database is (in theory) writable.
 1.43418 +*/
 1.43419 +SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
 1.43420 +  return pPager->readOnly;
 1.43421 +}
 1.43422 +
 1.43423 +/*
 1.43424 +** Return the number of references to the pager.
 1.43425 +*/
 1.43426 +SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
 1.43427 +  return sqlite3PcacheRefCount(pPager->pPCache);
 1.43428 +}
 1.43429 +
 1.43430 +/*
 1.43431 +** Return the approximate number of bytes of memory currently
 1.43432 +** used by the pager and its associated cache.
 1.43433 +*/
 1.43434 +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
 1.43435 +  int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
 1.43436 +                                     + 5*sizeof(void*);
 1.43437 +  return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
 1.43438 +           + sqlite3MallocSize(pPager)
 1.43439 +           + pPager->pageSize;
 1.43440 +}
 1.43441 +
 1.43442 +/*
 1.43443 +** Return the number of references to the specified page.
 1.43444 +*/
 1.43445 +SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
 1.43446 +  return sqlite3PcachePageRefcount(pPage);
 1.43447 +}
 1.43448 +
 1.43449 +#ifdef SQLITE_TEST
 1.43450 +/*
 1.43451 +** This routine is used for testing and analysis only.
 1.43452 +*/
 1.43453 +SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
 1.43454 +  static int a[11];
 1.43455 +  a[0] = sqlite3PcacheRefCount(pPager->pPCache);
 1.43456 +  a[1] = sqlite3PcachePagecount(pPager->pPCache);
 1.43457 +  a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
 1.43458 +  a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
 1.43459 +  a[4] = pPager->eState;
 1.43460 +  a[5] = pPager->errCode;
 1.43461 +  a[6] = pPager->aStat[PAGER_STAT_HIT];
 1.43462 +  a[7] = pPager->aStat[PAGER_STAT_MISS];
 1.43463 +  a[8] = 0;  /* Used to be pPager->nOvfl */
 1.43464 +  a[9] = pPager->nRead;
 1.43465 +  a[10] = pPager->aStat[PAGER_STAT_WRITE];
 1.43466 +  return a;
 1.43467 +}
 1.43468 +#endif
 1.43469 +
 1.43470 +/*
 1.43471 +** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 1.43472 +** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
 1.43473 +** current cache hit or miss count, according to the value of eStat. If the 
 1.43474 +** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 1.43475 +** returning.
 1.43476 +*/
 1.43477 +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 1.43478 +
 1.43479 +  assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 1.43480 +       || eStat==SQLITE_DBSTATUS_CACHE_MISS
 1.43481 +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE
 1.43482 +  );
 1.43483 +
 1.43484 +  assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 1.43485 +  assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 1.43486 +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
 1.43487 +
 1.43488 +  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
 1.43489 +  if( reset ){
 1.43490 +    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
 1.43491 +  }
 1.43492 +}
 1.43493 +
 1.43494 +/*
 1.43495 +** Return true if this is an in-memory pager.
 1.43496 +*/
 1.43497 +SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
 1.43498 +  return MEMDB;
 1.43499 +}
 1.43500 +
 1.43501 +/*
 1.43502 +** Check that there are at least nSavepoint savepoints open. If there are
 1.43503 +** currently less than nSavepoints open, then open one or more savepoints
 1.43504 +** to make up the difference. If the number of savepoints is already
 1.43505 +** equal to nSavepoint, then this function is a no-op.
 1.43506 +**
 1.43507 +** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
 1.43508 +** occurs while opening the sub-journal file, then an IO error code is
 1.43509 +** returned. Otherwise, SQLITE_OK.
 1.43510 +*/
 1.43511 +SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
 1.43512 +  int rc = SQLITE_OK;                       /* Return code */
 1.43513 +  int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
 1.43514 +
 1.43515 +  assert( pPager->eState>=PAGER_WRITER_LOCKED );
 1.43516 +  assert( assert_pager_state(pPager) );
 1.43517 +
 1.43518 +  if( nSavepoint>nCurrent && pPager->useJournal ){
 1.43519 +    int ii;                                 /* Iterator variable */
 1.43520 +    PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
 1.43521 +
 1.43522 +    /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
 1.43523 +    ** if the allocation fails. Otherwise, zero the new portion in case a 
 1.43524 +    ** malloc failure occurs while populating it in the for(...) loop below.
 1.43525 +    */
 1.43526 +    aNew = (PagerSavepoint *)sqlite3Realloc(
 1.43527 +        pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
 1.43528 +    );
 1.43529 +    if( !aNew ){
 1.43530 +      return SQLITE_NOMEM;
 1.43531 +    }
 1.43532 +    memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
 1.43533 +    pPager->aSavepoint = aNew;
 1.43534 +
 1.43535 +    /* Populate the PagerSavepoint structures just allocated. */
 1.43536 +    for(ii=nCurrent; ii<nSavepoint; ii++){
 1.43537 +      aNew[ii].nOrig = pPager->dbSize;
 1.43538 +      if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
 1.43539 +        aNew[ii].iOffset = pPager->journalOff;
 1.43540 +      }else{
 1.43541 +        aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
 1.43542 +      }
 1.43543 +      aNew[ii].iSubRec = pPager->nSubRec;
 1.43544 +      aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
 1.43545 +      if( !aNew[ii].pInSavepoint ){
 1.43546 +        return SQLITE_NOMEM;
 1.43547 +      }
 1.43548 +      if( pagerUseWal(pPager) ){
 1.43549 +        sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
 1.43550 +      }
 1.43551 +      pPager->nSavepoint = ii+1;
 1.43552 +    }
 1.43553 +    assert( pPager->nSavepoint==nSavepoint );
 1.43554 +    assertTruncateConstraint(pPager);
 1.43555 +  }
 1.43556 +
 1.43557 +  return rc;
 1.43558 +}
 1.43559 +
 1.43560 +/*
 1.43561 +** This function is called to rollback or release (commit) a savepoint.
 1.43562 +** The savepoint to release or rollback need not be the most recently 
 1.43563 +** created savepoint.
 1.43564 +**
 1.43565 +** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
 1.43566 +** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
 1.43567 +** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
 1.43568 +** that have occurred since the specified savepoint was created.
 1.43569 +**
 1.43570 +** The savepoint to rollback or release is identified by parameter 
 1.43571 +** iSavepoint. A value of 0 means to operate on the outermost savepoint
 1.43572 +** (the first created). A value of (Pager.nSavepoint-1) means operate
 1.43573 +** on the most recently created savepoint. If iSavepoint is greater than
 1.43574 +** (Pager.nSavepoint-1), then this function is a no-op.
 1.43575 +**
 1.43576 +** If a negative value is passed to this function, then the current
 1.43577 +** transaction is rolled back. This is different to calling 
 1.43578 +** sqlite3PagerRollback() because this function does not terminate
 1.43579 +** the transaction or unlock the database, it just restores the 
 1.43580 +** contents of the database to its original state. 
 1.43581 +**
 1.43582 +** In any case, all savepoints with an index greater than iSavepoint 
 1.43583 +** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
 1.43584 +** then savepoint iSavepoint is also destroyed.
 1.43585 +**
 1.43586 +** This function may return SQLITE_NOMEM if a memory allocation fails,
 1.43587 +** or an IO error code if an IO error occurs while rolling back a 
 1.43588 +** savepoint. If no errors occur, SQLITE_OK is returned.
 1.43589 +*/ 
 1.43590 +SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
 1.43591 +  int rc = pPager->errCode;       /* Return code */
 1.43592 +
 1.43593 +  assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 1.43594 +  assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
 1.43595 +
 1.43596 +  if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
 1.43597 +    int ii;            /* Iterator variable */
 1.43598 +    int nNew;          /* Number of remaining savepoints after this op. */
 1.43599 +
 1.43600 +    /* Figure out how many savepoints will still be active after this
 1.43601 +    ** operation. Store this value in nNew. Then free resources associated 
 1.43602 +    ** with any savepoints that are destroyed by this operation.
 1.43603 +    */
 1.43604 +    nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
 1.43605 +    for(ii=nNew; ii<pPager->nSavepoint; ii++){
 1.43606 +      sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
 1.43607 +    }
 1.43608 +    pPager->nSavepoint = nNew;
 1.43609 +
 1.43610 +    /* If this is a release of the outermost savepoint, truncate 
 1.43611 +    ** the sub-journal to zero bytes in size. */
 1.43612 +    if( op==SAVEPOINT_RELEASE ){
 1.43613 +      if( nNew==0 && isOpen(pPager->sjfd) ){
 1.43614 +        /* Only truncate if it is an in-memory sub-journal. */
 1.43615 +        if( sqlite3IsMemJournal(pPager->sjfd) ){
 1.43616 +          rc = sqlite3OsTruncate(pPager->sjfd, 0);
 1.43617 +          assert( rc==SQLITE_OK );
 1.43618 +        }
 1.43619 +        pPager->nSubRec = 0;
 1.43620 +      }
 1.43621 +    }
 1.43622 +    /* Else this is a rollback operation, playback the specified savepoint.
 1.43623 +    ** If this is a temp-file, it is possible that the journal file has
 1.43624 +    ** not yet been opened. In this case there have been no changes to
 1.43625 +    ** the database file, so the playback operation can be skipped.
 1.43626 +    */
 1.43627 +    else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
 1.43628 +      PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
 1.43629 +      rc = pagerPlaybackSavepoint(pPager, pSavepoint);
 1.43630 +      assert(rc!=SQLITE_DONE);
 1.43631 +    }
 1.43632 +  }
 1.43633 +
 1.43634 +  return rc;
 1.43635 +}
 1.43636 +
 1.43637 +/*
 1.43638 +** Return the full pathname of the database file.
 1.43639 +**
 1.43640 +** Except, if the pager is in-memory only, then return an empty string if
 1.43641 +** nullIfMemDb is true.  This routine is called with nullIfMemDb==1 when
 1.43642 +** used to report the filename to the user, for compatibility with legacy
 1.43643 +** behavior.  But when the Btree needs to know the filename for matching to
 1.43644 +** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
 1.43645 +** participate in shared-cache.
 1.43646 +*/
 1.43647 +SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
 1.43648 +  return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
 1.43649 +}
 1.43650 +
 1.43651 +/*
 1.43652 +** Return the VFS structure for the pager.
 1.43653 +*/
 1.43654 +SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
 1.43655 +  return pPager->pVfs;
 1.43656 +}
 1.43657 +
 1.43658 +/*
 1.43659 +** Return the file handle for the database file associated
 1.43660 +** with the pager.  This might return NULL if the file has
 1.43661 +** not yet been opened.
 1.43662 +*/
 1.43663 +SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
 1.43664 +  return pPager->fd;
 1.43665 +}
 1.43666 +
 1.43667 +/*
 1.43668 +** Return the full pathname of the journal file.
 1.43669 +*/
 1.43670 +SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
 1.43671 +  return pPager->zJournal;
 1.43672 +}
 1.43673 +
 1.43674 +/*
 1.43675 +** Return true if fsync() calls are disabled for this pager.  Return FALSE
 1.43676 +** if fsync()s are executed normally.
 1.43677 +*/
 1.43678 +SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
 1.43679 +  return pPager->noSync;
 1.43680 +}
 1.43681 +
 1.43682 +#ifdef SQLITE_HAS_CODEC
 1.43683 +/*
 1.43684 +** Set or retrieve the codec for this pager
 1.43685 +*/
 1.43686 +SQLITE_PRIVATE void sqlite3PagerSetCodec(
 1.43687 +  Pager *pPager,
 1.43688 +  void *(*xCodec)(void*,void*,Pgno,int),
 1.43689 +  void (*xCodecSizeChng)(void*,int,int),
 1.43690 +  void (*xCodecFree)(void*),
 1.43691 +  void *pCodec
 1.43692 +){
 1.43693 +  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
 1.43694 +  pPager->xCodec = pPager->memDb ? 0 : xCodec;
 1.43695 +  pPager->xCodecSizeChng = xCodecSizeChng;
 1.43696 +  pPager->xCodecFree = xCodecFree;
 1.43697 +  pPager->pCodec = pCodec;
 1.43698 +  pagerReportSize(pPager);
 1.43699 +}
 1.43700 +SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
 1.43701 +  return pPager->pCodec;
 1.43702 +}
 1.43703 +#endif
 1.43704 +
 1.43705 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.43706 +/*
 1.43707 +** Move the page pPg to location pgno in the file.
 1.43708 +**
 1.43709 +** There must be no references to the page previously located at
 1.43710 +** pgno (which we call pPgOld) though that page is allowed to be
 1.43711 +** in cache.  If the page previously located at pgno is not already
 1.43712 +** in the rollback journal, it is not put there by by this routine.
 1.43713 +**
 1.43714 +** References to the page pPg remain valid. Updating any
 1.43715 +** meta-data associated with pPg (i.e. data stored in the nExtra bytes
 1.43716 +** allocated along with the page) is the responsibility of the caller.
 1.43717 +**
 1.43718 +** A transaction must be active when this routine is called. It used to be
 1.43719 +** required that a statement transaction was not active, but this restriction
 1.43720 +** has been removed (CREATE INDEX needs to move a page when a statement
 1.43721 +** transaction is active).
 1.43722 +**
 1.43723 +** If the fourth argument, isCommit, is non-zero, then this page is being
 1.43724 +** moved as part of a database reorganization just before the transaction 
 1.43725 +** is being committed. In this case, it is guaranteed that the database page 
 1.43726 +** pPg refers to will not be written to again within this transaction.
 1.43727 +**
 1.43728 +** This function may return SQLITE_NOMEM or an IO error code if an error
 1.43729 +** occurs. Otherwise, it returns SQLITE_OK.
 1.43730 +*/
 1.43731 +SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
 1.43732 +  PgHdr *pPgOld;               /* The page being overwritten. */
 1.43733 +  Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
 1.43734 +  int rc;                      /* Return code */
 1.43735 +  Pgno origPgno;               /* The original page number */
 1.43736 +
 1.43737 +  assert( pPg->nRef>0 );
 1.43738 +  assert( pPager->eState==PAGER_WRITER_CACHEMOD
 1.43739 +       || pPager->eState==PAGER_WRITER_DBMOD
 1.43740 +  );
 1.43741 +  assert( assert_pager_state(pPager) );
 1.43742 +
 1.43743 +  /* In order to be able to rollback, an in-memory database must journal
 1.43744 +  ** the page we are moving from.
 1.43745 +  */
 1.43746 +  if( MEMDB ){
 1.43747 +    rc = sqlite3PagerWrite(pPg);
 1.43748 +    if( rc ) return rc;
 1.43749 +  }
 1.43750 +
 1.43751 +  /* If the page being moved is dirty and has not been saved by the latest
 1.43752 +  ** savepoint, then save the current contents of the page into the 
 1.43753 +  ** sub-journal now. This is required to handle the following scenario:
 1.43754 +  **
 1.43755 +  **   BEGIN;
 1.43756 +  **     <journal page X, then modify it in memory>
 1.43757 +  **     SAVEPOINT one;
 1.43758 +  **       <Move page X to location Y>
 1.43759 +  **     ROLLBACK TO one;
 1.43760 +  **
 1.43761 +  ** If page X were not written to the sub-journal here, it would not
 1.43762 +  ** be possible to restore its contents when the "ROLLBACK TO one"
 1.43763 +  ** statement were is processed.
 1.43764 +  **
 1.43765 +  ** subjournalPage() may need to allocate space to store pPg->pgno into
 1.43766 +  ** one or more savepoint bitvecs. This is the reason this function
 1.43767 +  ** may return SQLITE_NOMEM.
 1.43768 +  */
 1.43769 +  if( pPg->flags&PGHDR_DIRTY
 1.43770 +   && subjRequiresPage(pPg)
 1.43771 +   && SQLITE_OK!=(rc = subjournalPage(pPg))
 1.43772 +  ){
 1.43773 +    return rc;
 1.43774 +  }
 1.43775 +
 1.43776 +  PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 
 1.43777 +      PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
 1.43778 +  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
 1.43779 +
 1.43780 +  /* If the journal needs to be sync()ed before page pPg->pgno can
 1.43781 +  ** be written to, store pPg->pgno in local variable needSyncPgno.
 1.43782 +  **
 1.43783 +  ** If the isCommit flag is set, there is no need to remember that
 1.43784 +  ** the journal needs to be sync()ed before database page pPg->pgno 
 1.43785 +  ** can be written to. The caller has already promised not to write to it.
 1.43786 +  */
 1.43787 +  if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
 1.43788 +    needSyncPgno = pPg->pgno;
 1.43789 +    assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
 1.43790 +    assert( pPg->flags&PGHDR_DIRTY );
 1.43791 +  }
 1.43792 +
 1.43793 +  /* If the cache contains a page with page-number pgno, remove it
 1.43794 +  ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
 1.43795 +  ** page pgno before the 'move' operation, it needs to be retained 
 1.43796 +  ** for the page moved there.
 1.43797 +  */
 1.43798 +  pPg->flags &= ~PGHDR_NEED_SYNC;
 1.43799 +  pPgOld = pager_lookup(pPager, pgno);
 1.43800 +  assert( !pPgOld || pPgOld->nRef==1 );
 1.43801 +  if( pPgOld ){
 1.43802 +    pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
 1.43803 +    if( MEMDB ){
 1.43804 +      /* Do not discard pages from an in-memory database since we might
 1.43805 +      ** need to rollback later.  Just move the page out of the way. */
 1.43806 +      sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
 1.43807 +    }else{
 1.43808 +      sqlite3PcacheDrop(pPgOld);
 1.43809 +    }
 1.43810 +  }
 1.43811 +
 1.43812 +  origPgno = pPg->pgno;
 1.43813 +  sqlite3PcacheMove(pPg, pgno);
 1.43814 +  sqlite3PcacheMakeDirty(pPg);
 1.43815 +
 1.43816 +  /* For an in-memory database, make sure the original page continues
 1.43817 +  ** to exist, in case the transaction needs to roll back.  Use pPgOld
 1.43818 +  ** as the original page since it has already been allocated.
 1.43819 +  */
 1.43820 +  if( MEMDB ){
 1.43821 +    assert( pPgOld );
 1.43822 +    sqlite3PcacheMove(pPgOld, origPgno);
 1.43823 +    sqlite3PagerUnref(pPgOld);
 1.43824 +  }
 1.43825 +
 1.43826 +  if( needSyncPgno ){
 1.43827 +    /* If needSyncPgno is non-zero, then the journal file needs to be 
 1.43828 +    ** sync()ed before any data is written to database file page needSyncPgno.
 1.43829 +    ** Currently, no such page exists in the page-cache and the 
 1.43830 +    ** "is journaled" bitvec flag has been set. This needs to be remedied by
 1.43831 +    ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
 1.43832 +    ** flag.
 1.43833 +    **
 1.43834 +    ** If the attempt to load the page into the page-cache fails, (due
 1.43835 +    ** to a malloc() or IO failure), clear the bit in the pInJournal[]
 1.43836 +    ** array. Otherwise, if the page is loaded and written again in
 1.43837 +    ** this transaction, it may be written to the database file before
 1.43838 +    ** it is synced into the journal file. This way, it may end up in
 1.43839 +    ** the journal file twice, but that is not a problem.
 1.43840 +    */
 1.43841 +    PgHdr *pPgHdr;
 1.43842 +    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
 1.43843 +    if( rc!=SQLITE_OK ){
 1.43844 +      if( needSyncPgno<=pPager->dbOrigSize ){
 1.43845 +        assert( pPager->pTmpSpace!=0 );
 1.43846 +        sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
 1.43847 +      }
 1.43848 +      return rc;
 1.43849 +    }
 1.43850 +    pPgHdr->flags |= PGHDR_NEED_SYNC;
 1.43851 +    sqlite3PcacheMakeDirty(pPgHdr);
 1.43852 +    sqlite3PagerUnref(pPgHdr);
 1.43853 +  }
 1.43854 +
 1.43855 +  return SQLITE_OK;
 1.43856 +}
 1.43857 +#endif
 1.43858 +
 1.43859 +/*
 1.43860 +** Return a pointer to the data for the specified page.
 1.43861 +*/
 1.43862 +SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
 1.43863 +  assert( pPg->nRef>0 || pPg->pPager->memDb );
 1.43864 +  return pPg->pData;
 1.43865 +}
 1.43866 +
 1.43867 +/*
 1.43868 +** Return a pointer to the Pager.nExtra bytes of "extra" space 
 1.43869 +** allocated along with the specified page.
 1.43870 +*/
 1.43871 +SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
 1.43872 +  return pPg->pExtra;
 1.43873 +}
 1.43874 +
 1.43875 +/*
 1.43876 +** Get/set the locking-mode for this pager. Parameter eMode must be one
 1.43877 +** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
 1.43878 +** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
 1.43879 +** the locking-mode is set to the value specified.
 1.43880 +**
 1.43881 +** The returned value is either PAGER_LOCKINGMODE_NORMAL or
 1.43882 +** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
 1.43883 +** locking-mode.
 1.43884 +*/
 1.43885 +SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
 1.43886 +  assert( eMode==PAGER_LOCKINGMODE_QUERY
 1.43887 +            || eMode==PAGER_LOCKINGMODE_NORMAL
 1.43888 +            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
 1.43889 +  assert( PAGER_LOCKINGMODE_QUERY<0 );
 1.43890 +  assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
 1.43891 +  assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
 1.43892 +  if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
 1.43893 +    pPager->exclusiveMode = (u8)eMode;
 1.43894 +  }
 1.43895 +  return (int)pPager->exclusiveMode;
 1.43896 +}
 1.43897 +
 1.43898 +/*
 1.43899 +** Set the journal-mode for this pager. Parameter eMode must be one of:
 1.43900 +**
 1.43901 +**    PAGER_JOURNALMODE_DELETE
 1.43902 +**    PAGER_JOURNALMODE_TRUNCATE
 1.43903 +**    PAGER_JOURNALMODE_PERSIST
 1.43904 +**    PAGER_JOURNALMODE_OFF
 1.43905 +**    PAGER_JOURNALMODE_MEMORY
 1.43906 +**    PAGER_JOURNALMODE_WAL
 1.43907 +**
 1.43908 +** The journalmode is set to the value specified if the change is allowed.
 1.43909 +** The change may be disallowed for the following reasons:
 1.43910 +**
 1.43911 +**   *  An in-memory database can only have its journal_mode set to _OFF
 1.43912 +**      or _MEMORY.
 1.43913 +**
 1.43914 +**   *  Temporary databases cannot have _WAL journalmode.
 1.43915 +**
 1.43916 +** The returned indicate the current (possibly updated) journal-mode.
 1.43917 +*/
 1.43918 +SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
 1.43919 +  u8 eOld = pPager->journalMode;    /* Prior journalmode */
 1.43920 +
 1.43921 +#ifdef SQLITE_DEBUG
 1.43922 +  /* The print_pager_state() routine is intended to be used by the debugger
 1.43923 +  ** only.  We invoke it once here to suppress a compiler warning. */
 1.43924 +  print_pager_state(pPager);
 1.43925 +#endif
 1.43926 +
 1.43927 +
 1.43928 +  /* The eMode parameter is always valid */
 1.43929 +  assert(      eMode==PAGER_JOURNALMODE_DELETE
 1.43930 +            || eMode==PAGER_JOURNALMODE_TRUNCATE
 1.43931 +            || eMode==PAGER_JOURNALMODE_PERSIST
 1.43932 +            || eMode==PAGER_JOURNALMODE_OFF 
 1.43933 +            || eMode==PAGER_JOURNALMODE_WAL 
 1.43934 +            || eMode==PAGER_JOURNALMODE_MEMORY );
 1.43935 +
 1.43936 +  /* This routine is only called from the OP_JournalMode opcode, and
 1.43937 +  ** the logic there will never allow a temporary file to be changed
 1.43938 +  ** to WAL mode.
 1.43939 +  */
 1.43940 +  assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
 1.43941 +
 1.43942 +  /* Do allow the journalmode of an in-memory database to be set to
 1.43943 +  ** anything other than MEMORY or OFF
 1.43944 +  */
 1.43945 +  if( MEMDB ){
 1.43946 +    assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
 1.43947 +    if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
 1.43948 +      eMode = eOld;
 1.43949 +    }
 1.43950 +  }
 1.43951 +
 1.43952 +  if( eMode!=eOld ){
 1.43953 +
 1.43954 +    /* Change the journal mode. */
 1.43955 +    assert( pPager->eState!=PAGER_ERROR );
 1.43956 +    pPager->journalMode = (u8)eMode;
 1.43957 +
 1.43958 +    /* When transistioning from TRUNCATE or PERSIST to any other journal
 1.43959 +    ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
 1.43960 +    ** delete the journal file.
 1.43961 +    */
 1.43962 +    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
 1.43963 +    assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
 1.43964 +    assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
 1.43965 +    assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
 1.43966 +    assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
 1.43967 +    assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
 1.43968 +
 1.43969 +    assert( isOpen(pPager->fd) || pPager->exclusiveMode );
 1.43970 +    if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
 1.43971 +
 1.43972 +      /* In this case we would like to delete the journal file. If it is
 1.43973 +      ** not possible, then that is not a problem. Deleting the journal file
 1.43974 +      ** here is an optimization only.
 1.43975 +      **
 1.43976 +      ** Before deleting the journal file, obtain a RESERVED lock on the
 1.43977 +      ** database file. This ensures that the journal file is not deleted
 1.43978 +      ** while it is in use by some other client.
 1.43979 +      */
 1.43980 +      sqlite3OsClose(pPager->jfd);
 1.43981 +      if( pPager->eLock>=RESERVED_LOCK ){
 1.43982 +        sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 1.43983 +      }else{
 1.43984 +        int rc = SQLITE_OK;
 1.43985 +        int state = pPager->eState;
 1.43986 +        assert( state==PAGER_OPEN || state==PAGER_READER );
 1.43987 +        if( state==PAGER_OPEN ){
 1.43988 +          rc = sqlite3PagerSharedLock(pPager);
 1.43989 +        }
 1.43990 +        if( pPager->eState==PAGER_READER ){
 1.43991 +          assert( rc==SQLITE_OK );
 1.43992 +          rc = pagerLockDb(pPager, RESERVED_LOCK);
 1.43993 +        }
 1.43994 +        if( rc==SQLITE_OK ){
 1.43995 +          sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 1.43996 +        }
 1.43997 +        if( rc==SQLITE_OK && state==PAGER_READER ){
 1.43998 +          pagerUnlockDb(pPager, SHARED_LOCK);
 1.43999 +        }else if( state==PAGER_OPEN ){
 1.44000 +          pager_unlock(pPager);
 1.44001 +        }
 1.44002 +        assert( state==pPager->eState );
 1.44003 +      }
 1.44004 +    }
 1.44005 +  }
 1.44006 +
 1.44007 +  /* Return the new journal mode */
 1.44008 +  return (int)pPager->journalMode;
 1.44009 +}
 1.44010 +
 1.44011 +/*
 1.44012 +** Return the current journal mode.
 1.44013 +*/
 1.44014 +SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
 1.44015 +  return (int)pPager->journalMode;
 1.44016 +}
 1.44017 +
 1.44018 +/*
 1.44019 +** Return TRUE if the pager is in a state where it is OK to change the
 1.44020 +** journalmode.  Journalmode changes can only happen when the database
 1.44021 +** is unmodified.
 1.44022 +*/
 1.44023 +SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
 1.44024 +  assert( assert_pager_state(pPager) );
 1.44025 +  if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
 1.44026 +  if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
 1.44027 +  return 1;
 1.44028 +}
 1.44029 +
 1.44030 +/*
 1.44031 +** Get/set the size-limit used for persistent journal files.
 1.44032 +**
 1.44033 +** Setting the size limit to -1 means no limit is enforced.
 1.44034 +** An attempt to set a limit smaller than -1 is a no-op.
 1.44035 +*/
 1.44036 +SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
 1.44037 +  if( iLimit>=-1 ){
 1.44038 +    pPager->journalSizeLimit = iLimit;
 1.44039 +    sqlite3WalLimit(pPager->pWal, iLimit);
 1.44040 +  }
 1.44041 +  return pPager->journalSizeLimit;
 1.44042 +}
 1.44043 +
 1.44044 +/*
 1.44045 +** Return a pointer to the pPager->pBackup variable. The backup module
 1.44046 +** in backup.c maintains the content of this variable. This module
 1.44047 +** uses it opaquely as an argument to sqlite3BackupRestart() and
 1.44048 +** sqlite3BackupUpdate() only.
 1.44049 +*/
 1.44050 +SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
 1.44051 +  return &pPager->pBackup;
 1.44052 +}
 1.44053 +
 1.44054 +#ifndef SQLITE_OMIT_VACUUM
 1.44055 +/*
 1.44056 +** Unless this is an in-memory or temporary database, clear the pager cache.
 1.44057 +*/
 1.44058 +SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
 1.44059 +  if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
 1.44060 +}
 1.44061 +#endif
 1.44062 +
 1.44063 +#ifndef SQLITE_OMIT_WAL
 1.44064 +/*
 1.44065 +** This function is called when the user invokes "PRAGMA wal_checkpoint",
 1.44066 +** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
 1.44067 +** or wal_blocking_checkpoint() API functions.
 1.44068 +**
 1.44069 +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 1.44070 +*/
 1.44071 +SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
 1.44072 +  int rc = SQLITE_OK;
 1.44073 +  if( pPager->pWal ){
 1.44074 +    rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
 1.44075 +        pPager->xBusyHandler, pPager->pBusyHandlerArg,
 1.44076 +        pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 1.44077 +        pnLog, pnCkpt
 1.44078 +    );
 1.44079 +  }
 1.44080 +  return rc;
 1.44081 +}
 1.44082 +
 1.44083 +SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 1.44084 +  return sqlite3WalCallback(pPager->pWal);
 1.44085 +}
 1.44086 +
 1.44087 +/*
 1.44088 +** Return true if the underlying VFS for the given pager supports the
 1.44089 +** primitives necessary for write-ahead logging.
 1.44090 +*/
 1.44091 +SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
 1.44092 +  const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
 1.44093 +  return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
 1.44094 +}
 1.44095 +
 1.44096 +/*
 1.44097 +** Attempt to take an exclusive lock on the database file. If a PENDING lock
 1.44098 +** is obtained instead, immediately release it.
 1.44099 +*/
 1.44100 +static int pagerExclusiveLock(Pager *pPager){
 1.44101 +  int rc;                         /* Return code */
 1.44102 +
 1.44103 +  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
 1.44104 +  rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 1.44105 +  if( rc!=SQLITE_OK ){
 1.44106 +    /* If the attempt to grab the exclusive lock failed, release the 
 1.44107 +    ** pending lock that may have been obtained instead.  */
 1.44108 +    pagerUnlockDb(pPager, SHARED_LOCK);
 1.44109 +  }
 1.44110 +
 1.44111 +  return rc;
 1.44112 +}
 1.44113 +
 1.44114 +/*
 1.44115 +** Call sqlite3WalOpen() to open the WAL handle. If the pager is in 
 1.44116 +** exclusive-locking mode when this function is called, take an EXCLUSIVE
 1.44117 +** lock on the database file and use heap-memory to store the wal-index
 1.44118 +** in. Otherwise, use the normal shared-memory.
 1.44119 +*/
 1.44120 +static int pagerOpenWal(Pager *pPager){
 1.44121 +  int rc = SQLITE_OK;
 1.44122 +
 1.44123 +  assert( pPager->pWal==0 && pPager->tempFile==0 );
 1.44124 +  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
 1.44125 +
 1.44126 +  /* If the pager is already in exclusive-mode, the WAL module will use 
 1.44127 +  ** heap-memory for the wal-index instead of the VFS shared-memory 
 1.44128 +  ** implementation. Take the exclusive lock now, before opening the WAL
 1.44129 +  ** file, to make sure this is safe.
 1.44130 +  */
 1.44131 +  if( pPager->exclusiveMode ){
 1.44132 +    rc = pagerExclusiveLock(pPager);
 1.44133 +  }
 1.44134 +
 1.44135 +  /* Open the connection to the log file. If this operation fails, 
 1.44136 +  ** (e.g. due to malloc() failure), return an error code.
 1.44137 +  */
 1.44138 +  if( rc==SQLITE_OK ){
 1.44139 +    rc = sqlite3WalOpen(pPager->pVfs, 
 1.44140 +        pPager->fd, pPager->zWal, pPager->exclusiveMode,
 1.44141 +        pPager->journalSizeLimit, &pPager->pWal
 1.44142 +    );
 1.44143 +  }
 1.44144 +
 1.44145 +  return rc;
 1.44146 +}
 1.44147 +
 1.44148 +
 1.44149 +/*
 1.44150 +** The caller must be holding a SHARED lock on the database file to call
 1.44151 +** this function.
 1.44152 +**
 1.44153 +** If the pager passed as the first argument is open on a real database
 1.44154 +** file (not a temp file or an in-memory database), and the WAL file
 1.44155 +** is not already open, make an attempt to open it now. If successful,
 1.44156 +** return SQLITE_OK. If an error occurs or the VFS used by the pager does 
 1.44157 +** not support the xShmXXX() methods, return an error code. *pbOpen is
 1.44158 +** not modified in either case.
 1.44159 +**
 1.44160 +** If the pager is open on a temp-file (or in-memory database), or if
 1.44161 +** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
 1.44162 +** without doing anything.
 1.44163 +*/
 1.44164 +SQLITE_PRIVATE int sqlite3PagerOpenWal(
 1.44165 +  Pager *pPager,                  /* Pager object */
 1.44166 +  int *pbOpen                     /* OUT: Set to true if call is a no-op */
 1.44167 +){
 1.44168 +  int rc = SQLITE_OK;             /* Return code */
 1.44169 +
 1.44170 +  assert( assert_pager_state(pPager) );
 1.44171 +  assert( pPager->eState==PAGER_OPEN   || pbOpen );
 1.44172 +  assert( pPager->eState==PAGER_READER || !pbOpen );
 1.44173 +  assert( pbOpen==0 || *pbOpen==0 );
 1.44174 +  assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
 1.44175 +
 1.44176 +  if( !pPager->tempFile && !pPager->pWal ){
 1.44177 +    if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
 1.44178 +
 1.44179 +    /* Close any rollback journal previously open */
 1.44180 +    sqlite3OsClose(pPager->jfd);
 1.44181 +
 1.44182 +    rc = pagerOpenWal(pPager);
 1.44183 +    if( rc==SQLITE_OK ){
 1.44184 +      pPager->journalMode = PAGER_JOURNALMODE_WAL;
 1.44185 +      pPager->eState = PAGER_OPEN;
 1.44186 +    }
 1.44187 +  }else{
 1.44188 +    *pbOpen = 1;
 1.44189 +  }
 1.44190 +
 1.44191 +  return rc;
 1.44192 +}
 1.44193 +
 1.44194 +/*
 1.44195 +** This function is called to close the connection to the log file prior
 1.44196 +** to switching from WAL to rollback mode.
 1.44197 +**
 1.44198 +** Before closing the log file, this function attempts to take an 
 1.44199 +** EXCLUSIVE lock on the database file. If this cannot be obtained, an
 1.44200 +** error (SQLITE_BUSY) is returned and the log connection is not closed.
 1.44201 +** If successful, the EXCLUSIVE lock is not released before returning.
 1.44202 +*/
 1.44203 +SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
 1.44204 +  int rc = SQLITE_OK;
 1.44205 +
 1.44206 +  assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
 1.44207 +
 1.44208 +  /* If the log file is not already open, but does exist in the file-system,
 1.44209 +  ** it may need to be checkpointed before the connection can switch to
 1.44210 +  ** rollback mode. Open it now so this can happen.
 1.44211 +  */
 1.44212 +  if( !pPager->pWal ){
 1.44213 +    int logexists = 0;
 1.44214 +    rc = pagerLockDb(pPager, SHARED_LOCK);
 1.44215 +    if( rc==SQLITE_OK ){
 1.44216 +      rc = sqlite3OsAccess(
 1.44217 +          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
 1.44218 +      );
 1.44219 +    }
 1.44220 +    if( rc==SQLITE_OK && logexists ){
 1.44221 +      rc = pagerOpenWal(pPager);
 1.44222 +    }
 1.44223 +  }
 1.44224 +    
 1.44225 +  /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
 1.44226 +  ** the database file, the log and log-summary files will be deleted.
 1.44227 +  */
 1.44228 +  if( rc==SQLITE_OK && pPager->pWal ){
 1.44229 +    rc = pagerExclusiveLock(pPager);
 1.44230 +    if( rc==SQLITE_OK ){
 1.44231 +      rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
 1.44232 +                           pPager->pageSize, (u8*)pPager->pTmpSpace);
 1.44233 +      pPager->pWal = 0;
 1.44234 +    }
 1.44235 +  }
 1.44236 +  return rc;
 1.44237 +}
 1.44238 +
 1.44239 +#endif /* !SQLITE_OMIT_WAL */
 1.44240 +
 1.44241 +#ifdef SQLITE_ENABLE_ZIPVFS
 1.44242 +/*
 1.44243 +** A read-lock must be held on the pager when this function is called. If
 1.44244 +** the pager is in WAL mode and the WAL file currently contains one or more
 1.44245 +** frames, return the size in bytes of the page images stored within the
 1.44246 +** WAL frames. Otherwise, if this is not a WAL database or the WAL file
 1.44247 +** is empty, return 0.
 1.44248 +*/
 1.44249 +SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
 1.44250 +  assert( pPager->eState==PAGER_READER );
 1.44251 +  return sqlite3WalFramesize(pPager->pWal);
 1.44252 +}
 1.44253 +#endif
 1.44254 +
 1.44255 +#ifdef SQLITE_HAS_CODEC
 1.44256 +/*
 1.44257 +** This function is called by the wal module when writing page content
 1.44258 +** into the log file.
 1.44259 +**
 1.44260 +** This function returns a pointer to a buffer containing the encrypted
 1.44261 +** page content. If a malloc fails, this function may return NULL.
 1.44262 +*/
 1.44263 +SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
 1.44264 +  void *aData = 0;
 1.44265 +  CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
 1.44266 +  return aData;
 1.44267 +}
 1.44268 +#endif /* SQLITE_HAS_CODEC */
 1.44269 +
 1.44270 +#endif /* SQLITE_OMIT_DISKIO */
 1.44271 +
 1.44272 +/************** End of pager.c ***********************************************/
 1.44273 +/************** Begin file wal.c *********************************************/
 1.44274 +/*
 1.44275 +** 2010 February 1
 1.44276 +**
 1.44277 +** The author disclaims copyright to this source code.  In place of
 1.44278 +** a legal notice, here is a blessing:
 1.44279 +**
 1.44280 +**    May you do good and not evil.
 1.44281 +**    May you find forgiveness for yourself and forgive others.
 1.44282 +**    May you share freely, never taking more than you give.
 1.44283 +**
 1.44284 +*************************************************************************
 1.44285 +**
 1.44286 +** This file contains the implementation of a write-ahead log (WAL) used in 
 1.44287 +** "journal_mode=WAL" mode.
 1.44288 +**
 1.44289 +** WRITE-AHEAD LOG (WAL) FILE FORMAT
 1.44290 +**
 1.44291 +** A WAL file consists of a header followed by zero or more "frames".
 1.44292 +** Each frame records the revised content of a single page from the
 1.44293 +** database file.  All changes to the database are recorded by writing
 1.44294 +** frames into the WAL.  Transactions commit when a frame is written that
 1.44295 +** contains a commit marker.  A single WAL can and usually does record 
 1.44296 +** multiple transactions.  Periodically, the content of the WAL is
 1.44297 +** transferred back into the database file in an operation called a
 1.44298 +** "checkpoint".
 1.44299 +**
 1.44300 +** A single WAL file can be used multiple times.  In other words, the
 1.44301 +** WAL can fill up with frames and then be checkpointed and then new
 1.44302 +** frames can overwrite the old ones.  A WAL always grows from beginning
 1.44303 +** toward the end.  Checksums and counters attached to each frame are
 1.44304 +** used to determine which frames within the WAL are valid and which
 1.44305 +** are leftovers from prior checkpoints.
 1.44306 +**
 1.44307 +** The WAL header is 32 bytes in size and consists of the following eight
 1.44308 +** big-endian 32-bit unsigned integer values:
 1.44309 +**
 1.44310 +**     0: Magic number.  0x377f0682 or 0x377f0683
 1.44311 +**     4: File format version.  Currently 3007000
 1.44312 +**     8: Database page size.  Example: 1024
 1.44313 +**    12: Checkpoint sequence number
 1.44314 +**    16: Salt-1, random integer incremented with each checkpoint
 1.44315 +**    20: Salt-2, a different random integer changing with each ckpt
 1.44316 +**    24: Checksum-1 (first part of checksum for first 24 bytes of header).
 1.44317 +**    28: Checksum-2 (second part of checksum for first 24 bytes of header).
 1.44318 +**
 1.44319 +** Immediately following the wal-header are zero or more frames. Each
 1.44320 +** frame consists of a 24-byte frame-header followed by a <page-size> bytes
 1.44321 +** of page data. The frame-header is six big-endian 32-bit unsigned 
 1.44322 +** integer values, as follows:
 1.44323 +**
 1.44324 +**     0: Page number.
 1.44325 +**     4: For commit records, the size of the database image in pages 
 1.44326 +**        after the commit. For all other records, zero.
 1.44327 +**     8: Salt-1 (copied from the header)
 1.44328 +**    12: Salt-2 (copied from the header)
 1.44329 +**    16: Checksum-1.
 1.44330 +**    20: Checksum-2.
 1.44331 +**
 1.44332 +** A frame is considered valid if and only if the following conditions are
 1.44333 +** true:
 1.44334 +**
 1.44335 +**    (1) The salt-1 and salt-2 values in the frame-header match
 1.44336 +**        salt values in the wal-header
 1.44337 +**
 1.44338 +**    (2) The checksum values in the final 8 bytes of the frame-header
 1.44339 +**        exactly match the checksum computed consecutively on the
 1.44340 +**        WAL header and the first 8 bytes and the content of all frames
 1.44341 +**        up to and including the current frame.
 1.44342 +**
 1.44343 +** The checksum is computed using 32-bit big-endian integers if the
 1.44344 +** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
 1.44345 +** is computed using little-endian if the magic number is 0x377f0682.
 1.44346 +** The checksum values are always stored in the frame header in a
 1.44347 +** big-endian format regardless of which byte order is used to compute
 1.44348 +** the checksum.  The checksum is computed by interpreting the input as
 1.44349 +** an even number of unsigned 32-bit integers: x[0] through x[N].  The
 1.44350 +** algorithm used for the checksum is as follows:
 1.44351 +** 
 1.44352 +**   for i from 0 to n-1 step 2:
 1.44353 +**     s0 += x[i] + s1;
 1.44354 +**     s1 += x[i+1] + s0;
 1.44355 +**   endfor
 1.44356 +**
 1.44357 +** Note that s0 and s1 are both weighted checksums using fibonacci weights
 1.44358 +** in reverse order (the largest fibonacci weight occurs on the first element
 1.44359 +** of the sequence being summed.)  The s1 value spans all 32-bit 
 1.44360 +** terms of the sequence whereas s0 omits the final term.
 1.44361 +**
 1.44362 +** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
 1.44363 +** WAL is transferred into the database, then the database is VFS.xSync-ed.
 1.44364 +** The VFS.xSync operations serve as write barriers - all writes launched
 1.44365 +** before the xSync must complete before any write that launches after the
 1.44366 +** xSync begins.
 1.44367 +**
 1.44368 +** After each checkpoint, the salt-1 value is incremented and the salt-2
 1.44369 +** value is randomized.  This prevents old and new frames in the WAL from
 1.44370 +** being considered valid at the same time and being checkpointing together
 1.44371 +** following a crash.
 1.44372 +**
 1.44373 +** READER ALGORITHM
 1.44374 +**
 1.44375 +** To read a page from the database (call it page number P), a reader
 1.44376 +** first checks the WAL to see if it contains page P.  If so, then the
 1.44377 +** last valid instance of page P that is a followed by a commit frame
 1.44378 +** or is a commit frame itself becomes the value read.  If the WAL
 1.44379 +** contains no copies of page P that are valid and which are a commit
 1.44380 +** frame or are followed by a commit frame, then page P is read from
 1.44381 +** the database file.
 1.44382 +**
 1.44383 +** To start a read transaction, the reader records the index of the last
 1.44384 +** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
 1.44385 +** for all subsequent read operations.  New transactions can be appended
 1.44386 +** to the WAL, but as long as the reader uses its original mxFrame value
 1.44387 +** and ignores the newly appended content, it will see a consistent snapshot
 1.44388 +** of the database from a single point in time.  This technique allows
 1.44389 +** multiple concurrent readers to view different versions of the database
 1.44390 +** content simultaneously.
 1.44391 +**
 1.44392 +** The reader algorithm in the previous paragraphs works correctly, but 
 1.44393 +** because frames for page P can appear anywhere within the WAL, the
 1.44394 +** reader has to scan the entire WAL looking for page P frames.  If the
 1.44395 +** WAL is large (multiple megabytes is typical) that scan can be slow,
 1.44396 +** and read performance suffers.  To overcome this problem, a separate
 1.44397 +** data structure called the wal-index is maintained to expedite the
 1.44398 +** search for frames of a particular page.
 1.44399 +** 
 1.44400 +** WAL-INDEX FORMAT
 1.44401 +**
 1.44402 +** Conceptually, the wal-index is shared memory, though VFS implementations
 1.44403 +** might choose to implement the wal-index using a mmapped file.  Because
 1.44404 +** the wal-index is shared memory, SQLite does not support journal_mode=WAL 
 1.44405 +** on a network filesystem.  All users of the database must be able to
 1.44406 +** share memory.
 1.44407 +**
 1.44408 +** The wal-index is transient.  After a crash, the wal-index can (and should
 1.44409 +** be) reconstructed from the original WAL file.  In fact, the VFS is required
 1.44410 +** to either truncate or zero the header of the wal-index when the last
 1.44411 +** connection to it closes.  Because the wal-index is transient, it can
 1.44412 +** use an architecture-specific format; it does not have to be cross-platform.
 1.44413 +** Hence, unlike the database and WAL file formats which store all values
 1.44414 +** as big endian, the wal-index can store multi-byte values in the native
 1.44415 +** byte order of the host computer.
 1.44416 +**
 1.44417 +** The purpose of the wal-index is to answer this question quickly:  Given
 1.44418 +** a page number P and a maximum frame index M, return the index of the 
 1.44419 +** last frame in the wal before frame M for page P in the WAL, or return
 1.44420 +** NULL if there are no frames for page P in the WAL prior to M.
 1.44421 +**
 1.44422 +** The wal-index consists of a header region, followed by an one or
 1.44423 +** more index blocks.  
 1.44424 +**
 1.44425 +** The wal-index header contains the total number of frames within the WAL
 1.44426 +** in the mxFrame field.
 1.44427 +**
 1.44428 +** Each index block except for the first contains information on 
 1.44429 +** HASHTABLE_NPAGE frames. The first index block contains information on
 1.44430 +** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and 
 1.44431 +** HASHTABLE_NPAGE are selected so that together the wal-index header and
 1.44432 +** first index block are the same size as all other index blocks in the
 1.44433 +** wal-index.
 1.44434 +**
 1.44435 +** Each index block contains two sections, a page-mapping that contains the
 1.44436 +** database page number associated with each wal frame, and a hash-table 
 1.44437 +** that allows readers to query an index block for a specific page number.
 1.44438 +** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
 1.44439 +** for the first index block) 32-bit page numbers. The first entry in the 
 1.44440 +** first index-block contains the database page number corresponding to the
 1.44441 +** first frame in the WAL file. The first entry in the second index block
 1.44442 +** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
 1.44443 +** the log, and so on.
 1.44444 +**
 1.44445 +** The last index block in a wal-index usually contains less than the full
 1.44446 +** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
 1.44447 +** depending on the contents of the WAL file. This does not change the
 1.44448 +** allocated size of the page-mapping array - the page-mapping array merely
 1.44449 +** contains unused entries.
 1.44450 +**
 1.44451 +** Even without using the hash table, the last frame for page P
 1.44452 +** can be found by scanning the page-mapping sections of each index block
 1.44453 +** starting with the last index block and moving toward the first, and
 1.44454 +** within each index block, starting at the end and moving toward the
 1.44455 +** beginning.  The first entry that equals P corresponds to the frame
 1.44456 +** holding the content for that page.
 1.44457 +**
 1.44458 +** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
 1.44459 +** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
 1.44460 +** hash table for each page number in the mapping section, so the hash 
 1.44461 +** table is never more than half full.  The expected number of collisions 
 1.44462 +** prior to finding a match is 1.  Each entry of the hash table is an
 1.44463 +** 1-based index of an entry in the mapping section of the same
 1.44464 +** index block.   Let K be the 1-based index of the largest entry in
 1.44465 +** the mapping section.  (For index blocks other than the last, K will
 1.44466 +** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
 1.44467 +** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
 1.44468 +** contain a value of 0.
 1.44469 +**
 1.44470 +** To look for page P in the hash table, first compute a hash iKey on
 1.44471 +** P as follows:
 1.44472 +**
 1.44473 +**      iKey = (P * 383) % HASHTABLE_NSLOT
 1.44474 +**
 1.44475 +** Then start scanning entries of the hash table, starting with iKey
 1.44476 +** (wrapping around to the beginning when the end of the hash table is
 1.44477 +** reached) until an unused hash slot is found. Let the first unused slot
 1.44478 +** be at index iUnused.  (iUnused might be less than iKey if there was
 1.44479 +** wrap-around.) Because the hash table is never more than half full,
 1.44480 +** the search is guaranteed to eventually hit an unused entry.  Let 
 1.44481 +** iMax be the value between iKey and iUnused, closest to iUnused,
 1.44482 +** where aHash[iMax]==P.  If there is no iMax entry (if there exists
 1.44483 +** no hash slot such that aHash[i]==p) then page P is not in the
 1.44484 +** current index block.  Otherwise the iMax-th mapping entry of the
 1.44485 +** current index block corresponds to the last entry that references 
 1.44486 +** page P.
 1.44487 +**
 1.44488 +** A hash search begins with the last index block and moves toward the
 1.44489 +** first index block, looking for entries corresponding to page P.  On
 1.44490 +** average, only two or three slots in each index block need to be
 1.44491 +** examined in order to either find the last entry for page P, or to
 1.44492 +** establish that no such entry exists in the block.  Each index block
 1.44493 +** holds over 4000 entries.  So two or three index blocks are sufficient
 1.44494 +** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
 1.44495 +** comparisons (on average) suffice to either locate a frame in the
 1.44496 +** WAL or to establish that the frame does not exist in the WAL.  This
 1.44497 +** is much faster than scanning the entire 10MB WAL.
 1.44498 +**
 1.44499 +** Note that entries are added in order of increasing K.  Hence, one
 1.44500 +** reader might be using some value K0 and a second reader that started
 1.44501 +** at a later time (after additional transactions were added to the WAL
 1.44502 +** and to the wal-index) might be using a different value K1, where K1>K0.
 1.44503 +** Both readers can use the same hash table and mapping section to get
 1.44504 +** the correct result.  There may be entries in the hash table with
 1.44505 +** K>K0 but to the first reader, those entries will appear to be unused
 1.44506 +** slots in the hash table and so the first reader will get an answer as
 1.44507 +** if no values greater than K0 had ever been inserted into the hash table
 1.44508 +** in the first place - which is what reader one wants.  Meanwhile, the
 1.44509 +** second reader using K1 will see additional values that were inserted
 1.44510 +** later, which is exactly what reader two wants.  
 1.44511 +**
 1.44512 +** When a rollback occurs, the value of K is decreased. Hash table entries
 1.44513 +** that correspond to frames greater than the new K value are removed
 1.44514 +** from the hash table at this point.
 1.44515 +*/
 1.44516 +#ifndef SQLITE_OMIT_WAL
 1.44517 +
 1.44518 +
 1.44519 +/*
 1.44520 +** Trace output macros
 1.44521 +*/
 1.44522 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.44523 +SQLITE_PRIVATE int sqlite3WalTrace = 0;
 1.44524 +# define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
 1.44525 +#else
 1.44526 +# define WALTRACE(X)
 1.44527 +#endif
 1.44528 +
 1.44529 +/*
 1.44530 +** The maximum (and only) versions of the wal and wal-index formats
 1.44531 +** that may be interpreted by this version of SQLite.
 1.44532 +**
 1.44533 +** If a client begins recovering a WAL file and finds that (a) the checksum
 1.44534 +** values in the wal-header are correct and (b) the version field is not
 1.44535 +** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
 1.44536 +**
 1.44537 +** Similarly, if a client successfully reads a wal-index header (i.e. the 
 1.44538 +** checksum test is successful) and finds that the version field is not
 1.44539 +** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
 1.44540 +** returns SQLITE_CANTOPEN.
 1.44541 +*/
 1.44542 +#define WAL_MAX_VERSION      3007000
 1.44543 +#define WALINDEX_MAX_VERSION 3007000
 1.44544 +
 1.44545 +/*
 1.44546 +** Indices of various locking bytes.   WAL_NREADER is the number
 1.44547 +** of available reader locks and should be at least 3.
 1.44548 +*/
 1.44549 +#define WAL_WRITE_LOCK         0
 1.44550 +#define WAL_ALL_BUT_WRITE      1
 1.44551 +#define WAL_CKPT_LOCK          1
 1.44552 +#define WAL_RECOVER_LOCK       2
 1.44553 +#define WAL_READ_LOCK(I)       (3+(I))
 1.44554 +#define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
 1.44555 +
 1.44556 +
 1.44557 +/* Object declarations */
 1.44558 +typedef struct WalIndexHdr WalIndexHdr;
 1.44559 +typedef struct WalIterator WalIterator;
 1.44560 +typedef struct WalCkptInfo WalCkptInfo;
 1.44561 +
 1.44562 +
 1.44563 +/*
 1.44564 +** The following object holds a copy of the wal-index header content.
 1.44565 +**
 1.44566 +** The actual header in the wal-index consists of two copies of this
 1.44567 +** object.
 1.44568 +**
 1.44569 +** The szPage value can be any power of 2 between 512 and 32768, inclusive.
 1.44570 +** Or it can be 1 to represent a 65536-byte page.  The latter case was
 1.44571 +** added in 3.7.1 when support for 64K pages was added.  
 1.44572 +*/
 1.44573 +struct WalIndexHdr {
 1.44574 +  u32 iVersion;                   /* Wal-index version */
 1.44575 +  u32 unused;                     /* Unused (padding) field */
 1.44576 +  u32 iChange;                    /* Counter incremented each transaction */
 1.44577 +  u8 isInit;                      /* 1 when initialized */
 1.44578 +  u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
 1.44579 +  u16 szPage;                     /* Database page size in bytes. 1==64K */
 1.44580 +  u32 mxFrame;                    /* Index of last valid frame in the WAL */
 1.44581 +  u32 nPage;                      /* Size of database in pages */
 1.44582 +  u32 aFrameCksum[2];             /* Checksum of last frame in log */
 1.44583 +  u32 aSalt[2];                   /* Two salt values copied from WAL header */
 1.44584 +  u32 aCksum[2];                  /* Checksum over all prior fields */
 1.44585 +};
 1.44586 +
 1.44587 +/*
 1.44588 +** A copy of the following object occurs in the wal-index immediately
 1.44589 +** following the second copy of the WalIndexHdr.  This object stores
 1.44590 +** information used by checkpoint.
 1.44591 +**
 1.44592 +** nBackfill is the number of frames in the WAL that have been written
 1.44593 +** back into the database. (We call the act of moving content from WAL to
 1.44594 +** database "backfilling".)  The nBackfill number is never greater than
 1.44595 +** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
 1.44596 +** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
 1.44597 +** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
 1.44598 +** mxFrame back to zero when the WAL is reset.
 1.44599 +**
 1.44600 +** There is one entry in aReadMark[] for each reader lock.  If a reader
 1.44601 +** holds read-lock K, then the value in aReadMark[K] is no greater than
 1.44602 +** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
 1.44603 +** for any aReadMark[] means that entry is unused.  aReadMark[0] is 
 1.44604 +** a special case; its value is never used and it exists as a place-holder
 1.44605 +** to avoid having to offset aReadMark[] indexs by one.  Readers holding
 1.44606 +** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
 1.44607 +** directly from the database.
 1.44608 +**
 1.44609 +** The value of aReadMark[K] may only be changed by a thread that
 1.44610 +** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
 1.44611 +** aReadMark[K] cannot changed while there is a reader is using that mark
 1.44612 +** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
 1.44613 +**
 1.44614 +** The checkpointer may only transfer frames from WAL to database where
 1.44615 +** the frame numbers are less than or equal to every aReadMark[] that is
 1.44616 +** in use (that is, every aReadMark[j] for which there is a corresponding
 1.44617 +** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
 1.44618 +** largest value and will increase an unused aReadMark[] to mxFrame if there
 1.44619 +** is not already an aReadMark[] equal to mxFrame.  The exception to the
 1.44620 +** previous sentence is when nBackfill equals mxFrame (meaning that everything
 1.44621 +** in the WAL has been backfilled into the database) then new readers
 1.44622 +** will choose aReadMark[0] which has value 0 and hence such reader will
 1.44623 +** get all their all content directly from the database file and ignore 
 1.44624 +** the WAL.
 1.44625 +**
 1.44626 +** Writers normally append new frames to the end of the WAL.  However,
 1.44627 +** if nBackfill equals mxFrame (meaning that all WAL content has been
 1.44628 +** written back into the database) and if no readers are using the WAL
 1.44629 +** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
 1.44630 +** the writer will first "reset" the WAL back to the beginning and start
 1.44631 +** writing new content beginning at frame 1.
 1.44632 +**
 1.44633 +** We assume that 32-bit loads are atomic and so no locks are needed in
 1.44634 +** order to read from any aReadMark[] entries.
 1.44635 +*/
 1.44636 +struct WalCkptInfo {
 1.44637 +  u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
 1.44638 +  u32 aReadMark[WAL_NREADER];     /* Reader marks */
 1.44639 +};
 1.44640 +#define READMARK_NOT_USED  0xffffffff
 1.44641 +
 1.44642 +
 1.44643 +/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
 1.44644 +** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
 1.44645 +** only support mandatory file-locks, we do not read or write data
 1.44646 +** from the region of the file on which locks are applied.
 1.44647 +*/
 1.44648 +#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
 1.44649 +#define WALINDEX_LOCK_RESERVED 16
 1.44650 +#define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
 1.44651 +
 1.44652 +/* Size of header before each frame in wal */
 1.44653 +#define WAL_FRAME_HDRSIZE 24
 1.44654 +
 1.44655 +/* Size of write ahead log header, including checksum. */
 1.44656 +/* #define WAL_HDRSIZE 24 */
 1.44657 +#define WAL_HDRSIZE 32
 1.44658 +
 1.44659 +/* WAL magic value. Either this value, or the same value with the least
 1.44660 +** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
 1.44661 +** big-endian format in the first 4 bytes of a WAL file.
 1.44662 +**
 1.44663 +** If the LSB is set, then the checksums for each frame within the WAL
 1.44664 +** file are calculated by treating all data as an array of 32-bit 
 1.44665 +** big-endian words. Otherwise, they are calculated by interpreting 
 1.44666 +** all data as 32-bit little-endian words.
 1.44667 +*/
 1.44668 +#define WAL_MAGIC 0x377f0682
 1.44669 +
 1.44670 +/*
 1.44671 +** Return the offset of frame iFrame in the write-ahead log file, 
 1.44672 +** assuming a database page size of szPage bytes. The offset returned
 1.44673 +** is to the start of the write-ahead log frame-header.
 1.44674 +*/
 1.44675 +#define walFrameOffset(iFrame, szPage) (                               \
 1.44676 +  WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
 1.44677 +)
 1.44678 +
 1.44679 +/*
 1.44680 +** An open write-ahead log file is represented by an instance of the
 1.44681 +** following object.
 1.44682 +*/
 1.44683 +struct Wal {
 1.44684 +  sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
 1.44685 +  sqlite3_file *pDbFd;       /* File handle for the database file */
 1.44686 +  sqlite3_file *pWalFd;      /* File handle for WAL file */
 1.44687 +  u32 iCallback;             /* Value to pass to log callback (or 0) */
 1.44688 +  i64 mxWalSize;             /* Truncate WAL to this size upon reset */
 1.44689 +  int nWiData;               /* Size of array apWiData */
 1.44690 +  int szFirstBlock;          /* Size of first block written to WAL file */
 1.44691 +  volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
 1.44692 +  u32 szPage;                /* Database page size */
 1.44693 +  i16 readLock;              /* Which read lock is being held.  -1 for none */
 1.44694 +  u8 syncFlags;              /* Flags to use to sync header writes */
 1.44695 +  u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
 1.44696 +  u8 writeLock;              /* True if in a write transaction */
 1.44697 +  u8 ckptLock;               /* True if holding a checkpoint lock */
 1.44698 +  u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
 1.44699 +  u8 truncateOnCommit;       /* True to truncate WAL file on commit */
 1.44700 +  u8 syncHeader;             /* Fsync the WAL header if true */
 1.44701 +  u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
 1.44702 +  WalIndexHdr hdr;           /* Wal-index header for current transaction */
 1.44703 +  const char *zWalName;      /* Name of WAL file */
 1.44704 +  u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
 1.44705 +#ifdef SQLITE_DEBUG
 1.44706 +  u8 lockError;              /* True if a locking error has occurred */
 1.44707 +#endif
 1.44708 +};
 1.44709 +
 1.44710 +/*
 1.44711 +** Candidate values for Wal.exclusiveMode.
 1.44712 +*/
 1.44713 +#define WAL_NORMAL_MODE     0
 1.44714 +#define WAL_EXCLUSIVE_MODE  1     
 1.44715 +#define WAL_HEAPMEMORY_MODE 2
 1.44716 +
 1.44717 +/*
 1.44718 +** Possible values for WAL.readOnly
 1.44719 +*/
 1.44720 +#define WAL_RDWR        0    /* Normal read/write connection */
 1.44721 +#define WAL_RDONLY      1    /* The WAL file is readonly */
 1.44722 +#define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
 1.44723 +
 1.44724 +/*
 1.44725 +** Each page of the wal-index mapping contains a hash-table made up of
 1.44726 +** an array of HASHTABLE_NSLOT elements of the following type.
 1.44727 +*/
 1.44728 +typedef u16 ht_slot;
 1.44729 +
 1.44730 +/*
 1.44731 +** This structure is used to implement an iterator that loops through
 1.44732 +** all frames in the WAL in database page order. Where two or more frames
 1.44733 +** correspond to the same database page, the iterator visits only the 
 1.44734 +** frame most recently written to the WAL (in other words, the frame with
 1.44735 +** the largest index).
 1.44736 +**
 1.44737 +** The internals of this structure are only accessed by:
 1.44738 +**
 1.44739 +**   walIteratorInit() - Create a new iterator,
 1.44740 +**   walIteratorNext() - Step an iterator,
 1.44741 +**   walIteratorFree() - Free an iterator.
 1.44742 +**
 1.44743 +** This functionality is used by the checkpoint code (see walCheckpoint()).
 1.44744 +*/
 1.44745 +struct WalIterator {
 1.44746 +  int iPrior;                     /* Last result returned from the iterator */
 1.44747 +  int nSegment;                   /* Number of entries in aSegment[] */
 1.44748 +  struct WalSegment {
 1.44749 +    int iNext;                    /* Next slot in aIndex[] not yet returned */
 1.44750 +    ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
 1.44751 +    u32 *aPgno;                   /* Array of page numbers. */
 1.44752 +    int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
 1.44753 +    int iZero;                    /* Frame number associated with aPgno[0] */
 1.44754 +  } aSegment[1];                  /* One for every 32KB page in the wal-index */
 1.44755 +};
 1.44756 +
 1.44757 +/*
 1.44758 +** Define the parameters of the hash tables in the wal-index file. There
 1.44759 +** is a hash-table following every HASHTABLE_NPAGE page numbers in the
 1.44760 +** wal-index.
 1.44761 +**
 1.44762 +** Changing any of these constants will alter the wal-index format and
 1.44763 +** create incompatibilities.
 1.44764 +*/
 1.44765 +#define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
 1.44766 +#define HASHTABLE_HASH_1     383                  /* Should be prime */
 1.44767 +#define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
 1.44768 +
 1.44769 +/* 
 1.44770 +** The block of page numbers associated with the first hash-table in a
 1.44771 +** wal-index is smaller than usual. This is so that there is a complete
 1.44772 +** hash-table on each aligned 32KB page of the wal-index.
 1.44773 +*/
 1.44774 +#define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
 1.44775 +
 1.44776 +/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
 1.44777 +#define WALINDEX_PGSZ   (                                         \
 1.44778 +    sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
 1.44779 +)
 1.44780 +
 1.44781 +/*
 1.44782 +** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
 1.44783 +** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
 1.44784 +** numbered from zero.
 1.44785 +**
 1.44786 +** If this call is successful, *ppPage is set to point to the wal-index
 1.44787 +** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 1.44788 +** then an SQLite error code is returned and *ppPage is set to 0.
 1.44789 +*/
 1.44790 +static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
 1.44791 +  int rc = SQLITE_OK;
 1.44792 +
 1.44793 +  /* Enlarge the pWal->apWiData[] array if required */
 1.44794 +  if( pWal->nWiData<=iPage ){
 1.44795 +    int nByte = sizeof(u32*)*(iPage+1);
 1.44796 +    volatile u32 **apNew;
 1.44797 +    apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
 1.44798 +    if( !apNew ){
 1.44799 +      *ppPage = 0;
 1.44800 +      return SQLITE_NOMEM;
 1.44801 +    }
 1.44802 +    memset((void*)&apNew[pWal->nWiData], 0,
 1.44803 +           sizeof(u32*)*(iPage+1-pWal->nWiData));
 1.44804 +    pWal->apWiData = apNew;
 1.44805 +    pWal->nWiData = iPage+1;
 1.44806 +  }
 1.44807 +
 1.44808 +  /* Request a pointer to the required page from the VFS */
 1.44809 +  if( pWal->apWiData[iPage]==0 ){
 1.44810 +    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 1.44811 +      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 1.44812 +      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
 1.44813 +    }else{
 1.44814 +      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 1.44815 +          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 1.44816 +      );
 1.44817 +      if( rc==SQLITE_READONLY ){
 1.44818 +        pWal->readOnly |= WAL_SHM_RDONLY;
 1.44819 +        rc = SQLITE_OK;
 1.44820 +      }
 1.44821 +    }
 1.44822 +  }
 1.44823 +
 1.44824 +  *ppPage = pWal->apWiData[iPage];
 1.44825 +  assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 1.44826 +  return rc;
 1.44827 +}
 1.44828 +
 1.44829 +/*
 1.44830 +** Return a pointer to the WalCkptInfo structure in the wal-index.
 1.44831 +*/
 1.44832 +static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
 1.44833 +  assert( pWal->nWiData>0 && pWal->apWiData[0] );
 1.44834 +  return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
 1.44835 +}
 1.44836 +
 1.44837 +/*
 1.44838 +** Return a pointer to the WalIndexHdr structure in the wal-index.
 1.44839 +*/
 1.44840 +static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
 1.44841 +  assert( pWal->nWiData>0 && pWal->apWiData[0] );
 1.44842 +  return (volatile WalIndexHdr*)pWal->apWiData[0];
 1.44843 +}
 1.44844 +
 1.44845 +/*
 1.44846 +** The argument to this macro must be of type u32. On a little-endian
 1.44847 +** architecture, it returns the u32 value that results from interpreting
 1.44848 +** the 4 bytes as a big-endian value. On a big-endian architecture, it
 1.44849 +** returns the value that would be produced by intepreting the 4 bytes
 1.44850 +** of the input value as a little-endian integer.
 1.44851 +*/
 1.44852 +#define BYTESWAP32(x) ( \
 1.44853 +    (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
 1.44854 +  + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
 1.44855 +)
 1.44856 +
 1.44857 +/*
 1.44858 +** Generate or extend an 8 byte checksum based on the data in 
 1.44859 +** array aByte[] and the initial values of aIn[0] and aIn[1] (or
 1.44860 +** initial values of 0 and 0 if aIn==NULL).
 1.44861 +**
 1.44862 +** The checksum is written back into aOut[] before returning.
 1.44863 +**
 1.44864 +** nByte must be a positive multiple of 8.
 1.44865 +*/
 1.44866 +static void walChecksumBytes(
 1.44867 +  int nativeCksum, /* True for native byte-order, false for non-native */
 1.44868 +  u8 *a,           /* Content to be checksummed */
 1.44869 +  int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
 1.44870 +  const u32 *aIn,  /* Initial checksum value input */
 1.44871 +  u32 *aOut        /* OUT: Final checksum value output */
 1.44872 +){
 1.44873 +  u32 s1, s2;
 1.44874 +  u32 *aData = (u32 *)a;
 1.44875 +  u32 *aEnd = (u32 *)&a[nByte];
 1.44876 +
 1.44877 +  if( aIn ){
 1.44878 +    s1 = aIn[0];
 1.44879 +    s2 = aIn[1];
 1.44880 +  }else{
 1.44881 +    s1 = s2 = 0;
 1.44882 +  }
 1.44883 +
 1.44884 +  assert( nByte>=8 );
 1.44885 +  assert( (nByte&0x00000007)==0 );
 1.44886 +
 1.44887 +  if( nativeCksum ){
 1.44888 +    do {
 1.44889 +      s1 += *aData++ + s2;
 1.44890 +      s2 += *aData++ + s1;
 1.44891 +    }while( aData<aEnd );
 1.44892 +  }else{
 1.44893 +    do {
 1.44894 +      s1 += BYTESWAP32(aData[0]) + s2;
 1.44895 +      s2 += BYTESWAP32(aData[1]) + s1;
 1.44896 +      aData += 2;
 1.44897 +    }while( aData<aEnd );
 1.44898 +  }
 1.44899 +
 1.44900 +  aOut[0] = s1;
 1.44901 +  aOut[1] = s2;
 1.44902 +}
 1.44903 +
 1.44904 +static void walShmBarrier(Wal *pWal){
 1.44905 +  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
 1.44906 +    sqlite3OsShmBarrier(pWal->pDbFd);
 1.44907 +  }
 1.44908 +}
 1.44909 +
 1.44910 +/*
 1.44911 +** Write the header information in pWal->hdr into the wal-index.
 1.44912 +**
 1.44913 +** The checksum on pWal->hdr is updated before it is written.
 1.44914 +*/
 1.44915 +static void walIndexWriteHdr(Wal *pWal){
 1.44916 +  volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
 1.44917 +  const int nCksum = offsetof(WalIndexHdr, aCksum);
 1.44918 +
 1.44919 +  assert( pWal->writeLock );
 1.44920 +  pWal->hdr.isInit = 1;
 1.44921 +  pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
 1.44922 +  walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
 1.44923 +  memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
 1.44924 +  walShmBarrier(pWal);
 1.44925 +  memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
 1.44926 +}
 1.44927 +
 1.44928 +/*
 1.44929 +** This function encodes a single frame header and writes it to a buffer
 1.44930 +** supplied by the caller. A frame-header is made up of a series of 
 1.44931 +** 4-byte big-endian integers, as follows:
 1.44932 +**
 1.44933 +**     0: Page number.
 1.44934 +**     4: For commit records, the size of the database image in pages 
 1.44935 +**        after the commit. For all other records, zero.
 1.44936 +**     8: Salt-1 (copied from the wal-header)
 1.44937 +**    12: Salt-2 (copied from the wal-header)
 1.44938 +**    16: Checksum-1.
 1.44939 +**    20: Checksum-2.
 1.44940 +*/
 1.44941 +static void walEncodeFrame(
 1.44942 +  Wal *pWal,                      /* The write-ahead log */
 1.44943 +  u32 iPage,                      /* Database page number for frame */
 1.44944 +  u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
 1.44945 +  u8 *aData,                      /* Pointer to page data */
 1.44946 +  u8 *aFrame                      /* OUT: Write encoded frame here */
 1.44947 +){
 1.44948 +  int nativeCksum;                /* True for native byte-order checksums */
 1.44949 +  u32 *aCksum = pWal->hdr.aFrameCksum;
 1.44950 +  assert( WAL_FRAME_HDRSIZE==24 );
 1.44951 +  sqlite3Put4byte(&aFrame[0], iPage);
 1.44952 +  sqlite3Put4byte(&aFrame[4], nTruncate);
 1.44953 +  memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
 1.44954 +
 1.44955 +  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
 1.44956 +  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
 1.44957 +  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
 1.44958 +
 1.44959 +  sqlite3Put4byte(&aFrame[16], aCksum[0]);
 1.44960 +  sqlite3Put4byte(&aFrame[20], aCksum[1]);
 1.44961 +}
 1.44962 +
 1.44963 +/*
 1.44964 +** Check to see if the frame with header in aFrame[] and content
 1.44965 +** in aData[] is valid.  If it is a valid frame, fill *piPage and
 1.44966 +** *pnTruncate and return true.  Return if the frame is not valid.
 1.44967 +*/
 1.44968 +static int walDecodeFrame(
 1.44969 +  Wal *pWal,                      /* The write-ahead log */
 1.44970 +  u32 *piPage,                    /* OUT: Database page number for frame */
 1.44971 +  u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
 1.44972 +  u8 *aData,                      /* Pointer to page data (for checksum) */
 1.44973 +  u8 *aFrame                      /* Frame data */
 1.44974 +){
 1.44975 +  int nativeCksum;                /* True for native byte-order checksums */
 1.44976 +  u32 *aCksum = pWal->hdr.aFrameCksum;
 1.44977 +  u32 pgno;                       /* Page number of the frame */
 1.44978 +  assert( WAL_FRAME_HDRSIZE==24 );
 1.44979 +
 1.44980 +  /* A frame is only valid if the salt values in the frame-header
 1.44981 +  ** match the salt values in the wal-header. 
 1.44982 +  */
 1.44983 +  if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
 1.44984 +    return 0;
 1.44985 +  }
 1.44986 +
 1.44987 +  /* A frame is only valid if the page number is creater than zero.
 1.44988 +  */
 1.44989 +  pgno = sqlite3Get4byte(&aFrame[0]);
 1.44990 +  if( pgno==0 ){
 1.44991 +    return 0;
 1.44992 +  }
 1.44993 +
 1.44994 +  /* A frame is only valid if a checksum of the WAL header,
 1.44995 +  ** all prior frams, the first 16 bytes of this frame-header, 
 1.44996 +  ** and the frame-data matches the checksum in the last 8 
 1.44997 +  ** bytes of this frame-header.
 1.44998 +  */
 1.44999 +  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
 1.45000 +  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
 1.45001 +  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
 1.45002 +  if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) 
 1.45003 +   || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) 
 1.45004 +  ){
 1.45005 +    /* Checksum failed. */
 1.45006 +    return 0;
 1.45007 +  }
 1.45008 +
 1.45009 +  /* If we reach this point, the frame is valid.  Return the page number
 1.45010 +  ** and the new database size.
 1.45011 +  */
 1.45012 +  *piPage = pgno;
 1.45013 +  *pnTruncate = sqlite3Get4byte(&aFrame[4]);
 1.45014 +  return 1;
 1.45015 +}
 1.45016 +
 1.45017 +
 1.45018 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.45019 +/*
 1.45020 +** Names of locks.  This routine is used to provide debugging output and is not
 1.45021 +** a part of an ordinary build.
 1.45022 +*/
 1.45023 +static const char *walLockName(int lockIdx){
 1.45024 +  if( lockIdx==WAL_WRITE_LOCK ){
 1.45025 +    return "WRITE-LOCK";
 1.45026 +  }else if( lockIdx==WAL_CKPT_LOCK ){
 1.45027 +    return "CKPT-LOCK";
 1.45028 +  }else if( lockIdx==WAL_RECOVER_LOCK ){
 1.45029 +    return "RECOVER-LOCK";
 1.45030 +  }else{
 1.45031 +    static char zName[15];
 1.45032 +    sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
 1.45033 +                     lockIdx-WAL_READ_LOCK(0));
 1.45034 +    return zName;
 1.45035 +  }
 1.45036 +}
 1.45037 +#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
 1.45038 +    
 1.45039 +
 1.45040 +/*
 1.45041 +** Set or release locks on the WAL.  Locks are either shared or exclusive.
 1.45042 +** A lock cannot be moved directly between shared and exclusive - it must go
 1.45043 +** through the unlocked state first.
 1.45044 +**
 1.45045 +** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
 1.45046 +*/
 1.45047 +static int walLockShared(Wal *pWal, int lockIdx){
 1.45048 +  int rc;
 1.45049 +  if( pWal->exclusiveMode ) return SQLITE_OK;
 1.45050 +  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
 1.45051 +                        SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
 1.45052 +  WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
 1.45053 +            walLockName(lockIdx), rc ? "failed" : "ok"));
 1.45054 +  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
 1.45055 +  return rc;
 1.45056 +}
 1.45057 +static void walUnlockShared(Wal *pWal, int lockIdx){
 1.45058 +  if( pWal->exclusiveMode ) return;
 1.45059 +  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
 1.45060 +                         SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
 1.45061 +  WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
 1.45062 +}
 1.45063 +static int walLockExclusive(Wal *pWal, int lockIdx, int n){
 1.45064 +  int rc;
 1.45065 +  if( pWal->exclusiveMode ) return SQLITE_OK;
 1.45066 +  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
 1.45067 +                        SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
 1.45068 +  WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
 1.45069 +            walLockName(lockIdx), n, rc ? "failed" : "ok"));
 1.45070 +  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
 1.45071 +  return rc;
 1.45072 +}
 1.45073 +static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
 1.45074 +  if( pWal->exclusiveMode ) return;
 1.45075 +  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
 1.45076 +                         SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
 1.45077 +  WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
 1.45078 +             walLockName(lockIdx), n));
 1.45079 +}
 1.45080 +
 1.45081 +/*
 1.45082 +** Compute a hash on a page number.  The resulting hash value must land
 1.45083 +** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
 1.45084 +** the hash to the next value in the event of a collision.
 1.45085 +*/
 1.45086 +static int walHash(u32 iPage){
 1.45087 +  assert( iPage>0 );
 1.45088 +  assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
 1.45089 +  return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
 1.45090 +}
 1.45091 +static int walNextHash(int iPriorHash){
 1.45092 +  return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
 1.45093 +}
 1.45094 +
 1.45095 +/* 
 1.45096 +** Return pointers to the hash table and page number array stored on
 1.45097 +** page iHash of the wal-index. The wal-index is broken into 32KB pages
 1.45098 +** numbered starting from 0.
 1.45099 +**
 1.45100 +** Set output variable *paHash to point to the start of the hash table
 1.45101 +** in the wal-index file. Set *piZero to one less than the frame 
 1.45102 +** number of the first frame indexed by this hash table. If a
 1.45103 +** slot in the hash table is set to N, it refers to frame number 
 1.45104 +** (*piZero+N) in the log.
 1.45105 +**
 1.45106 +** Finally, set *paPgno so that *paPgno[1] is the page number of the
 1.45107 +** first frame indexed by the hash table, frame (*piZero+1).
 1.45108 +*/
 1.45109 +static int walHashGet(
 1.45110 +  Wal *pWal,                      /* WAL handle */
 1.45111 +  int iHash,                      /* Find the iHash'th table */
 1.45112 +  volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
 1.45113 +  volatile u32 **paPgno,          /* OUT: Pointer to page number array */
 1.45114 +  u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
 1.45115 +){
 1.45116 +  int rc;                         /* Return code */
 1.45117 +  volatile u32 *aPgno;
 1.45118 +
 1.45119 +  rc = walIndexPage(pWal, iHash, &aPgno);
 1.45120 +  assert( rc==SQLITE_OK || iHash>0 );
 1.45121 +
 1.45122 +  if( rc==SQLITE_OK ){
 1.45123 +    u32 iZero;
 1.45124 +    volatile ht_slot *aHash;
 1.45125 +
 1.45126 +    aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
 1.45127 +    if( iHash==0 ){
 1.45128 +      aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
 1.45129 +      iZero = 0;
 1.45130 +    }else{
 1.45131 +      iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
 1.45132 +    }
 1.45133 +  
 1.45134 +    *paPgno = &aPgno[-1];
 1.45135 +    *paHash = aHash;
 1.45136 +    *piZero = iZero;
 1.45137 +  }
 1.45138 +  return rc;
 1.45139 +}
 1.45140 +
 1.45141 +/*
 1.45142 +** Return the number of the wal-index page that contains the hash-table
 1.45143 +** and page-number array that contain entries corresponding to WAL frame
 1.45144 +** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages 
 1.45145 +** are numbered starting from 0.
 1.45146 +*/
 1.45147 +static int walFramePage(u32 iFrame){
 1.45148 +  int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
 1.45149 +  assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
 1.45150 +       && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
 1.45151 +       && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
 1.45152 +       && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
 1.45153 +       && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
 1.45154 +  );
 1.45155 +  return iHash;
 1.45156 +}
 1.45157 +
 1.45158 +/*
 1.45159 +** Return the page number associated with frame iFrame in this WAL.
 1.45160 +*/
 1.45161 +static u32 walFramePgno(Wal *pWal, u32 iFrame){
 1.45162 +  int iHash = walFramePage(iFrame);
 1.45163 +  if( iHash==0 ){
 1.45164 +    return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
 1.45165 +  }
 1.45166 +  return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
 1.45167 +}
 1.45168 +
 1.45169 +/*
 1.45170 +** Remove entries from the hash table that point to WAL slots greater
 1.45171 +** than pWal->hdr.mxFrame.
 1.45172 +**
 1.45173 +** This function is called whenever pWal->hdr.mxFrame is decreased due
 1.45174 +** to a rollback or savepoint.
 1.45175 +**
 1.45176 +** At most only the hash table containing pWal->hdr.mxFrame needs to be
 1.45177 +** updated.  Any later hash tables will be automatically cleared when
 1.45178 +** pWal->hdr.mxFrame advances to the point where those hash tables are
 1.45179 +** actually needed.
 1.45180 +*/
 1.45181 +static void walCleanupHash(Wal *pWal){
 1.45182 +  volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
 1.45183 +  volatile u32 *aPgno = 0;        /* Page number array for hash table */
 1.45184 +  u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
 1.45185 +  int iLimit = 0;                 /* Zero values greater than this */
 1.45186 +  int nByte;                      /* Number of bytes to zero in aPgno[] */
 1.45187 +  int i;                          /* Used to iterate through aHash[] */
 1.45188 +
 1.45189 +  assert( pWal->writeLock );
 1.45190 +  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
 1.45191 +  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
 1.45192 +  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
 1.45193 +
 1.45194 +  if( pWal->hdr.mxFrame==0 ) return;
 1.45195 +
 1.45196 +  /* Obtain pointers to the hash-table and page-number array containing 
 1.45197 +  ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
 1.45198 +  ** that the page said hash-table and array reside on is already mapped.
 1.45199 +  */
 1.45200 +  assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
 1.45201 +  assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
 1.45202 +  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
 1.45203 +
 1.45204 +  /* Zero all hash-table entries that correspond to frame numbers greater
 1.45205 +  ** than pWal->hdr.mxFrame.
 1.45206 +  */
 1.45207 +  iLimit = pWal->hdr.mxFrame - iZero;
 1.45208 +  assert( iLimit>0 );
 1.45209 +  for(i=0; i<HASHTABLE_NSLOT; i++){
 1.45210 +    if( aHash[i]>iLimit ){
 1.45211 +      aHash[i] = 0;
 1.45212 +    }
 1.45213 +  }
 1.45214 +  
 1.45215 +  /* Zero the entries in the aPgno array that correspond to frames with
 1.45216 +  ** frame numbers greater than pWal->hdr.mxFrame. 
 1.45217 +  */
 1.45218 +  nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
 1.45219 +  memset((void *)&aPgno[iLimit+1], 0, nByte);
 1.45220 +
 1.45221 +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 1.45222 +  /* Verify that the every entry in the mapping region is still reachable
 1.45223 +  ** via the hash table even after the cleanup.
 1.45224 +  */
 1.45225 +  if( iLimit ){
 1.45226 +    int i;           /* Loop counter */
 1.45227 +    int iKey;        /* Hash key */
 1.45228 +    for(i=1; i<=iLimit; i++){
 1.45229 +      for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
 1.45230 +        if( aHash[iKey]==i ) break;
 1.45231 +      }
 1.45232 +      assert( aHash[iKey]==i );
 1.45233 +    }
 1.45234 +  }
 1.45235 +#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
 1.45236 +}
 1.45237 +
 1.45238 +
 1.45239 +/*
 1.45240 +** Set an entry in the wal-index that will map database page number
 1.45241 +** pPage into WAL frame iFrame.
 1.45242 +*/
 1.45243 +static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
 1.45244 +  int rc;                         /* Return code */
 1.45245 +  u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
 1.45246 +  volatile u32 *aPgno = 0;        /* Page number array */
 1.45247 +  volatile ht_slot *aHash = 0;    /* Hash table */
 1.45248 +
 1.45249 +  rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
 1.45250 +
 1.45251 +  /* Assuming the wal-index file was successfully mapped, populate the
 1.45252 +  ** page number array and hash table entry.
 1.45253 +  */
 1.45254 +  if( rc==SQLITE_OK ){
 1.45255 +    int iKey;                     /* Hash table key */
 1.45256 +    int idx;                      /* Value to write to hash-table slot */
 1.45257 +    int nCollide;                 /* Number of hash collisions */
 1.45258 +
 1.45259 +    idx = iFrame - iZero;
 1.45260 +    assert( idx <= HASHTABLE_NSLOT/2 + 1 );
 1.45261 +    
 1.45262 +    /* If this is the first entry to be added to this hash-table, zero the
 1.45263 +    ** entire hash table and aPgno[] array before proceding. 
 1.45264 +    */
 1.45265 +    if( idx==1 ){
 1.45266 +      int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
 1.45267 +      memset((void*)&aPgno[1], 0, nByte);
 1.45268 +    }
 1.45269 +
 1.45270 +    /* If the entry in aPgno[] is already set, then the previous writer
 1.45271 +    ** must have exited unexpectedly in the middle of a transaction (after
 1.45272 +    ** writing one or more dirty pages to the WAL to free up memory). 
 1.45273 +    ** Remove the remnants of that writers uncommitted transaction from 
 1.45274 +    ** the hash-table before writing any new entries.
 1.45275 +    */
 1.45276 +    if( aPgno[idx] ){
 1.45277 +      walCleanupHash(pWal);
 1.45278 +      assert( !aPgno[idx] );
 1.45279 +    }
 1.45280 +
 1.45281 +    /* Write the aPgno[] array entry and the hash-table slot. */
 1.45282 +    nCollide = idx;
 1.45283 +    for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
 1.45284 +      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
 1.45285 +    }
 1.45286 +    aPgno[idx] = iPage;
 1.45287 +    aHash[iKey] = (ht_slot)idx;
 1.45288 +
 1.45289 +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 1.45290 +    /* Verify that the number of entries in the hash table exactly equals
 1.45291 +    ** the number of entries in the mapping region.
 1.45292 +    */
 1.45293 +    {
 1.45294 +      int i;           /* Loop counter */
 1.45295 +      int nEntry = 0;  /* Number of entries in the hash table */
 1.45296 +      for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
 1.45297 +      assert( nEntry==idx );
 1.45298 +    }
 1.45299 +
 1.45300 +    /* Verify that the every entry in the mapping region is reachable
 1.45301 +    ** via the hash table.  This turns out to be a really, really expensive
 1.45302 +    ** thing to check, so only do this occasionally - not on every
 1.45303 +    ** iteration.
 1.45304 +    */
 1.45305 +    if( (idx&0x3ff)==0 ){
 1.45306 +      int i;           /* Loop counter */
 1.45307 +      for(i=1; i<=idx; i++){
 1.45308 +        for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
 1.45309 +          if( aHash[iKey]==i ) break;
 1.45310 +        }
 1.45311 +        assert( aHash[iKey]==i );
 1.45312 +      }
 1.45313 +    }
 1.45314 +#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
 1.45315 +  }
 1.45316 +
 1.45317 +
 1.45318 +  return rc;
 1.45319 +}
 1.45320 +
 1.45321 +
 1.45322 +/*
 1.45323 +** Recover the wal-index by reading the write-ahead log file. 
 1.45324 +**
 1.45325 +** This routine first tries to establish an exclusive lock on the
 1.45326 +** wal-index to prevent other threads/processes from doing anything
 1.45327 +** with the WAL or wal-index while recovery is running.  The
 1.45328 +** WAL_RECOVER_LOCK is also held so that other threads will know
 1.45329 +** that this thread is running recovery.  If unable to establish
 1.45330 +** the necessary locks, this routine returns SQLITE_BUSY.
 1.45331 +*/
 1.45332 +static int walIndexRecover(Wal *pWal){
 1.45333 +  int rc;                         /* Return Code */
 1.45334 +  i64 nSize;                      /* Size of log file */
 1.45335 +  u32 aFrameCksum[2] = {0, 0};
 1.45336 +  int iLock;                      /* Lock offset to lock for checkpoint */
 1.45337 +  int nLock;                      /* Number of locks to hold */
 1.45338 +
 1.45339 +  /* Obtain an exclusive lock on all byte in the locking range not already
 1.45340 +  ** locked by the caller. The caller is guaranteed to have locked the
 1.45341 +  ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
 1.45342 +  ** If successful, the same bytes that are locked here are unlocked before
 1.45343 +  ** this function returns.
 1.45344 +  */
 1.45345 +  assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
 1.45346 +  assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
 1.45347 +  assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
 1.45348 +  assert( pWal->writeLock );
 1.45349 +  iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
 1.45350 +  nLock = SQLITE_SHM_NLOCK - iLock;
 1.45351 +  rc = walLockExclusive(pWal, iLock, nLock);
 1.45352 +  if( rc ){
 1.45353 +    return rc;
 1.45354 +  }
 1.45355 +  WALTRACE(("WAL%p: recovery begin...\n", pWal));
 1.45356 +
 1.45357 +  memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 1.45358 +
 1.45359 +  rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
 1.45360 +  if( rc!=SQLITE_OK ){
 1.45361 +    goto recovery_error;
 1.45362 +  }
 1.45363 +
 1.45364 +  if( nSize>WAL_HDRSIZE ){
 1.45365 +    u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
 1.45366 +    u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
 1.45367 +    int szFrame;                  /* Number of bytes in buffer aFrame[] */
 1.45368 +    u8 *aData;                    /* Pointer to data part of aFrame buffer */
 1.45369 +    int iFrame;                   /* Index of last frame read */
 1.45370 +    i64 iOffset;                  /* Next offset to read from log file */
 1.45371 +    int szPage;                   /* Page size according to the log */
 1.45372 +    u32 magic;                    /* Magic value read from WAL header */
 1.45373 +    u32 version;                  /* Magic value read from WAL header */
 1.45374 +    int isValid;                  /* True if this frame is valid */
 1.45375 +
 1.45376 +    /* Read in the WAL header. */
 1.45377 +    rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
 1.45378 +    if( rc!=SQLITE_OK ){
 1.45379 +      goto recovery_error;
 1.45380 +    }
 1.45381 +
 1.45382 +    /* If the database page size is not a power of two, or is greater than
 1.45383 +    ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid 
 1.45384 +    ** data. Similarly, if the 'magic' value is invalid, ignore the whole
 1.45385 +    ** WAL file.
 1.45386 +    */
 1.45387 +    magic = sqlite3Get4byte(&aBuf[0]);
 1.45388 +    szPage = sqlite3Get4byte(&aBuf[8]);
 1.45389 +    if( (magic&0xFFFFFFFE)!=WAL_MAGIC 
 1.45390 +     || szPage&(szPage-1) 
 1.45391 +     || szPage>SQLITE_MAX_PAGE_SIZE 
 1.45392 +     || szPage<512 
 1.45393 +    ){
 1.45394 +      goto finished;
 1.45395 +    }
 1.45396 +    pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
 1.45397 +    pWal->szPage = szPage;
 1.45398 +    pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
 1.45399 +    memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
 1.45400 +
 1.45401 +    /* Verify that the WAL header checksum is correct */
 1.45402 +    walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, 
 1.45403 +        aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
 1.45404 +    );
 1.45405 +    if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
 1.45406 +     || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
 1.45407 +    ){
 1.45408 +      goto finished;
 1.45409 +    }
 1.45410 +
 1.45411 +    /* Verify that the version number on the WAL format is one that
 1.45412 +    ** are able to understand */
 1.45413 +    version = sqlite3Get4byte(&aBuf[4]);
 1.45414 +    if( version!=WAL_MAX_VERSION ){
 1.45415 +      rc = SQLITE_CANTOPEN_BKPT;
 1.45416 +      goto finished;
 1.45417 +    }
 1.45418 +
 1.45419 +    /* Malloc a buffer to read frames into. */
 1.45420 +    szFrame = szPage + WAL_FRAME_HDRSIZE;
 1.45421 +    aFrame = (u8 *)sqlite3_malloc(szFrame);
 1.45422 +    if( !aFrame ){
 1.45423 +      rc = SQLITE_NOMEM;
 1.45424 +      goto recovery_error;
 1.45425 +    }
 1.45426 +    aData = &aFrame[WAL_FRAME_HDRSIZE];
 1.45427 +
 1.45428 +    /* Read all frames from the log file. */
 1.45429 +    iFrame = 0;
 1.45430 +    for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
 1.45431 +      u32 pgno;                   /* Database page number for frame */
 1.45432 +      u32 nTruncate;              /* dbsize field from frame header */
 1.45433 +
 1.45434 +      /* Read and decode the next log frame. */
 1.45435 +      iFrame++;
 1.45436 +      rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
 1.45437 +      if( rc!=SQLITE_OK ) break;
 1.45438 +      isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
 1.45439 +      if( !isValid ) break;
 1.45440 +      rc = walIndexAppend(pWal, iFrame, pgno);
 1.45441 +      if( rc!=SQLITE_OK ) break;
 1.45442 +
 1.45443 +      /* If nTruncate is non-zero, this is a commit record. */
 1.45444 +      if( nTruncate ){
 1.45445 +        pWal->hdr.mxFrame = iFrame;
 1.45446 +        pWal->hdr.nPage = nTruncate;
 1.45447 +        pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
 1.45448 +        testcase( szPage<=32768 );
 1.45449 +        testcase( szPage>=65536 );
 1.45450 +        aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
 1.45451 +        aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
 1.45452 +      }
 1.45453 +    }
 1.45454 +
 1.45455 +    sqlite3_free(aFrame);
 1.45456 +  }
 1.45457 +
 1.45458 +finished:
 1.45459 +  if( rc==SQLITE_OK ){
 1.45460 +    volatile WalCkptInfo *pInfo;
 1.45461 +    int i;
 1.45462 +    pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
 1.45463 +    pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
 1.45464 +    walIndexWriteHdr(pWal);
 1.45465 +
 1.45466 +    /* Reset the checkpoint-header. This is safe because this thread is 
 1.45467 +    ** currently holding locks that exclude all other readers, writers and
 1.45468 +    ** checkpointers.
 1.45469 +    */
 1.45470 +    pInfo = walCkptInfo(pWal);
 1.45471 +    pInfo->nBackfill = 0;
 1.45472 +    pInfo->aReadMark[0] = 0;
 1.45473 +    for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 1.45474 +    if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
 1.45475 +
 1.45476 +    /* If more than one frame was recovered from the log file, report an
 1.45477 +    ** event via sqlite3_log(). This is to help with identifying performance
 1.45478 +    ** problems caused by applications routinely shutting down without
 1.45479 +    ** checkpointing the log file.
 1.45480 +    */
 1.45481 +    if( pWal->hdr.nPage ){
 1.45482 +      sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
 1.45483 +          pWal->hdr.nPage, pWal->zWalName
 1.45484 +      );
 1.45485 +    }
 1.45486 +  }
 1.45487 +
 1.45488 +recovery_error:
 1.45489 +  WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
 1.45490 +  walUnlockExclusive(pWal, iLock, nLock);
 1.45491 +  return rc;
 1.45492 +}
 1.45493 +
 1.45494 +/*
 1.45495 +** Close an open wal-index.
 1.45496 +*/
 1.45497 +static void walIndexClose(Wal *pWal, int isDelete){
 1.45498 +  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 1.45499 +    int i;
 1.45500 +    for(i=0; i<pWal->nWiData; i++){
 1.45501 +      sqlite3_free((void *)pWal->apWiData[i]);
 1.45502 +      pWal->apWiData[i] = 0;
 1.45503 +    }
 1.45504 +  }else{
 1.45505 +    sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
 1.45506 +  }
 1.45507 +}
 1.45508 +
 1.45509 +/* 
 1.45510 +** Open a connection to the WAL file zWalName. The database file must 
 1.45511 +** already be opened on connection pDbFd. The buffer that zWalName points
 1.45512 +** to must remain valid for the lifetime of the returned Wal* handle.
 1.45513 +**
 1.45514 +** A SHARED lock should be held on the database file when this function
 1.45515 +** is called. The purpose of this SHARED lock is to prevent any other
 1.45516 +** client from unlinking the WAL or wal-index file. If another process
 1.45517 +** were to do this just after this client opened one of these files, the
 1.45518 +** system would be badly broken.
 1.45519 +**
 1.45520 +** If the log file is successfully opened, SQLITE_OK is returned and 
 1.45521 +** *ppWal is set to point to a new WAL handle. If an error occurs,
 1.45522 +** an SQLite error code is returned and *ppWal is left unmodified.
 1.45523 +*/
 1.45524 +SQLITE_PRIVATE int sqlite3WalOpen(
 1.45525 +  sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
 1.45526 +  sqlite3_file *pDbFd,            /* The open database file */
 1.45527 +  const char *zWalName,           /* Name of the WAL file */
 1.45528 +  int bNoShm,                     /* True to run in heap-memory mode */
 1.45529 +  i64 mxWalSize,                  /* Truncate WAL to this size on reset */
 1.45530 +  Wal **ppWal                     /* OUT: Allocated Wal handle */
 1.45531 +){
 1.45532 +  int rc;                         /* Return Code */
 1.45533 +  Wal *pRet;                      /* Object to allocate and return */
 1.45534 +  int flags;                      /* Flags passed to OsOpen() */
 1.45535 +
 1.45536 +  assert( zWalName && zWalName[0] );
 1.45537 +  assert( pDbFd );
 1.45538 +
 1.45539 +  /* In the amalgamation, the os_unix.c and os_win.c source files come before
 1.45540 +  ** this source file.  Verify that the #defines of the locking byte offsets
 1.45541 +  ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
 1.45542 +  */
 1.45543 +#ifdef WIN_SHM_BASE
 1.45544 +  assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
 1.45545 +#endif
 1.45546 +#ifdef UNIX_SHM_BASE
 1.45547 +  assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
 1.45548 +#endif
 1.45549 +
 1.45550 +
 1.45551 +  /* Allocate an instance of struct Wal to return. */
 1.45552 +  *ppWal = 0;
 1.45553 +  pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
 1.45554 +  if( !pRet ){
 1.45555 +    return SQLITE_NOMEM;
 1.45556 +  }
 1.45557 +
 1.45558 +  pRet->pVfs = pVfs;
 1.45559 +  pRet->pWalFd = (sqlite3_file *)&pRet[1];
 1.45560 +  pRet->pDbFd = pDbFd;
 1.45561 +  pRet->readLock = -1;
 1.45562 +  pRet->mxWalSize = mxWalSize;
 1.45563 +  pRet->zWalName = zWalName;
 1.45564 +  pRet->syncHeader = 1;
 1.45565 +  pRet->padToSectorBoundary = 1;
 1.45566 +  pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
 1.45567 +
 1.45568 +  /* Open file handle on the write-ahead log file. */
 1.45569 +  flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
 1.45570 +  rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
 1.45571 +  if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
 1.45572 +    pRet->readOnly = WAL_RDONLY;
 1.45573 +  }
 1.45574 +
 1.45575 +  if( rc!=SQLITE_OK ){
 1.45576 +    walIndexClose(pRet, 0);
 1.45577 +    sqlite3OsClose(pRet->pWalFd);
 1.45578 +    sqlite3_free(pRet);
 1.45579 +  }else{
 1.45580 +    int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
 1.45581 +    if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
 1.45582 +    if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
 1.45583 +      pRet->padToSectorBoundary = 0;
 1.45584 +    }
 1.45585 +    *ppWal = pRet;
 1.45586 +    WALTRACE(("WAL%d: opened\n", pRet));
 1.45587 +  }
 1.45588 +  return rc;
 1.45589 +}
 1.45590 +
 1.45591 +/*
 1.45592 +** Change the size to which the WAL file is trucated on each reset.
 1.45593 +*/
 1.45594 +SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
 1.45595 +  if( pWal ) pWal->mxWalSize = iLimit;
 1.45596 +}
 1.45597 +
 1.45598 +/*
 1.45599 +** Find the smallest page number out of all pages held in the WAL that
 1.45600 +** has not been returned by any prior invocation of this method on the
 1.45601 +** same WalIterator object.   Write into *piFrame the frame index where
 1.45602 +** that page was last written into the WAL.  Write into *piPage the page
 1.45603 +** number.
 1.45604 +**
 1.45605 +** Return 0 on success.  If there are no pages in the WAL with a page
 1.45606 +** number larger than *piPage, then return 1.
 1.45607 +*/
 1.45608 +static int walIteratorNext(
 1.45609 +  WalIterator *p,               /* Iterator */
 1.45610 +  u32 *piPage,                  /* OUT: The page number of the next page */
 1.45611 +  u32 *piFrame                  /* OUT: Wal frame index of next page */
 1.45612 +){
 1.45613 +  u32 iMin;                     /* Result pgno must be greater than iMin */
 1.45614 +  u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
 1.45615 +  int i;                        /* For looping through segments */
 1.45616 +
 1.45617 +  iMin = p->iPrior;
 1.45618 +  assert( iMin<0xffffffff );
 1.45619 +  for(i=p->nSegment-1; i>=0; i--){
 1.45620 +    struct WalSegment *pSegment = &p->aSegment[i];
 1.45621 +    while( pSegment->iNext<pSegment->nEntry ){
 1.45622 +      u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
 1.45623 +      if( iPg>iMin ){
 1.45624 +        if( iPg<iRet ){
 1.45625 +          iRet = iPg;
 1.45626 +          *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
 1.45627 +        }
 1.45628 +        break;
 1.45629 +      }
 1.45630 +      pSegment->iNext++;
 1.45631 +    }
 1.45632 +  }
 1.45633 +
 1.45634 +  *piPage = p->iPrior = iRet;
 1.45635 +  return (iRet==0xFFFFFFFF);
 1.45636 +}
 1.45637 +
 1.45638 +/*
 1.45639 +** This function merges two sorted lists into a single sorted list.
 1.45640 +**
 1.45641 +** aLeft[] and aRight[] are arrays of indices.  The sort key is
 1.45642 +** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
 1.45643 +** is guaranteed for all J<K:
 1.45644 +**
 1.45645 +**        aContent[aLeft[J]] < aContent[aLeft[K]]
 1.45646 +**        aContent[aRight[J]] < aContent[aRight[K]]
 1.45647 +**
 1.45648 +** This routine overwrites aRight[] with a new (probably longer) sequence
 1.45649 +** of indices such that the aRight[] contains every index that appears in
 1.45650 +** either aLeft[] or the old aRight[] and such that the second condition
 1.45651 +** above is still met.
 1.45652 +**
 1.45653 +** The aContent[aLeft[X]] values will be unique for all X.  And the
 1.45654 +** aContent[aRight[X]] values will be unique too.  But there might be
 1.45655 +** one or more combinations of X and Y such that
 1.45656 +**
 1.45657 +**      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
 1.45658 +**
 1.45659 +** When that happens, omit the aLeft[X] and use the aRight[Y] index.
 1.45660 +*/
 1.45661 +static void walMerge(
 1.45662 +  const u32 *aContent,            /* Pages in wal - keys for the sort */
 1.45663 +  ht_slot *aLeft,                 /* IN: Left hand input list */
 1.45664 +  int nLeft,                      /* IN: Elements in array *paLeft */
 1.45665 +  ht_slot **paRight,              /* IN/OUT: Right hand input list */
 1.45666 +  int *pnRight,                   /* IN/OUT: Elements in *paRight */
 1.45667 +  ht_slot *aTmp                   /* Temporary buffer */
 1.45668 +){
 1.45669 +  int iLeft = 0;                  /* Current index in aLeft */
 1.45670 +  int iRight = 0;                 /* Current index in aRight */
 1.45671 +  int iOut = 0;                   /* Current index in output buffer */
 1.45672 +  int nRight = *pnRight;
 1.45673 +  ht_slot *aRight = *paRight;
 1.45674 +
 1.45675 +  assert( nLeft>0 && nRight>0 );
 1.45676 +  while( iRight<nRight || iLeft<nLeft ){
 1.45677 +    ht_slot logpage;
 1.45678 +    Pgno dbpage;
 1.45679 +
 1.45680 +    if( (iLeft<nLeft) 
 1.45681 +     && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
 1.45682 +    ){
 1.45683 +      logpage = aLeft[iLeft++];
 1.45684 +    }else{
 1.45685 +      logpage = aRight[iRight++];
 1.45686 +    }
 1.45687 +    dbpage = aContent[logpage];
 1.45688 +
 1.45689 +    aTmp[iOut++] = logpage;
 1.45690 +    if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
 1.45691 +
 1.45692 +    assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
 1.45693 +    assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
 1.45694 +  }
 1.45695 +
 1.45696 +  *paRight = aLeft;
 1.45697 +  *pnRight = iOut;
 1.45698 +  memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
 1.45699 +}
 1.45700 +
 1.45701 +/*
 1.45702 +** Sort the elements in list aList using aContent[] as the sort key.
 1.45703 +** Remove elements with duplicate keys, preferring to keep the
 1.45704 +** larger aList[] values.
 1.45705 +**
 1.45706 +** The aList[] entries are indices into aContent[].  The values in
 1.45707 +** aList[] are to be sorted so that for all J<K:
 1.45708 +**
 1.45709 +**      aContent[aList[J]] < aContent[aList[K]]
 1.45710 +**
 1.45711 +** For any X and Y such that
 1.45712 +**
 1.45713 +**      aContent[aList[X]] == aContent[aList[Y]]
 1.45714 +**
 1.45715 +** Keep the larger of the two values aList[X] and aList[Y] and discard
 1.45716 +** the smaller.
 1.45717 +*/
 1.45718 +static void walMergesort(
 1.45719 +  const u32 *aContent,            /* Pages in wal */
 1.45720 +  ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
 1.45721 +  ht_slot *aList,                 /* IN/OUT: List to sort */
 1.45722 +  int *pnList                     /* IN/OUT: Number of elements in aList[] */
 1.45723 +){
 1.45724 +  struct Sublist {
 1.45725 +    int nList;                    /* Number of elements in aList */
 1.45726 +    ht_slot *aList;               /* Pointer to sub-list content */
 1.45727 +  };
 1.45728 +
 1.45729 +  const int nList = *pnList;      /* Size of input list */
 1.45730 +  int nMerge = 0;                 /* Number of elements in list aMerge */
 1.45731 +  ht_slot *aMerge = 0;            /* List to be merged */
 1.45732 +  int iList;                      /* Index into input list */
 1.45733 +  int iSub = 0;                   /* Index into aSub array */
 1.45734 +  struct Sublist aSub[13];        /* Array of sub-lists */
 1.45735 +
 1.45736 +  memset(aSub, 0, sizeof(aSub));
 1.45737 +  assert( nList<=HASHTABLE_NPAGE && nList>0 );
 1.45738 +  assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
 1.45739 +
 1.45740 +  for(iList=0; iList<nList; iList++){
 1.45741 +    nMerge = 1;
 1.45742 +    aMerge = &aList[iList];
 1.45743 +    for(iSub=0; iList & (1<<iSub); iSub++){
 1.45744 +      struct Sublist *p = &aSub[iSub];
 1.45745 +      assert( p->aList && p->nList<=(1<<iSub) );
 1.45746 +      assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
 1.45747 +      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
 1.45748 +    }
 1.45749 +    aSub[iSub].aList = aMerge;
 1.45750 +    aSub[iSub].nList = nMerge;
 1.45751 +  }
 1.45752 +
 1.45753 +  for(iSub++; iSub<ArraySize(aSub); iSub++){
 1.45754 +    if( nList & (1<<iSub) ){
 1.45755 +      struct Sublist *p = &aSub[iSub];
 1.45756 +      assert( p->nList<=(1<<iSub) );
 1.45757 +      assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
 1.45758 +      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
 1.45759 +    }
 1.45760 +  }
 1.45761 +  assert( aMerge==aList );
 1.45762 +  *pnList = nMerge;
 1.45763 +
 1.45764 +#ifdef SQLITE_DEBUG
 1.45765 +  {
 1.45766 +    int i;
 1.45767 +    for(i=1; i<*pnList; i++){
 1.45768 +      assert( aContent[aList[i]] > aContent[aList[i-1]] );
 1.45769 +    }
 1.45770 +  }
 1.45771 +#endif
 1.45772 +}
 1.45773 +
 1.45774 +/* 
 1.45775 +** Free an iterator allocated by walIteratorInit().
 1.45776 +*/
 1.45777 +static void walIteratorFree(WalIterator *p){
 1.45778 +  sqlite3ScratchFree(p);
 1.45779 +}
 1.45780 +
 1.45781 +/*
 1.45782 +** Construct a WalInterator object that can be used to loop over all 
 1.45783 +** pages in the WAL in ascending order. The caller must hold the checkpoint
 1.45784 +** lock.
 1.45785 +**
 1.45786 +** On success, make *pp point to the newly allocated WalInterator object
 1.45787 +** return SQLITE_OK. Otherwise, return an error code. If this routine
 1.45788 +** returns an error, the value of *pp is undefined.
 1.45789 +**
 1.45790 +** The calling routine should invoke walIteratorFree() to destroy the
 1.45791 +** WalIterator object when it has finished with it.
 1.45792 +*/
 1.45793 +static int walIteratorInit(Wal *pWal, WalIterator **pp){
 1.45794 +  WalIterator *p;                 /* Return value */
 1.45795 +  int nSegment;                   /* Number of segments to merge */
 1.45796 +  u32 iLast;                      /* Last frame in log */
 1.45797 +  int nByte;                      /* Number of bytes to allocate */
 1.45798 +  int i;                          /* Iterator variable */
 1.45799 +  ht_slot *aTmp;                  /* Temp space used by merge-sort */
 1.45800 +  int rc = SQLITE_OK;             /* Return Code */
 1.45801 +
 1.45802 +  /* This routine only runs while holding the checkpoint lock. And
 1.45803 +  ** it only runs if there is actually content in the log (mxFrame>0).
 1.45804 +  */
 1.45805 +  assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
 1.45806 +  iLast = pWal->hdr.mxFrame;
 1.45807 +
 1.45808 +  /* Allocate space for the WalIterator object. */
 1.45809 +  nSegment = walFramePage(iLast) + 1;
 1.45810 +  nByte = sizeof(WalIterator) 
 1.45811 +        + (nSegment-1)*sizeof(struct WalSegment)
 1.45812 +        + iLast*sizeof(ht_slot);
 1.45813 +  p = (WalIterator *)sqlite3ScratchMalloc(nByte);
 1.45814 +  if( !p ){
 1.45815 +    return SQLITE_NOMEM;
 1.45816 +  }
 1.45817 +  memset(p, 0, nByte);
 1.45818 +  p->nSegment = nSegment;
 1.45819 +
 1.45820 +  /* Allocate temporary space used by the merge-sort routine. This block
 1.45821 +  ** of memory will be freed before this function returns.
 1.45822 +  */
 1.45823 +  aTmp = (ht_slot *)sqlite3ScratchMalloc(
 1.45824 +      sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
 1.45825 +  );
 1.45826 +  if( !aTmp ){
 1.45827 +    rc = SQLITE_NOMEM;
 1.45828 +  }
 1.45829 +
 1.45830 +  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
 1.45831 +    volatile ht_slot *aHash;
 1.45832 +    u32 iZero;
 1.45833 +    volatile u32 *aPgno;
 1.45834 +
 1.45835 +    rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
 1.45836 +    if( rc==SQLITE_OK ){
 1.45837 +      int j;                      /* Counter variable */
 1.45838 +      int nEntry;                 /* Number of entries in this segment */
 1.45839 +      ht_slot *aIndex;            /* Sorted index for this segment */
 1.45840 +
 1.45841 +      aPgno++;
 1.45842 +      if( (i+1)==nSegment ){
 1.45843 +        nEntry = (int)(iLast - iZero);
 1.45844 +      }else{
 1.45845 +        nEntry = (int)((u32*)aHash - (u32*)aPgno);
 1.45846 +      }
 1.45847 +      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
 1.45848 +      iZero++;
 1.45849 +  
 1.45850 +      for(j=0; j<nEntry; j++){
 1.45851 +        aIndex[j] = (ht_slot)j;
 1.45852 +      }
 1.45853 +      walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
 1.45854 +      p->aSegment[i].iZero = iZero;
 1.45855 +      p->aSegment[i].nEntry = nEntry;
 1.45856 +      p->aSegment[i].aIndex = aIndex;
 1.45857 +      p->aSegment[i].aPgno = (u32 *)aPgno;
 1.45858 +    }
 1.45859 +  }
 1.45860 +  sqlite3ScratchFree(aTmp);
 1.45861 +
 1.45862 +  if( rc!=SQLITE_OK ){
 1.45863 +    walIteratorFree(p);
 1.45864 +  }
 1.45865 +  *pp = p;
 1.45866 +  return rc;
 1.45867 +}
 1.45868 +
 1.45869 +/*
 1.45870 +** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
 1.45871 +** n. If the attempt fails and parameter xBusy is not NULL, then it is a
 1.45872 +** busy-handler function. Invoke it and retry the lock until either the
 1.45873 +** lock is successfully obtained or the busy-handler returns 0.
 1.45874 +*/
 1.45875 +static int walBusyLock(
 1.45876 +  Wal *pWal,                      /* WAL connection */
 1.45877 +  int (*xBusy)(void*),            /* Function to call when busy */
 1.45878 +  void *pBusyArg,                 /* Context argument for xBusyHandler */
 1.45879 +  int lockIdx,                    /* Offset of first byte to lock */
 1.45880 +  int n                           /* Number of bytes to lock */
 1.45881 +){
 1.45882 +  int rc;
 1.45883 +  do {
 1.45884 +    rc = walLockExclusive(pWal, lockIdx, n);
 1.45885 +  }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
 1.45886 +  return rc;
 1.45887 +}
 1.45888 +
 1.45889 +/*
 1.45890 +** The cache of the wal-index header must be valid to call this function.
 1.45891 +** Return the page-size in bytes used by the database.
 1.45892 +*/
 1.45893 +static int walPagesize(Wal *pWal){
 1.45894 +  return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
 1.45895 +}
 1.45896 +
 1.45897 +/*
 1.45898 +** Copy as much content as we can from the WAL back into the database file
 1.45899 +** in response to an sqlite3_wal_checkpoint() request or the equivalent.
 1.45900 +**
 1.45901 +** The amount of information copies from WAL to database might be limited
 1.45902 +** by active readers.  This routine will never overwrite a database page
 1.45903 +** that a concurrent reader might be using.
 1.45904 +**
 1.45905 +** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
 1.45906 +** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if 
 1.45907 +** checkpoints are always run by a background thread or background 
 1.45908 +** process, foreground threads will never block on a lengthy fsync call.
 1.45909 +**
 1.45910 +** Fsync is called on the WAL before writing content out of the WAL and
 1.45911 +** into the database.  This ensures that if the new content is persistent
 1.45912 +** in the WAL and can be recovered following a power-loss or hard reset.
 1.45913 +**
 1.45914 +** Fsync is also called on the database file if (and only if) the entire
 1.45915 +** WAL content is copied into the database file.  This second fsync makes
 1.45916 +** it safe to delete the WAL since the new content will persist in the
 1.45917 +** database file.
 1.45918 +**
 1.45919 +** This routine uses and updates the nBackfill field of the wal-index header.
 1.45920 +** This is the only routine tha will increase the value of nBackfill.  
 1.45921 +** (A WAL reset or recovery will revert nBackfill to zero, but not increase
 1.45922 +** its value.)
 1.45923 +**
 1.45924 +** The caller must be holding sufficient locks to ensure that no other
 1.45925 +** checkpoint is running (in any other thread or process) at the same
 1.45926 +** time.
 1.45927 +*/
 1.45928 +static int walCheckpoint(
 1.45929 +  Wal *pWal,                      /* Wal connection */
 1.45930 +  int eMode,                      /* One of PASSIVE, FULL or RESTART */
 1.45931 +  int (*xBusyCall)(void*),        /* Function to call when busy */
 1.45932 +  void *pBusyArg,                 /* Context argument for xBusyHandler */
 1.45933 +  int sync_flags,                 /* Flags for OsSync() (or 0) */
 1.45934 +  u8 *zBuf                        /* Temporary buffer to use */
 1.45935 +){
 1.45936 +  int rc;                         /* Return code */
 1.45937 +  int szPage;                     /* Database page-size */
 1.45938 +  WalIterator *pIter = 0;         /* Wal iterator context */
 1.45939 +  u32 iDbpage = 0;                /* Next database page to write */
 1.45940 +  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
 1.45941 +  u32 mxSafeFrame;                /* Max frame that can be backfilled */
 1.45942 +  u32 mxPage;                     /* Max database page to write */
 1.45943 +  int i;                          /* Loop counter */
 1.45944 +  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
 1.45945 +  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
 1.45946 +
 1.45947 +  szPage = walPagesize(pWal);
 1.45948 +  testcase( szPage<=32768 );
 1.45949 +  testcase( szPage>=65536 );
 1.45950 +  pInfo = walCkptInfo(pWal);
 1.45951 +  if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
 1.45952 +
 1.45953 +  /* Allocate the iterator */
 1.45954 +  rc = walIteratorInit(pWal, &pIter);
 1.45955 +  if( rc!=SQLITE_OK ){
 1.45956 +    return rc;
 1.45957 +  }
 1.45958 +  assert( pIter );
 1.45959 +
 1.45960 +  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
 1.45961 +
 1.45962 +  /* Compute in mxSafeFrame the index of the last frame of the WAL that is
 1.45963 +  ** safe to write into the database.  Frames beyond mxSafeFrame might
 1.45964 +  ** overwrite database pages that are in use by active readers and thus
 1.45965 +  ** cannot be backfilled from the WAL.
 1.45966 +  */
 1.45967 +  mxSafeFrame = pWal->hdr.mxFrame;
 1.45968 +  mxPage = pWal->hdr.nPage;
 1.45969 +  for(i=1; i<WAL_NREADER; i++){
 1.45970 +    u32 y = pInfo->aReadMark[i];
 1.45971 +    if( mxSafeFrame>y ){
 1.45972 +      assert( y<=pWal->hdr.mxFrame );
 1.45973 +      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
 1.45974 +      if( rc==SQLITE_OK ){
 1.45975 +        pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
 1.45976 +        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 1.45977 +      }else if( rc==SQLITE_BUSY ){
 1.45978 +        mxSafeFrame = y;
 1.45979 +        xBusy = 0;
 1.45980 +      }else{
 1.45981 +        goto walcheckpoint_out;
 1.45982 +      }
 1.45983 +    }
 1.45984 +  }
 1.45985 +
 1.45986 +  if( pInfo->nBackfill<mxSafeFrame
 1.45987 +   && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
 1.45988 +  ){
 1.45989 +    i64 nSize;                    /* Current size of database file */
 1.45990 +    u32 nBackfill = pInfo->nBackfill;
 1.45991 +
 1.45992 +    /* Sync the WAL to disk */
 1.45993 +    if( sync_flags ){
 1.45994 +      rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
 1.45995 +    }
 1.45996 +
 1.45997 +    /* If the database file may grow as a result of this checkpoint, hint
 1.45998 +    ** about the eventual size of the db file to the VFS layer. 
 1.45999 +    */
 1.46000 +    if( rc==SQLITE_OK ){
 1.46001 +      i64 nReq = ((i64)mxPage * szPage);
 1.46002 +      rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
 1.46003 +      if( rc==SQLITE_OK && nSize<nReq ){
 1.46004 +        sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
 1.46005 +      }
 1.46006 +    }
 1.46007 +
 1.46008 +    /* Iterate through the contents of the WAL, copying data to the db file. */
 1.46009 +    while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
 1.46010 +      i64 iOffset;
 1.46011 +      assert( walFramePgno(pWal, iFrame)==iDbpage );
 1.46012 +      if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
 1.46013 +      iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
 1.46014 +      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
 1.46015 +      rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
 1.46016 +      if( rc!=SQLITE_OK ) break;
 1.46017 +      iOffset = (iDbpage-1)*(i64)szPage;
 1.46018 +      testcase( IS_BIG_INT(iOffset) );
 1.46019 +      rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
 1.46020 +      if( rc!=SQLITE_OK ) break;
 1.46021 +    }
 1.46022 +
 1.46023 +    /* If work was actually accomplished... */
 1.46024 +    if( rc==SQLITE_OK ){
 1.46025 +      if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
 1.46026 +        i64 szDb = pWal->hdr.nPage*(i64)szPage;
 1.46027 +        testcase( IS_BIG_INT(szDb) );
 1.46028 +        rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
 1.46029 +        if( rc==SQLITE_OK && sync_flags ){
 1.46030 +          rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
 1.46031 +        }
 1.46032 +      }
 1.46033 +      if( rc==SQLITE_OK ){
 1.46034 +        pInfo->nBackfill = mxSafeFrame;
 1.46035 +      }
 1.46036 +    }
 1.46037 +
 1.46038 +    /* Release the reader lock held while backfilling */
 1.46039 +    walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
 1.46040 +  }
 1.46041 +
 1.46042 +  if( rc==SQLITE_BUSY ){
 1.46043 +    /* Reset the return code so as not to report a checkpoint failure
 1.46044 +    ** just because there are active readers.  */
 1.46045 +    rc = SQLITE_OK;
 1.46046 +  }
 1.46047 +
 1.46048 +  /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
 1.46049 +  ** file has been copied into the database file, then block until all
 1.46050 +  ** readers have finished using the wal file. This ensures that the next
 1.46051 +  ** process to write to the database restarts the wal file.
 1.46052 +  */
 1.46053 +  if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
 1.46054 +    assert( pWal->writeLock );
 1.46055 +    if( pInfo->nBackfill<pWal->hdr.mxFrame ){
 1.46056 +      rc = SQLITE_BUSY;
 1.46057 +    }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
 1.46058 +      assert( mxSafeFrame==pWal->hdr.mxFrame );
 1.46059 +      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
 1.46060 +      if( rc==SQLITE_OK ){
 1.46061 +        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 1.46062 +      }
 1.46063 +    }
 1.46064 +  }
 1.46065 +
 1.46066 + walcheckpoint_out:
 1.46067 +  walIteratorFree(pIter);
 1.46068 +  return rc;
 1.46069 +}
 1.46070 +
 1.46071 +/*
 1.46072 +** If the WAL file is currently larger than nMax bytes in size, truncate
 1.46073 +** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
 1.46074 +*/
 1.46075 +static void walLimitSize(Wal *pWal, i64 nMax){
 1.46076 +  i64 sz;
 1.46077 +  int rx;
 1.46078 +  sqlite3BeginBenignMalloc();
 1.46079 +  rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
 1.46080 +  if( rx==SQLITE_OK && (sz > nMax ) ){
 1.46081 +    rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
 1.46082 +  }
 1.46083 +  sqlite3EndBenignMalloc();
 1.46084 +  if( rx ){
 1.46085 +    sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
 1.46086 +  }
 1.46087 +}
 1.46088 +
 1.46089 +/*
 1.46090 +** Close a connection to a log file.
 1.46091 +*/
 1.46092 +SQLITE_PRIVATE int sqlite3WalClose(
 1.46093 +  Wal *pWal,                      /* Wal to close */
 1.46094 +  int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
 1.46095 +  int nBuf,
 1.46096 +  u8 *zBuf                        /* Buffer of at least nBuf bytes */
 1.46097 +){
 1.46098 +  int rc = SQLITE_OK;
 1.46099 +  if( pWal ){
 1.46100 +    int isDelete = 0;             /* True to unlink wal and wal-index files */
 1.46101 +
 1.46102 +    /* If an EXCLUSIVE lock can be obtained on the database file (using the
 1.46103 +    ** ordinary, rollback-mode locking methods, this guarantees that the
 1.46104 +    ** connection associated with this log file is the only connection to
 1.46105 +    ** the database. In this case checkpoint the database and unlink both
 1.46106 +    ** the wal and wal-index files.
 1.46107 +    **
 1.46108 +    ** The EXCLUSIVE lock is not released before returning.
 1.46109 +    */
 1.46110 +    rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
 1.46111 +    if( rc==SQLITE_OK ){
 1.46112 +      if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
 1.46113 +        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
 1.46114 +      }
 1.46115 +      rc = sqlite3WalCheckpoint(
 1.46116 +          pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
 1.46117 +      );
 1.46118 +      if( rc==SQLITE_OK ){
 1.46119 +        int bPersist = -1;
 1.46120 +        sqlite3OsFileControlHint(
 1.46121 +            pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
 1.46122 +        );
 1.46123 +        if( bPersist!=1 ){
 1.46124 +          /* Try to delete the WAL file if the checkpoint completed and
 1.46125 +          ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
 1.46126 +          ** mode (!bPersist) */
 1.46127 +          isDelete = 1;
 1.46128 +        }else if( pWal->mxWalSize>=0 ){
 1.46129 +          /* Try to truncate the WAL file to zero bytes if the checkpoint
 1.46130 +          ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
 1.46131 +          ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
 1.46132 +          ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
 1.46133 +          ** to zero bytes as truncating to the journal_size_limit might
 1.46134 +          ** leave a corrupt WAL file on disk. */
 1.46135 +          walLimitSize(pWal, 0);
 1.46136 +        }
 1.46137 +      }
 1.46138 +    }
 1.46139 +
 1.46140 +    walIndexClose(pWal, isDelete);
 1.46141 +    sqlite3OsClose(pWal->pWalFd);
 1.46142 +    if( isDelete ){
 1.46143 +      sqlite3BeginBenignMalloc();
 1.46144 +      sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
 1.46145 +      sqlite3EndBenignMalloc();
 1.46146 +    }
 1.46147 +    WALTRACE(("WAL%p: closed\n", pWal));
 1.46148 +    sqlite3_free((void *)pWal->apWiData);
 1.46149 +    sqlite3_free(pWal);
 1.46150 +  }
 1.46151 +  return rc;
 1.46152 +}
 1.46153 +
 1.46154 +/*
 1.46155 +** Try to read the wal-index header.  Return 0 on success and 1 if
 1.46156 +** there is a problem.
 1.46157 +**
 1.46158 +** The wal-index is in shared memory.  Another thread or process might
 1.46159 +** be writing the header at the same time this procedure is trying to
 1.46160 +** read it, which might result in inconsistency.  A dirty read is detected
 1.46161 +** by verifying that both copies of the header are the same and also by
 1.46162 +** a checksum on the header.
 1.46163 +**
 1.46164 +** If and only if the read is consistent and the header is different from
 1.46165 +** pWal->hdr, then pWal->hdr is updated to the content of the new header
 1.46166 +** and *pChanged is set to 1.
 1.46167 +**
 1.46168 +** If the checksum cannot be verified return non-zero. If the header
 1.46169 +** is read successfully and the checksum verified, return zero.
 1.46170 +*/
 1.46171 +static int walIndexTryHdr(Wal *pWal, int *pChanged){
 1.46172 +  u32 aCksum[2];                  /* Checksum on the header content */
 1.46173 +  WalIndexHdr h1, h2;             /* Two copies of the header content */
 1.46174 +  WalIndexHdr volatile *aHdr;     /* Header in shared memory */
 1.46175 +
 1.46176 +  /* The first page of the wal-index must be mapped at this point. */
 1.46177 +  assert( pWal->nWiData>0 && pWal->apWiData[0] );
 1.46178 +
 1.46179 +  /* Read the header. This might happen concurrently with a write to the
 1.46180 +  ** same area of shared memory on a different CPU in a SMP,
 1.46181 +  ** meaning it is possible that an inconsistent snapshot is read
 1.46182 +  ** from the file. If this happens, return non-zero.
 1.46183 +  **
 1.46184 +  ** There are two copies of the header at the beginning of the wal-index.
 1.46185 +  ** When reading, read [0] first then [1].  Writes are in the reverse order.
 1.46186 +  ** Memory barriers are used to prevent the compiler or the hardware from
 1.46187 +  ** reordering the reads and writes.
 1.46188 +  */
 1.46189 +  aHdr = walIndexHdr(pWal);
 1.46190 +  memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
 1.46191 +  walShmBarrier(pWal);
 1.46192 +  memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
 1.46193 +
 1.46194 +  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
 1.46195 +    return 1;   /* Dirty read */
 1.46196 +  }  
 1.46197 +  if( h1.isInit==0 ){
 1.46198 +    return 1;   /* Malformed header - probably all zeros */
 1.46199 +  }
 1.46200 +  walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
 1.46201 +  if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
 1.46202 +    return 1;   /* Checksum does not match */
 1.46203 +  }
 1.46204 +
 1.46205 +  if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
 1.46206 +    *pChanged = 1;
 1.46207 +    memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
 1.46208 +    pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
 1.46209 +    testcase( pWal->szPage<=32768 );
 1.46210 +    testcase( pWal->szPage>=65536 );
 1.46211 +  }
 1.46212 +
 1.46213 +  /* The header was successfully read. Return zero. */
 1.46214 +  return 0;
 1.46215 +}
 1.46216 +
 1.46217 +/*
 1.46218 +** Read the wal-index header from the wal-index and into pWal->hdr.
 1.46219 +** If the wal-header appears to be corrupt, try to reconstruct the
 1.46220 +** wal-index from the WAL before returning.
 1.46221 +**
 1.46222 +** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
 1.46223 +** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
 1.46224 +** to 0.
 1.46225 +**
 1.46226 +** If the wal-index header is successfully read, return SQLITE_OK. 
 1.46227 +** Otherwise an SQLite error code.
 1.46228 +*/
 1.46229 +static int walIndexReadHdr(Wal *pWal, int *pChanged){
 1.46230 +  int rc;                         /* Return code */
 1.46231 +  int badHdr;                     /* True if a header read failed */
 1.46232 +  volatile u32 *page0;            /* Chunk of wal-index containing header */
 1.46233 +
 1.46234 +  /* Ensure that page 0 of the wal-index (the page that contains the 
 1.46235 +  ** wal-index header) is mapped. Return early if an error occurs here.
 1.46236 +  */
 1.46237 +  assert( pChanged );
 1.46238 +  rc = walIndexPage(pWal, 0, &page0);
 1.46239 +  if( rc!=SQLITE_OK ){
 1.46240 +    return rc;
 1.46241 +  };
 1.46242 +  assert( page0 || pWal->writeLock==0 );
 1.46243 +
 1.46244 +  /* If the first page of the wal-index has been mapped, try to read the
 1.46245 +  ** wal-index header immediately, without holding any lock. This usually
 1.46246 +  ** works, but may fail if the wal-index header is corrupt or currently 
 1.46247 +  ** being modified by another thread or process.
 1.46248 +  */
 1.46249 +  badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
 1.46250 +
 1.46251 +  /* If the first attempt failed, it might have been due to a race
 1.46252 +  ** with a writer.  So get a WRITE lock and try again.
 1.46253 +  */
 1.46254 +  assert( badHdr==0 || pWal->writeLock==0 );
 1.46255 +  if( badHdr ){
 1.46256 +    if( pWal->readOnly & WAL_SHM_RDONLY ){
 1.46257 +      if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
 1.46258 +        walUnlockShared(pWal, WAL_WRITE_LOCK);
 1.46259 +        rc = SQLITE_READONLY_RECOVERY;
 1.46260 +      }
 1.46261 +    }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
 1.46262 +      pWal->writeLock = 1;
 1.46263 +      if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
 1.46264 +        badHdr = walIndexTryHdr(pWal, pChanged);
 1.46265 +        if( badHdr ){
 1.46266 +          /* If the wal-index header is still malformed even while holding
 1.46267 +          ** a WRITE lock, it can only mean that the header is corrupted and
 1.46268 +          ** needs to be reconstructed.  So run recovery to do exactly that.
 1.46269 +          */
 1.46270 +          rc = walIndexRecover(pWal);
 1.46271 +          *pChanged = 1;
 1.46272 +        }
 1.46273 +      }
 1.46274 +      pWal->writeLock = 0;
 1.46275 +      walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 1.46276 +    }
 1.46277 +  }
 1.46278 +
 1.46279 +  /* If the header is read successfully, check the version number to make
 1.46280 +  ** sure the wal-index was not constructed with some future format that
 1.46281 +  ** this version of SQLite cannot understand.
 1.46282 +  */
 1.46283 +  if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
 1.46284 +    rc = SQLITE_CANTOPEN_BKPT;
 1.46285 +  }
 1.46286 +
 1.46287 +  return rc;
 1.46288 +}
 1.46289 +
 1.46290 +/*
 1.46291 +** This is the value that walTryBeginRead returns when it needs to
 1.46292 +** be retried.
 1.46293 +*/
 1.46294 +#define WAL_RETRY  (-1)
 1.46295 +
 1.46296 +/*
 1.46297 +** Attempt to start a read transaction.  This might fail due to a race or
 1.46298 +** other transient condition.  When that happens, it returns WAL_RETRY to
 1.46299 +** indicate to the caller that it is safe to retry immediately.
 1.46300 +**
 1.46301 +** On success return SQLITE_OK.  On a permanent failure (such an
 1.46302 +** I/O error or an SQLITE_BUSY because another process is running
 1.46303 +** recovery) return a positive error code.
 1.46304 +**
 1.46305 +** The useWal parameter is true to force the use of the WAL and disable
 1.46306 +** the case where the WAL is bypassed because it has been completely
 1.46307 +** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
 1.46308 +** to make a copy of the wal-index header into pWal->hdr.  If the 
 1.46309 +** wal-index header has changed, *pChanged is set to 1 (as an indication 
 1.46310 +** to the caller that the local paget cache is obsolete and needs to be 
 1.46311 +** flushed.)  When useWal==1, the wal-index header is assumed to already
 1.46312 +** be loaded and the pChanged parameter is unused.
 1.46313 +**
 1.46314 +** The caller must set the cnt parameter to the number of prior calls to
 1.46315 +** this routine during the current read attempt that returned WAL_RETRY.
 1.46316 +** This routine will start taking more aggressive measures to clear the
 1.46317 +** race conditions after multiple WAL_RETRY returns, and after an excessive
 1.46318 +** number of errors will ultimately return SQLITE_PROTOCOL.  The
 1.46319 +** SQLITE_PROTOCOL return indicates that some other process has gone rogue
 1.46320 +** and is not honoring the locking protocol.  There is a vanishingly small
 1.46321 +** chance that SQLITE_PROTOCOL could be returned because of a run of really
 1.46322 +** bad luck when there is lots of contention for the wal-index, but that
 1.46323 +** possibility is so small that it can be safely neglected, we believe.
 1.46324 +**
 1.46325 +** On success, this routine obtains a read lock on 
 1.46326 +** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
 1.46327 +** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
 1.46328 +** that means the Wal does not hold any read lock.  The reader must not
 1.46329 +** access any database page that is modified by a WAL frame up to and
 1.46330 +** including frame number aReadMark[pWal->readLock].  The reader will
 1.46331 +** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
 1.46332 +** Or if pWal->readLock==0, then the reader will ignore the WAL
 1.46333 +** completely and get all content directly from the database file.
 1.46334 +** If the useWal parameter is 1 then the WAL will never be ignored and
 1.46335 +** this routine will always set pWal->readLock>0 on success.
 1.46336 +** When the read transaction is completed, the caller must release the
 1.46337 +** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
 1.46338 +**
 1.46339 +** This routine uses the nBackfill and aReadMark[] fields of the header
 1.46340 +** to select a particular WAL_READ_LOCK() that strives to let the
 1.46341 +** checkpoint process do as much work as possible.  This routine might
 1.46342 +** update values of the aReadMark[] array in the header, but if it does
 1.46343 +** so it takes care to hold an exclusive lock on the corresponding
 1.46344 +** WAL_READ_LOCK() while changing values.
 1.46345 +*/
 1.46346 +static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
 1.46347 +  volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
 1.46348 +  u32 mxReadMark;                 /* Largest aReadMark[] value */
 1.46349 +  int mxI;                        /* Index of largest aReadMark[] value */
 1.46350 +  int i;                          /* Loop counter */
 1.46351 +  int rc = SQLITE_OK;             /* Return code  */
 1.46352 +
 1.46353 +  assert( pWal->readLock<0 );     /* Not currently locked */
 1.46354 +
 1.46355 +  /* Take steps to avoid spinning forever if there is a protocol error.
 1.46356 +  **
 1.46357 +  ** Circumstances that cause a RETRY should only last for the briefest
 1.46358 +  ** instances of time.  No I/O or other system calls are done while the
 1.46359 +  ** locks are held, so the locks should not be held for very long. But 
 1.46360 +  ** if we are unlucky, another process that is holding a lock might get
 1.46361 +  ** paged out or take a page-fault that is time-consuming to resolve, 
 1.46362 +  ** during the few nanoseconds that it is holding the lock.  In that case,
 1.46363 +  ** it might take longer than normal for the lock to free.
 1.46364 +  **
 1.46365 +  ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
 1.46366 +  ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
 1.46367 +  ** is more of a scheduler yield than an actual delay.  But on the 10th
 1.46368 +  ** an subsequent retries, the delays start becoming longer and longer, 
 1.46369 +  ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
 1.46370 +  ** The total delay time before giving up is less than 1 second.
 1.46371 +  */
 1.46372 +  if( cnt>5 ){
 1.46373 +    int nDelay = 1;                      /* Pause time in microseconds */
 1.46374 +    if( cnt>100 ){
 1.46375 +      VVA_ONLY( pWal->lockError = 1; )
 1.46376 +      return SQLITE_PROTOCOL;
 1.46377 +    }
 1.46378 +    if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
 1.46379 +    sqlite3OsSleep(pWal->pVfs, nDelay);
 1.46380 +  }
 1.46381 +
 1.46382 +  if( !useWal ){
 1.46383 +    rc = walIndexReadHdr(pWal, pChanged);
 1.46384 +    if( rc==SQLITE_BUSY ){
 1.46385 +      /* If there is not a recovery running in another thread or process
 1.46386 +      ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
 1.46387 +      ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
 1.46388 +      ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
 1.46389 +      ** would be technically correct.  But the race is benign since with
 1.46390 +      ** WAL_RETRY this routine will be called again and will probably be
 1.46391 +      ** right on the second iteration.
 1.46392 +      */
 1.46393 +      if( pWal->apWiData[0]==0 ){
 1.46394 +        /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
 1.46395 +        ** We assume this is a transient condition, so return WAL_RETRY. The
 1.46396 +        ** xShmMap() implementation used by the default unix and win32 VFS 
 1.46397 +        ** modules may return SQLITE_BUSY due to a race condition in the 
 1.46398 +        ** code that determines whether or not the shared-memory region 
 1.46399 +        ** must be zeroed before the requested page is returned.
 1.46400 +        */
 1.46401 +        rc = WAL_RETRY;
 1.46402 +      }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
 1.46403 +        walUnlockShared(pWal, WAL_RECOVER_LOCK);
 1.46404 +        rc = WAL_RETRY;
 1.46405 +      }else if( rc==SQLITE_BUSY ){
 1.46406 +        rc = SQLITE_BUSY_RECOVERY;
 1.46407 +      }
 1.46408 +    }
 1.46409 +    if( rc!=SQLITE_OK ){
 1.46410 +      return rc;
 1.46411 +    }
 1.46412 +  }
 1.46413 +
 1.46414 +  pInfo = walCkptInfo(pWal);
 1.46415 +  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
 1.46416 +    /* The WAL has been completely backfilled (or it is empty).
 1.46417 +    ** and can be safely ignored.
 1.46418 +    */
 1.46419 +    rc = walLockShared(pWal, WAL_READ_LOCK(0));
 1.46420 +    walShmBarrier(pWal);
 1.46421 +    if( rc==SQLITE_OK ){
 1.46422 +      if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
 1.46423 +        /* It is not safe to allow the reader to continue here if frames
 1.46424 +        ** may have been appended to the log before READ_LOCK(0) was obtained.
 1.46425 +        ** When holding READ_LOCK(0), the reader ignores the entire log file,
 1.46426 +        ** which implies that the database file contains a trustworthy
 1.46427 +        ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
 1.46428 +        ** happening, this is usually correct.
 1.46429 +        **
 1.46430 +        ** However, if frames have been appended to the log (or if the log 
 1.46431 +        ** is wrapped and written for that matter) before the READ_LOCK(0)
 1.46432 +        ** is obtained, that is not necessarily true. A checkpointer may
 1.46433 +        ** have started to backfill the appended frames but crashed before
 1.46434 +        ** it finished. Leaving a corrupt image in the database file.
 1.46435 +        */
 1.46436 +        walUnlockShared(pWal, WAL_READ_LOCK(0));
 1.46437 +        return WAL_RETRY;
 1.46438 +      }
 1.46439 +      pWal->readLock = 0;
 1.46440 +      return SQLITE_OK;
 1.46441 +    }else if( rc!=SQLITE_BUSY ){
 1.46442 +      return rc;
 1.46443 +    }
 1.46444 +  }
 1.46445 +
 1.46446 +  /* If we get this far, it means that the reader will want to use
 1.46447 +  ** the WAL to get at content from recent commits.  The job now is
 1.46448 +  ** to select one of the aReadMark[] entries that is closest to
 1.46449 +  ** but not exceeding pWal->hdr.mxFrame and lock that entry.
 1.46450 +  */
 1.46451 +  mxReadMark = 0;
 1.46452 +  mxI = 0;
 1.46453 +  for(i=1; i<WAL_NREADER; i++){
 1.46454 +    u32 thisMark = pInfo->aReadMark[i];
 1.46455 +    if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
 1.46456 +      assert( thisMark!=READMARK_NOT_USED );
 1.46457 +      mxReadMark = thisMark;
 1.46458 +      mxI = i;
 1.46459 +    }
 1.46460 +  }
 1.46461 +  /* There was once an "if" here. The extra "{" is to preserve indentation. */
 1.46462 +  {
 1.46463 +    if( (pWal->readOnly & WAL_SHM_RDONLY)==0
 1.46464 +     && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
 1.46465 +    ){
 1.46466 +      for(i=1; i<WAL_NREADER; i++){
 1.46467 +        rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
 1.46468 +        if( rc==SQLITE_OK ){
 1.46469 +          mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
 1.46470 +          mxI = i;
 1.46471 +          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 1.46472 +          break;
 1.46473 +        }else if( rc!=SQLITE_BUSY ){
 1.46474 +          return rc;
 1.46475 +        }
 1.46476 +      }
 1.46477 +    }
 1.46478 +    if( mxI==0 ){
 1.46479 +      assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
 1.46480 +      return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
 1.46481 +    }
 1.46482 +
 1.46483 +    rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
 1.46484 +    if( rc ){
 1.46485 +      return rc==SQLITE_BUSY ? WAL_RETRY : rc;
 1.46486 +    }
 1.46487 +    /* Now that the read-lock has been obtained, check that neither the
 1.46488 +    ** value in the aReadMark[] array or the contents of the wal-index
 1.46489 +    ** header have changed.
 1.46490 +    **
 1.46491 +    ** It is necessary to check that the wal-index header did not change
 1.46492 +    ** between the time it was read and when the shared-lock was obtained
 1.46493 +    ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
 1.46494 +    ** that the log file may have been wrapped by a writer, or that frames
 1.46495 +    ** that occur later in the log than pWal->hdr.mxFrame may have been
 1.46496 +    ** copied into the database by a checkpointer. If either of these things
 1.46497 +    ** happened, then reading the database with the current value of
 1.46498 +    ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
 1.46499 +    ** instead.
 1.46500 +    **
 1.46501 +    ** This does not guarantee that the copy of the wal-index header is up to
 1.46502 +    ** date before proceeding. That would not be possible without somehow
 1.46503 +    ** blocking writers. It only guarantees that a dangerous checkpoint or 
 1.46504 +    ** log-wrap (either of which would require an exclusive lock on
 1.46505 +    ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
 1.46506 +    */
 1.46507 +    walShmBarrier(pWal);
 1.46508 +    if( pInfo->aReadMark[mxI]!=mxReadMark
 1.46509 +     || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
 1.46510 +    ){
 1.46511 +      walUnlockShared(pWal, WAL_READ_LOCK(mxI));
 1.46512 +      return WAL_RETRY;
 1.46513 +    }else{
 1.46514 +      assert( mxReadMark<=pWal->hdr.mxFrame );
 1.46515 +      pWal->readLock = (i16)mxI;
 1.46516 +    }
 1.46517 +  }
 1.46518 +  return rc;
 1.46519 +}
 1.46520 +
 1.46521 +/*
 1.46522 +** Begin a read transaction on the database.
 1.46523 +**
 1.46524 +** This routine used to be called sqlite3OpenSnapshot() and with good reason:
 1.46525 +** it takes a snapshot of the state of the WAL and wal-index for the current
 1.46526 +** instant in time.  The current thread will continue to use this snapshot.
 1.46527 +** Other threads might append new content to the WAL and wal-index but
 1.46528 +** that extra content is ignored by the current thread.
 1.46529 +**
 1.46530 +** If the database contents have changes since the previous read
 1.46531 +** transaction, then *pChanged is set to 1 before returning.  The
 1.46532 +** Pager layer will use this to know that is cache is stale and
 1.46533 +** needs to be flushed.
 1.46534 +*/
 1.46535 +SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
 1.46536 +  int rc;                         /* Return code */
 1.46537 +  int cnt = 0;                    /* Number of TryBeginRead attempts */
 1.46538 +
 1.46539 +  do{
 1.46540 +    rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
 1.46541 +  }while( rc==WAL_RETRY );
 1.46542 +  testcase( (rc&0xff)==SQLITE_BUSY );
 1.46543 +  testcase( (rc&0xff)==SQLITE_IOERR );
 1.46544 +  testcase( rc==SQLITE_PROTOCOL );
 1.46545 +  testcase( rc==SQLITE_OK );
 1.46546 +  return rc;
 1.46547 +}
 1.46548 +
 1.46549 +/*
 1.46550 +** Finish with a read transaction.  All this does is release the
 1.46551 +** read-lock.
 1.46552 +*/
 1.46553 +SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
 1.46554 +  sqlite3WalEndWriteTransaction(pWal);
 1.46555 +  if( pWal->readLock>=0 ){
 1.46556 +    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
 1.46557 +    pWal->readLock = -1;
 1.46558 +  }
 1.46559 +}
 1.46560 +
 1.46561 +/*
 1.46562 +** Read a page from the WAL, if it is present in the WAL and if the 
 1.46563 +** current read transaction is configured to use the WAL.  
 1.46564 +**
 1.46565 +** The *pInWal is set to 1 if the requested page is in the WAL and
 1.46566 +** has been loaded.  Or *pInWal is set to 0 if the page was not in 
 1.46567 +** the WAL and needs to be read out of the database.
 1.46568 +*/
 1.46569 +SQLITE_PRIVATE int sqlite3WalRead(
 1.46570 +  Wal *pWal,                      /* WAL handle */
 1.46571 +  Pgno pgno,                      /* Database page number to read data for */
 1.46572 +  int *pInWal,                    /* OUT: True if data is read from WAL */
 1.46573 +  int nOut,                       /* Size of buffer pOut in bytes */
 1.46574 +  u8 *pOut                        /* Buffer to write page data to */
 1.46575 +){
 1.46576 +  u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
 1.46577 +  u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
 1.46578 +  int iHash;                      /* Used to loop through N hash tables */
 1.46579 +
 1.46580 +  /* This routine is only be called from within a read transaction. */
 1.46581 +  assert( pWal->readLock>=0 || pWal->lockError );
 1.46582 +
 1.46583 +  /* If the "last page" field of the wal-index header snapshot is 0, then
 1.46584 +  ** no data will be read from the wal under any circumstances. Return early
 1.46585 +  ** in this case as an optimization.  Likewise, if pWal->readLock==0, 
 1.46586 +  ** then the WAL is ignored by the reader so return early, as if the 
 1.46587 +  ** WAL were empty.
 1.46588 +  */
 1.46589 +  if( iLast==0 || pWal->readLock==0 ){
 1.46590 +    *pInWal = 0;
 1.46591 +    return SQLITE_OK;
 1.46592 +  }
 1.46593 +
 1.46594 +  /* Search the hash table or tables for an entry matching page number
 1.46595 +  ** pgno. Each iteration of the following for() loop searches one
 1.46596 +  ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
 1.46597 +  **
 1.46598 +  ** This code might run concurrently to the code in walIndexAppend()
 1.46599 +  ** that adds entries to the wal-index (and possibly to this hash 
 1.46600 +  ** table). This means the value just read from the hash 
 1.46601 +  ** slot (aHash[iKey]) may have been added before or after the 
 1.46602 +  ** current read transaction was opened. Values added after the
 1.46603 +  ** read transaction was opened may have been written incorrectly -
 1.46604 +  ** i.e. these slots may contain garbage data. However, we assume
 1.46605 +  ** that any slots written before the current read transaction was
 1.46606 +  ** opened remain unmodified.
 1.46607 +  **
 1.46608 +  ** For the reasons above, the if(...) condition featured in the inner
 1.46609 +  ** loop of the following block is more stringent that would be required 
 1.46610 +  ** if we had exclusive access to the hash-table:
 1.46611 +  **
 1.46612 +  **   (aPgno[iFrame]==pgno): 
 1.46613 +  **     This condition filters out normal hash-table collisions.
 1.46614 +  **
 1.46615 +  **   (iFrame<=iLast): 
 1.46616 +  **     This condition filters out entries that were added to the hash
 1.46617 +  **     table after the current read-transaction had started.
 1.46618 +  */
 1.46619 +  for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
 1.46620 +    volatile ht_slot *aHash;      /* Pointer to hash table */
 1.46621 +    volatile u32 *aPgno;          /* Pointer to array of page numbers */
 1.46622 +    u32 iZero;                    /* Frame number corresponding to aPgno[0] */
 1.46623 +    int iKey;                     /* Hash slot index */
 1.46624 +    int nCollide;                 /* Number of hash collisions remaining */
 1.46625 +    int rc;                       /* Error code */
 1.46626 +
 1.46627 +    rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
 1.46628 +    if( rc!=SQLITE_OK ){
 1.46629 +      return rc;
 1.46630 +    }
 1.46631 +    nCollide = HASHTABLE_NSLOT;
 1.46632 +    for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
 1.46633 +      u32 iFrame = aHash[iKey] + iZero;
 1.46634 +      if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
 1.46635 +        /* assert( iFrame>iRead ); -- not true if there is corruption */
 1.46636 +        iRead = iFrame;
 1.46637 +      }
 1.46638 +      if( (nCollide--)==0 ){
 1.46639 +        return SQLITE_CORRUPT_BKPT;
 1.46640 +      }
 1.46641 +    }
 1.46642 +  }
 1.46643 +
 1.46644 +#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 1.46645 +  /* If expensive assert() statements are available, do a linear search
 1.46646 +  ** of the wal-index file content. Make sure the results agree with the
 1.46647 +  ** result obtained using the hash indexes above.  */
 1.46648 +  {
 1.46649 +    u32 iRead2 = 0;
 1.46650 +    u32 iTest;
 1.46651 +    for(iTest=iLast; iTest>0; iTest--){
 1.46652 +      if( walFramePgno(pWal, iTest)==pgno ){
 1.46653 +        iRead2 = iTest;
 1.46654 +        break;
 1.46655 +      }
 1.46656 +    }
 1.46657 +    assert( iRead==iRead2 );
 1.46658 +  }
 1.46659 +#endif
 1.46660 +
 1.46661 +  /* If iRead is non-zero, then it is the log frame number that contains the
 1.46662 +  ** required page. Read and return data from the log file.
 1.46663 +  */
 1.46664 +  if( iRead ){
 1.46665 +    int sz;
 1.46666 +    i64 iOffset;
 1.46667 +    sz = pWal->hdr.szPage;
 1.46668 +    sz = (sz&0xfe00) + ((sz&0x0001)<<16);
 1.46669 +    testcase( sz<=32768 );
 1.46670 +    testcase( sz>=65536 );
 1.46671 +    iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
 1.46672 +    *pInWal = 1;
 1.46673 +    /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
 1.46674 +    return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
 1.46675 +  }
 1.46676 +
 1.46677 +  *pInWal = 0;
 1.46678 +  return SQLITE_OK;
 1.46679 +}
 1.46680 +
 1.46681 +
 1.46682 +/* 
 1.46683 +** Return the size of the database in pages (or zero, if unknown).
 1.46684 +*/
 1.46685 +SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
 1.46686 +  if( pWal && ALWAYS(pWal->readLock>=0) ){
 1.46687 +    return pWal->hdr.nPage;
 1.46688 +  }
 1.46689 +  return 0;
 1.46690 +}
 1.46691 +
 1.46692 +
 1.46693 +/* 
 1.46694 +** This function starts a write transaction on the WAL.
 1.46695 +**
 1.46696 +** A read transaction must have already been started by a prior call
 1.46697 +** to sqlite3WalBeginReadTransaction().
 1.46698 +**
 1.46699 +** If another thread or process has written into the database since
 1.46700 +** the read transaction was started, then it is not possible for this
 1.46701 +** thread to write as doing so would cause a fork.  So this routine
 1.46702 +** returns SQLITE_BUSY in that case and no write transaction is started.
 1.46703 +**
 1.46704 +** There can only be a single writer active at a time.
 1.46705 +*/
 1.46706 +SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
 1.46707 +  int rc;
 1.46708 +
 1.46709 +  /* Cannot start a write transaction without first holding a read
 1.46710 +  ** transaction. */
 1.46711 +  assert( pWal->readLock>=0 );
 1.46712 +
 1.46713 +  if( pWal->readOnly ){
 1.46714 +    return SQLITE_READONLY;
 1.46715 +  }
 1.46716 +
 1.46717 +  /* Only one writer allowed at a time.  Get the write lock.  Return
 1.46718 +  ** SQLITE_BUSY if unable.
 1.46719 +  */
 1.46720 +  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
 1.46721 +  if( rc ){
 1.46722 +    return rc;
 1.46723 +  }
 1.46724 +  pWal->writeLock = 1;
 1.46725 +
 1.46726 +  /* If another connection has written to the database file since the
 1.46727 +  ** time the read transaction on this connection was started, then
 1.46728 +  ** the write is disallowed.
 1.46729 +  */
 1.46730 +  if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
 1.46731 +    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 1.46732 +    pWal->writeLock = 0;
 1.46733 +    rc = SQLITE_BUSY;
 1.46734 +  }
 1.46735 +
 1.46736 +  return rc;
 1.46737 +}
 1.46738 +
 1.46739 +/*
 1.46740 +** End a write transaction.  The commit has already been done.  This
 1.46741 +** routine merely releases the lock.
 1.46742 +*/
 1.46743 +SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
 1.46744 +  if( pWal->writeLock ){
 1.46745 +    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 1.46746 +    pWal->writeLock = 0;
 1.46747 +    pWal->truncateOnCommit = 0;
 1.46748 +  }
 1.46749 +  return SQLITE_OK;
 1.46750 +}
 1.46751 +
 1.46752 +/*
 1.46753 +** If any data has been written (but not committed) to the log file, this
 1.46754 +** function moves the write-pointer back to the start of the transaction.
 1.46755 +**
 1.46756 +** Additionally, the callback function is invoked for each frame written
 1.46757 +** to the WAL since the start of the transaction. If the callback returns
 1.46758 +** other than SQLITE_OK, it is not invoked again and the error code is
 1.46759 +** returned to the caller.
 1.46760 +**
 1.46761 +** Otherwise, if the callback function does not return an error, this
 1.46762 +** function returns SQLITE_OK.
 1.46763 +*/
 1.46764 +SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
 1.46765 +  int rc = SQLITE_OK;
 1.46766 +  if( ALWAYS(pWal->writeLock) ){
 1.46767 +    Pgno iMax = pWal->hdr.mxFrame;
 1.46768 +    Pgno iFrame;
 1.46769 +  
 1.46770 +    /* Restore the clients cache of the wal-index header to the state it
 1.46771 +    ** was in before the client began writing to the database. 
 1.46772 +    */
 1.46773 +    memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
 1.46774 +
 1.46775 +    for(iFrame=pWal->hdr.mxFrame+1; 
 1.46776 +        ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; 
 1.46777 +        iFrame++
 1.46778 +    ){
 1.46779 +      /* This call cannot fail. Unless the page for which the page number
 1.46780 +      ** is passed as the second argument is (a) in the cache and 
 1.46781 +      ** (b) has an outstanding reference, then xUndo is either a no-op
 1.46782 +      ** (if (a) is false) or simply expels the page from the cache (if (b)
 1.46783 +      ** is false).
 1.46784 +      **
 1.46785 +      ** If the upper layer is doing a rollback, it is guaranteed that there
 1.46786 +      ** are no outstanding references to any page other than page 1. And
 1.46787 +      ** page 1 is never written to the log until the transaction is
 1.46788 +      ** committed. As a result, the call to xUndo may not fail.
 1.46789 +      */
 1.46790 +      assert( walFramePgno(pWal, iFrame)!=1 );
 1.46791 +      rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
 1.46792 +    }
 1.46793 +    if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
 1.46794 +  }
 1.46795 +  assert( rc==SQLITE_OK );
 1.46796 +  return rc;
 1.46797 +}
 1.46798 +
 1.46799 +/* 
 1.46800 +** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 
 1.46801 +** values. This function populates the array with values required to 
 1.46802 +** "rollback" the write position of the WAL handle back to the current 
 1.46803 +** point in the event of a savepoint rollback (via WalSavepointUndo()).
 1.46804 +*/
 1.46805 +SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
 1.46806 +  assert( pWal->writeLock );
 1.46807 +  aWalData[0] = pWal->hdr.mxFrame;
 1.46808 +  aWalData[1] = pWal->hdr.aFrameCksum[0];
 1.46809 +  aWalData[2] = pWal->hdr.aFrameCksum[1];
 1.46810 +  aWalData[3] = pWal->nCkpt;
 1.46811 +}
 1.46812 +
 1.46813 +/* 
 1.46814 +** Move the write position of the WAL back to the point identified by
 1.46815 +** the values in the aWalData[] array. aWalData must point to an array
 1.46816 +** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
 1.46817 +** by a call to WalSavepoint().
 1.46818 +*/
 1.46819 +SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
 1.46820 +  int rc = SQLITE_OK;
 1.46821 +
 1.46822 +  assert( pWal->writeLock );
 1.46823 +  assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
 1.46824 +
 1.46825 +  if( aWalData[3]!=pWal->nCkpt ){
 1.46826 +    /* This savepoint was opened immediately after the write-transaction
 1.46827 +    ** was started. Right after that, the writer decided to wrap around
 1.46828 +    ** to the start of the log. Update the savepoint values to match.
 1.46829 +    */
 1.46830 +    aWalData[0] = 0;
 1.46831 +    aWalData[3] = pWal->nCkpt;
 1.46832 +  }
 1.46833 +
 1.46834 +  if( aWalData[0]<pWal->hdr.mxFrame ){
 1.46835 +    pWal->hdr.mxFrame = aWalData[0];
 1.46836 +    pWal->hdr.aFrameCksum[0] = aWalData[1];
 1.46837 +    pWal->hdr.aFrameCksum[1] = aWalData[2];
 1.46838 +    walCleanupHash(pWal);
 1.46839 +  }
 1.46840 +
 1.46841 +  return rc;
 1.46842 +}
 1.46843 +
 1.46844 +
 1.46845 +/*
 1.46846 +** This function is called just before writing a set of frames to the log
 1.46847 +** file (see sqlite3WalFrames()). It checks to see if, instead of appending
 1.46848 +** to the current log file, it is possible to overwrite the start of the
 1.46849 +** existing log file with the new frames (i.e. "reset" the log). If so,
 1.46850 +** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
 1.46851 +** unchanged.
 1.46852 +**
 1.46853 +** SQLITE_OK is returned if no error is encountered (regardless of whether
 1.46854 +** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
 1.46855 +** if an error occurs.
 1.46856 +*/
 1.46857 +static int walRestartLog(Wal *pWal){
 1.46858 +  int rc = SQLITE_OK;
 1.46859 +  int cnt;
 1.46860 +
 1.46861 +  if( pWal->readLock==0 ){
 1.46862 +    volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
 1.46863 +    assert( pInfo->nBackfill==pWal->hdr.mxFrame );
 1.46864 +    if( pInfo->nBackfill>0 ){
 1.46865 +      u32 salt1;
 1.46866 +      sqlite3_randomness(4, &salt1);
 1.46867 +      rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 1.46868 +      if( rc==SQLITE_OK ){
 1.46869 +        /* If all readers are using WAL_READ_LOCK(0) (in other words if no
 1.46870 +        ** readers are currently using the WAL), then the transactions
 1.46871 +        ** frames will overwrite the start of the existing log. Update the
 1.46872 +        ** wal-index header to reflect this.
 1.46873 +        **
 1.46874 +        ** In theory it would be Ok to update the cache of the header only
 1.46875 +        ** at this point. But updating the actual wal-index header is also
 1.46876 +        ** safe and means there is no special case for sqlite3WalUndo()
 1.46877 +        ** to handle if this transaction is rolled back.
 1.46878 +        */
 1.46879 +        int i;                    /* Loop counter */
 1.46880 +        u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
 1.46881 +
 1.46882 +        pWal->nCkpt++;
 1.46883 +        pWal->hdr.mxFrame = 0;
 1.46884 +        sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
 1.46885 +        aSalt[1] = salt1;
 1.46886 +        walIndexWriteHdr(pWal);
 1.46887 +        pInfo->nBackfill = 0;
 1.46888 +        pInfo->aReadMark[1] = 0;
 1.46889 +        for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 1.46890 +        assert( pInfo->aReadMark[0]==0 );
 1.46891 +        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 1.46892 +      }else if( rc!=SQLITE_BUSY ){
 1.46893 +        return rc;
 1.46894 +      }
 1.46895 +    }
 1.46896 +    walUnlockShared(pWal, WAL_READ_LOCK(0));
 1.46897 +    pWal->readLock = -1;
 1.46898 +    cnt = 0;
 1.46899 +    do{
 1.46900 +      int notUsed;
 1.46901 +      rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
 1.46902 +    }while( rc==WAL_RETRY );
 1.46903 +    assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
 1.46904 +    testcase( (rc&0xff)==SQLITE_IOERR );
 1.46905 +    testcase( rc==SQLITE_PROTOCOL );
 1.46906 +    testcase( rc==SQLITE_OK );
 1.46907 +  }
 1.46908 +  return rc;
 1.46909 +}
 1.46910 +
 1.46911 +/*
 1.46912 +** Information about the current state of the WAL file and where
 1.46913 +** the next fsync should occur - passed from sqlite3WalFrames() into
 1.46914 +** walWriteToLog().
 1.46915 +*/
 1.46916 +typedef struct WalWriter {
 1.46917 +  Wal *pWal;                   /* The complete WAL information */
 1.46918 +  sqlite3_file *pFd;           /* The WAL file to which we write */
 1.46919 +  sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
 1.46920 +  int syncFlags;               /* Flags for the fsync */
 1.46921 +  int szPage;                  /* Size of one page */
 1.46922 +} WalWriter;
 1.46923 +
 1.46924 +/*
 1.46925 +** Write iAmt bytes of content into the WAL file beginning at iOffset.
 1.46926 +** Do a sync when crossing the p->iSyncPoint boundary.
 1.46927 +**
 1.46928 +** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
 1.46929 +** first write the part before iSyncPoint, then sync, then write the
 1.46930 +** rest.
 1.46931 +*/
 1.46932 +static int walWriteToLog(
 1.46933 +  WalWriter *p,              /* WAL to write to */
 1.46934 +  void *pContent,            /* Content to be written */
 1.46935 +  int iAmt,                  /* Number of bytes to write */
 1.46936 +  sqlite3_int64 iOffset      /* Start writing at this offset */
 1.46937 +){
 1.46938 +  int rc;
 1.46939 +  if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
 1.46940 +    int iFirstAmt = (int)(p->iSyncPoint - iOffset);
 1.46941 +    rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
 1.46942 +    if( rc ) return rc;
 1.46943 +    iOffset += iFirstAmt;
 1.46944 +    iAmt -= iFirstAmt;
 1.46945 +    pContent = (void*)(iFirstAmt + (char*)pContent);
 1.46946 +    assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
 1.46947 +    rc = sqlite3OsSync(p->pFd, p->syncFlags);
 1.46948 +    if( iAmt==0 || rc ) return rc;
 1.46949 +  }
 1.46950 +  rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
 1.46951 +  return rc;
 1.46952 +}
 1.46953 +
 1.46954 +/*
 1.46955 +** Write out a single frame of the WAL
 1.46956 +*/
 1.46957 +static int walWriteOneFrame(
 1.46958 +  WalWriter *p,               /* Where to write the frame */
 1.46959 +  PgHdr *pPage,               /* The page of the frame to be written */
 1.46960 +  int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
 1.46961 +  sqlite3_int64 iOffset       /* Byte offset at which to write */
 1.46962 +){
 1.46963 +  int rc;                         /* Result code from subfunctions */
 1.46964 +  void *pData;                    /* Data actually written */
 1.46965 +  u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
 1.46966 +#if defined(SQLITE_HAS_CODEC)
 1.46967 +  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
 1.46968 +#else
 1.46969 +  pData = pPage->pData;
 1.46970 +#endif
 1.46971 +  walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
 1.46972 +  rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
 1.46973 +  if( rc ) return rc;
 1.46974 +  /* Write the page data */
 1.46975 +  rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
 1.46976 +  return rc;
 1.46977 +}
 1.46978 +
 1.46979 +/* 
 1.46980 +** Write a set of frames to the log. The caller must hold the write-lock
 1.46981 +** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
 1.46982 +*/
 1.46983 +SQLITE_PRIVATE int sqlite3WalFrames(
 1.46984 +  Wal *pWal,                      /* Wal handle to write to */
 1.46985 +  int szPage,                     /* Database page-size in bytes */
 1.46986 +  PgHdr *pList,                   /* List of dirty pages to write */
 1.46987 +  Pgno nTruncate,                 /* Database size after this commit */
 1.46988 +  int isCommit,                   /* True if this is a commit */
 1.46989 +  int sync_flags                  /* Flags to pass to OsSync() (or 0) */
 1.46990 +){
 1.46991 +  int rc;                         /* Used to catch return codes */
 1.46992 +  u32 iFrame;                     /* Next frame address */
 1.46993 +  PgHdr *p;                       /* Iterator to run through pList with. */
 1.46994 +  PgHdr *pLast = 0;               /* Last frame in list */
 1.46995 +  int nExtra = 0;                 /* Number of extra copies of last page */
 1.46996 +  int szFrame;                    /* The size of a single frame */
 1.46997 +  i64 iOffset;                    /* Next byte to write in WAL file */
 1.46998 +  WalWriter w;                    /* The writer */
 1.46999 +
 1.47000 +  assert( pList );
 1.47001 +  assert( pWal->writeLock );
 1.47002 +
 1.47003 +  /* If this frame set completes a transaction, then nTruncate>0.  If
 1.47004 +  ** nTruncate==0 then this frame set does not complete the transaction. */
 1.47005 +  assert( (isCommit!=0)==(nTruncate!=0) );
 1.47006 +
 1.47007 +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 1.47008 +  { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
 1.47009 +    WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
 1.47010 +              pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
 1.47011 +  }
 1.47012 +#endif
 1.47013 +
 1.47014 +  /* See if it is possible to write these frames into the start of the
 1.47015 +  ** log file, instead of appending to it at pWal->hdr.mxFrame.
 1.47016 +  */
 1.47017 +  if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
 1.47018 +    return rc;
 1.47019 +  }
 1.47020 +
 1.47021 +  /* If this is the first frame written into the log, write the WAL
 1.47022 +  ** header to the start of the WAL file. See comments at the top of
 1.47023 +  ** this source file for a description of the WAL header format.
 1.47024 +  */
 1.47025 +  iFrame = pWal->hdr.mxFrame;
 1.47026 +  if( iFrame==0 ){
 1.47027 +    u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
 1.47028 +    u32 aCksum[2];                /* Checksum for wal-header */
 1.47029 +
 1.47030 +    sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
 1.47031 +    sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
 1.47032 +    sqlite3Put4byte(&aWalHdr[8], szPage);
 1.47033 +    sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
 1.47034 +    if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
 1.47035 +    memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
 1.47036 +    walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
 1.47037 +    sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
 1.47038 +    sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
 1.47039 +    
 1.47040 +    pWal->szPage = szPage;
 1.47041 +    pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
 1.47042 +    pWal->hdr.aFrameCksum[0] = aCksum[0];
 1.47043 +    pWal->hdr.aFrameCksum[1] = aCksum[1];
 1.47044 +    pWal->truncateOnCommit = 1;
 1.47045 +
 1.47046 +    rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
 1.47047 +    WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
 1.47048 +    if( rc!=SQLITE_OK ){
 1.47049 +      return rc;
 1.47050 +    }
 1.47051 +
 1.47052 +    /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
 1.47053 +    ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
 1.47054 +    ** an out-of-order write following a WAL restart could result in
 1.47055 +    ** database corruption.  See the ticket:
 1.47056 +    **
 1.47057 +    **     http://localhost:591/sqlite/info/ff5be73dee
 1.47058 +    */
 1.47059 +    if( pWal->syncHeader && sync_flags ){
 1.47060 +      rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
 1.47061 +      if( rc ) return rc;
 1.47062 +    }
 1.47063 +  }
 1.47064 +  assert( (int)pWal->szPage==szPage );
 1.47065 +
 1.47066 +  /* Setup information needed to write frames into the WAL */
 1.47067 +  w.pWal = pWal;
 1.47068 +  w.pFd = pWal->pWalFd;
 1.47069 +  w.iSyncPoint = 0;
 1.47070 +  w.syncFlags = sync_flags;
 1.47071 +  w.szPage = szPage;
 1.47072 +  iOffset = walFrameOffset(iFrame+1, szPage);
 1.47073 +  szFrame = szPage + WAL_FRAME_HDRSIZE;
 1.47074 +
 1.47075 +  /* Write all frames into the log file exactly once */
 1.47076 +  for(p=pList; p; p=p->pDirty){
 1.47077 +    int nDbSize;   /* 0 normally.  Positive == commit flag */
 1.47078 +    iFrame++;
 1.47079 +    assert( iOffset==walFrameOffset(iFrame, szPage) );
 1.47080 +    nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
 1.47081 +    rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
 1.47082 +    if( rc ) return rc;
 1.47083 +    pLast = p;
 1.47084 +    iOffset += szFrame;
 1.47085 +  }
 1.47086 +
 1.47087 +  /* If this is the end of a transaction, then we might need to pad
 1.47088 +  ** the transaction and/or sync the WAL file.
 1.47089 +  **
 1.47090 +  ** Padding and syncing only occur if this set of frames complete a
 1.47091 +  ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
 1.47092 +  ** or synchonous==OFF, then no padding or syncing are needed.
 1.47093 +  **
 1.47094 +  ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
 1.47095 +  ** needed and only the sync is done.  If padding is needed, then the
 1.47096 +  ** final frame is repeated (with its commit mark) until the next sector
 1.47097 +  ** boundary is crossed.  Only the part of the WAL prior to the last
 1.47098 +  ** sector boundary is synced; the part of the last frame that extends
 1.47099 +  ** past the sector boundary is written after the sync.
 1.47100 +  */
 1.47101 +  if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
 1.47102 +    if( pWal->padToSectorBoundary ){
 1.47103 +      int sectorSize = sqlite3SectorSize(pWal->pWalFd);
 1.47104 +      w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
 1.47105 +      while( iOffset<w.iSyncPoint ){
 1.47106 +        rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
 1.47107 +        if( rc ) return rc;
 1.47108 +        iOffset += szFrame;
 1.47109 +        nExtra++;
 1.47110 +      }
 1.47111 +    }else{
 1.47112 +      rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
 1.47113 +    }
 1.47114 +  }
 1.47115 +
 1.47116 +  /* If this frame set completes the first transaction in the WAL and
 1.47117 +  ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
 1.47118 +  ** journal size limit, if possible.
 1.47119 +  */
 1.47120 +  if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
 1.47121 +    i64 sz = pWal->mxWalSize;
 1.47122 +    if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
 1.47123 +      sz = walFrameOffset(iFrame+nExtra+1, szPage);
 1.47124 +    }
 1.47125 +    walLimitSize(pWal, sz);
 1.47126 +    pWal->truncateOnCommit = 0;
 1.47127 +  }
 1.47128 +
 1.47129 +  /* Append data to the wal-index. It is not necessary to lock the 
 1.47130 +  ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
 1.47131 +  ** guarantees that there are no other writers, and no data that may
 1.47132 +  ** be in use by existing readers is being overwritten.
 1.47133 +  */
 1.47134 +  iFrame = pWal->hdr.mxFrame;
 1.47135 +  for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
 1.47136 +    iFrame++;
 1.47137 +    rc = walIndexAppend(pWal, iFrame, p->pgno);
 1.47138 +  }
 1.47139 +  while( rc==SQLITE_OK && nExtra>0 ){
 1.47140 +    iFrame++;
 1.47141 +    nExtra--;
 1.47142 +    rc = walIndexAppend(pWal, iFrame, pLast->pgno);
 1.47143 +  }
 1.47144 +
 1.47145 +  if( rc==SQLITE_OK ){
 1.47146 +    /* Update the private copy of the header. */
 1.47147 +    pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
 1.47148 +    testcase( szPage<=32768 );
 1.47149 +    testcase( szPage>=65536 );
 1.47150 +    pWal->hdr.mxFrame = iFrame;
 1.47151 +    if( isCommit ){
 1.47152 +      pWal->hdr.iChange++;
 1.47153 +      pWal->hdr.nPage = nTruncate;
 1.47154 +    }
 1.47155 +    /* If this is a commit, update the wal-index header too. */
 1.47156 +    if( isCommit ){
 1.47157 +      walIndexWriteHdr(pWal);
 1.47158 +      pWal->iCallback = iFrame;
 1.47159 +    }
 1.47160 +  }
 1.47161 +
 1.47162 +  WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
 1.47163 +  return rc;
 1.47164 +}
 1.47165 +
 1.47166 +/* 
 1.47167 +** This routine is called to implement sqlite3_wal_checkpoint() and
 1.47168 +** related interfaces.
 1.47169 +**
 1.47170 +** Obtain a CHECKPOINT lock and then backfill as much information as
 1.47171 +** we can from WAL into the database.
 1.47172 +**
 1.47173 +** If parameter xBusy is not NULL, it is a pointer to a busy-handler
 1.47174 +** callback. In this case this function runs a blocking checkpoint.
 1.47175 +*/
 1.47176 +SQLITE_PRIVATE int sqlite3WalCheckpoint(
 1.47177 +  Wal *pWal,                      /* Wal connection */
 1.47178 +  int eMode,                      /* PASSIVE, FULL or RESTART */
 1.47179 +  int (*xBusy)(void*),            /* Function to call when busy */
 1.47180 +  void *pBusyArg,                 /* Context argument for xBusyHandler */
 1.47181 +  int sync_flags,                 /* Flags to sync db file with (or 0) */
 1.47182 +  int nBuf,                       /* Size of temporary buffer */
 1.47183 +  u8 *zBuf,                       /* Temporary buffer to use */
 1.47184 +  int *pnLog,                     /* OUT: Number of frames in WAL */
 1.47185 +  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 1.47186 +){
 1.47187 +  int rc;                         /* Return code */
 1.47188 +  int isChanged = 0;              /* True if a new wal-index header is loaded */
 1.47189 +  int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
 1.47190 +
 1.47191 +  assert( pWal->ckptLock==0 );
 1.47192 +  assert( pWal->writeLock==0 );
 1.47193 +
 1.47194 +  if( pWal->readOnly ) return SQLITE_READONLY;
 1.47195 +  WALTRACE(("WAL%p: checkpoint begins\n", pWal));
 1.47196 +  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
 1.47197 +  if( rc ){
 1.47198 +    /* Usually this is SQLITE_BUSY meaning that another thread or process
 1.47199 +    ** is already running a checkpoint, or maybe a recovery.  But it might
 1.47200 +    ** also be SQLITE_IOERR. */
 1.47201 +    return rc;
 1.47202 +  }
 1.47203 +  pWal->ckptLock = 1;
 1.47204 +
 1.47205 +  /* If this is a blocking-checkpoint, then obtain the write-lock as well
 1.47206 +  ** to prevent any writers from running while the checkpoint is underway.
 1.47207 +  ** This has to be done before the call to walIndexReadHdr() below.
 1.47208 +  **
 1.47209 +  ** If the writer lock cannot be obtained, then a passive checkpoint is
 1.47210 +  ** run instead. Since the checkpointer is not holding the writer lock,
 1.47211 +  ** there is no point in blocking waiting for any readers. Assuming no 
 1.47212 +  ** other error occurs, this function will return SQLITE_BUSY to the caller.
 1.47213 +  */
 1.47214 +  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
 1.47215 +    rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
 1.47216 +    if( rc==SQLITE_OK ){
 1.47217 +      pWal->writeLock = 1;
 1.47218 +    }else if( rc==SQLITE_BUSY ){
 1.47219 +      eMode2 = SQLITE_CHECKPOINT_PASSIVE;
 1.47220 +      rc = SQLITE_OK;
 1.47221 +    }
 1.47222 +  }
 1.47223 +
 1.47224 +  /* Read the wal-index header. */
 1.47225 +  if( rc==SQLITE_OK ){
 1.47226 +    rc = walIndexReadHdr(pWal, &isChanged);
 1.47227 +  }
 1.47228 +
 1.47229 +  /* Copy data from the log to the database file. */
 1.47230 +  if( rc==SQLITE_OK ){
 1.47231 +    if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
 1.47232 +      rc = SQLITE_CORRUPT_BKPT;
 1.47233 +    }else{
 1.47234 +      rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
 1.47235 +    }
 1.47236 +
 1.47237 +    /* If no error occurred, set the output variables. */
 1.47238 +    if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
 1.47239 +      if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
 1.47240 +      if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
 1.47241 +    }
 1.47242 +  }
 1.47243 +
 1.47244 +  if( isChanged ){
 1.47245 +    /* If a new wal-index header was loaded before the checkpoint was 
 1.47246 +    ** performed, then the pager-cache associated with pWal is now
 1.47247 +    ** out of date. So zero the cached wal-index header to ensure that
 1.47248 +    ** next time the pager opens a snapshot on this database it knows that
 1.47249 +    ** the cache needs to be reset.
 1.47250 +    */
 1.47251 +    memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 1.47252 +  }
 1.47253 +
 1.47254 +  /* Release the locks. */
 1.47255 +  sqlite3WalEndWriteTransaction(pWal);
 1.47256 +  walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
 1.47257 +  pWal->ckptLock = 0;
 1.47258 +  WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
 1.47259 +  return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
 1.47260 +}
 1.47261 +
 1.47262 +/* Return the value to pass to a sqlite3_wal_hook callback, the
 1.47263 +** number of frames in the WAL at the point of the last commit since
 1.47264 +** sqlite3WalCallback() was called.  If no commits have occurred since
 1.47265 +** the last call, then return 0.
 1.47266 +*/
 1.47267 +SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
 1.47268 +  u32 ret = 0;
 1.47269 +  if( pWal ){
 1.47270 +    ret = pWal->iCallback;
 1.47271 +    pWal->iCallback = 0;
 1.47272 +  }
 1.47273 +  return (int)ret;
 1.47274 +}
 1.47275 +
 1.47276 +/*
 1.47277 +** This function is called to change the WAL subsystem into or out
 1.47278 +** of locking_mode=EXCLUSIVE.
 1.47279 +**
 1.47280 +** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
 1.47281 +** into locking_mode=NORMAL.  This means that we must acquire a lock
 1.47282 +** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
 1.47283 +** or if the acquisition of the lock fails, then return 0.  If the
 1.47284 +** transition out of exclusive-mode is successful, return 1.  This
 1.47285 +** operation must occur while the pager is still holding the exclusive
 1.47286 +** lock on the main database file.
 1.47287 +**
 1.47288 +** If op is one, then change from locking_mode=NORMAL into 
 1.47289 +** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
 1.47290 +** be released.  Return 1 if the transition is made and 0 if the
 1.47291 +** WAL is already in exclusive-locking mode - meaning that this
 1.47292 +** routine is a no-op.  The pager must already hold the exclusive lock
 1.47293 +** on the main database file before invoking this operation.
 1.47294 +**
 1.47295 +** If op is negative, then do a dry-run of the op==1 case but do
 1.47296 +** not actually change anything. The pager uses this to see if it
 1.47297 +** should acquire the database exclusive lock prior to invoking
 1.47298 +** the op==1 case.
 1.47299 +*/
 1.47300 +SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
 1.47301 +  int rc;
 1.47302 +  assert( pWal->writeLock==0 );
 1.47303 +  assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
 1.47304 +
 1.47305 +  /* pWal->readLock is usually set, but might be -1 if there was a 
 1.47306 +  ** prior error while attempting to acquire are read-lock. This cannot 
 1.47307 +  ** happen if the connection is actually in exclusive mode (as no xShmLock
 1.47308 +  ** locks are taken in this case). Nor should the pager attempt to
 1.47309 +  ** upgrade to exclusive-mode following such an error.
 1.47310 +  */
 1.47311 +  assert( pWal->readLock>=0 || pWal->lockError );
 1.47312 +  assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
 1.47313 +
 1.47314 +  if( op==0 ){
 1.47315 +    if( pWal->exclusiveMode ){
 1.47316 +      pWal->exclusiveMode = 0;
 1.47317 +      if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
 1.47318 +        pWal->exclusiveMode = 1;
 1.47319 +      }
 1.47320 +      rc = pWal->exclusiveMode==0;
 1.47321 +    }else{
 1.47322 +      /* Already in locking_mode=NORMAL */
 1.47323 +      rc = 0;
 1.47324 +    }
 1.47325 +  }else if( op>0 ){
 1.47326 +    assert( pWal->exclusiveMode==0 );
 1.47327 +    assert( pWal->readLock>=0 );
 1.47328 +    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
 1.47329 +    pWal->exclusiveMode = 1;
 1.47330 +    rc = 1;
 1.47331 +  }else{
 1.47332 +    rc = pWal->exclusiveMode==0;
 1.47333 +  }
 1.47334 +  return rc;
 1.47335 +}
 1.47336 +
 1.47337 +/* 
 1.47338 +** Return true if the argument is non-NULL and the WAL module is using
 1.47339 +** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
 1.47340 +** WAL module is using shared-memory, return false. 
 1.47341 +*/
 1.47342 +SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
 1.47343 +  return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
 1.47344 +}
 1.47345 +
 1.47346 +#ifdef SQLITE_ENABLE_ZIPVFS
 1.47347 +/*
 1.47348 +** If the argument is not NULL, it points to a Wal object that holds a
 1.47349 +** read-lock. This function returns the database page-size if it is known,
 1.47350 +** or zero if it is not (or if pWal is NULL).
 1.47351 +*/
 1.47352 +SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
 1.47353 +  assert( pWal==0 || pWal->readLock>=0 );
 1.47354 +  return (pWal ? pWal->szPage : 0);
 1.47355 +}
 1.47356 +#endif
 1.47357 +
 1.47358 +#endif /* #ifndef SQLITE_OMIT_WAL */
 1.47359 +
 1.47360 +/************** End of wal.c *************************************************/
 1.47361 +/************** Begin file btmutex.c *****************************************/
 1.47362 +/*
 1.47363 +** 2007 August 27
 1.47364 +**
 1.47365 +** The author disclaims copyright to this source code.  In place of
 1.47366 +** a legal notice, here is a blessing:
 1.47367 +**
 1.47368 +**    May you do good and not evil.
 1.47369 +**    May you find forgiveness for yourself and forgive others.
 1.47370 +**    May you share freely, never taking more than you give.
 1.47371 +**
 1.47372 +*************************************************************************
 1.47373 +**
 1.47374 +** This file contains code used to implement mutexes on Btree objects.
 1.47375 +** This code really belongs in btree.c.  But btree.c is getting too
 1.47376 +** big and we want to break it down some.  This packaged seemed like
 1.47377 +** a good breakout.
 1.47378 +*/
 1.47379 +/************** Include btreeInt.h in the middle of btmutex.c ****************/
 1.47380 +/************** Begin file btreeInt.h ****************************************/
 1.47381 +/*
 1.47382 +** 2004 April 6
 1.47383 +**
 1.47384 +** The author disclaims copyright to this source code.  In place of
 1.47385 +** a legal notice, here is a blessing:
 1.47386 +**
 1.47387 +**    May you do good and not evil.
 1.47388 +**    May you find forgiveness for yourself and forgive others.
 1.47389 +**    May you share freely, never taking more than you give.
 1.47390 +**
 1.47391 +*************************************************************************
 1.47392 +** This file implements a external (disk-based) database using BTrees.
 1.47393 +** For a detailed discussion of BTrees, refer to
 1.47394 +**
 1.47395 +**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
 1.47396 +**     "Sorting And Searching", pages 473-480. Addison-Wesley
 1.47397 +**     Publishing Company, Reading, Massachusetts.
 1.47398 +**
 1.47399 +** The basic idea is that each page of the file contains N database
 1.47400 +** entries and N+1 pointers to subpages.
 1.47401 +**
 1.47402 +**   ----------------------------------------------------------------
 1.47403 +**   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
 1.47404 +**   ----------------------------------------------------------------
 1.47405 +**
 1.47406 +** All of the keys on the page that Ptr(0) points to have values less
 1.47407 +** than Key(0).  All of the keys on page Ptr(1) and its subpages have
 1.47408 +** values greater than Key(0) and less than Key(1).  All of the keys
 1.47409 +** on Ptr(N) and its subpages have values greater than Key(N-1).  And
 1.47410 +** so forth.
 1.47411 +**
 1.47412 +** Finding a particular key requires reading O(log(M)) pages from the 
 1.47413 +** disk where M is the number of entries in the tree.
 1.47414 +**
 1.47415 +** In this implementation, a single file can hold one or more separate 
 1.47416 +** BTrees.  Each BTree is identified by the index of its root page.  The
 1.47417 +** key and data for any entry are combined to form the "payload".  A
 1.47418 +** fixed amount of payload can be carried directly on the database
 1.47419 +** page.  If the payload is larger than the preset amount then surplus
 1.47420 +** bytes are stored on overflow pages.  The payload for an entry
 1.47421 +** and the preceding pointer are combined to form a "Cell".  Each 
 1.47422 +** page has a small header which contains the Ptr(N) pointer and other
 1.47423 +** information such as the size of key and data.
 1.47424 +**
 1.47425 +** FORMAT DETAILS
 1.47426 +**
 1.47427 +** The file is divided into pages.  The first page is called page 1,
 1.47428 +** the second is page 2, and so forth.  A page number of zero indicates
 1.47429 +** "no such page".  The page size can be any power of 2 between 512 and 65536.
 1.47430 +** Each page can be either a btree page, a freelist page, an overflow
 1.47431 +** page, or a pointer-map page.
 1.47432 +**
 1.47433 +** The first page is always a btree page.  The first 100 bytes of the first
 1.47434 +** page contain a special header (the "file header") that describes the file.
 1.47435 +** The format of the file header is as follows:
 1.47436 +**
 1.47437 +**   OFFSET   SIZE    DESCRIPTION
 1.47438 +**      0      16     Header string: "SQLite format 3\000"
 1.47439 +**     16       2     Page size in bytes.  
 1.47440 +**     18       1     File format write version
 1.47441 +**     19       1     File format read version
 1.47442 +**     20       1     Bytes of unused space at the end of each page
 1.47443 +**     21       1     Max embedded payload fraction
 1.47444 +**     22       1     Min embedded payload fraction
 1.47445 +**     23       1     Min leaf payload fraction
 1.47446 +**     24       4     File change counter
 1.47447 +**     28       4     Reserved for future use
 1.47448 +**     32       4     First freelist page
 1.47449 +**     36       4     Number of freelist pages in the file
 1.47450 +**     40      60     15 4-byte meta values passed to higher layers
 1.47451 +**
 1.47452 +**     40       4     Schema cookie
 1.47453 +**     44       4     File format of schema layer
 1.47454 +**     48       4     Size of page cache
 1.47455 +**     52       4     Largest root-page (auto/incr_vacuum)
 1.47456 +**     56       4     1=UTF-8 2=UTF16le 3=UTF16be
 1.47457 +**     60       4     User version
 1.47458 +**     64       4     Incremental vacuum mode
 1.47459 +**     68       4     unused
 1.47460 +**     72       4     unused
 1.47461 +**     76       4     unused
 1.47462 +**
 1.47463 +** All of the integer values are big-endian (most significant byte first).
 1.47464 +**
 1.47465 +** The file change counter is incremented when the database is changed
 1.47466 +** This counter allows other processes to know when the file has changed
 1.47467 +** and thus when they need to flush their cache.
 1.47468 +**
 1.47469 +** The max embedded payload fraction is the amount of the total usable
 1.47470 +** space in a page that can be consumed by a single cell for standard
 1.47471 +** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
 1.47472 +** is to limit the maximum cell size so that at least 4 cells will fit
 1.47473 +** on one page.  Thus the default max embedded payload fraction is 64.
 1.47474 +**
 1.47475 +** If the payload for a cell is larger than the max payload, then extra
 1.47476 +** payload is spilled to overflow pages.  Once an overflow page is allocated,
 1.47477 +** as many bytes as possible are moved into the overflow pages without letting
 1.47478 +** the cell size drop below the min embedded payload fraction.
 1.47479 +**
 1.47480 +** The min leaf payload fraction is like the min embedded payload fraction
 1.47481 +** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
 1.47482 +** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
 1.47483 +** not specified in the header.
 1.47484 +**
 1.47485 +** Each btree pages is divided into three sections:  The header, the
 1.47486 +** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
 1.47487 +** file header that occurs before the page header.
 1.47488 +**
 1.47489 +**      |----------------|
 1.47490 +**      | file header    |   100 bytes.  Page 1 only.
 1.47491 +**      |----------------|
 1.47492 +**      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
 1.47493 +**      |----------------|
 1.47494 +**      | cell pointer   |   |  2 bytes per cell.  Sorted order.
 1.47495 +**      | array          |   |  Grows downward
 1.47496 +**      |                |   v
 1.47497 +**      |----------------|
 1.47498 +**      | unallocated    |
 1.47499 +**      | space          |
 1.47500 +**      |----------------|   ^  Grows upwards
 1.47501 +**      | cell content   |   |  Arbitrary order interspersed with freeblocks.
 1.47502 +**      | area           |   |  and free space fragments.
 1.47503 +**      |----------------|
 1.47504 +**
 1.47505 +** The page headers looks like this:
 1.47506 +**
 1.47507 +**   OFFSET   SIZE     DESCRIPTION
 1.47508 +**      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
 1.47509 +**      1       2      byte offset to the first freeblock
 1.47510 +**      3       2      number of cells on this page
 1.47511 +**      5       2      first byte of the cell content area
 1.47512 +**      7       1      number of fragmented free bytes
 1.47513 +**      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
 1.47514 +**
 1.47515 +** The flags define the format of this btree page.  The leaf flag means that
 1.47516 +** this page has no children.  The zerodata flag means that this page carries
 1.47517 +** only keys and no data.  The intkey flag means that the key is a integer
 1.47518 +** which is stored in the key size entry of the cell header rather than in
 1.47519 +** the payload area.
 1.47520 +**
 1.47521 +** The cell pointer array begins on the first byte after the page header.
 1.47522 +** The cell pointer array contains zero or more 2-byte numbers which are
 1.47523 +** offsets from the beginning of the page to the cell content in the cell
 1.47524 +** content area.  The cell pointers occur in sorted order.  The system strives
 1.47525 +** to keep free space after the last cell pointer so that new cells can
 1.47526 +** be easily added without having to defragment the page.
 1.47527 +**
 1.47528 +** Cell content is stored at the very end of the page and grows toward the
 1.47529 +** beginning of the page.
 1.47530 +**
 1.47531 +** Unused space within the cell content area is collected into a linked list of
 1.47532 +** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
 1.47533 +** to the first freeblock is given in the header.  Freeblocks occur in
 1.47534 +** increasing order.  Because a freeblock must be at least 4 bytes in size,
 1.47535 +** any group of 3 or fewer unused bytes in the cell content area cannot
 1.47536 +** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
 1.47537 +** a fragment.  The total number of bytes in all fragments is recorded.
 1.47538 +** in the page header at offset 7.
 1.47539 +**
 1.47540 +**    SIZE    DESCRIPTION
 1.47541 +**      2     Byte offset of the next freeblock
 1.47542 +**      2     Bytes in this freeblock
 1.47543 +**
 1.47544 +** Cells are of variable length.  Cells are stored in the cell content area at
 1.47545 +** the end of the page.  Pointers to the cells are in the cell pointer array
 1.47546 +** that immediately follows the page header.  Cells is not necessarily
 1.47547 +** contiguous or in order, but cell pointers are contiguous and in order.
 1.47548 +**
 1.47549 +** Cell content makes use of variable length integers.  A variable
 1.47550 +** length integer is 1 to 9 bytes where the lower 7 bits of each 
 1.47551 +** byte are used.  The integer consists of all bytes that have bit 8 set and
 1.47552 +** the first byte with bit 8 clear.  The most significant byte of the integer
 1.47553 +** appears first.  A variable-length integer may not be more than 9 bytes long.
 1.47554 +** As a special case, all 8 bytes of the 9th byte are used as data.  This
 1.47555 +** allows a 64-bit integer to be encoded in 9 bytes.
 1.47556 +**
 1.47557 +**    0x00                      becomes  0x00000000
 1.47558 +**    0x7f                      becomes  0x0000007f
 1.47559 +**    0x81 0x00                 becomes  0x00000080
 1.47560 +**    0x82 0x00                 becomes  0x00000100
 1.47561 +**    0x80 0x7f                 becomes  0x0000007f
 1.47562 +**    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
 1.47563 +**    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
 1.47564 +**
 1.47565 +** Variable length integers are used for rowids and to hold the number of
 1.47566 +** bytes of key and data in a btree cell.
 1.47567 +**
 1.47568 +** The content of a cell looks like this:
 1.47569 +**
 1.47570 +**    SIZE    DESCRIPTION
 1.47571 +**      4     Page number of the left child. Omitted if leaf flag is set.
 1.47572 +**     var    Number of bytes of data. Omitted if the zerodata flag is set.
 1.47573 +**     var    Number of bytes of key. Or the key itself if intkey flag is set.
 1.47574 +**      *     Payload
 1.47575 +**      4     First page of the overflow chain.  Omitted if no overflow
 1.47576 +**
 1.47577 +** Overflow pages form a linked list.  Each page except the last is completely
 1.47578 +** filled with data (pagesize - 4 bytes).  The last page can have as little
 1.47579 +** as 1 byte of data.
 1.47580 +**
 1.47581 +**    SIZE    DESCRIPTION
 1.47582 +**      4     Page number of next overflow page
 1.47583 +**      *     Data
 1.47584 +**
 1.47585 +** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
 1.47586 +** file header points to the first in a linked list of trunk page.  Each trunk
 1.47587 +** page points to multiple leaf pages.  The content of a leaf page is
 1.47588 +** unspecified.  A trunk page looks like this:
 1.47589 +**
 1.47590 +**    SIZE    DESCRIPTION
 1.47591 +**      4     Page number of next trunk page
 1.47592 +**      4     Number of leaf pointers on this page
 1.47593 +**      *     zero or more pages numbers of leaves
 1.47594 +*/
 1.47595 +
 1.47596 +
 1.47597 +/* The following value is the maximum cell size assuming a maximum page
 1.47598 +** size give above.
 1.47599 +*/
 1.47600 +#define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
 1.47601 +
 1.47602 +/* The maximum number of cells on a single page of the database.  This
 1.47603 +** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
 1.47604 +** plus 2 bytes for the index to the cell in the page header).  Such
 1.47605 +** small cells will be rare, but they are possible.
 1.47606 +*/
 1.47607 +#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
 1.47608 +
 1.47609 +/* Forward declarations */
 1.47610 +typedef struct MemPage MemPage;
 1.47611 +typedef struct BtLock BtLock;
 1.47612 +
 1.47613 +/*
 1.47614 +** This is a magic string that appears at the beginning of every
 1.47615 +** SQLite database in order to identify the file as a real database.
 1.47616 +**
 1.47617 +** You can change this value at compile-time by specifying a
 1.47618 +** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
 1.47619 +** header must be exactly 16 bytes including the zero-terminator so
 1.47620 +** the string itself should be 15 characters long.  If you change
 1.47621 +** the header, then your custom library will not be able to read 
 1.47622 +** databases generated by the standard tools and the standard tools
 1.47623 +** will not be able to read databases created by your custom library.
 1.47624 +*/
 1.47625 +#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
 1.47626 +#  define SQLITE_FILE_HEADER "SQLite format 3"
 1.47627 +#endif
 1.47628 +
 1.47629 +/*
 1.47630 +** Page type flags.  An ORed combination of these flags appear as the
 1.47631 +** first byte of on-disk image of every BTree page.
 1.47632 +*/
 1.47633 +#define PTF_INTKEY    0x01
 1.47634 +#define PTF_ZERODATA  0x02
 1.47635 +#define PTF_LEAFDATA  0x04
 1.47636 +#define PTF_LEAF      0x08
 1.47637 +
 1.47638 +/*
 1.47639 +** As each page of the file is loaded into memory, an instance of the following
 1.47640 +** structure is appended and initialized to zero.  This structure stores
 1.47641 +** information about the page that is decoded from the raw file page.
 1.47642 +**
 1.47643 +** The pParent field points back to the parent page.  This allows us to
 1.47644 +** walk up the BTree from any leaf to the root.  Care must be taken to
 1.47645 +** unref() the parent page pointer when this page is no longer referenced.
 1.47646 +** The pageDestructor() routine handles that chore.
 1.47647 +**
 1.47648 +** Access to all fields of this structure is controlled by the mutex
 1.47649 +** stored in MemPage.pBt->mutex.
 1.47650 +*/
 1.47651 +struct MemPage {
 1.47652 +  u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
 1.47653 +  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
 1.47654 +  u8 intKey;           /* True if intkey flag is set */
 1.47655 +  u8 leaf;             /* True if leaf flag is set */
 1.47656 +  u8 hasData;          /* True if this page stores data */
 1.47657 +  u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
 1.47658 +  u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
 1.47659 +  u8 max1bytePayload;  /* min(maxLocal,127) */
 1.47660 +  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
 1.47661 +  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
 1.47662 +  u16 cellOffset;      /* Index in aData of first cell pointer */
 1.47663 +  u16 nFree;           /* Number of free bytes on the page */
 1.47664 +  u16 nCell;           /* Number of cells on this page, local and ovfl */
 1.47665 +  u16 maskPage;        /* Mask for page offset */
 1.47666 +  u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
 1.47667 +                       ** non-overflow cell */
 1.47668 +  u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
 1.47669 +  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
 1.47670 +  u8 *aData;           /* Pointer to disk image of the page data */
 1.47671 +  u8 *aDataEnd;        /* One byte past the end of usable data */
 1.47672 +  u8 *aCellIdx;        /* The cell index area */
 1.47673 +  DbPage *pDbPage;     /* Pager page handle */
 1.47674 +  Pgno pgno;           /* Page number for this page */
 1.47675 +};
 1.47676 +
 1.47677 +/*
 1.47678 +** The in-memory image of a disk page has the auxiliary information appended
 1.47679 +** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
 1.47680 +** that extra information.
 1.47681 +*/
 1.47682 +#define EXTRA_SIZE sizeof(MemPage)
 1.47683 +
 1.47684 +/*
 1.47685 +** A linked list of the following structures is stored at BtShared.pLock.
 1.47686 +** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
 1.47687 +** is opened on the table with root page BtShared.iTable. Locks are removed
 1.47688 +** from this list when a transaction is committed or rolled back, or when
 1.47689 +** a btree handle is closed.
 1.47690 +*/
 1.47691 +struct BtLock {
 1.47692 +  Btree *pBtree;        /* Btree handle holding this lock */
 1.47693 +  Pgno iTable;          /* Root page of table */
 1.47694 +  u8 eLock;             /* READ_LOCK or WRITE_LOCK */
 1.47695 +  BtLock *pNext;        /* Next in BtShared.pLock list */
 1.47696 +};
 1.47697 +
 1.47698 +/* Candidate values for BtLock.eLock */
 1.47699 +#define READ_LOCK     1
 1.47700 +#define WRITE_LOCK    2
 1.47701 +
 1.47702 +/* A Btree handle
 1.47703 +**
 1.47704 +** A database connection contains a pointer to an instance of
 1.47705 +** this object for every database file that it has open.  This structure
 1.47706 +** is opaque to the database connection.  The database connection cannot
 1.47707 +** see the internals of this structure and only deals with pointers to
 1.47708 +** this structure.
 1.47709 +**
 1.47710 +** For some database files, the same underlying database cache might be 
 1.47711 +** shared between multiple connections.  In that case, each connection
 1.47712 +** has it own instance of this object.  But each instance of this object
 1.47713 +** points to the same BtShared object.  The database cache and the
 1.47714 +** schema associated with the database file are all contained within
 1.47715 +** the BtShared object.
 1.47716 +**
 1.47717 +** All fields in this structure are accessed under sqlite3.mutex.
 1.47718 +** The pBt pointer itself may not be changed while there exists cursors 
 1.47719 +** in the referenced BtShared that point back to this Btree since those
 1.47720 +** cursors have to go through this Btree to find their BtShared and
 1.47721 +** they often do so without holding sqlite3.mutex.
 1.47722 +*/
 1.47723 +struct Btree {
 1.47724 +  sqlite3 *db;       /* The database connection holding this btree */
 1.47725 +  BtShared *pBt;     /* Sharable content of this btree */
 1.47726 +  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
 1.47727 +  u8 sharable;       /* True if we can share pBt with another db */
 1.47728 +  u8 locked;         /* True if db currently has pBt locked */
 1.47729 +  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
 1.47730 +  int nBackup;       /* Number of backup operations reading this btree */
 1.47731 +  Btree *pNext;      /* List of other sharable Btrees from the same db */
 1.47732 +  Btree *pPrev;      /* Back pointer of the same list */
 1.47733 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.47734 +  BtLock lock;       /* Object used to lock page 1 */
 1.47735 +#endif
 1.47736 +};
 1.47737 +
 1.47738 +/*
 1.47739 +** Btree.inTrans may take one of the following values.
 1.47740 +**
 1.47741 +** If the shared-data extension is enabled, there may be multiple users
 1.47742 +** of the Btree structure. At most one of these may open a write transaction,
 1.47743 +** but any number may have active read transactions.
 1.47744 +*/
 1.47745 +#define TRANS_NONE  0
 1.47746 +#define TRANS_READ  1
 1.47747 +#define TRANS_WRITE 2
 1.47748 +
 1.47749 +/*
 1.47750 +** An instance of this object represents a single database file.
 1.47751 +** 
 1.47752 +** A single database file can be in use at the same time by two
 1.47753 +** or more database connections.  When two or more connections are
 1.47754 +** sharing the same database file, each connection has it own
 1.47755 +** private Btree object for the file and each of those Btrees points
 1.47756 +** to this one BtShared object.  BtShared.nRef is the number of
 1.47757 +** connections currently sharing this database file.
 1.47758 +**
 1.47759 +** Fields in this structure are accessed under the BtShared.mutex
 1.47760 +** mutex, except for nRef and pNext which are accessed under the
 1.47761 +** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
 1.47762 +** may not be modified once it is initially set as long as nRef>0.
 1.47763 +** The pSchema field may be set once under BtShared.mutex and
 1.47764 +** thereafter is unchanged as long as nRef>0.
 1.47765 +**
 1.47766 +** isPending:
 1.47767 +**
 1.47768 +**   If a BtShared client fails to obtain a write-lock on a database
 1.47769 +**   table (because there exists one or more read-locks on the table),
 1.47770 +**   the shared-cache enters 'pending-lock' state and isPending is
 1.47771 +**   set to true.
 1.47772 +**
 1.47773 +**   The shared-cache leaves the 'pending lock' state when either of
 1.47774 +**   the following occur:
 1.47775 +**
 1.47776 +**     1) The current writer (BtShared.pWriter) concludes its transaction, OR
 1.47777 +**     2) The number of locks held by other connections drops to zero.
 1.47778 +**
 1.47779 +**   while in the 'pending-lock' state, no connection may start a new
 1.47780 +**   transaction.
 1.47781 +**
 1.47782 +**   This feature is included to help prevent writer-starvation.
 1.47783 +*/
 1.47784 +struct BtShared {
 1.47785 +  Pager *pPager;        /* The page cache */
 1.47786 +  sqlite3 *db;          /* Database connection currently using this Btree */
 1.47787 +  BtCursor *pCursor;    /* A list of all open cursors */
 1.47788 +  MemPage *pPage1;      /* First page of the database */
 1.47789 +  u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
 1.47790 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.47791 +  u8 autoVacuum;        /* True if auto-vacuum is enabled */
 1.47792 +  u8 incrVacuum;        /* True if incr-vacuum is enabled */
 1.47793 +#endif
 1.47794 +  u8 inTransaction;     /* Transaction state */
 1.47795 +  u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
 1.47796 +  u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
 1.47797 +  u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
 1.47798 +  u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
 1.47799 +  u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
 1.47800 +  u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
 1.47801 +  u32 pageSize;         /* Total number of bytes on a page */
 1.47802 +  u32 usableSize;       /* Number of usable bytes on each page */
 1.47803 +  int nTransaction;     /* Number of open transactions (read + write) */
 1.47804 +  u32 nPage;            /* Number of pages in the database */
 1.47805 +  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
 1.47806 +  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
 1.47807 +  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
 1.47808 +  Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
 1.47809 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.47810 +  int nRef;             /* Number of references to this structure */
 1.47811 +  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
 1.47812 +  BtLock *pLock;        /* List of locks held on this shared-btree struct */
 1.47813 +  Btree *pWriter;       /* Btree with currently open write transaction */
 1.47814 +#endif
 1.47815 +  u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
 1.47816 +};
 1.47817 +
 1.47818 +/*
 1.47819 +** Allowed values for BtShared.btsFlags
 1.47820 +*/
 1.47821 +#define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
 1.47822 +#define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
 1.47823 +#define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
 1.47824 +#define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
 1.47825 +#define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
 1.47826 +#define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
 1.47827 +#define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
 1.47828 +
 1.47829 +/*
 1.47830 +** An instance of the following structure is used to hold information
 1.47831 +** about a cell.  The parseCellPtr() function fills in this structure
 1.47832 +** based on information extract from the raw disk page.
 1.47833 +*/
 1.47834 +typedef struct CellInfo CellInfo;
 1.47835 +struct CellInfo {
 1.47836 +  i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
 1.47837 +  u8 *pCell;     /* Pointer to the start of cell content */
 1.47838 +  u32 nData;     /* Number of bytes of data */
 1.47839 +  u32 nPayload;  /* Total amount of payload */
 1.47840 +  u16 nHeader;   /* Size of the cell content header in bytes */
 1.47841 +  u16 nLocal;    /* Amount of payload held locally */
 1.47842 +  u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
 1.47843 +  u16 nSize;     /* Size of the cell content on the main b-tree page */
 1.47844 +};
 1.47845 +
 1.47846 +/*
 1.47847 +** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
 1.47848 +** this will be declared corrupt. This value is calculated based on a
 1.47849 +** maximum database size of 2^31 pages a minimum fanout of 2 for a
 1.47850 +** root-node and 3 for all other internal nodes.
 1.47851 +**
 1.47852 +** If a tree that appears to be taller than this is encountered, it is
 1.47853 +** assumed that the database is corrupt.
 1.47854 +*/
 1.47855 +#define BTCURSOR_MAX_DEPTH 20
 1.47856 +
 1.47857 +/*
 1.47858 +** A cursor is a pointer to a particular entry within a particular
 1.47859 +** b-tree within a database file.
 1.47860 +**
 1.47861 +** The entry is identified by its MemPage and the index in
 1.47862 +** MemPage.aCell[] of the entry.
 1.47863 +**
 1.47864 +** A single database file can be shared by two more database connections,
 1.47865 +** but cursors cannot be shared.  Each cursor is associated with a
 1.47866 +** particular database connection identified BtCursor.pBtree.db.
 1.47867 +**
 1.47868 +** Fields in this structure are accessed under the BtShared.mutex
 1.47869 +** found at self->pBt->mutex. 
 1.47870 +*/
 1.47871 +struct BtCursor {
 1.47872 +  Btree *pBtree;            /* The Btree to which this cursor belongs */
 1.47873 +  BtShared *pBt;            /* The BtShared this cursor points to */
 1.47874 +  BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
 1.47875 +  struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
 1.47876 +#ifndef SQLITE_OMIT_INCRBLOB
 1.47877 +  Pgno *aOverflow;          /* Cache of overflow page locations */
 1.47878 +#endif
 1.47879 +  Pgno pgnoRoot;            /* The root page of this tree */
 1.47880 +  sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
 1.47881 +  CellInfo info;            /* A parse of the cell we are pointing at */
 1.47882 +  i64 nKey;        /* Size of pKey, or last integer key */
 1.47883 +  void *pKey;      /* Saved key that was cursor's last known position */
 1.47884 +  int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
 1.47885 +  u8 wrFlag;                /* True if writable */
 1.47886 +  u8 atLast;                /* Cursor pointing to the last entry */
 1.47887 +  u8 validNKey;             /* True if info.nKey is valid */
 1.47888 +  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 1.47889 +#ifndef SQLITE_OMIT_INCRBLOB
 1.47890 +  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
 1.47891 +#endif
 1.47892 +  u8 hints;                             /* As configured by CursorSetHints() */
 1.47893 +  i16 iPage;                            /* Index of current page in apPage */
 1.47894 +  u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
 1.47895 +  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
 1.47896 +};
 1.47897 +
 1.47898 +/*
 1.47899 +** Potential values for BtCursor.eState.
 1.47900 +**
 1.47901 +** CURSOR_VALID:
 1.47902 +**   Cursor points to a valid entry. getPayload() etc. may be called.
 1.47903 +**
 1.47904 +** CURSOR_INVALID:
 1.47905 +**   Cursor does not point to a valid entry. This can happen (for example) 
 1.47906 +**   because the table is empty or because BtreeCursorFirst() has not been
 1.47907 +**   called.
 1.47908 +**
 1.47909 +** CURSOR_REQUIRESEEK:
 1.47910 +**   The table that this cursor was opened on still exists, but has been 
 1.47911 +**   modified since the cursor was last used. The cursor position is saved
 1.47912 +**   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
 1.47913 +**   this state, restoreCursorPosition() can be called to attempt to
 1.47914 +**   seek the cursor to the saved position.
 1.47915 +**
 1.47916 +** CURSOR_FAULT:
 1.47917 +**   A unrecoverable error (an I/O error or a malloc failure) has occurred
 1.47918 +**   on a different connection that shares the BtShared cache with this
 1.47919 +**   cursor.  The error has left the cache in an inconsistent state.
 1.47920 +**   Do nothing else with this cursor.  Any attempt to use the cursor
 1.47921 +**   should return the error code stored in BtCursor.skip
 1.47922 +*/
 1.47923 +#define CURSOR_INVALID           0
 1.47924 +#define CURSOR_VALID             1
 1.47925 +#define CURSOR_REQUIRESEEK       2
 1.47926 +#define CURSOR_FAULT             3
 1.47927 +
 1.47928 +/* 
 1.47929 +** The database page the PENDING_BYTE occupies. This page is never used.
 1.47930 +*/
 1.47931 +# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
 1.47932 +
 1.47933 +/*
 1.47934 +** These macros define the location of the pointer-map entry for a 
 1.47935 +** database page. The first argument to each is the number of usable
 1.47936 +** bytes on each page of the database (often 1024). The second is the
 1.47937 +** page number to look up in the pointer map.
 1.47938 +**
 1.47939 +** PTRMAP_PAGENO returns the database page number of the pointer-map
 1.47940 +** page that stores the required pointer. PTRMAP_PTROFFSET returns
 1.47941 +** the offset of the requested map entry.
 1.47942 +**
 1.47943 +** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
 1.47944 +** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
 1.47945 +** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
 1.47946 +** this test.
 1.47947 +*/
 1.47948 +#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
 1.47949 +#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
 1.47950 +#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
 1.47951 +
 1.47952 +/*
 1.47953 +** The pointer map is a lookup table that identifies the parent page for
 1.47954 +** each child page in the database file.  The parent page is the page that
 1.47955 +** contains a pointer to the child.  Every page in the database contains
 1.47956 +** 0 or 1 parent pages.  (In this context 'database page' refers
 1.47957 +** to any page that is not part of the pointer map itself.)  Each pointer map
 1.47958 +** entry consists of a single byte 'type' and a 4 byte parent page number.
 1.47959 +** The PTRMAP_XXX identifiers below are the valid types.
 1.47960 +**
 1.47961 +** The purpose of the pointer map is to facility moving pages from one
 1.47962 +** position in the file to another as part of autovacuum.  When a page
 1.47963 +** is moved, the pointer in its parent must be updated to point to the
 1.47964 +** new location.  The pointer map is used to locate the parent page quickly.
 1.47965 +**
 1.47966 +** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
 1.47967 +**                  used in this case.
 1.47968 +**
 1.47969 +** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
 1.47970 +**                  is not used in this case.
 1.47971 +**
 1.47972 +** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
 1.47973 +**                   overflow pages. The page number identifies the page that
 1.47974 +**                   contains the cell with a pointer to this overflow page.
 1.47975 +**
 1.47976 +** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
 1.47977 +**                   overflow pages. The page-number identifies the previous
 1.47978 +**                   page in the overflow page list.
 1.47979 +**
 1.47980 +** PTRMAP_BTREE: The database page is a non-root btree page. The page number
 1.47981 +**               identifies the parent page in the btree.
 1.47982 +*/
 1.47983 +#define PTRMAP_ROOTPAGE 1
 1.47984 +#define PTRMAP_FREEPAGE 2
 1.47985 +#define PTRMAP_OVERFLOW1 3
 1.47986 +#define PTRMAP_OVERFLOW2 4
 1.47987 +#define PTRMAP_BTREE 5
 1.47988 +
 1.47989 +/* A bunch of assert() statements to check the transaction state variables
 1.47990 +** of handle p (type Btree*) are internally consistent.
 1.47991 +*/
 1.47992 +#define btreeIntegrity(p) \
 1.47993 +  assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
 1.47994 +  assert( p->pBt->inTransaction>=p->inTrans ); 
 1.47995 +
 1.47996 +
 1.47997 +/*
 1.47998 +** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
 1.47999 +** if the database supports auto-vacuum or not. Because it is used
 1.48000 +** within an expression that is an argument to another macro 
 1.48001 +** (sqliteMallocRaw), it is not possible to use conditional compilation.
 1.48002 +** So, this macro is defined instead.
 1.48003 +*/
 1.48004 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.48005 +#define ISAUTOVACUUM (pBt->autoVacuum)
 1.48006 +#else
 1.48007 +#define ISAUTOVACUUM 0
 1.48008 +#endif
 1.48009 +
 1.48010 +
 1.48011 +/*
 1.48012 +** This structure is passed around through all the sanity checking routines
 1.48013 +** in order to keep track of some global state information.
 1.48014 +**
 1.48015 +** The aRef[] array is allocated so that there is 1 bit for each page in
 1.48016 +** the database. As the integrity-check proceeds, for each page used in
 1.48017 +** the database the corresponding bit is set. This allows integrity-check to 
 1.48018 +** detect pages that are used twice and orphaned pages (both of which 
 1.48019 +** indicate corruption).
 1.48020 +*/
 1.48021 +typedef struct IntegrityCk IntegrityCk;
 1.48022 +struct IntegrityCk {
 1.48023 +  BtShared *pBt;    /* The tree being checked out */
 1.48024 +  Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
 1.48025 +  u8 *aPgRef;       /* 1 bit per page in the db (see above) */
 1.48026 +  Pgno nPage;       /* Number of pages in the database */
 1.48027 +  int mxErr;        /* Stop accumulating errors when this reaches zero */
 1.48028 +  int nErr;         /* Number of messages written to zErrMsg so far */
 1.48029 +  int mallocFailed; /* A memory allocation error has occurred */
 1.48030 +  StrAccum errMsg;  /* Accumulate the error message text here */
 1.48031 +};
 1.48032 +
 1.48033 +/*
 1.48034 +** Routines to read or write a two- and four-byte big-endian integer values.
 1.48035 +*/
 1.48036 +#define get2byte(x)   ((x)[0]<<8 | (x)[1])
 1.48037 +#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
 1.48038 +#define get4byte sqlite3Get4byte
 1.48039 +#define put4byte sqlite3Put4byte
 1.48040 +
 1.48041 +/************** End of btreeInt.h ********************************************/
 1.48042 +/************** Continuing where we left off in btmutex.c ********************/
 1.48043 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.48044 +#if SQLITE_THREADSAFE
 1.48045 +
 1.48046 +/*
 1.48047 +** Obtain the BtShared mutex associated with B-Tree handle p. Also,
 1.48048 +** set BtShared.db to the database handle associated with p and the
 1.48049 +** p->locked boolean to true.
 1.48050 +*/
 1.48051 +static void lockBtreeMutex(Btree *p){
 1.48052 +  assert( p->locked==0 );
 1.48053 +  assert( sqlite3_mutex_notheld(p->pBt->mutex) );
 1.48054 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.48055 +
 1.48056 +  sqlite3_mutex_enter(p->pBt->mutex);
 1.48057 +  p->pBt->db = p->db;
 1.48058 +  p->locked = 1;
 1.48059 +}
 1.48060 +
 1.48061 +/*
 1.48062 +** Release the BtShared mutex associated with B-Tree handle p and
 1.48063 +** clear the p->locked boolean.
 1.48064 +*/
 1.48065 +static void unlockBtreeMutex(Btree *p){
 1.48066 +  BtShared *pBt = p->pBt;
 1.48067 +  assert( p->locked==1 );
 1.48068 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.48069 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.48070 +  assert( p->db==pBt->db );
 1.48071 +
 1.48072 +  sqlite3_mutex_leave(pBt->mutex);
 1.48073 +  p->locked = 0;
 1.48074 +}
 1.48075 +
 1.48076 +/*
 1.48077 +** Enter a mutex on the given BTree object.
 1.48078 +**
 1.48079 +** If the object is not sharable, then no mutex is ever required
 1.48080 +** and this routine is a no-op.  The underlying mutex is non-recursive.
 1.48081 +** But we keep a reference count in Btree.wantToLock so the behavior
 1.48082 +** of this interface is recursive.
 1.48083 +**
 1.48084 +** To avoid deadlocks, multiple Btrees are locked in the same order
 1.48085 +** by all database connections.  The p->pNext is a list of other
 1.48086 +** Btrees belonging to the same database connection as the p Btree
 1.48087 +** which need to be locked after p.  If we cannot get a lock on
 1.48088 +** p, then first unlock all of the others on p->pNext, then wait
 1.48089 +** for the lock to become available on p, then relock all of the
 1.48090 +** subsequent Btrees that desire a lock.
 1.48091 +*/
 1.48092 +SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
 1.48093 +  Btree *pLater;
 1.48094 +
 1.48095 +  /* Some basic sanity checking on the Btree.  The list of Btrees
 1.48096 +  ** connected by pNext and pPrev should be in sorted order by
 1.48097 +  ** Btree.pBt value. All elements of the list should belong to
 1.48098 +  ** the same connection. Only shared Btrees are on the list. */
 1.48099 +  assert( p->pNext==0 || p->pNext->pBt>p->pBt );
 1.48100 +  assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
 1.48101 +  assert( p->pNext==0 || p->pNext->db==p->db );
 1.48102 +  assert( p->pPrev==0 || p->pPrev->db==p->db );
 1.48103 +  assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
 1.48104 +
 1.48105 +  /* Check for locking consistency */
 1.48106 +  assert( !p->locked || p->wantToLock>0 );
 1.48107 +  assert( p->sharable || p->wantToLock==0 );
 1.48108 +
 1.48109 +  /* We should already hold a lock on the database connection */
 1.48110 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.48111 +
 1.48112 +  /* Unless the database is sharable and unlocked, then BtShared.db
 1.48113 +  ** should already be set correctly. */
 1.48114 +  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
 1.48115 +
 1.48116 +  if( !p->sharable ) return;
 1.48117 +  p->wantToLock++;
 1.48118 +  if( p->locked ) return;
 1.48119 +
 1.48120 +  /* In most cases, we should be able to acquire the lock we
 1.48121 +  ** want without having to go throught the ascending lock
 1.48122 +  ** procedure that follows.  Just be sure not to block.
 1.48123 +  */
 1.48124 +  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
 1.48125 +    p->pBt->db = p->db;
 1.48126 +    p->locked = 1;
 1.48127 +    return;
 1.48128 +  }
 1.48129 +
 1.48130 +  /* To avoid deadlock, first release all locks with a larger
 1.48131 +  ** BtShared address.  Then acquire our lock.  Then reacquire
 1.48132 +  ** the other BtShared locks that we used to hold in ascending
 1.48133 +  ** order.
 1.48134 +  */
 1.48135 +  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
 1.48136 +    assert( pLater->sharable );
 1.48137 +    assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
 1.48138 +    assert( !pLater->locked || pLater->wantToLock>0 );
 1.48139 +    if( pLater->locked ){
 1.48140 +      unlockBtreeMutex(pLater);
 1.48141 +    }
 1.48142 +  }
 1.48143 +  lockBtreeMutex(p);
 1.48144 +  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
 1.48145 +    if( pLater->wantToLock ){
 1.48146 +      lockBtreeMutex(pLater);
 1.48147 +    }
 1.48148 +  }
 1.48149 +}
 1.48150 +
 1.48151 +/*
 1.48152 +** Exit the recursive mutex on a Btree.
 1.48153 +*/
 1.48154 +SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
 1.48155 +  if( p->sharable ){
 1.48156 +    assert( p->wantToLock>0 );
 1.48157 +    p->wantToLock--;
 1.48158 +    if( p->wantToLock==0 ){
 1.48159 +      unlockBtreeMutex(p);
 1.48160 +    }
 1.48161 +  }
 1.48162 +}
 1.48163 +
 1.48164 +#ifndef NDEBUG
 1.48165 +/*
 1.48166 +** Return true if the BtShared mutex is held on the btree, or if the
 1.48167 +** B-Tree is not marked as sharable.
 1.48168 +**
 1.48169 +** This routine is used only from within assert() statements.
 1.48170 +*/
 1.48171 +SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
 1.48172 +  assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
 1.48173 +  assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
 1.48174 +  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
 1.48175 +  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
 1.48176 +
 1.48177 +  return (p->sharable==0 || p->locked);
 1.48178 +}
 1.48179 +#endif
 1.48180 +
 1.48181 +
 1.48182 +#ifndef SQLITE_OMIT_INCRBLOB
 1.48183 +/*
 1.48184 +** Enter and leave a mutex on a Btree given a cursor owned by that
 1.48185 +** Btree.  These entry points are used by incremental I/O and can be
 1.48186 +** omitted if that module is not used.
 1.48187 +*/
 1.48188 +SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
 1.48189 +  sqlite3BtreeEnter(pCur->pBtree);
 1.48190 +}
 1.48191 +SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
 1.48192 +  sqlite3BtreeLeave(pCur->pBtree);
 1.48193 +}
 1.48194 +#endif /* SQLITE_OMIT_INCRBLOB */
 1.48195 +
 1.48196 +
 1.48197 +/*
 1.48198 +** Enter the mutex on every Btree associated with a database
 1.48199 +** connection.  This is needed (for example) prior to parsing
 1.48200 +** a statement since we will be comparing table and column names
 1.48201 +** against all schemas and we do not want those schemas being
 1.48202 +** reset out from under us.
 1.48203 +**
 1.48204 +** There is a corresponding leave-all procedures.
 1.48205 +**
 1.48206 +** Enter the mutexes in accending order by BtShared pointer address
 1.48207 +** to avoid the possibility of deadlock when two threads with
 1.48208 +** two or more btrees in common both try to lock all their btrees
 1.48209 +** at the same instant.
 1.48210 +*/
 1.48211 +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 1.48212 +  int i;
 1.48213 +  Btree *p;
 1.48214 +  assert( sqlite3_mutex_held(db->mutex) );
 1.48215 +  for(i=0; i<db->nDb; i++){
 1.48216 +    p = db->aDb[i].pBt;
 1.48217 +    if( p ) sqlite3BtreeEnter(p);
 1.48218 +  }
 1.48219 +}
 1.48220 +SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
 1.48221 +  int i;
 1.48222 +  Btree *p;
 1.48223 +  assert( sqlite3_mutex_held(db->mutex) );
 1.48224 +  for(i=0; i<db->nDb; i++){
 1.48225 +    p = db->aDb[i].pBt;
 1.48226 +    if( p ) sqlite3BtreeLeave(p);
 1.48227 +  }
 1.48228 +}
 1.48229 +
 1.48230 +/*
 1.48231 +** Return true if a particular Btree requires a lock.  Return FALSE if
 1.48232 +** no lock is ever required since it is not sharable.
 1.48233 +*/
 1.48234 +SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
 1.48235 +  return p->sharable;
 1.48236 +}
 1.48237 +
 1.48238 +#ifndef NDEBUG
 1.48239 +/*
 1.48240 +** Return true if the current thread holds the database connection
 1.48241 +** mutex and all required BtShared mutexes.
 1.48242 +**
 1.48243 +** This routine is used inside assert() statements only.
 1.48244 +*/
 1.48245 +SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
 1.48246 +  int i;
 1.48247 +  if( !sqlite3_mutex_held(db->mutex) ){
 1.48248 +    return 0;
 1.48249 +  }
 1.48250 +  for(i=0; i<db->nDb; i++){
 1.48251 +    Btree *p;
 1.48252 +    p = db->aDb[i].pBt;
 1.48253 +    if( p && p->sharable &&
 1.48254 +         (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
 1.48255 +      return 0;
 1.48256 +    }
 1.48257 +  }
 1.48258 +  return 1;
 1.48259 +}
 1.48260 +#endif /* NDEBUG */
 1.48261 +
 1.48262 +#ifndef NDEBUG
 1.48263 +/*
 1.48264 +** Return true if the correct mutexes are held for accessing the
 1.48265 +** db->aDb[iDb].pSchema structure.  The mutexes required for schema
 1.48266 +** access are:
 1.48267 +**
 1.48268 +**   (1) The mutex on db
 1.48269 +**   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
 1.48270 +**
 1.48271 +** If pSchema is not NULL, then iDb is computed from pSchema and
 1.48272 +** db using sqlite3SchemaToIndex().
 1.48273 +*/
 1.48274 +SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
 1.48275 +  Btree *p;
 1.48276 +  assert( db!=0 );
 1.48277 +  if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
 1.48278 +  assert( iDb>=0 && iDb<db->nDb );
 1.48279 +  if( !sqlite3_mutex_held(db->mutex) ) return 0;
 1.48280 +  if( iDb==1 ) return 1;
 1.48281 +  p = db->aDb[iDb].pBt;
 1.48282 +  assert( p!=0 );
 1.48283 +  return p->sharable==0 || p->locked==1;
 1.48284 +}
 1.48285 +#endif /* NDEBUG */
 1.48286 +
 1.48287 +#else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
 1.48288 +/*
 1.48289 +** The following are special cases for mutex enter routines for use
 1.48290 +** in single threaded applications that use shared cache.  Except for
 1.48291 +** these two routines, all mutex operations are no-ops in that case and
 1.48292 +** are null #defines in btree.h.
 1.48293 +**
 1.48294 +** If shared cache is disabled, then all btree mutex routines, including
 1.48295 +** the ones below, are no-ops and are null #defines in btree.h.
 1.48296 +*/
 1.48297 +
 1.48298 +SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
 1.48299 +  p->pBt->db = p->db;
 1.48300 +}
 1.48301 +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 1.48302 +  int i;
 1.48303 +  for(i=0; i<db->nDb; i++){
 1.48304 +    Btree *p = db->aDb[i].pBt;
 1.48305 +    if( p ){
 1.48306 +      p->pBt->db = p->db;
 1.48307 +    }
 1.48308 +  }
 1.48309 +}
 1.48310 +#endif /* if SQLITE_THREADSAFE */
 1.48311 +#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
 1.48312 +
 1.48313 +/************** End of btmutex.c *********************************************/
 1.48314 +/************** Begin file btree.c *******************************************/
 1.48315 +/*
 1.48316 +** 2004 April 6
 1.48317 +**
 1.48318 +** The author disclaims copyright to this source code.  In place of
 1.48319 +** a legal notice, here is a blessing:
 1.48320 +**
 1.48321 +**    May you do good and not evil.
 1.48322 +**    May you find forgiveness for yourself and forgive others.
 1.48323 +**    May you share freely, never taking more than you give.
 1.48324 +**
 1.48325 +*************************************************************************
 1.48326 +** This file implements a external (disk-based) database using BTrees.
 1.48327 +** See the header comment on "btreeInt.h" for additional information.
 1.48328 +** Including a description of file format and an overview of operation.
 1.48329 +*/
 1.48330 +
 1.48331 +/*
 1.48332 +** The header string that appears at the beginning of every
 1.48333 +** SQLite database.
 1.48334 +*/
 1.48335 +static const char zMagicHeader[] = SQLITE_FILE_HEADER;
 1.48336 +
 1.48337 +/*
 1.48338 +** Set this global variable to 1 to enable tracing using the TRACE
 1.48339 +** macro.
 1.48340 +*/
 1.48341 +#if 0
 1.48342 +int sqlite3BtreeTrace=1;  /* True to enable tracing */
 1.48343 +# define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
 1.48344 +#else
 1.48345 +# define TRACE(X)
 1.48346 +#endif
 1.48347 +
 1.48348 +/*
 1.48349 +** Extract a 2-byte big-endian integer from an array of unsigned bytes.
 1.48350 +** But if the value is zero, make it 65536.
 1.48351 +**
 1.48352 +** This routine is used to extract the "offset to cell content area" value
 1.48353 +** from the header of a btree page.  If the page size is 65536 and the page
 1.48354 +** is empty, the offset should be 65536, but the 2-byte value stores zero.
 1.48355 +** This routine makes the necessary adjustment to 65536.
 1.48356 +*/
 1.48357 +#define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
 1.48358 +
 1.48359 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.48360 +/*
 1.48361 +** A list of BtShared objects that are eligible for participation
 1.48362 +** in shared cache.  This variable has file scope during normal builds,
 1.48363 +** but the test harness needs to access it so we make it global for 
 1.48364 +** test builds.
 1.48365 +**
 1.48366 +** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
 1.48367 +*/
 1.48368 +#ifdef SQLITE_TEST
 1.48369 +SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
 1.48370 +#else
 1.48371 +static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
 1.48372 +#endif
 1.48373 +#endif /* SQLITE_OMIT_SHARED_CACHE */
 1.48374 +
 1.48375 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.48376 +/*
 1.48377 +** Enable or disable the shared pager and schema features.
 1.48378 +**
 1.48379 +** This routine has no effect on existing database connections.
 1.48380 +** The shared cache setting effects only future calls to
 1.48381 +** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
 1.48382 +*/
 1.48383 +SQLITE_API int sqlite3_enable_shared_cache(int enable){
 1.48384 +  sqlite3GlobalConfig.sharedCacheEnabled = enable;
 1.48385 +  return SQLITE_OK;
 1.48386 +}
 1.48387 +#endif
 1.48388 +
 1.48389 +
 1.48390 +
 1.48391 +#ifdef SQLITE_OMIT_SHARED_CACHE
 1.48392 +  /*
 1.48393 +  ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
 1.48394 +  ** and clearAllSharedCacheTableLocks()
 1.48395 +  ** manipulate entries in the BtShared.pLock linked list used to store
 1.48396 +  ** shared-cache table level locks. If the library is compiled with the
 1.48397 +  ** shared-cache feature disabled, then there is only ever one user
 1.48398 +  ** of each BtShared structure and so this locking is not necessary. 
 1.48399 +  ** So define the lock related functions as no-ops.
 1.48400 +  */
 1.48401 +  #define querySharedCacheTableLock(a,b,c) SQLITE_OK
 1.48402 +  #define setSharedCacheTableLock(a,b,c) SQLITE_OK
 1.48403 +  #define clearAllSharedCacheTableLocks(a)
 1.48404 +  #define downgradeAllSharedCacheTableLocks(a)
 1.48405 +  #define hasSharedCacheTableLock(a,b,c,d) 1
 1.48406 +  #define hasReadConflicts(a, b) 0
 1.48407 +#endif
 1.48408 +
 1.48409 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.48410 +
 1.48411 +#ifdef SQLITE_DEBUG
 1.48412 +/*
 1.48413 +**** This function is only used as part of an assert() statement. ***
 1.48414 +**
 1.48415 +** Check to see if pBtree holds the required locks to read or write to the 
 1.48416 +** table with root page iRoot.   Return 1 if it does and 0 if not.
 1.48417 +**
 1.48418 +** For example, when writing to a table with root-page iRoot via 
 1.48419 +** Btree connection pBtree:
 1.48420 +**
 1.48421 +**    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
 1.48422 +**
 1.48423 +** When writing to an index that resides in a sharable database, the 
 1.48424 +** caller should have first obtained a lock specifying the root page of
 1.48425 +** the corresponding table. This makes things a bit more complicated,
 1.48426 +** as this module treats each table as a separate structure. To determine
 1.48427 +** the table corresponding to the index being written, this
 1.48428 +** function has to search through the database schema.
 1.48429 +**
 1.48430 +** Instead of a lock on the table/index rooted at page iRoot, the caller may
 1.48431 +** hold a write-lock on the schema table (root page 1). This is also
 1.48432 +** acceptable.
 1.48433 +*/
 1.48434 +static int hasSharedCacheTableLock(
 1.48435 +  Btree *pBtree,         /* Handle that must hold lock */
 1.48436 +  Pgno iRoot,            /* Root page of b-tree */
 1.48437 +  int isIndex,           /* True if iRoot is the root of an index b-tree */
 1.48438 +  int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
 1.48439 +){
 1.48440 +  Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
 1.48441 +  Pgno iTab = 0;
 1.48442 +  BtLock *pLock;
 1.48443 +
 1.48444 +  /* If this database is not shareable, or if the client is reading
 1.48445 +  ** and has the read-uncommitted flag set, then no lock is required. 
 1.48446 +  ** Return true immediately.
 1.48447 +  */
 1.48448 +  if( (pBtree->sharable==0)
 1.48449 +   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
 1.48450 +  ){
 1.48451 +    return 1;
 1.48452 +  }
 1.48453 +
 1.48454 +  /* If the client is reading  or writing an index and the schema is
 1.48455 +  ** not loaded, then it is too difficult to actually check to see if
 1.48456 +  ** the correct locks are held.  So do not bother - just return true.
 1.48457 +  ** This case does not come up very often anyhow.
 1.48458 +  */
 1.48459 +  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
 1.48460 +    return 1;
 1.48461 +  }
 1.48462 +
 1.48463 +  /* Figure out the root-page that the lock should be held on. For table
 1.48464 +  ** b-trees, this is just the root page of the b-tree being read or
 1.48465 +  ** written. For index b-trees, it is the root page of the associated
 1.48466 +  ** table.  */
 1.48467 +  if( isIndex ){
 1.48468 +    HashElem *p;
 1.48469 +    for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
 1.48470 +      Index *pIdx = (Index *)sqliteHashData(p);
 1.48471 +      if( pIdx->tnum==(int)iRoot ){
 1.48472 +        iTab = pIdx->pTable->tnum;
 1.48473 +      }
 1.48474 +    }
 1.48475 +  }else{
 1.48476 +    iTab = iRoot;
 1.48477 +  }
 1.48478 +
 1.48479 +  /* Search for the required lock. Either a write-lock on root-page iTab, a 
 1.48480 +  ** write-lock on the schema table, or (if the client is reading) a
 1.48481 +  ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
 1.48482 +  for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
 1.48483 +    if( pLock->pBtree==pBtree 
 1.48484 +     && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
 1.48485 +     && pLock->eLock>=eLockType 
 1.48486 +    ){
 1.48487 +      return 1;
 1.48488 +    }
 1.48489 +  }
 1.48490 +
 1.48491 +  /* Failed to find the required lock. */
 1.48492 +  return 0;
 1.48493 +}
 1.48494 +#endif /* SQLITE_DEBUG */
 1.48495 +
 1.48496 +#ifdef SQLITE_DEBUG
 1.48497 +/*
 1.48498 +**** This function may be used as part of assert() statements only. ****
 1.48499 +**
 1.48500 +** Return true if it would be illegal for pBtree to write into the
 1.48501 +** table or index rooted at iRoot because other shared connections are
 1.48502 +** simultaneously reading that same table or index.
 1.48503 +**
 1.48504 +** It is illegal for pBtree to write if some other Btree object that
 1.48505 +** shares the same BtShared object is currently reading or writing
 1.48506 +** the iRoot table.  Except, if the other Btree object has the
 1.48507 +** read-uncommitted flag set, then it is OK for the other object to
 1.48508 +** have a read cursor.
 1.48509 +**
 1.48510 +** For example, before writing to any part of the table or index
 1.48511 +** rooted at page iRoot, one should call:
 1.48512 +**
 1.48513 +**    assert( !hasReadConflicts(pBtree, iRoot) );
 1.48514 +*/
 1.48515 +static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
 1.48516 +  BtCursor *p;
 1.48517 +  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 1.48518 +    if( p->pgnoRoot==iRoot 
 1.48519 +     && p->pBtree!=pBtree
 1.48520 +     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
 1.48521 +    ){
 1.48522 +      return 1;
 1.48523 +    }
 1.48524 +  }
 1.48525 +  return 0;
 1.48526 +}
 1.48527 +#endif    /* #ifdef SQLITE_DEBUG */
 1.48528 +
 1.48529 +/*
 1.48530 +** Query to see if Btree handle p may obtain a lock of type eLock 
 1.48531 +** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
 1.48532 +** SQLITE_OK if the lock may be obtained (by calling
 1.48533 +** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
 1.48534 +*/
 1.48535 +static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
 1.48536 +  BtShared *pBt = p->pBt;
 1.48537 +  BtLock *pIter;
 1.48538 +
 1.48539 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.48540 +  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
 1.48541 +  assert( p->db!=0 );
 1.48542 +  assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
 1.48543 +  
 1.48544 +  /* If requesting a write-lock, then the Btree must have an open write
 1.48545 +  ** transaction on this file. And, obviously, for this to be so there 
 1.48546 +  ** must be an open write transaction on the file itself.
 1.48547 +  */
 1.48548 +  assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
 1.48549 +  assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
 1.48550 +  
 1.48551 +  /* This routine is a no-op if the shared-cache is not enabled */
 1.48552 +  if( !p->sharable ){
 1.48553 +    return SQLITE_OK;
 1.48554 +  }
 1.48555 +
 1.48556 +  /* If some other connection is holding an exclusive lock, the
 1.48557 +  ** requested lock may not be obtained.
 1.48558 +  */
 1.48559 +  if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
 1.48560 +    sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
 1.48561 +    return SQLITE_LOCKED_SHAREDCACHE;
 1.48562 +  }
 1.48563 +
 1.48564 +  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 1.48565 +    /* The condition (pIter->eLock!=eLock) in the following if(...) 
 1.48566 +    ** statement is a simplification of:
 1.48567 +    **
 1.48568 +    **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
 1.48569 +    **
 1.48570 +    ** since we know that if eLock==WRITE_LOCK, then no other connection
 1.48571 +    ** may hold a WRITE_LOCK on any table in this file (since there can
 1.48572 +    ** only be a single writer).
 1.48573 +    */
 1.48574 +    assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
 1.48575 +    assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
 1.48576 +    if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
 1.48577 +      sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
 1.48578 +      if( eLock==WRITE_LOCK ){
 1.48579 +        assert( p==pBt->pWriter );
 1.48580 +        pBt->btsFlags |= BTS_PENDING;
 1.48581 +      }
 1.48582 +      return SQLITE_LOCKED_SHAREDCACHE;
 1.48583 +    }
 1.48584 +  }
 1.48585 +  return SQLITE_OK;
 1.48586 +}
 1.48587 +#endif /* !SQLITE_OMIT_SHARED_CACHE */
 1.48588 +
 1.48589 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.48590 +/*
 1.48591 +** Add a lock on the table with root-page iTable to the shared-btree used
 1.48592 +** by Btree handle p. Parameter eLock must be either READ_LOCK or 
 1.48593 +** WRITE_LOCK.
 1.48594 +**
 1.48595 +** This function assumes the following:
 1.48596 +**
 1.48597 +**   (a) The specified Btree object p is connected to a sharable
 1.48598 +**       database (one with the BtShared.sharable flag set), and
 1.48599 +**
 1.48600 +**   (b) No other Btree objects hold a lock that conflicts
 1.48601 +**       with the requested lock (i.e. querySharedCacheTableLock() has
 1.48602 +**       already been called and returned SQLITE_OK).
 1.48603 +**
 1.48604 +** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 
 1.48605 +** is returned if a malloc attempt fails.
 1.48606 +*/
 1.48607 +static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
 1.48608 +  BtShared *pBt = p->pBt;
 1.48609 +  BtLock *pLock = 0;
 1.48610 +  BtLock *pIter;
 1.48611 +
 1.48612 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.48613 +  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
 1.48614 +  assert( p->db!=0 );
 1.48615 +
 1.48616 +  /* A connection with the read-uncommitted flag set will never try to
 1.48617 +  ** obtain a read-lock using this function. The only read-lock obtained
 1.48618 +  ** by a connection in read-uncommitted mode is on the sqlite_master 
 1.48619 +  ** table, and that lock is obtained in BtreeBeginTrans().  */
 1.48620 +  assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
 1.48621 +
 1.48622 +  /* This function should only be called on a sharable b-tree after it 
 1.48623 +  ** has been determined that no other b-tree holds a conflicting lock.  */
 1.48624 +  assert( p->sharable );
 1.48625 +  assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
 1.48626 +
 1.48627 +  /* First search the list for an existing lock on this table. */
 1.48628 +  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 1.48629 +    if( pIter->iTable==iTable && pIter->pBtree==p ){
 1.48630 +      pLock = pIter;
 1.48631 +      break;
 1.48632 +    }
 1.48633 +  }
 1.48634 +
 1.48635 +  /* If the above search did not find a BtLock struct associating Btree p
 1.48636 +  ** with table iTable, allocate one and link it into the list.
 1.48637 +  */
 1.48638 +  if( !pLock ){
 1.48639 +    pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
 1.48640 +    if( !pLock ){
 1.48641 +      return SQLITE_NOMEM;
 1.48642 +    }
 1.48643 +    pLock->iTable = iTable;
 1.48644 +    pLock->pBtree = p;
 1.48645 +    pLock->pNext = pBt->pLock;
 1.48646 +    pBt->pLock = pLock;
 1.48647 +  }
 1.48648 +
 1.48649 +  /* Set the BtLock.eLock variable to the maximum of the current lock
 1.48650 +  ** and the requested lock. This means if a write-lock was already held
 1.48651 +  ** and a read-lock requested, we don't incorrectly downgrade the lock.
 1.48652 +  */
 1.48653 +  assert( WRITE_LOCK>READ_LOCK );
 1.48654 +  if( eLock>pLock->eLock ){
 1.48655 +    pLock->eLock = eLock;
 1.48656 +  }
 1.48657 +
 1.48658 +  return SQLITE_OK;
 1.48659 +}
 1.48660 +#endif /* !SQLITE_OMIT_SHARED_CACHE */
 1.48661 +
 1.48662 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.48663 +/*
 1.48664 +** Release all the table locks (locks obtained via calls to
 1.48665 +** the setSharedCacheTableLock() procedure) held by Btree object p.
 1.48666 +**
 1.48667 +** This function assumes that Btree p has an open read or write 
 1.48668 +** transaction. If it does not, then the BTS_PENDING flag
 1.48669 +** may be incorrectly cleared.
 1.48670 +*/
 1.48671 +static void clearAllSharedCacheTableLocks(Btree *p){
 1.48672 +  BtShared *pBt = p->pBt;
 1.48673 +  BtLock **ppIter = &pBt->pLock;
 1.48674 +
 1.48675 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.48676 +  assert( p->sharable || 0==*ppIter );
 1.48677 +  assert( p->inTrans>0 );
 1.48678 +
 1.48679 +  while( *ppIter ){
 1.48680 +    BtLock *pLock = *ppIter;
 1.48681 +    assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
 1.48682 +    assert( pLock->pBtree->inTrans>=pLock->eLock );
 1.48683 +    if( pLock->pBtree==p ){
 1.48684 +      *ppIter = pLock->pNext;
 1.48685 +      assert( pLock->iTable!=1 || pLock==&p->lock );
 1.48686 +      if( pLock->iTable!=1 ){
 1.48687 +        sqlite3_free(pLock);
 1.48688 +      }
 1.48689 +    }else{
 1.48690 +      ppIter = &pLock->pNext;
 1.48691 +    }
 1.48692 +  }
 1.48693 +
 1.48694 +  assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
 1.48695 +  if( pBt->pWriter==p ){
 1.48696 +    pBt->pWriter = 0;
 1.48697 +    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 1.48698 +  }else if( pBt->nTransaction==2 ){
 1.48699 +    /* This function is called when Btree p is concluding its 
 1.48700 +    ** transaction. If there currently exists a writer, and p is not
 1.48701 +    ** that writer, then the number of locks held by connections other
 1.48702 +    ** than the writer must be about to drop to zero. In this case
 1.48703 +    ** set the BTS_PENDING flag to 0.
 1.48704 +    **
 1.48705 +    ** If there is not currently a writer, then BTS_PENDING must
 1.48706 +    ** be zero already. So this next line is harmless in that case.
 1.48707 +    */
 1.48708 +    pBt->btsFlags &= ~BTS_PENDING;
 1.48709 +  }
 1.48710 +}
 1.48711 +
 1.48712 +/*
 1.48713 +** This function changes all write-locks held by Btree p into read-locks.
 1.48714 +*/
 1.48715 +static void downgradeAllSharedCacheTableLocks(Btree *p){
 1.48716 +  BtShared *pBt = p->pBt;
 1.48717 +  if( pBt->pWriter==p ){
 1.48718 +    BtLock *pLock;
 1.48719 +    pBt->pWriter = 0;
 1.48720 +    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 1.48721 +    for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
 1.48722 +      assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
 1.48723 +      pLock->eLock = READ_LOCK;
 1.48724 +    }
 1.48725 +  }
 1.48726 +}
 1.48727 +
 1.48728 +#endif /* SQLITE_OMIT_SHARED_CACHE */
 1.48729 +
 1.48730 +static void releasePage(MemPage *pPage);  /* Forward reference */
 1.48731 +
 1.48732 +/*
 1.48733 +***** This routine is used inside of assert() only ****
 1.48734 +**
 1.48735 +** Verify that the cursor holds the mutex on its BtShared
 1.48736 +*/
 1.48737 +#ifdef SQLITE_DEBUG
 1.48738 +static int cursorHoldsMutex(BtCursor *p){
 1.48739 +  return sqlite3_mutex_held(p->pBt->mutex);
 1.48740 +}
 1.48741 +#endif
 1.48742 +
 1.48743 +
 1.48744 +#ifndef SQLITE_OMIT_INCRBLOB
 1.48745 +/*
 1.48746 +** Invalidate the overflow page-list cache for cursor pCur, if any.
 1.48747 +*/
 1.48748 +static void invalidateOverflowCache(BtCursor *pCur){
 1.48749 +  assert( cursorHoldsMutex(pCur) );
 1.48750 +  sqlite3_free(pCur->aOverflow);
 1.48751 +  pCur->aOverflow = 0;
 1.48752 +}
 1.48753 +
 1.48754 +/*
 1.48755 +** Invalidate the overflow page-list cache for all cursors opened
 1.48756 +** on the shared btree structure pBt.
 1.48757 +*/
 1.48758 +static void invalidateAllOverflowCache(BtShared *pBt){
 1.48759 +  BtCursor *p;
 1.48760 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.48761 +  for(p=pBt->pCursor; p; p=p->pNext){
 1.48762 +    invalidateOverflowCache(p);
 1.48763 +  }
 1.48764 +}
 1.48765 +
 1.48766 +/*
 1.48767 +** This function is called before modifying the contents of a table
 1.48768 +** to invalidate any incrblob cursors that are open on the
 1.48769 +** row or one of the rows being modified.
 1.48770 +**
 1.48771 +** If argument isClearTable is true, then the entire contents of the
 1.48772 +** table is about to be deleted. In this case invalidate all incrblob
 1.48773 +** cursors open on any row within the table with root-page pgnoRoot.
 1.48774 +**
 1.48775 +** Otherwise, if argument isClearTable is false, then the row with
 1.48776 +** rowid iRow is being replaced or deleted. In this case invalidate
 1.48777 +** only those incrblob cursors open on that specific row.
 1.48778 +*/
 1.48779 +static void invalidateIncrblobCursors(
 1.48780 +  Btree *pBtree,          /* The database file to check */
 1.48781 +  i64 iRow,               /* The rowid that might be changing */
 1.48782 +  int isClearTable        /* True if all rows are being deleted */
 1.48783 +){
 1.48784 +  BtCursor *p;
 1.48785 +  BtShared *pBt = pBtree->pBt;
 1.48786 +  assert( sqlite3BtreeHoldsMutex(pBtree) );
 1.48787 +  for(p=pBt->pCursor; p; p=p->pNext){
 1.48788 +    if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
 1.48789 +      p->eState = CURSOR_INVALID;
 1.48790 +    }
 1.48791 +  }
 1.48792 +}
 1.48793 +
 1.48794 +#else
 1.48795 +  /* Stub functions when INCRBLOB is omitted */
 1.48796 +  #define invalidateOverflowCache(x)
 1.48797 +  #define invalidateAllOverflowCache(x)
 1.48798 +  #define invalidateIncrblobCursors(x,y,z)
 1.48799 +#endif /* SQLITE_OMIT_INCRBLOB */
 1.48800 +
 1.48801 +/*
 1.48802 +** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
 1.48803 +** when a page that previously contained data becomes a free-list leaf 
 1.48804 +** page.
 1.48805 +**
 1.48806 +** The BtShared.pHasContent bitvec exists to work around an obscure
 1.48807 +** bug caused by the interaction of two useful IO optimizations surrounding
 1.48808 +** free-list leaf pages:
 1.48809 +**
 1.48810 +**   1) When all data is deleted from a page and the page becomes
 1.48811 +**      a free-list leaf page, the page is not written to the database
 1.48812 +**      (as free-list leaf pages contain no meaningful data). Sometimes
 1.48813 +**      such a page is not even journalled (as it will not be modified,
 1.48814 +**      why bother journalling it?).
 1.48815 +**
 1.48816 +**   2) When a free-list leaf page is reused, its content is not read
 1.48817 +**      from the database or written to the journal file (why should it
 1.48818 +**      be, if it is not at all meaningful?).
 1.48819 +**
 1.48820 +** By themselves, these optimizations work fine and provide a handy
 1.48821 +** performance boost to bulk delete or insert operations. However, if
 1.48822 +** a page is moved to the free-list and then reused within the same
 1.48823 +** transaction, a problem comes up. If the page is not journalled when
 1.48824 +** it is moved to the free-list and it is also not journalled when it
 1.48825 +** is extracted from the free-list and reused, then the original data
 1.48826 +** may be lost. In the event of a rollback, it may not be possible
 1.48827 +** to restore the database to its original configuration.
 1.48828 +**
 1.48829 +** The solution is the BtShared.pHasContent bitvec. Whenever a page is 
 1.48830 +** moved to become a free-list leaf page, the corresponding bit is
 1.48831 +** set in the bitvec. Whenever a leaf page is extracted from the free-list,
 1.48832 +** optimization 2 above is omitted if the corresponding bit is already
 1.48833 +** set in BtShared.pHasContent. The contents of the bitvec are cleared
 1.48834 +** at the end of every transaction.
 1.48835 +*/
 1.48836 +static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
 1.48837 +  int rc = SQLITE_OK;
 1.48838 +  if( !pBt->pHasContent ){
 1.48839 +    assert( pgno<=pBt->nPage );
 1.48840 +    pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
 1.48841 +    if( !pBt->pHasContent ){
 1.48842 +      rc = SQLITE_NOMEM;
 1.48843 +    }
 1.48844 +  }
 1.48845 +  if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
 1.48846 +    rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
 1.48847 +  }
 1.48848 +  return rc;
 1.48849 +}
 1.48850 +
 1.48851 +/*
 1.48852 +** Query the BtShared.pHasContent vector.
 1.48853 +**
 1.48854 +** This function is called when a free-list leaf page is removed from the
 1.48855 +** free-list for reuse. It returns false if it is safe to retrieve the
 1.48856 +** page from the pager layer with the 'no-content' flag set. True otherwise.
 1.48857 +*/
 1.48858 +static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
 1.48859 +  Bitvec *p = pBt->pHasContent;
 1.48860 +  return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
 1.48861 +}
 1.48862 +
 1.48863 +/*
 1.48864 +** Clear (destroy) the BtShared.pHasContent bitvec. This should be
 1.48865 +** invoked at the conclusion of each write-transaction.
 1.48866 +*/
 1.48867 +static void btreeClearHasContent(BtShared *pBt){
 1.48868 +  sqlite3BitvecDestroy(pBt->pHasContent);
 1.48869 +  pBt->pHasContent = 0;
 1.48870 +}
 1.48871 +
 1.48872 +/*
 1.48873 +** Save the current cursor position in the variables BtCursor.nKey 
 1.48874 +** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
 1.48875 +**
 1.48876 +** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
 1.48877 +** prior to calling this routine.  
 1.48878 +*/
 1.48879 +static int saveCursorPosition(BtCursor *pCur){
 1.48880 +  int rc;
 1.48881 +
 1.48882 +  assert( CURSOR_VALID==pCur->eState );
 1.48883 +  assert( 0==pCur->pKey );
 1.48884 +  assert( cursorHoldsMutex(pCur) );
 1.48885 +
 1.48886 +  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
 1.48887 +  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
 1.48888 +
 1.48889 +  /* If this is an intKey table, then the above call to BtreeKeySize()
 1.48890 +  ** stores the integer key in pCur->nKey. In this case this value is
 1.48891 +  ** all that is required. Otherwise, if pCur is not open on an intKey
 1.48892 +  ** table, then malloc space for and store the pCur->nKey bytes of key 
 1.48893 +  ** data.
 1.48894 +  */
 1.48895 +  if( 0==pCur->apPage[0]->intKey ){
 1.48896 +    void *pKey = sqlite3Malloc( (int)pCur->nKey );
 1.48897 +    if( pKey ){
 1.48898 +      rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
 1.48899 +      if( rc==SQLITE_OK ){
 1.48900 +        pCur->pKey = pKey;
 1.48901 +      }else{
 1.48902 +        sqlite3_free(pKey);
 1.48903 +      }
 1.48904 +    }else{
 1.48905 +      rc = SQLITE_NOMEM;
 1.48906 +    }
 1.48907 +  }
 1.48908 +  assert( !pCur->apPage[0]->intKey || !pCur->pKey );
 1.48909 +
 1.48910 +  if( rc==SQLITE_OK ){
 1.48911 +    int i;
 1.48912 +    for(i=0; i<=pCur->iPage; i++){
 1.48913 +      releasePage(pCur->apPage[i]);
 1.48914 +      pCur->apPage[i] = 0;
 1.48915 +    }
 1.48916 +    pCur->iPage = -1;
 1.48917 +    pCur->eState = CURSOR_REQUIRESEEK;
 1.48918 +  }
 1.48919 +
 1.48920 +  invalidateOverflowCache(pCur);
 1.48921 +  return rc;
 1.48922 +}
 1.48923 +
 1.48924 +/*
 1.48925 +** Save the positions of all cursors (except pExcept) that are open on
 1.48926 +** the table  with root-page iRoot. Usually, this is called just before cursor
 1.48927 +** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
 1.48928 +*/
 1.48929 +static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
 1.48930 +  BtCursor *p;
 1.48931 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.48932 +  assert( pExcept==0 || pExcept->pBt==pBt );
 1.48933 +  for(p=pBt->pCursor; p; p=p->pNext){
 1.48934 +    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && 
 1.48935 +        p->eState==CURSOR_VALID ){
 1.48936 +      int rc = saveCursorPosition(p);
 1.48937 +      if( SQLITE_OK!=rc ){
 1.48938 +        return rc;
 1.48939 +      }
 1.48940 +    }
 1.48941 +  }
 1.48942 +  return SQLITE_OK;
 1.48943 +}
 1.48944 +
 1.48945 +/*
 1.48946 +** Clear the current cursor position.
 1.48947 +*/
 1.48948 +SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
 1.48949 +  assert( cursorHoldsMutex(pCur) );
 1.48950 +  sqlite3_free(pCur->pKey);
 1.48951 +  pCur->pKey = 0;
 1.48952 +  pCur->eState = CURSOR_INVALID;
 1.48953 +}
 1.48954 +
 1.48955 +/*
 1.48956 +** In this version of BtreeMoveto, pKey is a packed index record
 1.48957 +** such as is generated by the OP_MakeRecord opcode.  Unpack the
 1.48958 +** record and then call BtreeMovetoUnpacked() to do the work.
 1.48959 +*/
 1.48960 +static int btreeMoveto(
 1.48961 +  BtCursor *pCur,     /* Cursor open on the btree to be searched */
 1.48962 +  const void *pKey,   /* Packed key if the btree is an index */
 1.48963 +  i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
 1.48964 +  int bias,           /* Bias search to the high end */
 1.48965 +  int *pRes           /* Write search results here */
 1.48966 +){
 1.48967 +  int rc;                    /* Status code */
 1.48968 +  UnpackedRecord *pIdxKey;   /* Unpacked index key */
 1.48969 +  char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
 1.48970 +  char *pFree = 0;
 1.48971 +
 1.48972 +  if( pKey ){
 1.48973 +    assert( nKey==(i64)(int)nKey );
 1.48974 +    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 1.48975 +        pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
 1.48976 +    );
 1.48977 +    if( pIdxKey==0 ) return SQLITE_NOMEM;
 1.48978 +    sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
 1.48979 +  }else{
 1.48980 +    pIdxKey = 0;
 1.48981 +  }
 1.48982 +  rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
 1.48983 +  if( pFree ){
 1.48984 +    sqlite3DbFree(pCur->pKeyInfo->db, pFree);
 1.48985 +  }
 1.48986 +  return rc;
 1.48987 +}
 1.48988 +
 1.48989 +/*
 1.48990 +** Restore the cursor to the position it was in (or as close to as possible)
 1.48991 +** when saveCursorPosition() was called. Note that this call deletes the 
 1.48992 +** saved position info stored by saveCursorPosition(), so there can be
 1.48993 +** at most one effective restoreCursorPosition() call after each 
 1.48994 +** saveCursorPosition().
 1.48995 +*/
 1.48996 +static int btreeRestoreCursorPosition(BtCursor *pCur){
 1.48997 +  int rc;
 1.48998 +  assert( cursorHoldsMutex(pCur) );
 1.48999 +  assert( pCur->eState>=CURSOR_REQUIRESEEK );
 1.49000 +  if( pCur->eState==CURSOR_FAULT ){
 1.49001 +    return pCur->skipNext;
 1.49002 +  }
 1.49003 +  pCur->eState = CURSOR_INVALID;
 1.49004 +  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
 1.49005 +  if( rc==SQLITE_OK ){
 1.49006 +    sqlite3_free(pCur->pKey);
 1.49007 +    pCur->pKey = 0;
 1.49008 +    assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
 1.49009 +  }
 1.49010 +  return rc;
 1.49011 +}
 1.49012 +
 1.49013 +#define restoreCursorPosition(p) \
 1.49014 +  (p->eState>=CURSOR_REQUIRESEEK ? \
 1.49015 +         btreeRestoreCursorPosition(p) : \
 1.49016 +         SQLITE_OK)
 1.49017 +
 1.49018 +/*
 1.49019 +** Determine whether or not a cursor has moved from the position it
 1.49020 +** was last placed at.  Cursors can move when the row they are pointing
 1.49021 +** at is deleted out from under them.
 1.49022 +**
 1.49023 +** This routine returns an error code if something goes wrong.  The
 1.49024 +** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
 1.49025 +*/
 1.49026 +SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
 1.49027 +  int rc;
 1.49028 +
 1.49029 +  rc = restoreCursorPosition(pCur);
 1.49030 +  if( rc ){
 1.49031 +    *pHasMoved = 1;
 1.49032 +    return rc;
 1.49033 +  }
 1.49034 +  if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
 1.49035 +    *pHasMoved = 1;
 1.49036 +  }else{
 1.49037 +    *pHasMoved = 0;
 1.49038 +  }
 1.49039 +  return SQLITE_OK;
 1.49040 +}
 1.49041 +
 1.49042 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.49043 +/*
 1.49044 +** Given a page number of a regular database page, return the page
 1.49045 +** number for the pointer-map page that contains the entry for the
 1.49046 +** input page number.
 1.49047 +**
 1.49048 +** Return 0 (not a valid page) for pgno==1 since there is
 1.49049 +** no pointer map associated with page 1.  The integrity_check logic
 1.49050 +** requires that ptrmapPageno(*,1)!=1.
 1.49051 +*/
 1.49052 +static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
 1.49053 +  int nPagesPerMapPage;
 1.49054 +  Pgno iPtrMap, ret;
 1.49055 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.49056 +  if( pgno<2 ) return 0;
 1.49057 +  nPagesPerMapPage = (pBt->usableSize/5)+1;
 1.49058 +  iPtrMap = (pgno-2)/nPagesPerMapPage;
 1.49059 +  ret = (iPtrMap*nPagesPerMapPage) + 2; 
 1.49060 +  if( ret==PENDING_BYTE_PAGE(pBt) ){
 1.49061 +    ret++;
 1.49062 +  }
 1.49063 +  return ret;
 1.49064 +}
 1.49065 +
 1.49066 +/*
 1.49067 +** Write an entry into the pointer map.
 1.49068 +**
 1.49069 +** This routine updates the pointer map entry for page number 'key'
 1.49070 +** so that it maps to type 'eType' and parent page number 'pgno'.
 1.49071 +**
 1.49072 +** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
 1.49073 +** a no-op.  If an error occurs, the appropriate error code is written
 1.49074 +** into *pRC.
 1.49075 +*/
 1.49076 +static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
 1.49077 +  DbPage *pDbPage;  /* The pointer map page */
 1.49078 +  u8 *pPtrmap;      /* The pointer map data */
 1.49079 +  Pgno iPtrmap;     /* The pointer map page number */
 1.49080 +  int offset;       /* Offset in pointer map page */
 1.49081 +  int rc;           /* Return code from subfunctions */
 1.49082 +
 1.49083 +  if( *pRC ) return;
 1.49084 +
 1.49085 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.49086 +  /* The master-journal page number must never be used as a pointer map page */
 1.49087 +  assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
 1.49088 +
 1.49089 +  assert( pBt->autoVacuum );
 1.49090 +  if( key==0 ){
 1.49091 +    *pRC = SQLITE_CORRUPT_BKPT;
 1.49092 +    return;
 1.49093 +  }
 1.49094 +  iPtrmap = PTRMAP_PAGENO(pBt, key);
 1.49095 +  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
 1.49096 +  if( rc!=SQLITE_OK ){
 1.49097 +    *pRC = rc;
 1.49098 +    return;
 1.49099 +  }
 1.49100 +  offset = PTRMAP_PTROFFSET(iPtrmap, key);
 1.49101 +  if( offset<0 ){
 1.49102 +    *pRC = SQLITE_CORRUPT_BKPT;
 1.49103 +    goto ptrmap_exit;
 1.49104 +  }
 1.49105 +  assert( offset <= (int)pBt->usableSize-5 );
 1.49106 +  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
 1.49107 +
 1.49108 +  if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
 1.49109 +    TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
 1.49110 +    *pRC= rc = sqlite3PagerWrite(pDbPage);
 1.49111 +    if( rc==SQLITE_OK ){
 1.49112 +      pPtrmap[offset] = eType;
 1.49113 +      put4byte(&pPtrmap[offset+1], parent);
 1.49114 +    }
 1.49115 +  }
 1.49116 +
 1.49117 +ptrmap_exit:
 1.49118 +  sqlite3PagerUnref(pDbPage);
 1.49119 +}
 1.49120 +
 1.49121 +/*
 1.49122 +** Read an entry from the pointer map.
 1.49123 +**
 1.49124 +** This routine retrieves the pointer map entry for page 'key', writing
 1.49125 +** the type and parent page number to *pEType and *pPgno respectively.
 1.49126 +** An error code is returned if something goes wrong, otherwise SQLITE_OK.
 1.49127 +*/
 1.49128 +static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
 1.49129 +  DbPage *pDbPage;   /* The pointer map page */
 1.49130 +  int iPtrmap;       /* Pointer map page index */
 1.49131 +  u8 *pPtrmap;       /* Pointer map page data */
 1.49132 +  int offset;        /* Offset of entry in pointer map */
 1.49133 +  int rc;
 1.49134 +
 1.49135 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.49136 +
 1.49137 +  iPtrmap = PTRMAP_PAGENO(pBt, key);
 1.49138 +  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
 1.49139 +  if( rc!=0 ){
 1.49140 +    return rc;
 1.49141 +  }
 1.49142 +  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
 1.49143 +
 1.49144 +  offset = PTRMAP_PTROFFSET(iPtrmap, key);
 1.49145 +  if( offset<0 ){
 1.49146 +    sqlite3PagerUnref(pDbPage);
 1.49147 +    return SQLITE_CORRUPT_BKPT;
 1.49148 +  }
 1.49149 +  assert( offset <= (int)pBt->usableSize-5 );
 1.49150 +  assert( pEType!=0 );
 1.49151 +  *pEType = pPtrmap[offset];
 1.49152 +  if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
 1.49153 +
 1.49154 +  sqlite3PagerUnref(pDbPage);
 1.49155 +  if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
 1.49156 +  return SQLITE_OK;
 1.49157 +}
 1.49158 +
 1.49159 +#else /* if defined SQLITE_OMIT_AUTOVACUUM */
 1.49160 +  #define ptrmapPut(w,x,y,z,rc)
 1.49161 +  #define ptrmapGet(w,x,y,z) SQLITE_OK
 1.49162 +  #define ptrmapPutOvflPtr(x, y, rc)
 1.49163 +#endif
 1.49164 +
 1.49165 +/*
 1.49166 +** Given a btree page and a cell index (0 means the first cell on
 1.49167 +** the page, 1 means the second cell, and so forth) return a pointer
 1.49168 +** to the cell content.
 1.49169 +**
 1.49170 +** This routine works only for pages that do not contain overflow cells.
 1.49171 +*/
 1.49172 +#define findCell(P,I) \
 1.49173 +  ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
 1.49174 +#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
 1.49175 +
 1.49176 +
 1.49177 +/*
 1.49178 +** This a more complex version of findCell() that works for
 1.49179 +** pages that do contain overflow cells.
 1.49180 +*/
 1.49181 +static u8 *findOverflowCell(MemPage *pPage, int iCell){
 1.49182 +  int i;
 1.49183 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49184 +  for(i=pPage->nOverflow-1; i>=0; i--){
 1.49185 +    int k;
 1.49186 +    k = pPage->aiOvfl[i];
 1.49187 +    if( k<=iCell ){
 1.49188 +      if( k==iCell ){
 1.49189 +        return pPage->apOvfl[i];
 1.49190 +      }
 1.49191 +      iCell--;
 1.49192 +    }
 1.49193 +  }
 1.49194 +  return findCell(pPage, iCell);
 1.49195 +}
 1.49196 +
 1.49197 +/*
 1.49198 +** Parse a cell content block and fill in the CellInfo structure.  There
 1.49199 +** are two versions of this function.  btreeParseCell() takes a 
 1.49200 +** cell index as the second argument and btreeParseCellPtr() 
 1.49201 +** takes a pointer to the body of the cell as its second argument.
 1.49202 +**
 1.49203 +** Within this file, the parseCell() macro can be called instead of
 1.49204 +** btreeParseCellPtr(). Using some compilers, this will be faster.
 1.49205 +*/
 1.49206 +static void btreeParseCellPtr(
 1.49207 +  MemPage *pPage,         /* Page containing the cell */
 1.49208 +  u8 *pCell,              /* Pointer to the cell text. */
 1.49209 +  CellInfo *pInfo         /* Fill in this structure */
 1.49210 +){
 1.49211 +  u16 n;                  /* Number bytes in cell content header */
 1.49212 +  u32 nPayload;           /* Number of bytes of cell payload */
 1.49213 +
 1.49214 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49215 +
 1.49216 +  pInfo->pCell = pCell;
 1.49217 +  assert( pPage->leaf==0 || pPage->leaf==1 );
 1.49218 +  n = pPage->childPtrSize;
 1.49219 +  assert( n==4-4*pPage->leaf );
 1.49220 +  if( pPage->intKey ){
 1.49221 +    if( pPage->hasData ){
 1.49222 +      n += getVarint32(&pCell[n], nPayload);
 1.49223 +    }else{
 1.49224 +      nPayload = 0;
 1.49225 +    }
 1.49226 +    n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
 1.49227 +    pInfo->nData = nPayload;
 1.49228 +  }else{
 1.49229 +    pInfo->nData = 0;
 1.49230 +    n += getVarint32(&pCell[n], nPayload);
 1.49231 +    pInfo->nKey = nPayload;
 1.49232 +  }
 1.49233 +  pInfo->nPayload = nPayload;
 1.49234 +  pInfo->nHeader = n;
 1.49235 +  testcase( nPayload==pPage->maxLocal );
 1.49236 +  testcase( nPayload==pPage->maxLocal+1 );
 1.49237 +  if( likely(nPayload<=pPage->maxLocal) ){
 1.49238 +    /* This is the (easy) common case where the entire payload fits
 1.49239 +    ** on the local page.  No overflow is required.
 1.49240 +    */
 1.49241 +    if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
 1.49242 +    pInfo->nLocal = (u16)nPayload;
 1.49243 +    pInfo->iOverflow = 0;
 1.49244 +  }else{
 1.49245 +    /* If the payload will not fit completely on the local page, we have
 1.49246 +    ** to decide how much to store locally and how much to spill onto
 1.49247 +    ** overflow pages.  The strategy is to minimize the amount of unused
 1.49248 +    ** space on overflow pages while keeping the amount of local storage
 1.49249 +    ** in between minLocal and maxLocal.
 1.49250 +    **
 1.49251 +    ** Warning:  changing the way overflow payload is distributed in any
 1.49252 +    ** way will result in an incompatible file format.
 1.49253 +    */
 1.49254 +    int minLocal;  /* Minimum amount of payload held locally */
 1.49255 +    int maxLocal;  /* Maximum amount of payload held locally */
 1.49256 +    int surplus;   /* Overflow payload available for local storage */
 1.49257 +
 1.49258 +    minLocal = pPage->minLocal;
 1.49259 +    maxLocal = pPage->maxLocal;
 1.49260 +    surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
 1.49261 +    testcase( surplus==maxLocal );
 1.49262 +    testcase( surplus==maxLocal+1 );
 1.49263 +    if( surplus <= maxLocal ){
 1.49264 +      pInfo->nLocal = (u16)surplus;
 1.49265 +    }else{
 1.49266 +      pInfo->nLocal = (u16)minLocal;
 1.49267 +    }
 1.49268 +    pInfo->iOverflow = (u16)(pInfo->nLocal + n);
 1.49269 +    pInfo->nSize = pInfo->iOverflow + 4;
 1.49270 +  }
 1.49271 +}
 1.49272 +#define parseCell(pPage, iCell, pInfo) \
 1.49273 +  btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
 1.49274 +static void btreeParseCell(
 1.49275 +  MemPage *pPage,         /* Page containing the cell */
 1.49276 +  int iCell,              /* The cell index.  First cell is 0 */
 1.49277 +  CellInfo *pInfo         /* Fill in this structure */
 1.49278 +){
 1.49279 +  parseCell(pPage, iCell, pInfo);
 1.49280 +}
 1.49281 +
 1.49282 +/*
 1.49283 +** Compute the total number of bytes that a Cell needs in the cell
 1.49284 +** data area of the btree-page.  The return number includes the cell
 1.49285 +** data header and the local payload, but not any overflow page or
 1.49286 +** the space used by the cell pointer.
 1.49287 +*/
 1.49288 +static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
 1.49289 +  u8 *pIter = &pCell[pPage->childPtrSize];
 1.49290 +  u32 nSize;
 1.49291 +
 1.49292 +#ifdef SQLITE_DEBUG
 1.49293 +  /* The value returned by this function should always be the same as
 1.49294 +  ** the (CellInfo.nSize) value found by doing a full parse of the
 1.49295 +  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
 1.49296 +  ** this function verifies that this invariant is not violated. */
 1.49297 +  CellInfo debuginfo;
 1.49298 +  btreeParseCellPtr(pPage, pCell, &debuginfo);
 1.49299 +#endif
 1.49300 +
 1.49301 +  if( pPage->intKey ){
 1.49302 +    u8 *pEnd;
 1.49303 +    if( pPage->hasData ){
 1.49304 +      pIter += getVarint32(pIter, nSize);
 1.49305 +    }else{
 1.49306 +      nSize = 0;
 1.49307 +    }
 1.49308 +
 1.49309 +    /* pIter now points at the 64-bit integer key value, a variable length 
 1.49310 +    ** integer. The following block moves pIter to point at the first byte
 1.49311 +    ** past the end of the key value. */
 1.49312 +    pEnd = &pIter[9];
 1.49313 +    while( (*pIter++)&0x80 && pIter<pEnd );
 1.49314 +  }else{
 1.49315 +    pIter += getVarint32(pIter, nSize);
 1.49316 +  }
 1.49317 +
 1.49318 +  testcase( nSize==pPage->maxLocal );
 1.49319 +  testcase( nSize==pPage->maxLocal+1 );
 1.49320 +  if( nSize>pPage->maxLocal ){
 1.49321 +    int minLocal = pPage->minLocal;
 1.49322 +    nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
 1.49323 +    testcase( nSize==pPage->maxLocal );
 1.49324 +    testcase( nSize==pPage->maxLocal+1 );
 1.49325 +    if( nSize>pPage->maxLocal ){
 1.49326 +      nSize = minLocal;
 1.49327 +    }
 1.49328 +    nSize += 4;
 1.49329 +  }
 1.49330 +  nSize += (u32)(pIter - pCell);
 1.49331 +
 1.49332 +  /* The minimum size of any cell is 4 bytes. */
 1.49333 +  if( nSize<4 ){
 1.49334 +    nSize = 4;
 1.49335 +  }
 1.49336 +
 1.49337 +  assert( nSize==debuginfo.nSize );
 1.49338 +  return (u16)nSize;
 1.49339 +}
 1.49340 +
 1.49341 +#ifdef SQLITE_DEBUG
 1.49342 +/* This variation on cellSizePtr() is used inside of assert() statements
 1.49343 +** only. */
 1.49344 +static u16 cellSize(MemPage *pPage, int iCell){
 1.49345 +  return cellSizePtr(pPage, findCell(pPage, iCell));
 1.49346 +}
 1.49347 +#endif
 1.49348 +
 1.49349 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.49350 +/*
 1.49351 +** If the cell pCell, part of page pPage contains a pointer
 1.49352 +** to an overflow page, insert an entry into the pointer-map
 1.49353 +** for the overflow page.
 1.49354 +*/
 1.49355 +static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
 1.49356 +  CellInfo info;
 1.49357 +  if( *pRC ) return;
 1.49358 +  assert( pCell!=0 );
 1.49359 +  btreeParseCellPtr(pPage, pCell, &info);
 1.49360 +  assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
 1.49361 +  if( info.iOverflow ){
 1.49362 +    Pgno ovfl = get4byte(&pCell[info.iOverflow]);
 1.49363 +    ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
 1.49364 +  }
 1.49365 +}
 1.49366 +#endif
 1.49367 +
 1.49368 +
 1.49369 +/*
 1.49370 +** Defragment the page given.  All Cells are moved to the
 1.49371 +** end of the page and all free space is collected into one
 1.49372 +** big FreeBlk that occurs in between the header and cell
 1.49373 +** pointer array and the cell content area.
 1.49374 +*/
 1.49375 +static int defragmentPage(MemPage *pPage){
 1.49376 +  int i;                     /* Loop counter */
 1.49377 +  int pc;                    /* Address of a i-th cell */
 1.49378 +  int hdr;                   /* Offset to the page header */
 1.49379 +  int size;                  /* Size of a cell */
 1.49380 +  int usableSize;            /* Number of usable bytes on a page */
 1.49381 +  int cellOffset;            /* Offset to the cell pointer array */
 1.49382 +  int cbrk;                  /* Offset to the cell content area */
 1.49383 +  int nCell;                 /* Number of cells on the page */
 1.49384 +  unsigned char *data;       /* The page data */
 1.49385 +  unsigned char *temp;       /* Temp area for cell content */
 1.49386 +  int iCellFirst;            /* First allowable cell index */
 1.49387 +  int iCellLast;             /* Last possible cell index */
 1.49388 +
 1.49389 +
 1.49390 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.49391 +  assert( pPage->pBt!=0 );
 1.49392 +  assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
 1.49393 +  assert( pPage->nOverflow==0 );
 1.49394 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49395 +  temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
 1.49396 +  data = pPage->aData;
 1.49397 +  hdr = pPage->hdrOffset;
 1.49398 +  cellOffset = pPage->cellOffset;
 1.49399 +  nCell = pPage->nCell;
 1.49400 +  assert( nCell==get2byte(&data[hdr+3]) );
 1.49401 +  usableSize = pPage->pBt->usableSize;
 1.49402 +  cbrk = get2byte(&data[hdr+5]);
 1.49403 +  memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
 1.49404 +  cbrk = usableSize;
 1.49405 +  iCellFirst = cellOffset + 2*nCell;
 1.49406 +  iCellLast = usableSize - 4;
 1.49407 +  for(i=0; i<nCell; i++){
 1.49408 +    u8 *pAddr;     /* The i-th cell pointer */
 1.49409 +    pAddr = &data[cellOffset + i*2];
 1.49410 +    pc = get2byte(pAddr);
 1.49411 +    testcase( pc==iCellFirst );
 1.49412 +    testcase( pc==iCellLast );
 1.49413 +#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 1.49414 +    /* These conditions have already been verified in btreeInitPage()
 1.49415 +    ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
 1.49416 +    */
 1.49417 +    if( pc<iCellFirst || pc>iCellLast ){
 1.49418 +      return SQLITE_CORRUPT_BKPT;
 1.49419 +    }
 1.49420 +#endif
 1.49421 +    assert( pc>=iCellFirst && pc<=iCellLast );
 1.49422 +    size = cellSizePtr(pPage, &temp[pc]);
 1.49423 +    cbrk -= size;
 1.49424 +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 1.49425 +    if( cbrk<iCellFirst ){
 1.49426 +      return SQLITE_CORRUPT_BKPT;
 1.49427 +    }
 1.49428 +#else
 1.49429 +    if( cbrk<iCellFirst || pc+size>usableSize ){
 1.49430 +      return SQLITE_CORRUPT_BKPT;
 1.49431 +    }
 1.49432 +#endif
 1.49433 +    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
 1.49434 +    testcase( cbrk+size==usableSize );
 1.49435 +    testcase( pc+size==usableSize );
 1.49436 +    memcpy(&data[cbrk], &temp[pc], size);
 1.49437 +    put2byte(pAddr, cbrk);
 1.49438 +  }
 1.49439 +  assert( cbrk>=iCellFirst );
 1.49440 +  put2byte(&data[hdr+5], cbrk);
 1.49441 +  data[hdr+1] = 0;
 1.49442 +  data[hdr+2] = 0;
 1.49443 +  data[hdr+7] = 0;
 1.49444 +  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
 1.49445 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.49446 +  if( cbrk-iCellFirst!=pPage->nFree ){
 1.49447 +    return SQLITE_CORRUPT_BKPT;
 1.49448 +  }
 1.49449 +  return SQLITE_OK;
 1.49450 +}
 1.49451 +
 1.49452 +/*
 1.49453 +** Allocate nByte bytes of space from within the B-Tree page passed
 1.49454 +** as the first argument. Write into *pIdx the index into pPage->aData[]
 1.49455 +** of the first byte of allocated space. Return either SQLITE_OK or
 1.49456 +** an error code (usually SQLITE_CORRUPT).
 1.49457 +**
 1.49458 +** The caller guarantees that there is sufficient space to make the
 1.49459 +** allocation.  This routine might need to defragment in order to bring
 1.49460 +** all the space together, however.  This routine will avoid using
 1.49461 +** the first two bytes past the cell pointer area since presumably this
 1.49462 +** allocation is being made in order to insert a new cell, so we will
 1.49463 +** also end up needing a new cell pointer.
 1.49464 +*/
 1.49465 +static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
 1.49466 +  const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
 1.49467 +  u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
 1.49468 +  int nFrag;                           /* Number of fragmented bytes on pPage */
 1.49469 +  int top;                             /* First byte of cell content area */
 1.49470 +  int gap;        /* First byte of gap between cell pointers and cell content */
 1.49471 +  int rc;         /* Integer return code */
 1.49472 +  int usableSize; /* Usable size of the page */
 1.49473 +  
 1.49474 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.49475 +  assert( pPage->pBt );
 1.49476 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49477 +  assert( nByte>=0 );  /* Minimum cell size is 4 */
 1.49478 +  assert( pPage->nFree>=nByte );
 1.49479 +  assert( pPage->nOverflow==0 );
 1.49480 +  usableSize = pPage->pBt->usableSize;
 1.49481 +  assert( nByte < usableSize-8 );
 1.49482 +
 1.49483 +  nFrag = data[hdr+7];
 1.49484 +  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
 1.49485 +  gap = pPage->cellOffset + 2*pPage->nCell;
 1.49486 +  top = get2byteNotZero(&data[hdr+5]);
 1.49487 +  if( gap>top ) return SQLITE_CORRUPT_BKPT;
 1.49488 +  testcase( gap+2==top );
 1.49489 +  testcase( gap+1==top );
 1.49490 +  testcase( gap==top );
 1.49491 +
 1.49492 +  if( nFrag>=60 ){
 1.49493 +    /* Always defragment highly fragmented pages */
 1.49494 +    rc = defragmentPage(pPage);
 1.49495 +    if( rc ) return rc;
 1.49496 +    top = get2byteNotZero(&data[hdr+5]);
 1.49497 +  }else if( gap+2<=top ){
 1.49498 +    /* Search the freelist looking for a free slot big enough to satisfy 
 1.49499 +    ** the request. The allocation is made from the first free slot in 
 1.49500 +    ** the list that is large enough to accomadate it.
 1.49501 +    */
 1.49502 +    int pc, addr;
 1.49503 +    for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
 1.49504 +      int size;            /* Size of the free slot */
 1.49505 +      if( pc>usableSize-4 || pc<addr+4 ){
 1.49506 +        return SQLITE_CORRUPT_BKPT;
 1.49507 +      }
 1.49508 +      size = get2byte(&data[pc+2]);
 1.49509 +      if( size>=nByte ){
 1.49510 +        int x = size - nByte;
 1.49511 +        testcase( x==4 );
 1.49512 +        testcase( x==3 );
 1.49513 +        if( x<4 ){
 1.49514 +          /* Remove the slot from the free-list. Update the number of
 1.49515 +          ** fragmented bytes within the page. */
 1.49516 +          memcpy(&data[addr], &data[pc], 2);
 1.49517 +          data[hdr+7] = (u8)(nFrag + x);
 1.49518 +        }else if( size+pc > usableSize ){
 1.49519 +          return SQLITE_CORRUPT_BKPT;
 1.49520 +        }else{
 1.49521 +          /* The slot remains on the free-list. Reduce its size to account
 1.49522 +          ** for the portion used by the new allocation. */
 1.49523 +          put2byte(&data[pc+2], x);
 1.49524 +        }
 1.49525 +        *pIdx = pc + x;
 1.49526 +        return SQLITE_OK;
 1.49527 +      }
 1.49528 +    }
 1.49529 +  }
 1.49530 +
 1.49531 +  /* Check to make sure there is enough space in the gap to satisfy
 1.49532 +  ** the allocation.  If not, defragment.
 1.49533 +  */
 1.49534 +  testcase( gap+2+nByte==top );
 1.49535 +  if( gap+2+nByte>top ){
 1.49536 +    rc = defragmentPage(pPage);
 1.49537 +    if( rc ) return rc;
 1.49538 +    top = get2byteNotZero(&data[hdr+5]);
 1.49539 +    assert( gap+nByte<=top );
 1.49540 +  }
 1.49541 +
 1.49542 +
 1.49543 +  /* Allocate memory from the gap in between the cell pointer array
 1.49544 +  ** and the cell content area.  The btreeInitPage() call has already
 1.49545 +  ** validated the freelist.  Given that the freelist is valid, there
 1.49546 +  ** is no way that the allocation can extend off the end of the page.
 1.49547 +  ** The assert() below verifies the previous sentence.
 1.49548 +  */
 1.49549 +  top -= nByte;
 1.49550 +  put2byte(&data[hdr+5], top);
 1.49551 +  assert( top+nByte <= (int)pPage->pBt->usableSize );
 1.49552 +  *pIdx = top;
 1.49553 +  return SQLITE_OK;
 1.49554 +}
 1.49555 +
 1.49556 +/*
 1.49557 +** Return a section of the pPage->aData to the freelist.
 1.49558 +** The first byte of the new free block is pPage->aDisk[start]
 1.49559 +** and the size of the block is "size" bytes.
 1.49560 +**
 1.49561 +** Most of the effort here is involved in coalesing adjacent
 1.49562 +** free blocks into a single big free block.
 1.49563 +*/
 1.49564 +static int freeSpace(MemPage *pPage, int start, int size){
 1.49565 +  int addr, pbegin, hdr;
 1.49566 +  int iLast;                        /* Largest possible freeblock offset */
 1.49567 +  unsigned char *data = pPage->aData;
 1.49568 +
 1.49569 +  assert( pPage->pBt!=0 );
 1.49570 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.49571 +  assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
 1.49572 +  assert( (start + size) <= (int)pPage->pBt->usableSize );
 1.49573 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49574 +  assert( size>=0 );   /* Minimum cell size is 4 */
 1.49575 +
 1.49576 +  if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
 1.49577 +    /* Overwrite deleted information with zeros when the secure_delete
 1.49578 +    ** option is enabled */
 1.49579 +    memset(&data[start], 0, size);
 1.49580 +  }
 1.49581 +
 1.49582 +  /* Add the space back into the linked list of freeblocks.  Note that
 1.49583 +  ** even though the freeblock list was checked by btreeInitPage(),
 1.49584 +  ** btreeInitPage() did not detect overlapping cells or
 1.49585 +  ** freeblocks that overlapped cells.   Nor does it detect when the
 1.49586 +  ** cell content area exceeds the value in the page header.  If these
 1.49587 +  ** situations arise, then subsequent insert operations might corrupt
 1.49588 +  ** the freelist.  So we do need to check for corruption while scanning
 1.49589 +  ** the freelist.
 1.49590 +  */
 1.49591 +  hdr = pPage->hdrOffset;
 1.49592 +  addr = hdr + 1;
 1.49593 +  iLast = pPage->pBt->usableSize - 4;
 1.49594 +  assert( start<=iLast );
 1.49595 +  while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
 1.49596 +    if( pbegin<addr+4 ){
 1.49597 +      return SQLITE_CORRUPT_BKPT;
 1.49598 +    }
 1.49599 +    addr = pbegin;
 1.49600 +  }
 1.49601 +  if( pbegin>iLast ){
 1.49602 +    return SQLITE_CORRUPT_BKPT;
 1.49603 +  }
 1.49604 +  assert( pbegin>addr || pbegin==0 );
 1.49605 +  put2byte(&data[addr], start);
 1.49606 +  put2byte(&data[start], pbegin);
 1.49607 +  put2byte(&data[start+2], size);
 1.49608 +  pPage->nFree = pPage->nFree + (u16)size;
 1.49609 +
 1.49610 +  /* Coalesce adjacent free blocks */
 1.49611 +  addr = hdr + 1;
 1.49612 +  while( (pbegin = get2byte(&data[addr]))>0 ){
 1.49613 +    int pnext, psize, x;
 1.49614 +    assert( pbegin>addr );
 1.49615 +    assert( pbegin <= (int)pPage->pBt->usableSize-4 );
 1.49616 +    pnext = get2byte(&data[pbegin]);
 1.49617 +    psize = get2byte(&data[pbegin+2]);
 1.49618 +    if( pbegin + psize + 3 >= pnext && pnext>0 ){
 1.49619 +      int frag = pnext - (pbegin+psize);
 1.49620 +      if( (frag<0) || (frag>(int)data[hdr+7]) ){
 1.49621 +        return SQLITE_CORRUPT_BKPT;
 1.49622 +      }
 1.49623 +      data[hdr+7] -= (u8)frag;
 1.49624 +      x = get2byte(&data[pnext]);
 1.49625 +      put2byte(&data[pbegin], x);
 1.49626 +      x = pnext + get2byte(&data[pnext+2]) - pbegin;
 1.49627 +      put2byte(&data[pbegin+2], x);
 1.49628 +    }else{
 1.49629 +      addr = pbegin;
 1.49630 +    }
 1.49631 +  }
 1.49632 +
 1.49633 +  /* If the cell content area begins with a freeblock, remove it. */
 1.49634 +  if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
 1.49635 +    int top;
 1.49636 +    pbegin = get2byte(&data[hdr+1]);
 1.49637 +    memcpy(&data[hdr+1], &data[pbegin], 2);
 1.49638 +    top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
 1.49639 +    put2byte(&data[hdr+5], top);
 1.49640 +  }
 1.49641 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.49642 +  return SQLITE_OK;
 1.49643 +}
 1.49644 +
 1.49645 +/*
 1.49646 +** Decode the flags byte (the first byte of the header) for a page
 1.49647 +** and initialize fields of the MemPage structure accordingly.
 1.49648 +**
 1.49649 +** Only the following combinations are supported.  Anything different
 1.49650 +** indicates a corrupt database files:
 1.49651 +**
 1.49652 +**         PTF_ZERODATA
 1.49653 +**         PTF_ZERODATA | PTF_LEAF
 1.49654 +**         PTF_LEAFDATA | PTF_INTKEY
 1.49655 +**         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
 1.49656 +*/
 1.49657 +static int decodeFlags(MemPage *pPage, int flagByte){
 1.49658 +  BtShared *pBt;     /* A copy of pPage->pBt */
 1.49659 +
 1.49660 +  assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
 1.49661 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49662 +  pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
 1.49663 +  flagByte &= ~PTF_LEAF;
 1.49664 +  pPage->childPtrSize = 4-4*pPage->leaf;
 1.49665 +  pBt = pPage->pBt;
 1.49666 +  if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
 1.49667 +    pPage->intKey = 1;
 1.49668 +    pPage->hasData = pPage->leaf;
 1.49669 +    pPage->maxLocal = pBt->maxLeaf;
 1.49670 +    pPage->minLocal = pBt->minLeaf;
 1.49671 +  }else if( flagByte==PTF_ZERODATA ){
 1.49672 +    pPage->intKey = 0;
 1.49673 +    pPage->hasData = 0;
 1.49674 +    pPage->maxLocal = pBt->maxLocal;
 1.49675 +    pPage->minLocal = pBt->minLocal;
 1.49676 +  }else{
 1.49677 +    return SQLITE_CORRUPT_BKPT;
 1.49678 +  }
 1.49679 +  pPage->max1bytePayload = pBt->max1bytePayload;
 1.49680 +  return SQLITE_OK;
 1.49681 +}
 1.49682 +
 1.49683 +/*
 1.49684 +** Initialize the auxiliary information for a disk block.
 1.49685 +**
 1.49686 +** Return SQLITE_OK on success.  If we see that the page does
 1.49687 +** not contain a well-formed database page, then return 
 1.49688 +** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
 1.49689 +** guarantee that the page is well-formed.  It only shows that
 1.49690 +** we failed to detect any corruption.
 1.49691 +*/
 1.49692 +static int btreeInitPage(MemPage *pPage){
 1.49693 +
 1.49694 +  assert( pPage->pBt!=0 );
 1.49695 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49696 +  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
 1.49697 +  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
 1.49698 +  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
 1.49699 +
 1.49700 +  if( !pPage->isInit ){
 1.49701 +    u16 pc;            /* Address of a freeblock within pPage->aData[] */
 1.49702 +    u8 hdr;            /* Offset to beginning of page header */
 1.49703 +    u8 *data;          /* Equal to pPage->aData */
 1.49704 +    BtShared *pBt;        /* The main btree structure */
 1.49705 +    int usableSize;    /* Amount of usable space on each page */
 1.49706 +    u16 cellOffset;    /* Offset from start of page to first cell pointer */
 1.49707 +    int nFree;         /* Number of unused bytes on the page */
 1.49708 +    int top;           /* First byte of the cell content area */
 1.49709 +    int iCellFirst;    /* First allowable cell or freeblock offset */
 1.49710 +    int iCellLast;     /* Last possible cell or freeblock offset */
 1.49711 +
 1.49712 +    pBt = pPage->pBt;
 1.49713 +
 1.49714 +    hdr = pPage->hdrOffset;
 1.49715 +    data = pPage->aData;
 1.49716 +    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
 1.49717 +    assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 1.49718 +    pPage->maskPage = (u16)(pBt->pageSize - 1);
 1.49719 +    pPage->nOverflow = 0;
 1.49720 +    usableSize = pBt->usableSize;
 1.49721 +    pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
 1.49722 +    pPage->aDataEnd = &data[usableSize];
 1.49723 +    pPage->aCellIdx = &data[cellOffset];
 1.49724 +    top = get2byteNotZero(&data[hdr+5]);
 1.49725 +    pPage->nCell = get2byte(&data[hdr+3]);
 1.49726 +    if( pPage->nCell>MX_CELL(pBt) ){
 1.49727 +      /* To many cells for a single page.  The page must be corrupt */
 1.49728 +      return SQLITE_CORRUPT_BKPT;
 1.49729 +    }
 1.49730 +    testcase( pPage->nCell==MX_CELL(pBt) );
 1.49731 +
 1.49732 +    /* A malformed database page might cause us to read past the end
 1.49733 +    ** of page when parsing a cell.  
 1.49734 +    **
 1.49735 +    ** The following block of code checks early to see if a cell extends
 1.49736 +    ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
 1.49737 +    ** returned if it does.
 1.49738 +    */
 1.49739 +    iCellFirst = cellOffset + 2*pPage->nCell;
 1.49740 +    iCellLast = usableSize - 4;
 1.49741 +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 1.49742 +    {
 1.49743 +      int i;            /* Index into the cell pointer array */
 1.49744 +      int sz;           /* Size of a cell */
 1.49745 +
 1.49746 +      if( !pPage->leaf ) iCellLast--;
 1.49747 +      for(i=0; i<pPage->nCell; i++){
 1.49748 +        pc = get2byte(&data[cellOffset+i*2]);
 1.49749 +        testcase( pc==iCellFirst );
 1.49750 +        testcase( pc==iCellLast );
 1.49751 +        if( pc<iCellFirst || pc>iCellLast ){
 1.49752 +          return SQLITE_CORRUPT_BKPT;
 1.49753 +        }
 1.49754 +        sz = cellSizePtr(pPage, &data[pc]);
 1.49755 +        testcase( pc+sz==usableSize );
 1.49756 +        if( pc+sz>usableSize ){
 1.49757 +          return SQLITE_CORRUPT_BKPT;
 1.49758 +        }
 1.49759 +      }
 1.49760 +      if( !pPage->leaf ) iCellLast++;
 1.49761 +    }  
 1.49762 +#endif
 1.49763 +
 1.49764 +    /* Compute the total free space on the page */
 1.49765 +    pc = get2byte(&data[hdr+1]);
 1.49766 +    nFree = data[hdr+7] + top;
 1.49767 +    while( pc>0 ){
 1.49768 +      u16 next, size;
 1.49769 +      if( pc<iCellFirst || pc>iCellLast ){
 1.49770 +        /* Start of free block is off the page */
 1.49771 +        return SQLITE_CORRUPT_BKPT; 
 1.49772 +      }
 1.49773 +      next = get2byte(&data[pc]);
 1.49774 +      size = get2byte(&data[pc+2]);
 1.49775 +      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
 1.49776 +        /* Free blocks must be in ascending order. And the last byte of
 1.49777 +        ** the free-block must lie on the database page.  */
 1.49778 +        return SQLITE_CORRUPT_BKPT; 
 1.49779 +      }
 1.49780 +      nFree = nFree + size;
 1.49781 +      pc = next;
 1.49782 +    }
 1.49783 +
 1.49784 +    /* At this point, nFree contains the sum of the offset to the start
 1.49785 +    ** of the cell-content area plus the number of free bytes within
 1.49786 +    ** the cell-content area. If this is greater than the usable-size
 1.49787 +    ** of the page, then the page must be corrupted. This check also
 1.49788 +    ** serves to verify that the offset to the start of the cell-content
 1.49789 +    ** area, according to the page header, lies within the page.
 1.49790 +    */
 1.49791 +    if( nFree>usableSize ){
 1.49792 +      return SQLITE_CORRUPT_BKPT; 
 1.49793 +    }
 1.49794 +    pPage->nFree = (u16)(nFree - iCellFirst);
 1.49795 +    pPage->isInit = 1;
 1.49796 +  }
 1.49797 +  return SQLITE_OK;
 1.49798 +}
 1.49799 +
 1.49800 +/*
 1.49801 +** Set up a raw page so that it looks like a database page holding
 1.49802 +** no entries.
 1.49803 +*/
 1.49804 +static void zeroPage(MemPage *pPage, int flags){
 1.49805 +  unsigned char *data = pPage->aData;
 1.49806 +  BtShared *pBt = pPage->pBt;
 1.49807 +  u8 hdr = pPage->hdrOffset;
 1.49808 +  u16 first;
 1.49809 +
 1.49810 +  assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
 1.49811 +  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
 1.49812 +  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
 1.49813 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.49814 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.49815 +  if( pBt->btsFlags & BTS_SECURE_DELETE ){
 1.49816 +    memset(&data[hdr], 0, pBt->usableSize - hdr);
 1.49817 +  }
 1.49818 +  data[hdr] = (char)flags;
 1.49819 +  first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
 1.49820 +  memset(&data[hdr+1], 0, 4);
 1.49821 +  data[hdr+7] = 0;
 1.49822 +  put2byte(&data[hdr+5], pBt->usableSize);
 1.49823 +  pPage->nFree = (u16)(pBt->usableSize - first);
 1.49824 +  decodeFlags(pPage, flags);
 1.49825 +  pPage->hdrOffset = hdr;
 1.49826 +  pPage->cellOffset = first;
 1.49827 +  pPage->aDataEnd = &data[pBt->usableSize];
 1.49828 +  pPage->aCellIdx = &data[first];
 1.49829 +  pPage->nOverflow = 0;
 1.49830 +  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 1.49831 +  pPage->maskPage = (u16)(pBt->pageSize - 1);
 1.49832 +  pPage->nCell = 0;
 1.49833 +  pPage->isInit = 1;
 1.49834 +}
 1.49835 +
 1.49836 +
 1.49837 +/*
 1.49838 +** Convert a DbPage obtained from the pager into a MemPage used by
 1.49839 +** the btree layer.
 1.49840 +*/
 1.49841 +static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
 1.49842 +  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
 1.49843 +  pPage->aData = sqlite3PagerGetData(pDbPage);
 1.49844 +  pPage->pDbPage = pDbPage;
 1.49845 +  pPage->pBt = pBt;
 1.49846 +  pPage->pgno = pgno;
 1.49847 +  pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
 1.49848 +  return pPage; 
 1.49849 +}
 1.49850 +
 1.49851 +/*
 1.49852 +** Get a page from the pager.  Initialize the MemPage.pBt and
 1.49853 +** MemPage.aData elements if needed.
 1.49854 +**
 1.49855 +** If the noContent flag is set, it means that we do not care about
 1.49856 +** the content of the page at this time.  So do not go to the disk
 1.49857 +** to fetch the content.  Just fill in the content with zeros for now.
 1.49858 +** If in the future we call sqlite3PagerWrite() on this page, that
 1.49859 +** means we have started to be concerned about content and the disk
 1.49860 +** read should occur at that point.
 1.49861 +*/
 1.49862 +static int btreeGetPage(
 1.49863 +  BtShared *pBt,       /* The btree */
 1.49864 +  Pgno pgno,           /* Number of the page to fetch */
 1.49865 +  MemPage **ppPage,    /* Return the page in this parameter */
 1.49866 +  int noContent        /* Do not load page content if true */
 1.49867 +){
 1.49868 +  int rc;
 1.49869 +  DbPage *pDbPage;
 1.49870 +
 1.49871 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.49872 +  rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
 1.49873 +  if( rc ) return rc;
 1.49874 +  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
 1.49875 +  return SQLITE_OK;
 1.49876 +}
 1.49877 +
 1.49878 +/*
 1.49879 +** Retrieve a page from the pager cache. If the requested page is not
 1.49880 +** already in the pager cache return NULL. Initialize the MemPage.pBt and
 1.49881 +** MemPage.aData elements if needed.
 1.49882 +*/
 1.49883 +static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
 1.49884 +  DbPage *pDbPage;
 1.49885 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.49886 +  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
 1.49887 +  if( pDbPage ){
 1.49888 +    return btreePageFromDbPage(pDbPage, pgno, pBt);
 1.49889 +  }
 1.49890 +  return 0;
 1.49891 +}
 1.49892 +
 1.49893 +/*
 1.49894 +** Return the size of the database file in pages. If there is any kind of
 1.49895 +** error, return ((unsigned int)-1).
 1.49896 +*/
 1.49897 +static Pgno btreePagecount(BtShared *pBt){
 1.49898 +  return pBt->nPage;
 1.49899 +}
 1.49900 +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
 1.49901 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.49902 +  assert( ((p->pBt->nPage)&0x8000000)==0 );
 1.49903 +  return (int)btreePagecount(p->pBt);
 1.49904 +}
 1.49905 +
 1.49906 +/*
 1.49907 +** Get a page from the pager and initialize it.  This routine is just a
 1.49908 +** convenience wrapper around separate calls to btreeGetPage() and 
 1.49909 +** btreeInitPage().
 1.49910 +**
 1.49911 +** If an error occurs, then the value *ppPage is set to is undefined. It
 1.49912 +** may remain unchanged, or it may be set to an invalid value.
 1.49913 +*/
 1.49914 +static int getAndInitPage(
 1.49915 +  BtShared *pBt,          /* The database file */
 1.49916 +  Pgno pgno,           /* Number of the page to get */
 1.49917 +  MemPage **ppPage     /* Write the page pointer here */
 1.49918 +){
 1.49919 +  int rc;
 1.49920 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.49921 +
 1.49922 +  if( pgno>btreePagecount(pBt) ){
 1.49923 +    rc = SQLITE_CORRUPT_BKPT;
 1.49924 +  }else{
 1.49925 +    rc = btreeGetPage(pBt, pgno, ppPage, 0);
 1.49926 +    if( rc==SQLITE_OK ){
 1.49927 +      rc = btreeInitPage(*ppPage);
 1.49928 +      if( rc!=SQLITE_OK ){
 1.49929 +        releasePage(*ppPage);
 1.49930 +      }
 1.49931 +    }
 1.49932 +  }
 1.49933 +
 1.49934 +  testcase( pgno==0 );
 1.49935 +  assert( pgno!=0 || rc==SQLITE_CORRUPT );
 1.49936 +  return rc;
 1.49937 +}
 1.49938 +
 1.49939 +/*
 1.49940 +** Release a MemPage.  This should be called once for each prior
 1.49941 +** call to btreeGetPage.
 1.49942 +*/
 1.49943 +static void releasePage(MemPage *pPage){
 1.49944 +  if( pPage ){
 1.49945 +    assert( pPage->aData );
 1.49946 +    assert( pPage->pBt );
 1.49947 +    assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
 1.49948 +    assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
 1.49949 +    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49950 +    sqlite3PagerUnref(pPage->pDbPage);
 1.49951 +  }
 1.49952 +}
 1.49953 +
 1.49954 +/*
 1.49955 +** During a rollback, when the pager reloads information into the cache
 1.49956 +** so that the cache is restored to its original state at the start of
 1.49957 +** the transaction, for each page restored this routine is called.
 1.49958 +**
 1.49959 +** This routine needs to reset the extra data section at the end of the
 1.49960 +** page to agree with the restored data.
 1.49961 +*/
 1.49962 +static void pageReinit(DbPage *pData){
 1.49963 +  MemPage *pPage;
 1.49964 +  pPage = (MemPage *)sqlite3PagerGetExtra(pData);
 1.49965 +  assert( sqlite3PagerPageRefcount(pData)>0 );
 1.49966 +  if( pPage->isInit ){
 1.49967 +    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.49968 +    pPage->isInit = 0;
 1.49969 +    if( sqlite3PagerPageRefcount(pData)>1 ){
 1.49970 +      /* pPage might not be a btree page;  it might be an overflow page
 1.49971 +      ** or ptrmap page or a free page.  In those cases, the following
 1.49972 +      ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
 1.49973 +      ** But no harm is done by this.  And it is very important that
 1.49974 +      ** btreeInitPage() be called on every btree page so we make
 1.49975 +      ** the call for every page that comes in for re-initing. */
 1.49976 +      btreeInitPage(pPage);
 1.49977 +    }
 1.49978 +  }
 1.49979 +}
 1.49980 +
 1.49981 +/*
 1.49982 +** Invoke the busy handler for a btree.
 1.49983 +*/
 1.49984 +static int btreeInvokeBusyHandler(void *pArg){
 1.49985 +  BtShared *pBt = (BtShared*)pArg;
 1.49986 +  assert( pBt->db );
 1.49987 +  assert( sqlite3_mutex_held(pBt->db->mutex) );
 1.49988 +  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
 1.49989 +}
 1.49990 +
 1.49991 +/*
 1.49992 +** Open a database file.
 1.49993 +** 
 1.49994 +** zFilename is the name of the database file.  If zFilename is NULL
 1.49995 +** then an ephemeral database is created.  The ephemeral database might
 1.49996 +** be exclusively in memory, or it might use a disk-based memory cache.
 1.49997 +** Either way, the ephemeral database will be automatically deleted 
 1.49998 +** when sqlite3BtreeClose() is called.
 1.49999 +**
 1.50000 +** If zFilename is ":memory:" then an in-memory database is created
 1.50001 +** that is automatically destroyed when it is closed.
 1.50002 +**
 1.50003 +** The "flags" parameter is a bitmask that might contain bits like
 1.50004 +** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
 1.50005 +**
 1.50006 +** If the database is already opened in the same database connection
 1.50007 +** and we are in shared cache mode, then the open will fail with an
 1.50008 +** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
 1.50009 +** objects in the same database connection since doing so will lead
 1.50010 +** to problems with locking.
 1.50011 +*/
 1.50012 +SQLITE_PRIVATE int sqlite3BtreeOpen(
 1.50013 +  sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
 1.50014 +  const char *zFilename,  /* Name of the file containing the BTree database */
 1.50015 +  sqlite3 *db,            /* Associated database handle */
 1.50016 +  Btree **ppBtree,        /* Pointer to new Btree object written here */
 1.50017 +  int flags,              /* Options */
 1.50018 +  int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
 1.50019 +){
 1.50020 +  BtShared *pBt = 0;             /* Shared part of btree structure */
 1.50021 +  Btree *p;                      /* Handle to return */
 1.50022 +  sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
 1.50023 +  int rc = SQLITE_OK;            /* Result code from this function */
 1.50024 +  u8 nReserve;                   /* Byte of unused space on each page */
 1.50025 +  unsigned char zDbHeader[100];  /* Database header content */
 1.50026 +
 1.50027 +  /* True if opening an ephemeral, temporary database */
 1.50028 +  const int isTempDb = zFilename==0 || zFilename[0]==0;
 1.50029 +
 1.50030 +  /* Set the variable isMemdb to true for an in-memory database, or 
 1.50031 +  ** false for a file-based database.
 1.50032 +  */
 1.50033 +#ifdef SQLITE_OMIT_MEMORYDB
 1.50034 +  const int isMemdb = 0;
 1.50035 +#else
 1.50036 +  const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
 1.50037 +                       || (isTempDb && sqlite3TempInMemory(db))
 1.50038 +                       || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
 1.50039 +#endif
 1.50040 +
 1.50041 +  assert( db!=0 );
 1.50042 +  assert( pVfs!=0 );
 1.50043 +  assert( sqlite3_mutex_held(db->mutex) );
 1.50044 +  assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
 1.50045 +
 1.50046 +  /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
 1.50047 +  assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
 1.50048 +
 1.50049 +  /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
 1.50050 +  assert( (flags & BTREE_SINGLE)==0 || isTempDb );
 1.50051 +
 1.50052 +  if( isMemdb ){
 1.50053 +    flags |= BTREE_MEMORY;
 1.50054 +  }
 1.50055 +  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
 1.50056 +    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
 1.50057 +  }
 1.50058 +  p = sqlite3MallocZero(sizeof(Btree));
 1.50059 +  if( !p ){
 1.50060 +    return SQLITE_NOMEM;
 1.50061 +  }
 1.50062 +  p->inTrans = TRANS_NONE;
 1.50063 +  p->db = db;
 1.50064 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.50065 +  p->lock.pBtree = p;
 1.50066 +  p->lock.iTable = 1;
 1.50067 +#endif
 1.50068 +
 1.50069 +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 1.50070 +  /*
 1.50071 +  ** If this Btree is a candidate for shared cache, try to find an
 1.50072 +  ** existing BtShared object that we can share with
 1.50073 +  */
 1.50074 +  if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
 1.50075 +    if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
 1.50076 +      int nFullPathname = pVfs->mxPathname+1;
 1.50077 +      char *zFullPathname = sqlite3Malloc(nFullPathname);
 1.50078 +      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 1.50079 +      p->sharable = 1;
 1.50080 +      if( !zFullPathname ){
 1.50081 +        sqlite3_free(p);
 1.50082 +        return SQLITE_NOMEM;
 1.50083 +      }
 1.50084 +      if( isMemdb ){
 1.50085 +        memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
 1.50086 +      }else{
 1.50087 +        rc = sqlite3OsFullPathname(pVfs, zFilename,
 1.50088 +                                   nFullPathname, zFullPathname);
 1.50089 +        if( rc ){
 1.50090 +          sqlite3_free(zFullPathname);
 1.50091 +          sqlite3_free(p);
 1.50092 +          return rc;
 1.50093 +        }
 1.50094 +      }
 1.50095 +#if SQLITE_THREADSAFE
 1.50096 +      mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
 1.50097 +      sqlite3_mutex_enter(mutexOpen);
 1.50098 +      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 1.50099 +      sqlite3_mutex_enter(mutexShared);
 1.50100 +#endif
 1.50101 +      for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
 1.50102 +        assert( pBt->nRef>0 );
 1.50103 +        if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
 1.50104 +                 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
 1.50105 +          int iDb;
 1.50106 +          for(iDb=db->nDb-1; iDb>=0; iDb--){
 1.50107 +            Btree *pExisting = db->aDb[iDb].pBt;
 1.50108 +            if( pExisting && pExisting->pBt==pBt ){
 1.50109 +              sqlite3_mutex_leave(mutexShared);
 1.50110 +              sqlite3_mutex_leave(mutexOpen);
 1.50111 +              sqlite3_free(zFullPathname);
 1.50112 +              sqlite3_free(p);
 1.50113 +              return SQLITE_CONSTRAINT;
 1.50114 +            }
 1.50115 +          }
 1.50116 +          p->pBt = pBt;
 1.50117 +          pBt->nRef++;
 1.50118 +          break;
 1.50119 +        }
 1.50120 +      }
 1.50121 +      sqlite3_mutex_leave(mutexShared);
 1.50122 +      sqlite3_free(zFullPathname);
 1.50123 +    }
 1.50124 +#ifdef SQLITE_DEBUG
 1.50125 +    else{
 1.50126 +      /* In debug mode, we mark all persistent databases as sharable
 1.50127 +      ** even when they are not.  This exercises the locking code and
 1.50128 +      ** gives more opportunity for asserts(sqlite3_mutex_held())
 1.50129 +      ** statements to find locking problems.
 1.50130 +      */
 1.50131 +      p->sharable = 1;
 1.50132 +    }
 1.50133 +#endif
 1.50134 +  }
 1.50135 +#endif
 1.50136 +  if( pBt==0 ){
 1.50137 +    /*
 1.50138 +    ** The following asserts make sure that structures used by the btree are
 1.50139 +    ** the right size.  This is to guard against size changes that result
 1.50140 +    ** when compiling on a different architecture.
 1.50141 +    */
 1.50142 +    assert( sizeof(i64)==8 || sizeof(i64)==4 );
 1.50143 +    assert( sizeof(u64)==8 || sizeof(u64)==4 );
 1.50144 +    assert( sizeof(u32)==4 );
 1.50145 +    assert( sizeof(u16)==2 );
 1.50146 +    assert( sizeof(Pgno)==4 );
 1.50147 +  
 1.50148 +    pBt = sqlite3MallocZero( sizeof(*pBt) );
 1.50149 +    if( pBt==0 ){
 1.50150 +      rc = SQLITE_NOMEM;
 1.50151 +      goto btree_open_out;
 1.50152 +    }
 1.50153 +    rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
 1.50154 +                          EXTRA_SIZE, flags, vfsFlags, pageReinit);
 1.50155 +    if( rc==SQLITE_OK ){
 1.50156 +      rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
 1.50157 +    }
 1.50158 +    if( rc!=SQLITE_OK ){
 1.50159 +      goto btree_open_out;
 1.50160 +    }
 1.50161 +    pBt->openFlags = (u8)flags;
 1.50162 +    pBt->db = db;
 1.50163 +    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
 1.50164 +    p->pBt = pBt;
 1.50165 +  
 1.50166 +    pBt->pCursor = 0;
 1.50167 +    pBt->pPage1 = 0;
 1.50168 +    if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
 1.50169 +#ifdef SQLITE_SECURE_DELETE
 1.50170 +    pBt->btsFlags |= BTS_SECURE_DELETE;
 1.50171 +#endif
 1.50172 +    pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
 1.50173 +    if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
 1.50174 +         || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
 1.50175 +      pBt->pageSize = 0;
 1.50176 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.50177 +      /* If the magic name ":memory:" will create an in-memory database, then
 1.50178 +      ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
 1.50179 +      ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
 1.50180 +      ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
 1.50181 +      ** regular file-name. In this case the auto-vacuum applies as per normal.
 1.50182 +      */
 1.50183 +      if( zFilename && !isMemdb ){
 1.50184 +        pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
 1.50185 +        pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
 1.50186 +      }
 1.50187 +#endif
 1.50188 +      nReserve = 0;
 1.50189 +    }else{
 1.50190 +      nReserve = zDbHeader[20];
 1.50191 +      pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 1.50192 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.50193 +      pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
 1.50194 +      pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
 1.50195 +#endif
 1.50196 +    }
 1.50197 +    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
 1.50198 +    if( rc ) goto btree_open_out;
 1.50199 +    pBt->usableSize = pBt->pageSize - nReserve;
 1.50200 +    assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
 1.50201 +   
 1.50202 +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 1.50203 +    /* Add the new BtShared object to the linked list sharable BtShareds.
 1.50204 +    */
 1.50205 +    if( p->sharable ){
 1.50206 +      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 1.50207 +      pBt->nRef = 1;
 1.50208 +      MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
 1.50209 +      if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
 1.50210 +        pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
 1.50211 +        if( pBt->mutex==0 ){
 1.50212 +          rc = SQLITE_NOMEM;
 1.50213 +          db->mallocFailed = 0;
 1.50214 +          goto btree_open_out;
 1.50215 +        }
 1.50216 +      }
 1.50217 +      sqlite3_mutex_enter(mutexShared);
 1.50218 +      pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
 1.50219 +      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
 1.50220 +      sqlite3_mutex_leave(mutexShared);
 1.50221 +    }
 1.50222 +#endif
 1.50223 +  }
 1.50224 +
 1.50225 +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 1.50226 +  /* If the new Btree uses a sharable pBtShared, then link the new
 1.50227 +  ** Btree into the list of all sharable Btrees for the same connection.
 1.50228 +  ** The list is kept in ascending order by pBt address.
 1.50229 +  */
 1.50230 +  if( p->sharable ){
 1.50231 +    int i;
 1.50232 +    Btree *pSib;
 1.50233 +    for(i=0; i<db->nDb; i++){
 1.50234 +      if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
 1.50235 +        while( pSib->pPrev ){ pSib = pSib->pPrev; }
 1.50236 +        if( p->pBt<pSib->pBt ){
 1.50237 +          p->pNext = pSib;
 1.50238 +          p->pPrev = 0;
 1.50239 +          pSib->pPrev = p;
 1.50240 +        }else{
 1.50241 +          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
 1.50242 +            pSib = pSib->pNext;
 1.50243 +          }
 1.50244 +          p->pNext = pSib->pNext;
 1.50245 +          p->pPrev = pSib;
 1.50246 +          if( p->pNext ){
 1.50247 +            p->pNext->pPrev = p;
 1.50248 +          }
 1.50249 +          pSib->pNext = p;
 1.50250 +        }
 1.50251 +        break;
 1.50252 +      }
 1.50253 +    }
 1.50254 +  }
 1.50255 +#endif
 1.50256 +  *ppBtree = p;
 1.50257 +
 1.50258 +btree_open_out:
 1.50259 +  if( rc!=SQLITE_OK ){
 1.50260 +    if( pBt && pBt->pPager ){
 1.50261 +      sqlite3PagerClose(pBt->pPager);
 1.50262 +    }
 1.50263 +    sqlite3_free(pBt);
 1.50264 +    sqlite3_free(p);
 1.50265 +    *ppBtree = 0;
 1.50266 +  }else{
 1.50267 +    /* If the B-Tree was successfully opened, set the pager-cache size to the
 1.50268 +    ** default value. Except, when opening on an existing shared pager-cache,
 1.50269 +    ** do not change the pager-cache size.
 1.50270 +    */
 1.50271 +    if( sqlite3BtreeSchema(p, 0, 0)==0 ){
 1.50272 +      sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
 1.50273 +    }
 1.50274 +  }
 1.50275 +  if( mutexOpen ){
 1.50276 +    assert( sqlite3_mutex_held(mutexOpen) );
 1.50277 +    sqlite3_mutex_leave(mutexOpen);
 1.50278 +  }
 1.50279 +  return rc;
 1.50280 +}
 1.50281 +
 1.50282 +/*
 1.50283 +** Decrement the BtShared.nRef counter.  When it reaches zero,
 1.50284 +** remove the BtShared structure from the sharing list.  Return
 1.50285 +** true if the BtShared.nRef counter reaches zero and return
 1.50286 +** false if it is still positive.
 1.50287 +*/
 1.50288 +static int removeFromSharingList(BtShared *pBt){
 1.50289 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.50290 +  MUTEX_LOGIC( sqlite3_mutex *pMaster; )
 1.50291 +  BtShared *pList;
 1.50292 +  int removed = 0;
 1.50293 +
 1.50294 +  assert( sqlite3_mutex_notheld(pBt->mutex) );
 1.50295 +  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 1.50296 +  sqlite3_mutex_enter(pMaster);
 1.50297 +  pBt->nRef--;
 1.50298 +  if( pBt->nRef<=0 ){
 1.50299 +    if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
 1.50300 +      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
 1.50301 +    }else{
 1.50302 +      pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
 1.50303 +      while( ALWAYS(pList) && pList->pNext!=pBt ){
 1.50304 +        pList=pList->pNext;
 1.50305 +      }
 1.50306 +      if( ALWAYS(pList) ){
 1.50307 +        pList->pNext = pBt->pNext;
 1.50308 +      }
 1.50309 +    }
 1.50310 +    if( SQLITE_THREADSAFE ){
 1.50311 +      sqlite3_mutex_free(pBt->mutex);
 1.50312 +    }
 1.50313 +    removed = 1;
 1.50314 +  }
 1.50315 +  sqlite3_mutex_leave(pMaster);
 1.50316 +  return removed;
 1.50317 +#else
 1.50318 +  return 1;
 1.50319 +#endif
 1.50320 +}
 1.50321 +
 1.50322 +/*
 1.50323 +** Make sure pBt->pTmpSpace points to an allocation of 
 1.50324 +** MX_CELL_SIZE(pBt) bytes.
 1.50325 +*/
 1.50326 +static void allocateTempSpace(BtShared *pBt){
 1.50327 +  if( !pBt->pTmpSpace ){
 1.50328 +    pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
 1.50329 +  }
 1.50330 +}
 1.50331 +
 1.50332 +/*
 1.50333 +** Free the pBt->pTmpSpace allocation
 1.50334 +*/
 1.50335 +static void freeTempSpace(BtShared *pBt){
 1.50336 +  sqlite3PageFree( pBt->pTmpSpace);
 1.50337 +  pBt->pTmpSpace = 0;
 1.50338 +}
 1.50339 +
 1.50340 +/*
 1.50341 +** Close an open database and invalidate all cursors.
 1.50342 +*/
 1.50343 +SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
 1.50344 +  BtShared *pBt = p->pBt;
 1.50345 +  BtCursor *pCur;
 1.50346 +
 1.50347 +  /* Close all cursors opened via this handle.  */
 1.50348 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.50349 +  sqlite3BtreeEnter(p);
 1.50350 +  pCur = pBt->pCursor;
 1.50351 +  while( pCur ){
 1.50352 +    BtCursor *pTmp = pCur;
 1.50353 +    pCur = pCur->pNext;
 1.50354 +    if( pTmp->pBtree==p ){
 1.50355 +      sqlite3BtreeCloseCursor(pTmp);
 1.50356 +    }
 1.50357 +  }
 1.50358 +
 1.50359 +  /* Rollback any active transaction and free the handle structure.
 1.50360 +  ** The call to sqlite3BtreeRollback() drops any table-locks held by
 1.50361 +  ** this handle.
 1.50362 +  */
 1.50363 +  sqlite3BtreeRollback(p, SQLITE_OK);
 1.50364 +  sqlite3BtreeLeave(p);
 1.50365 +
 1.50366 +  /* If there are still other outstanding references to the shared-btree
 1.50367 +  ** structure, return now. The remainder of this procedure cleans 
 1.50368 +  ** up the shared-btree.
 1.50369 +  */
 1.50370 +  assert( p->wantToLock==0 && p->locked==0 );
 1.50371 +  if( !p->sharable || removeFromSharingList(pBt) ){
 1.50372 +    /* The pBt is no longer on the sharing list, so we can access
 1.50373 +    ** it without having to hold the mutex.
 1.50374 +    **
 1.50375 +    ** Clean out and delete the BtShared object.
 1.50376 +    */
 1.50377 +    assert( !pBt->pCursor );
 1.50378 +    sqlite3PagerClose(pBt->pPager);
 1.50379 +    if( pBt->xFreeSchema && pBt->pSchema ){
 1.50380 +      pBt->xFreeSchema(pBt->pSchema);
 1.50381 +    }
 1.50382 +    sqlite3DbFree(0, pBt->pSchema);
 1.50383 +    freeTempSpace(pBt);
 1.50384 +    sqlite3_free(pBt);
 1.50385 +  }
 1.50386 +
 1.50387 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.50388 +  assert( p->wantToLock==0 );
 1.50389 +  assert( p->locked==0 );
 1.50390 +  if( p->pPrev ) p->pPrev->pNext = p->pNext;
 1.50391 +  if( p->pNext ) p->pNext->pPrev = p->pPrev;
 1.50392 +#endif
 1.50393 +
 1.50394 +  sqlite3_free(p);
 1.50395 +  return SQLITE_OK;
 1.50396 +}
 1.50397 +
 1.50398 +/*
 1.50399 +** Change the limit on the number of pages allowed in the cache.
 1.50400 +**
 1.50401 +** The maximum number of cache pages is set to the absolute
 1.50402 +** value of mxPage.  If mxPage is negative, the pager will
 1.50403 +** operate asynchronously - it will not stop to do fsync()s
 1.50404 +** to insure data is written to the disk surface before
 1.50405 +** continuing.  Transactions still work if synchronous is off,
 1.50406 +** and the database cannot be corrupted if this program
 1.50407 +** crashes.  But if the operating system crashes or there is
 1.50408 +** an abrupt power failure when synchronous is off, the database
 1.50409 +** could be left in an inconsistent and unrecoverable state.
 1.50410 +** Synchronous is on by default so database corruption is not
 1.50411 +** normally a worry.
 1.50412 +*/
 1.50413 +SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
 1.50414 +  BtShared *pBt = p->pBt;
 1.50415 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.50416 +  sqlite3BtreeEnter(p);
 1.50417 +  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
 1.50418 +  sqlite3BtreeLeave(p);
 1.50419 +  return SQLITE_OK;
 1.50420 +}
 1.50421 +
 1.50422 +/*
 1.50423 +** Change the way data is synced to disk in order to increase or decrease
 1.50424 +** how well the database resists damage due to OS crashes and power
 1.50425 +** failures.  Level 1 is the same as asynchronous (no syncs() occur and
 1.50426 +** there is a high probability of damage)  Level 2 is the default.  There
 1.50427 +** is a very low but non-zero probability of damage.  Level 3 reduces the
 1.50428 +** probability of damage to near zero but with a write performance reduction.
 1.50429 +*/
 1.50430 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
 1.50431 +SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
 1.50432 +  Btree *p,              /* The btree to set the safety level on */
 1.50433 +  int level,             /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
 1.50434 +  int fullSync,          /* PRAGMA fullfsync. */
 1.50435 +  int ckptFullSync       /* PRAGMA checkpoint_fullfync */
 1.50436 +){
 1.50437 +  BtShared *pBt = p->pBt;
 1.50438 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.50439 +  assert( level>=1 && level<=3 );
 1.50440 +  sqlite3BtreeEnter(p);
 1.50441 +  sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
 1.50442 +  sqlite3BtreeLeave(p);
 1.50443 +  return SQLITE_OK;
 1.50444 +}
 1.50445 +#endif
 1.50446 +
 1.50447 +/*
 1.50448 +** Return TRUE if the given btree is set to safety level 1.  In other
 1.50449 +** words, return TRUE if no sync() occurs on the disk files.
 1.50450 +*/
 1.50451 +SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
 1.50452 +  BtShared *pBt = p->pBt;
 1.50453 +  int rc;
 1.50454 +  assert( sqlite3_mutex_held(p->db->mutex) );  
 1.50455 +  sqlite3BtreeEnter(p);
 1.50456 +  assert( pBt && pBt->pPager );
 1.50457 +  rc = sqlite3PagerNosync(pBt->pPager);
 1.50458 +  sqlite3BtreeLeave(p);
 1.50459 +  return rc;
 1.50460 +}
 1.50461 +
 1.50462 +/*
 1.50463 +** Change the default pages size and the number of reserved bytes per page.
 1.50464 +** Or, if the page size has already been fixed, return SQLITE_READONLY 
 1.50465 +** without changing anything.
 1.50466 +**
 1.50467 +** The page size must be a power of 2 between 512 and 65536.  If the page
 1.50468 +** size supplied does not meet this constraint then the page size is not
 1.50469 +** changed.
 1.50470 +**
 1.50471 +** Page sizes are constrained to be a power of two so that the region
 1.50472 +** of the database file used for locking (beginning at PENDING_BYTE,
 1.50473 +** the first byte past the 1GB boundary, 0x40000000) needs to occur
 1.50474 +** at the beginning of a page.
 1.50475 +**
 1.50476 +** If parameter nReserve is less than zero, then the number of reserved
 1.50477 +** bytes per page is left unchanged.
 1.50478 +**
 1.50479 +** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
 1.50480 +** and autovacuum mode can no longer be changed.
 1.50481 +*/
 1.50482 +SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
 1.50483 +  int rc = SQLITE_OK;
 1.50484 +  BtShared *pBt = p->pBt;
 1.50485 +  assert( nReserve>=-1 && nReserve<=255 );
 1.50486 +  sqlite3BtreeEnter(p);
 1.50487 +  if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
 1.50488 +    sqlite3BtreeLeave(p);
 1.50489 +    return SQLITE_READONLY;
 1.50490 +  }
 1.50491 +  if( nReserve<0 ){
 1.50492 +    nReserve = pBt->pageSize - pBt->usableSize;
 1.50493 +  }
 1.50494 +  assert( nReserve>=0 && nReserve<=255 );
 1.50495 +  if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
 1.50496 +        ((pageSize-1)&pageSize)==0 ){
 1.50497 +    assert( (pageSize & 7)==0 );
 1.50498 +    assert( !pBt->pPage1 && !pBt->pCursor );
 1.50499 +    pBt->pageSize = (u32)pageSize;
 1.50500 +    freeTempSpace(pBt);
 1.50501 +  }
 1.50502 +  rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
 1.50503 +  pBt->usableSize = pBt->pageSize - (u16)nReserve;
 1.50504 +  if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 1.50505 +  sqlite3BtreeLeave(p);
 1.50506 +  return rc;
 1.50507 +}
 1.50508 +
 1.50509 +/*
 1.50510 +** Return the currently defined page size
 1.50511 +*/
 1.50512 +SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
 1.50513 +  return p->pBt->pageSize;
 1.50514 +}
 1.50515 +
 1.50516 +#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
 1.50517 +/*
 1.50518 +** This function is similar to sqlite3BtreeGetReserve(), except that it
 1.50519 +** may only be called if it is guaranteed that the b-tree mutex is already
 1.50520 +** held.
 1.50521 +**
 1.50522 +** This is useful in one special case in the backup API code where it is
 1.50523 +** known that the shared b-tree mutex is held, but the mutex on the 
 1.50524 +** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
 1.50525 +** were to be called, it might collide with some other operation on the
 1.50526 +** database handle that owns *p, causing undefined behaviour.
 1.50527 +*/
 1.50528 +SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
 1.50529 +  assert( sqlite3_mutex_held(p->pBt->mutex) );
 1.50530 +  return p->pBt->pageSize - p->pBt->usableSize;
 1.50531 +}
 1.50532 +#endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
 1.50533 +
 1.50534 +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
 1.50535 +/*
 1.50536 +** Return the number of bytes of space at the end of every page that
 1.50537 +** are intentually left unused.  This is the "reserved" space that is
 1.50538 +** sometimes used by extensions.
 1.50539 +*/
 1.50540 +SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
 1.50541 +  int n;
 1.50542 +  sqlite3BtreeEnter(p);
 1.50543 +  n = p->pBt->pageSize - p->pBt->usableSize;
 1.50544 +  sqlite3BtreeLeave(p);
 1.50545 +  return n;
 1.50546 +}
 1.50547 +
 1.50548 +/*
 1.50549 +** Set the maximum page count for a database if mxPage is positive.
 1.50550 +** No changes are made if mxPage is 0 or negative.
 1.50551 +** Regardless of the value of mxPage, return the maximum page count.
 1.50552 +*/
 1.50553 +SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
 1.50554 +  int n;
 1.50555 +  sqlite3BtreeEnter(p);
 1.50556 +  n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
 1.50557 +  sqlite3BtreeLeave(p);
 1.50558 +  return n;
 1.50559 +}
 1.50560 +
 1.50561 +/*
 1.50562 +** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
 1.50563 +** then make no changes.  Always return the value of the BTS_SECURE_DELETE
 1.50564 +** setting after the change.
 1.50565 +*/
 1.50566 +SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
 1.50567 +  int b;
 1.50568 +  if( p==0 ) return 0;
 1.50569 +  sqlite3BtreeEnter(p);
 1.50570 +  if( newFlag>=0 ){
 1.50571 +    p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
 1.50572 +    if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
 1.50573 +  } 
 1.50574 +  b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
 1.50575 +  sqlite3BtreeLeave(p);
 1.50576 +  return b;
 1.50577 +}
 1.50578 +#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
 1.50579 +
 1.50580 +/*
 1.50581 +** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
 1.50582 +** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
 1.50583 +** is disabled. The default value for the auto-vacuum property is 
 1.50584 +** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
 1.50585 +*/
 1.50586 +SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
 1.50587 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.50588 +  return SQLITE_READONLY;
 1.50589 +#else
 1.50590 +  BtShared *pBt = p->pBt;
 1.50591 +  int rc = SQLITE_OK;
 1.50592 +  u8 av = (u8)autoVacuum;
 1.50593 +
 1.50594 +  sqlite3BtreeEnter(p);
 1.50595 +  if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
 1.50596 +    rc = SQLITE_READONLY;
 1.50597 +  }else{
 1.50598 +    pBt->autoVacuum = av ?1:0;
 1.50599 +    pBt->incrVacuum = av==2 ?1:0;
 1.50600 +  }
 1.50601 +  sqlite3BtreeLeave(p);
 1.50602 +  return rc;
 1.50603 +#endif
 1.50604 +}
 1.50605 +
 1.50606 +/*
 1.50607 +** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
 1.50608 +** enabled 1 is returned. Otherwise 0.
 1.50609 +*/
 1.50610 +SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
 1.50611 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.50612 +  return BTREE_AUTOVACUUM_NONE;
 1.50613 +#else
 1.50614 +  int rc;
 1.50615 +  sqlite3BtreeEnter(p);
 1.50616 +  rc = (
 1.50617 +    (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
 1.50618 +    (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
 1.50619 +    BTREE_AUTOVACUUM_INCR
 1.50620 +  );
 1.50621 +  sqlite3BtreeLeave(p);
 1.50622 +  return rc;
 1.50623 +#endif
 1.50624 +}
 1.50625 +
 1.50626 +
 1.50627 +/*
 1.50628 +** Get a reference to pPage1 of the database file.  This will
 1.50629 +** also acquire a readlock on that file.
 1.50630 +**
 1.50631 +** SQLITE_OK is returned on success.  If the file is not a
 1.50632 +** well-formed database file, then SQLITE_CORRUPT is returned.
 1.50633 +** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
 1.50634 +** is returned if we run out of memory. 
 1.50635 +*/
 1.50636 +static int lockBtree(BtShared *pBt){
 1.50637 +  int rc;              /* Result code from subfunctions */
 1.50638 +  MemPage *pPage1;     /* Page 1 of the database file */
 1.50639 +  int nPage;           /* Number of pages in the database */
 1.50640 +  int nPageFile = 0;   /* Number of pages in the database file */
 1.50641 +  int nPageHeader;     /* Number of pages in the database according to hdr */
 1.50642 +
 1.50643 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.50644 +  assert( pBt->pPage1==0 );
 1.50645 +  rc = sqlite3PagerSharedLock(pBt->pPager);
 1.50646 +  if( rc!=SQLITE_OK ) return rc;
 1.50647 +  rc = btreeGetPage(pBt, 1, &pPage1, 0);
 1.50648 +  if( rc!=SQLITE_OK ) return rc;
 1.50649 +
 1.50650 +  /* Do some checking to help insure the file we opened really is
 1.50651 +  ** a valid database file. 
 1.50652 +  */
 1.50653 +  nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
 1.50654 +  sqlite3PagerPagecount(pBt->pPager, &nPageFile);
 1.50655 +  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
 1.50656 +    nPage = nPageFile;
 1.50657 +  }
 1.50658 +  if( nPage>0 ){
 1.50659 +    u32 pageSize;
 1.50660 +    u32 usableSize;
 1.50661 +    u8 *page1 = pPage1->aData;
 1.50662 +    rc = SQLITE_NOTADB;
 1.50663 +    if( memcmp(page1, zMagicHeader, 16)!=0 ){
 1.50664 +      goto page1_init_failed;
 1.50665 +    }
 1.50666 +
 1.50667 +#ifdef SQLITE_OMIT_WAL
 1.50668 +    if( page1[18]>1 ){
 1.50669 +      pBt->btsFlags |= BTS_READ_ONLY;
 1.50670 +    }
 1.50671 +    if( page1[19]>1 ){
 1.50672 +      goto page1_init_failed;
 1.50673 +    }
 1.50674 +#else
 1.50675 +    if( page1[18]>2 ){
 1.50676 +      pBt->btsFlags |= BTS_READ_ONLY;
 1.50677 +    }
 1.50678 +    if( page1[19]>2 ){
 1.50679 +      goto page1_init_failed;
 1.50680 +    }
 1.50681 +
 1.50682 +    /* If the write version is set to 2, this database should be accessed
 1.50683 +    ** in WAL mode. If the log is not already open, open it now. Then 
 1.50684 +    ** return SQLITE_OK and return without populating BtShared.pPage1.
 1.50685 +    ** The caller detects this and calls this function again. This is
 1.50686 +    ** required as the version of page 1 currently in the page1 buffer
 1.50687 +    ** may not be the latest version - there may be a newer one in the log
 1.50688 +    ** file.
 1.50689 +    */
 1.50690 +    if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
 1.50691 +      int isOpen = 0;
 1.50692 +      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
 1.50693 +      if( rc!=SQLITE_OK ){
 1.50694 +        goto page1_init_failed;
 1.50695 +      }else if( isOpen==0 ){
 1.50696 +        releasePage(pPage1);
 1.50697 +        return SQLITE_OK;
 1.50698 +      }
 1.50699 +      rc = SQLITE_NOTADB;
 1.50700 +    }
 1.50701 +#endif
 1.50702 +
 1.50703 +    /* The maximum embedded fraction must be exactly 25%.  And the minimum
 1.50704 +    ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
 1.50705 +    ** The original design allowed these amounts to vary, but as of
 1.50706 +    ** version 3.6.0, we require them to be fixed.
 1.50707 +    */
 1.50708 +    if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
 1.50709 +      goto page1_init_failed;
 1.50710 +    }
 1.50711 +    pageSize = (page1[16]<<8) | (page1[17]<<16);
 1.50712 +    if( ((pageSize-1)&pageSize)!=0
 1.50713 +     || pageSize>SQLITE_MAX_PAGE_SIZE 
 1.50714 +     || pageSize<=256 
 1.50715 +    ){
 1.50716 +      goto page1_init_failed;
 1.50717 +    }
 1.50718 +    assert( (pageSize & 7)==0 );
 1.50719 +    usableSize = pageSize - page1[20];
 1.50720 +    if( (u32)pageSize!=pBt->pageSize ){
 1.50721 +      /* After reading the first page of the database assuming a page size
 1.50722 +      ** of BtShared.pageSize, we have discovered that the page-size is
 1.50723 +      ** actually pageSize. Unlock the database, leave pBt->pPage1 at
 1.50724 +      ** zero and return SQLITE_OK. The caller will call this function
 1.50725 +      ** again with the correct page-size.
 1.50726 +      */
 1.50727 +      releasePage(pPage1);
 1.50728 +      pBt->usableSize = usableSize;
 1.50729 +      pBt->pageSize = pageSize;
 1.50730 +      freeTempSpace(pBt);
 1.50731 +      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 1.50732 +                                   pageSize-usableSize);
 1.50733 +      return rc;
 1.50734 +    }
 1.50735 +    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
 1.50736 +      rc = SQLITE_CORRUPT_BKPT;
 1.50737 +      goto page1_init_failed;
 1.50738 +    }
 1.50739 +    if( usableSize<480 ){
 1.50740 +      goto page1_init_failed;
 1.50741 +    }
 1.50742 +    pBt->pageSize = pageSize;
 1.50743 +    pBt->usableSize = usableSize;
 1.50744 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.50745 +    pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
 1.50746 +    pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
 1.50747 +#endif
 1.50748 +  }
 1.50749 +
 1.50750 +  /* maxLocal is the maximum amount of payload to store locally for
 1.50751 +  ** a cell.  Make sure it is small enough so that at least minFanout
 1.50752 +  ** cells can will fit on one page.  We assume a 10-byte page header.
 1.50753 +  ** Besides the payload, the cell must store:
 1.50754 +  **     2-byte pointer to the cell
 1.50755 +  **     4-byte child pointer
 1.50756 +  **     9-byte nKey value
 1.50757 +  **     4-byte nData value
 1.50758 +  **     4-byte overflow page pointer
 1.50759 +  ** So a cell consists of a 2-byte pointer, a header which is as much as
 1.50760 +  ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
 1.50761 +  ** page pointer.
 1.50762 +  */
 1.50763 +  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
 1.50764 +  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
 1.50765 +  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
 1.50766 +  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
 1.50767 +  if( pBt->maxLocal>127 ){
 1.50768 +    pBt->max1bytePayload = 127;
 1.50769 +  }else{
 1.50770 +    pBt->max1bytePayload = (u8)pBt->maxLocal;
 1.50771 +  }
 1.50772 +  assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
 1.50773 +  pBt->pPage1 = pPage1;
 1.50774 +  pBt->nPage = nPage;
 1.50775 +  return SQLITE_OK;
 1.50776 +
 1.50777 +page1_init_failed:
 1.50778 +  releasePage(pPage1);
 1.50779 +  pBt->pPage1 = 0;
 1.50780 +  return rc;
 1.50781 +}
 1.50782 +
 1.50783 +/*
 1.50784 +** If there are no outstanding cursors and we are not in the middle
 1.50785 +** of a transaction but there is a read lock on the database, then
 1.50786 +** this routine unrefs the first page of the database file which 
 1.50787 +** has the effect of releasing the read lock.
 1.50788 +**
 1.50789 +** If there is a transaction in progress, this routine is a no-op.
 1.50790 +*/
 1.50791 +static void unlockBtreeIfUnused(BtShared *pBt){
 1.50792 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.50793 +  assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
 1.50794 +  if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
 1.50795 +    assert( pBt->pPage1->aData );
 1.50796 +    assert( sqlite3PagerRefcount(pBt->pPager)==1 );
 1.50797 +    assert( pBt->pPage1->aData );
 1.50798 +    releasePage(pBt->pPage1);
 1.50799 +    pBt->pPage1 = 0;
 1.50800 +  }
 1.50801 +}
 1.50802 +
 1.50803 +/*
 1.50804 +** If pBt points to an empty file then convert that empty file
 1.50805 +** into a new empty database by initializing the first page of
 1.50806 +** the database.
 1.50807 +*/
 1.50808 +static int newDatabase(BtShared *pBt){
 1.50809 +  MemPage *pP1;
 1.50810 +  unsigned char *data;
 1.50811 +  int rc;
 1.50812 +
 1.50813 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.50814 +  if( pBt->nPage>0 ){
 1.50815 +    return SQLITE_OK;
 1.50816 +  }
 1.50817 +  pP1 = pBt->pPage1;
 1.50818 +  assert( pP1!=0 );
 1.50819 +  data = pP1->aData;
 1.50820 +  rc = sqlite3PagerWrite(pP1->pDbPage);
 1.50821 +  if( rc ) return rc;
 1.50822 +  memcpy(data, zMagicHeader, sizeof(zMagicHeader));
 1.50823 +  assert( sizeof(zMagicHeader)==16 );
 1.50824 +  data[16] = (u8)((pBt->pageSize>>8)&0xff);
 1.50825 +  data[17] = (u8)((pBt->pageSize>>16)&0xff);
 1.50826 +  data[18] = 1;
 1.50827 +  data[19] = 1;
 1.50828 +  assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
 1.50829 +  data[20] = (u8)(pBt->pageSize - pBt->usableSize);
 1.50830 +  data[21] = 64;
 1.50831 +  data[22] = 32;
 1.50832 +  data[23] = 32;
 1.50833 +  memset(&data[24], 0, 100-24);
 1.50834 +  zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
 1.50835 +  pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 1.50836 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.50837 +  assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
 1.50838 +  assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
 1.50839 +  put4byte(&data[36 + 4*4], pBt->autoVacuum);
 1.50840 +  put4byte(&data[36 + 7*4], pBt->incrVacuum);
 1.50841 +#endif
 1.50842 +  pBt->nPage = 1;
 1.50843 +  data[31] = 1;
 1.50844 +  return SQLITE_OK;
 1.50845 +}
 1.50846 +
 1.50847 +/*
 1.50848 +** Initialize the first page of the database file (creating a database
 1.50849 +** consisting of a single page and no schema objects). Return SQLITE_OK
 1.50850 +** if successful, or an SQLite error code otherwise.
 1.50851 +*/
 1.50852 +SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
 1.50853 +  int rc;
 1.50854 +  sqlite3BtreeEnter(p);
 1.50855 +  p->pBt->nPage = 0;
 1.50856 +  rc = newDatabase(p->pBt);
 1.50857 +  sqlite3BtreeLeave(p);
 1.50858 +  return rc;
 1.50859 +}
 1.50860 +
 1.50861 +/*
 1.50862 +** Attempt to start a new transaction. A write-transaction
 1.50863 +** is started if the second argument is nonzero, otherwise a read-
 1.50864 +** transaction.  If the second argument is 2 or more and exclusive
 1.50865 +** transaction is started, meaning that no other process is allowed
 1.50866 +** to access the database.  A preexisting transaction may not be
 1.50867 +** upgraded to exclusive by calling this routine a second time - the
 1.50868 +** exclusivity flag only works for a new transaction.
 1.50869 +**
 1.50870 +** A write-transaction must be started before attempting any 
 1.50871 +** changes to the database.  None of the following routines 
 1.50872 +** will work unless a transaction is started first:
 1.50873 +**
 1.50874 +**      sqlite3BtreeCreateTable()
 1.50875 +**      sqlite3BtreeCreateIndex()
 1.50876 +**      sqlite3BtreeClearTable()
 1.50877 +**      sqlite3BtreeDropTable()
 1.50878 +**      sqlite3BtreeInsert()
 1.50879 +**      sqlite3BtreeDelete()
 1.50880 +**      sqlite3BtreeUpdateMeta()
 1.50881 +**
 1.50882 +** If an initial attempt to acquire the lock fails because of lock contention
 1.50883 +** and the database was previously unlocked, then invoke the busy handler
 1.50884 +** if there is one.  But if there was previously a read-lock, do not
 1.50885 +** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
 1.50886 +** returned when there is already a read-lock in order to avoid a deadlock.
 1.50887 +**
 1.50888 +** Suppose there are two processes A and B.  A has a read lock and B has
 1.50889 +** a reserved lock.  B tries to promote to exclusive but is blocked because
 1.50890 +** of A's read lock.  A tries to promote to reserved but is blocked by B.
 1.50891 +** One or the other of the two processes must give way or there can be
 1.50892 +** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
 1.50893 +** when A already has a read lock, we encourage A to give up and let B
 1.50894 +** proceed.
 1.50895 +*/
 1.50896 +SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
 1.50897 +  sqlite3 *pBlock = 0;
 1.50898 +  BtShared *pBt = p->pBt;
 1.50899 +  int rc = SQLITE_OK;
 1.50900 +
 1.50901 +  sqlite3BtreeEnter(p);
 1.50902 +  btreeIntegrity(p);
 1.50903 +
 1.50904 +  /* If the btree is already in a write-transaction, or it
 1.50905 +  ** is already in a read-transaction and a read-transaction
 1.50906 +  ** is requested, this is a no-op.
 1.50907 +  */
 1.50908 +  if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
 1.50909 +    goto trans_begun;
 1.50910 +  }
 1.50911 +
 1.50912 +  /* Write transactions are not possible on a read-only database */
 1.50913 +  if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
 1.50914 +    rc = SQLITE_READONLY;
 1.50915 +    goto trans_begun;
 1.50916 +  }
 1.50917 +
 1.50918 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.50919 +  /* If another database handle has already opened a write transaction 
 1.50920 +  ** on this shared-btree structure and a second write transaction is
 1.50921 +  ** requested, return SQLITE_LOCKED.
 1.50922 +  */
 1.50923 +  if( (wrflag && pBt->inTransaction==TRANS_WRITE)
 1.50924 +   || (pBt->btsFlags & BTS_PENDING)!=0
 1.50925 +  ){
 1.50926 +    pBlock = pBt->pWriter->db;
 1.50927 +  }else if( wrflag>1 ){
 1.50928 +    BtLock *pIter;
 1.50929 +    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 1.50930 +      if( pIter->pBtree!=p ){
 1.50931 +        pBlock = pIter->pBtree->db;
 1.50932 +        break;
 1.50933 +      }
 1.50934 +    }
 1.50935 +  }
 1.50936 +  if( pBlock ){
 1.50937 +    sqlite3ConnectionBlocked(p->db, pBlock);
 1.50938 +    rc = SQLITE_LOCKED_SHAREDCACHE;
 1.50939 +    goto trans_begun;
 1.50940 +  }
 1.50941 +#endif
 1.50942 +
 1.50943 +  /* Any read-only or read-write transaction implies a read-lock on 
 1.50944 +  ** page 1. So if some other shared-cache client already has a write-lock 
 1.50945 +  ** on page 1, the transaction cannot be opened. */
 1.50946 +  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
 1.50947 +  if( SQLITE_OK!=rc ) goto trans_begun;
 1.50948 +
 1.50949 +  pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
 1.50950 +  if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
 1.50951 +  do {
 1.50952 +    /* Call lockBtree() until either pBt->pPage1 is populated or
 1.50953 +    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
 1.50954 +    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
 1.50955 +    ** reading page 1 it discovers that the page-size of the database 
 1.50956 +    ** file is not pBt->pageSize. In this case lockBtree() will update
 1.50957 +    ** pBt->pageSize to the page-size of the file on disk.
 1.50958 +    */
 1.50959 +    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
 1.50960 +
 1.50961 +    if( rc==SQLITE_OK && wrflag ){
 1.50962 +      if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
 1.50963 +        rc = SQLITE_READONLY;
 1.50964 +      }else{
 1.50965 +        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
 1.50966 +        if( rc==SQLITE_OK ){
 1.50967 +          rc = newDatabase(pBt);
 1.50968 +        }
 1.50969 +      }
 1.50970 +    }
 1.50971 +  
 1.50972 +    if( rc!=SQLITE_OK ){
 1.50973 +      unlockBtreeIfUnused(pBt);
 1.50974 +    }
 1.50975 +  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
 1.50976 +          btreeInvokeBusyHandler(pBt) );
 1.50977 +
 1.50978 +  if( rc==SQLITE_OK ){
 1.50979 +    if( p->inTrans==TRANS_NONE ){
 1.50980 +      pBt->nTransaction++;
 1.50981 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.50982 +      if( p->sharable ){
 1.50983 +        assert( p->lock.pBtree==p && p->lock.iTable==1 );
 1.50984 +        p->lock.eLock = READ_LOCK;
 1.50985 +        p->lock.pNext = pBt->pLock;
 1.50986 +        pBt->pLock = &p->lock;
 1.50987 +      }
 1.50988 +#endif
 1.50989 +    }
 1.50990 +    p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
 1.50991 +    if( p->inTrans>pBt->inTransaction ){
 1.50992 +      pBt->inTransaction = p->inTrans;
 1.50993 +    }
 1.50994 +    if( wrflag ){
 1.50995 +      MemPage *pPage1 = pBt->pPage1;
 1.50996 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.50997 +      assert( !pBt->pWriter );
 1.50998 +      pBt->pWriter = p;
 1.50999 +      pBt->btsFlags &= ~BTS_EXCLUSIVE;
 1.51000 +      if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
 1.51001 +#endif
 1.51002 +
 1.51003 +      /* If the db-size header field is incorrect (as it may be if an old
 1.51004 +      ** client has been writing the database file), update it now. Doing
 1.51005 +      ** this sooner rather than later means the database size can safely 
 1.51006 +      ** re-read the database size from page 1 if a savepoint or transaction
 1.51007 +      ** rollback occurs within the transaction.
 1.51008 +      */
 1.51009 +      if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
 1.51010 +        rc = sqlite3PagerWrite(pPage1->pDbPage);
 1.51011 +        if( rc==SQLITE_OK ){
 1.51012 +          put4byte(&pPage1->aData[28], pBt->nPage);
 1.51013 +        }
 1.51014 +      }
 1.51015 +    }
 1.51016 +  }
 1.51017 +
 1.51018 +
 1.51019 +trans_begun:
 1.51020 +  if( rc==SQLITE_OK && wrflag ){
 1.51021 +    /* This call makes sure that the pager has the correct number of
 1.51022 +    ** open savepoints. If the second parameter is greater than 0 and
 1.51023 +    ** the sub-journal is not already open, then it will be opened here.
 1.51024 +    */
 1.51025 +    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
 1.51026 +  }
 1.51027 +
 1.51028 +  btreeIntegrity(p);
 1.51029 +  sqlite3BtreeLeave(p);
 1.51030 +  return rc;
 1.51031 +}
 1.51032 +
 1.51033 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.51034 +
 1.51035 +/*
 1.51036 +** Set the pointer-map entries for all children of page pPage. Also, if
 1.51037 +** pPage contains cells that point to overflow pages, set the pointer
 1.51038 +** map entries for the overflow pages as well.
 1.51039 +*/
 1.51040 +static int setChildPtrmaps(MemPage *pPage){
 1.51041 +  int i;                             /* Counter variable */
 1.51042 +  int nCell;                         /* Number of cells in page pPage */
 1.51043 +  int rc;                            /* Return code */
 1.51044 +  BtShared *pBt = pPage->pBt;
 1.51045 +  u8 isInitOrig = pPage->isInit;
 1.51046 +  Pgno pgno = pPage->pgno;
 1.51047 +
 1.51048 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.51049 +  rc = btreeInitPage(pPage);
 1.51050 +  if( rc!=SQLITE_OK ){
 1.51051 +    goto set_child_ptrmaps_out;
 1.51052 +  }
 1.51053 +  nCell = pPage->nCell;
 1.51054 +
 1.51055 +  for(i=0; i<nCell; i++){
 1.51056 +    u8 *pCell = findCell(pPage, i);
 1.51057 +
 1.51058 +    ptrmapPutOvflPtr(pPage, pCell, &rc);
 1.51059 +
 1.51060 +    if( !pPage->leaf ){
 1.51061 +      Pgno childPgno = get4byte(pCell);
 1.51062 +      ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
 1.51063 +    }
 1.51064 +  }
 1.51065 +
 1.51066 +  if( !pPage->leaf ){
 1.51067 +    Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 1.51068 +    ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
 1.51069 +  }
 1.51070 +
 1.51071 +set_child_ptrmaps_out:
 1.51072 +  pPage->isInit = isInitOrig;
 1.51073 +  return rc;
 1.51074 +}
 1.51075 +
 1.51076 +/*
 1.51077 +** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
 1.51078 +** that it points to iTo. Parameter eType describes the type of pointer to
 1.51079 +** be modified, as  follows:
 1.51080 +**
 1.51081 +** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
 1.51082 +**                   page of pPage.
 1.51083 +**
 1.51084 +** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
 1.51085 +**                   page pointed to by one of the cells on pPage.
 1.51086 +**
 1.51087 +** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
 1.51088 +**                   overflow page in the list.
 1.51089 +*/
 1.51090 +static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
 1.51091 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.51092 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.51093 +  if( eType==PTRMAP_OVERFLOW2 ){
 1.51094 +    /* The pointer is always the first 4 bytes of the page in this case.  */
 1.51095 +    if( get4byte(pPage->aData)!=iFrom ){
 1.51096 +      return SQLITE_CORRUPT_BKPT;
 1.51097 +    }
 1.51098 +    put4byte(pPage->aData, iTo);
 1.51099 +  }else{
 1.51100 +    u8 isInitOrig = pPage->isInit;
 1.51101 +    int i;
 1.51102 +    int nCell;
 1.51103 +
 1.51104 +    btreeInitPage(pPage);
 1.51105 +    nCell = pPage->nCell;
 1.51106 +
 1.51107 +    for(i=0; i<nCell; i++){
 1.51108 +      u8 *pCell = findCell(pPage, i);
 1.51109 +      if( eType==PTRMAP_OVERFLOW1 ){
 1.51110 +        CellInfo info;
 1.51111 +        btreeParseCellPtr(pPage, pCell, &info);
 1.51112 +        if( info.iOverflow
 1.51113 +         && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
 1.51114 +         && iFrom==get4byte(&pCell[info.iOverflow])
 1.51115 +        ){
 1.51116 +          put4byte(&pCell[info.iOverflow], iTo);
 1.51117 +          break;
 1.51118 +        }
 1.51119 +      }else{
 1.51120 +        if( get4byte(pCell)==iFrom ){
 1.51121 +          put4byte(pCell, iTo);
 1.51122 +          break;
 1.51123 +        }
 1.51124 +      }
 1.51125 +    }
 1.51126 +  
 1.51127 +    if( i==nCell ){
 1.51128 +      if( eType!=PTRMAP_BTREE || 
 1.51129 +          get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
 1.51130 +        return SQLITE_CORRUPT_BKPT;
 1.51131 +      }
 1.51132 +      put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
 1.51133 +    }
 1.51134 +
 1.51135 +    pPage->isInit = isInitOrig;
 1.51136 +  }
 1.51137 +  return SQLITE_OK;
 1.51138 +}
 1.51139 +
 1.51140 +
 1.51141 +/*
 1.51142 +** Move the open database page pDbPage to location iFreePage in the 
 1.51143 +** database. The pDbPage reference remains valid.
 1.51144 +**
 1.51145 +** The isCommit flag indicates that there is no need to remember that
 1.51146 +** the journal needs to be sync()ed before database page pDbPage->pgno 
 1.51147 +** can be written to. The caller has already promised not to write to that
 1.51148 +** page.
 1.51149 +*/
 1.51150 +static int relocatePage(
 1.51151 +  BtShared *pBt,           /* Btree */
 1.51152 +  MemPage *pDbPage,        /* Open page to move */
 1.51153 +  u8 eType,                /* Pointer map 'type' entry for pDbPage */
 1.51154 +  Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
 1.51155 +  Pgno iFreePage,          /* The location to move pDbPage to */
 1.51156 +  int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
 1.51157 +){
 1.51158 +  MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
 1.51159 +  Pgno iDbPage = pDbPage->pgno;
 1.51160 +  Pager *pPager = pBt->pPager;
 1.51161 +  int rc;
 1.51162 +
 1.51163 +  assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
 1.51164 +      eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
 1.51165 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.51166 +  assert( pDbPage->pBt==pBt );
 1.51167 +
 1.51168 +  /* Move page iDbPage from its current location to page number iFreePage */
 1.51169 +  TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
 1.51170 +      iDbPage, iFreePage, iPtrPage, eType));
 1.51171 +  rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
 1.51172 +  if( rc!=SQLITE_OK ){
 1.51173 +    return rc;
 1.51174 +  }
 1.51175 +  pDbPage->pgno = iFreePage;
 1.51176 +
 1.51177 +  /* If pDbPage was a btree-page, then it may have child pages and/or cells
 1.51178 +  ** that point to overflow pages. The pointer map entries for all these
 1.51179 +  ** pages need to be changed.
 1.51180 +  **
 1.51181 +  ** If pDbPage is an overflow page, then the first 4 bytes may store a
 1.51182 +  ** pointer to a subsequent overflow page. If this is the case, then
 1.51183 +  ** the pointer map needs to be updated for the subsequent overflow page.
 1.51184 +  */
 1.51185 +  if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
 1.51186 +    rc = setChildPtrmaps(pDbPage);
 1.51187 +    if( rc!=SQLITE_OK ){
 1.51188 +      return rc;
 1.51189 +    }
 1.51190 +  }else{
 1.51191 +    Pgno nextOvfl = get4byte(pDbPage->aData);
 1.51192 +    if( nextOvfl!=0 ){
 1.51193 +      ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
 1.51194 +      if( rc!=SQLITE_OK ){
 1.51195 +        return rc;
 1.51196 +      }
 1.51197 +    }
 1.51198 +  }
 1.51199 +
 1.51200 +  /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
 1.51201 +  ** that it points at iFreePage. Also fix the pointer map entry for
 1.51202 +  ** iPtrPage.
 1.51203 +  */
 1.51204 +  if( eType!=PTRMAP_ROOTPAGE ){
 1.51205 +    rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
 1.51206 +    if( rc!=SQLITE_OK ){
 1.51207 +      return rc;
 1.51208 +    }
 1.51209 +    rc = sqlite3PagerWrite(pPtrPage->pDbPage);
 1.51210 +    if( rc!=SQLITE_OK ){
 1.51211 +      releasePage(pPtrPage);
 1.51212 +      return rc;
 1.51213 +    }
 1.51214 +    rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
 1.51215 +    releasePage(pPtrPage);
 1.51216 +    if( rc==SQLITE_OK ){
 1.51217 +      ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
 1.51218 +    }
 1.51219 +  }
 1.51220 +  return rc;
 1.51221 +}
 1.51222 +
 1.51223 +/* Forward declaration required by incrVacuumStep(). */
 1.51224 +static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
 1.51225 +
 1.51226 +/*
 1.51227 +** Perform a single step of an incremental-vacuum. If successful,
 1.51228 +** return SQLITE_OK. If there is no work to do (and therefore no
 1.51229 +** point in calling this function again), return SQLITE_DONE.
 1.51230 +**
 1.51231 +** More specificly, this function attempts to re-organize the 
 1.51232 +** database so that the last page of the file currently in use
 1.51233 +** is no longer in use.
 1.51234 +**
 1.51235 +** If the nFin parameter is non-zero, this function assumes
 1.51236 +** that the caller will keep calling incrVacuumStep() until
 1.51237 +** it returns SQLITE_DONE or an error, and that nFin is the
 1.51238 +** number of pages the database file will contain after this 
 1.51239 +** process is complete.  If nFin is zero, it is assumed that
 1.51240 +** incrVacuumStep() will be called a finite amount of times
 1.51241 +** which may or may not empty the freelist.  A full autovacuum
 1.51242 +** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
 1.51243 +*/
 1.51244 +static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
 1.51245 +  Pgno nFreeList;           /* Number of pages still on the free-list */
 1.51246 +  int rc;
 1.51247 +
 1.51248 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.51249 +  assert( iLastPg>nFin );
 1.51250 +
 1.51251 +  if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
 1.51252 +    u8 eType;
 1.51253 +    Pgno iPtrPage;
 1.51254 +
 1.51255 +    nFreeList = get4byte(&pBt->pPage1->aData[36]);
 1.51256 +    if( nFreeList==0 ){
 1.51257 +      return SQLITE_DONE;
 1.51258 +    }
 1.51259 +
 1.51260 +    rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
 1.51261 +    if( rc!=SQLITE_OK ){
 1.51262 +      return rc;
 1.51263 +    }
 1.51264 +    if( eType==PTRMAP_ROOTPAGE ){
 1.51265 +      return SQLITE_CORRUPT_BKPT;
 1.51266 +    }
 1.51267 +
 1.51268 +    if( eType==PTRMAP_FREEPAGE ){
 1.51269 +      if( nFin==0 ){
 1.51270 +        /* Remove the page from the files free-list. This is not required
 1.51271 +        ** if nFin is non-zero. In that case, the free-list will be
 1.51272 +        ** truncated to zero after this function returns, so it doesn't 
 1.51273 +        ** matter if it still contains some garbage entries.
 1.51274 +        */
 1.51275 +        Pgno iFreePg;
 1.51276 +        MemPage *pFreePg;
 1.51277 +        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
 1.51278 +        if( rc!=SQLITE_OK ){
 1.51279 +          return rc;
 1.51280 +        }
 1.51281 +        assert( iFreePg==iLastPg );
 1.51282 +        releasePage(pFreePg);
 1.51283 +      }
 1.51284 +    } else {
 1.51285 +      Pgno iFreePg;             /* Index of free page to move pLastPg to */
 1.51286 +      MemPage *pLastPg;
 1.51287 +
 1.51288 +      rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
 1.51289 +      if( rc!=SQLITE_OK ){
 1.51290 +        return rc;
 1.51291 +      }
 1.51292 +
 1.51293 +      /* If nFin is zero, this loop runs exactly once and page pLastPg
 1.51294 +      ** is swapped with the first free page pulled off the free list.
 1.51295 +      **
 1.51296 +      ** On the other hand, if nFin is greater than zero, then keep
 1.51297 +      ** looping until a free-page located within the first nFin pages
 1.51298 +      ** of the file is found.
 1.51299 +      */
 1.51300 +      do {
 1.51301 +        MemPage *pFreePg;
 1.51302 +        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
 1.51303 +        if( rc!=SQLITE_OK ){
 1.51304 +          releasePage(pLastPg);
 1.51305 +          return rc;
 1.51306 +        }
 1.51307 +        releasePage(pFreePg);
 1.51308 +      }while( nFin!=0 && iFreePg>nFin );
 1.51309 +      assert( iFreePg<iLastPg );
 1.51310 +      
 1.51311 +      rc = sqlite3PagerWrite(pLastPg->pDbPage);
 1.51312 +      if( rc==SQLITE_OK ){
 1.51313 +        rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
 1.51314 +      }
 1.51315 +      releasePage(pLastPg);
 1.51316 +      if( rc!=SQLITE_OK ){
 1.51317 +        return rc;
 1.51318 +      }
 1.51319 +    }
 1.51320 +  }
 1.51321 +
 1.51322 +  if( nFin==0 ){
 1.51323 +    iLastPg--;
 1.51324 +    while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
 1.51325 +      if( PTRMAP_ISPAGE(pBt, iLastPg) ){
 1.51326 +        MemPage *pPg;
 1.51327 +        rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
 1.51328 +        if( rc!=SQLITE_OK ){
 1.51329 +          return rc;
 1.51330 +        }
 1.51331 +        rc = sqlite3PagerWrite(pPg->pDbPage);
 1.51332 +        releasePage(pPg);
 1.51333 +        if( rc!=SQLITE_OK ){
 1.51334 +          return rc;
 1.51335 +        }
 1.51336 +      }
 1.51337 +      iLastPg--;
 1.51338 +    }
 1.51339 +    sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
 1.51340 +    pBt->nPage = iLastPg;
 1.51341 +  }
 1.51342 +  return SQLITE_OK;
 1.51343 +}
 1.51344 +
 1.51345 +/*
 1.51346 +** A write-transaction must be opened before calling this function.
 1.51347 +** It performs a single unit of work towards an incremental vacuum.
 1.51348 +**
 1.51349 +** If the incremental vacuum is finished after this function has run,
 1.51350 +** SQLITE_DONE is returned. If it is not finished, but no error occurred,
 1.51351 +** SQLITE_OK is returned. Otherwise an SQLite error code. 
 1.51352 +*/
 1.51353 +SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
 1.51354 +  int rc;
 1.51355 +  BtShared *pBt = p->pBt;
 1.51356 +
 1.51357 +  sqlite3BtreeEnter(p);
 1.51358 +  assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
 1.51359 +  if( !pBt->autoVacuum ){
 1.51360 +    rc = SQLITE_DONE;
 1.51361 +  }else{
 1.51362 +    invalidateAllOverflowCache(pBt);
 1.51363 +    rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
 1.51364 +    if( rc==SQLITE_OK ){
 1.51365 +      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 1.51366 +      put4byte(&pBt->pPage1->aData[28], pBt->nPage);
 1.51367 +    }
 1.51368 +  }
 1.51369 +  sqlite3BtreeLeave(p);
 1.51370 +  return rc;
 1.51371 +}
 1.51372 +
 1.51373 +/*
 1.51374 +** This routine is called prior to sqlite3PagerCommit when a transaction
 1.51375 +** is commited for an auto-vacuum database.
 1.51376 +**
 1.51377 +** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
 1.51378 +** the database file should be truncated to during the commit process. 
 1.51379 +** i.e. the database has been reorganized so that only the first *pnTrunc
 1.51380 +** pages are in use.
 1.51381 +*/
 1.51382 +static int autoVacuumCommit(BtShared *pBt){
 1.51383 +  int rc = SQLITE_OK;
 1.51384 +  Pager *pPager = pBt->pPager;
 1.51385 +  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
 1.51386 +
 1.51387 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.51388 +  invalidateAllOverflowCache(pBt);
 1.51389 +  assert(pBt->autoVacuum);
 1.51390 +  if( !pBt->incrVacuum ){
 1.51391 +    Pgno nFin;         /* Number of pages in database after autovacuuming */
 1.51392 +    Pgno nFree;        /* Number of pages on the freelist initially */
 1.51393 +    Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
 1.51394 +    Pgno iFree;        /* The next page to be freed */
 1.51395 +    int nEntry;        /* Number of entries on one ptrmap page */
 1.51396 +    Pgno nOrig;        /* Database size before freeing */
 1.51397 +
 1.51398 +    nOrig = btreePagecount(pBt);
 1.51399 +    if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
 1.51400 +      /* It is not possible to create a database for which the final page
 1.51401 +      ** is either a pointer-map page or the pending-byte page. If one
 1.51402 +      ** is encountered, this indicates corruption.
 1.51403 +      */
 1.51404 +      return SQLITE_CORRUPT_BKPT;
 1.51405 +    }
 1.51406 +
 1.51407 +    nFree = get4byte(&pBt->pPage1->aData[36]);
 1.51408 +    nEntry = pBt->usableSize/5;
 1.51409 +    nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
 1.51410 +    nFin = nOrig - nFree - nPtrmap;
 1.51411 +    if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
 1.51412 +      nFin--;
 1.51413 +    }
 1.51414 +    while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
 1.51415 +      nFin--;
 1.51416 +    }
 1.51417 +    if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
 1.51418 +
 1.51419 +    for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
 1.51420 +      rc = incrVacuumStep(pBt, nFin, iFree);
 1.51421 +    }
 1.51422 +    if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
 1.51423 +      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 1.51424 +      put4byte(&pBt->pPage1->aData[32], 0);
 1.51425 +      put4byte(&pBt->pPage1->aData[36], 0);
 1.51426 +      put4byte(&pBt->pPage1->aData[28], nFin);
 1.51427 +      sqlite3PagerTruncateImage(pBt->pPager, nFin);
 1.51428 +      pBt->nPage = nFin;
 1.51429 +    }
 1.51430 +    if( rc!=SQLITE_OK ){
 1.51431 +      sqlite3PagerRollback(pPager);
 1.51432 +    }
 1.51433 +  }
 1.51434 +
 1.51435 +  assert( nRef==sqlite3PagerRefcount(pPager) );
 1.51436 +  return rc;
 1.51437 +}
 1.51438 +
 1.51439 +#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
 1.51440 +# define setChildPtrmaps(x) SQLITE_OK
 1.51441 +#endif
 1.51442 +
 1.51443 +/*
 1.51444 +** This routine does the first phase of a two-phase commit.  This routine
 1.51445 +** causes a rollback journal to be created (if it does not already exist)
 1.51446 +** and populated with enough information so that if a power loss occurs
 1.51447 +** the database can be restored to its original state by playing back
 1.51448 +** the journal.  Then the contents of the journal are flushed out to
 1.51449 +** the disk.  After the journal is safely on oxide, the changes to the
 1.51450 +** database are written into the database file and flushed to oxide.
 1.51451 +** At the end of this call, the rollback journal still exists on the
 1.51452 +** disk and we are still holding all locks, so the transaction has not
 1.51453 +** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
 1.51454 +** commit process.
 1.51455 +**
 1.51456 +** This call is a no-op if no write-transaction is currently active on pBt.
 1.51457 +**
 1.51458 +** Otherwise, sync the database file for the btree pBt. zMaster points to
 1.51459 +** the name of a master journal file that should be written into the
 1.51460 +** individual journal file, or is NULL, indicating no master journal file 
 1.51461 +** (single database transaction).
 1.51462 +**
 1.51463 +** When this is called, the master journal should already have been
 1.51464 +** created, populated with this journal pointer and synced to disk.
 1.51465 +**
 1.51466 +** Once this is routine has returned, the only thing required to commit
 1.51467 +** the write-transaction for this database file is to delete the journal.
 1.51468 +*/
 1.51469 +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
 1.51470 +  int rc = SQLITE_OK;
 1.51471 +  if( p->inTrans==TRANS_WRITE ){
 1.51472 +    BtShared *pBt = p->pBt;
 1.51473 +    sqlite3BtreeEnter(p);
 1.51474 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.51475 +    if( pBt->autoVacuum ){
 1.51476 +      rc = autoVacuumCommit(pBt);
 1.51477 +      if( rc!=SQLITE_OK ){
 1.51478 +        sqlite3BtreeLeave(p);
 1.51479 +        return rc;
 1.51480 +      }
 1.51481 +    }
 1.51482 +#endif
 1.51483 +    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
 1.51484 +    sqlite3BtreeLeave(p);
 1.51485 +  }
 1.51486 +  return rc;
 1.51487 +}
 1.51488 +
 1.51489 +/*
 1.51490 +** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
 1.51491 +** at the conclusion of a transaction.
 1.51492 +*/
 1.51493 +static void btreeEndTransaction(Btree *p){
 1.51494 +  BtShared *pBt = p->pBt;
 1.51495 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.51496 +
 1.51497 +  btreeClearHasContent(pBt);
 1.51498 +  if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
 1.51499 +    /* If there are other active statements that belong to this database
 1.51500 +    ** handle, downgrade to a read-only transaction. The other statements
 1.51501 +    ** may still be reading from the database.  */
 1.51502 +    downgradeAllSharedCacheTableLocks(p);
 1.51503 +    p->inTrans = TRANS_READ;
 1.51504 +  }else{
 1.51505 +    /* If the handle had any kind of transaction open, decrement the 
 1.51506 +    ** transaction count of the shared btree. If the transaction count 
 1.51507 +    ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
 1.51508 +    ** call below will unlock the pager.  */
 1.51509 +    if( p->inTrans!=TRANS_NONE ){
 1.51510 +      clearAllSharedCacheTableLocks(p);
 1.51511 +      pBt->nTransaction--;
 1.51512 +      if( 0==pBt->nTransaction ){
 1.51513 +        pBt->inTransaction = TRANS_NONE;
 1.51514 +      }
 1.51515 +    }
 1.51516 +
 1.51517 +    /* Set the current transaction state to TRANS_NONE and unlock the 
 1.51518 +    ** pager if this call closed the only read or write transaction.  */
 1.51519 +    p->inTrans = TRANS_NONE;
 1.51520 +    unlockBtreeIfUnused(pBt);
 1.51521 +  }
 1.51522 +
 1.51523 +  btreeIntegrity(p);
 1.51524 +}
 1.51525 +
 1.51526 +/*
 1.51527 +** Commit the transaction currently in progress.
 1.51528 +**
 1.51529 +** This routine implements the second phase of a 2-phase commit.  The
 1.51530 +** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
 1.51531 +** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
 1.51532 +** routine did all the work of writing information out to disk and flushing the
 1.51533 +** contents so that they are written onto the disk platter.  All this
 1.51534 +** routine has to do is delete or truncate or zero the header in the
 1.51535 +** the rollback journal (which causes the transaction to commit) and
 1.51536 +** drop locks.
 1.51537 +**
 1.51538 +** Normally, if an error occurs while the pager layer is attempting to 
 1.51539 +** finalize the underlying journal file, this function returns an error and
 1.51540 +** the upper layer will attempt a rollback. However, if the second argument
 1.51541 +** is non-zero then this b-tree transaction is part of a multi-file 
 1.51542 +** transaction. In this case, the transaction has already been committed 
 1.51543 +** (by deleting a master journal file) and the caller will ignore this 
 1.51544 +** functions return code. So, even if an error occurs in the pager layer,
 1.51545 +** reset the b-tree objects internal state to indicate that the write
 1.51546 +** transaction has been closed. This is quite safe, as the pager will have
 1.51547 +** transitioned to the error state.
 1.51548 +**
 1.51549 +** This will release the write lock on the database file.  If there
 1.51550 +** are no active cursors, it also releases the read lock.
 1.51551 +*/
 1.51552 +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
 1.51553 +
 1.51554 +  if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
 1.51555 +  sqlite3BtreeEnter(p);
 1.51556 +  btreeIntegrity(p);
 1.51557 +
 1.51558 +  /* If the handle has a write-transaction open, commit the shared-btrees 
 1.51559 +  ** transaction and set the shared state to TRANS_READ.
 1.51560 +  */
 1.51561 +  if( p->inTrans==TRANS_WRITE ){
 1.51562 +    int rc;
 1.51563 +    BtShared *pBt = p->pBt;
 1.51564 +    assert( pBt->inTransaction==TRANS_WRITE );
 1.51565 +    assert( pBt->nTransaction>0 );
 1.51566 +    rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
 1.51567 +    if( rc!=SQLITE_OK && bCleanup==0 ){
 1.51568 +      sqlite3BtreeLeave(p);
 1.51569 +      return rc;
 1.51570 +    }
 1.51571 +    pBt->inTransaction = TRANS_READ;
 1.51572 +  }
 1.51573 +
 1.51574 +  btreeEndTransaction(p);
 1.51575 +  sqlite3BtreeLeave(p);
 1.51576 +  return SQLITE_OK;
 1.51577 +}
 1.51578 +
 1.51579 +/*
 1.51580 +** Do both phases of a commit.
 1.51581 +*/
 1.51582 +SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
 1.51583 +  int rc;
 1.51584 +  sqlite3BtreeEnter(p);
 1.51585 +  rc = sqlite3BtreeCommitPhaseOne(p, 0);
 1.51586 +  if( rc==SQLITE_OK ){
 1.51587 +    rc = sqlite3BtreeCommitPhaseTwo(p, 0);
 1.51588 +  }
 1.51589 +  sqlite3BtreeLeave(p);
 1.51590 +  return rc;
 1.51591 +}
 1.51592 +
 1.51593 +#ifndef NDEBUG
 1.51594 +/*
 1.51595 +** Return the number of write-cursors open on this handle. This is for use
 1.51596 +** in assert() expressions, so it is only compiled if NDEBUG is not
 1.51597 +** defined.
 1.51598 +**
 1.51599 +** For the purposes of this routine, a write-cursor is any cursor that
 1.51600 +** is capable of writing to the databse.  That means the cursor was
 1.51601 +** originally opened for writing and the cursor has not be disabled
 1.51602 +** by having its state changed to CURSOR_FAULT.
 1.51603 +*/
 1.51604 +static int countWriteCursors(BtShared *pBt){
 1.51605 +  BtCursor *pCur;
 1.51606 +  int r = 0;
 1.51607 +  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
 1.51608 +    if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; 
 1.51609 +  }
 1.51610 +  return r;
 1.51611 +}
 1.51612 +#endif
 1.51613 +
 1.51614 +/*
 1.51615 +** This routine sets the state to CURSOR_FAULT and the error
 1.51616 +** code to errCode for every cursor on BtShared that pBtree
 1.51617 +** references.
 1.51618 +**
 1.51619 +** Every cursor is tripped, including cursors that belong
 1.51620 +** to other database connections that happen to be sharing
 1.51621 +** the cache with pBtree.
 1.51622 +**
 1.51623 +** This routine gets called when a rollback occurs.
 1.51624 +** All cursors using the same cache must be tripped
 1.51625 +** to prevent them from trying to use the btree after
 1.51626 +** the rollback.  The rollback may have deleted tables
 1.51627 +** or moved root pages, so it is not sufficient to
 1.51628 +** save the state of the cursor.  The cursor must be
 1.51629 +** invalidated.
 1.51630 +*/
 1.51631 +SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
 1.51632 +  BtCursor *p;
 1.51633 +  if( pBtree==0 ) return;
 1.51634 +  sqlite3BtreeEnter(pBtree);
 1.51635 +  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 1.51636 +    int i;
 1.51637 +    sqlite3BtreeClearCursor(p);
 1.51638 +    p->eState = CURSOR_FAULT;
 1.51639 +    p->skipNext = errCode;
 1.51640 +    for(i=0; i<=p->iPage; i++){
 1.51641 +      releasePage(p->apPage[i]);
 1.51642 +      p->apPage[i] = 0;
 1.51643 +    }
 1.51644 +  }
 1.51645 +  sqlite3BtreeLeave(pBtree);
 1.51646 +}
 1.51647 +
 1.51648 +/*
 1.51649 +** Rollback the transaction in progress.  All cursors will be
 1.51650 +** invalided by this operation.  Any attempt to use a cursor
 1.51651 +** that was open at the beginning of this operation will result
 1.51652 +** in an error.
 1.51653 +**
 1.51654 +** This will release the write lock on the database file.  If there
 1.51655 +** are no active cursors, it also releases the read lock.
 1.51656 +*/
 1.51657 +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){
 1.51658 +  int rc;
 1.51659 +  BtShared *pBt = p->pBt;
 1.51660 +  MemPage *pPage1;
 1.51661 +
 1.51662 +  sqlite3BtreeEnter(p);
 1.51663 +  if( tripCode==SQLITE_OK ){
 1.51664 +    rc = tripCode = saveAllCursors(pBt, 0, 0);
 1.51665 +  }else{
 1.51666 +    rc = SQLITE_OK;
 1.51667 +  }
 1.51668 +  if( tripCode ){
 1.51669 +    sqlite3BtreeTripAllCursors(p, tripCode);
 1.51670 +  }
 1.51671 +  btreeIntegrity(p);
 1.51672 +
 1.51673 +  if( p->inTrans==TRANS_WRITE ){
 1.51674 +    int rc2;
 1.51675 +
 1.51676 +    assert( TRANS_WRITE==pBt->inTransaction );
 1.51677 +    rc2 = sqlite3PagerRollback(pBt->pPager);
 1.51678 +    if( rc2!=SQLITE_OK ){
 1.51679 +      rc = rc2;
 1.51680 +    }
 1.51681 +
 1.51682 +    /* The rollback may have destroyed the pPage1->aData value.  So
 1.51683 +    ** call btreeGetPage() on page 1 again to make
 1.51684 +    ** sure pPage1->aData is set correctly. */
 1.51685 +    if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
 1.51686 +      int nPage = get4byte(28+(u8*)pPage1->aData);
 1.51687 +      testcase( nPage==0 );
 1.51688 +      if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
 1.51689 +      testcase( pBt->nPage!=nPage );
 1.51690 +      pBt->nPage = nPage;
 1.51691 +      releasePage(pPage1);
 1.51692 +    }
 1.51693 +    assert( countWriteCursors(pBt)==0 );
 1.51694 +    pBt->inTransaction = TRANS_READ;
 1.51695 +  }
 1.51696 +
 1.51697 +  btreeEndTransaction(p);
 1.51698 +  sqlite3BtreeLeave(p);
 1.51699 +  return rc;
 1.51700 +}
 1.51701 +
 1.51702 +/*
 1.51703 +** Start a statement subtransaction. The subtransaction can can be rolled
 1.51704 +** back independently of the main transaction. You must start a transaction 
 1.51705 +** before starting a subtransaction. The subtransaction is ended automatically 
 1.51706 +** if the main transaction commits or rolls back.
 1.51707 +**
 1.51708 +** Statement subtransactions are used around individual SQL statements
 1.51709 +** that are contained within a BEGIN...COMMIT block.  If a constraint
 1.51710 +** error occurs within the statement, the effect of that one statement
 1.51711 +** can be rolled back without having to rollback the entire transaction.
 1.51712 +**
 1.51713 +** A statement sub-transaction is implemented as an anonymous savepoint. The
 1.51714 +** value passed as the second parameter is the total number of savepoints,
 1.51715 +** including the new anonymous savepoint, open on the B-Tree. i.e. if there
 1.51716 +** are no active savepoints and no other statement-transactions open,
 1.51717 +** iStatement is 1. This anonymous savepoint can be released or rolled back
 1.51718 +** using the sqlite3BtreeSavepoint() function.
 1.51719 +*/
 1.51720 +SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
 1.51721 +  int rc;
 1.51722 +  BtShared *pBt = p->pBt;
 1.51723 +  sqlite3BtreeEnter(p);
 1.51724 +  assert( p->inTrans==TRANS_WRITE );
 1.51725 +  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 1.51726 +  assert( iStatement>0 );
 1.51727 +  assert( iStatement>p->db->nSavepoint );
 1.51728 +  assert( pBt->inTransaction==TRANS_WRITE );
 1.51729 +  /* At the pager level, a statement transaction is a savepoint with
 1.51730 +  ** an index greater than all savepoints created explicitly using
 1.51731 +  ** SQL statements. It is illegal to open, release or rollback any
 1.51732 +  ** such savepoints while the statement transaction savepoint is active.
 1.51733 +  */
 1.51734 +  rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
 1.51735 +  sqlite3BtreeLeave(p);
 1.51736 +  return rc;
 1.51737 +}
 1.51738 +
 1.51739 +/*
 1.51740 +** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
 1.51741 +** or SAVEPOINT_RELEASE. This function either releases or rolls back the
 1.51742 +** savepoint identified by parameter iSavepoint, depending on the value 
 1.51743 +** of op.
 1.51744 +**
 1.51745 +** Normally, iSavepoint is greater than or equal to zero. However, if op is
 1.51746 +** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 
 1.51747 +** contents of the entire transaction are rolled back. This is different
 1.51748 +** from a normal transaction rollback, as no locks are released and the
 1.51749 +** transaction remains open.
 1.51750 +*/
 1.51751 +SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
 1.51752 +  int rc = SQLITE_OK;
 1.51753 +  if( p && p->inTrans==TRANS_WRITE ){
 1.51754 +    BtShared *pBt = p->pBt;
 1.51755 +    assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 1.51756 +    assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
 1.51757 +    sqlite3BtreeEnter(p);
 1.51758 +    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
 1.51759 +    if( rc==SQLITE_OK ){
 1.51760 +      if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
 1.51761 +        pBt->nPage = 0;
 1.51762 +      }
 1.51763 +      rc = newDatabase(pBt);
 1.51764 +      pBt->nPage = get4byte(28 + pBt->pPage1->aData);
 1.51765 +
 1.51766 +      /* The database size was written into the offset 28 of the header
 1.51767 +      ** when the transaction started, so we know that the value at offset
 1.51768 +      ** 28 is nonzero. */
 1.51769 +      assert( pBt->nPage>0 );
 1.51770 +    }
 1.51771 +    sqlite3BtreeLeave(p);
 1.51772 +  }
 1.51773 +  return rc;
 1.51774 +}
 1.51775 +
 1.51776 +/*
 1.51777 +** Create a new cursor for the BTree whose root is on the page
 1.51778 +** iTable. If a read-only cursor is requested, it is assumed that
 1.51779 +** the caller already has at least a read-only transaction open
 1.51780 +** on the database already. If a write-cursor is requested, then
 1.51781 +** the caller is assumed to have an open write transaction.
 1.51782 +**
 1.51783 +** If wrFlag==0, then the cursor can only be used for reading.
 1.51784 +** If wrFlag==1, then the cursor can be used for reading or for
 1.51785 +** writing if other conditions for writing are also met.  These
 1.51786 +** are the conditions that must be met in order for writing to
 1.51787 +** be allowed:
 1.51788 +**
 1.51789 +** 1:  The cursor must have been opened with wrFlag==1
 1.51790 +**
 1.51791 +** 2:  Other database connections that share the same pager cache
 1.51792 +**     but which are not in the READ_UNCOMMITTED state may not have
 1.51793 +**     cursors open with wrFlag==0 on the same table.  Otherwise
 1.51794 +**     the changes made by this write cursor would be visible to
 1.51795 +**     the read cursors in the other database connection.
 1.51796 +**
 1.51797 +** 3:  The database must be writable (not on read-only media)
 1.51798 +**
 1.51799 +** 4:  There must be an active transaction.
 1.51800 +**
 1.51801 +** No checking is done to make sure that page iTable really is the
 1.51802 +** root page of a b-tree.  If it is not, then the cursor acquired
 1.51803 +** will not work correctly.
 1.51804 +**
 1.51805 +** It is assumed that the sqlite3BtreeCursorZero() has been called
 1.51806 +** on pCur to initialize the memory space prior to invoking this routine.
 1.51807 +*/
 1.51808 +static int btreeCursor(
 1.51809 +  Btree *p,                              /* The btree */
 1.51810 +  int iTable,                            /* Root page of table to open */
 1.51811 +  int wrFlag,                            /* 1 to write. 0 read-only */
 1.51812 +  struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
 1.51813 +  BtCursor *pCur                         /* Space for new cursor */
 1.51814 +){
 1.51815 +  BtShared *pBt = p->pBt;                /* Shared b-tree handle */
 1.51816 +
 1.51817 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.51818 +  assert( wrFlag==0 || wrFlag==1 );
 1.51819 +
 1.51820 +  /* The following assert statements verify that if this is a sharable 
 1.51821 +  ** b-tree database, the connection is holding the required table locks, 
 1.51822 +  ** and that no other connection has any open cursor that conflicts with 
 1.51823 +  ** this lock.  */
 1.51824 +  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
 1.51825 +  assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
 1.51826 +
 1.51827 +  /* Assert that the caller has opened the required transaction. */
 1.51828 +  assert( p->inTrans>TRANS_NONE );
 1.51829 +  assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
 1.51830 +  assert( pBt->pPage1 && pBt->pPage1->aData );
 1.51831 +
 1.51832 +  if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
 1.51833 +    return SQLITE_READONLY;
 1.51834 +  }
 1.51835 +  if( iTable==1 && btreePagecount(pBt)==0 ){
 1.51836 +    assert( wrFlag==0 );
 1.51837 +    iTable = 0;
 1.51838 +  }
 1.51839 +
 1.51840 +  /* Now that no other errors can occur, finish filling in the BtCursor
 1.51841 +  ** variables and link the cursor into the BtShared list.  */
 1.51842 +  pCur->pgnoRoot = (Pgno)iTable;
 1.51843 +  pCur->iPage = -1;
 1.51844 +  pCur->pKeyInfo = pKeyInfo;
 1.51845 +  pCur->pBtree = p;
 1.51846 +  pCur->pBt = pBt;
 1.51847 +  pCur->wrFlag = (u8)wrFlag;
 1.51848 +  pCur->pNext = pBt->pCursor;
 1.51849 +  if( pCur->pNext ){
 1.51850 +    pCur->pNext->pPrev = pCur;
 1.51851 +  }
 1.51852 +  pBt->pCursor = pCur;
 1.51853 +  pCur->eState = CURSOR_INVALID;
 1.51854 +  pCur->cachedRowid = 0;
 1.51855 +  return SQLITE_OK;
 1.51856 +}
 1.51857 +SQLITE_PRIVATE int sqlite3BtreeCursor(
 1.51858 +  Btree *p,                                   /* The btree */
 1.51859 +  int iTable,                                 /* Root page of table to open */
 1.51860 +  int wrFlag,                                 /* 1 to write. 0 read-only */
 1.51861 +  struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
 1.51862 +  BtCursor *pCur                              /* Write new cursor here */
 1.51863 +){
 1.51864 +  int rc;
 1.51865 +  sqlite3BtreeEnter(p);
 1.51866 +  rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
 1.51867 +  sqlite3BtreeLeave(p);
 1.51868 +  return rc;
 1.51869 +}
 1.51870 +
 1.51871 +/*
 1.51872 +** Return the size of a BtCursor object in bytes.
 1.51873 +**
 1.51874 +** This interfaces is needed so that users of cursors can preallocate
 1.51875 +** sufficient storage to hold a cursor.  The BtCursor object is opaque
 1.51876 +** to users so they cannot do the sizeof() themselves - they must call
 1.51877 +** this routine.
 1.51878 +*/
 1.51879 +SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
 1.51880 +  return ROUND8(sizeof(BtCursor));
 1.51881 +}
 1.51882 +
 1.51883 +/*
 1.51884 +** Initialize memory that will be converted into a BtCursor object.
 1.51885 +**
 1.51886 +** The simple approach here would be to memset() the entire object
 1.51887 +** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
 1.51888 +** do not need to be zeroed and they are large, so we can save a lot
 1.51889 +** of run-time by skipping the initialization of those elements.
 1.51890 +*/
 1.51891 +SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
 1.51892 +  memset(p, 0, offsetof(BtCursor, iPage));
 1.51893 +}
 1.51894 +
 1.51895 +/*
 1.51896 +** Set the cached rowid value of every cursor in the same database file
 1.51897 +** as pCur and having the same root page number as pCur.  The value is
 1.51898 +** set to iRowid.
 1.51899 +**
 1.51900 +** Only positive rowid values are considered valid for this cache.
 1.51901 +** The cache is initialized to zero, indicating an invalid cache.
 1.51902 +** A btree will work fine with zero or negative rowids.  We just cannot
 1.51903 +** cache zero or negative rowids, which means tables that use zero or
 1.51904 +** negative rowids might run a little slower.  But in practice, zero
 1.51905 +** or negative rowids are very uncommon so this should not be a problem.
 1.51906 +*/
 1.51907 +SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
 1.51908 +  BtCursor *p;
 1.51909 +  for(p=pCur->pBt->pCursor; p; p=p->pNext){
 1.51910 +    if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
 1.51911 +  }
 1.51912 +  assert( pCur->cachedRowid==iRowid );
 1.51913 +}
 1.51914 +
 1.51915 +/*
 1.51916 +** Return the cached rowid for the given cursor.  A negative or zero
 1.51917 +** return value indicates that the rowid cache is invalid and should be
 1.51918 +** ignored.  If the rowid cache has never before been set, then a
 1.51919 +** zero is returned.
 1.51920 +*/
 1.51921 +SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
 1.51922 +  return pCur->cachedRowid;
 1.51923 +}
 1.51924 +
 1.51925 +/*
 1.51926 +** Close a cursor.  The read lock on the database file is released
 1.51927 +** when the last cursor is closed.
 1.51928 +*/
 1.51929 +SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
 1.51930 +  Btree *pBtree = pCur->pBtree;
 1.51931 +  if( pBtree ){
 1.51932 +    int i;
 1.51933 +    BtShared *pBt = pCur->pBt;
 1.51934 +    sqlite3BtreeEnter(pBtree);
 1.51935 +    sqlite3BtreeClearCursor(pCur);
 1.51936 +    if( pCur->pPrev ){
 1.51937 +      pCur->pPrev->pNext = pCur->pNext;
 1.51938 +    }else{
 1.51939 +      pBt->pCursor = pCur->pNext;
 1.51940 +    }
 1.51941 +    if( pCur->pNext ){
 1.51942 +      pCur->pNext->pPrev = pCur->pPrev;
 1.51943 +    }
 1.51944 +    for(i=0; i<=pCur->iPage; i++){
 1.51945 +      releasePage(pCur->apPage[i]);
 1.51946 +    }
 1.51947 +    unlockBtreeIfUnused(pBt);
 1.51948 +    invalidateOverflowCache(pCur);
 1.51949 +    /* sqlite3_free(pCur); */
 1.51950 +    sqlite3BtreeLeave(pBtree);
 1.51951 +  }
 1.51952 +  return SQLITE_OK;
 1.51953 +}
 1.51954 +
 1.51955 +/*
 1.51956 +** Make sure the BtCursor* given in the argument has a valid
 1.51957 +** BtCursor.info structure.  If it is not already valid, call
 1.51958 +** btreeParseCell() to fill it in.
 1.51959 +**
 1.51960 +** BtCursor.info is a cache of the information in the current cell.
 1.51961 +** Using this cache reduces the number of calls to btreeParseCell().
 1.51962 +**
 1.51963 +** 2007-06-25:  There is a bug in some versions of MSVC that cause the
 1.51964 +** compiler to crash when getCellInfo() is implemented as a macro.
 1.51965 +** But there is a measureable speed advantage to using the macro on gcc
 1.51966 +** (when less compiler optimizations like -Os or -O0 are used and the
 1.51967 +** compiler is not doing agressive inlining.)  So we use a real function
 1.51968 +** for MSVC and a macro for everything else.  Ticket #2457.
 1.51969 +*/
 1.51970 +#ifndef NDEBUG
 1.51971 +  static void assertCellInfo(BtCursor *pCur){
 1.51972 +    CellInfo info;
 1.51973 +    int iPage = pCur->iPage;
 1.51974 +    memset(&info, 0, sizeof(info));
 1.51975 +    btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
 1.51976 +    assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
 1.51977 +  }
 1.51978 +#else
 1.51979 +  #define assertCellInfo(x)
 1.51980 +#endif
 1.51981 +#ifdef _MSC_VER
 1.51982 +  /* Use a real function in MSVC to work around bugs in that compiler. */
 1.51983 +  static void getCellInfo(BtCursor *pCur){
 1.51984 +    if( pCur->info.nSize==0 ){
 1.51985 +      int iPage = pCur->iPage;
 1.51986 +      btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
 1.51987 +      pCur->validNKey = 1;
 1.51988 +    }else{
 1.51989 +      assertCellInfo(pCur);
 1.51990 +    }
 1.51991 +  }
 1.51992 +#else /* if not _MSC_VER */
 1.51993 +  /* Use a macro in all other compilers so that the function is inlined */
 1.51994 +#define getCellInfo(pCur)                                                      \
 1.51995 +  if( pCur->info.nSize==0 ){                                                   \
 1.51996 +    int iPage = pCur->iPage;                                                   \
 1.51997 +    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
 1.51998 +    pCur->validNKey = 1;                                                       \
 1.51999 +  }else{                                                                       \
 1.52000 +    assertCellInfo(pCur);                                                      \
 1.52001 +  }
 1.52002 +#endif /* _MSC_VER */
 1.52003 +
 1.52004 +#ifndef NDEBUG  /* The next routine used only within assert() statements */
 1.52005 +/*
 1.52006 +** Return true if the given BtCursor is valid.  A valid cursor is one
 1.52007 +** that is currently pointing to a row in a (non-empty) table.
 1.52008 +** This is a verification routine is used only within assert() statements.
 1.52009 +*/
 1.52010 +SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
 1.52011 +  return pCur && pCur->eState==CURSOR_VALID;
 1.52012 +}
 1.52013 +#endif /* NDEBUG */
 1.52014 +
 1.52015 +/*
 1.52016 +** Set *pSize to the size of the buffer needed to hold the value of
 1.52017 +** the key for the current entry.  If the cursor is not pointing
 1.52018 +** to a valid entry, *pSize is set to 0. 
 1.52019 +**
 1.52020 +** For a table with the INTKEY flag set, this routine returns the key
 1.52021 +** itself, not the number of bytes in the key.
 1.52022 +**
 1.52023 +** The caller must position the cursor prior to invoking this routine.
 1.52024 +** 
 1.52025 +** This routine cannot fail.  It always returns SQLITE_OK.  
 1.52026 +*/
 1.52027 +SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
 1.52028 +  assert( cursorHoldsMutex(pCur) );
 1.52029 +  assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
 1.52030 +  if( pCur->eState!=CURSOR_VALID ){
 1.52031 +    *pSize = 0;
 1.52032 +  }else{
 1.52033 +    getCellInfo(pCur);
 1.52034 +    *pSize = pCur->info.nKey;
 1.52035 +  }
 1.52036 +  return SQLITE_OK;
 1.52037 +}
 1.52038 +
 1.52039 +/*
 1.52040 +** Set *pSize to the number of bytes of data in the entry the
 1.52041 +** cursor currently points to.
 1.52042 +**
 1.52043 +** The caller must guarantee that the cursor is pointing to a non-NULL
 1.52044 +** valid entry.  In other words, the calling procedure must guarantee
 1.52045 +** that the cursor has Cursor.eState==CURSOR_VALID.
 1.52046 +**
 1.52047 +** Failure is not possible.  This function always returns SQLITE_OK.
 1.52048 +** It might just as well be a procedure (returning void) but we continue
 1.52049 +** to return an integer result code for historical reasons.
 1.52050 +*/
 1.52051 +SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
 1.52052 +  assert( cursorHoldsMutex(pCur) );
 1.52053 +  assert( pCur->eState==CURSOR_VALID );
 1.52054 +  getCellInfo(pCur);
 1.52055 +  *pSize = pCur->info.nData;
 1.52056 +  return SQLITE_OK;
 1.52057 +}
 1.52058 +
 1.52059 +/*
 1.52060 +** Given the page number of an overflow page in the database (parameter
 1.52061 +** ovfl), this function finds the page number of the next page in the 
 1.52062 +** linked list of overflow pages. If possible, it uses the auto-vacuum
 1.52063 +** pointer-map data instead of reading the content of page ovfl to do so. 
 1.52064 +**
 1.52065 +** If an error occurs an SQLite error code is returned. Otherwise:
 1.52066 +**
 1.52067 +** The page number of the next overflow page in the linked list is 
 1.52068 +** written to *pPgnoNext. If page ovfl is the last page in its linked 
 1.52069 +** list, *pPgnoNext is set to zero. 
 1.52070 +**
 1.52071 +** If ppPage is not NULL, and a reference to the MemPage object corresponding
 1.52072 +** to page number pOvfl was obtained, then *ppPage is set to point to that
 1.52073 +** reference. It is the responsibility of the caller to call releasePage()
 1.52074 +** on *ppPage to free the reference. In no reference was obtained (because
 1.52075 +** the pointer-map was used to obtain the value for *pPgnoNext), then
 1.52076 +** *ppPage is set to zero.
 1.52077 +*/
 1.52078 +static int getOverflowPage(
 1.52079 +  BtShared *pBt,               /* The database file */
 1.52080 +  Pgno ovfl,                   /* Current overflow page number */
 1.52081 +  MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
 1.52082 +  Pgno *pPgnoNext              /* OUT: Next overflow page number */
 1.52083 +){
 1.52084 +  Pgno next = 0;
 1.52085 +  MemPage *pPage = 0;
 1.52086 +  int rc = SQLITE_OK;
 1.52087 +
 1.52088 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.52089 +  assert(pPgnoNext);
 1.52090 +
 1.52091 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.52092 +  /* Try to find the next page in the overflow list using the
 1.52093 +  ** autovacuum pointer-map pages. Guess that the next page in 
 1.52094 +  ** the overflow list is page number (ovfl+1). If that guess turns 
 1.52095 +  ** out to be wrong, fall back to loading the data of page 
 1.52096 +  ** number ovfl to determine the next page number.
 1.52097 +  */
 1.52098 +  if( pBt->autoVacuum ){
 1.52099 +    Pgno pgno;
 1.52100 +    Pgno iGuess = ovfl+1;
 1.52101 +    u8 eType;
 1.52102 +
 1.52103 +    while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
 1.52104 +      iGuess++;
 1.52105 +    }
 1.52106 +
 1.52107 +    if( iGuess<=btreePagecount(pBt) ){
 1.52108 +      rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
 1.52109 +      if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
 1.52110 +        next = iGuess;
 1.52111 +        rc = SQLITE_DONE;
 1.52112 +      }
 1.52113 +    }
 1.52114 +  }
 1.52115 +#endif
 1.52116 +
 1.52117 +  assert( next==0 || rc==SQLITE_DONE );
 1.52118 +  if( rc==SQLITE_OK ){
 1.52119 +    rc = btreeGetPage(pBt, ovfl, &pPage, 0);
 1.52120 +    assert( rc==SQLITE_OK || pPage==0 );
 1.52121 +    if( rc==SQLITE_OK ){
 1.52122 +      next = get4byte(pPage->aData);
 1.52123 +    }
 1.52124 +  }
 1.52125 +
 1.52126 +  *pPgnoNext = next;
 1.52127 +  if( ppPage ){
 1.52128 +    *ppPage = pPage;
 1.52129 +  }else{
 1.52130 +    releasePage(pPage);
 1.52131 +  }
 1.52132 +  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
 1.52133 +}
 1.52134 +
 1.52135 +/*
 1.52136 +** Copy data from a buffer to a page, or from a page to a buffer.
 1.52137 +**
 1.52138 +** pPayload is a pointer to data stored on database page pDbPage.
 1.52139 +** If argument eOp is false, then nByte bytes of data are copied
 1.52140 +** from pPayload to the buffer pointed at by pBuf. If eOp is true,
 1.52141 +** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
 1.52142 +** of data are copied from the buffer pBuf to pPayload.
 1.52143 +**
 1.52144 +** SQLITE_OK is returned on success, otherwise an error code.
 1.52145 +*/
 1.52146 +static int copyPayload(
 1.52147 +  void *pPayload,           /* Pointer to page data */
 1.52148 +  void *pBuf,               /* Pointer to buffer */
 1.52149 +  int nByte,                /* Number of bytes to copy */
 1.52150 +  int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
 1.52151 +  DbPage *pDbPage           /* Page containing pPayload */
 1.52152 +){
 1.52153 +  if( eOp ){
 1.52154 +    /* Copy data from buffer to page (a write operation) */
 1.52155 +    int rc = sqlite3PagerWrite(pDbPage);
 1.52156 +    if( rc!=SQLITE_OK ){
 1.52157 +      return rc;
 1.52158 +    }
 1.52159 +    memcpy(pPayload, pBuf, nByte);
 1.52160 +  }else{
 1.52161 +    /* Copy data from page to buffer (a read operation) */
 1.52162 +    memcpy(pBuf, pPayload, nByte);
 1.52163 +  }
 1.52164 +  return SQLITE_OK;
 1.52165 +}
 1.52166 +
 1.52167 +/*
 1.52168 +** This function is used to read or overwrite payload information
 1.52169 +** for the entry that the pCur cursor is pointing to. If the eOp
 1.52170 +** parameter is 0, this is a read operation (data copied into
 1.52171 +** buffer pBuf). If it is non-zero, a write (data copied from
 1.52172 +** buffer pBuf).
 1.52173 +**
 1.52174 +** A total of "amt" bytes are read or written beginning at "offset".
 1.52175 +** Data is read to or from the buffer pBuf.
 1.52176 +**
 1.52177 +** The content being read or written might appear on the main page
 1.52178 +** or be scattered out on multiple overflow pages.
 1.52179 +**
 1.52180 +** If the BtCursor.isIncrblobHandle flag is set, and the current
 1.52181 +** cursor entry uses one or more overflow pages, this function
 1.52182 +** allocates space for and lazily popluates the overflow page-list 
 1.52183 +** cache array (BtCursor.aOverflow). Subsequent calls use this
 1.52184 +** cache to make seeking to the supplied offset more efficient.
 1.52185 +**
 1.52186 +** Once an overflow page-list cache has been allocated, it may be
 1.52187 +** invalidated if some other cursor writes to the same table, or if
 1.52188 +** the cursor is moved to a different row. Additionally, in auto-vacuum
 1.52189 +** mode, the following events may invalidate an overflow page-list cache.
 1.52190 +**
 1.52191 +**   * An incremental vacuum,
 1.52192 +**   * A commit in auto_vacuum="full" mode,
 1.52193 +**   * Creating a table (may require moving an overflow page).
 1.52194 +*/
 1.52195 +static int accessPayload(
 1.52196 +  BtCursor *pCur,      /* Cursor pointing to entry to read from */
 1.52197 +  u32 offset,          /* Begin reading this far into payload */
 1.52198 +  u32 amt,             /* Read this many bytes */
 1.52199 +  unsigned char *pBuf, /* Write the bytes into this buffer */ 
 1.52200 +  int eOp              /* zero to read. non-zero to write. */
 1.52201 +){
 1.52202 +  unsigned char *aPayload;
 1.52203 +  int rc = SQLITE_OK;
 1.52204 +  u32 nKey;
 1.52205 +  int iIdx = 0;
 1.52206 +  MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
 1.52207 +  BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
 1.52208 +
 1.52209 +  assert( pPage );
 1.52210 +  assert( pCur->eState==CURSOR_VALID );
 1.52211 +  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 1.52212 +  assert( cursorHoldsMutex(pCur) );
 1.52213 +
 1.52214 +  getCellInfo(pCur);
 1.52215 +  aPayload = pCur->info.pCell + pCur->info.nHeader;
 1.52216 +  nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
 1.52217 +
 1.52218 +  if( NEVER(offset+amt > nKey+pCur->info.nData) 
 1.52219 +   || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
 1.52220 +  ){
 1.52221 +    /* Trying to read or write past the end of the data is an error */
 1.52222 +    return SQLITE_CORRUPT_BKPT;
 1.52223 +  }
 1.52224 +
 1.52225 +  /* Check if data must be read/written to/from the btree page itself. */
 1.52226 +  if( offset<pCur->info.nLocal ){
 1.52227 +    int a = amt;
 1.52228 +    if( a+offset>pCur->info.nLocal ){
 1.52229 +      a = pCur->info.nLocal - offset;
 1.52230 +    }
 1.52231 +    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
 1.52232 +    offset = 0;
 1.52233 +    pBuf += a;
 1.52234 +    amt -= a;
 1.52235 +  }else{
 1.52236 +    offset -= pCur->info.nLocal;
 1.52237 +  }
 1.52238 +
 1.52239 +  if( rc==SQLITE_OK && amt>0 ){
 1.52240 +    const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
 1.52241 +    Pgno nextPage;
 1.52242 +
 1.52243 +    nextPage = get4byte(&aPayload[pCur->info.nLocal]);
 1.52244 +
 1.52245 +#ifndef SQLITE_OMIT_INCRBLOB
 1.52246 +    /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
 1.52247 +    ** has not been allocated, allocate it now. The array is sized at
 1.52248 +    ** one entry for each overflow page in the overflow chain. The
 1.52249 +    ** page number of the first overflow page is stored in aOverflow[0],
 1.52250 +    ** etc. A value of 0 in the aOverflow[] array means "not yet known"
 1.52251 +    ** (the cache is lazily populated).
 1.52252 +    */
 1.52253 +    if( pCur->isIncrblobHandle && !pCur->aOverflow ){
 1.52254 +      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 1.52255 +      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
 1.52256 +      /* nOvfl is always positive.  If it were zero, fetchPayload would have
 1.52257 +      ** been used instead of this routine. */
 1.52258 +      if( ALWAYS(nOvfl) && !pCur->aOverflow ){
 1.52259 +        rc = SQLITE_NOMEM;
 1.52260 +      }
 1.52261 +    }
 1.52262 +
 1.52263 +    /* If the overflow page-list cache has been allocated and the
 1.52264 +    ** entry for the first required overflow page is valid, skip
 1.52265 +    ** directly to it.
 1.52266 +    */
 1.52267 +    if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
 1.52268 +      iIdx = (offset/ovflSize);
 1.52269 +      nextPage = pCur->aOverflow[iIdx];
 1.52270 +      offset = (offset%ovflSize);
 1.52271 +    }
 1.52272 +#endif
 1.52273 +
 1.52274 +    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
 1.52275 +
 1.52276 +#ifndef SQLITE_OMIT_INCRBLOB
 1.52277 +      /* If required, populate the overflow page-list cache. */
 1.52278 +      if( pCur->aOverflow ){
 1.52279 +        assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
 1.52280 +        pCur->aOverflow[iIdx] = nextPage;
 1.52281 +      }
 1.52282 +#endif
 1.52283 +
 1.52284 +      if( offset>=ovflSize ){
 1.52285 +        /* The only reason to read this page is to obtain the page
 1.52286 +        ** number for the next page in the overflow chain. The page
 1.52287 +        ** data is not required. So first try to lookup the overflow
 1.52288 +        ** page-list cache, if any, then fall back to the getOverflowPage()
 1.52289 +        ** function.
 1.52290 +        */
 1.52291 +#ifndef SQLITE_OMIT_INCRBLOB
 1.52292 +        if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
 1.52293 +          nextPage = pCur->aOverflow[iIdx+1];
 1.52294 +        } else 
 1.52295 +#endif
 1.52296 +          rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 1.52297 +        offset -= ovflSize;
 1.52298 +      }else{
 1.52299 +        /* Need to read this page properly. It contains some of the
 1.52300 +        ** range of data that is being read (eOp==0) or written (eOp!=0).
 1.52301 +        */
 1.52302 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
 1.52303 +        sqlite3_file *fd;
 1.52304 +#endif
 1.52305 +        int a = amt;
 1.52306 +        if( a + offset > ovflSize ){
 1.52307 +          a = ovflSize - offset;
 1.52308 +        }
 1.52309 +
 1.52310 +#ifdef SQLITE_DIRECT_OVERFLOW_READ
 1.52311 +        /* If all the following are true:
 1.52312 +        **
 1.52313 +        **   1) this is a read operation, and 
 1.52314 +        **   2) data is required from the start of this overflow page, and
 1.52315 +        **   3) the database is file-backed, and
 1.52316 +        **   4) there is no open write-transaction, and
 1.52317 +        **   5) the database is not a WAL database,
 1.52318 +        **
 1.52319 +        ** then data can be read directly from the database file into the
 1.52320 +        ** output buffer, bypassing the page-cache altogether. This speeds
 1.52321 +        ** up loading large records that span many overflow pages.
 1.52322 +        */
 1.52323 +        if( eOp==0                                             /* (1) */
 1.52324 +         && offset==0                                          /* (2) */
 1.52325 +         && pBt->inTransaction==TRANS_READ                     /* (4) */
 1.52326 +         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
 1.52327 +         && pBt->pPage1->aData[19]==0x01                       /* (5) */
 1.52328 +        ){
 1.52329 +          u8 aSave[4];
 1.52330 +          u8 *aWrite = &pBuf[-4];
 1.52331 +          memcpy(aSave, aWrite, 4);
 1.52332 +          rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 1.52333 +          nextPage = get4byte(aWrite);
 1.52334 +          memcpy(aWrite, aSave, 4);
 1.52335 +        }else
 1.52336 +#endif
 1.52337 +
 1.52338 +        {
 1.52339 +          DbPage *pDbPage;
 1.52340 +          rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
 1.52341 +          if( rc==SQLITE_OK ){
 1.52342 +            aPayload = sqlite3PagerGetData(pDbPage);
 1.52343 +            nextPage = get4byte(aPayload);
 1.52344 +            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
 1.52345 +            sqlite3PagerUnref(pDbPage);
 1.52346 +            offset = 0;
 1.52347 +          }
 1.52348 +        }
 1.52349 +        amt -= a;
 1.52350 +        pBuf += a;
 1.52351 +      }
 1.52352 +    }
 1.52353 +  }
 1.52354 +
 1.52355 +  if( rc==SQLITE_OK && amt>0 ){
 1.52356 +    return SQLITE_CORRUPT_BKPT;
 1.52357 +  }
 1.52358 +  return rc;
 1.52359 +}
 1.52360 +
 1.52361 +/*
 1.52362 +** Read part of the key associated with cursor pCur.  Exactly
 1.52363 +** "amt" bytes will be transfered into pBuf[].  The transfer
 1.52364 +** begins at "offset".
 1.52365 +**
 1.52366 +** The caller must ensure that pCur is pointing to a valid row
 1.52367 +** in the table.
 1.52368 +**
 1.52369 +** Return SQLITE_OK on success or an error code if anything goes
 1.52370 +** wrong.  An error is returned if "offset+amt" is larger than
 1.52371 +** the available payload.
 1.52372 +*/
 1.52373 +SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 1.52374 +  assert( cursorHoldsMutex(pCur) );
 1.52375 +  assert( pCur->eState==CURSOR_VALID );
 1.52376 +  assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 1.52377 +  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 1.52378 +  return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
 1.52379 +}
 1.52380 +
 1.52381 +/*
 1.52382 +** Read part of the data associated with cursor pCur.  Exactly
 1.52383 +** "amt" bytes will be transfered into pBuf[].  The transfer
 1.52384 +** begins at "offset".
 1.52385 +**
 1.52386 +** Return SQLITE_OK on success or an error code if anything goes
 1.52387 +** wrong.  An error is returned if "offset+amt" is larger than
 1.52388 +** the available payload.
 1.52389 +*/
 1.52390 +SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 1.52391 +  int rc;
 1.52392 +
 1.52393 +#ifndef SQLITE_OMIT_INCRBLOB
 1.52394 +  if ( pCur->eState==CURSOR_INVALID ){
 1.52395 +    return SQLITE_ABORT;
 1.52396 +  }
 1.52397 +#endif
 1.52398 +
 1.52399 +  assert( cursorHoldsMutex(pCur) );
 1.52400 +  rc = restoreCursorPosition(pCur);
 1.52401 +  if( rc==SQLITE_OK ){
 1.52402 +    assert( pCur->eState==CURSOR_VALID );
 1.52403 +    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 1.52404 +    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 1.52405 +    rc = accessPayload(pCur, offset, amt, pBuf, 0);
 1.52406 +  }
 1.52407 +  return rc;
 1.52408 +}
 1.52409 +
 1.52410 +/*
 1.52411 +** Return a pointer to payload information from the entry that the 
 1.52412 +** pCur cursor is pointing to.  The pointer is to the beginning of
 1.52413 +** the key if skipKey==0 and it points to the beginning of data if
 1.52414 +** skipKey==1.  The number of bytes of available key/data is written
 1.52415 +** into *pAmt.  If *pAmt==0, then the value returned will not be
 1.52416 +** a valid pointer.
 1.52417 +**
 1.52418 +** This routine is an optimization.  It is common for the entire key
 1.52419 +** and data to fit on the local page and for there to be no overflow
 1.52420 +** pages.  When that is so, this routine can be used to access the
 1.52421 +** key and data without making a copy.  If the key and/or data spills
 1.52422 +** onto overflow pages, then accessPayload() must be used to reassemble
 1.52423 +** the key/data and copy it into a preallocated buffer.
 1.52424 +**
 1.52425 +** The pointer returned by this routine looks directly into the cached
 1.52426 +** page of the database.  The data might change or move the next time
 1.52427 +** any btree routine is called.
 1.52428 +*/
 1.52429 +static const unsigned char *fetchPayload(
 1.52430 +  BtCursor *pCur,      /* Cursor pointing to entry to read from */
 1.52431 +  int *pAmt,           /* Write the number of available bytes here */
 1.52432 +  int skipKey          /* read beginning at data if this is true */
 1.52433 +){
 1.52434 +  unsigned char *aPayload;
 1.52435 +  MemPage *pPage;
 1.52436 +  u32 nKey;
 1.52437 +  u32 nLocal;
 1.52438 +
 1.52439 +  assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
 1.52440 +  assert( pCur->eState==CURSOR_VALID );
 1.52441 +  assert( cursorHoldsMutex(pCur) );
 1.52442 +  pPage = pCur->apPage[pCur->iPage];
 1.52443 +  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 1.52444 +  if( NEVER(pCur->info.nSize==0) ){
 1.52445 +    btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
 1.52446 +                   &pCur->info);
 1.52447 +  }
 1.52448 +  aPayload = pCur->info.pCell;
 1.52449 +  aPayload += pCur->info.nHeader;
 1.52450 +  if( pPage->intKey ){
 1.52451 +    nKey = 0;
 1.52452 +  }else{
 1.52453 +    nKey = (int)pCur->info.nKey;
 1.52454 +  }
 1.52455 +  if( skipKey ){
 1.52456 +    aPayload += nKey;
 1.52457 +    nLocal = pCur->info.nLocal - nKey;
 1.52458 +  }else{
 1.52459 +    nLocal = pCur->info.nLocal;
 1.52460 +    assert( nLocal<=nKey );
 1.52461 +  }
 1.52462 +  *pAmt = nLocal;
 1.52463 +  return aPayload;
 1.52464 +}
 1.52465 +
 1.52466 +
 1.52467 +/*
 1.52468 +** For the entry that cursor pCur is point to, return as
 1.52469 +** many bytes of the key or data as are available on the local
 1.52470 +** b-tree page.  Write the number of available bytes into *pAmt.
 1.52471 +**
 1.52472 +** The pointer returned is ephemeral.  The key/data may move
 1.52473 +** or be destroyed on the next call to any Btree routine,
 1.52474 +** including calls from other threads against the same cache.
 1.52475 +** Hence, a mutex on the BtShared should be held prior to calling
 1.52476 +** this routine.
 1.52477 +**
 1.52478 +** These routines is used to get quick access to key and data
 1.52479 +** in the common case where no overflow pages are used.
 1.52480 +*/
 1.52481 +SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
 1.52482 +  const void *p = 0;
 1.52483 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 1.52484 +  assert( cursorHoldsMutex(pCur) );
 1.52485 +  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
 1.52486 +    p = (const void*)fetchPayload(pCur, pAmt, 0);
 1.52487 +  }
 1.52488 +  return p;
 1.52489 +}
 1.52490 +SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
 1.52491 +  const void *p = 0;
 1.52492 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 1.52493 +  assert( cursorHoldsMutex(pCur) );
 1.52494 +  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
 1.52495 +    p = (const void*)fetchPayload(pCur, pAmt, 1);
 1.52496 +  }
 1.52497 +  return p;
 1.52498 +}
 1.52499 +
 1.52500 +
 1.52501 +/*
 1.52502 +** Move the cursor down to a new child page.  The newPgno argument is the
 1.52503 +** page number of the child page to move to.
 1.52504 +**
 1.52505 +** This function returns SQLITE_CORRUPT if the page-header flags field of
 1.52506 +** the new child page does not match the flags field of the parent (i.e.
 1.52507 +** if an intkey page appears to be the parent of a non-intkey page, or
 1.52508 +** vice-versa).
 1.52509 +*/
 1.52510 +static int moveToChild(BtCursor *pCur, u32 newPgno){
 1.52511 +  int rc;
 1.52512 +  int i = pCur->iPage;
 1.52513 +  MemPage *pNewPage;
 1.52514 +  BtShared *pBt = pCur->pBt;
 1.52515 +
 1.52516 +  assert( cursorHoldsMutex(pCur) );
 1.52517 +  assert( pCur->eState==CURSOR_VALID );
 1.52518 +  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
 1.52519 +  if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
 1.52520 +    return SQLITE_CORRUPT_BKPT;
 1.52521 +  }
 1.52522 +  rc = getAndInitPage(pBt, newPgno, &pNewPage);
 1.52523 +  if( rc ) return rc;
 1.52524 +  pCur->apPage[i+1] = pNewPage;
 1.52525 +  pCur->aiIdx[i+1] = 0;
 1.52526 +  pCur->iPage++;
 1.52527 +
 1.52528 +  pCur->info.nSize = 0;
 1.52529 +  pCur->validNKey = 0;
 1.52530 +  if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
 1.52531 +    return SQLITE_CORRUPT_BKPT;
 1.52532 +  }
 1.52533 +  return SQLITE_OK;
 1.52534 +}
 1.52535 +
 1.52536 +#if 0
 1.52537 +/*
 1.52538 +** Page pParent is an internal (non-leaf) tree page. This function 
 1.52539 +** asserts that page number iChild is the left-child if the iIdx'th
 1.52540 +** cell in page pParent. Or, if iIdx is equal to the total number of
 1.52541 +** cells in pParent, that page number iChild is the right-child of
 1.52542 +** the page.
 1.52543 +*/
 1.52544 +static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
 1.52545 +  assert( iIdx<=pParent->nCell );
 1.52546 +  if( iIdx==pParent->nCell ){
 1.52547 +    assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
 1.52548 +  }else{
 1.52549 +    assert( get4byte(findCell(pParent, iIdx))==iChild );
 1.52550 +  }
 1.52551 +}
 1.52552 +#else
 1.52553 +#  define assertParentIndex(x,y,z) 
 1.52554 +#endif
 1.52555 +
 1.52556 +/*
 1.52557 +** Move the cursor up to the parent page.
 1.52558 +**
 1.52559 +** pCur->idx is set to the cell index that contains the pointer
 1.52560 +** to the page we are coming from.  If we are coming from the
 1.52561 +** right-most child page then pCur->idx is set to one more than
 1.52562 +** the largest cell index.
 1.52563 +*/
 1.52564 +static void moveToParent(BtCursor *pCur){
 1.52565 +  assert( cursorHoldsMutex(pCur) );
 1.52566 +  assert( pCur->eState==CURSOR_VALID );
 1.52567 +  assert( pCur->iPage>0 );
 1.52568 +  assert( pCur->apPage[pCur->iPage] );
 1.52569 +
 1.52570 +  /* UPDATE: It is actually possible for the condition tested by the assert
 1.52571 +  ** below to be untrue if the database file is corrupt. This can occur if
 1.52572 +  ** one cursor has modified page pParent while a reference to it is held 
 1.52573 +  ** by a second cursor. Which can only happen if a single page is linked
 1.52574 +  ** into more than one b-tree structure in a corrupt database.  */
 1.52575 +#if 0
 1.52576 +  assertParentIndex(
 1.52577 +    pCur->apPage[pCur->iPage-1], 
 1.52578 +    pCur->aiIdx[pCur->iPage-1], 
 1.52579 +    pCur->apPage[pCur->iPage]->pgno
 1.52580 +  );
 1.52581 +#endif
 1.52582 +  testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
 1.52583 +
 1.52584 +  releasePage(pCur->apPage[pCur->iPage]);
 1.52585 +  pCur->iPage--;
 1.52586 +  pCur->info.nSize = 0;
 1.52587 +  pCur->validNKey = 0;
 1.52588 +}
 1.52589 +
 1.52590 +/*
 1.52591 +** Move the cursor to point to the root page of its b-tree structure.
 1.52592 +**
 1.52593 +** If the table has a virtual root page, then the cursor is moved to point
 1.52594 +** to the virtual root page instead of the actual root page. A table has a
 1.52595 +** virtual root page when the actual root page contains no cells and a 
 1.52596 +** single child page. This can only happen with the table rooted at page 1.
 1.52597 +**
 1.52598 +** If the b-tree structure is empty, the cursor state is set to 
 1.52599 +** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
 1.52600 +** cell located on the root (or virtual root) page and the cursor state
 1.52601 +** is set to CURSOR_VALID.
 1.52602 +**
 1.52603 +** If this function returns successfully, it may be assumed that the
 1.52604 +** page-header flags indicate that the [virtual] root-page is the expected 
 1.52605 +** kind of b-tree page (i.e. if when opening the cursor the caller did not
 1.52606 +** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
 1.52607 +** indicating a table b-tree, or if the caller did specify a KeyInfo 
 1.52608 +** structure the flags byte is set to 0x02 or 0x0A, indicating an index
 1.52609 +** b-tree).
 1.52610 +*/
 1.52611 +static int moveToRoot(BtCursor *pCur){
 1.52612 +  MemPage *pRoot;
 1.52613 +  int rc = SQLITE_OK;
 1.52614 +  Btree *p = pCur->pBtree;
 1.52615 +  BtShared *pBt = p->pBt;
 1.52616 +
 1.52617 +  assert( cursorHoldsMutex(pCur) );
 1.52618 +  assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
 1.52619 +  assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
 1.52620 +  assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
 1.52621 +  if( pCur->eState>=CURSOR_REQUIRESEEK ){
 1.52622 +    if( pCur->eState==CURSOR_FAULT ){
 1.52623 +      assert( pCur->skipNext!=SQLITE_OK );
 1.52624 +      return pCur->skipNext;
 1.52625 +    }
 1.52626 +    sqlite3BtreeClearCursor(pCur);
 1.52627 +  }
 1.52628 +
 1.52629 +  if( pCur->iPage>=0 ){
 1.52630 +    int i;
 1.52631 +    for(i=1; i<=pCur->iPage; i++){
 1.52632 +      releasePage(pCur->apPage[i]);
 1.52633 +    }
 1.52634 +    pCur->iPage = 0;
 1.52635 +  }else if( pCur->pgnoRoot==0 ){
 1.52636 +    pCur->eState = CURSOR_INVALID;
 1.52637 +    return SQLITE_OK;
 1.52638 +  }else{
 1.52639 +    rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
 1.52640 +    if( rc!=SQLITE_OK ){
 1.52641 +      pCur->eState = CURSOR_INVALID;
 1.52642 +      return rc;
 1.52643 +    }
 1.52644 +    pCur->iPage = 0;
 1.52645 +
 1.52646 +    /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
 1.52647 +    ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
 1.52648 +    ** NULL, the caller expects a table b-tree. If this is not the case,
 1.52649 +    ** return an SQLITE_CORRUPT error.  */
 1.52650 +    assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
 1.52651 +    if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
 1.52652 +      return SQLITE_CORRUPT_BKPT;
 1.52653 +    }
 1.52654 +  }
 1.52655 +
 1.52656 +  /* Assert that the root page is of the correct type. This must be the
 1.52657 +  ** case as the call to this function that loaded the root-page (either
 1.52658 +  ** this call or a previous invocation) would have detected corruption 
 1.52659 +  ** if the assumption were not true, and it is not possible for the flags 
 1.52660 +  ** byte to have been modified while this cursor is holding a reference
 1.52661 +  ** to the page.  */
 1.52662 +  pRoot = pCur->apPage[0];
 1.52663 +  assert( pRoot->pgno==pCur->pgnoRoot );
 1.52664 +  assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
 1.52665 +
 1.52666 +  pCur->aiIdx[0] = 0;
 1.52667 +  pCur->info.nSize = 0;
 1.52668 +  pCur->atLast = 0;
 1.52669 +  pCur->validNKey = 0;
 1.52670 +
 1.52671 +  if( pRoot->nCell==0 && !pRoot->leaf ){
 1.52672 +    Pgno subpage;
 1.52673 +    if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
 1.52674 +    subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
 1.52675 +    pCur->eState = CURSOR_VALID;
 1.52676 +    rc = moveToChild(pCur, subpage);
 1.52677 +  }else{
 1.52678 +    pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
 1.52679 +  }
 1.52680 +  return rc;
 1.52681 +}
 1.52682 +
 1.52683 +/*
 1.52684 +** Move the cursor down to the left-most leaf entry beneath the
 1.52685 +** entry to which it is currently pointing.
 1.52686 +**
 1.52687 +** The left-most leaf is the one with the smallest key - the first
 1.52688 +** in ascending order.
 1.52689 +*/
 1.52690 +static int moveToLeftmost(BtCursor *pCur){
 1.52691 +  Pgno pgno;
 1.52692 +  int rc = SQLITE_OK;
 1.52693 +  MemPage *pPage;
 1.52694 +
 1.52695 +  assert( cursorHoldsMutex(pCur) );
 1.52696 +  assert( pCur->eState==CURSOR_VALID );
 1.52697 +  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
 1.52698 +    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 1.52699 +    pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
 1.52700 +    rc = moveToChild(pCur, pgno);
 1.52701 +  }
 1.52702 +  return rc;
 1.52703 +}
 1.52704 +
 1.52705 +/*
 1.52706 +** Move the cursor down to the right-most leaf entry beneath the
 1.52707 +** page to which it is currently pointing.  Notice the difference
 1.52708 +** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
 1.52709 +** finds the left-most entry beneath the *entry* whereas moveToRightmost()
 1.52710 +** finds the right-most entry beneath the *page*.
 1.52711 +**
 1.52712 +** The right-most entry is the one with the largest key - the last
 1.52713 +** key in ascending order.
 1.52714 +*/
 1.52715 +static int moveToRightmost(BtCursor *pCur){
 1.52716 +  Pgno pgno;
 1.52717 +  int rc = SQLITE_OK;
 1.52718 +  MemPage *pPage = 0;
 1.52719 +
 1.52720 +  assert( cursorHoldsMutex(pCur) );
 1.52721 +  assert( pCur->eState==CURSOR_VALID );
 1.52722 +  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
 1.52723 +    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 1.52724 +    pCur->aiIdx[pCur->iPage] = pPage->nCell;
 1.52725 +    rc = moveToChild(pCur, pgno);
 1.52726 +  }
 1.52727 +  if( rc==SQLITE_OK ){
 1.52728 +    pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
 1.52729 +    pCur->info.nSize = 0;
 1.52730 +    pCur->validNKey = 0;
 1.52731 +  }
 1.52732 +  return rc;
 1.52733 +}
 1.52734 +
 1.52735 +/* Move the cursor to the first entry in the table.  Return SQLITE_OK
 1.52736 +** on success.  Set *pRes to 0 if the cursor actually points to something
 1.52737 +** or set *pRes to 1 if the table is empty.
 1.52738 +*/
 1.52739 +SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
 1.52740 +  int rc;
 1.52741 +
 1.52742 +  assert( cursorHoldsMutex(pCur) );
 1.52743 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 1.52744 +  rc = moveToRoot(pCur);
 1.52745 +  if( rc==SQLITE_OK ){
 1.52746 +    if( pCur->eState==CURSOR_INVALID ){
 1.52747 +      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 1.52748 +      *pRes = 1;
 1.52749 +    }else{
 1.52750 +      assert( pCur->apPage[pCur->iPage]->nCell>0 );
 1.52751 +      *pRes = 0;
 1.52752 +      rc = moveToLeftmost(pCur);
 1.52753 +    }
 1.52754 +  }
 1.52755 +  return rc;
 1.52756 +}
 1.52757 +
 1.52758 +/* Move the cursor to the last entry in the table.  Return SQLITE_OK
 1.52759 +** on success.  Set *pRes to 0 if the cursor actually points to something
 1.52760 +** or set *pRes to 1 if the table is empty.
 1.52761 +*/
 1.52762 +SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 1.52763 +  int rc;
 1.52764 + 
 1.52765 +  assert( cursorHoldsMutex(pCur) );
 1.52766 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 1.52767 +
 1.52768 +  /* If the cursor already points to the last entry, this is a no-op. */
 1.52769 +  if( CURSOR_VALID==pCur->eState && pCur->atLast ){
 1.52770 +#ifdef SQLITE_DEBUG
 1.52771 +    /* This block serves to assert() that the cursor really does point 
 1.52772 +    ** to the last entry in the b-tree. */
 1.52773 +    int ii;
 1.52774 +    for(ii=0; ii<pCur->iPage; ii++){
 1.52775 +      assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
 1.52776 +    }
 1.52777 +    assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
 1.52778 +    assert( pCur->apPage[pCur->iPage]->leaf );
 1.52779 +#endif
 1.52780 +    return SQLITE_OK;
 1.52781 +  }
 1.52782 +
 1.52783 +  rc = moveToRoot(pCur);
 1.52784 +  if( rc==SQLITE_OK ){
 1.52785 +    if( CURSOR_INVALID==pCur->eState ){
 1.52786 +      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 1.52787 +      *pRes = 1;
 1.52788 +    }else{
 1.52789 +      assert( pCur->eState==CURSOR_VALID );
 1.52790 +      *pRes = 0;
 1.52791 +      rc = moveToRightmost(pCur);
 1.52792 +      pCur->atLast = rc==SQLITE_OK ?1:0;
 1.52793 +    }
 1.52794 +  }
 1.52795 +  return rc;
 1.52796 +}
 1.52797 +
 1.52798 +/* Move the cursor so that it points to an entry near the key 
 1.52799 +** specified by pIdxKey or intKey.   Return a success code.
 1.52800 +**
 1.52801 +** For INTKEY tables, the intKey parameter is used.  pIdxKey 
 1.52802 +** must be NULL.  For index tables, pIdxKey is used and intKey
 1.52803 +** is ignored.
 1.52804 +**
 1.52805 +** If an exact match is not found, then the cursor is always
 1.52806 +** left pointing at a leaf page which would hold the entry if it
 1.52807 +** were present.  The cursor might point to an entry that comes
 1.52808 +** before or after the key.
 1.52809 +**
 1.52810 +** An integer is written into *pRes which is the result of
 1.52811 +** comparing the key with the entry to which the cursor is 
 1.52812 +** pointing.  The meaning of the integer written into
 1.52813 +** *pRes is as follows:
 1.52814 +**
 1.52815 +**     *pRes<0      The cursor is left pointing at an entry that
 1.52816 +**                  is smaller than intKey/pIdxKey or if the table is empty
 1.52817 +**                  and the cursor is therefore left point to nothing.
 1.52818 +**
 1.52819 +**     *pRes==0     The cursor is left pointing at an entry that
 1.52820 +**                  exactly matches intKey/pIdxKey.
 1.52821 +**
 1.52822 +**     *pRes>0      The cursor is left pointing at an entry that
 1.52823 +**                  is larger than intKey/pIdxKey.
 1.52824 +**
 1.52825 +*/
 1.52826 +SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
 1.52827 +  BtCursor *pCur,          /* The cursor to be moved */
 1.52828 +  UnpackedRecord *pIdxKey, /* Unpacked index key */
 1.52829 +  i64 intKey,              /* The table key */
 1.52830 +  int biasRight,           /* If true, bias the search to the high end */
 1.52831 +  int *pRes                /* Write search results here */
 1.52832 +){
 1.52833 +  int rc;
 1.52834 +
 1.52835 +  assert( cursorHoldsMutex(pCur) );
 1.52836 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 1.52837 +  assert( pRes );
 1.52838 +  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
 1.52839 +
 1.52840 +  /* If the cursor is already positioned at the point we are trying
 1.52841 +  ** to move to, then just return without doing any work */
 1.52842 +  if( pCur->eState==CURSOR_VALID && pCur->validNKey 
 1.52843 +   && pCur->apPage[0]->intKey 
 1.52844 +  ){
 1.52845 +    if( pCur->info.nKey==intKey ){
 1.52846 +      *pRes = 0;
 1.52847 +      return SQLITE_OK;
 1.52848 +    }
 1.52849 +    if( pCur->atLast && pCur->info.nKey<intKey ){
 1.52850 +      *pRes = -1;
 1.52851 +      return SQLITE_OK;
 1.52852 +    }
 1.52853 +  }
 1.52854 +
 1.52855 +  rc = moveToRoot(pCur);
 1.52856 +  if( rc ){
 1.52857 +    return rc;
 1.52858 +  }
 1.52859 +  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
 1.52860 +  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
 1.52861 +  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
 1.52862 +  if( pCur->eState==CURSOR_INVALID ){
 1.52863 +    *pRes = -1;
 1.52864 +    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 1.52865 +    return SQLITE_OK;
 1.52866 +  }
 1.52867 +  assert( pCur->apPage[0]->intKey || pIdxKey );
 1.52868 +  for(;;){
 1.52869 +    int lwr, upr, idx;
 1.52870 +    Pgno chldPg;
 1.52871 +    MemPage *pPage = pCur->apPage[pCur->iPage];
 1.52872 +    int c;
 1.52873 +
 1.52874 +    /* pPage->nCell must be greater than zero. If this is the root-page
 1.52875 +    ** the cursor would have been INVALID above and this for(;;) loop
 1.52876 +    ** not run. If this is not the root-page, then the moveToChild() routine
 1.52877 +    ** would have already detected db corruption. Similarly, pPage must
 1.52878 +    ** be the right kind (index or table) of b-tree page. Otherwise
 1.52879 +    ** a moveToChild() or moveToRoot() call would have detected corruption.  */
 1.52880 +    assert( pPage->nCell>0 );
 1.52881 +    assert( pPage->intKey==(pIdxKey==0) );
 1.52882 +    lwr = 0;
 1.52883 +    upr = pPage->nCell-1;
 1.52884 +    if( biasRight ){
 1.52885 +      pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
 1.52886 +    }else{
 1.52887 +      pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
 1.52888 +    }
 1.52889 +    for(;;){
 1.52890 +      u8 *pCell;                          /* Pointer to current cell in pPage */
 1.52891 +
 1.52892 +      assert( idx==pCur->aiIdx[pCur->iPage] );
 1.52893 +      pCur->info.nSize = 0;
 1.52894 +      pCell = findCell(pPage, idx) + pPage->childPtrSize;
 1.52895 +      if( pPage->intKey ){
 1.52896 +        i64 nCellKey;
 1.52897 +        if( pPage->hasData ){
 1.52898 +          u32 dummy;
 1.52899 +          pCell += getVarint32(pCell, dummy);
 1.52900 +        }
 1.52901 +        getVarint(pCell, (u64*)&nCellKey);
 1.52902 +        if( nCellKey==intKey ){
 1.52903 +          c = 0;
 1.52904 +        }else if( nCellKey<intKey ){
 1.52905 +          c = -1;
 1.52906 +        }else{
 1.52907 +          assert( nCellKey>intKey );
 1.52908 +          c = +1;
 1.52909 +        }
 1.52910 +        pCur->validNKey = 1;
 1.52911 +        pCur->info.nKey = nCellKey;
 1.52912 +      }else{
 1.52913 +        /* The maximum supported page-size is 65536 bytes. This means that
 1.52914 +        ** the maximum number of record bytes stored on an index B-Tree
 1.52915 +        ** page is less than 16384 bytes and may be stored as a 2-byte
 1.52916 +        ** varint. This information is used to attempt to avoid parsing 
 1.52917 +        ** the entire cell by checking for the cases where the record is 
 1.52918 +        ** stored entirely within the b-tree page by inspecting the first 
 1.52919 +        ** 2 bytes of the cell.
 1.52920 +        */
 1.52921 +        int nCell = pCell[0];
 1.52922 +        if( nCell<=pPage->max1bytePayload
 1.52923 +         /* && (pCell+nCell)<pPage->aDataEnd */
 1.52924 +        ){
 1.52925 +          /* This branch runs if the record-size field of the cell is a
 1.52926 +          ** single byte varint and the record fits entirely on the main
 1.52927 +          ** b-tree page.  */
 1.52928 +          testcase( pCell+nCell+1==pPage->aDataEnd );
 1.52929 +          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
 1.52930 +        }else if( !(pCell[1] & 0x80) 
 1.52931 +          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
 1.52932 +          /* && (pCell+nCell+2)<=pPage->aDataEnd */
 1.52933 +        ){
 1.52934 +          /* The record-size field is a 2 byte varint and the record 
 1.52935 +          ** fits entirely on the main b-tree page.  */
 1.52936 +          testcase( pCell+nCell+2==pPage->aDataEnd );
 1.52937 +          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
 1.52938 +        }else{
 1.52939 +          /* The record flows over onto one or more overflow pages. In
 1.52940 +          ** this case the whole cell needs to be parsed, a buffer allocated
 1.52941 +          ** and accessPayload() used to retrieve the record into the
 1.52942 +          ** buffer before VdbeRecordCompare() can be called. */
 1.52943 +          void *pCellKey;
 1.52944 +          u8 * const pCellBody = pCell - pPage->childPtrSize;
 1.52945 +          btreeParseCellPtr(pPage, pCellBody, &pCur->info);
 1.52946 +          nCell = (int)pCur->info.nKey;
 1.52947 +          pCellKey = sqlite3Malloc( nCell );
 1.52948 +          if( pCellKey==0 ){
 1.52949 +            rc = SQLITE_NOMEM;
 1.52950 +            goto moveto_finish;
 1.52951 +          }
 1.52952 +          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
 1.52953 +          if( rc ){
 1.52954 +            sqlite3_free(pCellKey);
 1.52955 +            goto moveto_finish;
 1.52956 +          }
 1.52957 +          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
 1.52958 +          sqlite3_free(pCellKey);
 1.52959 +        }
 1.52960 +      }
 1.52961 +      if( c==0 ){
 1.52962 +        if( pPage->intKey && !pPage->leaf ){
 1.52963 +          lwr = idx;
 1.52964 +          break;
 1.52965 +        }else{
 1.52966 +          *pRes = 0;
 1.52967 +          rc = SQLITE_OK;
 1.52968 +          goto moveto_finish;
 1.52969 +        }
 1.52970 +      }
 1.52971 +      if( c<0 ){
 1.52972 +        lwr = idx+1;
 1.52973 +      }else{
 1.52974 +        upr = idx-1;
 1.52975 +      }
 1.52976 +      if( lwr>upr ){
 1.52977 +        break;
 1.52978 +      }
 1.52979 +      pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
 1.52980 +    }
 1.52981 +    assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
 1.52982 +    assert( pPage->isInit );
 1.52983 +    if( pPage->leaf ){
 1.52984 +      chldPg = 0;
 1.52985 +    }else if( lwr>=pPage->nCell ){
 1.52986 +      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 1.52987 +    }else{
 1.52988 +      chldPg = get4byte(findCell(pPage, lwr));
 1.52989 +    }
 1.52990 +    if( chldPg==0 ){
 1.52991 +      assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 1.52992 +      *pRes = c;
 1.52993 +      rc = SQLITE_OK;
 1.52994 +      goto moveto_finish;
 1.52995 +    }
 1.52996 +    pCur->aiIdx[pCur->iPage] = (u16)lwr;
 1.52997 +    pCur->info.nSize = 0;
 1.52998 +    pCur->validNKey = 0;
 1.52999 +    rc = moveToChild(pCur, chldPg);
 1.53000 +    if( rc ) goto moveto_finish;
 1.53001 +  }
 1.53002 +moveto_finish:
 1.53003 +  return rc;
 1.53004 +}
 1.53005 +
 1.53006 +
 1.53007 +/*
 1.53008 +** Return TRUE if the cursor is not pointing at an entry of the table.
 1.53009 +**
 1.53010 +** TRUE will be returned after a call to sqlite3BtreeNext() moves
 1.53011 +** past the last entry in the table or sqlite3BtreePrev() moves past
 1.53012 +** the first entry.  TRUE is also returned if the table is empty.
 1.53013 +*/
 1.53014 +SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
 1.53015 +  /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
 1.53016 +  ** have been deleted? This API will need to change to return an error code
 1.53017 +  ** as well as the boolean result value.
 1.53018 +  */
 1.53019 +  return (CURSOR_VALID!=pCur->eState);
 1.53020 +}
 1.53021 +
 1.53022 +/*
 1.53023 +** Advance the cursor to the next entry in the database.  If
 1.53024 +** successful then set *pRes=0.  If the cursor
 1.53025 +** was already pointing to the last entry in the database before
 1.53026 +** this routine was called, then set *pRes=1.
 1.53027 +*/
 1.53028 +SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
 1.53029 +  int rc;
 1.53030 +  int idx;
 1.53031 +  MemPage *pPage;
 1.53032 +
 1.53033 +  assert( cursorHoldsMutex(pCur) );
 1.53034 +  rc = restoreCursorPosition(pCur);
 1.53035 +  if( rc!=SQLITE_OK ){
 1.53036 +    return rc;
 1.53037 +  }
 1.53038 +  assert( pRes!=0 );
 1.53039 +  if( CURSOR_INVALID==pCur->eState ){
 1.53040 +    *pRes = 1;
 1.53041 +    return SQLITE_OK;
 1.53042 +  }
 1.53043 +  if( pCur->skipNext>0 ){
 1.53044 +    pCur->skipNext = 0;
 1.53045 +    *pRes = 0;
 1.53046 +    return SQLITE_OK;
 1.53047 +  }
 1.53048 +  pCur->skipNext = 0;
 1.53049 +
 1.53050 +  pPage = pCur->apPage[pCur->iPage];
 1.53051 +  idx = ++pCur->aiIdx[pCur->iPage];
 1.53052 +  assert( pPage->isInit );
 1.53053 +
 1.53054 +  /* If the database file is corrupt, it is possible for the value of idx 
 1.53055 +  ** to be invalid here. This can only occur if a second cursor modifies
 1.53056 +  ** the page while cursor pCur is holding a reference to it. Which can
 1.53057 +  ** only happen if the database is corrupt in such a way as to link the
 1.53058 +  ** page into more than one b-tree structure. */
 1.53059 +  testcase( idx>pPage->nCell );
 1.53060 +
 1.53061 +  pCur->info.nSize = 0;
 1.53062 +  pCur->validNKey = 0;
 1.53063 +  if( idx>=pPage->nCell ){
 1.53064 +    if( !pPage->leaf ){
 1.53065 +      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
 1.53066 +      if( rc ) return rc;
 1.53067 +      rc = moveToLeftmost(pCur);
 1.53068 +      *pRes = 0;
 1.53069 +      return rc;
 1.53070 +    }
 1.53071 +    do{
 1.53072 +      if( pCur->iPage==0 ){
 1.53073 +        *pRes = 1;
 1.53074 +        pCur->eState = CURSOR_INVALID;
 1.53075 +        return SQLITE_OK;
 1.53076 +      }
 1.53077 +      moveToParent(pCur);
 1.53078 +      pPage = pCur->apPage[pCur->iPage];
 1.53079 +    }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
 1.53080 +    *pRes = 0;
 1.53081 +    if( pPage->intKey ){
 1.53082 +      rc = sqlite3BtreeNext(pCur, pRes);
 1.53083 +    }else{
 1.53084 +      rc = SQLITE_OK;
 1.53085 +    }
 1.53086 +    return rc;
 1.53087 +  }
 1.53088 +  *pRes = 0;
 1.53089 +  if( pPage->leaf ){
 1.53090 +    return SQLITE_OK;
 1.53091 +  }
 1.53092 +  rc = moveToLeftmost(pCur);
 1.53093 +  return rc;
 1.53094 +}
 1.53095 +
 1.53096 +
 1.53097 +/*
 1.53098 +** Step the cursor to the back to the previous entry in the database.  If
 1.53099 +** successful then set *pRes=0.  If the cursor
 1.53100 +** was already pointing to the first entry in the database before
 1.53101 +** this routine was called, then set *pRes=1.
 1.53102 +*/
 1.53103 +SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
 1.53104 +  int rc;
 1.53105 +  MemPage *pPage;
 1.53106 +
 1.53107 +  assert( cursorHoldsMutex(pCur) );
 1.53108 +  rc = restoreCursorPosition(pCur);
 1.53109 +  if( rc!=SQLITE_OK ){
 1.53110 +    return rc;
 1.53111 +  }
 1.53112 +  pCur->atLast = 0;
 1.53113 +  if( CURSOR_INVALID==pCur->eState ){
 1.53114 +    *pRes = 1;
 1.53115 +    return SQLITE_OK;
 1.53116 +  }
 1.53117 +  if( pCur->skipNext<0 ){
 1.53118 +    pCur->skipNext = 0;
 1.53119 +    *pRes = 0;
 1.53120 +    return SQLITE_OK;
 1.53121 +  }
 1.53122 +  pCur->skipNext = 0;
 1.53123 +
 1.53124 +  pPage = pCur->apPage[pCur->iPage];
 1.53125 +  assert( pPage->isInit );
 1.53126 +  if( !pPage->leaf ){
 1.53127 +    int idx = pCur->aiIdx[pCur->iPage];
 1.53128 +    rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
 1.53129 +    if( rc ){
 1.53130 +      return rc;
 1.53131 +    }
 1.53132 +    rc = moveToRightmost(pCur);
 1.53133 +  }else{
 1.53134 +    while( pCur->aiIdx[pCur->iPage]==0 ){
 1.53135 +      if( pCur->iPage==0 ){
 1.53136 +        pCur->eState = CURSOR_INVALID;
 1.53137 +        *pRes = 1;
 1.53138 +        return SQLITE_OK;
 1.53139 +      }
 1.53140 +      moveToParent(pCur);
 1.53141 +    }
 1.53142 +    pCur->info.nSize = 0;
 1.53143 +    pCur->validNKey = 0;
 1.53144 +
 1.53145 +    pCur->aiIdx[pCur->iPage]--;
 1.53146 +    pPage = pCur->apPage[pCur->iPage];
 1.53147 +    if( pPage->intKey && !pPage->leaf ){
 1.53148 +      rc = sqlite3BtreePrevious(pCur, pRes);
 1.53149 +    }else{
 1.53150 +      rc = SQLITE_OK;
 1.53151 +    }
 1.53152 +  }
 1.53153 +  *pRes = 0;
 1.53154 +  return rc;
 1.53155 +}
 1.53156 +
 1.53157 +/*
 1.53158 +** Allocate a new page from the database file.
 1.53159 +**
 1.53160 +** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
 1.53161 +** has already been called on the new page.)  The new page has also
 1.53162 +** been referenced and the calling routine is responsible for calling
 1.53163 +** sqlite3PagerUnref() on the new page when it is done.
 1.53164 +**
 1.53165 +** SQLITE_OK is returned on success.  Any other return value indicates
 1.53166 +** an error.  *ppPage and *pPgno are undefined in the event of an error.
 1.53167 +** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
 1.53168 +**
 1.53169 +** If the "nearby" parameter is not 0, then a (feeble) effort is made to 
 1.53170 +** locate a page close to the page number "nearby".  This can be used in an
 1.53171 +** attempt to keep related pages close to each other in the database file,
 1.53172 +** which in turn can make database access faster.
 1.53173 +**
 1.53174 +** If the "exact" parameter is not 0, and the page-number nearby exists 
 1.53175 +** anywhere on the free-list, then it is guarenteed to be returned. This
 1.53176 +** is only used by auto-vacuum databases when allocating a new table.
 1.53177 +*/
 1.53178 +static int allocateBtreePage(
 1.53179 +  BtShared *pBt, 
 1.53180 +  MemPage **ppPage, 
 1.53181 +  Pgno *pPgno, 
 1.53182 +  Pgno nearby,
 1.53183 +  u8 exact
 1.53184 +){
 1.53185 +  MemPage *pPage1;
 1.53186 +  int rc;
 1.53187 +  u32 n;     /* Number of pages on the freelist */
 1.53188 +  u32 k;     /* Number of leaves on the trunk of the freelist */
 1.53189 +  MemPage *pTrunk = 0;
 1.53190 +  MemPage *pPrevTrunk = 0;
 1.53191 +  Pgno mxPage;     /* Total size of the database file */
 1.53192 +
 1.53193 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.53194 +  pPage1 = pBt->pPage1;
 1.53195 +  mxPage = btreePagecount(pBt);
 1.53196 +  n = get4byte(&pPage1->aData[36]);
 1.53197 +  testcase( n==mxPage-1 );
 1.53198 +  if( n>=mxPage ){
 1.53199 +    return SQLITE_CORRUPT_BKPT;
 1.53200 +  }
 1.53201 +  if( n>0 ){
 1.53202 +    /* There are pages on the freelist.  Reuse one of those pages. */
 1.53203 +    Pgno iTrunk;
 1.53204 +    u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
 1.53205 +    
 1.53206 +    /* If the 'exact' parameter was true and a query of the pointer-map
 1.53207 +    ** shows that the page 'nearby' is somewhere on the free-list, then
 1.53208 +    ** the entire-list will be searched for that page.
 1.53209 +    */
 1.53210 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.53211 +    if( exact && nearby<=mxPage ){
 1.53212 +      u8 eType;
 1.53213 +      assert( nearby>0 );
 1.53214 +      assert( pBt->autoVacuum );
 1.53215 +      rc = ptrmapGet(pBt, nearby, &eType, 0);
 1.53216 +      if( rc ) return rc;
 1.53217 +      if( eType==PTRMAP_FREEPAGE ){
 1.53218 +        searchList = 1;
 1.53219 +      }
 1.53220 +      *pPgno = nearby;
 1.53221 +    }
 1.53222 +#endif
 1.53223 +
 1.53224 +    /* Decrement the free-list count by 1. Set iTrunk to the index of the
 1.53225 +    ** first free-list trunk page. iPrevTrunk is initially 1.
 1.53226 +    */
 1.53227 +    rc = sqlite3PagerWrite(pPage1->pDbPage);
 1.53228 +    if( rc ) return rc;
 1.53229 +    put4byte(&pPage1->aData[36], n-1);
 1.53230 +
 1.53231 +    /* The code within this loop is run only once if the 'searchList' variable
 1.53232 +    ** is not true. Otherwise, it runs once for each trunk-page on the
 1.53233 +    ** free-list until the page 'nearby' is located.
 1.53234 +    */
 1.53235 +    do {
 1.53236 +      pPrevTrunk = pTrunk;
 1.53237 +      if( pPrevTrunk ){
 1.53238 +        iTrunk = get4byte(&pPrevTrunk->aData[0]);
 1.53239 +      }else{
 1.53240 +        iTrunk = get4byte(&pPage1->aData[32]);
 1.53241 +      }
 1.53242 +      testcase( iTrunk==mxPage );
 1.53243 +      if( iTrunk>mxPage ){
 1.53244 +        rc = SQLITE_CORRUPT_BKPT;
 1.53245 +      }else{
 1.53246 +        rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 1.53247 +      }
 1.53248 +      if( rc ){
 1.53249 +        pTrunk = 0;
 1.53250 +        goto end_allocate_page;
 1.53251 +      }
 1.53252 +      assert( pTrunk!=0 );
 1.53253 +      assert( pTrunk->aData!=0 );
 1.53254 +
 1.53255 +      k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
 1.53256 +      if( k==0 && !searchList ){
 1.53257 +        /* The trunk has no leaves and the list is not being searched. 
 1.53258 +        ** So extract the trunk page itself and use it as the newly 
 1.53259 +        ** allocated page */
 1.53260 +        assert( pPrevTrunk==0 );
 1.53261 +        rc = sqlite3PagerWrite(pTrunk->pDbPage);
 1.53262 +        if( rc ){
 1.53263 +          goto end_allocate_page;
 1.53264 +        }
 1.53265 +        *pPgno = iTrunk;
 1.53266 +        memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
 1.53267 +        *ppPage = pTrunk;
 1.53268 +        pTrunk = 0;
 1.53269 +        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
 1.53270 +      }else if( k>(u32)(pBt->usableSize/4 - 2) ){
 1.53271 +        /* Value of k is out of range.  Database corruption */
 1.53272 +        rc = SQLITE_CORRUPT_BKPT;
 1.53273 +        goto end_allocate_page;
 1.53274 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.53275 +      }else if( searchList && nearby==iTrunk ){
 1.53276 +        /* The list is being searched and this trunk page is the page
 1.53277 +        ** to allocate, regardless of whether it has leaves.
 1.53278 +        */
 1.53279 +        assert( *pPgno==iTrunk );
 1.53280 +        *ppPage = pTrunk;
 1.53281 +        searchList = 0;
 1.53282 +        rc = sqlite3PagerWrite(pTrunk->pDbPage);
 1.53283 +        if( rc ){
 1.53284 +          goto end_allocate_page;
 1.53285 +        }
 1.53286 +        if( k==0 ){
 1.53287 +          if( !pPrevTrunk ){
 1.53288 +            memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
 1.53289 +          }else{
 1.53290 +            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
 1.53291 +            if( rc!=SQLITE_OK ){
 1.53292 +              goto end_allocate_page;
 1.53293 +            }
 1.53294 +            memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
 1.53295 +          }
 1.53296 +        }else{
 1.53297 +          /* The trunk page is required by the caller but it contains 
 1.53298 +          ** pointers to free-list leaves. The first leaf becomes a trunk
 1.53299 +          ** page in this case.
 1.53300 +          */
 1.53301 +          MemPage *pNewTrunk;
 1.53302 +          Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
 1.53303 +          if( iNewTrunk>mxPage ){ 
 1.53304 +            rc = SQLITE_CORRUPT_BKPT;
 1.53305 +            goto end_allocate_page;
 1.53306 +          }
 1.53307 +          testcase( iNewTrunk==mxPage );
 1.53308 +          rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
 1.53309 +          if( rc!=SQLITE_OK ){
 1.53310 +            goto end_allocate_page;
 1.53311 +          }
 1.53312 +          rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
 1.53313 +          if( rc!=SQLITE_OK ){
 1.53314 +            releasePage(pNewTrunk);
 1.53315 +            goto end_allocate_page;
 1.53316 +          }
 1.53317 +          memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
 1.53318 +          put4byte(&pNewTrunk->aData[4], k-1);
 1.53319 +          memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
 1.53320 +          releasePage(pNewTrunk);
 1.53321 +          if( !pPrevTrunk ){
 1.53322 +            assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
 1.53323 +            put4byte(&pPage1->aData[32], iNewTrunk);
 1.53324 +          }else{
 1.53325 +            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
 1.53326 +            if( rc ){
 1.53327 +              goto end_allocate_page;
 1.53328 +            }
 1.53329 +            put4byte(&pPrevTrunk->aData[0], iNewTrunk);
 1.53330 +          }
 1.53331 +        }
 1.53332 +        pTrunk = 0;
 1.53333 +        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
 1.53334 +#endif
 1.53335 +      }else if( k>0 ){
 1.53336 +        /* Extract a leaf from the trunk */
 1.53337 +        u32 closest;
 1.53338 +        Pgno iPage;
 1.53339 +        unsigned char *aData = pTrunk->aData;
 1.53340 +        if( nearby>0 ){
 1.53341 +          u32 i;
 1.53342 +          int dist;
 1.53343 +          closest = 0;
 1.53344 +          dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
 1.53345 +          for(i=1; i<k; i++){
 1.53346 +            int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
 1.53347 +            if( d2<dist ){
 1.53348 +              closest = i;
 1.53349 +              dist = d2;
 1.53350 +            }
 1.53351 +          }
 1.53352 +        }else{
 1.53353 +          closest = 0;
 1.53354 +        }
 1.53355 +
 1.53356 +        iPage = get4byte(&aData[8+closest*4]);
 1.53357 +        testcase( iPage==mxPage );
 1.53358 +        if( iPage>mxPage ){
 1.53359 +          rc = SQLITE_CORRUPT_BKPT;
 1.53360 +          goto end_allocate_page;
 1.53361 +        }
 1.53362 +        testcase( iPage==mxPage );
 1.53363 +        if( !searchList || iPage==nearby ){
 1.53364 +          int noContent;
 1.53365 +          *pPgno = iPage;
 1.53366 +          TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
 1.53367 +                 ": %d more free pages\n",
 1.53368 +                 *pPgno, closest+1, k, pTrunk->pgno, n-1));
 1.53369 +          rc = sqlite3PagerWrite(pTrunk->pDbPage);
 1.53370 +          if( rc ) goto end_allocate_page;
 1.53371 +          if( closest<k-1 ){
 1.53372 +            memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
 1.53373 +          }
 1.53374 +          put4byte(&aData[4], k-1);
 1.53375 +          noContent = !btreeGetHasContent(pBt, *pPgno);
 1.53376 +          rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
 1.53377 +          if( rc==SQLITE_OK ){
 1.53378 +            rc = sqlite3PagerWrite((*ppPage)->pDbPage);
 1.53379 +            if( rc!=SQLITE_OK ){
 1.53380 +              releasePage(*ppPage);
 1.53381 +            }
 1.53382 +          }
 1.53383 +          searchList = 0;
 1.53384 +        }
 1.53385 +      }
 1.53386 +      releasePage(pPrevTrunk);
 1.53387 +      pPrevTrunk = 0;
 1.53388 +    }while( searchList );
 1.53389 +  }else{
 1.53390 +    /* There are no pages on the freelist, so create a new page at the
 1.53391 +    ** end of the file */
 1.53392 +    rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 1.53393 +    if( rc ) return rc;
 1.53394 +    pBt->nPage++;
 1.53395 +    if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
 1.53396 +
 1.53397 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.53398 +    if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
 1.53399 +      /* If *pPgno refers to a pointer-map page, allocate two new pages
 1.53400 +      ** at the end of the file instead of one. The first allocated page
 1.53401 +      ** becomes a new pointer-map page, the second is used by the caller.
 1.53402 +      */
 1.53403 +      MemPage *pPg = 0;
 1.53404 +      TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
 1.53405 +      assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
 1.53406 +      rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
 1.53407 +      if( rc==SQLITE_OK ){
 1.53408 +        rc = sqlite3PagerWrite(pPg->pDbPage);
 1.53409 +        releasePage(pPg);
 1.53410 +      }
 1.53411 +      if( rc ) return rc;
 1.53412 +      pBt->nPage++;
 1.53413 +      if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
 1.53414 +    }
 1.53415 +#endif
 1.53416 +    put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
 1.53417 +    *pPgno = pBt->nPage;
 1.53418 +
 1.53419 +    assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
 1.53420 +    rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
 1.53421 +    if( rc ) return rc;
 1.53422 +    rc = sqlite3PagerWrite((*ppPage)->pDbPage);
 1.53423 +    if( rc!=SQLITE_OK ){
 1.53424 +      releasePage(*ppPage);
 1.53425 +    }
 1.53426 +    TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
 1.53427 +  }
 1.53428 +
 1.53429 +  assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
 1.53430 +
 1.53431 +end_allocate_page:
 1.53432 +  releasePage(pTrunk);
 1.53433 +  releasePage(pPrevTrunk);
 1.53434 +  if( rc==SQLITE_OK ){
 1.53435 +    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
 1.53436 +      releasePage(*ppPage);
 1.53437 +      return SQLITE_CORRUPT_BKPT;
 1.53438 +    }
 1.53439 +    (*ppPage)->isInit = 0;
 1.53440 +  }else{
 1.53441 +    *ppPage = 0;
 1.53442 +  }
 1.53443 +  assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
 1.53444 +  return rc;
 1.53445 +}
 1.53446 +
 1.53447 +/*
 1.53448 +** This function is used to add page iPage to the database file free-list. 
 1.53449 +** It is assumed that the page is not already a part of the free-list.
 1.53450 +**
 1.53451 +** The value passed as the second argument to this function is optional.
 1.53452 +** If the caller happens to have a pointer to the MemPage object 
 1.53453 +** corresponding to page iPage handy, it may pass it as the second value. 
 1.53454 +** Otherwise, it may pass NULL.
 1.53455 +**
 1.53456 +** If a pointer to a MemPage object is passed as the second argument,
 1.53457 +** its reference count is not altered by this function.
 1.53458 +*/
 1.53459 +static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
 1.53460 +  MemPage *pTrunk = 0;                /* Free-list trunk page */
 1.53461 +  Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
 1.53462 +  MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
 1.53463 +  MemPage *pPage;                     /* Page being freed. May be NULL. */
 1.53464 +  int rc;                             /* Return Code */
 1.53465 +  int nFree;                          /* Initial number of pages on free-list */
 1.53466 +
 1.53467 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.53468 +  assert( iPage>1 );
 1.53469 +  assert( !pMemPage || pMemPage->pgno==iPage );
 1.53470 +
 1.53471 +  if( pMemPage ){
 1.53472 +    pPage = pMemPage;
 1.53473 +    sqlite3PagerRef(pPage->pDbPage);
 1.53474 +  }else{
 1.53475 +    pPage = btreePageLookup(pBt, iPage);
 1.53476 +  }
 1.53477 +
 1.53478 +  /* Increment the free page count on pPage1 */
 1.53479 +  rc = sqlite3PagerWrite(pPage1->pDbPage);
 1.53480 +  if( rc ) goto freepage_out;
 1.53481 +  nFree = get4byte(&pPage1->aData[36]);
 1.53482 +  put4byte(&pPage1->aData[36], nFree+1);
 1.53483 +
 1.53484 +  if( pBt->btsFlags & BTS_SECURE_DELETE ){
 1.53485 +    /* If the secure_delete option is enabled, then
 1.53486 +    ** always fully overwrite deleted information with zeros.
 1.53487 +    */
 1.53488 +    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
 1.53489 +     ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
 1.53490 +    ){
 1.53491 +      goto freepage_out;
 1.53492 +    }
 1.53493 +    memset(pPage->aData, 0, pPage->pBt->pageSize);
 1.53494 +  }
 1.53495 +
 1.53496 +  /* If the database supports auto-vacuum, write an entry in the pointer-map
 1.53497 +  ** to indicate that the page is free.
 1.53498 +  */
 1.53499 +  if( ISAUTOVACUUM ){
 1.53500 +    ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
 1.53501 +    if( rc ) goto freepage_out;
 1.53502 +  }
 1.53503 +
 1.53504 +  /* Now manipulate the actual database free-list structure. There are two
 1.53505 +  ** possibilities. If the free-list is currently empty, or if the first
 1.53506 +  ** trunk page in the free-list is full, then this page will become a
 1.53507 +  ** new free-list trunk page. Otherwise, it will become a leaf of the
 1.53508 +  ** first trunk page in the current free-list. This block tests if it
 1.53509 +  ** is possible to add the page as a new free-list leaf.
 1.53510 +  */
 1.53511 +  if( nFree!=0 ){
 1.53512 +    u32 nLeaf;                /* Initial number of leaf cells on trunk page */
 1.53513 +
 1.53514 +    iTrunk = get4byte(&pPage1->aData[32]);
 1.53515 +    rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 1.53516 +    if( rc!=SQLITE_OK ){
 1.53517 +      goto freepage_out;
 1.53518 +    }
 1.53519 +
 1.53520 +    nLeaf = get4byte(&pTrunk->aData[4]);
 1.53521 +    assert( pBt->usableSize>32 );
 1.53522 +    if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
 1.53523 +      rc = SQLITE_CORRUPT_BKPT;
 1.53524 +      goto freepage_out;
 1.53525 +    }
 1.53526 +    if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
 1.53527 +      /* In this case there is room on the trunk page to insert the page
 1.53528 +      ** being freed as a new leaf.
 1.53529 +      **
 1.53530 +      ** Note that the trunk page is not really full until it contains
 1.53531 +      ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
 1.53532 +      ** coded.  But due to a coding error in versions of SQLite prior to
 1.53533 +      ** 3.6.0, databases with freelist trunk pages holding more than
 1.53534 +      ** usableSize/4 - 8 entries will be reported as corrupt.  In order
 1.53535 +      ** to maintain backwards compatibility with older versions of SQLite,
 1.53536 +      ** we will continue to restrict the number of entries to usableSize/4 - 8
 1.53537 +      ** for now.  At some point in the future (once everyone has upgraded
 1.53538 +      ** to 3.6.0 or later) we should consider fixing the conditional above
 1.53539 +      ** to read "usableSize/4-2" instead of "usableSize/4-8".
 1.53540 +      */
 1.53541 +      rc = sqlite3PagerWrite(pTrunk->pDbPage);
 1.53542 +      if( rc==SQLITE_OK ){
 1.53543 +        put4byte(&pTrunk->aData[4], nLeaf+1);
 1.53544 +        put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
 1.53545 +        if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
 1.53546 +          sqlite3PagerDontWrite(pPage->pDbPage);
 1.53547 +        }
 1.53548 +        rc = btreeSetHasContent(pBt, iPage);
 1.53549 +      }
 1.53550 +      TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
 1.53551 +      goto freepage_out;
 1.53552 +    }
 1.53553 +  }
 1.53554 +
 1.53555 +  /* If control flows to this point, then it was not possible to add the
 1.53556 +  ** the page being freed as a leaf page of the first trunk in the free-list.
 1.53557 +  ** Possibly because the free-list is empty, or possibly because the 
 1.53558 +  ** first trunk in the free-list is full. Either way, the page being freed
 1.53559 +  ** will become the new first trunk page in the free-list.
 1.53560 +  */
 1.53561 +  if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
 1.53562 +    goto freepage_out;
 1.53563 +  }
 1.53564 +  rc = sqlite3PagerWrite(pPage->pDbPage);
 1.53565 +  if( rc!=SQLITE_OK ){
 1.53566 +    goto freepage_out;
 1.53567 +  }
 1.53568 +  put4byte(pPage->aData, iTrunk);
 1.53569 +  put4byte(&pPage->aData[4], 0);
 1.53570 +  put4byte(&pPage1->aData[32], iPage);
 1.53571 +  TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
 1.53572 +
 1.53573 +freepage_out:
 1.53574 +  if( pPage ){
 1.53575 +    pPage->isInit = 0;
 1.53576 +  }
 1.53577 +  releasePage(pPage);
 1.53578 +  releasePage(pTrunk);
 1.53579 +  return rc;
 1.53580 +}
 1.53581 +static void freePage(MemPage *pPage, int *pRC){
 1.53582 +  if( (*pRC)==SQLITE_OK ){
 1.53583 +    *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
 1.53584 +  }
 1.53585 +}
 1.53586 +
 1.53587 +/*
 1.53588 +** Free any overflow pages associated with the given Cell.
 1.53589 +*/
 1.53590 +static int clearCell(MemPage *pPage, unsigned char *pCell){
 1.53591 +  BtShared *pBt = pPage->pBt;
 1.53592 +  CellInfo info;
 1.53593 +  Pgno ovflPgno;
 1.53594 +  int rc;
 1.53595 +  int nOvfl;
 1.53596 +  u32 ovflPageSize;
 1.53597 +
 1.53598 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.53599 +  btreeParseCellPtr(pPage, pCell, &info);
 1.53600 +  if( info.iOverflow==0 ){
 1.53601 +    return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 1.53602 +  }
 1.53603 +  if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
 1.53604 +    return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
 1.53605 +  }
 1.53606 +  ovflPgno = get4byte(&pCell[info.iOverflow]);
 1.53607 +  assert( pBt->usableSize > 4 );
 1.53608 +  ovflPageSize = pBt->usableSize - 4;
 1.53609 +  nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
 1.53610 +  assert( ovflPgno==0 || nOvfl>0 );
 1.53611 +  while( nOvfl-- ){
 1.53612 +    Pgno iNext = 0;
 1.53613 +    MemPage *pOvfl = 0;
 1.53614 +    if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
 1.53615 +      /* 0 is not a legal page number and page 1 cannot be an 
 1.53616 +      ** overflow page. Therefore if ovflPgno<2 or past the end of the 
 1.53617 +      ** file the database must be corrupt. */
 1.53618 +      return SQLITE_CORRUPT_BKPT;
 1.53619 +    }
 1.53620 +    if( nOvfl ){
 1.53621 +      rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
 1.53622 +      if( rc ) return rc;
 1.53623 +    }
 1.53624 +
 1.53625 +    if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
 1.53626 +     && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
 1.53627 +    ){
 1.53628 +      /* There is no reason any cursor should have an outstanding reference 
 1.53629 +      ** to an overflow page belonging to a cell that is being deleted/updated.
 1.53630 +      ** So if there exists more than one reference to this page, then it 
 1.53631 +      ** must not really be an overflow page and the database must be corrupt. 
 1.53632 +      ** It is helpful to detect this before calling freePage2(), as 
 1.53633 +      ** freePage2() may zero the page contents if secure-delete mode is
 1.53634 +      ** enabled. If this 'overflow' page happens to be a page that the
 1.53635 +      ** caller is iterating through or using in some other way, this
 1.53636 +      ** can be problematic.
 1.53637 +      */
 1.53638 +      rc = SQLITE_CORRUPT_BKPT;
 1.53639 +    }else{
 1.53640 +      rc = freePage2(pBt, pOvfl, ovflPgno);
 1.53641 +    }
 1.53642 +
 1.53643 +    if( pOvfl ){
 1.53644 +      sqlite3PagerUnref(pOvfl->pDbPage);
 1.53645 +    }
 1.53646 +    if( rc ) return rc;
 1.53647 +    ovflPgno = iNext;
 1.53648 +  }
 1.53649 +  return SQLITE_OK;
 1.53650 +}
 1.53651 +
 1.53652 +/*
 1.53653 +** Create the byte sequence used to represent a cell on page pPage
 1.53654 +** and write that byte sequence into pCell[].  Overflow pages are
 1.53655 +** allocated and filled in as necessary.  The calling procedure
 1.53656 +** is responsible for making sure sufficient space has been allocated
 1.53657 +** for pCell[].
 1.53658 +**
 1.53659 +** Note that pCell does not necessary need to point to the pPage->aData
 1.53660 +** area.  pCell might point to some temporary storage.  The cell will
 1.53661 +** be constructed in this temporary area then copied into pPage->aData
 1.53662 +** later.
 1.53663 +*/
 1.53664 +static int fillInCell(
 1.53665 +  MemPage *pPage,                /* The page that contains the cell */
 1.53666 +  unsigned char *pCell,          /* Complete text of the cell */
 1.53667 +  const void *pKey, i64 nKey,    /* The key */
 1.53668 +  const void *pData,int nData,   /* The data */
 1.53669 +  int nZero,                     /* Extra zero bytes to append to pData */
 1.53670 +  int *pnSize                    /* Write cell size here */
 1.53671 +){
 1.53672 +  int nPayload;
 1.53673 +  const u8 *pSrc;
 1.53674 +  int nSrc, n, rc;
 1.53675 +  int spaceLeft;
 1.53676 +  MemPage *pOvfl = 0;
 1.53677 +  MemPage *pToRelease = 0;
 1.53678 +  unsigned char *pPrior;
 1.53679 +  unsigned char *pPayload;
 1.53680 +  BtShared *pBt = pPage->pBt;
 1.53681 +  Pgno pgnoOvfl = 0;
 1.53682 +  int nHeader;
 1.53683 +  CellInfo info;
 1.53684 +
 1.53685 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.53686 +
 1.53687 +  /* pPage is not necessarily writeable since pCell might be auxiliary
 1.53688 +  ** buffer space that is separate from the pPage buffer area */
 1.53689 +  assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
 1.53690 +            || sqlite3PagerIswriteable(pPage->pDbPage) );
 1.53691 +
 1.53692 +  /* Fill in the header. */
 1.53693 +  nHeader = 0;
 1.53694 +  if( !pPage->leaf ){
 1.53695 +    nHeader += 4;
 1.53696 +  }
 1.53697 +  if( pPage->hasData ){
 1.53698 +    nHeader += putVarint(&pCell[nHeader], nData+nZero);
 1.53699 +  }else{
 1.53700 +    nData = nZero = 0;
 1.53701 +  }
 1.53702 +  nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
 1.53703 +  btreeParseCellPtr(pPage, pCell, &info);
 1.53704 +  assert( info.nHeader==nHeader );
 1.53705 +  assert( info.nKey==nKey );
 1.53706 +  assert( info.nData==(u32)(nData+nZero) );
 1.53707 +  
 1.53708 +  /* Fill in the payload */
 1.53709 +  nPayload = nData + nZero;
 1.53710 +  if( pPage->intKey ){
 1.53711 +    pSrc = pData;
 1.53712 +    nSrc = nData;
 1.53713 +    nData = 0;
 1.53714 +  }else{ 
 1.53715 +    if( NEVER(nKey>0x7fffffff || pKey==0) ){
 1.53716 +      return SQLITE_CORRUPT_BKPT;
 1.53717 +    }
 1.53718 +    nPayload += (int)nKey;
 1.53719 +    pSrc = pKey;
 1.53720 +    nSrc = (int)nKey;
 1.53721 +  }
 1.53722 +  *pnSize = info.nSize;
 1.53723 +  spaceLeft = info.nLocal;
 1.53724 +  pPayload = &pCell[nHeader];
 1.53725 +  pPrior = &pCell[info.iOverflow];
 1.53726 +
 1.53727 +  while( nPayload>0 ){
 1.53728 +    if( spaceLeft==0 ){
 1.53729 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.53730 +      Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
 1.53731 +      if( pBt->autoVacuum ){
 1.53732 +        do{
 1.53733 +          pgnoOvfl++;
 1.53734 +        } while( 
 1.53735 +          PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
 1.53736 +        );
 1.53737 +      }
 1.53738 +#endif
 1.53739 +      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
 1.53740 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.53741 +      /* If the database supports auto-vacuum, and the second or subsequent
 1.53742 +      ** overflow page is being allocated, add an entry to the pointer-map
 1.53743 +      ** for that page now. 
 1.53744 +      **
 1.53745 +      ** If this is the first overflow page, then write a partial entry 
 1.53746 +      ** to the pointer-map. If we write nothing to this pointer-map slot,
 1.53747 +      ** then the optimistic overflow chain processing in clearCell()
 1.53748 +      ** may misinterpret the uninitialised values and delete the
 1.53749 +      ** wrong pages from the database.
 1.53750 +      */
 1.53751 +      if( pBt->autoVacuum && rc==SQLITE_OK ){
 1.53752 +        u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
 1.53753 +        ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
 1.53754 +        if( rc ){
 1.53755 +          releasePage(pOvfl);
 1.53756 +        }
 1.53757 +      }
 1.53758 +#endif
 1.53759 +      if( rc ){
 1.53760 +        releasePage(pToRelease);
 1.53761 +        return rc;
 1.53762 +      }
 1.53763 +
 1.53764 +      /* If pToRelease is not zero than pPrior points into the data area
 1.53765 +      ** of pToRelease.  Make sure pToRelease is still writeable. */
 1.53766 +      assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
 1.53767 +
 1.53768 +      /* If pPrior is part of the data area of pPage, then make sure pPage
 1.53769 +      ** is still writeable */
 1.53770 +      assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
 1.53771 +            || sqlite3PagerIswriteable(pPage->pDbPage) );
 1.53772 +
 1.53773 +      put4byte(pPrior, pgnoOvfl);
 1.53774 +      releasePage(pToRelease);
 1.53775 +      pToRelease = pOvfl;
 1.53776 +      pPrior = pOvfl->aData;
 1.53777 +      put4byte(pPrior, 0);
 1.53778 +      pPayload = &pOvfl->aData[4];
 1.53779 +      spaceLeft = pBt->usableSize - 4;
 1.53780 +    }
 1.53781 +    n = nPayload;
 1.53782 +    if( n>spaceLeft ) n = spaceLeft;
 1.53783 +
 1.53784 +    /* If pToRelease is not zero than pPayload points into the data area
 1.53785 +    ** of pToRelease.  Make sure pToRelease is still writeable. */
 1.53786 +    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
 1.53787 +
 1.53788 +    /* If pPayload is part of the data area of pPage, then make sure pPage
 1.53789 +    ** is still writeable */
 1.53790 +    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
 1.53791 +            || sqlite3PagerIswriteable(pPage->pDbPage) );
 1.53792 +
 1.53793 +    if( nSrc>0 ){
 1.53794 +      if( n>nSrc ) n = nSrc;
 1.53795 +      assert( pSrc );
 1.53796 +      memcpy(pPayload, pSrc, n);
 1.53797 +    }else{
 1.53798 +      memset(pPayload, 0, n);
 1.53799 +    }
 1.53800 +    nPayload -= n;
 1.53801 +    pPayload += n;
 1.53802 +    pSrc += n;
 1.53803 +    nSrc -= n;
 1.53804 +    spaceLeft -= n;
 1.53805 +    if( nSrc==0 ){
 1.53806 +      nSrc = nData;
 1.53807 +      pSrc = pData;
 1.53808 +    }
 1.53809 +  }
 1.53810 +  releasePage(pToRelease);
 1.53811 +  return SQLITE_OK;
 1.53812 +}
 1.53813 +
 1.53814 +/*
 1.53815 +** Remove the i-th cell from pPage.  This routine effects pPage only.
 1.53816 +** The cell content is not freed or deallocated.  It is assumed that
 1.53817 +** the cell content has been copied someplace else.  This routine just
 1.53818 +** removes the reference to the cell from pPage.
 1.53819 +**
 1.53820 +** "sz" must be the number of bytes in the cell.
 1.53821 +*/
 1.53822 +static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
 1.53823 +  u32 pc;         /* Offset to cell content of cell being deleted */
 1.53824 +  u8 *data;       /* pPage->aData */
 1.53825 +  u8 *ptr;        /* Used to move bytes around within data[] */
 1.53826 +  u8 *endPtr;     /* End of loop */
 1.53827 +  int rc;         /* The return code */
 1.53828 +  int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
 1.53829 +
 1.53830 +  if( *pRC ) return;
 1.53831 +
 1.53832 +  assert( idx>=0 && idx<pPage->nCell );
 1.53833 +  assert( sz==cellSize(pPage, idx) );
 1.53834 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.53835 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.53836 +  data = pPage->aData;
 1.53837 +  ptr = &pPage->aCellIdx[2*idx];
 1.53838 +  pc = get2byte(ptr);
 1.53839 +  hdr = pPage->hdrOffset;
 1.53840 +  testcase( pc==get2byte(&data[hdr+5]) );
 1.53841 +  testcase( pc+sz==pPage->pBt->usableSize );
 1.53842 +  if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
 1.53843 +    *pRC = SQLITE_CORRUPT_BKPT;
 1.53844 +    return;
 1.53845 +  }
 1.53846 +  rc = freeSpace(pPage, pc, sz);
 1.53847 +  if( rc ){
 1.53848 +    *pRC = rc;
 1.53849 +    return;
 1.53850 +  }
 1.53851 +  endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
 1.53852 +  assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
 1.53853 +  while( ptr<endPtr ){
 1.53854 +    *(u16*)ptr = *(u16*)&ptr[2];
 1.53855 +    ptr += 2;
 1.53856 +  }
 1.53857 +  pPage->nCell--;
 1.53858 +  put2byte(&data[hdr+3], pPage->nCell);
 1.53859 +  pPage->nFree += 2;
 1.53860 +}
 1.53861 +
 1.53862 +/*
 1.53863 +** Insert a new cell on pPage at cell index "i".  pCell points to the
 1.53864 +** content of the cell.
 1.53865 +**
 1.53866 +** If the cell content will fit on the page, then put it there.  If it
 1.53867 +** will not fit, then make a copy of the cell content into pTemp if
 1.53868 +** pTemp is not null.  Regardless of pTemp, allocate a new entry
 1.53869 +** in pPage->apOvfl[] and make it point to the cell content (either
 1.53870 +** in pTemp or the original pCell) and also record its index. 
 1.53871 +** Allocating a new entry in pPage->aCell[] implies that 
 1.53872 +** pPage->nOverflow is incremented.
 1.53873 +**
 1.53874 +** If nSkip is non-zero, then do not copy the first nSkip bytes of the
 1.53875 +** cell. The caller will overwrite them after this function returns. If
 1.53876 +** nSkip is non-zero, then pCell may not point to an invalid memory location 
 1.53877 +** (but pCell+nSkip is always valid).
 1.53878 +*/
 1.53879 +static void insertCell(
 1.53880 +  MemPage *pPage,   /* Page into which we are copying */
 1.53881 +  int i,            /* New cell becomes the i-th cell of the page */
 1.53882 +  u8 *pCell,        /* Content of the new cell */
 1.53883 +  int sz,           /* Bytes of content in pCell */
 1.53884 +  u8 *pTemp,        /* Temp storage space for pCell, if needed */
 1.53885 +  Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
 1.53886 +  int *pRC          /* Read and write return code from here */
 1.53887 +){
 1.53888 +  int idx = 0;      /* Where to write new cell content in data[] */
 1.53889 +  int j;            /* Loop counter */
 1.53890 +  int end;          /* First byte past the last cell pointer in data[] */
 1.53891 +  int ins;          /* Index in data[] where new cell pointer is inserted */
 1.53892 +  int cellOffset;   /* Address of first cell pointer in data[] */
 1.53893 +  u8 *data;         /* The content of the whole page */
 1.53894 +  u8 *ptr;          /* Used for moving information around in data[] */
 1.53895 +  u8 *endPtr;       /* End of the loop */
 1.53896 +
 1.53897 +  int nSkip = (iChild ? 4 : 0);
 1.53898 +
 1.53899 +  if( *pRC ) return;
 1.53900 +
 1.53901 +  assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
 1.53902 +  assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
 1.53903 +  assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
 1.53904 +  assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
 1.53905 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.53906 +  /* The cell should normally be sized correctly.  However, when moving a
 1.53907 +  ** malformed cell from a leaf page to an interior page, if the cell size
 1.53908 +  ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
 1.53909 +  ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
 1.53910 +  ** the term after the || in the following assert(). */
 1.53911 +  assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
 1.53912 +  if( pPage->nOverflow || sz+2>pPage->nFree ){
 1.53913 +    if( pTemp ){
 1.53914 +      memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
 1.53915 +      pCell = pTemp;
 1.53916 +    }
 1.53917 +    if( iChild ){
 1.53918 +      put4byte(pCell, iChild);
 1.53919 +    }
 1.53920 +    j = pPage->nOverflow++;
 1.53921 +    assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
 1.53922 +    pPage->apOvfl[j] = pCell;
 1.53923 +    pPage->aiOvfl[j] = (u16)i;
 1.53924 +  }else{
 1.53925 +    int rc = sqlite3PagerWrite(pPage->pDbPage);
 1.53926 +    if( rc!=SQLITE_OK ){
 1.53927 +      *pRC = rc;
 1.53928 +      return;
 1.53929 +    }
 1.53930 +    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.53931 +    data = pPage->aData;
 1.53932 +    cellOffset = pPage->cellOffset;
 1.53933 +    end = cellOffset + 2*pPage->nCell;
 1.53934 +    ins = cellOffset + 2*i;
 1.53935 +    rc = allocateSpace(pPage, sz, &idx);
 1.53936 +    if( rc ){ *pRC = rc; return; }
 1.53937 +    /* The allocateSpace() routine guarantees the following two properties
 1.53938 +    ** if it returns success */
 1.53939 +    assert( idx >= end+2 );
 1.53940 +    assert( idx+sz <= (int)pPage->pBt->usableSize );
 1.53941 +    pPage->nCell++;
 1.53942 +    pPage->nFree -= (u16)(2 + sz);
 1.53943 +    memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
 1.53944 +    if( iChild ){
 1.53945 +      put4byte(&data[idx], iChild);
 1.53946 +    }
 1.53947 +    ptr = &data[end];
 1.53948 +    endPtr = &data[ins];
 1.53949 +    assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
 1.53950 +    while( ptr>endPtr ){
 1.53951 +      *(u16*)ptr = *(u16*)&ptr[-2];
 1.53952 +      ptr -= 2;
 1.53953 +    }
 1.53954 +    put2byte(&data[ins], idx);
 1.53955 +    put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
 1.53956 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.53957 +    if( pPage->pBt->autoVacuum ){
 1.53958 +      /* The cell may contain a pointer to an overflow page. If so, write
 1.53959 +      ** the entry for the overflow page into the pointer map.
 1.53960 +      */
 1.53961 +      ptrmapPutOvflPtr(pPage, pCell, pRC);
 1.53962 +    }
 1.53963 +#endif
 1.53964 +  }
 1.53965 +}
 1.53966 +
 1.53967 +/*
 1.53968 +** Add a list of cells to a page.  The page should be initially empty.
 1.53969 +** The cells are guaranteed to fit on the page.
 1.53970 +*/
 1.53971 +static void assemblePage(
 1.53972 +  MemPage *pPage,   /* The page to be assemblied */
 1.53973 +  int nCell,        /* The number of cells to add to this page */
 1.53974 +  u8 **apCell,      /* Pointers to cell bodies */
 1.53975 +  u16 *aSize        /* Sizes of the cells */
 1.53976 +){
 1.53977 +  int i;            /* Loop counter */
 1.53978 +  u8 *pCellptr;     /* Address of next cell pointer */
 1.53979 +  int cellbody;     /* Address of next cell body */
 1.53980 +  u8 * const data = pPage->aData;             /* Pointer to data for pPage */
 1.53981 +  const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
 1.53982 +  const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
 1.53983 +
 1.53984 +  assert( pPage->nOverflow==0 );
 1.53985 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.53986 +  assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
 1.53987 +            && (int)MX_CELL(pPage->pBt)<=10921);
 1.53988 +  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 1.53989 +
 1.53990 +  /* Check that the page has just been zeroed by zeroPage() */
 1.53991 +  assert( pPage->nCell==0 );
 1.53992 +  assert( get2byteNotZero(&data[hdr+5])==nUsable );
 1.53993 +
 1.53994 +  pCellptr = &pPage->aCellIdx[nCell*2];
 1.53995 +  cellbody = nUsable;
 1.53996 +  for(i=nCell-1; i>=0; i--){
 1.53997 +    u16 sz = aSize[i];
 1.53998 +    pCellptr -= 2;
 1.53999 +    cellbody -= sz;
 1.54000 +    put2byte(pCellptr, cellbody);
 1.54001 +    memcpy(&data[cellbody], apCell[i], sz);
 1.54002 +  }
 1.54003 +  put2byte(&data[hdr+3], nCell);
 1.54004 +  put2byte(&data[hdr+5], cellbody);
 1.54005 +  pPage->nFree -= (nCell*2 + nUsable - cellbody);
 1.54006 +  pPage->nCell = (u16)nCell;
 1.54007 +}
 1.54008 +
 1.54009 +/*
 1.54010 +** The following parameters determine how many adjacent pages get involved
 1.54011 +** in a balancing operation.  NN is the number of neighbors on either side
 1.54012 +** of the page that participate in the balancing operation.  NB is the
 1.54013 +** total number of pages that participate, including the target page and
 1.54014 +** NN neighbors on either side.
 1.54015 +**
 1.54016 +** The minimum value of NN is 1 (of course).  Increasing NN above 1
 1.54017 +** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
 1.54018 +** in exchange for a larger degradation in INSERT and UPDATE performance.
 1.54019 +** The value of NN appears to give the best results overall.
 1.54020 +*/
 1.54021 +#define NN 1             /* Number of neighbors on either side of pPage */
 1.54022 +#define NB (NN*2+1)      /* Total pages involved in the balance */
 1.54023 +
 1.54024 +
 1.54025 +#ifndef SQLITE_OMIT_QUICKBALANCE
 1.54026 +/*
 1.54027 +** This version of balance() handles the common special case where
 1.54028 +** a new entry is being inserted on the extreme right-end of the
 1.54029 +** tree, in other words, when the new entry will become the largest
 1.54030 +** entry in the tree.
 1.54031 +**
 1.54032 +** Instead of trying to balance the 3 right-most leaf pages, just add
 1.54033 +** a new page to the right-hand side and put the one new entry in
 1.54034 +** that page.  This leaves the right side of the tree somewhat
 1.54035 +** unbalanced.  But odds are that we will be inserting new entries
 1.54036 +** at the end soon afterwards so the nearly empty page will quickly
 1.54037 +** fill up.  On average.
 1.54038 +**
 1.54039 +** pPage is the leaf page which is the right-most page in the tree.
 1.54040 +** pParent is its parent.  pPage must have a single overflow entry
 1.54041 +** which is also the right-most entry on the page.
 1.54042 +**
 1.54043 +** The pSpace buffer is used to store a temporary copy of the divider
 1.54044 +** cell that will be inserted into pParent. Such a cell consists of a 4
 1.54045 +** byte page number followed by a variable length integer. In other
 1.54046 +** words, at most 13 bytes. Hence the pSpace buffer must be at
 1.54047 +** least 13 bytes in size.
 1.54048 +*/
 1.54049 +static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
 1.54050 +  BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
 1.54051 +  MemPage *pNew;                       /* Newly allocated page */
 1.54052 +  int rc;                              /* Return Code */
 1.54053 +  Pgno pgnoNew;                        /* Page number of pNew */
 1.54054 +
 1.54055 +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 1.54056 +  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 1.54057 +  assert( pPage->nOverflow==1 );
 1.54058 +
 1.54059 +  /* This error condition is now caught prior to reaching this function */
 1.54060 +  if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
 1.54061 +
 1.54062 +  /* Allocate a new page. This page will become the right-sibling of 
 1.54063 +  ** pPage. Make the parent page writable, so that the new divider cell
 1.54064 +  ** may be inserted. If both these operations are successful, proceed.
 1.54065 +  */
 1.54066 +  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
 1.54067 +
 1.54068 +  if( rc==SQLITE_OK ){
 1.54069 +
 1.54070 +    u8 *pOut = &pSpace[4];
 1.54071 +    u8 *pCell = pPage->apOvfl[0];
 1.54072 +    u16 szCell = cellSizePtr(pPage, pCell);
 1.54073 +    u8 *pStop;
 1.54074 +
 1.54075 +    assert( sqlite3PagerIswriteable(pNew->pDbPage) );
 1.54076 +    assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
 1.54077 +    zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
 1.54078 +    assemblePage(pNew, 1, &pCell, &szCell);
 1.54079 +
 1.54080 +    /* If this is an auto-vacuum database, update the pointer map
 1.54081 +    ** with entries for the new page, and any pointer from the 
 1.54082 +    ** cell on the page to an overflow page. If either of these
 1.54083 +    ** operations fails, the return code is set, but the contents
 1.54084 +    ** of the parent page are still manipulated by thh code below.
 1.54085 +    ** That is Ok, at this point the parent page is guaranteed to
 1.54086 +    ** be marked as dirty. Returning an error code will cause a
 1.54087 +    ** rollback, undoing any changes made to the parent page.
 1.54088 +    */
 1.54089 +    if( ISAUTOVACUUM ){
 1.54090 +      ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
 1.54091 +      if( szCell>pNew->minLocal ){
 1.54092 +        ptrmapPutOvflPtr(pNew, pCell, &rc);
 1.54093 +      }
 1.54094 +    }
 1.54095 +  
 1.54096 +    /* Create a divider cell to insert into pParent. The divider cell
 1.54097 +    ** consists of a 4-byte page number (the page number of pPage) and
 1.54098 +    ** a variable length key value (which must be the same value as the
 1.54099 +    ** largest key on pPage).
 1.54100 +    **
 1.54101 +    ** To find the largest key value on pPage, first find the right-most 
 1.54102 +    ** cell on pPage. The first two fields of this cell are the 
 1.54103 +    ** record-length (a variable length integer at most 32-bits in size)
 1.54104 +    ** and the key value (a variable length integer, may have any value).
 1.54105 +    ** The first of the while(...) loops below skips over the record-length
 1.54106 +    ** field. The second while(...) loop copies the key value from the
 1.54107 +    ** cell on pPage into the pSpace buffer.
 1.54108 +    */
 1.54109 +    pCell = findCell(pPage, pPage->nCell-1);
 1.54110 +    pStop = &pCell[9];
 1.54111 +    while( (*(pCell++)&0x80) && pCell<pStop );
 1.54112 +    pStop = &pCell[9];
 1.54113 +    while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
 1.54114 +
 1.54115 +    /* Insert the new divider cell into pParent. */
 1.54116 +    insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
 1.54117 +               0, pPage->pgno, &rc);
 1.54118 +
 1.54119 +    /* Set the right-child pointer of pParent to point to the new page. */
 1.54120 +    put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
 1.54121 +  
 1.54122 +    /* Release the reference to the new page. */
 1.54123 +    releasePage(pNew);
 1.54124 +  }
 1.54125 +
 1.54126 +  return rc;
 1.54127 +}
 1.54128 +#endif /* SQLITE_OMIT_QUICKBALANCE */
 1.54129 +
 1.54130 +#if 0
 1.54131 +/*
 1.54132 +** This function does not contribute anything to the operation of SQLite.
 1.54133 +** it is sometimes activated temporarily while debugging code responsible 
 1.54134 +** for setting pointer-map entries.
 1.54135 +*/
 1.54136 +static int ptrmapCheckPages(MemPage **apPage, int nPage){
 1.54137 +  int i, j;
 1.54138 +  for(i=0; i<nPage; i++){
 1.54139 +    Pgno n;
 1.54140 +    u8 e;
 1.54141 +    MemPage *pPage = apPage[i];
 1.54142 +    BtShared *pBt = pPage->pBt;
 1.54143 +    assert( pPage->isInit );
 1.54144 +
 1.54145 +    for(j=0; j<pPage->nCell; j++){
 1.54146 +      CellInfo info;
 1.54147 +      u8 *z;
 1.54148 +     
 1.54149 +      z = findCell(pPage, j);
 1.54150 +      btreeParseCellPtr(pPage, z, &info);
 1.54151 +      if( info.iOverflow ){
 1.54152 +        Pgno ovfl = get4byte(&z[info.iOverflow]);
 1.54153 +        ptrmapGet(pBt, ovfl, &e, &n);
 1.54154 +        assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
 1.54155 +      }
 1.54156 +      if( !pPage->leaf ){
 1.54157 +        Pgno child = get4byte(z);
 1.54158 +        ptrmapGet(pBt, child, &e, &n);
 1.54159 +        assert( n==pPage->pgno && e==PTRMAP_BTREE );
 1.54160 +      }
 1.54161 +    }
 1.54162 +    if( !pPage->leaf ){
 1.54163 +      Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 1.54164 +      ptrmapGet(pBt, child, &e, &n);
 1.54165 +      assert( n==pPage->pgno && e==PTRMAP_BTREE );
 1.54166 +    }
 1.54167 +  }
 1.54168 +  return 1;
 1.54169 +}
 1.54170 +#endif
 1.54171 +
 1.54172 +/*
 1.54173 +** This function is used to copy the contents of the b-tree node stored 
 1.54174 +** on page pFrom to page pTo. If page pFrom was not a leaf page, then
 1.54175 +** the pointer-map entries for each child page are updated so that the
 1.54176 +** parent page stored in the pointer map is page pTo. If pFrom contained
 1.54177 +** any cells with overflow page pointers, then the corresponding pointer
 1.54178 +** map entries are also updated so that the parent page is page pTo.
 1.54179 +**
 1.54180 +** If pFrom is currently carrying any overflow cells (entries in the
 1.54181 +** MemPage.apOvfl[] array), they are not copied to pTo. 
 1.54182 +**
 1.54183 +** Before returning, page pTo is reinitialized using btreeInitPage().
 1.54184 +**
 1.54185 +** The performance of this function is not critical. It is only used by 
 1.54186 +** the balance_shallower() and balance_deeper() procedures, neither of
 1.54187 +** which are called often under normal circumstances.
 1.54188 +*/
 1.54189 +static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
 1.54190 +  if( (*pRC)==SQLITE_OK ){
 1.54191 +    BtShared * const pBt = pFrom->pBt;
 1.54192 +    u8 * const aFrom = pFrom->aData;
 1.54193 +    u8 * const aTo = pTo->aData;
 1.54194 +    int const iFromHdr = pFrom->hdrOffset;
 1.54195 +    int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
 1.54196 +    int rc;
 1.54197 +    int iData;
 1.54198 +  
 1.54199 +  
 1.54200 +    assert( pFrom->isInit );
 1.54201 +    assert( pFrom->nFree>=iToHdr );
 1.54202 +    assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
 1.54203 +  
 1.54204 +    /* Copy the b-tree node content from page pFrom to page pTo. */
 1.54205 +    iData = get2byte(&aFrom[iFromHdr+5]);
 1.54206 +    memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
 1.54207 +    memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
 1.54208 +  
 1.54209 +    /* Reinitialize page pTo so that the contents of the MemPage structure
 1.54210 +    ** match the new data. The initialization of pTo can actually fail under
 1.54211 +    ** fairly obscure circumstances, even though it is a copy of initialized 
 1.54212 +    ** page pFrom.
 1.54213 +    */
 1.54214 +    pTo->isInit = 0;
 1.54215 +    rc = btreeInitPage(pTo);
 1.54216 +    if( rc!=SQLITE_OK ){
 1.54217 +      *pRC = rc;
 1.54218 +      return;
 1.54219 +    }
 1.54220 +  
 1.54221 +    /* If this is an auto-vacuum database, update the pointer-map entries
 1.54222 +    ** for any b-tree or overflow pages that pTo now contains the pointers to.
 1.54223 +    */
 1.54224 +    if( ISAUTOVACUUM ){
 1.54225 +      *pRC = setChildPtrmaps(pTo);
 1.54226 +    }
 1.54227 +  }
 1.54228 +}
 1.54229 +
 1.54230 +/*
 1.54231 +** This routine redistributes cells on the iParentIdx'th child of pParent
 1.54232 +** (hereafter "the page") and up to 2 siblings so that all pages have about the
 1.54233 +** same amount of free space. Usually a single sibling on either side of the
 1.54234 +** page are used in the balancing, though both siblings might come from one
 1.54235 +** side if the page is the first or last child of its parent. If the page 
 1.54236 +** has fewer than 2 siblings (something which can only happen if the page
 1.54237 +** is a root page or a child of a root page) then all available siblings
 1.54238 +** participate in the balancing.
 1.54239 +**
 1.54240 +** The number of siblings of the page might be increased or decreased by 
 1.54241 +** one or two in an effort to keep pages nearly full but not over full. 
 1.54242 +**
 1.54243 +** Note that when this routine is called, some of the cells on the page
 1.54244 +** might not actually be stored in MemPage.aData[]. This can happen
 1.54245 +** if the page is overfull. This routine ensures that all cells allocated
 1.54246 +** to the page and its siblings fit into MemPage.aData[] before returning.
 1.54247 +**
 1.54248 +** In the course of balancing the page and its siblings, cells may be
 1.54249 +** inserted into or removed from the parent page (pParent). Doing so
 1.54250 +** may cause the parent page to become overfull or underfull. If this
 1.54251 +** happens, it is the responsibility of the caller to invoke the correct
 1.54252 +** balancing routine to fix this problem (see the balance() routine). 
 1.54253 +**
 1.54254 +** If this routine fails for any reason, it might leave the database
 1.54255 +** in a corrupted state. So if this routine fails, the database should
 1.54256 +** be rolled back.
 1.54257 +**
 1.54258 +** The third argument to this function, aOvflSpace, is a pointer to a
 1.54259 +** buffer big enough to hold one page. If while inserting cells into the parent
 1.54260 +** page (pParent) the parent page becomes overfull, this buffer is
 1.54261 +** used to store the parent's overflow cells. Because this function inserts
 1.54262 +** a maximum of four divider cells into the parent page, and the maximum
 1.54263 +** size of a cell stored within an internal node is always less than 1/4
 1.54264 +** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
 1.54265 +** enough for all overflow cells.
 1.54266 +**
 1.54267 +** If aOvflSpace is set to a null pointer, this function returns 
 1.54268 +** SQLITE_NOMEM.
 1.54269 +*/
 1.54270 +#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
 1.54271 +#pragma optimize("", off)
 1.54272 +#endif
 1.54273 +static int balance_nonroot(
 1.54274 +  MemPage *pParent,               /* Parent page of siblings being balanced */
 1.54275 +  int iParentIdx,                 /* Index of "the page" in pParent */
 1.54276 +  u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
 1.54277 +  int isRoot,                     /* True if pParent is a root-page */
 1.54278 +  int bBulk                       /* True if this call is part of a bulk load */
 1.54279 +){
 1.54280 +  BtShared *pBt;               /* The whole database */
 1.54281 +  int nCell = 0;               /* Number of cells in apCell[] */
 1.54282 +  int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
 1.54283 +  int nNew = 0;                /* Number of pages in apNew[] */
 1.54284 +  int nOld;                    /* Number of pages in apOld[] */
 1.54285 +  int i, j, k;                 /* Loop counters */
 1.54286 +  int nxDiv;                   /* Next divider slot in pParent->aCell[] */
 1.54287 +  int rc = SQLITE_OK;          /* The return code */
 1.54288 +  u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
 1.54289 +  int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
 1.54290 +  int usableSpace;             /* Bytes in pPage beyond the header */
 1.54291 +  int pageFlags;               /* Value of pPage->aData[0] */
 1.54292 +  int subtotal;                /* Subtotal of bytes in cells on one page */
 1.54293 +  int iSpace1 = 0;             /* First unused byte of aSpace1[] */
 1.54294 +  int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
 1.54295 +  int szScratch;               /* Size of scratch memory requested */
 1.54296 +  MemPage *apOld[NB];          /* pPage and up to two siblings */
 1.54297 +  MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
 1.54298 +  MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
 1.54299 +  u8 *pRight;                  /* Location in parent of right-sibling pointer */
 1.54300 +  u8 *apDiv[NB-1];             /* Divider cells in pParent */
 1.54301 +  int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
 1.54302 +  int szNew[NB+2];             /* Combined size of cells place on i-th page */
 1.54303 +  u8 **apCell = 0;             /* All cells begin balanced */
 1.54304 +  u16 *szCell;                 /* Local size of all cells in apCell[] */
 1.54305 +  u8 *aSpace1;                 /* Space for copies of dividers cells */
 1.54306 +  Pgno pgno;                   /* Temp var to store a page number in */
 1.54307 +
 1.54308 +  pBt = pParent->pBt;
 1.54309 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.54310 +  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 1.54311 +
 1.54312 +#if 0
 1.54313 +  TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
 1.54314 +#endif
 1.54315 +
 1.54316 +  /* At this point pParent may have at most one overflow cell. And if
 1.54317 +  ** this overflow cell is present, it must be the cell with 
 1.54318 +  ** index iParentIdx. This scenario comes about when this function
 1.54319 +  ** is called (indirectly) from sqlite3BtreeDelete().
 1.54320 +  */
 1.54321 +  assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
 1.54322 +  assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
 1.54323 +
 1.54324 +  if( !aOvflSpace ){
 1.54325 +    return SQLITE_NOMEM;
 1.54326 +  }
 1.54327 +
 1.54328 +  /* Find the sibling pages to balance. Also locate the cells in pParent 
 1.54329 +  ** that divide the siblings. An attempt is made to find NN siblings on 
 1.54330 +  ** either side of pPage. More siblings are taken from one side, however, 
 1.54331 +  ** if there are fewer than NN siblings on the other side. If pParent
 1.54332 +  ** has NB or fewer children then all children of pParent are taken.  
 1.54333 +  **
 1.54334 +  ** This loop also drops the divider cells from the parent page. This
 1.54335 +  ** way, the remainder of the function does not have to deal with any
 1.54336 +  ** overflow cells in the parent page, since if any existed they will
 1.54337 +  ** have already been removed.
 1.54338 +  */
 1.54339 +  i = pParent->nOverflow + pParent->nCell;
 1.54340 +  if( i<2 ){
 1.54341 +    nxDiv = 0;
 1.54342 +  }else{
 1.54343 +    assert( bBulk==0 || bBulk==1 );
 1.54344 +    if( iParentIdx==0 ){                 
 1.54345 +      nxDiv = 0;
 1.54346 +    }else if( iParentIdx==i ){
 1.54347 +      nxDiv = i-2+bBulk;
 1.54348 +    }else{
 1.54349 +      assert( bBulk==0 );
 1.54350 +      nxDiv = iParentIdx-1;
 1.54351 +    }
 1.54352 +    i = 2-bBulk;
 1.54353 +  }
 1.54354 +  nOld = i+1;
 1.54355 +  if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
 1.54356 +    pRight = &pParent->aData[pParent->hdrOffset+8];
 1.54357 +  }else{
 1.54358 +    pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
 1.54359 +  }
 1.54360 +  pgno = get4byte(pRight);
 1.54361 +  while( 1 ){
 1.54362 +    rc = getAndInitPage(pBt, pgno, &apOld[i]);
 1.54363 +    if( rc ){
 1.54364 +      memset(apOld, 0, (i+1)*sizeof(MemPage*));
 1.54365 +      goto balance_cleanup;
 1.54366 +    }
 1.54367 +    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
 1.54368 +    if( (i--)==0 ) break;
 1.54369 +
 1.54370 +    if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
 1.54371 +      apDiv[i] = pParent->apOvfl[0];
 1.54372 +      pgno = get4byte(apDiv[i]);
 1.54373 +      szNew[i] = cellSizePtr(pParent, apDiv[i]);
 1.54374 +      pParent->nOverflow = 0;
 1.54375 +    }else{
 1.54376 +      apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
 1.54377 +      pgno = get4byte(apDiv[i]);
 1.54378 +      szNew[i] = cellSizePtr(pParent, apDiv[i]);
 1.54379 +
 1.54380 +      /* Drop the cell from the parent page. apDiv[i] still points to
 1.54381 +      ** the cell within the parent, even though it has been dropped.
 1.54382 +      ** This is safe because dropping a cell only overwrites the first
 1.54383 +      ** four bytes of it, and this function does not need the first
 1.54384 +      ** four bytes of the divider cell. So the pointer is safe to use
 1.54385 +      ** later on.  
 1.54386 +      **
 1.54387 +      ** But not if we are in secure-delete mode. In secure-delete mode,
 1.54388 +      ** the dropCell() routine will overwrite the entire cell with zeroes.
 1.54389 +      ** In this case, temporarily copy the cell into the aOvflSpace[]
 1.54390 +      ** buffer. It will be copied out again as soon as the aSpace[] buffer
 1.54391 +      ** is allocated.  */
 1.54392 +      if( pBt->btsFlags & BTS_SECURE_DELETE ){
 1.54393 +        int iOff;
 1.54394 +
 1.54395 +        iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
 1.54396 +        if( (iOff+szNew[i])>(int)pBt->usableSize ){
 1.54397 +          rc = SQLITE_CORRUPT_BKPT;
 1.54398 +          memset(apOld, 0, (i+1)*sizeof(MemPage*));
 1.54399 +          goto balance_cleanup;
 1.54400 +        }else{
 1.54401 +          memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
 1.54402 +          apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
 1.54403 +        }
 1.54404 +      }
 1.54405 +      dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
 1.54406 +    }
 1.54407 +  }
 1.54408 +
 1.54409 +  /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
 1.54410 +  ** alignment */
 1.54411 +  nMaxCells = (nMaxCells + 3)&~3;
 1.54412 +
 1.54413 +  /*
 1.54414 +  ** Allocate space for memory structures
 1.54415 +  */
 1.54416 +  k = pBt->pageSize + ROUND8(sizeof(MemPage));
 1.54417 +  szScratch =
 1.54418 +       nMaxCells*sizeof(u8*)                       /* apCell */
 1.54419 +     + nMaxCells*sizeof(u16)                       /* szCell */
 1.54420 +     + pBt->pageSize                               /* aSpace1 */
 1.54421 +     + k*nOld;                                     /* Page copies (apCopy) */
 1.54422 +  apCell = sqlite3ScratchMalloc( szScratch ); 
 1.54423 +  if( apCell==0 ){
 1.54424 +    rc = SQLITE_NOMEM;
 1.54425 +    goto balance_cleanup;
 1.54426 +  }
 1.54427 +  szCell = (u16*)&apCell[nMaxCells];
 1.54428 +  aSpace1 = (u8*)&szCell[nMaxCells];
 1.54429 +  assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
 1.54430 +
 1.54431 +  /*
 1.54432 +  ** Load pointers to all cells on sibling pages and the divider cells
 1.54433 +  ** into the local apCell[] array.  Make copies of the divider cells
 1.54434 +  ** into space obtained from aSpace1[] and remove the divider cells
 1.54435 +  ** from pParent.
 1.54436 +  **
 1.54437 +  ** If the siblings are on leaf pages, then the child pointers of the
 1.54438 +  ** divider cells are stripped from the cells before they are copied
 1.54439 +  ** into aSpace1[].  In this way, all cells in apCell[] are without
 1.54440 +  ** child pointers.  If siblings are not leaves, then all cell in
 1.54441 +  ** apCell[] include child pointers.  Either way, all cells in apCell[]
 1.54442 +  ** are alike.
 1.54443 +  **
 1.54444 +  ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
 1.54445 +  **       leafData:  1 if pPage holds key+data and pParent holds only keys.
 1.54446 +  */
 1.54447 +  leafCorrection = apOld[0]->leaf*4;
 1.54448 +  leafData = apOld[0]->hasData;
 1.54449 +  for(i=0; i<nOld; i++){
 1.54450 +    int limit;
 1.54451 +    
 1.54452 +    /* Before doing anything else, take a copy of the i'th original sibling
 1.54453 +    ** The rest of this function will use data from the copies rather
 1.54454 +    ** that the original pages since the original pages will be in the
 1.54455 +    ** process of being overwritten.  */
 1.54456 +    MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
 1.54457 +    memcpy(pOld, apOld[i], sizeof(MemPage));
 1.54458 +    pOld->aData = (void*)&pOld[1];
 1.54459 +    memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
 1.54460 +
 1.54461 +    limit = pOld->nCell+pOld->nOverflow;
 1.54462 +    if( pOld->nOverflow>0 ){
 1.54463 +      for(j=0; j<limit; j++){
 1.54464 +        assert( nCell<nMaxCells );
 1.54465 +        apCell[nCell] = findOverflowCell(pOld, j);
 1.54466 +        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 1.54467 +        nCell++;
 1.54468 +      }
 1.54469 +    }else{
 1.54470 +      u8 *aData = pOld->aData;
 1.54471 +      u16 maskPage = pOld->maskPage;
 1.54472 +      u16 cellOffset = pOld->cellOffset;
 1.54473 +      for(j=0; j<limit; j++){
 1.54474 +        assert( nCell<nMaxCells );
 1.54475 +        apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
 1.54476 +        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 1.54477 +        nCell++;
 1.54478 +      }
 1.54479 +    }       
 1.54480 +    if( i<nOld-1 && !leafData){
 1.54481 +      u16 sz = (u16)szNew[i];
 1.54482 +      u8 *pTemp;
 1.54483 +      assert( nCell<nMaxCells );
 1.54484 +      szCell[nCell] = sz;
 1.54485 +      pTemp = &aSpace1[iSpace1];
 1.54486 +      iSpace1 += sz;
 1.54487 +      assert( sz<=pBt->maxLocal+23 );
 1.54488 +      assert( iSpace1 <= (int)pBt->pageSize );
 1.54489 +      memcpy(pTemp, apDiv[i], sz);
 1.54490 +      apCell[nCell] = pTemp+leafCorrection;
 1.54491 +      assert( leafCorrection==0 || leafCorrection==4 );
 1.54492 +      szCell[nCell] = szCell[nCell] - leafCorrection;
 1.54493 +      if( !pOld->leaf ){
 1.54494 +        assert( leafCorrection==0 );
 1.54495 +        assert( pOld->hdrOffset==0 );
 1.54496 +        /* The right pointer of the child page pOld becomes the left
 1.54497 +        ** pointer of the divider cell */
 1.54498 +        memcpy(apCell[nCell], &pOld->aData[8], 4);
 1.54499 +      }else{
 1.54500 +        assert( leafCorrection==4 );
 1.54501 +        if( szCell[nCell]<4 ){
 1.54502 +          /* Do not allow any cells smaller than 4 bytes. */
 1.54503 +          szCell[nCell] = 4;
 1.54504 +        }
 1.54505 +      }
 1.54506 +      nCell++;
 1.54507 +    }
 1.54508 +  }
 1.54509 +
 1.54510 +  /*
 1.54511 +  ** Figure out the number of pages needed to hold all nCell cells.
 1.54512 +  ** Store this number in "k".  Also compute szNew[] which is the total
 1.54513 +  ** size of all cells on the i-th page and cntNew[] which is the index
 1.54514 +  ** in apCell[] of the cell that divides page i from page i+1.  
 1.54515 +  ** cntNew[k] should equal nCell.
 1.54516 +  **
 1.54517 +  ** Values computed by this block:
 1.54518 +  **
 1.54519 +  **           k: The total number of sibling pages
 1.54520 +  **    szNew[i]: Spaced used on the i-th sibling page.
 1.54521 +  **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
 1.54522 +  **              the right of the i-th sibling page.
 1.54523 +  ** usableSpace: Number of bytes of space available on each sibling.
 1.54524 +  ** 
 1.54525 +  */
 1.54526 +  usableSpace = pBt->usableSize - 12 + leafCorrection;
 1.54527 +  for(subtotal=k=i=0; i<nCell; i++){
 1.54528 +    assert( i<nMaxCells );
 1.54529 +    subtotal += szCell[i] + 2;
 1.54530 +    if( subtotal > usableSpace ){
 1.54531 +      szNew[k] = subtotal - szCell[i];
 1.54532 +      cntNew[k] = i;
 1.54533 +      if( leafData ){ i--; }
 1.54534 +      subtotal = 0;
 1.54535 +      k++;
 1.54536 +      if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
 1.54537 +    }
 1.54538 +  }
 1.54539 +  szNew[k] = subtotal;
 1.54540 +  cntNew[k] = nCell;
 1.54541 +  k++;
 1.54542 +
 1.54543 +  /*
 1.54544 +  ** The packing computed by the previous block is biased toward the siblings
 1.54545 +  ** on the left side.  The left siblings are always nearly full, while the
 1.54546 +  ** right-most sibling might be nearly empty.  This block of code attempts
 1.54547 +  ** to adjust the packing of siblings to get a better balance.
 1.54548 +  **
 1.54549 +  ** This adjustment is more than an optimization.  The packing above might
 1.54550 +  ** be so out of balance as to be illegal.  For example, the right-most
 1.54551 +  ** sibling might be completely empty.  This adjustment is not optional.
 1.54552 +  */
 1.54553 +  for(i=k-1; i>0; i--){
 1.54554 +    int szRight = szNew[i];  /* Size of sibling on the right */
 1.54555 +    int szLeft = szNew[i-1]; /* Size of sibling on the left */
 1.54556 +    int r;              /* Index of right-most cell in left sibling */
 1.54557 +    int d;              /* Index of first cell to the left of right sibling */
 1.54558 +
 1.54559 +    r = cntNew[i-1] - 1;
 1.54560 +    d = r + 1 - leafData;
 1.54561 +    assert( d<nMaxCells );
 1.54562 +    assert( r<nMaxCells );
 1.54563 +    while( szRight==0 
 1.54564 +       || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
 1.54565 +    ){
 1.54566 +      szRight += szCell[d] + 2;
 1.54567 +      szLeft -= szCell[r] + 2;
 1.54568 +      cntNew[i-1]--;
 1.54569 +      r = cntNew[i-1] - 1;
 1.54570 +      d = r + 1 - leafData;
 1.54571 +    }
 1.54572 +    szNew[i] = szRight;
 1.54573 +    szNew[i-1] = szLeft;
 1.54574 +  }
 1.54575 +
 1.54576 +  /* Either we found one or more cells (cntnew[0])>0) or pPage is
 1.54577 +  ** a virtual root page.  A virtual root page is when the real root
 1.54578 +  ** page is page 1 and we are the only child of that page.
 1.54579 +  **
 1.54580 +  ** UPDATE:  The assert() below is not necessarily true if the database
 1.54581 +  ** file is corrupt.  The corruption will be detected and reported later
 1.54582 +  ** in this procedure so there is no need to act upon it now.
 1.54583 +  */
 1.54584 +#if 0
 1.54585 +  assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
 1.54586 +#endif
 1.54587 +
 1.54588 +  TRACE(("BALANCE: old: %d %d %d  ",
 1.54589 +    apOld[0]->pgno, 
 1.54590 +    nOld>=2 ? apOld[1]->pgno : 0,
 1.54591 +    nOld>=3 ? apOld[2]->pgno : 0
 1.54592 +  ));
 1.54593 +
 1.54594 +  /*
 1.54595 +  ** Allocate k new pages.  Reuse old pages where possible.
 1.54596 +  */
 1.54597 +  if( apOld[0]->pgno<=1 ){
 1.54598 +    rc = SQLITE_CORRUPT_BKPT;
 1.54599 +    goto balance_cleanup;
 1.54600 +  }
 1.54601 +  pageFlags = apOld[0]->aData[0];
 1.54602 +  for(i=0; i<k; i++){
 1.54603 +    MemPage *pNew;
 1.54604 +    if( i<nOld ){
 1.54605 +      pNew = apNew[i] = apOld[i];
 1.54606 +      apOld[i] = 0;
 1.54607 +      rc = sqlite3PagerWrite(pNew->pDbPage);
 1.54608 +      nNew++;
 1.54609 +      if( rc ) goto balance_cleanup;
 1.54610 +    }else{
 1.54611 +      assert( i>0 );
 1.54612 +      rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
 1.54613 +      if( rc ) goto balance_cleanup;
 1.54614 +      apNew[i] = pNew;
 1.54615 +      nNew++;
 1.54616 +
 1.54617 +      /* Set the pointer-map entry for the new sibling page. */
 1.54618 +      if( ISAUTOVACUUM ){
 1.54619 +        ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
 1.54620 +        if( rc!=SQLITE_OK ){
 1.54621 +          goto balance_cleanup;
 1.54622 +        }
 1.54623 +      }
 1.54624 +    }
 1.54625 +  }
 1.54626 +
 1.54627 +  /* Free any old pages that were not reused as new pages.
 1.54628 +  */
 1.54629 +  while( i<nOld ){
 1.54630 +    freePage(apOld[i], &rc);
 1.54631 +    if( rc ) goto balance_cleanup;
 1.54632 +    releasePage(apOld[i]);
 1.54633 +    apOld[i] = 0;
 1.54634 +    i++;
 1.54635 +  }
 1.54636 +
 1.54637 +  /*
 1.54638 +  ** Put the new pages in accending order.  This helps to
 1.54639 +  ** keep entries in the disk file in order so that a scan
 1.54640 +  ** of the table is a linear scan through the file.  That
 1.54641 +  ** in turn helps the operating system to deliver pages
 1.54642 +  ** from the disk more rapidly.
 1.54643 +  **
 1.54644 +  ** An O(n^2) insertion sort algorithm is used, but since
 1.54645 +  ** n is never more than NB (a small constant), that should
 1.54646 +  ** not be a problem.
 1.54647 +  **
 1.54648 +  ** When NB==3, this one optimization makes the database
 1.54649 +  ** about 25% faster for large insertions and deletions.
 1.54650 +  */
 1.54651 +  for(i=0; i<k-1; i++){
 1.54652 +    int minV = apNew[i]->pgno;
 1.54653 +    int minI = i;
 1.54654 +    for(j=i+1; j<k; j++){
 1.54655 +      if( apNew[j]->pgno<(unsigned)minV ){
 1.54656 +        minI = j;
 1.54657 +        minV = apNew[j]->pgno;
 1.54658 +      }
 1.54659 +    }
 1.54660 +    if( minI>i ){
 1.54661 +      MemPage *pT;
 1.54662 +      pT = apNew[i];
 1.54663 +      apNew[i] = apNew[minI];
 1.54664 +      apNew[minI] = pT;
 1.54665 +    }
 1.54666 +  }
 1.54667 +  TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
 1.54668 +    apNew[0]->pgno, szNew[0],
 1.54669 +    nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
 1.54670 +    nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
 1.54671 +    nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
 1.54672 +    nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
 1.54673 +
 1.54674 +  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 1.54675 +  put4byte(pRight, apNew[nNew-1]->pgno);
 1.54676 +
 1.54677 +  /*
 1.54678 +  ** Evenly distribute the data in apCell[] across the new pages.
 1.54679 +  ** Insert divider cells into pParent as necessary.
 1.54680 +  */
 1.54681 +  j = 0;
 1.54682 +  for(i=0; i<nNew; i++){
 1.54683 +    /* Assemble the new sibling page. */
 1.54684 +    MemPage *pNew = apNew[i];
 1.54685 +    assert( j<nMaxCells );
 1.54686 +    zeroPage(pNew, pageFlags);
 1.54687 +    assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
 1.54688 +    assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
 1.54689 +    assert( pNew->nOverflow==0 );
 1.54690 +
 1.54691 +    j = cntNew[i];
 1.54692 +
 1.54693 +    /* If the sibling page assembled above was not the right-most sibling,
 1.54694 +    ** insert a divider cell into the parent page.
 1.54695 +    */
 1.54696 +    assert( i<nNew-1 || j==nCell );
 1.54697 +    if( j<nCell ){
 1.54698 +      u8 *pCell;
 1.54699 +      u8 *pTemp;
 1.54700 +      int sz;
 1.54701 +
 1.54702 +      assert( j<nMaxCells );
 1.54703 +      pCell = apCell[j];
 1.54704 +      sz = szCell[j] + leafCorrection;
 1.54705 +      pTemp = &aOvflSpace[iOvflSpace];
 1.54706 +      if( !pNew->leaf ){
 1.54707 +        memcpy(&pNew->aData[8], pCell, 4);
 1.54708 +      }else if( leafData ){
 1.54709 +        /* If the tree is a leaf-data tree, and the siblings are leaves, 
 1.54710 +        ** then there is no divider cell in apCell[]. Instead, the divider 
 1.54711 +        ** cell consists of the integer key for the right-most cell of 
 1.54712 +        ** the sibling-page assembled above only.
 1.54713 +        */
 1.54714 +        CellInfo info;
 1.54715 +        j--;
 1.54716 +        btreeParseCellPtr(pNew, apCell[j], &info);
 1.54717 +        pCell = pTemp;
 1.54718 +        sz = 4 + putVarint(&pCell[4], info.nKey);
 1.54719 +        pTemp = 0;
 1.54720 +      }else{
 1.54721 +        pCell -= 4;
 1.54722 +        /* Obscure case for non-leaf-data trees: If the cell at pCell was
 1.54723 +        ** previously stored on a leaf node, and its reported size was 4
 1.54724 +        ** bytes, then it may actually be smaller than this 
 1.54725 +        ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
 1.54726 +        ** any cell). But it is important to pass the correct size to 
 1.54727 +        ** insertCell(), so reparse the cell now.
 1.54728 +        **
 1.54729 +        ** Note that this can never happen in an SQLite data file, as all
 1.54730 +        ** cells are at least 4 bytes. It only happens in b-trees used
 1.54731 +        ** to evaluate "IN (SELECT ...)" and similar clauses.
 1.54732 +        */
 1.54733 +        if( szCell[j]==4 ){
 1.54734 +          assert(leafCorrection==4);
 1.54735 +          sz = cellSizePtr(pParent, pCell);
 1.54736 +        }
 1.54737 +      }
 1.54738 +      iOvflSpace += sz;
 1.54739 +      assert( sz<=pBt->maxLocal+23 );
 1.54740 +      assert( iOvflSpace <= (int)pBt->pageSize );
 1.54741 +      insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
 1.54742 +      if( rc!=SQLITE_OK ) goto balance_cleanup;
 1.54743 +      assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 1.54744 +
 1.54745 +      j++;
 1.54746 +      nxDiv++;
 1.54747 +    }
 1.54748 +  }
 1.54749 +  assert( j==nCell );
 1.54750 +  assert( nOld>0 );
 1.54751 +  assert( nNew>0 );
 1.54752 +  if( (pageFlags & PTF_LEAF)==0 ){
 1.54753 +    u8 *zChild = &apCopy[nOld-1]->aData[8];
 1.54754 +    memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
 1.54755 +  }
 1.54756 +
 1.54757 +  if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
 1.54758 +    /* The root page of the b-tree now contains no cells. The only sibling
 1.54759 +    ** page is the right-child of the parent. Copy the contents of the
 1.54760 +    ** child page into the parent, decreasing the overall height of the
 1.54761 +    ** b-tree structure by one. This is described as the "balance-shallower"
 1.54762 +    ** sub-algorithm in some documentation.
 1.54763 +    **
 1.54764 +    ** If this is an auto-vacuum database, the call to copyNodeContent() 
 1.54765 +    ** sets all pointer-map entries corresponding to database image pages 
 1.54766 +    ** for which the pointer is stored within the content being copied.
 1.54767 +    **
 1.54768 +    ** The second assert below verifies that the child page is defragmented
 1.54769 +    ** (it must be, as it was just reconstructed using assemblePage()). This
 1.54770 +    ** is important if the parent page happens to be page 1 of the database
 1.54771 +    ** image.  */
 1.54772 +    assert( nNew==1 );
 1.54773 +    assert( apNew[0]->nFree == 
 1.54774 +        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) 
 1.54775 +    );
 1.54776 +    copyNodeContent(apNew[0], pParent, &rc);
 1.54777 +    freePage(apNew[0], &rc);
 1.54778 +  }else if( ISAUTOVACUUM ){
 1.54779 +    /* Fix the pointer-map entries for all the cells that were shifted around. 
 1.54780 +    ** There are several different types of pointer-map entries that need to
 1.54781 +    ** be dealt with by this routine. Some of these have been set already, but
 1.54782 +    ** many have not. The following is a summary:
 1.54783 +    **
 1.54784 +    **   1) The entries associated with new sibling pages that were not
 1.54785 +    **      siblings when this function was called. These have already
 1.54786 +    **      been set. We don't need to worry about old siblings that were
 1.54787 +    **      moved to the free-list - the freePage() code has taken care
 1.54788 +    **      of those.
 1.54789 +    **
 1.54790 +    **   2) The pointer-map entries associated with the first overflow
 1.54791 +    **      page in any overflow chains used by new divider cells. These 
 1.54792 +    **      have also already been taken care of by the insertCell() code.
 1.54793 +    **
 1.54794 +    **   3) If the sibling pages are not leaves, then the child pages of
 1.54795 +    **      cells stored on the sibling pages may need to be updated.
 1.54796 +    **
 1.54797 +    **   4) If the sibling pages are not internal intkey nodes, then any
 1.54798 +    **      overflow pages used by these cells may need to be updated
 1.54799 +    **      (internal intkey nodes never contain pointers to overflow pages).
 1.54800 +    **
 1.54801 +    **   5) If the sibling pages are not leaves, then the pointer-map
 1.54802 +    **      entries for the right-child pages of each sibling may need
 1.54803 +    **      to be updated.
 1.54804 +    **
 1.54805 +    ** Cases 1 and 2 are dealt with above by other code. The next
 1.54806 +    ** block deals with cases 3 and 4 and the one after that, case 5. Since
 1.54807 +    ** setting a pointer map entry is a relatively expensive operation, this
 1.54808 +    ** code only sets pointer map entries for child or overflow pages that have
 1.54809 +    ** actually moved between pages.  */
 1.54810 +    MemPage *pNew = apNew[0];
 1.54811 +    MemPage *pOld = apCopy[0];
 1.54812 +    int nOverflow = pOld->nOverflow;
 1.54813 +    int iNextOld = pOld->nCell + nOverflow;
 1.54814 +    int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1);
 1.54815 +    j = 0;                             /* Current 'old' sibling page */
 1.54816 +    k = 0;                             /* Current 'new' sibling page */
 1.54817 +    for(i=0; i<nCell; i++){
 1.54818 +      int isDivider = 0;
 1.54819 +      while( i==iNextOld ){
 1.54820 +        /* Cell i is the cell immediately following the last cell on old
 1.54821 +        ** sibling page j. If the siblings are not leaf pages of an
 1.54822 +        ** intkey b-tree, then cell i was a divider cell. */
 1.54823 +        assert( j+1 < ArraySize(apCopy) );
 1.54824 +        assert( j+1 < nOld );
 1.54825 +        pOld = apCopy[++j];
 1.54826 +        iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
 1.54827 +        if( pOld->nOverflow ){
 1.54828 +          nOverflow = pOld->nOverflow;
 1.54829 +          iOverflow = i + !leafData + pOld->aiOvfl[0];
 1.54830 +        }
 1.54831 +        isDivider = !leafData;  
 1.54832 +      }
 1.54833 +
 1.54834 +      assert(nOverflow>0 || iOverflow<i );
 1.54835 +      assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1);
 1.54836 +      assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1);
 1.54837 +      if( i==iOverflow ){
 1.54838 +        isDivider = 1;
 1.54839 +        if( (--nOverflow)>0 ){
 1.54840 +          iOverflow++;
 1.54841 +        }
 1.54842 +      }
 1.54843 +
 1.54844 +      if( i==cntNew[k] ){
 1.54845 +        /* Cell i is the cell immediately following the last cell on new
 1.54846 +        ** sibling page k. If the siblings are not leaf pages of an
 1.54847 +        ** intkey b-tree, then cell i is a divider cell.  */
 1.54848 +        pNew = apNew[++k];
 1.54849 +        if( !leafData ) continue;
 1.54850 +      }
 1.54851 +      assert( j<nOld );
 1.54852 +      assert( k<nNew );
 1.54853 +
 1.54854 +      /* If the cell was originally divider cell (and is not now) or
 1.54855 +      ** an overflow cell, or if the cell was located on a different sibling
 1.54856 +      ** page before the balancing, then the pointer map entries associated
 1.54857 +      ** with any child or overflow pages need to be updated.  */
 1.54858 +      if( isDivider || pOld->pgno!=pNew->pgno ){
 1.54859 +        if( !leafCorrection ){
 1.54860 +          ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
 1.54861 +        }
 1.54862 +        if( szCell[i]>pNew->minLocal ){
 1.54863 +          ptrmapPutOvflPtr(pNew, apCell[i], &rc);
 1.54864 +        }
 1.54865 +      }
 1.54866 +    }
 1.54867 +
 1.54868 +    if( !leafCorrection ){
 1.54869 +      for(i=0; i<nNew; i++){
 1.54870 +        u32 key = get4byte(&apNew[i]->aData[8]);
 1.54871 +        ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
 1.54872 +      }
 1.54873 +    }
 1.54874 +
 1.54875 +#if 0
 1.54876 +    /* The ptrmapCheckPages() contains assert() statements that verify that
 1.54877 +    ** all pointer map pages are set correctly. This is helpful while 
 1.54878 +    ** debugging. This is usually disabled because a corrupt database may
 1.54879 +    ** cause an assert() statement to fail.  */
 1.54880 +    ptrmapCheckPages(apNew, nNew);
 1.54881 +    ptrmapCheckPages(&pParent, 1);
 1.54882 +#endif
 1.54883 +  }
 1.54884 +
 1.54885 +  assert( pParent->isInit );
 1.54886 +  TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
 1.54887 +          nOld, nNew, nCell));
 1.54888 +
 1.54889 +  /*
 1.54890 +  ** Cleanup before returning.
 1.54891 +  */
 1.54892 +balance_cleanup:
 1.54893 +  sqlite3ScratchFree(apCell);
 1.54894 +  for(i=0; i<nOld; i++){
 1.54895 +    releasePage(apOld[i]);
 1.54896 +  }
 1.54897 +  for(i=0; i<nNew; i++){
 1.54898 +    releasePage(apNew[i]);
 1.54899 +  }
 1.54900 +
 1.54901 +  return rc;
 1.54902 +}
 1.54903 +#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
 1.54904 +#pragma optimize("", on)
 1.54905 +#endif
 1.54906 +
 1.54907 +
 1.54908 +/*
 1.54909 +** This function is called when the root page of a b-tree structure is
 1.54910 +** overfull (has one or more overflow pages).
 1.54911 +**
 1.54912 +** A new child page is allocated and the contents of the current root
 1.54913 +** page, including overflow cells, are copied into the child. The root
 1.54914 +** page is then overwritten to make it an empty page with the right-child 
 1.54915 +** pointer pointing to the new page.
 1.54916 +**
 1.54917 +** Before returning, all pointer-map entries corresponding to pages 
 1.54918 +** that the new child-page now contains pointers to are updated. The
 1.54919 +** entry corresponding to the new right-child pointer of the root
 1.54920 +** page is also updated.
 1.54921 +**
 1.54922 +** If successful, *ppChild is set to contain a reference to the child 
 1.54923 +** page and SQLITE_OK is returned. In this case the caller is required
 1.54924 +** to call releasePage() on *ppChild exactly once. If an error occurs,
 1.54925 +** an error code is returned and *ppChild is set to 0.
 1.54926 +*/
 1.54927 +static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
 1.54928 +  int rc;                        /* Return value from subprocedures */
 1.54929 +  MemPage *pChild = 0;           /* Pointer to a new child page */
 1.54930 +  Pgno pgnoChild = 0;            /* Page number of the new child page */
 1.54931 +  BtShared *pBt = pRoot->pBt;    /* The BTree */
 1.54932 +
 1.54933 +  assert( pRoot->nOverflow>0 );
 1.54934 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.54935 +
 1.54936 +  /* Make pRoot, the root page of the b-tree, writable. Allocate a new 
 1.54937 +  ** page that will become the new right-child of pPage. Copy the contents
 1.54938 +  ** of the node stored on pRoot into the new child page.
 1.54939 +  */
 1.54940 +  rc = sqlite3PagerWrite(pRoot->pDbPage);
 1.54941 +  if( rc==SQLITE_OK ){
 1.54942 +    rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
 1.54943 +    copyNodeContent(pRoot, pChild, &rc);
 1.54944 +    if( ISAUTOVACUUM ){
 1.54945 +      ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
 1.54946 +    }
 1.54947 +  }
 1.54948 +  if( rc ){
 1.54949 +    *ppChild = 0;
 1.54950 +    releasePage(pChild);
 1.54951 +    return rc;
 1.54952 +  }
 1.54953 +  assert( sqlite3PagerIswriteable(pChild->pDbPage) );
 1.54954 +  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 1.54955 +  assert( pChild->nCell==pRoot->nCell );
 1.54956 +
 1.54957 +  TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
 1.54958 +
 1.54959 +  /* Copy the overflow cells from pRoot to pChild */
 1.54960 +  memcpy(pChild->aiOvfl, pRoot->aiOvfl,
 1.54961 +         pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
 1.54962 +  memcpy(pChild->apOvfl, pRoot->apOvfl,
 1.54963 +         pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
 1.54964 +  pChild->nOverflow = pRoot->nOverflow;
 1.54965 +
 1.54966 +  /* Zero the contents of pRoot. Then install pChild as the right-child. */
 1.54967 +  zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
 1.54968 +  put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
 1.54969 +
 1.54970 +  *ppChild = pChild;
 1.54971 +  return SQLITE_OK;
 1.54972 +}
 1.54973 +
 1.54974 +/*
 1.54975 +** The page that pCur currently points to has just been modified in
 1.54976 +** some way. This function figures out if this modification means the
 1.54977 +** tree needs to be balanced, and if so calls the appropriate balancing 
 1.54978 +** routine. Balancing routines are:
 1.54979 +**
 1.54980 +**   balance_quick()
 1.54981 +**   balance_deeper()
 1.54982 +**   balance_nonroot()
 1.54983 +*/
 1.54984 +static int balance(BtCursor *pCur){
 1.54985 +  int rc = SQLITE_OK;
 1.54986 +  const int nMin = pCur->pBt->usableSize * 2 / 3;
 1.54987 +  u8 aBalanceQuickSpace[13];
 1.54988 +  u8 *pFree = 0;
 1.54989 +
 1.54990 +  TESTONLY( int balance_quick_called = 0 );
 1.54991 +  TESTONLY( int balance_deeper_called = 0 );
 1.54992 +
 1.54993 +  do {
 1.54994 +    int iPage = pCur->iPage;
 1.54995 +    MemPage *pPage = pCur->apPage[iPage];
 1.54996 +
 1.54997 +    if( iPage==0 ){
 1.54998 +      if( pPage->nOverflow ){
 1.54999 +        /* The root page of the b-tree is overfull. In this case call the
 1.55000 +        ** balance_deeper() function to create a new child for the root-page
 1.55001 +        ** and copy the current contents of the root-page to it. The
 1.55002 +        ** next iteration of the do-loop will balance the child page.
 1.55003 +        */ 
 1.55004 +        assert( (balance_deeper_called++)==0 );
 1.55005 +        rc = balance_deeper(pPage, &pCur->apPage[1]);
 1.55006 +        if( rc==SQLITE_OK ){
 1.55007 +          pCur->iPage = 1;
 1.55008 +          pCur->aiIdx[0] = 0;
 1.55009 +          pCur->aiIdx[1] = 0;
 1.55010 +          assert( pCur->apPage[1]->nOverflow );
 1.55011 +        }
 1.55012 +      }else{
 1.55013 +        break;
 1.55014 +      }
 1.55015 +    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
 1.55016 +      break;
 1.55017 +    }else{
 1.55018 +      MemPage * const pParent = pCur->apPage[iPage-1];
 1.55019 +      int const iIdx = pCur->aiIdx[iPage-1];
 1.55020 +
 1.55021 +      rc = sqlite3PagerWrite(pParent->pDbPage);
 1.55022 +      if( rc==SQLITE_OK ){
 1.55023 +#ifndef SQLITE_OMIT_QUICKBALANCE
 1.55024 +        if( pPage->hasData
 1.55025 +         && pPage->nOverflow==1
 1.55026 +         && pPage->aiOvfl[0]==pPage->nCell
 1.55027 +         && pParent->pgno!=1
 1.55028 +         && pParent->nCell==iIdx
 1.55029 +        ){
 1.55030 +          /* Call balance_quick() to create a new sibling of pPage on which
 1.55031 +          ** to store the overflow cell. balance_quick() inserts a new cell
 1.55032 +          ** into pParent, which may cause pParent overflow. If this
 1.55033 +          ** happens, the next interation of the do-loop will balance pParent 
 1.55034 +          ** use either balance_nonroot() or balance_deeper(). Until this
 1.55035 +          ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
 1.55036 +          ** buffer. 
 1.55037 +          **
 1.55038 +          ** The purpose of the following assert() is to check that only a
 1.55039 +          ** single call to balance_quick() is made for each call to this
 1.55040 +          ** function. If this were not verified, a subtle bug involving reuse
 1.55041 +          ** of the aBalanceQuickSpace[] might sneak in.
 1.55042 +          */
 1.55043 +          assert( (balance_quick_called++)==0 );
 1.55044 +          rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
 1.55045 +        }else
 1.55046 +#endif
 1.55047 +        {
 1.55048 +          /* In this case, call balance_nonroot() to redistribute cells
 1.55049 +          ** between pPage and up to 2 of its sibling pages. This involves
 1.55050 +          ** modifying the contents of pParent, which may cause pParent to
 1.55051 +          ** become overfull or underfull. The next iteration of the do-loop
 1.55052 +          ** will balance the parent page to correct this.
 1.55053 +          ** 
 1.55054 +          ** If the parent page becomes overfull, the overflow cell or cells
 1.55055 +          ** are stored in the pSpace buffer allocated immediately below. 
 1.55056 +          ** A subsequent iteration of the do-loop will deal with this by
 1.55057 +          ** calling balance_nonroot() (balance_deeper() may be called first,
 1.55058 +          ** but it doesn't deal with overflow cells - just moves them to a
 1.55059 +          ** different page). Once this subsequent call to balance_nonroot() 
 1.55060 +          ** has completed, it is safe to release the pSpace buffer used by
 1.55061 +          ** the previous call, as the overflow cell data will have been 
 1.55062 +          ** copied either into the body of a database page or into the new
 1.55063 +          ** pSpace buffer passed to the latter call to balance_nonroot().
 1.55064 +          */
 1.55065 +          u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
 1.55066 +          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
 1.55067 +          if( pFree ){
 1.55068 +            /* If pFree is not NULL, it points to the pSpace buffer used 
 1.55069 +            ** by a previous call to balance_nonroot(). Its contents are
 1.55070 +            ** now stored either on real database pages or within the 
 1.55071 +            ** new pSpace buffer, so it may be safely freed here. */
 1.55072 +            sqlite3PageFree(pFree);
 1.55073 +          }
 1.55074 +
 1.55075 +          /* The pSpace buffer will be freed after the next call to
 1.55076 +          ** balance_nonroot(), or just before this function returns, whichever
 1.55077 +          ** comes first. */
 1.55078 +          pFree = pSpace;
 1.55079 +        }
 1.55080 +      }
 1.55081 +
 1.55082 +      pPage->nOverflow = 0;
 1.55083 +
 1.55084 +      /* The next iteration of the do-loop balances the parent page. */
 1.55085 +      releasePage(pPage);
 1.55086 +      pCur->iPage--;
 1.55087 +    }
 1.55088 +  }while( rc==SQLITE_OK );
 1.55089 +
 1.55090 +  if( pFree ){
 1.55091 +    sqlite3PageFree(pFree);
 1.55092 +  }
 1.55093 +  return rc;
 1.55094 +}
 1.55095 +
 1.55096 +
 1.55097 +/*
 1.55098 +** Insert a new record into the BTree.  The key is given by (pKey,nKey)
 1.55099 +** and the data is given by (pData,nData).  The cursor is used only to
 1.55100 +** define what table the record should be inserted into.  The cursor
 1.55101 +** is left pointing at a random location.
 1.55102 +**
 1.55103 +** For an INTKEY table, only the nKey value of the key is used.  pKey is
 1.55104 +** ignored.  For a ZERODATA table, the pData and nData are both ignored.
 1.55105 +**
 1.55106 +** If the seekResult parameter is non-zero, then a successful call to
 1.55107 +** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
 1.55108 +** been performed. seekResult is the search result returned (a negative
 1.55109 +** number if pCur points at an entry that is smaller than (pKey, nKey), or
 1.55110 +** a positive value if pCur points at an etry that is larger than 
 1.55111 +** (pKey, nKey)). 
 1.55112 +**
 1.55113 +** If the seekResult parameter is non-zero, then the caller guarantees that
 1.55114 +** cursor pCur is pointing at the existing copy of a row that is to be
 1.55115 +** overwritten.  If the seekResult parameter is 0, then cursor pCur may
 1.55116 +** point to any entry or to no entry at all and so this function has to seek
 1.55117 +** the cursor before the new key can be inserted.
 1.55118 +*/
 1.55119 +SQLITE_PRIVATE int sqlite3BtreeInsert(
 1.55120 +  BtCursor *pCur,                /* Insert data into the table of this cursor */
 1.55121 +  const void *pKey, i64 nKey,    /* The key of the new record */
 1.55122 +  const void *pData, int nData,  /* The data of the new record */
 1.55123 +  int nZero,                     /* Number of extra 0 bytes to append to data */
 1.55124 +  int appendBias,                /* True if this is likely an append */
 1.55125 +  int seekResult                 /* Result of prior MovetoUnpacked() call */
 1.55126 +){
 1.55127 +  int rc;
 1.55128 +  int loc = seekResult;          /* -1: before desired location  +1: after */
 1.55129 +  int szNew = 0;
 1.55130 +  int idx;
 1.55131 +  MemPage *pPage;
 1.55132 +  Btree *p = pCur->pBtree;
 1.55133 +  BtShared *pBt = p->pBt;
 1.55134 +  unsigned char *oldCell;
 1.55135 +  unsigned char *newCell = 0;
 1.55136 +
 1.55137 +  if( pCur->eState==CURSOR_FAULT ){
 1.55138 +    assert( pCur->skipNext!=SQLITE_OK );
 1.55139 +    return pCur->skipNext;
 1.55140 +  }
 1.55141 +
 1.55142 +  assert( cursorHoldsMutex(pCur) );
 1.55143 +  assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
 1.55144 +              && (pBt->btsFlags & BTS_READ_ONLY)==0 );
 1.55145 +  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 1.55146 +
 1.55147 +  /* Assert that the caller has been consistent. If this cursor was opened
 1.55148 +  ** expecting an index b-tree, then the caller should be inserting blob
 1.55149 +  ** keys with no associated data. If the cursor was opened expecting an
 1.55150 +  ** intkey table, the caller should be inserting integer keys with a
 1.55151 +  ** blob of associated data.  */
 1.55152 +  assert( (pKey==0)==(pCur->pKeyInfo==0) );
 1.55153 +
 1.55154 +  /* Save the positions of any other cursors open on this table.
 1.55155 +  **
 1.55156 +  ** In some cases, the call to btreeMoveto() below is a no-op. For
 1.55157 +  ** example, when inserting data into a table with auto-generated integer
 1.55158 +  ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
 1.55159 +  ** integer key to use. It then calls this function to actually insert the 
 1.55160 +  ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
 1.55161 +  ** that the cursor is already where it needs to be and returns without
 1.55162 +  ** doing any work. To avoid thwarting these optimizations, it is important
 1.55163 +  ** not to clear the cursor here.
 1.55164 +  */
 1.55165 +  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 1.55166 +  if( rc ) return rc;
 1.55167 +
 1.55168 +  /* If this is an insert into a table b-tree, invalidate any incrblob 
 1.55169 +  ** cursors open on the row being replaced (assuming this is a replace
 1.55170 +  ** operation - if it is not, the following is a no-op).  */
 1.55171 +  if( pCur->pKeyInfo==0 ){
 1.55172 +    invalidateIncrblobCursors(p, nKey, 0);
 1.55173 +  }
 1.55174 +
 1.55175 +  if( !loc ){
 1.55176 +    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
 1.55177 +    if( rc ) return rc;
 1.55178 +  }
 1.55179 +  assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 1.55180 +
 1.55181 +  pPage = pCur->apPage[pCur->iPage];
 1.55182 +  assert( pPage->intKey || nKey>=0 );
 1.55183 +  assert( pPage->leaf || !pPage->intKey );
 1.55184 +
 1.55185 +  TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 1.55186 +          pCur->pgnoRoot, nKey, nData, pPage->pgno,
 1.55187 +          loc==0 ? "overwrite" : "new entry"));
 1.55188 +  assert( pPage->isInit );
 1.55189 +  allocateTempSpace(pBt);
 1.55190 +  newCell = pBt->pTmpSpace;
 1.55191 +  if( newCell==0 ) return SQLITE_NOMEM;
 1.55192 +  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
 1.55193 +  if( rc ) goto end_insert;
 1.55194 +  assert( szNew==cellSizePtr(pPage, newCell) );
 1.55195 +  assert( szNew <= MX_CELL_SIZE(pBt) );
 1.55196 +  idx = pCur->aiIdx[pCur->iPage];
 1.55197 +  if( loc==0 ){
 1.55198 +    u16 szOld;
 1.55199 +    assert( idx<pPage->nCell );
 1.55200 +    rc = sqlite3PagerWrite(pPage->pDbPage);
 1.55201 +    if( rc ){
 1.55202 +      goto end_insert;
 1.55203 +    }
 1.55204 +    oldCell = findCell(pPage, idx);
 1.55205 +    if( !pPage->leaf ){
 1.55206 +      memcpy(newCell, oldCell, 4);
 1.55207 +    }
 1.55208 +    szOld = cellSizePtr(pPage, oldCell);
 1.55209 +    rc = clearCell(pPage, oldCell);
 1.55210 +    dropCell(pPage, idx, szOld, &rc);
 1.55211 +    if( rc ) goto end_insert;
 1.55212 +  }else if( loc<0 && pPage->nCell>0 ){
 1.55213 +    assert( pPage->leaf );
 1.55214 +    idx = ++pCur->aiIdx[pCur->iPage];
 1.55215 +  }else{
 1.55216 +    assert( pPage->leaf );
 1.55217 +  }
 1.55218 +  insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
 1.55219 +  assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
 1.55220 +
 1.55221 +  /* If no error has occured and pPage has an overflow cell, call balance() 
 1.55222 +  ** to redistribute the cells within the tree. Since balance() may move
 1.55223 +  ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
 1.55224 +  ** variables.
 1.55225 +  **
 1.55226 +  ** Previous versions of SQLite called moveToRoot() to move the cursor
 1.55227 +  ** back to the root page as balance() used to invalidate the contents
 1.55228 +  ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
 1.55229 +  ** set the cursor state to "invalid". This makes common insert operations
 1.55230 +  ** slightly faster.
 1.55231 +  **
 1.55232 +  ** There is a subtle but important optimization here too. When inserting
 1.55233 +  ** multiple records into an intkey b-tree using a single cursor (as can
 1.55234 +  ** happen while processing an "INSERT INTO ... SELECT" statement), it
 1.55235 +  ** is advantageous to leave the cursor pointing to the last entry in
 1.55236 +  ** the b-tree if possible. If the cursor is left pointing to the last
 1.55237 +  ** entry in the table, and the next row inserted has an integer key
 1.55238 +  ** larger than the largest existing key, it is possible to insert the
 1.55239 +  ** row without seeking the cursor. This can be a big performance boost.
 1.55240 +  */
 1.55241 +  pCur->info.nSize = 0;
 1.55242 +  pCur->validNKey = 0;
 1.55243 +  if( rc==SQLITE_OK && pPage->nOverflow ){
 1.55244 +    rc = balance(pCur);
 1.55245 +
 1.55246 +    /* Must make sure nOverflow is reset to zero even if the balance()
 1.55247 +    ** fails. Internal data structure corruption will result otherwise. 
 1.55248 +    ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 1.55249 +    ** from trying to save the current position of the cursor.  */
 1.55250 +    pCur->apPage[pCur->iPage]->nOverflow = 0;
 1.55251 +    pCur->eState = CURSOR_INVALID;
 1.55252 +  }
 1.55253 +  assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
 1.55254 +
 1.55255 +end_insert:
 1.55256 +  return rc;
 1.55257 +}
 1.55258 +
 1.55259 +/*
 1.55260 +** Delete the entry that the cursor is pointing to.  The cursor
 1.55261 +** is left pointing at a arbitrary location.
 1.55262 +*/
 1.55263 +SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
 1.55264 +  Btree *p = pCur->pBtree;
 1.55265 +  BtShared *pBt = p->pBt;              
 1.55266 +  int rc;                              /* Return code */
 1.55267 +  MemPage *pPage;                      /* Page to delete cell from */
 1.55268 +  unsigned char *pCell;                /* Pointer to cell to delete */
 1.55269 +  int iCellIdx;                        /* Index of cell to delete */
 1.55270 +  int iCellDepth;                      /* Depth of node containing pCell */ 
 1.55271 +
 1.55272 +  assert( cursorHoldsMutex(pCur) );
 1.55273 +  assert( pBt->inTransaction==TRANS_WRITE );
 1.55274 +  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 1.55275 +  assert( pCur->wrFlag );
 1.55276 +  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 1.55277 +  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 1.55278 +
 1.55279 +  if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
 1.55280 +   || NEVER(pCur->eState!=CURSOR_VALID)
 1.55281 +  ){
 1.55282 +    return SQLITE_ERROR;  /* Something has gone awry. */
 1.55283 +  }
 1.55284 +
 1.55285 +  iCellDepth = pCur->iPage;
 1.55286 +  iCellIdx = pCur->aiIdx[iCellDepth];
 1.55287 +  pPage = pCur->apPage[iCellDepth];
 1.55288 +  pCell = findCell(pPage, iCellIdx);
 1.55289 +
 1.55290 +  /* If the page containing the entry to delete is not a leaf page, move
 1.55291 +  ** the cursor to the largest entry in the tree that is smaller than
 1.55292 +  ** the entry being deleted. This cell will replace the cell being deleted
 1.55293 +  ** from the internal node. The 'previous' entry is used for this instead
 1.55294 +  ** of the 'next' entry, as the previous entry is always a part of the
 1.55295 +  ** sub-tree headed by the child page of the cell being deleted. This makes
 1.55296 +  ** balancing the tree following the delete operation easier.  */
 1.55297 +  if( !pPage->leaf ){
 1.55298 +    int notUsed;
 1.55299 +    rc = sqlite3BtreePrevious(pCur, &notUsed);
 1.55300 +    if( rc ) return rc;
 1.55301 +  }
 1.55302 +
 1.55303 +  /* Save the positions of any other cursors open on this table before
 1.55304 +  ** making any modifications. Make the page containing the entry to be 
 1.55305 +  ** deleted writable. Then free any overflow pages associated with the 
 1.55306 +  ** entry and finally remove the cell itself from within the page.  
 1.55307 +  */
 1.55308 +  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 1.55309 +  if( rc ) return rc;
 1.55310 +
 1.55311 +  /* If this is a delete operation to remove a row from a table b-tree,
 1.55312 +  ** invalidate any incrblob cursors open on the row being deleted.  */
 1.55313 +  if( pCur->pKeyInfo==0 ){
 1.55314 +    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
 1.55315 +  }
 1.55316 +
 1.55317 +  rc = sqlite3PagerWrite(pPage->pDbPage);
 1.55318 +  if( rc ) return rc;
 1.55319 +  rc = clearCell(pPage, pCell);
 1.55320 +  dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
 1.55321 +  if( rc ) return rc;
 1.55322 +
 1.55323 +  /* If the cell deleted was not located on a leaf page, then the cursor
 1.55324 +  ** is currently pointing to the largest entry in the sub-tree headed
 1.55325 +  ** by the child-page of the cell that was just deleted from an internal
 1.55326 +  ** node. The cell from the leaf node needs to be moved to the internal
 1.55327 +  ** node to replace the deleted cell.  */
 1.55328 +  if( !pPage->leaf ){
 1.55329 +    MemPage *pLeaf = pCur->apPage[pCur->iPage];
 1.55330 +    int nCell;
 1.55331 +    Pgno n = pCur->apPage[iCellDepth+1]->pgno;
 1.55332 +    unsigned char *pTmp;
 1.55333 +
 1.55334 +    pCell = findCell(pLeaf, pLeaf->nCell-1);
 1.55335 +    nCell = cellSizePtr(pLeaf, pCell);
 1.55336 +    assert( MX_CELL_SIZE(pBt) >= nCell );
 1.55337 +
 1.55338 +    allocateTempSpace(pBt);
 1.55339 +    pTmp = pBt->pTmpSpace;
 1.55340 +
 1.55341 +    rc = sqlite3PagerWrite(pLeaf->pDbPage);
 1.55342 +    insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
 1.55343 +    dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
 1.55344 +    if( rc ) return rc;
 1.55345 +  }
 1.55346 +
 1.55347 +  /* Balance the tree. If the entry deleted was located on a leaf page,
 1.55348 +  ** then the cursor still points to that page. In this case the first
 1.55349 +  ** call to balance() repairs the tree, and the if(...) condition is
 1.55350 +  ** never true.
 1.55351 +  **
 1.55352 +  ** Otherwise, if the entry deleted was on an internal node page, then
 1.55353 +  ** pCur is pointing to the leaf page from which a cell was removed to
 1.55354 +  ** replace the cell deleted from the internal node. This is slightly
 1.55355 +  ** tricky as the leaf node may be underfull, and the internal node may
 1.55356 +  ** be either under or overfull. In this case run the balancing algorithm
 1.55357 +  ** on the leaf node first. If the balance proceeds far enough up the
 1.55358 +  ** tree that we can be sure that any problem in the internal node has
 1.55359 +  ** been corrected, so be it. Otherwise, after balancing the leaf node,
 1.55360 +  ** walk the cursor up the tree to the internal node and balance it as 
 1.55361 +  ** well.  */
 1.55362 +  rc = balance(pCur);
 1.55363 +  if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
 1.55364 +    while( pCur->iPage>iCellDepth ){
 1.55365 +      releasePage(pCur->apPage[pCur->iPage--]);
 1.55366 +    }
 1.55367 +    rc = balance(pCur);
 1.55368 +  }
 1.55369 +
 1.55370 +  if( rc==SQLITE_OK ){
 1.55371 +    moveToRoot(pCur);
 1.55372 +  }
 1.55373 +  return rc;
 1.55374 +}
 1.55375 +
 1.55376 +/*
 1.55377 +** Create a new BTree table.  Write into *piTable the page
 1.55378 +** number for the root page of the new table.
 1.55379 +**
 1.55380 +** The type of type is determined by the flags parameter.  Only the
 1.55381 +** following values of flags are currently in use.  Other values for
 1.55382 +** flags might not work:
 1.55383 +**
 1.55384 +**     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
 1.55385 +**     BTREE_ZERODATA                  Used for SQL indices
 1.55386 +*/
 1.55387 +static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
 1.55388 +  BtShared *pBt = p->pBt;
 1.55389 +  MemPage *pRoot;
 1.55390 +  Pgno pgnoRoot;
 1.55391 +  int rc;
 1.55392 +  int ptfFlags;          /* Page-type flage for the root page of new table */
 1.55393 +
 1.55394 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.55395 +  assert( pBt->inTransaction==TRANS_WRITE );
 1.55396 +  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 1.55397 +
 1.55398 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.55399 +  rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
 1.55400 +  if( rc ){
 1.55401 +    return rc;
 1.55402 +  }
 1.55403 +#else
 1.55404 +  if( pBt->autoVacuum ){
 1.55405 +    Pgno pgnoMove;      /* Move a page here to make room for the root-page */
 1.55406 +    MemPage *pPageMove; /* The page to move to. */
 1.55407 +
 1.55408 +    /* Creating a new table may probably require moving an existing database
 1.55409 +    ** to make room for the new tables root page. In case this page turns
 1.55410 +    ** out to be an overflow page, delete all overflow page-map caches
 1.55411 +    ** held by open cursors.
 1.55412 +    */
 1.55413 +    invalidateAllOverflowCache(pBt);
 1.55414 +
 1.55415 +    /* Read the value of meta[3] from the database to determine where the
 1.55416 +    ** root page of the new table should go. meta[3] is the largest root-page
 1.55417 +    ** created so far, so the new root-page is (meta[3]+1).
 1.55418 +    */
 1.55419 +    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
 1.55420 +    pgnoRoot++;
 1.55421 +
 1.55422 +    /* The new root-page may not be allocated on a pointer-map page, or the
 1.55423 +    ** PENDING_BYTE page.
 1.55424 +    */
 1.55425 +    while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
 1.55426 +        pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
 1.55427 +      pgnoRoot++;
 1.55428 +    }
 1.55429 +    assert( pgnoRoot>=3 );
 1.55430 +
 1.55431 +    /* Allocate a page. The page that currently resides at pgnoRoot will
 1.55432 +    ** be moved to the allocated page (unless the allocated page happens
 1.55433 +    ** to reside at pgnoRoot).
 1.55434 +    */
 1.55435 +    rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
 1.55436 +    if( rc!=SQLITE_OK ){
 1.55437 +      return rc;
 1.55438 +    }
 1.55439 +
 1.55440 +    if( pgnoMove!=pgnoRoot ){
 1.55441 +      /* pgnoRoot is the page that will be used for the root-page of
 1.55442 +      ** the new table (assuming an error did not occur). But we were
 1.55443 +      ** allocated pgnoMove. If required (i.e. if it was not allocated
 1.55444 +      ** by extending the file), the current page at position pgnoMove
 1.55445 +      ** is already journaled.
 1.55446 +      */
 1.55447 +      u8 eType = 0;
 1.55448 +      Pgno iPtrPage = 0;
 1.55449 +
 1.55450 +      releasePage(pPageMove);
 1.55451 +
 1.55452 +      /* Move the page currently at pgnoRoot to pgnoMove. */
 1.55453 +      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
 1.55454 +      if( rc!=SQLITE_OK ){
 1.55455 +        return rc;
 1.55456 +      }
 1.55457 +      rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
 1.55458 +      if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
 1.55459 +        rc = SQLITE_CORRUPT_BKPT;
 1.55460 +      }
 1.55461 +      if( rc!=SQLITE_OK ){
 1.55462 +        releasePage(pRoot);
 1.55463 +        return rc;
 1.55464 +      }
 1.55465 +      assert( eType!=PTRMAP_ROOTPAGE );
 1.55466 +      assert( eType!=PTRMAP_FREEPAGE );
 1.55467 +      rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
 1.55468 +      releasePage(pRoot);
 1.55469 +
 1.55470 +      /* Obtain the page at pgnoRoot */
 1.55471 +      if( rc!=SQLITE_OK ){
 1.55472 +        return rc;
 1.55473 +      }
 1.55474 +      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
 1.55475 +      if( rc!=SQLITE_OK ){
 1.55476 +        return rc;
 1.55477 +      }
 1.55478 +      rc = sqlite3PagerWrite(pRoot->pDbPage);
 1.55479 +      if( rc!=SQLITE_OK ){
 1.55480 +        releasePage(pRoot);
 1.55481 +        return rc;
 1.55482 +      }
 1.55483 +    }else{
 1.55484 +      pRoot = pPageMove;
 1.55485 +    } 
 1.55486 +
 1.55487 +    /* Update the pointer-map and meta-data with the new root-page number. */
 1.55488 +    ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
 1.55489 +    if( rc ){
 1.55490 +      releasePage(pRoot);
 1.55491 +      return rc;
 1.55492 +    }
 1.55493 +
 1.55494 +    /* When the new root page was allocated, page 1 was made writable in
 1.55495 +    ** order either to increase the database filesize, or to decrement the
 1.55496 +    ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
 1.55497 +    */
 1.55498 +    assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
 1.55499 +    rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
 1.55500 +    if( NEVER(rc) ){
 1.55501 +      releasePage(pRoot);
 1.55502 +      return rc;
 1.55503 +    }
 1.55504 +
 1.55505 +  }else{
 1.55506 +    rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
 1.55507 +    if( rc ) return rc;
 1.55508 +  }
 1.55509 +#endif
 1.55510 +  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 1.55511 +  if( createTabFlags & BTREE_INTKEY ){
 1.55512 +    ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
 1.55513 +  }else{
 1.55514 +    ptfFlags = PTF_ZERODATA | PTF_LEAF;
 1.55515 +  }
 1.55516 +  zeroPage(pRoot, ptfFlags);
 1.55517 +  sqlite3PagerUnref(pRoot->pDbPage);
 1.55518 +  assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
 1.55519 +  *piTable = (int)pgnoRoot;
 1.55520 +  return SQLITE_OK;
 1.55521 +}
 1.55522 +SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
 1.55523 +  int rc;
 1.55524 +  sqlite3BtreeEnter(p);
 1.55525 +  rc = btreeCreateTable(p, piTable, flags);
 1.55526 +  sqlite3BtreeLeave(p);
 1.55527 +  return rc;
 1.55528 +}
 1.55529 +
 1.55530 +/*
 1.55531 +** Erase the given database page and all its children.  Return
 1.55532 +** the page to the freelist.
 1.55533 +*/
 1.55534 +static int clearDatabasePage(
 1.55535 +  BtShared *pBt,           /* The BTree that contains the table */
 1.55536 +  Pgno pgno,               /* Page number to clear */
 1.55537 +  int freePageFlag,        /* Deallocate page if true */
 1.55538 +  int *pnChange            /* Add number of Cells freed to this counter */
 1.55539 +){
 1.55540 +  MemPage *pPage;
 1.55541 +  int rc;
 1.55542 +  unsigned char *pCell;
 1.55543 +  int i;
 1.55544 +
 1.55545 +  assert( sqlite3_mutex_held(pBt->mutex) );
 1.55546 +  if( pgno>btreePagecount(pBt) ){
 1.55547 +    return SQLITE_CORRUPT_BKPT;
 1.55548 +  }
 1.55549 +
 1.55550 +  rc = getAndInitPage(pBt, pgno, &pPage);
 1.55551 +  if( rc ) return rc;
 1.55552 +  for(i=0; i<pPage->nCell; i++){
 1.55553 +    pCell = findCell(pPage, i);
 1.55554 +    if( !pPage->leaf ){
 1.55555 +      rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
 1.55556 +      if( rc ) goto cleardatabasepage_out;
 1.55557 +    }
 1.55558 +    rc = clearCell(pPage, pCell);
 1.55559 +    if( rc ) goto cleardatabasepage_out;
 1.55560 +  }
 1.55561 +  if( !pPage->leaf ){
 1.55562 +    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
 1.55563 +    if( rc ) goto cleardatabasepage_out;
 1.55564 +  }else if( pnChange ){
 1.55565 +    assert( pPage->intKey );
 1.55566 +    *pnChange += pPage->nCell;
 1.55567 +  }
 1.55568 +  if( freePageFlag ){
 1.55569 +    freePage(pPage, &rc);
 1.55570 +  }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
 1.55571 +    zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
 1.55572 +  }
 1.55573 +
 1.55574 +cleardatabasepage_out:
 1.55575 +  releasePage(pPage);
 1.55576 +  return rc;
 1.55577 +}
 1.55578 +
 1.55579 +/*
 1.55580 +** Delete all information from a single table in the database.  iTable is
 1.55581 +** the page number of the root of the table.  After this routine returns,
 1.55582 +** the root page is empty, but still exists.
 1.55583 +**
 1.55584 +** This routine will fail with SQLITE_LOCKED if there are any open
 1.55585 +** read cursors on the table.  Open write cursors are moved to the
 1.55586 +** root of the table.
 1.55587 +**
 1.55588 +** If pnChange is not NULL, then table iTable must be an intkey table. The
 1.55589 +** integer value pointed to by pnChange is incremented by the number of
 1.55590 +** entries in the table.
 1.55591 +*/
 1.55592 +SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
 1.55593 +  int rc;
 1.55594 +  BtShared *pBt = p->pBt;
 1.55595 +  sqlite3BtreeEnter(p);
 1.55596 +  assert( p->inTrans==TRANS_WRITE );
 1.55597 +
 1.55598 +  rc = saveAllCursors(pBt, (Pgno)iTable, 0);
 1.55599 +
 1.55600 +  if( SQLITE_OK==rc ){
 1.55601 +    /* Invalidate all incrblob cursors open on table iTable (assuming iTable
 1.55602 +    ** is the root of a table b-tree - if it is not, the following call is
 1.55603 +    ** a no-op).  */
 1.55604 +    invalidateIncrblobCursors(p, 0, 1);
 1.55605 +    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
 1.55606 +  }
 1.55607 +  sqlite3BtreeLeave(p);
 1.55608 +  return rc;
 1.55609 +}
 1.55610 +
 1.55611 +/*
 1.55612 +** Erase all information in a table and add the root of the table to
 1.55613 +** the freelist.  Except, the root of the principle table (the one on
 1.55614 +** page 1) is never added to the freelist.
 1.55615 +**
 1.55616 +** This routine will fail with SQLITE_LOCKED if there are any open
 1.55617 +** cursors on the table.
 1.55618 +**
 1.55619 +** If AUTOVACUUM is enabled and the page at iTable is not the last
 1.55620 +** root page in the database file, then the last root page 
 1.55621 +** in the database file is moved into the slot formerly occupied by
 1.55622 +** iTable and that last slot formerly occupied by the last root page
 1.55623 +** is added to the freelist instead of iTable.  In this say, all
 1.55624 +** root pages are kept at the beginning of the database file, which
 1.55625 +** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
 1.55626 +** page number that used to be the last root page in the file before
 1.55627 +** the move.  If no page gets moved, *piMoved is set to 0.
 1.55628 +** The last root page is recorded in meta[3] and the value of
 1.55629 +** meta[3] is updated by this procedure.
 1.55630 +*/
 1.55631 +static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
 1.55632 +  int rc;
 1.55633 +  MemPage *pPage = 0;
 1.55634 +  BtShared *pBt = p->pBt;
 1.55635 +
 1.55636 +  assert( sqlite3BtreeHoldsMutex(p) );
 1.55637 +  assert( p->inTrans==TRANS_WRITE );
 1.55638 +
 1.55639 +  /* It is illegal to drop a table if any cursors are open on the
 1.55640 +  ** database. This is because in auto-vacuum mode the backend may
 1.55641 +  ** need to move another root-page to fill a gap left by the deleted
 1.55642 +  ** root page. If an open cursor was using this page a problem would 
 1.55643 +  ** occur.
 1.55644 +  **
 1.55645 +  ** This error is caught long before control reaches this point.
 1.55646 +  */
 1.55647 +  if( NEVER(pBt->pCursor) ){
 1.55648 +    sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
 1.55649 +    return SQLITE_LOCKED_SHAREDCACHE;
 1.55650 +  }
 1.55651 +
 1.55652 +  rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
 1.55653 +  if( rc ) return rc;
 1.55654 +  rc = sqlite3BtreeClearTable(p, iTable, 0);
 1.55655 +  if( rc ){
 1.55656 +    releasePage(pPage);
 1.55657 +    return rc;
 1.55658 +  }
 1.55659 +
 1.55660 +  *piMoved = 0;
 1.55661 +
 1.55662 +  if( iTable>1 ){
 1.55663 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.55664 +    freePage(pPage, &rc);
 1.55665 +    releasePage(pPage);
 1.55666 +#else
 1.55667 +    if( pBt->autoVacuum ){
 1.55668 +      Pgno maxRootPgno;
 1.55669 +      sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
 1.55670 +
 1.55671 +      if( iTable==maxRootPgno ){
 1.55672 +        /* If the table being dropped is the table with the largest root-page
 1.55673 +        ** number in the database, put the root page on the free list. 
 1.55674 +        */
 1.55675 +        freePage(pPage, &rc);
 1.55676 +        releasePage(pPage);
 1.55677 +        if( rc!=SQLITE_OK ){
 1.55678 +          return rc;
 1.55679 +        }
 1.55680 +      }else{
 1.55681 +        /* The table being dropped does not have the largest root-page
 1.55682 +        ** number in the database. So move the page that does into the 
 1.55683 +        ** gap left by the deleted root-page.
 1.55684 +        */
 1.55685 +        MemPage *pMove;
 1.55686 +        releasePage(pPage);
 1.55687 +        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 1.55688 +        if( rc!=SQLITE_OK ){
 1.55689 +          return rc;
 1.55690 +        }
 1.55691 +        rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
 1.55692 +        releasePage(pMove);
 1.55693 +        if( rc!=SQLITE_OK ){
 1.55694 +          return rc;
 1.55695 +        }
 1.55696 +        pMove = 0;
 1.55697 +        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 1.55698 +        freePage(pMove, &rc);
 1.55699 +        releasePage(pMove);
 1.55700 +        if( rc!=SQLITE_OK ){
 1.55701 +          return rc;
 1.55702 +        }
 1.55703 +        *piMoved = maxRootPgno;
 1.55704 +      }
 1.55705 +
 1.55706 +      /* Set the new 'max-root-page' value in the database header. This
 1.55707 +      ** is the old value less one, less one more if that happens to
 1.55708 +      ** be a root-page number, less one again if that is the
 1.55709 +      ** PENDING_BYTE_PAGE.
 1.55710 +      */
 1.55711 +      maxRootPgno--;
 1.55712 +      while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
 1.55713 +             || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
 1.55714 +        maxRootPgno--;
 1.55715 +      }
 1.55716 +      assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
 1.55717 +
 1.55718 +      rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
 1.55719 +    }else{
 1.55720 +      freePage(pPage, &rc);
 1.55721 +      releasePage(pPage);
 1.55722 +    }
 1.55723 +#endif
 1.55724 +  }else{
 1.55725 +    /* If sqlite3BtreeDropTable was called on page 1.
 1.55726 +    ** This really never should happen except in a corrupt
 1.55727 +    ** database. 
 1.55728 +    */
 1.55729 +    zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
 1.55730 +    releasePage(pPage);
 1.55731 +  }
 1.55732 +  return rc;  
 1.55733 +}
 1.55734 +SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
 1.55735 +  int rc;
 1.55736 +  sqlite3BtreeEnter(p);
 1.55737 +  rc = btreeDropTable(p, iTable, piMoved);
 1.55738 +  sqlite3BtreeLeave(p);
 1.55739 +  return rc;
 1.55740 +}
 1.55741 +
 1.55742 +
 1.55743 +/*
 1.55744 +** This function may only be called if the b-tree connection already
 1.55745 +** has a read or write transaction open on the database.
 1.55746 +**
 1.55747 +** Read the meta-information out of a database file.  Meta[0]
 1.55748 +** is the number of free pages currently in the database.  Meta[1]
 1.55749 +** through meta[15] are available for use by higher layers.  Meta[0]
 1.55750 +** is read-only, the others are read/write.
 1.55751 +** 
 1.55752 +** The schema layer numbers meta values differently.  At the schema
 1.55753 +** layer (and the SetCookie and ReadCookie opcodes) the number of
 1.55754 +** free pages is not visible.  So Cookie[0] is the same as Meta[1].
 1.55755 +*/
 1.55756 +SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
 1.55757 +  BtShared *pBt = p->pBt;
 1.55758 +
 1.55759 +  sqlite3BtreeEnter(p);
 1.55760 +  assert( p->inTrans>TRANS_NONE );
 1.55761 +  assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
 1.55762 +  assert( pBt->pPage1 );
 1.55763 +  assert( idx>=0 && idx<=15 );
 1.55764 +
 1.55765 +  *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
 1.55766 +
 1.55767 +  /* If auto-vacuum is disabled in this build and this is an auto-vacuum
 1.55768 +  ** database, mark the database as read-only.  */
 1.55769 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.55770 +  if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
 1.55771 +    pBt->btsFlags |= BTS_READ_ONLY;
 1.55772 +  }
 1.55773 +#endif
 1.55774 +
 1.55775 +  sqlite3BtreeLeave(p);
 1.55776 +}
 1.55777 +
 1.55778 +/*
 1.55779 +** Write meta-information back into the database.  Meta[0] is
 1.55780 +** read-only and may not be written.
 1.55781 +*/
 1.55782 +SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
 1.55783 +  BtShared *pBt = p->pBt;
 1.55784 +  unsigned char *pP1;
 1.55785 +  int rc;
 1.55786 +  assert( idx>=1 && idx<=15 );
 1.55787 +  sqlite3BtreeEnter(p);
 1.55788 +  assert( p->inTrans==TRANS_WRITE );
 1.55789 +  assert( pBt->pPage1!=0 );
 1.55790 +  pP1 = pBt->pPage1->aData;
 1.55791 +  rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 1.55792 +  if( rc==SQLITE_OK ){
 1.55793 +    put4byte(&pP1[36 + idx*4], iMeta);
 1.55794 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.55795 +    if( idx==BTREE_INCR_VACUUM ){
 1.55796 +      assert( pBt->autoVacuum || iMeta==0 );
 1.55797 +      assert( iMeta==0 || iMeta==1 );
 1.55798 +      pBt->incrVacuum = (u8)iMeta;
 1.55799 +    }
 1.55800 +#endif
 1.55801 +  }
 1.55802 +  sqlite3BtreeLeave(p);
 1.55803 +  return rc;
 1.55804 +}
 1.55805 +
 1.55806 +#ifndef SQLITE_OMIT_BTREECOUNT
 1.55807 +/*
 1.55808 +** The first argument, pCur, is a cursor opened on some b-tree. Count the
 1.55809 +** number of entries in the b-tree and write the result to *pnEntry.
 1.55810 +**
 1.55811 +** SQLITE_OK is returned if the operation is successfully executed. 
 1.55812 +** Otherwise, if an error is encountered (i.e. an IO error or database
 1.55813 +** corruption) an SQLite error code is returned.
 1.55814 +*/
 1.55815 +SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
 1.55816 +  i64 nEntry = 0;                      /* Value to return in *pnEntry */
 1.55817 +  int rc;                              /* Return code */
 1.55818 +
 1.55819 +  if( pCur->pgnoRoot==0 ){
 1.55820 +    *pnEntry = 0;
 1.55821 +    return SQLITE_OK;
 1.55822 +  }
 1.55823 +  rc = moveToRoot(pCur);
 1.55824 +
 1.55825 +  /* Unless an error occurs, the following loop runs one iteration for each
 1.55826 +  ** page in the B-Tree structure (not including overflow pages). 
 1.55827 +  */
 1.55828 +  while( rc==SQLITE_OK ){
 1.55829 +    int iIdx;                          /* Index of child node in parent */
 1.55830 +    MemPage *pPage;                    /* Current page of the b-tree */
 1.55831 +
 1.55832 +    /* If this is a leaf page or the tree is not an int-key tree, then 
 1.55833 +    ** this page contains countable entries. Increment the entry counter
 1.55834 +    ** accordingly.
 1.55835 +    */
 1.55836 +    pPage = pCur->apPage[pCur->iPage];
 1.55837 +    if( pPage->leaf || !pPage->intKey ){
 1.55838 +      nEntry += pPage->nCell;
 1.55839 +    }
 1.55840 +
 1.55841 +    /* pPage is a leaf node. This loop navigates the cursor so that it 
 1.55842 +    ** points to the first interior cell that it points to the parent of
 1.55843 +    ** the next page in the tree that has not yet been visited. The
 1.55844 +    ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
 1.55845 +    ** of the page, or to the number of cells in the page if the next page
 1.55846 +    ** to visit is the right-child of its parent.
 1.55847 +    **
 1.55848 +    ** If all pages in the tree have been visited, return SQLITE_OK to the
 1.55849 +    ** caller.
 1.55850 +    */
 1.55851 +    if( pPage->leaf ){
 1.55852 +      do {
 1.55853 +        if( pCur->iPage==0 ){
 1.55854 +          /* All pages of the b-tree have been visited. Return successfully. */
 1.55855 +          *pnEntry = nEntry;
 1.55856 +          return SQLITE_OK;
 1.55857 +        }
 1.55858 +        moveToParent(pCur);
 1.55859 +      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
 1.55860 +
 1.55861 +      pCur->aiIdx[pCur->iPage]++;
 1.55862 +      pPage = pCur->apPage[pCur->iPage];
 1.55863 +    }
 1.55864 +
 1.55865 +    /* Descend to the child node of the cell that the cursor currently 
 1.55866 +    ** points at. This is the right-child if (iIdx==pPage->nCell).
 1.55867 +    */
 1.55868 +    iIdx = pCur->aiIdx[pCur->iPage];
 1.55869 +    if( iIdx==pPage->nCell ){
 1.55870 +      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
 1.55871 +    }else{
 1.55872 +      rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
 1.55873 +    }
 1.55874 +  }
 1.55875 +
 1.55876 +  /* An error has occurred. Return an error code. */
 1.55877 +  return rc;
 1.55878 +}
 1.55879 +#endif
 1.55880 +
 1.55881 +/*
 1.55882 +** Return the pager associated with a BTree.  This routine is used for
 1.55883 +** testing and debugging only.
 1.55884 +*/
 1.55885 +SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
 1.55886 +  return p->pBt->pPager;
 1.55887 +}
 1.55888 +
 1.55889 +#ifndef SQLITE_OMIT_INTEGRITY_CHECK
 1.55890 +/*
 1.55891 +** Append a message to the error message string.
 1.55892 +*/
 1.55893 +static void checkAppendMsg(
 1.55894 +  IntegrityCk *pCheck,
 1.55895 +  char *zMsg1,
 1.55896 +  const char *zFormat,
 1.55897 +  ...
 1.55898 +){
 1.55899 +  va_list ap;
 1.55900 +  if( !pCheck->mxErr ) return;
 1.55901 +  pCheck->mxErr--;
 1.55902 +  pCheck->nErr++;
 1.55903 +  va_start(ap, zFormat);
 1.55904 +  if( pCheck->errMsg.nChar ){
 1.55905 +    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
 1.55906 +  }
 1.55907 +  if( zMsg1 ){
 1.55908 +    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
 1.55909 +  }
 1.55910 +  sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
 1.55911 +  va_end(ap);
 1.55912 +  if( pCheck->errMsg.mallocFailed ){
 1.55913 +    pCheck->mallocFailed = 1;
 1.55914 +  }
 1.55915 +}
 1.55916 +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 1.55917 +
 1.55918 +#ifndef SQLITE_OMIT_INTEGRITY_CHECK
 1.55919 +
 1.55920 +/*
 1.55921 +** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
 1.55922 +** corresponds to page iPg is already set.
 1.55923 +*/
 1.55924 +static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
 1.55925 +  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
 1.55926 +  return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
 1.55927 +}
 1.55928 +
 1.55929 +/*
 1.55930 +** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
 1.55931 +*/
 1.55932 +static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
 1.55933 +  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
 1.55934 +  pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
 1.55935 +}
 1.55936 +
 1.55937 +
 1.55938 +/*
 1.55939 +** Add 1 to the reference count for page iPage.  If this is the second
 1.55940 +** reference to the page, add an error message to pCheck->zErrMsg.
 1.55941 +** Return 1 if there are 2 ore more references to the page and 0 if
 1.55942 +** if this is the first reference to the page.
 1.55943 +**
 1.55944 +** Also check that the page number is in bounds.
 1.55945 +*/
 1.55946 +static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
 1.55947 +  if( iPage==0 ) return 1;
 1.55948 +  if( iPage>pCheck->nPage ){
 1.55949 +    checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
 1.55950 +    return 1;
 1.55951 +  }
 1.55952 +  if( getPageReferenced(pCheck, iPage) ){
 1.55953 +    checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
 1.55954 +    return 1;
 1.55955 +  }
 1.55956 +  setPageReferenced(pCheck, iPage);
 1.55957 +  return 0;
 1.55958 +}
 1.55959 +
 1.55960 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.55961 +/*
 1.55962 +** Check that the entry in the pointer-map for page iChild maps to 
 1.55963 +** page iParent, pointer type ptrType. If not, append an error message
 1.55964 +** to pCheck.
 1.55965 +*/
 1.55966 +static void checkPtrmap(
 1.55967 +  IntegrityCk *pCheck,   /* Integrity check context */
 1.55968 +  Pgno iChild,           /* Child page number */
 1.55969 +  u8 eType,              /* Expected pointer map type */
 1.55970 +  Pgno iParent,          /* Expected pointer map parent page number */
 1.55971 +  char *zContext         /* Context description (used for error msg) */
 1.55972 +){
 1.55973 +  int rc;
 1.55974 +  u8 ePtrmapType;
 1.55975 +  Pgno iPtrmapParent;
 1.55976 +
 1.55977 +  rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
 1.55978 +  if( rc!=SQLITE_OK ){
 1.55979 +    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
 1.55980 +    checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
 1.55981 +    return;
 1.55982 +  }
 1.55983 +
 1.55984 +  if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
 1.55985 +    checkAppendMsg(pCheck, zContext, 
 1.55986 +      "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
 1.55987 +      iChild, eType, iParent, ePtrmapType, iPtrmapParent);
 1.55988 +  }
 1.55989 +}
 1.55990 +#endif
 1.55991 +
 1.55992 +/*
 1.55993 +** Check the integrity of the freelist or of an overflow page list.
 1.55994 +** Verify that the number of pages on the list is N.
 1.55995 +*/
 1.55996 +static void checkList(
 1.55997 +  IntegrityCk *pCheck,  /* Integrity checking context */
 1.55998 +  int isFreeList,       /* True for a freelist.  False for overflow page list */
 1.55999 +  int iPage,            /* Page number for first page in the list */
 1.56000 +  int N,                /* Expected number of pages in the list */
 1.56001 +  char *zContext        /* Context for error messages */
 1.56002 +){
 1.56003 +  int i;
 1.56004 +  int expected = N;
 1.56005 +  int iFirst = iPage;
 1.56006 +  while( N-- > 0 && pCheck->mxErr ){
 1.56007 +    DbPage *pOvflPage;
 1.56008 +    unsigned char *pOvflData;
 1.56009 +    if( iPage<1 ){
 1.56010 +      checkAppendMsg(pCheck, zContext,
 1.56011 +         "%d of %d pages missing from overflow list starting at %d",
 1.56012 +          N+1, expected, iFirst);
 1.56013 +      break;
 1.56014 +    }
 1.56015 +    if( checkRef(pCheck, iPage, zContext) ) break;
 1.56016 +    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
 1.56017 +      checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
 1.56018 +      break;
 1.56019 +    }
 1.56020 +    pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
 1.56021 +    if( isFreeList ){
 1.56022 +      int n = get4byte(&pOvflData[4]);
 1.56023 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.56024 +      if( pCheck->pBt->autoVacuum ){
 1.56025 +        checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
 1.56026 +      }
 1.56027 +#endif
 1.56028 +      if( n>(int)pCheck->pBt->usableSize/4-2 ){
 1.56029 +        checkAppendMsg(pCheck, zContext,
 1.56030 +           "freelist leaf count too big on page %d", iPage);
 1.56031 +        N--;
 1.56032 +      }else{
 1.56033 +        for(i=0; i<n; i++){
 1.56034 +          Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
 1.56035 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.56036 +          if( pCheck->pBt->autoVacuum ){
 1.56037 +            checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
 1.56038 +          }
 1.56039 +#endif
 1.56040 +          checkRef(pCheck, iFreePage, zContext);
 1.56041 +        }
 1.56042 +        N -= n;
 1.56043 +      }
 1.56044 +    }
 1.56045 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.56046 +    else{
 1.56047 +      /* If this database supports auto-vacuum and iPage is not the last
 1.56048 +      ** page in this overflow list, check that the pointer-map entry for
 1.56049 +      ** the following page matches iPage.
 1.56050 +      */
 1.56051 +      if( pCheck->pBt->autoVacuum && N>0 ){
 1.56052 +        i = get4byte(pOvflData);
 1.56053 +        checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
 1.56054 +      }
 1.56055 +    }
 1.56056 +#endif
 1.56057 +    iPage = get4byte(pOvflData);
 1.56058 +    sqlite3PagerUnref(pOvflPage);
 1.56059 +  }
 1.56060 +}
 1.56061 +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 1.56062 +
 1.56063 +#ifndef SQLITE_OMIT_INTEGRITY_CHECK
 1.56064 +/*
 1.56065 +** Do various sanity checks on a single page of a tree.  Return
 1.56066 +** the tree depth.  Root pages return 0.  Parents of root pages
 1.56067 +** return 1, and so forth.
 1.56068 +** 
 1.56069 +** These checks are done:
 1.56070 +**
 1.56071 +**      1.  Make sure that cells and freeblocks do not overlap
 1.56072 +**          but combine to completely cover the page.
 1.56073 +**  NO  2.  Make sure cell keys are in order.
 1.56074 +**  NO  3.  Make sure no key is less than or equal to zLowerBound.
 1.56075 +**  NO  4.  Make sure no key is greater than or equal to zUpperBound.
 1.56076 +**      5.  Check the integrity of overflow pages.
 1.56077 +**      6.  Recursively call checkTreePage on all children.
 1.56078 +**      7.  Verify that the depth of all children is the same.
 1.56079 +**      8.  Make sure this page is at least 33% full or else it is
 1.56080 +**          the root of the tree.
 1.56081 +*/
 1.56082 +static int checkTreePage(
 1.56083 +  IntegrityCk *pCheck,  /* Context for the sanity check */
 1.56084 +  int iPage,            /* Page number of the page to check */
 1.56085 +  char *zParentContext, /* Parent context */
 1.56086 +  i64 *pnParentMinKey, 
 1.56087 +  i64 *pnParentMaxKey
 1.56088 +){
 1.56089 +  MemPage *pPage;
 1.56090 +  int i, rc, depth, d2, pgno, cnt;
 1.56091 +  int hdr, cellStart;
 1.56092 +  int nCell;
 1.56093 +  u8 *data;
 1.56094 +  BtShared *pBt;
 1.56095 +  int usableSize;
 1.56096 +  char zContext[100];
 1.56097 +  char *hit = 0;
 1.56098 +  i64 nMinKey = 0;
 1.56099 +  i64 nMaxKey = 0;
 1.56100 +
 1.56101 +  sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
 1.56102 +
 1.56103 +  /* Check that the page exists
 1.56104 +  */
 1.56105 +  pBt = pCheck->pBt;
 1.56106 +  usableSize = pBt->usableSize;
 1.56107 +  if( iPage==0 ) return 0;
 1.56108 +  if( checkRef(pCheck, iPage, zParentContext) ) return 0;
 1.56109 +  if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
 1.56110 +    checkAppendMsg(pCheck, zContext,
 1.56111 +       "unable to get the page. error code=%d", rc);
 1.56112 +    return 0;
 1.56113 +  }
 1.56114 +
 1.56115 +  /* Clear MemPage.isInit to make sure the corruption detection code in
 1.56116 +  ** btreeInitPage() is executed.  */
 1.56117 +  pPage->isInit = 0;
 1.56118 +  if( (rc = btreeInitPage(pPage))!=0 ){
 1.56119 +    assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
 1.56120 +    checkAppendMsg(pCheck, zContext, 
 1.56121 +                   "btreeInitPage() returns error code %d", rc);
 1.56122 +    releasePage(pPage);
 1.56123 +    return 0;
 1.56124 +  }
 1.56125 +
 1.56126 +  /* Check out all the cells.
 1.56127 +  */
 1.56128 +  depth = 0;
 1.56129 +  for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
 1.56130 +    u8 *pCell;
 1.56131 +    u32 sz;
 1.56132 +    CellInfo info;
 1.56133 +
 1.56134 +    /* Check payload overflow pages
 1.56135 +    */
 1.56136 +    sqlite3_snprintf(sizeof(zContext), zContext,
 1.56137 +             "On tree page %d cell %d: ", iPage, i);
 1.56138 +    pCell = findCell(pPage,i);
 1.56139 +    btreeParseCellPtr(pPage, pCell, &info);
 1.56140 +    sz = info.nData;
 1.56141 +    if( !pPage->intKey ) sz += (int)info.nKey;
 1.56142 +    /* For intKey pages, check that the keys are in order.
 1.56143 +    */
 1.56144 +    else if( i==0 ) nMinKey = nMaxKey = info.nKey;
 1.56145 +    else{
 1.56146 +      if( info.nKey <= nMaxKey ){
 1.56147 +        checkAppendMsg(pCheck, zContext, 
 1.56148 +            "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
 1.56149 +      }
 1.56150 +      nMaxKey = info.nKey;
 1.56151 +    }
 1.56152 +    assert( sz==info.nPayload );
 1.56153 +    if( (sz>info.nLocal) 
 1.56154 +     && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
 1.56155 +    ){
 1.56156 +      int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
 1.56157 +      Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
 1.56158 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.56159 +      if( pBt->autoVacuum ){
 1.56160 +        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
 1.56161 +      }
 1.56162 +#endif
 1.56163 +      checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
 1.56164 +    }
 1.56165 +
 1.56166 +    /* Check sanity of left child page.
 1.56167 +    */
 1.56168 +    if( !pPage->leaf ){
 1.56169 +      pgno = get4byte(pCell);
 1.56170 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.56171 +      if( pBt->autoVacuum ){
 1.56172 +        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
 1.56173 +      }
 1.56174 +#endif
 1.56175 +      d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
 1.56176 +      if( i>0 && d2!=depth ){
 1.56177 +        checkAppendMsg(pCheck, zContext, "Child page depth differs");
 1.56178 +      }
 1.56179 +      depth = d2;
 1.56180 +    }
 1.56181 +  }
 1.56182 +
 1.56183 +  if( !pPage->leaf ){
 1.56184 +    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 1.56185 +    sqlite3_snprintf(sizeof(zContext), zContext, 
 1.56186 +                     "On page %d at right child: ", iPage);
 1.56187 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.56188 +    if( pBt->autoVacuum ){
 1.56189 +      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
 1.56190 +    }
 1.56191 +#endif
 1.56192 +    checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
 1.56193 +  }
 1.56194 + 
 1.56195 +  /* For intKey leaf pages, check that the min/max keys are in order
 1.56196 +  ** with any left/parent/right pages.
 1.56197 +  */
 1.56198 +  if( pPage->leaf && pPage->intKey ){
 1.56199 +    /* if we are a left child page */
 1.56200 +    if( pnParentMinKey ){
 1.56201 +      /* if we are the left most child page */
 1.56202 +      if( !pnParentMaxKey ){
 1.56203 +        if( nMaxKey > *pnParentMinKey ){
 1.56204 +          checkAppendMsg(pCheck, zContext, 
 1.56205 +              "Rowid %lld out of order (max larger than parent min of %lld)",
 1.56206 +              nMaxKey, *pnParentMinKey);
 1.56207 +        }
 1.56208 +      }else{
 1.56209 +        if( nMinKey <= *pnParentMinKey ){
 1.56210 +          checkAppendMsg(pCheck, zContext, 
 1.56211 +              "Rowid %lld out of order (min less than parent min of %lld)",
 1.56212 +              nMinKey, *pnParentMinKey);
 1.56213 +        }
 1.56214 +        if( nMaxKey > *pnParentMaxKey ){
 1.56215 +          checkAppendMsg(pCheck, zContext, 
 1.56216 +              "Rowid %lld out of order (max larger than parent max of %lld)",
 1.56217 +              nMaxKey, *pnParentMaxKey);
 1.56218 +        }
 1.56219 +        *pnParentMinKey = nMaxKey;
 1.56220 +      }
 1.56221 +    /* else if we're a right child page */
 1.56222 +    } else if( pnParentMaxKey ){
 1.56223 +      if( nMinKey <= *pnParentMaxKey ){
 1.56224 +        checkAppendMsg(pCheck, zContext, 
 1.56225 +            "Rowid %lld out of order (min less than parent max of %lld)",
 1.56226 +            nMinKey, *pnParentMaxKey);
 1.56227 +      }
 1.56228 +    }
 1.56229 +  }
 1.56230 +
 1.56231 +  /* Check for complete coverage of the page
 1.56232 +  */
 1.56233 +  data = pPage->aData;
 1.56234 +  hdr = pPage->hdrOffset;
 1.56235 +  hit = sqlite3PageMalloc( pBt->pageSize );
 1.56236 +  if( hit==0 ){
 1.56237 +    pCheck->mallocFailed = 1;
 1.56238 +  }else{
 1.56239 +    int contentOffset = get2byteNotZero(&data[hdr+5]);
 1.56240 +    assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
 1.56241 +    memset(hit+contentOffset, 0, usableSize-contentOffset);
 1.56242 +    memset(hit, 1, contentOffset);
 1.56243 +    nCell = get2byte(&data[hdr+3]);
 1.56244 +    cellStart = hdr + 12 - 4*pPage->leaf;
 1.56245 +    for(i=0; i<nCell; i++){
 1.56246 +      int pc = get2byte(&data[cellStart+i*2]);
 1.56247 +      u32 size = 65536;
 1.56248 +      int j;
 1.56249 +      if( pc<=usableSize-4 ){
 1.56250 +        size = cellSizePtr(pPage, &data[pc]);
 1.56251 +      }
 1.56252 +      if( (int)(pc+size-1)>=usableSize ){
 1.56253 +        checkAppendMsg(pCheck, 0, 
 1.56254 +            "Corruption detected in cell %d on page %d",i,iPage);
 1.56255 +      }else{
 1.56256 +        for(j=pc+size-1; j>=pc; j--) hit[j]++;
 1.56257 +      }
 1.56258 +    }
 1.56259 +    i = get2byte(&data[hdr+1]);
 1.56260 +    while( i>0 ){
 1.56261 +      int size, j;
 1.56262 +      assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
 1.56263 +      size = get2byte(&data[i+2]);
 1.56264 +      assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
 1.56265 +      for(j=i+size-1; j>=i; j--) hit[j]++;
 1.56266 +      j = get2byte(&data[i]);
 1.56267 +      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
 1.56268 +      assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
 1.56269 +      i = j;
 1.56270 +    }
 1.56271 +    for(i=cnt=0; i<usableSize; i++){
 1.56272 +      if( hit[i]==0 ){
 1.56273 +        cnt++;
 1.56274 +      }else if( hit[i]>1 ){
 1.56275 +        checkAppendMsg(pCheck, 0,
 1.56276 +          "Multiple uses for byte %d of page %d", i, iPage);
 1.56277 +        break;
 1.56278 +      }
 1.56279 +    }
 1.56280 +    if( cnt!=data[hdr+7] ){
 1.56281 +      checkAppendMsg(pCheck, 0, 
 1.56282 +          "Fragmentation of %d bytes reported as %d on page %d",
 1.56283 +          cnt, data[hdr+7], iPage);
 1.56284 +    }
 1.56285 +  }
 1.56286 +  sqlite3PageFree(hit);
 1.56287 +  releasePage(pPage);
 1.56288 +  return depth+1;
 1.56289 +}
 1.56290 +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 1.56291 +
 1.56292 +#ifndef SQLITE_OMIT_INTEGRITY_CHECK
 1.56293 +/*
 1.56294 +** This routine does a complete check of the given BTree file.  aRoot[] is
 1.56295 +** an array of pages numbers were each page number is the root page of
 1.56296 +** a table.  nRoot is the number of entries in aRoot.
 1.56297 +**
 1.56298 +** A read-only or read-write transaction must be opened before calling
 1.56299 +** this function.
 1.56300 +**
 1.56301 +** Write the number of error seen in *pnErr.  Except for some memory
 1.56302 +** allocation errors,  an error message held in memory obtained from
 1.56303 +** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
 1.56304 +** returned.  If a memory allocation error occurs, NULL is returned.
 1.56305 +*/
 1.56306 +SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
 1.56307 +  Btree *p,     /* The btree to be checked */
 1.56308 +  int *aRoot,   /* An array of root pages numbers for individual trees */
 1.56309 +  int nRoot,    /* Number of entries in aRoot[] */
 1.56310 +  int mxErr,    /* Stop reporting errors after this many */
 1.56311 +  int *pnErr    /* Write number of errors seen to this variable */
 1.56312 +){
 1.56313 +  Pgno i;
 1.56314 +  int nRef;
 1.56315 +  IntegrityCk sCheck;
 1.56316 +  BtShared *pBt = p->pBt;
 1.56317 +  char zErr[100];
 1.56318 +
 1.56319 +  sqlite3BtreeEnter(p);
 1.56320 +  assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
 1.56321 +  nRef = sqlite3PagerRefcount(pBt->pPager);
 1.56322 +  sCheck.pBt = pBt;
 1.56323 +  sCheck.pPager = pBt->pPager;
 1.56324 +  sCheck.nPage = btreePagecount(sCheck.pBt);
 1.56325 +  sCheck.mxErr = mxErr;
 1.56326 +  sCheck.nErr = 0;
 1.56327 +  sCheck.mallocFailed = 0;
 1.56328 +  *pnErr = 0;
 1.56329 +  if( sCheck.nPage==0 ){
 1.56330 +    sqlite3BtreeLeave(p);
 1.56331 +    return 0;
 1.56332 +  }
 1.56333 +
 1.56334 +  sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
 1.56335 +  if( !sCheck.aPgRef ){
 1.56336 +    *pnErr = 1;
 1.56337 +    sqlite3BtreeLeave(p);
 1.56338 +    return 0;
 1.56339 +  }
 1.56340 +  i = PENDING_BYTE_PAGE(pBt);
 1.56341 +  if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 1.56342 +  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
 1.56343 +  sCheck.errMsg.useMalloc = 2;
 1.56344 +
 1.56345 +  /* Check the integrity of the freelist
 1.56346 +  */
 1.56347 +  checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
 1.56348 +            get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
 1.56349 +
 1.56350 +  /* Check all the tables.
 1.56351 +  */
 1.56352 +  for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
 1.56353 +    if( aRoot[i]==0 ) continue;
 1.56354 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.56355 +    if( pBt->autoVacuum && aRoot[i]>1 ){
 1.56356 +      checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
 1.56357 +    }
 1.56358 +#endif
 1.56359 +    checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
 1.56360 +  }
 1.56361 +
 1.56362 +  /* Make sure every page in the file is referenced
 1.56363 +  */
 1.56364 +  for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
 1.56365 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.56366 +    if( getPageReferenced(&sCheck, i)==0 ){
 1.56367 +      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 1.56368 +    }
 1.56369 +#else
 1.56370 +    /* If the database supports auto-vacuum, make sure no tables contain
 1.56371 +    ** references to pointer-map pages.
 1.56372 +    */
 1.56373 +    if( getPageReferenced(&sCheck, i)==0 && 
 1.56374 +       (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
 1.56375 +      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 1.56376 +    }
 1.56377 +    if( getPageReferenced(&sCheck, i)!=0 && 
 1.56378 +       (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
 1.56379 +      checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
 1.56380 +    }
 1.56381 +#endif
 1.56382 +  }
 1.56383 +
 1.56384 +  /* Make sure this analysis did not leave any unref() pages.
 1.56385 +  ** This is an internal consistency check; an integrity check
 1.56386 +  ** of the integrity check.
 1.56387 +  */
 1.56388 +  if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
 1.56389 +    checkAppendMsg(&sCheck, 0, 
 1.56390 +      "Outstanding page count goes from %d to %d during this analysis",
 1.56391 +      nRef, sqlite3PagerRefcount(pBt->pPager)
 1.56392 +    );
 1.56393 +  }
 1.56394 +
 1.56395 +  /* Clean  up and report errors.
 1.56396 +  */
 1.56397 +  sqlite3BtreeLeave(p);
 1.56398 +  sqlite3_free(sCheck.aPgRef);
 1.56399 +  if( sCheck.mallocFailed ){
 1.56400 +    sqlite3StrAccumReset(&sCheck.errMsg);
 1.56401 +    *pnErr = sCheck.nErr+1;
 1.56402 +    return 0;
 1.56403 +  }
 1.56404 +  *pnErr = sCheck.nErr;
 1.56405 +  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
 1.56406 +  return sqlite3StrAccumFinish(&sCheck.errMsg);
 1.56407 +}
 1.56408 +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 1.56409 +
 1.56410 +/*
 1.56411 +** Return the full pathname of the underlying database file.  Return
 1.56412 +** an empty string if the database is in-memory or a TEMP database.
 1.56413 +**
 1.56414 +** The pager filename is invariant as long as the pager is
 1.56415 +** open so it is safe to access without the BtShared mutex.
 1.56416 +*/
 1.56417 +SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
 1.56418 +  assert( p->pBt->pPager!=0 );
 1.56419 +  return sqlite3PagerFilename(p->pBt->pPager, 1);
 1.56420 +}
 1.56421 +
 1.56422 +/*
 1.56423 +** Return the pathname of the journal file for this database. The return
 1.56424 +** value of this routine is the same regardless of whether the journal file
 1.56425 +** has been created or not.
 1.56426 +**
 1.56427 +** The pager journal filename is invariant as long as the pager is
 1.56428 +** open so it is safe to access without the BtShared mutex.
 1.56429 +*/
 1.56430 +SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
 1.56431 +  assert( p->pBt->pPager!=0 );
 1.56432 +  return sqlite3PagerJournalname(p->pBt->pPager);
 1.56433 +}
 1.56434 +
 1.56435 +/*
 1.56436 +** Return non-zero if a transaction is active.
 1.56437 +*/
 1.56438 +SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
 1.56439 +  assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
 1.56440 +  return (p && (p->inTrans==TRANS_WRITE));
 1.56441 +}
 1.56442 +
 1.56443 +#ifndef SQLITE_OMIT_WAL
 1.56444 +/*
 1.56445 +** Run a checkpoint on the Btree passed as the first argument.
 1.56446 +**
 1.56447 +** Return SQLITE_LOCKED if this or any other connection has an open 
 1.56448 +** transaction on the shared-cache the argument Btree is connected to.
 1.56449 +**
 1.56450 +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 1.56451 +*/
 1.56452 +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
 1.56453 +  int rc = SQLITE_OK;
 1.56454 +  if( p ){
 1.56455 +    BtShared *pBt = p->pBt;
 1.56456 +    sqlite3BtreeEnter(p);
 1.56457 +    if( pBt->inTransaction!=TRANS_NONE ){
 1.56458 +      rc = SQLITE_LOCKED;
 1.56459 +    }else{
 1.56460 +      rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
 1.56461 +    }
 1.56462 +    sqlite3BtreeLeave(p);
 1.56463 +  }
 1.56464 +  return rc;
 1.56465 +}
 1.56466 +#endif
 1.56467 +
 1.56468 +/*
 1.56469 +** Return non-zero if a read (or write) transaction is active.
 1.56470 +*/
 1.56471 +SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
 1.56472 +  assert( p );
 1.56473 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.56474 +  return p->inTrans!=TRANS_NONE;
 1.56475 +}
 1.56476 +
 1.56477 +SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
 1.56478 +  assert( p );
 1.56479 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.56480 +  return p->nBackup!=0;
 1.56481 +}
 1.56482 +
 1.56483 +/*
 1.56484 +** This function returns a pointer to a blob of memory associated with
 1.56485 +** a single shared-btree. The memory is used by client code for its own
 1.56486 +** purposes (for example, to store a high-level schema associated with 
 1.56487 +** the shared-btree). The btree layer manages reference counting issues.
 1.56488 +**
 1.56489 +** The first time this is called on a shared-btree, nBytes bytes of memory
 1.56490 +** are allocated, zeroed, and returned to the caller. For each subsequent 
 1.56491 +** call the nBytes parameter is ignored and a pointer to the same blob
 1.56492 +** of memory returned. 
 1.56493 +**
 1.56494 +** If the nBytes parameter is 0 and the blob of memory has not yet been
 1.56495 +** allocated, a null pointer is returned. If the blob has already been
 1.56496 +** allocated, it is returned as normal.
 1.56497 +**
 1.56498 +** Just before the shared-btree is closed, the function passed as the 
 1.56499 +** xFree argument when the memory allocation was made is invoked on the 
 1.56500 +** blob of allocated memory. The xFree function should not call sqlite3_free()
 1.56501 +** on the memory, the btree layer does that.
 1.56502 +*/
 1.56503 +SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
 1.56504 +  BtShared *pBt = p->pBt;
 1.56505 +  sqlite3BtreeEnter(p);
 1.56506 +  if( !pBt->pSchema && nBytes ){
 1.56507 +    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
 1.56508 +    pBt->xFreeSchema = xFree;
 1.56509 +  }
 1.56510 +  sqlite3BtreeLeave(p);
 1.56511 +  return pBt->pSchema;
 1.56512 +}
 1.56513 +
 1.56514 +/*
 1.56515 +** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 
 1.56516 +** btree as the argument handle holds an exclusive lock on the 
 1.56517 +** sqlite_master table. Otherwise SQLITE_OK.
 1.56518 +*/
 1.56519 +SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
 1.56520 +  int rc;
 1.56521 +  assert( sqlite3_mutex_held(p->db->mutex) );
 1.56522 +  sqlite3BtreeEnter(p);
 1.56523 +  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
 1.56524 +  assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
 1.56525 +  sqlite3BtreeLeave(p);
 1.56526 +  return rc;
 1.56527 +}
 1.56528 +
 1.56529 +
 1.56530 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.56531 +/*
 1.56532 +** Obtain a lock on the table whose root page is iTab.  The
 1.56533 +** lock is a write lock if isWritelock is true or a read lock
 1.56534 +** if it is false.
 1.56535 +*/
 1.56536 +SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
 1.56537 +  int rc = SQLITE_OK;
 1.56538 +  assert( p->inTrans!=TRANS_NONE );
 1.56539 +  if( p->sharable ){
 1.56540 +    u8 lockType = READ_LOCK + isWriteLock;
 1.56541 +    assert( READ_LOCK+1==WRITE_LOCK );
 1.56542 +    assert( isWriteLock==0 || isWriteLock==1 );
 1.56543 +
 1.56544 +    sqlite3BtreeEnter(p);
 1.56545 +    rc = querySharedCacheTableLock(p, iTab, lockType);
 1.56546 +    if( rc==SQLITE_OK ){
 1.56547 +      rc = setSharedCacheTableLock(p, iTab, lockType);
 1.56548 +    }
 1.56549 +    sqlite3BtreeLeave(p);
 1.56550 +  }
 1.56551 +  return rc;
 1.56552 +}
 1.56553 +#endif
 1.56554 +
 1.56555 +#ifndef SQLITE_OMIT_INCRBLOB
 1.56556 +/*
 1.56557 +** Argument pCsr must be a cursor opened for writing on an 
 1.56558 +** INTKEY table currently pointing at a valid table entry. 
 1.56559 +** This function modifies the data stored as part of that entry.
 1.56560 +**
 1.56561 +** Only the data content may only be modified, it is not possible to 
 1.56562 +** change the length of the data stored. If this function is called with
 1.56563 +** parameters that attempt to write past the end of the existing data,
 1.56564 +** no modifications are made and SQLITE_CORRUPT is returned.
 1.56565 +*/
 1.56566 +SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
 1.56567 +  int rc;
 1.56568 +  assert( cursorHoldsMutex(pCsr) );
 1.56569 +  assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
 1.56570 +  assert( pCsr->isIncrblobHandle );
 1.56571 +
 1.56572 +  rc = restoreCursorPosition(pCsr);
 1.56573 +  if( rc!=SQLITE_OK ){
 1.56574 +    return rc;
 1.56575 +  }
 1.56576 +  assert( pCsr->eState!=CURSOR_REQUIRESEEK );
 1.56577 +  if( pCsr->eState!=CURSOR_VALID ){
 1.56578 +    return SQLITE_ABORT;
 1.56579 +  }
 1.56580 +
 1.56581 +  /* Check some assumptions: 
 1.56582 +  **   (a) the cursor is open for writing,
 1.56583 +  **   (b) there is a read/write transaction open,
 1.56584 +  **   (c) the connection holds a write-lock on the table (if required),
 1.56585 +  **   (d) there are no conflicting read-locks, and
 1.56586 +  **   (e) the cursor points at a valid row of an intKey table.
 1.56587 +  */
 1.56588 +  if( !pCsr->wrFlag ){
 1.56589 +    return SQLITE_READONLY;
 1.56590 +  }
 1.56591 +  assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
 1.56592 +              && pCsr->pBt->inTransaction==TRANS_WRITE );
 1.56593 +  assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
 1.56594 +  assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
 1.56595 +  assert( pCsr->apPage[pCsr->iPage]->intKey );
 1.56596 +
 1.56597 +  return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
 1.56598 +}
 1.56599 +
 1.56600 +/* 
 1.56601 +** Set a flag on this cursor to cache the locations of pages from the 
 1.56602 +** overflow list for the current row. This is used by cursors opened
 1.56603 +** for incremental blob IO only.
 1.56604 +**
 1.56605 +** This function sets a flag only. The actual page location cache
 1.56606 +** (stored in BtCursor.aOverflow[]) is allocated and used by function
 1.56607 +** accessPayload() (the worker function for sqlite3BtreeData() and
 1.56608 +** sqlite3BtreePutData()).
 1.56609 +*/
 1.56610 +SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
 1.56611 +  assert( cursorHoldsMutex(pCur) );
 1.56612 +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 1.56613 +  invalidateOverflowCache(pCur);
 1.56614 +  pCur->isIncrblobHandle = 1;
 1.56615 +}
 1.56616 +#endif
 1.56617 +
 1.56618 +/*
 1.56619 +** Set both the "read version" (single byte at byte offset 18) and 
 1.56620 +** "write version" (single byte at byte offset 19) fields in the database
 1.56621 +** header to iVersion.
 1.56622 +*/
 1.56623 +SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
 1.56624 +  BtShared *pBt = pBtree->pBt;
 1.56625 +  int rc;                         /* Return code */
 1.56626 + 
 1.56627 +  assert( iVersion==1 || iVersion==2 );
 1.56628 +
 1.56629 +  /* If setting the version fields to 1, do not automatically open the
 1.56630 +  ** WAL connection, even if the version fields are currently set to 2.
 1.56631 +  */
 1.56632 +  pBt->btsFlags &= ~BTS_NO_WAL;
 1.56633 +  if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
 1.56634 +
 1.56635 +  rc = sqlite3BtreeBeginTrans(pBtree, 0);
 1.56636 +  if( rc==SQLITE_OK ){
 1.56637 +    u8 *aData = pBt->pPage1->aData;
 1.56638 +    if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
 1.56639 +      rc = sqlite3BtreeBeginTrans(pBtree, 2);
 1.56640 +      if( rc==SQLITE_OK ){
 1.56641 +        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 1.56642 +        if( rc==SQLITE_OK ){
 1.56643 +          aData[18] = (u8)iVersion;
 1.56644 +          aData[19] = (u8)iVersion;
 1.56645 +        }
 1.56646 +      }
 1.56647 +    }
 1.56648 +  }
 1.56649 +
 1.56650 +  pBt->btsFlags &= ~BTS_NO_WAL;
 1.56651 +  return rc;
 1.56652 +}
 1.56653 +
 1.56654 +/*
 1.56655 +** set the mask of hint flags for cursor pCsr. Currently the only valid
 1.56656 +** values are 0 and BTREE_BULKLOAD.
 1.56657 +*/
 1.56658 +SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
 1.56659 +  assert( mask==BTREE_BULKLOAD || mask==0 );
 1.56660 +  pCsr->hints = mask;
 1.56661 +}
 1.56662 +
 1.56663 +/************** End of btree.c ***********************************************/
 1.56664 +/************** Begin file backup.c ******************************************/
 1.56665 +/*
 1.56666 +** 2009 January 28
 1.56667 +**
 1.56668 +** The author disclaims copyright to this source code.  In place of
 1.56669 +** a legal notice, here is a blessing:
 1.56670 +**
 1.56671 +**    May you do good and not evil.
 1.56672 +**    May you find forgiveness for yourself and forgive others.
 1.56673 +**    May you share freely, never taking more than you give.
 1.56674 +**
 1.56675 +*************************************************************************
 1.56676 +** This file contains the implementation of the sqlite3_backup_XXX() 
 1.56677 +** API functions and the related features.
 1.56678 +*/
 1.56679 +
 1.56680 +/* Macro to find the minimum of two numeric values.
 1.56681 +*/
 1.56682 +#ifndef MIN
 1.56683 +# define MIN(x,y) ((x)<(y)?(x):(y))
 1.56684 +#endif
 1.56685 +
 1.56686 +/*
 1.56687 +** Structure allocated for each backup operation.
 1.56688 +*/
 1.56689 +struct sqlite3_backup {
 1.56690 +  sqlite3* pDestDb;        /* Destination database handle */
 1.56691 +  Btree *pDest;            /* Destination b-tree file */
 1.56692 +  u32 iDestSchema;         /* Original schema cookie in destination */
 1.56693 +  int bDestLocked;         /* True once a write-transaction is open on pDest */
 1.56694 +
 1.56695 +  Pgno iNext;              /* Page number of the next source page to copy */
 1.56696 +  sqlite3* pSrcDb;         /* Source database handle */
 1.56697 +  Btree *pSrc;             /* Source b-tree file */
 1.56698 +
 1.56699 +  int rc;                  /* Backup process error code */
 1.56700 +
 1.56701 +  /* These two variables are set by every call to backup_step(). They are
 1.56702 +  ** read by calls to backup_remaining() and backup_pagecount().
 1.56703 +  */
 1.56704 +  Pgno nRemaining;         /* Number of pages left to copy */
 1.56705 +  Pgno nPagecount;         /* Total number of pages to copy */
 1.56706 +
 1.56707 +  int isAttached;          /* True once backup has been registered with pager */
 1.56708 +  sqlite3_backup *pNext;   /* Next backup associated with source pager */
 1.56709 +};
 1.56710 +
 1.56711 +/*
 1.56712 +** THREAD SAFETY NOTES:
 1.56713 +**
 1.56714 +**   Once it has been created using backup_init(), a single sqlite3_backup
 1.56715 +**   structure may be accessed via two groups of thread-safe entry points:
 1.56716 +**
 1.56717 +**     * Via the sqlite3_backup_XXX() API function backup_step() and 
 1.56718 +**       backup_finish(). Both these functions obtain the source database
 1.56719 +**       handle mutex and the mutex associated with the source BtShared 
 1.56720 +**       structure, in that order.
 1.56721 +**
 1.56722 +**     * Via the BackupUpdate() and BackupRestart() functions, which are
 1.56723 +**       invoked by the pager layer to report various state changes in
 1.56724 +**       the page cache associated with the source database. The mutex
 1.56725 +**       associated with the source database BtShared structure will always 
 1.56726 +**       be held when either of these functions are invoked.
 1.56727 +**
 1.56728 +**   The other sqlite3_backup_XXX() API functions, backup_remaining() and
 1.56729 +**   backup_pagecount() are not thread-safe functions. If they are called
 1.56730 +**   while some other thread is calling backup_step() or backup_finish(),
 1.56731 +**   the values returned may be invalid. There is no way for a call to
 1.56732 +**   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
 1.56733 +**   or backup_pagecount().
 1.56734 +**
 1.56735 +**   Depending on the SQLite configuration, the database handles and/or
 1.56736 +**   the Btree objects may have their own mutexes that require locking.
 1.56737 +**   Non-sharable Btrees (in-memory databases for example), do not have
 1.56738 +**   associated mutexes.
 1.56739 +*/
 1.56740 +
 1.56741 +/*
 1.56742 +** Return a pointer corresponding to database zDb (i.e. "main", "temp")
 1.56743 +** in connection handle pDb. If such a database cannot be found, return
 1.56744 +** a NULL pointer and write an error message to pErrorDb.
 1.56745 +**
 1.56746 +** If the "temp" database is requested, it may need to be opened by this 
 1.56747 +** function. If an error occurs while doing so, return 0 and write an 
 1.56748 +** error message to pErrorDb.
 1.56749 +*/
 1.56750 +static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
 1.56751 +  int i = sqlite3FindDbName(pDb, zDb);
 1.56752 +
 1.56753 +  if( i==1 ){
 1.56754 +    Parse *pParse;
 1.56755 +    int rc = 0;
 1.56756 +    pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
 1.56757 +    if( pParse==0 ){
 1.56758 +      sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
 1.56759 +      rc = SQLITE_NOMEM;
 1.56760 +    }else{
 1.56761 +      pParse->db = pDb;
 1.56762 +      if( sqlite3OpenTempDatabase(pParse) ){
 1.56763 +        sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
 1.56764 +        rc = SQLITE_ERROR;
 1.56765 +      }
 1.56766 +      sqlite3DbFree(pErrorDb, pParse->zErrMsg);
 1.56767 +      sqlite3StackFree(pErrorDb, pParse);
 1.56768 +    }
 1.56769 +    if( rc ){
 1.56770 +      return 0;
 1.56771 +    }
 1.56772 +  }
 1.56773 +
 1.56774 +  if( i<0 ){
 1.56775 +    sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
 1.56776 +    return 0;
 1.56777 +  }
 1.56778 +
 1.56779 +  return pDb->aDb[i].pBt;
 1.56780 +}
 1.56781 +
 1.56782 +/*
 1.56783 +** Attempt to set the page size of the destination to match the page size
 1.56784 +** of the source.
 1.56785 +*/
 1.56786 +static int setDestPgsz(sqlite3_backup *p){
 1.56787 +  int rc;
 1.56788 +  rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
 1.56789 +  return rc;
 1.56790 +}
 1.56791 +
 1.56792 +/*
 1.56793 +** Create an sqlite3_backup process to copy the contents of zSrcDb from
 1.56794 +** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
 1.56795 +** a pointer to the new sqlite3_backup object.
 1.56796 +**
 1.56797 +** If an error occurs, NULL is returned and an error code and error message
 1.56798 +** stored in database handle pDestDb.
 1.56799 +*/
 1.56800 +SQLITE_API sqlite3_backup *sqlite3_backup_init(
 1.56801 +  sqlite3* pDestDb,                     /* Database to write to */
 1.56802 +  const char *zDestDb,                  /* Name of database within pDestDb */
 1.56803 +  sqlite3* pSrcDb,                      /* Database connection to read from */
 1.56804 +  const char *zSrcDb                    /* Name of database within pSrcDb */
 1.56805 +){
 1.56806 +  sqlite3_backup *p;                    /* Value to return */
 1.56807 +
 1.56808 +  /* Lock the source database handle. The destination database
 1.56809 +  ** handle is not locked in this routine, but it is locked in
 1.56810 +  ** sqlite3_backup_step(). The user is required to ensure that no
 1.56811 +  ** other thread accesses the destination handle for the duration
 1.56812 +  ** of the backup operation.  Any attempt to use the destination
 1.56813 +  ** database connection while a backup is in progress may cause
 1.56814 +  ** a malfunction or a deadlock.
 1.56815 +  */
 1.56816 +  sqlite3_mutex_enter(pSrcDb->mutex);
 1.56817 +  sqlite3_mutex_enter(pDestDb->mutex);
 1.56818 +
 1.56819 +  if( pSrcDb==pDestDb ){
 1.56820 +    sqlite3Error(
 1.56821 +        pDestDb, SQLITE_ERROR, "source and destination must be distinct"
 1.56822 +    );
 1.56823 +    p = 0;
 1.56824 +  }else {
 1.56825 +    /* Allocate space for a new sqlite3_backup object...
 1.56826 +    ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 1.56827 +    ** call to sqlite3_backup_init() and is destroyed by a call to
 1.56828 +    ** sqlite3_backup_finish(). */
 1.56829 +    p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
 1.56830 +    if( !p ){
 1.56831 +      sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
 1.56832 +    }
 1.56833 +  }
 1.56834 +
 1.56835 +  /* If the allocation succeeded, populate the new object. */
 1.56836 +  if( p ){
 1.56837 +    p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
 1.56838 +    p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
 1.56839 +    p->pDestDb = pDestDb;
 1.56840 +    p->pSrcDb = pSrcDb;
 1.56841 +    p->iNext = 1;
 1.56842 +    p->isAttached = 0;
 1.56843 +
 1.56844 +    if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
 1.56845 +      /* One (or both) of the named databases did not exist or an OOM
 1.56846 +      ** error was hit.  The error has already been written into the
 1.56847 +      ** pDestDb handle.  All that is left to do here is free the
 1.56848 +      ** sqlite3_backup structure.
 1.56849 +      */
 1.56850 +      sqlite3_free(p);
 1.56851 +      p = 0;
 1.56852 +    }
 1.56853 +  }
 1.56854 +  if( p ){
 1.56855 +    p->pSrc->nBackup++;
 1.56856 +  }
 1.56857 +
 1.56858 +  sqlite3_mutex_leave(pDestDb->mutex);
 1.56859 +  sqlite3_mutex_leave(pSrcDb->mutex);
 1.56860 +  return p;
 1.56861 +}
 1.56862 +
 1.56863 +/*
 1.56864 +** Argument rc is an SQLite error code. Return true if this error is 
 1.56865 +** considered fatal if encountered during a backup operation. All errors
 1.56866 +** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
 1.56867 +*/
 1.56868 +static int isFatalError(int rc){
 1.56869 +  return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
 1.56870 +}
 1.56871 +
 1.56872 +/*
 1.56873 +** Parameter zSrcData points to a buffer containing the data for 
 1.56874 +** page iSrcPg from the source database. Copy this data into the 
 1.56875 +** destination database.
 1.56876 +*/
 1.56877 +static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
 1.56878 +  Pager * const pDestPager = sqlite3BtreePager(p->pDest);
 1.56879 +  const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
 1.56880 +  int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
 1.56881 +  const int nCopy = MIN(nSrcPgsz, nDestPgsz);
 1.56882 +  const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
 1.56883 +#ifdef SQLITE_HAS_CODEC
 1.56884 +  /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
 1.56885 +  ** guaranteed that the shared-mutex is held by this thread, handle
 1.56886 +  ** p->pSrc may not actually be the owner.  */
 1.56887 +  int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
 1.56888 +  int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
 1.56889 +#endif
 1.56890 +  int rc = SQLITE_OK;
 1.56891 +  i64 iOff;
 1.56892 +
 1.56893 +  assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
 1.56894 +  assert( p->bDestLocked );
 1.56895 +  assert( !isFatalError(p->rc) );
 1.56896 +  assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
 1.56897 +  assert( zSrcData );
 1.56898 +
 1.56899 +  /* Catch the case where the destination is an in-memory database and the
 1.56900 +  ** page sizes of the source and destination differ. 
 1.56901 +  */
 1.56902 +  if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
 1.56903 +    rc = SQLITE_READONLY;
 1.56904 +  }
 1.56905 +
 1.56906 +#ifdef SQLITE_HAS_CODEC
 1.56907 +  /* Backup is not possible if the page size of the destination is changing
 1.56908 +  ** and a codec is in use.
 1.56909 +  */
 1.56910 +  if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
 1.56911 +    rc = SQLITE_READONLY;
 1.56912 +  }
 1.56913 +
 1.56914 +  /* Backup is not possible if the number of bytes of reserve space differ
 1.56915 +  ** between source and destination.  If there is a difference, try to
 1.56916 +  ** fix the destination to agree with the source.  If that is not possible,
 1.56917 +  ** then the backup cannot proceed.
 1.56918 +  */
 1.56919 +  if( nSrcReserve!=nDestReserve ){
 1.56920 +    u32 newPgsz = nSrcPgsz;
 1.56921 +    rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
 1.56922 +    if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
 1.56923 +  }
 1.56924 +#endif
 1.56925 +
 1.56926 +  /* This loop runs once for each destination page spanned by the source 
 1.56927 +  ** page. For each iteration, variable iOff is set to the byte offset
 1.56928 +  ** of the destination page.
 1.56929 +  */
 1.56930 +  for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
 1.56931 +    DbPage *pDestPg = 0;
 1.56932 +    Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
 1.56933 +    if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
 1.56934 +    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
 1.56935 +     && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
 1.56936 +    ){
 1.56937 +      const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
 1.56938 +      u8 *zDestData = sqlite3PagerGetData(pDestPg);
 1.56939 +      u8 *zOut = &zDestData[iOff%nDestPgsz];
 1.56940 +
 1.56941 +      /* Copy the data from the source page into the destination page.
 1.56942 +      ** Then clear the Btree layer MemPage.isInit flag. Both this module
 1.56943 +      ** and the pager code use this trick (clearing the first byte
 1.56944 +      ** of the page 'extra' space to invalidate the Btree layers
 1.56945 +      ** cached parse of the page). MemPage.isInit is marked 
 1.56946 +      ** "MUST BE FIRST" for this purpose.
 1.56947 +      */
 1.56948 +      memcpy(zOut, zIn, nCopy);
 1.56949 +      ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
 1.56950 +    }
 1.56951 +    sqlite3PagerUnref(pDestPg);
 1.56952 +  }
 1.56953 +
 1.56954 +  return rc;
 1.56955 +}
 1.56956 +
 1.56957 +/*
 1.56958 +** If pFile is currently larger than iSize bytes, then truncate it to
 1.56959 +** exactly iSize bytes. If pFile is not larger than iSize bytes, then
 1.56960 +** this function is a no-op.
 1.56961 +**
 1.56962 +** Return SQLITE_OK if everything is successful, or an SQLite error 
 1.56963 +** code if an error occurs.
 1.56964 +*/
 1.56965 +static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
 1.56966 +  i64 iCurrent;
 1.56967 +  int rc = sqlite3OsFileSize(pFile, &iCurrent);
 1.56968 +  if( rc==SQLITE_OK && iCurrent>iSize ){
 1.56969 +    rc = sqlite3OsTruncate(pFile, iSize);
 1.56970 +  }
 1.56971 +  return rc;
 1.56972 +}
 1.56973 +
 1.56974 +/*
 1.56975 +** Register this backup object with the associated source pager for
 1.56976 +** callbacks when pages are changed or the cache invalidated.
 1.56977 +*/
 1.56978 +static void attachBackupObject(sqlite3_backup *p){
 1.56979 +  sqlite3_backup **pp;
 1.56980 +  assert( sqlite3BtreeHoldsMutex(p->pSrc) );
 1.56981 +  pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
 1.56982 +  p->pNext = *pp;
 1.56983 +  *pp = p;
 1.56984 +  p->isAttached = 1;
 1.56985 +}
 1.56986 +
 1.56987 +/*
 1.56988 +** Copy nPage pages from the source b-tree to the destination.
 1.56989 +*/
 1.56990 +SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
 1.56991 +  int rc;
 1.56992 +  int destMode;       /* Destination journal mode */
 1.56993 +  int pgszSrc = 0;    /* Source page size */
 1.56994 +  int pgszDest = 0;   /* Destination page size */
 1.56995 +
 1.56996 +  sqlite3_mutex_enter(p->pSrcDb->mutex);
 1.56997 +  sqlite3BtreeEnter(p->pSrc);
 1.56998 +  if( p->pDestDb ){
 1.56999 +    sqlite3_mutex_enter(p->pDestDb->mutex);
 1.57000 +  }
 1.57001 +
 1.57002 +  rc = p->rc;
 1.57003 +  if( !isFatalError(rc) ){
 1.57004 +    Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
 1.57005 +    Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
 1.57006 +    int ii;                            /* Iterator variable */
 1.57007 +    int nSrcPage = -1;                 /* Size of source db in pages */
 1.57008 +    int bCloseTrans = 0;               /* True if src db requires unlocking */
 1.57009 +
 1.57010 +    /* If the source pager is currently in a write-transaction, return
 1.57011 +    ** SQLITE_BUSY immediately.
 1.57012 +    */
 1.57013 +    if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
 1.57014 +      rc = SQLITE_BUSY;
 1.57015 +    }else{
 1.57016 +      rc = SQLITE_OK;
 1.57017 +    }
 1.57018 +
 1.57019 +    /* Lock the destination database, if it is not locked already. */
 1.57020 +    if( SQLITE_OK==rc && p->bDestLocked==0
 1.57021 +     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
 1.57022 +    ){
 1.57023 +      p->bDestLocked = 1;
 1.57024 +      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
 1.57025 +    }
 1.57026 +
 1.57027 +    /* If there is no open read-transaction on the source database, open
 1.57028 +    ** one now. If a transaction is opened here, then it will be closed
 1.57029 +    ** before this function exits.
 1.57030 +    */
 1.57031 +    if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
 1.57032 +      rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
 1.57033 +      bCloseTrans = 1;
 1.57034 +    }
 1.57035 +
 1.57036 +    /* Do not allow backup if the destination database is in WAL mode
 1.57037 +    ** and the page sizes are different between source and destination */
 1.57038 +    pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
 1.57039 +    pgszDest = sqlite3BtreeGetPageSize(p->pDest);
 1.57040 +    destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
 1.57041 +    if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
 1.57042 +      rc = SQLITE_READONLY;
 1.57043 +    }
 1.57044 +  
 1.57045 +    /* Now that there is a read-lock on the source database, query the
 1.57046 +    ** source pager for the number of pages in the database.
 1.57047 +    */
 1.57048 +    nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
 1.57049 +    assert( nSrcPage>=0 );
 1.57050 +    for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
 1.57051 +      const Pgno iSrcPg = p->iNext;                 /* Source page number */
 1.57052 +      if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
 1.57053 +        DbPage *pSrcPg;                             /* Source page object */
 1.57054 +        rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
 1.57055 +        if( rc==SQLITE_OK ){
 1.57056 +          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
 1.57057 +          sqlite3PagerUnref(pSrcPg);
 1.57058 +        }
 1.57059 +      }
 1.57060 +      p->iNext++;
 1.57061 +    }
 1.57062 +    if( rc==SQLITE_OK ){
 1.57063 +      p->nPagecount = nSrcPage;
 1.57064 +      p->nRemaining = nSrcPage+1-p->iNext;
 1.57065 +      if( p->iNext>(Pgno)nSrcPage ){
 1.57066 +        rc = SQLITE_DONE;
 1.57067 +      }else if( !p->isAttached ){
 1.57068 +        attachBackupObject(p);
 1.57069 +      }
 1.57070 +    }
 1.57071 +  
 1.57072 +    /* Update the schema version field in the destination database. This
 1.57073 +    ** is to make sure that the schema-version really does change in
 1.57074 +    ** the case where the source and destination databases have the
 1.57075 +    ** same schema version.
 1.57076 +    */
 1.57077 +    if( rc==SQLITE_DONE ){
 1.57078 +      if( nSrcPage==0 ){
 1.57079 +        rc = sqlite3BtreeNewDb(p->pDest);
 1.57080 +        nSrcPage = 1;
 1.57081 +      }
 1.57082 +      if( rc==SQLITE_OK || rc==SQLITE_DONE ){
 1.57083 +        rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
 1.57084 +      }
 1.57085 +      if( rc==SQLITE_OK ){
 1.57086 +        if( p->pDestDb ){
 1.57087 +          sqlite3ResetAllSchemasOfConnection(p->pDestDb);
 1.57088 +        }
 1.57089 +        if( destMode==PAGER_JOURNALMODE_WAL ){
 1.57090 +          rc = sqlite3BtreeSetVersion(p->pDest, 2);
 1.57091 +        }
 1.57092 +      }
 1.57093 +      if( rc==SQLITE_OK ){
 1.57094 +        int nDestTruncate;
 1.57095 +        /* Set nDestTruncate to the final number of pages in the destination
 1.57096 +        ** database. The complication here is that the destination page
 1.57097 +        ** size may be different to the source page size. 
 1.57098 +        **
 1.57099 +        ** If the source page size is smaller than the destination page size, 
 1.57100 +        ** round up. In this case the call to sqlite3OsTruncate() below will
 1.57101 +        ** fix the size of the file. However it is important to call
 1.57102 +        ** sqlite3PagerTruncateImage() here so that any pages in the 
 1.57103 +        ** destination file that lie beyond the nDestTruncate page mark are
 1.57104 +        ** journalled by PagerCommitPhaseOne() before they are destroyed
 1.57105 +        ** by the file truncation.
 1.57106 +        */
 1.57107 +        assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
 1.57108 +        assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
 1.57109 +        if( pgszSrc<pgszDest ){
 1.57110 +          int ratio = pgszDest/pgszSrc;
 1.57111 +          nDestTruncate = (nSrcPage+ratio-1)/ratio;
 1.57112 +          if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
 1.57113 +            nDestTruncate--;
 1.57114 +          }
 1.57115 +        }else{
 1.57116 +          nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
 1.57117 +        }
 1.57118 +        assert( nDestTruncate>0 );
 1.57119 +        sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
 1.57120 +
 1.57121 +        if( pgszSrc<pgszDest ){
 1.57122 +          /* If the source page-size is smaller than the destination page-size,
 1.57123 +          ** two extra things may need to happen:
 1.57124 +          **
 1.57125 +          **   * The destination may need to be truncated, and
 1.57126 +          **
 1.57127 +          **   * Data stored on the pages immediately following the 
 1.57128 +          **     pending-byte page in the source database may need to be
 1.57129 +          **     copied into the destination database.
 1.57130 +          */
 1.57131 +          const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
 1.57132 +          sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
 1.57133 +          i64 iOff;
 1.57134 +          i64 iEnd;
 1.57135 +
 1.57136 +          assert( pFile );
 1.57137 +          assert( nDestTruncate==0 
 1.57138 +              || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
 1.57139 +                nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
 1.57140 +             && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
 1.57141 +          ));
 1.57142 +
 1.57143 +          /* This call ensures that all data required to recreate the original
 1.57144 +          ** database has been stored in the journal for pDestPager and the
 1.57145 +          ** journal synced to disk. So at this point we may safely modify
 1.57146 +          ** the database file in any way, knowing that if a power failure
 1.57147 +          ** occurs, the original database will be reconstructed from the 
 1.57148 +          ** journal file.  */
 1.57149 +          rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
 1.57150 +
 1.57151 +          /* Write the extra pages and truncate the database file as required */
 1.57152 +          iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
 1.57153 +          for(
 1.57154 +            iOff=PENDING_BYTE+pgszSrc; 
 1.57155 +            rc==SQLITE_OK && iOff<iEnd; 
 1.57156 +            iOff+=pgszSrc
 1.57157 +          ){
 1.57158 +            PgHdr *pSrcPg = 0;
 1.57159 +            const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
 1.57160 +            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
 1.57161 +            if( rc==SQLITE_OK ){
 1.57162 +              u8 *zData = sqlite3PagerGetData(pSrcPg);
 1.57163 +              rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
 1.57164 +            }
 1.57165 +            sqlite3PagerUnref(pSrcPg);
 1.57166 +          }
 1.57167 +          if( rc==SQLITE_OK ){
 1.57168 +            rc = backupTruncateFile(pFile, iSize);
 1.57169 +          }
 1.57170 +
 1.57171 +          /* Sync the database file to disk. */
 1.57172 +          if( rc==SQLITE_OK ){
 1.57173 +            rc = sqlite3PagerSync(pDestPager);
 1.57174 +          }
 1.57175 +        }else{
 1.57176 +          rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
 1.57177 +        }
 1.57178 +    
 1.57179 +        /* Finish committing the transaction to the destination database. */
 1.57180 +        if( SQLITE_OK==rc
 1.57181 +         && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
 1.57182 +        ){
 1.57183 +          rc = SQLITE_DONE;
 1.57184 +        }
 1.57185 +      }
 1.57186 +    }
 1.57187 +  
 1.57188 +    /* If bCloseTrans is true, then this function opened a read transaction
 1.57189 +    ** on the source database. Close the read transaction here. There is
 1.57190 +    ** no need to check the return values of the btree methods here, as
 1.57191 +    ** "committing" a read-only transaction cannot fail.
 1.57192 +    */
 1.57193 +    if( bCloseTrans ){
 1.57194 +      TESTONLY( int rc2 );
 1.57195 +      TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
 1.57196 +      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
 1.57197 +      assert( rc2==SQLITE_OK );
 1.57198 +    }
 1.57199 +  
 1.57200 +    if( rc==SQLITE_IOERR_NOMEM ){
 1.57201 +      rc = SQLITE_NOMEM;
 1.57202 +    }
 1.57203 +    p->rc = rc;
 1.57204 +  }
 1.57205 +  if( p->pDestDb ){
 1.57206 +    sqlite3_mutex_leave(p->pDestDb->mutex);
 1.57207 +  }
 1.57208 +  sqlite3BtreeLeave(p->pSrc);
 1.57209 +  sqlite3_mutex_leave(p->pSrcDb->mutex);
 1.57210 +  return rc;
 1.57211 +}
 1.57212 +
 1.57213 +/*
 1.57214 +** Release all resources associated with an sqlite3_backup* handle.
 1.57215 +*/
 1.57216 +SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
 1.57217 +  sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
 1.57218 +  sqlite3 *pSrcDb;                     /* Source database connection */
 1.57219 +  int rc;                              /* Value to return */
 1.57220 +
 1.57221 +  /* Enter the mutexes */
 1.57222 +  if( p==0 ) return SQLITE_OK;
 1.57223 +  pSrcDb = p->pSrcDb;
 1.57224 +  sqlite3_mutex_enter(pSrcDb->mutex);
 1.57225 +  sqlite3BtreeEnter(p->pSrc);
 1.57226 +  if( p->pDestDb ){
 1.57227 +    sqlite3_mutex_enter(p->pDestDb->mutex);
 1.57228 +  }
 1.57229 +
 1.57230 +  /* Detach this backup from the source pager. */
 1.57231 +  if( p->pDestDb ){
 1.57232 +    p->pSrc->nBackup--;
 1.57233 +  }
 1.57234 +  if( p->isAttached ){
 1.57235 +    pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
 1.57236 +    while( *pp!=p ){
 1.57237 +      pp = &(*pp)->pNext;
 1.57238 +    }
 1.57239 +    *pp = p->pNext;
 1.57240 +  }
 1.57241 +
 1.57242 +  /* If a transaction is still open on the Btree, roll it back. */
 1.57243 +  sqlite3BtreeRollback(p->pDest, SQLITE_OK);
 1.57244 +
 1.57245 +  /* Set the error code of the destination database handle. */
 1.57246 +  rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
 1.57247 +  sqlite3Error(p->pDestDb, rc, 0);
 1.57248 +
 1.57249 +  /* Exit the mutexes and free the backup context structure. */
 1.57250 +  if( p->pDestDb ){
 1.57251 +    sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
 1.57252 +  }
 1.57253 +  sqlite3BtreeLeave(p->pSrc);
 1.57254 +  if( p->pDestDb ){
 1.57255 +    /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 1.57256 +    ** call to sqlite3_backup_init() and is destroyed by a call to
 1.57257 +    ** sqlite3_backup_finish(). */
 1.57258 +    sqlite3_free(p);
 1.57259 +  }
 1.57260 +  sqlite3LeaveMutexAndCloseZombie(pSrcDb);
 1.57261 +  return rc;
 1.57262 +}
 1.57263 +
 1.57264 +/*
 1.57265 +** Return the number of pages still to be backed up as of the most recent
 1.57266 +** call to sqlite3_backup_step().
 1.57267 +*/
 1.57268 +SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
 1.57269 +  return p->nRemaining;
 1.57270 +}
 1.57271 +
 1.57272 +/*
 1.57273 +** Return the total number of pages in the source database as of the most 
 1.57274 +** recent call to sqlite3_backup_step().
 1.57275 +*/
 1.57276 +SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
 1.57277 +  return p->nPagecount;
 1.57278 +}
 1.57279 +
 1.57280 +/*
 1.57281 +** This function is called after the contents of page iPage of the
 1.57282 +** source database have been modified. If page iPage has already been 
 1.57283 +** copied into the destination database, then the data written to the
 1.57284 +** destination is now invalidated. The destination copy of iPage needs
 1.57285 +** to be updated with the new data before the backup operation is
 1.57286 +** complete.
 1.57287 +**
 1.57288 +** It is assumed that the mutex associated with the BtShared object
 1.57289 +** corresponding to the source database is held when this function is
 1.57290 +** called.
 1.57291 +*/
 1.57292 +SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
 1.57293 +  sqlite3_backup *p;                   /* Iterator variable */
 1.57294 +  for(p=pBackup; p; p=p->pNext){
 1.57295 +    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
 1.57296 +    if( !isFatalError(p->rc) && iPage<p->iNext ){
 1.57297 +      /* The backup process p has already copied page iPage. But now it
 1.57298 +      ** has been modified by a transaction on the source pager. Copy
 1.57299 +      ** the new data into the backup.
 1.57300 +      */
 1.57301 +      int rc;
 1.57302 +      assert( p->pDestDb );
 1.57303 +      sqlite3_mutex_enter(p->pDestDb->mutex);
 1.57304 +      rc = backupOnePage(p, iPage, aData);
 1.57305 +      sqlite3_mutex_leave(p->pDestDb->mutex);
 1.57306 +      assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
 1.57307 +      if( rc!=SQLITE_OK ){
 1.57308 +        p->rc = rc;
 1.57309 +      }
 1.57310 +    }
 1.57311 +  }
 1.57312 +}
 1.57313 +
 1.57314 +/*
 1.57315 +** Restart the backup process. This is called when the pager layer
 1.57316 +** detects that the database has been modified by an external database
 1.57317 +** connection. In this case there is no way of knowing which of the
 1.57318 +** pages that have been copied into the destination database are still 
 1.57319 +** valid and which are not, so the entire process needs to be restarted.
 1.57320 +**
 1.57321 +** It is assumed that the mutex associated with the BtShared object
 1.57322 +** corresponding to the source database is held when this function is
 1.57323 +** called.
 1.57324 +*/
 1.57325 +SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
 1.57326 +  sqlite3_backup *p;                   /* Iterator variable */
 1.57327 +  for(p=pBackup; p; p=p->pNext){
 1.57328 +    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
 1.57329 +    p->iNext = 1;
 1.57330 +  }
 1.57331 +}
 1.57332 +
 1.57333 +#ifndef SQLITE_OMIT_VACUUM
 1.57334 +/*
 1.57335 +** Copy the complete content of pBtFrom into pBtTo.  A transaction
 1.57336 +** must be active for both files.
 1.57337 +**
 1.57338 +** The size of file pTo may be reduced by this operation. If anything 
 1.57339 +** goes wrong, the transaction on pTo is rolled back. If successful, the 
 1.57340 +** transaction is committed before returning.
 1.57341 +*/
 1.57342 +SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
 1.57343 +  int rc;
 1.57344 +  sqlite3_file *pFd;              /* File descriptor for database pTo */
 1.57345 +  sqlite3_backup b;
 1.57346 +  sqlite3BtreeEnter(pTo);
 1.57347 +  sqlite3BtreeEnter(pFrom);
 1.57348 +
 1.57349 +  assert( sqlite3BtreeIsInTrans(pTo) );
 1.57350 +  pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
 1.57351 +  if( pFd->pMethods ){
 1.57352 +    i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
 1.57353 +    rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
 1.57354 +    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 1.57355 +    if( rc ) goto copy_finished;
 1.57356 +  }
 1.57357 +
 1.57358 +  /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
 1.57359 +  ** to 0. This is used by the implementations of sqlite3_backup_step()
 1.57360 +  ** and sqlite3_backup_finish() to detect that they are being called
 1.57361 +  ** from this function, not directly by the user.
 1.57362 +  */
 1.57363 +  memset(&b, 0, sizeof(b));
 1.57364 +  b.pSrcDb = pFrom->db;
 1.57365 +  b.pSrc = pFrom;
 1.57366 +  b.pDest = pTo;
 1.57367 +  b.iNext = 1;
 1.57368 +
 1.57369 +  /* 0x7FFFFFFF is the hard limit for the number of pages in a database
 1.57370 +  ** file. By passing this as the number of pages to copy to
 1.57371 +  ** sqlite3_backup_step(), we can guarantee that the copy finishes 
 1.57372 +  ** within a single call (unless an error occurs). The assert() statement
 1.57373 +  ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
 1.57374 +  ** or an error code.
 1.57375 +  */
 1.57376 +  sqlite3_backup_step(&b, 0x7FFFFFFF);
 1.57377 +  assert( b.rc!=SQLITE_OK );
 1.57378 +  rc = sqlite3_backup_finish(&b);
 1.57379 +  if( rc==SQLITE_OK ){
 1.57380 +    pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
 1.57381 +  }else{
 1.57382 +    sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
 1.57383 +  }
 1.57384 +
 1.57385 +  assert( sqlite3BtreeIsInTrans(pTo)==0 );
 1.57386 +copy_finished:
 1.57387 +  sqlite3BtreeLeave(pFrom);
 1.57388 +  sqlite3BtreeLeave(pTo);
 1.57389 +  return rc;
 1.57390 +}
 1.57391 +#endif /* SQLITE_OMIT_VACUUM */
 1.57392 +
 1.57393 +/************** End of backup.c **********************************************/
 1.57394 +/************** Begin file vdbemem.c *****************************************/
 1.57395 +/*
 1.57396 +** 2004 May 26
 1.57397 +**
 1.57398 +** The author disclaims copyright to this source code.  In place of
 1.57399 +** a legal notice, here is a blessing:
 1.57400 +**
 1.57401 +**    May you do good and not evil.
 1.57402 +**    May you find forgiveness for yourself and forgive others.
 1.57403 +**    May you share freely, never taking more than you give.
 1.57404 +**
 1.57405 +*************************************************************************
 1.57406 +**
 1.57407 +** This file contains code use to manipulate "Mem" structure.  A "Mem"
 1.57408 +** stores a single value in the VDBE.  Mem is an opaque structure visible
 1.57409 +** only within the VDBE.  Interface routines refer to a Mem using the
 1.57410 +** name sqlite_value
 1.57411 +*/
 1.57412 +
 1.57413 +/*
 1.57414 +** If pMem is an object with a valid string representation, this routine
 1.57415 +** ensures the internal encoding for the string representation is
 1.57416 +** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
 1.57417 +**
 1.57418 +** If pMem is not a string object, or the encoding of the string
 1.57419 +** representation is already stored using the requested encoding, then this
 1.57420 +** routine is a no-op.
 1.57421 +**
 1.57422 +** SQLITE_OK is returned if the conversion is successful (or not required).
 1.57423 +** SQLITE_NOMEM may be returned if a malloc() fails during conversion
 1.57424 +** between formats.
 1.57425 +*/
 1.57426 +SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
 1.57427 +  int rc;
 1.57428 +  assert( (pMem->flags&MEM_RowSet)==0 );
 1.57429 +  assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
 1.57430 +           || desiredEnc==SQLITE_UTF16BE );
 1.57431 +  if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
 1.57432 +    return SQLITE_OK;
 1.57433 +  }
 1.57434 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57435 +#ifdef SQLITE_OMIT_UTF16
 1.57436 +  return SQLITE_ERROR;
 1.57437 +#else
 1.57438 +
 1.57439 +  /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
 1.57440 +  ** then the encoding of the value may not have changed.
 1.57441 +  */
 1.57442 +  rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
 1.57443 +  assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
 1.57444 +  assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
 1.57445 +  assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
 1.57446 +  return rc;
 1.57447 +#endif
 1.57448 +}
 1.57449 +
 1.57450 +/*
 1.57451 +** Make sure pMem->z points to a writable allocation of at least 
 1.57452 +** n bytes.
 1.57453 +**
 1.57454 +** If the third argument passed to this function is true, then memory
 1.57455 +** cell pMem must contain a string or blob. In this case the content is
 1.57456 +** preserved. Otherwise, if the third parameter to this function is false,
 1.57457 +** any current string or blob value may be discarded.
 1.57458 +**
 1.57459 +** This function sets the MEM_Dyn flag and clears any xDel callback.
 1.57460 +** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
 1.57461 +** not set, Mem.n is zeroed.
 1.57462 +*/
 1.57463 +SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
 1.57464 +  assert( 1 >=
 1.57465 +    ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
 1.57466 +    (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
 1.57467 +    ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
 1.57468 +    ((pMem->flags&MEM_Static) ? 1 : 0)
 1.57469 +  );
 1.57470 +  assert( (pMem->flags&MEM_RowSet)==0 );
 1.57471 +
 1.57472 +  /* If the preserve flag is set to true, then the memory cell must already
 1.57473 +  ** contain a valid string or blob value.  */
 1.57474 +  assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 1.57475 +
 1.57476 +  if( n<32 ) n = 32;
 1.57477 +  if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
 1.57478 +    if( preserve && pMem->z==pMem->zMalloc ){
 1.57479 +      pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 1.57480 +      preserve = 0;
 1.57481 +    }else{
 1.57482 +      sqlite3DbFree(pMem->db, pMem->zMalloc);
 1.57483 +      pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 1.57484 +    }
 1.57485 +  }
 1.57486 +
 1.57487 +  if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
 1.57488 +    memcpy(pMem->zMalloc, pMem->z, pMem->n);
 1.57489 +  }
 1.57490 +  if( pMem->flags&MEM_Dyn && pMem->xDel ){
 1.57491 +    assert( pMem->xDel!=SQLITE_DYNAMIC );
 1.57492 +    pMem->xDel((void *)(pMem->z));
 1.57493 +  }
 1.57494 +
 1.57495 +  pMem->z = pMem->zMalloc;
 1.57496 +  if( pMem->z==0 ){
 1.57497 +    pMem->flags = MEM_Null;
 1.57498 +  }else{
 1.57499 +    pMem->flags &= ~(MEM_Ephem|MEM_Static);
 1.57500 +  }
 1.57501 +  pMem->xDel = 0;
 1.57502 +  return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
 1.57503 +}
 1.57504 +
 1.57505 +/*
 1.57506 +** Make the given Mem object MEM_Dyn.  In other words, make it so
 1.57507 +** that any TEXT or BLOB content is stored in memory obtained from
 1.57508 +** malloc().  In this way, we know that the memory is safe to be
 1.57509 +** overwritten or altered.
 1.57510 +**
 1.57511 +** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
 1.57512 +*/
 1.57513 +SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
 1.57514 +  int f;
 1.57515 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57516 +  assert( (pMem->flags&MEM_RowSet)==0 );
 1.57517 +  ExpandBlob(pMem);
 1.57518 +  f = pMem->flags;
 1.57519 +  if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
 1.57520 +    if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
 1.57521 +      return SQLITE_NOMEM;
 1.57522 +    }
 1.57523 +    pMem->z[pMem->n] = 0;
 1.57524 +    pMem->z[pMem->n+1] = 0;
 1.57525 +    pMem->flags |= MEM_Term;
 1.57526 +#ifdef SQLITE_DEBUG
 1.57527 +    pMem->pScopyFrom = 0;
 1.57528 +#endif
 1.57529 +  }
 1.57530 +
 1.57531 +  return SQLITE_OK;
 1.57532 +}
 1.57533 +
 1.57534 +/*
 1.57535 +** If the given Mem* has a zero-filled tail, turn it into an ordinary
 1.57536 +** blob stored in dynamically allocated space.
 1.57537 +*/
 1.57538 +#ifndef SQLITE_OMIT_INCRBLOB
 1.57539 +SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
 1.57540 +  if( pMem->flags & MEM_Zero ){
 1.57541 +    int nByte;
 1.57542 +    assert( pMem->flags&MEM_Blob );
 1.57543 +    assert( (pMem->flags&MEM_RowSet)==0 );
 1.57544 +    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57545 +
 1.57546 +    /* Set nByte to the number of bytes required to store the expanded blob. */
 1.57547 +    nByte = pMem->n + pMem->u.nZero;
 1.57548 +    if( nByte<=0 ){
 1.57549 +      nByte = 1;
 1.57550 +    }
 1.57551 +    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
 1.57552 +      return SQLITE_NOMEM;
 1.57553 +    }
 1.57554 +
 1.57555 +    memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
 1.57556 +    pMem->n += pMem->u.nZero;
 1.57557 +    pMem->flags &= ~(MEM_Zero|MEM_Term);
 1.57558 +  }
 1.57559 +  return SQLITE_OK;
 1.57560 +}
 1.57561 +#endif
 1.57562 +
 1.57563 +
 1.57564 +/*
 1.57565 +** Make sure the given Mem is \u0000 terminated.
 1.57566 +*/
 1.57567 +SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
 1.57568 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57569 +  if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
 1.57570 +    return SQLITE_OK;   /* Nothing to do */
 1.57571 +  }
 1.57572 +  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
 1.57573 +    return SQLITE_NOMEM;
 1.57574 +  }
 1.57575 +  pMem->z[pMem->n] = 0;
 1.57576 +  pMem->z[pMem->n+1] = 0;
 1.57577 +  pMem->flags |= MEM_Term;
 1.57578 +  return SQLITE_OK;
 1.57579 +}
 1.57580 +
 1.57581 +/*
 1.57582 +** Add MEM_Str to the set of representations for the given Mem.  Numbers
 1.57583 +** are converted using sqlite3_snprintf().  Converting a BLOB to a string
 1.57584 +** is a no-op.
 1.57585 +**
 1.57586 +** Existing representations MEM_Int and MEM_Real are *not* invalidated.
 1.57587 +**
 1.57588 +** A MEM_Null value will never be passed to this function. This function is
 1.57589 +** used for converting values to text for returning to the user (i.e. via
 1.57590 +** sqlite3_value_text()), or for ensuring that values to be used as btree
 1.57591 +** keys are strings. In the former case a NULL pointer is returned the
 1.57592 +** user and the later is an internal programming error.
 1.57593 +*/
 1.57594 +SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
 1.57595 +  int rc = SQLITE_OK;
 1.57596 +  int fg = pMem->flags;
 1.57597 +  const int nByte = 32;
 1.57598 +
 1.57599 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57600 +  assert( !(fg&MEM_Zero) );
 1.57601 +  assert( !(fg&(MEM_Str|MEM_Blob)) );
 1.57602 +  assert( fg&(MEM_Int|MEM_Real) );
 1.57603 +  assert( (pMem->flags&MEM_RowSet)==0 );
 1.57604 +  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 1.57605 +
 1.57606 +
 1.57607 +  if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
 1.57608 +    return SQLITE_NOMEM;
 1.57609 +  }
 1.57610 +
 1.57611 +  /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
 1.57612 +  ** string representation of the value. Then, if the required encoding
 1.57613 +  ** is UTF-16le or UTF-16be do a translation.
 1.57614 +  ** 
 1.57615 +  ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
 1.57616 +  */
 1.57617 +  if( fg & MEM_Int ){
 1.57618 +    sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
 1.57619 +  }else{
 1.57620 +    assert( fg & MEM_Real );
 1.57621 +    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
 1.57622 +  }
 1.57623 +  pMem->n = sqlite3Strlen30(pMem->z);
 1.57624 +  pMem->enc = SQLITE_UTF8;
 1.57625 +  pMem->flags |= MEM_Str|MEM_Term;
 1.57626 +  sqlite3VdbeChangeEncoding(pMem, enc);
 1.57627 +  return rc;
 1.57628 +}
 1.57629 +
 1.57630 +/*
 1.57631 +** Memory cell pMem contains the context of an aggregate function.
 1.57632 +** This routine calls the finalize method for that function.  The
 1.57633 +** result of the aggregate is stored back into pMem.
 1.57634 +**
 1.57635 +** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
 1.57636 +** otherwise.
 1.57637 +*/
 1.57638 +SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
 1.57639 +  int rc = SQLITE_OK;
 1.57640 +  if( ALWAYS(pFunc && pFunc->xFinalize) ){
 1.57641 +    sqlite3_context ctx;
 1.57642 +    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
 1.57643 +    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57644 +    memset(&ctx, 0, sizeof(ctx));
 1.57645 +    ctx.s.flags = MEM_Null;
 1.57646 +    ctx.s.db = pMem->db;
 1.57647 +    ctx.pMem = pMem;
 1.57648 +    ctx.pFunc = pFunc;
 1.57649 +    pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
 1.57650 +    assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
 1.57651 +    sqlite3DbFree(pMem->db, pMem->zMalloc);
 1.57652 +    memcpy(pMem, &ctx.s, sizeof(ctx.s));
 1.57653 +    rc = ctx.isError;
 1.57654 +  }
 1.57655 +  return rc;
 1.57656 +}
 1.57657 +
 1.57658 +/*
 1.57659 +** If the memory cell contains a string value that must be freed by
 1.57660 +** invoking an external callback, free it now. Calling this function
 1.57661 +** does not free any Mem.zMalloc buffer.
 1.57662 +*/
 1.57663 +SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
 1.57664 +  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
 1.57665 +  if( p->flags&MEM_Agg ){
 1.57666 +    sqlite3VdbeMemFinalize(p, p->u.pDef);
 1.57667 +    assert( (p->flags & MEM_Agg)==0 );
 1.57668 +    sqlite3VdbeMemRelease(p);
 1.57669 +  }else if( p->flags&MEM_Dyn && p->xDel ){
 1.57670 +    assert( (p->flags&MEM_RowSet)==0 );
 1.57671 +    assert( p->xDel!=SQLITE_DYNAMIC );
 1.57672 +    p->xDel((void *)p->z);
 1.57673 +    p->xDel = 0;
 1.57674 +  }else if( p->flags&MEM_RowSet ){
 1.57675 +    sqlite3RowSetClear(p->u.pRowSet);
 1.57676 +  }else if( p->flags&MEM_Frame ){
 1.57677 +    sqlite3VdbeMemSetNull(p);
 1.57678 +  }
 1.57679 +}
 1.57680 +
 1.57681 +/*
 1.57682 +** Release any memory held by the Mem. This may leave the Mem in an
 1.57683 +** inconsistent state, for example with (Mem.z==0) and
 1.57684 +** (Mem.type==SQLITE_TEXT).
 1.57685 +*/
 1.57686 +SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
 1.57687 +  VdbeMemRelease(p);
 1.57688 +  sqlite3DbFree(p->db, p->zMalloc);
 1.57689 +  p->z = 0;
 1.57690 +  p->zMalloc = 0;
 1.57691 +  p->xDel = 0;
 1.57692 +}
 1.57693 +
 1.57694 +/*
 1.57695 +** Convert a 64-bit IEEE double into a 64-bit signed integer.
 1.57696 +** If the double is too large, return 0x8000000000000000.
 1.57697 +**
 1.57698 +** Most systems appear to do this simply by assigning
 1.57699 +** variables and without the extra range tests.  But
 1.57700 +** there are reports that windows throws an expection
 1.57701 +** if the floating point value is out of range. (See ticket #2880.)
 1.57702 +** Because we do not completely understand the problem, we will
 1.57703 +** take the conservative approach and always do range tests
 1.57704 +** before attempting the conversion.
 1.57705 +*/
 1.57706 +static i64 doubleToInt64(double r){
 1.57707 +#ifdef SQLITE_OMIT_FLOATING_POINT
 1.57708 +  /* When floating-point is omitted, double and int64 are the same thing */
 1.57709 +  return r;
 1.57710 +#else
 1.57711 +  /*
 1.57712 +  ** Many compilers we encounter do not define constants for the
 1.57713 +  ** minimum and maximum 64-bit integers, or they define them
 1.57714 +  ** inconsistently.  And many do not understand the "LL" notation.
 1.57715 +  ** So we define our own static constants here using nothing
 1.57716 +  ** larger than a 32-bit integer constant.
 1.57717 +  */
 1.57718 +  static const i64 maxInt = LARGEST_INT64;
 1.57719 +  static const i64 minInt = SMALLEST_INT64;
 1.57720 +
 1.57721 +  if( r<(double)minInt ){
 1.57722 +    return minInt;
 1.57723 +  }else if( r>(double)maxInt ){
 1.57724 +    /* minInt is correct here - not maxInt.  It turns out that assigning
 1.57725 +    ** a very large positive number to an integer results in a very large
 1.57726 +    ** negative integer.  This makes no sense, but it is what x86 hardware
 1.57727 +    ** does so for compatibility we will do the same in software. */
 1.57728 +    return minInt;
 1.57729 +  }else{
 1.57730 +    return (i64)r;
 1.57731 +  }
 1.57732 +#endif
 1.57733 +}
 1.57734 +
 1.57735 +/*
 1.57736 +** Return some kind of integer value which is the best we can do
 1.57737 +** at representing the value that *pMem describes as an integer.
 1.57738 +** If pMem is an integer, then the value is exact.  If pMem is
 1.57739 +** a floating-point then the value returned is the integer part.
 1.57740 +** If pMem is a string or blob, then we make an attempt to convert
 1.57741 +** it into a integer and return that.  If pMem represents an
 1.57742 +** an SQL-NULL value, return 0.
 1.57743 +**
 1.57744 +** If pMem represents a string value, its encoding might be changed.
 1.57745 +*/
 1.57746 +SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
 1.57747 +  int flags;
 1.57748 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57749 +  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 1.57750 +  flags = pMem->flags;
 1.57751 +  if( flags & MEM_Int ){
 1.57752 +    return pMem->u.i;
 1.57753 +  }else if( flags & MEM_Real ){
 1.57754 +    return doubleToInt64(pMem->r);
 1.57755 +  }else if( flags & (MEM_Str|MEM_Blob) ){
 1.57756 +    i64 value = 0;
 1.57757 +    assert( pMem->z || pMem->n==0 );
 1.57758 +    testcase( pMem->z==0 );
 1.57759 +    sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
 1.57760 +    return value;
 1.57761 +  }else{
 1.57762 +    return 0;
 1.57763 +  }
 1.57764 +}
 1.57765 +
 1.57766 +/*
 1.57767 +** Return the best representation of pMem that we can get into a
 1.57768 +** double.  If pMem is already a double or an integer, return its
 1.57769 +** value.  If it is a string or blob, try to convert it to a double.
 1.57770 +** If it is a NULL, return 0.0.
 1.57771 +*/
 1.57772 +SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
 1.57773 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57774 +  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 1.57775 +  if( pMem->flags & MEM_Real ){
 1.57776 +    return pMem->r;
 1.57777 +  }else if( pMem->flags & MEM_Int ){
 1.57778 +    return (double)pMem->u.i;
 1.57779 +  }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
 1.57780 +    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 1.57781 +    double val = (double)0;
 1.57782 +    sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
 1.57783 +    return val;
 1.57784 +  }else{
 1.57785 +    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 1.57786 +    return (double)0;
 1.57787 +  }
 1.57788 +}
 1.57789 +
 1.57790 +/*
 1.57791 +** The MEM structure is already a MEM_Real.  Try to also make it a
 1.57792 +** MEM_Int if we can.
 1.57793 +*/
 1.57794 +SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
 1.57795 +  assert( pMem->flags & MEM_Real );
 1.57796 +  assert( (pMem->flags & MEM_RowSet)==0 );
 1.57797 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57798 +  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 1.57799 +
 1.57800 +  pMem->u.i = doubleToInt64(pMem->r);
 1.57801 +
 1.57802 +  /* Only mark the value as an integer if
 1.57803 +  **
 1.57804 +  **    (1) the round-trip conversion real->int->real is a no-op, and
 1.57805 +  **    (2) The integer is neither the largest nor the smallest
 1.57806 +  **        possible integer (ticket #3922)
 1.57807 +  **
 1.57808 +  ** The second and third terms in the following conditional enforces
 1.57809 +  ** the second condition under the assumption that addition overflow causes
 1.57810 +  ** values to wrap around.  On x86 hardware, the third term is always
 1.57811 +  ** true and could be omitted.  But we leave it in because other
 1.57812 +  ** architectures might behave differently.
 1.57813 +  */
 1.57814 +  if( pMem->r==(double)pMem->u.i
 1.57815 +   && pMem->u.i>SMALLEST_INT64
 1.57816 +#if defined(__i486__) || defined(__x86_64__)
 1.57817 +   && ALWAYS(pMem->u.i<LARGEST_INT64)
 1.57818 +#else
 1.57819 +   && pMem->u.i<LARGEST_INT64
 1.57820 +#endif
 1.57821 +  ){
 1.57822 +    pMem->flags |= MEM_Int;
 1.57823 +  }
 1.57824 +}
 1.57825 +
 1.57826 +/*
 1.57827 +** Convert pMem to type integer.  Invalidate any prior representations.
 1.57828 +*/
 1.57829 +SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
 1.57830 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57831 +  assert( (pMem->flags & MEM_RowSet)==0 );
 1.57832 +  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 1.57833 +
 1.57834 +  pMem->u.i = sqlite3VdbeIntValue(pMem);
 1.57835 +  MemSetTypeFlag(pMem, MEM_Int);
 1.57836 +  return SQLITE_OK;
 1.57837 +}
 1.57838 +
 1.57839 +/*
 1.57840 +** Convert pMem so that it is of type MEM_Real.
 1.57841 +** Invalidate any prior representations.
 1.57842 +*/
 1.57843 +SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
 1.57844 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57845 +  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 1.57846 +
 1.57847 +  pMem->r = sqlite3VdbeRealValue(pMem);
 1.57848 +  MemSetTypeFlag(pMem, MEM_Real);
 1.57849 +  return SQLITE_OK;
 1.57850 +}
 1.57851 +
 1.57852 +/*
 1.57853 +** Convert pMem so that it has types MEM_Real or MEM_Int or both.
 1.57854 +** Invalidate any prior representations.
 1.57855 +**
 1.57856 +** Every effort is made to force the conversion, even if the input
 1.57857 +** is a string that does not look completely like a number.  Convert
 1.57858 +** as much of the string as we can and ignore the rest.
 1.57859 +*/
 1.57860 +SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
 1.57861 +  if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
 1.57862 +    assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
 1.57863 +    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.57864 +    if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
 1.57865 +      MemSetTypeFlag(pMem, MEM_Int);
 1.57866 +    }else{
 1.57867 +      pMem->r = sqlite3VdbeRealValue(pMem);
 1.57868 +      MemSetTypeFlag(pMem, MEM_Real);
 1.57869 +      sqlite3VdbeIntegerAffinity(pMem);
 1.57870 +    }
 1.57871 +  }
 1.57872 +  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
 1.57873 +  pMem->flags &= ~(MEM_Str|MEM_Blob);
 1.57874 +  return SQLITE_OK;
 1.57875 +}
 1.57876 +
 1.57877 +/*
 1.57878 +** Delete any previous value and set the value stored in *pMem to NULL.
 1.57879 +*/
 1.57880 +SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
 1.57881 +  if( pMem->flags & MEM_Frame ){
 1.57882 +    VdbeFrame *pFrame = pMem->u.pFrame;
 1.57883 +    pFrame->pParent = pFrame->v->pDelFrame;
 1.57884 +    pFrame->v->pDelFrame = pFrame;
 1.57885 +  }
 1.57886 +  if( pMem->flags & MEM_RowSet ){
 1.57887 +    sqlite3RowSetClear(pMem->u.pRowSet);
 1.57888 +  }
 1.57889 +  MemSetTypeFlag(pMem, MEM_Null);
 1.57890 +  pMem->type = SQLITE_NULL;
 1.57891 +}
 1.57892 +
 1.57893 +/*
 1.57894 +** Delete any previous value and set the value to be a BLOB of length
 1.57895 +** n containing all zeros.
 1.57896 +*/
 1.57897 +SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
 1.57898 +  sqlite3VdbeMemRelease(pMem);
 1.57899 +  pMem->flags = MEM_Blob|MEM_Zero;
 1.57900 +  pMem->type = SQLITE_BLOB;
 1.57901 +  pMem->n = 0;
 1.57902 +  if( n<0 ) n = 0;
 1.57903 +  pMem->u.nZero = n;
 1.57904 +  pMem->enc = SQLITE_UTF8;
 1.57905 +
 1.57906 +#ifdef SQLITE_OMIT_INCRBLOB
 1.57907 +  sqlite3VdbeMemGrow(pMem, n, 0);
 1.57908 +  if( pMem->z ){
 1.57909 +    pMem->n = n;
 1.57910 +    memset(pMem->z, 0, n);
 1.57911 +  }
 1.57912 +#endif
 1.57913 +}
 1.57914 +
 1.57915 +/*
 1.57916 +** Delete any previous value and set the value stored in *pMem to val,
 1.57917 +** manifest type INTEGER.
 1.57918 +*/
 1.57919 +SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
 1.57920 +  sqlite3VdbeMemRelease(pMem);
 1.57921 +  pMem->u.i = val;
 1.57922 +  pMem->flags = MEM_Int;
 1.57923 +  pMem->type = SQLITE_INTEGER;
 1.57924 +}
 1.57925 +
 1.57926 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.57927 +/*
 1.57928 +** Delete any previous value and set the value stored in *pMem to val,
 1.57929 +** manifest type REAL.
 1.57930 +*/
 1.57931 +SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
 1.57932 +  if( sqlite3IsNaN(val) ){
 1.57933 +    sqlite3VdbeMemSetNull(pMem);
 1.57934 +  }else{
 1.57935 +    sqlite3VdbeMemRelease(pMem);
 1.57936 +    pMem->r = val;
 1.57937 +    pMem->flags = MEM_Real;
 1.57938 +    pMem->type = SQLITE_FLOAT;
 1.57939 +  }
 1.57940 +}
 1.57941 +#endif
 1.57942 +
 1.57943 +/*
 1.57944 +** Delete any previous value and set the value of pMem to be an
 1.57945 +** empty boolean index.
 1.57946 +*/
 1.57947 +SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
 1.57948 +  sqlite3 *db = pMem->db;
 1.57949 +  assert( db!=0 );
 1.57950 +  assert( (pMem->flags & MEM_RowSet)==0 );
 1.57951 +  sqlite3VdbeMemRelease(pMem);
 1.57952 +  pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
 1.57953 +  if( db->mallocFailed ){
 1.57954 +    pMem->flags = MEM_Null;
 1.57955 +  }else{
 1.57956 +    assert( pMem->zMalloc );
 1.57957 +    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
 1.57958 +                                       sqlite3DbMallocSize(db, pMem->zMalloc));
 1.57959 +    assert( pMem->u.pRowSet!=0 );
 1.57960 +    pMem->flags = MEM_RowSet;
 1.57961 +  }
 1.57962 +}
 1.57963 +
 1.57964 +/*
 1.57965 +** Return true if the Mem object contains a TEXT or BLOB that is
 1.57966 +** too large - whose size exceeds SQLITE_MAX_LENGTH.
 1.57967 +*/
 1.57968 +SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
 1.57969 +  assert( p->db!=0 );
 1.57970 +  if( p->flags & (MEM_Str|MEM_Blob) ){
 1.57971 +    int n = p->n;
 1.57972 +    if( p->flags & MEM_Zero ){
 1.57973 +      n += p->u.nZero;
 1.57974 +    }
 1.57975 +    return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
 1.57976 +  }
 1.57977 +  return 0; 
 1.57978 +}
 1.57979 +
 1.57980 +#ifdef SQLITE_DEBUG
 1.57981 +/*
 1.57982 +** This routine prepares a memory cell for modication by breaking
 1.57983 +** its link to a shallow copy and by marking any current shallow
 1.57984 +** copies of this cell as invalid.
 1.57985 +**
 1.57986 +** This is used for testing and debugging only - to make sure shallow
 1.57987 +** copies are not misused.
 1.57988 +*/
 1.57989 +SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
 1.57990 +  int i;
 1.57991 +  Mem *pX;
 1.57992 +  for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
 1.57993 +    if( pX->pScopyFrom==pMem ){
 1.57994 +      pX->flags |= MEM_Invalid;
 1.57995 +      pX->pScopyFrom = 0;
 1.57996 +    }
 1.57997 +  }
 1.57998 +  pMem->pScopyFrom = 0;
 1.57999 +}
 1.58000 +#endif /* SQLITE_DEBUG */
 1.58001 +
 1.58002 +/*
 1.58003 +** Size of struct Mem not including the Mem.zMalloc member.
 1.58004 +*/
 1.58005 +#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
 1.58006 +
 1.58007 +/*
 1.58008 +** Make an shallow copy of pFrom into pTo.  Prior contents of
 1.58009 +** pTo are freed.  The pFrom->z field is not duplicated.  If
 1.58010 +** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
 1.58011 +** and flags gets srcType (either MEM_Ephem or MEM_Static).
 1.58012 +*/
 1.58013 +SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
 1.58014 +  assert( (pFrom->flags & MEM_RowSet)==0 );
 1.58015 +  VdbeMemRelease(pTo);
 1.58016 +  memcpy(pTo, pFrom, MEMCELLSIZE);
 1.58017 +  pTo->xDel = 0;
 1.58018 +  if( (pFrom->flags&MEM_Static)==0 ){
 1.58019 +    pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
 1.58020 +    assert( srcType==MEM_Ephem || srcType==MEM_Static );
 1.58021 +    pTo->flags |= srcType;
 1.58022 +  }
 1.58023 +}
 1.58024 +
 1.58025 +/*
 1.58026 +** Make a full copy of pFrom into pTo.  Prior contents of pTo are
 1.58027 +** freed before the copy is made.
 1.58028 +*/
 1.58029 +SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
 1.58030 +  int rc = SQLITE_OK;
 1.58031 +
 1.58032 +  assert( (pFrom->flags & MEM_RowSet)==0 );
 1.58033 +  VdbeMemRelease(pTo);
 1.58034 +  memcpy(pTo, pFrom, MEMCELLSIZE);
 1.58035 +  pTo->flags &= ~MEM_Dyn;
 1.58036 +
 1.58037 +  if( pTo->flags&(MEM_Str|MEM_Blob) ){
 1.58038 +    if( 0==(pFrom->flags&MEM_Static) ){
 1.58039 +      pTo->flags |= MEM_Ephem;
 1.58040 +      rc = sqlite3VdbeMemMakeWriteable(pTo);
 1.58041 +    }
 1.58042 +  }
 1.58043 +
 1.58044 +  return rc;
 1.58045 +}
 1.58046 +
 1.58047 +/*
 1.58048 +** Transfer the contents of pFrom to pTo. Any existing value in pTo is
 1.58049 +** freed. If pFrom contains ephemeral data, a copy is made.
 1.58050 +**
 1.58051 +** pFrom contains an SQL NULL when this routine returns.
 1.58052 +*/
 1.58053 +SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
 1.58054 +  assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
 1.58055 +  assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
 1.58056 +  assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
 1.58057 +
 1.58058 +  sqlite3VdbeMemRelease(pTo);
 1.58059 +  memcpy(pTo, pFrom, sizeof(Mem));
 1.58060 +  pFrom->flags = MEM_Null;
 1.58061 +  pFrom->xDel = 0;
 1.58062 +  pFrom->zMalloc = 0;
 1.58063 +}
 1.58064 +
 1.58065 +/*
 1.58066 +** Change the value of a Mem to be a string or a BLOB.
 1.58067 +**
 1.58068 +** The memory management strategy depends on the value of the xDel
 1.58069 +** parameter. If the value passed is SQLITE_TRANSIENT, then the 
 1.58070 +** string is copied into a (possibly existing) buffer managed by the 
 1.58071 +** Mem structure. Otherwise, any existing buffer is freed and the
 1.58072 +** pointer copied.
 1.58073 +**
 1.58074 +** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
 1.58075 +** size limit) then no memory allocation occurs.  If the string can be
 1.58076 +** stored without allocating memory, then it is.  If a memory allocation
 1.58077 +** is required to store the string, then value of pMem is unchanged.  In
 1.58078 +** either case, SQLITE_TOOBIG is returned.
 1.58079 +*/
 1.58080 +SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
 1.58081 +  Mem *pMem,          /* Memory cell to set to string value */
 1.58082 +  const char *z,      /* String pointer */
 1.58083 +  int n,              /* Bytes in string, or negative */
 1.58084 +  u8 enc,             /* Encoding of z.  0 for BLOBs */
 1.58085 +  void (*xDel)(void*) /* Destructor function */
 1.58086 +){
 1.58087 +  int nByte = n;      /* New value for pMem->n */
 1.58088 +  int iLimit;         /* Maximum allowed string or blob size */
 1.58089 +  u16 flags = 0;      /* New value for pMem->flags */
 1.58090 +
 1.58091 +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 1.58092 +  assert( (pMem->flags & MEM_RowSet)==0 );
 1.58093 +
 1.58094 +  /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
 1.58095 +  if( !z ){
 1.58096 +    sqlite3VdbeMemSetNull(pMem);
 1.58097 +    return SQLITE_OK;
 1.58098 +  }
 1.58099 +
 1.58100 +  if( pMem->db ){
 1.58101 +    iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
 1.58102 +  }else{
 1.58103 +    iLimit = SQLITE_MAX_LENGTH;
 1.58104 +  }
 1.58105 +  flags = (enc==0?MEM_Blob:MEM_Str);
 1.58106 +  if( nByte<0 ){
 1.58107 +    assert( enc!=0 );
 1.58108 +    if( enc==SQLITE_UTF8 ){
 1.58109 +      for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
 1.58110 +    }else{
 1.58111 +      for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
 1.58112 +    }
 1.58113 +    flags |= MEM_Term;
 1.58114 +  }
 1.58115 +
 1.58116 +  /* The following block sets the new values of Mem.z and Mem.xDel. It
 1.58117 +  ** also sets a flag in local variable "flags" to indicate the memory
 1.58118 +  ** management (one of MEM_Dyn or MEM_Static).
 1.58119 +  */
 1.58120 +  if( xDel==SQLITE_TRANSIENT ){
 1.58121 +    int nAlloc = nByte;
 1.58122 +    if( flags&MEM_Term ){
 1.58123 +      nAlloc += (enc==SQLITE_UTF8?1:2);
 1.58124 +    }
 1.58125 +    if( nByte>iLimit ){
 1.58126 +      return SQLITE_TOOBIG;
 1.58127 +    }
 1.58128 +    if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
 1.58129 +      return SQLITE_NOMEM;
 1.58130 +    }
 1.58131 +    memcpy(pMem->z, z, nAlloc);
 1.58132 +  }else if( xDel==SQLITE_DYNAMIC ){
 1.58133 +    sqlite3VdbeMemRelease(pMem);
 1.58134 +    pMem->zMalloc = pMem->z = (char *)z;
 1.58135 +    pMem->xDel = 0;
 1.58136 +  }else{
 1.58137 +    sqlite3VdbeMemRelease(pMem);
 1.58138 +    pMem->z = (char *)z;
 1.58139 +    pMem->xDel = xDel;
 1.58140 +    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
 1.58141 +  }
 1.58142 +
 1.58143 +  pMem->n = nByte;
 1.58144 +  pMem->flags = flags;
 1.58145 +  pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
 1.58146 +  pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
 1.58147 +
 1.58148 +#ifndef SQLITE_OMIT_UTF16
 1.58149 +  if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
 1.58150 +    return SQLITE_NOMEM;
 1.58151 +  }
 1.58152 +#endif
 1.58153 +
 1.58154 +  if( nByte>iLimit ){
 1.58155 +    return SQLITE_TOOBIG;
 1.58156 +  }
 1.58157 +
 1.58158 +  return SQLITE_OK;
 1.58159 +}
 1.58160 +
 1.58161 +/*
 1.58162 +** Compare the values contained by the two memory cells, returning
 1.58163 +** negative, zero or positive if pMem1 is less than, equal to, or greater
 1.58164 +** than pMem2. Sorting order is NULL's first, followed by numbers (integers
 1.58165 +** and reals) sorted numerically, followed by text ordered by the collating
 1.58166 +** sequence pColl and finally blob's ordered by memcmp().
 1.58167 +**
 1.58168 +** Two NULL values are considered equal by this function.
 1.58169 +*/
 1.58170 +SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
 1.58171 +  int rc;
 1.58172 +  int f1, f2;
 1.58173 +  int combined_flags;
 1.58174 +
 1.58175 +  f1 = pMem1->flags;
 1.58176 +  f2 = pMem2->flags;
 1.58177 +  combined_flags = f1|f2;
 1.58178 +  assert( (combined_flags & MEM_RowSet)==0 );
 1.58179 + 
 1.58180 +  /* If one value is NULL, it is less than the other. If both values
 1.58181 +  ** are NULL, return 0.
 1.58182 +  */
 1.58183 +  if( combined_flags&MEM_Null ){
 1.58184 +    return (f2&MEM_Null) - (f1&MEM_Null);
 1.58185 +  }
 1.58186 +
 1.58187 +  /* If one value is a number and the other is not, the number is less.
 1.58188 +  ** If both are numbers, compare as reals if one is a real, or as integers
 1.58189 +  ** if both values are integers.
 1.58190 +  */
 1.58191 +  if( combined_flags&(MEM_Int|MEM_Real) ){
 1.58192 +    if( !(f1&(MEM_Int|MEM_Real)) ){
 1.58193 +      return 1;
 1.58194 +    }
 1.58195 +    if( !(f2&(MEM_Int|MEM_Real)) ){
 1.58196 +      return -1;
 1.58197 +    }
 1.58198 +    if( (f1 & f2 & MEM_Int)==0 ){
 1.58199 +      double r1, r2;
 1.58200 +      if( (f1&MEM_Real)==0 ){
 1.58201 +        r1 = (double)pMem1->u.i;
 1.58202 +      }else{
 1.58203 +        r1 = pMem1->r;
 1.58204 +      }
 1.58205 +      if( (f2&MEM_Real)==0 ){
 1.58206 +        r2 = (double)pMem2->u.i;
 1.58207 +      }else{
 1.58208 +        r2 = pMem2->r;
 1.58209 +      }
 1.58210 +      if( r1<r2 ) return -1;
 1.58211 +      if( r1>r2 ) return 1;
 1.58212 +      return 0;
 1.58213 +    }else{
 1.58214 +      assert( f1&MEM_Int );
 1.58215 +      assert( f2&MEM_Int );
 1.58216 +      if( pMem1->u.i < pMem2->u.i ) return -1;
 1.58217 +      if( pMem1->u.i > pMem2->u.i ) return 1;
 1.58218 +      return 0;
 1.58219 +    }
 1.58220 +  }
 1.58221 +
 1.58222 +  /* If one value is a string and the other is a blob, the string is less.
 1.58223 +  ** If both are strings, compare using the collating functions.
 1.58224 +  */
 1.58225 +  if( combined_flags&MEM_Str ){
 1.58226 +    if( (f1 & MEM_Str)==0 ){
 1.58227 +      return 1;
 1.58228 +    }
 1.58229 +    if( (f2 & MEM_Str)==0 ){
 1.58230 +      return -1;
 1.58231 +    }
 1.58232 +
 1.58233 +    assert( pMem1->enc==pMem2->enc );
 1.58234 +    assert( pMem1->enc==SQLITE_UTF8 || 
 1.58235 +            pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
 1.58236 +
 1.58237 +    /* The collation sequence must be defined at this point, even if
 1.58238 +    ** the user deletes the collation sequence after the vdbe program is
 1.58239 +    ** compiled (this was not always the case).
 1.58240 +    */
 1.58241 +    assert( !pColl || pColl->xCmp );
 1.58242 +
 1.58243 +    if( pColl ){
 1.58244 +      if( pMem1->enc==pColl->enc ){
 1.58245 +        /* The strings are already in the correct encoding.  Call the
 1.58246 +        ** comparison function directly */
 1.58247 +        return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
 1.58248 +      }else{
 1.58249 +        const void *v1, *v2;
 1.58250 +        int n1, n2;
 1.58251 +        Mem c1;
 1.58252 +        Mem c2;
 1.58253 +        memset(&c1, 0, sizeof(c1));
 1.58254 +        memset(&c2, 0, sizeof(c2));
 1.58255 +        sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
 1.58256 +        sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
 1.58257 +        v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
 1.58258 +        n1 = v1==0 ? 0 : c1.n;
 1.58259 +        v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
 1.58260 +        n2 = v2==0 ? 0 : c2.n;
 1.58261 +        rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
 1.58262 +        sqlite3VdbeMemRelease(&c1);
 1.58263 +        sqlite3VdbeMemRelease(&c2);
 1.58264 +        return rc;
 1.58265 +      }
 1.58266 +    }
 1.58267 +    /* If a NULL pointer was passed as the collate function, fall through
 1.58268 +    ** to the blob case and use memcmp().  */
 1.58269 +  }
 1.58270 + 
 1.58271 +  /* Both values must be blobs.  Compare using memcmp().  */
 1.58272 +  rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
 1.58273 +  if( rc==0 ){
 1.58274 +    rc = pMem1->n - pMem2->n;
 1.58275 +  }
 1.58276 +  return rc;
 1.58277 +}
 1.58278 +
 1.58279 +/*
 1.58280 +** Move data out of a btree key or data field and into a Mem structure.
 1.58281 +** The data or key is taken from the entry that pCur is currently pointing
 1.58282 +** to.  offset and amt determine what portion of the data or key to retrieve.
 1.58283 +** key is true to get the key or false to get data.  The result is written
 1.58284 +** into the pMem element.
 1.58285 +**
 1.58286 +** The pMem structure is assumed to be uninitialized.  Any prior content
 1.58287 +** is overwritten without being freed.
 1.58288 +**
 1.58289 +** If this routine fails for any reason (malloc returns NULL or unable
 1.58290 +** to read from the disk) then the pMem is left in an inconsistent state.
 1.58291 +*/
 1.58292 +SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
 1.58293 +  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
 1.58294 +  int offset,       /* Offset from the start of data to return bytes from. */
 1.58295 +  int amt,          /* Number of bytes to return. */
 1.58296 +  int key,          /* If true, retrieve from the btree key, not data. */
 1.58297 +  Mem *pMem         /* OUT: Return data in this Mem structure. */
 1.58298 +){
 1.58299 +  char *zData;        /* Data from the btree layer */
 1.58300 +  int available = 0;  /* Number of bytes available on the local btree page */
 1.58301 +  int rc = SQLITE_OK; /* Return code */
 1.58302 +
 1.58303 +  assert( sqlite3BtreeCursorIsValid(pCur) );
 1.58304 +
 1.58305 +  /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
 1.58306 +  ** that both the BtShared and database handle mutexes are held. */
 1.58307 +  assert( (pMem->flags & MEM_RowSet)==0 );
 1.58308 +  if( key ){
 1.58309 +    zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
 1.58310 +  }else{
 1.58311 +    zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
 1.58312 +  }
 1.58313 +  assert( zData!=0 );
 1.58314 +
 1.58315 +  if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
 1.58316 +    sqlite3VdbeMemRelease(pMem);
 1.58317 +    pMem->z = &zData[offset];
 1.58318 +    pMem->flags = MEM_Blob|MEM_Ephem;
 1.58319 +  }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
 1.58320 +    pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
 1.58321 +    pMem->enc = 0;
 1.58322 +    pMem->type = SQLITE_BLOB;
 1.58323 +    if( key ){
 1.58324 +      rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
 1.58325 +    }else{
 1.58326 +      rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
 1.58327 +    }
 1.58328 +    pMem->z[amt] = 0;
 1.58329 +    pMem->z[amt+1] = 0;
 1.58330 +    if( rc!=SQLITE_OK ){
 1.58331 +      sqlite3VdbeMemRelease(pMem);
 1.58332 +    }
 1.58333 +  }
 1.58334 +  pMem->n = amt;
 1.58335 +
 1.58336 +  return rc;
 1.58337 +}
 1.58338 +
 1.58339 +/* This function is only available internally, it is not part of the
 1.58340 +** external API. It works in a similar way to sqlite3_value_text(),
 1.58341 +** except the data returned is in the encoding specified by the second
 1.58342 +** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
 1.58343 +** SQLITE_UTF8.
 1.58344 +**
 1.58345 +** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
 1.58346 +** If that is the case, then the result must be aligned on an even byte
 1.58347 +** boundary.
 1.58348 +*/
 1.58349 +SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
 1.58350 +  if( !pVal ) return 0;
 1.58351 +
 1.58352 +  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 1.58353 +  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 1.58354 +  assert( (pVal->flags & MEM_RowSet)==0 );
 1.58355 +
 1.58356 +  if( pVal->flags&MEM_Null ){
 1.58357 +    return 0;
 1.58358 +  }
 1.58359 +  assert( (MEM_Blob>>3) == MEM_Str );
 1.58360 +  pVal->flags |= (pVal->flags & MEM_Blob)>>3;
 1.58361 +  ExpandBlob(pVal);
 1.58362 +  if( pVal->flags&MEM_Str ){
 1.58363 +    sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
 1.58364 +    if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
 1.58365 +      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
 1.58366 +      if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
 1.58367 +        return 0;
 1.58368 +      }
 1.58369 +    }
 1.58370 +    sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
 1.58371 +  }else{
 1.58372 +    assert( (pVal->flags&MEM_Blob)==0 );
 1.58373 +    sqlite3VdbeMemStringify(pVal, enc);
 1.58374 +    assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
 1.58375 +  }
 1.58376 +  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
 1.58377 +              || pVal->db->mallocFailed );
 1.58378 +  if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
 1.58379 +    return pVal->z;
 1.58380 +  }else{
 1.58381 +    return 0;
 1.58382 +  }
 1.58383 +}
 1.58384 +
 1.58385 +/*
 1.58386 +** Create a new sqlite3_value object.
 1.58387 +*/
 1.58388 +SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
 1.58389 +  Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
 1.58390 +  if( p ){
 1.58391 +    p->flags = MEM_Null;
 1.58392 +    p->type = SQLITE_NULL;
 1.58393 +    p->db = db;
 1.58394 +  }
 1.58395 +  return p;
 1.58396 +}
 1.58397 +
 1.58398 +/*
 1.58399 +** Create a new sqlite3_value object, containing the value of pExpr.
 1.58400 +**
 1.58401 +** This only works for very simple expressions that consist of one constant
 1.58402 +** token (i.e. "5", "5.1", "'a string'"). If the expression can
 1.58403 +** be converted directly into a value, then the value is allocated and
 1.58404 +** a pointer written to *ppVal. The caller is responsible for deallocating
 1.58405 +** the value by passing it to sqlite3ValueFree() later on. If the expression
 1.58406 +** cannot be converted to a value, then *ppVal is set to NULL.
 1.58407 +*/
 1.58408 +SQLITE_PRIVATE int sqlite3ValueFromExpr(
 1.58409 +  sqlite3 *db,              /* The database connection */
 1.58410 +  Expr *pExpr,              /* The expression to evaluate */
 1.58411 +  u8 enc,                   /* Encoding to use */
 1.58412 +  u8 affinity,              /* Affinity to use */
 1.58413 +  sqlite3_value **ppVal     /* Write the new value here */
 1.58414 +){
 1.58415 +  int op;
 1.58416 +  char *zVal = 0;
 1.58417 +  sqlite3_value *pVal = 0;
 1.58418 +  int negInt = 1;
 1.58419 +  const char *zNeg = "";
 1.58420 +
 1.58421 +  if( !pExpr ){
 1.58422 +    *ppVal = 0;
 1.58423 +    return SQLITE_OK;
 1.58424 +  }
 1.58425 +  op = pExpr->op;
 1.58426 +
 1.58427 +  /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
 1.58428 +  ** The ifdef here is to enable us to achieve 100% branch test coverage even
 1.58429 +  ** when SQLITE_ENABLE_STAT3 is omitted.
 1.58430 +  */
 1.58431 +#ifdef SQLITE_ENABLE_STAT3
 1.58432 +  if( op==TK_REGISTER ) op = pExpr->op2;
 1.58433 +#else
 1.58434 +  if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
 1.58435 +#endif
 1.58436 +
 1.58437 +  /* Handle negative integers in a single step.  This is needed in the
 1.58438 +  ** case when the value is -9223372036854775808.
 1.58439 +  */
 1.58440 +  if( op==TK_UMINUS
 1.58441 +   && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
 1.58442 +    pExpr = pExpr->pLeft;
 1.58443 +    op = pExpr->op;
 1.58444 +    negInt = -1;
 1.58445 +    zNeg = "-";
 1.58446 +  }
 1.58447 +
 1.58448 +  if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
 1.58449 +    pVal = sqlite3ValueNew(db);
 1.58450 +    if( pVal==0 ) goto no_mem;
 1.58451 +    if( ExprHasProperty(pExpr, EP_IntValue) ){
 1.58452 +      sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
 1.58453 +    }else{
 1.58454 +      zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
 1.58455 +      if( zVal==0 ) goto no_mem;
 1.58456 +      sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
 1.58457 +      if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
 1.58458 +    }
 1.58459 +    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
 1.58460 +      sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
 1.58461 +    }else{
 1.58462 +      sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
 1.58463 +    }
 1.58464 +    if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
 1.58465 +    if( enc!=SQLITE_UTF8 ){
 1.58466 +      sqlite3VdbeChangeEncoding(pVal, enc);
 1.58467 +    }
 1.58468 +  }else if( op==TK_UMINUS ) {
 1.58469 +    /* This branch happens for multiple negative signs.  Ex: -(-5) */
 1.58470 +    if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
 1.58471 +      sqlite3VdbeMemNumerify(pVal);
 1.58472 +      if( pVal->u.i==SMALLEST_INT64 ){
 1.58473 +        pVal->flags &= MEM_Int;
 1.58474 +        pVal->flags |= MEM_Real;
 1.58475 +        pVal->r = (double)LARGEST_INT64;
 1.58476 +      }else{
 1.58477 +        pVal->u.i = -pVal->u.i;
 1.58478 +      }
 1.58479 +      pVal->r = -pVal->r;
 1.58480 +      sqlite3ValueApplyAffinity(pVal, affinity, enc);
 1.58481 +    }
 1.58482 +  }else if( op==TK_NULL ){
 1.58483 +    pVal = sqlite3ValueNew(db);
 1.58484 +    if( pVal==0 ) goto no_mem;
 1.58485 +  }
 1.58486 +#ifndef SQLITE_OMIT_BLOB_LITERAL
 1.58487 +  else if( op==TK_BLOB ){
 1.58488 +    int nVal;
 1.58489 +    assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 1.58490 +    assert( pExpr->u.zToken[1]=='\'' );
 1.58491 +    pVal = sqlite3ValueNew(db);
 1.58492 +    if( !pVal ) goto no_mem;
 1.58493 +    zVal = &pExpr->u.zToken[2];
 1.58494 +    nVal = sqlite3Strlen30(zVal)-1;
 1.58495 +    assert( zVal[nVal]=='\'' );
 1.58496 +    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
 1.58497 +                         0, SQLITE_DYNAMIC);
 1.58498 +  }
 1.58499 +#endif
 1.58500 +
 1.58501 +  if( pVal ){
 1.58502 +    sqlite3VdbeMemStoreType(pVal);
 1.58503 +  }
 1.58504 +  *ppVal = pVal;
 1.58505 +  return SQLITE_OK;
 1.58506 +
 1.58507 +no_mem:
 1.58508 +  db->mallocFailed = 1;
 1.58509 +  sqlite3DbFree(db, zVal);
 1.58510 +  sqlite3ValueFree(pVal);
 1.58511 +  *ppVal = 0;
 1.58512 +  return SQLITE_NOMEM;
 1.58513 +}
 1.58514 +
 1.58515 +/*
 1.58516 +** Change the string value of an sqlite3_value object
 1.58517 +*/
 1.58518 +SQLITE_PRIVATE void sqlite3ValueSetStr(
 1.58519 +  sqlite3_value *v,     /* Value to be set */
 1.58520 +  int n,                /* Length of string z */
 1.58521 +  const void *z,        /* Text of the new string */
 1.58522 +  u8 enc,               /* Encoding to use */
 1.58523 +  void (*xDel)(void*)   /* Destructor for the string */
 1.58524 +){
 1.58525 +  if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
 1.58526 +}
 1.58527 +
 1.58528 +/*
 1.58529 +** Free an sqlite3_value object
 1.58530 +*/
 1.58531 +SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
 1.58532 +  if( !v ) return;
 1.58533 +  sqlite3VdbeMemRelease((Mem *)v);
 1.58534 +  sqlite3DbFree(((Mem*)v)->db, v);
 1.58535 +}
 1.58536 +
 1.58537 +/*
 1.58538 +** Return the number of bytes in the sqlite3_value object assuming
 1.58539 +** that it uses the encoding "enc"
 1.58540 +*/
 1.58541 +SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
 1.58542 +  Mem *p = (Mem*)pVal;
 1.58543 +  if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
 1.58544 +    if( p->flags & MEM_Zero ){
 1.58545 +      return p->n + p->u.nZero;
 1.58546 +    }else{
 1.58547 +      return p->n;
 1.58548 +    }
 1.58549 +  }
 1.58550 +  return 0;
 1.58551 +}
 1.58552 +
 1.58553 +/************** End of vdbemem.c *********************************************/
 1.58554 +/************** Begin file vdbeaux.c *****************************************/
 1.58555 +/*
 1.58556 +** 2003 September 6
 1.58557 +**
 1.58558 +** The author disclaims copyright to this source code.  In place of
 1.58559 +** a legal notice, here is a blessing:
 1.58560 +**
 1.58561 +**    May you do good and not evil.
 1.58562 +**    May you find forgiveness for yourself and forgive others.
 1.58563 +**    May you share freely, never taking more than you give.
 1.58564 +**
 1.58565 +*************************************************************************
 1.58566 +** This file contains code used for creating, destroying, and populating
 1.58567 +** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
 1.58568 +** to version 2.8.7, all this code was combined into the vdbe.c source file.
 1.58569 +** But that file was getting too big so this subroutines were split out.
 1.58570 +*/
 1.58571 +
 1.58572 +
 1.58573 +
 1.58574 +/*
 1.58575 +** When debugging the code generator in a symbolic debugger, one can
 1.58576 +** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
 1.58577 +** as they are added to the instruction stream.
 1.58578 +*/
 1.58579 +#ifdef SQLITE_DEBUG
 1.58580 +SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
 1.58581 +#endif
 1.58582 +
 1.58583 +
 1.58584 +/*
 1.58585 +** Create a new virtual database engine.
 1.58586 +*/
 1.58587 +SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
 1.58588 +  Vdbe *p;
 1.58589 +  p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
 1.58590 +  if( p==0 ) return 0;
 1.58591 +  p->db = db;
 1.58592 +  if( db->pVdbe ){
 1.58593 +    db->pVdbe->pPrev = p;
 1.58594 +  }
 1.58595 +  p->pNext = db->pVdbe;
 1.58596 +  p->pPrev = 0;
 1.58597 +  db->pVdbe = p;
 1.58598 +  p->magic = VDBE_MAGIC_INIT;
 1.58599 +  return p;
 1.58600 +}
 1.58601 +
 1.58602 +/*
 1.58603 +** Remember the SQL string for a prepared statement.
 1.58604 +*/
 1.58605 +SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
 1.58606 +  assert( isPrepareV2==1 || isPrepareV2==0 );
 1.58607 +  if( p==0 ) return;
 1.58608 +#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
 1.58609 +  if( !isPrepareV2 ) return;
 1.58610 +#endif
 1.58611 +  assert( p->zSql==0 );
 1.58612 +  p->zSql = sqlite3DbStrNDup(p->db, z, n);
 1.58613 +  p->isPrepareV2 = (u8)isPrepareV2;
 1.58614 +}
 1.58615 +
 1.58616 +/*
 1.58617 +** Return the SQL associated with a prepared statement
 1.58618 +*/
 1.58619 +SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
 1.58620 +  Vdbe *p = (Vdbe *)pStmt;
 1.58621 +  return (p && p->isPrepareV2) ? p->zSql : 0;
 1.58622 +}
 1.58623 +
 1.58624 +/*
 1.58625 +** Swap all content between two VDBE structures.
 1.58626 +*/
 1.58627 +SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
 1.58628 +  Vdbe tmp, *pTmp;
 1.58629 +  char *zTmp;
 1.58630 +  tmp = *pA;
 1.58631 +  *pA = *pB;
 1.58632 +  *pB = tmp;
 1.58633 +  pTmp = pA->pNext;
 1.58634 +  pA->pNext = pB->pNext;
 1.58635 +  pB->pNext = pTmp;
 1.58636 +  pTmp = pA->pPrev;
 1.58637 +  pA->pPrev = pB->pPrev;
 1.58638 +  pB->pPrev = pTmp;
 1.58639 +  zTmp = pA->zSql;
 1.58640 +  pA->zSql = pB->zSql;
 1.58641 +  pB->zSql = zTmp;
 1.58642 +  pB->isPrepareV2 = pA->isPrepareV2;
 1.58643 +}
 1.58644 +
 1.58645 +#ifdef SQLITE_DEBUG
 1.58646 +/*
 1.58647 +** Turn tracing on or off
 1.58648 +*/
 1.58649 +SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
 1.58650 +  p->trace = trace;
 1.58651 +}
 1.58652 +#endif
 1.58653 +
 1.58654 +/*
 1.58655 +** Resize the Vdbe.aOp array so that it is at least one op larger than 
 1.58656 +** it was.
 1.58657 +**
 1.58658 +** If an out-of-memory error occurs while resizing the array, return
 1.58659 +** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
 1.58660 +** unchanged (this is so that any opcodes already allocated can be 
 1.58661 +** correctly deallocated along with the rest of the Vdbe).
 1.58662 +*/
 1.58663 +static int growOpArray(Vdbe *p){
 1.58664 +  VdbeOp *pNew;
 1.58665 +  int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
 1.58666 +  pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
 1.58667 +  if( pNew ){
 1.58668 +    p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
 1.58669 +    p->aOp = pNew;
 1.58670 +  }
 1.58671 +  return (pNew ? SQLITE_OK : SQLITE_NOMEM);
 1.58672 +}
 1.58673 +
 1.58674 +/*
 1.58675 +** Add a new instruction to the list of instructions current in the
 1.58676 +** VDBE.  Return the address of the new instruction.
 1.58677 +**
 1.58678 +** Parameters:
 1.58679 +**
 1.58680 +**    p               Pointer to the VDBE
 1.58681 +**
 1.58682 +**    op              The opcode for this instruction
 1.58683 +**
 1.58684 +**    p1, p2, p3      Operands
 1.58685 +**
 1.58686 +** Use the sqlite3VdbeResolveLabel() function to fix an address and
 1.58687 +** the sqlite3VdbeChangeP4() function to change the value of the P4
 1.58688 +** operand.
 1.58689 +*/
 1.58690 +SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
 1.58691 +  int i;
 1.58692 +  VdbeOp *pOp;
 1.58693 +
 1.58694 +  i = p->nOp;
 1.58695 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.58696 +  assert( op>0 && op<0xff );
 1.58697 +  if( p->nOpAlloc<=i ){
 1.58698 +    if( growOpArray(p) ){
 1.58699 +      return 1;
 1.58700 +    }
 1.58701 +  }
 1.58702 +  p->nOp++;
 1.58703 +  pOp = &p->aOp[i];
 1.58704 +  pOp->opcode = (u8)op;
 1.58705 +  pOp->p5 = 0;
 1.58706 +  pOp->p1 = p1;
 1.58707 +  pOp->p2 = p2;
 1.58708 +  pOp->p3 = p3;
 1.58709 +  pOp->p4.p = 0;
 1.58710 +  pOp->p4type = P4_NOTUSED;
 1.58711 +#ifdef SQLITE_DEBUG
 1.58712 +  pOp->zComment = 0;
 1.58713 +  if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
 1.58714 +#endif
 1.58715 +#ifdef VDBE_PROFILE
 1.58716 +  pOp->cycles = 0;
 1.58717 +  pOp->cnt = 0;
 1.58718 +#endif
 1.58719 +  return i;
 1.58720 +}
 1.58721 +SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
 1.58722 +  return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
 1.58723 +}
 1.58724 +SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
 1.58725 +  return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
 1.58726 +}
 1.58727 +SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
 1.58728 +  return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
 1.58729 +}
 1.58730 +
 1.58731 +
 1.58732 +/*
 1.58733 +** Add an opcode that includes the p4 value as a pointer.
 1.58734 +*/
 1.58735 +SQLITE_PRIVATE int sqlite3VdbeAddOp4(
 1.58736 +  Vdbe *p,            /* Add the opcode to this VM */
 1.58737 +  int op,             /* The new opcode */
 1.58738 +  int p1,             /* The P1 operand */
 1.58739 +  int p2,             /* The P2 operand */
 1.58740 +  int p3,             /* The P3 operand */
 1.58741 +  const char *zP4,    /* The P4 operand */
 1.58742 +  int p4type          /* P4 operand type */
 1.58743 +){
 1.58744 +  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
 1.58745 +  sqlite3VdbeChangeP4(p, addr, zP4, p4type);
 1.58746 +  return addr;
 1.58747 +}
 1.58748 +
 1.58749 +/*
 1.58750 +** Add an OP_ParseSchema opcode.  This routine is broken out from
 1.58751 +** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
 1.58752 +** as having been used.
 1.58753 +**
 1.58754 +** The zWhere string must have been obtained from sqlite3_malloc().
 1.58755 +** This routine will take ownership of the allocated memory.
 1.58756 +*/
 1.58757 +SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
 1.58758 +  int j;
 1.58759 +  int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
 1.58760 +  sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
 1.58761 +  for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
 1.58762 +}
 1.58763 +
 1.58764 +/*
 1.58765 +** Add an opcode that includes the p4 value as an integer.
 1.58766 +*/
 1.58767 +SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
 1.58768 +  Vdbe *p,            /* Add the opcode to this VM */
 1.58769 +  int op,             /* The new opcode */
 1.58770 +  int p1,             /* The P1 operand */
 1.58771 +  int p2,             /* The P2 operand */
 1.58772 +  int p3,             /* The P3 operand */
 1.58773 +  int p4              /* The P4 operand as an integer */
 1.58774 +){
 1.58775 +  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
 1.58776 +  sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
 1.58777 +  return addr;
 1.58778 +}
 1.58779 +
 1.58780 +/*
 1.58781 +** Create a new symbolic label for an instruction that has yet to be
 1.58782 +** coded.  The symbolic label is really just a negative number.  The
 1.58783 +** label can be used as the P2 value of an operation.  Later, when
 1.58784 +** the label is resolved to a specific address, the VDBE will scan
 1.58785 +** through its operation list and change all values of P2 which match
 1.58786 +** the label into the resolved address.
 1.58787 +**
 1.58788 +** The VDBE knows that a P2 value is a label because labels are
 1.58789 +** always negative and P2 values are suppose to be non-negative.
 1.58790 +** Hence, a negative P2 value is a label that has yet to be resolved.
 1.58791 +**
 1.58792 +** Zero is returned if a malloc() fails.
 1.58793 +*/
 1.58794 +SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
 1.58795 +  int i = p->nLabel++;
 1.58796 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.58797 +  if( (i & (i-1))==0 ){
 1.58798 +    p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 
 1.58799 +                                       (i*2+1)*sizeof(p->aLabel[0]));
 1.58800 +  }
 1.58801 +  if( p->aLabel ){
 1.58802 +    p->aLabel[i] = -1;
 1.58803 +  }
 1.58804 +  return -1-i;
 1.58805 +}
 1.58806 +
 1.58807 +/*
 1.58808 +** Resolve label "x" to be the address of the next instruction to
 1.58809 +** be inserted.  The parameter "x" must have been obtained from
 1.58810 +** a prior call to sqlite3VdbeMakeLabel().
 1.58811 +*/
 1.58812 +SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
 1.58813 +  int j = -1-x;
 1.58814 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.58815 +  assert( j>=0 && j<p->nLabel );
 1.58816 +  if( p->aLabel ){
 1.58817 +    p->aLabel[j] = p->nOp;
 1.58818 +  }
 1.58819 +}
 1.58820 +
 1.58821 +/*
 1.58822 +** Mark the VDBE as one that can only be run one time.
 1.58823 +*/
 1.58824 +SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
 1.58825 +  p->runOnlyOnce = 1;
 1.58826 +}
 1.58827 +
 1.58828 +#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
 1.58829 +
 1.58830 +/*
 1.58831 +** The following type and function are used to iterate through all opcodes
 1.58832 +** in a Vdbe main program and each of the sub-programs (triggers) it may 
 1.58833 +** invoke directly or indirectly. It should be used as follows:
 1.58834 +**
 1.58835 +**   Op *pOp;
 1.58836 +**   VdbeOpIter sIter;
 1.58837 +**
 1.58838 +**   memset(&sIter, 0, sizeof(sIter));
 1.58839 +**   sIter.v = v;                            // v is of type Vdbe* 
 1.58840 +**   while( (pOp = opIterNext(&sIter)) ){
 1.58841 +**     // Do something with pOp
 1.58842 +**   }
 1.58843 +**   sqlite3DbFree(v->db, sIter.apSub);
 1.58844 +** 
 1.58845 +*/
 1.58846 +typedef struct VdbeOpIter VdbeOpIter;
 1.58847 +struct VdbeOpIter {
 1.58848 +  Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
 1.58849 +  SubProgram **apSub;        /* Array of subprograms */
 1.58850 +  int nSub;                  /* Number of entries in apSub */
 1.58851 +  int iAddr;                 /* Address of next instruction to return */
 1.58852 +  int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
 1.58853 +};
 1.58854 +static Op *opIterNext(VdbeOpIter *p){
 1.58855 +  Vdbe *v = p->v;
 1.58856 +  Op *pRet = 0;
 1.58857 +  Op *aOp;
 1.58858 +  int nOp;
 1.58859 +
 1.58860 +  if( p->iSub<=p->nSub ){
 1.58861 +
 1.58862 +    if( p->iSub==0 ){
 1.58863 +      aOp = v->aOp;
 1.58864 +      nOp = v->nOp;
 1.58865 +    }else{
 1.58866 +      aOp = p->apSub[p->iSub-1]->aOp;
 1.58867 +      nOp = p->apSub[p->iSub-1]->nOp;
 1.58868 +    }
 1.58869 +    assert( p->iAddr<nOp );
 1.58870 +
 1.58871 +    pRet = &aOp[p->iAddr];
 1.58872 +    p->iAddr++;
 1.58873 +    if( p->iAddr==nOp ){
 1.58874 +      p->iSub++;
 1.58875 +      p->iAddr = 0;
 1.58876 +    }
 1.58877 +  
 1.58878 +    if( pRet->p4type==P4_SUBPROGRAM ){
 1.58879 +      int nByte = (p->nSub+1)*sizeof(SubProgram*);
 1.58880 +      int j;
 1.58881 +      for(j=0; j<p->nSub; j++){
 1.58882 +        if( p->apSub[j]==pRet->p4.pProgram ) break;
 1.58883 +      }
 1.58884 +      if( j==p->nSub ){
 1.58885 +        p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
 1.58886 +        if( !p->apSub ){
 1.58887 +          pRet = 0;
 1.58888 +        }else{
 1.58889 +          p->apSub[p->nSub++] = pRet->p4.pProgram;
 1.58890 +        }
 1.58891 +      }
 1.58892 +    }
 1.58893 +  }
 1.58894 +
 1.58895 +  return pRet;
 1.58896 +}
 1.58897 +
 1.58898 +/*
 1.58899 +** Check if the program stored in the VM associated with pParse may
 1.58900 +** throw an ABORT exception (causing the statement, but not entire transaction
 1.58901 +** to be rolled back). This condition is true if the main program or any
 1.58902 +** sub-programs contains any of the following:
 1.58903 +**
 1.58904 +**   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 1.58905 +**   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 1.58906 +**   *  OP_Destroy
 1.58907 +**   *  OP_VUpdate
 1.58908 +**   *  OP_VRename
 1.58909 +**   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
 1.58910 +**
 1.58911 +** Then check that the value of Parse.mayAbort is true if an
 1.58912 +** ABORT may be thrown, or false otherwise. Return true if it does
 1.58913 +** match, or false otherwise. This function is intended to be used as
 1.58914 +** part of an assert statement in the compiler. Similar to:
 1.58915 +**
 1.58916 +**   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
 1.58917 +*/
 1.58918 +SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
 1.58919 +  int hasAbort = 0;
 1.58920 +  Op *pOp;
 1.58921 +  VdbeOpIter sIter;
 1.58922 +  memset(&sIter, 0, sizeof(sIter));
 1.58923 +  sIter.v = v;
 1.58924 +
 1.58925 +  while( (pOp = opIterNext(&sIter))!=0 ){
 1.58926 +    int opcode = pOp->opcode;
 1.58927 +    if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 1.58928 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.58929 +     || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) 
 1.58930 +#endif
 1.58931 +     || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 1.58932 +      && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
 1.58933 +    ){
 1.58934 +      hasAbort = 1;
 1.58935 +      break;
 1.58936 +    }
 1.58937 +  }
 1.58938 +  sqlite3DbFree(v->db, sIter.apSub);
 1.58939 +
 1.58940 +  /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
 1.58941 +  ** If malloc failed, then the while() loop above may not have iterated
 1.58942 +  ** through all opcodes and hasAbort may be set incorrectly. Return
 1.58943 +  ** true for this case to prevent the assert() in the callers frame
 1.58944 +  ** from failing.  */
 1.58945 +  return ( v->db->mallocFailed || hasAbort==mayAbort );
 1.58946 +}
 1.58947 +#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
 1.58948 +
 1.58949 +/*
 1.58950 +** Loop through the program looking for P2 values that are negative
 1.58951 +** on jump instructions.  Each such value is a label.  Resolve the
 1.58952 +** label by setting the P2 value to its correct non-zero value.
 1.58953 +**
 1.58954 +** This routine is called once after all opcodes have been inserted.
 1.58955 +**
 1.58956 +** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
 1.58957 +** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
 1.58958 +** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
 1.58959 +**
 1.58960 +** The Op.opflags field is set on all opcodes.
 1.58961 +*/
 1.58962 +static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
 1.58963 +  int i;
 1.58964 +  int nMaxArgs = *pMaxFuncArgs;
 1.58965 +  Op *pOp;
 1.58966 +  int *aLabel = p->aLabel;
 1.58967 +  p->readOnly = 1;
 1.58968 +  for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
 1.58969 +    u8 opcode = pOp->opcode;
 1.58970 +
 1.58971 +    pOp->opflags = sqlite3OpcodeProperty[opcode];
 1.58972 +    if( opcode==OP_Function || opcode==OP_AggStep ){
 1.58973 +      if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
 1.58974 +    }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
 1.58975 +      p->readOnly = 0;
 1.58976 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.58977 +    }else if( opcode==OP_VUpdate ){
 1.58978 +      if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
 1.58979 +    }else if( opcode==OP_VFilter ){
 1.58980 +      int n;
 1.58981 +      assert( p->nOp - i >= 3 );
 1.58982 +      assert( pOp[-1].opcode==OP_Integer );
 1.58983 +      n = pOp[-1].p1;
 1.58984 +      if( n>nMaxArgs ) nMaxArgs = n;
 1.58985 +#endif
 1.58986 +    }else if( opcode==OP_Next || opcode==OP_SorterNext ){
 1.58987 +      pOp->p4.xAdvance = sqlite3BtreeNext;
 1.58988 +      pOp->p4type = P4_ADVANCE;
 1.58989 +    }else if( opcode==OP_Prev ){
 1.58990 +      pOp->p4.xAdvance = sqlite3BtreePrevious;
 1.58991 +      pOp->p4type = P4_ADVANCE;
 1.58992 +    }
 1.58993 +
 1.58994 +    if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
 1.58995 +      assert( -1-pOp->p2<p->nLabel );
 1.58996 +      pOp->p2 = aLabel[-1-pOp->p2];
 1.58997 +    }
 1.58998 +  }
 1.58999 +  sqlite3DbFree(p->db, p->aLabel);
 1.59000 +  p->aLabel = 0;
 1.59001 +
 1.59002 +  *pMaxFuncArgs = nMaxArgs;
 1.59003 +}
 1.59004 +
 1.59005 +/*
 1.59006 +** Return the address of the next instruction to be inserted.
 1.59007 +*/
 1.59008 +SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
 1.59009 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.59010 +  return p->nOp;
 1.59011 +}
 1.59012 +
 1.59013 +/*
 1.59014 +** This function returns a pointer to the array of opcodes associated with
 1.59015 +** the Vdbe passed as the first argument. It is the callers responsibility
 1.59016 +** to arrange for the returned array to be eventually freed using the 
 1.59017 +** vdbeFreeOpArray() function.
 1.59018 +**
 1.59019 +** Before returning, *pnOp is set to the number of entries in the returned
 1.59020 +** array. Also, *pnMaxArg is set to the larger of its current value and 
 1.59021 +** the number of entries in the Vdbe.apArg[] array required to execute the 
 1.59022 +** returned program.
 1.59023 +*/
 1.59024 +SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
 1.59025 +  VdbeOp *aOp = p->aOp;
 1.59026 +  assert( aOp && !p->db->mallocFailed );
 1.59027 +
 1.59028 +  /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
 1.59029 +  assert( p->btreeMask==0 );
 1.59030 +
 1.59031 +  resolveP2Values(p, pnMaxArg);
 1.59032 +  *pnOp = p->nOp;
 1.59033 +  p->aOp = 0;
 1.59034 +  return aOp;
 1.59035 +}
 1.59036 +
 1.59037 +/*
 1.59038 +** Add a whole list of operations to the operation stack.  Return the
 1.59039 +** address of the first operation added.
 1.59040 +*/
 1.59041 +SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
 1.59042 +  int addr;
 1.59043 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.59044 +  if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
 1.59045 +    return 0;
 1.59046 +  }
 1.59047 +  addr = p->nOp;
 1.59048 +  if( ALWAYS(nOp>0) ){
 1.59049 +    int i;
 1.59050 +    VdbeOpList const *pIn = aOp;
 1.59051 +    for(i=0; i<nOp; i++, pIn++){
 1.59052 +      int p2 = pIn->p2;
 1.59053 +      VdbeOp *pOut = &p->aOp[i+addr];
 1.59054 +      pOut->opcode = pIn->opcode;
 1.59055 +      pOut->p1 = pIn->p1;
 1.59056 +      if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
 1.59057 +        pOut->p2 = addr + ADDR(p2);
 1.59058 +      }else{
 1.59059 +        pOut->p2 = p2;
 1.59060 +      }
 1.59061 +      pOut->p3 = pIn->p3;
 1.59062 +      pOut->p4type = P4_NOTUSED;
 1.59063 +      pOut->p4.p = 0;
 1.59064 +      pOut->p5 = 0;
 1.59065 +#ifdef SQLITE_DEBUG
 1.59066 +      pOut->zComment = 0;
 1.59067 +      if( sqlite3VdbeAddopTrace ){
 1.59068 +        sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
 1.59069 +      }
 1.59070 +#endif
 1.59071 +    }
 1.59072 +    p->nOp += nOp;
 1.59073 +  }
 1.59074 +  return addr;
 1.59075 +}
 1.59076 +
 1.59077 +/*
 1.59078 +** Change the value of the P1 operand for a specific instruction.
 1.59079 +** This routine is useful when a large program is loaded from a
 1.59080 +** static array using sqlite3VdbeAddOpList but we want to make a
 1.59081 +** few minor changes to the program.
 1.59082 +*/
 1.59083 +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
 1.59084 +  assert( p!=0 );
 1.59085 +  if( ((u32)p->nOp)>addr ){
 1.59086 +    p->aOp[addr].p1 = val;
 1.59087 +  }
 1.59088 +}
 1.59089 +
 1.59090 +/*
 1.59091 +** Change the value of the P2 operand for a specific instruction.
 1.59092 +** This routine is useful for setting a jump destination.
 1.59093 +*/
 1.59094 +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
 1.59095 +  assert( p!=0 );
 1.59096 +  if( ((u32)p->nOp)>addr ){
 1.59097 +    p->aOp[addr].p2 = val;
 1.59098 +  }
 1.59099 +}
 1.59100 +
 1.59101 +/*
 1.59102 +** Change the value of the P3 operand for a specific instruction.
 1.59103 +*/
 1.59104 +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
 1.59105 +  assert( p!=0 );
 1.59106 +  if( ((u32)p->nOp)>addr ){
 1.59107 +    p->aOp[addr].p3 = val;
 1.59108 +  }
 1.59109 +}
 1.59110 +
 1.59111 +/*
 1.59112 +** Change the value of the P5 operand for the most recently
 1.59113 +** added operation.
 1.59114 +*/
 1.59115 +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
 1.59116 +  assert( p!=0 );
 1.59117 +  if( p->aOp ){
 1.59118 +    assert( p->nOp>0 );
 1.59119 +    p->aOp[p->nOp-1].p5 = val;
 1.59120 +  }
 1.59121 +}
 1.59122 +
 1.59123 +/*
 1.59124 +** Change the P2 operand of instruction addr so that it points to
 1.59125 +** the address of the next instruction to be coded.
 1.59126 +*/
 1.59127 +SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
 1.59128 +  assert( addr>=0 || p->db->mallocFailed );
 1.59129 +  if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
 1.59130 +}
 1.59131 +
 1.59132 +
 1.59133 +/*
 1.59134 +** If the input FuncDef structure is ephemeral, then free it.  If
 1.59135 +** the FuncDef is not ephermal, then do nothing.
 1.59136 +*/
 1.59137 +static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
 1.59138 +  if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
 1.59139 +    sqlite3DbFree(db, pDef);
 1.59140 +  }
 1.59141 +}
 1.59142 +
 1.59143 +static void vdbeFreeOpArray(sqlite3 *, Op *, int);
 1.59144 +
 1.59145 +/*
 1.59146 +** Delete a P4 value if necessary.
 1.59147 +*/
 1.59148 +static void freeP4(sqlite3 *db, int p4type, void *p4){
 1.59149 +  if( p4 ){
 1.59150 +    assert( db );
 1.59151 +    switch( p4type ){
 1.59152 +      case P4_REAL:
 1.59153 +      case P4_INT64:
 1.59154 +      case P4_DYNAMIC:
 1.59155 +      case P4_KEYINFO:
 1.59156 +      case P4_INTARRAY:
 1.59157 +      case P4_KEYINFO_HANDOFF: {
 1.59158 +        sqlite3DbFree(db, p4);
 1.59159 +        break;
 1.59160 +      }
 1.59161 +      case P4_MPRINTF: {
 1.59162 +        if( db->pnBytesFreed==0 ) sqlite3_free(p4);
 1.59163 +        break;
 1.59164 +      }
 1.59165 +      case P4_VDBEFUNC: {
 1.59166 +        VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
 1.59167 +        freeEphemeralFunction(db, pVdbeFunc->pFunc);
 1.59168 +        if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
 1.59169 +        sqlite3DbFree(db, pVdbeFunc);
 1.59170 +        break;
 1.59171 +      }
 1.59172 +      case P4_FUNCDEF: {
 1.59173 +        freeEphemeralFunction(db, (FuncDef*)p4);
 1.59174 +        break;
 1.59175 +      }
 1.59176 +      case P4_MEM: {
 1.59177 +        if( db->pnBytesFreed==0 ){
 1.59178 +          sqlite3ValueFree((sqlite3_value*)p4);
 1.59179 +        }else{
 1.59180 +          Mem *p = (Mem*)p4;
 1.59181 +          sqlite3DbFree(db, p->zMalloc);
 1.59182 +          sqlite3DbFree(db, p);
 1.59183 +        }
 1.59184 +        break;
 1.59185 +      }
 1.59186 +      case P4_VTAB : {
 1.59187 +        if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
 1.59188 +        break;
 1.59189 +      }
 1.59190 +    }
 1.59191 +  }
 1.59192 +}
 1.59193 +
 1.59194 +/*
 1.59195 +** Free the space allocated for aOp and any p4 values allocated for the
 1.59196 +** opcodes contained within. If aOp is not NULL it is assumed to contain 
 1.59197 +** nOp entries. 
 1.59198 +*/
 1.59199 +static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
 1.59200 +  if( aOp ){
 1.59201 +    Op *pOp;
 1.59202 +    for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
 1.59203 +      freeP4(db, pOp->p4type, pOp->p4.p);
 1.59204 +#ifdef SQLITE_DEBUG
 1.59205 +      sqlite3DbFree(db, pOp->zComment);
 1.59206 +#endif     
 1.59207 +    }
 1.59208 +  }
 1.59209 +  sqlite3DbFree(db, aOp);
 1.59210 +}
 1.59211 +
 1.59212 +/*
 1.59213 +** Link the SubProgram object passed as the second argument into the linked
 1.59214 +** list at Vdbe.pSubProgram. This list is used to delete all sub-program
 1.59215 +** objects when the VM is no longer required.
 1.59216 +*/
 1.59217 +SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
 1.59218 +  p->pNext = pVdbe->pProgram;
 1.59219 +  pVdbe->pProgram = p;
 1.59220 +}
 1.59221 +
 1.59222 +/*
 1.59223 +** Change the opcode at addr into OP_Noop
 1.59224 +*/
 1.59225 +SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
 1.59226 +  if( p->aOp ){
 1.59227 +    VdbeOp *pOp = &p->aOp[addr];
 1.59228 +    sqlite3 *db = p->db;
 1.59229 +    freeP4(db, pOp->p4type, pOp->p4.p);
 1.59230 +    memset(pOp, 0, sizeof(pOp[0]));
 1.59231 +    pOp->opcode = OP_Noop;
 1.59232 +  }
 1.59233 +}
 1.59234 +
 1.59235 +/*
 1.59236 +** Change the value of the P4 operand for a specific instruction.
 1.59237 +** This routine is useful when a large program is loaded from a
 1.59238 +** static array using sqlite3VdbeAddOpList but we want to make a
 1.59239 +** few minor changes to the program.
 1.59240 +**
 1.59241 +** If n>=0 then the P4 operand is dynamic, meaning that a copy of
 1.59242 +** the string is made into memory obtained from sqlite3_malloc().
 1.59243 +** A value of n==0 means copy bytes of zP4 up to and including the
 1.59244 +** first null byte.  If n>0 then copy n+1 bytes of zP4.
 1.59245 +**
 1.59246 +** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
 1.59247 +** A copy is made of the KeyInfo structure into memory obtained from
 1.59248 +** sqlite3_malloc, to be freed when the Vdbe is finalized.
 1.59249 +** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
 1.59250 +** stored in memory that the caller has obtained from sqlite3_malloc. The 
 1.59251 +** caller should not free the allocation, it will be freed when the Vdbe is
 1.59252 +** finalized.
 1.59253 +** 
 1.59254 +** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
 1.59255 +** to a string or structure that is guaranteed to exist for the lifetime of
 1.59256 +** the Vdbe. In these cases we can just copy the pointer.
 1.59257 +**
 1.59258 +** If addr<0 then change P4 on the most recently inserted instruction.
 1.59259 +*/
 1.59260 +SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
 1.59261 +  Op *pOp;
 1.59262 +  sqlite3 *db;
 1.59263 +  assert( p!=0 );
 1.59264 +  db = p->db;
 1.59265 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.59266 +  if( p->aOp==0 || db->mallocFailed ){
 1.59267 +    if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
 1.59268 +      freeP4(db, n, (void*)*(char**)&zP4);
 1.59269 +    }
 1.59270 +    return;
 1.59271 +  }
 1.59272 +  assert( p->nOp>0 );
 1.59273 +  assert( addr<p->nOp );
 1.59274 +  if( addr<0 ){
 1.59275 +    addr = p->nOp - 1;
 1.59276 +  }
 1.59277 +  pOp = &p->aOp[addr];
 1.59278 +  assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
 1.59279 +  freeP4(db, pOp->p4type, pOp->p4.p);
 1.59280 +  pOp->p4.p = 0;
 1.59281 +  if( n==P4_INT32 ){
 1.59282 +    /* Note: this cast is safe, because the origin data point was an int
 1.59283 +    ** that was cast to a (const char *). */
 1.59284 +    pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
 1.59285 +    pOp->p4type = P4_INT32;
 1.59286 +  }else if( zP4==0 ){
 1.59287 +    pOp->p4.p = 0;
 1.59288 +    pOp->p4type = P4_NOTUSED;
 1.59289 +  }else if( n==P4_KEYINFO ){
 1.59290 +    KeyInfo *pKeyInfo;
 1.59291 +    int nField, nByte;
 1.59292 +
 1.59293 +    nField = ((KeyInfo*)zP4)->nField;
 1.59294 +    nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
 1.59295 +    pKeyInfo = sqlite3DbMallocRaw(0, nByte);
 1.59296 +    pOp->p4.pKeyInfo = pKeyInfo;
 1.59297 +    if( pKeyInfo ){
 1.59298 +      u8 *aSortOrder;
 1.59299 +      memcpy((char*)pKeyInfo, zP4, nByte - nField);
 1.59300 +      aSortOrder = pKeyInfo->aSortOrder;
 1.59301 +      assert( aSortOrder!=0 );
 1.59302 +      pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
 1.59303 +      memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
 1.59304 +      pOp->p4type = P4_KEYINFO;
 1.59305 +    }else{
 1.59306 +      p->db->mallocFailed = 1;
 1.59307 +      pOp->p4type = P4_NOTUSED;
 1.59308 +    }
 1.59309 +  }else if( n==P4_KEYINFO_HANDOFF ){
 1.59310 +    pOp->p4.p = (void*)zP4;
 1.59311 +    pOp->p4type = P4_KEYINFO;
 1.59312 +  }else if( n==P4_VTAB ){
 1.59313 +    pOp->p4.p = (void*)zP4;
 1.59314 +    pOp->p4type = P4_VTAB;
 1.59315 +    sqlite3VtabLock((VTable *)zP4);
 1.59316 +    assert( ((VTable *)zP4)->db==p->db );
 1.59317 +  }else if( n<0 ){
 1.59318 +    pOp->p4.p = (void*)zP4;
 1.59319 +    pOp->p4type = (signed char)n;
 1.59320 +  }else{
 1.59321 +    if( n==0 ) n = sqlite3Strlen30(zP4);
 1.59322 +    pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
 1.59323 +    pOp->p4type = P4_DYNAMIC;
 1.59324 +  }
 1.59325 +}
 1.59326 +
 1.59327 +#ifndef NDEBUG
 1.59328 +/*
 1.59329 +** Change the comment on the most recently coded instruction.  Or
 1.59330 +** insert a No-op and add the comment to that new instruction.  This
 1.59331 +** makes the code easier to read during debugging.  None of this happens
 1.59332 +** in a production build.
 1.59333 +*/
 1.59334 +static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
 1.59335 +  assert( p->nOp>0 || p->aOp==0 );
 1.59336 +  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
 1.59337 +  if( p->nOp ){
 1.59338 +    assert( p->aOp );
 1.59339 +    sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
 1.59340 +    p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
 1.59341 +  }
 1.59342 +}
 1.59343 +SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
 1.59344 +  va_list ap;
 1.59345 +  if( p ){
 1.59346 +    va_start(ap, zFormat);
 1.59347 +    vdbeVComment(p, zFormat, ap);
 1.59348 +    va_end(ap);
 1.59349 +  }
 1.59350 +}
 1.59351 +SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
 1.59352 +  va_list ap;
 1.59353 +  if( p ){
 1.59354 +    sqlite3VdbeAddOp0(p, OP_Noop);
 1.59355 +    va_start(ap, zFormat);
 1.59356 +    vdbeVComment(p, zFormat, ap);
 1.59357 +    va_end(ap);
 1.59358 +  }
 1.59359 +}
 1.59360 +#endif  /* NDEBUG */
 1.59361 +
 1.59362 +/*
 1.59363 +** Return the opcode for a given address.  If the address is -1, then
 1.59364 +** return the most recently inserted opcode.
 1.59365 +**
 1.59366 +** If a memory allocation error has occurred prior to the calling of this
 1.59367 +** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
 1.59368 +** is readable but not writable, though it is cast to a writable value.
 1.59369 +** The return of a dummy opcode allows the call to continue functioning
 1.59370 +** after a OOM fault without having to check to see if the return from 
 1.59371 +** this routine is a valid pointer.  But because the dummy.opcode is 0,
 1.59372 +** dummy will never be written to.  This is verified by code inspection and
 1.59373 +** by running with Valgrind.
 1.59374 +**
 1.59375 +** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
 1.59376 +** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
 1.59377 +** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
 1.59378 +** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
 1.59379 +** having to double-check to make sure that the result is non-negative. But
 1.59380 +** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
 1.59381 +** check the value of p->nOp-1 before continuing.
 1.59382 +*/
 1.59383 +SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
 1.59384 +  /* C89 specifies that the constant "dummy" will be initialized to all
 1.59385 +  ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
 1.59386 +  static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
 1.59387 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.59388 +  if( addr<0 ){
 1.59389 +#ifdef SQLITE_OMIT_TRACE
 1.59390 +    if( p->nOp==0 ) return (VdbeOp*)&dummy;
 1.59391 +#endif
 1.59392 +    addr = p->nOp - 1;
 1.59393 +  }
 1.59394 +  assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
 1.59395 +  if( p->db->mallocFailed ){
 1.59396 +    return (VdbeOp*)&dummy;
 1.59397 +  }else{
 1.59398 +    return &p->aOp[addr];
 1.59399 +  }
 1.59400 +}
 1.59401 +
 1.59402 +#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
 1.59403 +     || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 1.59404 +/*
 1.59405 +** Compute a string that describes the P4 parameter for an opcode.
 1.59406 +** Use zTemp for any required temporary buffer space.
 1.59407 +*/
 1.59408 +static char *displayP4(Op *pOp, char *zTemp, int nTemp){
 1.59409 +  char *zP4 = zTemp;
 1.59410 +  assert( nTemp>=20 );
 1.59411 +  switch( pOp->p4type ){
 1.59412 +    case P4_KEYINFO_STATIC:
 1.59413 +    case P4_KEYINFO: {
 1.59414 +      int i, j;
 1.59415 +      KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
 1.59416 +      assert( pKeyInfo->aSortOrder!=0 );
 1.59417 +      sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
 1.59418 +      i = sqlite3Strlen30(zTemp);
 1.59419 +      for(j=0; j<pKeyInfo->nField; j++){
 1.59420 +        CollSeq *pColl = pKeyInfo->aColl[j];
 1.59421 +        const char *zColl = pColl ? pColl->zName : "nil";
 1.59422 +        int n = sqlite3Strlen30(zColl);
 1.59423 +        if( i+n>nTemp-6 ){
 1.59424 +          memcpy(&zTemp[i],",...",4);
 1.59425 +          break;
 1.59426 +        }
 1.59427 +        zTemp[i++] = ',';
 1.59428 +        if( pKeyInfo->aSortOrder[j] ){
 1.59429 +          zTemp[i++] = '-';
 1.59430 +        }
 1.59431 +        memcpy(&zTemp[i], zColl, n+1);
 1.59432 +        i += n;
 1.59433 +      }
 1.59434 +      zTemp[i++] = ')';
 1.59435 +      zTemp[i] = 0;
 1.59436 +      assert( i<nTemp );
 1.59437 +      break;
 1.59438 +    }
 1.59439 +    case P4_COLLSEQ: {
 1.59440 +      CollSeq *pColl = pOp->p4.pColl;
 1.59441 +      sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
 1.59442 +      break;
 1.59443 +    }
 1.59444 +    case P4_FUNCDEF: {
 1.59445 +      FuncDef *pDef = pOp->p4.pFunc;
 1.59446 +      sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
 1.59447 +      break;
 1.59448 +    }
 1.59449 +    case P4_INT64: {
 1.59450 +      sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
 1.59451 +      break;
 1.59452 +    }
 1.59453 +    case P4_INT32: {
 1.59454 +      sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
 1.59455 +      break;
 1.59456 +    }
 1.59457 +    case P4_REAL: {
 1.59458 +      sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
 1.59459 +      break;
 1.59460 +    }
 1.59461 +    case P4_MEM: {
 1.59462 +      Mem *pMem = pOp->p4.pMem;
 1.59463 +      if( pMem->flags & MEM_Str ){
 1.59464 +        zP4 = pMem->z;
 1.59465 +      }else if( pMem->flags & MEM_Int ){
 1.59466 +        sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
 1.59467 +      }else if( pMem->flags & MEM_Real ){
 1.59468 +        sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
 1.59469 +      }else if( pMem->flags & MEM_Null ){
 1.59470 +        sqlite3_snprintf(nTemp, zTemp, "NULL");
 1.59471 +      }else{
 1.59472 +        assert( pMem->flags & MEM_Blob );
 1.59473 +        zP4 = "(blob)";
 1.59474 +      }
 1.59475 +      break;
 1.59476 +    }
 1.59477 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.59478 +    case P4_VTAB: {
 1.59479 +      sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
 1.59480 +      sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
 1.59481 +      break;
 1.59482 +    }
 1.59483 +#endif
 1.59484 +    case P4_INTARRAY: {
 1.59485 +      sqlite3_snprintf(nTemp, zTemp, "intarray");
 1.59486 +      break;
 1.59487 +    }
 1.59488 +    case P4_SUBPROGRAM: {
 1.59489 +      sqlite3_snprintf(nTemp, zTemp, "program");
 1.59490 +      break;
 1.59491 +    }
 1.59492 +    case P4_ADVANCE: {
 1.59493 +      zTemp[0] = 0;
 1.59494 +      break;
 1.59495 +    }
 1.59496 +    default: {
 1.59497 +      zP4 = pOp->p4.z;
 1.59498 +      if( zP4==0 ){
 1.59499 +        zP4 = zTemp;
 1.59500 +        zTemp[0] = 0;
 1.59501 +      }
 1.59502 +    }
 1.59503 +  }
 1.59504 +  assert( zP4!=0 );
 1.59505 +  return zP4;
 1.59506 +}
 1.59507 +#endif
 1.59508 +
 1.59509 +/*
 1.59510 +** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
 1.59511 +**
 1.59512 +** The prepared statements need to know in advance the complete set of
 1.59513 +** attached databases that will be use.  A mask of these databases
 1.59514 +** is maintained in p->btreeMask.  The p->lockMask value is the subset of
 1.59515 +** p->btreeMask of databases that will require a lock.
 1.59516 +*/
 1.59517 +SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
 1.59518 +  assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
 1.59519 +  assert( i<(int)sizeof(p->btreeMask)*8 );
 1.59520 +  p->btreeMask |= ((yDbMask)1)<<i;
 1.59521 +  if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
 1.59522 +    p->lockMask |= ((yDbMask)1)<<i;
 1.59523 +  }
 1.59524 +}
 1.59525 +
 1.59526 +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 1.59527 +/*
 1.59528 +** If SQLite is compiled to support shared-cache mode and to be threadsafe,
 1.59529 +** this routine obtains the mutex associated with each BtShared structure
 1.59530 +** that may be accessed by the VM passed as an argument. In doing so it also
 1.59531 +** sets the BtShared.db member of each of the BtShared structures, ensuring
 1.59532 +** that the correct busy-handler callback is invoked if required.
 1.59533 +**
 1.59534 +** If SQLite is not threadsafe but does support shared-cache mode, then
 1.59535 +** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
 1.59536 +** of all of BtShared structures accessible via the database handle 
 1.59537 +** associated with the VM.
 1.59538 +**
 1.59539 +** If SQLite is not threadsafe and does not support shared-cache mode, this
 1.59540 +** function is a no-op.
 1.59541 +**
 1.59542 +** The p->btreeMask field is a bitmask of all btrees that the prepared 
 1.59543 +** statement p will ever use.  Let N be the number of bits in p->btreeMask
 1.59544 +** corresponding to btrees that use shared cache.  Then the runtime of
 1.59545 +** this routine is N*N.  But as N is rarely more than 1, this should not
 1.59546 +** be a problem.
 1.59547 +*/
 1.59548 +SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
 1.59549 +  int i;
 1.59550 +  yDbMask mask;
 1.59551 +  sqlite3 *db;
 1.59552 +  Db *aDb;
 1.59553 +  int nDb;
 1.59554 +  if( p->lockMask==0 ) return;  /* The common case */
 1.59555 +  db = p->db;
 1.59556 +  aDb = db->aDb;
 1.59557 +  nDb = db->nDb;
 1.59558 +  for(i=0, mask=1; i<nDb; i++, mask += mask){
 1.59559 +    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
 1.59560 +      sqlite3BtreeEnter(aDb[i].pBt);
 1.59561 +    }
 1.59562 +  }
 1.59563 +}
 1.59564 +#endif
 1.59565 +
 1.59566 +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 1.59567 +/*
 1.59568 +** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
 1.59569 +*/
 1.59570 +SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
 1.59571 +  int i;
 1.59572 +  yDbMask mask;
 1.59573 +  sqlite3 *db;
 1.59574 +  Db *aDb;
 1.59575 +  int nDb;
 1.59576 +  if( p->lockMask==0 ) return;  /* The common case */
 1.59577 +  db = p->db;
 1.59578 +  aDb = db->aDb;
 1.59579 +  nDb = db->nDb;
 1.59580 +  for(i=0, mask=1; i<nDb; i++, mask += mask){
 1.59581 +    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
 1.59582 +      sqlite3BtreeLeave(aDb[i].pBt);
 1.59583 +    }
 1.59584 +  }
 1.59585 +}
 1.59586 +#endif
 1.59587 +
 1.59588 +#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 1.59589 +/*
 1.59590 +** Print a single opcode.  This routine is used for debugging only.
 1.59591 +*/
 1.59592 +SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
 1.59593 +  char *zP4;
 1.59594 +  char zPtr[50];
 1.59595 +  static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
 1.59596 +  if( pOut==0 ) pOut = stdout;
 1.59597 +  zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
 1.59598 +  fprintf(pOut, zFormat1, pc, 
 1.59599 +      sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
 1.59600 +#ifdef SQLITE_DEBUG
 1.59601 +      pOp->zComment ? pOp->zComment : ""
 1.59602 +#else
 1.59603 +      ""
 1.59604 +#endif
 1.59605 +  );
 1.59606 +  fflush(pOut);
 1.59607 +}
 1.59608 +#endif
 1.59609 +
 1.59610 +/*
 1.59611 +** Release an array of N Mem elements
 1.59612 +*/
 1.59613 +static void releaseMemArray(Mem *p, int N){
 1.59614 +  if( p && N ){
 1.59615 +    Mem *pEnd;
 1.59616 +    sqlite3 *db = p->db;
 1.59617 +    u8 malloc_failed = db->mallocFailed;
 1.59618 +    if( db->pnBytesFreed ){
 1.59619 +      for(pEnd=&p[N]; p<pEnd; p++){
 1.59620 +        sqlite3DbFree(db, p->zMalloc);
 1.59621 +      }
 1.59622 +      return;
 1.59623 +    }
 1.59624 +    for(pEnd=&p[N]; p<pEnd; p++){
 1.59625 +      assert( (&p[1])==pEnd || p[0].db==p[1].db );
 1.59626 +
 1.59627 +      /* This block is really an inlined version of sqlite3VdbeMemRelease()
 1.59628 +      ** that takes advantage of the fact that the memory cell value is 
 1.59629 +      ** being set to NULL after releasing any dynamic resources.
 1.59630 +      **
 1.59631 +      ** The justification for duplicating code is that according to 
 1.59632 +      ** callgrind, this causes a certain test case to hit the CPU 4.7 
 1.59633 +      ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 
 1.59634 +      ** sqlite3MemRelease() were called from here. With -O2, this jumps
 1.59635 +      ** to 6.6 percent. The test case is inserting 1000 rows into a table 
 1.59636 +      ** with no indexes using a single prepared INSERT statement, bind() 
 1.59637 +      ** and reset(). Inserts are grouped into a transaction.
 1.59638 +      */
 1.59639 +      if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
 1.59640 +        sqlite3VdbeMemRelease(p);
 1.59641 +      }else if( p->zMalloc ){
 1.59642 +        sqlite3DbFree(db, p->zMalloc);
 1.59643 +        p->zMalloc = 0;
 1.59644 +      }
 1.59645 +
 1.59646 +      p->flags = MEM_Invalid;
 1.59647 +    }
 1.59648 +    db->mallocFailed = malloc_failed;
 1.59649 +  }
 1.59650 +}
 1.59651 +
 1.59652 +/*
 1.59653 +** Delete a VdbeFrame object and its contents. VdbeFrame objects are
 1.59654 +** allocated by the OP_Program opcode in sqlite3VdbeExec().
 1.59655 +*/
 1.59656 +SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
 1.59657 +  int i;
 1.59658 +  Mem *aMem = VdbeFrameMem(p);
 1.59659 +  VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
 1.59660 +  for(i=0; i<p->nChildCsr; i++){
 1.59661 +    sqlite3VdbeFreeCursor(p->v, apCsr[i]);
 1.59662 +  }
 1.59663 +  releaseMemArray(aMem, p->nChildMem);
 1.59664 +  sqlite3DbFree(p->v->db, p);
 1.59665 +}
 1.59666 +
 1.59667 +#ifndef SQLITE_OMIT_EXPLAIN
 1.59668 +/*
 1.59669 +** Give a listing of the program in the virtual machine.
 1.59670 +**
 1.59671 +** The interface is the same as sqlite3VdbeExec().  But instead of
 1.59672 +** running the code, it invokes the callback once for each instruction.
 1.59673 +** This feature is used to implement "EXPLAIN".
 1.59674 +**
 1.59675 +** When p->explain==1, each instruction is listed.  When
 1.59676 +** p->explain==2, only OP_Explain instructions are listed and these
 1.59677 +** are shown in a different format.  p->explain==2 is used to implement
 1.59678 +** EXPLAIN QUERY PLAN.
 1.59679 +**
 1.59680 +** When p->explain==1, first the main program is listed, then each of
 1.59681 +** the trigger subprograms are listed one by one.
 1.59682 +*/
 1.59683 +SQLITE_PRIVATE int sqlite3VdbeList(
 1.59684 +  Vdbe *p                   /* The VDBE */
 1.59685 +){
 1.59686 +  int nRow;                            /* Stop when row count reaches this */
 1.59687 +  int nSub = 0;                        /* Number of sub-vdbes seen so far */
 1.59688 +  SubProgram **apSub = 0;              /* Array of sub-vdbes */
 1.59689 +  Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
 1.59690 +  sqlite3 *db = p->db;                 /* The database connection */
 1.59691 +  int i;                               /* Loop counter */
 1.59692 +  int rc = SQLITE_OK;                  /* Return code */
 1.59693 +  Mem *pMem = &p->aMem[1];             /* First Mem of result set */
 1.59694 +
 1.59695 +  assert( p->explain );
 1.59696 +  assert( p->magic==VDBE_MAGIC_RUN );
 1.59697 +  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
 1.59698 +
 1.59699 +  /* Even though this opcode does not use dynamic strings for
 1.59700 +  ** the result, result columns may become dynamic if the user calls
 1.59701 +  ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
 1.59702 +  */
 1.59703 +  releaseMemArray(pMem, 8);
 1.59704 +  p->pResultSet = 0;
 1.59705 +
 1.59706 +  if( p->rc==SQLITE_NOMEM ){
 1.59707 +    /* This happens if a malloc() inside a call to sqlite3_column_text() or
 1.59708 +    ** sqlite3_column_text16() failed.  */
 1.59709 +    db->mallocFailed = 1;
 1.59710 +    return SQLITE_ERROR;
 1.59711 +  }
 1.59712 +
 1.59713 +  /* When the number of output rows reaches nRow, that means the
 1.59714 +  ** listing has finished and sqlite3_step() should return SQLITE_DONE.
 1.59715 +  ** nRow is the sum of the number of rows in the main program, plus
 1.59716 +  ** the sum of the number of rows in all trigger subprograms encountered
 1.59717 +  ** so far.  The nRow value will increase as new trigger subprograms are
 1.59718 +  ** encountered, but p->pc will eventually catch up to nRow.
 1.59719 +  */
 1.59720 +  nRow = p->nOp;
 1.59721 +  if( p->explain==1 ){
 1.59722 +    /* The first 8 memory cells are used for the result set.  So we will
 1.59723 +    ** commandeer the 9th cell to use as storage for an array of pointers
 1.59724 +    ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
 1.59725 +    ** cells.  */
 1.59726 +    assert( p->nMem>9 );
 1.59727 +    pSub = &p->aMem[9];
 1.59728 +    if( pSub->flags&MEM_Blob ){
 1.59729 +      /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
 1.59730 +      ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
 1.59731 +      nSub = pSub->n/sizeof(Vdbe*);
 1.59732 +      apSub = (SubProgram **)pSub->z;
 1.59733 +    }
 1.59734 +    for(i=0; i<nSub; i++){
 1.59735 +      nRow += apSub[i]->nOp;
 1.59736 +    }
 1.59737 +  }
 1.59738 +
 1.59739 +  do{
 1.59740 +    i = p->pc++;
 1.59741 +  }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
 1.59742 +  if( i>=nRow ){
 1.59743 +    p->rc = SQLITE_OK;
 1.59744 +    rc = SQLITE_DONE;
 1.59745 +  }else if( db->u1.isInterrupted ){
 1.59746 +    p->rc = SQLITE_INTERRUPT;
 1.59747 +    rc = SQLITE_ERROR;
 1.59748 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
 1.59749 +  }else{
 1.59750 +    char *z;
 1.59751 +    Op *pOp;
 1.59752 +    if( i<p->nOp ){
 1.59753 +      /* The output line number is small enough that we are still in the
 1.59754 +      ** main program. */
 1.59755 +      pOp = &p->aOp[i];
 1.59756 +    }else{
 1.59757 +      /* We are currently listing subprograms.  Figure out which one and
 1.59758 +      ** pick up the appropriate opcode. */
 1.59759 +      int j;
 1.59760 +      i -= p->nOp;
 1.59761 +      for(j=0; i>=apSub[j]->nOp; j++){
 1.59762 +        i -= apSub[j]->nOp;
 1.59763 +      }
 1.59764 +      pOp = &apSub[j]->aOp[i];
 1.59765 +    }
 1.59766 +    if( p->explain==1 ){
 1.59767 +      pMem->flags = MEM_Int;
 1.59768 +      pMem->type = SQLITE_INTEGER;
 1.59769 +      pMem->u.i = i;                                /* Program counter */
 1.59770 +      pMem++;
 1.59771 +  
 1.59772 +      pMem->flags = MEM_Static|MEM_Str|MEM_Term;
 1.59773 +      pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
 1.59774 +      assert( pMem->z!=0 );
 1.59775 +      pMem->n = sqlite3Strlen30(pMem->z);
 1.59776 +      pMem->type = SQLITE_TEXT;
 1.59777 +      pMem->enc = SQLITE_UTF8;
 1.59778 +      pMem++;
 1.59779 +
 1.59780 +      /* When an OP_Program opcode is encounter (the only opcode that has
 1.59781 +      ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
 1.59782 +      ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
 1.59783 +      ** has not already been seen.
 1.59784 +      */
 1.59785 +      if( pOp->p4type==P4_SUBPROGRAM ){
 1.59786 +        int nByte = (nSub+1)*sizeof(SubProgram*);
 1.59787 +        int j;
 1.59788 +        for(j=0; j<nSub; j++){
 1.59789 +          if( apSub[j]==pOp->p4.pProgram ) break;
 1.59790 +        }
 1.59791 +        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
 1.59792 +          apSub = (SubProgram **)pSub->z;
 1.59793 +          apSub[nSub++] = pOp->p4.pProgram;
 1.59794 +          pSub->flags |= MEM_Blob;
 1.59795 +          pSub->n = nSub*sizeof(SubProgram*);
 1.59796 +        }
 1.59797 +      }
 1.59798 +    }
 1.59799 +
 1.59800 +    pMem->flags = MEM_Int;
 1.59801 +    pMem->u.i = pOp->p1;                          /* P1 */
 1.59802 +    pMem->type = SQLITE_INTEGER;
 1.59803 +    pMem++;
 1.59804 +
 1.59805 +    pMem->flags = MEM_Int;
 1.59806 +    pMem->u.i = pOp->p2;                          /* P2 */
 1.59807 +    pMem->type = SQLITE_INTEGER;
 1.59808 +    pMem++;
 1.59809 +
 1.59810 +    pMem->flags = MEM_Int;
 1.59811 +    pMem->u.i = pOp->p3;                          /* P3 */
 1.59812 +    pMem->type = SQLITE_INTEGER;
 1.59813 +    pMem++;
 1.59814 +
 1.59815 +    if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
 1.59816 +      assert( p->db->mallocFailed );
 1.59817 +      return SQLITE_ERROR;
 1.59818 +    }
 1.59819 +    pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
 1.59820 +    z = displayP4(pOp, pMem->z, 32);
 1.59821 +    if( z!=pMem->z ){
 1.59822 +      sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
 1.59823 +    }else{
 1.59824 +      assert( pMem->z!=0 );
 1.59825 +      pMem->n = sqlite3Strlen30(pMem->z);
 1.59826 +      pMem->enc = SQLITE_UTF8;
 1.59827 +    }
 1.59828 +    pMem->type = SQLITE_TEXT;
 1.59829 +    pMem++;
 1.59830 +
 1.59831 +    if( p->explain==1 ){
 1.59832 +      if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
 1.59833 +        assert( p->db->mallocFailed );
 1.59834 +        return SQLITE_ERROR;
 1.59835 +      }
 1.59836 +      pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
 1.59837 +      pMem->n = 2;
 1.59838 +      sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
 1.59839 +      pMem->type = SQLITE_TEXT;
 1.59840 +      pMem->enc = SQLITE_UTF8;
 1.59841 +      pMem++;
 1.59842 +  
 1.59843 +#ifdef SQLITE_DEBUG
 1.59844 +      if( pOp->zComment ){
 1.59845 +        pMem->flags = MEM_Str|MEM_Term;
 1.59846 +        pMem->z = pOp->zComment;
 1.59847 +        pMem->n = sqlite3Strlen30(pMem->z);
 1.59848 +        pMem->enc = SQLITE_UTF8;
 1.59849 +        pMem->type = SQLITE_TEXT;
 1.59850 +      }else
 1.59851 +#endif
 1.59852 +      {
 1.59853 +        pMem->flags = MEM_Null;                       /* Comment */
 1.59854 +        pMem->type = SQLITE_NULL;
 1.59855 +      }
 1.59856 +    }
 1.59857 +
 1.59858 +    p->nResColumn = 8 - 4*(p->explain-1);
 1.59859 +    p->pResultSet = &p->aMem[1];
 1.59860 +    p->rc = SQLITE_OK;
 1.59861 +    rc = SQLITE_ROW;
 1.59862 +  }
 1.59863 +  return rc;
 1.59864 +}
 1.59865 +#endif /* SQLITE_OMIT_EXPLAIN */
 1.59866 +
 1.59867 +#ifdef SQLITE_DEBUG
 1.59868 +/*
 1.59869 +** Print the SQL that was used to generate a VDBE program.
 1.59870 +*/
 1.59871 +SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
 1.59872 +  int nOp = p->nOp;
 1.59873 +  VdbeOp *pOp;
 1.59874 +  if( nOp<1 ) return;
 1.59875 +  pOp = &p->aOp[0];
 1.59876 +  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
 1.59877 +    const char *z = pOp->p4.z;
 1.59878 +    while( sqlite3Isspace(*z) ) z++;
 1.59879 +    printf("SQL: [%s]\n", z);
 1.59880 +  }
 1.59881 +}
 1.59882 +#endif
 1.59883 +
 1.59884 +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
 1.59885 +/*
 1.59886 +** Print an IOTRACE message showing SQL content.
 1.59887 +*/
 1.59888 +SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
 1.59889 +  int nOp = p->nOp;
 1.59890 +  VdbeOp *pOp;
 1.59891 +  if( sqlite3IoTrace==0 ) return;
 1.59892 +  if( nOp<1 ) return;
 1.59893 +  pOp = &p->aOp[0];
 1.59894 +  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
 1.59895 +    int i, j;
 1.59896 +    char z[1000];
 1.59897 +    sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
 1.59898 +    for(i=0; sqlite3Isspace(z[i]); i++){}
 1.59899 +    for(j=0; z[i]; i++){
 1.59900 +      if( sqlite3Isspace(z[i]) ){
 1.59901 +        if( z[i-1]!=' ' ){
 1.59902 +          z[j++] = ' ';
 1.59903 +        }
 1.59904 +      }else{
 1.59905 +        z[j++] = z[i];
 1.59906 +      }
 1.59907 +    }
 1.59908 +    z[j] = 0;
 1.59909 +    sqlite3IoTrace("SQL %s\n", z);
 1.59910 +  }
 1.59911 +}
 1.59912 +#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
 1.59913 +
 1.59914 +/*
 1.59915 +** Allocate space from a fixed size buffer and return a pointer to
 1.59916 +** that space.  If insufficient space is available, return NULL.
 1.59917 +**
 1.59918 +** The pBuf parameter is the initial value of a pointer which will
 1.59919 +** receive the new memory.  pBuf is normally NULL.  If pBuf is not
 1.59920 +** NULL, it means that memory space has already been allocated and that
 1.59921 +** this routine should not allocate any new memory.  When pBuf is not
 1.59922 +** NULL simply return pBuf.  Only allocate new memory space when pBuf
 1.59923 +** is NULL.
 1.59924 +**
 1.59925 +** nByte is the number of bytes of space needed.
 1.59926 +**
 1.59927 +** *ppFrom points to available space and pEnd points to the end of the
 1.59928 +** available space.  When space is allocated, *ppFrom is advanced past
 1.59929 +** the end of the allocated space.
 1.59930 +**
 1.59931 +** *pnByte is a counter of the number of bytes of space that have failed
 1.59932 +** to allocate.  If there is insufficient space in *ppFrom to satisfy the
 1.59933 +** request, then increment *pnByte by the amount of the request.
 1.59934 +*/
 1.59935 +static void *allocSpace(
 1.59936 +  void *pBuf,          /* Where return pointer will be stored */
 1.59937 +  int nByte,           /* Number of bytes to allocate */
 1.59938 +  u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
 1.59939 +  u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
 1.59940 +  int *pnByte          /* If allocation cannot be made, increment *pnByte */
 1.59941 +){
 1.59942 +  assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
 1.59943 +  if( pBuf ) return pBuf;
 1.59944 +  nByte = ROUND8(nByte);
 1.59945 +  if( &(*ppFrom)[nByte] <= pEnd ){
 1.59946 +    pBuf = (void*)*ppFrom;
 1.59947 +    *ppFrom += nByte;
 1.59948 +  }else{
 1.59949 +    *pnByte += nByte;
 1.59950 +  }
 1.59951 +  return pBuf;
 1.59952 +}
 1.59953 +
 1.59954 +/*
 1.59955 +** Rewind the VDBE back to the beginning in preparation for
 1.59956 +** running it.
 1.59957 +*/
 1.59958 +SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
 1.59959 +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 1.59960 +  int i;
 1.59961 +#endif
 1.59962 +  assert( p!=0 );
 1.59963 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.59964 +
 1.59965 +  /* There should be at least one opcode.
 1.59966 +  */
 1.59967 +  assert( p->nOp>0 );
 1.59968 +
 1.59969 +  /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
 1.59970 +  p->magic = VDBE_MAGIC_RUN;
 1.59971 +
 1.59972 +#ifdef SQLITE_DEBUG
 1.59973 +  for(i=1; i<p->nMem; i++){
 1.59974 +    assert( p->aMem[i].db==p->db );
 1.59975 +  }
 1.59976 +#endif
 1.59977 +  p->pc = -1;
 1.59978 +  p->rc = SQLITE_OK;
 1.59979 +  p->errorAction = OE_Abort;
 1.59980 +  p->magic = VDBE_MAGIC_RUN;
 1.59981 +  p->nChange = 0;
 1.59982 +  p->cacheCtr = 1;
 1.59983 +  p->minWriteFileFormat = 255;
 1.59984 +  p->iStatement = 0;
 1.59985 +  p->nFkConstraint = 0;
 1.59986 +#ifdef VDBE_PROFILE
 1.59987 +  for(i=0; i<p->nOp; i++){
 1.59988 +    p->aOp[i].cnt = 0;
 1.59989 +    p->aOp[i].cycles = 0;
 1.59990 +  }
 1.59991 +#endif
 1.59992 +}
 1.59993 +
 1.59994 +/*
 1.59995 +** Prepare a virtual machine for execution for the first time after
 1.59996 +** creating the virtual machine.  This involves things such
 1.59997 +** as allocating stack space and initializing the program counter.
 1.59998 +** After the VDBE has be prepped, it can be executed by one or more
 1.59999 +** calls to sqlite3VdbeExec().  
 1.60000 +**
 1.60001 +** This function may be called exact once on a each virtual machine.
 1.60002 +** After this routine is called the VM has been "packaged" and is ready
 1.60003 +** to run.  After this routine is called, futher calls to 
 1.60004 +** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
 1.60005 +** the Vdbe from the Parse object that helped generate it so that the
 1.60006 +** the Vdbe becomes an independent entity and the Parse object can be
 1.60007 +** destroyed.
 1.60008 +**
 1.60009 +** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
 1.60010 +** to its initial state after it has been run.
 1.60011 +*/
 1.60012 +SQLITE_PRIVATE void sqlite3VdbeMakeReady(
 1.60013 +  Vdbe *p,                       /* The VDBE */
 1.60014 +  Parse *pParse                  /* Parsing context */
 1.60015 +){
 1.60016 +  sqlite3 *db;                   /* The database connection */
 1.60017 +  int nVar;                      /* Number of parameters */
 1.60018 +  int nMem;                      /* Number of VM memory registers */
 1.60019 +  int nCursor;                   /* Number of cursors required */
 1.60020 +  int nArg;                      /* Number of arguments in subprograms */
 1.60021 +  int nOnce;                     /* Number of OP_Once instructions */
 1.60022 +  int n;                         /* Loop counter */
 1.60023 +  u8 *zCsr;                      /* Memory available for allocation */
 1.60024 +  u8 *zEnd;                      /* First byte past allocated memory */
 1.60025 +  int nByte;                     /* How much extra memory is needed */
 1.60026 +
 1.60027 +  assert( p!=0 );
 1.60028 +  assert( p->nOp>0 );
 1.60029 +  assert( pParse!=0 );
 1.60030 +  assert( p->magic==VDBE_MAGIC_INIT );
 1.60031 +  db = p->db;
 1.60032 +  assert( db->mallocFailed==0 );
 1.60033 +  nVar = pParse->nVar;
 1.60034 +  nMem = pParse->nMem;
 1.60035 +  nCursor = pParse->nTab;
 1.60036 +  nArg = pParse->nMaxArg;
 1.60037 +  nOnce = pParse->nOnce;
 1.60038 +  if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
 1.60039 +  
 1.60040 +  /* For each cursor required, also allocate a memory cell. Memory
 1.60041 +  ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
 1.60042 +  ** the vdbe program. Instead they are used to allocate space for
 1.60043 +  ** VdbeCursor/BtCursor structures. The blob of memory associated with 
 1.60044 +  ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
 1.60045 +  ** stores the blob of memory associated with cursor 1, etc.
 1.60046 +  **
 1.60047 +  ** See also: allocateCursor().
 1.60048 +  */
 1.60049 +  nMem += nCursor;
 1.60050 +
 1.60051 +  /* Allocate space for memory registers, SQL variables, VDBE cursors and 
 1.60052 +  ** an array to marshal SQL function arguments in.
 1.60053 +  */
 1.60054 +  zCsr = (u8*)&p->aOp[p->nOp];       /* Memory avaliable for allocation */
 1.60055 +  zEnd = (u8*)&p->aOp[p->nOpAlloc];  /* First byte past end of zCsr[] */
 1.60056 +
 1.60057 +  resolveP2Values(p, &nArg);
 1.60058 +  p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
 1.60059 +  if( pParse->explain && nMem<10 ){
 1.60060 +    nMem = 10;
 1.60061 +  }
 1.60062 +  memset(zCsr, 0, zEnd-zCsr);
 1.60063 +  zCsr += (zCsr - (u8*)0)&7;
 1.60064 +  assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
 1.60065 +  p->expired = 0;
 1.60066 +
 1.60067 +  /* Memory for registers, parameters, cursor, etc, is allocated in two
 1.60068 +  ** passes.  On the first pass, we try to reuse unused space at the 
 1.60069 +  ** end of the opcode array.  If we are unable to satisfy all memory
 1.60070 +  ** requirements by reusing the opcode array tail, then the second
 1.60071 +  ** pass will fill in the rest using a fresh allocation.  
 1.60072 +  **
 1.60073 +  ** This two-pass approach that reuses as much memory as possible from
 1.60074 +  ** the leftover space at the end of the opcode array can significantly
 1.60075 +  ** reduce the amount of memory held by a prepared statement.
 1.60076 +  */
 1.60077 +  do {
 1.60078 +    nByte = 0;
 1.60079 +    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
 1.60080 +    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
 1.60081 +    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
 1.60082 +    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
 1.60083 +    p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
 1.60084 +                          &zCsr, zEnd, &nByte);
 1.60085 +    p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
 1.60086 +    if( nByte ){
 1.60087 +      p->pFree = sqlite3DbMallocZero(db, nByte);
 1.60088 +    }
 1.60089 +    zCsr = p->pFree;
 1.60090 +    zEnd = &zCsr[nByte];
 1.60091 +  }while( nByte && !db->mallocFailed );
 1.60092 +
 1.60093 +  p->nCursor = (u16)nCursor;
 1.60094 +  p->nOnceFlag = nOnce;
 1.60095 +  if( p->aVar ){
 1.60096 +    p->nVar = (ynVar)nVar;
 1.60097 +    for(n=0; n<nVar; n++){
 1.60098 +      p->aVar[n].flags = MEM_Null;
 1.60099 +      p->aVar[n].db = db;
 1.60100 +    }
 1.60101 +  }
 1.60102 +  if( p->azVar ){
 1.60103 +    p->nzVar = pParse->nzVar;
 1.60104 +    memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
 1.60105 +    memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
 1.60106 +  }
 1.60107 +  if( p->aMem ){
 1.60108 +    p->aMem--;                      /* aMem[] goes from 1..nMem */
 1.60109 +    p->nMem = nMem;                 /*       not from 0..nMem-1 */
 1.60110 +    for(n=1; n<=nMem; n++){
 1.60111 +      p->aMem[n].flags = MEM_Invalid;
 1.60112 +      p->aMem[n].db = db;
 1.60113 +    }
 1.60114 +  }
 1.60115 +  p->explain = pParse->explain;
 1.60116 +  sqlite3VdbeRewind(p);
 1.60117 +}
 1.60118 +
 1.60119 +/*
 1.60120 +** Close a VDBE cursor and release all the resources that cursor 
 1.60121 +** happens to hold.
 1.60122 +*/
 1.60123 +SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
 1.60124 +  if( pCx==0 ){
 1.60125 +    return;
 1.60126 +  }
 1.60127 +  sqlite3VdbeSorterClose(p->db, pCx);
 1.60128 +  if( pCx->pBt ){
 1.60129 +    sqlite3BtreeClose(pCx->pBt);
 1.60130 +    /* The pCx->pCursor will be close automatically, if it exists, by
 1.60131 +    ** the call above. */
 1.60132 +  }else if( pCx->pCursor ){
 1.60133 +    sqlite3BtreeCloseCursor(pCx->pCursor);
 1.60134 +  }
 1.60135 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.60136 +  if( pCx->pVtabCursor ){
 1.60137 +    sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
 1.60138 +    const sqlite3_module *pModule = pCx->pModule;
 1.60139 +    p->inVtabMethod = 1;
 1.60140 +    pModule->xClose(pVtabCursor);
 1.60141 +    p->inVtabMethod = 0;
 1.60142 +  }
 1.60143 +#endif
 1.60144 +}
 1.60145 +
 1.60146 +/*
 1.60147 +** Copy the values stored in the VdbeFrame structure to its Vdbe. This
 1.60148 +** is used, for example, when a trigger sub-program is halted to restore
 1.60149 +** control to the main program.
 1.60150 +*/
 1.60151 +SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
 1.60152 +  Vdbe *v = pFrame->v;
 1.60153 +  v->aOnceFlag = pFrame->aOnceFlag;
 1.60154 +  v->nOnceFlag = pFrame->nOnceFlag;
 1.60155 +  v->aOp = pFrame->aOp;
 1.60156 +  v->nOp = pFrame->nOp;
 1.60157 +  v->aMem = pFrame->aMem;
 1.60158 +  v->nMem = pFrame->nMem;
 1.60159 +  v->apCsr = pFrame->apCsr;
 1.60160 +  v->nCursor = pFrame->nCursor;
 1.60161 +  v->db->lastRowid = pFrame->lastRowid;
 1.60162 +  v->nChange = pFrame->nChange;
 1.60163 +  return pFrame->pc;
 1.60164 +}
 1.60165 +
 1.60166 +/*
 1.60167 +** Close all cursors.
 1.60168 +**
 1.60169 +** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
 1.60170 +** cell array. This is necessary as the memory cell array may contain
 1.60171 +** pointers to VdbeFrame objects, which may in turn contain pointers to
 1.60172 +** open cursors.
 1.60173 +*/
 1.60174 +static void closeAllCursors(Vdbe *p){
 1.60175 +  if( p->pFrame ){
 1.60176 +    VdbeFrame *pFrame;
 1.60177 +    for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 1.60178 +    sqlite3VdbeFrameRestore(pFrame);
 1.60179 +  }
 1.60180 +  p->pFrame = 0;
 1.60181 +  p->nFrame = 0;
 1.60182 +
 1.60183 +  if( p->apCsr ){
 1.60184 +    int i;
 1.60185 +    for(i=0; i<p->nCursor; i++){
 1.60186 +      VdbeCursor *pC = p->apCsr[i];
 1.60187 +      if( pC ){
 1.60188 +        sqlite3VdbeFreeCursor(p, pC);
 1.60189 +        p->apCsr[i] = 0;
 1.60190 +      }
 1.60191 +    }
 1.60192 +  }
 1.60193 +  if( p->aMem ){
 1.60194 +    releaseMemArray(&p->aMem[1], p->nMem);
 1.60195 +  }
 1.60196 +  while( p->pDelFrame ){
 1.60197 +    VdbeFrame *pDel = p->pDelFrame;
 1.60198 +    p->pDelFrame = pDel->pParent;
 1.60199 +    sqlite3VdbeFrameDelete(pDel);
 1.60200 +  }
 1.60201 +}
 1.60202 +
 1.60203 +/*
 1.60204 +** Clean up the VM after execution.
 1.60205 +**
 1.60206 +** This routine will automatically close any cursors, lists, and/or
 1.60207 +** sorters that were left open.  It also deletes the values of
 1.60208 +** variables in the aVar[] array.
 1.60209 +*/
 1.60210 +static void Cleanup(Vdbe *p){
 1.60211 +  sqlite3 *db = p->db;
 1.60212 +
 1.60213 +#ifdef SQLITE_DEBUG
 1.60214 +  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
 1.60215 +  ** Vdbe.aMem[] arrays have already been cleaned up.  */
 1.60216 +  int i;
 1.60217 +  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
 1.60218 +  if( p->aMem ){
 1.60219 +    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
 1.60220 +  }
 1.60221 +#endif
 1.60222 +
 1.60223 +  sqlite3DbFree(db, p->zErrMsg);
 1.60224 +  p->zErrMsg = 0;
 1.60225 +  p->pResultSet = 0;
 1.60226 +}
 1.60227 +
 1.60228 +/*
 1.60229 +** Set the number of result columns that will be returned by this SQL
 1.60230 +** statement. This is now set at compile time, rather than during
 1.60231 +** execution of the vdbe program so that sqlite3_column_count() can
 1.60232 +** be called on an SQL statement before sqlite3_step().
 1.60233 +*/
 1.60234 +SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
 1.60235 +  Mem *pColName;
 1.60236 +  int n;
 1.60237 +  sqlite3 *db = p->db;
 1.60238 +
 1.60239 +  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 1.60240 +  sqlite3DbFree(db, p->aColName);
 1.60241 +  n = nResColumn*COLNAME_N;
 1.60242 +  p->nResColumn = (u16)nResColumn;
 1.60243 +  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
 1.60244 +  if( p->aColName==0 ) return;
 1.60245 +  while( n-- > 0 ){
 1.60246 +    pColName->flags = MEM_Null;
 1.60247 +    pColName->db = p->db;
 1.60248 +    pColName++;
 1.60249 +  }
 1.60250 +}
 1.60251 +
 1.60252 +/*
 1.60253 +** Set the name of the idx'th column to be returned by the SQL statement.
 1.60254 +** zName must be a pointer to a nul terminated string.
 1.60255 +**
 1.60256 +** This call must be made after a call to sqlite3VdbeSetNumCols().
 1.60257 +**
 1.60258 +** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
 1.60259 +** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
 1.60260 +** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
 1.60261 +*/
 1.60262 +SQLITE_PRIVATE int sqlite3VdbeSetColName(
 1.60263 +  Vdbe *p,                         /* Vdbe being configured */
 1.60264 +  int idx,                         /* Index of column zName applies to */
 1.60265 +  int var,                         /* One of the COLNAME_* constants */
 1.60266 +  const char *zName,               /* Pointer to buffer containing name */
 1.60267 +  void (*xDel)(void*)              /* Memory management strategy for zName */
 1.60268 +){
 1.60269 +  int rc;
 1.60270 +  Mem *pColName;
 1.60271 +  assert( idx<p->nResColumn );
 1.60272 +  assert( var<COLNAME_N );
 1.60273 +  if( p->db->mallocFailed ){
 1.60274 +    assert( !zName || xDel!=SQLITE_DYNAMIC );
 1.60275 +    return SQLITE_NOMEM;
 1.60276 +  }
 1.60277 +  assert( p->aColName!=0 );
 1.60278 +  pColName = &(p->aColName[idx+var*p->nResColumn]);
 1.60279 +  rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
 1.60280 +  assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
 1.60281 +  return rc;
 1.60282 +}
 1.60283 +
 1.60284 +/*
 1.60285 +** A read or write transaction may or may not be active on database handle
 1.60286 +** db. If a transaction is active, commit it. If there is a
 1.60287 +** write-transaction spanning more than one database file, this routine
 1.60288 +** takes care of the master journal trickery.
 1.60289 +*/
 1.60290 +static int vdbeCommit(sqlite3 *db, Vdbe *p){
 1.60291 +  int i;
 1.60292 +  int nTrans = 0;  /* Number of databases with an active write-transaction */
 1.60293 +  int rc = SQLITE_OK;
 1.60294 +  int needXcommit = 0;
 1.60295 +
 1.60296 +#ifdef SQLITE_OMIT_VIRTUALTABLE
 1.60297 +  /* With this option, sqlite3VtabSync() is defined to be simply 
 1.60298 +  ** SQLITE_OK so p is not used. 
 1.60299 +  */
 1.60300 +  UNUSED_PARAMETER(p);
 1.60301 +#endif
 1.60302 +
 1.60303 +  /* Before doing anything else, call the xSync() callback for any
 1.60304 +  ** virtual module tables written in this transaction. This has to
 1.60305 +  ** be done before determining whether a master journal file is 
 1.60306 +  ** required, as an xSync() callback may add an attached database
 1.60307 +  ** to the transaction.
 1.60308 +  */
 1.60309 +  rc = sqlite3VtabSync(db, &p->zErrMsg);
 1.60310 +
 1.60311 +  /* This loop determines (a) if the commit hook should be invoked and
 1.60312 +  ** (b) how many database files have open write transactions, not 
 1.60313 +  ** including the temp database. (b) is important because if more than 
 1.60314 +  ** one database file has an open write transaction, a master journal
 1.60315 +  ** file is required for an atomic commit.
 1.60316 +  */ 
 1.60317 +  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
 1.60318 +    Btree *pBt = db->aDb[i].pBt;
 1.60319 +    if( sqlite3BtreeIsInTrans(pBt) ){
 1.60320 +      needXcommit = 1;
 1.60321 +      if( i!=1 ) nTrans++;
 1.60322 +      sqlite3BtreeEnter(pBt);
 1.60323 +      rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
 1.60324 +      sqlite3BtreeLeave(pBt);
 1.60325 +    }
 1.60326 +  }
 1.60327 +  if( rc!=SQLITE_OK ){
 1.60328 +    return rc;
 1.60329 +  }
 1.60330 +
 1.60331 +  /* If there are any write-transactions at all, invoke the commit hook */
 1.60332 +  if( needXcommit && db->xCommitCallback ){
 1.60333 +    rc = db->xCommitCallback(db->pCommitArg);
 1.60334 +    if( rc ){
 1.60335 +      return SQLITE_CONSTRAINT;
 1.60336 +    }
 1.60337 +  }
 1.60338 +
 1.60339 +  /* The simple case - no more than one database file (not counting the
 1.60340 +  ** TEMP database) has a transaction active.   There is no need for the
 1.60341 +  ** master-journal.
 1.60342 +  **
 1.60343 +  ** If the return value of sqlite3BtreeGetFilename() is a zero length
 1.60344 +  ** string, it means the main database is :memory: or a temp file.  In 
 1.60345 +  ** that case we do not support atomic multi-file commits, so use the 
 1.60346 +  ** simple case then too.
 1.60347 +  */
 1.60348 +  if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
 1.60349 +   || nTrans<=1
 1.60350 +  ){
 1.60351 +    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 1.60352 +      Btree *pBt = db->aDb[i].pBt;
 1.60353 +      if( pBt ){
 1.60354 +        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
 1.60355 +      }
 1.60356 +    }
 1.60357 +
 1.60358 +    /* Do the commit only if all databases successfully complete phase 1. 
 1.60359 +    ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
 1.60360 +    ** IO error while deleting or truncating a journal file. It is unlikely,
 1.60361 +    ** but could happen. In this case abandon processing and return the error.
 1.60362 +    */
 1.60363 +    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 1.60364 +      Btree *pBt = db->aDb[i].pBt;
 1.60365 +      if( pBt ){
 1.60366 +        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
 1.60367 +      }
 1.60368 +    }
 1.60369 +    if( rc==SQLITE_OK ){
 1.60370 +      sqlite3VtabCommit(db);
 1.60371 +    }
 1.60372 +  }
 1.60373 +
 1.60374 +  /* The complex case - There is a multi-file write-transaction active.
 1.60375 +  ** This requires a master journal file to ensure the transaction is
 1.60376 +  ** committed atomicly.
 1.60377 +  */
 1.60378 +#ifndef SQLITE_OMIT_DISKIO
 1.60379 +  else{
 1.60380 +    sqlite3_vfs *pVfs = db->pVfs;
 1.60381 +    int needSync = 0;
 1.60382 +    char *zMaster = 0;   /* File-name for the master journal */
 1.60383 +    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
 1.60384 +    sqlite3_file *pMaster = 0;
 1.60385 +    i64 offset = 0;
 1.60386 +    int res;
 1.60387 +    int retryCount = 0;
 1.60388 +    int nMainFile;
 1.60389 +
 1.60390 +    /* Select a master journal file name */
 1.60391 +    nMainFile = sqlite3Strlen30(zMainFile);
 1.60392 +    zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
 1.60393 +    if( zMaster==0 ) return SQLITE_NOMEM;
 1.60394 +    do {
 1.60395 +      u32 iRandom;
 1.60396 +      if( retryCount ){
 1.60397 +        if( retryCount>100 ){
 1.60398 +          sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
 1.60399 +          sqlite3OsDelete(pVfs, zMaster, 0);
 1.60400 +          break;
 1.60401 +        }else if( retryCount==1 ){
 1.60402 +          sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
 1.60403 +        }
 1.60404 +      }
 1.60405 +      retryCount++;
 1.60406 +      sqlite3_randomness(sizeof(iRandom), &iRandom);
 1.60407 +      sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
 1.60408 +                               (iRandom>>8)&0xffffff, iRandom&0xff);
 1.60409 +      /* The antipenultimate character of the master journal name must
 1.60410 +      ** be "9" to avoid name collisions when using 8+3 filenames. */
 1.60411 +      assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
 1.60412 +      sqlite3FileSuffix3(zMainFile, zMaster);
 1.60413 +      rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
 1.60414 +    }while( rc==SQLITE_OK && res );
 1.60415 +    if( rc==SQLITE_OK ){
 1.60416 +      /* Open the master journal. */
 1.60417 +      rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
 1.60418 +          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
 1.60419 +          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
 1.60420 +      );
 1.60421 +    }
 1.60422 +    if( rc!=SQLITE_OK ){
 1.60423 +      sqlite3DbFree(db, zMaster);
 1.60424 +      return rc;
 1.60425 +    }
 1.60426 + 
 1.60427 +    /* Write the name of each database file in the transaction into the new
 1.60428 +    ** master journal file. If an error occurs at this point close
 1.60429 +    ** and delete the master journal file. All the individual journal files
 1.60430 +    ** still have 'null' as the master journal pointer, so they will roll
 1.60431 +    ** back independently if a failure occurs.
 1.60432 +    */
 1.60433 +    for(i=0; i<db->nDb; i++){
 1.60434 +      Btree *pBt = db->aDb[i].pBt;
 1.60435 +      if( sqlite3BtreeIsInTrans(pBt) ){
 1.60436 +        char const *zFile = sqlite3BtreeGetJournalname(pBt);
 1.60437 +        if( zFile==0 ){
 1.60438 +          continue;  /* Ignore TEMP and :memory: databases */
 1.60439 +        }
 1.60440 +        assert( zFile[0]!=0 );
 1.60441 +        if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
 1.60442 +          needSync = 1;
 1.60443 +        }
 1.60444 +        rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
 1.60445 +        offset += sqlite3Strlen30(zFile)+1;
 1.60446 +        if( rc!=SQLITE_OK ){
 1.60447 +          sqlite3OsCloseFree(pMaster);
 1.60448 +          sqlite3OsDelete(pVfs, zMaster, 0);
 1.60449 +          sqlite3DbFree(db, zMaster);
 1.60450 +          return rc;
 1.60451 +        }
 1.60452 +      }
 1.60453 +    }
 1.60454 +
 1.60455 +    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
 1.60456 +    ** flag is set this is not required.
 1.60457 +    */
 1.60458 +    if( needSync 
 1.60459 +     && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
 1.60460 +     && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
 1.60461 +    ){
 1.60462 +      sqlite3OsCloseFree(pMaster);
 1.60463 +      sqlite3OsDelete(pVfs, zMaster, 0);
 1.60464 +      sqlite3DbFree(db, zMaster);
 1.60465 +      return rc;
 1.60466 +    }
 1.60467 +
 1.60468 +    /* Sync all the db files involved in the transaction. The same call
 1.60469 +    ** sets the master journal pointer in each individual journal. If
 1.60470 +    ** an error occurs here, do not delete the master journal file.
 1.60471 +    **
 1.60472 +    ** If the error occurs during the first call to
 1.60473 +    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
 1.60474 +    ** master journal file will be orphaned. But we cannot delete it,
 1.60475 +    ** in case the master journal file name was written into the journal
 1.60476 +    ** file before the failure occurred.
 1.60477 +    */
 1.60478 +    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
 1.60479 +      Btree *pBt = db->aDb[i].pBt;
 1.60480 +      if( pBt ){
 1.60481 +        rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
 1.60482 +      }
 1.60483 +    }
 1.60484 +    sqlite3OsCloseFree(pMaster);
 1.60485 +    assert( rc!=SQLITE_BUSY );
 1.60486 +    if( rc!=SQLITE_OK ){
 1.60487 +      sqlite3DbFree(db, zMaster);
 1.60488 +      return rc;
 1.60489 +    }
 1.60490 +
 1.60491 +    /* Delete the master journal file. This commits the transaction. After
 1.60492 +    ** doing this the directory is synced again before any individual
 1.60493 +    ** transaction files are deleted.
 1.60494 +    */
 1.60495 +    rc = sqlite3OsDelete(pVfs, zMaster, 1);
 1.60496 +    sqlite3DbFree(db, zMaster);
 1.60497 +    zMaster = 0;
 1.60498 +    if( rc ){
 1.60499 +      return rc;
 1.60500 +    }
 1.60501 +
 1.60502 +    /* All files and directories have already been synced, so the following
 1.60503 +    ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
 1.60504 +    ** deleting or truncating journals. If something goes wrong while
 1.60505 +    ** this is happening we don't really care. The integrity of the
 1.60506 +    ** transaction is already guaranteed, but some stray 'cold' journals
 1.60507 +    ** may be lying around. Returning an error code won't help matters.
 1.60508 +    */
 1.60509 +    disable_simulated_io_errors();
 1.60510 +    sqlite3BeginBenignMalloc();
 1.60511 +    for(i=0; i<db->nDb; i++){ 
 1.60512 +      Btree *pBt = db->aDb[i].pBt;
 1.60513 +      if( pBt ){
 1.60514 +        sqlite3BtreeCommitPhaseTwo(pBt, 1);
 1.60515 +      }
 1.60516 +    }
 1.60517 +    sqlite3EndBenignMalloc();
 1.60518 +    enable_simulated_io_errors();
 1.60519 +
 1.60520 +    sqlite3VtabCommit(db);
 1.60521 +  }
 1.60522 +#endif
 1.60523 +
 1.60524 +  return rc;
 1.60525 +}
 1.60526 +
 1.60527 +/* 
 1.60528 +** This routine checks that the sqlite3.activeVdbeCnt count variable
 1.60529 +** matches the number of vdbe's in the list sqlite3.pVdbe that are
 1.60530 +** currently active. An assertion fails if the two counts do not match.
 1.60531 +** This is an internal self-check only - it is not an essential processing
 1.60532 +** step.
 1.60533 +**
 1.60534 +** This is a no-op if NDEBUG is defined.
 1.60535 +*/
 1.60536 +#ifndef NDEBUG
 1.60537 +static void checkActiveVdbeCnt(sqlite3 *db){
 1.60538 +  Vdbe *p;
 1.60539 +  int cnt = 0;
 1.60540 +  int nWrite = 0;
 1.60541 +  p = db->pVdbe;
 1.60542 +  while( p ){
 1.60543 +    if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
 1.60544 +      cnt++;
 1.60545 +      if( p->readOnly==0 ) nWrite++;
 1.60546 +    }
 1.60547 +    p = p->pNext;
 1.60548 +  }
 1.60549 +  assert( cnt==db->activeVdbeCnt );
 1.60550 +  assert( nWrite==db->writeVdbeCnt );
 1.60551 +}
 1.60552 +#else
 1.60553 +#define checkActiveVdbeCnt(x)
 1.60554 +#endif
 1.60555 +
 1.60556 +/*
 1.60557 +** If the Vdbe passed as the first argument opened a statement-transaction,
 1.60558 +** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
 1.60559 +** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
 1.60560 +** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
 1.60561 +** statement transaction is commtted.
 1.60562 +**
 1.60563 +** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
 1.60564 +** Otherwise SQLITE_OK.
 1.60565 +*/
 1.60566 +SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
 1.60567 +  sqlite3 *const db = p->db;
 1.60568 +  int rc = SQLITE_OK;
 1.60569 +
 1.60570 +  /* If p->iStatement is greater than zero, then this Vdbe opened a 
 1.60571 +  ** statement transaction that should be closed here. The only exception
 1.60572 +  ** is that an IO error may have occured, causing an emergency rollback.
 1.60573 +  ** In this case (db->nStatement==0), and there is nothing to do.
 1.60574 +  */
 1.60575 +  if( db->nStatement && p->iStatement ){
 1.60576 +    int i;
 1.60577 +    const int iSavepoint = p->iStatement-1;
 1.60578 +
 1.60579 +    assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
 1.60580 +    assert( db->nStatement>0 );
 1.60581 +    assert( p->iStatement==(db->nStatement+db->nSavepoint) );
 1.60582 +
 1.60583 +    for(i=0; i<db->nDb; i++){ 
 1.60584 +      int rc2 = SQLITE_OK;
 1.60585 +      Btree *pBt = db->aDb[i].pBt;
 1.60586 +      if( pBt ){
 1.60587 +        if( eOp==SAVEPOINT_ROLLBACK ){
 1.60588 +          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
 1.60589 +        }
 1.60590 +        if( rc2==SQLITE_OK ){
 1.60591 +          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
 1.60592 +        }
 1.60593 +        if( rc==SQLITE_OK ){
 1.60594 +          rc = rc2;
 1.60595 +        }
 1.60596 +      }
 1.60597 +    }
 1.60598 +    db->nStatement--;
 1.60599 +    p->iStatement = 0;
 1.60600 +
 1.60601 +    if( rc==SQLITE_OK ){
 1.60602 +      if( eOp==SAVEPOINT_ROLLBACK ){
 1.60603 +        rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
 1.60604 +      }
 1.60605 +      if( rc==SQLITE_OK ){
 1.60606 +        rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
 1.60607 +      }
 1.60608 +    }
 1.60609 +
 1.60610 +    /* If the statement transaction is being rolled back, also restore the 
 1.60611 +    ** database handles deferred constraint counter to the value it had when 
 1.60612 +    ** the statement transaction was opened.  */
 1.60613 +    if( eOp==SAVEPOINT_ROLLBACK ){
 1.60614 +      db->nDeferredCons = p->nStmtDefCons;
 1.60615 +    }
 1.60616 +  }
 1.60617 +  return rc;
 1.60618 +}
 1.60619 +
 1.60620 +/*
 1.60621 +** This function is called when a transaction opened by the database 
 1.60622 +** handle associated with the VM passed as an argument is about to be 
 1.60623 +** committed. If there are outstanding deferred foreign key constraint
 1.60624 +** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
 1.60625 +**
 1.60626 +** If there are outstanding FK violations and this function returns 
 1.60627 +** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
 1.60628 +** an error message to it. Then return SQLITE_ERROR.
 1.60629 +*/
 1.60630 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.60631 +SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
 1.60632 +  sqlite3 *db = p->db;
 1.60633 +  if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
 1.60634 +    p->rc = SQLITE_CONSTRAINT;
 1.60635 +    p->errorAction = OE_Abort;
 1.60636 +    sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
 1.60637 +    return SQLITE_ERROR;
 1.60638 +  }
 1.60639 +  return SQLITE_OK;
 1.60640 +}
 1.60641 +#endif
 1.60642 +
 1.60643 +/*
 1.60644 +** This routine is called the when a VDBE tries to halt.  If the VDBE
 1.60645 +** has made changes and is in autocommit mode, then commit those
 1.60646 +** changes.  If a rollback is needed, then do the rollback.
 1.60647 +**
 1.60648 +** This routine is the only way to move the state of a VM from
 1.60649 +** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
 1.60650 +** call this on a VM that is in the SQLITE_MAGIC_HALT state.
 1.60651 +**
 1.60652 +** Return an error code.  If the commit could not complete because of
 1.60653 +** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
 1.60654 +** means the close did not happen and needs to be repeated.
 1.60655 +*/
 1.60656 +SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
 1.60657 +  int rc;                         /* Used to store transient return codes */
 1.60658 +  sqlite3 *db = p->db;
 1.60659 +
 1.60660 +  /* This function contains the logic that determines if a statement or
 1.60661 +  ** transaction will be committed or rolled back as a result of the
 1.60662 +  ** execution of this virtual machine. 
 1.60663 +  **
 1.60664 +  ** If any of the following errors occur:
 1.60665 +  **
 1.60666 +  **     SQLITE_NOMEM
 1.60667 +  **     SQLITE_IOERR
 1.60668 +  **     SQLITE_FULL
 1.60669 +  **     SQLITE_INTERRUPT
 1.60670 +  **
 1.60671 +  ** Then the internal cache might have been left in an inconsistent
 1.60672 +  ** state.  We need to rollback the statement transaction, if there is
 1.60673 +  ** one, or the complete transaction if there is no statement transaction.
 1.60674 +  */
 1.60675 +
 1.60676 +  if( p->db->mallocFailed ){
 1.60677 +    p->rc = SQLITE_NOMEM;
 1.60678 +  }
 1.60679 +  if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
 1.60680 +  closeAllCursors(p);
 1.60681 +  if( p->magic!=VDBE_MAGIC_RUN ){
 1.60682 +    return SQLITE_OK;
 1.60683 +  }
 1.60684 +  checkActiveVdbeCnt(db);
 1.60685 +
 1.60686 +  /* No commit or rollback needed if the program never started */
 1.60687 +  if( p->pc>=0 ){
 1.60688 +    int mrc;   /* Primary error code from p->rc */
 1.60689 +    int eStatementOp = 0;
 1.60690 +    int isSpecialError;            /* Set to true if a 'special' error */
 1.60691 +
 1.60692 +    /* Lock all btrees used by the statement */
 1.60693 +    sqlite3VdbeEnter(p);
 1.60694 +
 1.60695 +    /* Check for one of the special errors */
 1.60696 +    mrc = p->rc & 0xff;
 1.60697 +    assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
 1.60698 +    isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
 1.60699 +                     || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
 1.60700 +    if( isSpecialError ){
 1.60701 +      /* If the query was read-only and the error code is SQLITE_INTERRUPT, 
 1.60702 +      ** no rollback is necessary. Otherwise, at least a savepoint 
 1.60703 +      ** transaction must be rolled back to restore the database to a 
 1.60704 +      ** consistent state.
 1.60705 +      **
 1.60706 +      ** Even if the statement is read-only, it is important to perform
 1.60707 +      ** a statement or transaction rollback operation. If the error 
 1.60708 +      ** occured while writing to the journal, sub-journal or database
 1.60709 +      ** file as part of an effort to free up cache space (see function
 1.60710 +      ** pagerStress() in pager.c), the rollback is required to restore 
 1.60711 +      ** the pager to a consistent state.
 1.60712 +      */
 1.60713 +      if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
 1.60714 +        if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
 1.60715 +          eStatementOp = SAVEPOINT_ROLLBACK;
 1.60716 +        }else{
 1.60717 +          /* We are forced to roll back the active transaction. Before doing
 1.60718 +          ** so, abort any other statements this handle currently has active.
 1.60719 +          */
 1.60720 +          sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 1.60721 +          sqlite3CloseSavepoints(db);
 1.60722 +          db->autoCommit = 1;
 1.60723 +        }
 1.60724 +      }
 1.60725 +    }
 1.60726 +
 1.60727 +    /* Check for immediate foreign key violations. */
 1.60728 +    if( p->rc==SQLITE_OK ){
 1.60729 +      sqlite3VdbeCheckFk(p, 0);
 1.60730 +    }
 1.60731 +  
 1.60732 +    /* If the auto-commit flag is set and this is the only active writer 
 1.60733 +    ** VM, then we do either a commit or rollback of the current transaction. 
 1.60734 +    **
 1.60735 +    ** Note: This block also runs if one of the special errors handled 
 1.60736 +    ** above has occurred. 
 1.60737 +    */
 1.60738 +    if( !sqlite3VtabInSync(db) 
 1.60739 +     && db->autoCommit 
 1.60740 +     && db->writeVdbeCnt==(p->readOnly==0) 
 1.60741 +    ){
 1.60742 +      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
 1.60743 +        rc = sqlite3VdbeCheckFk(p, 1);
 1.60744 +        if( rc!=SQLITE_OK ){
 1.60745 +          if( NEVER(p->readOnly) ){
 1.60746 +            sqlite3VdbeLeave(p);
 1.60747 +            return SQLITE_ERROR;
 1.60748 +          }
 1.60749 +          rc = SQLITE_CONSTRAINT;
 1.60750 +        }else{ 
 1.60751 +          /* The auto-commit flag is true, the vdbe program was successful 
 1.60752 +          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
 1.60753 +          ** key constraints to hold up the transaction. This means a commit 
 1.60754 +          ** is required. */
 1.60755 +          rc = vdbeCommit(db, p);
 1.60756 +        }
 1.60757 +        if( rc==SQLITE_BUSY && p->readOnly ){
 1.60758 +          sqlite3VdbeLeave(p);
 1.60759 +          return SQLITE_BUSY;
 1.60760 +        }else if( rc!=SQLITE_OK ){
 1.60761 +          p->rc = rc;
 1.60762 +          sqlite3RollbackAll(db, SQLITE_OK);
 1.60763 +        }else{
 1.60764 +          db->nDeferredCons = 0;
 1.60765 +          sqlite3CommitInternalChanges(db);
 1.60766 +        }
 1.60767 +      }else{
 1.60768 +        sqlite3RollbackAll(db, SQLITE_OK);
 1.60769 +      }
 1.60770 +      db->nStatement = 0;
 1.60771 +    }else if( eStatementOp==0 ){
 1.60772 +      if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
 1.60773 +        eStatementOp = SAVEPOINT_RELEASE;
 1.60774 +      }else if( p->errorAction==OE_Abort ){
 1.60775 +        eStatementOp = SAVEPOINT_ROLLBACK;
 1.60776 +      }else{
 1.60777 +        sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 1.60778 +        sqlite3CloseSavepoints(db);
 1.60779 +        db->autoCommit = 1;
 1.60780 +      }
 1.60781 +    }
 1.60782 +  
 1.60783 +    /* If eStatementOp is non-zero, then a statement transaction needs to
 1.60784 +    ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
 1.60785 +    ** do so. If this operation returns an error, and the current statement
 1.60786 +    ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
 1.60787 +    ** current statement error code.
 1.60788 +    */
 1.60789 +    if( eStatementOp ){
 1.60790 +      rc = sqlite3VdbeCloseStatement(p, eStatementOp);
 1.60791 +      if( rc ){
 1.60792 +        if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
 1.60793 +          p->rc = rc;
 1.60794 +          sqlite3DbFree(db, p->zErrMsg);
 1.60795 +          p->zErrMsg = 0;
 1.60796 +        }
 1.60797 +        sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 1.60798 +        sqlite3CloseSavepoints(db);
 1.60799 +        db->autoCommit = 1;
 1.60800 +      }
 1.60801 +    }
 1.60802 +  
 1.60803 +    /* If this was an INSERT, UPDATE or DELETE and no statement transaction
 1.60804 +    ** has been rolled back, update the database connection change-counter. 
 1.60805 +    */
 1.60806 +    if( p->changeCntOn ){
 1.60807 +      if( eStatementOp!=SAVEPOINT_ROLLBACK ){
 1.60808 +        sqlite3VdbeSetChanges(db, p->nChange);
 1.60809 +      }else{
 1.60810 +        sqlite3VdbeSetChanges(db, 0);
 1.60811 +      }
 1.60812 +      p->nChange = 0;
 1.60813 +    }
 1.60814 +
 1.60815 +    /* Release the locks */
 1.60816 +    sqlite3VdbeLeave(p);
 1.60817 +  }
 1.60818 +
 1.60819 +  /* We have successfully halted and closed the VM.  Record this fact. */
 1.60820 +  if( p->pc>=0 ){
 1.60821 +    db->activeVdbeCnt--;
 1.60822 +    if( !p->readOnly ){
 1.60823 +      db->writeVdbeCnt--;
 1.60824 +    }
 1.60825 +    assert( db->activeVdbeCnt>=db->writeVdbeCnt );
 1.60826 +  }
 1.60827 +  p->magic = VDBE_MAGIC_HALT;
 1.60828 +  checkActiveVdbeCnt(db);
 1.60829 +  if( p->db->mallocFailed ){
 1.60830 +    p->rc = SQLITE_NOMEM;
 1.60831 +  }
 1.60832 +
 1.60833 +  /* If the auto-commit flag is set to true, then any locks that were held
 1.60834 +  ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 
 1.60835 +  ** to invoke any required unlock-notify callbacks.
 1.60836 +  */
 1.60837 +  if( db->autoCommit ){
 1.60838 +    sqlite3ConnectionUnlocked(db);
 1.60839 +  }
 1.60840 +
 1.60841 +  assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
 1.60842 +  return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
 1.60843 +}
 1.60844 +
 1.60845 +
 1.60846 +/*
 1.60847 +** Each VDBE holds the result of the most recent sqlite3_step() call
 1.60848 +** in p->rc.  This routine sets that result back to SQLITE_OK.
 1.60849 +*/
 1.60850 +SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
 1.60851 +  p->rc = SQLITE_OK;
 1.60852 +}
 1.60853 +
 1.60854 +/*
 1.60855 +** Copy the error code and error message belonging to the VDBE passed
 1.60856 +** as the first argument to its database handle (so that they will be 
 1.60857 +** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
 1.60858 +**
 1.60859 +** This function does not clear the VDBE error code or message, just
 1.60860 +** copies them to the database handle.
 1.60861 +*/
 1.60862 +SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
 1.60863 +  sqlite3 *db = p->db;
 1.60864 +  int rc = p->rc;
 1.60865 +  if( p->zErrMsg ){
 1.60866 +    u8 mallocFailed = db->mallocFailed;
 1.60867 +    sqlite3BeginBenignMalloc();
 1.60868 +    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
 1.60869 +    sqlite3EndBenignMalloc();
 1.60870 +    db->mallocFailed = mallocFailed;
 1.60871 +    db->errCode = rc;
 1.60872 +  }else{
 1.60873 +    sqlite3Error(db, rc, 0);
 1.60874 +  }
 1.60875 +  return rc;
 1.60876 +}
 1.60877 +
 1.60878 +#ifdef SQLITE_ENABLE_SQLLOG
 1.60879 +/*
 1.60880 +** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
 1.60881 +** invoke it.
 1.60882 +*/
 1.60883 +static void vdbeInvokeSqllog(Vdbe *v){
 1.60884 +  if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
 1.60885 +    char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
 1.60886 +    assert( v->db->init.busy==0 );
 1.60887 +    if( zExpanded ){
 1.60888 +      sqlite3GlobalConfig.xSqllog(
 1.60889 +          sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
 1.60890 +      );
 1.60891 +      sqlite3DbFree(v->db, zExpanded);
 1.60892 +    }
 1.60893 +  }
 1.60894 +}
 1.60895 +#else
 1.60896 +# define vdbeInvokeSqllog(x)
 1.60897 +#endif
 1.60898 +
 1.60899 +/*
 1.60900 +** Clean up a VDBE after execution but do not delete the VDBE just yet.
 1.60901 +** Write any error messages into *pzErrMsg.  Return the result code.
 1.60902 +**
 1.60903 +** After this routine is run, the VDBE should be ready to be executed
 1.60904 +** again.
 1.60905 +**
 1.60906 +** To look at it another way, this routine resets the state of the
 1.60907 +** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
 1.60908 +** VDBE_MAGIC_INIT.
 1.60909 +*/
 1.60910 +SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
 1.60911 +  sqlite3 *db;
 1.60912 +  db = p->db;
 1.60913 +
 1.60914 +  /* If the VM did not run to completion or if it encountered an
 1.60915 +  ** error, then it might not have been halted properly.  So halt
 1.60916 +  ** it now.
 1.60917 +  */
 1.60918 +  sqlite3VdbeHalt(p);
 1.60919 +
 1.60920 +  /* If the VDBE has be run even partially, then transfer the error code
 1.60921 +  ** and error message from the VDBE into the main database structure.  But
 1.60922 +  ** if the VDBE has just been set to run but has not actually executed any
 1.60923 +  ** instructions yet, leave the main database error information unchanged.
 1.60924 +  */
 1.60925 +  if( p->pc>=0 ){
 1.60926 +    vdbeInvokeSqllog(p);
 1.60927 +    sqlite3VdbeTransferError(p);
 1.60928 +    sqlite3DbFree(db, p->zErrMsg);
 1.60929 +    p->zErrMsg = 0;
 1.60930 +    if( p->runOnlyOnce ) p->expired = 1;
 1.60931 +  }else if( p->rc && p->expired ){
 1.60932 +    /* The expired flag was set on the VDBE before the first call
 1.60933 +    ** to sqlite3_step(). For consistency (since sqlite3_step() was
 1.60934 +    ** called), set the database error in this case as well.
 1.60935 +    */
 1.60936 +    sqlite3Error(db, p->rc, 0);
 1.60937 +    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
 1.60938 +    sqlite3DbFree(db, p->zErrMsg);
 1.60939 +    p->zErrMsg = 0;
 1.60940 +  }
 1.60941 +
 1.60942 +  /* Reclaim all memory used by the VDBE
 1.60943 +  */
 1.60944 +  Cleanup(p);
 1.60945 +
 1.60946 +  /* Save profiling information from this VDBE run.
 1.60947 +  */
 1.60948 +#ifdef VDBE_PROFILE
 1.60949 +  {
 1.60950 +    FILE *out = fopen("vdbe_profile.out", "a");
 1.60951 +    if( out ){
 1.60952 +      int i;
 1.60953 +      fprintf(out, "---- ");
 1.60954 +      for(i=0; i<p->nOp; i++){
 1.60955 +        fprintf(out, "%02x", p->aOp[i].opcode);
 1.60956 +      }
 1.60957 +      fprintf(out, "\n");
 1.60958 +      for(i=0; i<p->nOp; i++){
 1.60959 +        fprintf(out, "%6d %10lld %8lld ",
 1.60960 +           p->aOp[i].cnt,
 1.60961 +           p->aOp[i].cycles,
 1.60962 +           p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
 1.60963 +        );
 1.60964 +        sqlite3VdbePrintOp(out, i, &p->aOp[i]);
 1.60965 +      }
 1.60966 +      fclose(out);
 1.60967 +    }
 1.60968 +  }
 1.60969 +#endif
 1.60970 +  p->magic = VDBE_MAGIC_INIT;
 1.60971 +  return p->rc & db->errMask;
 1.60972 +}
 1.60973 + 
 1.60974 +/*
 1.60975 +** Clean up and delete a VDBE after execution.  Return an integer which is
 1.60976 +** the result code.  Write any error message text into *pzErrMsg.
 1.60977 +*/
 1.60978 +SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
 1.60979 +  int rc = SQLITE_OK;
 1.60980 +  if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
 1.60981 +    rc = sqlite3VdbeReset(p);
 1.60982 +    assert( (rc & p->db->errMask)==rc );
 1.60983 +  }
 1.60984 +  sqlite3VdbeDelete(p);
 1.60985 +  return rc;
 1.60986 +}
 1.60987 +
 1.60988 +/*
 1.60989 +** Call the destructor for each auxdata entry in pVdbeFunc for which
 1.60990 +** the corresponding bit in mask is clear.  Auxdata entries beyond 31
 1.60991 +** are always destroyed.  To destroy all auxdata entries, call this
 1.60992 +** routine with mask==0.
 1.60993 +*/
 1.60994 +SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
 1.60995 +  int i;
 1.60996 +  for(i=0; i<pVdbeFunc->nAux; i++){
 1.60997 +    struct AuxData *pAux = &pVdbeFunc->apAux[i];
 1.60998 +    if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
 1.60999 +      if( pAux->xDelete ){
 1.61000 +        pAux->xDelete(pAux->pAux);
 1.61001 +      }
 1.61002 +      pAux->pAux = 0;
 1.61003 +    }
 1.61004 +  }
 1.61005 +}
 1.61006 +
 1.61007 +/*
 1.61008 +** Free all memory associated with the Vdbe passed as the second argument,
 1.61009 +** except for object itself, which is preserved.
 1.61010 +**
 1.61011 +** The difference between this function and sqlite3VdbeDelete() is that
 1.61012 +** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
 1.61013 +** the database connection and frees the object itself.
 1.61014 +*/
 1.61015 +SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
 1.61016 +  SubProgram *pSub, *pNext;
 1.61017 +  int i;
 1.61018 +  assert( p->db==0 || p->db==db );
 1.61019 +  releaseMemArray(p->aVar, p->nVar);
 1.61020 +  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 1.61021 +  for(pSub=p->pProgram; pSub; pSub=pNext){
 1.61022 +    pNext = pSub->pNext;
 1.61023 +    vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
 1.61024 +    sqlite3DbFree(db, pSub);
 1.61025 +  }
 1.61026 +  for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
 1.61027 +  vdbeFreeOpArray(db, p->aOp, p->nOp);
 1.61028 +  sqlite3DbFree(db, p->aLabel);
 1.61029 +  sqlite3DbFree(db, p->aColName);
 1.61030 +  sqlite3DbFree(db, p->zSql);
 1.61031 +  sqlite3DbFree(db, p->pFree);
 1.61032 +#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 1.61033 +  sqlite3_free(p->zExplain);
 1.61034 +  sqlite3DbFree(db, p->pExplain);
 1.61035 +#endif
 1.61036 +}
 1.61037 +
 1.61038 +/*
 1.61039 +** Delete an entire VDBE.
 1.61040 +*/
 1.61041 +SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
 1.61042 +  sqlite3 *db;
 1.61043 +
 1.61044 +  if( NEVER(p==0) ) return;
 1.61045 +  db = p->db;
 1.61046 +  assert( sqlite3_mutex_held(db->mutex) );
 1.61047 +  sqlite3VdbeClearObject(db, p);
 1.61048 +  if( p->pPrev ){
 1.61049 +    p->pPrev->pNext = p->pNext;
 1.61050 +  }else{
 1.61051 +    assert( db->pVdbe==p );
 1.61052 +    db->pVdbe = p->pNext;
 1.61053 +  }
 1.61054 +  if( p->pNext ){
 1.61055 +    p->pNext->pPrev = p->pPrev;
 1.61056 +  }
 1.61057 +  p->magic = VDBE_MAGIC_DEAD;
 1.61058 +  p->db = 0;
 1.61059 +  sqlite3DbFree(db, p);
 1.61060 +}
 1.61061 +
 1.61062 +/*
 1.61063 +** Make sure the cursor p is ready to read or write the row to which it
 1.61064 +** was last positioned.  Return an error code if an OOM fault or I/O error
 1.61065 +** prevents us from positioning the cursor to its correct position.
 1.61066 +**
 1.61067 +** If a MoveTo operation is pending on the given cursor, then do that
 1.61068 +** MoveTo now.  If no move is pending, check to see if the row has been
 1.61069 +** deleted out from under the cursor and if it has, mark the row as
 1.61070 +** a NULL row.
 1.61071 +**
 1.61072 +** If the cursor is already pointing to the correct row and that row has
 1.61073 +** not been deleted out from under the cursor, then this routine is a no-op.
 1.61074 +*/
 1.61075 +SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
 1.61076 +  if( p->deferredMoveto ){
 1.61077 +    int res, rc;
 1.61078 +#ifdef SQLITE_TEST
 1.61079 +    extern int sqlite3_search_count;
 1.61080 +#endif
 1.61081 +    assert( p->isTable );
 1.61082 +    rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
 1.61083 +    if( rc ) return rc;
 1.61084 +    p->lastRowid = p->movetoTarget;
 1.61085 +    if( res!=0 ) return SQLITE_CORRUPT_BKPT;
 1.61086 +    p->rowidIsValid = 1;
 1.61087 +#ifdef SQLITE_TEST
 1.61088 +    sqlite3_search_count++;
 1.61089 +#endif
 1.61090 +    p->deferredMoveto = 0;
 1.61091 +    p->cacheStatus = CACHE_STALE;
 1.61092 +  }else if( ALWAYS(p->pCursor) ){
 1.61093 +    int hasMoved;
 1.61094 +    int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
 1.61095 +    if( rc ) return rc;
 1.61096 +    if( hasMoved ){
 1.61097 +      p->cacheStatus = CACHE_STALE;
 1.61098 +      p->nullRow = 1;
 1.61099 +    }
 1.61100 +  }
 1.61101 +  return SQLITE_OK;
 1.61102 +}
 1.61103 +
 1.61104 +/*
 1.61105 +** The following functions:
 1.61106 +**
 1.61107 +** sqlite3VdbeSerialType()
 1.61108 +** sqlite3VdbeSerialTypeLen()
 1.61109 +** sqlite3VdbeSerialLen()
 1.61110 +** sqlite3VdbeSerialPut()
 1.61111 +** sqlite3VdbeSerialGet()
 1.61112 +**
 1.61113 +** encapsulate the code that serializes values for storage in SQLite
 1.61114 +** data and index records. Each serialized value consists of a
 1.61115 +** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
 1.61116 +** integer, stored as a varint.
 1.61117 +**
 1.61118 +** In an SQLite index record, the serial type is stored directly before
 1.61119 +** the blob of data that it corresponds to. In a table record, all serial
 1.61120 +** types are stored at the start of the record, and the blobs of data at
 1.61121 +** the end. Hence these functions allow the caller to handle the
 1.61122 +** serial-type and data blob seperately.
 1.61123 +**
 1.61124 +** The following table describes the various storage classes for data:
 1.61125 +**
 1.61126 +**   serial type        bytes of data      type
 1.61127 +**   --------------     ---------------    ---------------
 1.61128 +**      0                     0            NULL
 1.61129 +**      1                     1            signed integer
 1.61130 +**      2                     2            signed integer
 1.61131 +**      3                     3            signed integer
 1.61132 +**      4                     4            signed integer
 1.61133 +**      5                     6            signed integer
 1.61134 +**      6                     8            signed integer
 1.61135 +**      7                     8            IEEE float
 1.61136 +**      8                     0            Integer constant 0
 1.61137 +**      9                     0            Integer constant 1
 1.61138 +**     10,11                               reserved for expansion
 1.61139 +**    N>=12 and even       (N-12)/2        BLOB
 1.61140 +**    N>=13 and odd        (N-13)/2        text
 1.61141 +**
 1.61142 +** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
 1.61143 +** of SQLite will not understand those serial types.
 1.61144 +*/
 1.61145 +
 1.61146 +/*
 1.61147 +** Return the serial-type for the value stored in pMem.
 1.61148 +*/
 1.61149 +SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
 1.61150 +  int flags = pMem->flags;
 1.61151 +  int n;
 1.61152 +
 1.61153 +  if( flags&MEM_Null ){
 1.61154 +    return 0;
 1.61155 +  }
 1.61156 +  if( flags&MEM_Int ){
 1.61157 +    /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
 1.61158 +#   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
 1.61159 +    i64 i = pMem->u.i;
 1.61160 +    u64 u;
 1.61161 +    if( i<0 ){
 1.61162 +      if( i<(-MAX_6BYTE) ) return 6;
 1.61163 +      /* Previous test prevents:  u = -(-9223372036854775808) */
 1.61164 +      u = -i;
 1.61165 +    }else{
 1.61166 +      u = i;
 1.61167 +    }
 1.61168 +    if( u<=127 ){
 1.61169 +      return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
 1.61170 +    }
 1.61171 +    if( u<=32767 ) return 2;
 1.61172 +    if( u<=8388607 ) return 3;
 1.61173 +    if( u<=2147483647 ) return 4;
 1.61174 +    if( u<=MAX_6BYTE ) return 5;
 1.61175 +    return 6;
 1.61176 +  }
 1.61177 +  if( flags&MEM_Real ){
 1.61178 +    return 7;
 1.61179 +  }
 1.61180 +  assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
 1.61181 +  n = pMem->n;
 1.61182 +  if( flags & MEM_Zero ){
 1.61183 +    n += pMem->u.nZero;
 1.61184 +  }
 1.61185 +  assert( n>=0 );
 1.61186 +  return ((n*2) + 12 + ((flags&MEM_Str)!=0));
 1.61187 +}
 1.61188 +
 1.61189 +/*
 1.61190 +** Return the length of the data corresponding to the supplied serial-type.
 1.61191 +*/
 1.61192 +SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
 1.61193 +  if( serial_type>=12 ){
 1.61194 +    return (serial_type-12)/2;
 1.61195 +  }else{
 1.61196 +    static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
 1.61197 +    return aSize[serial_type];
 1.61198 +  }
 1.61199 +}
 1.61200 +
 1.61201 +/*
 1.61202 +** If we are on an architecture with mixed-endian floating 
 1.61203 +** points (ex: ARM7) then swap the lower 4 bytes with the 
 1.61204 +** upper 4 bytes.  Return the result.
 1.61205 +**
 1.61206 +** For most architectures, this is a no-op.
 1.61207 +**
 1.61208 +** (later):  It is reported to me that the mixed-endian problem
 1.61209 +** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
 1.61210 +** that early versions of GCC stored the two words of a 64-bit
 1.61211 +** float in the wrong order.  And that error has been propagated
 1.61212 +** ever since.  The blame is not necessarily with GCC, though.
 1.61213 +** GCC might have just copying the problem from a prior compiler.
 1.61214 +** I am also told that newer versions of GCC that follow a different
 1.61215 +** ABI get the byte order right.
 1.61216 +**
 1.61217 +** Developers using SQLite on an ARM7 should compile and run their
 1.61218 +** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
 1.61219 +** enabled, some asserts below will ensure that the byte order of
 1.61220 +** floating point values is correct.
 1.61221 +**
 1.61222 +** (2007-08-30)  Frank van Vugt has studied this problem closely
 1.61223 +** and has send his findings to the SQLite developers.  Frank
 1.61224 +** writes that some Linux kernels offer floating point hardware
 1.61225 +** emulation that uses only 32-bit mantissas instead of a full 
 1.61226 +** 48-bits as required by the IEEE standard.  (This is the
 1.61227 +** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
 1.61228 +** byte swapping becomes very complicated.  To avoid problems,
 1.61229 +** the necessary byte swapping is carried out using a 64-bit integer
 1.61230 +** rather than a 64-bit float.  Frank assures us that the code here
 1.61231 +** works for him.  We, the developers, have no way to independently
 1.61232 +** verify this, but Frank seems to know what he is talking about
 1.61233 +** so we trust him.
 1.61234 +*/
 1.61235 +#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 1.61236 +static u64 floatSwap(u64 in){
 1.61237 +  union {
 1.61238 +    u64 r;
 1.61239 +    u32 i[2];
 1.61240 +  } u;
 1.61241 +  u32 t;
 1.61242 +
 1.61243 +  u.r = in;
 1.61244 +  t = u.i[0];
 1.61245 +  u.i[0] = u.i[1];
 1.61246 +  u.i[1] = t;
 1.61247 +  return u.r;
 1.61248 +}
 1.61249 +# define swapMixedEndianFloat(X)  X = floatSwap(X)
 1.61250 +#else
 1.61251 +# define swapMixedEndianFloat(X)
 1.61252 +#endif
 1.61253 +
 1.61254 +/*
 1.61255 +** Write the serialized data blob for the value stored in pMem into 
 1.61256 +** buf. It is assumed that the caller has allocated sufficient space.
 1.61257 +** Return the number of bytes written.
 1.61258 +**
 1.61259 +** nBuf is the amount of space left in buf[].  nBuf must always be
 1.61260 +** large enough to hold the entire field.  Except, if the field is
 1.61261 +** a blob with a zero-filled tail, then buf[] might be just the right
 1.61262 +** size to hold everything except for the zero-filled tail.  If buf[]
 1.61263 +** is only big enough to hold the non-zero prefix, then only write that
 1.61264 +** prefix into buf[].  But if buf[] is large enough to hold both the
 1.61265 +** prefix and the tail then write the prefix and set the tail to all
 1.61266 +** zeros.
 1.61267 +**
 1.61268 +** Return the number of bytes actually written into buf[].  The number
 1.61269 +** of bytes in the zero-filled tail is included in the return value only
 1.61270 +** if those bytes were zeroed in buf[].
 1.61271 +*/ 
 1.61272 +SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
 1.61273 +  u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
 1.61274 +  u32 len;
 1.61275 +
 1.61276 +  /* Integer and Real */
 1.61277 +  if( serial_type<=7 && serial_type>0 ){
 1.61278 +    u64 v;
 1.61279 +    u32 i;
 1.61280 +    if( serial_type==7 ){
 1.61281 +      assert( sizeof(v)==sizeof(pMem->r) );
 1.61282 +      memcpy(&v, &pMem->r, sizeof(v));
 1.61283 +      swapMixedEndianFloat(v);
 1.61284 +    }else{
 1.61285 +      v = pMem->u.i;
 1.61286 +    }
 1.61287 +    len = i = sqlite3VdbeSerialTypeLen(serial_type);
 1.61288 +    assert( len<=(u32)nBuf );
 1.61289 +    while( i-- ){
 1.61290 +      buf[i] = (u8)(v&0xFF);
 1.61291 +      v >>= 8;
 1.61292 +    }
 1.61293 +    return len;
 1.61294 +  }
 1.61295 +
 1.61296 +  /* String or blob */
 1.61297 +  if( serial_type>=12 ){
 1.61298 +    assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
 1.61299 +             == (int)sqlite3VdbeSerialTypeLen(serial_type) );
 1.61300 +    assert( pMem->n<=nBuf );
 1.61301 +    len = pMem->n;
 1.61302 +    memcpy(buf, pMem->z, len);
 1.61303 +    if( pMem->flags & MEM_Zero ){
 1.61304 +      len += pMem->u.nZero;
 1.61305 +      assert( nBuf>=0 );
 1.61306 +      if( len > (u32)nBuf ){
 1.61307 +        len = (u32)nBuf;
 1.61308 +      }
 1.61309 +      memset(&buf[pMem->n], 0, len-pMem->n);
 1.61310 +    }
 1.61311 +    return len;
 1.61312 +  }
 1.61313 +
 1.61314 +  /* NULL or constants 0 or 1 */
 1.61315 +  return 0;
 1.61316 +}
 1.61317 +
 1.61318 +/*
 1.61319 +** Deserialize the data blob pointed to by buf as serial type serial_type
 1.61320 +** and store the result in pMem.  Return the number of bytes read.
 1.61321 +*/ 
 1.61322 +SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
 1.61323 +  const unsigned char *buf,     /* Buffer to deserialize from */
 1.61324 +  u32 serial_type,              /* Serial type to deserialize */
 1.61325 +  Mem *pMem                     /* Memory cell to write value into */
 1.61326 +){
 1.61327 +  switch( serial_type ){
 1.61328 +    case 10:   /* Reserved for future use */
 1.61329 +    case 11:   /* Reserved for future use */
 1.61330 +    case 0: {  /* NULL */
 1.61331 +      pMem->flags = MEM_Null;
 1.61332 +      break;
 1.61333 +    }
 1.61334 +    case 1: { /* 1-byte signed integer */
 1.61335 +      pMem->u.i = (signed char)buf[0];
 1.61336 +      pMem->flags = MEM_Int;
 1.61337 +      return 1;
 1.61338 +    }
 1.61339 +    case 2: { /* 2-byte signed integer */
 1.61340 +      pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
 1.61341 +      pMem->flags = MEM_Int;
 1.61342 +      return 2;
 1.61343 +    }
 1.61344 +    case 3: { /* 3-byte signed integer */
 1.61345 +      pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
 1.61346 +      pMem->flags = MEM_Int;
 1.61347 +      return 3;
 1.61348 +    }
 1.61349 +    case 4: { /* 4-byte signed integer */
 1.61350 +      pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
 1.61351 +      pMem->flags = MEM_Int;
 1.61352 +      return 4;
 1.61353 +    }
 1.61354 +    case 5: { /* 6-byte signed integer */
 1.61355 +      u64 x = (((signed char)buf[0])<<8) | buf[1];
 1.61356 +      u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
 1.61357 +      x = (x<<32) | y;
 1.61358 +      pMem->u.i = *(i64*)&x;
 1.61359 +      pMem->flags = MEM_Int;
 1.61360 +      return 6;
 1.61361 +    }
 1.61362 +    case 6:   /* 8-byte signed integer */
 1.61363 +    case 7: { /* IEEE floating point */
 1.61364 +      u64 x;
 1.61365 +      u32 y;
 1.61366 +#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
 1.61367 +      /* Verify that integers and floating point values use the same
 1.61368 +      ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
 1.61369 +      ** defined that 64-bit floating point values really are mixed
 1.61370 +      ** endian.
 1.61371 +      */
 1.61372 +      static const u64 t1 = ((u64)0x3ff00000)<<32;
 1.61373 +      static const double r1 = 1.0;
 1.61374 +      u64 t2 = t1;
 1.61375 +      swapMixedEndianFloat(t2);
 1.61376 +      assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
 1.61377 +#endif
 1.61378 +
 1.61379 +      x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
 1.61380 +      y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
 1.61381 +      x = (x<<32) | y;
 1.61382 +      if( serial_type==6 ){
 1.61383 +        pMem->u.i = *(i64*)&x;
 1.61384 +        pMem->flags = MEM_Int;
 1.61385 +      }else{
 1.61386 +        assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
 1.61387 +        swapMixedEndianFloat(x);
 1.61388 +        memcpy(&pMem->r, &x, sizeof(x));
 1.61389 +        pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
 1.61390 +      }
 1.61391 +      return 8;
 1.61392 +    }
 1.61393 +    case 8:    /* Integer 0 */
 1.61394 +    case 9: {  /* Integer 1 */
 1.61395 +      pMem->u.i = serial_type-8;
 1.61396 +      pMem->flags = MEM_Int;
 1.61397 +      return 0;
 1.61398 +    }
 1.61399 +    default: {
 1.61400 +      u32 len = (serial_type-12)/2;
 1.61401 +      pMem->z = (char *)buf;
 1.61402 +      pMem->n = len;
 1.61403 +      pMem->xDel = 0;
 1.61404 +      if( serial_type&0x01 ){
 1.61405 +        pMem->flags = MEM_Str | MEM_Ephem;
 1.61406 +      }else{
 1.61407 +        pMem->flags = MEM_Blob | MEM_Ephem;
 1.61408 +      }
 1.61409 +      return len;
 1.61410 +    }
 1.61411 +  }
 1.61412 +  return 0;
 1.61413 +}
 1.61414 +
 1.61415 +/*
 1.61416 +** This routine is used to allocate sufficient space for an UnpackedRecord
 1.61417 +** structure large enough to be used with sqlite3VdbeRecordUnpack() if
 1.61418 +** the first argument is a pointer to KeyInfo structure pKeyInfo.
 1.61419 +**
 1.61420 +** The space is either allocated using sqlite3DbMallocRaw() or from within
 1.61421 +** the unaligned buffer passed via the second and third arguments (presumably
 1.61422 +** stack space). If the former, then *ppFree is set to a pointer that should
 1.61423 +** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
 1.61424 +** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
 1.61425 +** before returning.
 1.61426 +**
 1.61427 +** If an OOM error occurs, NULL is returned.
 1.61428 +*/
 1.61429 +SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
 1.61430 +  KeyInfo *pKeyInfo,              /* Description of the record */
 1.61431 +  char *pSpace,                   /* Unaligned space available */
 1.61432 +  int szSpace,                    /* Size of pSpace[] in bytes */
 1.61433 +  char **ppFree                   /* OUT: Caller should free this pointer */
 1.61434 +){
 1.61435 +  UnpackedRecord *p;              /* Unpacked record to return */
 1.61436 +  int nOff;                       /* Increment pSpace by nOff to align it */
 1.61437 +  int nByte;                      /* Number of bytes required for *p */
 1.61438 +
 1.61439 +  /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
 1.61440 +  ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
 1.61441 +  ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
 1.61442 +  */
 1.61443 +  nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
 1.61444 +  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
 1.61445 +  if( nByte>szSpace+nOff ){
 1.61446 +    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
 1.61447 +    *ppFree = (char *)p;
 1.61448 +    if( !p ) return 0;
 1.61449 +  }else{
 1.61450 +    p = (UnpackedRecord*)&pSpace[nOff];
 1.61451 +    *ppFree = 0;
 1.61452 +  }
 1.61453 +
 1.61454 +  p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
 1.61455 +  assert( pKeyInfo->aSortOrder!=0 );
 1.61456 +  p->pKeyInfo = pKeyInfo;
 1.61457 +  p->nField = pKeyInfo->nField + 1;
 1.61458 +  return p;
 1.61459 +}
 1.61460 +
 1.61461 +/*
 1.61462 +** Given the nKey-byte encoding of a record in pKey[], populate the 
 1.61463 +** UnpackedRecord structure indicated by the fourth argument with the
 1.61464 +** contents of the decoded record.
 1.61465 +*/ 
 1.61466 +SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
 1.61467 +  KeyInfo *pKeyInfo,     /* Information about the record format */
 1.61468 +  int nKey,              /* Size of the binary record */
 1.61469 +  const void *pKey,      /* The binary record */
 1.61470 +  UnpackedRecord *p      /* Populate this structure before returning. */
 1.61471 +){
 1.61472 +  const unsigned char *aKey = (const unsigned char *)pKey;
 1.61473 +  int d; 
 1.61474 +  u32 idx;                        /* Offset in aKey[] to read from */
 1.61475 +  u16 u;                          /* Unsigned loop counter */
 1.61476 +  u32 szHdr;
 1.61477 +  Mem *pMem = p->aMem;
 1.61478 +
 1.61479 +  p->flags = 0;
 1.61480 +  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 1.61481 +  idx = getVarint32(aKey, szHdr);
 1.61482 +  d = szHdr;
 1.61483 +  u = 0;
 1.61484 +  while( idx<szHdr && u<p->nField && d<=nKey ){
 1.61485 +    u32 serial_type;
 1.61486 +
 1.61487 +    idx += getVarint32(&aKey[idx], serial_type);
 1.61488 +    pMem->enc = pKeyInfo->enc;
 1.61489 +    pMem->db = pKeyInfo->db;
 1.61490 +    /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
 1.61491 +    pMem->zMalloc = 0;
 1.61492 +    d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
 1.61493 +    pMem++;
 1.61494 +    u++;
 1.61495 +  }
 1.61496 +  assert( u<=pKeyInfo->nField + 1 );
 1.61497 +  p->nField = u;
 1.61498 +}
 1.61499 +
 1.61500 +/*
 1.61501 +** This function compares the two table rows or index records
 1.61502 +** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
 1.61503 +** or positive integer if key1 is less than, equal to or 
 1.61504 +** greater than key2.  The {nKey1, pKey1} key must be a blob
 1.61505 +** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
 1.61506 +** key must be a parsed key such as obtained from
 1.61507 +** sqlite3VdbeParseRecord.
 1.61508 +**
 1.61509 +** Key1 and Key2 do not have to contain the same number of fields.
 1.61510 +** The key with fewer fields is usually compares less than the 
 1.61511 +** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
 1.61512 +** and the common prefixes are equal, then key1 is less than key2.
 1.61513 +** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
 1.61514 +** equal, then the keys are considered to be equal and
 1.61515 +** the parts beyond the common prefix are ignored.
 1.61516 +*/
 1.61517 +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
 1.61518 +  int nKey1, const void *pKey1, /* Left key */
 1.61519 +  UnpackedRecord *pPKey2        /* Right key */
 1.61520 +){
 1.61521 +  int d1;            /* Offset into aKey[] of next data element */
 1.61522 +  u32 idx1;          /* Offset into aKey[] of next header element */
 1.61523 +  u32 szHdr1;        /* Number of bytes in header */
 1.61524 +  int i = 0;
 1.61525 +  int nField;
 1.61526 +  int rc = 0;
 1.61527 +  const unsigned char *aKey1 = (const unsigned char *)pKey1;
 1.61528 +  KeyInfo *pKeyInfo;
 1.61529 +  Mem mem1;
 1.61530 +
 1.61531 +  pKeyInfo = pPKey2->pKeyInfo;
 1.61532 +  mem1.enc = pKeyInfo->enc;
 1.61533 +  mem1.db = pKeyInfo->db;
 1.61534 +  /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
 1.61535 +  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
 1.61536 +
 1.61537 +  /* Compilers may complain that mem1.u.i is potentially uninitialized.
 1.61538 +  ** We could initialize it, as shown here, to silence those complaints.
 1.61539 +  ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
 1.61540 +  ** the unnecessary initialization has a measurable negative performance
 1.61541 +  ** impact, since this routine is a very high runner.  And so, we choose
 1.61542 +  ** to ignore the compiler warnings and leave this variable uninitialized.
 1.61543 +  */
 1.61544 +  /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
 1.61545 +  
 1.61546 +  idx1 = getVarint32(aKey1, szHdr1);
 1.61547 +  d1 = szHdr1;
 1.61548 +  nField = pKeyInfo->nField;
 1.61549 +  assert( pKeyInfo->aSortOrder!=0 );
 1.61550 +  while( idx1<szHdr1 && i<pPKey2->nField ){
 1.61551 +    u32 serial_type1;
 1.61552 +
 1.61553 +    /* Read the serial types for the next element in each key. */
 1.61554 +    idx1 += getVarint32( aKey1+idx1, serial_type1 );
 1.61555 +    if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
 1.61556 +
 1.61557 +    /* Extract the values to be compared.
 1.61558 +    */
 1.61559 +    d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
 1.61560 +
 1.61561 +    /* Do the comparison
 1.61562 +    */
 1.61563 +    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
 1.61564 +                           i<nField ? pKeyInfo->aColl[i] : 0);
 1.61565 +    if( rc!=0 ){
 1.61566 +      assert( mem1.zMalloc==0 );  /* See comment below */
 1.61567 +
 1.61568 +      /* Invert the result if we are using DESC sort order. */
 1.61569 +      if( i<nField && pKeyInfo->aSortOrder[i] ){
 1.61570 +        rc = -rc;
 1.61571 +      }
 1.61572 +    
 1.61573 +      /* If the PREFIX_SEARCH flag is set and all fields except the final
 1.61574 +      ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
 1.61575 +      ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
 1.61576 +      ** This is used by the OP_IsUnique opcode.
 1.61577 +      */
 1.61578 +      if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
 1.61579 +        assert( idx1==szHdr1 && rc );
 1.61580 +        assert( mem1.flags & MEM_Int );
 1.61581 +        pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
 1.61582 +        pPKey2->rowid = mem1.u.i;
 1.61583 +      }
 1.61584 +    
 1.61585 +      return rc;
 1.61586 +    }
 1.61587 +    i++;
 1.61588 +  }
 1.61589 +
 1.61590 +  /* No memory allocation is ever used on mem1.  Prove this using
 1.61591 +  ** the following assert().  If the assert() fails, it indicates a
 1.61592 +  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
 1.61593 +  */
 1.61594 +  assert( mem1.zMalloc==0 );
 1.61595 +
 1.61596 +  /* rc==0 here means that one of the keys ran out of fields and
 1.61597 +  ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
 1.61598 +  ** flag is set, then break the tie by treating key2 as larger.
 1.61599 +  ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
 1.61600 +  ** are considered to be equal.  Otherwise, the longer key is the 
 1.61601 +  ** larger.  As it happens, the pPKey2 will always be the longer
 1.61602 +  ** if there is a difference.
 1.61603 +  */
 1.61604 +  assert( rc==0 );
 1.61605 +  if( pPKey2->flags & UNPACKED_INCRKEY ){
 1.61606 +    rc = -1;
 1.61607 +  }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
 1.61608 +    /* Leave rc==0 */
 1.61609 +  }else if( idx1<szHdr1 ){
 1.61610 +    rc = 1;
 1.61611 +  }
 1.61612 +  return rc;
 1.61613 +}
 1.61614 + 
 1.61615 +
 1.61616 +/*
 1.61617 +** pCur points at an index entry created using the OP_MakeRecord opcode.
 1.61618 +** Read the rowid (the last field in the record) and store it in *rowid.
 1.61619 +** Return SQLITE_OK if everything works, or an error code otherwise.
 1.61620 +**
 1.61621 +** pCur might be pointing to text obtained from a corrupt database file.
 1.61622 +** So the content cannot be trusted.  Do appropriate checks on the content.
 1.61623 +*/
 1.61624 +SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
 1.61625 +  i64 nCellKey = 0;
 1.61626 +  int rc;
 1.61627 +  u32 szHdr;        /* Size of the header */
 1.61628 +  u32 typeRowid;    /* Serial type of the rowid */
 1.61629 +  u32 lenRowid;     /* Size of the rowid */
 1.61630 +  Mem m, v;
 1.61631 +
 1.61632 +  UNUSED_PARAMETER(db);
 1.61633 +
 1.61634 +  /* Get the size of the index entry.  Only indices entries of less
 1.61635 +  ** than 2GiB are support - anything large must be database corruption.
 1.61636 +  ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
 1.61637 +  ** this code can safely assume that nCellKey is 32-bits  
 1.61638 +  */
 1.61639 +  assert( sqlite3BtreeCursorIsValid(pCur) );
 1.61640 +  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 1.61641 +  assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
 1.61642 +  assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
 1.61643 +
 1.61644 +  /* Read in the complete content of the index entry */
 1.61645 +  memset(&m, 0, sizeof(m));
 1.61646 +  rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
 1.61647 +  if( rc ){
 1.61648 +    return rc;
 1.61649 +  }
 1.61650 +
 1.61651 +  /* The index entry must begin with a header size */
 1.61652 +  (void)getVarint32((u8*)m.z, szHdr);
 1.61653 +  testcase( szHdr==3 );
 1.61654 +  testcase( szHdr==m.n );
 1.61655 +  if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
 1.61656 +    goto idx_rowid_corruption;
 1.61657 +  }
 1.61658 +
 1.61659 +  /* The last field of the index should be an integer - the ROWID.
 1.61660 +  ** Verify that the last entry really is an integer. */
 1.61661 +  (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
 1.61662 +  testcase( typeRowid==1 );
 1.61663 +  testcase( typeRowid==2 );
 1.61664 +  testcase( typeRowid==3 );
 1.61665 +  testcase( typeRowid==4 );
 1.61666 +  testcase( typeRowid==5 );
 1.61667 +  testcase( typeRowid==6 );
 1.61668 +  testcase( typeRowid==8 );
 1.61669 +  testcase( typeRowid==9 );
 1.61670 +  if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
 1.61671 +    goto idx_rowid_corruption;
 1.61672 +  }
 1.61673 +  lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
 1.61674 +  testcase( (u32)m.n==szHdr+lenRowid );
 1.61675 +  if( unlikely((u32)m.n<szHdr+lenRowid) ){
 1.61676 +    goto idx_rowid_corruption;
 1.61677 +  }
 1.61678 +
 1.61679 +  /* Fetch the integer off the end of the index record */
 1.61680 +  sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
 1.61681 +  *rowid = v.u.i;
 1.61682 +  sqlite3VdbeMemRelease(&m);
 1.61683 +  return SQLITE_OK;
 1.61684 +
 1.61685 +  /* Jump here if database corruption is detected after m has been
 1.61686 +  ** allocated.  Free the m object and return SQLITE_CORRUPT. */
 1.61687 +idx_rowid_corruption:
 1.61688 +  testcase( m.zMalloc!=0 );
 1.61689 +  sqlite3VdbeMemRelease(&m);
 1.61690 +  return SQLITE_CORRUPT_BKPT;
 1.61691 +}
 1.61692 +
 1.61693 +/*
 1.61694 +** Compare the key of the index entry that cursor pC is pointing to against
 1.61695 +** the key string in pUnpacked.  Write into *pRes a number
 1.61696 +** that is negative, zero, or positive if pC is less than, equal to,
 1.61697 +** or greater than pUnpacked.  Return SQLITE_OK on success.
 1.61698 +**
 1.61699 +** pUnpacked is either created without a rowid or is truncated so that it
 1.61700 +** omits the rowid at the end.  The rowid at the end of the index entry
 1.61701 +** is ignored as well.  Hence, this routine only compares the prefixes 
 1.61702 +** of the keys prior to the final rowid, not the entire key.
 1.61703 +*/
 1.61704 +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
 1.61705 +  VdbeCursor *pC,             /* The cursor to compare against */
 1.61706 +  UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
 1.61707 +  int *res                    /* Write the comparison result here */
 1.61708 +){
 1.61709 +  i64 nCellKey = 0;
 1.61710 +  int rc;
 1.61711 +  BtCursor *pCur = pC->pCursor;
 1.61712 +  Mem m;
 1.61713 +
 1.61714 +  assert( sqlite3BtreeCursorIsValid(pCur) );
 1.61715 +  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 1.61716 +  assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
 1.61717 +  /* nCellKey will always be between 0 and 0xffffffff because of the say
 1.61718 +  ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
 1.61719 +  if( nCellKey<=0 || nCellKey>0x7fffffff ){
 1.61720 +    *res = 0;
 1.61721 +    return SQLITE_CORRUPT_BKPT;
 1.61722 +  }
 1.61723 +  memset(&m, 0, sizeof(m));
 1.61724 +  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
 1.61725 +  if( rc ){
 1.61726 +    return rc;
 1.61727 +  }
 1.61728 +  assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
 1.61729 +  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
 1.61730 +  sqlite3VdbeMemRelease(&m);
 1.61731 +  return SQLITE_OK;
 1.61732 +}
 1.61733 +
 1.61734 +/*
 1.61735 +** This routine sets the value to be returned by subsequent calls to
 1.61736 +** sqlite3_changes() on the database handle 'db'. 
 1.61737 +*/
 1.61738 +SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
 1.61739 +  assert( sqlite3_mutex_held(db->mutex) );
 1.61740 +  db->nChange = nChange;
 1.61741 +  db->nTotalChange += nChange;
 1.61742 +}
 1.61743 +
 1.61744 +/*
 1.61745 +** Set a flag in the vdbe to update the change counter when it is finalised
 1.61746 +** or reset.
 1.61747 +*/
 1.61748 +SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
 1.61749 +  v->changeCntOn = 1;
 1.61750 +}
 1.61751 +
 1.61752 +/*
 1.61753 +** Mark every prepared statement associated with a database connection
 1.61754 +** as expired.
 1.61755 +**
 1.61756 +** An expired statement means that recompilation of the statement is
 1.61757 +** recommend.  Statements expire when things happen that make their
 1.61758 +** programs obsolete.  Removing user-defined functions or collating
 1.61759 +** sequences, or changing an authorization function are the types of
 1.61760 +** things that make prepared statements obsolete.
 1.61761 +*/
 1.61762 +SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
 1.61763 +  Vdbe *p;
 1.61764 +  for(p = db->pVdbe; p; p=p->pNext){
 1.61765 +    p->expired = 1;
 1.61766 +  }
 1.61767 +}
 1.61768 +
 1.61769 +/*
 1.61770 +** Return the database associated with the Vdbe.
 1.61771 +*/
 1.61772 +SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
 1.61773 +  return v->db;
 1.61774 +}
 1.61775 +
 1.61776 +/*
 1.61777 +** Return a pointer to an sqlite3_value structure containing the value bound
 1.61778 +** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
 1.61779 +** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
 1.61780 +** constants) to the value before returning it.
 1.61781 +**
 1.61782 +** The returned value must be freed by the caller using sqlite3ValueFree().
 1.61783 +*/
 1.61784 +SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
 1.61785 +  assert( iVar>0 );
 1.61786 +  if( v ){
 1.61787 +    Mem *pMem = &v->aVar[iVar-1];
 1.61788 +    if( 0==(pMem->flags & MEM_Null) ){
 1.61789 +      sqlite3_value *pRet = sqlite3ValueNew(v->db);
 1.61790 +      if( pRet ){
 1.61791 +        sqlite3VdbeMemCopy((Mem *)pRet, pMem);
 1.61792 +        sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
 1.61793 +        sqlite3VdbeMemStoreType((Mem *)pRet);
 1.61794 +      }
 1.61795 +      return pRet;
 1.61796 +    }
 1.61797 +  }
 1.61798 +  return 0;
 1.61799 +}
 1.61800 +
 1.61801 +/*
 1.61802 +** Configure SQL variable iVar so that binding a new value to it signals
 1.61803 +** to sqlite3_reoptimize() that re-preparing the statement may result
 1.61804 +** in a better query plan.
 1.61805 +*/
 1.61806 +SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
 1.61807 +  assert( iVar>0 );
 1.61808 +  if( iVar>32 ){
 1.61809 +    v->expmask = 0xffffffff;
 1.61810 +  }else{
 1.61811 +    v->expmask |= ((u32)1 << (iVar-1));
 1.61812 +  }
 1.61813 +}
 1.61814 +
 1.61815 +/************** End of vdbeaux.c *********************************************/
 1.61816 +/************** Begin file vdbeapi.c *****************************************/
 1.61817 +/*
 1.61818 +** 2004 May 26
 1.61819 +**
 1.61820 +** The author disclaims copyright to this source code.  In place of
 1.61821 +** a legal notice, here is a blessing:
 1.61822 +**
 1.61823 +**    May you do good and not evil.
 1.61824 +**    May you find forgiveness for yourself and forgive others.
 1.61825 +**    May you share freely, never taking more than you give.
 1.61826 +**
 1.61827 +*************************************************************************
 1.61828 +**
 1.61829 +** This file contains code use to implement APIs that are part of the
 1.61830 +** VDBE.
 1.61831 +*/
 1.61832 +
 1.61833 +#ifndef SQLITE_OMIT_DEPRECATED
 1.61834 +/*
 1.61835 +** Return TRUE (non-zero) of the statement supplied as an argument needs
 1.61836 +** to be recompiled.  A statement needs to be recompiled whenever the
 1.61837 +** execution environment changes in a way that would alter the program
 1.61838 +** that sqlite3_prepare() generates.  For example, if new functions or
 1.61839 +** collating sequences are registered or if an authorizer function is
 1.61840 +** added or changed.
 1.61841 +*/
 1.61842 +SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
 1.61843 +  Vdbe *p = (Vdbe*)pStmt;
 1.61844 +  return p==0 || p->expired;
 1.61845 +}
 1.61846 +#endif
 1.61847 +
 1.61848 +/*
 1.61849 +** Check on a Vdbe to make sure it has not been finalized.  Log
 1.61850 +** an error and return true if it has been finalized (or is otherwise
 1.61851 +** invalid).  Return false if it is ok.
 1.61852 +*/
 1.61853 +static int vdbeSafety(Vdbe *p){
 1.61854 +  if( p->db==0 ){
 1.61855 +    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
 1.61856 +    return 1;
 1.61857 +  }else{
 1.61858 +    return 0;
 1.61859 +  }
 1.61860 +}
 1.61861 +static int vdbeSafetyNotNull(Vdbe *p){
 1.61862 +  if( p==0 ){
 1.61863 +    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
 1.61864 +    return 1;
 1.61865 +  }else{
 1.61866 +    return vdbeSafety(p);
 1.61867 +  }
 1.61868 +}
 1.61869 +
 1.61870 +/*
 1.61871 +** The following routine destroys a virtual machine that is created by
 1.61872 +** the sqlite3_compile() routine. The integer returned is an SQLITE_
 1.61873 +** success/failure code that describes the result of executing the virtual
 1.61874 +** machine.
 1.61875 +**
 1.61876 +** This routine sets the error code and string returned by
 1.61877 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 1.61878 +*/
 1.61879 +SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
 1.61880 +  int rc;
 1.61881 +  if( pStmt==0 ){
 1.61882 +    /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
 1.61883 +    ** pointer is a harmless no-op. */
 1.61884 +    rc = SQLITE_OK;
 1.61885 +  }else{
 1.61886 +    Vdbe *v = (Vdbe*)pStmt;
 1.61887 +    sqlite3 *db = v->db;
 1.61888 +    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
 1.61889 +    sqlite3_mutex_enter(db->mutex);
 1.61890 +    rc = sqlite3VdbeFinalize(v);
 1.61891 +    rc = sqlite3ApiExit(db, rc);
 1.61892 +    sqlite3LeaveMutexAndCloseZombie(db);
 1.61893 +  }
 1.61894 +  return rc;
 1.61895 +}
 1.61896 +
 1.61897 +/*
 1.61898 +** Terminate the current execution of an SQL statement and reset it
 1.61899 +** back to its starting state so that it can be reused. A success code from
 1.61900 +** the prior execution is returned.
 1.61901 +**
 1.61902 +** This routine sets the error code and string returned by
 1.61903 +** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 1.61904 +*/
 1.61905 +SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
 1.61906 +  int rc;
 1.61907 +  if( pStmt==0 ){
 1.61908 +    rc = SQLITE_OK;
 1.61909 +  }else{
 1.61910 +    Vdbe *v = (Vdbe*)pStmt;
 1.61911 +    sqlite3_mutex_enter(v->db->mutex);
 1.61912 +    rc = sqlite3VdbeReset(v);
 1.61913 +    sqlite3VdbeRewind(v);
 1.61914 +    assert( (rc & (v->db->errMask))==rc );
 1.61915 +    rc = sqlite3ApiExit(v->db, rc);
 1.61916 +    sqlite3_mutex_leave(v->db->mutex);
 1.61917 +  }
 1.61918 +  return rc;
 1.61919 +}
 1.61920 +
 1.61921 +/*
 1.61922 +** Set all the parameters in the compiled SQL statement to NULL.
 1.61923 +*/
 1.61924 +SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
 1.61925 +  int i;
 1.61926 +  int rc = SQLITE_OK;
 1.61927 +  Vdbe *p = (Vdbe*)pStmt;
 1.61928 +#if SQLITE_THREADSAFE
 1.61929 +  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
 1.61930 +#endif
 1.61931 +  sqlite3_mutex_enter(mutex);
 1.61932 +  for(i=0; i<p->nVar; i++){
 1.61933 +    sqlite3VdbeMemRelease(&p->aVar[i]);
 1.61934 +    p->aVar[i].flags = MEM_Null;
 1.61935 +  }
 1.61936 +  if( p->isPrepareV2 && p->expmask ){
 1.61937 +    p->expired = 1;
 1.61938 +  }
 1.61939 +  sqlite3_mutex_leave(mutex);
 1.61940 +  return rc;
 1.61941 +}
 1.61942 +
 1.61943 +
 1.61944 +/**************************** sqlite3_value_  *******************************
 1.61945 +** The following routines extract information from a Mem or sqlite3_value
 1.61946 +** structure.
 1.61947 +*/
 1.61948 +SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
 1.61949 +  Mem *p = (Mem*)pVal;
 1.61950 +  if( p->flags & (MEM_Blob|MEM_Str) ){
 1.61951 +    sqlite3VdbeMemExpandBlob(p);
 1.61952 +    p->flags &= ~MEM_Str;
 1.61953 +    p->flags |= MEM_Blob;
 1.61954 +    return p->n ? p->z : 0;
 1.61955 +  }else{
 1.61956 +    return sqlite3_value_text(pVal);
 1.61957 +  }
 1.61958 +}
 1.61959 +SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
 1.61960 +  return sqlite3ValueBytes(pVal, SQLITE_UTF8);
 1.61961 +}
 1.61962 +SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
 1.61963 +  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
 1.61964 +}
 1.61965 +SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
 1.61966 +  return sqlite3VdbeRealValue((Mem*)pVal);
 1.61967 +}
 1.61968 +SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
 1.61969 +  return (int)sqlite3VdbeIntValue((Mem*)pVal);
 1.61970 +}
 1.61971 +SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
 1.61972 +  return sqlite3VdbeIntValue((Mem*)pVal);
 1.61973 +}
 1.61974 +SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
 1.61975 +  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
 1.61976 +}
 1.61977 +#ifndef SQLITE_OMIT_UTF16
 1.61978 +SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
 1.61979 +  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
 1.61980 +}
 1.61981 +SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
 1.61982 +  return sqlite3ValueText(pVal, SQLITE_UTF16BE);
 1.61983 +}
 1.61984 +SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
 1.61985 +  return sqlite3ValueText(pVal, SQLITE_UTF16LE);
 1.61986 +}
 1.61987 +#endif /* SQLITE_OMIT_UTF16 */
 1.61988 +SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
 1.61989 +  return pVal->type;
 1.61990 +}
 1.61991 +
 1.61992 +/**************************** sqlite3_result_  *******************************
 1.61993 +** The following routines are used by user-defined functions to specify
 1.61994 +** the function result.
 1.61995 +**
 1.61996 +** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
 1.61997 +** result as a string or blob but if the string or blob is too large, it
 1.61998 +** then sets the error code to SQLITE_TOOBIG
 1.61999 +*/
 1.62000 +static void setResultStrOrError(
 1.62001 +  sqlite3_context *pCtx,  /* Function context */
 1.62002 +  const char *z,          /* String pointer */
 1.62003 +  int n,                  /* Bytes in string, or negative */
 1.62004 +  u8 enc,                 /* Encoding of z.  0 for BLOBs */
 1.62005 +  void (*xDel)(void*)     /* Destructor function */
 1.62006 +){
 1.62007 +  if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
 1.62008 +    sqlite3_result_error_toobig(pCtx);
 1.62009 +  }
 1.62010 +}
 1.62011 +SQLITE_API void sqlite3_result_blob(
 1.62012 +  sqlite3_context *pCtx, 
 1.62013 +  const void *z, 
 1.62014 +  int n, 
 1.62015 +  void (*xDel)(void *)
 1.62016 +){
 1.62017 +  assert( n>=0 );
 1.62018 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62019 +  setResultStrOrError(pCtx, z, n, 0, xDel);
 1.62020 +}
 1.62021 +SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
 1.62022 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62023 +  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
 1.62024 +}
 1.62025 +SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
 1.62026 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62027 +  pCtx->isError = SQLITE_ERROR;
 1.62028 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
 1.62029 +}
 1.62030 +#ifndef SQLITE_OMIT_UTF16
 1.62031 +SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
 1.62032 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62033 +  pCtx->isError = SQLITE_ERROR;
 1.62034 +  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
 1.62035 +}
 1.62036 +#endif
 1.62037 +SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
 1.62038 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62039 +  sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
 1.62040 +}
 1.62041 +SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
 1.62042 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62043 +  sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
 1.62044 +}
 1.62045 +SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
 1.62046 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62047 +  sqlite3VdbeMemSetNull(&pCtx->s);
 1.62048 +}
 1.62049 +SQLITE_API void sqlite3_result_text(
 1.62050 +  sqlite3_context *pCtx, 
 1.62051 +  const char *z, 
 1.62052 +  int n,
 1.62053 +  void (*xDel)(void *)
 1.62054 +){
 1.62055 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62056 +  setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
 1.62057 +}
 1.62058 +#ifndef SQLITE_OMIT_UTF16
 1.62059 +SQLITE_API void sqlite3_result_text16(
 1.62060 +  sqlite3_context *pCtx, 
 1.62061 +  const void *z, 
 1.62062 +  int n, 
 1.62063 +  void (*xDel)(void *)
 1.62064 +){
 1.62065 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62066 +  setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
 1.62067 +}
 1.62068 +SQLITE_API void sqlite3_result_text16be(
 1.62069 +  sqlite3_context *pCtx, 
 1.62070 +  const void *z, 
 1.62071 +  int n, 
 1.62072 +  void (*xDel)(void *)
 1.62073 +){
 1.62074 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62075 +  setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
 1.62076 +}
 1.62077 +SQLITE_API void sqlite3_result_text16le(
 1.62078 +  sqlite3_context *pCtx, 
 1.62079 +  const void *z, 
 1.62080 +  int n, 
 1.62081 +  void (*xDel)(void *)
 1.62082 +){
 1.62083 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62084 +  setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
 1.62085 +}
 1.62086 +#endif /* SQLITE_OMIT_UTF16 */
 1.62087 +SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
 1.62088 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62089 +  sqlite3VdbeMemCopy(&pCtx->s, pValue);
 1.62090 +}
 1.62091 +SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
 1.62092 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62093 +  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
 1.62094 +}
 1.62095 +SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
 1.62096 +  pCtx->isError = errCode;
 1.62097 +  if( pCtx->s.flags & MEM_Null ){
 1.62098 +    sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
 1.62099 +                         SQLITE_UTF8, SQLITE_STATIC);
 1.62100 +  }
 1.62101 +}
 1.62102 +
 1.62103 +/* Force an SQLITE_TOOBIG error. */
 1.62104 +SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
 1.62105 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62106 +  pCtx->isError = SQLITE_TOOBIG;
 1.62107 +  sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
 1.62108 +                       SQLITE_UTF8, SQLITE_STATIC);
 1.62109 +}
 1.62110 +
 1.62111 +/* An SQLITE_NOMEM error. */
 1.62112 +SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
 1.62113 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62114 +  sqlite3VdbeMemSetNull(&pCtx->s);
 1.62115 +  pCtx->isError = SQLITE_NOMEM;
 1.62116 +  pCtx->s.db->mallocFailed = 1;
 1.62117 +}
 1.62118 +
 1.62119 +/*
 1.62120 +** This function is called after a transaction has been committed. It 
 1.62121 +** invokes callbacks registered with sqlite3_wal_hook() as required.
 1.62122 +*/
 1.62123 +static int doWalCallbacks(sqlite3 *db){
 1.62124 +  int rc = SQLITE_OK;
 1.62125 +#ifndef SQLITE_OMIT_WAL
 1.62126 +  int i;
 1.62127 +  for(i=0; i<db->nDb; i++){
 1.62128 +    Btree *pBt = db->aDb[i].pBt;
 1.62129 +    if( pBt ){
 1.62130 +      int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
 1.62131 +      if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
 1.62132 +        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
 1.62133 +      }
 1.62134 +    }
 1.62135 +  }
 1.62136 +#endif
 1.62137 +  return rc;
 1.62138 +}
 1.62139 +
 1.62140 +/*
 1.62141 +** Execute the statement pStmt, either until a row of data is ready, the
 1.62142 +** statement is completely executed or an error occurs.
 1.62143 +**
 1.62144 +** This routine implements the bulk of the logic behind the sqlite_step()
 1.62145 +** API.  The only thing omitted is the automatic recompile if a 
 1.62146 +** schema change has occurred.  That detail is handled by the
 1.62147 +** outer sqlite3_step() wrapper procedure.
 1.62148 +*/
 1.62149 +static int sqlite3Step(Vdbe *p){
 1.62150 +  sqlite3 *db;
 1.62151 +  int rc;
 1.62152 +
 1.62153 +  assert(p);
 1.62154 +  if( p->magic!=VDBE_MAGIC_RUN ){
 1.62155 +    /* We used to require that sqlite3_reset() be called before retrying
 1.62156 +    ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
 1.62157 +    ** with version 3.7.0, we changed this so that sqlite3_reset() would
 1.62158 +    ** be called automatically instead of throwing the SQLITE_MISUSE error.
 1.62159 +    ** This "automatic-reset" change is not technically an incompatibility, 
 1.62160 +    ** since any application that receives an SQLITE_MISUSE is broken by
 1.62161 +    ** definition.
 1.62162 +    **
 1.62163 +    ** Nevertheless, some published applications that were originally written
 1.62164 +    ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 
 1.62165 +    ** returns, and those were broken by the automatic-reset change.  As a
 1.62166 +    ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
 1.62167 +    ** legacy behavior of returning SQLITE_MISUSE for cases where the 
 1.62168 +    ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
 1.62169 +    ** or SQLITE_BUSY error.
 1.62170 +    */
 1.62171 +#ifdef SQLITE_OMIT_AUTORESET
 1.62172 +    if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
 1.62173 +      sqlite3_reset((sqlite3_stmt*)p);
 1.62174 +    }else{
 1.62175 +      return SQLITE_MISUSE_BKPT;
 1.62176 +    }
 1.62177 +#else
 1.62178 +    sqlite3_reset((sqlite3_stmt*)p);
 1.62179 +#endif
 1.62180 +  }
 1.62181 +
 1.62182 +  /* Check that malloc() has not failed. If it has, return early. */
 1.62183 +  db = p->db;
 1.62184 +  if( db->mallocFailed ){
 1.62185 +    p->rc = SQLITE_NOMEM;
 1.62186 +    return SQLITE_NOMEM;
 1.62187 +  }
 1.62188 +
 1.62189 +  if( p->pc<=0 && p->expired ){
 1.62190 +    p->rc = SQLITE_SCHEMA;
 1.62191 +    rc = SQLITE_ERROR;
 1.62192 +    goto end_of_step;
 1.62193 +  }
 1.62194 +  if( p->pc<0 ){
 1.62195 +    /* If there are no other statements currently running, then
 1.62196 +    ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
 1.62197 +    ** from interrupting a statement that has not yet started.
 1.62198 +    */
 1.62199 +    if( db->activeVdbeCnt==0 ){
 1.62200 +      db->u1.isInterrupted = 0;
 1.62201 +    }
 1.62202 +
 1.62203 +    assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
 1.62204 +
 1.62205 +#ifndef SQLITE_OMIT_TRACE
 1.62206 +    if( db->xProfile && !db->init.busy ){
 1.62207 +      sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
 1.62208 +    }
 1.62209 +#endif
 1.62210 +
 1.62211 +    db->activeVdbeCnt++;
 1.62212 +    if( p->readOnly==0 ) db->writeVdbeCnt++;
 1.62213 +    p->pc = 0;
 1.62214 +  }
 1.62215 +#ifndef SQLITE_OMIT_EXPLAIN
 1.62216 +  if( p->explain ){
 1.62217 +    rc = sqlite3VdbeList(p);
 1.62218 +  }else
 1.62219 +#endif /* SQLITE_OMIT_EXPLAIN */
 1.62220 +  {
 1.62221 +    db->vdbeExecCnt++;
 1.62222 +    rc = sqlite3VdbeExec(p);
 1.62223 +    db->vdbeExecCnt--;
 1.62224 +  }
 1.62225 +
 1.62226 +#ifndef SQLITE_OMIT_TRACE
 1.62227 +  /* Invoke the profile callback if there is one
 1.62228 +  */
 1.62229 +  if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
 1.62230 +    sqlite3_int64 iNow;
 1.62231 +    sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
 1.62232 +    db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
 1.62233 +  }
 1.62234 +#endif
 1.62235 +
 1.62236 +  if( rc==SQLITE_DONE ){
 1.62237 +    assert( p->rc==SQLITE_OK );
 1.62238 +    p->rc = doWalCallbacks(db);
 1.62239 +    if( p->rc!=SQLITE_OK ){
 1.62240 +      rc = SQLITE_ERROR;
 1.62241 +    }
 1.62242 +  }
 1.62243 +
 1.62244 +  db->errCode = rc;
 1.62245 +  if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
 1.62246 +    p->rc = SQLITE_NOMEM;
 1.62247 +  }
 1.62248 +end_of_step:
 1.62249 +  /* At this point local variable rc holds the value that should be 
 1.62250 +  ** returned if this statement was compiled using the legacy 
 1.62251 +  ** sqlite3_prepare() interface. According to the docs, this can only
 1.62252 +  ** be one of the values in the first assert() below. Variable p->rc 
 1.62253 +  ** contains the value that would be returned if sqlite3_finalize() 
 1.62254 +  ** were called on statement p.
 1.62255 +  */
 1.62256 +  assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR 
 1.62257 +       || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
 1.62258 +  );
 1.62259 +  assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
 1.62260 +  if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
 1.62261 +    /* If this statement was prepared using sqlite3_prepare_v2(), and an
 1.62262 +    ** error has occured, then return the error code in p->rc to the
 1.62263 +    ** caller. Set the error code in the database handle to the same value.
 1.62264 +    */ 
 1.62265 +    rc = sqlite3VdbeTransferError(p);
 1.62266 +  }
 1.62267 +  return (rc&db->errMask);
 1.62268 +}
 1.62269 +
 1.62270 +/*
 1.62271 +** The maximum number of times that a statement will try to reparse
 1.62272 +** itself before giving up and returning SQLITE_SCHEMA.
 1.62273 +*/
 1.62274 +#ifndef SQLITE_MAX_SCHEMA_RETRY
 1.62275 +# define SQLITE_MAX_SCHEMA_RETRY 5
 1.62276 +#endif
 1.62277 +
 1.62278 +/*
 1.62279 +** This is the top-level implementation of sqlite3_step().  Call
 1.62280 +** sqlite3Step() to do most of the work.  If a schema error occurs,
 1.62281 +** call sqlite3Reprepare() and try again.
 1.62282 +*/
 1.62283 +SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
 1.62284 +  int rc = SQLITE_OK;      /* Result from sqlite3Step() */
 1.62285 +  int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
 1.62286 +  Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
 1.62287 +  int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
 1.62288 +  sqlite3 *db;             /* The database connection */
 1.62289 +
 1.62290 +  if( vdbeSafetyNotNull(v) ){
 1.62291 +    return SQLITE_MISUSE_BKPT;
 1.62292 +  }
 1.62293 +  db = v->db;
 1.62294 +  sqlite3_mutex_enter(db->mutex);
 1.62295 +  v->doingRerun = 0;
 1.62296 +  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
 1.62297 +         && cnt++ < SQLITE_MAX_SCHEMA_RETRY
 1.62298 +         && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
 1.62299 +    sqlite3_reset(pStmt);
 1.62300 +    v->doingRerun = 1;
 1.62301 +    assert( v->expired==0 );
 1.62302 +  }
 1.62303 +  if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
 1.62304 +    /* This case occurs after failing to recompile an sql statement. 
 1.62305 +    ** The error message from the SQL compiler has already been loaded 
 1.62306 +    ** into the database handle. This block copies the error message 
 1.62307 +    ** from the database handle into the statement and sets the statement
 1.62308 +    ** program counter to 0 to ensure that when the statement is 
 1.62309 +    ** finalized or reset the parser error message is available via
 1.62310 +    ** sqlite3_errmsg() and sqlite3_errcode().
 1.62311 +    */
 1.62312 +    const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
 1.62313 +    sqlite3DbFree(db, v->zErrMsg);
 1.62314 +    if( !db->mallocFailed ){
 1.62315 +      v->zErrMsg = sqlite3DbStrDup(db, zErr);
 1.62316 +      v->rc = rc2;
 1.62317 +    } else {
 1.62318 +      v->zErrMsg = 0;
 1.62319 +      v->rc = rc = SQLITE_NOMEM;
 1.62320 +    }
 1.62321 +  }
 1.62322 +  rc = sqlite3ApiExit(db, rc);
 1.62323 +  sqlite3_mutex_leave(db->mutex);
 1.62324 +  return rc;
 1.62325 +}
 1.62326 +
 1.62327 +/*
 1.62328 +** Extract the user data from a sqlite3_context structure and return a
 1.62329 +** pointer to it.
 1.62330 +*/
 1.62331 +SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
 1.62332 +  assert( p && p->pFunc );
 1.62333 +  return p->pFunc->pUserData;
 1.62334 +}
 1.62335 +
 1.62336 +/*
 1.62337 +** Extract the user data from a sqlite3_context structure and return a
 1.62338 +** pointer to it.
 1.62339 +**
 1.62340 +** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
 1.62341 +** returns a copy of the pointer to the database connection (the 1st
 1.62342 +** parameter) of the sqlite3_create_function() and
 1.62343 +** sqlite3_create_function16() routines that originally registered the
 1.62344 +** application defined function.
 1.62345 +*/
 1.62346 +SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
 1.62347 +  assert( p && p->pFunc );
 1.62348 +  return p->s.db;
 1.62349 +}
 1.62350 +
 1.62351 +/*
 1.62352 +** The following is the implementation of an SQL function that always
 1.62353 +** fails with an error message stating that the function is used in the
 1.62354 +** wrong context.  The sqlite3_overload_function() API might construct
 1.62355 +** SQL function that use this routine so that the functions will exist
 1.62356 +** for name resolution but are actually overloaded by the xFindFunction
 1.62357 +** method of virtual tables.
 1.62358 +*/
 1.62359 +SQLITE_PRIVATE void sqlite3InvalidFunction(
 1.62360 +  sqlite3_context *context,  /* The function calling context */
 1.62361 +  int NotUsed,               /* Number of arguments to the function */
 1.62362 +  sqlite3_value **NotUsed2   /* Value of each argument */
 1.62363 +){
 1.62364 +  const char *zName = context->pFunc->zName;
 1.62365 +  char *zErr;
 1.62366 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.62367 +  zErr = sqlite3_mprintf(
 1.62368 +      "unable to use function %s in the requested context", zName);
 1.62369 +  sqlite3_result_error(context, zErr, -1);
 1.62370 +  sqlite3_free(zErr);
 1.62371 +}
 1.62372 +
 1.62373 +/*
 1.62374 +** Allocate or return the aggregate context for a user function.  A new
 1.62375 +** context is allocated on the first call.  Subsequent calls return the
 1.62376 +** same context that was returned on prior calls.
 1.62377 +*/
 1.62378 +SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
 1.62379 +  Mem *pMem;
 1.62380 +  assert( p && p->pFunc && p->pFunc->xStep );
 1.62381 +  assert( sqlite3_mutex_held(p->s.db->mutex) );
 1.62382 +  pMem = p->pMem;
 1.62383 +  testcase( nByte<0 );
 1.62384 +  if( (pMem->flags & MEM_Agg)==0 ){
 1.62385 +    if( nByte<=0 ){
 1.62386 +      sqlite3VdbeMemReleaseExternal(pMem);
 1.62387 +      pMem->flags = MEM_Null;
 1.62388 +      pMem->z = 0;
 1.62389 +    }else{
 1.62390 +      sqlite3VdbeMemGrow(pMem, nByte, 0);
 1.62391 +      pMem->flags = MEM_Agg;
 1.62392 +      pMem->u.pDef = p->pFunc;
 1.62393 +      if( pMem->z ){
 1.62394 +        memset(pMem->z, 0, nByte);
 1.62395 +      }
 1.62396 +    }
 1.62397 +  }
 1.62398 +  return (void*)pMem->z;
 1.62399 +}
 1.62400 +
 1.62401 +/*
 1.62402 +** Return the auxilary data pointer, if any, for the iArg'th argument to
 1.62403 +** the user-function defined by pCtx.
 1.62404 +*/
 1.62405 +SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
 1.62406 +  VdbeFunc *pVdbeFunc;
 1.62407 +
 1.62408 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62409 +  pVdbeFunc = pCtx->pVdbeFunc;
 1.62410 +  if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
 1.62411 +    return 0;
 1.62412 +  }
 1.62413 +  return pVdbeFunc->apAux[iArg].pAux;
 1.62414 +}
 1.62415 +
 1.62416 +/*
 1.62417 +** Set the auxilary data pointer and delete function, for the iArg'th
 1.62418 +** argument to the user-function defined by pCtx. Any previous value is
 1.62419 +** deleted by calling the delete function specified when it was set.
 1.62420 +*/
 1.62421 +SQLITE_API void sqlite3_set_auxdata(
 1.62422 +  sqlite3_context *pCtx, 
 1.62423 +  int iArg, 
 1.62424 +  void *pAux, 
 1.62425 +  void (*xDelete)(void*)
 1.62426 +){
 1.62427 +  struct AuxData *pAuxData;
 1.62428 +  VdbeFunc *pVdbeFunc;
 1.62429 +  if( iArg<0 ) goto failed;
 1.62430 +
 1.62431 +  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 1.62432 +  pVdbeFunc = pCtx->pVdbeFunc;
 1.62433 +  if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
 1.62434 +    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
 1.62435 +    int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
 1.62436 +    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
 1.62437 +    if( !pVdbeFunc ){
 1.62438 +      goto failed;
 1.62439 +    }
 1.62440 +    pCtx->pVdbeFunc = pVdbeFunc;
 1.62441 +    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
 1.62442 +    pVdbeFunc->nAux = iArg+1;
 1.62443 +    pVdbeFunc->pFunc = pCtx->pFunc;
 1.62444 +  }
 1.62445 +
 1.62446 +  pAuxData = &pVdbeFunc->apAux[iArg];
 1.62447 +  if( pAuxData->pAux && pAuxData->xDelete ){
 1.62448 +    pAuxData->xDelete(pAuxData->pAux);
 1.62449 +  }
 1.62450 +  pAuxData->pAux = pAux;
 1.62451 +  pAuxData->xDelete = xDelete;
 1.62452 +  return;
 1.62453 +
 1.62454 +failed:
 1.62455 +  if( xDelete ){
 1.62456 +    xDelete(pAux);
 1.62457 +  }
 1.62458 +}
 1.62459 +
 1.62460 +#ifndef SQLITE_OMIT_DEPRECATED
 1.62461 +/*
 1.62462 +** Return the number of times the Step function of a aggregate has been 
 1.62463 +** called.
 1.62464 +**
 1.62465 +** This function is deprecated.  Do not use it for new code.  It is
 1.62466 +** provide only to avoid breaking legacy code.  New aggregate function
 1.62467 +** implementations should keep their own counts within their aggregate
 1.62468 +** context.
 1.62469 +*/
 1.62470 +SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
 1.62471 +  assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
 1.62472 +  return p->pMem->n;
 1.62473 +}
 1.62474 +#endif
 1.62475 +
 1.62476 +/*
 1.62477 +** Return the number of columns in the result set for the statement pStmt.
 1.62478 +*/
 1.62479 +SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
 1.62480 +  Vdbe *pVm = (Vdbe *)pStmt;
 1.62481 +  return pVm ? pVm->nResColumn : 0;
 1.62482 +}
 1.62483 +
 1.62484 +/*
 1.62485 +** Return the number of values available from the current row of the
 1.62486 +** currently executing statement pStmt.
 1.62487 +*/
 1.62488 +SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
 1.62489 +  Vdbe *pVm = (Vdbe *)pStmt;
 1.62490 +  if( pVm==0 || pVm->pResultSet==0 ) return 0;
 1.62491 +  return pVm->nResColumn;
 1.62492 +}
 1.62493 +
 1.62494 +
 1.62495 +/*
 1.62496 +** Check to see if column iCol of the given statement is valid.  If
 1.62497 +** it is, return a pointer to the Mem for the value of that column.
 1.62498 +** If iCol is not valid, return a pointer to a Mem which has a value
 1.62499 +** of NULL.
 1.62500 +*/
 1.62501 +static Mem *columnMem(sqlite3_stmt *pStmt, int i){
 1.62502 +  Vdbe *pVm;
 1.62503 +  Mem *pOut;
 1.62504 +
 1.62505 +  pVm = (Vdbe *)pStmt;
 1.62506 +  if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
 1.62507 +    sqlite3_mutex_enter(pVm->db->mutex);
 1.62508 +    pOut = &pVm->pResultSet[i];
 1.62509 +  }else{
 1.62510 +    /* If the value passed as the second argument is out of range, return
 1.62511 +    ** a pointer to the following static Mem object which contains the
 1.62512 +    ** value SQL NULL. Even though the Mem structure contains an element
 1.62513 +    ** of type i64, on certain architectures (x86) with certain compiler
 1.62514 +    ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
 1.62515 +    ** instead of an 8-byte one. This all works fine, except that when
 1.62516 +    ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
 1.62517 +    ** that a Mem structure is located on an 8-byte boundary. To prevent
 1.62518 +    ** these assert()s from failing, when building with SQLITE_DEBUG defined
 1.62519 +    ** using gcc, we force nullMem to be 8-byte aligned using the magical
 1.62520 +    ** __attribute__((aligned(8))) macro.  */
 1.62521 +    static const Mem nullMem 
 1.62522 +#if defined(SQLITE_DEBUG) && defined(__GNUC__)
 1.62523 +      __attribute__((aligned(8))) 
 1.62524 +#endif
 1.62525 +      = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
 1.62526 +#ifdef SQLITE_DEBUG
 1.62527 +         0, 0,  /* pScopyFrom, pFiller */
 1.62528 +#endif
 1.62529 +         0, 0 };
 1.62530 +
 1.62531 +    if( pVm && ALWAYS(pVm->db) ){
 1.62532 +      sqlite3_mutex_enter(pVm->db->mutex);
 1.62533 +      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
 1.62534 +    }
 1.62535 +    pOut = (Mem*)&nullMem;
 1.62536 +  }
 1.62537 +  return pOut;
 1.62538 +}
 1.62539 +
 1.62540 +/*
 1.62541 +** This function is called after invoking an sqlite3_value_XXX function on a 
 1.62542 +** column value (i.e. a value returned by evaluating an SQL expression in the
 1.62543 +** select list of a SELECT statement) that may cause a malloc() failure. If 
 1.62544 +** malloc() has failed, the threads mallocFailed flag is cleared and the result
 1.62545 +** code of statement pStmt set to SQLITE_NOMEM.
 1.62546 +**
 1.62547 +** Specifically, this is called from within:
 1.62548 +**
 1.62549 +**     sqlite3_column_int()
 1.62550 +**     sqlite3_column_int64()
 1.62551 +**     sqlite3_column_text()
 1.62552 +**     sqlite3_column_text16()
 1.62553 +**     sqlite3_column_real()
 1.62554 +**     sqlite3_column_bytes()
 1.62555 +**     sqlite3_column_bytes16()
 1.62556 +**     sqiite3_column_blob()
 1.62557 +*/
 1.62558 +static void columnMallocFailure(sqlite3_stmt *pStmt)
 1.62559 +{
 1.62560 +  /* If malloc() failed during an encoding conversion within an
 1.62561 +  ** sqlite3_column_XXX API, then set the return code of the statement to
 1.62562 +  ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
 1.62563 +  ** and _finalize() will return NOMEM.
 1.62564 +  */
 1.62565 +  Vdbe *p = (Vdbe *)pStmt;
 1.62566 +  if( p ){
 1.62567 +    p->rc = sqlite3ApiExit(p->db, p->rc);
 1.62568 +    sqlite3_mutex_leave(p->db->mutex);
 1.62569 +  }
 1.62570 +}
 1.62571 +
 1.62572 +/**************************** sqlite3_column_  *******************************
 1.62573 +** The following routines are used to access elements of the current row
 1.62574 +** in the result set.
 1.62575 +*/
 1.62576 +SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
 1.62577 +  const void *val;
 1.62578 +  val = sqlite3_value_blob( columnMem(pStmt,i) );
 1.62579 +  /* Even though there is no encoding conversion, value_blob() might
 1.62580 +  ** need to call malloc() to expand the result of a zeroblob() 
 1.62581 +  ** expression. 
 1.62582 +  */
 1.62583 +  columnMallocFailure(pStmt);
 1.62584 +  return val;
 1.62585 +}
 1.62586 +SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
 1.62587 +  int val = sqlite3_value_bytes( columnMem(pStmt,i) );
 1.62588 +  columnMallocFailure(pStmt);
 1.62589 +  return val;
 1.62590 +}
 1.62591 +SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
 1.62592 +  int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
 1.62593 +  columnMallocFailure(pStmt);
 1.62594 +  return val;
 1.62595 +}
 1.62596 +SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
 1.62597 +  double val = sqlite3_value_double( columnMem(pStmt,i) );
 1.62598 +  columnMallocFailure(pStmt);
 1.62599 +  return val;
 1.62600 +}
 1.62601 +SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
 1.62602 +  int val = sqlite3_value_int( columnMem(pStmt,i) );
 1.62603 +  columnMallocFailure(pStmt);
 1.62604 +  return val;
 1.62605 +}
 1.62606 +SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
 1.62607 +  sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
 1.62608 +  columnMallocFailure(pStmt);
 1.62609 +  return val;
 1.62610 +}
 1.62611 +SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
 1.62612 +  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
 1.62613 +  columnMallocFailure(pStmt);
 1.62614 +  return val;
 1.62615 +}
 1.62616 +SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
 1.62617 +  Mem *pOut = columnMem(pStmt, i);
 1.62618 +  if( pOut->flags&MEM_Static ){
 1.62619 +    pOut->flags &= ~MEM_Static;
 1.62620 +    pOut->flags |= MEM_Ephem;
 1.62621 +  }
 1.62622 +  columnMallocFailure(pStmt);
 1.62623 +  return (sqlite3_value *)pOut;
 1.62624 +}
 1.62625 +#ifndef SQLITE_OMIT_UTF16
 1.62626 +SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
 1.62627 +  const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
 1.62628 +  columnMallocFailure(pStmt);
 1.62629 +  return val;
 1.62630 +}
 1.62631 +#endif /* SQLITE_OMIT_UTF16 */
 1.62632 +SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
 1.62633 +  int iType = sqlite3_value_type( columnMem(pStmt,i) );
 1.62634 +  columnMallocFailure(pStmt);
 1.62635 +  return iType;
 1.62636 +}
 1.62637 +
 1.62638 +/* The following function is experimental and subject to change or
 1.62639 +** removal */
 1.62640 +/*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
 1.62641 +**  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
 1.62642 +**}
 1.62643 +*/
 1.62644 +
 1.62645 +/*
 1.62646 +** Convert the N-th element of pStmt->pColName[] into a string using
 1.62647 +** xFunc() then return that string.  If N is out of range, return 0.
 1.62648 +**
 1.62649 +** There are up to 5 names for each column.  useType determines which
 1.62650 +** name is returned.  Here are the names:
 1.62651 +**
 1.62652 +**    0      The column name as it should be displayed for output
 1.62653 +**    1      The datatype name for the column
 1.62654 +**    2      The name of the database that the column derives from
 1.62655 +**    3      The name of the table that the column derives from
 1.62656 +**    4      The name of the table column that the result column derives from
 1.62657 +**
 1.62658 +** If the result is not a simple column reference (if it is an expression
 1.62659 +** or a constant) then useTypes 2, 3, and 4 return NULL.
 1.62660 +*/
 1.62661 +static const void *columnName(
 1.62662 +  sqlite3_stmt *pStmt,
 1.62663 +  int N,
 1.62664 +  const void *(*xFunc)(Mem*),
 1.62665 +  int useType
 1.62666 +){
 1.62667 +  const void *ret = 0;
 1.62668 +  Vdbe *p = (Vdbe *)pStmt;
 1.62669 +  int n;
 1.62670 +  sqlite3 *db = p->db;
 1.62671 +  
 1.62672 +  assert( db!=0 );
 1.62673 +  n = sqlite3_column_count(pStmt);
 1.62674 +  if( N<n && N>=0 ){
 1.62675 +    N += useType*n;
 1.62676 +    sqlite3_mutex_enter(db->mutex);
 1.62677 +    assert( db->mallocFailed==0 );
 1.62678 +    ret = xFunc(&p->aColName[N]);
 1.62679 +     /* A malloc may have failed inside of the xFunc() call. If this
 1.62680 +    ** is the case, clear the mallocFailed flag and return NULL.
 1.62681 +    */
 1.62682 +    if( db->mallocFailed ){
 1.62683 +      db->mallocFailed = 0;
 1.62684 +      ret = 0;
 1.62685 +    }
 1.62686 +    sqlite3_mutex_leave(db->mutex);
 1.62687 +  }
 1.62688 +  return ret;
 1.62689 +}
 1.62690 +
 1.62691 +/*
 1.62692 +** Return the name of the Nth column of the result set returned by SQL
 1.62693 +** statement pStmt.
 1.62694 +*/
 1.62695 +SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
 1.62696 +  return columnName(
 1.62697 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
 1.62698 +}
 1.62699 +#ifndef SQLITE_OMIT_UTF16
 1.62700 +SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
 1.62701 +  return columnName(
 1.62702 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
 1.62703 +}
 1.62704 +#endif
 1.62705 +
 1.62706 +/*
 1.62707 +** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
 1.62708 +** not define OMIT_DECLTYPE.
 1.62709 +*/
 1.62710 +#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
 1.62711 +# error "Must not define both SQLITE_OMIT_DECLTYPE \
 1.62712 +         and SQLITE_ENABLE_COLUMN_METADATA"
 1.62713 +#endif
 1.62714 +
 1.62715 +#ifndef SQLITE_OMIT_DECLTYPE
 1.62716 +/*
 1.62717 +** Return the column declaration type (if applicable) of the 'i'th column
 1.62718 +** of the result set of SQL statement pStmt.
 1.62719 +*/
 1.62720 +SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
 1.62721 +  return columnName(
 1.62722 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
 1.62723 +}
 1.62724 +#ifndef SQLITE_OMIT_UTF16
 1.62725 +SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
 1.62726 +  return columnName(
 1.62727 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
 1.62728 +}
 1.62729 +#endif /* SQLITE_OMIT_UTF16 */
 1.62730 +#endif /* SQLITE_OMIT_DECLTYPE */
 1.62731 +
 1.62732 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
 1.62733 +/*
 1.62734 +** Return the name of the database from which a result column derives.
 1.62735 +** NULL is returned if the result column is an expression or constant or
 1.62736 +** anything else which is not an unabiguous reference to a database column.
 1.62737 +*/
 1.62738 +SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
 1.62739 +  return columnName(
 1.62740 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
 1.62741 +}
 1.62742 +#ifndef SQLITE_OMIT_UTF16
 1.62743 +SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
 1.62744 +  return columnName(
 1.62745 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
 1.62746 +}
 1.62747 +#endif /* SQLITE_OMIT_UTF16 */
 1.62748 +
 1.62749 +/*
 1.62750 +** Return the name of the table from which a result column derives.
 1.62751 +** NULL is returned if the result column is an expression or constant or
 1.62752 +** anything else which is not an unabiguous reference to a database column.
 1.62753 +*/
 1.62754 +SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
 1.62755 +  return columnName(
 1.62756 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
 1.62757 +}
 1.62758 +#ifndef SQLITE_OMIT_UTF16
 1.62759 +SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
 1.62760 +  return columnName(
 1.62761 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
 1.62762 +}
 1.62763 +#endif /* SQLITE_OMIT_UTF16 */
 1.62764 +
 1.62765 +/*
 1.62766 +** Return the name of the table column from which a result column derives.
 1.62767 +** NULL is returned if the result column is an expression or constant or
 1.62768 +** anything else which is not an unabiguous reference to a database column.
 1.62769 +*/
 1.62770 +SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
 1.62771 +  return columnName(
 1.62772 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
 1.62773 +}
 1.62774 +#ifndef SQLITE_OMIT_UTF16
 1.62775 +SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
 1.62776 +  return columnName(
 1.62777 +      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
 1.62778 +}
 1.62779 +#endif /* SQLITE_OMIT_UTF16 */
 1.62780 +#endif /* SQLITE_ENABLE_COLUMN_METADATA */
 1.62781 +
 1.62782 +
 1.62783 +/******************************* sqlite3_bind_  ***************************
 1.62784 +** 
 1.62785 +** Routines used to attach values to wildcards in a compiled SQL statement.
 1.62786 +*/
 1.62787 +/*
 1.62788 +** Unbind the value bound to variable i in virtual machine p. This is the 
 1.62789 +** the same as binding a NULL value to the column. If the "i" parameter is
 1.62790 +** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
 1.62791 +**
 1.62792 +** A successful evaluation of this routine acquires the mutex on p.
 1.62793 +** the mutex is released if any kind of error occurs.
 1.62794 +**
 1.62795 +** The error code stored in database p->db is overwritten with the return
 1.62796 +** value in any case.
 1.62797 +*/
 1.62798 +static int vdbeUnbind(Vdbe *p, int i){
 1.62799 +  Mem *pVar;
 1.62800 +  if( vdbeSafetyNotNull(p) ){
 1.62801 +    return SQLITE_MISUSE_BKPT;
 1.62802 +  }
 1.62803 +  sqlite3_mutex_enter(p->db->mutex);
 1.62804 +  if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
 1.62805 +    sqlite3Error(p->db, SQLITE_MISUSE, 0);
 1.62806 +    sqlite3_mutex_leave(p->db->mutex);
 1.62807 +    sqlite3_log(SQLITE_MISUSE, 
 1.62808 +        "bind on a busy prepared statement: [%s]", p->zSql);
 1.62809 +    return SQLITE_MISUSE_BKPT;
 1.62810 +  }
 1.62811 +  if( i<1 || i>p->nVar ){
 1.62812 +    sqlite3Error(p->db, SQLITE_RANGE, 0);
 1.62813 +    sqlite3_mutex_leave(p->db->mutex);
 1.62814 +    return SQLITE_RANGE;
 1.62815 +  }
 1.62816 +  i--;
 1.62817 +  pVar = &p->aVar[i];
 1.62818 +  sqlite3VdbeMemRelease(pVar);
 1.62819 +  pVar->flags = MEM_Null;
 1.62820 +  sqlite3Error(p->db, SQLITE_OK, 0);
 1.62821 +
 1.62822 +  /* If the bit corresponding to this variable in Vdbe.expmask is set, then 
 1.62823 +  ** binding a new value to this variable invalidates the current query plan.
 1.62824 +  **
 1.62825 +  ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
 1.62826 +  ** parameter in the WHERE clause might influence the choice of query plan
 1.62827 +  ** for a statement, then the statement will be automatically recompiled,
 1.62828 +  ** as if there had been a schema change, on the first sqlite3_step() call
 1.62829 +  ** following any change to the bindings of that parameter.
 1.62830 +  */
 1.62831 +  if( p->isPrepareV2 &&
 1.62832 +     ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
 1.62833 +  ){
 1.62834 +    p->expired = 1;
 1.62835 +  }
 1.62836 +  return SQLITE_OK;
 1.62837 +}
 1.62838 +
 1.62839 +/*
 1.62840 +** Bind a text or BLOB value.
 1.62841 +*/
 1.62842 +static int bindText(
 1.62843 +  sqlite3_stmt *pStmt,   /* The statement to bind against */
 1.62844 +  int i,                 /* Index of the parameter to bind */
 1.62845 +  const void *zData,     /* Pointer to the data to be bound */
 1.62846 +  int nData,             /* Number of bytes of data to be bound */
 1.62847 +  void (*xDel)(void*),   /* Destructor for the data */
 1.62848 +  u8 encoding            /* Encoding for the data */
 1.62849 +){
 1.62850 +  Vdbe *p = (Vdbe *)pStmt;
 1.62851 +  Mem *pVar;
 1.62852 +  int rc;
 1.62853 +
 1.62854 +  rc = vdbeUnbind(p, i);
 1.62855 +  if( rc==SQLITE_OK ){
 1.62856 +    if( zData!=0 ){
 1.62857 +      pVar = &p->aVar[i-1];
 1.62858 +      rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
 1.62859 +      if( rc==SQLITE_OK && encoding!=0 ){
 1.62860 +        rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
 1.62861 +      }
 1.62862 +      sqlite3Error(p->db, rc, 0);
 1.62863 +      rc = sqlite3ApiExit(p->db, rc);
 1.62864 +    }
 1.62865 +    sqlite3_mutex_leave(p->db->mutex);
 1.62866 +  }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
 1.62867 +    xDel((void*)zData);
 1.62868 +  }
 1.62869 +  return rc;
 1.62870 +}
 1.62871 +
 1.62872 +
 1.62873 +/*
 1.62874 +** Bind a blob value to an SQL statement variable.
 1.62875 +*/
 1.62876 +SQLITE_API int sqlite3_bind_blob(
 1.62877 +  sqlite3_stmt *pStmt, 
 1.62878 +  int i, 
 1.62879 +  const void *zData, 
 1.62880 +  int nData, 
 1.62881 +  void (*xDel)(void*)
 1.62882 +){
 1.62883 +  return bindText(pStmt, i, zData, nData, xDel, 0);
 1.62884 +}
 1.62885 +SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
 1.62886 +  int rc;
 1.62887 +  Vdbe *p = (Vdbe *)pStmt;
 1.62888 +  rc = vdbeUnbind(p, i);
 1.62889 +  if( rc==SQLITE_OK ){
 1.62890 +    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
 1.62891 +    sqlite3_mutex_leave(p->db->mutex);
 1.62892 +  }
 1.62893 +  return rc;
 1.62894 +}
 1.62895 +SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
 1.62896 +  return sqlite3_bind_int64(p, i, (i64)iValue);
 1.62897 +}
 1.62898 +SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
 1.62899 +  int rc;
 1.62900 +  Vdbe *p = (Vdbe *)pStmt;
 1.62901 +  rc = vdbeUnbind(p, i);
 1.62902 +  if( rc==SQLITE_OK ){
 1.62903 +    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
 1.62904 +    sqlite3_mutex_leave(p->db->mutex);
 1.62905 +  }
 1.62906 +  return rc;
 1.62907 +}
 1.62908 +SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
 1.62909 +  int rc;
 1.62910 +  Vdbe *p = (Vdbe*)pStmt;
 1.62911 +  rc = vdbeUnbind(p, i);
 1.62912 +  if( rc==SQLITE_OK ){
 1.62913 +    sqlite3_mutex_leave(p->db->mutex);
 1.62914 +  }
 1.62915 +  return rc;
 1.62916 +}
 1.62917 +SQLITE_API int sqlite3_bind_text( 
 1.62918 +  sqlite3_stmt *pStmt, 
 1.62919 +  int i, 
 1.62920 +  const char *zData, 
 1.62921 +  int nData, 
 1.62922 +  void (*xDel)(void*)
 1.62923 +){
 1.62924 +  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
 1.62925 +}
 1.62926 +#ifndef SQLITE_OMIT_UTF16
 1.62927 +SQLITE_API int sqlite3_bind_text16(
 1.62928 +  sqlite3_stmt *pStmt, 
 1.62929 +  int i, 
 1.62930 +  const void *zData, 
 1.62931 +  int nData, 
 1.62932 +  void (*xDel)(void*)
 1.62933 +){
 1.62934 +  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
 1.62935 +}
 1.62936 +#endif /* SQLITE_OMIT_UTF16 */
 1.62937 +SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
 1.62938 +  int rc;
 1.62939 +  switch( pValue->type ){
 1.62940 +    case SQLITE_INTEGER: {
 1.62941 +      rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
 1.62942 +      break;
 1.62943 +    }
 1.62944 +    case SQLITE_FLOAT: {
 1.62945 +      rc = sqlite3_bind_double(pStmt, i, pValue->r);
 1.62946 +      break;
 1.62947 +    }
 1.62948 +    case SQLITE_BLOB: {
 1.62949 +      if( pValue->flags & MEM_Zero ){
 1.62950 +        rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
 1.62951 +      }else{
 1.62952 +        rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
 1.62953 +      }
 1.62954 +      break;
 1.62955 +    }
 1.62956 +    case SQLITE_TEXT: {
 1.62957 +      rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
 1.62958 +                              pValue->enc);
 1.62959 +      break;
 1.62960 +    }
 1.62961 +    default: {
 1.62962 +      rc = sqlite3_bind_null(pStmt, i);
 1.62963 +      break;
 1.62964 +    }
 1.62965 +  }
 1.62966 +  return rc;
 1.62967 +}
 1.62968 +SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
 1.62969 +  int rc;
 1.62970 +  Vdbe *p = (Vdbe *)pStmt;
 1.62971 +  rc = vdbeUnbind(p, i);
 1.62972 +  if( rc==SQLITE_OK ){
 1.62973 +    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
 1.62974 +    sqlite3_mutex_leave(p->db->mutex);
 1.62975 +  }
 1.62976 +  return rc;
 1.62977 +}
 1.62978 +
 1.62979 +/*
 1.62980 +** Return the number of wildcards that can be potentially bound to.
 1.62981 +** This routine is added to support DBD::SQLite.  
 1.62982 +*/
 1.62983 +SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
 1.62984 +  Vdbe *p = (Vdbe*)pStmt;
 1.62985 +  return p ? p->nVar : 0;
 1.62986 +}
 1.62987 +
 1.62988 +/*
 1.62989 +** Return the name of a wildcard parameter.  Return NULL if the index
 1.62990 +** is out of range or if the wildcard is unnamed.
 1.62991 +**
 1.62992 +** The result is always UTF-8.
 1.62993 +*/
 1.62994 +SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
 1.62995 +  Vdbe *p = (Vdbe*)pStmt;
 1.62996 +  if( p==0 || i<1 || i>p->nzVar ){
 1.62997 +    return 0;
 1.62998 +  }
 1.62999 +  return p->azVar[i-1];
 1.63000 +}
 1.63001 +
 1.63002 +/*
 1.63003 +** Given a wildcard parameter name, return the index of the variable
 1.63004 +** with that name.  If there is no variable with the given name,
 1.63005 +** return 0.
 1.63006 +*/
 1.63007 +SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
 1.63008 +  int i;
 1.63009 +  if( p==0 ){
 1.63010 +    return 0;
 1.63011 +  }
 1.63012 +  if( zName ){
 1.63013 +    for(i=0; i<p->nzVar; i++){
 1.63014 +      const char *z = p->azVar[i];
 1.63015 +      if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
 1.63016 +        return i+1;
 1.63017 +      }
 1.63018 +    }
 1.63019 +  }
 1.63020 +  return 0;
 1.63021 +}
 1.63022 +SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
 1.63023 +  return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
 1.63024 +}
 1.63025 +
 1.63026 +/*
 1.63027 +** Transfer all bindings from the first statement over to the second.
 1.63028 +*/
 1.63029 +SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
 1.63030 +  Vdbe *pFrom = (Vdbe*)pFromStmt;
 1.63031 +  Vdbe *pTo = (Vdbe*)pToStmt;
 1.63032 +  int i;
 1.63033 +  assert( pTo->db==pFrom->db );
 1.63034 +  assert( pTo->nVar==pFrom->nVar );
 1.63035 +  sqlite3_mutex_enter(pTo->db->mutex);
 1.63036 +  for(i=0; i<pFrom->nVar; i++){
 1.63037 +    sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
 1.63038 +  }
 1.63039 +  sqlite3_mutex_leave(pTo->db->mutex);
 1.63040 +  return SQLITE_OK;
 1.63041 +}
 1.63042 +
 1.63043 +#ifndef SQLITE_OMIT_DEPRECATED
 1.63044 +/*
 1.63045 +** Deprecated external interface.  Internal/core SQLite code
 1.63046 +** should call sqlite3TransferBindings.
 1.63047 +**
 1.63048 +** Is is misuse to call this routine with statements from different
 1.63049 +** database connections.  But as this is a deprecated interface, we
 1.63050 +** will not bother to check for that condition.
 1.63051 +**
 1.63052 +** If the two statements contain a different number of bindings, then
 1.63053 +** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
 1.63054 +** SQLITE_OK is returned.
 1.63055 +*/
 1.63056 +SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
 1.63057 +  Vdbe *pFrom = (Vdbe*)pFromStmt;
 1.63058 +  Vdbe *pTo = (Vdbe*)pToStmt;
 1.63059 +  if( pFrom->nVar!=pTo->nVar ){
 1.63060 +    return SQLITE_ERROR;
 1.63061 +  }
 1.63062 +  if( pTo->isPrepareV2 && pTo->expmask ){
 1.63063 +    pTo->expired = 1;
 1.63064 +  }
 1.63065 +  if( pFrom->isPrepareV2 && pFrom->expmask ){
 1.63066 +    pFrom->expired = 1;
 1.63067 +  }
 1.63068 +  return sqlite3TransferBindings(pFromStmt, pToStmt);
 1.63069 +}
 1.63070 +#endif
 1.63071 +
 1.63072 +/*
 1.63073 +** Return the sqlite3* database handle to which the prepared statement given
 1.63074 +** in the argument belongs.  This is the same database handle that was
 1.63075 +** the first argument to the sqlite3_prepare() that was used to create
 1.63076 +** the statement in the first place.
 1.63077 +*/
 1.63078 +SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
 1.63079 +  return pStmt ? ((Vdbe*)pStmt)->db : 0;
 1.63080 +}
 1.63081 +
 1.63082 +/*
 1.63083 +** Return true if the prepared statement is guaranteed to not modify the
 1.63084 +** database.
 1.63085 +*/
 1.63086 +SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
 1.63087 +  return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
 1.63088 +}
 1.63089 +
 1.63090 +/*
 1.63091 +** Return true if the prepared statement is in need of being reset.
 1.63092 +*/
 1.63093 +SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
 1.63094 +  Vdbe *v = (Vdbe*)pStmt;
 1.63095 +  return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
 1.63096 +}
 1.63097 +
 1.63098 +/*
 1.63099 +** Return a pointer to the next prepared statement after pStmt associated
 1.63100 +** with database connection pDb.  If pStmt is NULL, return the first
 1.63101 +** prepared statement for the database connection.  Return NULL if there
 1.63102 +** are no more.
 1.63103 +*/
 1.63104 +SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
 1.63105 +  sqlite3_stmt *pNext;
 1.63106 +  sqlite3_mutex_enter(pDb->mutex);
 1.63107 +  if( pStmt==0 ){
 1.63108 +    pNext = (sqlite3_stmt*)pDb->pVdbe;
 1.63109 +  }else{
 1.63110 +    pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
 1.63111 +  }
 1.63112 +  sqlite3_mutex_leave(pDb->mutex);
 1.63113 +  return pNext;
 1.63114 +}
 1.63115 +
 1.63116 +/*
 1.63117 +** Return the value of a status counter for a prepared statement
 1.63118 +*/
 1.63119 +SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
 1.63120 +  Vdbe *pVdbe = (Vdbe*)pStmt;
 1.63121 +  int v = pVdbe->aCounter[op-1];
 1.63122 +  if( resetFlag ) pVdbe->aCounter[op-1] = 0;
 1.63123 +  return v;
 1.63124 +}
 1.63125 +
 1.63126 +/************** End of vdbeapi.c *********************************************/
 1.63127 +/************** Begin file vdbetrace.c ***************************************/
 1.63128 +/*
 1.63129 +** 2009 November 25
 1.63130 +**
 1.63131 +** The author disclaims copyright to this source code.  In place of
 1.63132 +** a legal notice, here is a blessing:
 1.63133 +**
 1.63134 +**    May you do good and not evil.
 1.63135 +**    May you find forgiveness for yourself and forgive others.
 1.63136 +**    May you share freely, never taking more than you give.
 1.63137 +**
 1.63138 +*************************************************************************
 1.63139 +**
 1.63140 +** This file contains code used to insert the values of host parameters
 1.63141 +** (aka "wildcards") into the SQL text output by sqlite3_trace().
 1.63142 +**
 1.63143 +** The Vdbe parse-tree explainer is also found here.
 1.63144 +*/
 1.63145 +
 1.63146 +#ifndef SQLITE_OMIT_TRACE
 1.63147 +
 1.63148 +/*
 1.63149 +** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
 1.63150 +** bytes in this text up to but excluding the first character in
 1.63151 +** a host parameter.  If the text contains no host parameters, return
 1.63152 +** the total number of bytes in the text.
 1.63153 +*/
 1.63154 +static int findNextHostParameter(const char *zSql, int *pnToken){
 1.63155 +  int tokenType;
 1.63156 +  int nTotal = 0;
 1.63157 +  int n;
 1.63158 +
 1.63159 +  *pnToken = 0;
 1.63160 +  while( zSql[0] ){
 1.63161 +    n = sqlite3GetToken((u8*)zSql, &tokenType);
 1.63162 +    assert( n>0 && tokenType!=TK_ILLEGAL );
 1.63163 +    if( tokenType==TK_VARIABLE ){
 1.63164 +      *pnToken = n;
 1.63165 +      break;
 1.63166 +    }
 1.63167 +    nTotal += n;
 1.63168 +    zSql += n;
 1.63169 +  }
 1.63170 +  return nTotal;
 1.63171 +}
 1.63172 +
 1.63173 +/*
 1.63174 +** This function returns a pointer to a nul-terminated string in memory
 1.63175 +** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
 1.63176 +** string contains a copy of zRawSql but with host parameters expanded to 
 1.63177 +** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1, 
 1.63178 +** then the returned string holds a copy of zRawSql with "-- " prepended
 1.63179 +** to each line of text.
 1.63180 +**
 1.63181 +** The calling function is responsible for making sure the memory returned
 1.63182 +** is eventually freed.
 1.63183 +**
 1.63184 +** ALGORITHM:  Scan the input string looking for host parameters in any of
 1.63185 +** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
 1.63186 +** string literals, quoted identifier names, and comments.  For text forms,
 1.63187 +** the host parameter index is found by scanning the perpared
 1.63188 +** statement for the corresponding OP_Variable opcode.  Once the host
 1.63189 +** parameter index is known, locate the value in p->aVar[].  Then render
 1.63190 +** the value as a literal in place of the host parameter name.
 1.63191 +*/
 1.63192 +SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
 1.63193 +  Vdbe *p,                 /* The prepared statement being evaluated */
 1.63194 +  const char *zRawSql      /* Raw text of the SQL statement */
 1.63195 +){
 1.63196 +  sqlite3 *db;             /* The database connection */
 1.63197 +  int idx = 0;             /* Index of a host parameter */
 1.63198 +  int nextIndex = 1;       /* Index of next ? host parameter */
 1.63199 +  int n;                   /* Length of a token prefix */
 1.63200 +  int nToken;              /* Length of the parameter token */
 1.63201 +  int i;                   /* Loop counter */
 1.63202 +  Mem *pVar;               /* Value of a host parameter */
 1.63203 +  StrAccum out;            /* Accumulate the output here */
 1.63204 +  char zBase[100];         /* Initial working space */
 1.63205 +
 1.63206 +  db = p->db;
 1.63207 +  sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
 1.63208 +                      db->aLimit[SQLITE_LIMIT_LENGTH]);
 1.63209 +  out.db = db;
 1.63210 +  if( db->vdbeExecCnt>1 ){
 1.63211 +    while( *zRawSql ){
 1.63212 +      const char *zStart = zRawSql;
 1.63213 +      while( *(zRawSql++)!='\n' && *zRawSql );
 1.63214 +      sqlite3StrAccumAppend(&out, "-- ", 3);
 1.63215 +      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
 1.63216 +    }
 1.63217 +  }else{
 1.63218 +    while( zRawSql[0] ){
 1.63219 +      n = findNextHostParameter(zRawSql, &nToken);
 1.63220 +      assert( n>0 );
 1.63221 +      sqlite3StrAccumAppend(&out, zRawSql, n);
 1.63222 +      zRawSql += n;
 1.63223 +      assert( zRawSql[0] || nToken==0 );
 1.63224 +      if( nToken==0 ) break;
 1.63225 +      if( zRawSql[0]=='?' ){
 1.63226 +        if( nToken>1 ){
 1.63227 +          assert( sqlite3Isdigit(zRawSql[1]) );
 1.63228 +          sqlite3GetInt32(&zRawSql[1], &idx);
 1.63229 +        }else{
 1.63230 +          idx = nextIndex;
 1.63231 +        }
 1.63232 +      }else{
 1.63233 +        assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
 1.63234 +        testcase( zRawSql[0]==':' );
 1.63235 +        testcase( zRawSql[0]=='$' );
 1.63236 +        testcase( zRawSql[0]=='@' );
 1.63237 +        idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
 1.63238 +        assert( idx>0 );
 1.63239 +      }
 1.63240 +      zRawSql += nToken;
 1.63241 +      nextIndex = idx + 1;
 1.63242 +      assert( idx>0 && idx<=p->nVar );
 1.63243 +      pVar = &p->aVar[idx-1];
 1.63244 +      if( pVar->flags & MEM_Null ){
 1.63245 +        sqlite3StrAccumAppend(&out, "NULL", 4);
 1.63246 +      }else if( pVar->flags & MEM_Int ){
 1.63247 +        sqlite3XPrintf(&out, "%lld", pVar->u.i);
 1.63248 +      }else if( pVar->flags & MEM_Real ){
 1.63249 +        sqlite3XPrintf(&out, "%!.15g", pVar->r);
 1.63250 +      }else if( pVar->flags & MEM_Str ){
 1.63251 +#ifndef SQLITE_OMIT_UTF16
 1.63252 +        u8 enc = ENC(db);
 1.63253 +        if( enc!=SQLITE_UTF8 ){
 1.63254 +          Mem utf8;
 1.63255 +          memset(&utf8, 0, sizeof(utf8));
 1.63256 +          utf8.db = db;
 1.63257 +          sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
 1.63258 +          sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
 1.63259 +          sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
 1.63260 +          sqlite3VdbeMemRelease(&utf8);
 1.63261 +        }else
 1.63262 +#endif
 1.63263 +        {
 1.63264 +          sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
 1.63265 +        }
 1.63266 +      }else if( pVar->flags & MEM_Zero ){
 1.63267 +        sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
 1.63268 +      }else{
 1.63269 +        assert( pVar->flags & MEM_Blob );
 1.63270 +        sqlite3StrAccumAppend(&out, "x'", 2);
 1.63271 +        for(i=0; i<pVar->n; i++){
 1.63272 +          sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
 1.63273 +        }
 1.63274 +        sqlite3StrAccumAppend(&out, "'", 1);
 1.63275 +      }
 1.63276 +    }
 1.63277 +  }
 1.63278 +  return sqlite3StrAccumFinish(&out);
 1.63279 +}
 1.63280 +
 1.63281 +#endif /* #ifndef SQLITE_OMIT_TRACE */
 1.63282 +
 1.63283 +/*****************************************************************************
 1.63284 +** The following code implements the data-structure explaining logic
 1.63285 +** for the Vdbe.
 1.63286 +*/
 1.63287 +
 1.63288 +#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 1.63289 +
 1.63290 +/*
 1.63291 +** Allocate a new Explain object
 1.63292 +*/
 1.63293 +SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
 1.63294 +  if( pVdbe ){
 1.63295 +    Explain *p;
 1.63296 +    sqlite3BeginBenignMalloc();
 1.63297 +    p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
 1.63298 +    if( p ){
 1.63299 +      p->pVdbe = pVdbe;
 1.63300 +      sqlite3_free(pVdbe->pExplain);
 1.63301 +      pVdbe->pExplain = p;
 1.63302 +      sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
 1.63303 +                          SQLITE_MAX_LENGTH);
 1.63304 +      p->str.useMalloc = 2;
 1.63305 +    }else{
 1.63306 +      sqlite3EndBenignMalloc();
 1.63307 +    }
 1.63308 +  }
 1.63309 +}
 1.63310 +
 1.63311 +/*
 1.63312 +** Return true if the Explain ends with a new-line.
 1.63313 +*/
 1.63314 +static int endsWithNL(Explain *p){
 1.63315 +  return p && p->str.zText && p->str.nChar
 1.63316 +           && p->str.zText[p->str.nChar-1]=='\n';
 1.63317 +}
 1.63318 +    
 1.63319 +/*
 1.63320 +** Append text to the indentation
 1.63321 +*/
 1.63322 +SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
 1.63323 +  Explain *p;
 1.63324 +  if( pVdbe && (p = pVdbe->pExplain)!=0 ){
 1.63325 +    va_list ap;
 1.63326 +    if( p->nIndent && endsWithNL(p) ){
 1.63327 +      int n = p->nIndent;
 1.63328 +      if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
 1.63329 +      sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
 1.63330 +    }   
 1.63331 +    va_start(ap, zFormat);
 1.63332 +    sqlite3VXPrintf(&p->str, 1, zFormat, ap);
 1.63333 +    va_end(ap);
 1.63334 +  }
 1.63335 +}
 1.63336 +
 1.63337 +/*
 1.63338 +** Append a '\n' if there is not already one.
 1.63339 +*/
 1.63340 +SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
 1.63341 +  Explain *p;
 1.63342 +  if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
 1.63343 +    sqlite3StrAccumAppend(&p->str, "\n", 1);
 1.63344 +  }
 1.63345 +}
 1.63346 +
 1.63347 +/*
 1.63348 +** Push a new indentation level.  Subsequent lines will be indented
 1.63349 +** so that they begin at the current cursor position.
 1.63350 +*/
 1.63351 +SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
 1.63352 +  Explain *p;
 1.63353 +  if( pVdbe && (p = pVdbe->pExplain)!=0 ){
 1.63354 +    if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
 1.63355 +      const char *z = p->str.zText;
 1.63356 +      int i = p->str.nChar-1;
 1.63357 +      int x;
 1.63358 +      while( i>=0 && z[i]!='\n' ){ i--; }
 1.63359 +      x = (p->str.nChar - 1) - i;
 1.63360 +      if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
 1.63361 +        x = p->aIndent[p->nIndent-1];
 1.63362 +      }
 1.63363 +      p->aIndent[p->nIndent] = x;
 1.63364 +    }
 1.63365 +    p->nIndent++;
 1.63366 +  }
 1.63367 +}
 1.63368 +
 1.63369 +/*
 1.63370 +** Pop the indentation stack by one level.
 1.63371 +*/
 1.63372 +SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
 1.63373 +  if( p && p->pExplain ) p->pExplain->nIndent--;
 1.63374 +}
 1.63375 +
 1.63376 +/*
 1.63377 +** Free the indentation structure
 1.63378 +*/
 1.63379 +SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
 1.63380 +  if( pVdbe && pVdbe->pExplain ){
 1.63381 +    sqlite3_free(pVdbe->zExplain);
 1.63382 +    sqlite3ExplainNL(pVdbe);
 1.63383 +    pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
 1.63384 +    sqlite3_free(pVdbe->pExplain);
 1.63385 +    pVdbe->pExplain = 0;
 1.63386 +    sqlite3EndBenignMalloc();
 1.63387 +  }
 1.63388 +}
 1.63389 +
 1.63390 +/*
 1.63391 +** Return the explanation of a virtual machine.
 1.63392 +*/
 1.63393 +SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
 1.63394 +  return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
 1.63395 +}
 1.63396 +#endif /* defined(SQLITE_DEBUG) */
 1.63397 +
 1.63398 +/************** End of vdbetrace.c *******************************************/
 1.63399 +/************** Begin file vdbe.c ********************************************/
 1.63400 +/*
 1.63401 +** 2001 September 15
 1.63402 +**
 1.63403 +** The author disclaims copyright to this source code.  In place of
 1.63404 +** a legal notice, here is a blessing:
 1.63405 +**
 1.63406 +**    May you do good and not evil.
 1.63407 +**    May you find forgiveness for yourself and forgive others.
 1.63408 +**    May you share freely, never taking more than you give.
 1.63409 +**
 1.63410 +*************************************************************************
 1.63411 +** The code in this file implements execution method of the 
 1.63412 +** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
 1.63413 +** handles housekeeping details such as creating and deleting
 1.63414 +** VDBE instances.  This file is solely interested in executing
 1.63415 +** the VDBE program.
 1.63416 +**
 1.63417 +** In the external interface, an "sqlite3_stmt*" is an opaque pointer
 1.63418 +** to a VDBE.
 1.63419 +**
 1.63420 +** The SQL parser generates a program which is then executed by
 1.63421 +** the VDBE to do the work of the SQL statement.  VDBE programs are 
 1.63422 +** similar in form to assembly language.  The program consists of
 1.63423 +** a linear sequence of operations.  Each operation has an opcode 
 1.63424 +** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4 
 1.63425 +** is a null-terminated string.  Operand P5 is an unsigned character.
 1.63426 +** Few opcodes use all 5 operands.
 1.63427 +**
 1.63428 +** Computation results are stored on a set of registers numbered beginning
 1.63429 +** with 1 and going up to Vdbe.nMem.  Each register can store
 1.63430 +** either an integer, a null-terminated string, a floating point
 1.63431 +** number, or the SQL "NULL" value.  An implicit conversion from one
 1.63432 +** type to the other occurs as necessary.
 1.63433 +** 
 1.63434 +** Most of the code in this file is taken up by the sqlite3VdbeExec()
 1.63435 +** function which does the work of interpreting a VDBE program.
 1.63436 +** But other routines are also provided to help in building up
 1.63437 +** a program instruction by instruction.
 1.63438 +**
 1.63439 +** Various scripts scan this source file in order to generate HTML
 1.63440 +** documentation, headers files, or other derived files.  The formatting
 1.63441 +** of the code in this file is, therefore, important.  See other comments
 1.63442 +** in this file for details.  If in doubt, do not deviate from existing
 1.63443 +** commenting and indentation practices when changing or adding code.
 1.63444 +*/
 1.63445 +
 1.63446 +/*
 1.63447 +** Invoke this macro on memory cells just prior to changing the
 1.63448 +** value of the cell.  This macro verifies that shallow copies are
 1.63449 +** not misused.
 1.63450 +*/
 1.63451 +#ifdef SQLITE_DEBUG
 1.63452 +# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
 1.63453 +#else
 1.63454 +# define memAboutToChange(P,M)
 1.63455 +#endif
 1.63456 +
 1.63457 +/*
 1.63458 +** The following global variable is incremented every time a cursor
 1.63459 +** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
 1.63460 +** procedures use this information to make sure that indices are
 1.63461 +** working correctly.  This variable has no function other than to
 1.63462 +** help verify the correct operation of the library.
 1.63463 +*/
 1.63464 +#ifdef SQLITE_TEST
 1.63465 +SQLITE_API int sqlite3_search_count = 0;
 1.63466 +#endif
 1.63467 +
 1.63468 +/*
 1.63469 +** When this global variable is positive, it gets decremented once before
 1.63470 +** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
 1.63471 +** field of the sqlite3 structure is set in order to simulate an interrupt.
 1.63472 +**
 1.63473 +** This facility is used for testing purposes only.  It does not function
 1.63474 +** in an ordinary build.
 1.63475 +*/
 1.63476 +#ifdef SQLITE_TEST
 1.63477 +SQLITE_API int sqlite3_interrupt_count = 0;
 1.63478 +#endif
 1.63479 +
 1.63480 +/*
 1.63481 +** The next global variable is incremented each type the OP_Sort opcode
 1.63482 +** is executed.  The test procedures use this information to make sure that
 1.63483 +** sorting is occurring or not occurring at appropriate times.   This variable
 1.63484 +** has no function other than to help verify the correct operation of the
 1.63485 +** library.
 1.63486 +*/
 1.63487 +#ifdef SQLITE_TEST
 1.63488 +SQLITE_API int sqlite3_sort_count = 0;
 1.63489 +#endif
 1.63490 +
 1.63491 +/*
 1.63492 +** The next global variable records the size of the largest MEM_Blob
 1.63493 +** or MEM_Str that has been used by a VDBE opcode.  The test procedures
 1.63494 +** use this information to make sure that the zero-blob functionality
 1.63495 +** is working correctly.   This variable has no function other than to
 1.63496 +** help verify the correct operation of the library.
 1.63497 +*/
 1.63498 +#ifdef SQLITE_TEST
 1.63499 +SQLITE_API int sqlite3_max_blobsize = 0;
 1.63500 +static void updateMaxBlobsize(Mem *p){
 1.63501 +  if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
 1.63502 +    sqlite3_max_blobsize = p->n;
 1.63503 +  }
 1.63504 +}
 1.63505 +#endif
 1.63506 +
 1.63507 +/*
 1.63508 +** The next global variable is incremented each type the OP_Found opcode
 1.63509 +** is executed. This is used to test whether or not the foreign key
 1.63510 +** operation implemented using OP_FkIsZero is working. This variable
 1.63511 +** has no function other than to help verify the correct operation of the
 1.63512 +** library.
 1.63513 +*/
 1.63514 +#ifdef SQLITE_TEST
 1.63515 +SQLITE_API int sqlite3_found_count = 0;
 1.63516 +#endif
 1.63517 +
 1.63518 +/*
 1.63519 +** Test a register to see if it exceeds the current maximum blob size.
 1.63520 +** If it does, record the new maximum blob size.
 1.63521 +*/
 1.63522 +#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
 1.63523 +# define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
 1.63524 +#else
 1.63525 +# define UPDATE_MAX_BLOBSIZE(P)
 1.63526 +#endif
 1.63527 +
 1.63528 +/*
 1.63529 +** Convert the given register into a string if it isn't one
 1.63530 +** already. Return non-zero if a malloc() fails.
 1.63531 +*/
 1.63532 +#define Stringify(P, enc) \
 1.63533 +   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
 1.63534 +     { goto no_mem; }
 1.63535 +
 1.63536 +/*
 1.63537 +** An ephemeral string value (signified by the MEM_Ephem flag) contains
 1.63538 +** a pointer to a dynamically allocated string where some other entity
 1.63539 +** is responsible for deallocating that string.  Because the register
 1.63540 +** does not control the string, it might be deleted without the register
 1.63541 +** knowing it.
 1.63542 +**
 1.63543 +** This routine converts an ephemeral string into a dynamically allocated
 1.63544 +** string that the register itself controls.  In other words, it
 1.63545 +** converts an MEM_Ephem string into an MEM_Dyn string.
 1.63546 +*/
 1.63547 +#define Deephemeralize(P) \
 1.63548 +   if( ((P)->flags&MEM_Ephem)!=0 \
 1.63549 +       && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
 1.63550 +
 1.63551 +/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
 1.63552 +#ifdef SQLITE_OMIT_MERGE_SORT
 1.63553 +# define isSorter(x) 0
 1.63554 +#else
 1.63555 +# define isSorter(x) ((x)->pSorter!=0)
 1.63556 +#endif
 1.63557 +
 1.63558 +/*
 1.63559 +** Argument pMem points at a register that will be passed to a
 1.63560 +** user-defined function or returned to the user as the result of a query.
 1.63561 +** This routine sets the pMem->type variable used by the sqlite3_value_*() 
 1.63562 +** routines.
 1.63563 +*/
 1.63564 +SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
 1.63565 +  int flags = pMem->flags;
 1.63566 +  if( flags & MEM_Null ){
 1.63567 +    pMem->type = SQLITE_NULL;
 1.63568 +  }
 1.63569 +  else if( flags & MEM_Int ){
 1.63570 +    pMem->type = SQLITE_INTEGER;
 1.63571 +  }
 1.63572 +  else if( flags & MEM_Real ){
 1.63573 +    pMem->type = SQLITE_FLOAT;
 1.63574 +  }
 1.63575 +  else if( flags & MEM_Str ){
 1.63576 +    pMem->type = SQLITE_TEXT;
 1.63577 +  }else{
 1.63578 +    pMem->type = SQLITE_BLOB;
 1.63579 +  }
 1.63580 +}
 1.63581 +
 1.63582 +/*
 1.63583 +** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
 1.63584 +** if we run out of memory.
 1.63585 +*/
 1.63586 +static VdbeCursor *allocateCursor(
 1.63587 +  Vdbe *p,              /* The virtual machine */
 1.63588 +  int iCur,             /* Index of the new VdbeCursor */
 1.63589 +  int nField,           /* Number of fields in the table or index */
 1.63590 +  int iDb,              /* Database the cursor belongs to, or -1 */
 1.63591 +  int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
 1.63592 +){
 1.63593 +  /* Find the memory cell that will be used to store the blob of memory
 1.63594 +  ** required for this VdbeCursor structure. It is convenient to use a 
 1.63595 +  ** vdbe memory cell to manage the memory allocation required for a
 1.63596 +  ** VdbeCursor structure for the following reasons:
 1.63597 +  **
 1.63598 +  **   * Sometimes cursor numbers are used for a couple of different
 1.63599 +  **     purposes in a vdbe program. The different uses might require
 1.63600 +  **     different sized allocations. Memory cells provide growable
 1.63601 +  **     allocations.
 1.63602 +  **
 1.63603 +  **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
 1.63604 +  **     be freed lazily via the sqlite3_release_memory() API. This
 1.63605 +  **     minimizes the number of malloc calls made by the system.
 1.63606 +  **
 1.63607 +  ** Memory cells for cursors are allocated at the top of the address
 1.63608 +  ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
 1.63609 +  ** cursor 1 is managed by memory cell (p->nMem-1), etc.
 1.63610 +  */
 1.63611 +  Mem *pMem = &p->aMem[p->nMem-iCur];
 1.63612 +
 1.63613 +  int nByte;
 1.63614 +  VdbeCursor *pCx = 0;
 1.63615 +  nByte = 
 1.63616 +      ROUND8(sizeof(VdbeCursor)) + 
 1.63617 +      (isBtreeCursor?sqlite3BtreeCursorSize():0) + 
 1.63618 +      2*nField*sizeof(u32);
 1.63619 +
 1.63620 +  assert( iCur<p->nCursor );
 1.63621 +  if( p->apCsr[iCur] ){
 1.63622 +    sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
 1.63623 +    p->apCsr[iCur] = 0;
 1.63624 +  }
 1.63625 +  if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
 1.63626 +    p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
 1.63627 +    memset(pCx, 0, sizeof(VdbeCursor));
 1.63628 +    pCx->iDb = iDb;
 1.63629 +    pCx->nField = nField;
 1.63630 +    if( nField ){
 1.63631 +      pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
 1.63632 +    }
 1.63633 +    if( isBtreeCursor ){
 1.63634 +      pCx->pCursor = (BtCursor*)
 1.63635 +          &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
 1.63636 +      sqlite3BtreeCursorZero(pCx->pCursor);
 1.63637 +    }
 1.63638 +  }
 1.63639 +  return pCx;
 1.63640 +}
 1.63641 +
 1.63642 +/*
 1.63643 +** Try to convert a value into a numeric representation if we can
 1.63644 +** do so without loss of information.  In other words, if the string
 1.63645 +** looks like a number, convert it into a number.  If it does not
 1.63646 +** look like a number, leave it alone.
 1.63647 +*/
 1.63648 +static void applyNumericAffinity(Mem *pRec){
 1.63649 +  if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
 1.63650 +    double rValue;
 1.63651 +    i64 iValue;
 1.63652 +    u8 enc = pRec->enc;
 1.63653 +    if( (pRec->flags&MEM_Str)==0 ) return;
 1.63654 +    if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
 1.63655 +    if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
 1.63656 +      pRec->u.i = iValue;
 1.63657 +      pRec->flags |= MEM_Int;
 1.63658 +    }else{
 1.63659 +      pRec->r = rValue;
 1.63660 +      pRec->flags |= MEM_Real;
 1.63661 +    }
 1.63662 +  }
 1.63663 +}
 1.63664 +
 1.63665 +/*
 1.63666 +** Processing is determine by the affinity parameter:
 1.63667 +**
 1.63668 +** SQLITE_AFF_INTEGER:
 1.63669 +** SQLITE_AFF_REAL:
 1.63670 +** SQLITE_AFF_NUMERIC:
 1.63671 +**    Try to convert pRec to an integer representation or a 
 1.63672 +**    floating-point representation if an integer representation
 1.63673 +**    is not possible.  Note that the integer representation is
 1.63674 +**    always preferred, even if the affinity is REAL, because
 1.63675 +**    an integer representation is more space efficient on disk.
 1.63676 +**
 1.63677 +** SQLITE_AFF_TEXT:
 1.63678 +**    Convert pRec to a text representation.
 1.63679 +**
 1.63680 +** SQLITE_AFF_NONE:
 1.63681 +**    No-op.  pRec is unchanged.
 1.63682 +*/
 1.63683 +static void applyAffinity(
 1.63684 +  Mem *pRec,          /* The value to apply affinity to */
 1.63685 +  char affinity,      /* The affinity to be applied */
 1.63686 +  u8 enc              /* Use this text encoding */
 1.63687 +){
 1.63688 +  if( affinity==SQLITE_AFF_TEXT ){
 1.63689 +    /* Only attempt the conversion to TEXT if there is an integer or real
 1.63690 +    ** representation (blob and NULL do not get converted) but no string
 1.63691 +    ** representation.
 1.63692 +    */
 1.63693 +    if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
 1.63694 +      sqlite3VdbeMemStringify(pRec, enc);
 1.63695 +    }
 1.63696 +    pRec->flags &= ~(MEM_Real|MEM_Int);
 1.63697 +  }else if( affinity!=SQLITE_AFF_NONE ){
 1.63698 +    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
 1.63699 +             || affinity==SQLITE_AFF_NUMERIC );
 1.63700 +    applyNumericAffinity(pRec);
 1.63701 +    if( pRec->flags & MEM_Real ){
 1.63702 +      sqlite3VdbeIntegerAffinity(pRec);
 1.63703 +    }
 1.63704 +  }
 1.63705 +}
 1.63706 +
 1.63707 +/*
 1.63708 +** Try to convert the type of a function argument or a result column
 1.63709 +** into a numeric representation.  Use either INTEGER or REAL whichever
 1.63710 +** is appropriate.  But only do the conversion if it is possible without
 1.63711 +** loss of information and return the revised type of the argument.
 1.63712 +*/
 1.63713 +SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
 1.63714 +  Mem *pMem = (Mem*)pVal;
 1.63715 +  if( pMem->type==SQLITE_TEXT ){
 1.63716 +    applyNumericAffinity(pMem);
 1.63717 +    sqlite3VdbeMemStoreType(pMem);
 1.63718 +  }
 1.63719 +  return pMem->type;
 1.63720 +}
 1.63721 +
 1.63722 +/*
 1.63723 +** Exported version of applyAffinity(). This one works on sqlite3_value*, 
 1.63724 +** not the internal Mem* type.
 1.63725 +*/
 1.63726 +SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
 1.63727 +  sqlite3_value *pVal, 
 1.63728 +  u8 affinity, 
 1.63729 +  u8 enc
 1.63730 +){
 1.63731 +  applyAffinity((Mem *)pVal, affinity, enc);
 1.63732 +}
 1.63733 +
 1.63734 +#ifdef SQLITE_DEBUG
 1.63735 +/*
 1.63736 +** Write a nice string representation of the contents of cell pMem
 1.63737 +** into buffer zBuf, length nBuf.
 1.63738 +*/
 1.63739 +SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
 1.63740 +  char *zCsr = zBuf;
 1.63741 +  int f = pMem->flags;
 1.63742 +
 1.63743 +  static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
 1.63744 +
 1.63745 +  if( f&MEM_Blob ){
 1.63746 +    int i;
 1.63747 +    char c;
 1.63748 +    if( f & MEM_Dyn ){
 1.63749 +      c = 'z';
 1.63750 +      assert( (f & (MEM_Static|MEM_Ephem))==0 );
 1.63751 +    }else if( f & MEM_Static ){
 1.63752 +      c = 't';
 1.63753 +      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
 1.63754 +    }else if( f & MEM_Ephem ){
 1.63755 +      c = 'e';
 1.63756 +      assert( (f & (MEM_Static|MEM_Dyn))==0 );
 1.63757 +    }else{
 1.63758 +      c = 's';
 1.63759 +    }
 1.63760 +
 1.63761 +    sqlite3_snprintf(100, zCsr, "%c", c);
 1.63762 +    zCsr += sqlite3Strlen30(zCsr);
 1.63763 +    sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
 1.63764 +    zCsr += sqlite3Strlen30(zCsr);
 1.63765 +    for(i=0; i<16 && i<pMem->n; i++){
 1.63766 +      sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
 1.63767 +      zCsr += sqlite3Strlen30(zCsr);
 1.63768 +    }
 1.63769 +    for(i=0; i<16 && i<pMem->n; i++){
 1.63770 +      char z = pMem->z[i];
 1.63771 +      if( z<32 || z>126 ) *zCsr++ = '.';
 1.63772 +      else *zCsr++ = z;
 1.63773 +    }
 1.63774 +
 1.63775 +    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
 1.63776 +    zCsr += sqlite3Strlen30(zCsr);
 1.63777 +    if( f & MEM_Zero ){
 1.63778 +      sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
 1.63779 +      zCsr += sqlite3Strlen30(zCsr);
 1.63780 +    }
 1.63781 +    *zCsr = '\0';
 1.63782 +  }else if( f & MEM_Str ){
 1.63783 +    int j, k;
 1.63784 +    zBuf[0] = ' ';
 1.63785 +    if( f & MEM_Dyn ){
 1.63786 +      zBuf[1] = 'z';
 1.63787 +      assert( (f & (MEM_Static|MEM_Ephem))==0 );
 1.63788 +    }else if( f & MEM_Static ){
 1.63789 +      zBuf[1] = 't';
 1.63790 +      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
 1.63791 +    }else if( f & MEM_Ephem ){
 1.63792 +      zBuf[1] = 'e';
 1.63793 +      assert( (f & (MEM_Static|MEM_Dyn))==0 );
 1.63794 +    }else{
 1.63795 +      zBuf[1] = 's';
 1.63796 +    }
 1.63797 +    k = 2;
 1.63798 +    sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
 1.63799 +    k += sqlite3Strlen30(&zBuf[k]);
 1.63800 +    zBuf[k++] = '[';
 1.63801 +    for(j=0; j<15 && j<pMem->n; j++){
 1.63802 +      u8 c = pMem->z[j];
 1.63803 +      if( c>=0x20 && c<0x7f ){
 1.63804 +        zBuf[k++] = c;
 1.63805 +      }else{
 1.63806 +        zBuf[k++] = '.';
 1.63807 +      }
 1.63808 +    }
 1.63809 +    zBuf[k++] = ']';
 1.63810 +    sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
 1.63811 +    k += sqlite3Strlen30(&zBuf[k]);
 1.63812 +    zBuf[k++] = 0;
 1.63813 +  }
 1.63814 +}
 1.63815 +#endif
 1.63816 +
 1.63817 +#ifdef SQLITE_DEBUG
 1.63818 +/*
 1.63819 +** Print the value of a register for tracing purposes:
 1.63820 +*/
 1.63821 +static void memTracePrint(FILE *out, Mem *p){
 1.63822 +  if( p->flags & MEM_Invalid ){
 1.63823 +    fprintf(out, " undefined");
 1.63824 +  }else if( p->flags & MEM_Null ){
 1.63825 +    fprintf(out, " NULL");
 1.63826 +  }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
 1.63827 +    fprintf(out, " si:%lld", p->u.i);
 1.63828 +  }else if( p->flags & MEM_Int ){
 1.63829 +    fprintf(out, " i:%lld", p->u.i);
 1.63830 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.63831 +  }else if( p->flags & MEM_Real ){
 1.63832 +    fprintf(out, " r:%g", p->r);
 1.63833 +#endif
 1.63834 +  }else if( p->flags & MEM_RowSet ){
 1.63835 +    fprintf(out, " (rowset)");
 1.63836 +  }else{
 1.63837 +    char zBuf[200];
 1.63838 +    sqlite3VdbeMemPrettyPrint(p, zBuf);
 1.63839 +    fprintf(out, " ");
 1.63840 +    fprintf(out, "%s", zBuf);
 1.63841 +  }
 1.63842 +}
 1.63843 +static void registerTrace(FILE *out, int iReg, Mem *p){
 1.63844 +  fprintf(out, "REG[%d] = ", iReg);
 1.63845 +  memTracePrint(out, p);
 1.63846 +  fprintf(out, "\n");
 1.63847 +}
 1.63848 +#endif
 1.63849 +
 1.63850 +#ifdef SQLITE_DEBUG
 1.63851 +#  define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
 1.63852 +#else
 1.63853 +#  define REGISTER_TRACE(R,M)
 1.63854 +#endif
 1.63855 +
 1.63856 +
 1.63857 +#ifdef VDBE_PROFILE
 1.63858 +
 1.63859 +/* 
 1.63860 +** hwtime.h contains inline assembler code for implementing 
 1.63861 +** high-performance timing routines.
 1.63862 +*/
 1.63863 +/************** Include hwtime.h in the middle of vdbe.c *********************/
 1.63864 +/************** Begin file hwtime.h ******************************************/
 1.63865 +/*
 1.63866 +** 2008 May 27
 1.63867 +**
 1.63868 +** The author disclaims copyright to this source code.  In place of
 1.63869 +** a legal notice, here is a blessing:
 1.63870 +**
 1.63871 +**    May you do good and not evil.
 1.63872 +**    May you find forgiveness for yourself and forgive others.
 1.63873 +**    May you share freely, never taking more than you give.
 1.63874 +**
 1.63875 +******************************************************************************
 1.63876 +**
 1.63877 +** This file contains inline asm code for retrieving "high-performance"
 1.63878 +** counters for x86 class CPUs.
 1.63879 +*/
 1.63880 +#ifndef _HWTIME_H_
 1.63881 +#define _HWTIME_H_
 1.63882 +
 1.63883 +/*
 1.63884 +** The following routine only works on pentium-class (or newer) processors.
 1.63885 +** It uses the RDTSC opcode to read the cycle count value out of the
 1.63886 +** processor and returns that value.  This can be used for high-res
 1.63887 +** profiling.
 1.63888 +*/
 1.63889 +#if (defined(__GNUC__) || defined(_MSC_VER)) && \
 1.63890 +      (defined(i386) || defined(__i386__) || defined(_M_IX86))
 1.63891 +
 1.63892 +  #if defined(__GNUC__)
 1.63893 +
 1.63894 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.63895 +     unsigned int lo, hi;
 1.63896 +     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 1.63897 +     return (sqlite_uint64)hi << 32 | lo;
 1.63898 +  }
 1.63899 +
 1.63900 +  #elif defined(_MSC_VER)
 1.63901 +
 1.63902 +  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 1.63903 +     __asm {
 1.63904 +        rdtsc
 1.63905 +        ret       ; return value at EDX:EAX
 1.63906 +     }
 1.63907 +  }
 1.63908 +
 1.63909 +  #endif
 1.63910 +
 1.63911 +#elif (defined(__GNUC__) && defined(__x86_64__))
 1.63912 +
 1.63913 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.63914 +      unsigned long val;
 1.63915 +      __asm__ __volatile__ ("rdtsc" : "=A" (val));
 1.63916 +      return val;
 1.63917 +  }
 1.63918 + 
 1.63919 +#elif (defined(__GNUC__) && defined(__ppc__))
 1.63920 +
 1.63921 +  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 1.63922 +      unsigned long long retval;
 1.63923 +      unsigned long junk;
 1.63924 +      __asm__ __volatile__ ("\n\
 1.63925 +          1:      mftbu   %1\n\
 1.63926 +                  mftb    %L0\n\
 1.63927 +                  mftbu   %0\n\
 1.63928 +                  cmpw    %0,%1\n\
 1.63929 +                  bne     1b"
 1.63930 +                  : "=r" (retval), "=r" (junk));
 1.63931 +      return retval;
 1.63932 +  }
 1.63933 +
 1.63934 +#else
 1.63935 +
 1.63936 +  #error Need implementation of sqlite3Hwtime() for your platform.
 1.63937 +
 1.63938 +  /*
 1.63939 +  ** To compile without implementing sqlite3Hwtime() for your platform,
 1.63940 +  ** you can remove the above #error and use the following
 1.63941 +  ** stub function.  You will lose timing support for many
 1.63942 +  ** of the debugging and testing utilities, but it should at
 1.63943 +  ** least compile and run.
 1.63944 +  */
 1.63945 +SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 1.63946 +
 1.63947 +#endif
 1.63948 +
 1.63949 +#endif /* !defined(_HWTIME_H_) */
 1.63950 +
 1.63951 +/************** End of hwtime.h **********************************************/
 1.63952 +/************** Continuing where we left off in vdbe.c ***********************/
 1.63953 +
 1.63954 +#endif
 1.63955 +
 1.63956 +/*
 1.63957 +** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
 1.63958 +** sqlite3_interrupt() routine has been called.  If it has been, then
 1.63959 +** processing of the VDBE program is interrupted.
 1.63960 +**
 1.63961 +** This macro added to every instruction that does a jump in order to
 1.63962 +** implement a loop.  This test used to be on every single instruction,
 1.63963 +** but that meant we more testing than we needed.  By only testing the
 1.63964 +** flag on jump instructions, we get a (small) speed improvement.
 1.63965 +*/
 1.63966 +#define CHECK_FOR_INTERRUPT \
 1.63967 +   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 1.63968 +
 1.63969 +
 1.63970 +#ifndef NDEBUG
 1.63971 +/*
 1.63972 +** This function is only called from within an assert() expression. It
 1.63973 +** checks that the sqlite3.nTransaction variable is correctly set to
 1.63974 +** the number of non-transaction savepoints currently in the 
 1.63975 +** linked list starting at sqlite3.pSavepoint.
 1.63976 +** 
 1.63977 +** Usage:
 1.63978 +**
 1.63979 +**     assert( checkSavepointCount(db) );
 1.63980 +*/
 1.63981 +static int checkSavepointCount(sqlite3 *db){
 1.63982 +  int n = 0;
 1.63983 +  Savepoint *p;
 1.63984 +  for(p=db->pSavepoint; p; p=p->pNext) n++;
 1.63985 +  assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
 1.63986 +  return 1;
 1.63987 +}
 1.63988 +#endif
 1.63989 +
 1.63990 +/*
 1.63991 +** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
 1.63992 +** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
 1.63993 +** in memory obtained from sqlite3DbMalloc).
 1.63994 +*/
 1.63995 +static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
 1.63996 +  sqlite3 *db = p->db;
 1.63997 +  sqlite3DbFree(db, p->zErrMsg);
 1.63998 +  p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
 1.63999 +  sqlite3_free(pVtab->zErrMsg);
 1.64000 +  pVtab->zErrMsg = 0;
 1.64001 +}
 1.64002 +
 1.64003 +
 1.64004 +/*
 1.64005 +** Execute as much of a VDBE program as we can then return.
 1.64006 +**
 1.64007 +** sqlite3VdbeMakeReady() must be called before this routine in order to
 1.64008 +** close the program with a final OP_Halt and to set up the callbacks
 1.64009 +** and the error message pointer.
 1.64010 +**
 1.64011 +** Whenever a row or result data is available, this routine will either
 1.64012 +** invoke the result callback (if there is one) or return with
 1.64013 +** SQLITE_ROW.
 1.64014 +**
 1.64015 +** If an attempt is made to open a locked database, then this routine
 1.64016 +** will either invoke the busy callback (if there is one) or it will
 1.64017 +** return SQLITE_BUSY.
 1.64018 +**
 1.64019 +** If an error occurs, an error message is written to memory obtained
 1.64020 +** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
 1.64021 +** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
 1.64022 +**
 1.64023 +** If the callback ever returns non-zero, then the program exits
 1.64024 +** immediately.  There will be no error message but the p->rc field is
 1.64025 +** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
 1.64026 +**
 1.64027 +** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
 1.64028 +** routine to return SQLITE_ERROR.
 1.64029 +**
 1.64030 +** Other fatal errors return SQLITE_ERROR.
 1.64031 +**
 1.64032 +** After this routine has finished, sqlite3VdbeFinalize() should be
 1.64033 +** used to clean up the mess that was left behind.
 1.64034 +*/
 1.64035 +SQLITE_PRIVATE int sqlite3VdbeExec(
 1.64036 +  Vdbe *p                    /* The VDBE */
 1.64037 +){
 1.64038 +  int pc=0;                  /* The program counter */
 1.64039 +  Op *aOp = p->aOp;          /* Copy of p->aOp */
 1.64040 +  Op *pOp;                   /* Current operation */
 1.64041 +  int rc = SQLITE_OK;        /* Value to return */
 1.64042 +  sqlite3 *db = p->db;       /* The database */
 1.64043 +  u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
 1.64044 +  u8 encoding = ENC(db);     /* The database encoding */
 1.64045 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 1.64046 +  int checkProgress;         /* True if progress callbacks are enabled */
 1.64047 +  int nProgressOps = 0;      /* Opcodes executed since progress callback. */
 1.64048 +#endif
 1.64049 +  Mem *aMem = p->aMem;       /* Copy of p->aMem */
 1.64050 +  Mem *pIn1 = 0;             /* 1st input operand */
 1.64051 +  Mem *pIn2 = 0;             /* 2nd input operand */
 1.64052 +  Mem *pIn3 = 0;             /* 3rd input operand */
 1.64053 +  Mem *pOut = 0;             /* Output operand */
 1.64054 +  int iCompare = 0;          /* Result of last OP_Compare operation */
 1.64055 +  int *aPermute = 0;         /* Permutation of columns for OP_Compare */
 1.64056 +  i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
 1.64057 +#ifdef VDBE_PROFILE
 1.64058 +  u64 start;                 /* CPU clock count at start of opcode */
 1.64059 +  int origPc;                /* Program counter at start of opcode */
 1.64060 +#endif
 1.64061 +  /********************************************************************
 1.64062 +  ** Automatically generated code
 1.64063 +  **
 1.64064 +  ** The following union is automatically generated by the
 1.64065 +  ** vdbe-compress.tcl script.  The purpose of this union is to
 1.64066 +  ** reduce the amount of stack space required by this function.
 1.64067 +  ** See comments in the vdbe-compress.tcl script for details.
 1.64068 +  */
 1.64069 +  union vdbeExecUnion {
 1.64070 +    struct OP_Yield_stack_vars {
 1.64071 +      int pcDest;
 1.64072 +    } aa;
 1.64073 +    struct OP_Null_stack_vars {
 1.64074 +      int cnt;
 1.64075 +      u16 nullFlag;
 1.64076 +    } ab;
 1.64077 +    struct OP_Variable_stack_vars {
 1.64078 +      Mem *pVar;       /* Value being transferred */
 1.64079 +    } ac;
 1.64080 +    struct OP_Move_stack_vars {
 1.64081 +      char *zMalloc;   /* Holding variable for allocated memory */
 1.64082 +      int n;           /* Number of registers left to copy */
 1.64083 +      int p1;          /* Register to copy from */
 1.64084 +      int p2;          /* Register to copy to */
 1.64085 +    } ad;
 1.64086 +    struct OP_Copy_stack_vars {
 1.64087 +      int n;
 1.64088 +    } ae;
 1.64089 +    struct OP_ResultRow_stack_vars {
 1.64090 +      Mem *pMem;
 1.64091 +      int i;
 1.64092 +    } af;
 1.64093 +    struct OP_Concat_stack_vars {
 1.64094 +      i64 nByte;
 1.64095 +    } ag;
 1.64096 +    struct OP_Remainder_stack_vars {
 1.64097 +      char bIntint;   /* Started out as two integer operands */
 1.64098 +      int flags;      /* Combined MEM_* flags from both inputs */
 1.64099 +      i64 iA;         /* Integer value of left operand */
 1.64100 +      i64 iB;         /* Integer value of right operand */
 1.64101 +      double rA;      /* Real value of left operand */
 1.64102 +      double rB;      /* Real value of right operand */
 1.64103 +    } ah;
 1.64104 +    struct OP_Function_stack_vars {
 1.64105 +      int i;
 1.64106 +      Mem *pArg;
 1.64107 +      sqlite3_context ctx;
 1.64108 +      sqlite3_value **apVal;
 1.64109 +      int n;
 1.64110 +    } ai;
 1.64111 +    struct OP_ShiftRight_stack_vars {
 1.64112 +      i64 iA;
 1.64113 +      u64 uA;
 1.64114 +      i64 iB;
 1.64115 +      u8 op;
 1.64116 +    } aj;
 1.64117 +    struct OP_Ge_stack_vars {
 1.64118 +      int res;            /* Result of the comparison of pIn1 against pIn3 */
 1.64119 +      char affinity;      /* Affinity to use for comparison */
 1.64120 +      u16 flags1;         /* Copy of initial value of pIn1->flags */
 1.64121 +      u16 flags3;         /* Copy of initial value of pIn3->flags */
 1.64122 +    } ak;
 1.64123 +    struct OP_Compare_stack_vars {
 1.64124 +      int n;
 1.64125 +      int i;
 1.64126 +      int p1;
 1.64127 +      int p2;
 1.64128 +      const KeyInfo *pKeyInfo;
 1.64129 +      int idx;
 1.64130 +      CollSeq *pColl;    /* Collating sequence to use on this term */
 1.64131 +      int bRev;          /* True for DESCENDING sort order */
 1.64132 +    } al;
 1.64133 +    struct OP_Or_stack_vars {
 1.64134 +      int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 1.64135 +      int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 1.64136 +    } am;
 1.64137 +    struct OP_IfNot_stack_vars {
 1.64138 +      int c;
 1.64139 +    } an;
 1.64140 +    struct OP_Column_stack_vars {
 1.64141 +      u32 payloadSize;   /* Number of bytes in the record */
 1.64142 +      i64 payloadSize64; /* Number of bytes in the record */
 1.64143 +      int p1;            /* P1 value of the opcode */
 1.64144 +      int p2;            /* column number to retrieve */
 1.64145 +      VdbeCursor *pC;    /* The VDBE cursor */
 1.64146 +      char *zRec;        /* Pointer to complete record-data */
 1.64147 +      BtCursor *pCrsr;   /* The BTree cursor */
 1.64148 +      u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
 1.64149 +      u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
 1.64150 +      int nField;        /* number of fields in the record */
 1.64151 +      int len;           /* The length of the serialized data for the column */
 1.64152 +      int i;             /* Loop counter */
 1.64153 +      char *zData;       /* Part of the record being decoded */
 1.64154 +      Mem *pDest;        /* Where to write the extracted value */
 1.64155 +      Mem sMem;          /* For storing the record being decoded */
 1.64156 +      u8 *zIdx;          /* Index into header */
 1.64157 +      u8 *zEndHdr;       /* Pointer to first byte after the header */
 1.64158 +      u32 offset;        /* Offset into the data */
 1.64159 +      u32 szField;       /* Number of bytes in the content of a field */
 1.64160 +      int szHdr;         /* Size of the header size field at start of record */
 1.64161 +      int avail;         /* Number of bytes of available data */
 1.64162 +      u32 t;             /* A type code from the record header */
 1.64163 +      Mem *pReg;         /* PseudoTable input register */
 1.64164 +    } ao;
 1.64165 +    struct OP_Affinity_stack_vars {
 1.64166 +      const char *zAffinity;   /* The affinity to be applied */
 1.64167 +      char cAff;               /* A single character of affinity */
 1.64168 +    } ap;
 1.64169 +    struct OP_MakeRecord_stack_vars {
 1.64170 +      u8 *zNewRecord;        /* A buffer to hold the data for the new record */
 1.64171 +      Mem *pRec;             /* The new record */
 1.64172 +      u64 nData;             /* Number of bytes of data space */
 1.64173 +      int nHdr;              /* Number of bytes of header space */
 1.64174 +      i64 nByte;             /* Data space required for this record */
 1.64175 +      int nZero;             /* Number of zero bytes at the end of the record */
 1.64176 +      int nVarint;           /* Number of bytes in a varint */
 1.64177 +      u32 serial_type;       /* Type field */
 1.64178 +      Mem *pData0;           /* First field to be combined into the record */
 1.64179 +      Mem *pLast;            /* Last field of the record */
 1.64180 +      int nField;            /* Number of fields in the record */
 1.64181 +      char *zAffinity;       /* The affinity string for the record */
 1.64182 +      int file_format;       /* File format to use for encoding */
 1.64183 +      int i;                 /* Space used in zNewRecord[] */
 1.64184 +      int len;               /* Length of a field */
 1.64185 +    } aq;
 1.64186 +    struct OP_Count_stack_vars {
 1.64187 +      i64 nEntry;
 1.64188 +      BtCursor *pCrsr;
 1.64189 +    } ar;
 1.64190 +    struct OP_Savepoint_stack_vars {
 1.64191 +      int p1;                         /* Value of P1 operand */
 1.64192 +      char *zName;                    /* Name of savepoint */
 1.64193 +      int nName;
 1.64194 +      Savepoint *pNew;
 1.64195 +      Savepoint *pSavepoint;
 1.64196 +      Savepoint *pTmp;
 1.64197 +      int iSavepoint;
 1.64198 +      int ii;
 1.64199 +    } as;
 1.64200 +    struct OP_AutoCommit_stack_vars {
 1.64201 +      int desiredAutoCommit;
 1.64202 +      int iRollback;
 1.64203 +      int turnOnAC;
 1.64204 +    } at;
 1.64205 +    struct OP_Transaction_stack_vars {
 1.64206 +      Btree *pBt;
 1.64207 +    } au;
 1.64208 +    struct OP_ReadCookie_stack_vars {
 1.64209 +      int iMeta;
 1.64210 +      int iDb;
 1.64211 +      int iCookie;
 1.64212 +    } av;
 1.64213 +    struct OP_SetCookie_stack_vars {
 1.64214 +      Db *pDb;
 1.64215 +    } aw;
 1.64216 +    struct OP_VerifyCookie_stack_vars {
 1.64217 +      int iMeta;
 1.64218 +      int iGen;
 1.64219 +      Btree *pBt;
 1.64220 +    } ax;
 1.64221 +    struct OP_OpenWrite_stack_vars {
 1.64222 +      int nField;
 1.64223 +      KeyInfo *pKeyInfo;
 1.64224 +      int p2;
 1.64225 +      int iDb;
 1.64226 +      int wrFlag;
 1.64227 +      Btree *pX;
 1.64228 +      VdbeCursor *pCur;
 1.64229 +      Db *pDb;
 1.64230 +    } ay;
 1.64231 +    struct OP_OpenEphemeral_stack_vars {
 1.64232 +      VdbeCursor *pCx;
 1.64233 +    } az;
 1.64234 +    struct OP_SorterOpen_stack_vars {
 1.64235 +      VdbeCursor *pCx;
 1.64236 +    } ba;
 1.64237 +    struct OP_OpenPseudo_stack_vars {
 1.64238 +      VdbeCursor *pCx;
 1.64239 +    } bb;
 1.64240 +    struct OP_SeekGt_stack_vars {
 1.64241 +      int res;
 1.64242 +      int oc;
 1.64243 +      VdbeCursor *pC;
 1.64244 +      UnpackedRecord r;
 1.64245 +      int nField;
 1.64246 +      i64 iKey;      /* The rowid we are to seek to */
 1.64247 +    } bc;
 1.64248 +    struct OP_Seek_stack_vars {
 1.64249 +      VdbeCursor *pC;
 1.64250 +    } bd;
 1.64251 +    struct OP_Found_stack_vars {
 1.64252 +      int alreadyExists;
 1.64253 +      VdbeCursor *pC;
 1.64254 +      int res;
 1.64255 +      char *pFree;
 1.64256 +      UnpackedRecord *pIdxKey;
 1.64257 +      UnpackedRecord r;
 1.64258 +      char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
 1.64259 +    } be;
 1.64260 +    struct OP_IsUnique_stack_vars {
 1.64261 +      u16 ii;
 1.64262 +      VdbeCursor *pCx;
 1.64263 +      BtCursor *pCrsr;
 1.64264 +      u16 nField;
 1.64265 +      Mem *aMx;
 1.64266 +      UnpackedRecord r;                  /* B-Tree index search key */
 1.64267 +      i64 R;                             /* Rowid stored in register P3 */
 1.64268 +    } bf;
 1.64269 +    struct OP_NotExists_stack_vars {
 1.64270 +      VdbeCursor *pC;
 1.64271 +      BtCursor *pCrsr;
 1.64272 +      int res;
 1.64273 +      u64 iKey;
 1.64274 +    } bg;
 1.64275 +    struct OP_NewRowid_stack_vars {
 1.64276 +      i64 v;                 /* The new rowid */
 1.64277 +      VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 1.64278 +      int res;               /* Result of an sqlite3BtreeLast() */
 1.64279 +      int cnt;               /* Counter to limit the number of searches */
 1.64280 +      Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 1.64281 +      VdbeFrame *pFrame;     /* Root frame of VDBE */
 1.64282 +    } bh;
 1.64283 +    struct OP_InsertInt_stack_vars {
 1.64284 +      Mem *pData;       /* MEM cell holding data for the record to be inserted */
 1.64285 +      Mem *pKey;        /* MEM cell holding key  for the record */
 1.64286 +      i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 1.64287 +      VdbeCursor *pC;   /* Cursor to table into which insert is written */
 1.64288 +      int nZero;        /* Number of zero-bytes to append */
 1.64289 +      int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 1.64290 +      const char *zDb;  /* database name - used by the update hook */
 1.64291 +      const char *zTbl; /* Table name - used by the opdate hook */
 1.64292 +      int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 1.64293 +    } bi;
 1.64294 +    struct OP_Delete_stack_vars {
 1.64295 +      i64 iKey;
 1.64296 +      VdbeCursor *pC;
 1.64297 +    } bj;
 1.64298 +    struct OP_SorterCompare_stack_vars {
 1.64299 +      VdbeCursor *pC;
 1.64300 +      int res;
 1.64301 +    } bk;
 1.64302 +    struct OP_SorterData_stack_vars {
 1.64303 +      VdbeCursor *pC;
 1.64304 +    } bl;
 1.64305 +    struct OP_RowData_stack_vars {
 1.64306 +      VdbeCursor *pC;
 1.64307 +      BtCursor *pCrsr;
 1.64308 +      u32 n;
 1.64309 +      i64 n64;
 1.64310 +    } bm;
 1.64311 +    struct OP_Rowid_stack_vars {
 1.64312 +      VdbeCursor *pC;
 1.64313 +      i64 v;
 1.64314 +      sqlite3_vtab *pVtab;
 1.64315 +      const sqlite3_module *pModule;
 1.64316 +    } bn;
 1.64317 +    struct OP_NullRow_stack_vars {
 1.64318 +      VdbeCursor *pC;
 1.64319 +    } bo;
 1.64320 +    struct OP_Last_stack_vars {
 1.64321 +      VdbeCursor *pC;
 1.64322 +      BtCursor *pCrsr;
 1.64323 +      int res;
 1.64324 +    } bp;
 1.64325 +    struct OP_Rewind_stack_vars {
 1.64326 +      VdbeCursor *pC;
 1.64327 +      BtCursor *pCrsr;
 1.64328 +      int res;
 1.64329 +    } bq;
 1.64330 +    struct OP_Next_stack_vars {
 1.64331 +      VdbeCursor *pC;
 1.64332 +      int res;
 1.64333 +    } br;
 1.64334 +    struct OP_IdxInsert_stack_vars {
 1.64335 +      VdbeCursor *pC;
 1.64336 +      BtCursor *pCrsr;
 1.64337 +      int nKey;
 1.64338 +      const char *zKey;
 1.64339 +    } bs;
 1.64340 +    struct OP_IdxDelete_stack_vars {
 1.64341 +      VdbeCursor *pC;
 1.64342 +      BtCursor *pCrsr;
 1.64343 +      int res;
 1.64344 +      UnpackedRecord r;
 1.64345 +    } bt;
 1.64346 +    struct OP_IdxRowid_stack_vars {
 1.64347 +      BtCursor *pCrsr;
 1.64348 +      VdbeCursor *pC;
 1.64349 +      i64 rowid;
 1.64350 +    } bu;
 1.64351 +    struct OP_IdxGE_stack_vars {
 1.64352 +      VdbeCursor *pC;
 1.64353 +      int res;
 1.64354 +      UnpackedRecord r;
 1.64355 +    } bv;
 1.64356 +    struct OP_Destroy_stack_vars {
 1.64357 +      int iMoved;
 1.64358 +      int iCnt;
 1.64359 +      Vdbe *pVdbe;
 1.64360 +      int iDb;
 1.64361 +    } bw;
 1.64362 +    struct OP_Clear_stack_vars {
 1.64363 +      int nChange;
 1.64364 +    } bx;
 1.64365 +    struct OP_CreateTable_stack_vars {
 1.64366 +      int pgno;
 1.64367 +      int flags;
 1.64368 +      Db *pDb;
 1.64369 +    } by;
 1.64370 +    struct OP_ParseSchema_stack_vars {
 1.64371 +      int iDb;
 1.64372 +      const char *zMaster;
 1.64373 +      char *zSql;
 1.64374 +      InitData initData;
 1.64375 +    } bz;
 1.64376 +    struct OP_IntegrityCk_stack_vars {
 1.64377 +      int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 1.64378 +      int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 1.64379 +      int j;          /* Loop counter */
 1.64380 +      int nErr;       /* Number of errors reported */
 1.64381 +      char *z;        /* Text of the error report */
 1.64382 +      Mem *pnErr;     /* Register keeping track of errors remaining */
 1.64383 +    } ca;
 1.64384 +    struct OP_RowSetRead_stack_vars {
 1.64385 +      i64 val;
 1.64386 +    } cb;
 1.64387 +    struct OP_RowSetTest_stack_vars {
 1.64388 +      int iSet;
 1.64389 +      int exists;
 1.64390 +    } cc;
 1.64391 +    struct OP_Program_stack_vars {
 1.64392 +      int nMem;               /* Number of memory registers for sub-program */
 1.64393 +      int nByte;              /* Bytes of runtime space required for sub-program */
 1.64394 +      Mem *pRt;               /* Register to allocate runtime space */
 1.64395 +      Mem *pMem;              /* Used to iterate through memory cells */
 1.64396 +      Mem *pEnd;              /* Last memory cell in new array */
 1.64397 +      VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 1.64398 +      SubProgram *pProgram;   /* Sub-program to execute */
 1.64399 +      void *t;                /* Token identifying trigger */
 1.64400 +    } cd;
 1.64401 +    struct OP_Param_stack_vars {
 1.64402 +      VdbeFrame *pFrame;
 1.64403 +      Mem *pIn;
 1.64404 +    } ce;
 1.64405 +    struct OP_MemMax_stack_vars {
 1.64406 +      Mem *pIn1;
 1.64407 +      VdbeFrame *pFrame;
 1.64408 +    } cf;
 1.64409 +    struct OP_AggStep_stack_vars {
 1.64410 +      int n;
 1.64411 +      int i;
 1.64412 +      Mem *pMem;
 1.64413 +      Mem *pRec;
 1.64414 +      sqlite3_context ctx;
 1.64415 +      sqlite3_value **apVal;
 1.64416 +    } cg;
 1.64417 +    struct OP_AggFinal_stack_vars {
 1.64418 +      Mem *pMem;
 1.64419 +    } ch;
 1.64420 +    struct OP_Checkpoint_stack_vars {
 1.64421 +      int i;                          /* Loop counter */
 1.64422 +      int aRes[3];                    /* Results */
 1.64423 +      Mem *pMem;                      /* Write results here */
 1.64424 +    } ci;
 1.64425 +    struct OP_JournalMode_stack_vars {
 1.64426 +      Btree *pBt;                     /* Btree to change journal mode of */
 1.64427 +      Pager *pPager;                  /* Pager associated with pBt */
 1.64428 +      int eNew;                       /* New journal mode */
 1.64429 +      int eOld;                       /* The old journal mode */
 1.64430 +#ifndef SQLITE_OMIT_WAL
 1.64431 +      const char *zFilename;          /* Name of database file for pPager */
 1.64432 +#endif
 1.64433 +    } cj;
 1.64434 +    struct OP_IncrVacuum_stack_vars {
 1.64435 +      Btree *pBt;
 1.64436 +    } ck;
 1.64437 +    struct OP_VBegin_stack_vars {
 1.64438 +      VTable *pVTab;
 1.64439 +    } cl;
 1.64440 +    struct OP_VOpen_stack_vars {
 1.64441 +      VdbeCursor *pCur;
 1.64442 +      sqlite3_vtab_cursor *pVtabCursor;
 1.64443 +      sqlite3_vtab *pVtab;
 1.64444 +      sqlite3_module *pModule;
 1.64445 +    } cm;
 1.64446 +    struct OP_VFilter_stack_vars {
 1.64447 +      int nArg;
 1.64448 +      int iQuery;
 1.64449 +      const sqlite3_module *pModule;
 1.64450 +      Mem *pQuery;
 1.64451 +      Mem *pArgc;
 1.64452 +      sqlite3_vtab_cursor *pVtabCursor;
 1.64453 +      sqlite3_vtab *pVtab;
 1.64454 +      VdbeCursor *pCur;
 1.64455 +      int res;
 1.64456 +      int i;
 1.64457 +      Mem **apArg;
 1.64458 +    } cn;
 1.64459 +    struct OP_VColumn_stack_vars {
 1.64460 +      sqlite3_vtab *pVtab;
 1.64461 +      const sqlite3_module *pModule;
 1.64462 +      Mem *pDest;
 1.64463 +      sqlite3_context sContext;
 1.64464 +    } co;
 1.64465 +    struct OP_VNext_stack_vars {
 1.64466 +      sqlite3_vtab *pVtab;
 1.64467 +      const sqlite3_module *pModule;
 1.64468 +      int res;
 1.64469 +      VdbeCursor *pCur;
 1.64470 +    } cp;
 1.64471 +    struct OP_VRename_stack_vars {
 1.64472 +      sqlite3_vtab *pVtab;
 1.64473 +      Mem *pName;
 1.64474 +    } cq;
 1.64475 +    struct OP_VUpdate_stack_vars {
 1.64476 +      sqlite3_vtab *pVtab;
 1.64477 +      sqlite3_module *pModule;
 1.64478 +      int nArg;
 1.64479 +      int i;
 1.64480 +      sqlite_int64 rowid;
 1.64481 +      Mem **apArg;
 1.64482 +      Mem *pX;
 1.64483 +    } cr;
 1.64484 +    struct OP_Trace_stack_vars {
 1.64485 +      char *zTrace;
 1.64486 +      char *z;
 1.64487 +    } cs;
 1.64488 +  } u;
 1.64489 +  /* End automatically generated code
 1.64490 +  ********************************************************************/
 1.64491 +
 1.64492 +  assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 1.64493 +  sqlite3VdbeEnter(p);
 1.64494 +  if( p->rc==SQLITE_NOMEM ){
 1.64495 +    /* This happens if a malloc() inside a call to sqlite3_column_text() or
 1.64496 +    ** sqlite3_column_text16() failed.  */
 1.64497 +    goto no_mem;
 1.64498 +  }
 1.64499 +  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
 1.64500 +  p->rc = SQLITE_OK;
 1.64501 +  assert( p->explain==0 );
 1.64502 +  p->pResultSet = 0;
 1.64503 +  db->busyHandler.nBusy = 0;
 1.64504 +  CHECK_FOR_INTERRUPT;
 1.64505 +  sqlite3VdbeIOTraceSql(p);
 1.64506 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 1.64507 +  checkProgress = db->xProgress!=0;
 1.64508 +#endif
 1.64509 +#ifdef SQLITE_DEBUG
 1.64510 +  sqlite3BeginBenignMalloc();
 1.64511 +  if( p->pc==0  && (p->db->flags & SQLITE_VdbeListing)!=0 ){
 1.64512 +    int i;
 1.64513 +    printf("VDBE Program Listing:\n");
 1.64514 +    sqlite3VdbePrintSql(p);
 1.64515 +    for(i=0; i<p->nOp; i++){
 1.64516 +      sqlite3VdbePrintOp(stdout, i, &aOp[i]);
 1.64517 +    }
 1.64518 +  }
 1.64519 +  sqlite3EndBenignMalloc();
 1.64520 +#endif
 1.64521 +  for(pc=p->pc; rc==SQLITE_OK; pc++){
 1.64522 +    assert( pc>=0 && pc<p->nOp );
 1.64523 +    if( db->mallocFailed ) goto no_mem;
 1.64524 +#ifdef VDBE_PROFILE
 1.64525 +    origPc = pc;
 1.64526 +    start = sqlite3Hwtime();
 1.64527 +#endif
 1.64528 +    pOp = &aOp[pc];
 1.64529 +
 1.64530 +    /* Only allow tracing if SQLITE_DEBUG is defined.
 1.64531 +    */
 1.64532 +#ifdef SQLITE_DEBUG
 1.64533 +    if( p->trace ){
 1.64534 +      if( pc==0 ){
 1.64535 +        printf("VDBE Execution Trace:\n");
 1.64536 +        sqlite3VdbePrintSql(p);
 1.64537 +      }
 1.64538 +      sqlite3VdbePrintOp(p->trace, pc, pOp);
 1.64539 +    }
 1.64540 +#endif
 1.64541 +      
 1.64542 +
 1.64543 +    /* Check to see if we need to simulate an interrupt.  This only happens
 1.64544 +    ** if we have a special test build.
 1.64545 +    */
 1.64546 +#ifdef SQLITE_TEST
 1.64547 +    if( sqlite3_interrupt_count>0 ){
 1.64548 +      sqlite3_interrupt_count--;
 1.64549 +      if( sqlite3_interrupt_count==0 ){
 1.64550 +        sqlite3_interrupt(db);
 1.64551 +      }
 1.64552 +    }
 1.64553 +#endif
 1.64554 +
 1.64555 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 1.64556 +    /* Call the progress callback if it is configured and the required number
 1.64557 +    ** of VDBE ops have been executed (either since this invocation of
 1.64558 +    ** sqlite3VdbeExec() or since last time the progress callback was called).
 1.64559 +    ** If the progress callback returns non-zero, exit the virtual machine with
 1.64560 +    ** a return code SQLITE_ABORT.
 1.64561 +    */
 1.64562 +    if( checkProgress ){
 1.64563 +      if( db->nProgressOps==nProgressOps ){
 1.64564 +        int prc;
 1.64565 +        prc = db->xProgress(db->pProgressArg);
 1.64566 +        if( prc!=0 ){
 1.64567 +          rc = SQLITE_INTERRUPT;
 1.64568 +          goto vdbe_error_halt;
 1.64569 +        }
 1.64570 +        nProgressOps = 0;
 1.64571 +      }
 1.64572 +      nProgressOps++;
 1.64573 +    }
 1.64574 +#endif
 1.64575 +
 1.64576 +    /* On any opcode with the "out2-prerelease" tag, free any
 1.64577 +    ** external allocations out of mem[p2] and set mem[p2] to be
 1.64578 +    ** an undefined integer.  Opcodes will either fill in the integer
 1.64579 +    ** value or convert mem[p2] to a different type.
 1.64580 +    */
 1.64581 +    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
 1.64582 +    if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
 1.64583 +      assert( pOp->p2>0 );
 1.64584 +      assert( pOp->p2<=p->nMem );
 1.64585 +      pOut = &aMem[pOp->p2];
 1.64586 +      memAboutToChange(p, pOut);
 1.64587 +      VdbeMemRelease(pOut);
 1.64588 +      pOut->flags = MEM_Int;
 1.64589 +    }
 1.64590 +
 1.64591 +    /* Sanity checking on other operands */
 1.64592 +#ifdef SQLITE_DEBUG
 1.64593 +    if( (pOp->opflags & OPFLG_IN1)!=0 ){
 1.64594 +      assert( pOp->p1>0 );
 1.64595 +      assert( pOp->p1<=p->nMem );
 1.64596 +      assert( memIsValid(&aMem[pOp->p1]) );
 1.64597 +      REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
 1.64598 +    }
 1.64599 +    if( (pOp->opflags & OPFLG_IN2)!=0 ){
 1.64600 +      assert( pOp->p2>0 );
 1.64601 +      assert( pOp->p2<=p->nMem );
 1.64602 +      assert( memIsValid(&aMem[pOp->p2]) );
 1.64603 +      REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
 1.64604 +    }
 1.64605 +    if( (pOp->opflags & OPFLG_IN3)!=0 ){
 1.64606 +      assert( pOp->p3>0 );
 1.64607 +      assert( pOp->p3<=p->nMem );
 1.64608 +      assert( memIsValid(&aMem[pOp->p3]) );
 1.64609 +      REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
 1.64610 +    }
 1.64611 +    if( (pOp->opflags & OPFLG_OUT2)!=0 ){
 1.64612 +      assert( pOp->p2>0 );
 1.64613 +      assert( pOp->p2<=p->nMem );
 1.64614 +      memAboutToChange(p, &aMem[pOp->p2]);
 1.64615 +    }
 1.64616 +    if( (pOp->opflags & OPFLG_OUT3)!=0 ){
 1.64617 +      assert( pOp->p3>0 );
 1.64618 +      assert( pOp->p3<=p->nMem );
 1.64619 +      memAboutToChange(p, &aMem[pOp->p3]);
 1.64620 +    }
 1.64621 +#endif
 1.64622 +  
 1.64623 +    switch( pOp->opcode ){
 1.64624 +
 1.64625 +/*****************************************************************************
 1.64626 +** What follows is a massive switch statement where each case implements a
 1.64627 +** separate instruction in the virtual machine.  If we follow the usual
 1.64628 +** indentation conventions, each case should be indented by 6 spaces.  But
 1.64629 +** that is a lot of wasted space on the left margin.  So the code within
 1.64630 +** the switch statement will break with convention and be flush-left. Another
 1.64631 +** big comment (similar to this one) will mark the point in the code where
 1.64632 +** we transition back to normal indentation.
 1.64633 +**
 1.64634 +** The formatting of each case is important.  The makefile for SQLite
 1.64635 +** generates two C files "opcodes.h" and "opcodes.c" by scanning this
 1.64636 +** file looking for lines that begin with "case OP_".  The opcodes.h files
 1.64637 +** will be filled with #defines that give unique integer values to each
 1.64638 +** opcode and the opcodes.c file is filled with an array of strings where
 1.64639 +** each string is the symbolic name for the corresponding opcode.  If the
 1.64640 +** case statement is followed by a comment of the form "/# same as ... #/"
 1.64641 +** that comment is used to determine the particular value of the opcode.
 1.64642 +**
 1.64643 +** Other keywords in the comment that follows each case are used to
 1.64644 +** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
 1.64645 +** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
 1.64646 +** the mkopcodeh.awk script for additional information.
 1.64647 +**
 1.64648 +** Documentation about VDBE opcodes is generated by scanning this file
 1.64649 +** for lines of that contain "Opcode:".  That line and all subsequent
 1.64650 +** comment lines are used in the generation of the opcode.html documentation
 1.64651 +** file.
 1.64652 +**
 1.64653 +** SUMMARY:
 1.64654 +**
 1.64655 +**     Formatting is important to scripts that scan this file.
 1.64656 +**     Do not deviate from the formatting style currently in use.
 1.64657 +**
 1.64658 +*****************************************************************************/
 1.64659 +
 1.64660 +/* Opcode:  Goto * P2 * * *
 1.64661 +**
 1.64662 +** An unconditional jump to address P2.
 1.64663 +** The next instruction executed will be 
 1.64664 +** the one at index P2 from the beginning of
 1.64665 +** the program.
 1.64666 +*/
 1.64667 +case OP_Goto: {             /* jump */
 1.64668 +  CHECK_FOR_INTERRUPT;
 1.64669 +  pc = pOp->p2 - 1;
 1.64670 +  break;
 1.64671 +}
 1.64672 +
 1.64673 +/* Opcode:  Gosub P1 P2 * * *
 1.64674 +**
 1.64675 +** Write the current address onto register P1
 1.64676 +** and then jump to address P2.
 1.64677 +*/
 1.64678 +case OP_Gosub: {            /* jump */
 1.64679 +  assert( pOp->p1>0 && pOp->p1<=p->nMem );
 1.64680 +  pIn1 = &aMem[pOp->p1];
 1.64681 +  assert( (pIn1->flags & MEM_Dyn)==0 );
 1.64682 +  memAboutToChange(p, pIn1);
 1.64683 +  pIn1->flags = MEM_Int;
 1.64684 +  pIn1->u.i = pc;
 1.64685 +  REGISTER_TRACE(pOp->p1, pIn1);
 1.64686 +  pc = pOp->p2 - 1;
 1.64687 +  break;
 1.64688 +}
 1.64689 +
 1.64690 +/* Opcode:  Return P1 * * * *
 1.64691 +**
 1.64692 +** Jump to the next instruction after the address in register P1.
 1.64693 +*/
 1.64694 +case OP_Return: {           /* in1 */
 1.64695 +  pIn1 = &aMem[pOp->p1];
 1.64696 +  assert( pIn1->flags & MEM_Int );
 1.64697 +  pc = (int)pIn1->u.i;
 1.64698 +  break;
 1.64699 +}
 1.64700 +
 1.64701 +/* Opcode:  Yield P1 * * * *
 1.64702 +**
 1.64703 +** Swap the program counter with the value in register P1.
 1.64704 +*/
 1.64705 +case OP_Yield: {            /* in1 */
 1.64706 +#if 0  /* local variables moved into u.aa */
 1.64707 +  int pcDest;
 1.64708 +#endif /* local variables moved into u.aa */
 1.64709 +  pIn1 = &aMem[pOp->p1];
 1.64710 +  assert( (pIn1->flags & MEM_Dyn)==0 );
 1.64711 +  pIn1->flags = MEM_Int;
 1.64712 +  u.aa.pcDest = (int)pIn1->u.i;
 1.64713 +  pIn1->u.i = pc;
 1.64714 +  REGISTER_TRACE(pOp->p1, pIn1);
 1.64715 +  pc = u.aa.pcDest;
 1.64716 +  break;
 1.64717 +}
 1.64718 +
 1.64719 +/* Opcode:  HaltIfNull  P1 P2 P3 P4 *
 1.64720 +**
 1.64721 +** Check the value in register P3.  If it is NULL then Halt using
 1.64722 +** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
 1.64723 +** value in register P3 is not NULL, then this routine is a no-op.
 1.64724 +*/
 1.64725 +case OP_HaltIfNull: {      /* in3 */
 1.64726 +  pIn3 = &aMem[pOp->p3];
 1.64727 +  if( (pIn3->flags & MEM_Null)==0 ) break;
 1.64728 +  /* Fall through into OP_Halt */
 1.64729 +}
 1.64730 +
 1.64731 +/* Opcode:  Halt P1 P2 * P4 *
 1.64732 +**
 1.64733 +** Exit immediately.  All open cursors, etc are closed
 1.64734 +** automatically.
 1.64735 +**
 1.64736 +** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
 1.64737 +** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
 1.64738 +** For errors, it can be some other value.  If P1!=0 then P2 will determine
 1.64739 +** whether or not to rollback the current transaction.  Do not rollback
 1.64740 +** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
 1.64741 +** then back out all changes that have occurred during this execution of the
 1.64742 +** VDBE, but do not rollback the transaction. 
 1.64743 +**
 1.64744 +** If P4 is not null then it is an error message string.
 1.64745 +**
 1.64746 +** There is an implied "Halt 0 0 0" instruction inserted at the very end of
 1.64747 +** every program.  So a jump past the last instruction of the program
 1.64748 +** is the same as executing Halt.
 1.64749 +*/
 1.64750 +case OP_Halt: {
 1.64751 +  if( pOp->p1==SQLITE_OK && p->pFrame ){
 1.64752 +    /* Halt the sub-program. Return control to the parent frame. */
 1.64753 +    VdbeFrame *pFrame = p->pFrame;
 1.64754 +    p->pFrame = pFrame->pParent;
 1.64755 +    p->nFrame--;
 1.64756 +    sqlite3VdbeSetChanges(db, p->nChange);
 1.64757 +    pc = sqlite3VdbeFrameRestore(pFrame);
 1.64758 +    lastRowid = db->lastRowid;
 1.64759 +    if( pOp->p2==OE_Ignore ){
 1.64760 +      /* Instruction pc is the OP_Program that invoked the sub-program 
 1.64761 +      ** currently being halted. If the p2 instruction of this OP_Halt
 1.64762 +      ** instruction is set to OE_Ignore, then the sub-program is throwing
 1.64763 +      ** an IGNORE exception. In this case jump to the address specified
 1.64764 +      ** as the p2 of the calling OP_Program.  */
 1.64765 +      pc = p->aOp[pc].p2-1;
 1.64766 +    }
 1.64767 +    aOp = p->aOp;
 1.64768 +    aMem = p->aMem;
 1.64769 +    break;
 1.64770 +  }
 1.64771 +
 1.64772 +  p->rc = pOp->p1;
 1.64773 +  p->errorAction = (u8)pOp->p2;
 1.64774 +  p->pc = pc;
 1.64775 +  if( pOp->p4.z ){
 1.64776 +    assert( p->rc!=SQLITE_OK );
 1.64777 +    sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
 1.64778 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.64779 +    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
 1.64780 +  }else if( p->rc ){
 1.64781 +    testcase( sqlite3GlobalConfig.xLog!=0 );
 1.64782 +    sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
 1.64783 +  }
 1.64784 +  rc = sqlite3VdbeHalt(p);
 1.64785 +  assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
 1.64786 +  if( rc==SQLITE_BUSY ){
 1.64787 +    p->rc = rc = SQLITE_BUSY;
 1.64788 +  }else{
 1.64789 +    assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
 1.64790 +    assert( rc==SQLITE_OK || db->nDeferredCons>0 );
 1.64791 +    rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
 1.64792 +  }
 1.64793 +  goto vdbe_return;
 1.64794 +}
 1.64795 +
 1.64796 +/* Opcode: Integer P1 P2 * * *
 1.64797 +**
 1.64798 +** The 32-bit integer value P1 is written into register P2.
 1.64799 +*/
 1.64800 +case OP_Integer: {         /* out2-prerelease */
 1.64801 +  pOut->u.i = pOp->p1;
 1.64802 +  break;
 1.64803 +}
 1.64804 +
 1.64805 +/* Opcode: Int64 * P2 * P4 *
 1.64806 +**
 1.64807 +** P4 is a pointer to a 64-bit integer value.
 1.64808 +** Write that value into register P2.
 1.64809 +*/
 1.64810 +case OP_Int64: {           /* out2-prerelease */
 1.64811 +  assert( pOp->p4.pI64!=0 );
 1.64812 +  pOut->u.i = *pOp->p4.pI64;
 1.64813 +  break;
 1.64814 +}
 1.64815 +
 1.64816 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.64817 +/* Opcode: Real * P2 * P4 *
 1.64818 +**
 1.64819 +** P4 is a pointer to a 64-bit floating point value.
 1.64820 +** Write that value into register P2.
 1.64821 +*/
 1.64822 +case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
 1.64823 +  pOut->flags = MEM_Real;
 1.64824 +  assert( !sqlite3IsNaN(*pOp->p4.pReal) );
 1.64825 +  pOut->r = *pOp->p4.pReal;
 1.64826 +  break;
 1.64827 +}
 1.64828 +#endif
 1.64829 +
 1.64830 +/* Opcode: String8 * P2 * P4 *
 1.64831 +**
 1.64832 +** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
 1.64833 +** into an OP_String before it is executed for the first time.
 1.64834 +*/
 1.64835 +case OP_String8: {         /* same as TK_STRING, out2-prerelease */
 1.64836 +  assert( pOp->p4.z!=0 );
 1.64837 +  pOp->opcode = OP_String;
 1.64838 +  pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 1.64839 +
 1.64840 +#ifndef SQLITE_OMIT_UTF16
 1.64841 +  if( encoding!=SQLITE_UTF8 ){
 1.64842 +    rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 1.64843 +    if( rc==SQLITE_TOOBIG ) goto too_big;
 1.64844 +    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
 1.64845 +    assert( pOut->zMalloc==pOut->z );
 1.64846 +    assert( pOut->flags & MEM_Dyn );
 1.64847 +    pOut->zMalloc = 0;
 1.64848 +    pOut->flags |= MEM_Static;
 1.64849 +    pOut->flags &= ~MEM_Dyn;
 1.64850 +    if( pOp->p4type==P4_DYNAMIC ){
 1.64851 +      sqlite3DbFree(db, pOp->p4.z);
 1.64852 +    }
 1.64853 +    pOp->p4type = P4_DYNAMIC;
 1.64854 +    pOp->p4.z = pOut->z;
 1.64855 +    pOp->p1 = pOut->n;
 1.64856 +  }
 1.64857 +#endif
 1.64858 +  if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.64859 +    goto too_big;
 1.64860 +  }
 1.64861 +  /* Fall through to the next case, OP_String */
 1.64862 +}
 1.64863 +  
 1.64864 +/* Opcode: String P1 P2 * P4 *
 1.64865 +**
 1.64866 +** The string value P4 of length P1 (bytes) is stored in register P2.
 1.64867 +*/
 1.64868 +case OP_String: {          /* out2-prerelease */
 1.64869 +  assert( pOp->p4.z!=0 );
 1.64870 +  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 1.64871 +  pOut->z = pOp->p4.z;
 1.64872 +  pOut->n = pOp->p1;
 1.64873 +  pOut->enc = encoding;
 1.64874 +  UPDATE_MAX_BLOBSIZE(pOut);
 1.64875 +  break;
 1.64876 +}
 1.64877 +
 1.64878 +/* Opcode: Null P1 P2 P3 * *
 1.64879 +**
 1.64880 +** Write a NULL into registers P2.  If P3 greater than P2, then also write
 1.64881 +** NULL into register P3 and every register in between P2 and P3.  If P3
 1.64882 +** is less than P2 (typically P3 is zero) then only register P2 is
 1.64883 +** set to NULL.
 1.64884 +**
 1.64885 +** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
 1.64886 +** NULL values will not compare equal even if SQLITE_NULLEQ is set on
 1.64887 +** OP_Ne or OP_Eq.
 1.64888 +*/
 1.64889 +case OP_Null: {           /* out2-prerelease */
 1.64890 +#if 0  /* local variables moved into u.ab */
 1.64891 +  int cnt;
 1.64892 +  u16 nullFlag;
 1.64893 +#endif /* local variables moved into u.ab */
 1.64894 +  u.ab.cnt = pOp->p3-pOp->p2;
 1.64895 +  assert( pOp->p3<=p->nMem );
 1.64896 +  pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
 1.64897 +  while( u.ab.cnt>0 ){
 1.64898 +    pOut++;
 1.64899 +    memAboutToChange(p, pOut);
 1.64900 +    VdbeMemRelease(pOut);
 1.64901 +    pOut->flags = u.ab.nullFlag;
 1.64902 +    u.ab.cnt--;
 1.64903 +  }
 1.64904 +  break;
 1.64905 +}
 1.64906 +
 1.64907 +
 1.64908 +/* Opcode: Blob P1 P2 * P4
 1.64909 +**
 1.64910 +** P4 points to a blob of data P1 bytes long.  Store this
 1.64911 +** blob in register P2.
 1.64912 +*/
 1.64913 +case OP_Blob: {                /* out2-prerelease */
 1.64914 +  assert( pOp->p1 <= SQLITE_MAX_LENGTH );
 1.64915 +  sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
 1.64916 +  pOut->enc = encoding;
 1.64917 +  UPDATE_MAX_BLOBSIZE(pOut);
 1.64918 +  break;
 1.64919 +}
 1.64920 +
 1.64921 +/* Opcode: Variable P1 P2 * P4 *
 1.64922 +**
 1.64923 +** Transfer the values of bound parameter P1 into register P2
 1.64924 +**
 1.64925 +** If the parameter is named, then its name appears in P4 and P3==1.
 1.64926 +** The P4 value is used by sqlite3_bind_parameter_name().
 1.64927 +*/
 1.64928 +case OP_Variable: {            /* out2-prerelease */
 1.64929 +#if 0  /* local variables moved into u.ac */
 1.64930 +  Mem *pVar;       /* Value being transferred */
 1.64931 +#endif /* local variables moved into u.ac */
 1.64932 +
 1.64933 +  assert( pOp->p1>0 && pOp->p1<=p->nVar );
 1.64934 +  assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
 1.64935 +  u.ac.pVar = &p->aVar[pOp->p1 - 1];
 1.64936 +  if( sqlite3VdbeMemTooBig(u.ac.pVar) ){
 1.64937 +    goto too_big;
 1.64938 +  }
 1.64939 +  sqlite3VdbeMemShallowCopy(pOut, u.ac.pVar, MEM_Static);
 1.64940 +  UPDATE_MAX_BLOBSIZE(pOut);
 1.64941 +  break;
 1.64942 +}
 1.64943 +
 1.64944 +/* Opcode: Move P1 P2 P3 * *
 1.64945 +**
 1.64946 +** Move the values in register P1..P1+P3 over into
 1.64947 +** registers P2..P2+P3.  Registers P1..P1+P3 are
 1.64948 +** left holding a NULL.  It is an error for register ranges
 1.64949 +** P1..P1+P3 and P2..P2+P3 to overlap.
 1.64950 +*/
 1.64951 +case OP_Move: {
 1.64952 +#if 0  /* local variables moved into u.ad */
 1.64953 +  char *zMalloc;   /* Holding variable for allocated memory */
 1.64954 +  int n;           /* Number of registers left to copy */
 1.64955 +  int p1;          /* Register to copy from */
 1.64956 +  int p2;          /* Register to copy to */
 1.64957 +#endif /* local variables moved into u.ad */
 1.64958 +
 1.64959 +  u.ad.n = pOp->p3 + 1;
 1.64960 +  u.ad.p1 = pOp->p1;
 1.64961 +  u.ad.p2 = pOp->p2;
 1.64962 +  assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
 1.64963 +  assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
 1.64964 +
 1.64965 +  pIn1 = &aMem[u.ad.p1];
 1.64966 +  pOut = &aMem[u.ad.p2];
 1.64967 +  while( u.ad.n-- ){
 1.64968 +    assert( pOut<=&aMem[p->nMem] );
 1.64969 +    assert( pIn1<=&aMem[p->nMem] );
 1.64970 +    assert( memIsValid(pIn1) );
 1.64971 +    memAboutToChange(p, pOut);
 1.64972 +    u.ad.zMalloc = pOut->zMalloc;
 1.64973 +    pOut->zMalloc = 0;
 1.64974 +    sqlite3VdbeMemMove(pOut, pIn1);
 1.64975 +#ifdef SQLITE_DEBUG
 1.64976 +    if( pOut->pScopyFrom>=&aMem[u.ad.p1] && pOut->pScopyFrom<&aMem[u.ad.p1+pOp->p3] ){
 1.64977 +      pOut->pScopyFrom += u.ad.p1 - pOp->p2;
 1.64978 +    }
 1.64979 +#endif
 1.64980 +    pIn1->zMalloc = u.ad.zMalloc;
 1.64981 +    REGISTER_TRACE(u.ad.p2++, pOut);
 1.64982 +    pIn1++;
 1.64983 +    pOut++;
 1.64984 +  }
 1.64985 +  break;
 1.64986 +}
 1.64987 +
 1.64988 +/* Opcode: Copy P1 P2 P3 * *
 1.64989 +**
 1.64990 +** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
 1.64991 +**
 1.64992 +** This instruction makes a deep copy of the value.  A duplicate
 1.64993 +** is made of any string or blob constant.  See also OP_SCopy.
 1.64994 +*/
 1.64995 +case OP_Copy: {
 1.64996 +#if 0  /* local variables moved into u.ae */
 1.64997 +  int n;
 1.64998 +#endif /* local variables moved into u.ae */
 1.64999 +
 1.65000 +  u.ae.n = pOp->p3;
 1.65001 +  pIn1 = &aMem[pOp->p1];
 1.65002 +  pOut = &aMem[pOp->p2];
 1.65003 +  assert( pOut!=pIn1 );
 1.65004 +  while( 1 ){
 1.65005 +    sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
 1.65006 +    Deephemeralize(pOut);
 1.65007 +#ifdef SQLITE_DEBUG
 1.65008 +    pOut->pScopyFrom = 0;
 1.65009 +#endif
 1.65010 +    REGISTER_TRACE(pOp->p2+pOp->p3-u.ae.n, pOut);
 1.65011 +    if( (u.ae.n--)==0 ) break;
 1.65012 +    pOut++;
 1.65013 +    pIn1++;
 1.65014 +  }
 1.65015 +  break;
 1.65016 +}
 1.65017 +
 1.65018 +/* Opcode: SCopy P1 P2 * * *
 1.65019 +**
 1.65020 +** Make a shallow copy of register P1 into register P2.
 1.65021 +**
 1.65022 +** This instruction makes a shallow copy of the value.  If the value
 1.65023 +** is a string or blob, then the copy is only a pointer to the
 1.65024 +** original and hence if the original changes so will the copy.
 1.65025 +** Worse, if the original is deallocated, the copy becomes invalid.
 1.65026 +** Thus the program must guarantee that the original will not change
 1.65027 +** during the lifetime of the copy.  Use OP_Copy to make a complete
 1.65028 +** copy.
 1.65029 +*/
 1.65030 +case OP_SCopy: {            /* in1, out2 */
 1.65031 +  pIn1 = &aMem[pOp->p1];
 1.65032 +  pOut = &aMem[pOp->p2];
 1.65033 +  assert( pOut!=pIn1 );
 1.65034 +  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
 1.65035 +#ifdef SQLITE_DEBUG
 1.65036 +  if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
 1.65037 +#endif
 1.65038 +  REGISTER_TRACE(pOp->p2, pOut);
 1.65039 +  break;
 1.65040 +}
 1.65041 +
 1.65042 +/* Opcode: ResultRow P1 P2 * * *
 1.65043 +**
 1.65044 +** The registers P1 through P1+P2-1 contain a single row of
 1.65045 +** results. This opcode causes the sqlite3_step() call to terminate
 1.65046 +** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
 1.65047 +** structure to provide access to the top P1 values as the result
 1.65048 +** row.
 1.65049 +*/
 1.65050 +case OP_ResultRow: {
 1.65051 +#if 0  /* local variables moved into u.af */
 1.65052 +  Mem *pMem;
 1.65053 +  int i;
 1.65054 +#endif /* local variables moved into u.af */
 1.65055 +  assert( p->nResColumn==pOp->p2 );
 1.65056 +  assert( pOp->p1>0 );
 1.65057 +  assert( pOp->p1+pOp->p2<=p->nMem+1 );
 1.65058 +
 1.65059 +  /* If this statement has violated immediate foreign key constraints, do
 1.65060 +  ** not return the number of rows modified. And do not RELEASE the statement
 1.65061 +  ** transaction. It needs to be rolled back.  */
 1.65062 +  if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
 1.65063 +    assert( db->flags&SQLITE_CountRows );
 1.65064 +    assert( p->usesStmtJournal );
 1.65065 +    break;
 1.65066 +  }
 1.65067 +
 1.65068 +  /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
 1.65069 +  ** DML statements invoke this opcode to return the number of rows
 1.65070 +  ** modified to the user. This is the only way that a VM that
 1.65071 +  ** opens a statement transaction may invoke this opcode.
 1.65072 +  **
 1.65073 +  ** In case this is such a statement, close any statement transaction
 1.65074 +  ** opened by this VM before returning control to the user. This is to
 1.65075 +  ** ensure that statement-transactions are always nested, not overlapping.
 1.65076 +  ** If the open statement-transaction is not closed here, then the user
 1.65077 +  ** may step another VM that opens its own statement transaction. This
 1.65078 +  ** may lead to overlapping statement transactions.
 1.65079 +  **
 1.65080 +  ** The statement transaction is never a top-level transaction.  Hence
 1.65081 +  ** the RELEASE call below can never fail.
 1.65082 +  */
 1.65083 +  assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
 1.65084 +  rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
 1.65085 +  if( NEVER(rc!=SQLITE_OK) ){
 1.65086 +    break;
 1.65087 +  }
 1.65088 +
 1.65089 +  /* Invalidate all ephemeral cursor row caches */
 1.65090 +  p->cacheCtr = (p->cacheCtr + 2)|1;
 1.65091 +
 1.65092 +  /* Make sure the results of the current row are \000 terminated
 1.65093 +  ** and have an assigned type.  The results are de-ephemeralized as
 1.65094 +  ** a side effect.
 1.65095 +  */
 1.65096 +  u.af.pMem = p->pResultSet = &aMem[pOp->p1];
 1.65097 +  for(u.af.i=0; u.af.i<pOp->p2; u.af.i++){
 1.65098 +    assert( memIsValid(&u.af.pMem[u.af.i]) );
 1.65099 +    Deephemeralize(&u.af.pMem[u.af.i]);
 1.65100 +    assert( (u.af.pMem[u.af.i].flags & MEM_Ephem)==0
 1.65101 +            || (u.af.pMem[u.af.i].flags & (MEM_Str|MEM_Blob))==0 );
 1.65102 +    sqlite3VdbeMemNulTerminate(&u.af.pMem[u.af.i]);
 1.65103 +    sqlite3VdbeMemStoreType(&u.af.pMem[u.af.i]);
 1.65104 +    REGISTER_TRACE(pOp->p1+u.af.i, &u.af.pMem[u.af.i]);
 1.65105 +  }
 1.65106 +  if( db->mallocFailed ) goto no_mem;
 1.65107 +
 1.65108 +  /* Return SQLITE_ROW
 1.65109 +  */
 1.65110 +  p->pc = pc + 1;
 1.65111 +  rc = SQLITE_ROW;
 1.65112 +  goto vdbe_return;
 1.65113 +}
 1.65114 +
 1.65115 +/* Opcode: Concat P1 P2 P3 * *
 1.65116 +**
 1.65117 +** Add the text in register P1 onto the end of the text in
 1.65118 +** register P2 and store the result in register P3.
 1.65119 +** If either the P1 or P2 text are NULL then store NULL in P3.
 1.65120 +**
 1.65121 +**   P3 = P2 || P1
 1.65122 +**
 1.65123 +** It is illegal for P1 and P3 to be the same register. Sometimes,
 1.65124 +** if P3 is the same register as P2, the implementation is able
 1.65125 +** to avoid a memcpy().
 1.65126 +*/
 1.65127 +case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
 1.65128 +#if 0  /* local variables moved into u.ag */
 1.65129 +  i64 nByte;
 1.65130 +#endif /* local variables moved into u.ag */
 1.65131 +
 1.65132 +  pIn1 = &aMem[pOp->p1];
 1.65133 +  pIn2 = &aMem[pOp->p2];
 1.65134 +  pOut = &aMem[pOp->p3];
 1.65135 +  assert( pIn1!=pOut );
 1.65136 +  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
 1.65137 +    sqlite3VdbeMemSetNull(pOut);
 1.65138 +    break;
 1.65139 +  }
 1.65140 +  if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
 1.65141 +  Stringify(pIn1, encoding);
 1.65142 +  Stringify(pIn2, encoding);
 1.65143 +  u.ag.nByte = pIn1->n + pIn2->n;
 1.65144 +  if( u.ag.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.65145 +    goto too_big;
 1.65146 +  }
 1.65147 +  MemSetTypeFlag(pOut, MEM_Str);
 1.65148 +  if( sqlite3VdbeMemGrow(pOut, (int)u.ag.nByte+2, pOut==pIn2) ){
 1.65149 +    goto no_mem;
 1.65150 +  }
 1.65151 +  if( pOut!=pIn2 ){
 1.65152 +    memcpy(pOut->z, pIn2->z, pIn2->n);
 1.65153 +  }
 1.65154 +  memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
 1.65155 +  pOut->z[u.ag.nByte] = 0;
 1.65156 +  pOut->z[u.ag.nByte+1] = 0;
 1.65157 +  pOut->flags |= MEM_Term;
 1.65158 +  pOut->n = (int)u.ag.nByte;
 1.65159 +  pOut->enc = encoding;
 1.65160 +  UPDATE_MAX_BLOBSIZE(pOut);
 1.65161 +  break;
 1.65162 +}
 1.65163 +
 1.65164 +/* Opcode: Add P1 P2 P3 * *
 1.65165 +**
 1.65166 +** Add the value in register P1 to the value in register P2
 1.65167 +** and store the result in register P3.
 1.65168 +** If either input is NULL, the result is NULL.
 1.65169 +*/
 1.65170 +/* Opcode: Multiply P1 P2 P3 * *
 1.65171 +**
 1.65172 +**
 1.65173 +** Multiply the value in register P1 by the value in register P2
 1.65174 +** and store the result in register P3.
 1.65175 +** If either input is NULL, the result is NULL.
 1.65176 +*/
 1.65177 +/* Opcode: Subtract P1 P2 P3 * *
 1.65178 +**
 1.65179 +** Subtract the value in register P1 from the value in register P2
 1.65180 +** and store the result in register P3.
 1.65181 +** If either input is NULL, the result is NULL.
 1.65182 +*/
 1.65183 +/* Opcode: Divide P1 P2 P3 * *
 1.65184 +**
 1.65185 +** Divide the value in register P1 by the value in register P2
 1.65186 +** and store the result in register P3 (P3=P2/P1). If the value in 
 1.65187 +** register P1 is zero, then the result is NULL. If either input is 
 1.65188 +** NULL, the result is NULL.
 1.65189 +*/
 1.65190 +/* Opcode: Remainder P1 P2 P3 * *
 1.65191 +**
 1.65192 +** Compute the remainder after integer division of the value in
 1.65193 +** register P1 by the value in register P2 and store the result in P3. 
 1.65194 +** If the value in register P2 is zero the result is NULL.
 1.65195 +** If either operand is NULL, the result is NULL.
 1.65196 +*/
 1.65197 +case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
 1.65198 +case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
 1.65199 +case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
 1.65200 +case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
 1.65201 +case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
 1.65202 +#if 0  /* local variables moved into u.ah */
 1.65203 +  char bIntint;   /* Started out as two integer operands */
 1.65204 +  int flags;      /* Combined MEM_* flags from both inputs */
 1.65205 +  i64 iA;         /* Integer value of left operand */
 1.65206 +  i64 iB;         /* Integer value of right operand */
 1.65207 +  double rA;      /* Real value of left operand */
 1.65208 +  double rB;      /* Real value of right operand */
 1.65209 +#endif /* local variables moved into u.ah */
 1.65210 +
 1.65211 +  pIn1 = &aMem[pOp->p1];
 1.65212 +  applyNumericAffinity(pIn1);
 1.65213 +  pIn2 = &aMem[pOp->p2];
 1.65214 +  applyNumericAffinity(pIn2);
 1.65215 +  pOut = &aMem[pOp->p3];
 1.65216 +  u.ah.flags = pIn1->flags | pIn2->flags;
 1.65217 +  if( (u.ah.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
 1.65218 +  if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
 1.65219 +    u.ah.iA = pIn1->u.i;
 1.65220 +    u.ah.iB = pIn2->u.i;
 1.65221 +    u.ah.bIntint = 1;
 1.65222 +    switch( pOp->opcode ){
 1.65223 +      case OP_Add:       if( sqlite3AddInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
 1.65224 +      case OP_Subtract:  if( sqlite3SubInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
 1.65225 +      case OP_Multiply:  if( sqlite3MulInt64(&u.ah.iB,u.ah.iA) ) goto fp_math;  break;
 1.65226 +      case OP_Divide: {
 1.65227 +        if( u.ah.iA==0 ) goto arithmetic_result_is_null;
 1.65228 +        if( u.ah.iA==-1 && u.ah.iB==SMALLEST_INT64 ) goto fp_math;
 1.65229 +        u.ah.iB /= u.ah.iA;
 1.65230 +        break;
 1.65231 +      }
 1.65232 +      default: {
 1.65233 +        if( u.ah.iA==0 ) goto arithmetic_result_is_null;
 1.65234 +        if( u.ah.iA==-1 ) u.ah.iA = 1;
 1.65235 +        u.ah.iB %= u.ah.iA;
 1.65236 +        break;
 1.65237 +      }
 1.65238 +    }
 1.65239 +    pOut->u.i = u.ah.iB;
 1.65240 +    MemSetTypeFlag(pOut, MEM_Int);
 1.65241 +  }else{
 1.65242 +    u.ah.bIntint = 0;
 1.65243 +fp_math:
 1.65244 +    u.ah.rA = sqlite3VdbeRealValue(pIn1);
 1.65245 +    u.ah.rB = sqlite3VdbeRealValue(pIn2);
 1.65246 +    switch( pOp->opcode ){
 1.65247 +      case OP_Add:         u.ah.rB += u.ah.rA;       break;
 1.65248 +      case OP_Subtract:    u.ah.rB -= u.ah.rA;       break;
 1.65249 +      case OP_Multiply:    u.ah.rB *= u.ah.rA;       break;
 1.65250 +      case OP_Divide: {
 1.65251 +        /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 1.65252 +        if( u.ah.rA==(double)0 ) goto arithmetic_result_is_null;
 1.65253 +        u.ah.rB /= u.ah.rA;
 1.65254 +        break;
 1.65255 +      }
 1.65256 +      default: {
 1.65257 +        u.ah.iA = (i64)u.ah.rA;
 1.65258 +        u.ah.iB = (i64)u.ah.rB;
 1.65259 +        if( u.ah.iA==0 ) goto arithmetic_result_is_null;
 1.65260 +        if( u.ah.iA==-1 ) u.ah.iA = 1;
 1.65261 +        u.ah.rB = (double)(u.ah.iB % u.ah.iA);
 1.65262 +        break;
 1.65263 +      }
 1.65264 +    }
 1.65265 +#ifdef SQLITE_OMIT_FLOATING_POINT
 1.65266 +    pOut->u.i = u.ah.rB;
 1.65267 +    MemSetTypeFlag(pOut, MEM_Int);
 1.65268 +#else
 1.65269 +    if( sqlite3IsNaN(u.ah.rB) ){
 1.65270 +      goto arithmetic_result_is_null;
 1.65271 +    }
 1.65272 +    pOut->r = u.ah.rB;
 1.65273 +    MemSetTypeFlag(pOut, MEM_Real);
 1.65274 +    if( (u.ah.flags & MEM_Real)==0 && !u.ah.bIntint ){
 1.65275 +      sqlite3VdbeIntegerAffinity(pOut);
 1.65276 +    }
 1.65277 +#endif
 1.65278 +  }
 1.65279 +  break;
 1.65280 +
 1.65281 +arithmetic_result_is_null:
 1.65282 +  sqlite3VdbeMemSetNull(pOut);
 1.65283 +  break;
 1.65284 +}
 1.65285 +
 1.65286 +/* Opcode: CollSeq P1 * * P4
 1.65287 +**
 1.65288 +** P4 is a pointer to a CollSeq struct. If the next call to a user function
 1.65289 +** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
 1.65290 +** be returned. This is used by the built-in min(), max() and nullif()
 1.65291 +** functions.
 1.65292 +**
 1.65293 +** If P1 is not zero, then it is a register that a subsequent min() or
 1.65294 +** max() aggregate will set to 1 if the current row is not the minimum or
 1.65295 +** maximum.  The P1 register is initialized to 0 by this instruction.
 1.65296 +**
 1.65297 +** The interface used by the implementation of the aforementioned functions
 1.65298 +** to retrieve the collation sequence set by this opcode is not available
 1.65299 +** publicly, only to user functions defined in func.c.
 1.65300 +*/
 1.65301 +case OP_CollSeq: {
 1.65302 +  assert( pOp->p4type==P4_COLLSEQ );
 1.65303 +  if( pOp->p1 ){
 1.65304 +    sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
 1.65305 +  }
 1.65306 +  break;
 1.65307 +}
 1.65308 +
 1.65309 +/* Opcode: Function P1 P2 P3 P4 P5
 1.65310 +**
 1.65311 +** Invoke a user function (P4 is a pointer to a Function structure that
 1.65312 +** defines the function) with P5 arguments taken from register P2 and
 1.65313 +** successors.  The result of the function is stored in register P3.
 1.65314 +** Register P3 must not be one of the function inputs.
 1.65315 +**
 1.65316 +** P1 is a 32-bit bitmask indicating whether or not each argument to the 
 1.65317 +** function was determined to be constant at compile time. If the first
 1.65318 +** argument was constant then bit 0 of P1 is set. This is used to determine
 1.65319 +** whether meta data associated with a user function argument using the
 1.65320 +** sqlite3_set_auxdata() API may be safely retained until the next
 1.65321 +** invocation of this opcode.
 1.65322 +**
 1.65323 +** See also: AggStep and AggFinal
 1.65324 +*/
 1.65325 +case OP_Function: {
 1.65326 +#if 0  /* local variables moved into u.ai */
 1.65327 +  int i;
 1.65328 +  Mem *pArg;
 1.65329 +  sqlite3_context ctx;
 1.65330 +  sqlite3_value **apVal;
 1.65331 +  int n;
 1.65332 +#endif /* local variables moved into u.ai */
 1.65333 +
 1.65334 +  u.ai.n = pOp->p5;
 1.65335 +  u.ai.apVal = p->apArg;
 1.65336 +  assert( u.ai.apVal || u.ai.n==0 );
 1.65337 +  assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.65338 +  pOut = &aMem[pOp->p3];
 1.65339 +  memAboutToChange(p, pOut);
 1.65340 +
 1.65341 +  assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
 1.65342 +  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
 1.65343 +  u.ai.pArg = &aMem[pOp->p2];
 1.65344 +  for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
 1.65345 +    assert( memIsValid(u.ai.pArg) );
 1.65346 +    u.ai.apVal[u.ai.i] = u.ai.pArg;
 1.65347 +    Deephemeralize(u.ai.pArg);
 1.65348 +    sqlite3VdbeMemStoreType(u.ai.pArg);
 1.65349 +    REGISTER_TRACE(pOp->p2+u.ai.i, u.ai.pArg);
 1.65350 +  }
 1.65351 +
 1.65352 +  assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
 1.65353 +  if( pOp->p4type==P4_FUNCDEF ){
 1.65354 +    u.ai.ctx.pFunc = pOp->p4.pFunc;
 1.65355 +    u.ai.ctx.pVdbeFunc = 0;
 1.65356 +  }else{
 1.65357 +    u.ai.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
 1.65358 +    u.ai.ctx.pFunc = u.ai.ctx.pVdbeFunc->pFunc;
 1.65359 +  }
 1.65360 +
 1.65361 +  u.ai.ctx.s.flags = MEM_Null;
 1.65362 +  u.ai.ctx.s.db = db;
 1.65363 +  u.ai.ctx.s.xDel = 0;
 1.65364 +  u.ai.ctx.s.zMalloc = 0;
 1.65365 +
 1.65366 +  /* The output cell may already have a buffer allocated. Move
 1.65367 +  ** the pointer to u.ai.ctx.s so in case the user-function can use
 1.65368 +  ** the already allocated buffer instead of allocating a new one.
 1.65369 +  */
 1.65370 +  sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
 1.65371 +  MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
 1.65372 +
 1.65373 +  u.ai.ctx.isError = 0;
 1.65374 +  if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 1.65375 +    assert( pOp>aOp );
 1.65376 +    assert( pOp[-1].p4type==P4_COLLSEQ );
 1.65377 +    assert( pOp[-1].opcode==OP_CollSeq );
 1.65378 +    u.ai.ctx.pColl = pOp[-1].p4.pColl;
 1.65379 +  }
 1.65380 +  db->lastRowid = lastRowid;
 1.65381 +  (*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
 1.65382 +  lastRowid = db->lastRowid;
 1.65383 +
 1.65384 +  /* If any auxiliary data functions have been called by this user function,
 1.65385 +  ** immediately call the destructor for any non-static values.
 1.65386 +  */
 1.65387 +  if( u.ai.ctx.pVdbeFunc ){
 1.65388 +    sqlite3VdbeDeleteAuxData(u.ai.ctx.pVdbeFunc, pOp->p1);
 1.65389 +    pOp->p4.pVdbeFunc = u.ai.ctx.pVdbeFunc;
 1.65390 +    pOp->p4type = P4_VDBEFUNC;
 1.65391 +  }
 1.65392 +
 1.65393 +  if( db->mallocFailed ){
 1.65394 +    /* Even though a malloc() has failed, the implementation of the
 1.65395 +    ** user function may have called an sqlite3_result_XXX() function
 1.65396 +    ** to return a value. The following call releases any resources
 1.65397 +    ** associated with such a value.
 1.65398 +    */
 1.65399 +    sqlite3VdbeMemRelease(&u.ai.ctx.s);
 1.65400 +    goto no_mem;
 1.65401 +  }
 1.65402 +
 1.65403 +  /* If the function returned an error, throw an exception */
 1.65404 +  if( u.ai.ctx.isError ){
 1.65405 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
 1.65406 +    rc = u.ai.ctx.isError;
 1.65407 +  }
 1.65408 +
 1.65409 +  /* Copy the result of the function into register P3 */
 1.65410 +  sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
 1.65411 +  sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
 1.65412 +  if( sqlite3VdbeMemTooBig(pOut) ){
 1.65413 +    goto too_big;
 1.65414 +  }
 1.65415 +
 1.65416 +#if 0
 1.65417 +  /* The app-defined function has done something that as caused this
 1.65418 +  ** statement to expire.  (Perhaps the function called sqlite3_exec()
 1.65419 +  ** with a CREATE TABLE statement.)
 1.65420 +  */
 1.65421 +  if( p->expired ) rc = SQLITE_ABORT;
 1.65422 +#endif
 1.65423 +
 1.65424 +  REGISTER_TRACE(pOp->p3, pOut);
 1.65425 +  UPDATE_MAX_BLOBSIZE(pOut);
 1.65426 +  break;
 1.65427 +}
 1.65428 +
 1.65429 +/* Opcode: BitAnd P1 P2 P3 * *
 1.65430 +**
 1.65431 +** Take the bit-wise AND of the values in register P1 and P2 and
 1.65432 +** store the result in register P3.
 1.65433 +** If either input is NULL, the result is NULL.
 1.65434 +*/
 1.65435 +/* Opcode: BitOr P1 P2 P3 * *
 1.65436 +**
 1.65437 +** Take the bit-wise OR of the values in register P1 and P2 and
 1.65438 +** store the result in register P3.
 1.65439 +** If either input is NULL, the result is NULL.
 1.65440 +*/
 1.65441 +/* Opcode: ShiftLeft P1 P2 P3 * *
 1.65442 +**
 1.65443 +** Shift the integer value in register P2 to the left by the
 1.65444 +** number of bits specified by the integer in register P1.
 1.65445 +** Store the result in register P3.
 1.65446 +** If either input is NULL, the result is NULL.
 1.65447 +*/
 1.65448 +/* Opcode: ShiftRight P1 P2 P3 * *
 1.65449 +**
 1.65450 +** Shift the integer value in register P2 to the right by the
 1.65451 +** number of bits specified by the integer in register P1.
 1.65452 +** Store the result in register P3.
 1.65453 +** If either input is NULL, the result is NULL.
 1.65454 +*/
 1.65455 +case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
 1.65456 +case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
 1.65457 +case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
 1.65458 +case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
 1.65459 +#if 0  /* local variables moved into u.aj */
 1.65460 +  i64 iA;
 1.65461 +  u64 uA;
 1.65462 +  i64 iB;
 1.65463 +  u8 op;
 1.65464 +#endif /* local variables moved into u.aj */
 1.65465 +
 1.65466 +  pIn1 = &aMem[pOp->p1];
 1.65467 +  pIn2 = &aMem[pOp->p2];
 1.65468 +  pOut = &aMem[pOp->p3];
 1.65469 +  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
 1.65470 +    sqlite3VdbeMemSetNull(pOut);
 1.65471 +    break;
 1.65472 +  }
 1.65473 +  u.aj.iA = sqlite3VdbeIntValue(pIn2);
 1.65474 +  u.aj.iB = sqlite3VdbeIntValue(pIn1);
 1.65475 +  u.aj.op = pOp->opcode;
 1.65476 +  if( u.aj.op==OP_BitAnd ){
 1.65477 +    u.aj.iA &= u.aj.iB;
 1.65478 +  }else if( u.aj.op==OP_BitOr ){
 1.65479 +    u.aj.iA |= u.aj.iB;
 1.65480 +  }else if( u.aj.iB!=0 ){
 1.65481 +    assert( u.aj.op==OP_ShiftRight || u.aj.op==OP_ShiftLeft );
 1.65482 +
 1.65483 +    /* If shifting by a negative amount, shift in the other direction */
 1.65484 +    if( u.aj.iB<0 ){
 1.65485 +      assert( OP_ShiftRight==OP_ShiftLeft+1 );
 1.65486 +      u.aj.op = 2*OP_ShiftLeft + 1 - u.aj.op;
 1.65487 +      u.aj.iB = u.aj.iB>(-64) ? -u.aj.iB : 64;
 1.65488 +    }
 1.65489 +
 1.65490 +    if( u.aj.iB>=64 ){
 1.65491 +      u.aj.iA = (u.aj.iA>=0 || u.aj.op==OP_ShiftLeft) ? 0 : -1;
 1.65492 +    }else{
 1.65493 +      memcpy(&u.aj.uA, &u.aj.iA, sizeof(u.aj.uA));
 1.65494 +      if( u.aj.op==OP_ShiftLeft ){
 1.65495 +        u.aj.uA <<= u.aj.iB;
 1.65496 +      }else{
 1.65497 +        u.aj.uA >>= u.aj.iB;
 1.65498 +        /* Sign-extend on a right shift of a negative number */
 1.65499 +        if( u.aj.iA<0 ) u.aj.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.aj.iB);
 1.65500 +      }
 1.65501 +      memcpy(&u.aj.iA, &u.aj.uA, sizeof(u.aj.iA));
 1.65502 +    }
 1.65503 +  }
 1.65504 +  pOut->u.i = u.aj.iA;
 1.65505 +  MemSetTypeFlag(pOut, MEM_Int);
 1.65506 +  break;
 1.65507 +}
 1.65508 +
 1.65509 +/* Opcode: AddImm  P1 P2 * * *
 1.65510 +** 
 1.65511 +** Add the constant P2 to the value in register P1.
 1.65512 +** The result is always an integer.
 1.65513 +**
 1.65514 +** To force any register to be an integer, just add 0.
 1.65515 +*/
 1.65516 +case OP_AddImm: {            /* in1 */
 1.65517 +  pIn1 = &aMem[pOp->p1];
 1.65518 +  memAboutToChange(p, pIn1);
 1.65519 +  sqlite3VdbeMemIntegerify(pIn1);
 1.65520 +  pIn1->u.i += pOp->p2;
 1.65521 +  break;
 1.65522 +}
 1.65523 +
 1.65524 +/* Opcode: MustBeInt P1 P2 * * *
 1.65525 +** 
 1.65526 +** Force the value in register P1 to be an integer.  If the value
 1.65527 +** in P1 is not an integer and cannot be converted into an integer
 1.65528 +** without data loss, then jump immediately to P2, or if P2==0
 1.65529 +** raise an SQLITE_MISMATCH exception.
 1.65530 +*/
 1.65531 +case OP_MustBeInt: {            /* jump, in1 */
 1.65532 +  pIn1 = &aMem[pOp->p1];
 1.65533 +  applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
 1.65534 +  if( (pIn1->flags & MEM_Int)==0 ){
 1.65535 +    if( pOp->p2==0 ){
 1.65536 +      rc = SQLITE_MISMATCH;
 1.65537 +      goto abort_due_to_error;
 1.65538 +    }else{
 1.65539 +      pc = pOp->p2 - 1;
 1.65540 +    }
 1.65541 +  }else{
 1.65542 +    MemSetTypeFlag(pIn1, MEM_Int);
 1.65543 +  }
 1.65544 +  break;
 1.65545 +}
 1.65546 +
 1.65547 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.65548 +/* Opcode: RealAffinity P1 * * * *
 1.65549 +**
 1.65550 +** If register P1 holds an integer convert it to a real value.
 1.65551 +**
 1.65552 +** This opcode is used when extracting information from a column that
 1.65553 +** has REAL affinity.  Such column values may still be stored as
 1.65554 +** integers, for space efficiency, but after extraction we want them
 1.65555 +** to have only a real value.
 1.65556 +*/
 1.65557 +case OP_RealAffinity: {                  /* in1 */
 1.65558 +  pIn1 = &aMem[pOp->p1];
 1.65559 +  if( pIn1->flags & MEM_Int ){
 1.65560 +    sqlite3VdbeMemRealify(pIn1);
 1.65561 +  }
 1.65562 +  break;
 1.65563 +}
 1.65564 +#endif
 1.65565 +
 1.65566 +#ifndef SQLITE_OMIT_CAST
 1.65567 +/* Opcode: ToText P1 * * * *
 1.65568 +**
 1.65569 +** Force the value in register P1 to be text.
 1.65570 +** If the value is numeric, convert it to a string using the
 1.65571 +** equivalent of printf().  Blob values are unchanged and
 1.65572 +** are afterwards simply interpreted as text.
 1.65573 +**
 1.65574 +** A NULL value is not changed by this routine.  It remains NULL.
 1.65575 +*/
 1.65576 +case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
 1.65577 +  pIn1 = &aMem[pOp->p1];
 1.65578 +  memAboutToChange(p, pIn1);
 1.65579 +  if( pIn1->flags & MEM_Null ) break;
 1.65580 +  assert( MEM_Str==(MEM_Blob>>3) );
 1.65581 +  pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
 1.65582 +  applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
 1.65583 +  rc = ExpandBlob(pIn1);
 1.65584 +  assert( pIn1->flags & MEM_Str || db->mallocFailed );
 1.65585 +  pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
 1.65586 +  UPDATE_MAX_BLOBSIZE(pIn1);
 1.65587 +  break;
 1.65588 +}
 1.65589 +
 1.65590 +/* Opcode: ToBlob P1 * * * *
 1.65591 +**
 1.65592 +** Force the value in register P1 to be a BLOB.
 1.65593 +** If the value is numeric, convert it to a string first.
 1.65594 +** Strings are simply reinterpreted as blobs with no change
 1.65595 +** to the underlying data.
 1.65596 +**
 1.65597 +** A NULL value is not changed by this routine.  It remains NULL.
 1.65598 +*/
 1.65599 +case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
 1.65600 +  pIn1 = &aMem[pOp->p1];
 1.65601 +  if( pIn1->flags & MEM_Null ) break;
 1.65602 +  if( (pIn1->flags & MEM_Blob)==0 ){
 1.65603 +    applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
 1.65604 +    assert( pIn1->flags & MEM_Str || db->mallocFailed );
 1.65605 +    MemSetTypeFlag(pIn1, MEM_Blob);
 1.65606 +  }else{
 1.65607 +    pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
 1.65608 +  }
 1.65609 +  UPDATE_MAX_BLOBSIZE(pIn1);
 1.65610 +  break;
 1.65611 +}
 1.65612 +
 1.65613 +/* Opcode: ToNumeric P1 * * * *
 1.65614 +**
 1.65615 +** Force the value in register P1 to be numeric (either an
 1.65616 +** integer or a floating-point number.)
 1.65617 +** If the value is text or blob, try to convert it to an using the
 1.65618 +** equivalent of atoi() or atof() and store 0 if no such conversion 
 1.65619 +** is possible.
 1.65620 +**
 1.65621 +** A NULL value is not changed by this routine.  It remains NULL.
 1.65622 +*/
 1.65623 +case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
 1.65624 +  pIn1 = &aMem[pOp->p1];
 1.65625 +  sqlite3VdbeMemNumerify(pIn1);
 1.65626 +  break;
 1.65627 +}
 1.65628 +#endif /* SQLITE_OMIT_CAST */
 1.65629 +
 1.65630 +/* Opcode: ToInt P1 * * * *
 1.65631 +**
 1.65632 +** Force the value in register P1 to be an integer.  If
 1.65633 +** The value is currently a real number, drop its fractional part.
 1.65634 +** If the value is text or blob, try to convert it to an integer using the
 1.65635 +** equivalent of atoi() and store 0 if no such conversion is possible.
 1.65636 +**
 1.65637 +** A NULL value is not changed by this routine.  It remains NULL.
 1.65638 +*/
 1.65639 +case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
 1.65640 +  pIn1 = &aMem[pOp->p1];
 1.65641 +  if( (pIn1->flags & MEM_Null)==0 ){
 1.65642 +    sqlite3VdbeMemIntegerify(pIn1);
 1.65643 +  }
 1.65644 +  break;
 1.65645 +}
 1.65646 +
 1.65647 +#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
 1.65648 +/* Opcode: ToReal P1 * * * *
 1.65649 +**
 1.65650 +** Force the value in register P1 to be a floating point number.
 1.65651 +** If The value is currently an integer, convert it.
 1.65652 +** If the value is text or blob, try to convert it to an integer using the
 1.65653 +** equivalent of atoi() and store 0.0 if no such conversion is possible.
 1.65654 +**
 1.65655 +** A NULL value is not changed by this routine.  It remains NULL.
 1.65656 +*/
 1.65657 +case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
 1.65658 +  pIn1 = &aMem[pOp->p1];
 1.65659 +  memAboutToChange(p, pIn1);
 1.65660 +  if( (pIn1->flags & MEM_Null)==0 ){
 1.65661 +    sqlite3VdbeMemRealify(pIn1);
 1.65662 +  }
 1.65663 +  break;
 1.65664 +}
 1.65665 +#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
 1.65666 +
 1.65667 +/* Opcode: Lt P1 P2 P3 P4 P5
 1.65668 +**
 1.65669 +** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
 1.65670 +** jump to address P2.  
 1.65671 +**
 1.65672 +** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
 1.65673 +** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
 1.65674 +** bit is clear then fall through if either operand is NULL.
 1.65675 +**
 1.65676 +** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
 1.65677 +** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
 1.65678 +** to coerce both inputs according to this affinity before the
 1.65679 +** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
 1.65680 +** affinity is used. Note that the affinity conversions are stored
 1.65681 +** back into the input registers P1 and P3.  So this opcode can cause
 1.65682 +** persistent changes to registers P1 and P3.
 1.65683 +**
 1.65684 +** Once any conversions have taken place, and neither value is NULL, 
 1.65685 +** the values are compared. If both values are blobs then memcmp() is
 1.65686 +** used to determine the results of the comparison.  If both values
 1.65687 +** are text, then the appropriate collating function specified in
 1.65688 +** P4 is  used to do the comparison.  If P4 is not specified then
 1.65689 +** memcmp() is used to compare text string.  If both values are
 1.65690 +** numeric, then a numeric comparison is used. If the two values
 1.65691 +** are of different types, then numbers are considered less than
 1.65692 +** strings and strings are considered less than blobs.
 1.65693 +**
 1.65694 +** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
 1.65695 +** store a boolean result (either 0, or 1, or NULL) in register P2.
 1.65696 +**
 1.65697 +** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
 1.65698 +** equal to one another, provided that they do not have their MEM_Cleared
 1.65699 +** bit set.
 1.65700 +*/
 1.65701 +/* Opcode: Ne P1 P2 P3 P4 P5
 1.65702 +**
 1.65703 +** This works just like the Lt opcode except that the jump is taken if
 1.65704 +** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
 1.65705 +** additional information.
 1.65706 +**
 1.65707 +** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 1.65708 +** true or false and is never NULL.  If both operands are NULL then the result
 1.65709 +** of comparison is false.  If either operand is NULL then the result is true.
 1.65710 +** If neither operand is NULL the result is the same as it would be if
 1.65711 +** the SQLITE_NULLEQ flag were omitted from P5.
 1.65712 +*/
 1.65713 +/* Opcode: Eq P1 P2 P3 P4 P5
 1.65714 +**
 1.65715 +** This works just like the Lt opcode except that the jump is taken if
 1.65716 +** the operands in registers P1 and P3 are equal.
 1.65717 +** See the Lt opcode for additional information.
 1.65718 +**
 1.65719 +** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 1.65720 +** true or false and is never NULL.  If both operands are NULL then the result
 1.65721 +** of comparison is true.  If either operand is NULL then the result is false.
 1.65722 +** If neither operand is NULL the result is the same as it would be if
 1.65723 +** the SQLITE_NULLEQ flag were omitted from P5.
 1.65724 +*/
 1.65725 +/* Opcode: Le P1 P2 P3 P4 P5
 1.65726 +**
 1.65727 +** This works just like the Lt opcode except that the jump is taken if
 1.65728 +** the content of register P3 is less than or equal to the content of
 1.65729 +** register P1.  See the Lt opcode for additional information.
 1.65730 +*/
 1.65731 +/* Opcode: Gt P1 P2 P3 P4 P5
 1.65732 +**
 1.65733 +** This works just like the Lt opcode except that the jump is taken if
 1.65734 +** the content of register P3 is greater than the content of
 1.65735 +** register P1.  See the Lt opcode for additional information.
 1.65736 +*/
 1.65737 +/* Opcode: Ge P1 P2 P3 P4 P5
 1.65738 +**
 1.65739 +** This works just like the Lt opcode except that the jump is taken if
 1.65740 +** the content of register P3 is greater than or equal to the content of
 1.65741 +** register P1.  See the Lt opcode for additional information.
 1.65742 +*/
 1.65743 +case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
 1.65744 +case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
 1.65745 +case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
 1.65746 +case OP_Le:               /* same as TK_LE, jump, in1, in3 */
 1.65747 +case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
 1.65748 +case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
 1.65749 +#if 0  /* local variables moved into u.ak */
 1.65750 +  int res;            /* Result of the comparison of pIn1 against pIn3 */
 1.65751 +  char affinity;      /* Affinity to use for comparison */
 1.65752 +  u16 flags1;         /* Copy of initial value of pIn1->flags */
 1.65753 +  u16 flags3;         /* Copy of initial value of pIn3->flags */
 1.65754 +#endif /* local variables moved into u.ak */
 1.65755 +
 1.65756 +  pIn1 = &aMem[pOp->p1];
 1.65757 +  pIn3 = &aMem[pOp->p3];
 1.65758 +  u.ak.flags1 = pIn1->flags;
 1.65759 +  u.ak.flags3 = pIn3->flags;
 1.65760 +  if( (u.ak.flags1 | u.ak.flags3)&MEM_Null ){
 1.65761 +    /* One or both operands are NULL */
 1.65762 +    if( pOp->p5 & SQLITE_NULLEQ ){
 1.65763 +      /* If SQLITE_NULLEQ is set (which will only happen if the operator is
 1.65764 +      ** OP_Eq or OP_Ne) then take the jump or not depending on whether
 1.65765 +      ** or not both operands are null.
 1.65766 +      */
 1.65767 +      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 1.65768 +      assert( (u.ak.flags1 & MEM_Cleared)==0 );
 1.65769 +      if( (u.ak.flags1&MEM_Null)!=0
 1.65770 +       && (u.ak.flags3&MEM_Null)!=0
 1.65771 +       && (u.ak.flags3&MEM_Cleared)==0
 1.65772 +      ){
 1.65773 +        u.ak.res = 0;  /* Results are equal */
 1.65774 +      }else{
 1.65775 +        u.ak.res = 1;  /* Results are not equal */
 1.65776 +      }
 1.65777 +    }else{
 1.65778 +      /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 1.65779 +      ** then the result is always NULL.
 1.65780 +      ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 1.65781 +      */
 1.65782 +      if( pOp->p5 & SQLITE_STOREP2 ){
 1.65783 +        pOut = &aMem[pOp->p2];
 1.65784 +        MemSetTypeFlag(pOut, MEM_Null);
 1.65785 +        REGISTER_TRACE(pOp->p2, pOut);
 1.65786 +      }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
 1.65787 +        pc = pOp->p2-1;
 1.65788 +      }
 1.65789 +      break;
 1.65790 +    }
 1.65791 +  }else{
 1.65792 +    /* Neither operand is NULL.  Do a comparison. */
 1.65793 +    u.ak.affinity = pOp->p5 & SQLITE_AFF_MASK;
 1.65794 +    if( u.ak.affinity ){
 1.65795 +      applyAffinity(pIn1, u.ak.affinity, encoding);
 1.65796 +      applyAffinity(pIn3, u.ak.affinity, encoding);
 1.65797 +      if( db->mallocFailed ) goto no_mem;
 1.65798 +    }
 1.65799 +
 1.65800 +    assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
 1.65801 +    ExpandBlob(pIn1);
 1.65802 +    ExpandBlob(pIn3);
 1.65803 +    u.ak.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
 1.65804 +  }
 1.65805 +  switch( pOp->opcode ){
 1.65806 +    case OP_Eq:    u.ak.res = u.ak.res==0;     break;
 1.65807 +    case OP_Ne:    u.ak.res = u.ak.res!=0;     break;
 1.65808 +    case OP_Lt:    u.ak.res = u.ak.res<0;      break;
 1.65809 +    case OP_Le:    u.ak.res = u.ak.res<=0;     break;
 1.65810 +    case OP_Gt:    u.ak.res = u.ak.res>0;      break;
 1.65811 +    default:       u.ak.res = u.ak.res>=0;     break;
 1.65812 +  }
 1.65813 +
 1.65814 +  if( pOp->p5 & SQLITE_STOREP2 ){
 1.65815 +    pOut = &aMem[pOp->p2];
 1.65816 +    memAboutToChange(p, pOut);
 1.65817 +    MemSetTypeFlag(pOut, MEM_Int);
 1.65818 +    pOut->u.i = u.ak.res;
 1.65819 +    REGISTER_TRACE(pOp->p2, pOut);
 1.65820 +  }else if( u.ak.res ){
 1.65821 +    pc = pOp->p2-1;
 1.65822 +  }
 1.65823 +
 1.65824 +  /* Undo any changes made by applyAffinity() to the input registers. */
 1.65825 +  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ak.flags1&MEM_TypeMask);
 1.65826 +  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ak.flags3&MEM_TypeMask);
 1.65827 +  break;
 1.65828 +}
 1.65829 +
 1.65830 +/* Opcode: Permutation * * * P4 *
 1.65831 +**
 1.65832 +** Set the permutation used by the OP_Compare operator to be the array
 1.65833 +** of integers in P4.
 1.65834 +**
 1.65835 +** The permutation is only valid until the next OP_Compare that has
 1.65836 +** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
 1.65837 +** occur immediately prior to the OP_Compare.
 1.65838 +*/
 1.65839 +case OP_Permutation: {
 1.65840 +  assert( pOp->p4type==P4_INTARRAY );
 1.65841 +  assert( pOp->p4.ai );
 1.65842 +  aPermute = pOp->p4.ai;
 1.65843 +  break;
 1.65844 +}
 1.65845 +
 1.65846 +/* Opcode: Compare P1 P2 P3 P4 P5
 1.65847 +**
 1.65848 +** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
 1.65849 +** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
 1.65850 +** the comparison for use by the next OP_Jump instruct.
 1.65851 +**
 1.65852 +** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
 1.65853 +** determined by the most recent OP_Permutation operator.  If the
 1.65854 +** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
 1.65855 +** order.
 1.65856 +**
 1.65857 +** P4 is a KeyInfo structure that defines collating sequences and sort
 1.65858 +** orders for the comparison.  The permutation applies to registers
 1.65859 +** only.  The KeyInfo elements are used sequentially.
 1.65860 +**
 1.65861 +** The comparison is a sort comparison, so NULLs compare equal,
 1.65862 +** NULLs are less than numbers, numbers are less than strings,
 1.65863 +** and strings are less than blobs.
 1.65864 +*/
 1.65865 +case OP_Compare: {
 1.65866 +#if 0  /* local variables moved into u.al */
 1.65867 +  int n;
 1.65868 +  int i;
 1.65869 +  int p1;
 1.65870 +  int p2;
 1.65871 +  const KeyInfo *pKeyInfo;
 1.65872 +  int idx;
 1.65873 +  CollSeq *pColl;    /* Collating sequence to use on this term */
 1.65874 +  int bRev;          /* True for DESCENDING sort order */
 1.65875 +#endif /* local variables moved into u.al */
 1.65876 +
 1.65877 +  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
 1.65878 +  u.al.n = pOp->p3;
 1.65879 +  u.al.pKeyInfo = pOp->p4.pKeyInfo;
 1.65880 +  assert( u.al.n>0 );
 1.65881 +  assert( u.al.pKeyInfo!=0 );
 1.65882 +  u.al.p1 = pOp->p1;
 1.65883 +  u.al.p2 = pOp->p2;
 1.65884 +#if SQLITE_DEBUG
 1.65885 +  if( aPermute ){
 1.65886 +    int k, mx = 0;
 1.65887 +    for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
 1.65888 +    assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
 1.65889 +    assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
 1.65890 +  }else{
 1.65891 +    assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
 1.65892 +    assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
 1.65893 +  }
 1.65894 +#endif /* SQLITE_DEBUG */
 1.65895 +  for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
 1.65896 +    u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
 1.65897 +    assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
 1.65898 +    assert( memIsValid(&aMem[u.al.p2+u.al.idx]) );
 1.65899 +    REGISTER_TRACE(u.al.p1+u.al.idx, &aMem[u.al.p1+u.al.idx]);
 1.65900 +    REGISTER_TRACE(u.al.p2+u.al.idx, &aMem[u.al.p2+u.al.idx]);
 1.65901 +    assert( u.al.i<u.al.pKeyInfo->nField );
 1.65902 +    u.al.pColl = u.al.pKeyInfo->aColl[u.al.i];
 1.65903 +    u.al.bRev = u.al.pKeyInfo->aSortOrder[u.al.i];
 1.65904 +    iCompare = sqlite3MemCompare(&aMem[u.al.p1+u.al.idx], &aMem[u.al.p2+u.al.idx], u.al.pColl);
 1.65905 +    if( iCompare ){
 1.65906 +      if( u.al.bRev ) iCompare = -iCompare;
 1.65907 +      break;
 1.65908 +    }
 1.65909 +  }
 1.65910 +  aPermute = 0;
 1.65911 +  break;
 1.65912 +}
 1.65913 +
 1.65914 +/* Opcode: Jump P1 P2 P3 * *
 1.65915 +**
 1.65916 +** Jump to the instruction at address P1, P2, or P3 depending on whether
 1.65917 +** in the most recent OP_Compare instruction the P1 vector was less than
 1.65918 +** equal to, or greater than the P2 vector, respectively.
 1.65919 +*/
 1.65920 +case OP_Jump: {             /* jump */
 1.65921 +  if( iCompare<0 ){
 1.65922 +    pc = pOp->p1 - 1;
 1.65923 +  }else if( iCompare==0 ){
 1.65924 +    pc = pOp->p2 - 1;
 1.65925 +  }else{
 1.65926 +    pc = pOp->p3 - 1;
 1.65927 +  }
 1.65928 +  break;
 1.65929 +}
 1.65930 +
 1.65931 +/* Opcode: And P1 P2 P3 * *
 1.65932 +**
 1.65933 +** Take the logical AND of the values in registers P1 and P2 and
 1.65934 +** write the result into register P3.
 1.65935 +**
 1.65936 +** If either P1 or P2 is 0 (false) then the result is 0 even if
 1.65937 +** the other input is NULL.  A NULL and true or two NULLs give
 1.65938 +** a NULL output.
 1.65939 +*/
 1.65940 +/* Opcode: Or P1 P2 P3 * *
 1.65941 +**
 1.65942 +** Take the logical OR of the values in register P1 and P2 and
 1.65943 +** store the answer in register P3.
 1.65944 +**
 1.65945 +** If either P1 or P2 is nonzero (true) then the result is 1 (true)
 1.65946 +** even if the other input is NULL.  A NULL and false or two NULLs
 1.65947 +** give a NULL output.
 1.65948 +*/
 1.65949 +case OP_And:              /* same as TK_AND, in1, in2, out3 */
 1.65950 +case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
 1.65951 +#if 0  /* local variables moved into u.am */
 1.65952 +  int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 1.65953 +  int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 1.65954 +#endif /* local variables moved into u.am */
 1.65955 +
 1.65956 +  pIn1 = &aMem[pOp->p1];
 1.65957 +  if( pIn1->flags & MEM_Null ){
 1.65958 +    u.am.v1 = 2;
 1.65959 +  }else{
 1.65960 +    u.am.v1 = sqlite3VdbeIntValue(pIn1)!=0;
 1.65961 +  }
 1.65962 +  pIn2 = &aMem[pOp->p2];
 1.65963 +  if( pIn2->flags & MEM_Null ){
 1.65964 +    u.am.v2 = 2;
 1.65965 +  }else{
 1.65966 +    u.am.v2 = sqlite3VdbeIntValue(pIn2)!=0;
 1.65967 +  }
 1.65968 +  if( pOp->opcode==OP_And ){
 1.65969 +    static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
 1.65970 +    u.am.v1 = and_logic[u.am.v1*3+u.am.v2];
 1.65971 +  }else{
 1.65972 +    static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
 1.65973 +    u.am.v1 = or_logic[u.am.v1*3+u.am.v2];
 1.65974 +  }
 1.65975 +  pOut = &aMem[pOp->p3];
 1.65976 +  if( u.am.v1==2 ){
 1.65977 +    MemSetTypeFlag(pOut, MEM_Null);
 1.65978 +  }else{
 1.65979 +    pOut->u.i = u.am.v1;
 1.65980 +    MemSetTypeFlag(pOut, MEM_Int);
 1.65981 +  }
 1.65982 +  break;
 1.65983 +}
 1.65984 +
 1.65985 +/* Opcode: Not P1 P2 * * *
 1.65986 +**
 1.65987 +** Interpret the value in register P1 as a boolean value.  Store the
 1.65988 +** boolean complement in register P2.  If the value in register P1 is 
 1.65989 +** NULL, then a NULL is stored in P2.
 1.65990 +*/
 1.65991 +case OP_Not: {                /* same as TK_NOT, in1, out2 */
 1.65992 +  pIn1 = &aMem[pOp->p1];
 1.65993 +  pOut = &aMem[pOp->p2];
 1.65994 +  if( pIn1->flags & MEM_Null ){
 1.65995 +    sqlite3VdbeMemSetNull(pOut);
 1.65996 +  }else{
 1.65997 +    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
 1.65998 +  }
 1.65999 +  break;
 1.66000 +}
 1.66001 +
 1.66002 +/* Opcode: BitNot P1 P2 * * *
 1.66003 +**
 1.66004 +** Interpret the content of register P1 as an integer.  Store the
 1.66005 +** ones-complement of the P1 value into register P2.  If P1 holds
 1.66006 +** a NULL then store a NULL in P2.
 1.66007 +*/
 1.66008 +case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
 1.66009 +  pIn1 = &aMem[pOp->p1];
 1.66010 +  pOut = &aMem[pOp->p2];
 1.66011 +  if( pIn1->flags & MEM_Null ){
 1.66012 +    sqlite3VdbeMemSetNull(pOut);
 1.66013 +  }else{
 1.66014 +    sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
 1.66015 +  }
 1.66016 +  break;
 1.66017 +}
 1.66018 +
 1.66019 +/* Opcode: Once P1 P2 * * *
 1.66020 +**
 1.66021 +** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
 1.66022 +** set the flag and fall through to the next instruction.
 1.66023 +*/
 1.66024 +case OP_Once: {             /* jump */
 1.66025 +  assert( pOp->p1<p->nOnceFlag );
 1.66026 +  if( p->aOnceFlag[pOp->p1] ){
 1.66027 +    pc = pOp->p2-1;
 1.66028 +  }else{
 1.66029 +    p->aOnceFlag[pOp->p1] = 1;
 1.66030 +  }
 1.66031 +  break;
 1.66032 +}
 1.66033 +
 1.66034 +/* Opcode: If P1 P2 P3 * *
 1.66035 +**
 1.66036 +** Jump to P2 if the value in register P1 is true.  The value
 1.66037 +** is considered true if it is numeric and non-zero.  If the value
 1.66038 +** in P1 is NULL then take the jump if P3 is non-zero.
 1.66039 +*/
 1.66040 +/* Opcode: IfNot P1 P2 P3 * *
 1.66041 +**
 1.66042 +** Jump to P2 if the value in register P1 is False.  The value
 1.66043 +** is considered false if it has a numeric value of zero.  If the value
 1.66044 +** in P1 is NULL then take the jump if P3 is zero.
 1.66045 +*/
 1.66046 +case OP_If:                 /* jump, in1 */
 1.66047 +case OP_IfNot: {            /* jump, in1 */
 1.66048 +#if 0  /* local variables moved into u.an */
 1.66049 +  int c;
 1.66050 +#endif /* local variables moved into u.an */
 1.66051 +  pIn1 = &aMem[pOp->p1];
 1.66052 +  if( pIn1->flags & MEM_Null ){
 1.66053 +    u.an.c = pOp->p3;
 1.66054 +  }else{
 1.66055 +#ifdef SQLITE_OMIT_FLOATING_POINT
 1.66056 +    u.an.c = sqlite3VdbeIntValue(pIn1)!=0;
 1.66057 +#else
 1.66058 +    u.an.c = sqlite3VdbeRealValue(pIn1)!=0.0;
 1.66059 +#endif
 1.66060 +    if( pOp->opcode==OP_IfNot ) u.an.c = !u.an.c;
 1.66061 +  }
 1.66062 +  if( u.an.c ){
 1.66063 +    pc = pOp->p2-1;
 1.66064 +  }
 1.66065 +  break;
 1.66066 +}
 1.66067 +
 1.66068 +/* Opcode: IsNull P1 P2 * * *
 1.66069 +**
 1.66070 +** Jump to P2 if the value in register P1 is NULL.
 1.66071 +*/
 1.66072 +case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
 1.66073 +  pIn1 = &aMem[pOp->p1];
 1.66074 +  if( (pIn1->flags & MEM_Null)!=0 ){
 1.66075 +    pc = pOp->p2 - 1;
 1.66076 +  }
 1.66077 +  break;
 1.66078 +}
 1.66079 +
 1.66080 +/* Opcode: NotNull P1 P2 * * *
 1.66081 +**
 1.66082 +** Jump to P2 if the value in register P1 is not NULL.  
 1.66083 +*/
 1.66084 +case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
 1.66085 +  pIn1 = &aMem[pOp->p1];
 1.66086 +  if( (pIn1->flags & MEM_Null)==0 ){
 1.66087 +    pc = pOp->p2 - 1;
 1.66088 +  }
 1.66089 +  break;
 1.66090 +}
 1.66091 +
 1.66092 +/* Opcode: Column P1 P2 P3 P4 P5
 1.66093 +**
 1.66094 +** Interpret the data that cursor P1 points to as a structure built using
 1.66095 +** the MakeRecord instruction.  (See the MakeRecord opcode for additional
 1.66096 +** information about the format of the data.)  Extract the P2-th column
 1.66097 +** from this record.  If there are less that (P2+1) 
 1.66098 +** values in the record, extract a NULL.
 1.66099 +**
 1.66100 +** The value extracted is stored in register P3.
 1.66101 +**
 1.66102 +** If the column contains fewer than P2 fields, then extract a NULL.  Or,
 1.66103 +** if the P4 argument is a P4_MEM use the value of the P4 argument as
 1.66104 +** the result.
 1.66105 +**
 1.66106 +** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
 1.66107 +** then the cache of the cursor is reset prior to extracting the column.
 1.66108 +** The first OP_Column against a pseudo-table after the value of the content
 1.66109 +** register has changed should have this bit set.
 1.66110 +**
 1.66111 +** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
 1.66112 +** the result is guaranteed to only be used as the argument of a length()
 1.66113 +** or typeof() function, respectively.  The loading of large blobs can be
 1.66114 +** skipped for length() and all content loading can be skipped for typeof().
 1.66115 +*/
 1.66116 +case OP_Column: {
 1.66117 +#if 0  /* local variables moved into u.ao */
 1.66118 +  u32 payloadSize;   /* Number of bytes in the record */
 1.66119 +  i64 payloadSize64; /* Number of bytes in the record */
 1.66120 +  int p1;            /* P1 value of the opcode */
 1.66121 +  int p2;            /* column number to retrieve */
 1.66122 +  VdbeCursor *pC;    /* The VDBE cursor */
 1.66123 +  char *zRec;        /* Pointer to complete record-data */
 1.66124 +  BtCursor *pCrsr;   /* The BTree cursor */
 1.66125 +  u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
 1.66126 +  u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
 1.66127 +  int nField;        /* number of fields in the record */
 1.66128 +  int len;           /* The length of the serialized data for the column */
 1.66129 +  int i;             /* Loop counter */
 1.66130 +  char *zData;       /* Part of the record being decoded */
 1.66131 +  Mem *pDest;        /* Where to write the extracted value */
 1.66132 +  Mem sMem;          /* For storing the record being decoded */
 1.66133 +  u8 *zIdx;          /* Index into header */
 1.66134 +  u8 *zEndHdr;       /* Pointer to first byte after the header */
 1.66135 +  u32 offset;        /* Offset into the data */
 1.66136 +  u32 szField;       /* Number of bytes in the content of a field */
 1.66137 +  int szHdr;         /* Size of the header size field at start of record */
 1.66138 +  int avail;         /* Number of bytes of available data */
 1.66139 +  u32 t;             /* A type code from the record header */
 1.66140 +  Mem *pReg;         /* PseudoTable input register */
 1.66141 +#endif /* local variables moved into u.ao */
 1.66142 +
 1.66143 +
 1.66144 +  u.ao.p1 = pOp->p1;
 1.66145 +  u.ao.p2 = pOp->p2;
 1.66146 +  u.ao.pC = 0;
 1.66147 +  memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
 1.66148 +  assert( u.ao.p1<p->nCursor );
 1.66149 +  assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.66150 +  u.ao.pDest = &aMem[pOp->p3];
 1.66151 +  memAboutToChange(p, u.ao.pDest);
 1.66152 +  u.ao.zRec = 0;
 1.66153 +
 1.66154 +  /* This block sets the variable u.ao.payloadSize to be the total number of
 1.66155 +  ** bytes in the record.
 1.66156 +  **
 1.66157 +  ** u.ao.zRec is set to be the complete text of the record if it is available.
 1.66158 +  ** The complete record text is always available for pseudo-tables
 1.66159 +  ** If the record is stored in a cursor, the complete record text
 1.66160 +  ** might be available in the  u.ao.pC->aRow cache.  Or it might not be.
 1.66161 +  ** If the data is unavailable,  u.ao.zRec is set to NULL.
 1.66162 +  **
 1.66163 +  ** We also compute the number of columns in the record.  For cursors,
 1.66164 +  ** the number of columns is stored in the VdbeCursor.nField element.
 1.66165 +  */
 1.66166 +  u.ao.pC = p->apCsr[u.ao.p1];
 1.66167 +  assert( u.ao.pC!=0 );
 1.66168 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.66169 +  assert( u.ao.pC->pVtabCursor==0 );
 1.66170 +#endif
 1.66171 +  u.ao.pCrsr = u.ao.pC->pCursor;
 1.66172 +  if( u.ao.pCrsr!=0 ){
 1.66173 +    /* The record is stored in a B-Tree */
 1.66174 +    rc = sqlite3VdbeCursorMoveto(u.ao.pC);
 1.66175 +    if( rc ) goto abort_due_to_error;
 1.66176 +    if( u.ao.pC->nullRow ){
 1.66177 +      u.ao.payloadSize = 0;
 1.66178 +    }else if( u.ao.pC->cacheStatus==p->cacheCtr ){
 1.66179 +      u.ao.payloadSize = u.ao.pC->payloadSize;
 1.66180 +      u.ao.zRec = (char*)u.ao.pC->aRow;
 1.66181 +    }else if( u.ao.pC->isIndex ){
 1.66182 +      assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
 1.66183 +      VVA_ONLY(rc =) sqlite3BtreeKeySize(u.ao.pCrsr, &u.ao.payloadSize64);
 1.66184 +      assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
 1.66185 +      /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
 1.66186 +      ** payload size, so it is impossible for u.ao.payloadSize64 to be
 1.66187 +      ** larger than 32 bits. */
 1.66188 +      assert( (u.ao.payloadSize64 & SQLITE_MAX_U32)==(u64)u.ao.payloadSize64 );
 1.66189 +      u.ao.payloadSize = (u32)u.ao.payloadSize64;
 1.66190 +    }else{
 1.66191 +      assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
 1.66192 +      VVA_ONLY(rc =) sqlite3BtreeDataSize(u.ao.pCrsr, &u.ao.payloadSize);
 1.66193 +      assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
 1.66194 +    }
 1.66195 +  }else if( ALWAYS(u.ao.pC->pseudoTableReg>0) ){
 1.66196 +    u.ao.pReg = &aMem[u.ao.pC->pseudoTableReg];
 1.66197 +    if( u.ao.pC->multiPseudo ){
 1.66198 +      sqlite3VdbeMemShallowCopy(u.ao.pDest, u.ao.pReg+u.ao.p2, MEM_Ephem);
 1.66199 +      Deephemeralize(u.ao.pDest);
 1.66200 +      goto op_column_out;
 1.66201 +    }
 1.66202 +    assert( u.ao.pReg->flags & MEM_Blob );
 1.66203 +    assert( memIsValid(u.ao.pReg) );
 1.66204 +    u.ao.payloadSize = u.ao.pReg->n;
 1.66205 +    u.ao.zRec = u.ao.pReg->z;
 1.66206 +    u.ao.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
 1.66207 +    assert( u.ao.payloadSize==0 || u.ao.zRec!=0 );
 1.66208 +  }else{
 1.66209 +    /* Consider the row to be NULL */
 1.66210 +    u.ao.payloadSize = 0;
 1.66211 +  }
 1.66212 +
 1.66213 +  /* If u.ao.payloadSize is 0, then just store a NULL.  This can happen because of
 1.66214 +  ** nullRow or because of a corrupt database. */
 1.66215 +  if( u.ao.payloadSize==0 ){
 1.66216 +    MemSetTypeFlag(u.ao.pDest, MEM_Null);
 1.66217 +    goto op_column_out;
 1.66218 +  }
 1.66219 +  assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
 1.66220 +  if( u.ao.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.66221 +    goto too_big;
 1.66222 +  }
 1.66223 +
 1.66224 +  u.ao.nField = u.ao.pC->nField;
 1.66225 +  assert( u.ao.p2<u.ao.nField );
 1.66226 +
 1.66227 +  /* Read and parse the table header.  Store the results of the parse
 1.66228 +  ** into the record header cache fields of the cursor.
 1.66229 +  */
 1.66230 +  u.ao.aType = u.ao.pC->aType;
 1.66231 +  if( u.ao.pC->cacheStatus==p->cacheCtr ){
 1.66232 +    u.ao.aOffset = u.ao.pC->aOffset;
 1.66233 +  }else{
 1.66234 +    assert(u.ao.aType);
 1.66235 +    u.ao.avail = 0;
 1.66236 +    u.ao.pC->aOffset = u.ao.aOffset = &u.ao.aType[u.ao.nField];
 1.66237 +    u.ao.pC->payloadSize = u.ao.payloadSize;
 1.66238 +    u.ao.pC->cacheStatus = p->cacheCtr;
 1.66239 +
 1.66240 +    /* Figure out how many bytes are in the header */
 1.66241 +    if( u.ao.zRec ){
 1.66242 +      u.ao.zData = u.ao.zRec;
 1.66243 +    }else{
 1.66244 +      if( u.ao.pC->isIndex ){
 1.66245 +        u.ao.zData = (char*)sqlite3BtreeKeyFetch(u.ao.pCrsr, &u.ao.avail);
 1.66246 +      }else{
 1.66247 +        u.ao.zData = (char*)sqlite3BtreeDataFetch(u.ao.pCrsr, &u.ao.avail);
 1.66248 +      }
 1.66249 +      /* If KeyFetch()/DataFetch() managed to get the entire payload,
 1.66250 +      ** save the payload in the u.ao.pC->aRow cache.  That will save us from
 1.66251 +      ** having to make additional calls to fetch the content portion of
 1.66252 +      ** the record.
 1.66253 +      */
 1.66254 +      assert( u.ao.avail>=0 );
 1.66255 +      if( u.ao.payloadSize <= (u32)u.ao.avail ){
 1.66256 +        u.ao.zRec = u.ao.zData;
 1.66257 +        u.ao.pC->aRow = (u8*)u.ao.zData;
 1.66258 +      }else{
 1.66259 +        u.ao.pC->aRow = 0;
 1.66260 +      }
 1.66261 +    }
 1.66262 +    /* The following assert is true in all cases except when
 1.66263 +    ** the database file has been corrupted externally.
 1.66264 +    **    assert( u.ao.zRec!=0 || u.ao.avail>=u.ao.payloadSize || u.ao.avail>=9 ); */
 1.66265 +    u.ao.szHdr = getVarint32((u8*)u.ao.zData, u.ao.offset);
 1.66266 +
 1.66267 +    /* Make sure a corrupt database has not given us an oversize header.
 1.66268 +    ** Do this now to avoid an oversize memory allocation.
 1.66269 +    **
 1.66270 +    ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
 1.66271 +    ** types use so much data space that there can only be 4096 and 32 of
 1.66272 +    ** them, respectively.  So the maximum header length results from a
 1.66273 +    ** 3-byte type for each of the maximum of 32768 columns plus three
 1.66274 +    ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
 1.66275 +    */
 1.66276 +    if( u.ao.offset > 98307 ){
 1.66277 +      rc = SQLITE_CORRUPT_BKPT;
 1.66278 +      goto op_column_out;
 1.66279 +    }
 1.66280 +
 1.66281 +    /* Compute in u.ao.len the number of bytes of data we need to read in order
 1.66282 +    ** to get u.ao.nField type values.  u.ao.offset is an upper bound on this.  But
 1.66283 +    ** u.ao.nField might be significantly less than the true number of columns
 1.66284 +    ** in the table, and in that case, 5*u.ao.nField+3 might be smaller than u.ao.offset.
 1.66285 +    ** We want to minimize u.ao.len in order to limit the size of the memory
 1.66286 +    ** allocation, especially if a corrupt database file has caused u.ao.offset
 1.66287 +    ** to be oversized. Offset is limited to 98307 above.  But 98307 might
 1.66288 +    ** still exceed Robson memory allocation limits on some configurations.
 1.66289 +    ** On systems that cannot tolerate large memory allocations, u.ao.nField*5+3
 1.66290 +    ** will likely be much smaller since u.ao.nField will likely be less than
 1.66291 +    ** 20 or so.  This insures that Robson memory allocation limits are
 1.66292 +    ** not exceeded even for corrupt database files.
 1.66293 +    */
 1.66294 +    u.ao.len = u.ao.nField*5 + 3;
 1.66295 +    if( u.ao.len > (int)u.ao.offset ) u.ao.len = (int)u.ao.offset;
 1.66296 +
 1.66297 +    /* The KeyFetch() or DataFetch() above are fast and will get the entire
 1.66298 +    ** record header in most cases.  But they will fail to get the complete
 1.66299 +    ** record header if the record header does not fit on a single page
 1.66300 +    ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
 1.66301 +    ** acquire the complete header text.
 1.66302 +    */
 1.66303 +    if( !u.ao.zRec && u.ao.avail<u.ao.len ){
 1.66304 +      u.ao.sMem.flags = 0;
 1.66305 +      u.ao.sMem.db = 0;
 1.66306 +      rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, 0, u.ao.len, u.ao.pC->isIndex, &u.ao.sMem);
 1.66307 +      if( rc!=SQLITE_OK ){
 1.66308 +        goto op_column_out;
 1.66309 +      }
 1.66310 +      u.ao.zData = u.ao.sMem.z;
 1.66311 +    }
 1.66312 +    u.ao.zEndHdr = (u8 *)&u.ao.zData[u.ao.len];
 1.66313 +    u.ao.zIdx = (u8 *)&u.ao.zData[u.ao.szHdr];
 1.66314 +
 1.66315 +    /* Scan the header and use it to fill in the u.ao.aType[] and u.ao.aOffset[]
 1.66316 +    ** arrays.  u.ao.aType[u.ao.i] will contain the type integer for the u.ao.i-th
 1.66317 +    ** column and u.ao.aOffset[u.ao.i] will contain the u.ao.offset from the beginning
 1.66318 +    ** of the record to the start of the data for the u.ao.i-th column
 1.66319 +    */
 1.66320 +    for(u.ao.i=0; u.ao.i<u.ao.nField; u.ao.i++){
 1.66321 +      if( u.ao.zIdx<u.ao.zEndHdr ){
 1.66322 +        u.ao.aOffset[u.ao.i] = u.ao.offset;
 1.66323 +        if( u.ao.zIdx[0]<0x80 ){
 1.66324 +          u.ao.t = u.ao.zIdx[0];
 1.66325 +          u.ao.zIdx++;
 1.66326 +        }else{
 1.66327 +          u.ao.zIdx += sqlite3GetVarint32(u.ao.zIdx, &u.ao.t);
 1.66328 +        }
 1.66329 +        u.ao.aType[u.ao.i] = u.ao.t;
 1.66330 +        u.ao.szField = sqlite3VdbeSerialTypeLen(u.ao.t);
 1.66331 +        u.ao.offset += u.ao.szField;
 1.66332 +        if( u.ao.offset<u.ao.szField ){  /* True if u.ao.offset overflows */
 1.66333 +          u.ao.zIdx = &u.ao.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
 1.66334 +          break;
 1.66335 +        }
 1.66336 +      }else{
 1.66337 +        /* If u.ao.i is less that u.ao.nField, then there are fewer fields in this
 1.66338 +        ** record than SetNumColumns indicated there are columns in the
 1.66339 +        ** table. Set the u.ao.offset for any extra columns not present in
 1.66340 +        ** the record to 0. This tells code below to store the default value
 1.66341 +        ** for the column instead of deserializing a value from the record.
 1.66342 +        */
 1.66343 +        u.ao.aOffset[u.ao.i] = 0;
 1.66344 +      }
 1.66345 +    }
 1.66346 +    sqlite3VdbeMemRelease(&u.ao.sMem);
 1.66347 +    u.ao.sMem.flags = MEM_Null;
 1.66348 +
 1.66349 +    /* If we have read more header data than was contained in the header,
 1.66350 +    ** or if the end of the last field appears to be past the end of the
 1.66351 +    ** record, or if the end of the last field appears to be before the end
 1.66352 +    ** of the record (when all fields present), then we must be dealing
 1.66353 +    ** with a corrupt database.
 1.66354 +    */
 1.66355 +    if( (u.ao.zIdx > u.ao.zEndHdr) || (u.ao.offset > u.ao.payloadSize)
 1.66356 +         || (u.ao.zIdx==u.ao.zEndHdr && u.ao.offset!=u.ao.payloadSize) ){
 1.66357 +      rc = SQLITE_CORRUPT_BKPT;
 1.66358 +      goto op_column_out;
 1.66359 +    }
 1.66360 +  }
 1.66361 +
 1.66362 +  /* Get the column information. If u.ao.aOffset[u.ao.p2] is non-zero, then
 1.66363 +  ** deserialize the value from the record. If u.ao.aOffset[u.ao.p2] is zero,
 1.66364 +  ** then there are not enough fields in the record to satisfy the
 1.66365 +  ** request.  In this case, set the value NULL or to P4 if P4 is
 1.66366 +  ** a pointer to a Mem object.
 1.66367 +  */
 1.66368 +  if( u.ao.aOffset[u.ao.p2] ){
 1.66369 +    assert( rc==SQLITE_OK );
 1.66370 +    if( u.ao.zRec ){
 1.66371 +      /* This is the common case where the whole row fits on a single page */
 1.66372 +      VdbeMemRelease(u.ao.pDest);
 1.66373 +      sqlite3VdbeSerialGet((u8 *)&u.ao.zRec[u.ao.aOffset[u.ao.p2]], u.ao.aType[u.ao.p2], u.ao.pDest);
 1.66374 +    }else{
 1.66375 +      /* This branch happens only when the row overflows onto multiple pages */
 1.66376 +      u.ao.t = u.ao.aType[u.ao.p2];
 1.66377 +      if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
 1.66378 +       && ((u.ao.t>=12 && (u.ao.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
 1.66379 +      ){
 1.66380 +        /* Content is irrelevant for the typeof() function and for
 1.66381 +        ** the length(X) function if X is a blob.  So we might as well use
 1.66382 +        ** bogus content rather than reading content from disk.  NULL works
 1.66383 +        ** for text and blob and whatever is in the u.ao.payloadSize64 variable
 1.66384 +        ** will work for everything else. */
 1.66385 +        u.ao.zData = u.ao.t<12 ? (char*)&u.ao.payloadSize64 : 0;
 1.66386 +      }else{
 1.66387 +        u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.t);
 1.66388 +        sqlite3VdbeMemMove(&u.ao.sMem, u.ao.pDest);
 1.66389 +        rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, u.ao.aOffset[u.ao.p2], u.ao.len,  u.ao.pC->isIndex,
 1.66390 +                                     &u.ao.sMem);
 1.66391 +        if( rc!=SQLITE_OK ){
 1.66392 +          goto op_column_out;
 1.66393 +        }
 1.66394 +        u.ao.zData = u.ao.sMem.z;
 1.66395 +      }
 1.66396 +      sqlite3VdbeSerialGet((u8*)u.ao.zData, u.ao.t, u.ao.pDest);
 1.66397 +    }
 1.66398 +    u.ao.pDest->enc = encoding;
 1.66399 +  }else{
 1.66400 +    if( pOp->p4type==P4_MEM ){
 1.66401 +      sqlite3VdbeMemShallowCopy(u.ao.pDest, pOp->p4.pMem, MEM_Static);
 1.66402 +    }else{
 1.66403 +      MemSetTypeFlag(u.ao.pDest, MEM_Null);
 1.66404 +    }
 1.66405 +  }
 1.66406 +
 1.66407 +  /* If we dynamically allocated space to hold the data (in the
 1.66408 +  ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
 1.66409 +  ** dynamically allocated space over to the u.ao.pDest structure.
 1.66410 +  ** This prevents a memory copy.
 1.66411 +  */
 1.66412 +  if( u.ao.sMem.zMalloc ){
 1.66413 +    assert( u.ao.sMem.z==u.ao.sMem.zMalloc );
 1.66414 +    assert( !(u.ao.pDest->flags & MEM_Dyn) );
 1.66415 +    assert( !(u.ao.pDest->flags & (MEM_Blob|MEM_Str)) || u.ao.pDest->z==u.ao.sMem.z );
 1.66416 +    u.ao.pDest->flags &= ~(MEM_Ephem|MEM_Static);
 1.66417 +    u.ao.pDest->flags |= MEM_Term;
 1.66418 +    u.ao.pDest->z = u.ao.sMem.z;
 1.66419 +    u.ao.pDest->zMalloc = u.ao.sMem.zMalloc;
 1.66420 +  }
 1.66421 +
 1.66422 +  rc = sqlite3VdbeMemMakeWriteable(u.ao.pDest);
 1.66423 +
 1.66424 +op_column_out:
 1.66425 +  UPDATE_MAX_BLOBSIZE(u.ao.pDest);
 1.66426 +  REGISTER_TRACE(pOp->p3, u.ao.pDest);
 1.66427 +  break;
 1.66428 +}
 1.66429 +
 1.66430 +/* Opcode: Affinity P1 P2 * P4 *
 1.66431 +**
 1.66432 +** Apply affinities to a range of P2 registers starting with P1.
 1.66433 +**
 1.66434 +** P4 is a string that is P2 characters long. The nth character of the
 1.66435 +** string indicates the column affinity that should be used for the nth
 1.66436 +** memory cell in the range.
 1.66437 +*/
 1.66438 +case OP_Affinity: {
 1.66439 +#if 0  /* local variables moved into u.ap */
 1.66440 +  const char *zAffinity;   /* The affinity to be applied */
 1.66441 +  char cAff;               /* A single character of affinity */
 1.66442 +#endif /* local variables moved into u.ap */
 1.66443 +
 1.66444 +  u.ap.zAffinity = pOp->p4.z;
 1.66445 +  assert( u.ap.zAffinity!=0 );
 1.66446 +  assert( u.ap.zAffinity[pOp->p2]==0 );
 1.66447 +  pIn1 = &aMem[pOp->p1];
 1.66448 +  while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
 1.66449 +    assert( pIn1 <= &p->aMem[p->nMem] );
 1.66450 +    assert( memIsValid(pIn1) );
 1.66451 +    ExpandBlob(pIn1);
 1.66452 +    applyAffinity(pIn1, u.ap.cAff, encoding);
 1.66453 +    pIn1++;
 1.66454 +  }
 1.66455 +  break;
 1.66456 +}
 1.66457 +
 1.66458 +/* Opcode: MakeRecord P1 P2 P3 P4 *
 1.66459 +**
 1.66460 +** Convert P2 registers beginning with P1 into the [record format]
 1.66461 +** use as a data record in a database table or as a key
 1.66462 +** in an index.  The OP_Column opcode can decode the record later.
 1.66463 +**
 1.66464 +** P4 may be a string that is P2 characters long.  The nth character of the
 1.66465 +** string indicates the column affinity that should be used for the nth
 1.66466 +** field of the index key.
 1.66467 +**
 1.66468 +** The mapping from character to affinity is given by the SQLITE_AFF_
 1.66469 +** macros defined in sqliteInt.h.
 1.66470 +**
 1.66471 +** If P4 is NULL then all index fields have the affinity NONE.
 1.66472 +*/
 1.66473 +case OP_MakeRecord: {
 1.66474 +#if 0  /* local variables moved into u.aq */
 1.66475 +  u8 *zNewRecord;        /* A buffer to hold the data for the new record */
 1.66476 +  Mem *pRec;             /* The new record */
 1.66477 +  u64 nData;             /* Number of bytes of data space */
 1.66478 +  int nHdr;              /* Number of bytes of header space */
 1.66479 +  i64 nByte;             /* Data space required for this record */
 1.66480 +  int nZero;             /* Number of zero bytes at the end of the record */
 1.66481 +  int nVarint;           /* Number of bytes in a varint */
 1.66482 +  u32 serial_type;       /* Type field */
 1.66483 +  Mem *pData0;           /* First field to be combined into the record */
 1.66484 +  Mem *pLast;            /* Last field of the record */
 1.66485 +  int nField;            /* Number of fields in the record */
 1.66486 +  char *zAffinity;       /* The affinity string for the record */
 1.66487 +  int file_format;       /* File format to use for encoding */
 1.66488 +  int i;                 /* Space used in zNewRecord[] */
 1.66489 +  int len;               /* Length of a field */
 1.66490 +#endif /* local variables moved into u.aq */
 1.66491 +
 1.66492 +  /* Assuming the record contains N fields, the record format looks
 1.66493 +  ** like this:
 1.66494 +  **
 1.66495 +  ** ------------------------------------------------------------------------
 1.66496 +  ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
 1.66497 +  ** ------------------------------------------------------------------------
 1.66498 +  **
 1.66499 +  ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
 1.66500 +  ** and so froth.
 1.66501 +  **
 1.66502 +  ** Each type field is a varint representing the serial type of the
 1.66503 +  ** corresponding data element (see sqlite3VdbeSerialType()). The
 1.66504 +  ** hdr-size field is also a varint which is the offset from the beginning
 1.66505 +  ** of the record to data0.
 1.66506 +  */
 1.66507 +  u.aq.nData = 0;         /* Number of bytes of data space */
 1.66508 +  u.aq.nHdr = 0;          /* Number of bytes of header space */
 1.66509 +  u.aq.nZero = 0;         /* Number of zero bytes at the end of the record */
 1.66510 +  u.aq.nField = pOp->p1;
 1.66511 +  u.aq.zAffinity = pOp->p4.z;
 1.66512 +  assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
 1.66513 +  u.aq.pData0 = &aMem[u.aq.nField];
 1.66514 +  u.aq.nField = pOp->p2;
 1.66515 +  u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
 1.66516 +  u.aq.file_format = p->minWriteFileFormat;
 1.66517 +
 1.66518 +  /* Identify the output register */
 1.66519 +  assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
 1.66520 +  pOut = &aMem[pOp->p3];
 1.66521 +  memAboutToChange(p, pOut);
 1.66522 +
 1.66523 +  /* Loop through the elements that will make up the record to figure
 1.66524 +  ** out how much space is required for the new record.
 1.66525 +  */
 1.66526 +  for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
 1.66527 +    assert( memIsValid(u.aq.pRec) );
 1.66528 +    if( u.aq.zAffinity ){
 1.66529 +      applyAffinity(u.aq.pRec, u.aq.zAffinity[u.aq.pRec-u.aq.pData0], encoding);
 1.66530 +    }
 1.66531 +    if( u.aq.pRec->flags&MEM_Zero && u.aq.pRec->n>0 ){
 1.66532 +      sqlite3VdbeMemExpandBlob(u.aq.pRec);
 1.66533 +    }
 1.66534 +    u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
 1.66535 +    u.aq.len = sqlite3VdbeSerialTypeLen(u.aq.serial_type);
 1.66536 +    u.aq.nData += u.aq.len;
 1.66537 +    u.aq.nHdr += sqlite3VarintLen(u.aq.serial_type);
 1.66538 +    if( u.aq.pRec->flags & MEM_Zero ){
 1.66539 +      /* Only pure zero-filled BLOBs can be input to this Opcode.
 1.66540 +      ** We do not allow blobs with a prefix and a zero-filled tail. */
 1.66541 +      u.aq.nZero += u.aq.pRec->u.nZero;
 1.66542 +    }else if( u.aq.len ){
 1.66543 +      u.aq.nZero = 0;
 1.66544 +    }
 1.66545 +  }
 1.66546 +
 1.66547 +  /* Add the initial header varint and total the size */
 1.66548 +  u.aq.nHdr += u.aq.nVarint = sqlite3VarintLen(u.aq.nHdr);
 1.66549 +  if( u.aq.nVarint<sqlite3VarintLen(u.aq.nHdr) ){
 1.66550 +    u.aq.nHdr++;
 1.66551 +  }
 1.66552 +  u.aq.nByte = u.aq.nHdr+u.aq.nData-u.aq.nZero;
 1.66553 +  if( u.aq.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.66554 +    goto too_big;
 1.66555 +  }
 1.66556 +
 1.66557 +  /* Make sure the output register has a buffer large enough to store
 1.66558 +  ** the new record. The output register (pOp->p3) is not allowed to
 1.66559 +  ** be one of the input registers (because the following call to
 1.66560 +  ** sqlite3VdbeMemGrow() could clobber the value before it is used).
 1.66561 +  */
 1.66562 +  if( sqlite3VdbeMemGrow(pOut, (int)u.aq.nByte, 0) ){
 1.66563 +    goto no_mem;
 1.66564 +  }
 1.66565 +  u.aq.zNewRecord = (u8 *)pOut->z;
 1.66566 +
 1.66567 +  /* Write the record */
 1.66568 +  u.aq.i = putVarint32(u.aq.zNewRecord, u.aq.nHdr);
 1.66569 +  for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
 1.66570 +    u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
 1.66571 +    u.aq.i += putVarint32(&u.aq.zNewRecord[u.aq.i], u.aq.serial_type);      /* serial type */
 1.66572 +  }
 1.66573 +  for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){  /* serial data */
 1.66574 +    u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
 1.66575 +  }
 1.66576 +  assert( u.aq.i==u.aq.nByte );
 1.66577 +
 1.66578 +  assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.66579 +  pOut->n = (int)u.aq.nByte;
 1.66580 +  pOut->flags = MEM_Blob | MEM_Dyn;
 1.66581 +  pOut->xDel = 0;
 1.66582 +  if( u.aq.nZero ){
 1.66583 +    pOut->u.nZero = u.aq.nZero;
 1.66584 +    pOut->flags |= MEM_Zero;
 1.66585 +  }
 1.66586 +  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
 1.66587 +  REGISTER_TRACE(pOp->p3, pOut);
 1.66588 +  UPDATE_MAX_BLOBSIZE(pOut);
 1.66589 +  break;
 1.66590 +}
 1.66591 +
 1.66592 +/* Opcode: Count P1 P2 * * *
 1.66593 +**
 1.66594 +** Store the number of entries (an integer value) in the table or index 
 1.66595 +** opened by cursor P1 in register P2
 1.66596 +*/
 1.66597 +#ifndef SQLITE_OMIT_BTREECOUNT
 1.66598 +case OP_Count: {         /* out2-prerelease */
 1.66599 +#if 0  /* local variables moved into u.ar */
 1.66600 +  i64 nEntry;
 1.66601 +  BtCursor *pCrsr;
 1.66602 +#endif /* local variables moved into u.ar */
 1.66603 +
 1.66604 +  u.ar.pCrsr = p->apCsr[pOp->p1]->pCursor;
 1.66605 +  if( ALWAYS(u.ar.pCrsr) ){
 1.66606 +    rc = sqlite3BtreeCount(u.ar.pCrsr, &u.ar.nEntry);
 1.66607 +  }else{
 1.66608 +    u.ar.nEntry = 0;
 1.66609 +  }
 1.66610 +  pOut->u.i = u.ar.nEntry;
 1.66611 +  break;
 1.66612 +}
 1.66613 +#endif
 1.66614 +
 1.66615 +/* Opcode: Savepoint P1 * * P4 *
 1.66616 +**
 1.66617 +** Open, release or rollback the savepoint named by parameter P4, depending
 1.66618 +** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
 1.66619 +** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
 1.66620 +*/
 1.66621 +case OP_Savepoint: {
 1.66622 +#if 0  /* local variables moved into u.as */
 1.66623 +  int p1;                         /* Value of P1 operand */
 1.66624 +  char *zName;                    /* Name of savepoint */
 1.66625 +  int nName;
 1.66626 +  Savepoint *pNew;
 1.66627 +  Savepoint *pSavepoint;
 1.66628 +  Savepoint *pTmp;
 1.66629 +  int iSavepoint;
 1.66630 +  int ii;
 1.66631 +#endif /* local variables moved into u.as */
 1.66632 +
 1.66633 +  u.as.p1 = pOp->p1;
 1.66634 +  u.as.zName = pOp->p4.z;
 1.66635 +
 1.66636 +  /* Assert that the u.as.p1 parameter is valid. Also that if there is no open
 1.66637 +  ** transaction, then there cannot be any savepoints.
 1.66638 +  */
 1.66639 +  assert( db->pSavepoint==0 || db->autoCommit==0 );
 1.66640 +  assert( u.as.p1==SAVEPOINT_BEGIN||u.as.p1==SAVEPOINT_RELEASE||u.as.p1==SAVEPOINT_ROLLBACK );
 1.66641 +  assert( db->pSavepoint || db->isTransactionSavepoint==0 );
 1.66642 +  assert( checkSavepointCount(db) );
 1.66643 +
 1.66644 +  if( u.as.p1==SAVEPOINT_BEGIN ){
 1.66645 +    if( db->writeVdbeCnt>0 ){
 1.66646 +      /* A new savepoint cannot be created if there are active write
 1.66647 +      ** statements (i.e. open read/write incremental blob handles).
 1.66648 +      */
 1.66649 +      sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
 1.66650 +        "SQL statements in progress");
 1.66651 +      rc = SQLITE_BUSY;
 1.66652 +    }else{
 1.66653 +      u.as.nName = sqlite3Strlen30(u.as.zName);
 1.66654 +
 1.66655 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.66656 +      /* This call is Ok even if this savepoint is actually a transaction
 1.66657 +      ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
 1.66658 +      ** If this is a transaction savepoint being opened, it is guaranteed
 1.66659 +      ** that the db->aVTrans[] array is empty.  */
 1.66660 +      assert( db->autoCommit==0 || db->nVTrans==0 );
 1.66661 +      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
 1.66662 +                                db->nStatement+db->nSavepoint);
 1.66663 +      if( rc!=SQLITE_OK ) goto abort_due_to_error;
 1.66664 +#endif
 1.66665 +
 1.66666 +      /* Create a new savepoint structure. */
 1.66667 +      u.as.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.as.nName+1);
 1.66668 +      if( u.as.pNew ){
 1.66669 +        u.as.pNew->zName = (char *)&u.as.pNew[1];
 1.66670 +        memcpy(u.as.pNew->zName, u.as.zName, u.as.nName+1);
 1.66671 +
 1.66672 +        /* If there is no open transaction, then mark this as a special
 1.66673 +        ** "transaction savepoint". */
 1.66674 +        if( db->autoCommit ){
 1.66675 +          db->autoCommit = 0;
 1.66676 +          db->isTransactionSavepoint = 1;
 1.66677 +        }else{
 1.66678 +          db->nSavepoint++;
 1.66679 +        }
 1.66680 +
 1.66681 +        /* Link the new savepoint into the database handle's list. */
 1.66682 +        u.as.pNew->pNext = db->pSavepoint;
 1.66683 +        db->pSavepoint = u.as.pNew;
 1.66684 +        u.as.pNew->nDeferredCons = db->nDeferredCons;
 1.66685 +      }
 1.66686 +    }
 1.66687 +  }else{
 1.66688 +    u.as.iSavepoint = 0;
 1.66689 +
 1.66690 +    /* Find the named savepoint. If there is no such savepoint, then an
 1.66691 +    ** an error is returned to the user.  */
 1.66692 +    for(
 1.66693 +      u.as.pSavepoint = db->pSavepoint;
 1.66694 +      u.as.pSavepoint && sqlite3StrICmp(u.as.pSavepoint->zName, u.as.zName);
 1.66695 +      u.as.pSavepoint = u.as.pSavepoint->pNext
 1.66696 +    ){
 1.66697 +      u.as.iSavepoint++;
 1.66698 +    }
 1.66699 +    if( !u.as.pSavepoint ){
 1.66700 +      sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.as.zName);
 1.66701 +      rc = SQLITE_ERROR;
 1.66702 +    }else if( db->writeVdbeCnt>0 && u.as.p1==SAVEPOINT_RELEASE ){
 1.66703 +      /* It is not possible to release (commit) a savepoint if there are
 1.66704 +      ** active write statements.
 1.66705 +      */
 1.66706 +      sqlite3SetString(&p->zErrMsg, db,
 1.66707 +        "cannot release savepoint - SQL statements in progress"
 1.66708 +      );
 1.66709 +      rc = SQLITE_BUSY;
 1.66710 +    }else{
 1.66711 +
 1.66712 +      /* Determine whether or not this is a transaction savepoint. If so,
 1.66713 +      ** and this is a RELEASE command, then the current transaction
 1.66714 +      ** is committed.
 1.66715 +      */
 1.66716 +      int isTransaction = u.as.pSavepoint->pNext==0 && db->isTransactionSavepoint;
 1.66717 +      if( isTransaction && u.as.p1==SAVEPOINT_RELEASE ){
 1.66718 +        if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 1.66719 +          goto vdbe_return;
 1.66720 +        }
 1.66721 +        db->autoCommit = 1;
 1.66722 +        if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 1.66723 +          p->pc = pc;
 1.66724 +          db->autoCommit = 0;
 1.66725 +          p->rc = rc = SQLITE_BUSY;
 1.66726 +          goto vdbe_return;
 1.66727 +        }
 1.66728 +        db->isTransactionSavepoint = 0;
 1.66729 +        rc = p->rc;
 1.66730 +      }else{
 1.66731 +        u.as.iSavepoint = db->nSavepoint - u.as.iSavepoint - 1;
 1.66732 +        if( u.as.p1==SAVEPOINT_ROLLBACK ){
 1.66733 +          for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
 1.66734 +            sqlite3BtreeTripAllCursors(db->aDb[u.as.ii].pBt, SQLITE_ABORT);
 1.66735 +          }
 1.66736 +        }
 1.66737 +        for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
 1.66738 +          rc = sqlite3BtreeSavepoint(db->aDb[u.as.ii].pBt, u.as.p1, u.as.iSavepoint);
 1.66739 +          if( rc!=SQLITE_OK ){
 1.66740 +            goto abort_due_to_error;
 1.66741 +          }
 1.66742 +        }
 1.66743 +        if( u.as.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
 1.66744 +          sqlite3ExpirePreparedStatements(db);
 1.66745 +          sqlite3ResetAllSchemasOfConnection(db);
 1.66746 +          db->flags = (db->flags | SQLITE_InternChanges);
 1.66747 +        }
 1.66748 +      }
 1.66749 +
 1.66750 +      /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
 1.66751 +      ** savepoints nested inside of the savepoint being operated on. */
 1.66752 +      while( db->pSavepoint!=u.as.pSavepoint ){
 1.66753 +        u.as.pTmp = db->pSavepoint;
 1.66754 +        db->pSavepoint = u.as.pTmp->pNext;
 1.66755 +        sqlite3DbFree(db, u.as.pTmp);
 1.66756 +        db->nSavepoint--;
 1.66757 +      }
 1.66758 +
 1.66759 +      /* If it is a RELEASE, then destroy the savepoint being operated on
 1.66760 +      ** too. If it is a ROLLBACK TO, then set the number of deferred
 1.66761 +      ** constraint violations present in the database to the value stored
 1.66762 +      ** when the savepoint was created.  */
 1.66763 +      if( u.as.p1==SAVEPOINT_RELEASE ){
 1.66764 +        assert( u.as.pSavepoint==db->pSavepoint );
 1.66765 +        db->pSavepoint = u.as.pSavepoint->pNext;
 1.66766 +        sqlite3DbFree(db, u.as.pSavepoint);
 1.66767 +        if( !isTransaction ){
 1.66768 +          db->nSavepoint--;
 1.66769 +        }
 1.66770 +      }else{
 1.66771 +        db->nDeferredCons = u.as.pSavepoint->nDeferredCons;
 1.66772 +      }
 1.66773 +
 1.66774 +      if( !isTransaction ){
 1.66775 +        rc = sqlite3VtabSavepoint(db, u.as.p1, u.as.iSavepoint);
 1.66776 +        if( rc!=SQLITE_OK ) goto abort_due_to_error;
 1.66777 +      }
 1.66778 +    }
 1.66779 +  }
 1.66780 +
 1.66781 +  break;
 1.66782 +}
 1.66783 +
 1.66784 +/* Opcode: AutoCommit P1 P2 * * *
 1.66785 +**
 1.66786 +** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
 1.66787 +** back any currently active btree transactions. If there are any active
 1.66788 +** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
 1.66789 +** there are active writing VMs or active VMs that use shared cache.
 1.66790 +**
 1.66791 +** This instruction causes the VM to halt.
 1.66792 +*/
 1.66793 +case OP_AutoCommit: {
 1.66794 +#if 0  /* local variables moved into u.at */
 1.66795 +  int desiredAutoCommit;
 1.66796 +  int iRollback;
 1.66797 +  int turnOnAC;
 1.66798 +#endif /* local variables moved into u.at */
 1.66799 +
 1.66800 +  u.at.desiredAutoCommit = pOp->p1;
 1.66801 +  u.at.iRollback = pOp->p2;
 1.66802 +  u.at.turnOnAC = u.at.desiredAutoCommit && !db->autoCommit;
 1.66803 +  assert( u.at.desiredAutoCommit==1 || u.at.desiredAutoCommit==0 );
 1.66804 +  assert( u.at.desiredAutoCommit==1 || u.at.iRollback==0 );
 1.66805 +  assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
 1.66806 +
 1.66807 +#if 0
 1.66808 +  if( u.at.turnOnAC && u.at.iRollback && db->activeVdbeCnt>1 ){
 1.66809 +    /* If this instruction implements a ROLLBACK and other VMs are
 1.66810 +    ** still running, and a transaction is active, return an error indicating
 1.66811 +    ** that the other VMs must complete first.
 1.66812 +    */
 1.66813 +    sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
 1.66814 +        "SQL statements in progress");
 1.66815 +    rc = SQLITE_BUSY;
 1.66816 +  }else
 1.66817 +#endif
 1.66818 +  if( u.at.turnOnAC && !u.at.iRollback && db->writeVdbeCnt>0 ){
 1.66819 +    /* If this instruction implements a COMMIT and other VMs are writing
 1.66820 +    ** return an error indicating that the other VMs must complete first.
 1.66821 +    */
 1.66822 +    sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
 1.66823 +        "SQL statements in progress");
 1.66824 +    rc = SQLITE_BUSY;
 1.66825 +  }else if( u.at.desiredAutoCommit!=db->autoCommit ){
 1.66826 +    if( u.at.iRollback ){
 1.66827 +      assert( u.at.desiredAutoCommit==1 );
 1.66828 +      sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 1.66829 +      db->autoCommit = 1;
 1.66830 +    }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 1.66831 +      goto vdbe_return;
 1.66832 +    }else{
 1.66833 +      db->autoCommit = (u8)u.at.desiredAutoCommit;
 1.66834 +      if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 1.66835 +        p->pc = pc;
 1.66836 +        db->autoCommit = (u8)(1-u.at.desiredAutoCommit);
 1.66837 +        p->rc = rc = SQLITE_BUSY;
 1.66838 +        goto vdbe_return;
 1.66839 +      }
 1.66840 +    }
 1.66841 +    assert( db->nStatement==0 );
 1.66842 +    sqlite3CloseSavepoints(db);
 1.66843 +    if( p->rc==SQLITE_OK ){
 1.66844 +      rc = SQLITE_DONE;
 1.66845 +    }else{
 1.66846 +      rc = SQLITE_ERROR;
 1.66847 +    }
 1.66848 +    goto vdbe_return;
 1.66849 +  }else{
 1.66850 +    sqlite3SetString(&p->zErrMsg, db,
 1.66851 +        (!u.at.desiredAutoCommit)?"cannot start a transaction within a transaction":(
 1.66852 +        (u.at.iRollback)?"cannot rollback - no transaction is active":
 1.66853 +                   "cannot commit - no transaction is active"));
 1.66854 +
 1.66855 +    rc = SQLITE_ERROR;
 1.66856 +  }
 1.66857 +  break;
 1.66858 +}
 1.66859 +
 1.66860 +/* Opcode: Transaction P1 P2 * * *
 1.66861 +**
 1.66862 +** Begin a transaction.  The transaction ends when a Commit or Rollback
 1.66863 +** opcode is encountered.  Depending on the ON CONFLICT setting, the
 1.66864 +** transaction might also be rolled back if an error is encountered.
 1.66865 +**
 1.66866 +** P1 is the index of the database file on which the transaction is
 1.66867 +** started.  Index 0 is the main database file and index 1 is the
 1.66868 +** file used for temporary tables.  Indices of 2 or more are used for
 1.66869 +** attached databases.
 1.66870 +**
 1.66871 +** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
 1.66872 +** obtained on the database file when a write-transaction is started.  No
 1.66873 +** other process can start another write transaction while this transaction is
 1.66874 +** underway.  Starting a write transaction also creates a rollback journal. A
 1.66875 +** write transaction must be started before any changes can be made to the
 1.66876 +** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
 1.66877 +** on the file.
 1.66878 +**
 1.66879 +** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
 1.66880 +** true (this flag is set if the Vdbe may modify more than one row and may
 1.66881 +** throw an ABORT exception), a statement transaction may also be opened.
 1.66882 +** More specifically, a statement transaction is opened iff the database
 1.66883 +** connection is currently not in autocommit mode, or if there are other
 1.66884 +** active statements. A statement transaction allows the changes made by this
 1.66885 +** VDBE to be rolled back after an error without having to roll back the
 1.66886 +** entire transaction. If no error is encountered, the statement transaction
 1.66887 +** will automatically commit when the VDBE halts.
 1.66888 +**
 1.66889 +** If P2 is zero, then a read-lock is obtained on the database file.
 1.66890 +*/
 1.66891 +case OP_Transaction: {
 1.66892 +#if 0  /* local variables moved into u.au */
 1.66893 +  Btree *pBt;
 1.66894 +#endif /* local variables moved into u.au */
 1.66895 +
 1.66896 +  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.66897 +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 1.66898 +  u.au.pBt = db->aDb[pOp->p1].pBt;
 1.66899 +
 1.66900 +  if( u.au.pBt ){
 1.66901 +    rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2);
 1.66902 +    if( rc==SQLITE_BUSY ){
 1.66903 +      p->pc = pc;
 1.66904 +      p->rc = rc = SQLITE_BUSY;
 1.66905 +      goto vdbe_return;
 1.66906 +    }
 1.66907 +    if( rc!=SQLITE_OK ){
 1.66908 +      goto abort_due_to_error;
 1.66909 +    }
 1.66910 +
 1.66911 +    if( pOp->p2 && p->usesStmtJournal
 1.66912 +     && (db->autoCommit==0 || db->activeVdbeCnt>1)
 1.66913 +    ){
 1.66914 +      assert( sqlite3BtreeIsInTrans(u.au.pBt) );
 1.66915 +      if( p->iStatement==0 ){
 1.66916 +        assert( db->nStatement>=0 && db->nSavepoint>=0 );
 1.66917 +        db->nStatement++;
 1.66918 +        p->iStatement = db->nSavepoint + db->nStatement;
 1.66919 +      }
 1.66920 +
 1.66921 +      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
 1.66922 +      if( rc==SQLITE_OK ){
 1.66923 +        rc = sqlite3BtreeBeginStmt(u.au.pBt, p->iStatement);
 1.66924 +      }
 1.66925 +
 1.66926 +      /* Store the current value of the database handles deferred constraint
 1.66927 +      ** counter. If the statement transaction needs to be rolled back,
 1.66928 +      ** the value of this counter needs to be restored too.  */
 1.66929 +      p->nStmtDefCons = db->nDeferredCons;
 1.66930 +    }
 1.66931 +  }
 1.66932 +  break;
 1.66933 +}
 1.66934 +
 1.66935 +/* Opcode: ReadCookie P1 P2 P3 * *
 1.66936 +**
 1.66937 +** Read cookie number P3 from database P1 and write it into register P2.
 1.66938 +** P3==1 is the schema version.  P3==2 is the database format.
 1.66939 +** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
 1.66940 +** the main database file and P1==1 is the database file used to store
 1.66941 +** temporary tables.
 1.66942 +**
 1.66943 +** There must be a read-lock on the database (either a transaction
 1.66944 +** must be started or there must be an open cursor) before
 1.66945 +** executing this instruction.
 1.66946 +*/
 1.66947 +case OP_ReadCookie: {               /* out2-prerelease */
 1.66948 +#if 0  /* local variables moved into u.av */
 1.66949 +  int iMeta;
 1.66950 +  int iDb;
 1.66951 +  int iCookie;
 1.66952 +#endif /* local variables moved into u.av */
 1.66953 +
 1.66954 +  u.av.iDb = pOp->p1;
 1.66955 +  u.av.iCookie = pOp->p3;
 1.66956 +  assert( pOp->p3<SQLITE_N_BTREE_META );
 1.66957 +  assert( u.av.iDb>=0 && u.av.iDb<db->nDb );
 1.66958 +  assert( db->aDb[u.av.iDb].pBt!=0 );
 1.66959 +  assert( (p->btreeMask & (((yDbMask)1)<<u.av.iDb))!=0 );
 1.66960 +
 1.66961 +  sqlite3BtreeGetMeta(db->aDb[u.av.iDb].pBt, u.av.iCookie, (u32 *)&u.av.iMeta);
 1.66962 +  pOut->u.i = u.av.iMeta;
 1.66963 +  break;
 1.66964 +}
 1.66965 +
 1.66966 +/* Opcode: SetCookie P1 P2 P3 * *
 1.66967 +**
 1.66968 +** Write the content of register P3 (interpreted as an integer)
 1.66969 +** into cookie number P2 of database P1.  P2==1 is the schema version.  
 1.66970 +** P2==2 is the database format. P2==3 is the recommended pager cache 
 1.66971 +** size, and so forth.  P1==0 is the main database file and P1==1 is the 
 1.66972 +** database file used to store temporary tables.
 1.66973 +**
 1.66974 +** A transaction must be started before executing this opcode.
 1.66975 +*/
 1.66976 +case OP_SetCookie: {       /* in3 */
 1.66977 +#if 0  /* local variables moved into u.aw */
 1.66978 +  Db *pDb;
 1.66979 +#endif /* local variables moved into u.aw */
 1.66980 +  assert( pOp->p2<SQLITE_N_BTREE_META );
 1.66981 +  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.66982 +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 1.66983 +  u.aw.pDb = &db->aDb[pOp->p1];
 1.66984 +  assert( u.aw.pDb->pBt!=0 );
 1.66985 +  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 1.66986 +  pIn3 = &aMem[pOp->p3];
 1.66987 +  sqlite3VdbeMemIntegerify(pIn3);
 1.66988 +  /* See note about index shifting on OP_ReadCookie */
 1.66989 +  rc = sqlite3BtreeUpdateMeta(u.aw.pDb->pBt, pOp->p2, (int)pIn3->u.i);
 1.66990 +  if( pOp->p2==BTREE_SCHEMA_VERSION ){
 1.66991 +    /* When the schema cookie changes, record the new cookie internally */
 1.66992 +    u.aw.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
 1.66993 +    db->flags |= SQLITE_InternChanges;
 1.66994 +  }else if( pOp->p2==BTREE_FILE_FORMAT ){
 1.66995 +    /* Record changes in the file format */
 1.66996 +    u.aw.pDb->pSchema->file_format = (u8)pIn3->u.i;
 1.66997 +  }
 1.66998 +  if( pOp->p1==1 ){
 1.66999 +    /* Invalidate all prepared statements whenever the TEMP database
 1.67000 +    ** schema is changed.  Ticket #1644 */
 1.67001 +    sqlite3ExpirePreparedStatements(db);
 1.67002 +    p->expired = 0;
 1.67003 +  }
 1.67004 +  break;
 1.67005 +}
 1.67006 +
 1.67007 +/* Opcode: VerifyCookie P1 P2 P3 * *
 1.67008 +**
 1.67009 +** Check the value of global database parameter number 0 (the
 1.67010 +** schema version) and make sure it is equal to P2 and that the
 1.67011 +** generation counter on the local schema parse equals P3.
 1.67012 +**
 1.67013 +** P1 is the database number which is 0 for the main database file
 1.67014 +** and 1 for the file holding temporary tables and some higher number
 1.67015 +** for auxiliary databases.
 1.67016 +**
 1.67017 +** The cookie changes its value whenever the database schema changes.
 1.67018 +** This operation is used to detect when that the cookie has changed
 1.67019 +** and that the current process needs to reread the schema.
 1.67020 +**
 1.67021 +** Either a transaction needs to have been started or an OP_Open needs
 1.67022 +** to be executed (to establish a read lock) before this opcode is
 1.67023 +** invoked.
 1.67024 +*/
 1.67025 +case OP_VerifyCookie: {
 1.67026 +#if 0  /* local variables moved into u.ax */
 1.67027 +  int iMeta;
 1.67028 +  int iGen;
 1.67029 +  Btree *pBt;
 1.67030 +#endif /* local variables moved into u.ax */
 1.67031 +
 1.67032 +  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.67033 +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 1.67034 +  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 1.67035 +  u.ax.pBt = db->aDb[pOp->p1].pBt;
 1.67036 +  if( u.ax.pBt ){
 1.67037 +    sqlite3BtreeGetMeta(u.ax.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.ax.iMeta);
 1.67038 +    u.ax.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
 1.67039 +  }else{
 1.67040 +    u.ax.iGen = u.ax.iMeta = 0;
 1.67041 +  }
 1.67042 +  if( u.ax.iMeta!=pOp->p2 || u.ax.iGen!=pOp->p3 ){
 1.67043 +    sqlite3DbFree(db, p->zErrMsg);
 1.67044 +    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
 1.67045 +    /* If the schema-cookie from the database file matches the cookie
 1.67046 +    ** stored with the in-memory representation of the schema, do
 1.67047 +    ** not reload the schema from the database file.
 1.67048 +    **
 1.67049 +    ** If virtual-tables are in use, this is not just an optimization.
 1.67050 +    ** Often, v-tables store their data in other SQLite tables, which
 1.67051 +    ** are queried from within xNext() and other v-table methods using
 1.67052 +    ** prepared queries. If such a query is out-of-date, we do not want to
 1.67053 +    ** discard the database schema, as the user code implementing the
 1.67054 +    ** v-table would have to be ready for the sqlite3_vtab structure itself
 1.67055 +    ** to be invalidated whenever sqlite3_step() is called from within
 1.67056 +    ** a v-table method.
 1.67057 +    */
 1.67058 +    if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.ax.iMeta ){
 1.67059 +      sqlite3ResetOneSchema(db, pOp->p1);
 1.67060 +    }
 1.67061 +
 1.67062 +    p->expired = 1;
 1.67063 +    rc = SQLITE_SCHEMA;
 1.67064 +  }
 1.67065 +  break;
 1.67066 +}
 1.67067 +
 1.67068 +/* Opcode: OpenRead P1 P2 P3 P4 P5
 1.67069 +**
 1.67070 +** Open a read-only cursor for the database table whose root page is
 1.67071 +** P2 in a database file.  The database file is determined by P3. 
 1.67072 +** P3==0 means the main database, P3==1 means the database used for 
 1.67073 +** temporary tables, and P3>1 means used the corresponding attached
 1.67074 +** database.  Give the new cursor an identifier of P1.  The P1
 1.67075 +** values need not be contiguous but all P1 values should be small integers.
 1.67076 +** It is an error for P1 to be negative.
 1.67077 +**
 1.67078 +** If P5!=0 then use the content of register P2 as the root page, not
 1.67079 +** the value of P2 itself.
 1.67080 +**
 1.67081 +** There will be a read lock on the database whenever there is an
 1.67082 +** open cursor.  If the database was unlocked prior to this instruction
 1.67083 +** then a read lock is acquired as part of this instruction.  A read
 1.67084 +** lock allows other processes to read the database but prohibits
 1.67085 +** any other process from modifying the database.  The read lock is
 1.67086 +** released when all cursors are closed.  If this instruction attempts
 1.67087 +** to get a read lock but fails, the script terminates with an
 1.67088 +** SQLITE_BUSY error code.
 1.67089 +**
 1.67090 +** The P4 value may be either an integer (P4_INT32) or a pointer to
 1.67091 +** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
 1.67092 +** structure, then said structure defines the content and collating 
 1.67093 +** sequence of the index being opened. Otherwise, if P4 is an integer 
 1.67094 +** value, it is set to the number of columns in the table.
 1.67095 +**
 1.67096 +** See also OpenWrite.
 1.67097 +*/
 1.67098 +/* Opcode: OpenWrite P1 P2 P3 P4 P5
 1.67099 +**
 1.67100 +** Open a read/write cursor named P1 on the table or index whose root
 1.67101 +** page is P2.  Or if P5!=0 use the content of register P2 to find the
 1.67102 +** root page.
 1.67103 +**
 1.67104 +** The P4 value may be either an integer (P4_INT32) or a pointer to
 1.67105 +** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
 1.67106 +** structure, then said structure defines the content and collating 
 1.67107 +** sequence of the index being opened. Otherwise, if P4 is an integer 
 1.67108 +** value, it is set to the number of columns in the table, or to the
 1.67109 +** largest index of any column of the table that is actually used.
 1.67110 +**
 1.67111 +** This instruction works just like OpenRead except that it opens the cursor
 1.67112 +** in read/write mode.  For a given table, there can be one or more read-only
 1.67113 +** cursors or a single read/write cursor but not both.
 1.67114 +**
 1.67115 +** See also OpenRead.
 1.67116 +*/
 1.67117 +case OP_OpenRead:
 1.67118 +case OP_OpenWrite: {
 1.67119 +#if 0  /* local variables moved into u.ay */
 1.67120 +  int nField;
 1.67121 +  KeyInfo *pKeyInfo;
 1.67122 +  int p2;
 1.67123 +  int iDb;
 1.67124 +  int wrFlag;
 1.67125 +  Btree *pX;
 1.67126 +  VdbeCursor *pCur;
 1.67127 +  Db *pDb;
 1.67128 +#endif /* local variables moved into u.ay */
 1.67129 +
 1.67130 +  assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
 1.67131 +  assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
 1.67132 +
 1.67133 +  if( p->expired ){
 1.67134 +    rc = SQLITE_ABORT;
 1.67135 +    break;
 1.67136 +  }
 1.67137 +
 1.67138 +  u.ay.nField = 0;
 1.67139 +  u.ay.pKeyInfo = 0;
 1.67140 +  u.ay.p2 = pOp->p2;
 1.67141 +  u.ay.iDb = pOp->p3;
 1.67142 +  assert( u.ay.iDb>=0 && u.ay.iDb<db->nDb );
 1.67143 +  assert( (p->btreeMask & (((yDbMask)1)<<u.ay.iDb))!=0 );
 1.67144 +  u.ay.pDb = &db->aDb[u.ay.iDb];
 1.67145 +  u.ay.pX = u.ay.pDb->pBt;
 1.67146 +  assert( u.ay.pX!=0 );
 1.67147 +  if( pOp->opcode==OP_OpenWrite ){
 1.67148 +    u.ay.wrFlag = 1;
 1.67149 +    assert( sqlite3SchemaMutexHeld(db, u.ay.iDb, 0) );
 1.67150 +    if( u.ay.pDb->pSchema->file_format < p->minWriteFileFormat ){
 1.67151 +      p->minWriteFileFormat = u.ay.pDb->pSchema->file_format;
 1.67152 +    }
 1.67153 +  }else{
 1.67154 +    u.ay.wrFlag = 0;
 1.67155 +  }
 1.67156 +  if( pOp->p5 & OPFLAG_P2ISREG ){
 1.67157 +    assert( u.ay.p2>0 );
 1.67158 +    assert( u.ay.p2<=p->nMem );
 1.67159 +    pIn2 = &aMem[u.ay.p2];
 1.67160 +    assert( memIsValid(pIn2) );
 1.67161 +    assert( (pIn2->flags & MEM_Int)!=0 );
 1.67162 +    sqlite3VdbeMemIntegerify(pIn2);
 1.67163 +    u.ay.p2 = (int)pIn2->u.i;
 1.67164 +    /* The u.ay.p2 value always comes from a prior OP_CreateTable opcode and
 1.67165 +    ** that opcode will always set the u.ay.p2 value to 2 or more or else fail.
 1.67166 +    ** If there were a failure, the prepared statement would have halted
 1.67167 +    ** before reaching this instruction. */
 1.67168 +    if( NEVER(u.ay.p2<2) ) {
 1.67169 +      rc = SQLITE_CORRUPT_BKPT;
 1.67170 +      goto abort_due_to_error;
 1.67171 +    }
 1.67172 +  }
 1.67173 +  if( pOp->p4type==P4_KEYINFO ){
 1.67174 +    u.ay.pKeyInfo = pOp->p4.pKeyInfo;
 1.67175 +    u.ay.pKeyInfo->enc = ENC(p->db);
 1.67176 +    u.ay.nField = u.ay.pKeyInfo->nField+1;
 1.67177 +  }else if( pOp->p4type==P4_INT32 ){
 1.67178 +    u.ay.nField = pOp->p4.i;
 1.67179 +  }
 1.67180 +  assert( pOp->p1>=0 );
 1.67181 +  u.ay.pCur = allocateCursor(p, pOp->p1, u.ay.nField, u.ay.iDb, 1);
 1.67182 +  if( u.ay.pCur==0 ) goto no_mem;
 1.67183 +  u.ay.pCur->nullRow = 1;
 1.67184 +  u.ay.pCur->isOrdered = 1;
 1.67185 +  rc = sqlite3BtreeCursor(u.ay.pX, u.ay.p2, u.ay.wrFlag, u.ay.pKeyInfo, u.ay.pCur->pCursor);
 1.67186 +  u.ay.pCur->pKeyInfo = u.ay.pKeyInfo;
 1.67187 +  assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
 1.67188 +  sqlite3BtreeCursorHints(u.ay.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
 1.67189 +
 1.67190 +  /* Since it performs no memory allocation or IO, the only value that
 1.67191 +  ** sqlite3BtreeCursor() may return is SQLITE_OK. */
 1.67192 +  assert( rc==SQLITE_OK );
 1.67193 +
 1.67194 +  /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
 1.67195 +  ** SQLite used to check if the root-page flags were sane at this point
 1.67196 +  ** and report database corruption if they were not, but this check has
 1.67197 +  ** since moved into the btree layer.  */
 1.67198 +  u.ay.pCur->isTable = pOp->p4type!=P4_KEYINFO;
 1.67199 +  u.ay.pCur->isIndex = !u.ay.pCur->isTable;
 1.67200 +  break;
 1.67201 +}
 1.67202 +
 1.67203 +/* Opcode: OpenEphemeral P1 P2 * P4 P5
 1.67204 +**
 1.67205 +** Open a new cursor P1 to a transient table.
 1.67206 +** The cursor is always opened read/write even if 
 1.67207 +** the main database is read-only.  The ephemeral
 1.67208 +** table is deleted automatically when the cursor is closed.
 1.67209 +**
 1.67210 +** P2 is the number of columns in the ephemeral table.
 1.67211 +** The cursor points to a BTree table if P4==0 and to a BTree index
 1.67212 +** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
 1.67213 +** that defines the format of keys in the index.
 1.67214 +**
 1.67215 +** This opcode was once called OpenTemp.  But that created
 1.67216 +** confusion because the term "temp table", might refer either
 1.67217 +** to a TEMP table at the SQL level, or to a table opened by
 1.67218 +** this opcode.  Then this opcode was call OpenVirtual.  But
 1.67219 +** that created confusion with the whole virtual-table idea.
 1.67220 +**
 1.67221 +** The P5 parameter can be a mask of the BTREE_* flags defined
 1.67222 +** in btree.h.  These flags control aspects of the operation of
 1.67223 +** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
 1.67224 +** added automatically.
 1.67225 +*/
 1.67226 +/* Opcode: OpenAutoindex P1 P2 * P4 *
 1.67227 +**
 1.67228 +** This opcode works the same as OP_OpenEphemeral.  It has a
 1.67229 +** different name to distinguish its use.  Tables created using
 1.67230 +** by this opcode will be used for automatically created transient
 1.67231 +** indices in joins.
 1.67232 +*/
 1.67233 +case OP_OpenAutoindex: 
 1.67234 +case OP_OpenEphemeral: {
 1.67235 +#if 0  /* local variables moved into u.az */
 1.67236 +  VdbeCursor *pCx;
 1.67237 +#endif /* local variables moved into u.az */
 1.67238 +  static const int vfsFlags =
 1.67239 +      SQLITE_OPEN_READWRITE |
 1.67240 +      SQLITE_OPEN_CREATE |
 1.67241 +      SQLITE_OPEN_EXCLUSIVE |
 1.67242 +      SQLITE_OPEN_DELETEONCLOSE |
 1.67243 +      SQLITE_OPEN_TRANSIENT_DB;
 1.67244 +
 1.67245 +  assert( pOp->p1>=0 );
 1.67246 +  u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 1.67247 +  if( u.az.pCx==0 ) goto no_mem;
 1.67248 +  u.az.pCx->nullRow = 1;
 1.67249 +  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.az.pCx->pBt,
 1.67250 +                        BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
 1.67251 +  if( rc==SQLITE_OK ){
 1.67252 +    rc = sqlite3BtreeBeginTrans(u.az.pCx->pBt, 1);
 1.67253 +  }
 1.67254 +  if( rc==SQLITE_OK ){
 1.67255 +    /* If a transient index is required, create it by calling
 1.67256 +    ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
 1.67257 +    ** opening it. If a transient table is required, just use the
 1.67258 +    ** automatically created table with root-page 1 (an BLOB_INTKEY table).
 1.67259 +    */
 1.67260 +    if( pOp->p4.pKeyInfo ){
 1.67261 +      int pgno;
 1.67262 +      assert( pOp->p4type==P4_KEYINFO );
 1.67263 +      rc = sqlite3BtreeCreateTable(u.az.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
 1.67264 +      if( rc==SQLITE_OK ){
 1.67265 +        assert( pgno==MASTER_ROOT+1 );
 1.67266 +        rc = sqlite3BtreeCursor(u.az.pCx->pBt, pgno, 1,
 1.67267 +                                (KeyInfo*)pOp->p4.z, u.az.pCx->pCursor);
 1.67268 +        u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
 1.67269 +        u.az.pCx->pKeyInfo->enc = ENC(p->db);
 1.67270 +      }
 1.67271 +      u.az.pCx->isTable = 0;
 1.67272 +    }else{
 1.67273 +      rc = sqlite3BtreeCursor(u.az.pCx->pBt, MASTER_ROOT, 1, 0, u.az.pCx->pCursor);
 1.67274 +      u.az.pCx->isTable = 1;
 1.67275 +    }
 1.67276 +  }
 1.67277 +  u.az.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
 1.67278 +  u.az.pCx->isIndex = !u.az.pCx->isTable;
 1.67279 +  break;
 1.67280 +}
 1.67281 +
 1.67282 +/* Opcode: SorterOpen P1 P2 * P4 *
 1.67283 +**
 1.67284 +** This opcode works like OP_OpenEphemeral except that it opens
 1.67285 +** a transient index that is specifically designed to sort large
 1.67286 +** tables using an external merge-sort algorithm.
 1.67287 +*/
 1.67288 +case OP_SorterOpen: {
 1.67289 +#if 0  /* local variables moved into u.ba */
 1.67290 +  VdbeCursor *pCx;
 1.67291 +#endif /* local variables moved into u.ba */
 1.67292 +
 1.67293 +#ifndef SQLITE_OMIT_MERGE_SORT
 1.67294 +  u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 1.67295 +  if( u.ba.pCx==0 ) goto no_mem;
 1.67296 +  u.ba.pCx->pKeyInfo = pOp->p4.pKeyInfo;
 1.67297 +  u.ba.pCx->pKeyInfo->enc = ENC(p->db);
 1.67298 +  u.ba.pCx->isSorter = 1;
 1.67299 +  rc = sqlite3VdbeSorterInit(db, u.ba.pCx);
 1.67300 +#else
 1.67301 +  pOp->opcode = OP_OpenEphemeral;
 1.67302 +  pc--;
 1.67303 +#endif
 1.67304 +  break;
 1.67305 +}
 1.67306 +
 1.67307 +/* Opcode: OpenPseudo P1 P2 P3 * P5
 1.67308 +**
 1.67309 +** Open a new cursor that points to a fake table that contains a single
 1.67310 +** row of data.  The content of that one row in the content of memory
 1.67311 +** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the 
 1.67312 +** MEM_Blob content contained in register P2.  When P5==1, then the
 1.67313 +** row is represented by P3 consecutive registers beginning with P2.
 1.67314 +**
 1.67315 +** A pseudo-table created by this opcode is used to hold a single
 1.67316 +** row output from the sorter so that the row can be decomposed into
 1.67317 +** individual columns using the OP_Column opcode.  The OP_Column opcode
 1.67318 +** is the only cursor opcode that works with a pseudo-table.
 1.67319 +**
 1.67320 +** P3 is the number of fields in the records that will be stored by
 1.67321 +** the pseudo-table.
 1.67322 +*/
 1.67323 +case OP_OpenPseudo: {
 1.67324 +#if 0  /* local variables moved into u.bb */
 1.67325 +  VdbeCursor *pCx;
 1.67326 +#endif /* local variables moved into u.bb */
 1.67327 +
 1.67328 +  assert( pOp->p1>=0 );
 1.67329 +  u.bb.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
 1.67330 +  if( u.bb.pCx==0 ) goto no_mem;
 1.67331 +  u.bb.pCx->nullRow = 1;
 1.67332 +  u.bb.pCx->pseudoTableReg = pOp->p2;
 1.67333 +  u.bb.pCx->isTable = 1;
 1.67334 +  u.bb.pCx->isIndex = 0;
 1.67335 +  u.bb.pCx->multiPseudo = pOp->p5;
 1.67336 +  break;
 1.67337 +}
 1.67338 +
 1.67339 +/* Opcode: Close P1 * * * *
 1.67340 +**
 1.67341 +** Close a cursor previously opened as P1.  If P1 is not
 1.67342 +** currently open, this instruction is a no-op.
 1.67343 +*/
 1.67344 +case OP_Close: {
 1.67345 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67346 +  sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
 1.67347 +  p->apCsr[pOp->p1] = 0;
 1.67348 +  break;
 1.67349 +}
 1.67350 +
 1.67351 +/* Opcode: SeekGe P1 P2 P3 P4 *
 1.67352 +**
 1.67353 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 1.67354 +** use the value in register P3 as the key.  If cursor P1 refers 
 1.67355 +** to an SQL index, then P3 is the first in an array of P4 registers 
 1.67356 +** that are used as an unpacked index key. 
 1.67357 +**
 1.67358 +** Reposition cursor P1 so that  it points to the smallest entry that 
 1.67359 +** is greater than or equal to the key value. If there are no records 
 1.67360 +** greater than or equal to the key and P2 is not zero, then jump to P2.
 1.67361 +**
 1.67362 +** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
 1.67363 +*/
 1.67364 +/* Opcode: SeekGt P1 P2 P3 P4 *
 1.67365 +**
 1.67366 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 1.67367 +** use the value in register P3 as a key. If cursor P1 refers 
 1.67368 +** to an SQL index, then P3 is the first in an array of P4 registers 
 1.67369 +** that are used as an unpacked index key. 
 1.67370 +**
 1.67371 +** Reposition cursor P1 so that  it points to the smallest entry that 
 1.67372 +** is greater than the key value. If there are no records greater than 
 1.67373 +** the key and P2 is not zero, then jump to P2.
 1.67374 +**
 1.67375 +** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
 1.67376 +*/
 1.67377 +/* Opcode: SeekLt P1 P2 P3 P4 * 
 1.67378 +**
 1.67379 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 1.67380 +** use the value in register P3 as a key. If cursor P1 refers 
 1.67381 +** to an SQL index, then P3 is the first in an array of P4 registers 
 1.67382 +** that are used as an unpacked index key. 
 1.67383 +**
 1.67384 +** Reposition cursor P1 so that  it points to the largest entry that 
 1.67385 +** is less than the key value. If there are no records less than 
 1.67386 +** the key and P2 is not zero, then jump to P2.
 1.67387 +**
 1.67388 +** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
 1.67389 +*/
 1.67390 +/* Opcode: SeekLe P1 P2 P3 P4 *
 1.67391 +**
 1.67392 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 1.67393 +** use the value in register P3 as a key. If cursor P1 refers 
 1.67394 +** to an SQL index, then P3 is the first in an array of P4 registers 
 1.67395 +** that are used as an unpacked index key. 
 1.67396 +**
 1.67397 +** Reposition cursor P1 so that it points to the largest entry that 
 1.67398 +** is less than or equal to the key value. If there are no records 
 1.67399 +** less than or equal to the key and P2 is not zero, then jump to P2.
 1.67400 +**
 1.67401 +** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
 1.67402 +*/
 1.67403 +case OP_SeekLt:         /* jump, in3 */
 1.67404 +case OP_SeekLe:         /* jump, in3 */
 1.67405 +case OP_SeekGe:         /* jump, in3 */
 1.67406 +case OP_SeekGt: {       /* jump, in3 */
 1.67407 +#if 0  /* local variables moved into u.bc */
 1.67408 +  int res;
 1.67409 +  int oc;
 1.67410 +  VdbeCursor *pC;
 1.67411 +  UnpackedRecord r;
 1.67412 +  int nField;
 1.67413 +  i64 iKey;      /* The rowid we are to seek to */
 1.67414 +#endif /* local variables moved into u.bc */
 1.67415 +
 1.67416 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67417 +  assert( pOp->p2!=0 );
 1.67418 +  u.bc.pC = p->apCsr[pOp->p1];
 1.67419 +  assert( u.bc.pC!=0 );
 1.67420 +  assert( u.bc.pC->pseudoTableReg==0 );
 1.67421 +  assert( OP_SeekLe == OP_SeekLt+1 );
 1.67422 +  assert( OP_SeekGe == OP_SeekLt+2 );
 1.67423 +  assert( OP_SeekGt == OP_SeekLt+3 );
 1.67424 +  assert( u.bc.pC->isOrdered );
 1.67425 +  if( ALWAYS(u.bc.pC->pCursor!=0) ){
 1.67426 +    u.bc.oc = pOp->opcode;
 1.67427 +    u.bc.pC->nullRow = 0;
 1.67428 +    if( u.bc.pC->isTable ){
 1.67429 +      /* The input value in P3 might be of any type: integer, real, string,
 1.67430 +      ** blob, or NULL.  But it needs to be an integer before we can do
 1.67431 +      ** the seek, so covert it. */
 1.67432 +      pIn3 = &aMem[pOp->p3];
 1.67433 +      applyNumericAffinity(pIn3);
 1.67434 +      u.bc.iKey = sqlite3VdbeIntValue(pIn3);
 1.67435 +      u.bc.pC->rowidIsValid = 0;
 1.67436 +
 1.67437 +      /* If the P3 value could not be converted into an integer without
 1.67438 +      ** loss of information, then special processing is required... */
 1.67439 +      if( (pIn3->flags & MEM_Int)==0 ){
 1.67440 +        if( (pIn3->flags & MEM_Real)==0 ){
 1.67441 +          /* If the P3 value cannot be converted into any kind of a number,
 1.67442 +          ** then the seek is not possible, so jump to P2 */
 1.67443 +          pc = pOp->p2 - 1;
 1.67444 +          break;
 1.67445 +        }
 1.67446 +        /* If we reach this point, then the P3 value must be a floating
 1.67447 +        ** point number. */
 1.67448 +        assert( (pIn3->flags & MEM_Real)!=0 );
 1.67449 +
 1.67450 +        if( u.bc.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bc.iKey || pIn3->r>0) ){
 1.67451 +          /* The P3 value is too large in magnitude to be expressed as an
 1.67452 +          ** integer. */
 1.67453 +          u.bc.res = 1;
 1.67454 +          if( pIn3->r<0 ){
 1.67455 +            if( u.bc.oc>=OP_SeekGe ){  assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
 1.67456 +              rc = sqlite3BtreeFirst(u.bc.pC->pCursor, &u.bc.res);
 1.67457 +              if( rc!=SQLITE_OK ) goto abort_due_to_error;
 1.67458 +            }
 1.67459 +          }else{
 1.67460 +            if( u.bc.oc<=OP_SeekLe ){  assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
 1.67461 +              rc = sqlite3BtreeLast(u.bc.pC->pCursor, &u.bc.res);
 1.67462 +              if( rc!=SQLITE_OK ) goto abort_due_to_error;
 1.67463 +            }
 1.67464 +          }
 1.67465 +          if( u.bc.res ){
 1.67466 +            pc = pOp->p2 - 1;
 1.67467 +          }
 1.67468 +          break;
 1.67469 +        }else if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekGe ){
 1.67470 +          /* Use the ceiling() function to convert real->int */
 1.67471 +          if( pIn3->r > (double)u.bc.iKey ) u.bc.iKey++;
 1.67472 +        }else{
 1.67473 +          /* Use the floor() function to convert real->int */
 1.67474 +          assert( u.bc.oc==OP_SeekLe || u.bc.oc==OP_SeekGt );
 1.67475 +          if( pIn3->r < (double)u.bc.iKey ) u.bc.iKey--;
 1.67476 +        }
 1.67477 +      }
 1.67478 +      rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, 0, (u64)u.bc.iKey, 0, &u.bc.res);
 1.67479 +      if( rc!=SQLITE_OK ){
 1.67480 +        goto abort_due_to_error;
 1.67481 +      }
 1.67482 +      if( u.bc.res==0 ){
 1.67483 +        u.bc.pC->rowidIsValid = 1;
 1.67484 +        u.bc.pC->lastRowid = u.bc.iKey;
 1.67485 +      }
 1.67486 +    }else{
 1.67487 +      u.bc.nField = pOp->p4.i;
 1.67488 +      assert( pOp->p4type==P4_INT32 );
 1.67489 +      assert( u.bc.nField>0 );
 1.67490 +      u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
 1.67491 +      u.bc.r.nField = (u16)u.bc.nField;
 1.67492 +
 1.67493 +      /* The next line of code computes as follows, only faster:
 1.67494 +      **   if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekLe ){
 1.67495 +      **     u.bc.r.flags = UNPACKED_INCRKEY;
 1.67496 +      **   }else{
 1.67497 +      **     u.bc.r.flags = 0;
 1.67498 +      **   }
 1.67499 +      */
 1.67500 +      u.bc.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
 1.67501 +      assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
 1.67502 +      assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
 1.67503 +      assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
 1.67504 +      assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
 1.67505 +
 1.67506 +      u.bc.r.aMem = &aMem[pOp->p3];
 1.67507 +#ifdef SQLITE_DEBUG
 1.67508 +      { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
 1.67509 +#endif
 1.67510 +      ExpandBlob(u.bc.r.aMem);
 1.67511 +      rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, &u.bc.r, 0, 0, &u.bc.res);
 1.67512 +      if( rc!=SQLITE_OK ){
 1.67513 +        goto abort_due_to_error;
 1.67514 +      }
 1.67515 +      u.bc.pC->rowidIsValid = 0;
 1.67516 +    }
 1.67517 +    u.bc.pC->deferredMoveto = 0;
 1.67518 +    u.bc.pC->cacheStatus = CACHE_STALE;
 1.67519 +#ifdef SQLITE_TEST
 1.67520 +    sqlite3_search_count++;
 1.67521 +#endif
 1.67522 +    if( u.bc.oc>=OP_SeekGe ){  assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
 1.67523 +      if( u.bc.res<0 || (u.bc.res==0 && u.bc.oc==OP_SeekGt) ){
 1.67524 +        rc = sqlite3BtreeNext(u.bc.pC->pCursor, &u.bc.res);
 1.67525 +        if( rc!=SQLITE_OK ) goto abort_due_to_error;
 1.67526 +        u.bc.pC->rowidIsValid = 0;
 1.67527 +      }else{
 1.67528 +        u.bc.res = 0;
 1.67529 +      }
 1.67530 +    }else{
 1.67531 +      assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
 1.67532 +      if( u.bc.res>0 || (u.bc.res==0 && u.bc.oc==OP_SeekLt) ){
 1.67533 +        rc = sqlite3BtreePrevious(u.bc.pC->pCursor, &u.bc.res);
 1.67534 +        if( rc!=SQLITE_OK ) goto abort_due_to_error;
 1.67535 +        u.bc.pC->rowidIsValid = 0;
 1.67536 +      }else{
 1.67537 +        /* u.bc.res might be negative because the table is empty.  Check to
 1.67538 +        ** see if this is the case.
 1.67539 +        */
 1.67540 +        u.bc.res = sqlite3BtreeEof(u.bc.pC->pCursor);
 1.67541 +      }
 1.67542 +    }
 1.67543 +    assert( pOp->p2>0 );
 1.67544 +    if( u.bc.res ){
 1.67545 +      pc = pOp->p2 - 1;
 1.67546 +    }
 1.67547 +  }else{
 1.67548 +    /* This happens when attempting to open the sqlite3_master table
 1.67549 +    ** for read access returns SQLITE_EMPTY. In this case always
 1.67550 +    ** take the jump (since there are no records in the table).
 1.67551 +    */
 1.67552 +    pc = pOp->p2 - 1;
 1.67553 +  }
 1.67554 +  break;
 1.67555 +}
 1.67556 +
 1.67557 +/* Opcode: Seek P1 P2 * * *
 1.67558 +**
 1.67559 +** P1 is an open table cursor and P2 is a rowid integer.  Arrange
 1.67560 +** for P1 to move so that it points to the rowid given by P2.
 1.67561 +**
 1.67562 +** This is actually a deferred seek.  Nothing actually happens until
 1.67563 +** the cursor is used to read a record.  That way, if no reads
 1.67564 +** occur, no unnecessary I/O happens.
 1.67565 +*/
 1.67566 +case OP_Seek: {    /* in2 */
 1.67567 +#if 0  /* local variables moved into u.bd */
 1.67568 +  VdbeCursor *pC;
 1.67569 +#endif /* local variables moved into u.bd */
 1.67570 +
 1.67571 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67572 +  u.bd.pC = p->apCsr[pOp->p1];
 1.67573 +  assert( u.bd.pC!=0 );
 1.67574 +  if( ALWAYS(u.bd.pC->pCursor!=0) ){
 1.67575 +    assert( u.bd.pC->isTable );
 1.67576 +    u.bd.pC->nullRow = 0;
 1.67577 +    pIn2 = &aMem[pOp->p2];
 1.67578 +    u.bd.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
 1.67579 +    u.bd.pC->rowidIsValid = 0;
 1.67580 +    u.bd.pC->deferredMoveto = 1;
 1.67581 +  }
 1.67582 +  break;
 1.67583 +}
 1.67584 +  
 1.67585 +
 1.67586 +/* Opcode: Found P1 P2 P3 P4 *
 1.67587 +**
 1.67588 +** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 1.67589 +** P4>0 then register P3 is the first of P4 registers that form an unpacked
 1.67590 +** record.
 1.67591 +**
 1.67592 +** Cursor P1 is on an index btree.  If the record identified by P3 and P4
 1.67593 +** is a prefix of any entry in P1 then a jump is made to P2 and
 1.67594 +** P1 is left pointing at the matching entry.
 1.67595 +*/
 1.67596 +/* Opcode: NotFound P1 P2 P3 P4 *
 1.67597 +**
 1.67598 +** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 1.67599 +** P4>0 then register P3 is the first of P4 registers that form an unpacked
 1.67600 +** record.
 1.67601 +** 
 1.67602 +** Cursor P1 is on an index btree.  If the record identified by P3 and P4
 1.67603 +** is not the prefix of any entry in P1 then a jump is made to P2.  If P1 
 1.67604 +** does contain an entry whose prefix matches the P3/P4 record then control
 1.67605 +** falls through to the next instruction and P1 is left pointing at the
 1.67606 +** matching entry.
 1.67607 +**
 1.67608 +** See also: Found, NotExists, IsUnique
 1.67609 +*/
 1.67610 +case OP_NotFound:       /* jump, in3 */
 1.67611 +case OP_Found: {        /* jump, in3 */
 1.67612 +#if 0  /* local variables moved into u.be */
 1.67613 +  int alreadyExists;
 1.67614 +  VdbeCursor *pC;
 1.67615 +  int res;
 1.67616 +  char *pFree;
 1.67617 +  UnpackedRecord *pIdxKey;
 1.67618 +  UnpackedRecord r;
 1.67619 +  char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
 1.67620 +#endif /* local variables moved into u.be */
 1.67621 +
 1.67622 +#ifdef SQLITE_TEST
 1.67623 +  sqlite3_found_count++;
 1.67624 +#endif
 1.67625 +
 1.67626 +  u.be.alreadyExists = 0;
 1.67627 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67628 +  assert( pOp->p4type==P4_INT32 );
 1.67629 +  u.be.pC = p->apCsr[pOp->p1];
 1.67630 +  assert( u.be.pC!=0 );
 1.67631 +  pIn3 = &aMem[pOp->p3];
 1.67632 +  if( ALWAYS(u.be.pC->pCursor!=0) ){
 1.67633 +
 1.67634 +    assert( u.be.pC->isTable==0 );
 1.67635 +    if( pOp->p4.i>0 ){
 1.67636 +      u.be.r.pKeyInfo = u.be.pC->pKeyInfo;
 1.67637 +      u.be.r.nField = (u16)pOp->p4.i;
 1.67638 +      u.be.r.aMem = pIn3;
 1.67639 +#ifdef SQLITE_DEBUG
 1.67640 +      { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
 1.67641 +#endif
 1.67642 +      u.be.r.flags = UNPACKED_PREFIX_MATCH;
 1.67643 +      u.be.pIdxKey = &u.be.r;
 1.67644 +    }else{
 1.67645 +      u.be.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 1.67646 +          u.be.pC->pKeyInfo, u.be.aTempRec, sizeof(u.be.aTempRec), &u.be.pFree
 1.67647 +      );
 1.67648 +      if( u.be.pIdxKey==0 ) goto no_mem;
 1.67649 +      assert( pIn3->flags & MEM_Blob );
 1.67650 +      assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
 1.67651 +      sqlite3VdbeRecordUnpack(u.be.pC->pKeyInfo, pIn3->n, pIn3->z, u.be.pIdxKey);
 1.67652 +      u.be.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
 1.67653 +    }
 1.67654 +    rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, u.be.pIdxKey, 0, 0, &u.be.res);
 1.67655 +    if( pOp->p4.i==0 ){
 1.67656 +      sqlite3DbFree(db, u.be.pFree);
 1.67657 +    }
 1.67658 +    if( rc!=SQLITE_OK ){
 1.67659 +      break;
 1.67660 +    }
 1.67661 +    u.be.alreadyExists = (u.be.res==0);
 1.67662 +    u.be.pC->deferredMoveto = 0;
 1.67663 +    u.be.pC->cacheStatus = CACHE_STALE;
 1.67664 +  }
 1.67665 +  if( pOp->opcode==OP_Found ){
 1.67666 +    if( u.be.alreadyExists ) pc = pOp->p2 - 1;
 1.67667 +  }else{
 1.67668 +    if( !u.be.alreadyExists ) pc = pOp->p2 - 1;
 1.67669 +  }
 1.67670 +  break;
 1.67671 +}
 1.67672 +
 1.67673 +/* Opcode: IsUnique P1 P2 P3 P4 *
 1.67674 +**
 1.67675 +** Cursor P1 is open on an index b-tree - that is to say, a btree which
 1.67676 +** no data and where the key are records generated by OP_MakeRecord with
 1.67677 +** the list field being the integer ROWID of the entry that the index
 1.67678 +** entry refers to.
 1.67679 +**
 1.67680 +** The P3 register contains an integer record number. Call this record 
 1.67681 +** number R. Register P4 is the first in a set of N contiguous registers
 1.67682 +** that make up an unpacked index key that can be used with cursor P1.
 1.67683 +** The value of N can be inferred from the cursor. N includes the rowid
 1.67684 +** value appended to the end of the index record. This rowid value may
 1.67685 +** or may not be the same as R.
 1.67686 +**
 1.67687 +** If any of the N registers beginning with register P4 contains a NULL
 1.67688 +** value, jump immediately to P2.
 1.67689 +**
 1.67690 +** Otherwise, this instruction checks if cursor P1 contains an entry
 1.67691 +** where the first (N-1) fields match but the rowid value at the end
 1.67692 +** of the index entry is not R. If there is no such entry, control jumps
 1.67693 +** to instruction P2. Otherwise, the rowid of the conflicting index
 1.67694 +** entry is copied to register P3 and control falls through to the next
 1.67695 +** instruction.
 1.67696 +**
 1.67697 +** See also: NotFound, NotExists, Found
 1.67698 +*/
 1.67699 +case OP_IsUnique: {        /* jump, in3 */
 1.67700 +#if 0  /* local variables moved into u.bf */
 1.67701 +  u16 ii;
 1.67702 +  VdbeCursor *pCx;
 1.67703 +  BtCursor *pCrsr;
 1.67704 +  u16 nField;
 1.67705 +  Mem *aMx;
 1.67706 +  UnpackedRecord r;                  /* B-Tree index search key */
 1.67707 +  i64 R;                             /* Rowid stored in register P3 */
 1.67708 +#endif /* local variables moved into u.bf */
 1.67709 +
 1.67710 +  pIn3 = &aMem[pOp->p3];
 1.67711 +  u.bf.aMx = &aMem[pOp->p4.i];
 1.67712 +  /* Assert that the values of parameters P1 and P4 are in range. */
 1.67713 +  assert( pOp->p4type==P4_INT32 );
 1.67714 +  assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
 1.67715 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67716 +
 1.67717 +  /* Find the index cursor. */
 1.67718 +  u.bf.pCx = p->apCsr[pOp->p1];
 1.67719 +  assert( u.bf.pCx->deferredMoveto==0 );
 1.67720 +  u.bf.pCx->seekResult = 0;
 1.67721 +  u.bf.pCx->cacheStatus = CACHE_STALE;
 1.67722 +  u.bf.pCrsr = u.bf.pCx->pCursor;
 1.67723 +
 1.67724 +  /* If any of the values are NULL, take the jump. */
 1.67725 +  u.bf.nField = u.bf.pCx->pKeyInfo->nField;
 1.67726 +  for(u.bf.ii=0; u.bf.ii<u.bf.nField; u.bf.ii++){
 1.67727 +    if( u.bf.aMx[u.bf.ii].flags & MEM_Null ){
 1.67728 +      pc = pOp->p2 - 1;
 1.67729 +      u.bf.pCrsr = 0;
 1.67730 +      break;
 1.67731 +    }
 1.67732 +  }
 1.67733 +  assert( (u.bf.aMx[u.bf.nField].flags & MEM_Null)==0 );
 1.67734 +
 1.67735 +  if( u.bf.pCrsr!=0 ){
 1.67736 +    /* Populate the index search key. */
 1.67737 +    u.bf.r.pKeyInfo = u.bf.pCx->pKeyInfo;
 1.67738 +    u.bf.r.nField = u.bf.nField + 1;
 1.67739 +    u.bf.r.flags = UNPACKED_PREFIX_SEARCH;
 1.67740 +    u.bf.r.aMem = u.bf.aMx;
 1.67741 +#ifdef SQLITE_DEBUG
 1.67742 +    { int i; for(i=0; i<u.bf.r.nField; i++) assert( memIsValid(&u.bf.r.aMem[i]) ); }
 1.67743 +#endif
 1.67744 +
 1.67745 +    /* Extract the value of u.bf.R from register P3. */
 1.67746 +    sqlite3VdbeMemIntegerify(pIn3);
 1.67747 +    u.bf.R = pIn3->u.i;
 1.67748 +
 1.67749 +    /* Search the B-Tree index. If no conflicting record is found, jump
 1.67750 +    ** to P2. Otherwise, copy the rowid of the conflicting record to
 1.67751 +    ** register P3 and fall through to the next instruction.  */
 1.67752 +    rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, &u.bf.r, 0, 0, &u.bf.pCx->seekResult);
 1.67753 +    if( (u.bf.r.flags & UNPACKED_PREFIX_SEARCH) || u.bf.r.rowid==u.bf.R ){
 1.67754 +      pc = pOp->p2 - 1;
 1.67755 +    }else{
 1.67756 +      pIn3->u.i = u.bf.r.rowid;
 1.67757 +    }
 1.67758 +  }
 1.67759 +  break;
 1.67760 +}
 1.67761 +
 1.67762 +/* Opcode: NotExists P1 P2 P3 * *
 1.67763 +**
 1.67764 +** Use the content of register P3 as an integer key.  If a record 
 1.67765 +** with that key does not exist in table of P1, then jump to P2. 
 1.67766 +** If the record does exist, then fall through.  The cursor is left 
 1.67767 +** pointing to the record if it exists.
 1.67768 +**
 1.67769 +** The difference between this operation and NotFound is that this
 1.67770 +** operation assumes the key is an integer and that P1 is a table whereas
 1.67771 +** NotFound assumes key is a blob constructed from MakeRecord and
 1.67772 +** P1 is an index.
 1.67773 +**
 1.67774 +** See also: Found, NotFound, IsUnique
 1.67775 +*/
 1.67776 +case OP_NotExists: {        /* jump, in3 */
 1.67777 +#if 0  /* local variables moved into u.bg */
 1.67778 +  VdbeCursor *pC;
 1.67779 +  BtCursor *pCrsr;
 1.67780 +  int res;
 1.67781 +  u64 iKey;
 1.67782 +#endif /* local variables moved into u.bg */
 1.67783 +
 1.67784 +  pIn3 = &aMem[pOp->p3];
 1.67785 +  assert( pIn3->flags & MEM_Int );
 1.67786 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67787 +  u.bg.pC = p->apCsr[pOp->p1];
 1.67788 +  assert( u.bg.pC!=0 );
 1.67789 +  assert( u.bg.pC->isTable );
 1.67790 +  assert( u.bg.pC->pseudoTableReg==0 );
 1.67791 +  u.bg.pCrsr = u.bg.pC->pCursor;
 1.67792 +  if( ALWAYS(u.bg.pCrsr!=0) ){
 1.67793 +    u.bg.res = 0;
 1.67794 +    u.bg.iKey = pIn3->u.i;
 1.67795 +    rc = sqlite3BtreeMovetoUnpacked(u.bg.pCrsr, 0, u.bg.iKey, 0, &u.bg.res);
 1.67796 +    u.bg.pC->lastRowid = pIn3->u.i;
 1.67797 +    u.bg.pC->rowidIsValid = u.bg.res==0 ?1:0;
 1.67798 +    u.bg.pC->nullRow = 0;
 1.67799 +    u.bg.pC->cacheStatus = CACHE_STALE;
 1.67800 +    u.bg.pC->deferredMoveto = 0;
 1.67801 +    if( u.bg.res!=0 ){
 1.67802 +      pc = pOp->p2 - 1;
 1.67803 +      assert( u.bg.pC->rowidIsValid==0 );
 1.67804 +    }
 1.67805 +    u.bg.pC->seekResult = u.bg.res;
 1.67806 +  }else{
 1.67807 +    /* This happens when an attempt to open a read cursor on the
 1.67808 +    ** sqlite_master table returns SQLITE_EMPTY.
 1.67809 +    */
 1.67810 +    pc = pOp->p2 - 1;
 1.67811 +    assert( u.bg.pC->rowidIsValid==0 );
 1.67812 +    u.bg.pC->seekResult = 0;
 1.67813 +  }
 1.67814 +  break;
 1.67815 +}
 1.67816 +
 1.67817 +/* Opcode: Sequence P1 P2 * * *
 1.67818 +**
 1.67819 +** Find the next available sequence number for cursor P1.
 1.67820 +** Write the sequence number into register P2.
 1.67821 +** The sequence number on the cursor is incremented after this
 1.67822 +** instruction.  
 1.67823 +*/
 1.67824 +case OP_Sequence: {           /* out2-prerelease */
 1.67825 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67826 +  assert( p->apCsr[pOp->p1]!=0 );
 1.67827 +  pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
 1.67828 +  break;
 1.67829 +}
 1.67830 +
 1.67831 +
 1.67832 +/* Opcode: NewRowid P1 P2 P3 * *
 1.67833 +**
 1.67834 +** Get a new integer record number (a.k.a "rowid") used as the key to a table.
 1.67835 +** The record number is not previously used as a key in the database
 1.67836 +** table that cursor P1 points to.  The new record number is written
 1.67837 +** written to register P2.
 1.67838 +**
 1.67839 +** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
 1.67840 +** the largest previously generated record number. No new record numbers are
 1.67841 +** allowed to be less than this value. When this value reaches its maximum, 
 1.67842 +** an SQLITE_FULL error is generated. The P3 register is updated with the '
 1.67843 +** generated record number. This P3 mechanism is used to help implement the
 1.67844 +** AUTOINCREMENT feature.
 1.67845 +*/
 1.67846 +case OP_NewRowid: {           /* out2-prerelease */
 1.67847 +#if 0  /* local variables moved into u.bh */
 1.67848 +  i64 v;                 /* The new rowid */
 1.67849 +  VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 1.67850 +  int res;               /* Result of an sqlite3BtreeLast() */
 1.67851 +  int cnt;               /* Counter to limit the number of searches */
 1.67852 +  Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 1.67853 +  VdbeFrame *pFrame;     /* Root frame of VDBE */
 1.67854 +#endif /* local variables moved into u.bh */
 1.67855 +
 1.67856 +  u.bh.v = 0;
 1.67857 +  u.bh.res = 0;
 1.67858 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.67859 +  u.bh.pC = p->apCsr[pOp->p1];
 1.67860 +  assert( u.bh.pC!=0 );
 1.67861 +  if( NEVER(u.bh.pC->pCursor==0) ){
 1.67862 +    /* The zero initialization above is all that is needed */
 1.67863 +  }else{
 1.67864 +    /* The next rowid or record number (different terms for the same
 1.67865 +    ** thing) is obtained in a two-step algorithm.
 1.67866 +    **
 1.67867 +    ** First we attempt to find the largest existing rowid and add one
 1.67868 +    ** to that.  But if the largest existing rowid is already the maximum
 1.67869 +    ** positive integer, we have to fall through to the second
 1.67870 +    ** probabilistic algorithm
 1.67871 +    **
 1.67872 +    ** The second algorithm is to select a rowid at random and see if
 1.67873 +    ** it already exists in the table.  If it does not exist, we have
 1.67874 +    ** succeeded.  If the random rowid does exist, we select a new one
 1.67875 +    ** and try again, up to 100 times.
 1.67876 +    */
 1.67877 +    assert( u.bh.pC->isTable );
 1.67878 +
 1.67879 +#ifdef SQLITE_32BIT_ROWID
 1.67880 +#   define MAX_ROWID 0x7fffffff
 1.67881 +#else
 1.67882 +    /* Some compilers complain about constants of the form 0x7fffffffffffffff.
 1.67883 +    ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
 1.67884 +    ** to provide the constant while making all compilers happy.
 1.67885 +    */
 1.67886 +#   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
 1.67887 +#endif
 1.67888 +
 1.67889 +    if( !u.bh.pC->useRandomRowid ){
 1.67890 +      u.bh.v = sqlite3BtreeGetCachedRowid(u.bh.pC->pCursor);
 1.67891 +      if( u.bh.v==0 ){
 1.67892 +        rc = sqlite3BtreeLast(u.bh.pC->pCursor, &u.bh.res);
 1.67893 +        if( rc!=SQLITE_OK ){
 1.67894 +          goto abort_due_to_error;
 1.67895 +        }
 1.67896 +        if( u.bh.res ){
 1.67897 +          u.bh.v = 1;   /* IMP: R-61914-48074 */
 1.67898 +        }else{
 1.67899 +          assert( sqlite3BtreeCursorIsValid(u.bh.pC->pCursor) );
 1.67900 +          rc = sqlite3BtreeKeySize(u.bh.pC->pCursor, &u.bh.v);
 1.67901 +          assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
 1.67902 +          if( u.bh.v>=MAX_ROWID ){
 1.67903 +            u.bh.pC->useRandomRowid = 1;
 1.67904 +          }else{
 1.67905 +            u.bh.v++;   /* IMP: R-29538-34987 */
 1.67906 +          }
 1.67907 +        }
 1.67908 +      }
 1.67909 +
 1.67910 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.67911 +      if( pOp->p3 ){
 1.67912 +        /* Assert that P3 is a valid memory cell. */
 1.67913 +        assert( pOp->p3>0 );
 1.67914 +        if( p->pFrame ){
 1.67915 +          for(u.bh.pFrame=p->pFrame; u.bh.pFrame->pParent; u.bh.pFrame=u.bh.pFrame->pParent);
 1.67916 +          /* Assert that P3 is a valid memory cell. */
 1.67917 +          assert( pOp->p3<=u.bh.pFrame->nMem );
 1.67918 +          u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
 1.67919 +        }else{
 1.67920 +          /* Assert that P3 is a valid memory cell. */
 1.67921 +          assert( pOp->p3<=p->nMem );
 1.67922 +          u.bh.pMem = &aMem[pOp->p3];
 1.67923 +          memAboutToChange(p, u.bh.pMem);
 1.67924 +        }
 1.67925 +        assert( memIsValid(u.bh.pMem) );
 1.67926 +
 1.67927 +        REGISTER_TRACE(pOp->p3, u.bh.pMem);
 1.67928 +        sqlite3VdbeMemIntegerify(u.bh.pMem);
 1.67929 +        assert( (u.bh.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
 1.67930 +        if( u.bh.pMem->u.i==MAX_ROWID || u.bh.pC->useRandomRowid ){
 1.67931 +          rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
 1.67932 +          goto abort_due_to_error;
 1.67933 +        }
 1.67934 +        if( u.bh.v<u.bh.pMem->u.i+1 ){
 1.67935 +          u.bh.v = u.bh.pMem->u.i + 1;
 1.67936 +        }
 1.67937 +        u.bh.pMem->u.i = u.bh.v;
 1.67938 +      }
 1.67939 +#endif
 1.67940 +
 1.67941 +      sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, u.bh.v<MAX_ROWID ? u.bh.v+1 : 0);
 1.67942 +    }
 1.67943 +    if( u.bh.pC->useRandomRowid ){
 1.67944 +      /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
 1.67945 +      ** largest possible integer (9223372036854775807) then the database
 1.67946 +      ** engine starts picking positive candidate ROWIDs at random until
 1.67947 +      ** it finds one that is not previously used. */
 1.67948 +      assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
 1.67949 +                             ** an AUTOINCREMENT table. */
 1.67950 +      /* on the first attempt, simply do one more than previous */
 1.67951 +      u.bh.v = lastRowid;
 1.67952 +      u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 1.67953 +      u.bh.v++; /* ensure non-zero */
 1.67954 +      u.bh.cnt = 0;
 1.67955 +      while(   ((rc = sqlite3BtreeMovetoUnpacked(u.bh.pC->pCursor, 0, (u64)u.bh.v,
 1.67956 +                                                 0, &u.bh.res))==SQLITE_OK)
 1.67957 +            && (u.bh.res==0)
 1.67958 +            && (++u.bh.cnt<100)){
 1.67959 +        /* collision - try another random rowid */
 1.67960 +        sqlite3_randomness(sizeof(u.bh.v), &u.bh.v);
 1.67961 +        if( u.bh.cnt<5 ){
 1.67962 +          /* try "small" random rowids for the initial attempts */
 1.67963 +          u.bh.v &= 0xffffff;
 1.67964 +        }else{
 1.67965 +          u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 1.67966 +        }
 1.67967 +        u.bh.v++; /* ensure non-zero */
 1.67968 +      }
 1.67969 +      if( rc==SQLITE_OK && u.bh.res==0 ){
 1.67970 +        rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
 1.67971 +        goto abort_due_to_error;
 1.67972 +      }
 1.67973 +      assert( u.bh.v>0 );  /* EV: R-40812-03570 */
 1.67974 +    }
 1.67975 +    u.bh.pC->rowidIsValid = 0;
 1.67976 +    u.bh.pC->deferredMoveto = 0;
 1.67977 +    u.bh.pC->cacheStatus = CACHE_STALE;
 1.67978 +  }
 1.67979 +  pOut->u.i = u.bh.v;
 1.67980 +  break;
 1.67981 +}
 1.67982 +
 1.67983 +/* Opcode: Insert P1 P2 P3 P4 P5
 1.67984 +**
 1.67985 +** Write an entry into the table of cursor P1.  A new entry is
 1.67986 +** created if it doesn't already exist or the data for an existing
 1.67987 +** entry is overwritten.  The data is the value MEM_Blob stored in register
 1.67988 +** number P2. The key is stored in register P3. The key must
 1.67989 +** be a MEM_Int.
 1.67990 +**
 1.67991 +** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
 1.67992 +** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
 1.67993 +** then rowid is stored for subsequent return by the
 1.67994 +** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
 1.67995 +**
 1.67996 +** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
 1.67997 +** the last seek operation (OP_NotExists) was a success, then this
 1.67998 +** operation will not attempt to find the appropriate row before doing
 1.67999 +** the insert but will instead overwrite the row that the cursor is
 1.68000 +** currently pointing to.  Presumably, the prior OP_NotExists opcode
 1.68001 +** has already positioned the cursor correctly.  This is an optimization
 1.68002 +** that boosts performance by avoiding redundant seeks.
 1.68003 +**
 1.68004 +** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
 1.68005 +** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
 1.68006 +** is part of an INSERT operation.  The difference is only important to
 1.68007 +** the update hook.
 1.68008 +**
 1.68009 +** Parameter P4 may point to a string containing the table-name, or
 1.68010 +** may be NULL. If it is not NULL, then the update-hook 
 1.68011 +** (sqlite3.xUpdateCallback) is invoked following a successful insert.
 1.68012 +**
 1.68013 +** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
 1.68014 +** allocated, then ownership of P2 is transferred to the pseudo-cursor
 1.68015 +** and register P2 becomes ephemeral.  If the cursor is changed, the
 1.68016 +** value of register P2 will then change.  Make sure this does not
 1.68017 +** cause any problems.)
 1.68018 +**
 1.68019 +** This instruction only works on tables.  The equivalent instruction
 1.68020 +** for indices is OP_IdxInsert.
 1.68021 +*/
 1.68022 +/* Opcode: InsertInt P1 P2 P3 P4 P5
 1.68023 +**
 1.68024 +** This works exactly like OP_Insert except that the key is the
 1.68025 +** integer value P3, not the value of the integer stored in register P3.
 1.68026 +*/
 1.68027 +case OP_Insert: 
 1.68028 +case OP_InsertInt: {
 1.68029 +#if 0  /* local variables moved into u.bi */
 1.68030 +  Mem *pData;       /* MEM cell holding data for the record to be inserted */
 1.68031 +  Mem *pKey;        /* MEM cell holding key  for the record */
 1.68032 +  i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 1.68033 +  VdbeCursor *pC;   /* Cursor to table into which insert is written */
 1.68034 +  int nZero;        /* Number of zero-bytes to append */
 1.68035 +  int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 1.68036 +  const char *zDb;  /* database name - used by the update hook */
 1.68037 +  const char *zTbl; /* Table name - used by the opdate hook */
 1.68038 +  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 1.68039 +#endif /* local variables moved into u.bi */
 1.68040 +
 1.68041 +  u.bi.pData = &aMem[pOp->p2];
 1.68042 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68043 +  assert( memIsValid(u.bi.pData) );
 1.68044 +  u.bi.pC = p->apCsr[pOp->p1];
 1.68045 +  assert( u.bi.pC!=0 );
 1.68046 +  assert( u.bi.pC->pCursor!=0 );
 1.68047 +  assert( u.bi.pC->pseudoTableReg==0 );
 1.68048 +  assert( u.bi.pC->isTable );
 1.68049 +  REGISTER_TRACE(pOp->p2, u.bi.pData);
 1.68050 +
 1.68051 +  if( pOp->opcode==OP_Insert ){
 1.68052 +    u.bi.pKey = &aMem[pOp->p3];
 1.68053 +    assert( u.bi.pKey->flags & MEM_Int );
 1.68054 +    assert( memIsValid(u.bi.pKey) );
 1.68055 +    REGISTER_TRACE(pOp->p3, u.bi.pKey);
 1.68056 +    u.bi.iKey = u.bi.pKey->u.i;
 1.68057 +  }else{
 1.68058 +    assert( pOp->opcode==OP_InsertInt );
 1.68059 +    u.bi.iKey = pOp->p3;
 1.68060 +  }
 1.68061 +
 1.68062 +  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 1.68063 +  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bi.iKey;
 1.68064 +  if( u.bi.pData->flags & MEM_Null ){
 1.68065 +    u.bi.pData->z = 0;
 1.68066 +    u.bi.pData->n = 0;
 1.68067 +  }else{
 1.68068 +    assert( u.bi.pData->flags & (MEM_Blob|MEM_Str) );
 1.68069 +  }
 1.68070 +  u.bi.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bi.pC->seekResult : 0);
 1.68071 +  if( u.bi.pData->flags & MEM_Zero ){
 1.68072 +    u.bi.nZero = u.bi.pData->u.nZero;
 1.68073 +  }else{
 1.68074 +    u.bi.nZero = 0;
 1.68075 +  }
 1.68076 +  sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
 1.68077 +  rc = sqlite3BtreeInsert(u.bi.pC->pCursor, 0, u.bi.iKey,
 1.68078 +                          u.bi.pData->z, u.bi.pData->n, u.bi.nZero,
 1.68079 +                          pOp->p5 & OPFLAG_APPEND, u.bi.seekResult
 1.68080 +  );
 1.68081 +  u.bi.pC->rowidIsValid = 0;
 1.68082 +  u.bi.pC->deferredMoveto = 0;
 1.68083 +  u.bi.pC->cacheStatus = CACHE_STALE;
 1.68084 +
 1.68085 +  /* Invoke the update-hook if required. */
 1.68086 +  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
 1.68087 +    u.bi.zDb = db->aDb[u.bi.pC->iDb].zName;
 1.68088 +    u.bi.zTbl = pOp->p4.z;
 1.68089 +    u.bi.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 1.68090 +    assert( u.bi.pC->isTable );
 1.68091 +    db->xUpdateCallback(db->pUpdateArg, u.bi.op, u.bi.zDb, u.bi.zTbl, u.bi.iKey);
 1.68092 +    assert( u.bi.pC->iDb>=0 );
 1.68093 +  }
 1.68094 +  break;
 1.68095 +}
 1.68096 +
 1.68097 +/* Opcode: Delete P1 P2 * P4 *
 1.68098 +**
 1.68099 +** Delete the record at which the P1 cursor is currently pointing.
 1.68100 +**
 1.68101 +** The cursor will be left pointing at either the next or the previous
 1.68102 +** record in the table. If it is left pointing at the next record, then
 1.68103 +** the next Next instruction will be a no-op.  Hence it is OK to delete
 1.68104 +** a record from within an Next loop.
 1.68105 +**
 1.68106 +** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
 1.68107 +** incremented (otherwise not).
 1.68108 +**
 1.68109 +** P1 must not be pseudo-table.  It has to be a real table with
 1.68110 +** multiple rows.
 1.68111 +**
 1.68112 +** If P4 is not NULL, then it is the name of the table that P1 is
 1.68113 +** pointing to.  The update hook will be invoked, if it exists.
 1.68114 +** If P4 is not NULL then the P1 cursor must have been positioned
 1.68115 +** using OP_NotFound prior to invoking this opcode.
 1.68116 +*/
 1.68117 +case OP_Delete: {
 1.68118 +#if 0  /* local variables moved into u.bj */
 1.68119 +  i64 iKey;
 1.68120 +  VdbeCursor *pC;
 1.68121 +#endif /* local variables moved into u.bj */
 1.68122 +
 1.68123 +  u.bj.iKey = 0;
 1.68124 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68125 +  u.bj.pC = p->apCsr[pOp->p1];
 1.68126 +  assert( u.bj.pC!=0 );
 1.68127 +  assert( u.bj.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
 1.68128 +
 1.68129 +  /* If the update-hook will be invoked, set u.bj.iKey to the rowid of the
 1.68130 +  ** row being deleted.
 1.68131 +  */
 1.68132 +  if( db->xUpdateCallback && pOp->p4.z ){
 1.68133 +    assert( u.bj.pC->isTable );
 1.68134 +    assert( u.bj.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
 1.68135 +    u.bj.iKey = u.bj.pC->lastRowid;
 1.68136 +  }
 1.68137 +
 1.68138 +  /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
 1.68139 +  ** OP_Column on the same table without any intervening operations that
 1.68140 +  ** might move or invalidate the cursor.  Hence cursor u.bj.pC is always pointing
 1.68141 +  ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
 1.68142 +  ** below is always a no-op and cannot fail.  We will run it anyhow, though,
 1.68143 +  ** to guard against future changes to the code generator.
 1.68144 +  **/
 1.68145 +  assert( u.bj.pC->deferredMoveto==0 );
 1.68146 +  rc = sqlite3VdbeCursorMoveto(u.bj.pC);
 1.68147 +  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 1.68148 +
 1.68149 +  sqlite3BtreeSetCachedRowid(u.bj.pC->pCursor, 0);
 1.68150 +  rc = sqlite3BtreeDelete(u.bj.pC->pCursor);
 1.68151 +  u.bj.pC->cacheStatus = CACHE_STALE;
 1.68152 +
 1.68153 +  /* Invoke the update-hook if required. */
 1.68154 +  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
 1.68155 +    const char *zDb = db->aDb[u.bj.pC->iDb].zName;
 1.68156 +    const char *zTbl = pOp->p4.z;
 1.68157 +    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bj.iKey);
 1.68158 +    assert( u.bj.pC->iDb>=0 );
 1.68159 +  }
 1.68160 +  if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
 1.68161 +  break;
 1.68162 +}
 1.68163 +/* Opcode: ResetCount * * * * *
 1.68164 +**
 1.68165 +** The value of the change counter is copied to the database handle
 1.68166 +** change counter (returned by subsequent calls to sqlite3_changes()).
 1.68167 +** Then the VMs internal change counter resets to 0.
 1.68168 +** This is used by trigger programs.
 1.68169 +*/
 1.68170 +case OP_ResetCount: {
 1.68171 +  sqlite3VdbeSetChanges(db, p->nChange);
 1.68172 +  p->nChange = 0;
 1.68173 +  break;
 1.68174 +}
 1.68175 +
 1.68176 +/* Opcode: SorterCompare P1 P2 P3
 1.68177 +**
 1.68178 +** P1 is a sorter cursor. This instruction compares the record blob in 
 1.68179 +** register P3 with the entry that the sorter cursor currently points to.
 1.68180 +** If, excluding the rowid fields at the end, the two records are a match,
 1.68181 +** fall through to the next instruction. Otherwise, jump to instruction P2.
 1.68182 +*/
 1.68183 +case OP_SorterCompare: {
 1.68184 +#if 0  /* local variables moved into u.bk */
 1.68185 +  VdbeCursor *pC;
 1.68186 +  int res;
 1.68187 +#endif /* local variables moved into u.bk */
 1.68188 +
 1.68189 +  u.bk.pC = p->apCsr[pOp->p1];
 1.68190 +  assert( isSorter(u.bk.pC) );
 1.68191 +  pIn3 = &aMem[pOp->p3];
 1.68192 +  rc = sqlite3VdbeSorterCompare(u.bk.pC, pIn3, &u.bk.res);
 1.68193 +  if( u.bk.res ){
 1.68194 +    pc = pOp->p2-1;
 1.68195 +  }
 1.68196 +  break;
 1.68197 +};
 1.68198 +
 1.68199 +/* Opcode: SorterData P1 P2 * * *
 1.68200 +**
 1.68201 +** Write into register P2 the current sorter data for sorter cursor P1.
 1.68202 +*/
 1.68203 +case OP_SorterData: {
 1.68204 +#if 0  /* local variables moved into u.bl */
 1.68205 +  VdbeCursor *pC;
 1.68206 +#endif /* local variables moved into u.bl */
 1.68207 +
 1.68208 +#ifndef SQLITE_OMIT_MERGE_SORT
 1.68209 +  pOut = &aMem[pOp->p2];
 1.68210 +  u.bl.pC = p->apCsr[pOp->p1];
 1.68211 +  assert( u.bl.pC->isSorter );
 1.68212 +  rc = sqlite3VdbeSorterRowkey(u.bl.pC, pOut);
 1.68213 +#else
 1.68214 +  pOp->opcode = OP_RowKey;
 1.68215 +  pc--;
 1.68216 +#endif
 1.68217 +  break;
 1.68218 +}
 1.68219 +
 1.68220 +/* Opcode: RowData P1 P2 * * *
 1.68221 +**
 1.68222 +** Write into register P2 the complete row data for cursor P1.
 1.68223 +** There is no interpretation of the data.  
 1.68224 +** It is just copied onto the P2 register exactly as 
 1.68225 +** it is found in the database file.
 1.68226 +**
 1.68227 +** If the P1 cursor must be pointing to a valid row (not a NULL row)
 1.68228 +** of a real table, not a pseudo-table.
 1.68229 +*/
 1.68230 +/* Opcode: RowKey P1 P2 * * *
 1.68231 +**
 1.68232 +** Write into register P2 the complete row key for cursor P1.
 1.68233 +** There is no interpretation of the data.  
 1.68234 +** The key is copied onto the P3 register exactly as 
 1.68235 +** it is found in the database file.
 1.68236 +**
 1.68237 +** If the P1 cursor must be pointing to a valid row (not a NULL row)
 1.68238 +** of a real table, not a pseudo-table.
 1.68239 +*/
 1.68240 +case OP_RowKey:
 1.68241 +case OP_RowData: {
 1.68242 +#if 0  /* local variables moved into u.bm */
 1.68243 +  VdbeCursor *pC;
 1.68244 +  BtCursor *pCrsr;
 1.68245 +  u32 n;
 1.68246 +  i64 n64;
 1.68247 +#endif /* local variables moved into u.bm */
 1.68248 +
 1.68249 +  pOut = &aMem[pOp->p2];
 1.68250 +  memAboutToChange(p, pOut);
 1.68251 +
 1.68252 +  /* Note that RowKey and RowData are really exactly the same instruction */
 1.68253 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68254 +  u.bm.pC = p->apCsr[pOp->p1];
 1.68255 +  assert( u.bm.pC->isSorter==0 );
 1.68256 +  assert( u.bm.pC->isTable || pOp->opcode!=OP_RowData );
 1.68257 +  assert( u.bm.pC->isIndex || pOp->opcode==OP_RowData );
 1.68258 +  assert( u.bm.pC!=0 );
 1.68259 +  assert( u.bm.pC->nullRow==0 );
 1.68260 +  assert( u.bm.pC->pseudoTableReg==0 );
 1.68261 +  assert( u.bm.pC->pCursor!=0 );
 1.68262 +  u.bm.pCrsr = u.bm.pC->pCursor;
 1.68263 +  assert( sqlite3BtreeCursorIsValid(u.bm.pCrsr) );
 1.68264 +
 1.68265 +  /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
 1.68266 +  ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
 1.68267 +  ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
 1.68268 +  ** a no-op and can never fail.  But we leave it in place as a safety.
 1.68269 +  */
 1.68270 +  assert( u.bm.pC->deferredMoveto==0 );
 1.68271 +  rc = sqlite3VdbeCursorMoveto(u.bm.pC);
 1.68272 +  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 1.68273 +
 1.68274 +  if( u.bm.pC->isIndex ){
 1.68275 +    assert( !u.bm.pC->isTable );
 1.68276 +    VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bm.pCrsr, &u.bm.n64);
 1.68277 +    assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
 1.68278 +    if( u.bm.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.68279 +      goto too_big;
 1.68280 +    }
 1.68281 +    u.bm.n = (u32)u.bm.n64;
 1.68282 +  }else{
 1.68283 +    VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bm.pCrsr, &u.bm.n);
 1.68284 +    assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
 1.68285 +    if( u.bm.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.68286 +      goto too_big;
 1.68287 +    }
 1.68288 +  }
 1.68289 +  if( sqlite3VdbeMemGrow(pOut, u.bm.n, 0) ){
 1.68290 +    goto no_mem;
 1.68291 +  }
 1.68292 +  pOut->n = u.bm.n;
 1.68293 +  MemSetTypeFlag(pOut, MEM_Blob);
 1.68294 +  if( u.bm.pC->isIndex ){
 1.68295 +    rc = sqlite3BtreeKey(u.bm.pCrsr, 0, u.bm.n, pOut->z);
 1.68296 +  }else{
 1.68297 +    rc = sqlite3BtreeData(u.bm.pCrsr, 0, u.bm.n, pOut->z);
 1.68298 +  }
 1.68299 +  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
 1.68300 +  UPDATE_MAX_BLOBSIZE(pOut);
 1.68301 +  break;
 1.68302 +}
 1.68303 +
 1.68304 +/* Opcode: Rowid P1 P2 * * *
 1.68305 +**
 1.68306 +** Store in register P2 an integer which is the key of the table entry that
 1.68307 +** P1 is currently point to.
 1.68308 +**
 1.68309 +** P1 can be either an ordinary table or a virtual table.  There used to
 1.68310 +** be a separate OP_VRowid opcode for use with virtual tables, but this
 1.68311 +** one opcode now works for both table types.
 1.68312 +*/
 1.68313 +case OP_Rowid: {                 /* out2-prerelease */
 1.68314 +#if 0  /* local variables moved into u.bn */
 1.68315 +  VdbeCursor *pC;
 1.68316 +  i64 v;
 1.68317 +  sqlite3_vtab *pVtab;
 1.68318 +  const sqlite3_module *pModule;
 1.68319 +#endif /* local variables moved into u.bn */
 1.68320 +
 1.68321 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68322 +  u.bn.pC = p->apCsr[pOp->p1];
 1.68323 +  assert( u.bn.pC!=0 );
 1.68324 +  assert( u.bn.pC->pseudoTableReg==0 || u.bn.pC->nullRow );
 1.68325 +  if( u.bn.pC->nullRow ){
 1.68326 +    pOut->flags = MEM_Null;
 1.68327 +    break;
 1.68328 +  }else if( u.bn.pC->deferredMoveto ){
 1.68329 +    u.bn.v = u.bn.pC->movetoTarget;
 1.68330 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.68331 +  }else if( u.bn.pC->pVtabCursor ){
 1.68332 +    u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
 1.68333 +    u.bn.pModule = u.bn.pVtab->pModule;
 1.68334 +    assert( u.bn.pModule->xRowid );
 1.68335 +    rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
 1.68336 +    importVtabErrMsg(p, u.bn.pVtab);
 1.68337 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.68338 +  }else{
 1.68339 +    assert( u.bn.pC->pCursor!=0 );
 1.68340 +    rc = sqlite3VdbeCursorMoveto(u.bn.pC);
 1.68341 +    if( rc ) goto abort_due_to_error;
 1.68342 +    if( u.bn.pC->rowidIsValid ){
 1.68343 +      u.bn.v = u.bn.pC->lastRowid;
 1.68344 +    }else{
 1.68345 +      rc = sqlite3BtreeKeySize(u.bn.pC->pCursor, &u.bn.v);
 1.68346 +      assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
 1.68347 +    }
 1.68348 +  }
 1.68349 +  pOut->u.i = u.bn.v;
 1.68350 +  break;
 1.68351 +}
 1.68352 +
 1.68353 +/* Opcode: NullRow P1 * * * *
 1.68354 +**
 1.68355 +** Move the cursor P1 to a null row.  Any OP_Column operations
 1.68356 +** that occur while the cursor is on the null row will always
 1.68357 +** write a NULL.
 1.68358 +*/
 1.68359 +case OP_NullRow: {
 1.68360 +#if 0  /* local variables moved into u.bo */
 1.68361 +  VdbeCursor *pC;
 1.68362 +#endif /* local variables moved into u.bo */
 1.68363 +
 1.68364 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68365 +  u.bo.pC = p->apCsr[pOp->p1];
 1.68366 +  assert( u.bo.pC!=0 );
 1.68367 +  u.bo.pC->nullRow = 1;
 1.68368 +  u.bo.pC->rowidIsValid = 0;
 1.68369 +  assert( u.bo.pC->pCursor || u.bo.pC->pVtabCursor );
 1.68370 +  if( u.bo.pC->pCursor ){
 1.68371 +    sqlite3BtreeClearCursor(u.bo.pC->pCursor);
 1.68372 +  }
 1.68373 +  break;
 1.68374 +}
 1.68375 +
 1.68376 +/* Opcode: Last P1 P2 * * *
 1.68377 +**
 1.68378 +** The next use of the Rowid or Column or Next instruction for P1 
 1.68379 +** will refer to the last entry in the database table or index.
 1.68380 +** If the table or index is empty and P2>0, then jump immediately to P2.
 1.68381 +** If P2 is 0 or if the table or index is not empty, fall through
 1.68382 +** to the following instruction.
 1.68383 +*/
 1.68384 +case OP_Last: {        /* jump */
 1.68385 +#if 0  /* local variables moved into u.bp */
 1.68386 +  VdbeCursor *pC;
 1.68387 +  BtCursor *pCrsr;
 1.68388 +  int res;
 1.68389 +#endif /* local variables moved into u.bp */
 1.68390 +
 1.68391 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68392 +  u.bp.pC = p->apCsr[pOp->p1];
 1.68393 +  assert( u.bp.pC!=0 );
 1.68394 +  u.bp.pCrsr = u.bp.pC->pCursor;
 1.68395 +  u.bp.res = 0;
 1.68396 +  if( ALWAYS(u.bp.pCrsr!=0) ){
 1.68397 +    rc = sqlite3BtreeLast(u.bp.pCrsr, &u.bp.res);
 1.68398 +  }
 1.68399 +  u.bp.pC->nullRow = (u8)u.bp.res;
 1.68400 +  u.bp.pC->deferredMoveto = 0;
 1.68401 +  u.bp.pC->rowidIsValid = 0;
 1.68402 +  u.bp.pC->cacheStatus = CACHE_STALE;
 1.68403 +  if( pOp->p2>0 && u.bp.res ){
 1.68404 +    pc = pOp->p2 - 1;
 1.68405 +  }
 1.68406 +  break;
 1.68407 +}
 1.68408 +
 1.68409 +
 1.68410 +/* Opcode: Sort P1 P2 * * *
 1.68411 +**
 1.68412 +** This opcode does exactly the same thing as OP_Rewind except that
 1.68413 +** it increments an undocumented global variable used for testing.
 1.68414 +**
 1.68415 +** Sorting is accomplished by writing records into a sorting index,
 1.68416 +** then rewinding that index and playing it back from beginning to
 1.68417 +** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
 1.68418 +** rewinding so that the global variable will be incremented and
 1.68419 +** regression tests can determine whether or not the optimizer is
 1.68420 +** correctly optimizing out sorts.
 1.68421 +*/
 1.68422 +case OP_SorterSort:    /* jump */
 1.68423 +#ifdef SQLITE_OMIT_MERGE_SORT
 1.68424 +  pOp->opcode = OP_Sort;
 1.68425 +#endif
 1.68426 +case OP_Sort: {        /* jump */
 1.68427 +#ifdef SQLITE_TEST
 1.68428 +  sqlite3_sort_count++;
 1.68429 +  sqlite3_search_count--;
 1.68430 +#endif
 1.68431 +  p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
 1.68432 +  /* Fall through into OP_Rewind */
 1.68433 +}
 1.68434 +/* Opcode: Rewind P1 P2 * * *
 1.68435 +**
 1.68436 +** The next use of the Rowid or Column or Next instruction for P1 
 1.68437 +** will refer to the first entry in the database table or index.
 1.68438 +** If the table or index is empty and P2>0, then jump immediately to P2.
 1.68439 +** If P2 is 0 or if the table or index is not empty, fall through
 1.68440 +** to the following instruction.
 1.68441 +*/
 1.68442 +case OP_Rewind: {        /* jump */
 1.68443 +#if 0  /* local variables moved into u.bq */
 1.68444 +  VdbeCursor *pC;
 1.68445 +  BtCursor *pCrsr;
 1.68446 +  int res;
 1.68447 +#endif /* local variables moved into u.bq */
 1.68448 +
 1.68449 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68450 +  u.bq.pC = p->apCsr[pOp->p1];
 1.68451 +  assert( u.bq.pC!=0 );
 1.68452 +  assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterSort) );
 1.68453 +  u.bq.res = 1;
 1.68454 +  if( isSorter(u.bq.pC) ){
 1.68455 +    rc = sqlite3VdbeSorterRewind(db, u.bq.pC, &u.bq.res);
 1.68456 +  }else{
 1.68457 +    u.bq.pCrsr = u.bq.pC->pCursor;
 1.68458 +    assert( u.bq.pCrsr );
 1.68459 +    rc = sqlite3BtreeFirst(u.bq.pCrsr, &u.bq.res);
 1.68460 +    u.bq.pC->atFirst = u.bq.res==0 ?1:0;
 1.68461 +    u.bq.pC->deferredMoveto = 0;
 1.68462 +    u.bq.pC->cacheStatus = CACHE_STALE;
 1.68463 +    u.bq.pC->rowidIsValid = 0;
 1.68464 +  }
 1.68465 +  u.bq.pC->nullRow = (u8)u.bq.res;
 1.68466 +  assert( pOp->p2>0 && pOp->p2<p->nOp );
 1.68467 +  if( u.bq.res ){
 1.68468 +    pc = pOp->p2 - 1;
 1.68469 +  }
 1.68470 +  break;
 1.68471 +}
 1.68472 +
 1.68473 +/* Opcode: Next P1 P2 * P4 P5
 1.68474 +**
 1.68475 +** Advance cursor P1 so that it points to the next key/data pair in its
 1.68476 +** table or index.  If there are no more key/value pairs then fall through
 1.68477 +** to the following instruction.  But if the cursor advance was successful,
 1.68478 +** jump immediately to P2.
 1.68479 +**
 1.68480 +** The P1 cursor must be for a real table, not a pseudo-table.
 1.68481 +**
 1.68482 +** P4 is always of type P4_ADVANCE. The function pointer points to
 1.68483 +** sqlite3BtreeNext().
 1.68484 +**
 1.68485 +** If P5 is positive and the jump is taken, then event counter
 1.68486 +** number P5-1 in the prepared statement is incremented.
 1.68487 +**
 1.68488 +** See also: Prev
 1.68489 +*/
 1.68490 +/* Opcode: Prev P1 P2 * * P5
 1.68491 +**
 1.68492 +** Back up cursor P1 so that it points to the previous key/data pair in its
 1.68493 +** table or index.  If there is no previous key/value pairs then fall through
 1.68494 +** to the following instruction.  But if the cursor backup was successful,
 1.68495 +** jump immediately to P2.
 1.68496 +**
 1.68497 +** The P1 cursor must be for a real table, not a pseudo-table.
 1.68498 +**
 1.68499 +** P4 is always of type P4_ADVANCE. The function pointer points to
 1.68500 +** sqlite3BtreePrevious().
 1.68501 +**
 1.68502 +** If P5 is positive and the jump is taken, then event counter
 1.68503 +** number P5-1 in the prepared statement is incremented.
 1.68504 +*/
 1.68505 +case OP_SorterNext:    /* jump */
 1.68506 +#ifdef SQLITE_OMIT_MERGE_SORT
 1.68507 +  pOp->opcode = OP_Next;
 1.68508 +#endif
 1.68509 +case OP_Prev:          /* jump */
 1.68510 +case OP_Next: {        /* jump */
 1.68511 +#if 0  /* local variables moved into u.br */
 1.68512 +  VdbeCursor *pC;
 1.68513 +  int res;
 1.68514 +#endif /* local variables moved into u.br */
 1.68515 +
 1.68516 +  CHECK_FOR_INTERRUPT;
 1.68517 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68518 +  assert( pOp->p5<=ArraySize(p->aCounter) );
 1.68519 +  u.br.pC = p->apCsr[pOp->p1];
 1.68520 +  if( u.br.pC==0 ){
 1.68521 +    break;  /* See ticket #2273 */
 1.68522 +  }
 1.68523 +  assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
 1.68524 +  if( isSorter(u.br.pC) ){
 1.68525 +    assert( pOp->opcode==OP_SorterNext );
 1.68526 +    rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
 1.68527 +  }else{
 1.68528 +    u.br.res = 1;
 1.68529 +    assert( u.br.pC->deferredMoveto==0 );
 1.68530 +    assert( u.br.pC->pCursor );
 1.68531 +    assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
 1.68532 +    assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
 1.68533 +    rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
 1.68534 +  }
 1.68535 +  u.br.pC->nullRow = (u8)u.br.res;
 1.68536 +  u.br.pC->cacheStatus = CACHE_STALE;
 1.68537 +  if( u.br.res==0 ){
 1.68538 +    pc = pOp->p2 - 1;
 1.68539 +    if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
 1.68540 +#ifdef SQLITE_TEST
 1.68541 +    sqlite3_search_count++;
 1.68542 +#endif
 1.68543 +  }
 1.68544 +  u.br.pC->rowidIsValid = 0;
 1.68545 +  break;
 1.68546 +}
 1.68547 +
 1.68548 +/* Opcode: IdxInsert P1 P2 P3 * P5
 1.68549 +**
 1.68550 +** Register P2 holds an SQL index key made using the
 1.68551 +** MakeRecord instructions.  This opcode writes that key
 1.68552 +** into the index P1.  Data for the entry is nil.
 1.68553 +**
 1.68554 +** P3 is a flag that provides a hint to the b-tree layer that this
 1.68555 +** insert is likely to be an append.
 1.68556 +**
 1.68557 +** This instruction only works for indices.  The equivalent instruction
 1.68558 +** for tables is OP_Insert.
 1.68559 +*/
 1.68560 +case OP_SorterInsert:       /* in2 */
 1.68561 +#ifdef SQLITE_OMIT_MERGE_SORT
 1.68562 +  pOp->opcode = OP_IdxInsert;
 1.68563 +#endif
 1.68564 +case OP_IdxInsert: {        /* in2 */
 1.68565 +#if 0  /* local variables moved into u.bs */
 1.68566 +  VdbeCursor *pC;
 1.68567 +  BtCursor *pCrsr;
 1.68568 +  int nKey;
 1.68569 +  const char *zKey;
 1.68570 +#endif /* local variables moved into u.bs */
 1.68571 +
 1.68572 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68573 +  u.bs.pC = p->apCsr[pOp->p1];
 1.68574 +  assert( u.bs.pC!=0 );
 1.68575 +  assert( u.bs.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
 1.68576 +  pIn2 = &aMem[pOp->p2];
 1.68577 +  assert( pIn2->flags & MEM_Blob );
 1.68578 +  u.bs.pCrsr = u.bs.pC->pCursor;
 1.68579 +  if( ALWAYS(u.bs.pCrsr!=0) ){
 1.68580 +    assert( u.bs.pC->isTable==0 );
 1.68581 +    rc = ExpandBlob(pIn2);
 1.68582 +    if( rc==SQLITE_OK ){
 1.68583 +      if( isSorter(u.bs.pC) ){
 1.68584 +        rc = sqlite3VdbeSorterWrite(db, u.bs.pC, pIn2);
 1.68585 +      }else{
 1.68586 +        u.bs.nKey = pIn2->n;
 1.68587 +        u.bs.zKey = pIn2->z;
 1.68588 +        rc = sqlite3BtreeInsert(u.bs.pCrsr, u.bs.zKey, u.bs.nKey, "", 0, 0, pOp->p3,
 1.68589 +            ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bs.pC->seekResult : 0)
 1.68590 +            );
 1.68591 +        assert( u.bs.pC->deferredMoveto==0 );
 1.68592 +        u.bs.pC->cacheStatus = CACHE_STALE;
 1.68593 +      }
 1.68594 +    }
 1.68595 +  }
 1.68596 +  break;
 1.68597 +}
 1.68598 +
 1.68599 +/* Opcode: IdxDelete P1 P2 P3 * *
 1.68600 +**
 1.68601 +** The content of P3 registers starting at register P2 form
 1.68602 +** an unpacked index key. This opcode removes that entry from the 
 1.68603 +** index opened by cursor P1.
 1.68604 +*/
 1.68605 +case OP_IdxDelete: {
 1.68606 +#if 0  /* local variables moved into u.bt */
 1.68607 +  VdbeCursor *pC;
 1.68608 +  BtCursor *pCrsr;
 1.68609 +  int res;
 1.68610 +  UnpackedRecord r;
 1.68611 +#endif /* local variables moved into u.bt */
 1.68612 +
 1.68613 +  assert( pOp->p3>0 );
 1.68614 +  assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
 1.68615 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68616 +  u.bt.pC = p->apCsr[pOp->p1];
 1.68617 +  assert( u.bt.pC!=0 );
 1.68618 +  u.bt.pCrsr = u.bt.pC->pCursor;
 1.68619 +  if( ALWAYS(u.bt.pCrsr!=0) ){
 1.68620 +    u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
 1.68621 +    u.bt.r.nField = (u16)pOp->p3;
 1.68622 +    u.bt.r.flags = 0;
 1.68623 +    u.bt.r.aMem = &aMem[pOp->p2];
 1.68624 +#ifdef SQLITE_DEBUG
 1.68625 +    { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
 1.68626 +#endif
 1.68627 +    rc = sqlite3BtreeMovetoUnpacked(u.bt.pCrsr, &u.bt.r, 0, 0, &u.bt.res);
 1.68628 +    if( rc==SQLITE_OK && u.bt.res==0 ){
 1.68629 +      rc = sqlite3BtreeDelete(u.bt.pCrsr);
 1.68630 +    }
 1.68631 +    assert( u.bt.pC->deferredMoveto==0 );
 1.68632 +    u.bt.pC->cacheStatus = CACHE_STALE;
 1.68633 +  }
 1.68634 +  break;
 1.68635 +}
 1.68636 +
 1.68637 +/* Opcode: IdxRowid P1 P2 * * *
 1.68638 +**
 1.68639 +** Write into register P2 an integer which is the last entry in the record at
 1.68640 +** the end of the index key pointed to by cursor P1.  This integer should be
 1.68641 +** the rowid of the table entry to which this index entry points.
 1.68642 +**
 1.68643 +** See also: Rowid, MakeRecord.
 1.68644 +*/
 1.68645 +case OP_IdxRowid: {              /* out2-prerelease */
 1.68646 +#if 0  /* local variables moved into u.bu */
 1.68647 +  BtCursor *pCrsr;
 1.68648 +  VdbeCursor *pC;
 1.68649 +  i64 rowid;
 1.68650 +#endif /* local variables moved into u.bu */
 1.68651 +
 1.68652 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68653 +  u.bu.pC = p->apCsr[pOp->p1];
 1.68654 +  assert( u.bu.pC!=0 );
 1.68655 +  u.bu.pCrsr = u.bu.pC->pCursor;
 1.68656 +  pOut->flags = MEM_Null;
 1.68657 +  if( ALWAYS(u.bu.pCrsr!=0) ){
 1.68658 +    rc = sqlite3VdbeCursorMoveto(u.bu.pC);
 1.68659 +    if( NEVER(rc) ) goto abort_due_to_error;
 1.68660 +    assert( u.bu.pC->deferredMoveto==0 );
 1.68661 +    assert( u.bu.pC->isTable==0 );
 1.68662 +    if( !u.bu.pC->nullRow ){
 1.68663 +      rc = sqlite3VdbeIdxRowid(db, u.bu.pCrsr, &u.bu.rowid);
 1.68664 +      if( rc!=SQLITE_OK ){
 1.68665 +        goto abort_due_to_error;
 1.68666 +      }
 1.68667 +      pOut->u.i = u.bu.rowid;
 1.68668 +      pOut->flags = MEM_Int;
 1.68669 +    }
 1.68670 +  }
 1.68671 +  break;
 1.68672 +}
 1.68673 +
 1.68674 +/* Opcode: IdxGE P1 P2 P3 P4 P5
 1.68675 +**
 1.68676 +** The P4 register values beginning with P3 form an unpacked index 
 1.68677 +** key that omits the ROWID.  Compare this key value against the index 
 1.68678 +** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
 1.68679 +**
 1.68680 +** If the P1 index entry is greater than or equal to the key value
 1.68681 +** then jump to P2.  Otherwise fall through to the next instruction.
 1.68682 +**
 1.68683 +** If P5 is non-zero then the key value is increased by an epsilon 
 1.68684 +** prior to the comparison.  This make the opcode work like IdxGT except
 1.68685 +** that if the key from register P3 is a prefix of the key in the cursor,
 1.68686 +** the result is false whereas it would be true with IdxGT.
 1.68687 +*/
 1.68688 +/* Opcode: IdxLT P1 P2 P3 P4 P5
 1.68689 +**
 1.68690 +** The P4 register values beginning with P3 form an unpacked index 
 1.68691 +** key that omits the ROWID.  Compare this key value against the index 
 1.68692 +** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
 1.68693 +**
 1.68694 +** If the P1 index entry is less than the key value then jump to P2.
 1.68695 +** Otherwise fall through to the next instruction.
 1.68696 +**
 1.68697 +** If P5 is non-zero then the key value is increased by an epsilon prior 
 1.68698 +** to the comparison.  This makes the opcode work like IdxLE.
 1.68699 +*/
 1.68700 +case OP_IdxLT:          /* jump */
 1.68701 +case OP_IdxGE: {        /* jump */
 1.68702 +#if 0  /* local variables moved into u.bv */
 1.68703 +  VdbeCursor *pC;
 1.68704 +  int res;
 1.68705 +  UnpackedRecord r;
 1.68706 +#endif /* local variables moved into u.bv */
 1.68707 +
 1.68708 +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 1.68709 +  u.bv.pC = p->apCsr[pOp->p1];
 1.68710 +  assert( u.bv.pC!=0 );
 1.68711 +  assert( u.bv.pC->isOrdered );
 1.68712 +  if( ALWAYS(u.bv.pC->pCursor!=0) ){
 1.68713 +    assert( u.bv.pC->deferredMoveto==0 );
 1.68714 +    assert( pOp->p5==0 || pOp->p5==1 );
 1.68715 +    assert( pOp->p4type==P4_INT32 );
 1.68716 +    u.bv.r.pKeyInfo = u.bv.pC->pKeyInfo;
 1.68717 +    u.bv.r.nField = (u16)pOp->p4.i;
 1.68718 +    if( pOp->p5 ){
 1.68719 +      u.bv.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
 1.68720 +    }else{
 1.68721 +      u.bv.r.flags = UNPACKED_PREFIX_MATCH;
 1.68722 +    }
 1.68723 +    u.bv.r.aMem = &aMem[pOp->p3];
 1.68724 +#ifdef SQLITE_DEBUG
 1.68725 +    { int i; for(i=0; i<u.bv.r.nField; i++) assert( memIsValid(&u.bv.r.aMem[i]) ); }
 1.68726 +#endif
 1.68727 +    rc = sqlite3VdbeIdxKeyCompare(u.bv.pC, &u.bv.r, &u.bv.res);
 1.68728 +    if( pOp->opcode==OP_IdxLT ){
 1.68729 +      u.bv.res = -u.bv.res;
 1.68730 +    }else{
 1.68731 +      assert( pOp->opcode==OP_IdxGE );
 1.68732 +      u.bv.res++;
 1.68733 +    }
 1.68734 +    if( u.bv.res>0 ){
 1.68735 +      pc = pOp->p2 - 1 ;
 1.68736 +    }
 1.68737 +  }
 1.68738 +  break;
 1.68739 +}
 1.68740 +
 1.68741 +/* Opcode: Destroy P1 P2 P3 * *
 1.68742 +**
 1.68743 +** Delete an entire database table or index whose root page in the database
 1.68744 +** file is given by P1.
 1.68745 +**
 1.68746 +** The table being destroyed is in the main database file if P3==0.  If
 1.68747 +** P3==1 then the table to be clear is in the auxiliary database file
 1.68748 +** that is used to store tables create using CREATE TEMPORARY TABLE.
 1.68749 +**
 1.68750 +** If AUTOVACUUM is enabled then it is possible that another root page
 1.68751 +** might be moved into the newly deleted root page in order to keep all
 1.68752 +** root pages contiguous at the beginning of the database.  The former
 1.68753 +** value of the root page that moved - its value before the move occurred -
 1.68754 +** is stored in register P2.  If no page 
 1.68755 +** movement was required (because the table being dropped was already 
 1.68756 +** the last one in the database) then a zero is stored in register P2.
 1.68757 +** If AUTOVACUUM is disabled then a zero is stored in register P2.
 1.68758 +**
 1.68759 +** See also: Clear
 1.68760 +*/
 1.68761 +case OP_Destroy: {     /* out2-prerelease */
 1.68762 +#if 0  /* local variables moved into u.bw */
 1.68763 +  int iMoved;
 1.68764 +  int iCnt;
 1.68765 +  Vdbe *pVdbe;
 1.68766 +  int iDb;
 1.68767 +#endif /* local variables moved into u.bw */
 1.68768 +
 1.68769 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.68770 +  u.bw.iCnt = 0;
 1.68771 +  for(u.bw.pVdbe=db->pVdbe; u.bw.pVdbe; u.bw.pVdbe = u.bw.pVdbe->pNext){
 1.68772 +    if( u.bw.pVdbe->magic==VDBE_MAGIC_RUN && u.bw.pVdbe->inVtabMethod<2 && u.bw.pVdbe->pc>=0 ){
 1.68773 +      u.bw.iCnt++;
 1.68774 +    }
 1.68775 +  }
 1.68776 +#else
 1.68777 +  u.bw.iCnt = db->activeVdbeCnt;
 1.68778 +#endif
 1.68779 +  pOut->flags = MEM_Null;
 1.68780 +  if( u.bw.iCnt>1 ){
 1.68781 +    rc = SQLITE_LOCKED;
 1.68782 +    p->errorAction = OE_Abort;
 1.68783 +  }else{
 1.68784 +    u.bw.iDb = pOp->p3;
 1.68785 +    assert( u.bw.iCnt==1 );
 1.68786 +    assert( (p->btreeMask & (((yDbMask)1)<<u.bw.iDb))!=0 );
 1.68787 +    rc = sqlite3BtreeDropTable(db->aDb[u.bw.iDb].pBt, pOp->p1, &u.bw.iMoved);
 1.68788 +    pOut->flags = MEM_Int;
 1.68789 +    pOut->u.i = u.bw.iMoved;
 1.68790 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.68791 +    if( rc==SQLITE_OK && u.bw.iMoved!=0 ){
 1.68792 +      sqlite3RootPageMoved(db, u.bw.iDb, u.bw.iMoved, pOp->p1);
 1.68793 +      /* All OP_Destroy operations occur on the same btree */
 1.68794 +      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bw.iDb+1 );
 1.68795 +      resetSchemaOnFault = u.bw.iDb+1;
 1.68796 +    }
 1.68797 +#endif
 1.68798 +  }
 1.68799 +  break;
 1.68800 +}
 1.68801 +
 1.68802 +/* Opcode: Clear P1 P2 P3
 1.68803 +**
 1.68804 +** Delete all contents of the database table or index whose root page
 1.68805 +** in the database file is given by P1.  But, unlike Destroy, do not
 1.68806 +** remove the table or index from the database file.
 1.68807 +**
 1.68808 +** The table being clear is in the main database file if P2==0.  If
 1.68809 +** P2==1 then the table to be clear is in the auxiliary database file
 1.68810 +** that is used to store tables create using CREATE TEMPORARY TABLE.
 1.68811 +**
 1.68812 +** If the P3 value is non-zero, then the table referred to must be an
 1.68813 +** intkey table (an SQL table, not an index). In this case the row change 
 1.68814 +** count is incremented by the number of rows in the table being cleared. 
 1.68815 +** If P3 is greater than zero, then the value stored in register P3 is
 1.68816 +** also incremented by the number of rows in the table being cleared.
 1.68817 +**
 1.68818 +** See also: Destroy
 1.68819 +*/
 1.68820 +case OP_Clear: {
 1.68821 +#if 0  /* local variables moved into u.bx */
 1.68822 +  int nChange;
 1.68823 +#endif /* local variables moved into u.bx */
 1.68824 +
 1.68825 +  u.bx.nChange = 0;
 1.68826 +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
 1.68827 +  rc = sqlite3BtreeClearTable(
 1.68828 +      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
 1.68829 +  );
 1.68830 +  if( pOp->p3 ){
 1.68831 +    p->nChange += u.bx.nChange;
 1.68832 +    if( pOp->p3>0 ){
 1.68833 +      assert( memIsValid(&aMem[pOp->p3]) );
 1.68834 +      memAboutToChange(p, &aMem[pOp->p3]);
 1.68835 +      aMem[pOp->p3].u.i += u.bx.nChange;
 1.68836 +    }
 1.68837 +  }
 1.68838 +  break;
 1.68839 +}
 1.68840 +
 1.68841 +/* Opcode: CreateTable P1 P2 * * *
 1.68842 +**
 1.68843 +** Allocate a new table in the main database file if P1==0 or in the
 1.68844 +** auxiliary database file if P1==1 or in an attached database if
 1.68845 +** P1>1.  Write the root page number of the new table into
 1.68846 +** register P2
 1.68847 +**
 1.68848 +** The difference between a table and an index is this:  A table must
 1.68849 +** have a 4-byte integer key and can have arbitrary data.  An index
 1.68850 +** has an arbitrary key but no data.
 1.68851 +**
 1.68852 +** See also: CreateIndex
 1.68853 +*/
 1.68854 +/* Opcode: CreateIndex P1 P2 * * *
 1.68855 +**
 1.68856 +** Allocate a new index in the main database file if P1==0 or in the
 1.68857 +** auxiliary database file if P1==1 or in an attached database if
 1.68858 +** P1>1.  Write the root page number of the new table into
 1.68859 +** register P2.
 1.68860 +**
 1.68861 +** See documentation on OP_CreateTable for additional information.
 1.68862 +*/
 1.68863 +case OP_CreateIndex:            /* out2-prerelease */
 1.68864 +case OP_CreateTable: {          /* out2-prerelease */
 1.68865 +#if 0  /* local variables moved into u.by */
 1.68866 +  int pgno;
 1.68867 +  int flags;
 1.68868 +  Db *pDb;
 1.68869 +#endif /* local variables moved into u.by */
 1.68870 +
 1.68871 +  u.by.pgno = 0;
 1.68872 +  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.68873 +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 1.68874 +  u.by.pDb = &db->aDb[pOp->p1];
 1.68875 +  assert( u.by.pDb->pBt!=0 );
 1.68876 +  if( pOp->opcode==OP_CreateTable ){
 1.68877 +    /* u.by.flags = BTREE_INTKEY; */
 1.68878 +    u.by.flags = BTREE_INTKEY;
 1.68879 +  }else{
 1.68880 +    u.by.flags = BTREE_BLOBKEY;
 1.68881 +  }
 1.68882 +  rc = sqlite3BtreeCreateTable(u.by.pDb->pBt, &u.by.pgno, u.by.flags);
 1.68883 +  pOut->u.i = u.by.pgno;
 1.68884 +  break;
 1.68885 +}
 1.68886 +
 1.68887 +/* Opcode: ParseSchema P1 * * P4 *
 1.68888 +**
 1.68889 +** Read and parse all entries from the SQLITE_MASTER table of database P1
 1.68890 +** that match the WHERE clause P4. 
 1.68891 +**
 1.68892 +** This opcode invokes the parser to create a new virtual machine,
 1.68893 +** then runs the new virtual machine.  It is thus a re-entrant opcode.
 1.68894 +*/
 1.68895 +case OP_ParseSchema: {
 1.68896 +#if 0  /* local variables moved into u.bz */
 1.68897 +  int iDb;
 1.68898 +  const char *zMaster;
 1.68899 +  char *zSql;
 1.68900 +  InitData initData;
 1.68901 +#endif /* local variables moved into u.bz */
 1.68902 +
 1.68903 +  /* Any prepared statement that invokes this opcode will hold mutexes
 1.68904 +  ** on every btree.  This is a prerequisite for invoking
 1.68905 +  ** sqlite3InitCallback().
 1.68906 +  */
 1.68907 +#ifdef SQLITE_DEBUG
 1.68908 +  for(u.bz.iDb=0; u.bz.iDb<db->nDb; u.bz.iDb++){
 1.68909 +    assert( u.bz.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bz.iDb].pBt) );
 1.68910 +  }
 1.68911 +#endif
 1.68912 +
 1.68913 +  u.bz.iDb = pOp->p1;
 1.68914 +  assert( u.bz.iDb>=0 && u.bz.iDb<db->nDb );
 1.68915 +  assert( DbHasProperty(db, u.bz.iDb, DB_SchemaLoaded) );
 1.68916 +  /* Used to be a conditional */ {
 1.68917 +    u.bz.zMaster = SCHEMA_TABLE(u.bz.iDb);
 1.68918 +    u.bz.initData.db = db;
 1.68919 +    u.bz.initData.iDb = pOp->p1;
 1.68920 +    u.bz.initData.pzErrMsg = &p->zErrMsg;
 1.68921 +    u.bz.zSql = sqlite3MPrintf(db,
 1.68922 +       "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 1.68923 +       db->aDb[u.bz.iDb].zName, u.bz.zMaster, pOp->p4.z);
 1.68924 +    if( u.bz.zSql==0 ){
 1.68925 +      rc = SQLITE_NOMEM;
 1.68926 +    }else{
 1.68927 +      assert( db->init.busy==0 );
 1.68928 +      db->init.busy = 1;
 1.68929 +      u.bz.initData.rc = SQLITE_OK;
 1.68930 +      assert( !db->mallocFailed );
 1.68931 +      rc = sqlite3_exec(db, u.bz.zSql, sqlite3InitCallback, &u.bz.initData, 0);
 1.68932 +      if( rc==SQLITE_OK ) rc = u.bz.initData.rc;
 1.68933 +      sqlite3DbFree(db, u.bz.zSql);
 1.68934 +      db->init.busy = 0;
 1.68935 +    }
 1.68936 +  }
 1.68937 +  if( rc ) sqlite3ResetAllSchemasOfConnection(db);
 1.68938 +  if( rc==SQLITE_NOMEM ){
 1.68939 +    goto no_mem;
 1.68940 +  }
 1.68941 +  break;
 1.68942 +}
 1.68943 +
 1.68944 +#if !defined(SQLITE_OMIT_ANALYZE)
 1.68945 +/* Opcode: LoadAnalysis P1 * * * *
 1.68946 +**
 1.68947 +** Read the sqlite_stat1 table for database P1 and load the content
 1.68948 +** of that table into the internal index hash table.  This will cause
 1.68949 +** the analysis to be used when preparing all subsequent queries.
 1.68950 +*/
 1.68951 +case OP_LoadAnalysis: {
 1.68952 +  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.68953 +  rc = sqlite3AnalysisLoad(db, pOp->p1);
 1.68954 +  break;  
 1.68955 +}
 1.68956 +#endif /* !defined(SQLITE_OMIT_ANALYZE) */
 1.68957 +
 1.68958 +/* Opcode: DropTable P1 * * P4 *
 1.68959 +**
 1.68960 +** Remove the internal (in-memory) data structures that describe
 1.68961 +** the table named P4 in database P1.  This is called after a table
 1.68962 +** is dropped in order to keep the internal representation of the
 1.68963 +** schema consistent with what is on disk.
 1.68964 +*/
 1.68965 +case OP_DropTable: {
 1.68966 +  sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
 1.68967 +  break;
 1.68968 +}
 1.68969 +
 1.68970 +/* Opcode: DropIndex P1 * * P4 *
 1.68971 +**
 1.68972 +** Remove the internal (in-memory) data structures that describe
 1.68973 +** the index named P4 in database P1.  This is called after an index
 1.68974 +** is dropped in order to keep the internal representation of the
 1.68975 +** schema consistent with what is on disk.
 1.68976 +*/
 1.68977 +case OP_DropIndex: {
 1.68978 +  sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
 1.68979 +  break;
 1.68980 +}
 1.68981 +
 1.68982 +/* Opcode: DropTrigger P1 * * P4 *
 1.68983 +**
 1.68984 +** Remove the internal (in-memory) data structures that describe
 1.68985 +** the trigger named P4 in database P1.  This is called after a trigger
 1.68986 +** is dropped in order to keep the internal representation of the
 1.68987 +** schema consistent with what is on disk.
 1.68988 +*/
 1.68989 +case OP_DropTrigger: {
 1.68990 +  sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
 1.68991 +  break;
 1.68992 +}
 1.68993 +
 1.68994 +
 1.68995 +#ifndef SQLITE_OMIT_INTEGRITY_CHECK
 1.68996 +/* Opcode: IntegrityCk P1 P2 P3 * P5
 1.68997 +**
 1.68998 +** Do an analysis of the currently open database.  Store in
 1.68999 +** register P1 the text of an error message describing any problems.
 1.69000 +** If no problems are found, store a NULL in register P1.
 1.69001 +**
 1.69002 +** The register P3 contains the maximum number of allowed errors.
 1.69003 +** At most reg(P3) errors will be reported.
 1.69004 +** In other words, the analysis stops as soon as reg(P1) errors are 
 1.69005 +** seen.  Reg(P1) is updated with the number of errors remaining.
 1.69006 +**
 1.69007 +** The root page numbers of all tables in the database are integer
 1.69008 +** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
 1.69009 +** total.
 1.69010 +**
 1.69011 +** If P5 is not zero, the check is done on the auxiliary database
 1.69012 +** file, not the main database file.
 1.69013 +**
 1.69014 +** This opcode is used to implement the integrity_check pragma.
 1.69015 +*/
 1.69016 +case OP_IntegrityCk: {
 1.69017 +#if 0  /* local variables moved into u.ca */
 1.69018 +  int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 1.69019 +  int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 1.69020 +  int j;          /* Loop counter */
 1.69021 +  int nErr;       /* Number of errors reported */
 1.69022 +  char *z;        /* Text of the error report */
 1.69023 +  Mem *pnErr;     /* Register keeping track of errors remaining */
 1.69024 +#endif /* local variables moved into u.ca */
 1.69025 +
 1.69026 +  u.ca.nRoot = pOp->p2;
 1.69027 +  assert( u.ca.nRoot>0 );
 1.69028 +  u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
 1.69029 +  if( u.ca.aRoot==0 ) goto no_mem;
 1.69030 +  assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.69031 +  u.ca.pnErr = &aMem[pOp->p3];
 1.69032 +  assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
 1.69033 +  assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 1.69034 +  pIn1 = &aMem[pOp->p1];
 1.69035 +  for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
 1.69036 +    u.ca.aRoot[u.ca.j] = (int)sqlite3VdbeIntValue(&pIn1[u.ca.j]);
 1.69037 +  }
 1.69038 +  u.ca.aRoot[u.ca.j] = 0;
 1.69039 +  assert( pOp->p5<db->nDb );
 1.69040 +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
 1.69041 +  u.ca.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.ca.aRoot, u.ca.nRoot,
 1.69042 +                                 (int)u.ca.pnErr->u.i, &u.ca.nErr);
 1.69043 +  sqlite3DbFree(db, u.ca.aRoot);
 1.69044 +  u.ca.pnErr->u.i -= u.ca.nErr;
 1.69045 +  sqlite3VdbeMemSetNull(pIn1);
 1.69046 +  if( u.ca.nErr==0 ){
 1.69047 +    assert( u.ca.z==0 );
 1.69048 +  }else if( u.ca.z==0 ){
 1.69049 +    goto no_mem;
 1.69050 +  }else{
 1.69051 +    sqlite3VdbeMemSetStr(pIn1, u.ca.z, -1, SQLITE_UTF8, sqlite3_free);
 1.69052 +  }
 1.69053 +  UPDATE_MAX_BLOBSIZE(pIn1);
 1.69054 +  sqlite3VdbeChangeEncoding(pIn1, encoding);
 1.69055 +  break;
 1.69056 +}
 1.69057 +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 1.69058 +
 1.69059 +/* Opcode: RowSetAdd P1 P2 * * *
 1.69060 +**
 1.69061 +** Insert the integer value held by register P2 into a boolean index
 1.69062 +** held in register P1.
 1.69063 +**
 1.69064 +** An assertion fails if P2 is not an integer.
 1.69065 +*/
 1.69066 +case OP_RowSetAdd: {       /* in1, in2 */
 1.69067 +  pIn1 = &aMem[pOp->p1];
 1.69068 +  pIn2 = &aMem[pOp->p2];
 1.69069 +  assert( (pIn2->flags & MEM_Int)!=0 );
 1.69070 +  if( (pIn1->flags & MEM_RowSet)==0 ){
 1.69071 +    sqlite3VdbeMemSetRowSet(pIn1);
 1.69072 +    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 1.69073 +  }
 1.69074 +  sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
 1.69075 +  break;
 1.69076 +}
 1.69077 +
 1.69078 +/* Opcode: RowSetRead P1 P2 P3 * *
 1.69079 +**
 1.69080 +** Extract the smallest value from boolean index P1 and put that value into
 1.69081 +** register P3.  Or, if boolean index P1 is initially empty, leave P3
 1.69082 +** unchanged and jump to instruction P2.
 1.69083 +*/
 1.69084 +case OP_RowSetRead: {       /* jump, in1, out3 */
 1.69085 +#if 0  /* local variables moved into u.cb */
 1.69086 +  i64 val;
 1.69087 +#endif /* local variables moved into u.cb */
 1.69088 +  CHECK_FOR_INTERRUPT;
 1.69089 +  pIn1 = &aMem[pOp->p1];
 1.69090 +  if( (pIn1->flags & MEM_RowSet)==0
 1.69091 +   || sqlite3RowSetNext(pIn1->u.pRowSet, &u.cb.val)==0
 1.69092 +  ){
 1.69093 +    /* The boolean index is empty */
 1.69094 +    sqlite3VdbeMemSetNull(pIn1);
 1.69095 +    pc = pOp->p2 - 1;
 1.69096 +  }else{
 1.69097 +    /* A value was pulled from the index */
 1.69098 +    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.cb.val);
 1.69099 +  }
 1.69100 +  break;
 1.69101 +}
 1.69102 +
 1.69103 +/* Opcode: RowSetTest P1 P2 P3 P4
 1.69104 +**
 1.69105 +** Register P3 is assumed to hold a 64-bit integer value. If register P1
 1.69106 +** contains a RowSet object and that RowSet object contains
 1.69107 +** the value held in P3, jump to register P2. Otherwise, insert the
 1.69108 +** integer in P3 into the RowSet and continue on to the
 1.69109 +** next opcode.
 1.69110 +**
 1.69111 +** The RowSet object is optimized for the case where successive sets
 1.69112 +** of integers, where each set contains no duplicates. Each set
 1.69113 +** of values is identified by a unique P4 value. The first set
 1.69114 +** must have P4==0, the final set P4=-1.  P4 must be either -1 or
 1.69115 +** non-negative.  For non-negative values of P4 only the lower 4
 1.69116 +** bits are significant.
 1.69117 +**
 1.69118 +** This allows optimizations: (a) when P4==0 there is no need to test
 1.69119 +** the rowset object for P3, as it is guaranteed not to contain it,
 1.69120 +** (b) when P4==-1 there is no need to insert the value, as it will
 1.69121 +** never be tested for, and (c) when a value that is part of set X is
 1.69122 +** inserted, there is no need to search to see if the same value was
 1.69123 +** previously inserted as part of set X (only if it was previously
 1.69124 +** inserted as part of some other set).
 1.69125 +*/
 1.69126 +case OP_RowSetTest: {                     /* jump, in1, in3 */
 1.69127 +#if 0  /* local variables moved into u.cc */
 1.69128 +  int iSet;
 1.69129 +  int exists;
 1.69130 +#endif /* local variables moved into u.cc */
 1.69131 +
 1.69132 +  pIn1 = &aMem[pOp->p1];
 1.69133 +  pIn3 = &aMem[pOp->p3];
 1.69134 +  u.cc.iSet = pOp->p4.i;
 1.69135 +  assert( pIn3->flags&MEM_Int );
 1.69136 +
 1.69137 +  /* If there is anything other than a rowset object in memory cell P1,
 1.69138 +  ** delete it now and initialize P1 with an empty rowset
 1.69139 +  */
 1.69140 +  if( (pIn1->flags & MEM_RowSet)==0 ){
 1.69141 +    sqlite3VdbeMemSetRowSet(pIn1);
 1.69142 +    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 1.69143 +  }
 1.69144 +
 1.69145 +  assert( pOp->p4type==P4_INT32 );
 1.69146 +  assert( u.cc.iSet==-1 || u.cc.iSet>=0 );
 1.69147 +  if( u.cc.iSet ){
 1.69148 +    u.cc.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
 1.69149 +                               (u8)(u.cc.iSet>=0 ? u.cc.iSet & 0xf : 0xff),
 1.69150 +                               pIn3->u.i);
 1.69151 +    if( u.cc.exists ){
 1.69152 +      pc = pOp->p2 - 1;
 1.69153 +      break;
 1.69154 +    }
 1.69155 +  }
 1.69156 +  if( u.cc.iSet>=0 ){
 1.69157 +    sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
 1.69158 +  }
 1.69159 +  break;
 1.69160 +}
 1.69161 +
 1.69162 +
 1.69163 +#ifndef SQLITE_OMIT_TRIGGER
 1.69164 +
 1.69165 +/* Opcode: Program P1 P2 P3 P4 *
 1.69166 +**
 1.69167 +** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
 1.69168 +**
 1.69169 +** P1 contains the address of the memory cell that contains the first memory 
 1.69170 +** cell in an array of values used as arguments to the sub-program. P2 
 1.69171 +** contains the address to jump to if the sub-program throws an IGNORE 
 1.69172 +** exception using the RAISE() function. Register P3 contains the address 
 1.69173 +** of a memory cell in this (the parent) VM that is used to allocate the 
 1.69174 +** memory required by the sub-vdbe at runtime.
 1.69175 +**
 1.69176 +** P4 is a pointer to the VM containing the trigger program.
 1.69177 +*/
 1.69178 +case OP_Program: {        /* jump */
 1.69179 +#if 0  /* local variables moved into u.cd */
 1.69180 +  int nMem;               /* Number of memory registers for sub-program */
 1.69181 +  int nByte;              /* Bytes of runtime space required for sub-program */
 1.69182 +  Mem *pRt;               /* Register to allocate runtime space */
 1.69183 +  Mem *pMem;              /* Used to iterate through memory cells */
 1.69184 +  Mem *pEnd;              /* Last memory cell in new array */
 1.69185 +  VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 1.69186 +  SubProgram *pProgram;   /* Sub-program to execute */
 1.69187 +  void *t;                /* Token identifying trigger */
 1.69188 +#endif /* local variables moved into u.cd */
 1.69189 +
 1.69190 +  u.cd.pProgram = pOp->p4.pProgram;
 1.69191 +  u.cd.pRt = &aMem[pOp->p3];
 1.69192 +  assert( u.cd.pProgram->nOp>0 );
 1.69193 +
 1.69194 +  /* If the p5 flag is clear, then recursive invocation of triggers is
 1.69195 +  ** disabled for backwards compatibility (p5 is set if this sub-program
 1.69196 +  ** is really a trigger, not a foreign key action, and the flag set
 1.69197 +  ** and cleared by the "PRAGMA recursive_triggers" command is clear).
 1.69198 +  **
 1.69199 +  ** It is recursive invocation of triggers, at the SQL level, that is
 1.69200 +  ** disabled. In some cases a single trigger may generate more than one
 1.69201 +  ** SubProgram (if the trigger may be executed with more than one different
 1.69202 +  ** ON CONFLICT algorithm). SubProgram structures associated with a
 1.69203 +  ** single trigger all have the same value for the SubProgram.token
 1.69204 +  ** variable.  */
 1.69205 +  if( pOp->p5 ){
 1.69206 +    u.cd.t = u.cd.pProgram->token;
 1.69207 +    for(u.cd.pFrame=p->pFrame; u.cd.pFrame && u.cd.pFrame->token!=u.cd.t; u.cd.pFrame=u.cd.pFrame->pParent);
 1.69208 +    if( u.cd.pFrame ) break;
 1.69209 +  }
 1.69210 +
 1.69211 +  if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
 1.69212 +    rc = SQLITE_ERROR;
 1.69213 +    sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
 1.69214 +    break;
 1.69215 +  }
 1.69216 +
 1.69217 +  /* Register u.cd.pRt is used to store the memory required to save the state
 1.69218 +  ** of the current program, and the memory required at runtime to execute
 1.69219 +  ** the trigger program. If this trigger has been fired before, then u.cd.pRt
 1.69220 +  ** is already allocated. Otherwise, it must be initialized.  */
 1.69221 +  if( (u.cd.pRt->flags&MEM_Frame)==0 ){
 1.69222 +    /* SubProgram.nMem is set to the number of memory cells used by the
 1.69223 +    ** program stored in SubProgram.aOp. As well as these, one memory
 1.69224 +    ** cell is required for each cursor used by the program. Set local
 1.69225 +    ** variable u.cd.nMem (and later, VdbeFrame.nChildMem) to this value.
 1.69226 +    */
 1.69227 +    u.cd.nMem = u.cd.pProgram->nMem + u.cd.pProgram->nCsr;
 1.69228 +    u.cd.nByte = ROUND8(sizeof(VdbeFrame))
 1.69229 +              + u.cd.nMem * sizeof(Mem)
 1.69230 +              + u.cd.pProgram->nCsr * sizeof(VdbeCursor *)
 1.69231 +              + u.cd.pProgram->nOnce * sizeof(u8);
 1.69232 +    u.cd.pFrame = sqlite3DbMallocZero(db, u.cd.nByte);
 1.69233 +    if( !u.cd.pFrame ){
 1.69234 +      goto no_mem;
 1.69235 +    }
 1.69236 +    sqlite3VdbeMemRelease(u.cd.pRt);
 1.69237 +    u.cd.pRt->flags = MEM_Frame;
 1.69238 +    u.cd.pRt->u.pFrame = u.cd.pFrame;
 1.69239 +
 1.69240 +    u.cd.pFrame->v = p;
 1.69241 +    u.cd.pFrame->nChildMem = u.cd.nMem;
 1.69242 +    u.cd.pFrame->nChildCsr = u.cd.pProgram->nCsr;
 1.69243 +    u.cd.pFrame->pc = pc;
 1.69244 +    u.cd.pFrame->aMem = p->aMem;
 1.69245 +    u.cd.pFrame->nMem = p->nMem;
 1.69246 +    u.cd.pFrame->apCsr = p->apCsr;
 1.69247 +    u.cd.pFrame->nCursor = p->nCursor;
 1.69248 +    u.cd.pFrame->aOp = p->aOp;
 1.69249 +    u.cd.pFrame->nOp = p->nOp;
 1.69250 +    u.cd.pFrame->token = u.cd.pProgram->token;
 1.69251 +    u.cd.pFrame->aOnceFlag = p->aOnceFlag;
 1.69252 +    u.cd.pFrame->nOnceFlag = p->nOnceFlag;
 1.69253 +
 1.69254 +    u.cd.pEnd = &VdbeFrameMem(u.cd.pFrame)[u.cd.pFrame->nChildMem];
 1.69255 +    for(u.cd.pMem=VdbeFrameMem(u.cd.pFrame); u.cd.pMem!=u.cd.pEnd; u.cd.pMem++){
 1.69256 +      u.cd.pMem->flags = MEM_Invalid;
 1.69257 +      u.cd.pMem->db = db;
 1.69258 +    }
 1.69259 +  }else{
 1.69260 +    u.cd.pFrame = u.cd.pRt->u.pFrame;
 1.69261 +    assert( u.cd.pProgram->nMem+u.cd.pProgram->nCsr==u.cd.pFrame->nChildMem );
 1.69262 +    assert( u.cd.pProgram->nCsr==u.cd.pFrame->nChildCsr );
 1.69263 +    assert( pc==u.cd.pFrame->pc );
 1.69264 +  }
 1.69265 +
 1.69266 +  p->nFrame++;
 1.69267 +  u.cd.pFrame->pParent = p->pFrame;
 1.69268 +  u.cd.pFrame->lastRowid = lastRowid;
 1.69269 +  u.cd.pFrame->nChange = p->nChange;
 1.69270 +  p->nChange = 0;
 1.69271 +  p->pFrame = u.cd.pFrame;
 1.69272 +  p->aMem = aMem = &VdbeFrameMem(u.cd.pFrame)[-1];
 1.69273 +  p->nMem = u.cd.pFrame->nChildMem;
 1.69274 +  p->nCursor = (u16)u.cd.pFrame->nChildCsr;
 1.69275 +  p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
 1.69276 +  p->aOp = aOp = u.cd.pProgram->aOp;
 1.69277 +  p->nOp = u.cd.pProgram->nOp;
 1.69278 +  p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
 1.69279 +  p->nOnceFlag = u.cd.pProgram->nOnce;
 1.69280 +  pc = -1;
 1.69281 +  memset(p->aOnceFlag, 0, p->nOnceFlag);
 1.69282 +
 1.69283 +  break;
 1.69284 +}
 1.69285 +
 1.69286 +/* Opcode: Param P1 P2 * * *
 1.69287 +**
 1.69288 +** This opcode is only ever present in sub-programs called via the 
 1.69289 +** OP_Program instruction. Copy a value currently stored in a memory 
 1.69290 +** cell of the calling (parent) frame to cell P2 in the current frames 
 1.69291 +** address space. This is used by trigger programs to access the new.* 
 1.69292 +** and old.* values.
 1.69293 +**
 1.69294 +** The address of the cell in the parent frame is determined by adding
 1.69295 +** the value of the P1 argument to the value of the P1 argument to the
 1.69296 +** calling OP_Program instruction.
 1.69297 +*/
 1.69298 +case OP_Param: {           /* out2-prerelease */
 1.69299 +#if 0  /* local variables moved into u.ce */
 1.69300 +  VdbeFrame *pFrame;
 1.69301 +  Mem *pIn;
 1.69302 +#endif /* local variables moved into u.ce */
 1.69303 +  u.ce.pFrame = p->pFrame;
 1.69304 +  u.ce.pIn = &u.ce.pFrame->aMem[pOp->p1 + u.ce.pFrame->aOp[u.ce.pFrame->pc].p1];
 1.69305 +  sqlite3VdbeMemShallowCopy(pOut, u.ce.pIn, MEM_Ephem);
 1.69306 +  break;
 1.69307 +}
 1.69308 +
 1.69309 +#endif /* #ifndef SQLITE_OMIT_TRIGGER */
 1.69310 +
 1.69311 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.69312 +/* Opcode: FkCounter P1 P2 * * *
 1.69313 +**
 1.69314 +** Increment a "constraint counter" by P2 (P2 may be negative or positive).
 1.69315 +** If P1 is non-zero, the database constraint counter is incremented 
 1.69316 +** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
 1.69317 +** statement counter is incremented (immediate foreign key constraints).
 1.69318 +*/
 1.69319 +case OP_FkCounter: {
 1.69320 +  if( pOp->p1 ){
 1.69321 +    db->nDeferredCons += pOp->p2;
 1.69322 +  }else{
 1.69323 +    p->nFkConstraint += pOp->p2;
 1.69324 +  }
 1.69325 +  break;
 1.69326 +}
 1.69327 +
 1.69328 +/* Opcode: FkIfZero P1 P2 * * *
 1.69329 +**
 1.69330 +** This opcode tests if a foreign key constraint-counter is currently zero.
 1.69331 +** If so, jump to instruction P2. Otherwise, fall through to the next 
 1.69332 +** instruction.
 1.69333 +**
 1.69334 +** If P1 is non-zero, then the jump is taken if the database constraint-counter
 1.69335 +** is zero (the one that counts deferred constraint violations). If P1 is
 1.69336 +** zero, the jump is taken if the statement constraint-counter is zero
 1.69337 +** (immediate foreign key constraint violations).
 1.69338 +*/
 1.69339 +case OP_FkIfZero: {         /* jump */
 1.69340 +  if( pOp->p1 ){
 1.69341 +    if( db->nDeferredCons==0 ) pc = pOp->p2-1;
 1.69342 +  }else{
 1.69343 +    if( p->nFkConstraint==0 ) pc = pOp->p2-1;
 1.69344 +  }
 1.69345 +  break;
 1.69346 +}
 1.69347 +#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
 1.69348 +
 1.69349 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.69350 +/* Opcode: MemMax P1 P2 * * *
 1.69351 +**
 1.69352 +** P1 is a register in the root frame of this VM (the root frame is
 1.69353 +** different from the current frame if this instruction is being executed
 1.69354 +** within a sub-program). Set the value of register P1 to the maximum of 
 1.69355 +** its current value and the value in register P2.
 1.69356 +**
 1.69357 +** This instruction throws an error if the memory cell is not initially
 1.69358 +** an integer.
 1.69359 +*/
 1.69360 +case OP_MemMax: {        /* in2 */
 1.69361 +#if 0  /* local variables moved into u.cf */
 1.69362 +  Mem *pIn1;
 1.69363 +  VdbeFrame *pFrame;
 1.69364 +#endif /* local variables moved into u.cf */
 1.69365 +  if( p->pFrame ){
 1.69366 +    for(u.cf.pFrame=p->pFrame; u.cf.pFrame->pParent; u.cf.pFrame=u.cf.pFrame->pParent);
 1.69367 +    u.cf.pIn1 = &u.cf.pFrame->aMem[pOp->p1];
 1.69368 +  }else{
 1.69369 +    u.cf.pIn1 = &aMem[pOp->p1];
 1.69370 +  }
 1.69371 +  assert( memIsValid(u.cf.pIn1) );
 1.69372 +  sqlite3VdbeMemIntegerify(u.cf.pIn1);
 1.69373 +  pIn2 = &aMem[pOp->p2];
 1.69374 +  sqlite3VdbeMemIntegerify(pIn2);
 1.69375 +  if( u.cf.pIn1->u.i<pIn2->u.i){
 1.69376 +    u.cf.pIn1->u.i = pIn2->u.i;
 1.69377 +  }
 1.69378 +  break;
 1.69379 +}
 1.69380 +#endif /* SQLITE_OMIT_AUTOINCREMENT */
 1.69381 +
 1.69382 +/* Opcode: IfPos P1 P2 * * *
 1.69383 +**
 1.69384 +** If the value of register P1 is 1 or greater, jump to P2.
 1.69385 +**
 1.69386 +** It is illegal to use this instruction on a register that does
 1.69387 +** not contain an integer.  An assertion fault will result if you try.
 1.69388 +*/
 1.69389 +case OP_IfPos: {        /* jump, in1 */
 1.69390 +  pIn1 = &aMem[pOp->p1];
 1.69391 +  assert( pIn1->flags&MEM_Int );
 1.69392 +  if( pIn1->u.i>0 ){
 1.69393 +     pc = pOp->p2 - 1;
 1.69394 +  }
 1.69395 +  break;
 1.69396 +}
 1.69397 +
 1.69398 +/* Opcode: IfNeg P1 P2 * * *
 1.69399 +**
 1.69400 +** If the value of register P1 is less than zero, jump to P2. 
 1.69401 +**
 1.69402 +** It is illegal to use this instruction on a register that does
 1.69403 +** not contain an integer.  An assertion fault will result if you try.
 1.69404 +*/
 1.69405 +case OP_IfNeg: {        /* jump, in1 */
 1.69406 +  pIn1 = &aMem[pOp->p1];
 1.69407 +  assert( pIn1->flags&MEM_Int );
 1.69408 +  if( pIn1->u.i<0 ){
 1.69409 +     pc = pOp->p2 - 1;
 1.69410 +  }
 1.69411 +  break;
 1.69412 +}
 1.69413 +
 1.69414 +/* Opcode: IfZero P1 P2 P3 * *
 1.69415 +**
 1.69416 +** The register P1 must contain an integer.  Add literal P3 to the
 1.69417 +** value in register P1.  If the result is exactly 0, jump to P2. 
 1.69418 +**
 1.69419 +** It is illegal to use this instruction on a register that does
 1.69420 +** not contain an integer.  An assertion fault will result if you try.
 1.69421 +*/
 1.69422 +case OP_IfZero: {        /* jump, in1 */
 1.69423 +  pIn1 = &aMem[pOp->p1];
 1.69424 +  assert( pIn1->flags&MEM_Int );
 1.69425 +  pIn1->u.i += pOp->p3;
 1.69426 +  if( pIn1->u.i==0 ){
 1.69427 +     pc = pOp->p2 - 1;
 1.69428 +  }
 1.69429 +  break;
 1.69430 +}
 1.69431 +
 1.69432 +/* Opcode: AggStep * P2 P3 P4 P5
 1.69433 +**
 1.69434 +** Execute the step function for an aggregate.  The
 1.69435 +** function has P5 arguments.   P4 is a pointer to the FuncDef
 1.69436 +** structure that specifies the function.  Use register
 1.69437 +** P3 as the accumulator.
 1.69438 +**
 1.69439 +** The P5 arguments are taken from register P2 and its
 1.69440 +** successors.
 1.69441 +*/
 1.69442 +case OP_AggStep: {
 1.69443 +#if 0  /* local variables moved into u.cg */
 1.69444 +  int n;
 1.69445 +  int i;
 1.69446 +  Mem *pMem;
 1.69447 +  Mem *pRec;
 1.69448 +  sqlite3_context ctx;
 1.69449 +  sqlite3_value **apVal;
 1.69450 +#endif /* local variables moved into u.cg */
 1.69451 +
 1.69452 +  u.cg.n = pOp->p5;
 1.69453 +  assert( u.cg.n>=0 );
 1.69454 +  u.cg.pRec = &aMem[pOp->p2];
 1.69455 +  u.cg.apVal = p->apArg;
 1.69456 +  assert( u.cg.apVal || u.cg.n==0 );
 1.69457 +  for(u.cg.i=0; u.cg.i<u.cg.n; u.cg.i++, u.cg.pRec++){
 1.69458 +    assert( memIsValid(u.cg.pRec) );
 1.69459 +    u.cg.apVal[u.cg.i] = u.cg.pRec;
 1.69460 +    memAboutToChange(p, u.cg.pRec);
 1.69461 +    sqlite3VdbeMemStoreType(u.cg.pRec);
 1.69462 +  }
 1.69463 +  u.cg.ctx.pFunc = pOp->p4.pFunc;
 1.69464 +  assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.69465 +  u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
 1.69466 +  u.cg.pMem->n++;
 1.69467 +  u.cg.ctx.s.flags = MEM_Null;
 1.69468 +  u.cg.ctx.s.z = 0;
 1.69469 +  u.cg.ctx.s.zMalloc = 0;
 1.69470 +  u.cg.ctx.s.xDel = 0;
 1.69471 +  u.cg.ctx.s.db = db;
 1.69472 +  u.cg.ctx.isError = 0;
 1.69473 +  u.cg.ctx.pColl = 0;
 1.69474 +  u.cg.ctx.skipFlag = 0;
 1.69475 +  if( u.cg.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 1.69476 +    assert( pOp>p->aOp );
 1.69477 +    assert( pOp[-1].p4type==P4_COLLSEQ );
 1.69478 +    assert( pOp[-1].opcode==OP_CollSeq );
 1.69479 +    u.cg.ctx.pColl = pOp[-1].p4.pColl;
 1.69480 +  }
 1.69481 +  (u.cg.ctx.pFunc->xStep)(&u.cg.ctx, u.cg.n, u.cg.apVal); /* IMP: R-24505-23230 */
 1.69482 +  if( u.cg.ctx.isError ){
 1.69483 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cg.ctx.s));
 1.69484 +    rc = u.cg.ctx.isError;
 1.69485 +  }
 1.69486 +  if( u.cg.ctx.skipFlag ){
 1.69487 +    assert( pOp[-1].opcode==OP_CollSeq );
 1.69488 +    u.cg.i = pOp[-1].p1;
 1.69489 +    if( u.cg.i ) sqlite3VdbeMemSetInt64(&aMem[u.cg.i], 1);
 1.69490 +  }
 1.69491 +
 1.69492 +  sqlite3VdbeMemRelease(&u.cg.ctx.s);
 1.69493 +
 1.69494 +  break;
 1.69495 +}
 1.69496 +
 1.69497 +/* Opcode: AggFinal P1 P2 * P4 *
 1.69498 +**
 1.69499 +** Execute the finalizer function for an aggregate.  P1 is
 1.69500 +** the memory location that is the accumulator for the aggregate.
 1.69501 +**
 1.69502 +** P2 is the number of arguments that the step function takes and
 1.69503 +** P4 is a pointer to the FuncDef for this function.  The P2
 1.69504 +** argument is not used by this opcode.  It is only there to disambiguate
 1.69505 +** functions that can take varying numbers of arguments.  The
 1.69506 +** P4 argument is only needed for the degenerate case where
 1.69507 +** the step function was not previously called.
 1.69508 +*/
 1.69509 +case OP_AggFinal: {
 1.69510 +#if 0  /* local variables moved into u.ch */
 1.69511 +  Mem *pMem;
 1.69512 +#endif /* local variables moved into u.ch */
 1.69513 +  assert( pOp->p1>0 && pOp->p1<=p->nMem );
 1.69514 +  u.ch.pMem = &aMem[pOp->p1];
 1.69515 +  assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 1.69516 +  rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
 1.69517 +  if( rc ){
 1.69518 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
 1.69519 +  }
 1.69520 +  sqlite3VdbeChangeEncoding(u.ch.pMem, encoding);
 1.69521 +  UPDATE_MAX_BLOBSIZE(u.ch.pMem);
 1.69522 +  if( sqlite3VdbeMemTooBig(u.ch.pMem) ){
 1.69523 +    goto too_big;
 1.69524 +  }
 1.69525 +  break;
 1.69526 +}
 1.69527 +
 1.69528 +#ifndef SQLITE_OMIT_WAL
 1.69529 +/* Opcode: Checkpoint P1 P2 P3 * *
 1.69530 +**
 1.69531 +** Checkpoint database P1. This is a no-op if P1 is not currently in
 1.69532 +** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
 1.69533 +** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
 1.69534 +** SQLITE_BUSY or not, respectively.  Write the number of pages in the
 1.69535 +** WAL after the checkpoint into mem[P3+1] and the number of pages
 1.69536 +** in the WAL that have been checkpointed after the checkpoint
 1.69537 +** completes into mem[P3+2].  However on an error, mem[P3+1] and
 1.69538 +** mem[P3+2] are initialized to -1.
 1.69539 +*/
 1.69540 +case OP_Checkpoint: {
 1.69541 +#if 0  /* local variables moved into u.ci */
 1.69542 +  int i;                          /* Loop counter */
 1.69543 +  int aRes[3];                    /* Results */
 1.69544 +  Mem *pMem;                      /* Write results here */
 1.69545 +#endif /* local variables moved into u.ci */
 1.69546 +
 1.69547 +  u.ci.aRes[0] = 0;
 1.69548 +  u.ci.aRes[1] = u.ci.aRes[2] = -1;
 1.69549 +  assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
 1.69550 +       || pOp->p2==SQLITE_CHECKPOINT_FULL
 1.69551 +       || pOp->p2==SQLITE_CHECKPOINT_RESTART
 1.69552 +  );
 1.69553 +  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ci.aRes[1], &u.ci.aRes[2]);
 1.69554 +  if( rc==SQLITE_BUSY ){
 1.69555 +    rc = SQLITE_OK;
 1.69556 +    u.ci.aRes[0] = 1;
 1.69557 +  }
 1.69558 +  for(u.ci.i=0, u.ci.pMem = &aMem[pOp->p3]; u.ci.i<3; u.ci.i++, u.ci.pMem++){
 1.69559 +    sqlite3VdbeMemSetInt64(u.ci.pMem, (i64)u.ci.aRes[u.ci.i]);
 1.69560 +  }
 1.69561 +  break;
 1.69562 +};  
 1.69563 +#endif
 1.69564 +
 1.69565 +#ifndef SQLITE_OMIT_PRAGMA
 1.69566 +/* Opcode: JournalMode P1 P2 P3 * P5
 1.69567 +**
 1.69568 +** Change the journal mode of database P1 to P3. P3 must be one of the
 1.69569 +** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
 1.69570 +** modes (delete, truncate, persist, off and memory), this is a simple
 1.69571 +** operation. No IO is required.
 1.69572 +**
 1.69573 +** If changing into or out of WAL mode the procedure is more complicated.
 1.69574 +**
 1.69575 +** Write a string containing the final journal-mode to register P2.
 1.69576 +*/
 1.69577 +case OP_JournalMode: {    /* out2-prerelease */
 1.69578 +#if 0  /* local variables moved into u.cj */
 1.69579 +  Btree *pBt;                     /* Btree to change journal mode of */
 1.69580 +  Pager *pPager;                  /* Pager associated with pBt */
 1.69581 +  int eNew;                       /* New journal mode */
 1.69582 +  int eOld;                       /* The old journal mode */
 1.69583 +#ifndef SQLITE_OMIT_WAL
 1.69584 +  const char *zFilename;          /* Name of database file for pPager */
 1.69585 +#endif
 1.69586 +#endif /* local variables moved into u.cj */
 1.69587 +
 1.69588 +  u.cj.eNew = pOp->p3;
 1.69589 +  assert( u.cj.eNew==PAGER_JOURNALMODE_DELETE
 1.69590 +       || u.cj.eNew==PAGER_JOURNALMODE_TRUNCATE
 1.69591 +       || u.cj.eNew==PAGER_JOURNALMODE_PERSIST
 1.69592 +       || u.cj.eNew==PAGER_JOURNALMODE_OFF
 1.69593 +       || u.cj.eNew==PAGER_JOURNALMODE_MEMORY
 1.69594 +       || u.cj.eNew==PAGER_JOURNALMODE_WAL
 1.69595 +       || u.cj.eNew==PAGER_JOURNALMODE_QUERY
 1.69596 +  );
 1.69597 +  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.69598 +
 1.69599 +  u.cj.pBt = db->aDb[pOp->p1].pBt;
 1.69600 +  u.cj.pPager = sqlite3BtreePager(u.cj.pBt);
 1.69601 +  u.cj.eOld = sqlite3PagerGetJournalMode(u.cj.pPager);
 1.69602 +  if( u.cj.eNew==PAGER_JOURNALMODE_QUERY ) u.cj.eNew = u.cj.eOld;
 1.69603 +  if( !sqlite3PagerOkToChangeJournalMode(u.cj.pPager) ) u.cj.eNew = u.cj.eOld;
 1.69604 +
 1.69605 +#ifndef SQLITE_OMIT_WAL
 1.69606 +  u.cj.zFilename = sqlite3PagerFilename(u.cj.pPager, 1);
 1.69607 +
 1.69608 +  /* Do not allow a transition to journal_mode=WAL for a database
 1.69609 +  ** in temporary storage or if the VFS does not support shared memory
 1.69610 +  */
 1.69611 +  if( u.cj.eNew==PAGER_JOURNALMODE_WAL
 1.69612 +   && (sqlite3Strlen30(u.cj.zFilename)==0           /* Temp file */
 1.69613 +       || !sqlite3PagerWalSupported(u.cj.pPager))   /* No shared-memory support */
 1.69614 +  ){
 1.69615 +    u.cj.eNew = u.cj.eOld;
 1.69616 +  }
 1.69617 +
 1.69618 +  if( (u.cj.eNew!=u.cj.eOld)
 1.69619 +   && (u.cj.eOld==PAGER_JOURNALMODE_WAL || u.cj.eNew==PAGER_JOURNALMODE_WAL)
 1.69620 +  ){
 1.69621 +    if( !db->autoCommit || db->activeVdbeCnt>1 ){
 1.69622 +      rc = SQLITE_ERROR;
 1.69623 +      sqlite3SetString(&p->zErrMsg, db,
 1.69624 +          "cannot change %s wal mode from within a transaction",
 1.69625 +          (u.cj.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
 1.69626 +      );
 1.69627 +      break;
 1.69628 +    }else{
 1.69629 +
 1.69630 +      if( u.cj.eOld==PAGER_JOURNALMODE_WAL ){
 1.69631 +        /* If leaving WAL mode, close the log file. If successful, the call
 1.69632 +        ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
 1.69633 +        ** file. An EXCLUSIVE lock may still be held on the database file
 1.69634 +        ** after a successful return.
 1.69635 +        */
 1.69636 +        rc = sqlite3PagerCloseWal(u.cj.pPager);
 1.69637 +        if( rc==SQLITE_OK ){
 1.69638 +          sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
 1.69639 +        }
 1.69640 +      }else if( u.cj.eOld==PAGER_JOURNALMODE_MEMORY ){
 1.69641 +        /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
 1.69642 +        ** as an intermediate */
 1.69643 +        sqlite3PagerSetJournalMode(u.cj.pPager, PAGER_JOURNALMODE_OFF);
 1.69644 +      }
 1.69645 +
 1.69646 +      /* Open a transaction on the database file. Regardless of the journal
 1.69647 +      ** mode, this transaction always uses a rollback journal.
 1.69648 +      */
 1.69649 +      assert( sqlite3BtreeIsInTrans(u.cj.pBt)==0 );
 1.69650 +      if( rc==SQLITE_OK ){
 1.69651 +        rc = sqlite3BtreeSetVersion(u.cj.pBt, (u.cj.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
 1.69652 +      }
 1.69653 +    }
 1.69654 +  }
 1.69655 +#endif /* ifndef SQLITE_OMIT_WAL */
 1.69656 +
 1.69657 +  if( rc ){
 1.69658 +    u.cj.eNew = u.cj.eOld;
 1.69659 +  }
 1.69660 +  u.cj.eNew = sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
 1.69661 +
 1.69662 +  pOut = &aMem[pOp->p2];
 1.69663 +  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 1.69664 +  pOut->z = (char *)sqlite3JournalModename(u.cj.eNew);
 1.69665 +  pOut->n = sqlite3Strlen30(pOut->z);
 1.69666 +  pOut->enc = SQLITE_UTF8;
 1.69667 +  sqlite3VdbeChangeEncoding(pOut, encoding);
 1.69668 +  break;
 1.69669 +};
 1.69670 +#endif /* SQLITE_OMIT_PRAGMA */
 1.69671 +
 1.69672 +#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 1.69673 +/* Opcode: Vacuum * * * * *
 1.69674 +**
 1.69675 +** Vacuum the entire database.  This opcode will cause other virtual
 1.69676 +** machines to be created and run.  It may not be called from within
 1.69677 +** a transaction.
 1.69678 +*/
 1.69679 +case OP_Vacuum: {
 1.69680 +  rc = sqlite3RunVacuum(&p->zErrMsg, db);
 1.69681 +  break;
 1.69682 +}
 1.69683 +#endif
 1.69684 +
 1.69685 +#if !defined(SQLITE_OMIT_AUTOVACUUM)
 1.69686 +/* Opcode: IncrVacuum P1 P2 * * *
 1.69687 +**
 1.69688 +** Perform a single step of the incremental vacuum procedure on
 1.69689 +** the P1 database. If the vacuum has finished, jump to instruction
 1.69690 +** P2. Otherwise, fall through to the next instruction.
 1.69691 +*/
 1.69692 +case OP_IncrVacuum: {        /* jump */
 1.69693 +#if 0  /* local variables moved into u.ck */
 1.69694 +  Btree *pBt;
 1.69695 +#endif /* local variables moved into u.ck */
 1.69696 +
 1.69697 +  assert( pOp->p1>=0 && pOp->p1<db->nDb );
 1.69698 +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 1.69699 +  u.ck.pBt = db->aDb[pOp->p1].pBt;
 1.69700 +  rc = sqlite3BtreeIncrVacuum(u.ck.pBt);
 1.69701 +  if( rc==SQLITE_DONE ){
 1.69702 +    pc = pOp->p2 - 1;
 1.69703 +    rc = SQLITE_OK;
 1.69704 +  }
 1.69705 +  break;
 1.69706 +}
 1.69707 +#endif
 1.69708 +
 1.69709 +/* Opcode: Expire P1 * * * *
 1.69710 +**
 1.69711 +** Cause precompiled statements to become expired. An expired statement
 1.69712 +** fails with an error code of SQLITE_SCHEMA if it is ever executed 
 1.69713 +** (via sqlite3_step()).
 1.69714 +** 
 1.69715 +** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
 1.69716 +** then only the currently executing statement is affected. 
 1.69717 +*/
 1.69718 +case OP_Expire: {
 1.69719 +  if( !pOp->p1 ){
 1.69720 +    sqlite3ExpirePreparedStatements(db);
 1.69721 +  }else{
 1.69722 +    p->expired = 1;
 1.69723 +  }
 1.69724 +  break;
 1.69725 +}
 1.69726 +
 1.69727 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.69728 +/* Opcode: TableLock P1 P2 P3 P4 *
 1.69729 +**
 1.69730 +** Obtain a lock on a particular table. This instruction is only used when
 1.69731 +** the shared-cache feature is enabled. 
 1.69732 +**
 1.69733 +** P1 is the index of the database in sqlite3.aDb[] of the database
 1.69734 +** on which the lock is acquired.  A readlock is obtained if P3==0 or
 1.69735 +** a write lock if P3==1.
 1.69736 +**
 1.69737 +** P2 contains the root-page of the table to lock.
 1.69738 +**
 1.69739 +** P4 contains a pointer to the name of the table being locked. This is only
 1.69740 +** used to generate an error message if the lock cannot be obtained.
 1.69741 +*/
 1.69742 +case OP_TableLock: {
 1.69743 +  u8 isWriteLock = (u8)pOp->p3;
 1.69744 +  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
 1.69745 +    int p1 = pOp->p1; 
 1.69746 +    assert( p1>=0 && p1<db->nDb );
 1.69747 +    assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
 1.69748 +    assert( isWriteLock==0 || isWriteLock==1 );
 1.69749 +    rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
 1.69750 +    if( (rc&0xFF)==SQLITE_LOCKED ){
 1.69751 +      const char *z = pOp->p4.z;
 1.69752 +      sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
 1.69753 +    }
 1.69754 +  }
 1.69755 +  break;
 1.69756 +}
 1.69757 +#endif /* SQLITE_OMIT_SHARED_CACHE */
 1.69758 +
 1.69759 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.69760 +/* Opcode: VBegin * * * P4 *
 1.69761 +**
 1.69762 +** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 
 1.69763 +** xBegin method for that table.
 1.69764 +**
 1.69765 +** Also, whether or not P4 is set, check that this is not being called from
 1.69766 +** within a callback to a virtual table xSync() method. If it is, the error
 1.69767 +** code will be set to SQLITE_LOCKED.
 1.69768 +*/
 1.69769 +case OP_VBegin: {
 1.69770 +#if 0  /* local variables moved into u.cl */
 1.69771 +  VTable *pVTab;
 1.69772 +#endif /* local variables moved into u.cl */
 1.69773 +  u.cl.pVTab = pOp->p4.pVtab;
 1.69774 +  rc = sqlite3VtabBegin(db, u.cl.pVTab);
 1.69775 +  if( u.cl.pVTab ) importVtabErrMsg(p, u.cl.pVTab->pVtab);
 1.69776 +  break;
 1.69777 +}
 1.69778 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.69779 +
 1.69780 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.69781 +/* Opcode: VCreate P1 * * P4 *
 1.69782 +**
 1.69783 +** P4 is the name of a virtual table in database P1. Call the xCreate method
 1.69784 +** for that table.
 1.69785 +*/
 1.69786 +case OP_VCreate: {
 1.69787 +  rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
 1.69788 +  break;
 1.69789 +}
 1.69790 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.69791 +
 1.69792 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.69793 +/* Opcode: VDestroy P1 * * P4 *
 1.69794 +**
 1.69795 +** P4 is the name of a virtual table in database P1.  Call the xDestroy method
 1.69796 +** of that table.
 1.69797 +*/
 1.69798 +case OP_VDestroy: {
 1.69799 +  p->inVtabMethod = 2;
 1.69800 +  rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
 1.69801 +  p->inVtabMethod = 0;
 1.69802 +  break;
 1.69803 +}
 1.69804 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.69805 +
 1.69806 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.69807 +/* Opcode: VOpen P1 * * P4 *
 1.69808 +**
 1.69809 +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 1.69810 +** P1 is a cursor number.  This opcode opens a cursor to the virtual
 1.69811 +** table and stores that cursor in P1.
 1.69812 +*/
 1.69813 +case OP_VOpen: {
 1.69814 +#if 0  /* local variables moved into u.cm */
 1.69815 +  VdbeCursor *pCur;
 1.69816 +  sqlite3_vtab_cursor *pVtabCursor;
 1.69817 +  sqlite3_vtab *pVtab;
 1.69818 +  sqlite3_module *pModule;
 1.69819 +#endif /* local variables moved into u.cm */
 1.69820 +
 1.69821 +  u.cm.pCur = 0;
 1.69822 +  u.cm.pVtabCursor = 0;
 1.69823 +  u.cm.pVtab = pOp->p4.pVtab->pVtab;
 1.69824 +  u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
 1.69825 +  assert(u.cm.pVtab && u.cm.pModule);
 1.69826 +  rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
 1.69827 +  importVtabErrMsg(p, u.cm.pVtab);
 1.69828 +  if( SQLITE_OK==rc ){
 1.69829 +    /* Initialize sqlite3_vtab_cursor base class */
 1.69830 +    u.cm.pVtabCursor->pVtab = u.cm.pVtab;
 1.69831 +
 1.69832 +    /* Initialise vdbe cursor object */
 1.69833 +    u.cm.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 1.69834 +    if( u.cm.pCur ){
 1.69835 +      u.cm.pCur->pVtabCursor = u.cm.pVtabCursor;
 1.69836 +      u.cm.pCur->pModule = u.cm.pVtabCursor->pVtab->pModule;
 1.69837 +    }else{
 1.69838 +      db->mallocFailed = 1;
 1.69839 +      u.cm.pModule->xClose(u.cm.pVtabCursor);
 1.69840 +    }
 1.69841 +  }
 1.69842 +  break;
 1.69843 +}
 1.69844 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.69845 +
 1.69846 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.69847 +/* Opcode: VFilter P1 P2 P3 P4 *
 1.69848 +**
 1.69849 +** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
 1.69850 +** the filtered result set is empty.
 1.69851 +**
 1.69852 +** P4 is either NULL or a string that was generated by the xBestIndex
 1.69853 +** method of the module.  The interpretation of the P4 string is left
 1.69854 +** to the module implementation.
 1.69855 +**
 1.69856 +** This opcode invokes the xFilter method on the virtual table specified
 1.69857 +** by P1.  The integer query plan parameter to xFilter is stored in register
 1.69858 +** P3. Register P3+1 stores the argc parameter to be passed to the
 1.69859 +** xFilter method. Registers P3+2..P3+1+argc are the argc
 1.69860 +** additional parameters which are passed to
 1.69861 +** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
 1.69862 +**
 1.69863 +** A jump is made to P2 if the result set after filtering would be empty.
 1.69864 +*/
 1.69865 +case OP_VFilter: {   /* jump */
 1.69866 +#if 0  /* local variables moved into u.cn */
 1.69867 +  int nArg;
 1.69868 +  int iQuery;
 1.69869 +  const sqlite3_module *pModule;
 1.69870 +  Mem *pQuery;
 1.69871 +  Mem *pArgc;
 1.69872 +  sqlite3_vtab_cursor *pVtabCursor;
 1.69873 +  sqlite3_vtab *pVtab;
 1.69874 +  VdbeCursor *pCur;
 1.69875 +  int res;
 1.69876 +  int i;
 1.69877 +  Mem **apArg;
 1.69878 +#endif /* local variables moved into u.cn */
 1.69879 +
 1.69880 +  u.cn.pQuery = &aMem[pOp->p3];
 1.69881 +  u.cn.pArgc = &u.cn.pQuery[1];
 1.69882 +  u.cn.pCur = p->apCsr[pOp->p1];
 1.69883 +  assert( memIsValid(u.cn.pQuery) );
 1.69884 +  REGISTER_TRACE(pOp->p3, u.cn.pQuery);
 1.69885 +  assert( u.cn.pCur->pVtabCursor );
 1.69886 +  u.cn.pVtabCursor = u.cn.pCur->pVtabCursor;
 1.69887 +  u.cn.pVtab = u.cn.pVtabCursor->pVtab;
 1.69888 +  u.cn.pModule = u.cn.pVtab->pModule;
 1.69889 +
 1.69890 +  /* Grab the index number and argc parameters */
 1.69891 +  assert( (u.cn.pQuery->flags&MEM_Int)!=0 && u.cn.pArgc->flags==MEM_Int );
 1.69892 +  u.cn.nArg = (int)u.cn.pArgc->u.i;
 1.69893 +  u.cn.iQuery = (int)u.cn.pQuery->u.i;
 1.69894 +
 1.69895 +  /* Invoke the xFilter method */
 1.69896 +  {
 1.69897 +    u.cn.res = 0;
 1.69898 +    u.cn.apArg = p->apArg;
 1.69899 +    for(u.cn.i = 0; u.cn.i<u.cn.nArg; u.cn.i++){
 1.69900 +      u.cn.apArg[u.cn.i] = &u.cn.pArgc[u.cn.i+1];
 1.69901 +      sqlite3VdbeMemStoreType(u.cn.apArg[u.cn.i]);
 1.69902 +    }
 1.69903 +
 1.69904 +    p->inVtabMethod = 1;
 1.69905 +    rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
 1.69906 +    p->inVtabMethod = 0;
 1.69907 +    importVtabErrMsg(p, u.cn.pVtab);
 1.69908 +    if( rc==SQLITE_OK ){
 1.69909 +      u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
 1.69910 +    }
 1.69911 +
 1.69912 +    if( u.cn.res ){
 1.69913 +      pc = pOp->p2 - 1;
 1.69914 +    }
 1.69915 +  }
 1.69916 +  u.cn.pCur->nullRow = 0;
 1.69917 +
 1.69918 +  break;
 1.69919 +}
 1.69920 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.69921 +
 1.69922 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.69923 +/* Opcode: VColumn P1 P2 P3 * *
 1.69924 +**
 1.69925 +** Store the value of the P2-th column of
 1.69926 +** the row of the virtual-table that the 
 1.69927 +** P1 cursor is pointing to into register P3.
 1.69928 +*/
 1.69929 +case OP_VColumn: {
 1.69930 +#if 0  /* local variables moved into u.co */
 1.69931 +  sqlite3_vtab *pVtab;
 1.69932 +  const sqlite3_module *pModule;
 1.69933 +  Mem *pDest;
 1.69934 +  sqlite3_context sContext;
 1.69935 +#endif /* local variables moved into u.co */
 1.69936 +
 1.69937 +  VdbeCursor *pCur = p->apCsr[pOp->p1];
 1.69938 +  assert( pCur->pVtabCursor );
 1.69939 +  assert( pOp->p3>0 && pOp->p3<=p->nMem );
 1.69940 +  u.co.pDest = &aMem[pOp->p3];
 1.69941 +  memAboutToChange(p, u.co.pDest);
 1.69942 +  if( pCur->nullRow ){
 1.69943 +    sqlite3VdbeMemSetNull(u.co.pDest);
 1.69944 +    break;
 1.69945 +  }
 1.69946 +  u.co.pVtab = pCur->pVtabCursor->pVtab;
 1.69947 +  u.co.pModule = u.co.pVtab->pModule;
 1.69948 +  assert( u.co.pModule->xColumn );
 1.69949 +  memset(&u.co.sContext, 0, sizeof(u.co.sContext));
 1.69950 +
 1.69951 +  /* The output cell may already have a buffer allocated. Move
 1.69952 +  ** the current contents to u.co.sContext.s so in case the user-function
 1.69953 +  ** can use the already allocated buffer instead of allocating a
 1.69954 +  ** new one.
 1.69955 +  */
 1.69956 +  sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
 1.69957 +  MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
 1.69958 +
 1.69959 +  rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
 1.69960 +  importVtabErrMsg(p, u.co.pVtab);
 1.69961 +  if( u.co.sContext.isError ){
 1.69962 +    rc = u.co.sContext.isError;
 1.69963 +  }
 1.69964 +
 1.69965 +  /* Copy the result of the function to the P3 register. We
 1.69966 +  ** do this regardless of whether or not an error occurred to ensure any
 1.69967 +  ** dynamic allocation in u.co.sContext.s (a Mem struct) is  released.
 1.69968 +  */
 1.69969 +  sqlite3VdbeChangeEncoding(&u.co.sContext.s, encoding);
 1.69970 +  sqlite3VdbeMemMove(u.co.pDest, &u.co.sContext.s);
 1.69971 +  REGISTER_TRACE(pOp->p3, u.co.pDest);
 1.69972 +  UPDATE_MAX_BLOBSIZE(u.co.pDest);
 1.69973 +
 1.69974 +  if( sqlite3VdbeMemTooBig(u.co.pDest) ){
 1.69975 +    goto too_big;
 1.69976 +  }
 1.69977 +  break;
 1.69978 +}
 1.69979 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.69980 +
 1.69981 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.69982 +/* Opcode: VNext P1 P2 * * *
 1.69983 +**
 1.69984 +** Advance virtual table P1 to the next row in its result set and
 1.69985 +** jump to instruction P2.  Or, if the virtual table has reached
 1.69986 +** the end of its result set, then fall through to the next instruction.
 1.69987 +*/
 1.69988 +case OP_VNext: {   /* jump */
 1.69989 +#if 0  /* local variables moved into u.cp */
 1.69990 +  sqlite3_vtab *pVtab;
 1.69991 +  const sqlite3_module *pModule;
 1.69992 +  int res;
 1.69993 +  VdbeCursor *pCur;
 1.69994 +#endif /* local variables moved into u.cp */
 1.69995 +
 1.69996 +  u.cp.res = 0;
 1.69997 +  u.cp.pCur = p->apCsr[pOp->p1];
 1.69998 +  assert( u.cp.pCur->pVtabCursor );
 1.69999 +  if( u.cp.pCur->nullRow ){
 1.70000 +    break;
 1.70001 +  }
 1.70002 +  u.cp.pVtab = u.cp.pCur->pVtabCursor->pVtab;
 1.70003 +  u.cp.pModule = u.cp.pVtab->pModule;
 1.70004 +  assert( u.cp.pModule->xNext );
 1.70005 +
 1.70006 +  /* Invoke the xNext() method of the module. There is no way for the
 1.70007 +  ** underlying implementation to return an error if one occurs during
 1.70008 +  ** xNext(). Instead, if an error occurs, true is returned (indicating that
 1.70009 +  ** data is available) and the error code returned when xColumn or
 1.70010 +  ** some other method is next invoked on the save virtual table cursor.
 1.70011 +  */
 1.70012 +  p->inVtabMethod = 1;
 1.70013 +  rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
 1.70014 +  p->inVtabMethod = 0;
 1.70015 +  importVtabErrMsg(p, u.cp.pVtab);
 1.70016 +  if( rc==SQLITE_OK ){
 1.70017 +    u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
 1.70018 +  }
 1.70019 +
 1.70020 +  if( !u.cp.res ){
 1.70021 +    /* If there is data, jump to P2 */
 1.70022 +    pc = pOp->p2 - 1;
 1.70023 +  }
 1.70024 +  break;
 1.70025 +}
 1.70026 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.70027 +
 1.70028 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.70029 +/* Opcode: VRename P1 * * P4 *
 1.70030 +**
 1.70031 +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 1.70032 +** This opcode invokes the corresponding xRename method. The value
 1.70033 +** in register P1 is passed as the zName argument to the xRename method.
 1.70034 +*/
 1.70035 +case OP_VRename: {
 1.70036 +#if 0  /* local variables moved into u.cq */
 1.70037 +  sqlite3_vtab *pVtab;
 1.70038 +  Mem *pName;
 1.70039 +#endif /* local variables moved into u.cq */
 1.70040 +
 1.70041 +  u.cq.pVtab = pOp->p4.pVtab->pVtab;
 1.70042 +  u.cq.pName = &aMem[pOp->p1];
 1.70043 +  assert( u.cq.pVtab->pModule->xRename );
 1.70044 +  assert( memIsValid(u.cq.pName) );
 1.70045 +  REGISTER_TRACE(pOp->p1, u.cq.pName);
 1.70046 +  assert( u.cq.pName->flags & MEM_Str );
 1.70047 +  testcase( u.cq.pName->enc==SQLITE_UTF8 );
 1.70048 +  testcase( u.cq.pName->enc==SQLITE_UTF16BE );
 1.70049 +  testcase( u.cq.pName->enc==SQLITE_UTF16LE );
 1.70050 +  rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
 1.70051 +  if( rc==SQLITE_OK ){
 1.70052 +    rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
 1.70053 +    importVtabErrMsg(p, u.cq.pVtab);
 1.70054 +    p->expired = 0;
 1.70055 +  }
 1.70056 +  break;
 1.70057 +}
 1.70058 +#endif
 1.70059 +
 1.70060 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.70061 +/* Opcode: VUpdate P1 P2 P3 P4 *
 1.70062 +**
 1.70063 +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 1.70064 +** This opcode invokes the corresponding xUpdate method. P2 values
 1.70065 +** are contiguous memory cells starting at P3 to pass to the xUpdate 
 1.70066 +** invocation. The value in register (P3+P2-1) corresponds to the 
 1.70067 +** p2th element of the argv array passed to xUpdate.
 1.70068 +**
 1.70069 +** The xUpdate method will do a DELETE or an INSERT or both.
 1.70070 +** The argv[0] element (which corresponds to memory cell P3)
 1.70071 +** is the rowid of a row to delete.  If argv[0] is NULL then no 
 1.70072 +** deletion occurs.  The argv[1] element is the rowid of the new 
 1.70073 +** row.  This can be NULL to have the virtual table select the new 
 1.70074 +** rowid for itself.  The subsequent elements in the array are 
 1.70075 +** the values of columns in the new row.
 1.70076 +**
 1.70077 +** If P2==1 then no insert is performed.  argv[0] is the rowid of
 1.70078 +** a row to delete.
 1.70079 +**
 1.70080 +** P1 is a boolean flag. If it is set to true and the xUpdate call
 1.70081 +** is successful, then the value returned by sqlite3_last_insert_rowid() 
 1.70082 +** is set to the value of the rowid for the row just inserted.
 1.70083 +*/
 1.70084 +case OP_VUpdate: {
 1.70085 +#if 0  /* local variables moved into u.cr */
 1.70086 +  sqlite3_vtab *pVtab;
 1.70087 +  sqlite3_module *pModule;
 1.70088 +  int nArg;
 1.70089 +  int i;
 1.70090 +  sqlite_int64 rowid;
 1.70091 +  Mem **apArg;
 1.70092 +  Mem *pX;
 1.70093 +#endif /* local variables moved into u.cr */
 1.70094 +
 1.70095 +  assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
 1.70096 +       || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
 1.70097 +  );
 1.70098 +  u.cr.pVtab = pOp->p4.pVtab->pVtab;
 1.70099 +  u.cr.pModule = (sqlite3_module *)u.cr.pVtab->pModule;
 1.70100 +  u.cr.nArg = pOp->p2;
 1.70101 +  assert( pOp->p4type==P4_VTAB );
 1.70102 +  if( ALWAYS(u.cr.pModule->xUpdate) ){
 1.70103 +    u8 vtabOnConflict = db->vtabOnConflict;
 1.70104 +    u.cr.apArg = p->apArg;
 1.70105 +    u.cr.pX = &aMem[pOp->p3];
 1.70106 +    for(u.cr.i=0; u.cr.i<u.cr.nArg; u.cr.i++){
 1.70107 +      assert( memIsValid(u.cr.pX) );
 1.70108 +      memAboutToChange(p, u.cr.pX);
 1.70109 +      sqlite3VdbeMemStoreType(u.cr.pX);
 1.70110 +      u.cr.apArg[u.cr.i] = u.cr.pX;
 1.70111 +      u.cr.pX++;
 1.70112 +    }
 1.70113 +    db->vtabOnConflict = pOp->p5;
 1.70114 +    rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
 1.70115 +    db->vtabOnConflict = vtabOnConflict;
 1.70116 +    importVtabErrMsg(p, u.cr.pVtab);
 1.70117 +    if( rc==SQLITE_OK && pOp->p1 ){
 1.70118 +      assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
 1.70119 +      db->lastRowid = lastRowid = u.cr.rowid;
 1.70120 +    }
 1.70121 +    if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
 1.70122 +      if( pOp->p5==OE_Ignore ){
 1.70123 +        rc = SQLITE_OK;
 1.70124 +      }else{
 1.70125 +        p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
 1.70126 +      }
 1.70127 +    }else{
 1.70128 +      p->nChange++;
 1.70129 +    }
 1.70130 +  }
 1.70131 +  break;
 1.70132 +}
 1.70133 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
 1.70134 +
 1.70135 +#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
 1.70136 +/* Opcode: Pagecount P1 P2 * * *
 1.70137 +**
 1.70138 +** Write the current number of pages in database P1 to memory cell P2.
 1.70139 +*/
 1.70140 +case OP_Pagecount: {            /* out2-prerelease */
 1.70141 +  pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
 1.70142 +  break;
 1.70143 +}
 1.70144 +#endif
 1.70145 +
 1.70146 +
 1.70147 +#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
 1.70148 +/* Opcode: MaxPgcnt P1 P2 P3 * *
 1.70149 +**
 1.70150 +** Try to set the maximum page count for database P1 to the value in P3.
 1.70151 +** Do not let the maximum page count fall below the current page count and
 1.70152 +** do not change the maximum page count value if P3==0.
 1.70153 +**
 1.70154 +** Store the maximum page count after the change in register P2.
 1.70155 +*/
 1.70156 +case OP_MaxPgcnt: {            /* out2-prerelease */
 1.70157 +  unsigned int newMax;
 1.70158 +  Btree *pBt;
 1.70159 +
 1.70160 +  pBt = db->aDb[pOp->p1].pBt;
 1.70161 +  newMax = 0;
 1.70162 +  if( pOp->p3 ){
 1.70163 +    newMax = sqlite3BtreeLastPage(pBt);
 1.70164 +    if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
 1.70165 +  }
 1.70166 +  pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
 1.70167 +  break;
 1.70168 +}
 1.70169 +#endif
 1.70170 +
 1.70171 +
 1.70172 +#ifndef SQLITE_OMIT_TRACE
 1.70173 +/* Opcode: Trace * * * P4 *
 1.70174 +**
 1.70175 +** If tracing is enabled (by the sqlite3_trace()) interface, then
 1.70176 +** the UTF-8 string contained in P4 is emitted on the trace callback.
 1.70177 +*/
 1.70178 +case OP_Trace: {
 1.70179 +#if 0  /* local variables moved into u.cs */
 1.70180 +  char *zTrace;
 1.70181 +  char *z;
 1.70182 +#endif /* local variables moved into u.cs */
 1.70183 +
 1.70184 +  if( db->xTrace
 1.70185 +   && !p->doingRerun
 1.70186 +   && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 1.70187 +  ){
 1.70188 +    u.cs.z = sqlite3VdbeExpandSql(p, u.cs.zTrace);
 1.70189 +    db->xTrace(db->pTraceArg, u.cs.z);
 1.70190 +    sqlite3DbFree(db, u.cs.z);
 1.70191 +  }
 1.70192 +#ifdef SQLITE_DEBUG
 1.70193 +  if( (db->flags & SQLITE_SqlTrace)!=0
 1.70194 +   && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 1.70195 +  ){
 1.70196 +    sqlite3DebugPrintf("SQL-trace: %s\n", u.cs.zTrace);
 1.70197 +  }
 1.70198 +#endif /* SQLITE_DEBUG */
 1.70199 +  break;
 1.70200 +}
 1.70201 +#endif
 1.70202 +
 1.70203 +
 1.70204 +/* Opcode: Noop * * * * *
 1.70205 +**
 1.70206 +** Do nothing.  This instruction is often useful as a jump
 1.70207 +** destination.
 1.70208 +*/
 1.70209 +/*
 1.70210 +** The magic Explain opcode are only inserted when explain==2 (which
 1.70211 +** is to say when the EXPLAIN QUERY PLAN syntax is used.)
 1.70212 +** This opcode records information from the optimizer.  It is the
 1.70213 +** the same as a no-op.  This opcodesnever appears in a real VM program.
 1.70214 +*/
 1.70215 +default: {          /* This is really OP_Noop and OP_Explain */
 1.70216 +  assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
 1.70217 +  break;
 1.70218 +}
 1.70219 +
 1.70220 +/*****************************************************************************
 1.70221 +** The cases of the switch statement above this line should all be indented
 1.70222 +** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
 1.70223 +** readability.  From this point on down, the normal indentation rules are
 1.70224 +** restored.
 1.70225 +*****************************************************************************/
 1.70226 +    }
 1.70227 +
 1.70228 +#ifdef VDBE_PROFILE
 1.70229 +    {
 1.70230 +      u64 elapsed = sqlite3Hwtime() - start;
 1.70231 +      pOp->cycles += elapsed;
 1.70232 +      pOp->cnt++;
 1.70233 +#if 0
 1.70234 +        fprintf(stdout, "%10llu ", elapsed);
 1.70235 +        sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
 1.70236 +#endif
 1.70237 +    }
 1.70238 +#endif
 1.70239 +
 1.70240 +    /* The following code adds nothing to the actual functionality
 1.70241 +    ** of the program.  It is only here for testing and debugging.
 1.70242 +    ** On the other hand, it does burn CPU cycles every time through
 1.70243 +    ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
 1.70244 +    */
 1.70245 +#ifndef NDEBUG
 1.70246 +    assert( pc>=-1 && pc<p->nOp );
 1.70247 +
 1.70248 +#ifdef SQLITE_DEBUG
 1.70249 +    if( p->trace ){
 1.70250 +      if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
 1.70251 +      if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
 1.70252 +        registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
 1.70253 +      }
 1.70254 +      if( pOp->opflags & OPFLG_OUT3 ){
 1.70255 +        registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
 1.70256 +      }
 1.70257 +    }
 1.70258 +#endif  /* SQLITE_DEBUG */
 1.70259 +#endif  /* NDEBUG */
 1.70260 +  }  /* The end of the for(;;) loop the loops through opcodes */
 1.70261 +
 1.70262 +  /* If we reach this point, it means that execution is finished with
 1.70263 +  ** an error of some kind.
 1.70264 +  */
 1.70265 +vdbe_error_halt:
 1.70266 +  assert( rc );
 1.70267 +  p->rc = rc;
 1.70268 +  testcase( sqlite3GlobalConfig.xLog!=0 );
 1.70269 +  sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
 1.70270 +                   pc, p->zSql, p->zErrMsg);
 1.70271 +  sqlite3VdbeHalt(p);
 1.70272 +  if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
 1.70273 +  rc = SQLITE_ERROR;
 1.70274 +  if( resetSchemaOnFault>0 ){
 1.70275 +    sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 1.70276 +  }
 1.70277 +
 1.70278 +  /* This is the only way out of this procedure.  We have to
 1.70279 +  ** release the mutexes on btrees that were acquired at the
 1.70280 +  ** top. */
 1.70281 +vdbe_return:
 1.70282 +  db->lastRowid = lastRowid;
 1.70283 +  sqlite3VdbeLeave(p);
 1.70284 +  return rc;
 1.70285 +
 1.70286 +  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
 1.70287 +  ** is encountered.
 1.70288 +  */
 1.70289 +too_big:
 1.70290 +  sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
 1.70291 +  rc = SQLITE_TOOBIG;
 1.70292 +  goto vdbe_error_halt;
 1.70293 +
 1.70294 +  /* Jump to here if a malloc() fails.
 1.70295 +  */
 1.70296 +no_mem:
 1.70297 +  db->mallocFailed = 1;
 1.70298 +  sqlite3SetString(&p->zErrMsg, db, "out of memory");
 1.70299 +  rc = SQLITE_NOMEM;
 1.70300 +  goto vdbe_error_halt;
 1.70301 +
 1.70302 +  /* Jump to here for any other kind of fatal error.  The "rc" variable
 1.70303 +  ** should hold the error number.
 1.70304 +  */
 1.70305 +abort_due_to_error:
 1.70306 +  assert( p->zErrMsg==0 );
 1.70307 +  if( db->mallocFailed ) rc = SQLITE_NOMEM;
 1.70308 +  if( rc!=SQLITE_IOERR_NOMEM ){
 1.70309 +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
 1.70310 +  }
 1.70311 +  goto vdbe_error_halt;
 1.70312 +
 1.70313 +  /* Jump to here if the sqlite3_interrupt() API sets the interrupt
 1.70314 +  ** flag.
 1.70315 +  */
 1.70316 +abort_due_to_interrupt:
 1.70317 +  assert( db->u1.isInterrupted );
 1.70318 +  rc = SQLITE_INTERRUPT;
 1.70319 +  p->rc = rc;
 1.70320 +  sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
 1.70321 +  goto vdbe_error_halt;
 1.70322 +}
 1.70323 +
 1.70324 +/************** End of vdbe.c ************************************************/
 1.70325 +/************** Begin file vdbeblob.c ****************************************/
 1.70326 +/*
 1.70327 +** 2007 May 1
 1.70328 +**
 1.70329 +** The author disclaims copyright to this source code.  In place of
 1.70330 +** a legal notice, here is a blessing:
 1.70331 +**
 1.70332 +**    May you do good and not evil.
 1.70333 +**    May you find forgiveness for yourself and forgive others.
 1.70334 +**    May you share freely, never taking more than you give.
 1.70335 +**
 1.70336 +*************************************************************************
 1.70337 +**
 1.70338 +** This file contains code used to implement incremental BLOB I/O.
 1.70339 +*/
 1.70340 +
 1.70341 +
 1.70342 +#ifndef SQLITE_OMIT_INCRBLOB
 1.70343 +
 1.70344 +/*
 1.70345 +** Valid sqlite3_blob* handles point to Incrblob structures.
 1.70346 +*/
 1.70347 +typedef struct Incrblob Incrblob;
 1.70348 +struct Incrblob {
 1.70349 +  int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
 1.70350 +  int nByte;              /* Size of open blob, in bytes */
 1.70351 +  int iOffset;            /* Byte offset of blob in cursor data */
 1.70352 +  int iCol;               /* Table column this handle is open on */
 1.70353 +  BtCursor *pCsr;         /* Cursor pointing at blob row */
 1.70354 +  sqlite3_stmt *pStmt;    /* Statement holding cursor open */
 1.70355 +  sqlite3 *db;            /* The associated database */
 1.70356 +};
 1.70357 +
 1.70358 +
 1.70359 +/*
 1.70360 +** This function is used by both blob_open() and blob_reopen(). It seeks
 1.70361 +** the b-tree cursor associated with blob handle p to point to row iRow.
 1.70362 +** If successful, SQLITE_OK is returned and subsequent calls to
 1.70363 +** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
 1.70364 +**
 1.70365 +** If an error occurs, or if the specified row does not exist or does not
 1.70366 +** contain a value of type TEXT or BLOB in the column nominated when the
 1.70367 +** blob handle was opened, then an error code is returned and *pzErr may
 1.70368 +** be set to point to a buffer containing an error message. It is the
 1.70369 +** responsibility of the caller to free the error message buffer using
 1.70370 +** sqlite3DbFree().
 1.70371 +**
 1.70372 +** If an error does occur, then the b-tree cursor is closed. All subsequent
 1.70373 +** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will 
 1.70374 +** immediately return SQLITE_ABORT.
 1.70375 +*/
 1.70376 +static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
 1.70377 +  int rc;                         /* Error code */
 1.70378 +  char *zErr = 0;                 /* Error message */
 1.70379 +  Vdbe *v = (Vdbe *)p->pStmt;
 1.70380 +
 1.70381 +  /* Set the value of the SQL statements only variable to integer iRow. 
 1.70382 +  ** This is done directly instead of using sqlite3_bind_int64() to avoid 
 1.70383 +  ** triggering asserts related to mutexes.
 1.70384 +  */
 1.70385 +  assert( v->aVar[0].flags&MEM_Int );
 1.70386 +  v->aVar[0].u.i = iRow;
 1.70387 +
 1.70388 +  rc = sqlite3_step(p->pStmt);
 1.70389 +  if( rc==SQLITE_ROW ){
 1.70390 +    u32 type = v->apCsr[0]->aType[p->iCol];
 1.70391 +    if( type<12 ){
 1.70392 +      zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
 1.70393 +          type==0?"null": type==7?"real": "integer"
 1.70394 +      );
 1.70395 +      rc = SQLITE_ERROR;
 1.70396 +      sqlite3_finalize(p->pStmt);
 1.70397 +      p->pStmt = 0;
 1.70398 +    }else{
 1.70399 +      p->iOffset = v->apCsr[0]->aOffset[p->iCol];
 1.70400 +      p->nByte = sqlite3VdbeSerialTypeLen(type);
 1.70401 +      p->pCsr =  v->apCsr[0]->pCursor;
 1.70402 +      sqlite3BtreeEnterCursor(p->pCsr);
 1.70403 +      sqlite3BtreeCacheOverflow(p->pCsr);
 1.70404 +      sqlite3BtreeLeaveCursor(p->pCsr);
 1.70405 +    }
 1.70406 +  }
 1.70407 +
 1.70408 +  if( rc==SQLITE_ROW ){
 1.70409 +    rc = SQLITE_OK;
 1.70410 +  }else if( p->pStmt ){
 1.70411 +    rc = sqlite3_finalize(p->pStmt);
 1.70412 +    p->pStmt = 0;
 1.70413 +    if( rc==SQLITE_OK ){
 1.70414 +      zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
 1.70415 +      rc = SQLITE_ERROR;
 1.70416 +    }else{
 1.70417 +      zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
 1.70418 +    }
 1.70419 +  }
 1.70420 +
 1.70421 +  assert( rc!=SQLITE_OK || zErr==0 );
 1.70422 +  assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
 1.70423 +
 1.70424 +  *pzErr = zErr;
 1.70425 +  return rc;
 1.70426 +}
 1.70427 +
 1.70428 +/*
 1.70429 +** Open a blob handle.
 1.70430 +*/
 1.70431 +SQLITE_API int sqlite3_blob_open(
 1.70432 +  sqlite3* db,            /* The database connection */
 1.70433 +  const char *zDb,        /* The attached database containing the blob */
 1.70434 +  const char *zTable,     /* The table containing the blob */
 1.70435 +  const char *zColumn,    /* The column containing the blob */
 1.70436 +  sqlite_int64 iRow,      /* The row containing the glob */
 1.70437 +  int flags,              /* True -> read/write access, false -> read-only */
 1.70438 +  sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
 1.70439 +){
 1.70440 +  int nAttempt = 0;
 1.70441 +  int iCol;               /* Index of zColumn in row-record */
 1.70442 +
 1.70443 +  /* This VDBE program seeks a btree cursor to the identified 
 1.70444 +  ** db/table/row entry. The reason for using a vdbe program instead
 1.70445 +  ** of writing code to use the b-tree layer directly is that the
 1.70446 +  ** vdbe program will take advantage of the various transaction,
 1.70447 +  ** locking and error handling infrastructure built into the vdbe.
 1.70448 +  **
 1.70449 +  ** After seeking the cursor, the vdbe executes an OP_ResultRow.
 1.70450 +  ** Code external to the Vdbe then "borrows" the b-tree cursor and
 1.70451 +  ** uses it to implement the blob_read(), blob_write() and 
 1.70452 +  ** blob_bytes() functions.
 1.70453 +  **
 1.70454 +  ** The sqlite3_blob_close() function finalizes the vdbe program,
 1.70455 +  ** which closes the b-tree cursor and (possibly) commits the 
 1.70456 +  ** transaction.
 1.70457 +  */
 1.70458 +  static const VdbeOpList openBlob[] = {
 1.70459 +    {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
 1.70460 +    {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
 1.70461 +    {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
 1.70462 +
 1.70463 +    /* One of the following two instructions is replaced by an OP_Noop. */
 1.70464 +    {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
 1.70465 +    {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
 1.70466 +
 1.70467 +    {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
 1.70468 +    {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
 1.70469 +    {OP_Column, 0, 0, 1},          /* 7  */
 1.70470 +    {OP_ResultRow, 1, 0, 0},       /* 8  */
 1.70471 +    {OP_Goto, 0, 5, 0},            /* 9  */
 1.70472 +    {OP_Close, 0, 0, 0},           /* 10 */
 1.70473 +    {OP_Halt, 0, 0, 0},            /* 11 */
 1.70474 +  };
 1.70475 +
 1.70476 +  int rc = SQLITE_OK;
 1.70477 +  char *zErr = 0;
 1.70478 +  Table *pTab;
 1.70479 +  Parse *pParse = 0;
 1.70480 +  Incrblob *pBlob = 0;
 1.70481 +
 1.70482 +  flags = !!flags;                /* flags = (flags ? 1 : 0); */
 1.70483 +  *ppBlob = 0;
 1.70484 +
 1.70485 +  sqlite3_mutex_enter(db->mutex);
 1.70486 +
 1.70487 +  pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
 1.70488 +  if( !pBlob ) goto blob_open_out;
 1.70489 +  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
 1.70490 +  if( !pParse ) goto blob_open_out;
 1.70491 +
 1.70492 +  do {
 1.70493 +    memset(pParse, 0, sizeof(Parse));
 1.70494 +    pParse->db = db;
 1.70495 +    sqlite3DbFree(db, zErr);
 1.70496 +    zErr = 0;
 1.70497 +
 1.70498 +    sqlite3BtreeEnterAll(db);
 1.70499 +    pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
 1.70500 +    if( pTab && IsVirtual(pTab) ){
 1.70501 +      pTab = 0;
 1.70502 +      sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
 1.70503 +    }
 1.70504 +#ifndef SQLITE_OMIT_VIEW
 1.70505 +    if( pTab && pTab->pSelect ){
 1.70506 +      pTab = 0;
 1.70507 +      sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
 1.70508 +    }
 1.70509 +#endif
 1.70510 +    if( !pTab ){
 1.70511 +      if( pParse->zErrMsg ){
 1.70512 +        sqlite3DbFree(db, zErr);
 1.70513 +        zErr = pParse->zErrMsg;
 1.70514 +        pParse->zErrMsg = 0;
 1.70515 +      }
 1.70516 +      rc = SQLITE_ERROR;
 1.70517 +      sqlite3BtreeLeaveAll(db);
 1.70518 +      goto blob_open_out;
 1.70519 +    }
 1.70520 +
 1.70521 +    /* Now search pTab for the exact column. */
 1.70522 +    for(iCol=0; iCol<pTab->nCol; iCol++) {
 1.70523 +      if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
 1.70524 +        break;
 1.70525 +      }
 1.70526 +    }
 1.70527 +    if( iCol==pTab->nCol ){
 1.70528 +      sqlite3DbFree(db, zErr);
 1.70529 +      zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
 1.70530 +      rc = SQLITE_ERROR;
 1.70531 +      sqlite3BtreeLeaveAll(db);
 1.70532 +      goto blob_open_out;
 1.70533 +    }
 1.70534 +
 1.70535 +    /* If the value is being opened for writing, check that the
 1.70536 +    ** column is not indexed, and that it is not part of a foreign key. 
 1.70537 +    ** It is against the rules to open a column to which either of these
 1.70538 +    ** descriptions applies for writing.  */
 1.70539 +    if( flags ){
 1.70540 +      const char *zFault = 0;
 1.70541 +      Index *pIdx;
 1.70542 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.70543 +      if( db->flags&SQLITE_ForeignKeys ){
 1.70544 +        /* Check that the column is not part of an FK child key definition. It
 1.70545 +        ** is not necessary to check if it is part of a parent key, as parent
 1.70546 +        ** key columns must be indexed. The check below will pick up this 
 1.70547 +        ** case.  */
 1.70548 +        FKey *pFKey;
 1.70549 +        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
 1.70550 +          int j;
 1.70551 +          for(j=0; j<pFKey->nCol; j++){
 1.70552 +            if( pFKey->aCol[j].iFrom==iCol ){
 1.70553 +              zFault = "foreign key";
 1.70554 +            }
 1.70555 +          }
 1.70556 +        }
 1.70557 +      }
 1.70558 +#endif
 1.70559 +      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.70560 +        int j;
 1.70561 +        for(j=0; j<pIdx->nColumn; j++){
 1.70562 +          if( pIdx->aiColumn[j]==iCol ){
 1.70563 +            zFault = "indexed";
 1.70564 +          }
 1.70565 +        }
 1.70566 +      }
 1.70567 +      if( zFault ){
 1.70568 +        sqlite3DbFree(db, zErr);
 1.70569 +        zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
 1.70570 +        rc = SQLITE_ERROR;
 1.70571 +        sqlite3BtreeLeaveAll(db);
 1.70572 +        goto blob_open_out;
 1.70573 +      }
 1.70574 +    }
 1.70575 +
 1.70576 +    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
 1.70577 +    assert( pBlob->pStmt || db->mallocFailed );
 1.70578 +    if( pBlob->pStmt ){
 1.70579 +      Vdbe *v = (Vdbe *)pBlob->pStmt;
 1.70580 +      int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.70581 +
 1.70582 +      sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
 1.70583 +
 1.70584 +
 1.70585 +      /* Configure the OP_Transaction */
 1.70586 +      sqlite3VdbeChangeP1(v, 0, iDb);
 1.70587 +      sqlite3VdbeChangeP2(v, 0, flags);
 1.70588 +
 1.70589 +      /* Configure the OP_VerifyCookie */
 1.70590 +      sqlite3VdbeChangeP1(v, 1, iDb);
 1.70591 +      sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
 1.70592 +      sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
 1.70593 +
 1.70594 +      /* Make sure a mutex is held on the table to be accessed */
 1.70595 +      sqlite3VdbeUsesBtree(v, iDb); 
 1.70596 +
 1.70597 +      /* Configure the OP_TableLock instruction */
 1.70598 +#ifdef SQLITE_OMIT_SHARED_CACHE
 1.70599 +      sqlite3VdbeChangeToNoop(v, 2);
 1.70600 +#else
 1.70601 +      sqlite3VdbeChangeP1(v, 2, iDb);
 1.70602 +      sqlite3VdbeChangeP2(v, 2, pTab->tnum);
 1.70603 +      sqlite3VdbeChangeP3(v, 2, flags);
 1.70604 +      sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
 1.70605 +#endif
 1.70606 +
 1.70607 +      /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 1.70608 +      ** parameter of the other to pTab->tnum.  */
 1.70609 +      sqlite3VdbeChangeToNoop(v, 4 - flags);
 1.70610 +      sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
 1.70611 +      sqlite3VdbeChangeP3(v, 3 + flags, iDb);
 1.70612 +
 1.70613 +      /* Configure the number of columns. Configure the cursor to
 1.70614 +      ** think that the table has one more column than it really
 1.70615 +      ** does. An OP_Column to retrieve this imaginary column will
 1.70616 +      ** always return an SQL NULL. This is useful because it means
 1.70617 +      ** we can invoke OP_Column to fill in the vdbe cursors type 
 1.70618 +      ** and offset cache without causing any IO.
 1.70619 +      */
 1.70620 +      sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
 1.70621 +      sqlite3VdbeChangeP2(v, 7, pTab->nCol);
 1.70622 +      if( !db->mallocFailed ){
 1.70623 +        pParse->nVar = 1;
 1.70624 +        pParse->nMem = 1;
 1.70625 +        pParse->nTab = 1;
 1.70626 +        sqlite3VdbeMakeReady(v, pParse);
 1.70627 +      }
 1.70628 +    }
 1.70629 +   
 1.70630 +    pBlob->flags = flags;
 1.70631 +    pBlob->iCol = iCol;
 1.70632 +    pBlob->db = db;
 1.70633 +    sqlite3BtreeLeaveAll(db);
 1.70634 +    if( db->mallocFailed ){
 1.70635 +      goto blob_open_out;
 1.70636 +    }
 1.70637 +    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
 1.70638 +    rc = blobSeekToRow(pBlob, iRow, &zErr);
 1.70639 +  } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
 1.70640 +
 1.70641 +blob_open_out:
 1.70642 +  if( rc==SQLITE_OK && db->mallocFailed==0 ){
 1.70643 +    *ppBlob = (sqlite3_blob *)pBlob;
 1.70644 +  }else{
 1.70645 +    if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
 1.70646 +    sqlite3DbFree(db, pBlob);
 1.70647 +  }
 1.70648 +  sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
 1.70649 +  sqlite3DbFree(db, zErr);
 1.70650 +  sqlite3StackFree(db, pParse);
 1.70651 +  rc = sqlite3ApiExit(db, rc);
 1.70652 +  sqlite3_mutex_leave(db->mutex);
 1.70653 +  return rc;
 1.70654 +}
 1.70655 +
 1.70656 +/*
 1.70657 +** Close a blob handle that was previously created using
 1.70658 +** sqlite3_blob_open().
 1.70659 +*/
 1.70660 +SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
 1.70661 +  Incrblob *p = (Incrblob *)pBlob;
 1.70662 +  int rc;
 1.70663 +  sqlite3 *db;
 1.70664 +
 1.70665 +  if( p ){
 1.70666 +    db = p->db;
 1.70667 +    sqlite3_mutex_enter(db->mutex);
 1.70668 +    rc = sqlite3_finalize(p->pStmt);
 1.70669 +    sqlite3DbFree(db, p);
 1.70670 +    sqlite3_mutex_leave(db->mutex);
 1.70671 +  }else{
 1.70672 +    rc = SQLITE_OK;
 1.70673 +  }
 1.70674 +  return rc;
 1.70675 +}
 1.70676 +
 1.70677 +/*
 1.70678 +** Perform a read or write operation on a blob
 1.70679 +*/
 1.70680 +static int blobReadWrite(
 1.70681 +  sqlite3_blob *pBlob, 
 1.70682 +  void *z, 
 1.70683 +  int n, 
 1.70684 +  int iOffset, 
 1.70685 +  int (*xCall)(BtCursor*, u32, u32, void*)
 1.70686 +){
 1.70687 +  int rc;
 1.70688 +  Incrblob *p = (Incrblob *)pBlob;
 1.70689 +  Vdbe *v;
 1.70690 +  sqlite3 *db;
 1.70691 +
 1.70692 +  if( p==0 ) return SQLITE_MISUSE_BKPT;
 1.70693 +  db = p->db;
 1.70694 +  sqlite3_mutex_enter(db->mutex);
 1.70695 +  v = (Vdbe*)p->pStmt;
 1.70696 +
 1.70697 +  if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
 1.70698 +    /* Request is out of range. Return a transient error. */
 1.70699 +    rc = SQLITE_ERROR;
 1.70700 +    sqlite3Error(db, SQLITE_ERROR, 0);
 1.70701 +  }else if( v==0 ){
 1.70702 +    /* If there is no statement handle, then the blob-handle has
 1.70703 +    ** already been invalidated. Return SQLITE_ABORT in this case.
 1.70704 +    */
 1.70705 +    rc = SQLITE_ABORT;
 1.70706 +  }else{
 1.70707 +    /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
 1.70708 +    ** returned, clean-up the statement handle.
 1.70709 +    */
 1.70710 +    assert( db == v->db );
 1.70711 +    sqlite3BtreeEnterCursor(p->pCsr);
 1.70712 +    rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
 1.70713 +    sqlite3BtreeLeaveCursor(p->pCsr);
 1.70714 +    if( rc==SQLITE_ABORT ){
 1.70715 +      sqlite3VdbeFinalize(v);
 1.70716 +      p->pStmt = 0;
 1.70717 +    }else{
 1.70718 +      db->errCode = rc;
 1.70719 +      v->rc = rc;
 1.70720 +    }
 1.70721 +  }
 1.70722 +  rc = sqlite3ApiExit(db, rc);
 1.70723 +  sqlite3_mutex_leave(db->mutex);
 1.70724 +  return rc;
 1.70725 +}
 1.70726 +
 1.70727 +/*
 1.70728 +** Read data from a blob handle.
 1.70729 +*/
 1.70730 +SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
 1.70731 +  return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
 1.70732 +}
 1.70733 +
 1.70734 +/*
 1.70735 +** Write data to a blob handle.
 1.70736 +*/
 1.70737 +SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
 1.70738 +  return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
 1.70739 +}
 1.70740 +
 1.70741 +/*
 1.70742 +** Query a blob handle for the size of the data.
 1.70743 +**
 1.70744 +** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
 1.70745 +** so no mutex is required for access.
 1.70746 +*/
 1.70747 +SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
 1.70748 +  Incrblob *p = (Incrblob *)pBlob;
 1.70749 +  return (p && p->pStmt) ? p->nByte : 0;
 1.70750 +}
 1.70751 +
 1.70752 +/*
 1.70753 +** Move an existing blob handle to point to a different row of the same
 1.70754 +** database table.
 1.70755 +**
 1.70756 +** If an error occurs, or if the specified row does not exist or does not
 1.70757 +** contain a blob or text value, then an error code is returned and the
 1.70758 +** database handle error code and message set. If this happens, then all 
 1.70759 +** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) 
 1.70760 +** immediately return SQLITE_ABORT.
 1.70761 +*/
 1.70762 +SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
 1.70763 +  int rc;
 1.70764 +  Incrblob *p = (Incrblob *)pBlob;
 1.70765 +  sqlite3 *db;
 1.70766 +
 1.70767 +  if( p==0 ) return SQLITE_MISUSE_BKPT;
 1.70768 +  db = p->db;
 1.70769 +  sqlite3_mutex_enter(db->mutex);
 1.70770 +
 1.70771 +  if( p->pStmt==0 ){
 1.70772 +    /* If there is no statement handle, then the blob-handle has
 1.70773 +    ** already been invalidated. Return SQLITE_ABORT in this case.
 1.70774 +    */
 1.70775 +    rc = SQLITE_ABORT;
 1.70776 +  }else{
 1.70777 +    char *zErr;
 1.70778 +    rc = blobSeekToRow(p, iRow, &zErr);
 1.70779 +    if( rc!=SQLITE_OK ){
 1.70780 +      sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
 1.70781 +      sqlite3DbFree(db, zErr);
 1.70782 +    }
 1.70783 +    assert( rc!=SQLITE_SCHEMA );
 1.70784 +  }
 1.70785 +
 1.70786 +  rc = sqlite3ApiExit(db, rc);
 1.70787 +  assert( rc==SQLITE_OK || p->pStmt==0 );
 1.70788 +  sqlite3_mutex_leave(db->mutex);
 1.70789 +  return rc;
 1.70790 +}
 1.70791 +
 1.70792 +#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
 1.70793 +
 1.70794 +/************** End of vdbeblob.c ********************************************/
 1.70795 +/************** Begin file vdbesort.c ****************************************/
 1.70796 +/*
 1.70797 +** 2011 July 9
 1.70798 +**
 1.70799 +** The author disclaims copyright to this source code.  In place of
 1.70800 +** a legal notice, here is a blessing:
 1.70801 +**
 1.70802 +**    May you do good and not evil.
 1.70803 +**    May you find forgiveness for yourself and forgive others.
 1.70804 +**    May you share freely, never taking more than you give.
 1.70805 +**
 1.70806 +*************************************************************************
 1.70807 +** This file contains code for the VdbeSorter object, used in concert with
 1.70808 +** a VdbeCursor to sort large numbers of keys (as may be required, for
 1.70809 +** example, by CREATE INDEX statements on tables too large to fit in main
 1.70810 +** memory).
 1.70811 +*/
 1.70812 +
 1.70813 +
 1.70814 +#ifndef SQLITE_OMIT_MERGE_SORT
 1.70815 +
 1.70816 +typedef struct VdbeSorterIter VdbeSorterIter;
 1.70817 +typedef struct SorterRecord SorterRecord;
 1.70818 +typedef struct FileWriter FileWriter;
 1.70819 +
 1.70820 +/*
 1.70821 +** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
 1.70822 +**
 1.70823 +** As keys are added to the sorter, they are written to disk in a series
 1.70824 +** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
 1.70825 +** the same as the cache-size allowed for temporary databases. In order
 1.70826 +** to allow the caller to extract keys from the sorter in sorted order,
 1.70827 +** all PMAs currently stored on disk must be merged together. This comment
 1.70828 +** describes the data structure used to do so. The structure supports 
 1.70829 +** merging any number of arrays in a single pass with no redundant comparison 
 1.70830 +** operations.
 1.70831 +**
 1.70832 +** The aIter[] array contains an iterator for each of the PMAs being merged.
 1.70833 +** An aIter[] iterator either points to a valid key or else is at EOF. For 
 1.70834 +** the purposes of the paragraphs below, we assume that the array is actually 
 1.70835 +** N elements in size, where N is the smallest power of 2 greater to or equal 
 1.70836 +** to the number of iterators being merged. The extra aIter[] elements are 
 1.70837 +** treated as if they are empty (always at EOF).
 1.70838 +**
 1.70839 +** The aTree[] array is also N elements in size. The value of N is stored in
 1.70840 +** the VdbeSorter.nTree variable.
 1.70841 +**
 1.70842 +** The final (N/2) elements of aTree[] contain the results of comparing
 1.70843 +** pairs of iterator keys together. Element i contains the result of 
 1.70844 +** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
 1.70845 +** aTree element is set to the index of it. 
 1.70846 +**
 1.70847 +** For the purposes of this comparison, EOF is considered greater than any
 1.70848 +** other key value. If the keys are equal (only possible with two EOF
 1.70849 +** values), it doesn't matter which index is stored.
 1.70850 +**
 1.70851 +** The (N/4) elements of aTree[] that preceed the final (N/2) described 
 1.70852 +** above contains the index of the smallest of each block of 4 iterators.
 1.70853 +** And so on. So that aTree[1] contains the index of the iterator that 
 1.70854 +** currently points to the smallest key value. aTree[0] is unused.
 1.70855 +**
 1.70856 +** Example:
 1.70857 +**
 1.70858 +**     aIter[0] -> Banana
 1.70859 +**     aIter[1] -> Feijoa
 1.70860 +**     aIter[2] -> Elderberry
 1.70861 +**     aIter[3] -> Currant
 1.70862 +**     aIter[4] -> Grapefruit
 1.70863 +**     aIter[5] -> Apple
 1.70864 +**     aIter[6] -> Durian
 1.70865 +**     aIter[7] -> EOF
 1.70866 +**
 1.70867 +**     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
 1.70868 +**
 1.70869 +** The current element is "Apple" (the value of the key indicated by 
 1.70870 +** iterator 5). When the Next() operation is invoked, iterator 5 will
 1.70871 +** be advanced to the next key in its segment. Say the next key is
 1.70872 +** "Eggplant":
 1.70873 +**
 1.70874 +**     aIter[5] -> Eggplant
 1.70875 +**
 1.70876 +** The contents of aTree[] are updated first by comparing the new iterator
 1.70877 +** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
 1.70878 +** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
 1.70879 +** The value of iterator 6 - "Durian" - is now smaller than that of iterator
 1.70880 +** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
 1.70881 +** so the value written into element 1 of the array is 0. As follows:
 1.70882 +**
 1.70883 +**     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
 1.70884 +**
 1.70885 +** In other words, each time we advance to the next sorter element, log2(N)
 1.70886 +** key comparison operations are required, where N is the number of segments
 1.70887 +** being merged (rounded up to the next power of 2).
 1.70888 +*/
 1.70889 +struct VdbeSorter {
 1.70890 +  i64 iWriteOff;                  /* Current write offset within file pTemp1 */
 1.70891 +  i64 iReadOff;                   /* Current read offset within file pTemp1 */
 1.70892 +  int nInMemory;                  /* Current size of pRecord list as PMA */
 1.70893 +  int nTree;                      /* Used size of aTree/aIter (power of 2) */
 1.70894 +  int nPMA;                       /* Number of PMAs stored in pTemp1 */
 1.70895 +  int mnPmaSize;                  /* Minimum PMA size, in bytes */
 1.70896 +  int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
 1.70897 +  VdbeSorterIter *aIter;          /* Array of iterators to merge */
 1.70898 +  int *aTree;                     /* Current state of incremental merge */
 1.70899 +  sqlite3_file *pTemp1;           /* PMA file 1 */
 1.70900 +  SorterRecord *pRecord;          /* Head of in-memory record list */
 1.70901 +  UnpackedRecord *pUnpacked;      /* Used to unpack keys */
 1.70902 +};
 1.70903 +
 1.70904 +/*
 1.70905 +** The following type is an iterator for a PMA. It caches the current key in 
 1.70906 +** variables nKey/aKey. If the iterator is at EOF, pFile==0.
 1.70907 +*/
 1.70908 +struct VdbeSorterIter {
 1.70909 +  i64 iReadOff;                   /* Current read offset */
 1.70910 +  i64 iEof;                       /* 1 byte past EOF for this iterator */
 1.70911 +  int nAlloc;                     /* Bytes of space at aAlloc */
 1.70912 +  int nKey;                       /* Number of bytes in key */
 1.70913 +  sqlite3_file *pFile;            /* File iterator is reading from */
 1.70914 +  u8 *aAlloc;                     /* Allocated space */
 1.70915 +  u8 *aKey;                       /* Pointer to current key */
 1.70916 +  u8 *aBuffer;                    /* Current read buffer */
 1.70917 +  int nBuffer;                    /* Size of read buffer in bytes */
 1.70918 +};
 1.70919 +
 1.70920 +/*
 1.70921 +** An instance of this structure is used to organize the stream of records
 1.70922 +** being written to files by the merge-sort code into aligned, page-sized
 1.70923 +** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
 1.70924 +** faster on many operating systems.
 1.70925 +*/
 1.70926 +struct FileWriter {
 1.70927 +  int eFWErr;                     /* Non-zero if in an error state */
 1.70928 +  u8 *aBuffer;                    /* Pointer to write buffer */
 1.70929 +  int nBuffer;                    /* Size of write buffer in bytes */
 1.70930 +  int iBufStart;                  /* First byte of buffer to write */
 1.70931 +  int iBufEnd;                    /* Last byte of buffer to write */
 1.70932 +  i64 iWriteOff;                  /* Offset of start of buffer in file */
 1.70933 +  sqlite3_file *pFile;            /* File to write to */
 1.70934 +};
 1.70935 +
 1.70936 +/*
 1.70937 +** A structure to store a single record. All in-memory records are connected
 1.70938 +** together into a linked list headed at VdbeSorter.pRecord using the 
 1.70939 +** SorterRecord.pNext pointer.
 1.70940 +*/
 1.70941 +struct SorterRecord {
 1.70942 +  void *pVal;
 1.70943 +  int nVal;
 1.70944 +  SorterRecord *pNext;
 1.70945 +};
 1.70946 +
 1.70947 +/* Minimum allowable value for the VdbeSorter.nWorking variable */
 1.70948 +#define SORTER_MIN_WORKING 10
 1.70949 +
 1.70950 +/* Maximum number of segments to merge in a single pass. */
 1.70951 +#define SORTER_MAX_MERGE_COUNT 16
 1.70952 +
 1.70953 +/*
 1.70954 +** Free all memory belonging to the VdbeSorterIter object passed as the second
 1.70955 +** argument. All structure fields are set to zero before returning.
 1.70956 +*/
 1.70957 +static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
 1.70958 +  sqlite3DbFree(db, pIter->aAlloc);
 1.70959 +  sqlite3DbFree(db, pIter->aBuffer);
 1.70960 +  memset(pIter, 0, sizeof(VdbeSorterIter));
 1.70961 +}
 1.70962 +
 1.70963 +/*
 1.70964 +** Read nByte bytes of data from the stream of data iterated by object p.
 1.70965 +** If successful, set *ppOut to point to a buffer containing the data
 1.70966 +** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
 1.70967 +** error code.
 1.70968 +**
 1.70969 +** The buffer indicated by *ppOut may only be considered valid until the
 1.70970 +** next call to this function.
 1.70971 +*/
 1.70972 +static int vdbeSorterIterRead(
 1.70973 +  sqlite3 *db,                    /* Database handle (for malloc) */
 1.70974 +  VdbeSorterIter *p,              /* Iterator */
 1.70975 +  int nByte,                      /* Bytes of data to read */
 1.70976 +  u8 **ppOut                      /* OUT: Pointer to buffer containing data */
 1.70977 +){
 1.70978 +  int iBuf;                       /* Offset within buffer to read from */
 1.70979 +  int nAvail;                     /* Bytes of data available in buffer */
 1.70980 +  assert( p->aBuffer );
 1.70981 +
 1.70982 +  /* If there is no more data to be read from the buffer, read the next 
 1.70983 +  ** p->nBuffer bytes of data from the file into it. Or, if there are less
 1.70984 +  ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
 1.70985 +  iBuf = p->iReadOff % p->nBuffer;
 1.70986 +  if( iBuf==0 ){
 1.70987 +    int nRead;                    /* Bytes to read from disk */
 1.70988 +    int rc;                       /* sqlite3OsRead() return code */
 1.70989 +
 1.70990 +    /* Determine how many bytes of data to read. */
 1.70991 +    if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
 1.70992 +      nRead = p->nBuffer;
 1.70993 +    }else{
 1.70994 +      nRead = (int)(p->iEof - p->iReadOff);
 1.70995 +    }
 1.70996 +    assert( nRead>0 );
 1.70997 +
 1.70998 +    /* Read data from the file. Return early if an error occurs. */
 1.70999 +    rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
 1.71000 +    assert( rc!=SQLITE_IOERR_SHORT_READ );
 1.71001 +    if( rc!=SQLITE_OK ) return rc;
 1.71002 +  }
 1.71003 +  nAvail = p->nBuffer - iBuf; 
 1.71004 +
 1.71005 +  if( nByte<=nAvail ){
 1.71006 +    /* The requested data is available in the in-memory buffer. In this
 1.71007 +    ** case there is no need to make a copy of the data, just return a 
 1.71008 +    ** pointer into the buffer to the caller.  */
 1.71009 +    *ppOut = &p->aBuffer[iBuf];
 1.71010 +    p->iReadOff += nByte;
 1.71011 +  }else{
 1.71012 +    /* The requested data is not all available in the in-memory buffer.
 1.71013 +    ** In this case, allocate space at p->aAlloc[] to copy the requested
 1.71014 +    ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
 1.71015 +    int nRem;                     /* Bytes remaining to copy */
 1.71016 +
 1.71017 +    /* Extend the p->aAlloc[] allocation if required. */
 1.71018 +    if( p->nAlloc<nByte ){
 1.71019 +      int nNew = p->nAlloc*2;
 1.71020 +      while( nByte>nNew ) nNew = nNew*2;
 1.71021 +      p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
 1.71022 +      if( !p->aAlloc ) return SQLITE_NOMEM;
 1.71023 +      p->nAlloc = nNew;
 1.71024 +    }
 1.71025 +
 1.71026 +    /* Copy as much data as is available in the buffer into the start of
 1.71027 +    ** p->aAlloc[].  */
 1.71028 +    memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
 1.71029 +    p->iReadOff += nAvail;
 1.71030 +    nRem = nByte - nAvail;
 1.71031 +
 1.71032 +    /* The following loop copies up to p->nBuffer bytes per iteration into
 1.71033 +    ** the p->aAlloc[] buffer.  */
 1.71034 +    while( nRem>0 ){
 1.71035 +      int rc;                     /* vdbeSorterIterRead() return code */
 1.71036 +      int nCopy;                  /* Number of bytes to copy */
 1.71037 +      u8 *aNext;                  /* Pointer to buffer to copy data from */
 1.71038 +
 1.71039 +      nCopy = nRem;
 1.71040 +      if( nRem>p->nBuffer ) nCopy = p->nBuffer;
 1.71041 +      rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
 1.71042 +      if( rc!=SQLITE_OK ) return rc;
 1.71043 +      assert( aNext!=p->aAlloc );
 1.71044 +      memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
 1.71045 +      nRem -= nCopy;
 1.71046 +    }
 1.71047 +
 1.71048 +    *ppOut = p->aAlloc;
 1.71049 +  }
 1.71050 +
 1.71051 +  return SQLITE_OK;
 1.71052 +}
 1.71053 +
 1.71054 +/*
 1.71055 +** Read a varint from the stream of data accessed by p. Set *pnOut to
 1.71056 +** the value read.
 1.71057 +*/
 1.71058 +static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
 1.71059 +  int iBuf;
 1.71060 +
 1.71061 +  iBuf = p->iReadOff % p->nBuffer;
 1.71062 +  if( iBuf && (p->nBuffer-iBuf)>=9 ){
 1.71063 +    p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
 1.71064 +  }else{
 1.71065 +    u8 aVarint[16], *a;
 1.71066 +    int i = 0, rc;
 1.71067 +    do{
 1.71068 +      rc = vdbeSorterIterRead(db, p, 1, &a);
 1.71069 +      if( rc ) return rc;
 1.71070 +      aVarint[(i++)&0xf] = a[0];
 1.71071 +    }while( (a[0]&0x80)!=0 );
 1.71072 +    sqlite3GetVarint(aVarint, pnOut);
 1.71073 +  }
 1.71074 +
 1.71075 +  return SQLITE_OK;
 1.71076 +}
 1.71077 +
 1.71078 +
 1.71079 +/*
 1.71080 +** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
 1.71081 +** no error occurs, or an SQLite error code if one does.
 1.71082 +*/
 1.71083 +static int vdbeSorterIterNext(
 1.71084 +  sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
 1.71085 +  VdbeSorterIter *pIter           /* Iterator to advance */
 1.71086 +){
 1.71087 +  int rc;                         /* Return Code */
 1.71088 +  u64 nRec = 0;                   /* Size of record in bytes */
 1.71089 +
 1.71090 +  if( pIter->iReadOff>=pIter->iEof ){
 1.71091 +    /* This is an EOF condition */
 1.71092 +    vdbeSorterIterZero(db, pIter);
 1.71093 +    return SQLITE_OK;
 1.71094 +  }
 1.71095 +
 1.71096 +  rc = vdbeSorterIterVarint(db, pIter, &nRec);
 1.71097 +  if( rc==SQLITE_OK ){
 1.71098 +    pIter->nKey = (int)nRec;
 1.71099 +    rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
 1.71100 +  }
 1.71101 +
 1.71102 +  return rc;
 1.71103 +}
 1.71104 +
 1.71105 +/*
 1.71106 +** Initialize iterator pIter to scan through the PMA stored in file pFile
 1.71107 +** starting at offset iStart and ending at offset iEof-1. This function 
 1.71108 +** leaves the iterator pointing to the first key in the PMA (or EOF if the 
 1.71109 +** PMA is empty).
 1.71110 +*/
 1.71111 +static int vdbeSorterIterInit(
 1.71112 +  sqlite3 *db,                    /* Database handle */
 1.71113 +  const VdbeSorter *pSorter,      /* Sorter object */
 1.71114 +  i64 iStart,                     /* Start offset in pFile */
 1.71115 +  VdbeSorterIter *pIter,          /* Iterator to populate */
 1.71116 +  i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
 1.71117 +){
 1.71118 +  int rc = SQLITE_OK;
 1.71119 +  int nBuf;
 1.71120 +
 1.71121 +  nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 1.71122 +
 1.71123 +  assert( pSorter->iWriteOff>iStart );
 1.71124 +  assert( pIter->aAlloc==0 );
 1.71125 +  assert( pIter->aBuffer==0 );
 1.71126 +  pIter->pFile = pSorter->pTemp1;
 1.71127 +  pIter->iReadOff = iStart;
 1.71128 +  pIter->nAlloc = 128;
 1.71129 +  pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
 1.71130 +  pIter->nBuffer = nBuf;
 1.71131 +  pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 1.71132 +
 1.71133 +  if( !pIter->aBuffer ){
 1.71134 +    rc = SQLITE_NOMEM;
 1.71135 +  }else{
 1.71136 +    int iBuf;
 1.71137 +
 1.71138 +    iBuf = iStart % nBuf;
 1.71139 +    if( iBuf ){
 1.71140 +      int nRead = nBuf - iBuf;
 1.71141 +      if( (iStart + nRead) > pSorter->iWriteOff ){
 1.71142 +        nRead = (int)(pSorter->iWriteOff - iStart);
 1.71143 +      }
 1.71144 +      rc = sqlite3OsRead(
 1.71145 +          pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
 1.71146 +      );
 1.71147 +      assert( rc!=SQLITE_IOERR_SHORT_READ );
 1.71148 +    }
 1.71149 +
 1.71150 +    if( rc==SQLITE_OK ){
 1.71151 +      u64 nByte;                       /* Size of PMA in bytes */
 1.71152 +      pIter->iEof = pSorter->iWriteOff;
 1.71153 +      rc = vdbeSorterIterVarint(db, pIter, &nByte);
 1.71154 +      pIter->iEof = pIter->iReadOff + nByte;
 1.71155 +      *pnByte += nByte;
 1.71156 +    }
 1.71157 +  }
 1.71158 +
 1.71159 +  if( rc==SQLITE_OK ){
 1.71160 +    rc = vdbeSorterIterNext(db, pIter);
 1.71161 +  }
 1.71162 +  return rc;
 1.71163 +}
 1.71164 +
 1.71165 +
 1.71166 +/*
 1.71167 +** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
 1.71168 +** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
 1.71169 +** used by the comparison. If an error occurs, return an SQLite error code.
 1.71170 +** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
 1.71171 +** value, depending on whether key1 is smaller, equal to or larger than key2.
 1.71172 +**
 1.71173 +** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
 1.71174 +** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
 1.71175 +** is true and key1 contains even a single NULL value, it is considered to
 1.71176 +** be less than key2. Even if key2 also contains NULL values.
 1.71177 +**
 1.71178 +** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
 1.71179 +** has been allocated and contains an unpacked record that is used as key2.
 1.71180 +*/
 1.71181 +static void vdbeSorterCompare(
 1.71182 +  const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
 1.71183 +  int bOmitRowid,                 /* Ignore rowid field at end of keys */
 1.71184 +  const void *pKey1, int nKey1,   /* Left side of comparison */
 1.71185 +  const void *pKey2, int nKey2,   /* Right side of comparison */
 1.71186 +  int *pRes                       /* OUT: Result of comparison */
 1.71187 +){
 1.71188 +  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
 1.71189 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71190 +  UnpackedRecord *r2 = pSorter->pUnpacked;
 1.71191 +  int i;
 1.71192 +
 1.71193 +  if( pKey2 ){
 1.71194 +    sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
 1.71195 +  }
 1.71196 +
 1.71197 +  if( bOmitRowid ){
 1.71198 +    r2->nField = pKeyInfo->nField;
 1.71199 +    assert( r2->nField>0 );
 1.71200 +    for(i=0; i<r2->nField; i++){
 1.71201 +      if( r2->aMem[i].flags & MEM_Null ){
 1.71202 +        *pRes = -1;
 1.71203 +        return;
 1.71204 +      }
 1.71205 +    }
 1.71206 +    r2->flags |= UNPACKED_PREFIX_MATCH;
 1.71207 +  }
 1.71208 +
 1.71209 +  *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
 1.71210 +}
 1.71211 +
 1.71212 +/*
 1.71213 +** This function is called to compare two iterator keys when merging 
 1.71214 +** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
 1.71215 +** value to recalculate.
 1.71216 +*/
 1.71217 +static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
 1.71218 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71219 +  int i1;
 1.71220 +  int i2;
 1.71221 +  int iRes;
 1.71222 +  VdbeSorterIter *p1;
 1.71223 +  VdbeSorterIter *p2;
 1.71224 +
 1.71225 +  assert( iOut<pSorter->nTree && iOut>0 );
 1.71226 +
 1.71227 +  if( iOut>=(pSorter->nTree/2) ){
 1.71228 +    i1 = (iOut - pSorter->nTree/2) * 2;
 1.71229 +    i2 = i1 + 1;
 1.71230 +  }else{
 1.71231 +    i1 = pSorter->aTree[iOut*2];
 1.71232 +    i2 = pSorter->aTree[iOut*2+1];
 1.71233 +  }
 1.71234 +
 1.71235 +  p1 = &pSorter->aIter[i1];
 1.71236 +  p2 = &pSorter->aIter[i2];
 1.71237 +
 1.71238 +  if( p1->pFile==0 ){
 1.71239 +    iRes = i2;
 1.71240 +  }else if( p2->pFile==0 ){
 1.71241 +    iRes = i1;
 1.71242 +  }else{
 1.71243 +    int res;
 1.71244 +    assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
 1.71245 +    vdbeSorterCompare(
 1.71246 +        pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
 1.71247 +    );
 1.71248 +    if( res<=0 ){
 1.71249 +      iRes = i1;
 1.71250 +    }else{
 1.71251 +      iRes = i2;
 1.71252 +    }
 1.71253 +  }
 1.71254 +
 1.71255 +  pSorter->aTree[iOut] = iRes;
 1.71256 +  return SQLITE_OK;
 1.71257 +}
 1.71258 +
 1.71259 +/*
 1.71260 +** Initialize the temporary index cursor just opened as a sorter cursor.
 1.71261 +*/
 1.71262 +SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
 1.71263 +  int pgsz;                       /* Page size of main database */
 1.71264 +  int mxCache;                    /* Cache size */
 1.71265 +  VdbeSorter *pSorter;            /* The new sorter */
 1.71266 +  char *d;                        /* Dummy */
 1.71267 +
 1.71268 +  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
 1.71269 +  pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
 1.71270 +  if( pSorter==0 ){
 1.71271 +    return SQLITE_NOMEM;
 1.71272 +  }
 1.71273 +  
 1.71274 +  pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
 1.71275 +  if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
 1.71276 +  assert( pSorter->pUnpacked==(UnpackedRecord *)d );
 1.71277 +
 1.71278 +  if( !sqlite3TempInMemory(db) ){
 1.71279 +    pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 1.71280 +    pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
 1.71281 +    mxCache = db->aDb[0].pSchema->cache_size;
 1.71282 +    if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
 1.71283 +    pSorter->mxPmaSize = mxCache * pgsz;
 1.71284 +  }
 1.71285 +
 1.71286 +  return SQLITE_OK;
 1.71287 +}
 1.71288 +
 1.71289 +/*
 1.71290 +** Free the list of sorted records starting at pRecord.
 1.71291 +*/
 1.71292 +static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
 1.71293 +  SorterRecord *p;
 1.71294 +  SorterRecord *pNext;
 1.71295 +  for(p=pRecord; p; p=pNext){
 1.71296 +    pNext = p->pNext;
 1.71297 +    sqlite3DbFree(db, p);
 1.71298 +  }
 1.71299 +}
 1.71300 +
 1.71301 +/*
 1.71302 +** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
 1.71303 +*/
 1.71304 +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
 1.71305 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71306 +  if( pSorter ){
 1.71307 +    if( pSorter->aIter ){
 1.71308 +      int i;
 1.71309 +      for(i=0; i<pSorter->nTree; i++){
 1.71310 +        vdbeSorterIterZero(db, &pSorter->aIter[i]);
 1.71311 +      }
 1.71312 +      sqlite3DbFree(db, pSorter->aIter);
 1.71313 +    }
 1.71314 +    if( pSorter->pTemp1 ){
 1.71315 +      sqlite3OsCloseFree(pSorter->pTemp1);
 1.71316 +    }
 1.71317 +    vdbeSorterRecordFree(db, pSorter->pRecord);
 1.71318 +    sqlite3DbFree(db, pSorter->pUnpacked);
 1.71319 +    sqlite3DbFree(db, pSorter);
 1.71320 +    pCsr->pSorter = 0;
 1.71321 +  }
 1.71322 +}
 1.71323 +
 1.71324 +/*
 1.71325 +** Allocate space for a file-handle and open a temporary file. If successful,
 1.71326 +** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
 1.71327 +** Otherwise, set *ppFile to 0 and return an SQLite error code.
 1.71328 +*/
 1.71329 +static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
 1.71330 +  int dummy;
 1.71331 +  return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
 1.71332 +      SQLITE_OPEN_TEMP_JOURNAL |
 1.71333 +      SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
 1.71334 +      SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
 1.71335 +  );
 1.71336 +}
 1.71337 +
 1.71338 +/*
 1.71339 +** Merge the two sorted lists p1 and p2 into a single list.
 1.71340 +** Set *ppOut to the head of the new list.
 1.71341 +*/
 1.71342 +static void vdbeSorterMerge(
 1.71343 +  const VdbeCursor *pCsr,         /* For pKeyInfo */
 1.71344 +  SorterRecord *p1,               /* First list to merge */
 1.71345 +  SorterRecord *p2,               /* Second list to merge */
 1.71346 +  SorterRecord **ppOut            /* OUT: Head of merged list */
 1.71347 +){
 1.71348 +  SorterRecord *pFinal = 0;
 1.71349 +  SorterRecord **pp = &pFinal;
 1.71350 +  void *pVal2 = p2 ? p2->pVal : 0;
 1.71351 +
 1.71352 +  while( p1 && p2 ){
 1.71353 +    int res;
 1.71354 +    vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
 1.71355 +    if( res<=0 ){
 1.71356 +      *pp = p1;
 1.71357 +      pp = &p1->pNext;
 1.71358 +      p1 = p1->pNext;
 1.71359 +      pVal2 = 0;
 1.71360 +    }else{
 1.71361 +      *pp = p2;
 1.71362 +       pp = &p2->pNext;
 1.71363 +      p2 = p2->pNext;
 1.71364 +      if( p2==0 ) break;
 1.71365 +      pVal2 = p2->pVal;
 1.71366 +    }
 1.71367 +  }
 1.71368 +  *pp = p1 ? p1 : p2;
 1.71369 +  *ppOut = pFinal;
 1.71370 +}
 1.71371 +
 1.71372 +/*
 1.71373 +** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
 1.71374 +** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
 1.71375 +** occurs.
 1.71376 +*/
 1.71377 +static int vdbeSorterSort(const VdbeCursor *pCsr){
 1.71378 +  int i;
 1.71379 +  SorterRecord **aSlot;
 1.71380 +  SorterRecord *p;
 1.71381 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71382 +
 1.71383 +  aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
 1.71384 +  if( !aSlot ){
 1.71385 +    return SQLITE_NOMEM;
 1.71386 +  }
 1.71387 +
 1.71388 +  p = pSorter->pRecord;
 1.71389 +  while( p ){
 1.71390 +    SorterRecord *pNext = p->pNext;
 1.71391 +    p->pNext = 0;
 1.71392 +    for(i=0; aSlot[i]; i++){
 1.71393 +      vdbeSorterMerge(pCsr, p, aSlot[i], &p);
 1.71394 +      aSlot[i] = 0;
 1.71395 +    }
 1.71396 +    aSlot[i] = p;
 1.71397 +    p = pNext;
 1.71398 +  }
 1.71399 +
 1.71400 +  p = 0;
 1.71401 +  for(i=0; i<64; i++){
 1.71402 +    vdbeSorterMerge(pCsr, p, aSlot[i], &p);
 1.71403 +  }
 1.71404 +  pSorter->pRecord = p;
 1.71405 +
 1.71406 +  sqlite3_free(aSlot);
 1.71407 +  return SQLITE_OK;
 1.71408 +}
 1.71409 +
 1.71410 +/*
 1.71411 +** Initialize a file-writer object.
 1.71412 +*/
 1.71413 +static void fileWriterInit(
 1.71414 +  sqlite3 *db,                    /* Database (for malloc) */
 1.71415 +  sqlite3_file *pFile,            /* File to write to */
 1.71416 +  FileWriter *p,                  /* Object to populate */
 1.71417 +  i64 iStart                      /* Offset of pFile to begin writing at */
 1.71418 +){
 1.71419 +  int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 1.71420 +
 1.71421 +  memset(p, 0, sizeof(FileWriter));
 1.71422 +  p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 1.71423 +  if( !p->aBuffer ){
 1.71424 +    p->eFWErr = SQLITE_NOMEM;
 1.71425 +  }else{
 1.71426 +    p->iBufEnd = p->iBufStart = (iStart % nBuf);
 1.71427 +    p->iWriteOff = iStart - p->iBufStart;
 1.71428 +    p->nBuffer = nBuf;
 1.71429 +    p->pFile = pFile;
 1.71430 +  }
 1.71431 +}
 1.71432 +
 1.71433 +/*
 1.71434 +** Write nData bytes of data to the file-write object. Return SQLITE_OK
 1.71435 +** if successful, or an SQLite error code if an error occurs.
 1.71436 +*/
 1.71437 +static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
 1.71438 +  int nRem = nData;
 1.71439 +  while( nRem>0 && p->eFWErr==0 ){
 1.71440 +    int nCopy = nRem;
 1.71441 +    if( nCopy>(p->nBuffer - p->iBufEnd) ){
 1.71442 +      nCopy = p->nBuffer - p->iBufEnd;
 1.71443 +    }
 1.71444 +
 1.71445 +    memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
 1.71446 +    p->iBufEnd += nCopy;
 1.71447 +    if( p->iBufEnd==p->nBuffer ){
 1.71448 +      p->eFWErr = sqlite3OsWrite(p->pFile, 
 1.71449 +          &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 1.71450 +          p->iWriteOff + p->iBufStart
 1.71451 +      );
 1.71452 +      p->iBufStart = p->iBufEnd = 0;
 1.71453 +      p->iWriteOff += p->nBuffer;
 1.71454 +    }
 1.71455 +    assert( p->iBufEnd<p->nBuffer );
 1.71456 +
 1.71457 +    nRem -= nCopy;
 1.71458 +  }
 1.71459 +}
 1.71460 +
 1.71461 +/*
 1.71462 +** Flush any buffered data to disk and clean up the file-writer object.
 1.71463 +** The results of using the file-writer after this call are undefined.
 1.71464 +** Return SQLITE_OK if flushing the buffered data succeeds or is not 
 1.71465 +** required. Otherwise, return an SQLite error code.
 1.71466 +**
 1.71467 +** Before returning, set *piEof to the offset immediately following the
 1.71468 +** last byte written to the file.
 1.71469 +*/
 1.71470 +static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
 1.71471 +  int rc;
 1.71472 +  if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
 1.71473 +    p->eFWErr = sqlite3OsWrite(p->pFile, 
 1.71474 +        &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 1.71475 +        p->iWriteOff + p->iBufStart
 1.71476 +    );
 1.71477 +  }
 1.71478 +  *piEof = (p->iWriteOff + p->iBufEnd);
 1.71479 +  sqlite3DbFree(db, p->aBuffer);
 1.71480 +  rc = p->eFWErr;
 1.71481 +  memset(p, 0, sizeof(FileWriter));
 1.71482 +  return rc;
 1.71483 +}
 1.71484 +
 1.71485 +/*
 1.71486 +** Write value iVal encoded as a varint to the file-write object. Return 
 1.71487 +** SQLITE_OK if successful, or an SQLite error code if an error occurs.
 1.71488 +*/
 1.71489 +static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
 1.71490 +  int nByte; 
 1.71491 +  u8 aByte[10];
 1.71492 +  nByte = sqlite3PutVarint(aByte, iVal);
 1.71493 +  fileWriterWrite(p, aByte, nByte);
 1.71494 +}
 1.71495 +
 1.71496 +/*
 1.71497 +** Write the current contents of the in-memory linked-list to a PMA. Return
 1.71498 +** SQLITE_OK if successful, or an SQLite error code otherwise.
 1.71499 +**
 1.71500 +** The format of a PMA is:
 1.71501 +**
 1.71502 +**     * A varint. This varint contains the total number of bytes of content
 1.71503 +**       in the PMA (not including the varint itself).
 1.71504 +**
 1.71505 +**     * One or more records packed end-to-end in order of ascending keys. 
 1.71506 +**       Each record consists of a varint followed by a blob of data (the 
 1.71507 +**       key). The varint is the number of bytes in the blob of data.
 1.71508 +*/
 1.71509 +static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
 1.71510 +  int rc = SQLITE_OK;             /* Return code */
 1.71511 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71512 +  FileWriter writer;
 1.71513 +
 1.71514 +  memset(&writer, 0, sizeof(FileWriter));
 1.71515 +
 1.71516 +  if( pSorter->nInMemory==0 ){
 1.71517 +    assert( pSorter->pRecord==0 );
 1.71518 +    return rc;
 1.71519 +  }
 1.71520 +
 1.71521 +  rc = vdbeSorterSort(pCsr);
 1.71522 +
 1.71523 +  /* If the first temporary PMA file has not been opened, open it now. */
 1.71524 +  if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
 1.71525 +    rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
 1.71526 +    assert( rc!=SQLITE_OK || pSorter->pTemp1 );
 1.71527 +    assert( pSorter->iWriteOff==0 );
 1.71528 +    assert( pSorter->nPMA==0 );
 1.71529 +  }
 1.71530 +
 1.71531 +  if( rc==SQLITE_OK ){
 1.71532 +    SorterRecord *p;
 1.71533 +    SorterRecord *pNext = 0;
 1.71534 +
 1.71535 +    fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
 1.71536 +    pSorter->nPMA++;
 1.71537 +    fileWriterWriteVarint(&writer, pSorter->nInMemory);
 1.71538 +    for(p=pSorter->pRecord; p; p=pNext){
 1.71539 +      pNext = p->pNext;
 1.71540 +      fileWriterWriteVarint(&writer, p->nVal);
 1.71541 +      fileWriterWrite(&writer, p->pVal, p->nVal);
 1.71542 +      sqlite3DbFree(db, p);
 1.71543 +    }
 1.71544 +    pSorter->pRecord = p;
 1.71545 +    rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
 1.71546 +  }
 1.71547 +
 1.71548 +  return rc;
 1.71549 +}
 1.71550 +
 1.71551 +/*
 1.71552 +** Add a record to the sorter.
 1.71553 +*/
 1.71554 +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
 1.71555 +  sqlite3 *db,                    /* Database handle */
 1.71556 +  const VdbeCursor *pCsr,               /* Sorter cursor */
 1.71557 +  Mem *pVal                       /* Memory cell containing record */
 1.71558 +){
 1.71559 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71560 +  int rc = SQLITE_OK;             /* Return Code */
 1.71561 +  SorterRecord *pNew;             /* New list element */
 1.71562 +
 1.71563 +  assert( pSorter );
 1.71564 +  pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
 1.71565 +
 1.71566 +  pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
 1.71567 +  if( pNew==0 ){
 1.71568 +    rc = SQLITE_NOMEM;
 1.71569 +  }else{
 1.71570 +    pNew->pVal = (void *)&pNew[1];
 1.71571 +    memcpy(pNew->pVal, pVal->z, pVal->n);
 1.71572 +    pNew->nVal = pVal->n;
 1.71573 +    pNew->pNext = pSorter->pRecord;
 1.71574 +    pSorter->pRecord = pNew;
 1.71575 +  }
 1.71576 +
 1.71577 +  /* See if the contents of the sorter should now be written out. They
 1.71578 +  ** are written out when either of the following are true:
 1.71579 +  **
 1.71580 +  **   * The total memory allocated for the in-memory list is greater 
 1.71581 +  **     than (page-size * cache-size), or
 1.71582 +  **
 1.71583 +  **   * The total memory allocated for the in-memory list is greater 
 1.71584 +  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
 1.71585 +  */
 1.71586 +  if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
 1.71587 +        (pSorter->nInMemory>pSorter->mxPmaSize)
 1.71588 +     || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
 1.71589 +  )){
 1.71590 +#ifdef SQLITE_DEBUG
 1.71591 +    i64 nExpect = pSorter->iWriteOff
 1.71592 +                + sqlite3VarintLen(pSorter->nInMemory)
 1.71593 +                + pSorter->nInMemory;
 1.71594 +#endif
 1.71595 +    rc = vdbeSorterListToPMA(db, pCsr);
 1.71596 +    pSorter->nInMemory = 0;
 1.71597 +    assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
 1.71598 +  }
 1.71599 +
 1.71600 +  return rc;
 1.71601 +}
 1.71602 +
 1.71603 +/*
 1.71604 +** Helper function for sqlite3VdbeSorterRewind(). 
 1.71605 +*/
 1.71606 +static int vdbeSorterInitMerge(
 1.71607 +  sqlite3 *db,                    /* Database handle */
 1.71608 +  const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
 1.71609 +  i64 *pnByte                     /* Sum of bytes in all opened PMAs */
 1.71610 +){
 1.71611 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71612 +  int rc = SQLITE_OK;             /* Return code */
 1.71613 +  int i;                          /* Used to iterator through aIter[] */
 1.71614 +  i64 nByte = 0;                  /* Total bytes in all opened PMAs */
 1.71615 +
 1.71616 +  /* Initialize the iterators. */
 1.71617 +  for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
 1.71618 +    VdbeSorterIter *pIter = &pSorter->aIter[i];
 1.71619 +    rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
 1.71620 +    pSorter->iReadOff = pIter->iEof;
 1.71621 +    assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
 1.71622 +    if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
 1.71623 +  }
 1.71624 +
 1.71625 +  /* Initialize the aTree[] array. */
 1.71626 +  for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
 1.71627 +    rc = vdbeSorterDoCompare(pCsr, i);
 1.71628 +  }
 1.71629 +
 1.71630 +  *pnByte = nByte;
 1.71631 +  return rc;
 1.71632 +}
 1.71633 +
 1.71634 +/*
 1.71635 +** Once the sorter has been populated, this function is called to prepare
 1.71636 +** for iterating through its contents in sorted order.
 1.71637 +*/
 1.71638 +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 1.71639 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71640 +  int rc;                         /* Return code */
 1.71641 +  sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
 1.71642 +  i64 iWrite2 = 0;                /* Write offset for pTemp2 */
 1.71643 +  int nIter;                      /* Number of iterators used */
 1.71644 +  int nByte;                      /* Bytes of space required for aIter/aTree */
 1.71645 +  int N = 2;                      /* Power of 2 >= nIter */
 1.71646 +
 1.71647 +  assert( pSorter );
 1.71648 +
 1.71649 +  /* If no data has been written to disk, then do not do so now. Instead,
 1.71650 +  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
 1.71651 +  ** from the in-memory list.  */
 1.71652 +  if( pSorter->nPMA==0 ){
 1.71653 +    *pbEof = !pSorter->pRecord;
 1.71654 +    assert( pSorter->aTree==0 );
 1.71655 +    return vdbeSorterSort(pCsr);
 1.71656 +  }
 1.71657 +
 1.71658 +  /* Write the current in-memory list to a PMA. */
 1.71659 +  rc = vdbeSorterListToPMA(db, pCsr);
 1.71660 +  if( rc!=SQLITE_OK ) return rc;
 1.71661 +
 1.71662 +  /* Allocate space for aIter[] and aTree[]. */
 1.71663 +  nIter = pSorter->nPMA;
 1.71664 +  if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
 1.71665 +  assert( nIter>0 );
 1.71666 +  while( N<nIter ) N += N;
 1.71667 +  nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
 1.71668 +  pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
 1.71669 +  if( !pSorter->aIter ) return SQLITE_NOMEM;
 1.71670 +  pSorter->aTree = (int *)&pSorter->aIter[N];
 1.71671 +  pSorter->nTree = N;
 1.71672 +
 1.71673 +  do {
 1.71674 +    int iNew;                     /* Index of new, merged, PMA */
 1.71675 +
 1.71676 +    for(iNew=0; 
 1.71677 +        rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
 1.71678 +        iNew++
 1.71679 +    ){
 1.71680 +      int rc2;                    /* Return code from fileWriterFinish() */
 1.71681 +      FileWriter writer;          /* Object used to write to disk */
 1.71682 +      i64 nWrite;                 /* Number of bytes in new PMA */
 1.71683 +
 1.71684 +      memset(&writer, 0, sizeof(FileWriter));
 1.71685 +
 1.71686 +      /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
 1.71687 +      ** initialize an iterator for each of them and break out of the loop.
 1.71688 +      ** These iterators will be incrementally merged as the VDBE layer calls
 1.71689 +      ** sqlite3VdbeSorterNext().
 1.71690 +      **
 1.71691 +      ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
 1.71692 +      ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
 1.71693 +      ** are merged into a single PMA that is written to file pTemp2.
 1.71694 +      */
 1.71695 +      rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
 1.71696 +      assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
 1.71697 +      if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
 1.71698 +        break;
 1.71699 +      }
 1.71700 +
 1.71701 +      /* Open the second temp file, if it is not already open. */
 1.71702 +      if( pTemp2==0 ){
 1.71703 +        assert( iWrite2==0 );
 1.71704 +        rc = vdbeSorterOpenTempFile(db, &pTemp2);
 1.71705 +      }
 1.71706 +
 1.71707 +      if( rc==SQLITE_OK ){
 1.71708 +        int bEof = 0;
 1.71709 +        fileWriterInit(db, pTemp2, &writer, iWrite2);
 1.71710 +        fileWriterWriteVarint(&writer, nWrite);
 1.71711 +        while( rc==SQLITE_OK && bEof==0 ){
 1.71712 +          VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 1.71713 +          assert( pIter->pFile );
 1.71714 +
 1.71715 +          fileWriterWriteVarint(&writer, pIter->nKey);
 1.71716 +          fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
 1.71717 +          rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
 1.71718 +        }
 1.71719 +        rc2 = fileWriterFinish(db, &writer, &iWrite2);
 1.71720 +        if( rc==SQLITE_OK ) rc = rc2;
 1.71721 +      }
 1.71722 +    }
 1.71723 +
 1.71724 +    if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
 1.71725 +      break;
 1.71726 +    }else{
 1.71727 +      sqlite3_file *pTmp = pSorter->pTemp1;
 1.71728 +      pSorter->nPMA = iNew;
 1.71729 +      pSorter->pTemp1 = pTemp2;
 1.71730 +      pTemp2 = pTmp;
 1.71731 +      pSorter->iWriteOff = iWrite2;
 1.71732 +      pSorter->iReadOff = 0;
 1.71733 +      iWrite2 = 0;
 1.71734 +    }
 1.71735 +  }while( rc==SQLITE_OK );
 1.71736 +
 1.71737 +  if( pTemp2 ){
 1.71738 +    sqlite3OsCloseFree(pTemp2);
 1.71739 +  }
 1.71740 +  *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 1.71741 +  return rc;
 1.71742 +}
 1.71743 +
 1.71744 +/*
 1.71745 +** Advance to the next element in the sorter.
 1.71746 +*/
 1.71747 +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 1.71748 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71749 +  int rc;                         /* Return code */
 1.71750 +
 1.71751 +  if( pSorter->aTree ){
 1.71752 +    int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
 1.71753 +    int i;                        /* Index of aTree[] to recalculate */
 1.71754 +
 1.71755 +    rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
 1.71756 +    for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
 1.71757 +      rc = vdbeSorterDoCompare(pCsr, i);
 1.71758 +    }
 1.71759 +
 1.71760 +    *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 1.71761 +  }else{
 1.71762 +    SorterRecord *pFree = pSorter->pRecord;
 1.71763 +    pSorter->pRecord = pFree->pNext;
 1.71764 +    pFree->pNext = 0;
 1.71765 +    vdbeSorterRecordFree(db, pFree);
 1.71766 +    *pbEof = !pSorter->pRecord;
 1.71767 +    rc = SQLITE_OK;
 1.71768 +  }
 1.71769 +  return rc;
 1.71770 +}
 1.71771 +
 1.71772 +/*
 1.71773 +** Return a pointer to a buffer owned by the sorter that contains the 
 1.71774 +** current key.
 1.71775 +*/
 1.71776 +static void *vdbeSorterRowkey(
 1.71777 +  const VdbeSorter *pSorter,      /* Sorter object */
 1.71778 +  int *pnKey                      /* OUT: Size of current key in bytes */
 1.71779 +){
 1.71780 +  void *pKey;
 1.71781 +  if( pSorter->aTree ){
 1.71782 +    VdbeSorterIter *pIter;
 1.71783 +    pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 1.71784 +    *pnKey = pIter->nKey;
 1.71785 +    pKey = pIter->aKey;
 1.71786 +  }else{
 1.71787 +    *pnKey = pSorter->pRecord->nVal;
 1.71788 +    pKey = pSorter->pRecord->pVal;
 1.71789 +  }
 1.71790 +  return pKey;
 1.71791 +}
 1.71792 +
 1.71793 +/*
 1.71794 +** Copy the current sorter key into the memory cell pOut.
 1.71795 +*/
 1.71796 +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
 1.71797 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71798 +  void *pKey; int nKey;           /* Sorter key to copy into pOut */
 1.71799 +
 1.71800 +  pKey = vdbeSorterRowkey(pSorter, &nKey);
 1.71801 +  if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
 1.71802 +    return SQLITE_NOMEM;
 1.71803 +  }
 1.71804 +  pOut->n = nKey;
 1.71805 +  MemSetTypeFlag(pOut, MEM_Blob);
 1.71806 +  memcpy(pOut->z, pKey, nKey);
 1.71807 +
 1.71808 +  return SQLITE_OK;
 1.71809 +}
 1.71810 +
 1.71811 +/*
 1.71812 +** Compare the key in memory cell pVal with the key that the sorter cursor
 1.71813 +** passed as the first argument currently points to. For the purposes of
 1.71814 +** the comparison, ignore the rowid field at the end of each record.
 1.71815 +**
 1.71816 +** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
 1.71817 +** Otherwise, set *pRes to a negative, zero or positive value if the
 1.71818 +** key in pVal is smaller than, equal to or larger than the current sorter
 1.71819 +** key.
 1.71820 +*/
 1.71821 +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
 1.71822 +  const VdbeCursor *pCsr,         /* Sorter cursor */
 1.71823 +  Mem *pVal,                      /* Value to compare to current sorter key */
 1.71824 +  int *pRes                       /* OUT: Result of comparison */
 1.71825 +){
 1.71826 +  VdbeSorter *pSorter = pCsr->pSorter;
 1.71827 +  void *pKey; int nKey;           /* Sorter key to compare pVal with */
 1.71828 +
 1.71829 +  pKey = vdbeSorterRowkey(pSorter, &nKey);
 1.71830 +  vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
 1.71831 +  return SQLITE_OK;
 1.71832 +}
 1.71833 +
 1.71834 +#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
 1.71835 +
 1.71836 +/************** End of vdbesort.c ********************************************/
 1.71837 +/************** Begin file journal.c *****************************************/
 1.71838 +/*
 1.71839 +** 2007 August 22
 1.71840 +**
 1.71841 +** The author disclaims copyright to this source code.  In place of
 1.71842 +** a legal notice, here is a blessing:
 1.71843 +**
 1.71844 +**    May you do good and not evil.
 1.71845 +**    May you find forgiveness for yourself and forgive others.
 1.71846 +**    May you share freely, never taking more than you give.
 1.71847 +**
 1.71848 +*************************************************************************
 1.71849 +**
 1.71850 +** This file implements a special kind of sqlite3_file object used
 1.71851 +** by SQLite to create journal files if the atomic-write optimization
 1.71852 +** is enabled.
 1.71853 +**
 1.71854 +** The distinctive characteristic of this sqlite3_file is that the
 1.71855 +** actual on disk file is created lazily. When the file is created,
 1.71856 +** the caller specifies a buffer size for an in-memory buffer to
 1.71857 +** be used to service read() and write() requests. The actual file
 1.71858 +** on disk is not created or populated until either:
 1.71859 +**
 1.71860 +**   1) The in-memory representation grows too large for the allocated 
 1.71861 +**      buffer, or
 1.71862 +**   2) The sqlite3JournalCreate() function is called.
 1.71863 +*/
 1.71864 +#ifdef SQLITE_ENABLE_ATOMIC_WRITE
 1.71865 +
 1.71866 +
 1.71867 +/*
 1.71868 +** A JournalFile object is a subclass of sqlite3_file used by
 1.71869 +** as an open file handle for journal files.
 1.71870 +*/
 1.71871 +struct JournalFile {
 1.71872 +  sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
 1.71873 +  int nBuf;                       /* Size of zBuf[] in bytes */
 1.71874 +  char *zBuf;                     /* Space to buffer journal writes */
 1.71875 +  int iSize;                      /* Amount of zBuf[] currently used */
 1.71876 +  int flags;                      /* xOpen flags */
 1.71877 +  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
 1.71878 +  sqlite3_file *pReal;            /* The "real" underlying file descriptor */
 1.71879 +  const char *zJournal;           /* Name of the journal file */
 1.71880 +};
 1.71881 +typedef struct JournalFile JournalFile;
 1.71882 +
 1.71883 +/*
 1.71884 +** If it does not already exists, create and populate the on-disk file 
 1.71885 +** for JournalFile p.
 1.71886 +*/
 1.71887 +static int createFile(JournalFile *p){
 1.71888 +  int rc = SQLITE_OK;
 1.71889 +  if( !p->pReal ){
 1.71890 +    sqlite3_file *pReal = (sqlite3_file *)&p[1];
 1.71891 +    rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
 1.71892 +    if( rc==SQLITE_OK ){
 1.71893 +      p->pReal = pReal;
 1.71894 +      if( p->iSize>0 ){
 1.71895 +        assert(p->iSize<=p->nBuf);
 1.71896 +        rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
 1.71897 +      }
 1.71898 +    }
 1.71899 +  }
 1.71900 +  return rc;
 1.71901 +}
 1.71902 +
 1.71903 +/*
 1.71904 +** Close the file.
 1.71905 +*/
 1.71906 +static int jrnlClose(sqlite3_file *pJfd){
 1.71907 +  JournalFile *p = (JournalFile *)pJfd;
 1.71908 +  if( p->pReal ){
 1.71909 +    sqlite3OsClose(p->pReal);
 1.71910 +  }
 1.71911 +  sqlite3_free(p->zBuf);
 1.71912 +  return SQLITE_OK;
 1.71913 +}
 1.71914 +
 1.71915 +/*
 1.71916 +** Read data from the file.
 1.71917 +*/
 1.71918 +static int jrnlRead(
 1.71919 +  sqlite3_file *pJfd,    /* The journal file from which to read */
 1.71920 +  void *zBuf,            /* Put the results here */
 1.71921 +  int iAmt,              /* Number of bytes to read */
 1.71922 +  sqlite_int64 iOfst     /* Begin reading at this offset */
 1.71923 +){
 1.71924 +  int rc = SQLITE_OK;
 1.71925 +  JournalFile *p = (JournalFile *)pJfd;
 1.71926 +  if( p->pReal ){
 1.71927 +    rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
 1.71928 +  }else if( (iAmt+iOfst)>p->iSize ){
 1.71929 +    rc = SQLITE_IOERR_SHORT_READ;
 1.71930 +  }else{
 1.71931 +    memcpy(zBuf, &p->zBuf[iOfst], iAmt);
 1.71932 +  }
 1.71933 +  return rc;
 1.71934 +}
 1.71935 +
 1.71936 +/*
 1.71937 +** Write data to the file.
 1.71938 +*/
 1.71939 +static int jrnlWrite(
 1.71940 +  sqlite3_file *pJfd,    /* The journal file into which to write */
 1.71941 +  const void *zBuf,      /* Take data to be written from here */
 1.71942 +  int iAmt,              /* Number of bytes to write */
 1.71943 +  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
 1.71944 +){
 1.71945 +  int rc = SQLITE_OK;
 1.71946 +  JournalFile *p = (JournalFile *)pJfd;
 1.71947 +  if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
 1.71948 +    rc = createFile(p);
 1.71949 +  }
 1.71950 +  if( rc==SQLITE_OK ){
 1.71951 +    if( p->pReal ){
 1.71952 +      rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
 1.71953 +    }else{
 1.71954 +      memcpy(&p->zBuf[iOfst], zBuf, iAmt);
 1.71955 +      if( p->iSize<(iOfst+iAmt) ){
 1.71956 +        p->iSize = (iOfst+iAmt);
 1.71957 +      }
 1.71958 +    }
 1.71959 +  }
 1.71960 +  return rc;
 1.71961 +}
 1.71962 +
 1.71963 +/*
 1.71964 +** Truncate the file.
 1.71965 +*/
 1.71966 +static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
 1.71967 +  int rc = SQLITE_OK;
 1.71968 +  JournalFile *p = (JournalFile *)pJfd;
 1.71969 +  if( p->pReal ){
 1.71970 +    rc = sqlite3OsTruncate(p->pReal, size);
 1.71971 +  }else if( size<p->iSize ){
 1.71972 +    p->iSize = size;
 1.71973 +  }
 1.71974 +  return rc;
 1.71975 +}
 1.71976 +
 1.71977 +/*
 1.71978 +** Sync the file.
 1.71979 +*/
 1.71980 +static int jrnlSync(sqlite3_file *pJfd, int flags){
 1.71981 +  int rc;
 1.71982 +  JournalFile *p = (JournalFile *)pJfd;
 1.71983 +  if( p->pReal ){
 1.71984 +    rc = sqlite3OsSync(p->pReal, flags);
 1.71985 +  }else{
 1.71986 +    rc = SQLITE_OK;
 1.71987 +  }
 1.71988 +  return rc;
 1.71989 +}
 1.71990 +
 1.71991 +/*
 1.71992 +** Query the size of the file in bytes.
 1.71993 +*/
 1.71994 +static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
 1.71995 +  int rc = SQLITE_OK;
 1.71996 +  JournalFile *p = (JournalFile *)pJfd;
 1.71997 +  if( p->pReal ){
 1.71998 +    rc = sqlite3OsFileSize(p->pReal, pSize);
 1.71999 +  }else{
 1.72000 +    *pSize = (sqlite_int64) p->iSize;
 1.72001 +  }
 1.72002 +  return rc;
 1.72003 +}
 1.72004 +
 1.72005 +/*
 1.72006 +** Table of methods for JournalFile sqlite3_file object.
 1.72007 +*/
 1.72008 +static struct sqlite3_io_methods JournalFileMethods = {
 1.72009 +  1,             /* iVersion */
 1.72010 +  jrnlClose,     /* xClose */
 1.72011 +  jrnlRead,      /* xRead */
 1.72012 +  jrnlWrite,     /* xWrite */
 1.72013 +  jrnlTruncate,  /* xTruncate */
 1.72014 +  jrnlSync,      /* xSync */
 1.72015 +  jrnlFileSize,  /* xFileSize */
 1.72016 +  0,             /* xLock */
 1.72017 +  0,             /* xUnlock */
 1.72018 +  0,             /* xCheckReservedLock */
 1.72019 +  0,             /* xFileControl */
 1.72020 +  0,             /* xSectorSize */
 1.72021 +  0,             /* xDeviceCharacteristics */
 1.72022 +  0,             /* xShmMap */
 1.72023 +  0,             /* xShmLock */
 1.72024 +  0,             /* xShmBarrier */
 1.72025 +  0              /* xShmUnmap */
 1.72026 +};
 1.72027 +
 1.72028 +/* 
 1.72029 +** Open a journal file.
 1.72030 +*/
 1.72031 +SQLITE_PRIVATE int sqlite3JournalOpen(
 1.72032 +  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
 1.72033 +  const char *zName,         /* Name of the journal file */
 1.72034 +  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
 1.72035 +  int flags,                 /* Opening flags */
 1.72036 +  int nBuf                   /* Bytes buffered before opening the file */
 1.72037 +){
 1.72038 +  JournalFile *p = (JournalFile *)pJfd;
 1.72039 +  memset(p, 0, sqlite3JournalSize(pVfs));
 1.72040 +  if( nBuf>0 ){
 1.72041 +    p->zBuf = sqlite3MallocZero(nBuf);
 1.72042 +    if( !p->zBuf ){
 1.72043 +      return SQLITE_NOMEM;
 1.72044 +    }
 1.72045 +  }else{
 1.72046 +    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
 1.72047 +  }
 1.72048 +  p->pMethod = &JournalFileMethods;
 1.72049 +  p->nBuf = nBuf;
 1.72050 +  p->flags = flags;
 1.72051 +  p->zJournal = zName;
 1.72052 +  p->pVfs = pVfs;
 1.72053 +  return SQLITE_OK;
 1.72054 +}
 1.72055 +
 1.72056 +/*
 1.72057 +** If the argument p points to a JournalFile structure, and the underlying
 1.72058 +** file has not yet been created, create it now.
 1.72059 +*/
 1.72060 +SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
 1.72061 +  if( p->pMethods!=&JournalFileMethods ){
 1.72062 +    return SQLITE_OK;
 1.72063 +  }
 1.72064 +  return createFile((JournalFile *)p);
 1.72065 +}
 1.72066 +
 1.72067 +/*
 1.72068 +** The file-handle passed as the only argument is guaranteed to be an open
 1.72069 +** file. It may or may not be of class JournalFile. If the file is a
 1.72070 +** JournalFile, and the underlying file on disk has not yet been opened,
 1.72071 +** return 0. Otherwise, return 1.
 1.72072 +*/
 1.72073 +SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
 1.72074 +  return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
 1.72075 +}
 1.72076 +
 1.72077 +/* 
 1.72078 +** Return the number of bytes required to store a JournalFile that uses vfs
 1.72079 +** pVfs to create the underlying on-disk files.
 1.72080 +*/
 1.72081 +SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
 1.72082 +  return (pVfs->szOsFile+sizeof(JournalFile));
 1.72083 +}
 1.72084 +#endif
 1.72085 +
 1.72086 +/************** End of journal.c *********************************************/
 1.72087 +/************** Begin file memjournal.c **************************************/
 1.72088 +/*
 1.72089 +** 2008 October 7
 1.72090 +**
 1.72091 +** The author disclaims copyright to this source code.  In place of
 1.72092 +** a legal notice, here is a blessing:
 1.72093 +**
 1.72094 +**    May you do good and not evil.
 1.72095 +**    May you find forgiveness for yourself and forgive others.
 1.72096 +**    May you share freely, never taking more than you give.
 1.72097 +**
 1.72098 +*************************************************************************
 1.72099 +**
 1.72100 +** This file contains code use to implement an in-memory rollback journal.
 1.72101 +** The in-memory rollback journal is used to journal transactions for
 1.72102 +** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
 1.72103 +*/
 1.72104 +
 1.72105 +/* Forward references to internal structures */
 1.72106 +typedef struct MemJournal MemJournal;
 1.72107 +typedef struct FilePoint FilePoint;
 1.72108 +typedef struct FileChunk FileChunk;
 1.72109 +
 1.72110 +/* Space to hold the rollback journal is allocated in increments of
 1.72111 +** this many bytes.
 1.72112 +**
 1.72113 +** The size chosen is a little less than a power of two.  That way,
 1.72114 +** the FileChunk object will have a size that almost exactly fills
 1.72115 +** a power-of-two allocation.  This mimimizes wasted space in power-of-two
 1.72116 +** memory allocators.
 1.72117 +*/
 1.72118 +#define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
 1.72119 +
 1.72120 +/* Macro to find the minimum of two numeric values.
 1.72121 +*/
 1.72122 +#ifndef MIN
 1.72123 +# define MIN(x,y) ((x)<(y)?(x):(y))
 1.72124 +#endif
 1.72125 +
 1.72126 +/*
 1.72127 +** The rollback journal is composed of a linked list of these structures.
 1.72128 +*/
 1.72129 +struct FileChunk {
 1.72130 +  FileChunk *pNext;               /* Next chunk in the journal */
 1.72131 +  u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
 1.72132 +};
 1.72133 +
 1.72134 +/*
 1.72135 +** An instance of this object serves as a cursor into the rollback journal.
 1.72136 +** The cursor can be either for reading or writing.
 1.72137 +*/
 1.72138 +struct FilePoint {
 1.72139 +  sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
 1.72140 +  FileChunk *pChunk;              /* Specific chunk into which cursor points */
 1.72141 +};
 1.72142 +
 1.72143 +/*
 1.72144 +** This subclass is a subclass of sqlite3_file.  Each open memory-journal
 1.72145 +** is an instance of this class.
 1.72146 +*/
 1.72147 +struct MemJournal {
 1.72148 +  sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
 1.72149 +  FileChunk *pFirst;              /* Head of in-memory chunk-list */
 1.72150 +  FilePoint endpoint;             /* Pointer to the end of the file */
 1.72151 +  FilePoint readpoint;            /* Pointer to the end of the last xRead() */
 1.72152 +};
 1.72153 +
 1.72154 +/*
 1.72155 +** Read data from the in-memory journal file.  This is the implementation
 1.72156 +** of the sqlite3_vfs.xRead method.
 1.72157 +*/
 1.72158 +static int memjrnlRead(
 1.72159 +  sqlite3_file *pJfd,    /* The journal file from which to read */
 1.72160 +  void *zBuf,            /* Put the results here */
 1.72161 +  int iAmt,              /* Number of bytes to read */
 1.72162 +  sqlite_int64 iOfst     /* Begin reading at this offset */
 1.72163 +){
 1.72164 +  MemJournal *p = (MemJournal *)pJfd;
 1.72165 +  u8 *zOut = zBuf;
 1.72166 +  int nRead = iAmt;
 1.72167 +  int iChunkOffset;
 1.72168 +  FileChunk *pChunk;
 1.72169 +
 1.72170 +  /* SQLite never tries to read past the end of a rollback journal file */
 1.72171 +  assert( iOfst+iAmt<=p->endpoint.iOffset );
 1.72172 +
 1.72173 +  if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
 1.72174 +    sqlite3_int64 iOff = 0;
 1.72175 +    for(pChunk=p->pFirst; 
 1.72176 +        ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
 1.72177 +        pChunk=pChunk->pNext
 1.72178 +    ){
 1.72179 +      iOff += JOURNAL_CHUNKSIZE;
 1.72180 +    }
 1.72181 +  }else{
 1.72182 +    pChunk = p->readpoint.pChunk;
 1.72183 +  }
 1.72184 +
 1.72185 +  iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
 1.72186 +  do {
 1.72187 +    int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
 1.72188 +    int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
 1.72189 +    memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
 1.72190 +    zOut += nCopy;
 1.72191 +    nRead -= iSpace;
 1.72192 +    iChunkOffset = 0;
 1.72193 +  } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
 1.72194 +  p->readpoint.iOffset = iOfst+iAmt;
 1.72195 +  p->readpoint.pChunk = pChunk;
 1.72196 +
 1.72197 +  return SQLITE_OK;
 1.72198 +}
 1.72199 +
 1.72200 +/*
 1.72201 +** Write data to the file.
 1.72202 +*/
 1.72203 +static int memjrnlWrite(
 1.72204 +  sqlite3_file *pJfd,    /* The journal file into which to write */
 1.72205 +  const void *zBuf,      /* Take data to be written from here */
 1.72206 +  int iAmt,              /* Number of bytes to write */
 1.72207 +  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
 1.72208 +){
 1.72209 +  MemJournal *p = (MemJournal *)pJfd;
 1.72210 +  int nWrite = iAmt;
 1.72211 +  u8 *zWrite = (u8 *)zBuf;
 1.72212 +
 1.72213 +  /* An in-memory journal file should only ever be appended to. Random
 1.72214 +  ** access writes are not required by sqlite.
 1.72215 +  */
 1.72216 +  assert( iOfst==p->endpoint.iOffset );
 1.72217 +  UNUSED_PARAMETER(iOfst);
 1.72218 +
 1.72219 +  while( nWrite>0 ){
 1.72220 +    FileChunk *pChunk = p->endpoint.pChunk;
 1.72221 +    int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
 1.72222 +    int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
 1.72223 +
 1.72224 +    if( iChunkOffset==0 ){
 1.72225 +      /* New chunk is required to extend the file. */
 1.72226 +      FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
 1.72227 +      if( !pNew ){
 1.72228 +        return SQLITE_IOERR_NOMEM;
 1.72229 +      }
 1.72230 +      pNew->pNext = 0;
 1.72231 +      if( pChunk ){
 1.72232 +        assert( p->pFirst );
 1.72233 +        pChunk->pNext = pNew;
 1.72234 +      }else{
 1.72235 +        assert( !p->pFirst );
 1.72236 +        p->pFirst = pNew;
 1.72237 +      }
 1.72238 +      p->endpoint.pChunk = pNew;
 1.72239 +    }
 1.72240 +
 1.72241 +    memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
 1.72242 +    zWrite += iSpace;
 1.72243 +    nWrite -= iSpace;
 1.72244 +    p->endpoint.iOffset += iSpace;
 1.72245 +  }
 1.72246 +
 1.72247 +  return SQLITE_OK;
 1.72248 +}
 1.72249 +
 1.72250 +/*
 1.72251 +** Truncate the file.
 1.72252 +*/
 1.72253 +static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
 1.72254 +  MemJournal *p = (MemJournal *)pJfd;
 1.72255 +  FileChunk *pChunk;
 1.72256 +  assert(size==0);
 1.72257 +  UNUSED_PARAMETER(size);
 1.72258 +  pChunk = p->pFirst;
 1.72259 +  while( pChunk ){
 1.72260 +    FileChunk *pTmp = pChunk;
 1.72261 +    pChunk = pChunk->pNext;
 1.72262 +    sqlite3_free(pTmp);
 1.72263 +  }
 1.72264 +  sqlite3MemJournalOpen(pJfd);
 1.72265 +  return SQLITE_OK;
 1.72266 +}
 1.72267 +
 1.72268 +/*
 1.72269 +** Close the file.
 1.72270 +*/
 1.72271 +static int memjrnlClose(sqlite3_file *pJfd){
 1.72272 +  memjrnlTruncate(pJfd, 0);
 1.72273 +  return SQLITE_OK;
 1.72274 +}
 1.72275 +
 1.72276 +
 1.72277 +/*
 1.72278 +** Sync the file.
 1.72279 +**
 1.72280 +** Syncing an in-memory journal is a no-op.  And, in fact, this routine
 1.72281 +** is never called in a working implementation.  This implementation
 1.72282 +** exists purely as a contingency, in case some malfunction in some other
 1.72283 +** part of SQLite causes Sync to be called by mistake.
 1.72284 +*/
 1.72285 +static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
 1.72286 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.72287 +  return SQLITE_OK;
 1.72288 +}
 1.72289 +
 1.72290 +/*
 1.72291 +** Query the size of the file in bytes.
 1.72292 +*/
 1.72293 +static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
 1.72294 +  MemJournal *p = (MemJournal *)pJfd;
 1.72295 +  *pSize = (sqlite_int64) p->endpoint.iOffset;
 1.72296 +  return SQLITE_OK;
 1.72297 +}
 1.72298 +
 1.72299 +/*
 1.72300 +** Table of methods for MemJournal sqlite3_file object.
 1.72301 +*/
 1.72302 +static const struct sqlite3_io_methods MemJournalMethods = {
 1.72303 +  1,                /* iVersion */
 1.72304 +  memjrnlClose,     /* xClose */
 1.72305 +  memjrnlRead,      /* xRead */
 1.72306 +  memjrnlWrite,     /* xWrite */
 1.72307 +  memjrnlTruncate,  /* xTruncate */
 1.72308 +  memjrnlSync,      /* xSync */
 1.72309 +  memjrnlFileSize,  /* xFileSize */
 1.72310 +  0,                /* xLock */
 1.72311 +  0,                /* xUnlock */
 1.72312 +  0,                /* xCheckReservedLock */
 1.72313 +  0,                /* xFileControl */
 1.72314 +  0,                /* xSectorSize */
 1.72315 +  0,                /* xDeviceCharacteristics */
 1.72316 +  0,                /* xShmMap */
 1.72317 +  0,                /* xShmLock */
 1.72318 +  0,                /* xShmBarrier */
 1.72319 +  0                 /* xShmUnlock */
 1.72320 +};
 1.72321 +
 1.72322 +/* 
 1.72323 +** Open a journal file.
 1.72324 +*/
 1.72325 +SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
 1.72326 +  MemJournal *p = (MemJournal *)pJfd;
 1.72327 +  assert( EIGHT_BYTE_ALIGNMENT(p) );
 1.72328 +  memset(p, 0, sqlite3MemJournalSize());
 1.72329 +  p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
 1.72330 +}
 1.72331 +
 1.72332 +/*
 1.72333 +** Return true if the file-handle passed as an argument is 
 1.72334 +** an in-memory journal 
 1.72335 +*/
 1.72336 +SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
 1.72337 +  return pJfd->pMethods==&MemJournalMethods;
 1.72338 +}
 1.72339 +
 1.72340 +/* 
 1.72341 +** Return the number of bytes required to store a MemJournal file descriptor.
 1.72342 +*/
 1.72343 +SQLITE_PRIVATE int sqlite3MemJournalSize(void){
 1.72344 +  return sizeof(MemJournal);
 1.72345 +}
 1.72346 +
 1.72347 +/************** End of memjournal.c ******************************************/
 1.72348 +/************** Begin file walker.c ******************************************/
 1.72349 +/*
 1.72350 +** 2008 August 16
 1.72351 +**
 1.72352 +** The author disclaims copyright to this source code.  In place of
 1.72353 +** a legal notice, here is a blessing:
 1.72354 +**
 1.72355 +**    May you do good and not evil.
 1.72356 +**    May you find forgiveness for yourself and forgive others.
 1.72357 +**    May you share freely, never taking more than you give.
 1.72358 +**
 1.72359 +*************************************************************************
 1.72360 +** This file contains routines used for walking the parser tree for
 1.72361 +** an SQL statement.
 1.72362 +*/
 1.72363 +/* #include <stdlib.h> */
 1.72364 +/* #include <string.h> */
 1.72365 +
 1.72366 +
 1.72367 +/*
 1.72368 +** Walk an expression tree.  Invoke the callback once for each node
 1.72369 +** of the expression, while decending.  (In other words, the callback
 1.72370 +** is invoked before visiting children.)
 1.72371 +**
 1.72372 +** The return value from the callback should be one of the WRC_*
 1.72373 +** constants to specify how to proceed with the walk.
 1.72374 +**
 1.72375 +**    WRC_Continue      Continue descending down the tree.
 1.72376 +**
 1.72377 +**    WRC_Prune         Do not descend into child nodes.  But allow
 1.72378 +**                      the walk to continue with sibling nodes.
 1.72379 +**
 1.72380 +**    WRC_Abort         Do no more callbacks.  Unwind the stack and
 1.72381 +**                      return the top-level walk call.
 1.72382 +**
 1.72383 +** The return value from this routine is WRC_Abort to abandon the tree walk
 1.72384 +** and WRC_Continue to continue.
 1.72385 +*/
 1.72386 +SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
 1.72387 +  int rc;
 1.72388 +  if( pExpr==0 ) return WRC_Continue;
 1.72389 +  testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
 1.72390 +  testcase( ExprHasProperty(pExpr, EP_Reduced) );
 1.72391 +  rc = pWalker->xExprCallback(pWalker, pExpr);
 1.72392 +  if( rc==WRC_Continue
 1.72393 +              && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
 1.72394 +    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
 1.72395 +    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
 1.72396 +    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.72397 +      if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 1.72398 +    }else{
 1.72399 +      if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 1.72400 +    }
 1.72401 +  }
 1.72402 +  return rc & WRC_Abort;
 1.72403 +}
 1.72404 +
 1.72405 +/*
 1.72406 +** Call sqlite3WalkExpr() for every expression in list p or until
 1.72407 +** an abort request is seen.
 1.72408 +*/
 1.72409 +SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
 1.72410 +  int i;
 1.72411 +  struct ExprList_item *pItem;
 1.72412 +  if( p ){
 1.72413 +    for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
 1.72414 +      if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
 1.72415 +    }
 1.72416 +  }
 1.72417 +  return WRC_Continue;
 1.72418 +}
 1.72419 +
 1.72420 +/*
 1.72421 +** Walk all expressions associated with SELECT statement p.  Do
 1.72422 +** not invoke the SELECT callback on p, but do (of course) invoke
 1.72423 +** any expr callbacks and SELECT callbacks that come from subqueries.
 1.72424 +** Return WRC_Abort or WRC_Continue.
 1.72425 +*/
 1.72426 +SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
 1.72427 +  if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
 1.72428 +  if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
 1.72429 +  if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
 1.72430 +  if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
 1.72431 +  if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
 1.72432 +  if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
 1.72433 +  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
 1.72434 +  return WRC_Continue;
 1.72435 +}
 1.72436 +
 1.72437 +/*
 1.72438 +** Walk the parse trees associated with all subqueries in the
 1.72439 +** FROM clause of SELECT statement p.  Do not invoke the select
 1.72440 +** callback on p, but do invoke it on each FROM clause subquery
 1.72441 +** and on any subqueries further down in the tree.  Return 
 1.72442 +** WRC_Abort or WRC_Continue;
 1.72443 +*/
 1.72444 +SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
 1.72445 +  SrcList *pSrc;
 1.72446 +  int i;
 1.72447 +  struct SrcList_item *pItem;
 1.72448 +
 1.72449 +  pSrc = p->pSrc;
 1.72450 +  if( ALWAYS(pSrc) ){
 1.72451 +    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
 1.72452 +      if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
 1.72453 +        return WRC_Abort;
 1.72454 +      }
 1.72455 +    }
 1.72456 +  }
 1.72457 +  return WRC_Continue;
 1.72458 +} 
 1.72459 +
 1.72460 +/*
 1.72461 +** Call sqlite3WalkExpr() for every expression in Select statement p.
 1.72462 +** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
 1.72463 +** on the compound select chain, p->pPrior.
 1.72464 +**
 1.72465 +** Return WRC_Continue under normal conditions.  Return WRC_Abort if
 1.72466 +** there is an abort request.
 1.72467 +**
 1.72468 +** If the Walker does not have an xSelectCallback() then this routine
 1.72469 +** is a no-op returning WRC_Continue.
 1.72470 +*/
 1.72471 +SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
 1.72472 +  int rc;
 1.72473 +  if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
 1.72474 +  rc = WRC_Continue;
 1.72475 +  pWalker->walkerDepth++;
 1.72476 +  while( p ){
 1.72477 +    rc = pWalker->xSelectCallback(pWalker, p);
 1.72478 +    if( rc ) break;
 1.72479 +    if( sqlite3WalkSelectExpr(pWalker, p)
 1.72480 +     || sqlite3WalkSelectFrom(pWalker, p)
 1.72481 +    ){
 1.72482 +      pWalker->walkerDepth--;
 1.72483 +      return WRC_Abort;
 1.72484 +    }
 1.72485 +    p = p->pPrior;
 1.72486 +  }
 1.72487 +  pWalker->walkerDepth--;
 1.72488 +  return rc & WRC_Abort;
 1.72489 +}
 1.72490 +
 1.72491 +/************** End of walker.c **********************************************/
 1.72492 +/************** Begin file resolve.c *****************************************/
 1.72493 +/*
 1.72494 +** 2008 August 18
 1.72495 +**
 1.72496 +** The author disclaims copyright to this source code.  In place of
 1.72497 +** a legal notice, here is a blessing:
 1.72498 +**
 1.72499 +**    May you do good and not evil.
 1.72500 +**    May you find forgiveness for yourself and forgive others.
 1.72501 +**    May you share freely, never taking more than you give.
 1.72502 +**
 1.72503 +*************************************************************************
 1.72504 +**
 1.72505 +** This file contains routines used for walking the parser tree and
 1.72506 +** resolve all identifiers by associating them with a particular
 1.72507 +** table and column.
 1.72508 +*/
 1.72509 +/* #include <stdlib.h> */
 1.72510 +/* #include <string.h> */
 1.72511 +
 1.72512 +/*
 1.72513 +** Walk the expression tree pExpr and increase the aggregate function
 1.72514 +** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
 1.72515 +** This needs to occur when copying a TK_AGG_FUNCTION node from an
 1.72516 +** outer query into an inner subquery.
 1.72517 +**
 1.72518 +** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
 1.72519 +** is a helper function - a callback for the tree walker.
 1.72520 +*/
 1.72521 +static int incrAggDepth(Walker *pWalker, Expr *pExpr){
 1.72522 +  if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
 1.72523 +  return WRC_Continue;
 1.72524 +}
 1.72525 +static void incrAggFunctionDepth(Expr *pExpr, int N){
 1.72526 +  if( N>0 ){
 1.72527 +    Walker w;
 1.72528 +    memset(&w, 0, sizeof(w));
 1.72529 +    w.xExprCallback = incrAggDepth;
 1.72530 +    w.u.i = N;
 1.72531 +    sqlite3WalkExpr(&w, pExpr);
 1.72532 +  }
 1.72533 +}
 1.72534 +
 1.72535 +/*
 1.72536 +** Turn the pExpr expression into an alias for the iCol-th column of the
 1.72537 +** result set in pEList.
 1.72538 +**
 1.72539 +** If the result set column is a simple column reference, then this routine
 1.72540 +** makes an exact copy.  But for any other kind of expression, this
 1.72541 +** routine make a copy of the result set column as the argument to the
 1.72542 +** TK_AS operator.  The TK_AS operator causes the expression to be
 1.72543 +** evaluated just once and then reused for each alias.
 1.72544 +**
 1.72545 +** The reason for suppressing the TK_AS term when the expression is a simple
 1.72546 +** column reference is so that the column reference will be recognized as
 1.72547 +** usable by indices within the WHERE clause processing logic. 
 1.72548 +**
 1.72549 +** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
 1.72550 +** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
 1.72551 +**
 1.72552 +**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
 1.72553 +**
 1.72554 +** Is equivalent to:
 1.72555 +**
 1.72556 +**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
 1.72557 +**
 1.72558 +** The result of random()%5 in the GROUP BY clause is probably different
 1.72559 +** from the result in the result-set.  We might fix this someday.  Or
 1.72560 +** then again, we might not...
 1.72561 +**
 1.72562 +** If the reference is followed by a COLLATE operator, then make sure
 1.72563 +** the COLLATE operator is preserved.  For example:
 1.72564 +**
 1.72565 +**     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
 1.72566 +**
 1.72567 +** Should be transformed into:
 1.72568 +**
 1.72569 +**     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
 1.72570 +**
 1.72571 +** The nSubquery parameter specifies how many levels of subquery the
 1.72572 +** alias is removed from the original expression.  The usually value is
 1.72573 +** zero but it might be more if the alias is contained within a subquery
 1.72574 +** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
 1.72575 +** structures must be increased by the nSubquery amount.
 1.72576 +*/
 1.72577 +static void resolveAlias(
 1.72578 +  Parse *pParse,         /* Parsing context */
 1.72579 +  ExprList *pEList,      /* A result set */
 1.72580 +  int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
 1.72581 +  Expr *pExpr,           /* Transform this into an alias to the result set */
 1.72582 +  const char *zType,     /* "GROUP" or "ORDER" or "" */
 1.72583 +  int nSubquery          /* Number of subqueries that the label is moving */
 1.72584 +){
 1.72585 +  Expr *pOrig;           /* The iCol-th column of the result set */
 1.72586 +  Expr *pDup;            /* Copy of pOrig */
 1.72587 +  sqlite3 *db;           /* The database connection */
 1.72588 +
 1.72589 +  assert( iCol>=0 && iCol<pEList->nExpr );
 1.72590 +  pOrig = pEList->a[iCol].pExpr;
 1.72591 +  assert( pOrig!=0 );
 1.72592 +  assert( pOrig->flags & EP_Resolved );
 1.72593 +  db = pParse->db;
 1.72594 +  pDup = sqlite3ExprDup(db, pOrig, 0);
 1.72595 +  if( pDup==0 ) return;
 1.72596 +  if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
 1.72597 +    incrAggFunctionDepth(pDup, nSubquery);
 1.72598 +    pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
 1.72599 +    if( pDup==0 ) return;
 1.72600 +    if( pEList->a[iCol].iAlias==0 ){
 1.72601 +      pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
 1.72602 +    }
 1.72603 +    pDup->iTable = pEList->a[iCol].iAlias;
 1.72604 +  }
 1.72605 +  if( pExpr->op==TK_COLLATE ){
 1.72606 +    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
 1.72607 +  }
 1.72608 +
 1.72609 +  /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
 1.72610 +  ** prevents ExprDelete() from deleting the Expr structure itself,
 1.72611 +  ** allowing it to be repopulated by the memcpy() on the following line.
 1.72612 +  ** The pExpr->u.zToken might point into memory that will be freed by the
 1.72613 +  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
 1.72614 +  ** make a copy of the token before doing the sqlite3DbFree().
 1.72615 +  */
 1.72616 +  ExprSetProperty(pExpr, EP_Static);
 1.72617 +  sqlite3ExprDelete(db, pExpr);
 1.72618 +  memcpy(pExpr, pDup, sizeof(*pExpr));
 1.72619 +  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
 1.72620 +    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
 1.72621 +    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
 1.72622 +    pExpr->flags2 |= EP2_MallocedToken;
 1.72623 +  }
 1.72624 +  sqlite3DbFree(db, pDup);
 1.72625 +}
 1.72626 +
 1.72627 +
 1.72628 +/*
 1.72629 +** Return TRUE if the name zCol occurs anywhere in the USING clause.
 1.72630 +**
 1.72631 +** Return FALSE if the USING clause is NULL or if it does not contain
 1.72632 +** zCol.
 1.72633 +*/
 1.72634 +static int nameInUsingClause(IdList *pUsing, const char *zCol){
 1.72635 +  if( pUsing ){
 1.72636 +    int k;
 1.72637 +    for(k=0; k<pUsing->nId; k++){
 1.72638 +      if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
 1.72639 +    }
 1.72640 +  }
 1.72641 +  return 0;
 1.72642 +}
 1.72643 +
 1.72644 +
 1.72645 +/*
 1.72646 +** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
 1.72647 +** that name in the set of source tables in pSrcList and make the pExpr 
 1.72648 +** expression node refer back to that source column.  The following changes
 1.72649 +** are made to pExpr:
 1.72650 +**
 1.72651 +**    pExpr->iDb           Set the index in db->aDb[] of the database X
 1.72652 +**                         (even if X is implied).
 1.72653 +**    pExpr->iTable        Set to the cursor number for the table obtained
 1.72654 +**                         from pSrcList.
 1.72655 +**    pExpr->pTab          Points to the Table structure of X.Y (even if
 1.72656 +**                         X and/or Y are implied.)
 1.72657 +**    pExpr->iColumn       Set to the column number within the table.
 1.72658 +**    pExpr->op            Set to TK_COLUMN.
 1.72659 +**    pExpr->pLeft         Any expression this points to is deleted
 1.72660 +**    pExpr->pRight        Any expression this points to is deleted.
 1.72661 +**
 1.72662 +** The zDb variable is the name of the database (the "X").  This value may be
 1.72663 +** NULL meaning that name is of the form Y.Z or Z.  Any available database
 1.72664 +** can be used.  The zTable variable is the name of the table (the "Y").  This
 1.72665 +** value can be NULL if zDb is also NULL.  If zTable is NULL it
 1.72666 +** means that the form of the name is Z and that columns from any table
 1.72667 +** can be used.
 1.72668 +**
 1.72669 +** If the name cannot be resolved unambiguously, leave an error message
 1.72670 +** in pParse and return WRC_Abort.  Return WRC_Prune on success.
 1.72671 +*/
 1.72672 +static int lookupName(
 1.72673 +  Parse *pParse,       /* The parsing context */
 1.72674 +  const char *zDb,     /* Name of the database containing table, or NULL */
 1.72675 +  const char *zTab,    /* Name of table containing column, or NULL */
 1.72676 +  const char *zCol,    /* Name of the column. */
 1.72677 +  NameContext *pNC,    /* The name context used to resolve the name */
 1.72678 +  Expr *pExpr          /* Make this EXPR node point to the selected column */
 1.72679 +){
 1.72680 +  int i, j;                         /* Loop counters */
 1.72681 +  int cnt = 0;                      /* Number of matching column names */
 1.72682 +  int cntTab = 0;                   /* Number of matching table names */
 1.72683 +  int nSubquery = 0;                /* How many levels of subquery */
 1.72684 +  sqlite3 *db = pParse->db;         /* The database connection */
 1.72685 +  struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
 1.72686 +  struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
 1.72687 +  NameContext *pTopNC = pNC;        /* First namecontext in the list */
 1.72688 +  Schema *pSchema = 0;              /* Schema of the expression */
 1.72689 +  int isTrigger = 0;
 1.72690 +
 1.72691 +  assert( pNC );     /* the name context cannot be NULL. */
 1.72692 +  assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 1.72693 +  assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 1.72694 +
 1.72695 +  /* Initialize the node to no-match */
 1.72696 +  pExpr->iTable = -1;
 1.72697 +  pExpr->pTab = 0;
 1.72698 +  ExprSetIrreducible(pExpr);
 1.72699 +
 1.72700 +  /* Start at the inner-most context and move outward until a match is found */
 1.72701 +  while( pNC && cnt==0 ){
 1.72702 +    ExprList *pEList;
 1.72703 +    SrcList *pSrcList = pNC->pSrcList;
 1.72704 +
 1.72705 +    if( pSrcList ){
 1.72706 +      for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
 1.72707 +        Table *pTab;
 1.72708 +        int iDb;
 1.72709 +        Column *pCol;
 1.72710 +  
 1.72711 +        pTab = pItem->pTab;
 1.72712 +        assert( pTab!=0 && pTab->zName!=0 );
 1.72713 +        iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.72714 +        assert( pTab->nCol>0 );
 1.72715 +        if( zTab ){
 1.72716 +          if( pItem->zAlias ){
 1.72717 +            char *zTabName = pItem->zAlias;
 1.72718 +            if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
 1.72719 +          }else{
 1.72720 +            char *zTabName = pTab->zName;
 1.72721 +            if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
 1.72722 +              continue;
 1.72723 +            }
 1.72724 +            if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
 1.72725 +              continue;
 1.72726 +            }
 1.72727 +          }
 1.72728 +        }
 1.72729 +        if( 0==(cntTab++) ){
 1.72730 +          pExpr->iTable = pItem->iCursor;
 1.72731 +          pExpr->pTab = pTab;
 1.72732 +          pSchema = pTab->pSchema;
 1.72733 +          pMatch = pItem;
 1.72734 +        }
 1.72735 +        for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 1.72736 +          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 1.72737 +            /* If there has been exactly one prior match and this match
 1.72738 +            ** is for the right-hand table of a NATURAL JOIN or is in a 
 1.72739 +            ** USING clause, then skip this match.
 1.72740 +            */
 1.72741 +            if( cnt==1 ){
 1.72742 +              if( pItem->jointype & JT_NATURAL ) continue;
 1.72743 +              if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
 1.72744 +            }
 1.72745 +            cnt++;
 1.72746 +            pExpr->iTable = pItem->iCursor;
 1.72747 +            pExpr->pTab = pTab;
 1.72748 +            pMatch = pItem;
 1.72749 +            pSchema = pTab->pSchema;
 1.72750 +            /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
 1.72751 +            pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 1.72752 +            break;
 1.72753 +          }
 1.72754 +        }
 1.72755 +      }
 1.72756 +    }
 1.72757 +
 1.72758 +#ifndef SQLITE_OMIT_TRIGGER
 1.72759 +    /* If we have not already resolved the name, then maybe 
 1.72760 +    ** it is a new.* or old.* trigger argument reference
 1.72761 +    */
 1.72762 +    if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
 1.72763 +      int op = pParse->eTriggerOp;
 1.72764 +      Table *pTab = 0;
 1.72765 +      assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
 1.72766 +      if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
 1.72767 +        pExpr->iTable = 1;
 1.72768 +        pTab = pParse->pTriggerTab;
 1.72769 +      }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
 1.72770 +        pExpr->iTable = 0;
 1.72771 +        pTab = pParse->pTriggerTab;
 1.72772 +      }
 1.72773 +
 1.72774 +      if( pTab ){ 
 1.72775 +        int iCol;
 1.72776 +        pSchema = pTab->pSchema;
 1.72777 +        cntTab++;
 1.72778 +        for(iCol=0; iCol<pTab->nCol; iCol++){
 1.72779 +          Column *pCol = &pTab->aCol[iCol];
 1.72780 +          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 1.72781 +            if( iCol==pTab->iPKey ){
 1.72782 +              iCol = -1;
 1.72783 +            }
 1.72784 +            break;
 1.72785 +          }
 1.72786 +        }
 1.72787 +        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
 1.72788 +          iCol = -1;        /* IMP: R-44911-55124 */
 1.72789 +        }
 1.72790 +        if( iCol<pTab->nCol ){
 1.72791 +          cnt++;
 1.72792 +          if( iCol<0 ){
 1.72793 +            pExpr->affinity = SQLITE_AFF_INTEGER;
 1.72794 +          }else if( pExpr->iTable==0 ){
 1.72795 +            testcase( iCol==31 );
 1.72796 +            testcase( iCol==32 );
 1.72797 +            pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 1.72798 +          }else{
 1.72799 +            testcase( iCol==31 );
 1.72800 +            testcase( iCol==32 );
 1.72801 +            pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 1.72802 +          }
 1.72803 +          pExpr->iColumn = (i16)iCol;
 1.72804 +          pExpr->pTab = pTab;
 1.72805 +          isTrigger = 1;
 1.72806 +        }
 1.72807 +      }
 1.72808 +    }
 1.72809 +#endif /* !defined(SQLITE_OMIT_TRIGGER) */
 1.72810 +
 1.72811 +    /*
 1.72812 +    ** Perhaps the name is a reference to the ROWID
 1.72813 +    */
 1.72814 +    if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
 1.72815 +      cnt = 1;
 1.72816 +      pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
 1.72817 +      pExpr->affinity = SQLITE_AFF_INTEGER;
 1.72818 +    }
 1.72819 +
 1.72820 +    /*
 1.72821 +    ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
 1.72822 +    ** might refer to an result-set alias.  This happens, for example, when
 1.72823 +    ** we are resolving names in the WHERE clause of the following command:
 1.72824 +    **
 1.72825 +    **     SELECT a+b AS x FROM table WHERE x<10;
 1.72826 +    **
 1.72827 +    ** In cases like this, replace pExpr with a copy of the expression that
 1.72828 +    ** forms the result set entry ("a+b" in the example) and return immediately.
 1.72829 +    ** Note that the expression in the result set should have already been
 1.72830 +    ** resolved by the time the WHERE clause is resolved.
 1.72831 +    */
 1.72832 +    if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
 1.72833 +      for(j=0; j<pEList->nExpr; j++){
 1.72834 +        char *zAs = pEList->a[j].zName;
 1.72835 +        if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 1.72836 +          Expr *pOrig;
 1.72837 +          assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 1.72838 +          assert( pExpr->x.pList==0 );
 1.72839 +          assert( pExpr->x.pSelect==0 );
 1.72840 +          pOrig = pEList->a[j].pExpr;
 1.72841 +          if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
 1.72842 +            sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
 1.72843 +            return WRC_Abort;
 1.72844 +          }
 1.72845 +          resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
 1.72846 +          cnt = 1;
 1.72847 +          pMatch = 0;
 1.72848 +          assert( zTab==0 && zDb==0 );
 1.72849 +          goto lookupname_end;
 1.72850 +        }
 1.72851 +      } 
 1.72852 +    }
 1.72853 +
 1.72854 +    /* Advance to the next name context.  The loop will exit when either
 1.72855 +    ** we have a match (cnt>0) or when we run out of name contexts.
 1.72856 +    */
 1.72857 +    if( cnt==0 ){
 1.72858 +      pNC = pNC->pNext;
 1.72859 +      nSubquery++;
 1.72860 +    }
 1.72861 +  }
 1.72862 +
 1.72863 +  /*
 1.72864 +  ** If X and Y are NULL (in other words if only the column name Z is
 1.72865 +  ** supplied) and the value of Z is enclosed in double-quotes, then
 1.72866 +  ** Z is a string literal if it doesn't match any column names.  In that
 1.72867 +  ** case, we need to return right away and not make any changes to
 1.72868 +  ** pExpr.
 1.72869 +  **
 1.72870 +  ** Because no reference was made to outer contexts, the pNC->nRef
 1.72871 +  ** fields are not changed in any context.
 1.72872 +  */
 1.72873 +  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
 1.72874 +    pExpr->op = TK_STRING;
 1.72875 +    pExpr->pTab = 0;
 1.72876 +    return WRC_Prune;
 1.72877 +  }
 1.72878 +
 1.72879 +  /*
 1.72880 +  ** cnt==0 means there was not match.  cnt>1 means there were two or
 1.72881 +  ** more matches.  Either way, we have an error.
 1.72882 +  */
 1.72883 +  if( cnt!=1 ){
 1.72884 +    const char *zErr;
 1.72885 +    zErr = cnt==0 ? "no such column" : "ambiguous column name";
 1.72886 +    if( zDb ){
 1.72887 +      sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
 1.72888 +    }else if( zTab ){
 1.72889 +      sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
 1.72890 +    }else{
 1.72891 +      sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
 1.72892 +    }
 1.72893 +    pParse->checkSchema = 1;
 1.72894 +    pTopNC->nErr++;
 1.72895 +  }
 1.72896 +
 1.72897 +  /* If a column from a table in pSrcList is referenced, then record
 1.72898 +  ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
 1.72899 +  ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
 1.72900 +  ** column number is greater than the number of bits in the bitmask
 1.72901 +  ** then set the high-order bit of the bitmask.
 1.72902 +  */
 1.72903 +  if( pExpr->iColumn>=0 && pMatch!=0 ){
 1.72904 +    int n = pExpr->iColumn;
 1.72905 +    testcase( n==BMS-1 );
 1.72906 +    if( n>=BMS ){
 1.72907 +      n = BMS-1;
 1.72908 +    }
 1.72909 +    assert( pMatch->iCursor==pExpr->iTable );
 1.72910 +    pMatch->colUsed |= ((Bitmask)1)<<n;
 1.72911 +  }
 1.72912 +
 1.72913 +  /* Clean up and return
 1.72914 +  */
 1.72915 +  sqlite3ExprDelete(db, pExpr->pLeft);
 1.72916 +  pExpr->pLeft = 0;
 1.72917 +  sqlite3ExprDelete(db, pExpr->pRight);
 1.72918 +  pExpr->pRight = 0;
 1.72919 +  pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
 1.72920 +lookupname_end:
 1.72921 +  if( cnt==1 ){
 1.72922 +    assert( pNC!=0 );
 1.72923 +    sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
 1.72924 +    /* Increment the nRef value on all name contexts from TopNC up to
 1.72925 +    ** the point where the name matched. */
 1.72926 +    for(;;){
 1.72927 +      assert( pTopNC!=0 );
 1.72928 +      pTopNC->nRef++;
 1.72929 +      if( pTopNC==pNC ) break;
 1.72930 +      pTopNC = pTopNC->pNext;
 1.72931 +    }
 1.72932 +    return WRC_Prune;
 1.72933 +  } else {
 1.72934 +    return WRC_Abort;
 1.72935 +  }
 1.72936 +}
 1.72937 +
 1.72938 +/*
 1.72939 +** Allocate and return a pointer to an expression to load the column iCol
 1.72940 +** from datasource iSrc in SrcList pSrc.
 1.72941 +*/
 1.72942 +SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
 1.72943 +  Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
 1.72944 +  if( p ){
 1.72945 +    struct SrcList_item *pItem = &pSrc->a[iSrc];
 1.72946 +    p->pTab = pItem->pTab;
 1.72947 +    p->iTable = pItem->iCursor;
 1.72948 +    if( p->pTab->iPKey==iCol ){
 1.72949 +      p->iColumn = -1;
 1.72950 +    }else{
 1.72951 +      p->iColumn = (ynVar)iCol;
 1.72952 +      testcase( iCol==BMS );
 1.72953 +      testcase( iCol==BMS-1 );
 1.72954 +      pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
 1.72955 +    }
 1.72956 +    ExprSetProperty(p, EP_Resolved);
 1.72957 +  }
 1.72958 +  return p;
 1.72959 +}
 1.72960 +
 1.72961 +/*
 1.72962 +** This routine is callback for sqlite3WalkExpr().
 1.72963 +**
 1.72964 +** Resolve symbolic names into TK_COLUMN operators for the current
 1.72965 +** node in the expression tree.  Return 0 to continue the search down
 1.72966 +** the tree or 2 to abort the tree walk.
 1.72967 +**
 1.72968 +** This routine also does error checking and name resolution for
 1.72969 +** function names.  The operator for aggregate functions is changed
 1.72970 +** to TK_AGG_FUNCTION.
 1.72971 +*/
 1.72972 +static int resolveExprStep(Walker *pWalker, Expr *pExpr){
 1.72973 +  NameContext *pNC;
 1.72974 +  Parse *pParse;
 1.72975 +
 1.72976 +  pNC = pWalker->u.pNC;
 1.72977 +  assert( pNC!=0 );
 1.72978 +  pParse = pNC->pParse;
 1.72979 +  assert( pParse==pWalker->pParse );
 1.72980 +
 1.72981 +  if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
 1.72982 +  ExprSetProperty(pExpr, EP_Resolved);
 1.72983 +#ifndef NDEBUG
 1.72984 +  if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
 1.72985 +    SrcList *pSrcList = pNC->pSrcList;
 1.72986 +    int i;
 1.72987 +    for(i=0; i<pNC->pSrcList->nSrc; i++){
 1.72988 +      assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
 1.72989 +    }
 1.72990 +  }
 1.72991 +#endif
 1.72992 +  switch( pExpr->op ){
 1.72993 +
 1.72994 +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 1.72995 +    /* The special operator TK_ROW means use the rowid for the first
 1.72996 +    ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
 1.72997 +    ** clause processing on UPDATE and DELETE statements.
 1.72998 +    */
 1.72999 +    case TK_ROW: {
 1.73000 +      SrcList *pSrcList = pNC->pSrcList;
 1.73001 +      struct SrcList_item *pItem;
 1.73002 +      assert( pSrcList && pSrcList->nSrc==1 );
 1.73003 +      pItem = pSrcList->a; 
 1.73004 +      pExpr->op = TK_COLUMN;
 1.73005 +      pExpr->pTab = pItem->pTab;
 1.73006 +      pExpr->iTable = pItem->iCursor;
 1.73007 +      pExpr->iColumn = -1;
 1.73008 +      pExpr->affinity = SQLITE_AFF_INTEGER;
 1.73009 +      break;
 1.73010 +    }
 1.73011 +#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
 1.73012 +
 1.73013 +    /* A lone identifier is the name of a column.
 1.73014 +    */
 1.73015 +    case TK_ID: {
 1.73016 +      return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
 1.73017 +    }
 1.73018 +  
 1.73019 +    /* A table name and column name:     ID.ID
 1.73020 +    ** Or a database, table and column:  ID.ID.ID
 1.73021 +    */
 1.73022 +    case TK_DOT: {
 1.73023 +      const char *zColumn;
 1.73024 +      const char *zTable;
 1.73025 +      const char *zDb;
 1.73026 +      Expr *pRight;
 1.73027 +
 1.73028 +      /* if( pSrcList==0 ) break; */
 1.73029 +      pRight = pExpr->pRight;
 1.73030 +      if( pRight->op==TK_ID ){
 1.73031 +        zDb = 0;
 1.73032 +        zTable = pExpr->pLeft->u.zToken;
 1.73033 +        zColumn = pRight->u.zToken;
 1.73034 +      }else{
 1.73035 +        assert( pRight->op==TK_DOT );
 1.73036 +        zDb = pExpr->pLeft->u.zToken;
 1.73037 +        zTable = pRight->pLeft->u.zToken;
 1.73038 +        zColumn = pRight->pRight->u.zToken;
 1.73039 +      }
 1.73040 +      return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 1.73041 +    }
 1.73042 +
 1.73043 +    /* Resolve function names
 1.73044 +    */
 1.73045 +    case TK_CONST_FUNC:
 1.73046 +    case TK_FUNCTION: {
 1.73047 +      ExprList *pList = pExpr->x.pList;    /* The argument list */
 1.73048 +      int n = pList ? pList->nExpr : 0;    /* Number of arguments */
 1.73049 +      int no_such_func = 0;       /* True if no such function exists */
 1.73050 +      int wrong_num_args = 0;     /* True if wrong number of arguments */
 1.73051 +      int is_agg = 0;             /* True if is an aggregate function */
 1.73052 +      int auth;                   /* Authorization to use the function */
 1.73053 +      int nId;                    /* Number of characters in function name */
 1.73054 +      const char *zId;            /* The function name. */
 1.73055 +      FuncDef *pDef;              /* Information about the function */
 1.73056 +      u8 enc = ENC(pParse->db);   /* The database encoding */
 1.73057 +
 1.73058 +      testcase( pExpr->op==TK_CONST_FUNC );
 1.73059 +      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 1.73060 +      zId = pExpr->u.zToken;
 1.73061 +      nId = sqlite3Strlen30(zId);
 1.73062 +      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
 1.73063 +      if( pDef==0 ){
 1.73064 +        pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
 1.73065 +        if( pDef==0 ){
 1.73066 +          no_such_func = 1;
 1.73067 +        }else{
 1.73068 +          wrong_num_args = 1;
 1.73069 +        }
 1.73070 +      }else{
 1.73071 +        is_agg = pDef->xFunc==0;
 1.73072 +      }
 1.73073 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.73074 +      if( pDef ){
 1.73075 +        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
 1.73076 +        if( auth!=SQLITE_OK ){
 1.73077 +          if( auth==SQLITE_DENY ){
 1.73078 +            sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
 1.73079 +                                    pDef->zName);
 1.73080 +            pNC->nErr++;
 1.73081 +          }
 1.73082 +          pExpr->op = TK_NULL;
 1.73083 +          return WRC_Prune;
 1.73084 +        }
 1.73085 +      }
 1.73086 +#endif
 1.73087 +      if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
 1.73088 +        sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
 1.73089 +        pNC->nErr++;
 1.73090 +        is_agg = 0;
 1.73091 +      }else if( no_such_func ){
 1.73092 +        sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
 1.73093 +        pNC->nErr++;
 1.73094 +      }else if( wrong_num_args ){
 1.73095 +        sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 1.73096 +             nId, zId);
 1.73097 +        pNC->nErr++;
 1.73098 +      }
 1.73099 +      if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
 1.73100 +      sqlite3WalkExprList(pWalker, pList);
 1.73101 +      if( is_agg ){
 1.73102 +        NameContext *pNC2 = pNC;
 1.73103 +        pExpr->op = TK_AGG_FUNCTION;
 1.73104 +        pExpr->op2 = 0;
 1.73105 +        while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
 1.73106 +          pExpr->op2++;
 1.73107 +          pNC2 = pNC2->pNext;
 1.73108 +        }
 1.73109 +        if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
 1.73110 +        pNC->ncFlags |= NC_AllowAgg;
 1.73111 +      }
 1.73112 +      /* FIX ME:  Compute pExpr->affinity based on the expected return
 1.73113 +      ** type of the function 
 1.73114 +      */
 1.73115 +      return WRC_Prune;
 1.73116 +    }
 1.73117 +#ifndef SQLITE_OMIT_SUBQUERY
 1.73118 +    case TK_SELECT:
 1.73119 +    case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
 1.73120 +#endif
 1.73121 +    case TK_IN: {
 1.73122 +      testcase( pExpr->op==TK_IN );
 1.73123 +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.73124 +        int nRef = pNC->nRef;
 1.73125 +#ifndef SQLITE_OMIT_CHECK
 1.73126 +        if( (pNC->ncFlags & NC_IsCheck)!=0 ){
 1.73127 +          sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
 1.73128 +        }
 1.73129 +#endif
 1.73130 +        sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
 1.73131 +        assert( pNC->nRef>=nRef );
 1.73132 +        if( nRef!=pNC->nRef ){
 1.73133 +          ExprSetProperty(pExpr, EP_VarSelect);
 1.73134 +        }
 1.73135 +      }
 1.73136 +      break;
 1.73137 +    }
 1.73138 +#ifndef SQLITE_OMIT_CHECK
 1.73139 +    case TK_VARIABLE: {
 1.73140 +      if( (pNC->ncFlags & NC_IsCheck)!=0 ){
 1.73141 +        sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
 1.73142 +      }
 1.73143 +      break;
 1.73144 +    }
 1.73145 +#endif
 1.73146 +  }
 1.73147 +  return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
 1.73148 +}
 1.73149 +
 1.73150 +/*
 1.73151 +** pEList is a list of expressions which are really the result set of the
 1.73152 +** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
 1.73153 +** This routine checks to see if pE is a simple identifier which corresponds
 1.73154 +** to the AS-name of one of the terms of the expression list.  If it is,
 1.73155 +** this routine return an integer between 1 and N where N is the number of
 1.73156 +** elements in pEList, corresponding to the matching entry.  If there is
 1.73157 +** no match, or if pE is not a simple identifier, then this routine
 1.73158 +** return 0.
 1.73159 +**
 1.73160 +** pEList has been resolved.  pE has not.
 1.73161 +*/
 1.73162 +static int resolveAsName(
 1.73163 +  Parse *pParse,     /* Parsing context for error messages */
 1.73164 +  ExprList *pEList,  /* List of expressions to scan */
 1.73165 +  Expr *pE           /* Expression we are trying to match */
 1.73166 +){
 1.73167 +  int i;             /* Loop counter */
 1.73168 +
 1.73169 +  UNUSED_PARAMETER(pParse);
 1.73170 +
 1.73171 +  if( pE->op==TK_ID ){
 1.73172 +    char *zCol = pE->u.zToken;
 1.73173 +    for(i=0; i<pEList->nExpr; i++){
 1.73174 +      char *zAs = pEList->a[i].zName;
 1.73175 +      if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 1.73176 +        return i+1;
 1.73177 +      }
 1.73178 +    }
 1.73179 +  }
 1.73180 +  return 0;
 1.73181 +}
 1.73182 +
 1.73183 +/*
 1.73184 +** pE is a pointer to an expression which is a single term in the
 1.73185 +** ORDER BY of a compound SELECT.  The expression has not been
 1.73186 +** name resolved.
 1.73187 +**
 1.73188 +** At the point this routine is called, we already know that the
 1.73189 +** ORDER BY term is not an integer index into the result set.  That
 1.73190 +** case is handled by the calling routine.
 1.73191 +**
 1.73192 +** Attempt to match pE against result set columns in the left-most
 1.73193 +** SELECT statement.  Return the index i of the matching column,
 1.73194 +** as an indication to the caller that it should sort by the i-th column.
 1.73195 +** The left-most column is 1.  In other words, the value returned is the
 1.73196 +** same integer value that would be used in the SQL statement to indicate
 1.73197 +** the column.
 1.73198 +**
 1.73199 +** If there is no match, return 0.  Return -1 if an error occurs.
 1.73200 +*/
 1.73201 +static int resolveOrderByTermToExprList(
 1.73202 +  Parse *pParse,     /* Parsing context for error messages */
 1.73203 +  Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
 1.73204 +  Expr *pE           /* The specific ORDER BY term */
 1.73205 +){
 1.73206 +  int i;             /* Loop counter */
 1.73207 +  ExprList *pEList;  /* The columns of the result set */
 1.73208 +  NameContext nc;    /* Name context for resolving pE */
 1.73209 +  sqlite3 *db;       /* Database connection */
 1.73210 +  int rc;            /* Return code from subprocedures */
 1.73211 +  u8 savedSuppErr;   /* Saved value of db->suppressErr */
 1.73212 +
 1.73213 +  assert( sqlite3ExprIsInteger(pE, &i)==0 );
 1.73214 +  pEList = pSelect->pEList;
 1.73215 +
 1.73216 +  /* Resolve all names in the ORDER BY term expression
 1.73217 +  */
 1.73218 +  memset(&nc, 0, sizeof(nc));
 1.73219 +  nc.pParse = pParse;
 1.73220 +  nc.pSrcList = pSelect->pSrc;
 1.73221 +  nc.pEList = pEList;
 1.73222 +  nc.ncFlags = NC_AllowAgg;
 1.73223 +  nc.nErr = 0;
 1.73224 +  db = pParse->db;
 1.73225 +  savedSuppErr = db->suppressErr;
 1.73226 +  db->suppressErr = 1;
 1.73227 +  rc = sqlite3ResolveExprNames(&nc, pE);
 1.73228 +  db->suppressErr = savedSuppErr;
 1.73229 +  if( rc ) return 0;
 1.73230 +
 1.73231 +  /* Try to match the ORDER BY expression against an expression
 1.73232 +  ** in the result set.  Return an 1-based index of the matching
 1.73233 +  ** result-set entry.
 1.73234 +  */
 1.73235 +  for(i=0; i<pEList->nExpr; i++){
 1.73236 +    if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
 1.73237 +      return i+1;
 1.73238 +    }
 1.73239 +  }
 1.73240 +
 1.73241 +  /* If no match, return 0. */
 1.73242 +  return 0;
 1.73243 +}
 1.73244 +
 1.73245 +/*
 1.73246 +** Generate an ORDER BY or GROUP BY term out-of-range error.
 1.73247 +*/
 1.73248 +static void resolveOutOfRangeError(
 1.73249 +  Parse *pParse,         /* The error context into which to write the error */
 1.73250 +  const char *zType,     /* "ORDER" or "GROUP" */
 1.73251 +  int i,                 /* The index (1-based) of the term out of range */
 1.73252 +  int mx                 /* Largest permissible value of i */
 1.73253 +){
 1.73254 +  sqlite3ErrorMsg(pParse, 
 1.73255 +    "%r %s BY term out of range - should be "
 1.73256 +    "between 1 and %d", i, zType, mx);
 1.73257 +}
 1.73258 +
 1.73259 +/*
 1.73260 +** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
 1.73261 +** each term of the ORDER BY clause is a constant integer between 1
 1.73262 +** and N where N is the number of columns in the compound SELECT.
 1.73263 +**
 1.73264 +** ORDER BY terms that are already an integer between 1 and N are
 1.73265 +** unmodified.  ORDER BY terms that are integers outside the range of
 1.73266 +** 1 through N generate an error.  ORDER BY terms that are expressions
 1.73267 +** are matched against result set expressions of compound SELECT
 1.73268 +** beginning with the left-most SELECT and working toward the right.
 1.73269 +** At the first match, the ORDER BY expression is transformed into
 1.73270 +** the integer column number.
 1.73271 +**
 1.73272 +** Return the number of errors seen.
 1.73273 +*/
 1.73274 +static int resolveCompoundOrderBy(
 1.73275 +  Parse *pParse,        /* Parsing context.  Leave error messages here */
 1.73276 +  Select *pSelect       /* The SELECT statement containing the ORDER BY */
 1.73277 +){
 1.73278 +  int i;
 1.73279 +  ExprList *pOrderBy;
 1.73280 +  ExprList *pEList;
 1.73281 +  sqlite3 *db;
 1.73282 +  int moreToDo = 1;
 1.73283 +
 1.73284 +  pOrderBy = pSelect->pOrderBy;
 1.73285 +  if( pOrderBy==0 ) return 0;
 1.73286 +  db = pParse->db;
 1.73287 +#if SQLITE_MAX_COLUMN
 1.73288 +  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 1.73289 +    sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
 1.73290 +    return 1;
 1.73291 +  }
 1.73292 +#endif
 1.73293 +  for(i=0; i<pOrderBy->nExpr; i++){
 1.73294 +    pOrderBy->a[i].done = 0;
 1.73295 +  }
 1.73296 +  pSelect->pNext = 0;
 1.73297 +  while( pSelect->pPrior ){
 1.73298 +    pSelect->pPrior->pNext = pSelect;
 1.73299 +    pSelect = pSelect->pPrior;
 1.73300 +  }
 1.73301 +  while( pSelect && moreToDo ){
 1.73302 +    struct ExprList_item *pItem;
 1.73303 +    moreToDo = 0;
 1.73304 +    pEList = pSelect->pEList;
 1.73305 +    assert( pEList!=0 );
 1.73306 +    for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 1.73307 +      int iCol = -1;
 1.73308 +      Expr *pE, *pDup;
 1.73309 +      if( pItem->done ) continue;
 1.73310 +      pE = sqlite3ExprSkipCollate(pItem->pExpr);
 1.73311 +      if( sqlite3ExprIsInteger(pE, &iCol) ){
 1.73312 +        if( iCol<=0 || iCol>pEList->nExpr ){
 1.73313 +          resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
 1.73314 +          return 1;
 1.73315 +        }
 1.73316 +      }else{
 1.73317 +        iCol = resolveAsName(pParse, pEList, pE);
 1.73318 +        if( iCol==0 ){
 1.73319 +          pDup = sqlite3ExprDup(db, pE, 0);
 1.73320 +          if( !db->mallocFailed ){
 1.73321 +            assert(pDup);
 1.73322 +            iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
 1.73323 +          }
 1.73324 +          sqlite3ExprDelete(db, pDup);
 1.73325 +        }
 1.73326 +      }
 1.73327 +      if( iCol>0 ){
 1.73328 +        /* Convert the ORDER BY term into an integer column number iCol,
 1.73329 +        ** taking care to preserve the COLLATE clause if it exists */
 1.73330 +        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 1.73331 +        if( pNew==0 ) return 1;
 1.73332 +        pNew->flags |= EP_IntValue;
 1.73333 +        pNew->u.iValue = iCol;
 1.73334 +        if( pItem->pExpr==pE ){
 1.73335 +          pItem->pExpr = pNew;
 1.73336 +        }else{
 1.73337 +          assert( pItem->pExpr->op==TK_COLLATE );
 1.73338 +          assert( pItem->pExpr->pLeft==pE );
 1.73339 +          pItem->pExpr->pLeft = pNew;
 1.73340 +        }
 1.73341 +        sqlite3ExprDelete(db, pE);
 1.73342 +        pItem->iOrderByCol = (u16)iCol;
 1.73343 +        pItem->done = 1;
 1.73344 +      }else{
 1.73345 +        moreToDo = 1;
 1.73346 +      }
 1.73347 +    }
 1.73348 +    pSelect = pSelect->pNext;
 1.73349 +  }
 1.73350 +  for(i=0; i<pOrderBy->nExpr; i++){
 1.73351 +    if( pOrderBy->a[i].done==0 ){
 1.73352 +      sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
 1.73353 +            "column in the result set", i+1);
 1.73354 +      return 1;
 1.73355 +    }
 1.73356 +  }
 1.73357 +  return 0;
 1.73358 +}
 1.73359 +
 1.73360 +/*
 1.73361 +** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
 1.73362 +** the SELECT statement pSelect.  If any term is reference to a
 1.73363 +** result set expression (as determined by the ExprList.a.iCol field)
 1.73364 +** then convert that term into a copy of the corresponding result set
 1.73365 +** column.
 1.73366 +**
 1.73367 +** If any errors are detected, add an error message to pParse and
 1.73368 +** return non-zero.  Return zero if no errors are seen.
 1.73369 +*/
 1.73370 +SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
 1.73371 +  Parse *pParse,        /* Parsing context.  Leave error messages here */
 1.73372 +  Select *pSelect,      /* The SELECT statement containing the clause */
 1.73373 +  ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
 1.73374 +  const char *zType     /* "ORDER" or "GROUP" */
 1.73375 +){
 1.73376 +  int i;
 1.73377 +  sqlite3 *db = pParse->db;
 1.73378 +  ExprList *pEList;
 1.73379 +  struct ExprList_item *pItem;
 1.73380 +
 1.73381 +  if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
 1.73382 +#if SQLITE_MAX_COLUMN
 1.73383 +  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 1.73384 +    sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
 1.73385 +    return 1;
 1.73386 +  }
 1.73387 +#endif
 1.73388 +  pEList = pSelect->pEList;
 1.73389 +  assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
 1.73390 +  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 1.73391 +    if( pItem->iOrderByCol ){
 1.73392 +      if( pItem->iOrderByCol>pEList->nExpr ){
 1.73393 +        resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
 1.73394 +        return 1;
 1.73395 +      }
 1.73396 +      resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
 1.73397 +    }
 1.73398 +  }
 1.73399 +  return 0;
 1.73400 +}
 1.73401 +
 1.73402 +/*
 1.73403 +** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
 1.73404 +** The Name context of the SELECT statement is pNC.  zType is either
 1.73405 +** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
 1.73406 +**
 1.73407 +** This routine resolves each term of the clause into an expression.
 1.73408 +** If the order-by term is an integer I between 1 and N (where N is the
 1.73409 +** number of columns in the result set of the SELECT) then the expression
 1.73410 +** in the resolution is a copy of the I-th result-set expression.  If
 1.73411 +** the order-by term is an identify that corresponds to the AS-name of
 1.73412 +** a result-set expression, then the term resolves to a copy of the
 1.73413 +** result-set expression.  Otherwise, the expression is resolved in
 1.73414 +** the usual way - using sqlite3ResolveExprNames().
 1.73415 +**
 1.73416 +** This routine returns the number of errors.  If errors occur, then
 1.73417 +** an appropriate error message might be left in pParse.  (OOM errors
 1.73418 +** excepted.)
 1.73419 +*/
 1.73420 +static int resolveOrderGroupBy(
 1.73421 +  NameContext *pNC,     /* The name context of the SELECT statement */
 1.73422 +  Select *pSelect,      /* The SELECT statement holding pOrderBy */
 1.73423 +  ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
 1.73424 +  const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
 1.73425 +){
 1.73426 +  int i, j;                      /* Loop counters */
 1.73427 +  int iCol;                      /* Column number */
 1.73428 +  struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
 1.73429 +  Parse *pParse;                 /* Parsing context */
 1.73430 +  int nResult;                   /* Number of terms in the result set */
 1.73431 +
 1.73432 +  if( pOrderBy==0 ) return 0;
 1.73433 +  nResult = pSelect->pEList->nExpr;
 1.73434 +  pParse = pNC->pParse;
 1.73435 +  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 1.73436 +    Expr *pE = pItem->pExpr;
 1.73437 +    iCol = resolveAsName(pParse, pSelect->pEList, pE);
 1.73438 +    if( iCol>0 ){
 1.73439 +      /* If an AS-name match is found, mark this ORDER BY column as being
 1.73440 +      ** a copy of the iCol-th result-set column.  The subsequent call to
 1.73441 +      ** sqlite3ResolveOrderGroupBy() will convert the expression to a
 1.73442 +      ** copy of the iCol-th result-set expression. */
 1.73443 +      pItem->iOrderByCol = (u16)iCol;
 1.73444 +      continue;
 1.73445 +    }
 1.73446 +    if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
 1.73447 +      /* The ORDER BY term is an integer constant.  Again, set the column
 1.73448 +      ** number so that sqlite3ResolveOrderGroupBy() will convert the
 1.73449 +      ** order-by term to a copy of the result-set expression */
 1.73450 +      if( iCol<1 || iCol>0xffff ){
 1.73451 +        resolveOutOfRangeError(pParse, zType, i+1, nResult);
 1.73452 +        return 1;
 1.73453 +      }
 1.73454 +      pItem->iOrderByCol = (u16)iCol;
 1.73455 +      continue;
 1.73456 +    }
 1.73457 +
 1.73458 +    /* Otherwise, treat the ORDER BY term as an ordinary expression */
 1.73459 +    pItem->iOrderByCol = 0;
 1.73460 +    if( sqlite3ResolveExprNames(pNC, pE) ){
 1.73461 +      return 1;
 1.73462 +    }
 1.73463 +    for(j=0; j<pSelect->pEList->nExpr; j++){
 1.73464 +      if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr)==0 ){
 1.73465 +        pItem->iOrderByCol = j+1;
 1.73466 +      }
 1.73467 +    }
 1.73468 +  }
 1.73469 +  return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
 1.73470 +}
 1.73471 +
 1.73472 +/*
 1.73473 +** Resolve names in the SELECT statement p and all of its descendents.
 1.73474 +*/
 1.73475 +static int resolveSelectStep(Walker *pWalker, Select *p){
 1.73476 +  NameContext *pOuterNC;  /* Context that contains this SELECT */
 1.73477 +  NameContext sNC;        /* Name context of this SELECT */
 1.73478 +  int isCompound;         /* True if p is a compound select */
 1.73479 +  int nCompound;          /* Number of compound terms processed so far */
 1.73480 +  Parse *pParse;          /* Parsing context */
 1.73481 +  ExprList *pEList;       /* Result set expression list */
 1.73482 +  int i;                  /* Loop counter */
 1.73483 +  ExprList *pGroupBy;     /* The GROUP BY clause */
 1.73484 +  Select *pLeftmost;      /* Left-most of SELECT of a compound */
 1.73485 +  sqlite3 *db;            /* Database connection */
 1.73486 +  
 1.73487 +
 1.73488 +  assert( p!=0 );
 1.73489 +  if( p->selFlags & SF_Resolved ){
 1.73490 +    return WRC_Prune;
 1.73491 +  }
 1.73492 +  pOuterNC = pWalker->u.pNC;
 1.73493 +  pParse = pWalker->pParse;
 1.73494 +  db = pParse->db;
 1.73495 +
 1.73496 +  /* Normally sqlite3SelectExpand() will be called first and will have
 1.73497 +  ** already expanded this SELECT.  However, if this is a subquery within
 1.73498 +  ** an expression, sqlite3ResolveExprNames() will be called without a
 1.73499 +  ** prior call to sqlite3SelectExpand().  When that happens, let
 1.73500 +  ** sqlite3SelectPrep() do all of the processing for this SELECT.
 1.73501 +  ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
 1.73502 +  ** this routine in the correct order.
 1.73503 +  */
 1.73504 +  if( (p->selFlags & SF_Expanded)==0 ){
 1.73505 +    sqlite3SelectPrep(pParse, p, pOuterNC);
 1.73506 +    return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
 1.73507 +  }
 1.73508 +
 1.73509 +  isCompound = p->pPrior!=0;
 1.73510 +  nCompound = 0;
 1.73511 +  pLeftmost = p;
 1.73512 +  while( p ){
 1.73513 +    assert( (p->selFlags & SF_Expanded)!=0 );
 1.73514 +    assert( (p->selFlags & SF_Resolved)==0 );
 1.73515 +    p->selFlags |= SF_Resolved;
 1.73516 +
 1.73517 +    /* Resolve the expressions in the LIMIT and OFFSET clauses. These
 1.73518 +    ** are not allowed to refer to any names, so pass an empty NameContext.
 1.73519 +    */
 1.73520 +    memset(&sNC, 0, sizeof(sNC));
 1.73521 +    sNC.pParse = pParse;
 1.73522 +    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
 1.73523 +        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
 1.73524 +      return WRC_Abort;
 1.73525 +    }
 1.73526 +  
 1.73527 +    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
 1.73528 +    ** resolve the result-set expression list.
 1.73529 +    */
 1.73530 +    sNC.ncFlags = NC_AllowAgg;
 1.73531 +    sNC.pSrcList = p->pSrc;
 1.73532 +    sNC.pNext = pOuterNC;
 1.73533 +  
 1.73534 +    /* Resolve names in the result set. */
 1.73535 +    pEList = p->pEList;
 1.73536 +    assert( pEList!=0 );
 1.73537 +    for(i=0; i<pEList->nExpr; i++){
 1.73538 +      Expr *pX = pEList->a[i].pExpr;
 1.73539 +      if( sqlite3ResolveExprNames(&sNC, pX) ){
 1.73540 +        return WRC_Abort;
 1.73541 +      }
 1.73542 +    }
 1.73543 +  
 1.73544 +    /* Recursively resolve names in all subqueries
 1.73545 +    */
 1.73546 +    for(i=0; i<p->pSrc->nSrc; i++){
 1.73547 +      struct SrcList_item *pItem = &p->pSrc->a[i];
 1.73548 +      if( pItem->pSelect ){
 1.73549 +        NameContext *pNC;         /* Used to iterate name contexts */
 1.73550 +        int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
 1.73551 +        const char *zSavedContext = pParse->zAuthContext;
 1.73552 +
 1.73553 +        /* Count the total number of references to pOuterNC and all of its
 1.73554 +        ** parent contexts. After resolving references to expressions in
 1.73555 +        ** pItem->pSelect, check if this value has changed. If so, then
 1.73556 +        ** SELECT statement pItem->pSelect must be correlated. Set the
 1.73557 +        ** pItem->isCorrelated flag if this is the case. */
 1.73558 +        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
 1.73559 +
 1.73560 +        if( pItem->zName ) pParse->zAuthContext = pItem->zName;
 1.73561 +        sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
 1.73562 +        pParse->zAuthContext = zSavedContext;
 1.73563 +        if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
 1.73564 +
 1.73565 +        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
 1.73566 +        assert( pItem->isCorrelated==0 && nRef<=0 );
 1.73567 +        pItem->isCorrelated = (nRef!=0);
 1.73568 +      }
 1.73569 +    }
 1.73570 +  
 1.73571 +    /* If there are no aggregate functions in the result-set, and no GROUP BY 
 1.73572 +    ** expression, do not allow aggregates in any of the other expressions.
 1.73573 +    */
 1.73574 +    assert( (p->selFlags & SF_Aggregate)==0 );
 1.73575 +    pGroupBy = p->pGroupBy;
 1.73576 +    if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
 1.73577 +      p->selFlags |= SF_Aggregate;
 1.73578 +    }else{
 1.73579 +      sNC.ncFlags &= ~NC_AllowAgg;
 1.73580 +    }
 1.73581 +  
 1.73582 +    /* If a HAVING clause is present, then there must be a GROUP BY clause.
 1.73583 +    */
 1.73584 +    if( p->pHaving && !pGroupBy ){
 1.73585 +      sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
 1.73586 +      return WRC_Abort;
 1.73587 +    }
 1.73588 +  
 1.73589 +    /* Add the expression list to the name-context before parsing the
 1.73590 +    ** other expressions in the SELECT statement. This is so that
 1.73591 +    ** expressions in the WHERE clause (etc.) can refer to expressions by
 1.73592 +    ** aliases in the result set.
 1.73593 +    **
 1.73594 +    ** Minor point: If this is the case, then the expression will be
 1.73595 +    ** re-evaluated for each reference to it.
 1.73596 +    */
 1.73597 +    sNC.pEList = p->pEList;
 1.73598 +    if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
 1.73599 +       sqlite3ResolveExprNames(&sNC, p->pHaving)
 1.73600 +    ){
 1.73601 +      return WRC_Abort;
 1.73602 +    }
 1.73603 +
 1.73604 +    /* The ORDER BY and GROUP BY clauses may not refer to terms in
 1.73605 +    ** outer queries 
 1.73606 +    */
 1.73607 +    sNC.pNext = 0;
 1.73608 +    sNC.ncFlags |= NC_AllowAgg;
 1.73609 +
 1.73610 +    /* Process the ORDER BY clause for singleton SELECT statements.
 1.73611 +    ** The ORDER BY clause for compounds SELECT statements is handled
 1.73612 +    ** below, after all of the result-sets for all of the elements of
 1.73613 +    ** the compound have been resolved.
 1.73614 +    */
 1.73615 +    if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
 1.73616 +      return WRC_Abort;
 1.73617 +    }
 1.73618 +    if( db->mallocFailed ){
 1.73619 +      return WRC_Abort;
 1.73620 +    }
 1.73621 +  
 1.73622 +    /* Resolve the GROUP BY clause.  At the same time, make sure 
 1.73623 +    ** the GROUP BY clause does not contain aggregate functions.
 1.73624 +    */
 1.73625 +    if( pGroupBy ){
 1.73626 +      struct ExprList_item *pItem;
 1.73627 +    
 1.73628 +      if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
 1.73629 +        return WRC_Abort;
 1.73630 +      }
 1.73631 +      for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
 1.73632 +        if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
 1.73633 +          sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
 1.73634 +              "the GROUP BY clause");
 1.73635 +          return WRC_Abort;
 1.73636 +        }
 1.73637 +      }
 1.73638 +    }
 1.73639 +
 1.73640 +    /* Advance to the next term of the compound
 1.73641 +    */
 1.73642 +    p = p->pPrior;
 1.73643 +    nCompound++;
 1.73644 +  }
 1.73645 +
 1.73646 +  /* Resolve the ORDER BY on a compound SELECT after all terms of
 1.73647 +  ** the compound have been resolved.
 1.73648 +  */
 1.73649 +  if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
 1.73650 +    return WRC_Abort;
 1.73651 +  }
 1.73652 +
 1.73653 +  return WRC_Prune;
 1.73654 +}
 1.73655 +
 1.73656 +/*
 1.73657 +** This routine walks an expression tree and resolves references to
 1.73658 +** table columns and result-set columns.  At the same time, do error
 1.73659 +** checking on function usage and set a flag if any aggregate functions
 1.73660 +** are seen.
 1.73661 +**
 1.73662 +** To resolve table columns references we look for nodes (or subtrees) of the 
 1.73663 +** form X.Y.Z or Y.Z or just Z where
 1.73664 +**
 1.73665 +**      X:   The name of a database.  Ex:  "main" or "temp" or
 1.73666 +**           the symbolic name assigned to an ATTACH-ed database.
 1.73667 +**
 1.73668 +**      Y:   The name of a table in a FROM clause.  Or in a trigger
 1.73669 +**           one of the special names "old" or "new".
 1.73670 +**
 1.73671 +**      Z:   The name of a column in table Y.
 1.73672 +**
 1.73673 +** The node at the root of the subtree is modified as follows:
 1.73674 +**
 1.73675 +**    Expr.op        Changed to TK_COLUMN
 1.73676 +**    Expr.pTab      Points to the Table object for X.Y
 1.73677 +**    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
 1.73678 +**    Expr.iTable    The VDBE cursor number for X.Y
 1.73679 +**
 1.73680 +**
 1.73681 +** To resolve result-set references, look for expression nodes of the
 1.73682 +** form Z (with no X and Y prefix) where the Z matches the right-hand
 1.73683 +** size of an AS clause in the result-set of a SELECT.  The Z expression
 1.73684 +** is replaced by a copy of the left-hand side of the result-set expression.
 1.73685 +** Table-name and function resolution occurs on the substituted expression
 1.73686 +** tree.  For example, in:
 1.73687 +**
 1.73688 +**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
 1.73689 +**
 1.73690 +** The "x" term of the order by is replaced by "a+b" to render:
 1.73691 +**
 1.73692 +**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
 1.73693 +**
 1.73694 +** Function calls are checked to make sure that the function is 
 1.73695 +** defined and that the correct number of arguments are specified.
 1.73696 +** If the function is an aggregate function, then the NC_HasAgg flag is
 1.73697 +** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
 1.73698 +** If an expression contains aggregate functions then the EP_Agg
 1.73699 +** property on the expression is set.
 1.73700 +**
 1.73701 +** An error message is left in pParse if anything is amiss.  The number
 1.73702 +** if errors is returned.
 1.73703 +*/
 1.73704 +SQLITE_PRIVATE int sqlite3ResolveExprNames( 
 1.73705 +  NameContext *pNC,       /* Namespace to resolve expressions in. */
 1.73706 +  Expr *pExpr             /* The expression to be analyzed. */
 1.73707 +){
 1.73708 +  u8 savedHasAgg;
 1.73709 +  Walker w;
 1.73710 +
 1.73711 +  if( pExpr==0 ) return 0;
 1.73712 +#if SQLITE_MAX_EXPR_DEPTH>0
 1.73713 +  {
 1.73714 +    Parse *pParse = pNC->pParse;
 1.73715 +    if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
 1.73716 +      return 1;
 1.73717 +    }
 1.73718 +    pParse->nHeight += pExpr->nHeight;
 1.73719 +  }
 1.73720 +#endif
 1.73721 +  savedHasAgg = pNC->ncFlags & NC_HasAgg;
 1.73722 +  pNC->ncFlags &= ~NC_HasAgg;
 1.73723 +  w.xExprCallback = resolveExprStep;
 1.73724 +  w.xSelectCallback = resolveSelectStep;
 1.73725 +  w.pParse = pNC->pParse;
 1.73726 +  w.u.pNC = pNC;
 1.73727 +  sqlite3WalkExpr(&w, pExpr);
 1.73728 +#if SQLITE_MAX_EXPR_DEPTH>0
 1.73729 +  pNC->pParse->nHeight -= pExpr->nHeight;
 1.73730 +#endif
 1.73731 +  if( pNC->nErr>0 || w.pParse->nErr>0 ){
 1.73732 +    ExprSetProperty(pExpr, EP_Error);
 1.73733 +  }
 1.73734 +  if( pNC->ncFlags & NC_HasAgg ){
 1.73735 +    ExprSetProperty(pExpr, EP_Agg);
 1.73736 +  }else if( savedHasAgg ){
 1.73737 +    pNC->ncFlags |= NC_HasAgg;
 1.73738 +  }
 1.73739 +  return ExprHasProperty(pExpr, EP_Error);
 1.73740 +}
 1.73741 +
 1.73742 +
 1.73743 +/*
 1.73744 +** Resolve all names in all expressions of a SELECT and in all
 1.73745 +** decendents of the SELECT, including compounds off of p->pPrior,
 1.73746 +** subqueries in expressions, and subqueries used as FROM clause
 1.73747 +** terms.
 1.73748 +**
 1.73749 +** See sqlite3ResolveExprNames() for a description of the kinds of
 1.73750 +** transformations that occur.
 1.73751 +**
 1.73752 +** All SELECT statements should have been expanded using
 1.73753 +** sqlite3SelectExpand() prior to invoking this routine.
 1.73754 +*/
 1.73755 +SQLITE_PRIVATE void sqlite3ResolveSelectNames(
 1.73756 +  Parse *pParse,         /* The parser context */
 1.73757 +  Select *p,             /* The SELECT statement being coded. */
 1.73758 +  NameContext *pOuterNC  /* Name context for parent SELECT statement */
 1.73759 +){
 1.73760 +  Walker w;
 1.73761 +
 1.73762 +  assert( p!=0 );
 1.73763 +  w.xExprCallback = resolveExprStep;
 1.73764 +  w.xSelectCallback = resolveSelectStep;
 1.73765 +  w.pParse = pParse;
 1.73766 +  w.u.pNC = pOuterNC;
 1.73767 +  sqlite3WalkSelect(&w, p);
 1.73768 +}
 1.73769 +
 1.73770 +/************** End of resolve.c *********************************************/
 1.73771 +/************** Begin file expr.c ********************************************/
 1.73772 +/*
 1.73773 +** 2001 September 15
 1.73774 +**
 1.73775 +** The author disclaims copyright to this source code.  In place of
 1.73776 +** a legal notice, here is a blessing:
 1.73777 +**
 1.73778 +**    May you do good and not evil.
 1.73779 +**    May you find forgiveness for yourself and forgive others.
 1.73780 +**    May you share freely, never taking more than you give.
 1.73781 +**
 1.73782 +*************************************************************************
 1.73783 +** This file contains routines used for analyzing expressions and
 1.73784 +** for generating VDBE code that evaluates expressions in SQLite.
 1.73785 +*/
 1.73786 +
 1.73787 +/*
 1.73788 +** Return the 'affinity' of the expression pExpr if any.
 1.73789 +**
 1.73790 +** If pExpr is a column, a reference to a column via an 'AS' alias,
 1.73791 +** or a sub-select with a column as the return value, then the 
 1.73792 +** affinity of that column is returned. Otherwise, 0x00 is returned,
 1.73793 +** indicating no affinity for the expression.
 1.73794 +**
 1.73795 +** i.e. the WHERE clause expresssions in the following statements all
 1.73796 +** have an affinity:
 1.73797 +**
 1.73798 +** CREATE TABLE t1(a);
 1.73799 +** SELECT * FROM t1 WHERE a;
 1.73800 +** SELECT a AS b FROM t1 WHERE b;
 1.73801 +** SELECT * FROM t1 WHERE (select a from t1);
 1.73802 +*/
 1.73803 +SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
 1.73804 +  int op;
 1.73805 +  pExpr = sqlite3ExprSkipCollate(pExpr);
 1.73806 +  op = pExpr->op;
 1.73807 +  if( op==TK_SELECT ){
 1.73808 +    assert( pExpr->flags&EP_xIsSelect );
 1.73809 +    return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 1.73810 +  }
 1.73811 +#ifndef SQLITE_OMIT_CAST
 1.73812 +  if( op==TK_CAST ){
 1.73813 +    assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.73814 +    return sqlite3AffinityType(pExpr->u.zToken);
 1.73815 +  }
 1.73816 +#endif
 1.73817 +  if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
 1.73818 +   && pExpr->pTab!=0
 1.73819 +  ){
 1.73820 +    /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
 1.73821 +    ** a TK_COLUMN but was previously evaluated and cached in a register */
 1.73822 +    int j = pExpr->iColumn;
 1.73823 +    if( j<0 ) return SQLITE_AFF_INTEGER;
 1.73824 +    assert( pExpr->pTab && j<pExpr->pTab->nCol );
 1.73825 +    return pExpr->pTab->aCol[j].affinity;
 1.73826 +  }
 1.73827 +  return pExpr->affinity;
 1.73828 +}
 1.73829 +
 1.73830 +/*
 1.73831 +** Set the collating sequence for expression pExpr to be the collating
 1.73832 +** sequence named by pToken.   Return a pointer to a new Expr node that
 1.73833 +** implements the COLLATE operator.
 1.73834 +**
 1.73835 +** If a memory allocation error occurs, that fact is recorded in pParse->db
 1.73836 +** and the pExpr parameter is returned unchanged.
 1.73837 +*/
 1.73838 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
 1.73839 +  if( pCollName->n>0 ){
 1.73840 +    Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
 1.73841 +    if( pNew ){
 1.73842 +      pNew->pLeft = pExpr;
 1.73843 +      pNew->flags |= EP_Collate;
 1.73844 +      pExpr = pNew;
 1.73845 +    }
 1.73846 +  }
 1.73847 +  return pExpr;
 1.73848 +}
 1.73849 +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
 1.73850 +  Token s;
 1.73851 +  assert( zC!=0 );
 1.73852 +  s.z = zC;
 1.73853 +  s.n = sqlite3Strlen30(s.z);
 1.73854 +  return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
 1.73855 +}
 1.73856 +
 1.73857 +/*
 1.73858 +** Skip over any TK_COLLATE and/or TK_AS operators at the root of
 1.73859 +** an expression.
 1.73860 +*/
 1.73861 +SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
 1.73862 +  while( pExpr && (pExpr->op==TK_COLLATE || pExpr->op==TK_AS) ){
 1.73863 +    pExpr = pExpr->pLeft;
 1.73864 +  }
 1.73865 +  return pExpr;
 1.73866 +}
 1.73867 +
 1.73868 +/*
 1.73869 +** Return the collation sequence for the expression pExpr. If
 1.73870 +** there is no defined collating sequence, return NULL.
 1.73871 +**
 1.73872 +** The collating sequence might be determined by a COLLATE operator
 1.73873 +** or by the presence of a column with a defined collating sequence.
 1.73874 +** COLLATE operators take first precedence.  Left operands take
 1.73875 +** precedence over right operands.
 1.73876 +*/
 1.73877 +SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 1.73878 +  sqlite3 *db = pParse->db;
 1.73879 +  CollSeq *pColl = 0;
 1.73880 +  Expr *p = pExpr;
 1.73881 +  while( p ){
 1.73882 +    int op = p->op;
 1.73883 +    if( op==TK_CAST || op==TK_UPLUS ){
 1.73884 +      p = p->pLeft;
 1.73885 +      continue;
 1.73886 +    }
 1.73887 +    assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
 1.73888 +    if( op==TK_COLLATE ){
 1.73889 +      if( db->init.busy ){
 1.73890 +        /* Do not report errors when parsing while the schema */
 1.73891 +        pColl = sqlite3FindCollSeq(db, ENC(db), p->u.zToken, 0);
 1.73892 +      }else{
 1.73893 +        pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
 1.73894 +      }
 1.73895 +      break;
 1.73896 +    }
 1.73897 +    if( p->pTab!=0
 1.73898 +     && (op==TK_AGG_COLUMN || op==TK_COLUMN
 1.73899 +          || op==TK_REGISTER || op==TK_TRIGGER)
 1.73900 +    ){
 1.73901 +      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
 1.73902 +      ** a TK_COLUMN but was previously evaluated and cached in a register */
 1.73903 +      int j = p->iColumn;
 1.73904 +      if( j>=0 ){
 1.73905 +        const char *zColl = p->pTab->aCol[j].zColl;
 1.73906 +        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 1.73907 +      }
 1.73908 +      break;
 1.73909 +    }
 1.73910 +    if( p->flags & EP_Collate ){
 1.73911 +      if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
 1.73912 +        p = p->pLeft;
 1.73913 +      }else{
 1.73914 +        p = p->pRight;
 1.73915 +      }
 1.73916 +    }else{
 1.73917 +      break;
 1.73918 +    }
 1.73919 +  }
 1.73920 +  if( sqlite3CheckCollSeq(pParse, pColl) ){ 
 1.73921 +    pColl = 0;
 1.73922 +  }
 1.73923 +  return pColl;
 1.73924 +}
 1.73925 +
 1.73926 +/*
 1.73927 +** pExpr is an operand of a comparison operator.  aff2 is the
 1.73928 +** type affinity of the other operand.  This routine returns the
 1.73929 +** type affinity that should be used for the comparison operator.
 1.73930 +*/
 1.73931 +SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
 1.73932 +  char aff1 = sqlite3ExprAffinity(pExpr);
 1.73933 +  if( aff1 && aff2 ){
 1.73934 +    /* Both sides of the comparison are columns. If one has numeric
 1.73935 +    ** affinity, use that. Otherwise use no affinity.
 1.73936 +    */
 1.73937 +    if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
 1.73938 +      return SQLITE_AFF_NUMERIC;
 1.73939 +    }else{
 1.73940 +      return SQLITE_AFF_NONE;
 1.73941 +    }
 1.73942 +  }else if( !aff1 && !aff2 ){
 1.73943 +    /* Neither side of the comparison is a column.  Compare the
 1.73944 +    ** results directly.
 1.73945 +    */
 1.73946 +    return SQLITE_AFF_NONE;
 1.73947 +  }else{
 1.73948 +    /* One side is a column, the other is not. Use the columns affinity. */
 1.73949 +    assert( aff1==0 || aff2==0 );
 1.73950 +    return (aff1 + aff2);
 1.73951 +  }
 1.73952 +}
 1.73953 +
 1.73954 +/*
 1.73955 +** pExpr is a comparison operator.  Return the type affinity that should
 1.73956 +** be applied to both operands prior to doing the comparison.
 1.73957 +*/
 1.73958 +static char comparisonAffinity(Expr *pExpr){
 1.73959 +  char aff;
 1.73960 +  assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
 1.73961 +          pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
 1.73962 +          pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 1.73963 +  assert( pExpr->pLeft );
 1.73964 +  aff = sqlite3ExprAffinity(pExpr->pLeft);
 1.73965 +  if( pExpr->pRight ){
 1.73966 +    aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 1.73967 +  }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.73968 +    aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 1.73969 +  }else if( !aff ){
 1.73970 +    aff = SQLITE_AFF_NONE;
 1.73971 +  }
 1.73972 +  return aff;
 1.73973 +}
 1.73974 +
 1.73975 +/*
 1.73976 +** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
 1.73977 +** idx_affinity is the affinity of an indexed column. Return true
 1.73978 +** if the index with affinity idx_affinity may be used to implement
 1.73979 +** the comparison in pExpr.
 1.73980 +*/
 1.73981 +SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
 1.73982 +  char aff = comparisonAffinity(pExpr);
 1.73983 +  switch( aff ){
 1.73984 +    case SQLITE_AFF_NONE:
 1.73985 +      return 1;
 1.73986 +    case SQLITE_AFF_TEXT:
 1.73987 +      return idx_affinity==SQLITE_AFF_TEXT;
 1.73988 +    default:
 1.73989 +      return sqlite3IsNumericAffinity(idx_affinity);
 1.73990 +  }
 1.73991 +}
 1.73992 +
 1.73993 +/*
 1.73994 +** Return the P5 value that should be used for a binary comparison
 1.73995 +** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
 1.73996 +*/
 1.73997 +static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
 1.73998 +  u8 aff = (char)sqlite3ExprAffinity(pExpr2);
 1.73999 +  aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
 1.74000 +  return aff;
 1.74001 +}
 1.74002 +
 1.74003 +/*
 1.74004 +** Return a pointer to the collation sequence that should be used by
 1.74005 +** a binary comparison operator comparing pLeft and pRight.
 1.74006 +**
 1.74007 +** If the left hand expression has a collating sequence type, then it is
 1.74008 +** used. Otherwise the collation sequence for the right hand expression
 1.74009 +** is used, or the default (BINARY) if neither expression has a collating
 1.74010 +** type.
 1.74011 +**
 1.74012 +** Argument pRight (but not pLeft) may be a null pointer. In this case,
 1.74013 +** it is not considered.
 1.74014 +*/
 1.74015 +SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
 1.74016 +  Parse *pParse, 
 1.74017 +  Expr *pLeft, 
 1.74018 +  Expr *pRight
 1.74019 +){
 1.74020 +  CollSeq *pColl;
 1.74021 +  assert( pLeft );
 1.74022 +  if( pLeft->flags & EP_Collate ){
 1.74023 +    pColl = sqlite3ExprCollSeq(pParse, pLeft);
 1.74024 +  }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
 1.74025 +    pColl = sqlite3ExprCollSeq(pParse, pRight);
 1.74026 +  }else{
 1.74027 +    pColl = sqlite3ExprCollSeq(pParse, pLeft);
 1.74028 +    if( !pColl ){
 1.74029 +      pColl = sqlite3ExprCollSeq(pParse, pRight);
 1.74030 +    }
 1.74031 +  }
 1.74032 +  return pColl;
 1.74033 +}
 1.74034 +
 1.74035 +/*
 1.74036 +** Generate code for a comparison operator.
 1.74037 +*/
 1.74038 +static int codeCompare(
 1.74039 +  Parse *pParse,    /* The parsing (and code generating) context */
 1.74040 +  Expr *pLeft,      /* The left operand */
 1.74041 +  Expr *pRight,     /* The right operand */
 1.74042 +  int opcode,       /* The comparison opcode */
 1.74043 +  int in1, int in2, /* Register holding operands */
 1.74044 +  int dest,         /* Jump here if true.  */
 1.74045 +  int jumpIfNull    /* If true, jump if either operand is NULL */
 1.74046 +){
 1.74047 +  int p5;
 1.74048 +  int addr;
 1.74049 +  CollSeq *p4;
 1.74050 +
 1.74051 +  p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
 1.74052 +  p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
 1.74053 +  addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
 1.74054 +                           (void*)p4, P4_COLLSEQ);
 1.74055 +  sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
 1.74056 +  return addr;
 1.74057 +}
 1.74058 +
 1.74059 +#if SQLITE_MAX_EXPR_DEPTH>0
 1.74060 +/*
 1.74061 +** Check that argument nHeight is less than or equal to the maximum
 1.74062 +** expression depth allowed. If it is not, leave an error message in
 1.74063 +** pParse.
 1.74064 +*/
 1.74065 +SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
 1.74066 +  int rc = SQLITE_OK;
 1.74067 +  int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
 1.74068 +  if( nHeight>mxHeight ){
 1.74069 +    sqlite3ErrorMsg(pParse, 
 1.74070 +       "Expression tree is too large (maximum depth %d)", mxHeight
 1.74071 +    );
 1.74072 +    rc = SQLITE_ERROR;
 1.74073 +  }
 1.74074 +  return rc;
 1.74075 +}
 1.74076 +
 1.74077 +/* The following three functions, heightOfExpr(), heightOfExprList()
 1.74078 +** and heightOfSelect(), are used to determine the maximum height
 1.74079 +** of any expression tree referenced by the structure passed as the
 1.74080 +** first argument.
 1.74081 +**
 1.74082 +** If this maximum height is greater than the current value pointed
 1.74083 +** to by pnHeight, the second parameter, then set *pnHeight to that
 1.74084 +** value.
 1.74085 +*/
 1.74086 +static void heightOfExpr(Expr *p, int *pnHeight){
 1.74087 +  if( p ){
 1.74088 +    if( p->nHeight>*pnHeight ){
 1.74089 +      *pnHeight = p->nHeight;
 1.74090 +    }
 1.74091 +  }
 1.74092 +}
 1.74093 +static void heightOfExprList(ExprList *p, int *pnHeight){
 1.74094 +  if( p ){
 1.74095 +    int i;
 1.74096 +    for(i=0; i<p->nExpr; i++){
 1.74097 +      heightOfExpr(p->a[i].pExpr, pnHeight);
 1.74098 +    }
 1.74099 +  }
 1.74100 +}
 1.74101 +static void heightOfSelect(Select *p, int *pnHeight){
 1.74102 +  if( p ){
 1.74103 +    heightOfExpr(p->pWhere, pnHeight);
 1.74104 +    heightOfExpr(p->pHaving, pnHeight);
 1.74105 +    heightOfExpr(p->pLimit, pnHeight);
 1.74106 +    heightOfExpr(p->pOffset, pnHeight);
 1.74107 +    heightOfExprList(p->pEList, pnHeight);
 1.74108 +    heightOfExprList(p->pGroupBy, pnHeight);
 1.74109 +    heightOfExprList(p->pOrderBy, pnHeight);
 1.74110 +    heightOfSelect(p->pPrior, pnHeight);
 1.74111 +  }
 1.74112 +}
 1.74113 +
 1.74114 +/*
 1.74115 +** Set the Expr.nHeight variable in the structure passed as an 
 1.74116 +** argument. An expression with no children, Expr.pList or 
 1.74117 +** Expr.pSelect member has a height of 1. Any other expression
 1.74118 +** has a height equal to the maximum height of any other 
 1.74119 +** referenced Expr plus one.
 1.74120 +*/
 1.74121 +static void exprSetHeight(Expr *p){
 1.74122 +  int nHeight = 0;
 1.74123 +  heightOfExpr(p->pLeft, &nHeight);
 1.74124 +  heightOfExpr(p->pRight, &nHeight);
 1.74125 +  if( ExprHasProperty(p, EP_xIsSelect) ){
 1.74126 +    heightOfSelect(p->x.pSelect, &nHeight);
 1.74127 +  }else{
 1.74128 +    heightOfExprList(p->x.pList, &nHeight);
 1.74129 +  }
 1.74130 +  p->nHeight = nHeight + 1;
 1.74131 +}
 1.74132 +
 1.74133 +/*
 1.74134 +** Set the Expr.nHeight variable using the exprSetHeight() function. If
 1.74135 +** the height is greater than the maximum allowed expression depth,
 1.74136 +** leave an error in pParse.
 1.74137 +*/
 1.74138 +SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
 1.74139 +  exprSetHeight(p);
 1.74140 +  sqlite3ExprCheckHeight(pParse, p->nHeight);
 1.74141 +}
 1.74142 +
 1.74143 +/*
 1.74144 +** Return the maximum height of any expression tree referenced
 1.74145 +** by the select statement passed as an argument.
 1.74146 +*/
 1.74147 +SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
 1.74148 +  int nHeight = 0;
 1.74149 +  heightOfSelect(p, &nHeight);
 1.74150 +  return nHeight;
 1.74151 +}
 1.74152 +#else
 1.74153 +  #define exprSetHeight(y)
 1.74154 +#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
 1.74155 +
 1.74156 +/*
 1.74157 +** This routine is the core allocator for Expr nodes.
 1.74158 +**
 1.74159 +** Construct a new expression node and return a pointer to it.  Memory
 1.74160 +** for this node and for the pToken argument is a single allocation
 1.74161 +** obtained from sqlite3DbMalloc().  The calling function
 1.74162 +** is responsible for making sure the node eventually gets freed.
 1.74163 +**
 1.74164 +** If dequote is true, then the token (if it exists) is dequoted.
 1.74165 +** If dequote is false, no dequoting is performance.  The deQuote
 1.74166 +** parameter is ignored if pToken is NULL or if the token does not
 1.74167 +** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
 1.74168 +** then the EP_DblQuoted flag is set on the expression node.
 1.74169 +**
 1.74170 +** Special case:  If op==TK_INTEGER and pToken points to a string that
 1.74171 +** can be translated into a 32-bit integer, then the token is not
 1.74172 +** stored in u.zToken.  Instead, the integer values is written
 1.74173 +** into u.iValue and the EP_IntValue flag is set.  No extra storage
 1.74174 +** is allocated to hold the integer text and the dequote flag is ignored.
 1.74175 +*/
 1.74176 +SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
 1.74177 +  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
 1.74178 +  int op,                 /* Expression opcode */
 1.74179 +  const Token *pToken,    /* Token argument.  Might be NULL */
 1.74180 +  int dequote             /* True to dequote */
 1.74181 +){
 1.74182 +  Expr *pNew;
 1.74183 +  int nExtra = 0;
 1.74184 +  int iValue = 0;
 1.74185 +
 1.74186 +  if( pToken ){
 1.74187 +    if( op!=TK_INTEGER || pToken->z==0
 1.74188 +          || sqlite3GetInt32(pToken->z, &iValue)==0 ){
 1.74189 +      nExtra = pToken->n+1;
 1.74190 +      assert( iValue>=0 );
 1.74191 +    }
 1.74192 +  }
 1.74193 +  pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
 1.74194 +  if( pNew ){
 1.74195 +    pNew->op = (u8)op;
 1.74196 +    pNew->iAgg = -1;
 1.74197 +    if( pToken ){
 1.74198 +      if( nExtra==0 ){
 1.74199 +        pNew->flags |= EP_IntValue;
 1.74200 +        pNew->u.iValue = iValue;
 1.74201 +      }else{
 1.74202 +        int c;
 1.74203 +        pNew->u.zToken = (char*)&pNew[1];
 1.74204 +        assert( pToken->z!=0 || pToken->n==0 );
 1.74205 +        if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
 1.74206 +        pNew->u.zToken[pToken->n] = 0;
 1.74207 +        if( dequote && nExtra>=3 
 1.74208 +             && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
 1.74209 +          sqlite3Dequote(pNew->u.zToken);
 1.74210 +          if( c=='"' ) pNew->flags |= EP_DblQuoted;
 1.74211 +        }
 1.74212 +      }
 1.74213 +    }
 1.74214 +#if SQLITE_MAX_EXPR_DEPTH>0
 1.74215 +    pNew->nHeight = 1;
 1.74216 +#endif  
 1.74217 +  }
 1.74218 +  return pNew;
 1.74219 +}
 1.74220 +
 1.74221 +/*
 1.74222 +** Allocate a new expression node from a zero-terminated token that has
 1.74223 +** already been dequoted.
 1.74224 +*/
 1.74225 +SQLITE_PRIVATE Expr *sqlite3Expr(
 1.74226 +  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
 1.74227 +  int op,                 /* Expression opcode */
 1.74228 +  const char *zToken      /* Token argument.  Might be NULL */
 1.74229 +){
 1.74230 +  Token x;
 1.74231 +  x.z = zToken;
 1.74232 +  x.n = zToken ? sqlite3Strlen30(zToken) : 0;
 1.74233 +  return sqlite3ExprAlloc(db, op, &x, 0);
 1.74234 +}
 1.74235 +
 1.74236 +/*
 1.74237 +** Attach subtrees pLeft and pRight to the Expr node pRoot.
 1.74238 +**
 1.74239 +** If pRoot==NULL that means that a memory allocation error has occurred.
 1.74240 +** In that case, delete the subtrees pLeft and pRight.
 1.74241 +*/
 1.74242 +SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
 1.74243 +  sqlite3 *db,
 1.74244 +  Expr *pRoot,
 1.74245 +  Expr *pLeft,
 1.74246 +  Expr *pRight
 1.74247 +){
 1.74248 +  if( pRoot==0 ){
 1.74249 +    assert( db->mallocFailed );
 1.74250 +    sqlite3ExprDelete(db, pLeft);
 1.74251 +    sqlite3ExprDelete(db, pRight);
 1.74252 +  }else{
 1.74253 +    if( pRight ){
 1.74254 +      pRoot->pRight = pRight;
 1.74255 +      pRoot->flags |= EP_Collate & pRight->flags;
 1.74256 +    }
 1.74257 +    if( pLeft ){
 1.74258 +      pRoot->pLeft = pLeft;
 1.74259 +      pRoot->flags |= EP_Collate & pLeft->flags;
 1.74260 +    }
 1.74261 +    exprSetHeight(pRoot);
 1.74262 +  }
 1.74263 +}
 1.74264 +
 1.74265 +/*
 1.74266 +** Allocate a Expr node which joins as many as two subtrees.
 1.74267 +**
 1.74268 +** One or both of the subtrees can be NULL.  Return a pointer to the new
 1.74269 +** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
 1.74270 +** free the subtrees and return NULL.
 1.74271 +*/
 1.74272 +SQLITE_PRIVATE Expr *sqlite3PExpr(
 1.74273 +  Parse *pParse,          /* Parsing context */
 1.74274 +  int op,                 /* Expression opcode */
 1.74275 +  Expr *pLeft,            /* Left operand */
 1.74276 +  Expr *pRight,           /* Right operand */
 1.74277 +  const Token *pToken     /* Argument token */
 1.74278 +){
 1.74279 +  Expr *p;
 1.74280 +  if( op==TK_AND && pLeft && pRight ){
 1.74281 +    /* Take advantage of short-circuit false optimization for AND */
 1.74282 +    p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
 1.74283 +  }else{
 1.74284 +    p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
 1.74285 +    sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
 1.74286 +  }
 1.74287 +  if( p ) {
 1.74288 +    sqlite3ExprCheckHeight(pParse, p->nHeight);
 1.74289 +  }
 1.74290 +  return p;
 1.74291 +}
 1.74292 +
 1.74293 +/*
 1.74294 +** Return 1 if an expression must be FALSE in all cases and 0 if the
 1.74295 +** expression might be true.  This is an optimization.  If is OK to
 1.74296 +** return 0 here even if the expression really is always false (a 
 1.74297 +** false negative).  But it is a bug to return 1 if the expression
 1.74298 +** might be true in some rare circumstances (a false positive.)
 1.74299 +**
 1.74300 +** Note that if the expression is part of conditional for a
 1.74301 +** LEFT JOIN, then we cannot determine at compile-time whether or not
 1.74302 +** is it true or false, so always return 0.
 1.74303 +*/
 1.74304 +static int exprAlwaysFalse(Expr *p){
 1.74305 +  int v = 0;
 1.74306 +  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
 1.74307 +  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
 1.74308 +  return v==0;
 1.74309 +}
 1.74310 +
 1.74311 +/*
 1.74312 +** Join two expressions using an AND operator.  If either expression is
 1.74313 +** NULL, then just return the other expression.
 1.74314 +**
 1.74315 +** If one side or the other of the AND is known to be false, then instead
 1.74316 +** of returning an AND expression, just return a constant expression with
 1.74317 +** a value of false.
 1.74318 +*/
 1.74319 +SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
 1.74320 +  if( pLeft==0 ){
 1.74321 +    return pRight;
 1.74322 +  }else if( pRight==0 ){
 1.74323 +    return pLeft;
 1.74324 +  }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
 1.74325 +    sqlite3ExprDelete(db, pLeft);
 1.74326 +    sqlite3ExprDelete(db, pRight);
 1.74327 +    return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
 1.74328 +  }else{
 1.74329 +    Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
 1.74330 +    sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
 1.74331 +    return pNew;
 1.74332 +  }
 1.74333 +}
 1.74334 +
 1.74335 +/*
 1.74336 +** Construct a new expression node for a function with multiple
 1.74337 +** arguments.
 1.74338 +*/
 1.74339 +SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
 1.74340 +  Expr *pNew;
 1.74341 +  sqlite3 *db = pParse->db;
 1.74342 +  assert( pToken );
 1.74343 +  pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
 1.74344 +  if( pNew==0 ){
 1.74345 +    sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
 1.74346 +    return 0;
 1.74347 +  }
 1.74348 +  pNew->x.pList = pList;
 1.74349 +  assert( !ExprHasProperty(pNew, EP_xIsSelect) );
 1.74350 +  sqlite3ExprSetHeight(pParse, pNew);
 1.74351 +  return pNew;
 1.74352 +}
 1.74353 +
 1.74354 +/*
 1.74355 +** Assign a variable number to an expression that encodes a wildcard
 1.74356 +** in the original SQL statement.  
 1.74357 +**
 1.74358 +** Wildcards consisting of a single "?" are assigned the next sequential
 1.74359 +** variable number.
 1.74360 +**
 1.74361 +** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
 1.74362 +** sure "nnn" is not too be to avoid a denial of service attack when
 1.74363 +** the SQL statement comes from an external source.
 1.74364 +**
 1.74365 +** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
 1.74366 +** as the previous instance of the same wildcard.  Or if this is the first
 1.74367 +** instance of the wildcard, the next sequenial variable number is
 1.74368 +** assigned.
 1.74369 +*/
 1.74370 +SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
 1.74371 +  sqlite3 *db = pParse->db;
 1.74372 +  const char *z;
 1.74373 +
 1.74374 +  if( pExpr==0 ) return;
 1.74375 +  assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
 1.74376 +  z = pExpr->u.zToken;
 1.74377 +  assert( z!=0 );
 1.74378 +  assert( z[0]!=0 );
 1.74379 +  if( z[1]==0 ){
 1.74380 +    /* Wildcard of the form "?".  Assign the next variable number */
 1.74381 +    assert( z[0]=='?' );
 1.74382 +    pExpr->iColumn = (ynVar)(++pParse->nVar);
 1.74383 +  }else{
 1.74384 +    ynVar x = 0;
 1.74385 +    u32 n = sqlite3Strlen30(z);
 1.74386 +    if( z[0]=='?' ){
 1.74387 +      /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
 1.74388 +      ** use it as the variable number */
 1.74389 +      i64 i;
 1.74390 +      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
 1.74391 +      pExpr->iColumn = x = (ynVar)i;
 1.74392 +      testcase( i==0 );
 1.74393 +      testcase( i==1 );
 1.74394 +      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
 1.74395 +      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
 1.74396 +      if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 1.74397 +        sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
 1.74398 +            db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
 1.74399 +        x = 0;
 1.74400 +      }
 1.74401 +      if( i>pParse->nVar ){
 1.74402 +        pParse->nVar = (int)i;
 1.74403 +      }
 1.74404 +    }else{
 1.74405 +      /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
 1.74406 +      ** number as the prior appearance of the same name, or if the name
 1.74407 +      ** has never appeared before, reuse the same variable number
 1.74408 +      */
 1.74409 +      ynVar i;
 1.74410 +      for(i=0; i<pParse->nzVar; i++){
 1.74411 +        if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
 1.74412 +          pExpr->iColumn = x = (ynVar)i+1;
 1.74413 +          break;
 1.74414 +        }
 1.74415 +      }
 1.74416 +      if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
 1.74417 +    }
 1.74418 +    if( x>0 ){
 1.74419 +      if( x>pParse->nzVar ){
 1.74420 +        char **a;
 1.74421 +        a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
 1.74422 +        if( a==0 ) return;  /* Error reported through db->mallocFailed */
 1.74423 +        pParse->azVar = a;
 1.74424 +        memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
 1.74425 +        pParse->nzVar = x;
 1.74426 +      }
 1.74427 +      if( z[0]!='?' || pParse->azVar[x-1]==0 ){
 1.74428 +        sqlite3DbFree(db, pParse->azVar[x-1]);
 1.74429 +        pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
 1.74430 +      }
 1.74431 +    }
 1.74432 +  } 
 1.74433 +  if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 1.74434 +    sqlite3ErrorMsg(pParse, "too many SQL variables");
 1.74435 +  }
 1.74436 +}
 1.74437 +
 1.74438 +/*
 1.74439 +** Recursively delete an expression tree.
 1.74440 +*/
 1.74441 +SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
 1.74442 +  if( p==0 ) return;
 1.74443 +  /* Sanity check: Assert that the IntValue is non-negative if it exists */
 1.74444 +  assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
 1.74445 +  if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
 1.74446 +    sqlite3ExprDelete(db, p->pLeft);
 1.74447 +    sqlite3ExprDelete(db, p->pRight);
 1.74448 +    if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
 1.74449 +      sqlite3DbFree(db, p->u.zToken);
 1.74450 +    }
 1.74451 +    if( ExprHasProperty(p, EP_xIsSelect) ){
 1.74452 +      sqlite3SelectDelete(db, p->x.pSelect);
 1.74453 +    }else{
 1.74454 +      sqlite3ExprListDelete(db, p->x.pList);
 1.74455 +    }
 1.74456 +  }
 1.74457 +  if( !ExprHasProperty(p, EP_Static) ){
 1.74458 +    sqlite3DbFree(db, p);
 1.74459 +  }
 1.74460 +}
 1.74461 +
 1.74462 +/*
 1.74463 +** Return the number of bytes allocated for the expression structure 
 1.74464 +** passed as the first argument. This is always one of EXPR_FULLSIZE,
 1.74465 +** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
 1.74466 +*/
 1.74467 +static int exprStructSize(Expr *p){
 1.74468 +  if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
 1.74469 +  if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
 1.74470 +  return EXPR_FULLSIZE;
 1.74471 +}
 1.74472 +
 1.74473 +/*
 1.74474 +** The dupedExpr*Size() routines each return the number of bytes required
 1.74475 +** to store a copy of an expression or expression tree.  They differ in
 1.74476 +** how much of the tree is measured.
 1.74477 +**
 1.74478 +**     dupedExprStructSize()     Size of only the Expr structure 
 1.74479 +**     dupedExprNodeSize()       Size of Expr + space for token
 1.74480 +**     dupedExprSize()           Expr + token + subtree components
 1.74481 +**
 1.74482 +***************************************************************************
 1.74483 +**
 1.74484 +** The dupedExprStructSize() function returns two values OR-ed together:  
 1.74485 +** (1) the space required for a copy of the Expr structure only and 
 1.74486 +** (2) the EP_xxx flags that indicate what the structure size should be.
 1.74487 +** The return values is always one of:
 1.74488 +**
 1.74489 +**      EXPR_FULLSIZE
 1.74490 +**      EXPR_REDUCEDSIZE   | EP_Reduced
 1.74491 +**      EXPR_TOKENONLYSIZE | EP_TokenOnly
 1.74492 +**
 1.74493 +** The size of the structure can be found by masking the return value
 1.74494 +** of this routine with 0xfff.  The flags can be found by masking the
 1.74495 +** return value with EP_Reduced|EP_TokenOnly.
 1.74496 +**
 1.74497 +** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
 1.74498 +** (unreduced) Expr objects as they or originally constructed by the parser.
 1.74499 +** During expression analysis, extra information is computed and moved into
 1.74500 +** later parts of teh Expr object and that extra information might get chopped
 1.74501 +** off if the expression is reduced.  Note also that it does not work to
 1.74502 +** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
 1.74503 +** to reduce a pristine expression tree from the parser.  The implementation
 1.74504 +** of dupedExprStructSize() contain multiple assert() statements that attempt
 1.74505 +** to enforce this constraint.
 1.74506 +*/
 1.74507 +static int dupedExprStructSize(Expr *p, int flags){
 1.74508 +  int nSize;
 1.74509 +  assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
 1.74510 +  if( 0==(flags&EXPRDUP_REDUCE) ){
 1.74511 +    nSize = EXPR_FULLSIZE;
 1.74512 +  }else{
 1.74513 +    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
 1.74514 +    assert( !ExprHasProperty(p, EP_FromJoin) ); 
 1.74515 +    assert( (p->flags2 & EP2_MallocedToken)==0 );
 1.74516 +    assert( (p->flags2 & EP2_Irreducible)==0 );
 1.74517 +    if( p->pLeft || p->pRight || p->x.pList ){
 1.74518 +      nSize = EXPR_REDUCEDSIZE | EP_Reduced;
 1.74519 +    }else{
 1.74520 +      nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
 1.74521 +    }
 1.74522 +  }
 1.74523 +  return nSize;
 1.74524 +}
 1.74525 +
 1.74526 +/*
 1.74527 +** This function returns the space in bytes required to store the copy 
 1.74528 +** of the Expr structure and a copy of the Expr.u.zToken string (if that
 1.74529 +** string is defined.)
 1.74530 +*/
 1.74531 +static int dupedExprNodeSize(Expr *p, int flags){
 1.74532 +  int nByte = dupedExprStructSize(p, flags) & 0xfff;
 1.74533 +  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 1.74534 +    nByte += sqlite3Strlen30(p->u.zToken)+1;
 1.74535 +  }
 1.74536 +  return ROUND8(nByte);
 1.74537 +}
 1.74538 +
 1.74539 +/*
 1.74540 +** Return the number of bytes required to create a duplicate of the 
 1.74541 +** expression passed as the first argument. The second argument is a
 1.74542 +** mask containing EXPRDUP_XXX flags.
 1.74543 +**
 1.74544 +** The value returned includes space to create a copy of the Expr struct
 1.74545 +** itself and the buffer referred to by Expr.u.zToken, if any.
 1.74546 +**
 1.74547 +** If the EXPRDUP_REDUCE flag is set, then the return value includes 
 1.74548 +** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
 1.74549 +** and Expr.pRight variables (but not for any structures pointed to or 
 1.74550 +** descended from the Expr.x.pList or Expr.x.pSelect variables).
 1.74551 +*/
 1.74552 +static int dupedExprSize(Expr *p, int flags){
 1.74553 +  int nByte = 0;
 1.74554 +  if( p ){
 1.74555 +    nByte = dupedExprNodeSize(p, flags);
 1.74556 +    if( flags&EXPRDUP_REDUCE ){
 1.74557 +      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
 1.74558 +    }
 1.74559 +  }
 1.74560 +  return nByte;
 1.74561 +}
 1.74562 +
 1.74563 +/*
 1.74564 +** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
 1.74565 +** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
 1.74566 +** to store the copy of expression p, the copies of p->u.zToken
 1.74567 +** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
 1.74568 +** if any. Before returning, *pzBuffer is set to the first byte passed the
 1.74569 +** portion of the buffer copied into by this function.
 1.74570 +*/
 1.74571 +static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
 1.74572 +  Expr *pNew = 0;                      /* Value to return */
 1.74573 +  if( p ){
 1.74574 +    const int isReduced = (flags&EXPRDUP_REDUCE);
 1.74575 +    u8 *zAlloc;
 1.74576 +    u32 staticFlag = 0;
 1.74577 +
 1.74578 +    assert( pzBuffer==0 || isReduced );
 1.74579 +
 1.74580 +    /* Figure out where to write the new Expr structure. */
 1.74581 +    if( pzBuffer ){
 1.74582 +      zAlloc = *pzBuffer;
 1.74583 +      staticFlag = EP_Static;
 1.74584 +    }else{
 1.74585 +      zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
 1.74586 +    }
 1.74587 +    pNew = (Expr *)zAlloc;
 1.74588 +
 1.74589 +    if( pNew ){
 1.74590 +      /* Set nNewSize to the size allocated for the structure pointed to
 1.74591 +      ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
 1.74592 +      ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
 1.74593 +      ** by the copy of the p->u.zToken string (if any).
 1.74594 +      */
 1.74595 +      const unsigned nStructSize = dupedExprStructSize(p, flags);
 1.74596 +      const int nNewSize = nStructSize & 0xfff;
 1.74597 +      int nToken;
 1.74598 +      if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 1.74599 +        nToken = sqlite3Strlen30(p->u.zToken) + 1;
 1.74600 +      }else{
 1.74601 +        nToken = 0;
 1.74602 +      }
 1.74603 +      if( isReduced ){
 1.74604 +        assert( ExprHasProperty(p, EP_Reduced)==0 );
 1.74605 +        memcpy(zAlloc, p, nNewSize);
 1.74606 +      }else{
 1.74607 +        int nSize = exprStructSize(p);
 1.74608 +        memcpy(zAlloc, p, nSize);
 1.74609 +        memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
 1.74610 +      }
 1.74611 +
 1.74612 +      /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
 1.74613 +      pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
 1.74614 +      pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
 1.74615 +      pNew->flags |= staticFlag;
 1.74616 +
 1.74617 +      /* Copy the p->u.zToken string, if any. */
 1.74618 +      if( nToken ){
 1.74619 +        char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
 1.74620 +        memcpy(zToken, p->u.zToken, nToken);
 1.74621 +      }
 1.74622 +
 1.74623 +      if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
 1.74624 +        /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
 1.74625 +        if( ExprHasProperty(p, EP_xIsSelect) ){
 1.74626 +          pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
 1.74627 +        }else{
 1.74628 +          pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
 1.74629 +        }
 1.74630 +      }
 1.74631 +
 1.74632 +      /* Fill in pNew->pLeft and pNew->pRight. */
 1.74633 +      if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
 1.74634 +        zAlloc += dupedExprNodeSize(p, flags);
 1.74635 +        if( ExprHasProperty(pNew, EP_Reduced) ){
 1.74636 +          pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
 1.74637 +          pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
 1.74638 +        }
 1.74639 +        if( pzBuffer ){
 1.74640 +          *pzBuffer = zAlloc;
 1.74641 +        }
 1.74642 +      }else{
 1.74643 +        pNew->flags2 = 0;
 1.74644 +        if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
 1.74645 +          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
 1.74646 +          pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
 1.74647 +        }
 1.74648 +      }
 1.74649 +
 1.74650 +    }
 1.74651 +  }
 1.74652 +  return pNew;
 1.74653 +}
 1.74654 +
 1.74655 +/*
 1.74656 +** The following group of routines make deep copies of expressions,
 1.74657 +** expression lists, ID lists, and select statements.  The copies can
 1.74658 +** be deleted (by being passed to their respective ...Delete() routines)
 1.74659 +** without effecting the originals.
 1.74660 +**
 1.74661 +** The expression list, ID, and source lists return by sqlite3ExprListDup(),
 1.74662 +** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
 1.74663 +** by subsequent calls to sqlite*ListAppend() routines.
 1.74664 +**
 1.74665 +** Any tables that the SrcList might point to are not duplicated.
 1.74666 +**
 1.74667 +** The flags parameter contains a combination of the EXPRDUP_XXX flags.
 1.74668 +** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
 1.74669 +** truncated version of the usual Expr structure that will be stored as
 1.74670 +** part of the in-memory representation of the database schema.
 1.74671 +*/
 1.74672 +SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
 1.74673 +  return exprDup(db, p, flags, 0);
 1.74674 +}
 1.74675 +SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
 1.74676 +  ExprList *pNew;
 1.74677 +  struct ExprList_item *pItem, *pOldItem;
 1.74678 +  int i;
 1.74679 +  if( p==0 ) return 0;
 1.74680 +  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
 1.74681 +  if( pNew==0 ) return 0;
 1.74682 +  pNew->iECursor = 0;
 1.74683 +  pNew->nExpr = i = p->nExpr;
 1.74684 +  if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
 1.74685 +  pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
 1.74686 +  if( pItem==0 ){
 1.74687 +    sqlite3DbFree(db, pNew);
 1.74688 +    return 0;
 1.74689 +  } 
 1.74690 +  pOldItem = p->a;
 1.74691 +  for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
 1.74692 +    Expr *pOldExpr = pOldItem->pExpr;
 1.74693 +    pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
 1.74694 +    pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 1.74695 +    pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
 1.74696 +    pItem->sortOrder = pOldItem->sortOrder;
 1.74697 +    pItem->done = 0;
 1.74698 +    pItem->iOrderByCol = pOldItem->iOrderByCol;
 1.74699 +    pItem->iAlias = pOldItem->iAlias;
 1.74700 +  }
 1.74701 +  return pNew;
 1.74702 +}
 1.74703 +
 1.74704 +/*
 1.74705 +** If cursors, triggers, views and subqueries are all omitted from
 1.74706 +** the build, then none of the following routines, except for 
 1.74707 +** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
 1.74708 +** called with a NULL argument.
 1.74709 +*/
 1.74710 +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
 1.74711 + || !defined(SQLITE_OMIT_SUBQUERY)
 1.74712 +SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
 1.74713 +  SrcList *pNew;
 1.74714 +  int i;
 1.74715 +  int nByte;
 1.74716 +  if( p==0 ) return 0;
 1.74717 +  nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
 1.74718 +  pNew = sqlite3DbMallocRaw(db, nByte );
 1.74719 +  if( pNew==0 ) return 0;
 1.74720 +  pNew->nSrc = pNew->nAlloc = p->nSrc;
 1.74721 +  for(i=0; i<p->nSrc; i++){
 1.74722 +    struct SrcList_item *pNewItem = &pNew->a[i];
 1.74723 +    struct SrcList_item *pOldItem = &p->a[i];
 1.74724 +    Table *pTab;
 1.74725 +    pNewItem->pSchema = pOldItem->pSchema;
 1.74726 +    pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
 1.74727 +    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 1.74728 +    pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
 1.74729 +    pNewItem->jointype = pOldItem->jointype;
 1.74730 +    pNewItem->iCursor = pOldItem->iCursor;
 1.74731 +    pNewItem->addrFillSub = pOldItem->addrFillSub;
 1.74732 +    pNewItem->regReturn = pOldItem->regReturn;
 1.74733 +    pNewItem->isCorrelated = pOldItem->isCorrelated;
 1.74734 +    pNewItem->viaCoroutine = pOldItem->viaCoroutine;
 1.74735 +    pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
 1.74736 +    pNewItem->notIndexed = pOldItem->notIndexed;
 1.74737 +    pNewItem->pIndex = pOldItem->pIndex;
 1.74738 +    pTab = pNewItem->pTab = pOldItem->pTab;
 1.74739 +    if( pTab ){
 1.74740 +      pTab->nRef++;
 1.74741 +    }
 1.74742 +    pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
 1.74743 +    pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
 1.74744 +    pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
 1.74745 +    pNewItem->colUsed = pOldItem->colUsed;
 1.74746 +  }
 1.74747 +  return pNew;
 1.74748 +}
 1.74749 +SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
 1.74750 +  IdList *pNew;
 1.74751 +  int i;
 1.74752 +  if( p==0 ) return 0;
 1.74753 +  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
 1.74754 +  if( pNew==0 ) return 0;
 1.74755 +  pNew->nId = p->nId;
 1.74756 +  pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
 1.74757 +  if( pNew->a==0 ){
 1.74758 +    sqlite3DbFree(db, pNew);
 1.74759 +    return 0;
 1.74760 +  }
 1.74761 +  /* Note that because the size of the allocation for p->a[] is not
 1.74762 +  ** necessarily a power of two, sqlite3IdListAppend() may not be called
 1.74763 +  ** on the duplicate created by this function. */
 1.74764 +  for(i=0; i<p->nId; i++){
 1.74765 +    struct IdList_item *pNewItem = &pNew->a[i];
 1.74766 +    struct IdList_item *pOldItem = &p->a[i];
 1.74767 +    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 1.74768 +    pNewItem->idx = pOldItem->idx;
 1.74769 +  }
 1.74770 +  return pNew;
 1.74771 +}
 1.74772 +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 1.74773 +  Select *pNew, *pPrior;
 1.74774 +  if( p==0 ) return 0;
 1.74775 +  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
 1.74776 +  if( pNew==0 ) return 0;
 1.74777 +  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
 1.74778 +  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
 1.74779 +  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
 1.74780 +  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
 1.74781 +  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
 1.74782 +  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
 1.74783 +  pNew->op = p->op;
 1.74784 +  pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
 1.74785 +  if( pPrior ) pPrior->pNext = pNew;
 1.74786 +  pNew->pNext = 0;
 1.74787 +  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 1.74788 +  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 1.74789 +  pNew->iLimit = 0;
 1.74790 +  pNew->iOffset = 0;
 1.74791 +  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 1.74792 +  pNew->pRightmost = 0;
 1.74793 +  pNew->addrOpenEphm[0] = -1;
 1.74794 +  pNew->addrOpenEphm[1] = -1;
 1.74795 +  pNew->addrOpenEphm[2] = -1;
 1.74796 +  return pNew;
 1.74797 +}
 1.74798 +#else
 1.74799 +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 1.74800 +  assert( p==0 );
 1.74801 +  return 0;
 1.74802 +}
 1.74803 +#endif
 1.74804 +
 1.74805 +
 1.74806 +/*
 1.74807 +** Add a new element to the end of an expression list.  If pList is
 1.74808 +** initially NULL, then create a new expression list.
 1.74809 +**
 1.74810 +** If a memory allocation error occurs, the entire list is freed and
 1.74811 +** NULL is returned.  If non-NULL is returned, then it is guaranteed
 1.74812 +** that the new entry was successfully appended.
 1.74813 +*/
 1.74814 +SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
 1.74815 +  Parse *pParse,          /* Parsing context */
 1.74816 +  ExprList *pList,        /* List to which to append. Might be NULL */
 1.74817 +  Expr *pExpr             /* Expression to be appended. Might be NULL */
 1.74818 +){
 1.74819 +  sqlite3 *db = pParse->db;
 1.74820 +  if( pList==0 ){
 1.74821 +    pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
 1.74822 +    if( pList==0 ){
 1.74823 +      goto no_mem;
 1.74824 +    }
 1.74825 +    pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
 1.74826 +    if( pList->a==0 ) goto no_mem;
 1.74827 +  }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
 1.74828 +    struct ExprList_item *a;
 1.74829 +    assert( pList->nExpr>0 );
 1.74830 +    a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
 1.74831 +    if( a==0 ){
 1.74832 +      goto no_mem;
 1.74833 +    }
 1.74834 +    pList->a = a;
 1.74835 +  }
 1.74836 +  assert( pList->a!=0 );
 1.74837 +  if( 1 ){
 1.74838 +    struct ExprList_item *pItem = &pList->a[pList->nExpr++];
 1.74839 +    memset(pItem, 0, sizeof(*pItem));
 1.74840 +    pItem->pExpr = pExpr;
 1.74841 +  }
 1.74842 +  return pList;
 1.74843 +
 1.74844 +no_mem:     
 1.74845 +  /* Avoid leaking memory if malloc has failed. */
 1.74846 +  sqlite3ExprDelete(db, pExpr);
 1.74847 +  sqlite3ExprListDelete(db, pList);
 1.74848 +  return 0;
 1.74849 +}
 1.74850 +
 1.74851 +/*
 1.74852 +** Set the ExprList.a[].zName element of the most recently added item
 1.74853 +** on the expression list.
 1.74854 +**
 1.74855 +** pList might be NULL following an OOM error.  But pName should never be
 1.74856 +** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
 1.74857 +** is set.
 1.74858 +*/
 1.74859 +SQLITE_PRIVATE void sqlite3ExprListSetName(
 1.74860 +  Parse *pParse,          /* Parsing context */
 1.74861 +  ExprList *pList,        /* List to which to add the span. */
 1.74862 +  Token *pName,           /* Name to be added */
 1.74863 +  int dequote             /* True to cause the name to be dequoted */
 1.74864 +){
 1.74865 +  assert( pList!=0 || pParse->db->mallocFailed!=0 );
 1.74866 +  if( pList ){
 1.74867 +    struct ExprList_item *pItem;
 1.74868 +    assert( pList->nExpr>0 );
 1.74869 +    pItem = &pList->a[pList->nExpr-1];
 1.74870 +    assert( pItem->zName==0 );
 1.74871 +    pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
 1.74872 +    if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
 1.74873 +  }
 1.74874 +}
 1.74875 +
 1.74876 +/*
 1.74877 +** Set the ExprList.a[].zSpan element of the most recently added item
 1.74878 +** on the expression list.
 1.74879 +**
 1.74880 +** pList might be NULL following an OOM error.  But pSpan should never be
 1.74881 +** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
 1.74882 +** is set.
 1.74883 +*/
 1.74884 +SQLITE_PRIVATE void sqlite3ExprListSetSpan(
 1.74885 +  Parse *pParse,          /* Parsing context */
 1.74886 +  ExprList *pList,        /* List to which to add the span. */
 1.74887 +  ExprSpan *pSpan         /* The span to be added */
 1.74888 +){
 1.74889 +  sqlite3 *db = pParse->db;
 1.74890 +  assert( pList!=0 || db->mallocFailed!=0 );
 1.74891 +  if( pList ){
 1.74892 +    struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
 1.74893 +    assert( pList->nExpr>0 );
 1.74894 +    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
 1.74895 +    sqlite3DbFree(db, pItem->zSpan);
 1.74896 +    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
 1.74897 +                                    (int)(pSpan->zEnd - pSpan->zStart));
 1.74898 +  }
 1.74899 +}
 1.74900 +
 1.74901 +/*
 1.74902 +** If the expression list pEList contains more than iLimit elements,
 1.74903 +** leave an error message in pParse.
 1.74904 +*/
 1.74905 +SQLITE_PRIVATE void sqlite3ExprListCheckLength(
 1.74906 +  Parse *pParse,
 1.74907 +  ExprList *pEList,
 1.74908 +  const char *zObject
 1.74909 +){
 1.74910 +  int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
 1.74911 +  testcase( pEList && pEList->nExpr==mx );
 1.74912 +  testcase( pEList && pEList->nExpr==mx+1 );
 1.74913 +  if( pEList && pEList->nExpr>mx ){
 1.74914 +    sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
 1.74915 +  }
 1.74916 +}
 1.74917 +
 1.74918 +/*
 1.74919 +** Delete an entire expression list.
 1.74920 +*/
 1.74921 +SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
 1.74922 +  int i;
 1.74923 +  struct ExprList_item *pItem;
 1.74924 +  if( pList==0 ) return;
 1.74925 +  assert( pList->a!=0 || pList->nExpr==0 );
 1.74926 +  for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
 1.74927 +    sqlite3ExprDelete(db, pItem->pExpr);
 1.74928 +    sqlite3DbFree(db, pItem->zName);
 1.74929 +    sqlite3DbFree(db, pItem->zSpan);
 1.74930 +  }
 1.74931 +  sqlite3DbFree(db, pList->a);
 1.74932 +  sqlite3DbFree(db, pList);
 1.74933 +}
 1.74934 +
 1.74935 +/*
 1.74936 +** These routines are Walker callbacks.  Walker.u.pi is a pointer
 1.74937 +** to an integer.  These routines are checking an expression to see
 1.74938 +** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
 1.74939 +** not constant.
 1.74940 +**
 1.74941 +** These callback routines are used to implement the following:
 1.74942 +**
 1.74943 +**     sqlite3ExprIsConstant()
 1.74944 +**     sqlite3ExprIsConstantNotJoin()
 1.74945 +**     sqlite3ExprIsConstantOrFunction()
 1.74946 +**
 1.74947 +*/
 1.74948 +static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
 1.74949 +
 1.74950 +  /* If pWalker->u.i is 3 then any term of the expression that comes from
 1.74951 +  ** the ON or USING clauses of a join disqualifies the expression
 1.74952 +  ** from being considered constant. */
 1.74953 +  if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
 1.74954 +    pWalker->u.i = 0;
 1.74955 +    return WRC_Abort;
 1.74956 +  }
 1.74957 +
 1.74958 +  switch( pExpr->op ){
 1.74959 +    /* Consider functions to be constant if all their arguments are constant
 1.74960 +    ** and pWalker->u.i==2 */
 1.74961 +    case TK_FUNCTION:
 1.74962 +      if( pWalker->u.i==2 ) return 0;
 1.74963 +      /* Fall through */
 1.74964 +    case TK_ID:
 1.74965 +    case TK_COLUMN:
 1.74966 +    case TK_AGG_FUNCTION:
 1.74967 +    case TK_AGG_COLUMN:
 1.74968 +      testcase( pExpr->op==TK_ID );
 1.74969 +      testcase( pExpr->op==TK_COLUMN );
 1.74970 +      testcase( pExpr->op==TK_AGG_FUNCTION );
 1.74971 +      testcase( pExpr->op==TK_AGG_COLUMN );
 1.74972 +      pWalker->u.i = 0;
 1.74973 +      return WRC_Abort;
 1.74974 +    default:
 1.74975 +      testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
 1.74976 +      testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
 1.74977 +      return WRC_Continue;
 1.74978 +  }
 1.74979 +}
 1.74980 +static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
 1.74981 +  UNUSED_PARAMETER(NotUsed);
 1.74982 +  pWalker->u.i = 0;
 1.74983 +  return WRC_Abort;
 1.74984 +}
 1.74985 +static int exprIsConst(Expr *p, int initFlag){
 1.74986 +  Walker w;
 1.74987 +  w.u.i = initFlag;
 1.74988 +  w.xExprCallback = exprNodeIsConstant;
 1.74989 +  w.xSelectCallback = selectNodeIsConstant;
 1.74990 +  sqlite3WalkExpr(&w, p);
 1.74991 +  return w.u.i;
 1.74992 +}
 1.74993 +
 1.74994 +/*
 1.74995 +** Walk an expression tree.  Return 1 if the expression is constant
 1.74996 +** and 0 if it involves variables or function calls.
 1.74997 +**
 1.74998 +** For the purposes of this function, a double-quoted string (ex: "abc")
 1.74999 +** is considered a variable but a single-quoted string (ex: 'abc') is
 1.75000 +** a constant.
 1.75001 +*/
 1.75002 +SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
 1.75003 +  return exprIsConst(p, 1);
 1.75004 +}
 1.75005 +
 1.75006 +/*
 1.75007 +** Walk an expression tree.  Return 1 if the expression is constant
 1.75008 +** that does no originate from the ON or USING clauses of a join.
 1.75009 +** Return 0 if it involves variables or function calls or terms from
 1.75010 +** an ON or USING clause.
 1.75011 +*/
 1.75012 +SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
 1.75013 +  return exprIsConst(p, 3);
 1.75014 +}
 1.75015 +
 1.75016 +/*
 1.75017 +** Walk an expression tree.  Return 1 if the expression is constant
 1.75018 +** or a function call with constant arguments.  Return and 0 if there
 1.75019 +** are any variables.
 1.75020 +**
 1.75021 +** For the purposes of this function, a double-quoted string (ex: "abc")
 1.75022 +** is considered a variable but a single-quoted string (ex: 'abc') is
 1.75023 +** a constant.
 1.75024 +*/
 1.75025 +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
 1.75026 +  return exprIsConst(p, 2);
 1.75027 +}
 1.75028 +
 1.75029 +/*
 1.75030 +** If the expression p codes a constant integer that is small enough
 1.75031 +** to fit in a 32-bit integer, return 1 and put the value of the integer
 1.75032 +** in *pValue.  If the expression is not an integer or if it is too big
 1.75033 +** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
 1.75034 +*/
 1.75035 +SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
 1.75036 +  int rc = 0;
 1.75037 +
 1.75038 +  /* If an expression is an integer literal that fits in a signed 32-bit
 1.75039 +  ** integer, then the EP_IntValue flag will have already been set */
 1.75040 +  assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
 1.75041 +           || sqlite3GetInt32(p->u.zToken, &rc)==0 );
 1.75042 +
 1.75043 +  if( p->flags & EP_IntValue ){
 1.75044 +    *pValue = p->u.iValue;
 1.75045 +    return 1;
 1.75046 +  }
 1.75047 +  switch( p->op ){
 1.75048 +    case TK_UPLUS: {
 1.75049 +      rc = sqlite3ExprIsInteger(p->pLeft, pValue);
 1.75050 +      break;
 1.75051 +    }
 1.75052 +    case TK_UMINUS: {
 1.75053 +      int v;
 1.75054 +      if( sqlite3ExprIsInteger(p->pLeft, &v) ){
 1.75055 +        *pValue = -v;
 1.75056 +        rc = 1;
 1.75057 +      }
 1.75058 +      break;
 1.75059 +    }
 1.75060 +    default: break;
 1.75061 +  }
 1.75062 +  return rc;
 1.75063 +}
 1.75064 +
 1.75065 +/*
 1.75066 +** Return FALSE if there is no chance that the expression can be NULL.
 1.75067 +**
 1.75068 +** If the expression might be NULL or if the expression is too complex
 1.75069 +** to tell return TRUE.  
 1.75070 +**
 1.75071 +** This routine is used as an optimization, to skip OP_IsNull opcodes
 1.75072 +** when we know that a value cannot be NULL.  Hence, a false positive
 1.75073 +** (returning TRUE when in fact the expression can never be NULL) might
 1.75074 +** be a small performance hit but is otherwise harmless.  On the other
 1.75075 +** hand, a false negative (returning FALSE when the result could be NULL)
 1.75076 +** will likely result in an incorrect answer.  So when in doubt, return
 1.75077 +** TRUE.
 1.75078 +*/
 1.75079 +SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
 1.75080 +  u8 op;
 1.75081 +  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
 1.75082 +  op = p->op;
 1.75083 +  if( op==TK_REGISTER ) op = p->op2;
 1.75084 +  switch( op ){
 1.75085 +    case TK_INTEGER:
 1.75086 +    case TK_STRING:
 1.75087 +    case TK_FLOAT:
 1.75088 +    case TK_BLOB:
 1.75089 +      return 0;
 1.75090 +    default:
 1.75091 +      return 1;
 1.75092 +  }
 1.75093 +}
 1.75094 +
 1.75095 +/*
 1.75096 +** Generate an OP_IsNull instruction that tests register iReg and jumps
 1.75097 +** to location iDest if the value in iReg is NULL.  The value in iReg 
 1.75098 +** was computed by pExpr.  If we can look at pExpr at compile-time and
 1.75099 +** determine that it can never generate a NULL, then the OP_IsNull operation
 1.75100 +** can be omitted.
 1.75101 +*/
 1.75102 +SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
 1.75103 +  Vdbe *v,            /* The VDBE under construction */
 1.75104 +  const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
 1.75105 +  int iReg,           /* Test the value in this register for NULL */
 1.75106 +  int iDest           /* Jump here if the value is null */
 1.75107 +){
 1.75108 +  if( sqlite3ExprCanBeNull(pExpr) ){
 1.75109 +    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
 1.75110 +  }
 1.75111 +}
 1.75112 +
 1.75113 +/*
 1.75114 +** Return TRUE if the given expression is a constant which would be
 1.75115 +** unchanged by OP_Affinity with the affinity given in the second
 1.75116 +** argument.
 1.75117 +**
 1.75118 +** This routine is used to determine if the OP_Affinity operation
 1.75119 +** can be omitted.  When in doubt return FALSE.  A false negative
 1.75120 +** is harmless.  A false positive, however, can result in the wrong
 1.75121 +** answer.
 1.75122 +*/
 1.75123 +SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
 1.75124 +  u8 op;
 1.75125 +  if( aff==SQLITE_AFF_NONE ) return 1;
 1.75126 +  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
 1.75127 +  op = p->op;
 1.75128 +  if( op==TK_REGISTER ) op = p->op2;
 1.75129 +  switch( op ){
 1.75130 +    case TK_INTEGER: {
 1.75131 +      return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
 1.75132 +    }
 1.75133 +    case TK_FLOAT: {
 1.75134 +      return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
 1.75135 +    }
 1.75136 +    case TK_STRING: {
 1.75137 +      return aff==SQLITE_AFF_TEXT;
 1.75138 +    }
 1.75139 +    case TK_BLOB: {
 1.75140 +      return 1;
 1.75141 +    }
 1.75142 +    case TK_COLUMN: {
 1.75143 +      assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
 1.75144 +      return p->iColumn<0
 1.75145 +          && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
 1.75146 +    }
 1.75147 +    default: {
 1.75148 +      return 0;
 1.75149 +    }
 1.75150 +  }
 1.75151 +}
 1.75152 +
 1.75153 +/*
 1.75154 +** Return TRUE if the given string is a row-id column name.
 1.75155 +*/
 1.75156 +SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
 1.75157 +  if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
 1.75158 +  if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
 1.75159 +  if( sqlite3StrICmp(z, "OID")==0 ) return 1;
 1.75160 +  return 0;
 1.75161 +}
 1.75162 +
 1.75163 +/*
 1.75164 +** Return true if we are able to the IN operator optimization on a
 1.75165 +** query of the form
 1.75166 +**
 1.75167 +**       x IN (SELECT ...)
 1.75168 +**
 1.75169 +** Where the SELECT... clause is as specified by the parameter to this
 1.75170 +** routine.
 1.75171 +**
 1.75172 +** The Select object passed in has already been preprocessed and no
 1.75173 +** errors have been found.
 1.75174 +*/
 1.75175 +#ifndef SQLITE_OMIT_SUBQUERY
 1.75176 +static int isCandidateForInOpt(Select *p){
 1.75177 +  SrcList *pSrc;
 1.75178 +  ExprList *pEList;
 1.75179 +  Table *pTab;
 1.75180 +  if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
 1.75181 +  if( p->pPrior ) return 0;              /* Not a compound SELECT */
 1.75182 +  if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 1.75183 +    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 1.75184 +    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 1.75185 +    return 0; /* No DISTINCT keyword and no aggregate functions */
 1.75186 +  }
 1.75187 +  assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
 1.75188 +  if( p->pLimit ) return 0;              /* Has no LIMIT clause */
 1.75189 +  assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
 1.75190 +  if( p->pWhere ) return 0;              /* Has no WHERE clause */
 1.75191 +  pSrc = p->pSrc;
 1.75192 +  assert( pSrc!=0 );
 1.75193 +  if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
 1.75194 +  if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
 1.75195 +  pTab = pSrc->a[0].pTab;
 1.75196 +  if( NEVER(pTab==0) ) return 0;
 1.75197 +  assert( pTab->pSelect==0 );            /* FROM clause is not a view */
 1.75198 +  if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
 1.75199 +  pEList = p->pEList;
 1.75200 +  if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
 1.75201 +  if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
 1.75202 +  return 1;
 1.75203 +}
 1.75204 +#endif /* SQLITE_OMIT_SUBQUERY */
 1.75205 +
 1.75206 +/*
 1.75207 +** Code an OP_Once instruction and allocate space for its flag. Return the 
 1.75208 +** address of the new instruction.
 1.75209 +*/
 1.75210 +SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
 1.75211 +  Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
 1.75212 +  return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
 1.75213 +}
 1.75214 +
 1.75215 +/*
 1.75216 +** This function is used by the implementation of the IN (...) operator.
 1.75217 +** The pX parameter is the expression on the RHS of the IN operator, which
 1.75218 +** might be either a list of expressions or a subquery.
 1.75219 +**
 1.75220 +** The job of this routine is to find or create a b-tree object that can
 1.75221 +** be used either to test for membership in the RHS set or to iterate through
 1.75222 +** all members of the RHS set, skipping duplicates.
 1.75223 +**
 1.75224 +** A cursor is opened on the b-tree object that the RHS of the IN operator
 1.75225 +** and pX->iTable is set to the index of that cursor.
 1.75226 +**
 1.75227 +** The returned value of this function indicates the b-tree type, as follows:
 1.75228 +**
 1.75229 +**   IN_INDEX_ROWID - The cursor was opened on a database table.
 1.75230 +**   IN_INDEX_INDEX - The cursor was opened on a database index.
 1.75231 +**   IN_INDEX_EPH -   The cursor was opened on a specially created and
 1.75232 +**                    populated epheremal table.
 1.75233 +**
 1.75234 +** An existing b-tree might be used if the RHS expression pX is a simple
 1.75235 +** subquery such as:
 1.75236 +**
 1.75237 +**     SELECT <column> FROM <table>
 1.75238 +**
 1.75239 +** If the RHS of the IN operator is a list or a more complex subquery, then
 1.75240 +** an ephemeral table might need to be generated from the RHS and then
 1.75241 +** pX->iTable made to point to the ephermeral table instead of an
 1.75242 +** existing table.  
 1.75243 +**
 1.75244 +** If the prNotFound parameter is 0, then the b-tree will be used to iterate
 1.75245 +** through the set members, skipping any duplicates. In this case an
 1.75246 +** epheremal table must be used unless the selected <column> is guaranteed
 1.75247 +** to be unique - either because it is an INTEGER PRIMARY KEY or it
 1.75248 +** has a UNIQUE constraint or UNIQUE index.
 1.75249 +**
 1.75250 +** If the prNotFound parameter is not 0, then the b-tree will be used 
 1.75251 +** for fast set membership tests. In this case an epheremal table must 
 1.75252 +** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
 1.75253 +** be found with <column> as its left-most column.
 1.75254 +**
 1.75255 +** When the b-tree is being used for membership tests, the calling function
 1.75256 +** needs to know whether or not the structure contains an SQL NULL 
 1.75257 +** value in order to correctly evaluate expressions like "X IN (Y, Z)".
 1.75258 +** If there is any chance that the (...) might contain a NULL value at
 1.75259 +** runtime, then a register is allocated and the register number written
 1.75260 +** to *prNotFound. If there is no chance that the (...) contains a
 1.75261 +** NULL value, then *prNotFound is left unchanged.
 1.75262 +**
 1.75263 +** If a register is allocated and its location stored in *prNotFound, then
 1.75264 +** its initial value is NULL.  If the (...) does not remain constant
 1.75265 +** for the duration of the query (i.e. the SELECT within the (...)
 1.75266 +** is a correlated subquery) then the value of the allocated register is
 1.75267 +** reset to NULL each time the subquery is rerun. This allows the
 1.75268 +** caller to use vdbe code equivalent to the following:
 1.75269 +**
 1.75270 +**   if( register==NULL ){
 1.75271 +**     has_null = <test if data structure contains null>
 1.75272 +**     register = 1
 1.75273 +**   }
 1.75274 +**
 1.75275 +** in order to avoid running the <test if data structure contains null>
 1.75276 +** test more often than is necessary.
 1.75277 +*/
 1.75278 +#ifndef SQLITE_OMIT_SUBQUERY
 1.75279 +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
 1.75280 +  Select *p;                            /* SELECT to the right of IN operator */
 1.75281 +  int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
 1.75282 +  int iTab = pParse->nTab++;            /* Cursor of the RHS table */
 1.75283 +  int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
 1.75284 +  Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
 1.75285 +
 1.75286 +  assert( pX->op==TK_IN );
 1.75287 +
 1.75288 +  /* Check to see if an existing table or index can be used to
 1.75289 +  ** satisfy the query.  This is preferable to generating a new 
 1.75290 +  ** ephemeral table.
 1.75291 +  */
 1.75292 +  p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
 1.75293 +  if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
 1.75294 +    sqlite3 *db = pParse->db;              /* Database connection */
 1.75295 +    Table *pTab;                           /* Table <table>. */
 1.75296 +    Expr *pExpr;                           /* Expression <column> */
 1.75297 +    int iCol;                              /* Index of column <column> */
 1.75298 +    int iDb;                               /* Database idx for pTab */
 1.75299 +
 1.75300 +    assert( p );                        /* Because of isCandidateForInOpt(p) */
 1.75301 +    assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
 1.75302 +    assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
 1.75303 +    assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
 1.75304 +    pTab = p->pSrc->a[0].pTab;
 1.75305 +    pExpr = p->pEList->a[0].pExpr;
 1.75306 +    iCol = pExpr->iColumn;
 1.75307 +   
 1.75308 +    /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
 1.75309 +    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.75310 +    sqlite3CodeVerifySchema(pParse, iDb);
 1.75311 +    sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 1.75312 +
 1.75313 +    /* This function is only called from two places. In both cases the vdbe
 1.75314 +    ** has already been allocated. So assume sqlite3GetVdbe() is always
 1.75315 +    ** successful here.
 1.75316 +    */
 1.75317 +    assert(v);
 1.75318 +    if( iCol<0 ){
 1.75319 +      int iAddr;
 1.75320 +
 1.75321 +      iAddr = sqlite3CodeOnce(pParse);
 1.75322 +
 1.75323 +      sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 1.75324 +      eType = IN_INDEX_ROWID;
 1.75325 +
 1.75326 +      sqlite3VdbeJumpHere(v, iAddr);
 1.75327 +    }else{
 1.75328 +      Index *pIdx;                         /* Iterator variable */
 1.75329 +
 1.75330 +      /* The collation sequence used by the comparison. If an index is to
 1.75331 +      ** be used in place of a temp-table, it must be ordered according
 1.75332 +      ** to this collation sequence.  */
 1.75333 +      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
 1.75334 +
 1.75335 +      /* Check that the affinity that will be used to perform the 
 1.75336 +      ** comparison is the same as the affinity of the column. If
 1.75337 +      ** it is not, it is not possible to use any index.
 1.75338 +      */
 1.75339 +      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 1.75340 +
 1.75341 +      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
 1.75342 +        if( (pIdx->aiColumn[0]==iCol)
 1.75343 +         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
 1.75344 +         && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
 1.75345 +        ){
 1.75346 +          int iAddr;
 1.75347 +          char *pKey;
 1.75348 +  
 1.75349 +          pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
 1.75350 +          iAddr = sqlite3CodeOnce(pParse);
 1.75351 +  
 1.75352 +          sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
 1.75353 +                               pKey,P4_KEYINFO_HANDOFF);
 1.75354 +          VdbeComment((v, "%s", pIdx->zName));
 1.75355 +          eType = IN_INDEX_INDEX;
 1.75356 +
 1.75357 +          sqlite3VdbeJumpHere(v, iAddr);
 1.75358 +          if( prNotFound && !pTab->aCol[iCol].notNull ){
 1.75359 +            *prNotFound = ++pParse->nMem;
 1.75360 +            sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 1.75361 +          }
 1.75362 +        }
 1.75363 +      }
 1.75364 +    }
 1.75365 +  }
 1.75366 +
 1.75367 +  if( eType==0 ){
 1.75368 +    /* Could not found an existing table or index to use as the RHS b-tree.
 1.75369 +    ** We will have to generate an ephemeral table to do the job.
 1.75370 +    */
 1.75371 +    double savedNQueryLoop = pParse->nQueryLoop;
 1.75372 +    int rMayHaveNull = 0;
 1.75373 +    eType = IN_INDEX_EPH;
 1.75374 +    if( prNotFound ){
 1.75375 +      *prNotFound = rMayHaveNull = ++pParse->nMem;
 1.75376 +      sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 1.75377 +    }else{
 1.75378 +      testcase( pParse->nQueryLoop>(double)1 );
 1.75379 +      pParse->nQueryLoop = (double)1;
 1.75380 +      if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
 1.75381 +        eType = IN_INDEX_ROWID;
 1.75382 +      }
 1.75383 +    }
 1.75384 +    sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
 1.75385 +    pParse->nQueryLoop = savedNQueryLoop;
 1.75386 +  }else{
 1.75387 +    pX->iTable = iTab;
 1.75388 +  }
 1.75389 +  return eType;
 1.75390 +}
 1.75391 +#endif
 1.75392 +
 1.75393 +/*
 1.75394 +** Generate code for scalar subqueries used as a subquery expression, EXISTS,
 1.75395 +** or IN operators.  Examples:
 1.75396 +**
 1.75397 +**     (SELECT a FROM b)          -- subquery
 1.75398 +**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
 1.75399 +**     x IN (4,5,11)              -- IN operator with list on right-hand side
 1.75400 +**     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
 1.75401 +**
 1.75402 +** The pExpr parameter describes the expression that contains the IN
 1.75403 +** operator or subquery.
 1.75404 +**
 1.75405 +** If parameter isRowid is non-zero, then expression pExpr is guaranteed
 1.75406 +** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
 1.75407 +** to some integer key column of a table B-Tree. In this case, use an
 1.75408 +** intkey B-Tree to store the set of IN(...) values instead of the usual
 1.75409 +** (slower) variable length keys B-Tree.
 1.75410 +**
 1.75411 +** If rMayHaveNull is non-zero, that means that the operation is an IN
 1.75412 +** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
 1.75413 +** Furthermore, the IN is in a WHERE clause and that we really want
 1.75414 +** to iterate over the RHS of the IN operator in order to quickly locate
 1.75415 +** all corresponding LHS elements.  All this routine does is initialize
 1.75416 +** the register given by rMayHaveNull to NULL.  Calling routines will take
 1.75417 +** care of changing this register value to non-NULL if the RHS is NULL-free.
 1.75418 +**
 1.75419 +** If rMayHaveNull is zero, that means that the subquery is being used
 1.75420 +** for membership testing only.  There is no need to initialize any
 1.75421 +** registers to indicate the presense or absence of NULLs on the RHS.
 1.75422 +**
 1.75423 +** For a SELECT or EXISTS operator, return the register that holds the
 1.75424 +** result.  For IN operators or if an error occurs, the return value is 0.
 1.75425 +*/
 1.75426 +#ifndef SQLITE_OMIT_SUBQUERY
 1.75427 +SQLITE_PRIVATE int sqlite3CodeSubselect(
 1.75428 +  Parse *pParse,          /* Parsing context */
 1.75429 +  Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
 1.75430 +  int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
 1.75431 +  int isRowid             /* If true, LHS of IN operator is a rowid */
 1.75432 +){
 1.75433 +  int testAddr = -1;                      /* One-time test address */
 1.75434 +  int rReg = 0;                           /* Register storing resulting */
 1.75435 +  Vdbe *v = sqlite3GetVdbe(pParse);
 1.75436 +  if( NEVER(v==0) ) return 0;
 1.75437 +  sqlite3ExprCachePush(pParse);
 1.75438 +
 1.75439 +  /* This code must be run in its entirety every time it is encountered
 1.75440 +  ** if any of the following is true:
 1.75441 +  **
 1.75442 +  **    *  The right-hand side is a correlated subquery
 1.75443 +  **    *  The right-hand side is an expression list containing variables
 1.75444 +  **    *  We are inside a trigger
 1.75445 +  **
 1.75446 +  ** If all of the above are false, then we can run this code just once
 1.75447 +  ** save the results, and reuse the same result on subsequent invocations.
 1.75448 +  */
 1.75449 +  if( !ExprHasAnyProperty(pExpr, EP_VarSelect) ){
 1.75450 +    testAddr = sqlite3CodeOnce(pParse);
 1.75451 +  }
 1.75452 +
 1.75453 +#ifndef SQLITE_OMIT_EXPLAIN
 1.75454 +  if( pParse->explain==2 ){
 1.75455 +    char *zMsg = sqlite3MPrintf(
 1.75456 +        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
 1.75457 +        pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
 1.75458 +    );
 1.75459 +    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 1.75460 +  }
 1.75461 +#endif
 1.75462 +
 1.75463 +  switch( pExpr->op ){
 1.75464 +    case TK_IN: {
 1.75465 +      char affinity;              /* Affinity of the LHS of the IN */
 1.75466 +      KeyInfo keyInfo;            /* Keyinfo for the generated table */
 1.75467 +      static u8 sortOrder = 0;    /* Fake aSortOrder for keyInfo */
 1.75468 +      int addr;                   /* Address of OP_OpenEphemeral instruction */
 1.75469 +      Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 1.75470 +
 1.75471 +      if( rMayHaveNull ){
 1.75472 +        sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
 1.75473 +      }
 1.75474 +
 1.75475 +      affinity = sqlite3ExprAffinity(pLeft);
 1.75476 +
 1.75477 +      /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
 1.75478 +      ** expression it is handled the same way.  An ephemeral table is 
 1.75479 +      ** filled with single-field index keys representing the results
 1.75480 +      ** from the SELECT or the <exprlist>.
 1.75481 +      **
 1.75482 +      ** If the 'x' expression is a column value, or the SELECT...
 1.75483 +      ** statement returns a column value, then the affinity of that
 1.75484 +      ** column is used to build the index keys. If both 'x' and the
 1.75485 +      ** SELECT... statement are columns, then numeric affinity is used
 1.75486 +      ** if either column has NUMERIC or INTEGER affinity. If neither
 1.75487 +      ** 'x' nor the SELECT... statement are columns, then numeric affinity
 1.75488 +      ** is used.
 1.75489 +      */
 1.75490 +      pExpr->iTable = pParse->nTab++;
 1.75491 +      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
 1.75492 +      if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 1.75493 +      memset(&keyInfo, 0, sizeof(keyInfo));
 1.75494 +      keyInfo.nField = 1;
 1.75495 +      keyInfo.aSortOrder = &sortOrder;
 1.75496 +
 1.75497 +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.75498 +        /* Case 1:     expr IN (SELECT ...)
 1.75499 +        **
 1.75500 +        ** Generate code to write the results of the select into the temporary
 1.75501 +        ** table allocated and opened above.
 1.75502 +        */
 1.75503 +        SelectDest dest;
 1.75504 +        ExprList *pEList;
 1.75505 +
 1.75506 +        assert( !isRowid );
 1.75507 +        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 1.75508 +        dest.affSdst = (u8)affinity;
 1.75509 +        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 1.75510 +        pExpr->x.pSelect->iLimit = 0;
 1.75511 +        if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
 1.75512 +          return 0;
 1.75513 +        }
 1.75514 +        pEList = pExpr->x.pSelect->pEList;
 1.75515 +        if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ 
 1.75516 +          keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
 1.75517 +              pEList->a[0].pExpr);
 1.75518 +        }
 1.75519 +      }else if( ALWAYS(pExpr->x.pList!=0) ){
 1.75520 +        /* Case 2:     expr IN (exprlist)
 1.75521 +        **
 1.75522 +        ** For each expression, build an index key from the evaluation and
 1.75523 +        ** store it in the temporary table. If <expr> is a column, then use
 1.75524 +        ** that columns affinity when building index keys. If <expr> is not
 1.75525 +        ** a column, use numeric affinity.
 1.75526 +        */
 1.75527 +        int i;
 1.75528 +        ExprList *pList = pExpr->x.pList;
 1.75529 +        struct ExprList_item *pItem;
 1.75530 +        int r1, r2, r3;
 1.75531 +
 1.75532 +        if( !affinity ){
 1.75533 +          affinity = SQLITE_AFF_NONE;
 1.75534 +        }
 1.75535 +        keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 1.75536 +        keyInfo.aSortOrder = &sortOrder;
 1.75537 +
 1.75538 +        /* Loop through each expression in <exprlist>. */
 1.75539 +        r1 = sqlite3GetTempReg(pParse);
 1.75540 +        r2 = sqlite3GetTempReg(pParse);
 1.75541 +        sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
 1.75542 +        for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
 1.75543 +          Expr *pE2 = pItem->pExpr;
 1.75544 +          int iValToIns;
 1.75545 +
 1.75546 +          /* If the expression is not constant then we will need to
 1.75547 +          ** disable the test that was generated above that makes sure
 1.75548 +          ** this code only executes once.  Because for a non-constant
 1.75549 +          ** expression we need to rerun this code each time.
 1.75550 +          */
 1.75551 +          if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
 1.75552 +            sqlite3VdbeChangeToNoop(v, testAddr);
 1.75553 +            testAddr = -1;
 1.75554 +          }
 1.75555 +
 1.75556 +          /* Evaluate the expression and insert it into the temp table */
 1.75557 +          if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
 1.75558 +            sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
 1.75559 +          }else{
 1.75560 +            r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
 1.75561 +            if( isRowid ){
 1.75562 +              sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
 1.75563 +                                sqlite3VdbeCurrentAddr(v)+2);
 1.75564 +              sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
 1.75565 +            }else{
 1.75566 +              sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
 1.75567 +              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
 1.75568 +              sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
 1.75569 +            }
 1.75570 +          }
 1.75571 +        }
 1.75572 +        sqlite3ReleaseTempReg(pParse, r1);
 1.75573 +        sqlite3ReleaseTempReg(pParse, r2);
 1.75574 +      }
 1.75575 +      if( !isRowid ){
 1.75576 +        sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
 1.75577 +      }
 1.75578 +      break;
 1.75579 +    }
 1.75580 +
 1.75581 +    case TK_EXISTS:
 1.75582 +    case TK_SELECT:
 1.75583 +    default: {
 1.75584 +      /* If this has to be a scalar SELECT.  Generate code to put the
 1.75585 +      ** value of this select in a memory cell and record the number
 1.75586 +      ** of the memory cell in iColumn.  If this is an EXISTS, write
 1.75587 +      ** an integer 0 (not exists) or 1 (exists) into a memory cell
 1.75588 +      ** and record that memory cell in iColumn.
 1.75589 +      */
 1.75590 +      Select *pSel;                         /* SELECT statement to encode */
 1.75591 +      SelectDest dest;                      /* How to deal with SELECt result */
 1.75592 +
 1.75593 +      testcase( pExpr->op==TK_EXISTS );
 1.75594 +      testcase( pExpr->op==TK_SELECT );
 1.75595 +      assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 1.75596 +
 1.75597 +      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 1.75598 +      pSel = pExpr->x.pSelect;
 1.75599 +      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
 1.75600 +      if( pExpr->op==TK_SELECT ){
 1.75601 +        dest.eDest = SRT_Mem;
 1.75602 +        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
 1.75603 +        VdbeComment((v, "Init subquery result"));
 1.75604 +      }else{
 1.75605 +        dest.eDest = SRT_Exists;
 1.75606 +        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 1.75607 +        VdbeComment((v, "Init EXISTS result"));
 1.75608 +      }
 1.75609 +      sqlite3ExprDelete(pParse->db, pSel->pLimit);
 1.75610 +      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
 1.75611 +                                  &sqlite3IntTokens[1]);
 1.75612 +      pSel->iLimit = 0;
 1.75613 +      if( sqlite3Select(pParse, pSel, &dest) ){
 1.75614 +        return 0;
 1.75615 +      }
 1.75616 +      rReg = dest.iSDParm;
 1.75617 +      ExprSetIrreducible(pExpr);
 1.75618 +      break;
 1.75619 +    }
 1.75620 +  }
 1.75621 +
 1.75622 +  if( testAddr>=0 ){
 1.75623 +    sqlite3VdbeJumpHere(v, testAddr);
 1.75624 +  }
 1.75625 +  sqlite3ExprCachePop(pParse, 1);
 1.75626 +
 1.75627 +  return rReg;
 1.75628 +}
 1.75629 +#endif /* SQLITE_OMIT_SUBQUERY */
 1.75630 +
 1.75631 +#ifndef SQLITE_OMIT_SUBQUERY
 1.75632 +/*
 1.75633 +** Generate code for an IN expression.
 1.75634 +**
 1.75635 +**      x IN (SELECT ...)
 1.75636 +**      x IN (value, value, ...)
 1.75637 +**
 1.75638 +** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
 1.75639 +** is an array of zero or more values.  The expression is true if the LHS is
 1.75640 +** contained within the RHS.  The value of the expression is unknown (NULL)
 1.75641 +** if the LHS is NULL or if the LHS is not contained within the RHS and the
 1.75642 +** RHS contains one or more NULL values.
 1.75643 +**
 1.75644 +** This routine generates code will jump to destIfFalse if the LHS is not 
 1.75645 +** contained within the RHS.  If due to NULLs we cannot determine if the LHS
 1.75646 +** is contained in the RHS then jump to destIfNull.  If the LHS is contained
 1.75647 +** within the RHS then fall through.
 1.75648 +*/
 1.75649 +static void sqlite3ExprCodeIN(
 1.75650 +  Parse *pParse,        /* Parsing and code generating context */
 1.75651 +  Expr *pExpr,          /* The IN expression */
 1.75652 +  int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
 1.75653 +  int destIfNull        /* Jump here if the results are unknown due to NULLs */
 1.75654 +){
 1.75655 +  int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
 1.75656 +  char affinity;        /* Comparison affinity to use */
 1.75657 +  int eType;            /* Type of the RHS */
 1.75658 +  int r1;               /* Temporary use register */
 1.75659 +  Vdbe *v;              /* Statement under construction */
 1.75660 +
 1.75661 +  /* Compute the RHS.   After this step, the table with cursor
 1.75662 +  ** pExpr->iTable will contains the values that make up the RHS.
 1.75663 +  */
 1.75664 +  v = pParse->pVdbe;
 1.75665 +  assert( v!=0 );       /* OOM detected prior to this routine */
 1.75666 +  VdbeNoopComment((v, "begin IN expr"));
 1.75667 +  eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
 1.75668 +
 1.75669 +  /* Figure out the affinity to use to create a key from the results
 1.75670 +  ** of the expression. affinityStr stores a static string suitable for
 1.75671 +  ** P4 of OP_MakeRecord.
 1.75672 +  */
 1.75673 +  affinity = comparisonAffinity(pExpr);
 1.75674 +
 1.75675 +  /* Code the LHS, the <expr> from "<expr> IN (...)".
 1.75676 +  */
 1.75677 +  sqlite3ExprCachePush(pParse);
 1.75678 +  r1 = sqlite3GetTempReg(pParse);
 1.75679 +  sqlite3ExprCode(pParse, pExpr->pLeft, r1);
 1.75680 +
 1.75681 +  /* If the LHS is NULL, then the result is either false or NULL depending
 1.75682 +  ** on whether the RHS is empty or not, respectively.
 1.75683 +  */
 1.75684 +  if( destIfNull==destIfFalse ){
 1.75685 +    /* Shortcut for the common case where the false and NULL outcomes are
 1.75686 +    ** the same. */
 1.75687 +    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
 1.75688 +  }else{
 1.75689 +    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
 1.75690 +    sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
 1.75691 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
 1.75692 +    sqlite3VdbeJumpHere(v, addr1);
 1.75693 +  }
 1.75694 +
 1.75695 +  if( eType==IN_INDEX_ROWID ){
 1.75696 +    /* In this case, the RHS is the ROWID of table b-tree
 1.75697 +    */
 1.75698 +    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
 1.75699 +    sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
 1.75700 +  }else{
 1.75701 +    /* In this case, the RHS is an index b-tree.
 1.75702 +    */
 1.75703 +    sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
 1.75704 +
 1.75705 +    /* If the set membership test fails, then the result of the 
 1.75706 +    ** "x IN (...)" expression must be either 0 or NULL. If the set
 1.75707 +    ** contains no NULL values, then the result is 0. If the set 
 1.75708 +    ** contains one or more NULL values, then the result of the
 1.75709 +    ** expression is also NULL.
 1.75710 +    */
 1.75711 +    if( rRhsHasNull==0 || destIfFalse==destIfNull ){
 1.75712 +      /* This branch runs if it is known at compile time that the RHS
 1.75713 +      ** cannot contain NULL values. This happens as the result
 1.75714 +      ** of a "NOT NULL" constraint in the database schema.
 1.75715 +      **
 1.75716 +      ** Also run this branch if NULL is equivalent to FALSE
 1.75717 +      ** for this particular IN operator.
 1.75718 +      */
 1.75719 +      sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
 1.75720 +
 1.75721 +    }else{
 1.75722 +      /* In this branch, the RHS of the IN might contain a NULL and
 1.75723 +      ** the presence of a NULL on the RHS makes a difference in the
 1.75724 +      ** outcome.
 1.75725 +      */
 1.75726 +      int j1, j2, j3;
 1.75727 +
 1.75728 +      /* First check to see if the LHS is contained in the RHS.  If so,
 1.75729 +      ** then the presence of NULLs in the RHS does not matter, so jump
 1.75730 +      ** over all of the code that follows.
 1.75731 +      */
 1.75732 +      j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
 1.75733 +
 1.75734 +      /* Here we begin generating code that runs if the LHS is not
 1.75735 +      ** contained within the RHS.  Generate additional code that
 1.75736 +      ** tests the RHS for NULLs.  If the RHS contains a NULL then
 1.75737 +      ** jump to destIfNull.  If there are no NULLs in the RHS then
 1.75738 +      ** jump to destIfFalse.
 1.75739 +      */
 1.75740 +      j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
 1.75741 +      j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
 1.75742 +      sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
 1.75743 +      sqlite3VdbeJumpHere(v, j3);
 1.75744 +      sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
 1.75745 +      sqlite3VdbeJumpHere(v, j2);
 1.75746 +
 1.75747 +      /* Jump to the appropriate target depending on whether or not
 1.75748 +      ** the RHS contains a NULL
 1.75749 +      */
 1.75750 +      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
 1.75751 +      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
 1.75752 +
 1.75753 +      /* The OP_Found at the top of this branch jumps here when true, 
 1.75754 +      ** causing the overall IN expression evaluation to fall through.
 1.75755 +      */
 1.75756 +      sqlite3VdbeJumpHere(v, j1);
 1.75757 +    }
 1.75758 +  }
 1.75759 +  sqlite3ReleaseTempReg(pParse, r1);
 1.75760 +  sqlite3ExprCachePop(pParse, 1);
 1.75761 +  VdbeComment((v, "end IN expr"));
 1.75762 +}
 1.75763 +#endif /* SQLITE_OMIT_SUBQUERY */
 1.75764 +
 1.75765 +/*
 1.75766 +** Duplicate an 8-byte value
 1.75767 +*/
 1.75768 +static char *dup8bytes(Vdbe *v, const char *in){
 1.75769 +  char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
 1.75770 +  if( out ){
 1.75771 +    memcpy(out, in, 8);
 1.75772 +  }
 1.75773 +  return out;
 1.75774 +}
 1.75775 +
 1.75776 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.75777 +/*
 1.75778 +** Generate an instruction that will put the floating point
 1.75779 +** value described by z[0..n-1] into register iMem.
 1.75780 +**
 1.75781 +** The z[] string will probably not be zero-terminated.  But the 
 1.75782 +** z[n] character is guaranteed to be something that does not look
 1.75783 +** like the continuation of the number.
 1.75784 +*/
 1.75785 +static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
 1.75786 +  if( ALWAYS(z!=0) ){
 1.75787 +    double value;
 1.75788 +    char *zV;
 1.75789 +    sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 1.75790 +    assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
 1.75791 +    if( negateFlag ) value = -value;
 1.75792 +    zV = dup8bytes(v, (char*)&value);
 1.75793 +    sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
 1.75794 +  }
 1.75795 +}
 1.75796 +#endif
 1.75797 +
 1.75798 +
 1.75799 +/*
 1.75800 +** Generate an instruction that will put the integer describe by
 1.75801 +** text z[0..n-1] into register iMem.
 1.75802 +**
 1.75803 +** Expr.u.zToken is always UTF8 and zero-terminated.
 1.75804 +*/
 1.75805 +static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
 1.75806 +  Vdbe *v = pParse->pVdbe;
 1.75807 +  if( pExpr->flags & EP_IntValue ){
 1.75808 +    int i = pExpr->u.iValue;
 1.75809 +    assert( i>=0 );
 1.75810 +    if( negFlag ) i = -i;
 1.75811 +    sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
 1.75812 +  }else{
 1.75813 +    int c;
 1.75814 +    i64 value;
 1.75815 +    const char *z = pExpr->u.zToken;
 1.75816 +    assert( z!=0 );
 1.75817 +    c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 1.75818 +    if( c==0 || (c==2 && negFlag) ){
 1.75819 +      char *zV;
 1.75820 +      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
 1.75821 +      zV = dup8bytes(v, (char*)&value);
 1.75822 +      sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
 1.75823 +    }else{
 1.75824 +#ifdef SQLITE_OMIT_FLOATING_POINT
 1.75825 +      sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
 1.75826 +#else
 1.75827 +      codeReal(v, z, negFlag, iMem);
 1.75828 +#endif
 1.75829 +    }
 1.75830 +  }
 1.75831 +}
 1.75832 +
 1.75833 +/*
 1.75834 +** Clear a cache entry.
 1.75835 +*/
 1.75836 +static void cacheEntryClear(Parse *pParse, struct yColCache *p){
 1.75837 +  if( p->tempReg ){
 1.75838 +    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 1.75839 +      pParse->aTempReg[pParse->nTempReg++] = p->iReg;
 1.75840 +    }
 1.75841 +    p->tempReg = 0;
 1.75842 +  }
 1.75843 +}
 1.75844 +
 1.75845 +
 1.75846 +/*
 1.75847 +** Record in the column cache that a particular column from a
 1.75848 +** particular table is stored in a particular register.
 1.75849 +*/
 1.75850 +SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
 1.75851 +  int i;
 1.75852 +  int minLru;
 1.75853 +  int idxLru;
 1.75854 +  struct yColCache *p;
 1.75855 +
 1.75856 +  assert( iReg>0 );  /* Register numbers are always positive */
 1.75857 +  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
 1.75858 +
 1.75859 +  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
 1.75860 +  ** for testing only - to verify that SQLite always gets the same answer
 1.75861 +  ** with and without the column cache.
 1.75862 +  */
 1.75863 +  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
 1.75864 +
 1.75865 +  /* First replace any existing entry.
 1.75866 +  **
 1.75867 +  ** Actually, the way the column cache is currently used, we are guaranteed
 1.75868 +  ** that the object will never already be in cache.  Verify this guarantee.
 1.75869 +  */
 1.75870 +#ifndef NDEBUG
 1.75871 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.75872 +    assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
 1.75873 +  }
 1.75874 +#endif
 1.75875 +
 1.75876 +  /* Find an empty slot and replace it */
 1.75877 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.75878 +    if( p->iReg==0 ){
 1.75879 +      p->iLevel = pParse->iCacheLevel;
 1.75880 +      p->iTable = iTab;
 1.75881 +      p->iColumn = iCol;
 1.75882 +      p->iReg = iReg;
 1.75883 +      p->tempReg = 0;
 1.75884 +      p->lru = pParse->iCacheCnt++;
 1.75885 +      return;
 1.75886 +    }
 1.75887 +  }
 1.75888 +
 1.75889 +  /* Replace the last recently used */
 1.75890 +  minLru = 0x7fffffff;
 1.75891 +  idxLru = -1;
 1.75892 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.75893 +    if( p->lru<minLru ){
 1.75894 +      idxLru = i;
 1.75895 +      minLru = p->lru;
 1.75896 +    }
 1.75897 +  }
 1.75898 +  if( ALWAYS(idxLru>=0) ){
 1.75899 +    p = &pParse->aColCache[idxLru];
 1.75900 +    p->iLevel = pParse->iCacheLevel;
 1.75901 +    p->iTable = iTab;
 1.75902 +    p->iColumn = iCol;
 1.75903 +    p->iReg = iReg;
 1.75904 +    p->tempReg = 0;
 1.75905 +    p->lru = pParse->iCacheCnt++;
 1.75906 +    return;
 1.75907 +  }
 1.75908 +}
 1.75909 +
 1.75910 +/*
 1.75911 +** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
 1.75912 +** Purge the range of registers from the column cache.
 1.75913 +*/
 1.75914 +SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
 1.75915 +  int i;
 1.75916 +  int iLast = iReg + nReg - 1;
 1.75917 +  struct yColCache *p;
 1.75918 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.75919 +    int r = p->iReg;
 1.75920 +    if( r>=iReg && r<=iLast ){
 1.75921 +      cacheEntryClear(pParse, p);
 1.75922 +      p->iReg = 0;
 1.75923 +    }
 1.75924 +  }
 1.75925 +}
 1.75926 +
 1.75927 +/*
 1.75928 +** Remember the current column cache context.  Any new entries added
 1.75929 +** added to the column cache after this call are removed when the
 1.75930 +** corresponding pop occurs.
 1.75931 +*/
 1.75932 +SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
 1.75933 +  pParse->iCacheLevel++;
 1.75934 +}
 1.75935 +
 1.75936 +/*
 1.75937 +** Remove from the column cache any entries that were added since the
 1.75938 +** the previous N Push operations.  In other words, restore the cache
 1.75939 +** to the state it was in N Pushes ago.
 1.75940 +*/
 1.75941 +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
 1.75942 +  int i;
 1.75943 +  struct yColCache *p;
 1.75944 +  assert( N>0 );
 1.75945 +  assert( pParse->iCacheLevel>=N );
 1.75946 +  pParse->iCacheLevel -= N;
 1.75947 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.75948 +    if( p->iReg && p->iLevel>pParse->iCacheLevel ){
 1.75949 +      cacheEntryClear(pParse, p);
 1.75950 +      p->iReg = 0;
 1.75951 +    }
 1.75952 +  }
 1.75953 +}
 1.75954 +
 1.75955 +/*
 1.75956 +** When a cached column is reused, make sure that its register is
 1.75957 +** no longer available as a temp register.  ticket #3879:  that same
 1.75958 +** register might be in the cache in multiple places, so be sure to
 1.75959 +** get them all.
 1.75960 +*/
 1.75961 +static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
 1.75962 +  int i;
 1.75963 +  struct yColCache *p;
 1.75964 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.75965 +    if( p->iReg==iReg ){
 1.75966 +      p->tempReg = 0;
 1.75967 +    }
 1.75968 +  }
 1.75969 +}
 1.75970 +
 1.75971 +/*
 1.75972 +** Generate code to extract the value of the iCol-th column of a table.
 1.75973 +*/
 1.75974 +SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
 1.75975 +  Vdbe *v,        /* The VDBE under construction */
 1.75976 +  Table *pTab,    /* The table containing the value */
 1.75977 +  int iTabCur,    /* The cursor for this table */
 1.75978 +  int iCol,       /* Index of the column to extract */
 1.75979 +  int regOut      /* Extract the valud into this register */
 1.75980 +){
 1.75981 +  if( iCol<0 || iCol==pTab->iPKey ){
 1.75982 +    sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
 1.75983 +  }else{
 1.75984 +    int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
 1.75985 +    sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
 1.75986 +  }
 1.75987 +  if( iCol>=0 ){
 1.75988 +    sqlite3ColumnDefault(v, pTab, iCol, regOut);
 1.75989 +  }
 1.75990 +}
 1.75991 +
 1.75992 +/*
 1.75993 +** Generate code that will extract the iColumn-th column from
 1.75994 +** table pTab and store the column value in a register.  An effort
 1.75995 +** is made to store the column value in register iReg, but this is
 1.75996 +** not guaranteed.  The location of the column value is returned.
 1.75997 +**
 1.75998 +** There must be an open cursor to pTab in iTable when this routine
 1.75999 +** is called.  If iColumn<0 then code is generated that extracts the rowid.
 1.76000 +*/
 1.76001 +SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
 1.76002 +  Parse *pParse,   /* Parsing and code generating context */
 1.76003 +  Table *pTab,     /* Description of the table we are reading from */
 1.76004 +  int iColumn,     /* Index of the table column */
 1.76005 +  int iTable,      /* The cursor pointing to the table */
 1.76006 +  int iReg,        /* Store results here */
 1.76007 +  u8 p5            /* P5 value for OP_Column */
 1.76008 +){
 1.76009 +  Vdbe *v = pParse->pVdbe;
 1.76010 +  int i;
 1.76011 +  struct yColCache *p;
 1.76012 +
 1.76013 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.76014 +    if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
 1.76015 +      p->lru = pParse->iCacheCnt++;
 1.76016 +      sqlite3ExprCachePinRegister(pParse, p->iReg);
 1.76017 +      return p->iReg;
 1.76018 +    }
 1.76019 +  }  
 1.76020 +  assert( v!=0 );
 1.76021 +  sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
 1.76022 +  if( p5 ){
 1.76023 +    sqlite3VdbeChangeP5(v, p5);
 1.76024 +  }else{   
 1.76025 +    sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
 1.76026 +  }
 1.76027 +  return iReg;
 1.76028 +}
 1.76029 +
 1.76030 +/*
 1.76031 +** Clear all column cache entries.
 1.76032 +*/
 1.76033 +SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
 1.76034 +  int i;
 1.76035 +  struct yColCache *p;
 1.76036 +
 1.76037 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.76038 +    if( p->iReg ){
 1.76039 +      cacheEntryClear(pParse, p);
 1.76040 +      p->iReg = 0;
 1.76041 +    }
 1.76042 +  }
 1.76043 +}
 1.76044 +
 1.76045 +/*
 1.76046 +** Record the fact that an affinity change has occurred on iCount
 1.76047 +** registers starting with iStart.
 1.76048 +*/
 1.76049 +SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
 1.76050 +  sqlite3ExprCacheRemove(pParse, iStart, iCount);
 1.76051 +}
 1.76052 +
 1.76053 +/*
 1.76054 +** Generate code to move content from registers iFrom...iFrom+nReg-1
 1.76055 +** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
 1.76056 +*/
 1.76057 +SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
 1.76058 +  int i;
 1.76059 +  struct yColCache *p;
 1.76060 +  assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
 1.76061 +  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
 1.76062 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.76063 +    int x = p->iReg;
 1.76064 +    if( x>=iFrom && x<iFrom+nReg ){
 1.76065 +      p->iReg += iTo-iFrom;
 1.76066 +    }
 1.76067 +  }
 1.76068 +}
 1.76069 +
 1.76070 +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 1.76071 +/*
 1.76072 +** Return true if any register in the range iFrom..iTo (inclusive)
 1.76073 +** is used as part of the column cache.
 1.76074 +**
 1.76075 +** This routine is used within assert() and testcase() macros only
 1.76076 +** and does not appear in a normal build.
 1.76077 +*/
 1.76078 +static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
 1.76079 +  int i;
 1.76080 +  struct yColCache *p;
 1.76081 +  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.76082 +    int r = p->iReg;
 1.76083 +    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
 1.76084 +  }
 1.76085 +  return 0;
 1.76086 +}
 1.76087 +#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
 1.76088 +
 1.76089 +/*
 1.76090 +** Generate code into the current Vdbe to evaluate the given
 1.76091 +** expression.  Attempt to store the results in register "target".
 1.76092 +** Return the register where results are stored.
 1.76093 +**
 1.76094 +** With this routine, there is no guarantee that results will
 1.76095 +** be stored in target.  The result might be stored in some other
 1.76096 +** register if it is convenient to do so.  The calling function
 1.76097 +** must check the return code and move the results to the desired
 1.76098 +** register.
 1.76099 +*/
 1.76100 +SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
 1.76101 +  Vdbe *v = pParse->pVdbe;  /* The VM under construction */
 1.76102 +  int op;                   /* The opcode being coded */
 1.76103 +  int inReg = target;       /* Results stored in register inReg */
 1.76104 +  int regFree1 = 0;         /* If non-zero free this temporary register */
 1.76105 +  int regFree2 = 0;         /* If non-zero free this temporary register */
 1.76106 +  int r1, r2, r3, r4;       /* Various register numbers */
 1.76107 +  sqlite3 *db = pParse->db; /* The database connection */
 1.76108 +
 1.76109 +  assert( target>0 && target<=pParse->nMem );
 1.76110 +  if( v==0 ){
 1.76111 +    assert( pParse->db->mallocFailed );
 1.76112 +    return 0;
 1.76113 +  }
 1.76114 +
 1.76115 +  if( pExpr==0 ){
 1.76116 +    op = TK_NULL;
 1.76117 +  }else{
 1.76118 +    op = pExpr->op;
 1.76119 +  }
 1.76120 +  switch( op ){
 1.76121 +    case TK_AGG_COLUMN: {
 1.76122 +      AggInfo *pAggInfo = pExpr->pAggInfo;
 1.76123 +      struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
 1.76124 +      if( !pAggInfo->directMode ){
 1.76125 +        assert( pCol->iMem>0 );
 1.76126 +        inReg = pCol->iMem;
 1.76127 +        break;
 1.76128 +      }else if( pAggInfo->useSortingIdx ){
 1.76129 +        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
 1.76130 +                              pCol->iSorterColumn, target);
 1.76131 +        break;
 1.76132 +      }
 1.76133 +      /* Otherwise, fall thru into the TK_COLUMN case */
 1.76134 +    }
 1.76135 +    case TK_COLUMN: {
 1.76136 +      if( pExpr->iTable<0 ){
 1.76137 +        /* This only happens when coding check constraints */
 1.76138 +        assert( pParse->ckBase>0 );
 1.76139 +        inReg = pExpr->iColumn + pParse->ckBase;
 1.76140 +      }else{
 1.76141 +        inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 1.76142 +                                 pExpr->iColumn, pExpr->iTable, target,
 1.76143 +                                 pExpr->op2);
 1.76144 +      }
 1.76145 +      break;
 1.76146 +    }
 1.76147 +    case TK_INTEGER: {
 1.76148 +      codeInteger(pParse, pExpr, 0, target);
 1.76149 +      break;
 1.76150 +    }
 1.76151 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.76152 +    case TK_FLOAT: {
 1.76153 +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76154 +      codeReal(v, pExpr->u.zToken, 0, target);
 1.76155 +      break;
 1.76156 +    }
 1.76157 +#endif
 1.76158 +    case TK_STRING: {
 1.76159 +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76160 +      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
 1.76161 +      break;
 1.76162 +    }
 1.76163 +    case TK_NULL: {
 1.76164 +      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 1.76165 +      break;
 1.76166 +    }
 1.76167 +#ifndef SQLITE_OMIT_BLOB_LITERAL
 1.76168 +    case TK_BLOB: {
 1.76169 +      int n;
 1.76170 +      const char *z;
 1.76171 +      char *zBlob;
 1.76172 +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76173 +      assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 1.76174 +      assert( pExpr->u.zToken[1]=='\'' );
 1.76175 +      z = &pExpr->u.zToken[2];
 1.76176 +      n = sqlite3Strlen30(z) - 1;
 1.76177 +      assert( z[n]=='\'' );
 1.76178 +      zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
 1.76179 +      sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
 1.76180 +      break;
 1.76181 +    }
 1.76182 +#endif
 1.76183 +    case TK_VARIABLE: {
 1.76184 +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76185 +      assert( pExpr->u.zToken!=0 );
 1.76186 +      assert( pExpr->u.zToken[0]!=0 );
 1.76187 +      sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
 1.76188 +      if( pExpr->u.zToken[1]!=0 ){
 1.76189 +        assert( pExpr->u.zToken[0]=='?' 
 1.76190 +             || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
 1.76191 +        sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
 1.76192 +      }
 1.76193 +      break;
 1.76194 +    }
 1.76195 +    case TK_REGISTER: {
 1.76196 +      inReg = pExpr->iTable;
 1.76197 +      break;
 1.76198 +    }
 1.76199 +    case TK_AS: {
 1.76200 +      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 1.76201 +      break;
 1.76202 +    }
 1.76203 +#ifndef SQLITE_OMIT_CAST
 1.76204 +    case TK_CAST: {
 1.76205 +      /* Expressions of the form:   CAST(pLeft AS token) */
 1.76206 +      int aff, to_op;
 1.76207 +      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 1.76208 +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76209 +      aff = sqlite3AffinityType(pExpr->u.zToken);
 1.76210 +      to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
 1.76211 +      assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
 1.76212 +      assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
 1.76213 +      assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
 1.76214 +      assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
 1.76215 +      assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
 1.76216 +      testcase( to_op==OP_ToText );
 1.76217 +      testcase( to_op==OP_ToBlob );
 1.76218 +      testcase( to_op==OP_ToNumeric );
 1.76219 +      testcase( to_op==OP_ToInt );
 1.76220 +      testcase( to_op==OP_ToReal );
 1.76221 +      if( inReg!=target ){
 1.76222 +        sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
 1.76223 +        inReg = target;
 1.76224 +      }
 1.76225 +      sqlite3VdbeAddOp1(v, to_op, inReg);
 1.76226 +      testcase( usedAsColumnCache(pParse, inReg, inReg) );
 1.76227 +      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
 1.76228 +      break;
 1.76229 +    }
 1.76230 +#endif /* SQLITE_OMIT_CAST */
 1.76231 +    case TK_LT:
 1.76232 +    case TK_LE:
 1.76233 +    case TK_GT:
 1.76234 +    case TK_GE:
 1.76235 +    case TK_NE:
 1.76236 +    case TK_EQ: {
 1.76237 +      assert( TK_LT==OP_Lt );
 1.76238 +      assert( TK_LE==OP_Le );
 1.76239 +      assert( TK_GT==OP_Gt );
 1.76240 +      assert( TK_GE==OP_Ge );
 1.76241 +      assert( TK_EQ==OP_Eq );
 1.76242 +      assert( TK_NE==OP_Ne );
 1.76243 +      testcase( op==TK_LT );
 1.76244 +      testcase( op==TK_LE );
 1.76245 +      testcase( op==TK_GT );
 1.76246 +      testcase( op==TK_GE );
 1.76247 +      testcase( op==TK_EQ );
 1.76248 +      testcase( op==TK_NE );
 1.76249 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.76250 +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 1.76251 +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 1.76252 +                  r1, r2, inReg, SQLITE_STOREP2);
 1.76253 +      testcase( regFree1==0 );
 1.76254 +      testcase( regFree2==0 );
 1.76255 +      break;
 1.76256 +    }
 1.76257 +    case TK_IS:
 1.76258 +    case TK_ISNOT: {
 1.76259 +      testcase( op==TK_IS );
 1.76260 +      testcase( op==TK_ISNOT );
 1.76261 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.76262 +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 1.76263 +      op = (op==TK_IS) ? TK_EQ : TK_NE;
 1.76264 +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 1.76265 +                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
 1.76266 +      testcase( regFree1==0 );
 1.76267 +      testcase( regFree2==0 );
 1.76268 +      break;
 1.76269 +    }
 1.76270 +    case TK_AND:
 1.76271 +    case TK_OR:
 1.76272 +    case TK_PLUS:
 1.76273 +    case TK_STAR:
 1.76274 +    case TK_MINUS:
 1.76275 +    case TK_REM:
 1.76276 +    case TK_BITAND:
 1.76277 +    case TK_BITOR:
 1.76278 +    case TK_SLASH:
 1.76279 +    case TK_LSHIFT:
 1.76280 +    case TK_RSHIFT: 
 1.76281 +    case TK_CONCAT: {
 1.76282 +      assert( TK_AND==OP_And );
 1.76283 +      assert( TK_OR==OP_Or );
 1.76284 +      assert( TK_PLUS==OP_Add );
 1.76285 +      assert( TK_MINUS==OP_Subtract );
 1.76286 +      assert( TK_REM==OP_Remainder );
 1.76287 +      assert( TK_BITAND==OP_BitAnd );
 1.76288 +      assert( TK_BITOR==OP_BitOr );
 1.76289 +      assert( TK_SLASH==OP_Divide );
 1.76290 +      assert( TK_LSHIFT==OP_ShiftLeft );
 1.76291 +      assert( TK_RSHIFT==OP_ShiftRight );
 1.76292 +      assert( TK_CONCAT==OP_Concat );
 1.76293 +      testcase( op==TK_AND );
 1.76294 +      testcase( op==TK_OR );
 1.76295 +      testcase( op==TK_PLUS );
 1.76296 +      testcase( op==TK_MINUS );
 1.76297 +      testcase( op==TK_REM );
 1.76298 +      testcase( op==TK_BITAND );
 1.76299 +      testcase( op==TK_BITOR );
 1.76300 +      testcase( op==TK_SLASH );
 1.76301 +      testcase( op==TK_LSHIFT );
 1.76302 +      testcase( op==TK_RSHIFT );
 1.76303 +      testcase( op==TK_CONCAT );
 1.76304 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.76305 +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 1.76306 +      sqlite3VdbeAddOp3(v, op, r2, r1, target);
 1.76307 +      testcase( regFree1==0 );
 1.76308 +      testcase( regFree2==0 );
 1.76309 +      break;
 1.76310 +    }
 1.76311 +    case TK_UMINUS: {
 1.76312 +      Expr *pLeft = pExpr->pLeft;
 1.76313 +      assert( pLeft );
 1.76314 +      if( pLeft->op==TK_INTEGER ){
 1.76315 +        codeInteger(pParse, pLeft, 1, target);
 1.76316 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.76317 +      }else if( pLeft->op==TK_FLOAT ){
 1.76318 +        assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76319 +        codeReal(v, pLeft->u.zToken, 1, target);
 1.76320 +#endif
 1.76321 +      }else{
 1.76322 +        regFree1 = r1 = sqlite3GetTempReg(pParse);
 1.76323 +        sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
 1.76324 +        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
 1.76325 +        sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
 1.76326 +        testcase( regFree2==0 );
 1.76327 +      }
 1.76328 +      inReg = target;
 1.76329 +      break;
 1.76330 +    }
 1.76331 +    case TK_BITNOT:
 1.76332 +    case TK_NOT: {
 1.76333 +      assert( TK_BITNOT==OP_BitNot );
 1.76334 +      assert( TK_NOT==OP_Not );
 1.76335 +      testcase( op==TK_BITNOT );
 1.76336 +      testcase( op==TK_NOT );
 1.76337 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.76338 +      testcase( regFree1==0 );
 1.76339 +      inReg = target;
 1.76340 +      sqlite3VdbeAddOp2(v, op, r1, inReg);
 1.76341 +      break;
 1.76342 +    }
 1.76343 +    case TK_ISNULL:
 1.76344 +    case TK_NOTNULL: {
 1.76345 +      int addr;
 1.76346 +      assert( TK_ISNULL==OP_IsNull );
 1.76347 +      assert( TK_NOTNULL==OP_NotNull );
 1.76348 +      testcase( op==TK_ISNULL );
 1.76349 +      testcase( op==TK_NOTNULL );
 1.76350 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 1.76351 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.76352 +      testcase( regFree1==0 );
 1.76353 +      addr = sqlite3VdbeAddOp1(v, op, r1);
 1.76354 +      sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
 1.76355 +      sqlite3VdbeJumpHere(v, addr);
 1.76356 +      break;
 1.76357 +    }
 1.76358 +    case TK_AGG_FUNCTION: {
 1.76359 +      AggInfo *pInfo = pExpr->pAggInfo;
 1.76360 +      if( pInfo==0 ){
 1.76361 +        assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76362 +        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
 1.76363 +      }else{
 1.76364 +        inReg = pInfo->aFunc[pExpr->iAgg].iMem;
 1.76365 +      }
 1.76366 +      break;
 1.76367 +    }
 1.76368 +    case TK_CONST_FUNC:
 1.76369 +    case TK_FUNCTION: {
 1.76370 +      ExprList *pFarg;       /* List of function arguments */
 1.76371 +      int nFarg;             /* Number of function arguments */
 1.76372 +      FuncDef *pDef;         /* The function definition object */
 1.76373 +      int nId;               /* Length of the function name in bytes */
 1.76374 +      const char *zId;       /* The function name */
 1.76375 +      int constMask = 0;     /* Mask of function arguments that are constant */
 1.76376 +      int i;                 /* Loop counter */
 1.76377 +      u8 enc = ENC(db);      /* The text encoding used by this database */
 1.76378 +      CollSeq *pColl = 0;    /* A collating sequence */
 1.76379 +
 1.76380 +      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 1.76381 +      testcase( op==TK_CONST_FUNC );
 1.76382 +      testcase( op==TK_FUNCTION );
 1.76383 +      if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
 1.76384 +        pFarg = 0;
 1.76385 +      }else{
 1.76386 +        pFarg = pExpr->x.pList;
 1.76387 +      }
 1.76388 +      nFarg = pFarg ? pFarg->nExpr : 0;
 1.76389 +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76390 +      zId = pExpr->u.zToken;
 1.76391 +      nId = sqlite3Strlen30(zId);
 1.76392 +      pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
 1.76393 +      if( pDef==0 ){
 1.76394 +        sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
 1.76395 +        break;
 1.76396 +      }
 1.76397 +
 1.76398 +      /* Attempt a direct implementation of the built-in COALESCE() and
 1.76399 +      ** IFNULL() functions.  This avoids unnecessary evalation of
 1.76400 +      ** arguments past the first non-NULL argument.
 1.76401 +      */
 1.76402 +      if( pDef->flags & SQLITE_FUNC_COALESCE ){
 1.76403 +        int endCoalesce = sqlite3VdbeMakeLabel(v);
 1.76404 +        assert( nFarg>=2 );
 1.76405 +        sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
 1.76406 +        for(i=1; i<nFarg; i++){
 1.76407 +          sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
 1.76408 +          sqlite3ExprCacheRemove(pParse, target, 1);
 1.76409 +          sqlite3ExprCachePush(pParse);
 1.76410 +          sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
 1.76411 +          sqlite3ExprCachePop(pParse, 1);
 1.76412 +        }
 1.76413 +        sqlite3VdbeResolveLabel(v, endCoalesce);
 1.76414 +        break;
 1.76415 +      }
 1.76416 +
 1.76417 +
 1.76418 +      if( pFarg ){
 1.76419 +        r1 = sqlite3GetTempRange(pParse, nFarg);
 1.76420 +
 1.76421 +        /* For length() and typeof() functions with a column argument,
 1.76422 +        ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
 1.76423 +        ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
 1.76424 +        ** loading.
 1.76425 +        */
 1.76426 +        if( (pDef->flags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
 1.76427 +          u8 exprOp;
 1.76428 +          assert( nFarg==1 );
 1.76429 +          assert( pFarg->a[0].pExpr!=0 );
 1.76430 +          exprOp = pFarg->a[0].pExpr->op;
 1.76431 +          if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
 1.76432 +            assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
 1.76433 +            assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
 1.76434 +            testcase( pDef->flags==SQLITE_FUNC_LENGTH );
 1.76435 +            pFarg->a[0].pExpr->op2 = pDef->flags;
 1.76436 +          }
 1.76437 +        }
 1.76438 +
 1.76439 +        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
 1.76440 +        sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
 1.76441 +        sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
 1.76442 +      }else{
 1.76443 +        r1 = 0;
 1.76444 +      }
 1.76445 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.76446 +      /* Possibly overload the function if the first argument is
 1.76447 +      ** a virtual table column.
 1.76448 +      **
 1.76449 +      ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
 1.76450 +      ** second argument, not the first, as the argument to test to
 1.76451 +      ** see if it is a column in a virtual table.  This is done because
 1.76452 +      ** the left operand of infix functions (the operand we want to
 1.76453 +      ** control overloading) ends up as the second argument to the
 1.76454 +      ** function.  The expression "A glob B" is equivalent to 
 1.76455 +      ** "glob(B,A).  We want to use the A in "A glob B" to test
 1.76456 +      ** for function overloading.  But we use the B term in "glob(B,A)".
 1.76457 +      */
 1.76458 +      if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
 1.76459 +        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
 1.76460 +      }else if( nFarg>0 ){
 1.76461 +        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
 1.76462 +      }
 1.76463 +#endif
 1.76464 +      for(i=0; i<nFarg; i++){
 1.76465 +        if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
 1.76466 +          constMask |= (1<<i);
 1.76467 +        }
 1.76468 +        if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
 1.76469 +          pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
 1.76470 +        }
 1.76471 +      }
 1.76472 +      if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
 1.76473 +        if( !pColl ) pColl = db->pDfltColl; 
 1.76474 +        sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
 1.76475 +      }
 1.76476 +      sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
 1.76477 +                        (char*)pDef, P4_FUNCDEF);
 1.76478 +      sqlite3VdbeChangeP5(v, (u8)nFarg);
 1.76479 +      if( nFarg ){
 1.76480 +        sqlite3ReleaseTempRange(pParse, r1, nFarg);
 1.76481 +      }
 1.76482 +      break;
 1.76483 +    }
 1.76484 +#ifndef SQLITE_OMIT_SUBQUERY
 1.76485 +    case TK_EXISTS:
 1.76486 +    case TK_SELECT: {
 1.76487 +      testcase( op==TK_EXISTS );
 1.76488 +      testcase( op==TK_SELECT );
 1.76489 +      inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
 1.76490 +      break;
 1.76491 +    }
 1.76492 +    case TK_IN: {
 1.76493 +      int destIfFalse = sqlite3VdbeMakeLabel(v);
 1.76494 +      int destIfNull = sqlite3VdbeMakeLabel(v);
 1.76495 +      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 1.76496 +      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 1.76497 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 1.76498 +      sqlite3VdbeResolveLabel(v, destIfFalse);
 1.76499 +      sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
 1.76500 +      sqlite3VdbeResolveLabel(v, destIfNull);
 1.76501 +      break;
 1.76502 +    }
 1.76503 +#endif /* SQLITE_OMIT_SUBQUERY */
 1.76504 +
 1.76505 +
 1.76506 +    /*
 1.76507 +    **    x BETWEEN y AND z
 1.76508 +    **
 1.76509 +    ** This is equivalent to
 1.76510 +    **
 1.76511 +    **    x>=y AND x<=z
 1.76512 +    **
 1.76513 +    ** X is stored in pExpr->pLeft.
 1.76514 +    ** Y is stored in pExpr->pList->a[0].pExpr.
 1.76515 +    ** Z is stored in pExpr->pList->a[1].pExpr.
 1.76516 +    */
 1.76517 +    case TK_BETWEEN: {
 1.76518 +      Expr *pLeft = pExpr->pLeft;
 1.76519 +      struct ExprList_item *pLItem = pExpr->x.pList->a;
 1.76520 +      Expr *pRight = pLItem->pExpr;
 1.76521 +
 1.76522 +      r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
 1.76523 +      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 1.76524 +      testcase( regFree1==0 );
 1.76525 +      testcase( regFree2==0 );
 1.76526 +      r3 = sqlite3GetTempReg(pParse);
 1.76527 +      r4 = sqlite3GetTempReg(pParse);
 1.76528 +      codeCompare(pParse, pLeft, pRight, OP_Ge,
 1.76529 +                  r1, r2, r3, SQLITE_STOREP2);
 1.76530 +      pLItem++;
 1.76531 +      pRight = pLItem->pExpr;
 1.76532 +      sqlite3ReleaseTempReg(pParse, regFree2);
 1.76533 +      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 1.76534 +      testcase( regFree2==0 );
 1.76535 +      codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
 1.76536 +      sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
 1.76537 +      sqlite3ReleaseTempReg(pParse, r3);
 1.76538 +      sqlite3ReleaseTempReg(pParse, r4);
 1.76539 +      break;
 1.76540 +    }
 1.76541 +    case TK_COLLATE: 
 1.76542 +    case TK_UPLUS: {
 1.76543 +      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 1.76544 +      break;
 1.76545 +    }
 1.76546 +
 1.76547 +    case TK_TRIGGER: {
 1.76548 +      /* If the opcode is TK_TRIGGER, then the expression is a reference
 1.76549 +      ** to a column in the new.* or old.* pseudo-tables available to
 1.76550 +      ** trigger programs. In this case Expr.iTable is set to 1 for the
 1.76551 +      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
 1.76552 +      ** is set to the column of the pseudo-table to read, or to -1 to
 1.76553 +      ** read the rowid field.
 1.76554 +      **
 1.76555 +      ** The expression is implemented using an OP_Param opcode. The p1
 1.76556 +      ** parameter is set to 0 for an old.rowid reference, or to (i+1)
 1.76557 +      ** to reference another column of the old.* pseudo-table, where 
 1.76558 +      ** i is the index of the column. For a new.rowid reference, p1 is
 1.76559 +      ** set to (n+1), where n is the number of columns in each pseudo-table.
 1.76560 +      ** For a reference to any other column in the new.* pseudo-table, p1
 1.76561 +      ** is set to (n+2+i), where n and i are as defined previously. For
 1.76562 +      ** example, if the table on which triggers are being fired is
 1.76563 +      ** declared as:
 1.76564 +      **
 1.76565 +      **   CREATE TABLE t1(a, b);
 1.76566 +      **
 1.76567 +      ** Then p1 is interpreted as follows:
 1.76568 +      **
 1.76569 +      **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
 1.76570 +      **   p1==1   ->    old.a         p1==4   ->    new.a
 1.76571 +      **   p1==2   ->    old.b         p1==5   ->    new.b       
 1.76572 +      */
 1.76573 +      Table *pTab = pExpr->pTab;
 1.76574 +      int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
 1.76575 +
 1.76576 +      assert( pExpr->iTable==0 || pExpr->iTable==1 );
 1.76577 +      assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 1.76578 +      assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 1.76579 +      assert( p1>=0 && p1<(pTab->nCol*2+2) );
 1.76580 +
 1.76581 +      sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 1.76582 +      VdbeComment((v, "%s.%s -> $%d",
 1.76583 +        (pExpr->iTable ? "new" : "old"),
 1.76584 +        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
 1.76585 +        target
 1.76586 +      ));
 1.76587 +
 1.76588 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.76589 +      /* If the column has REAL affinity, it may currently be stored as an
 1.76590 +      ** integer. Use OP_RealAffinity to make sure it is really real.  */
 1.76591 +      if( pExpr->iColumn>=0 
 1.76592 +       && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
 1.76593 +      ){
 1.76594 +        sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
 1.76595 +      }
 1.76596 +#endif
 1.76597 +      break;
 1.76598 +    }
 1.76599 +
 1.76600 +
 1.76601 +    /*
 1.76602 +    ** Form A:
 1.76603 +    **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 1.76604 +    **
 1.76605 +    ** Form B:
 1.76606 +    **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 1.76607 +    **
 1.76608 +    ** Form A is can be transformed into the equivalent form B as follows:
 1.76609 +    **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
 1.76610 +    **        WHEN x=eN THEN rN ELSE y END
 1.76611 +    **
 1.76612 +    ** X (if it exists) is in pExpr->pLeft.
 1.76613 +    ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
 1.76614 +    ** ELSE clause and no other term matches, then the result of the
 1.76615 +    ** exprssion is NULL.
 1.76616 +    ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
 1.76617 +    **
 1.76618 +    ** The result of the expression is the Ri for the first matching Ei,
 1.76619 +    ** or if there is no matching Ei, the ELSE term Y, or if there is
 1.76620 +    ** no ELSE term, NULL.
 1.76621 +    */
 1.76622 +    default: assert( op==TK_CASE ); {
 1.76623 +      int endLabel;                     /* GOTO label for end of CASE stmt */
 1.76624 +      int nextCase;                     /* GOTO label for next WHEN clause */
 1.76625 +      int nExpr;                        /* 2x number of WHEN terms */
 1.76626 +      int i;                            /* Loop counter */
 1.76627 +      ExprList *pEList;                 /* List of WHEN terms */
 1.76628 +      struct ExprList_item *aListelem;  /* Array of WHEN terms */
 1.76629 +      Expr opCompare;                   /* The X==Ei expression */
 1.76630 +      Expr cacheX;                      /* Cached expression X */
 1.76631 +      Expr *pX;                         /* The X expression */
 1.76632 +      Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
 1.76633 +      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
 1.76634 +
 1.76635 +      assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
 1.76636 +      assert((pExpr->x.pList->nExpr % 2) == 0);
 1.76637 +      assert(pExpr->x.pList->nExpr > 0);
 1.76638 +      pEList = pExpr->x.pList;
 1.76639 +      aListelem = pEList->a;
 1.76640 +      nExpr = pEList->nExpr;
 1.76641 +      endLabel = sqlite3VdbeMakeLabel(v);
 1.76642 +      if( (pX = pExpr->pLeft)!=0 ){
 1.76643 +        cacheX = *pX;
 1.76644 +        testcase( pX->op==TK_COLUMN );
 1.76645 +        testcase( pX->op==TK_REGISTER );
 1.76646 +        cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
 1.76647 +        testcase( regFree1==0 );
 1.76648 +        cacheX.op = TK_REGISTER;
 1.76649 +        opCompare.op = TK_EQ;
 1.76650 +        opCompare.pLeft = &cacheX;
 1.76651 +        pTest = &opCompare;
 1.76652 +        /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
 1.76653 +        ** The value in regFree1 might get SCopy-ed into the file result.
 1.76654 +        ** So make sure that the regFree1 register is not reused for other
 1.76655 +        ** purposes and possibly overwritten.  */
 1.76656 +        regFree1 = 0;
 1.76657 +      }
 1.76658 +      for(i=0; i<nExpr; i=i+2){
 1.76659 +        sqlite3ExprCachePush(pParse);
 1.76660 +        if( pX ){
 1.76661 +          assert( pTest!=0 );
 1.76662 +          opCompare.pRight = aListelem[i].pExpr;
 1.76663 +        }else{
 1.76664 +          pTest = aListelem[i].pExpr;
 1.76665 +        }
 1.76666 +        nextCase = sqlite3VdbeMakeLabel(v);
 1.76667 +        testcase( pTest->op==TK_COLUMN );
 1.76668 +        sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
 1.76669 +        testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
 1.76670 +        testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
 1.76671 +        sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
 1.76672 +        sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
 1.76673 +        sqlite3ExprCachePop(pParse, 1);
 1.76674 +        sqlite3VdbeResolveLabel(v, nextCase);
 1.76675 +      }
 1.76676 +      if( pExpr->pRight ){
 1.76677 +        sqlite3ExprCachePush(pParse);
 1.76678 +        sqlite3ExprCode(pParse, pExpr->pRight, target);
 1.76679 +        sqlite3ExprCachePop(pParse, 1);
 1.76680 +      }else{
 1.76681 +        sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 1.76682 +      }
 1.76683 +      assert( db->mallocFailed || pParse->nErr>0 
 1.76684 +           || pParse->iCacheLevel==iCacheLevel );
 1.76685 +      sqlite3VdbeResolveLabel(v, endLabel);
 1.76686 +      break;
 1.76687 +    }
 1.76688 +#ifndef SQLITE_OMIT_TRIGGER
 1.76689 +    case TK_RAISE: {
 1.76690 +      assert( pExpr->affinity==OE_Rollback 
 1.76691 +           || pExpr->affinity==OE_Abort
 1.76692 +           || pExpr->affinity==OE_Fail
 1.76693 +           || pExpr->affinity==OE_Ignore
 1.76694 +      );
 1.76695 +      if( !pParse->pTriggerTab ){
 1.76696 +        sqlite3ErrorMsg(pParse,
 1.76697 +                       "RAISE() may only be used within a trigger-program");
 1.76698 +        return 0;
 1.76699 +      }
 1.76700 +      if( pExpr->affinity==OE_Abort ){
 1.76701 +        sqlite3MayAbort(pParse);
 1.76702 +      }
 1.76703 +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.76704 +      if( pExpr->affinity==OE_Ignore ){
 1.76705 +        sqlite3VdbeAddOp4(
 1.76706 +            v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
 1.76707 +      }else{
 1.76708 +        sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
 1.76709 +      }
 1.76710 +
 1.76711 +      break;
 1.76712 +    }
 1.76713 +#endif
 1.76714 +  }
 1.76715 +  sqlite3ReleaseTempReg(pParse, regFree1);
 1.76716 +  sqlite3ReleaseTempReg(pParse, regFree2);
 1.76717 +  return inReg;
 1.76718 +}
 1.76719 +
 1.76720 +/*
 1.76721 +** Generate code to evaluate an expression and store the results
 1.76722 +** into a register.  Return the register number where the results
 1.76723 +** are stored.
 1.76724 +**
 1.76725 +** If the register is a temporary register that can be deallocated,
 1.76726 +** then write its number into *pReg.  If the result register is not
 1.76727 +** a temporary, then set *pReg to zero.
 1.76728 +*/
 1.76729 +SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
 1.76730 +  int r1 = sqlite3GetTempReg(pParse);
 1.76731 +  int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 1.76732 +  if( r2==r1 ){
 1.76733 +    *pReg = r1;
 1.76734 +  }else{
 1.76735 +    sqlite3ReleaseTempReg(pParse, r1);
 1.76736 +    *pReg = 0;
 1.76737 +  }
 1.76738 +  return r2;
 1.76739 +}
 1.76740 +
 1.76741 +/*
 1.76742 +** Generate code that will evaluate expression pExpr and store the
 1.76743 +** results in register target.  The results are guaranteed to appear
 1.76744 +** in register target.
 1.76745 +*/
 1.76746 +SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
 1.76747 +  int inReg;
 1.76748 +
 1.76749 +  assert( target>0 && target<=pParse->nMem );
 1.76750 +  if( pExpr && pExpr->op==TK_REGISTER ){
 1.76751 +    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
 1.76752 +  }else{
 1.76753 +    inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
 1.76754 +    assert( pParse->pVdbe || pParse->db->mallocFailed );
 1.76755 +    if( inReg!=target && pParse->pVdbe ){
 1.76756 +      sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
 1.76757 +    }
 1.76758 +  }
 1.76759 +  return target;
 1.76760 +}
 1.76761 +
 1.76762 +/*
 1.76763 +** Generate code that evalutes the given expression and puts the result
 1.76764 +** in register target.
 1.76765 +**
 1.76766 +** Also make a copy of the expression results into another "cache" register
 1.76767 +** and modify the expression so that the next time it is evaluated,
 1.76768 +** the result is a copy of the cache register.
 1.76769 +**
 1.76770 +** This routine is used for expressions that are used multiple 
 1.76771 +** times.  They are evaluated once and the results of the expression
 1.76772 +** are reused.
 1.76773 +*/
 1.76774 +SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
 1.76775 +  Vdbe *v = pParse->pVdbe;
 1.76776 +  int inReg;
 1.76777 +  inReg = sqlite3ExprCode(pParse, pExpr, target);
 1.76778 +  assert( target>0 );
 1.76779 +  /* This routine is called for terms to INSERT or UPDATE.  And the only
 1.76780 +  ** other place where expressions can be converted into TK_REGISTER is
 1.76781 +  ** in WHERE clause processing.  So as currently implemented, there is
 1.76782 +  ** no way for a TK_REGISTER to exist here.  But it seems prudent to
 1.76783 +  ** keep the ALWAYS() in case the conditions above change with future
 1.76784 +  ** modifications or enhancements. */
 1.76785 +  if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
 1.76786 +    int iMem;
 1.76787 +    iMem = ++pParse->nMem;
 1.76788 +    sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
 1.76789 +    pExpr->iTable = iMem;
 1.76790 +    pExpr->op2 = pExpr->op;
 1.76791 +    pExpr->op = TK_REGISTER;
 1.76792 +  }
 1.76793 +  return inReg;
 1.76794 +}
 1.76795 +
 1.76796 +#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 1.76797 +/*
 1.76798 +** Generate a human-readable explanation of an expression tree.
 1.76799 +*/
 1.76800 +SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
 1.76801 +  int op;                   /* The opcode being coded */
 1.76802 +  const char *zBinOp = 0;   /* Binary operator */
 1.76803 +  const char *zUniOp = 0;   /* Unary operator */
 1.76804 +  if( pExpr==0 ){
 1.76805 +    op = TK_NULL;
 1.76806 +  }else{
 1.76807 +    op = pExpr->op;
 1.76808 +  }
 1.76809 +  switch( op ){
 1.76810 +    case TK_AGG_COLUMN: {
 1.76811 +      sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
 1.76812 +            pExpr->iTable, pExpr->iColumn);
 1.76813 +      break;
 1.76814 +    }
 1.76815 +    case TK_COLUMN: {
 1.76816 +      if( pExpr->iTable<0 ){
 1.76817 +        /* This only happens when coding check constraints */
 1.76818 +        sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
 1.76819 +      }else{
 1.76820 +        sqlite3ExplainPrintf(pOut, "{%d:%d}",
 1.76821 +                             pExpr->iTable, pExpr->iColumn);
 1.76822 +      }
 1.76823 +      break;
 1.76824 +    }
 1.76825 +    case TK_INTEGER: {
 1.76826 +      if( pExpr->flags & EP_IntValue ){
 1.76827 +        sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
 1.76828 +      }else{
 1.76829 +        sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
 1.76830 +      }
 1.76831 +      break;
 1.76832 +    }
 1.76833 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.76834 +    case TK_FLOAT: {
 1.76835 +      sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
 1.76836 +      break;
 1.76837 +    }
 1.76838 +#endif
 1.76839 +    case TK_STRING: {
 1.76840 +      sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
 1.76841 +      break;
 1.76842 +    }
 1.76843 +    case TK_NULL: {
 1.76844 +      sqlite3ExplainPrintf(pOut,"NULL");
 1.76845 +      break;
 1.76846 +    }
 1.76847 +#ifndef SQLITE_OMIT_BLOB_LITERAL
 1.76848 +    case TK_BLOB: {
 1.76849 +      sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
 1.76850 +      break;
 1.76851 +    }
 1.76852 +#endif
 1.76853 +    case TK_VARIABLE: {
 1.76854 +      sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
 1.76855 +                           pExpr->u.zToken, pExpr->iColumn);
 1.76856 +      break;
 1.76857 +    }
 1.76858 +    case TK_REGISTER: {
 1.76859 +      sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
 1.76860 +      break;
 1.76861 +    }
 1.76862 +    case TK_AS: {
 1.76863 +      sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.76864 +      break;
 1.76865 +    }
 1.76866 +#ifndef SQLITE_OMIT_CAST
 1.76867 +    case TK_CAST: {
 1.76868 +      /* Expressions of the form:   CAST(pLeft AS token) */
 1.76869 +      const char *zAff = "unk";
 1.76870 +      switch( sqlite3AffinityType(pExpr->u.zToken) ){
 1.76871 +        case SQLITE_AFF_TEXT:    zAff = "TEXT";     break;
 1.76872 +        case SQLITE_AFF_NONE:    zAff = "NONE";     break;
 1.76873 +        case SQLITE_AFF_NUMERIC: zAff = "NUMERIC";  break;
 1.76874 +        case SQLITE_AFF_INTEGER: zAff = "INTEGER";  break;
 1.76875 +        case SQLITE_AFF_REAL:    zAff = "REAL";     break;
 1.76876 +      }
 1.76877 +      sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
 1.76878 +      sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.76879 +      sqlite3ExplainPrintf(pOut, ")");
 1.76880 +      break;
 1.76881 +    }
 1.76882 +#endif /* SQLITE_OMIT_CAST */
 1.76883 +    case TK_LT:      zBinOp = "LT";     break;
 1.76884 +    case TK_LE:      zBinOp = "LE";     break;
 1.76885 +    case TK_GT:      zBinOp = "GT";     break;
 1.76886 +    case TK_GE:      zBinOp = "GE";     break;
 1.76887 +    case TK_NE:      zBinOp = "NE";     break;
 1.76888 +    case TK_EQ:      zBinOp = "EQ";     break;
 1.76889 +    case TK_IS:      zBinOp = "IS";     break;
 1.76890 +    case TK_ISNOT:   zBinOp = "ISNOT";  break;
 1.76891 +    case TK_AND:     zBinOp = "AND";    break;
 1.76892 +    case TK_OR:      zBinOp = "OR";     break;
 1.76893 +    case TK_PLUS:    zBinOp = "ADD";    break;
 1.76894 +    case TK_STAR:    zBinOp = "MUL";    break;
 1.76895 +    case TK_MINUS:   zBinOp = "SUB";    break;
 1.76896 +    case TK_REM:     zBinOp = "REM";    break;
 1.76897 +    case TK_BITAND:  zBinOp = "BITAND"; break;
 1.76898 +    case TK_BITOR:   zBinOp = "BITOR";  break;
 1.76899 +    case TK_SLASH:   zBinOp = "DIV";    break;
 1.76900 +    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
 1.76901 +    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
 1.76902 +    case TK_CONCAT:  zBinOp = "CONCAT"; break;
 1.76903 +
 1.76904 +    case TK_UMINUS:  zUniOp = "UMINUS"; break;
 1.76905 +    case TK_UPLUS:   zUniOp = "UPLUS";  break;
 1.76906 +    case TK_BITNOT:  zUniOp = "BITNOT"; break;
 1.76907 +    case TK_NOT:     zUniOp = "NOT";    break;
 1.76908 +    case TK_ISNULL:  zUniOp = "ISNULL"; break;
 1.76909 +    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
 1.76910 +
 1.76911 +    case TK_COLLATE: {
 1.76912 +      sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.76913 +      sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
 1.76914 +      break;
 1.76915 +    }
 1.76916 +
 1.76917 +    case TK_AGG_FUNCTION:
 1.76918 +    case TK_CONST_FUNC:
 1.76919 +    case TK_FUNCTION: {
 1.76920 +      ExprList *pFarg;       /* List of function arguments */
 1.76921 +      if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
 1.76922 +        pFarg = 0;
 1.76923 +      }else{
 1.76924 +        pFarg = pExpr->x.pList;
 1.76925 +      }
 1.76926 +      if( op==TK_AGG_FUNCTION ){
 1.76927 +        sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
 1.76928 +                             pExpr->op2, pExpr->u.zToken);
 1.76929 +      }else{
 1.76930 +        sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
 1.76931 +      }
 1.76932 +      if( pFarg ){
 1.76933 +        sqlite3ExplainExprList(pOut, pFarg);
 1.76934 +      }
 1.76935 +      sqlite3ExplainPrintf(pOut, ")");
 1.76936 +      break;
 1.76937 +    }
 1.76938 +#ifndef SQLITE_OMIT_SUBQUERY
 1.76939 +    case TK_EXISTS: {
 1.76940 +      sqlite3ExplainPrintf(pOut, "EXISTS(");
 1.76941 +      sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 1.76942 +      sqlite3ExplainPrintf(pOut,")");
 1.76943 +      break;
 1.76944 +    }
 1.76945 +    case TK_SELECT: {
 1.76946 +      sqlite3ExplainPrintf(pOut, "(");
 1.76947 +      sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 1.76948 +      sqlite3ExplainPrintf(pOut, ")");
 1.76949 +      break;
 1.76950 +    }
 1.76951 +    case TK_IN: {
 1.76952 +      sqlite3ExplainPrintf(pOut, "IN(");
 1.76953 +      sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.76954 +      sqlite3ExplainPrintf(pOut, ",");
 1.76955 +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.76956 +        sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 1.76957 +      }else{
 1.76958 +        sqlite3ExplainExprList(pOut, pExpr->x.pList);
 1.76959 +      }
 1.76960 +      sqlite3ExplainPrintf(pOut, ")");
 1.76961 +      break;
 1.76962 +    }
 1.76963 +#endif /* SQLITE_OMIT_SUBQUERY */
 1.76964 +
 1.76965 +    /*
 1.76966 +    **    x BETWEEN y AND z
 1.76967 +    **
 1.76968 +    ** This is equivalent to
 1.76969 +    **
 1.76970 +    **    x>=y AND x<=z
 1.76971 +    **
 1.76972 +    ** X is stored in pExpr->pLeft.
 1.76973 +    ** Y is stored in pExpr->pList->a[0].pExpr.
 1.76974 +    ** Z is stored in pExpr->pList->a[1].pExpr.
 1.76975 +    */
 1.76976 +    case TK_BETWEEN: {
 1.76977 +      Expr *pX = pExpr->pLeft;
 1.76978 +      Expr *pY = pExpr->x.pList->a[0].pExpr;
 1.76979 +      Expr *pZ = pExpr->x.pList->a[1].pExpr;
 1.76980 +      sqlite3ExplainPrintf(pOut, "BETWEEN(");
 1.76981 +      sqlite3ExplainExpr(pOut, pX);
 1.76982 +      sqlite3ExplainPrintf(pOut, ",");
 1.76983 +      sqlite3ExplainExpr(pOut, pY);
 1.76984 +      sqlite3ExplainPrintf(pOut, ",");
 1.76985 +      sqlite3ExplainExpr(pOut, pZ);
 1.76986 +      sqlite3ExplainPrintf(pOut, ")");
 1.76987 +      break;
 1.76988 +    }
 1.76989 +    case TK_TRIGGER: {
 1.76990 +      /* If the opcode is TK_TRIGGER, then the expression is a reference
 1.76991 +      ** to a column in the new.* or old.* pseudo-tables available to
 1.76992 +      ** trigger programs. In this case Expr.iTable is set to 1 for the
 1.76993 +      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
 1.76994 +      ** is set to the column of the pseudo-table to read, or to -1 to
 1.76995 +      ** read the rowid field.
 1.76996 +      */
 1.76997 +      sqlite3ExplainPrintf(pOut, "%s(%d)", 
 1.76998 +          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
 1.76999 +      break;
 1.77000 +    }
 1.77001 +    case TK_CASE: {
 1.77002 +      sqlite3ExplainPrintf(pOut, "CASE(");
 1.77003 +      sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.77004 +      sqlite3ExplainPrintf(pOut, ",");
 1.77005 +      sqlite3ExplainExprList(pOut, pExpr->x.pList);
 1.77006 +      break;
 1.77007 +    }
 1.77008 +#ifndef SQLITE_OMIT_TRIGGER
 1.77009 +    case TK_RAISE: {
 1.77010 +      const char *zType = "unk";
 1.77011 +      switch( pExpr->affinity ){
 1.77012 +        case OE_Rollback:   zType = "rollback";  break;
 1.77013 +        case OE_Abort:      zType = "abort";     break;
 1.77014 +        case OE_Fail:       zType = "fail";      break;
 1.77015 +        case OE_Ignore:     zType = "ignore";    break;
 1.77016 +      }
 1.77017 +      sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
 1.77018 +      break;
 1.77019 +    }
 1.77020 +#endif
 1.77021 +  }
 1.77022 +  if( zBinOp ){
 1.77023 +    sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
 1.77024 +    sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.77025 +    sqlite3ExplainPrintf(pOut,",");
 1.77026 +    sqlite3ExplainExpr(pOut, pExpr->pRight);
 1.77027 +    sqlite3ExplainPrintf(pOut,")");
 1.77028 +  }else if( zUniOp ){
 1.77029 +    sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
 1.77030 +    sqlite3ExplainExpr(pOut, pExpr->pLeft);
 1.77031 +    sqlite3ExplainPrintf(pOut,")");
 1.77032 +  }
 1.77033 +}
 1.77034 +#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
 1.77035 +
 1.77036 +#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 1.77037 +/*
 1.77038 +** Generate a human-readable explanation of an expression list.
 1.77039 +*/
 1.77040 +SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
 1.77041 +  int i;
 1.77042 +  if( pList==0 || pList->nExpr==0 ){
 1.77043 +    sqlite3ExplainPrintf(pOut, "(empty-list)");
 1.77044 +    return;
 1.77045 +  }else if( pList->nExpr==1 ){
 1.77046 +    sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
 1.77047 +  }else{
 1.77048 +    sqlite3ExplainPush(pOut);
 1.77049 +    for(i=0; i<pList->nExpr; i++){
 1.77050 +      sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
 1.77051 +      sqlite3ExplainPush(pOut);
 1.77052 +      sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
 1.77053 +      sqlite3ExplainPop(pOut);
 1.77054 +      if( i<pList->nExpr-1 ){
 1.77055 +        sqlite3ExplainNL(pOut);
 1.77056 +      }
 1.77057 +    }
 1.77058 +    sqlite3ExplainPop(pOut);
 1.77059 +  }
 1.77060 +}
 1.77061 +#endif /* SQLITE_DEBUG */
 1.77062 +
 1.77063 +/*
 1.77064 +** Return TRUE if pExpr is an constant expression that is appropriate
 1.77065 +** for factoring out of a loop.  Appropriate expressions are:
 1.77066 +**
 1.77067 +**    *  Any expression that evaluates to two or more opcodes.
 1.77068 +**
 1.77069 +**    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, 
 1.77070 +**       or OP_Variable that does not need to be placed in a 
 1.77071 +**       specific register.
 1.77072 +**
 1.77073 +** There is no point in factoring out single-instruction constant
 1.77074 +** expressions that need to be placed in a particular register.  
 1.77075 +** We could factor them out, but then we would end up adding an
 1.77076 +** OP_SCopy instruction to move the value into the correct register
 1.77077 +** later.  We might as well just use the original instruction and
 1.77078 +** avoid the OP_SCopy.
 1.77079 +*/
 1.77080 +static int isAppropriateForFactoring(Expr *p){
 1.77081 +  if( !sqlite3ExprIsConstantNotJoin(p) ){
 1.77082 +    return 0;  /* Only constant expressions are appropriate for factoring */
 1.77083 +  }
 1.77084 +  if( (p->flags & EP_FixedDest)==0 ){
 1.77085 +    return 1;  /* Any constant without a fixed destination is appropriate */
 1.77086 +  }
 1.77087 +  while( p->op==TK_UPLUS ) p = p->pLeft;
 1.77088 +  switch( p->op ){
 1.77089 +#ifndef SQLITE_OMIT_BLOB_LITERAL
 1.77090 +    case TK_BLOB:
 1.77091 +#endif
 1.77092 +    case TK_VARIABLE:
 1.77093 +    case TK_INTEGER:
 1.77094 +    case TK_FLOAT:
 1.77095 +    case TK_NULL:
 1.77096 +    case TK_STRING: {
 1.77097 +      testcase( p->op==TK_BLOB );
 1.77098 +      testcase( p->op==TK_VARIABLE );
 1.77099 +      testcase( p->op==TK_INTEGER );
 1.77100 +      testcase( p->op==TK_FLOAT );
 1.77101 +      testcase( p->op==TK_NULL );
 1.77102 +      testcase( p->op==TK_STRING );
 1.77103 +      /* Single-instruction constants with a fixed destination are
 1.77104 +      ** better done in-line.  If we factor them, they will just end
 1.77105 +      ** up generating an OP_SCopy to move the value to the destination
 1.77106 +      ** register. */
 1.77107 +      return 0;
 1.77108 +    }
 1.77109 +    case TK_UMINUS: {
 1.77110 +      if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
 1.77111 +        return 0;
 1.77112 +      }
 1.77113 +      break;
 1.77114 +    }
 1.77115 +    default: {
 1.77116 +      break;
 1.77117 +    }
 1.77118 +  }
 1.77119 +  return 1;
 1.77120 +}
 1.77121 +
 1.77122 +/*
 1.77123 +** If pExpr is a constant expression that is appropriate for
 1.77124 +** factoring out of a loop, then evaluate the expression
 1.77125 +** into a register and convert the expression into a TK_REGISTER
 1.77126 +** expression.
 1.77127 +*/
 1.77128 +static int evalConstExpr(Walker *pWalker, Expr *pExpr){
 1.77129 +  Parse *pParse = pWalker->pParse;
 1.77130 +  switch( pExpr->op ){
 1.77131 +    case TK_IN:
 1.77132 +    case TK_REGISTER: {
 1.77133 +      return WRC_Prune;
 1.77134 +    }
 1.77135 +    case TK_COLLATE: {
 1.77136 +      return WRC_Continue;
 1.77137 +    }
 1.77138 +    case TK_FUNCTION:
 1.77139 +    case TK_AGG_FUNCTION:
 1.77140 +    case TK_CONST_FUNC: {
 1.77141 +      /* The arguments to a function have a fixed destination.
 1.77142 +      ** Mark them this way to avoid generated unneeded OP_SCopy
 1.77143 +      ** instructions. 
 1.77144 +      */
 1.77145 +      ExprList *pList = pExpr->x.pList;
 1.77146 +      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 1.77147 +      if( pList ){
 1.77148 +        int i = pList->nExpr;
 1.77149 +        struct ExprList_item *pItem = pList->a;
 1.77150 +        for(; i>0; i--, pItem++){
 1.77151 +          if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
 1.77152 +        }
 1.77153 +      }
 1.77154 +      break;
 1.77155 +    }
 1.77156 +  }
 1.77157 +  if( isAppropriateForFactoring(pExpr) ){
 1.77158 +    int r1 = ++pParse->nMem;
 1.77159 +    int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 1.77160 +    /* If r2!=r1, it means that register r1 is never used.  That is harmless
 1.77161 +    ** but suboptimal, so we want to know about the situation to fix it.
 1.77162 +    ** Hence the following assert: */
 1.77163 +    assert( r2==r1 );
 1.77164 +    pExpr->op2 = pExpr->op;
 1.77165 +    pExpr->op = TK_REGISTER;
 1.77166 +    pExpr->iTable = r2;
 1.77167 +    return WRC_Prune;
 1.77168 +  }
 1.77169 +  return WRC_Continue;
 1.77170 +}
 1.77171 +
 1.77172 +/*
 1.77173 +** Preevaluate constant subexpressions within pExpr and store the
 1.77174 +** results in registers.  Modify pExpr so that the constant subexpresions
 1.77175 +** are TK_REGISTER opcodes that refer to the precomputed values.
 1.77176 +**
 1.77177 +** This routine is a no-op if the jump to the cookie-check code has
 1.77178 +** already occur.  Since the cookie-check jump is generated prior to
 1.77179 +** any other serious processing, this check ensures that there is no
 1.77180 +** way to accidently bypass the constant initializations.
 1.77181 +**
 1.77182 +** This routine is also a no-op if the SQLITE_FactorOutConst optimization
 1.77183 +** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
 1.77184 +** interface.  This allows test logic to verify that the same answer is
 1.77185 +** obtained for queries regardless of whether or not constants are
 1.77186 +** precomputed into registers or if they are inserted in-line.
 1.77187 +*/
 1.77188 +SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
 1.77189 +  Walker w;
 1.77190 +  if( pParse->cookieGoto ) return;
 1.77191 +  if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
 1.77192 +  w.xExprCallback = evalConstExpr;
 1.77193 +  w.xSelectCallback = 0;
 1.77194 +  w.pParse = pParse;
 1.77195 +  sqlite3WalkExpr(&w, pExpr);
 1.77196 +}
 1.77197 +
 1.77198 +
 1.77199 +/*
 1.77200 +** Generate code that pushes the value of every element of the given
 1.77201 +** expression list into a sequence of registers beginning at target.
 1.77202 +**
 1.77203 +** Return the number of elements evaluated.
 1.77204 +*/
 1.77205 +SQLITE_PRIVATE int sqlite3ExprCodeExprList(
 1.77206 +  Parse *pParse,     /* Parsing context */
 1.77207 +  ExprList *pList,   /* The expression list to be coded */
 1.77208 +  int target,        /* Where to write results */
 1.77209 +  int doHardCopy     /* Make a hard copy of every element */
 1.77210 +){
 1.77211 +  struct ExprList_item *pItem;
 1.77212 +  int i, n;
 1.77213 +  assert( pList!=0 );
 1.77214 +  assert( target>0 );
 1.77215 +  assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
 1.77216 +  n = pList->nExpr;
 1.77217 +  for(pItem=pList->a, i=0; i<n; i++, pItem++){
 1.77218 +    Expr *pExpr = pItem->pExpr;
 1.77219 +    int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
 1.77220 +    if( inReg!=target+i ){
 1.77221 +      sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
 1.77222 +                        inReg, target+i);
 1.77223 +    }
 1.77224 +  }
 1.77225 +  return n;
 1.77226 +}
 1.77227 +
 1.77228 +/*
 1.77229 +** Generate code for a BETWEEN operator.
 1.77230 +**
 1.77231 +**    x BETWEEN y AND z
 1.77232 +**
 1.77233 +** The above is equivalent to 
 1.77234 +**
 1.77235 +**    x>=y AND x<=z
 1.77236 +**
 1.77237 +** Code it as such, taking care to do the common subexpression
 1.77238 +** elementation of x.
 1.77239 +*/
 1.77240 +static void exprCodeBetween(
 1.77241 +  Parse *pParse,    /* Parsing and code generating context */
 1.77242 +  Expr *pExpr,      /* The BETWEEN expression */
 1.77243 +  int dest,         /* Jump here if the jump is taken */
 1.77244 +  int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
 1.77245 +  int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
 1.77246 +){
 1.77247 +  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
 1.77248 +  Expr compLeft;    /* The  x>=y  term */
 1.77249 +  Expr compRight;   /* The  x<=z  term */
 1.77250 +  Expr exprX;       /* The  x  subexpression */
 1.77251 +  int regFree1 = 0; /* Temporary use register */
 1.77252 +
 1.77253 +  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 1.77254 +  exprX = *pExpr->pLeft;
 1.77255 +  exprAnd.op = TK_AND;
 1.77256 +  exprAnd.pLeft = &compLeft;
 1.77257 +  exprAnd.pRight = &compRight;
 1.77258 +  compLeft.op = TK_GE;
 1.77259 +  compLeft.pLeft = &exprX;
 1.77260 +  compLeft.pRight = pExpr->x.pList->a[0].pExpr;
 1.77261 +  compRight.op = TK_LE;
 1.77262 +  compRight.pLeft = &exprX;
 1.77263 +  compRight.pRight = pExpr->x.pList->a[1].pExpr;
 1.77264 +  exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
 1.77265 +  exprX.op = TK_REGISTER;
 1.77266 +  if( jumpIfTrue ){
 1.77267 +    sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
 1.77268 +  }else{
 1.77269 +    sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
 1.77270 +  }
 1.77271 +  sqlite3ReleaseTempReg(pParse, regFree1);
 1.77272 +
 1.77273 +  /* Ensure adequate test coverage */
 1.77274 +  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
 1.77275 +  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
 1.77276 +  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
 1.77277 +  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
 1.77278 +  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
 1.77279 +  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
 1.77280 +  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
 1.77281 +  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
 1.77282 +}
 1.77283 +
 1.77284 +/*
 1.77285 +** Generate code for a boolean expression such that a jump is made
 1.77286 +** to the label "dest" if the expression is true but execution
 1.77287 +** continues straight thru if the expression is false.
 1.77288 +**
 1.77289 +** If the expression evaluates to NULL (neither true nor false), then
 1.77290 +** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
 1.77291 +**
 1.77292 +** This code depends on the fact that certain token values (ex: TK_EQ)
 1.77293 +** are the same as opcode values (ex: OP_Eq) that implement the corresponding
 1.77294 +** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
 1.77295 +** the make process cause these values to align.  Assert()s in the code
 1.77296 +** below verify that the numbers are aligned correctly.
 1.77297 +*/
 1.77298 +SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
 1.77299 +  Vdbe *v = pParse->pVdbe;
 1.77300 +  int op = 0;
 1.77301 +  int regFree1 = 0;
 1.77302 +  int regFree2 = 0;
 1.77303 +  int r1, r2;
 1.77304 +
 1.77305 +  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
 1.77306 +  if( NEVER(v==0) )     return;  /* Existance of VDBE checked by caller */
 1.77307 +  if( NEVER(pExpr==0) ) return;  /* No way this can happen */
 1.77308 +  op = pExpr->op;
 1.77309 +  switch( op ){
 1.77310 +    case TK_AND: {
 1.77311 +      int d2 = sqlite3VdbeMakeLabel(v);
 1.77312 +      testcase( jumpIfNull==0 );
 1.77313 +      sqlite3ExprCachePush(pParse);
 1.77314 +      sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
 1.77315 +      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 1.77316 +      sqlite3VdbeResolveLabel(v, d2);
 1.77317 +      sqlite3ExprCachePop(pParse, 1);
 1.77318 +      break;
 1.77319 +    }
 1.77320 +    case TK_OR: {
 1.77321 +      testcase( jumpIfNull==0 );
 1.77322 +      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 1.77323 +      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 1.77324 +      break;
 1.77325 +    }
 1.77326 +    case TK_NOT: {
 1.77327 +      testcase( jumpIfNull==0 );
 1.77328 +      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 1.77329 +      break;
 1.77330 +    }
 1.77331 +    case TK_LT:
 1.77332 +    case TK_LE:
 1.77333 +    case TK_GT:
 1.77334 +    case TK_GE:
 1.77335 +    case TK_NE:
 1.77336 +    case TK_EQ: {
 1.77337 +      assert( TK_LT==OP_Lt );
 1.77338 +      assert( TK_LE==OP_Le );
 1.77339 +      assert( TK_GT==OP_Gt );
 1.77340 +      assert( TK_GE==OP_Ge );
 1.77341 +      assert( TK_EQ==OP_Eq );
 1.77342 +      assert( TK_NE==OP_Ne );
 1.77343 +      testcase( op==TK_LT );
 1.77344 +      testcase( op==TK_LE );
 1.77345 +      testcase( op==TK_GT );
 1.77346 +      testcase( op==TK_GE );
 1.77347 +      testcase( op==TK_EQ );
 1.77348 +      testcase( op==TK_NE );
 1.77349 +      testcase( jumpIfNull==0 );
 1.77350 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.77351 +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 1.77352 +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 1.77353 +                  r1, r2, dest, jumpIfNull);
 1.77354 +      testcase( regFree1==0 );
 1.77355 +      testcase( regFree2==0 );
 1.77356 +      break;
 1.77357 +    }
 1.77358 +    case TK_IS:
 1.77359 +    case TK_ISNOT: {
 1.77360 +      testcase( op==TK_IS );
 1.77361 +      testcase( op==TK_ISNOT );
 1.77362 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.77363 +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 1.77364 +      op = (op==TK_IS) ? TK_EQ : TK_NE;
 1.77365 +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 1.77366 +                  r1, r2, dest, SQLITE_NULLEQ);
 1.77367 +      testcase( regFree1==0 );
 1.77368 +      testcase( regFree2==0 );
 1.77369 +      break;
 1.77370 +    }
 1.77371 +    case TK_ISNULL:
 1.77372 +    case TK_NOTNULL: {
 1.77373 +      assert( TK_ISNULL==OP_IsNull );
 1.77374 +      assert( TK_NOTNULL==OP_NotNull );
 1.77375 +      testcase( op==TK_ISNULL );
 1.77376 +      testcase( op==TK_NOTNULL );
 1.77377 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.77378 +      sqlite3VdbeAddOp2(v, op, r1, dest);
 1.77379 +      testcase( regFree1==0 );
 1.77380 +      break;
 1.77381 +    }
 1.77382 +    case TK_BETWEEN: {
 1.77383 +      testcase( jumpIfNull==0 );
 1.77384 +      exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
 1.77385 +      break;
 1.77386 +    }
 1.77387 +#ifndef SQLITE_OMIT_SUBQUERY
 1.77388 +    case TK_IN: {
 1.77389 +      int destIfFalse = sqlite3VdbeMakeLabel(v);
 1.77390 +      int destIfNull = jumpIfNull ? dest : destIfFalse;
 1.77391 +      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 1.77392 +      sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 1.77393 +      sqlite3VdbeResolveLabel(v, destIfFalse);
 1.77394 +      break;
 1.77395 +    }
 1.77396 +#endif
 1.77397 +    default: {
 1.77398 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 1.77399 +      sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
 1.77400 +      testcase( regFree1==0 );
 1.77401 +      testcase( jumpIfNull==0 );
 1.77402 +      break;
 1.77403 +    }
 1.77404 +  }
 1.77405 +  sqlite3ReleaseTempReg(pParse, regFree1);
 1.77406 +  sqlite3ReleaseTempReg(pParse, regFree2);  
 1.77407 +}
 1.77408 +
 1.77409 +/*
 1.77410 +** Generate code for a boolean expression such that a jump is made
 1.77411 +** to the label "dest" if the expression is false but execution
 1.77412 +** continues straight thru if the expression is true.
 1.77413 +**
 1.77414 +** If the expression evaluates to NULL (neither true nor false) then
 1.77415 +** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
 1.77416 +** is 0.
 1.77417 +*/
 1.77418 +SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
 1.77419 +  Vdbe *v = pParse->pVdbe;
 1.77420 +  int op = 0;
 1.77421 +  int regFree1 = 0;
 1.77422 +  int regFree2 = 0;
 1.77423 +  int r1, r2;
 1.77424 +
 1.77425 +  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
 1.77426 +  if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
 1.77427 +  if( pExpr==0 )    return;
 1.77428 +
 1.77429 +  /* The value of pExpr->op and op are related as follows:
 1.77430 +  **
 1.77431 +  **       pExpr->op            op
 1.77432 +  **       ---------          ----------
 1.77433 +  **       TK_ISNULL          OP_NotNull
 1.77434 +  **       TK_NOTNULL         OP_IsNull
 1.77435 +  **       TK_NE              OP_Eq
 1.77436 +  **       TK_EQ              OP_Ne
 1.77437 +  **       TK_GT              OP_Le
 1.77438 +  **       TK_LE              OP_Gt
 1.77439 +  **       TK_GE              OP_Lt
 1.77440 +  **       TK_LT              OP_Ge
 1.77441 +  **
 1.77442 +  ** For other values of pExpr->op, op is undefined and unused.
 1.77443 +  ** The value of TK_ and OP_ constants are arranged such that we
 1.77444 +  ** can compute the mapping above using the following expression.
 1.77445 +  ** Assert()s verify that the computation is correct.
 1.77446 +  */
 1.77447 +  op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
 1.77448 +
 1.77449 +  /* Verify correct alignment of TK_ and OP_ constants
 1.77450 +  */
 1.77451 +  assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
 1.77452 +  assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
 1.77453 +  assert( pExpr->op!=TK_NE || op==OP_Eq );
 1.77454 +  assert( pExpr->op!=TK_EQ || op==OP_Ne );
 1.77455 +  assert( pExpr->op!=TK_LT || op==OP_Ge );
 1.77456 +  assert( pExpr->op!=TK_LE || op==OP_Gt );
 1.77457 +  assert( pExpr->op!=TK_GT || op==OP_Le );
 1.77458 +  assert( pExpr->op!=TK_GE || op==OP_Lt );
 1.77459 +
 1.77460 +  switch( pExpr->op ){
 1.77461 +    case TK_AND: {
 1.77462 +      testcase( jumpIfNull==0 );
 1.77463 +      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 1.77464 +      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 1.77465 +      break;
 1.77466 +    }
 1.77467 +    case TK_OR: {
 1.77468 +      int d2 = sqlite3VdbeMakeLabel(v);
 1.77469 +      testcase( jumpIfNull==0 );
 1.77470 +      sqlite3ExprCachePush(pParse);
 1.77471 +      sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
 1.77472 +      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 1.77473 +      sqlite3VdbeResolveLabel(v, d2);
 1.77474 +      sqlite3ExprCachePop(pParse, 1);
 1.77475 +      break;
 1.77476 +    }
 1.77477 +    case TK_NOT: {
 1.77478 +      testcase( jumpIfNull==0 );
 1.77479 +      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 1.77480 +      break;
 1.77481 +    }
 1.77482 +    case TK_LT:
 1.77483 +    case TK_LE:
 1.77484 +    case TK_GT:
 1.77485 +    case TK_GE:
 1.77486 +    case TK_NE:
 1.77487 +    case TK_EQ: {
 1.77488 +      testcase( op==TK_LT );
 1.77489 +      testcase( op==TK_LE );
 1.77490 +      testcase( op==TK_GT );
 1.77491 +      testcase( op==TK_GE );
 1.77492 +      testcase( op==TK_EQ );
 1.77493 +      testcase( op==TK_NE );
 1.77494 +      testcase( jumpIfNull==0 );
 1.77495 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.77496 +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 1.77497 +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 1.77498 +                  r1, r2, dest, jumpIfNull);
 1.77499 +      testcase( regFree1==0 );
 1.77500 +      testcase( regFree2==0 );
 1.77501 +      break;
 1.77502 +    }
 1.77503 +    case TK_IS:
 1.77504 +    case TK_ISNOT: {
 1.77505 +      testcase( pExpr->op==TK_IS );
 1.77506 +      testcase( pExpr->op==TK_ISNOT );
 1.77507 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.77508 +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 1.77509 +      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
 1.77510 +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 1.77511 +                  r1, r2, dest, SQLITE_NULLEQ);
 1.77512 +      testcase( regFree1==0 );
 1.77513 +      testcase( regFree2==0 );
 1.77514 +      break;
 1.77515 +    }
 1.77516 +    case TK_ISNULL:
 1.77517 +    case TK_NOTNULL: {
 1.77518 +      testcase( op==TK_ISNULL );
 1.77519 +      testcase( op==TK_NOTNULL );
 1.77520 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 1.77521 +      sqlite3VdbeAddOp2(v, op, r1, dest);
 1.77522 +      testcase( regFree1==0 );
 1.77523 +      break;
 1.77524 +    }
 1.77525 +    case TK_BETWEEN: {
 1.77526 +      testcase( jumpIfNull==0 );
 1.77527 +      exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
 1.77528 +      break;
 1.77529 +    }
 1.77530 +#ifndef SQLITE_OMIT_SUBQUERY
 1.77531 +    case TK_IN: {
 1.77532 +      if( jumpIfNull ){
 1.77533 +        sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
 1.77534 +      }else{
 1.77535 +        int destIfNull = sqlite3VdbeMakeLabel(v);
 1.77536 +        sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
 1.77537 +        sqlite3VdbeResolveLabel(v, destIfNull);
 1.77538 +      }
 1.77539 +      break;
 1.77540 +    }
 1.77541 +#endif
 1.77542 +    default: {
 1.77543 +      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 1.77544 +      sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
 1.77545 +      testcase( regFree1==0 );
 1.77546 +      testcase( jumpIfNull==0 );
 1.77547 +      break;
 1.77548 +    }
 1.77549 +  }
 1.77550 +  sqlite3ReleaseTempReg(pParse, regFree1);
 1.77551 +  sqlite3ReleaseTempReg(pParse, regFree2);
 1.77552 +}
 1.77553 +
 1.77554 +/*
 1.77555 +** Do a deep comparison of two expression trees.  Return 0 if the two
 1.77556 +** expressions are completely identical.  Return 1 if they differ only
 1.77557 +** by a COLLATE operator at the top level.  Return 2 if there are differences
 1.77558 +** other than the top-level COLLATE operator.
 1.77559 +**
 1.77560 +** Sometimes this routine will return 2 even if the two expressions
 1.77561 +** really are equivalent.  If we cannot prove that the expressions are
 1.77562 +** identical, we return 2 just to be safe.  So if this routine
 1.77563 +** returns 2, then you do not really know for certain if the two
 1.77564 +** expressions are the same.  But if you get a 0 or 1 return, then you
 1.77565 +** can be sure the expressions are the same.  In the places where
 1.77566 +** this routine is used, it does not hurt to get an extra 2 - that
 1.77567 +** just might result in some slightly slower code.  But returning
 1.77568 +** an incorrect 0 or 1 could lead to a malfunction.
 1.77569 +*/
 1.77570 +SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
 1.77571 +  if( pA==0||pB==0 ){
 1.77572 +    return pB==pA ? 0 : 2;
 1.77573 +  }
 1.77574 +  assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
 1.77575 +  assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
 1.77576 +  if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
 1.77577 +    return 2;
 1.77578 +  }
 1.77579 +  if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 1.77580 +  if( pA->op!=pB->op ){
 1.77581 +    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB)<2 ){
 1.77582 +      return 1;
 1.77583 +    }
 1.77584 +    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft)<2 ){
 1.77585 +      return 1;
 1.77586 +    }
 1.77587 +    return 2;
 1.77588 +  }
 1.77589 +  if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
 1.77590 +  if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
 1.77591 +  if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
 1.77592 +  if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
 1.77593 +  if( ExprHasProperty(pA, EP_IntValue) ){
 1.77594 +    if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
 1.77595 +      return 2;
 1.77596 +    }
 1.77597 +  }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
 1.77598 +    if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
 1.77599 +    if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 1.77600 +      return pA->op==TK_COLLATE ? 1 : 2;
 1.77601 +    }
 1.77602 +  }
 1.77603 +  return 0;
 1.77604 +}
 1.77605 +
 1.77606 +/*
 1.77607 +** Compare two ExprList objects.  Return 0 if they are identical and 
 1.77608 +** non-zero if they differ in any way.
 1.77609 +**
 1.77610 +** This routine might return non-zero for equivalent ExprLists.  The
 1.77611 +** only consequence will be disabled optimizations.  But this routine
 1.77612 +** must never return 0 if the two ExprList objects are different, or
 1.77613 +** a malfunction will result.
 1.77614 +**
 1.77615 +** Two NULL pointers are considered to be the same.  But a NULL pointer
 1.77616 +** always differs from a non-NULL pointer.
 1.77617 +*/
 1.77618 +SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
 1.77619 +  int i;
 1.77620 +  if( pA==0 && pB==0 ) return 0;
 1.77621 +  if( pA==0 || pB==0 ) return 1;
 1.77622 +  if( pA->nExpr!=pB->nExpr ) return 1;
 1.77623 +  for(i=0; i<pA->nExpr; i++){
 1.77624 +    Expr *pExprA = pA->a[i].pExpr;
 1.77625 +    Expr *pExprB = pB->a[i].pExpr;
 1.77626 +    if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
 1.77627 +    if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
 1.77628 +  }
 1.77629 +  return 0;
 1.77630 +}
 1.77631 +
 1.77632 +/*
 1.77633 +** An instance of the following structure is used by the tree walker
 1.77634 +** to count references to table columns in the arguments of an 
 1.77635 +** aggregate function, in order to implement the
 1.77636 +** sqlite3FunctionThisSrc() routine.
 1.77637 +*/
 1.77638 +struct SrcCount {
 1.77639 +  SrcList *pSrc;   /* One particular FROM clause in a nested query */
 1.77640 +  int nThis;       /* Number of references to columns in pSrcList */
 1.77641 +  int nOther;      /* Number of references to columns in other FROM clauses */
 1.77642 +};
 1.77643 +
 1.77644 +/*
 1.77645 +** Count the number of references to columns.
 1.77646 +*/
 1.77647 +static int exprSrcCount(Walker *pWalker, Expr *pExpr){
 1.77648 +  /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
 1.77649 +  ** is always called before sqlite3ExprAnalyzeAggregates() and so the
 1.77650 +  ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
 1.77651 +  ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
 1.77652 +  ** NEVER() will need to be removed. */
 1.77653 +  if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
 1.77654 +    int i;
 1.77655 +    struct SrcCount *p = pWalker->u.pSrcCount;
 1.77656 +    SrcList *pSrc = p->pSrc;
 1.77657 +    for(i=0; i<pSrc->nSrc; i++){
 1.77658 +      if( pExpr->iTable==pSrc->a[i].iCursor ) break;
 1.77659 +    }
 1.77660 +    if( i<pSrc->nSrc ){
 1.77661 +      p->nThis++;
 1.77662 +    }else{
 1.77663 +      p->nOther++;
 1.77664 +    }
 1.77665 +  }
 1.77666 +  return WRC_Continue;
 1.77667 +}
 1.77668 +
 1.77669 +/*
 1.77670 +** Determine if any of the arguments to the pExpr Function reference
 1.77671 +** pSrcList.  Return true if they do.  Also return true if the function
 1.77672 +** has no arguments or has only constant arguments.  Return false if pExpr
 1.77673 +** references columns but not columns of tables found in pSrcList.
 1.77674 +*/
 1.77675 +SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
 1.77676 +  Walker w;
 1.77677 +  struct SrcCount cnt;
 1.77678 +  assert( pExpr->op==TK_AGG_FUNCTION );
 1.77679 +  memset(&w, 0, sizeof(w));
 1.77680 +  w.xExprCallback = exprSrcCount;
 1.77681 +  w.u.pSrcCount = &cnt;
 1.77682 +  cnt.pSrc = pSrcList;
 1.77683 +  cnt.nThis = 0;
 1.77684 +  cnt.nOther = 0;
 1.77685 +  sqlite3WalkExprList(&w, pExpr->x.pList);
 1.77686 +  return cnt.nThis>0 || cnt.nOther==0;
 1.77687 +}
 1.77688 +
 1.77689 +/*
 1.77690 +** Add a new element to the pAggInfo->aCol[] array.  Return the index of
 1.77691 +** the new element.  Return a negative number if malloc fails.
 1.77692 +*/
 1.77693 +static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
 1.77694 +  int i;
 1.77695 +  pInfo->aCol = sqlite3ArrayAllocate(
 1.77696 +       db,
 1.77697 +       pInfo->aCol,
 1.77698 +       sizeof(pInfo->aCol[0]),
 1.77699 +       &pInfo->nColumn,
 1.77700 +       &i
 1.77701 +  );
 1.77702 +  return i;
 1.77703 +}    
 1.77704 +
 1.77705 +/*
 1.77706 +** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
 1.77707 +** the new element.  Return a negative number if malloc fails.
 1.77708 +*/
 1.77709 +static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
 1.77710 +  int i;
 1.77711 +  pInfo->aFunc = sqlite3ArrayAllocate(
 1.77712 +       db, 
 1.77713 +       pInfo->aFunc,
 1.77714 +       sizeof(pInfo->aFunc[0]),
 1.77715 +       &pInfo->nFunc,
 1.77716 +       &i
 1.77717 +  );
 1.77718 +  return i;
 1.77719 +}    
 1.77720 +
 1.77721 +/*
 1.77722 +** This is the xExprCallback for a tree walker.  It is used to
 1.77723 +** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
 1.77724 +** for additional information.
 1.77725 +*/
 1.77726 +static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
 1.77727 +  int i;
 1.77728 +  NameContext *pNC = pWalker->u.pNC;
 1.77729 +  Parse *pParse = pNC->pParse;
 1.77730 +  SrcList *pSrcList = pNC->pSrcList;
 1.77731 +  AggInfo *pAggInfo = pNC->pAggInfo;
 1.77732 +
 1.77733 +  switch( pExpr->op ){
 1.77734 +    case TK_AGG_COLUMN:
 1.77735 +    case TK_COLUMN: {
 1.77736 +      testcase( pExpr->op==TK_AGG_COLUMN );
 1.77737 +      testcase( pExpr->op==TK_COLUMN );
 1.77738 +      /* Check to see if the column is in one of the tables in the FROM
 1.77739 +      ** clause of the aggregate query */
 1.77740 +      if( ALWAYS(pSrcList!=0) ){
 1.77741 +        struct SrcList_item *pItem = pSrcList->a;
 1.77742 +        for(i=0; i<pSrcList->nSrc; i++, pItem++){
 1.77743 +          struct AggInfo_col *pCol;
 1.77744 +          assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 1.77745 +          if( pExpr->iTable==pItem->iCursor ){
 1.77746 +            /* If we reach this point, it means that pExpr refers to a table
 1.77747 +            ** that is in the FROM clause of the aggregate query.  
 1.77748 +            **
 1.77749 +            ** Make an entry for the column in pAggInfo->aCol[] if there
 1.77750 +            ** is not an entry there already.
 1.77751 +            */
 1.77752 +            int k;
 1.77753 +            pCol = pAggInfo->aCol;
 1.77754 +            for(k=0; k<pAggInfo->nColumn; k++, pCol++){
 1.77755 +              if( pCol->iTable==pExpr->iTable &&
 1.77756 +                  pCol->iColumn==pExpr->iColumn ){
 1.77757 +                break;
 1.77758 +              }
 1.77759 +            }
 1.77760 +            if( (k>=pAggInfo->nColumn)
 1.77761 +             && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
 1.77762 +            ){
 1.77763 +              pCol = &pAggInfo->aCol[k];
 1.77764 +              pCol->pTab = pExpr->pTab;
 1.77765 +              pCol->iTable = pExpr->iTable;
 1.77766 +              pCol->iColumn = pExpr->iColumn;
 1.77767 +              pCol->iMem = ++pParse->nMem;
 1.77768 +              pCol->iSorterColumn = -1;
 1.77769 +              pCol->pExpr = pExpr;
 1.77770 +              if( pAggInfo->pGroupBy ){
 1.77771 +                int j, n;
 1.77772 +                ExprList *pGB = pAggInfo->pGroupBy;
 1.77773 +                struct ExprList_item *pTerm = pGB->a;
 1.77774 +                n = pGB->nExpr;
 1.77775 +                for(j=0; j<n; j++, pTerm++){
 1.77776 +                  Expr *pE = pTerm->pExpr;
 1.77777 +                  if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
 1.77778 +                      pE->iColumn==pExpr->iColumn ){
 1.77779 +                    pCol->iSorterColumn = j;
 1.77780 +                    break;
 1.77781 +                  }
 1.77782 +                }
 1.77783 +              }
 1.77784 +              if( pCol->iSorterColumn<0 ){
 1.77785 +                pCol->iSorterColumn = pAggInfo->nSortingColumn++;
 1.77786 +              }
 1.77787 +            }
 1.77788 +            /* There is now an entry for pExpr in pAggInfo->aCol[] (either
 1.77789 +            ** because it was there before or because we just created it).
 1.77790 +            ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
 1.77791 +            ** pAggInfo->aCol[] entry.
 1.77792 +            */
 1.77793 +            ExprSetIrreducible(pExpr);
 1.77794 +            pExpr->pAggInfo = pAggInfo;
 1.77795 +            pExpr->op = TK_AGG_COLUMN;
 1.77796 +            pExpr->iAgg = (i16)k;
 1.77797 +            break;
 1.77798 +          } /* endif pExpr->iTable==pItem->iCursor */
 1.77799 +        } /* end loop over pSrcList */
 1.77800 +      }
 1.77801 +      return WRC_Prune;
 1.77802 +    }
 1.77803 +    case TK_AGG_FUNCTION: {
 1.77804 +      if( (pNC->ncFlags & NC_InAggFunc)==0
 1.77805 +       && pWalker->walkerDepth==pExpr->op2
 1.77806 +      ){
 1.77807 +        /* Check to see if pExpr is a duplicate of another aggregate 
 1.77808 +        ** function that is already in the pAggInfo structure
 1.77809 +        */
 1.77810 +        struct AggInfo_func *pItem = pAggInfo->aFunc;
 1.77811 +        for(i=0; i<pAggInfo->nFunc; i++, pItem++){
 1.77812 +          if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
 1.77813 +            break;
 1.77814 +          }
 1.77815 +        }
 1.77816 +        if( i>=pAggInfo->nFunc ){
 1.77817 +          /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
 1.77818 +          */
 1.77819 +          u8 enc = ENC(pParse->db);
 1.77820 +          i = addAggInfoFunc(pParse->db, pAggInfo);
 1.77821 +          if( i>=0 ){
 1.77822 +            assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 1.77823 +            pItem = &pAggInfo->aFunc[i];
 1.77824 +            pItem->pExpr = pExpr;
 1.77825 +            pItem->iMem = ++pParse->nMem;
 1.77826 +            assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.77827 +            pItem->pFunc = sqlite3FindFunction(pParse->db,
 1.77828 +                   pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
 1.77829 +                   pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
 1.77830 +            if( pExpr->flags & EP_Distinct ){
 1.77831 +              pItem->iDistinct = pParse->nTab++;
 1.77832 +            }else{
 1.77833 +              pItem->iDistinct = -1;
 1.77834 +            }
 1.77835 +          }
 1.77836 +        }
 1.77837 +        /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
 1.77838 +        */
 1.77839 +        assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 1.77840 +        ExprSetIrreducible(pExpr);
 1.77841 +        pExpr->iAgg = (i16)i;
 1.77842 +        pExpr->pAggInfo = pAggInfo;
 1.77843 +        return WRC_Prune;
 1.77844 +      }else{
 1.77845 +        return WRC_Continue;
 1.77846 +      }
 1.77847 +    }
 1.77848 +  }
 1.77849 +  return WRC_Continue;
 1.77850 +}
 1.77851 +static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
 1.77852 +  UNUSED_PARAMETER(pWalker);
 1.77853 +  UNUSED_PARAMETER(pSelect);
 1.77854 +  return WRC_Continue;
 1.77855 +}
 1.77856 +
 1.77857 +/*
 1.77858 +** Analyze the pExpr expression looking for aggregate functions and
 1.77859 +** for variables that need to be added to AggInfo object that pNC->pAggInfo
 1.77860 +** points to.  Additional entries are made on the AggInfo object as
 1.77861 +** necessary.
 1.77862 +**
 1.77863 +** This routine should only be called after the expression has been
 1.77864 +** analyzed by sqlite3ResolveExprNames().
 1.77865 +*/
 1.77866 +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
 1.77867 +  Walker w;
 1.77868 +  memset(&w, 0, sizeof(w));
 1.77869 +  w.xExprCallback = analyzeAggregate;
 1.77870 +  w.xSelectCallback = analyzeAggregatesInSelect;
 1.77871 +  w.u.pNC = pNC;
 1.77872 +  assert( pNC->pSrcList!=0 );
 1.77873 +  sqlite3WalkExpr(&w, pExpr);
 1.77874 +}
 1.77875 +
 1.77876 +/*
 1.77877 +** Call sqlite3ExprAnalyzeAggregates() for every expression in an
 1.77878 +** expression list.  Return the number of errors.
 1.77879 +**
 1.77880 +** If an error is found, the analysis is cut short.
 1.77881 +*/
 1.77882 +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
 1.77883 +  struct ExprList_item *pItem;
 1.77884 +  int i;
 1.77885 +  if( pList ){
 1.77886 +    for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
 1.77887 +      sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
 1.77888 +    }
 1.77889 +  }
 1.77890 +}
 1.77891 +
 1.77892 +/*
 1.77893 +** Allocate a single new register for use to hold some intermediate result.
 1.77894 +*/
 1.77895 +SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
 1.77896 +  if( pParse->nTempReg==0 ){
 1.77897 +    return ++pParse->nMem;
 1.77898 +  }
 1.77899 +  return pParse->aTempReg[--pParse->nTempReg];
 1.77900 +}
 1.77901 +
 1.77902 +/*
 1.77903 +** Deallocate a register, making available for reuse for some other
 1.77904 +** purpose.
 1.77905 +**
 1.77906 +** If a register is currently being used by the column cache, then
 1.77907 +** the dallocation is deferred until the column cache line that uses
 1.77908 +** the register becomes stale.
 1.77909 +*/
 1.77910 +SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
 1.77911 +  if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 1.77912 +    int i;
 1.77913 +    struct yColCache *p;
 1.77914 +    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 1.77915 +      if( p->iReg==iReg ){
 1.77916 +        p->tempReg = 1;
 1.77917 +        return;
 1.77918 +      }
 1.77919 +    }
 1.77920 +    pParse->aTempReg[pParse->nTempReg++] = iReg;
 1.77921 +  }
 1.77922 +}
 1.77923 +
 1.77924 +/*
 1.77925 +** Allocate or deallocate a block of nReg consecutive registers
 1.77926 +*/
 1.77927 +SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
 1.77928 +  int i, n;
 1.77929 +  i = pParse->iRangeReg;
 1.77930 +  n = pParse->nRangeReg;
 1.77931 +  if( nReg<=n ){
 1.77932 +    assert( !usedAsColumnCache(pParse, i, i+n-1) );
 1.77933 +    pParse->iRangeReg += nReg;
 1.77934 +    pParse->nRangeReg -= nReg;
 1.77935 +  }else{
 1.77936 +    i = pParse->nMem+1;
 1.77937 +    pParse->nMem += nReg;
 1.77938 +  }
 1.77939 +  return i;
 1.77940 +}
 1.77941 +SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
 1.77942 +  sqlite3ExprCacheRemove(pParse, iReg, nReg);
 1.77943 +  if( nReg>pParse->nRangeReg ){
 1.77944 +    pParse->nRangeReg = nReg;
 1.77945 +    pParse->iRangeReg = iReg;
 1.77946 +  }
 1.77947 +}
 1.77948 +
 1.77949 +/*
 1.77950 +** Mark all temporary registers as being unavailable for reuse.
 1.77951 +*/
 1.77952 +SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
 1.77953 +  pParse->nTempReg = 0;
 1.77954 +  pParse->nRangeReg = 0;
 1.77955 +}
 1.77956 +
 1.77957 +/************** End of expr.c ************************************************/
 1.77958 +/************** Begin file alter.c *******************************************/
 1.77959 +/*
 1.77960 +** 2005 February 15
 1.77961 +**
 1.77962 +** The author disclaims copyright to this source code.  In place of
 1.77963 +** a legal notice, here is a blessing:
 1.77964 +**
 1.77965 +**    May you do good and not evil.
 1.77966 +**    May you find forgiveness for yourself and forgive others.
 1.77967 +**    May you share freely, never taking more than you give.
 1.77968 +**
 1.77969 +*************************************************************************
 1.77970 +** This file contains C code routines that used to generate VDBE code
 1.77971 +** that implements the ALTER TABLE command.
 1.77972 +*/
 1.77973 +
 1.77974 +/*
 1.77975 +** The code in this file only exists if we are not omitting the
 1.77976 +** ALTER TABLE logic from the build.
 1.77977 +*/
 1.77978 +#ifndef SQLITE_OMIT_ALTERTABLE
 1.77979 +
 1.77980 +
 1.77981 +/*
 1.77982 +** This function is used by SQL generated to implement the 
 1.77983 +** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
 1.77984 +** CREATE INDEX command. The second is a table name. The table name in 
 1.77985 +** the CREATE TABLE or CREATE INDEX statement is replaced with the third
 1.77986 +** argument and the result returned. Examples:
 1.77987 +**
 1.77988 +** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
 1.77989 +**     -> 'CREATE TABLE def(a, b, c)'
 1.77990 +**
 1.77991 +** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
 1.77992 +**     -> 'CREATE INDEX i ON def(a, b, c)'
 1.77993 +*/
 1.77994 +static void renameTableFunc(
 1.77995 +  sqlite3_context *context,
 1.77996 +  int NotUsed,
 1.77997 +  sqlite3_value **argv
 1.77998 +){
 1.77999 +  unsigned char const *zSql = sqlite3_value_text(argv[0]);
 1.78000 +  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
 1.78001 +
 1.78002 +  int token;
 1.78003 +  Token tname;
 1.78004 +  unsigned char const *zCsr = zSql;
 1.78005 +  int len = 0;
 1.78006 +  char *zRet;
 1.78007 +
 1.78008 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.78009 +
 1.78010 +  UNUSED_PARAMETER(NotUsed);
 1.78011 +
 1.78012 +  /* The principle used to locate the table name in the CREATE TABLE 
 1.78013 +  ** statement is that the table name is the first non-space token that
 1.78014 +  ** is immediately followed by a TK_LP or TK_USING token.
 1.78015 +  */
 1.78016 +  if( zSql ){
 1.78017 +    do {
 1.78018 +      if( !*zCsr ){
 1.78019 +        /* Ran out of input before finding an opening bracket. Return NULL. */
 1.78020 +        return;
 1.78021 +      }
 1.78022 +
 1.78023 +      /* Store the token that zCsr points to in tname. */
 1.78024 +      tname.z = (char*)zCsr;
 1.78025 +      tname.n = len;
 1.78026 +
 1.78027 +      /* Advance zCsr to the next token. Store that token type in 'token',
 1.78028 +      ** and its length in 'len' (to be used next iteration of this loop).
 1.78029 +      */
 1.78030 +      do {
 1.78031 +        zCsr += len;
 1.78032 +        len = sqlite3GetToken(zCsr, &token);
 1.78033 +      } while( token==TK_SPACE );
 1.78034 +      assert( len>0 );
 1.78035 +    } while( token!=TK_LP && token!=TK_USING );
 1.78036 +
 1.78037 +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
 1.78038 +       zTableName, tname.z+tname.n);
 1.78039 +    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 1.78040 +  }
 1.78041 +}
 1.78042 +
 1.78043 +/*
 1.78044 +** This C function implements an SQL user function that is used by SQL code
 1.78045 +** generated by the ALTER TABLE ... RENAME command to modify the definition
 1.78046 +** of any foreign key constraints that use the table being renamed as the 
 1.78047 +** parent table. It is passed three arguments:
 1.78048 +**
 1.78049 +**   1) The complete text of the CREATE TABLE statement being modified,
 1.78050 +**   2) The old name of the table being renamed, and
 1.78051 +**   3) The new name of the table being renamed.
 1.78052 +**
 1.78053 +** It returns the new CREATE TABLE statement. For example:
 1.78054 +**
 1.78055 +**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
 1.78056 +**       -> 'CREATE TABLE t1(a REFERENCES t3)'
 1.78057 +*/
 1.78058 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.78059 +static void renameParentFunc(
 1.78060 +  sqlite3_context *context,
 1.78061 +  int NotUsed,
 1.78062 +  sqlite3_value **argv
 1.78063 +){
 1.78064 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.78065 +  char *zOutput = 0;
 1.78066 +  char *zResult;
 1.78067 +  unsigned char const *zInput = sqlite3_value_text(argv[0]);
 1.78068 +  unsigned char const *zOld = sqlite3_value_text(argv[1]);
 1.78069 +  unsigned char const *zNew = sqlite3_value_text(argv[2]);
 1.78070 +
 1.78071 +  unsigned const char *z;         /* Pointer to token */
 1.78072 +  int n;                          /* Length of token z */
 1.78073 +  int token;                      /* Type of token */
 1.78074 +
 1.78075 +  UNUSED_PARAMETER(NotUsed);
 1.78076 +  for(z=zInput; *z; z=z+n){
 1.78077 +    n = sqlite3GetToken(z, &token);
 1.78078 +    if( token==TK_REFERENCES ){
 1.78079 +      char *zParent;
 1.78080 +      do {
 1.78081 +        z += n;
 1.78082 +        n = sqlite3GetToken(z, &token);
 1.78083 +      }while( token==TK_SPACE );
 1.78084 +
 1.78085 +      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
 1.78086 +      if( zParent==0 ) break;
 1.78087 +      sqlite3Dequote(zParent);
 1.78088 +      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
 1.78089 +        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
 1.78090 +            (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
 1.78091 +        );
 1.78092 +        sqlite3DbFree(db, zOutput);
 1.78093 +        zOutput = zOut;
 1.78094 +        zInput = &z[n];
 1.78095 +      }
 1.78096 +      sqlite3DbFree(db, zParent);
 1.78097 +    }
 1.78098 +  }
 1.78099 +
 1.78100 +  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
 1.78101 +  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
 1.78102 +  sqlite3DbFree(db, zOutput);
 1.78103 +}
 1.78104 +#endif
 1.78105 +
 1.78106 +#ifndef SQLITE_OMIT_TRIGGER
 1.78107 +/* This function is used by SQL generated to implement the
 1.78108 +** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
 1.78109 +** statement. The second is a table name. The table name in the CREATE 
 1.78110 +** TRIGGER statement is replaced with the third argument and the result 
 1.78111 +** returned. This is analagous to renameTableFunc() above, except for CREATE
 1.78112 +** TRIGGER, not CREATE INDEX and CREATE TABLE.
 1.78113 +*/
 1.78114 +static void renameTriggerFunc(
 1.78115 +  sqlite3_context *context,
 1.78116 +  int NotUsed,
 1.78117 +  sqlite3_value **argv
 1.78118 +){
 1.78119 +  unsigned char const *zSql = sqlite3_value_text(argv[0]);
 1.78120 +  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
 1.78121 +
 1.78122 +  int token;
 1.78123 +  Token tname;
 1.78124 +  int dist = 3;
 1.78125 +  unsigned char const *zCsr = zSql;
 1.78126 +  int len = 0;
 1.78127 +  char *zRet;
 1.78128 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.78129 +
 1.78130 +  UNUSED_PARAMETER(NotUsed);
 1.78131 +
 1.78132 +  /* The principle used to locate the table name in the CREATE TRIGGER 
 1.78133 +  ** statement is that the table name is the first token that is immediatedly
 1.78134 +  ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
 1.78135 +  ** of TK_WHEN, TK_BEGIN or TK_FOR.
 1.78136 +  */
 1.78137 +  if( zSql ){
 1.78138 +    do {
 1.78139 +
 1.78140 +      if( !*zCsr ){
 1.78141 +        /* Ran out of input before finding the table name. Return NULL. */
 1.78142 +        return;
 1.78143 +      }
 1.78144 +
 1.78145 +      /* Store the token that zCsr points to in tname. */
 1.78146 +      tname.z = (char*)zCsr;
 1.78147 +      tname.n = len;
 1.78148 +
 1.78149 +      /* Advance zCsr to the next token. Store that token type in 'token',
 1.78150 +      ** and its length in 'len' (to be used next iteration of this loop).
 1.78151 +      */
 1.78152 +      do {
 1.78153 +        zCsr += len;
 1.78154 +        len = sqlite3GetToken(zCsr, &token);
 1.78155 +      }while( token==TK_SPACE );
 1.78156 +      assert( len>0 );
 1.78157 +
 1.78158 +      /* Variable 'dist' stores the number of tokens read since the most
 1.78159 +      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
 1.78160 +      ** token is read and 'dist' equals 2, the condition stated above
 1.78161 +      ** to be met.
 1.78162 +      **
 1.78163 +      ** Note that ON cannot be a database, table or column name, so
 1.78164 +      ** there is no need to worry about syntax like 
 1.78165 +      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
 1.78166 +      */
 1.78167 +      dist++;
 1.78168 +      if( token==TK_DOT || token==TK_ON ){
 1.78169 +        dist = 0;
 1.78170 +      }
 1.78171 +    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
 1.78172 +
 1.78173 +    /* Variable tname now contains the token that is the old table-name
 1.78174 +    ** in the CREATE TRIGGER statement.
 1.78175 +    */
 1.78176 +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
 1.78177 +       zTableName, tname.z+tname.n);
 1.78178 +    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 1.78179 +  }
 1.78180 +}
 1.78181 +#endif   /* !SQLITE_OMIT_TRIGGER */
 1.78182 +
 1.78183 +/*
 1.78184 +** Register built-in functions used to help implement ALTER TABLE
 1.78185 +*/
 1.78186 +SQLITE_PRIVATE void sqlite3AlterFunctions(void){
 1.78187 +  static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
 1.78188 +    FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
 1.78189 +#ifndef SQLITE_OMIT_TRIGGER
 1.78190 +    FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
 1.78191 +#endif
 1.78192 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.78193 +    FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
 1.78194 +#endif
 1.78195 +  };
 1.78196 +  int i;
 1.78197 +  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 1.78198 +  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
 1.78199 +
 1.78200 +  for(i=0; i<ArraySize(aAlterTableFuncs); i++){
 1.78201 +    sqlite3FuncDefInsert(pHash, &aFunc[i]);
 1.78202 +  }
 1.78203 +}
 1.78204 +
 1.78205 +/*
 1.78206 +** This function is used to create the text of expressions of the form:
 1.78207 +**
 1.78208 +**   name=<constant1> OR name=<constant2> OR ...
 1.78209 +**
 1.78210 +** If argument zWhere is NULL, then a pointer string containing the text 
 1.78211 +** "name=<constant>" is returned, where <constant> is the quoted version
 1.78212 +** of the string passed as argument zConstant. The returned buffer is
 1.78213 +** allocated using sqlite3DbMalloc(). It is the responsibility of the
 1.78214 +** caller to ensure that it is eventually freed.
 1.78215 +**
 1.78216 +** If argument zWhere is not NULL, then the string returned is 
 1.78217 +** "<where> OR name=<constant>", where <where> is the contents of zWhere.
 1.78218 +** In this case zWhere is passed to sqlite3DbFree() before returning.
 1.78219 +** 
 1.78220 +*/
 1.78221 +static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
 1.78222 +  char *zNew;
 1.78223 +  if( !zWhere ){
 1.78224 +    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
 1.78225 +  }else{
 1.78226 +    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
 1.78227 +    sqlite3DbFree(db, zWhere);
 1.78228 +  }
 1.78229 +  return zNew;
 1.78230 +}
 1.78231 +
 1.78232 +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 1.78233 +/*
 1.78234 +** Generate the text of a WHERE expression which can be used to select all
 1.78235 +** tables that have foreign key constraints that refer to table pTab (i.e.
 1.78236 +** constraints for which pTab is the parent table) from the sqlite_master
 1.78237 +** table.
 1.78238 +*/
 1.78239 +static char *whereForeignKeys(Parse *pParse, Table *pTab){
 1.78240 +  FKey *p;
 1.78241 +  char *zWhere = 0;
 1.78242 +  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 1.78243 +    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
 1.78244 +  }
 1.78245 +  return zWhere;
 1.78246 +}
 1.78247 +#endif
 1.78248 +
 1.78249 +/*
 1.78250 +** Generate the text of a WHERE expression which can be used to select all
 1.78251 +** temporary triggers on table pTab from the sqlite_temp_master table. If
 1.78252 +** table pTab has no temporary triggers, or is itself stored in the 
 1.78253 +** temporary database, NULL is returned.
 1.78254 +*/
 1.78255 +static char *whereTempTriggers(Parse *pParse, Table *pTab){
 1.78256 +  Trigger *pTrig;
 1.78257 +  char *zWhere = 0;
 1.78258 +  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
 1.78259 +
 1.78260 +  /* If the table is not located in the temp-db (in which case NULL is 
 1.78261 +  ** returned, loop through the tables list of triggers. For each trigger
 1.78262 +  ** that is not part of the temp-db schema, add a clause to the WHERE 
 1.78263 +  ** expression being built up in zWhere.
 1.78264 +  */
 1.78265 +  if( pTab->pSchema!=pTempSchema ){
 1.78266 +    sqlite3 *db = pParse->db;
 1.78267 +    for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 1.78268 +      if( pTrig->pSchema==pTempSchema ){
 1.78269 +        zWhere = whereOrName(db, zWhere, pTrig->zName);
 1.78270 +      }
 1.78271 +    }
 1.78272 +  }
 1.78273 +  if( zWhere ){
 1.78274 +    char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
 1.78275 +    sqlite3DbFree(pParse->db, zWhere);
 1.78276 +    zWhere = zNew;
 1.78277 +  }
 1.78278 +  return zWhere;
 1.78279 +}
 1.78280 +
 1.78281 +/*
 1.78282 +** Generate code to drop and reload the internal representation of table
 1.78283 +** pTab from the database, including triggers and temporary triggers.
 1.78284 +** Argument zName is the name of the table in the database schema at
 1.78285 +** the time the generated code is executed. This can be different from
 1.78286 +** pTab->zName if this function is being called to code part of an 
 1.78287 +** "ALTER TABLE RENAME TO" statement.
 1.78288 +*/
 1.78289 +static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
 1.78290 +  Vdbe *v;
 1.78291 +  char *zWhere;
 1.78292 +  int iDb;                   /* Index of database containing pTab */
 1.78293 +#ifndef SQLITE_OMIT_TRIGGER
 1.78294 +  Trigger *pTrig;
 1.78295 +#endif
 1.78296 +
 1.78297 +  v = sqlite3GetVdbe(pParse);
 1.78298 +  if( NEVER(v==0) ) return;
 1.78299 +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 1.78300 +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.78301 +  assert( iDb>=0 );
 1.78302 +
 1.78303 +#ifndef SQLITE_OMIT_TRIGGER
 1.78304 +  /* Drop any table triggers from the internal schema. */
 1.78305 +  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 1.78306 +    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
 1.78307 +    assert( iTrigDb==iDb || iTrigDb==1 );
 1.78308 +    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
 1.78309 +  }
 1.78310 +#endif
 1.78311 +
 1.78312 +  /* Drop the table and index from the internal schema.  */
 1.78313 +  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 1.78314 +
 1.78315 +  /* Reload the table, index and permanent trigger schemas. */
 1.78316 +  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
 1.78317 +  if( !zWhere ) return;
 1.78318 +  sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
 1.78319 +
 1.78320 +#ifndef SQLITE_OMIT_TRIGGER
 1.78321 +  /* Now, if the table is not stored in the temp database, reload any temp 
 1.78322 +  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
 1.78323 +  */
 1.78324 +  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 1.78325 +    sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
 1.78326 +  }
 1.78327 +#endif
 1.78328 +}
 1.78329 +
 1.78330 +/*
 1.78331 +** Parameter zName is the name of a table that is about to be altered
 1.78332 +** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
 1.78333 +** If the table is a system table, this function leaves an error message
 1.78334 +** in pParse->zErr (system tables may not be altered) and returns non-zero.
 1.78335 +**
 1.78336 +** Or, if zName is not a system table, zero is returned.
 1.78337 +*/
 1.78338 +static int isSystemTable(Parse *pParse, const char *zName){
 1.78339 +  if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
 1.78340 +    sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
 1.78341 +    return 1;
 1.78342 +  }
 1.78343 +  return 0;
 1.78344 +}
 1.78345 +
 1.78346 +/*
 1.78347 +** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
 1.78348 +** command. 
 1.78349 +*/
 1.78350 +SQLITE_PRIVATE void sqlite3AlterRenameTable(
 1.78351 +  Parse *pParse,            /* Parser context. */
 1.78352 +  SrcList *pSrc,            /* The table to rename. */
 1.78353 +  Token *pName              /* The new table name. */
 1.78354 +){
 1.78355 +  int iDb;                  /* Database that contains the table */
 1.78356 +  char *zDb;                /* Name of database iDb */
 1.78357 +  Table *pTab;              /* Table being renamed */
 1.78358 +  char *zName = 0;          /* NULL-terminated version of pName */ 
 1.78359 +  sqlite3 *db = pParse->db; /* Database connection */
 1.78360 +  int nTabName;             /* Number of UTF-8 characters in zTabName */
 1.78361 +  const char *zTabName;     /* Original name of the table */
 1.78362 +  Vdbe *v;
 1.78363 +#ifndef SQLITE_OMIT_TRIGGER
 1.78364 +  char *zWhere = 0;         /* Where clause to locate temp triggers */
 1.78365 +#endif
 1.78366 +  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
 1.78367 +  int savedDbFlags;         /* Saved value of db->flags */
 1.78368 +
 1.78369 +  savedDbFlags = db->flags;  
 1.78370 +  if( NEVER(db->mallocFailed) ) goto exit_rename_table;
 1.78371 +  assert( pSrc->nSrc==1 );
 1.78372 +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 1.78373 +
 1.78374 +  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 1.78375 +  if( !pTab ) goto exit_rename_table;
 1.78376 +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.78377 +  zDb = db->aDb[iDb].zName;
 1.78378 +  db->flags |= SQLITE_PreferBuiltin;
 1.78379 +
 1.78380 +  /* Get a NULL terminated version of the new table name. */
 1.78381 +  zName = sqlite3NameFromToken(db, pName);
 1.78382 +  if( !zName ) goto exit_rename_table;
 1.78383 +
 1.78384 +  /* Check that a table or index named 'zName' does not already exist
 1.78385 +  ** in database iDb. If so, this is an error.
 1.78386 +  */
 1.78387 +  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
 1.78388 +    sqlite3ErrorMsg(pParse, 
 1.78389 +        "there is already another table or index with this name: %s", zName);
 1.78390 +    goto exit_rename_table;
 1.78391 +  }
 1.78392 +
 1.78393 +  /* Make sure it is not a system table being altered, or a reserved name
 1.78394 +  ** that the table is being renamed to.
 1.78395 +  */
 1.78396 +  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 1.78397 +    goto exit_rename_table;
 1.78398 +  }
 1.78399 +  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
 1.78400 +    exit_rename_table;
 1.78401 +  }
 1.78402 +
 1.78403 +#ifndef SQLITE_OMIT_VIEW
 1.78404 +  if( pTab->pSelect ){
 1.78405 +    sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
 1.78406 +    goto exit_rename_table;
 1.78407 +  }
 1.78408 +#endif
 1.78409 +
 1.78410 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.78411 +  /* Invoke the authorization callback. */
 1.78412 +  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
 1.78413 +    goto exit_rename_table;
 1.78414 +  }
 1.78415 +#endif
 1.78416 +
 1.78417 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.78418 +  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 1.78419 +    goto exit_rename_table;
 1.78420 +  }
 1.78421 +  if( IsVirtual(pTab) ){
 1.78422 +    pVTab = sqlite3GetVTable(db, pTab);
 1.78423 +    if( pVTab->pVtab->pModule->xRename==0 ){
 1.78424 +      pVTab = 0;
 1.78425 +    }
 1.78426 +  }
 1.78427 +#endif
 1.78428 +
 1.78429 +  /* Begin a transaction and code the VerifyCookie for database iDb. 
 1.78430 +  ** Then modify the schema cookie (since the ALTER TABLE modifies the
 1.78431 +  ** schema). Open a statement transaction if the table is a virtual
 1.78432 +  ** table.
 1.78433 +  */
 1.78434 +  v = sqlite3GetVdbe(pParse);
 1.78435 +  if( v==0 ){
 1.78436 +    goto exit_rename_table;
 1.78437 +  }
 1.78438 +  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
 1.78439 +  sqlite3ChangeCookie(pParse, iDb);
 1.78440 +
 1.78441 +  /* If this is a virtual table, invoke the xRename() function if
 1.78442 +  ** one is defined. The xRename() callback will modify the names
 1.78443 +  ** of any resources used by the v-table implementation (including other
 1.78444 +  ** SQLite tables) that are identified by the name of the virtual table.
 1.78445 +  */
 1.78446 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.78447 +  if( pVTab ){
 1.78448 +    int i = ++pParse->nMem;
 1.78449 +    sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
 1.78450 +    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
 1.78451 +    sqlite3MayAbort(pParse);
 1.78452 +  }
 1.78453 +#endif
 1.78454 +
 1.78455 +  /* figure out how many UTF-8 characters are in zName */
 1.78456 +  zTabName = pTab->zName;
 1.78457 +  nTabName = sqlite3Utf8CharLen(zTabName, -1);
 1.78458 +
 1.78459 +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 1.78460 +  if( db->flags&SQLITE_ForeignKeys ){
 1.78461 +    /* If foreign-key support is enabled, rewrite the CREATE TABLE 
 1.78462 +    ** statements corresponding to all child tables of foreign key constraints
 1.78463 +    ** for which the renamed table is the parent table.  */
 1.78464 +    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
 1.78465 +      sqlite3NestedParse(pParse, 
 1.78466 +          "UPDATE \"%w\".%s SET "
 1.78467 +              "sql = sqlite_rename_parent(sql, %Q, %Q) "
 1.78468 +              "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
 1.78469 +      sqlite3DbFree(db, zWhere);
 1.78470 +    }
 1.78471 +  }
 1.78472 +#endif
 1.78473 +
 1.78474 +  /* Modify the sqlite_master table to use the new table name. */
 1.78475 +  sqlite3NestedParse(pParse,
 1.78476 +      "UPDATE %Q.%s SET "
 1.78477 +#ifdef SQLITE_OMIT_TRIGGER
 1.78478 +          "sql = sqlite_rename_table(sql, %Q), "
 1.78479 +#else
 1.78480 +          "sql = CASE "
 1.78481 +            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
 1.78482 +            "ELSE sqlite_rename_table(sql, %Q) END, "
 1.78483 +#endif
 1.78484 +          "tbl_name = %Q, "
 1.78485 +          "name = CASE "
 1.78486 +            "WHEN type='table' THEN %Q "
 1.78487 +            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
 1.78488 +             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
 1.78489 +            "ELSE name END "
 1.78490 +      "WHERE tbl_name=%Q COLLATE nocase AND "
 1.78491 +          "(type='table' OR type='index' OR type='trigger');", 
 1.78492 +      zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
 1.78493 +#ifndef SQLITE_OMIT_TRIGGER
 1.78494 +      zName,
 1.78495 +#endif
 1.78496 +      zName, nTabName, zTabName
 1.78497 +  );
 1.78498 +
 1.78499 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.78500 +  /* If the sqlite_sequence table exists in this database, then update 
 1.78501 +  ** it with the new table name.
 1.78502 +  */
 1.78503 +  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
 1.78504 +    sqlite3NestedParse(pParse,
 1.78505 +        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
 1.78506 +        zDb, zName, pTab->zName);
 1.78507 +  }
 1.78508 +#endif
 1.78509 +
 1.78510 +#ifndef SQLITE_OMIT_TRIGGER
 1.78511 +  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
 1.78512 +  ** table. Don't do this if the table being ALTERed is itself located in
 1.78513 +  ** the temp database.
 1.78514 +  */
 1.78515 +  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 1.78516 +    sqlite3NestedParse(pParse, 
 1.78517 +        "UPDATE sqlite_temp_master SET "
 1.78518 +            "sql = sqlite_rename_trigger(sql, %Q), "
 1.78519 +            "tbl_name = %Q "
 1.78520 +            "WHERE %s;", zName, zName, zWhere);
 1.78521 +    sqlite3DbFree(db, zWhere);
 1.78522 +  }
 1.78523 +#endif
 1.78524 +
 1.78525 +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 1.78526 +  if( db->flags&SQLITE_ForeignKeys ){
 1.78527 +    FKey *p;
 1.78528 +    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 1.78529 +      Table *pFrom = p->pFrom;
 1.78530 +      if( pFrom!=pTab ){
 1.78531 +        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
 1.78532 +      }
 1.78533 +    }
 1.78534 +  }
 1.78535 +#endif
 1.78536 +
 1.78537 +  /* Drop and reload the internal table schema. */
 1.78538 +  reloadTableSchema(pParse, pTab, zName);
 1.78539 +
 1.78540 +exit_rename_table:
 1.78541 +  sqlite3SrcListDelete(db, pSrc);
 1.78542 +  sqlite3DbFree(db, zName);
 1.78543 +  db->flags = savedDbFlags;
 1.78544 +}
 1.78545 +
 1.78546 +
 1.78547 +/*
 1.78548 +** Generate code to make sure the file format number is at least minFormat.
 1.78549 +** The generated code will increase the file format number if necessary.
 1.78550 +*/
 1.78551 +SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
 1.78552 +  Vdbe *v;
 1.78553 +  v = sqlite3GetVdbe(pParse);
 1.78554 +  /* The VDBE should have been allocated before this routine is called.
 1.78555 +  ** If that allocation failed, we would have quit before reaching this
 1.78556 +  ** point */
 1.78557 +  if( ALWAYS(v) ){
 1.78558 +    int r1 = sqlite3GetTempReg(pParse);
 1.78559 +    int r2 = sqlite3GetTempReg(pParse);
 1.78560 +    int j1;
 1.78561 +    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
 1.78562 +    sqlite3VdbeUsesBtree(v, iDb);
 1.78563 +    sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
 1.78564 +    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
 1.78565 +    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
 1.78566 +    sqlite3VdbeJumpHere(v, j1);
 1.78567 +    sqlite3ReleaseTempReg(pParse, r1);
 1.78568 +    sqlite3ReleaseTempReg(pParse, r2);
 1.78569 +  }
 1.78570 +}
 1.78571 +
 1.78572 +/*
 1.78573 +** This function is called after an "ALTER TABLE ... ADD" statement
 1.78574 +** has been parsed. Argument pColDef contains the text of the new
 1.78575 +** column definition.
 1.78576 +**
 1.78577 +** The Table structure pParse->pNewTable was extended to include
 1.78578 +** the new column during parsing.
 1.78579 +*/
 1.78580 +SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
 1.78581 +  Table *pNew;              /* Copy of pParse->pNewTable */
 1.78582 +  Table *pTab;              /* Table being altered */
 1.78583 +  int iDb;                  /* Database number */
 1.78584 +  const char *zDb;          /* Database name */
 1.78585 +  const char *zTab;         /* Table name */
 1.78586 +  char *zCol;               /* Null-terminated column definition */
 1.78587 +  Column *pCol;             /* The new column */
 1.78588 +  Expr *pDflt;              /* Default value for the new column */
 1.78589 +  sqlite3 *db;              /* The database connection; */
 1.78590 +
 1.78591 +  db = pParse->db;
 1.78592 +  if( pParse->nErr || db->mallocFailed ) return;
 1.78593 +  pNew = pParse->pNewTable;
 1.78594 +  assert( pNew );
 1.78595 +
 1.78596 +  assert( sqlite3BtreeHoldsAllMutexes(db) );
 1.78597 +  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
 1.78598 +  zDb = db->aDb[iDb].zName;
 1.78599 +  zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
 1.78600 +  pCol = &pNew->aCol[pNew->nCol-1];
 1.78601 +  pDflt = pCol->pDflt;
 1.78602 +  pTab = sqlite3FindTable(db, zTab, zDb);
 1.78603 +  assert( pTab );
 1.78604 +
 1.78605 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.78606 +  /* Invoke the authorization callback. */
 1.78607 +  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
 1.78608 +    return;
 1.78609 +  }
 1.78610 +#endif
 1.78611 +
 1.78612 +  /* If the default value for the new column was specified with a 
 1.78613 +  ** literal NULL, then set pDflt to 0. This simplifies checking
 1.78614 +  ** for an SQL NULL default below.
 1.78615 +  */
 1.78616 +  if( pDflt && pDflt->op==TK_NULL ){
 1.78617 +    pDflt = 0;
 1.78618 +  }
 1.78619 +
 1.78620 +  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
 1.78621 +  ** If there is a NOT NULL constraint, then the default value for the
 1.78622 +  ** column must not be NULL.
 1.78623 +  */
 1.78624 +  if( pCol->colFlags & COLFLAG_PRIMKEY ){
 1.78625 +    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
 1.78626 +    return;
 1.78627 +  }
 1.78628 +  if( pNew->pIndex ){
 1.78629 +    sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
 1.78630 +    return;
 1.78631 +  }
 1.78632 +  if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
 1.78633 +    sqlite3ErrorMsg(pParse, 
 1.78634 +        "Cannot add a REFERENCES column with non-NULL default value");
 1.78635 +    return;
 1.78636 +  }
 1.78637 +  if( pCol->notNull && !pDflt ){
 1.78638 +    sqlite3ErrorMsg(pParse, 
 1.78639 +        "Cannot add a NOT NULL column with default value NULL");
 1.78640 +    return;
 1.78641 +  }
 1.78642 +
 1.78643 +  /* Ensure the default expression is something that sqlite3ValueFromExpr()
 1.78644 +  ** can handle (i.e. not CURRENT_TIME etc.)
 1.78645 +  */
 1.78646 +  if( pDflt ){
 1.78647 +    sqlite3_value *pVal;
 1.78648 +    if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
 1.78649 +      db->mallocFailed = 1;
 1.78650 +      return;
 1.78651 +    }
 1.78652 +    if( !pVal ){
 1.78653 +      sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
 1.78654 +      return;
 1.78655 +    }
 1.78656 +    sqlite3ValueFree(pVal);
 1.78657 +  }
 1.78658 +
 1.78659 +  /* Modify the CREATE TABLE statement. */
 1.78660 +  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
 1.78661 +  if( zCol ){
 1.78662 +    char *zEnd = &zCol[pColDef->n-1];
 1.78663 +    int savedDbFlags = db->flags;
 1.78664 +    while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
 1.78665 +      *zEnd-- = '\0';
 1.78666 +    }
 1.78667 +    db->flags |= SQLITE_PreferBuiltin;
 1.78668 +    sqlite3NestedParse(pParse, 
 1.78669 +        "UPDATE \"%w\".%s SET "
 1.78670 +          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
 1.78671 +        "WHERE type = 'table' AND name = %Q", 
 1.78672 +      zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
 1.78673 +      zTab
 1.78674 +    );
 1.78675 +    sqlite3DbFree(db, zCol);
 1.78676 +    db->flags = savedDbFlags;
 1.78677 +  }
 1.78678 +
 1.78679 +  /* If the default value of the new column is NULL, then set the file
 1.78680 +  ** format to 2. If the default value of the new column is not NULL,
 1.78681 +  ** the file format becomes 3.
 1.78682 +  */
 1.78683 +  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
 1.78684 +
 1.78685 +  /* Reload the schema of the modified table. */
 1.78686 +  reloadTableSchema(pParse, pTab, pTab->zName);
 1.78687 +}
 1.78688 +
 1.78689 +/*
 1.78690 +** This function is called by the parser after the table-name in
 1.78691 +** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
 1.78692 +** pSrc is the full-name of the table being altered.
 1.78693 +**
 1.78694 +** This routine makes a (partial) copy of the Table structure
 1.78695 +** for the table being altered and sets Parse.pNewTable to point
 1.78696 +** to it. Routines called by the parser as the column definition
 1.78697 +** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
 1.78698 +** the copy. The copy of the Table structure is deleted by tokenize.c 
 1.78699 +** after parsing is finished.
 1.78700 +**
 1.78701 +** Routine sqlite3AlterFinishAddColumn() will be called to complete
 1.78702 +** coding the "ALTER TABLE ... ADD" statement.
 1.78703 +*/
 1.78704 +SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
 1.78705 +  Table *pNew;
 1.78706 +  Table *pTab;
 1.78707 +  Vdbe *v;
 1.78708 +  int iDb;
 1.78709 +  int i;
 1.78710 +  int nAlloc;
 1.78711 +  sqlite3 *db = pParse->db;
 1.78712 +
 1.78713 +  /* Look up the table being altered. */
 1.78714 +  assert( pParse->pNewTable==0 );
 1.78715 +  assert( sqlite3BtreeHoldsAllMutexes(db) );
 1.78716 +  if( db->mallocFailed ) goto exit_begin_add_column;
 1.78717 +  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 1.78718 +  if( !pTab ) goto exit_begin_add_column;
 1.78719 +
 1.78720 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.78721 +  if( IsVirtual(pTab) ){
 1.78722 +    sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
 1.78723 +    goto exit_begin_add_column;
 1.78724 +  }
 1.78725 +#endif
 1.78726 +
 1.78727 +  /* Make sure this is not an attempt to ALTER a view. */
 1.78728 +  if( pTab->pSelect ){
 1.78729 +    sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
 1.78730 +    goto exit_begin_add_column;
 1.78731 +  }
 1.78732 +  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 1.78733 +    goto exit_begin_add_column;
 1.78734 +  }
 1.78735 +
 1.78736 +  assert( pTab->addColOffset>0 );
 1.78737 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.78738 +
 1.78739 +  /* Put a copy of the Table struct in Parse.pNewTable for the
 1.78740 +  ** sqlite3AddColumn() function and friends to modify.  But modify
 1.78741 +  ** the name by adding an "sqlite_altertab_" prefix.  By adding this
 1.78742 +  ** prefix, we insure that the name will not collide with an existing
 1.78743 +  ** table because user table are not allowed to have the "sqlite_"
 1.78744 +  ** prefix on their name.
 1.78745 +  */
 1.78746 +  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
 1.78747 +  if( !pNew ) goto exit_begin_add_column;
 1.78748 +  pParse->pNewTable = pNew;
 1.78749 +  pNew->nRef = 1;
 1.78750 +  pNew->nCol = pTab->nCol;
 1.78751 +  assert( pNew->nCol>0 );
 1.78752 +  nAlloc = (((pNew->nCol-1)/8)*8)+8;
 1.78753 +  assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
 1.78754 +  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
 1.78755 +  pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
 1.78756 +  if( !pNew->aCol || !pNew->zName ){
 1.78757 +    db->mallocFailed = 1;
 1.78758 +    goto exit_begin_add_column;
 1.78759 +  }
 1.78760 +  memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
 1.78761 +  for(i=0; i<pNew->nCol; i++){
 1.78762 +    Column *pCol = &pNew->aCol[i];
 1.78763 +    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
 1.78764 +    pCol->zColl = 0;
 1.78765 +    pCol->zType = 0;
 1.78766 +    pCol->pDflt = 0;
 1.78767 +    pCol->zDflt = 0;
 1.78768 +  }
 1.78769 +  pNew->pSchema = db->aDb[iDb].pSchema;
 1.78770 +  pNew->addColOffset = pTab->addColOffset;
 1.78771 +  pNew->nRef = 1;
 1.78772 +
 1.78773 +  /* Begin a transaction and increment the schema cookie.  */
 1.78774 +  sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.78775 +  v = sqlite3GetVdbe(pParse);
 1.78776 +  if( !v ) goto exit_begin_add_column;
 1.78777 +  sqlite3ChangeCookie(pParse, iDb);
 1.78778 +
 1.78779 +exit_begin_add_column:
 1.78780 +  sqlite3SrcListDelete(db, pSrc);
 1.78781 +  return;
 1.78782 +}
 1.78783 +#endif  /* SQLITE_ALTER_TABLE */
 1.78784 +
 1.78785 +/************** End of alter.c ***********************************************/
 1.78786 +/************** Begin file analyze.c *****************************************/
 1.78787 +/*
 1.78788 +** 2005 July 8
 1.78789 +**
 1.78790 +** The author disclaims copyright to this source code.  In place of
 1.78791 +** a legal notice, here is a blessing:
 1.78792 +**
 1.78793 +**    May you do good and not evil.
 1.78794 +**    May you find forgiveness for yourself and forgive others.
 1.78795 +**    May you share freely, never taking more than you give.
 1.78796 +**
 1.78797 +*************************************************************************
 1.78798 +** This file contains code associated with the ANALYZE command.
 1.78799 +**
 1.78800 +** The ANALYZE command gather statistics about the content of tables
 1.78801 +** and indices.  These statistics are made available to the query planner
 1.78802 +** to help it make better decisions about how to perform queries.
 1.78803 +**
 1.78804 +** The following system tables are or have been supported:
 1.78805 +**
 1.78806 +**    CREATE TABLE sqlite_stat1(tbl, idx, stat);
 1.78807 +**    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
 1.78808 +**    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
 1.78809 +**
 1.78810 +** Additional tables might be added in future releases of SQLite.
 1.78811 +** The sqlite_stat2 table is not created or used unless the SQLite version
 1.78812 +** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
 1.78813 +** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
 1.78814 +** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
 1.78815 +** created and used by SQLite versions 3.7.9 and later and with
 1.78816 +** SQLITE_ENABLE_STAT3 defined.  The fucntionality of sqlite_stat3
 1.78817 +** is a superset of sqlite_stat2.  
 1.78818 +**
 1.78819 +** Format of sqlite_stat1:
 1.78820 +**
 1.78821 +** There is normally one row per index, with the index identified by the
 1.78822 +** name in the idx column.  The tbl column is the name of the table to
 1.78823 +** which the index belongs.  In each such row, the stat column will be
 1.78824 +** a string consisting of a list of integers.  The first integer in this
 1.78825 +** list is the number of rows in the index and in the table.  The second
 1.78826 +** integer is the average number of rows in the index that have the same
 1.78827 +** value in the first column of the index.  The third integer is the average
 1.78828 +** number of rows in the index that have the same value for the first two
 1.78829 +** columns.  The N-th integer (for N>1) is the average number of rows in 
 1.78830 +** the index which have the same value for the first N-1 columns.  For
 1.78831 +** a K-column index, there will be K+1 integers in the stat column.  If
 1.78832 +** the index is unique, then the last integer will be 1.
 1.78833 +**
 1.78834 +** The list of integers in the stat column can optionally be followed
 1.78835 +** by the keyword "unordered".  The "unordered" keyword, if it is present,
 1.78836 +** must be separated from the last integer by a single space.  If the
 1.78837 +** "unordered" keyword is present, then the query planner assumes that
 1.78838 +** the index is unordered and will not use the index for a range query.
 1.78839 +** 
 1.78840 +** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
 1.78841 +** column contains a single integer which is the (estimated) number of
 1.78842 +** rows in the table identified by sqlite_stat1.tbl.
 1.78843 +**
 1.78844 +** Format of sqlite_stat2:
 1.78845 +**
 1.78846 +** The sqlite_stat2 is only created and is only used if SQLite is compiled
 1.78847 +** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
 1.78848 +** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
 1.78849 +** about the distribution of keys within an index.  The index is identified by
 1.78850 +** the "idx" column and the "tbl" column is the name of the table to which
 1.78851 +** the index belongs.  There are usually 10 rows in the sqlite_stat2
 1.78852 +** table for each index.
 1.78853 +**
 1.78854 +** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
 1.78855 +** inclusive are samples of the left-most key value in the index taken at
 1.78856 +** evenly spaced points along the index.  Let the number of samples be S
 1.78857 +** (10 in the standard build) and let C be the number of rows in the index.
 1.78858 +** Then the sampled rows are given by:
 1.78859 +**
 1.78860 +**     rownumber = (i*C*2 + C)/(S*2)
 1.78861 +**
 1.78862 +** For i between 0 and S-1.  Conceptually, the index space is divided into
 1.78863 +** S uniform buckets and the samples are the middle row from each bucket.
 1.78864 +**
 1.78865 +** The format for sqlite_stat2 is recorded here for legacy reference.  This
 1.78866 +** version of SQLite does not support sqlite_stat2.  It neither reads nor
 1.78867 +** writes the sqlite_stat2 table.  This version of SQLite only supports
 1.78868 +** sqlite_stat3.
 1.78869 +**
 1.78870 +** Format for sqlite_stat3:
 1.78871 +**
 1.78872 +** The sqlite_stat3 is an enhancement to sqlite_stat2.  A new name is
 1.78873 +** used to avoid compatibility problems.  
 1.78874 +**
 1.78875 +** The format of the sqlite_stat3 table is similar to the format of
 1.78876 +** the sqlite_stat2 table.  There are multiple entries for each index.
 1.78877 +** The idx column names the index and the tbl column is the table of the
 1.78878 +** index.  If the idx and tbl columns are the same, then the sample is
 1.78879 +** of the INTEGER PRIMARY KEY.  The sample column is a value taken from
 1.78880 +** the left-most column of the index.  The nEq column is the approximate
 1.78881 +** number of entires in the index whose left-most column exactly matches
 1.78882 +** the sample.  nLt is the approximate number of entires whose left-most
 1.78883 +** column is less than the sample.  The nDLt column is the approximate
 1.78884 +** number of distinct left-most entries in the index that are less than
 1.78885 +** the sample.
 1.78886 +**
 1.78887 +** Future versions of SQLite might change to store a string containing
 1.78888 +** multiple integers values in the nDLt column of sqlite_stat3.  The first
 1.78889 +** integer will be the number of prior index entires that are distinct in
 1.78890 +** the left-most column.  The second integer will be the number of prior index
 1.78891 +** entries that are distinct in the first two columns.  The third integer
 1.78892 +** will be the number of prior index entries that are distinct in the first
 1.78893 +** three columns.  And so forth.  With that extension, the nDLt field is
 1.78894 +** similar in function to the sqlite_stat1.stat field.
 1.78895 +**
 1.78896 +** There can be an arbitrary number of sqlite_stat3 entries per index.
 1.78897 +** The ANALYZE command will typically generate sqlite_stat3 tables
 1.78898 +** that contain between 10 and 40 samples which are distributed across
 1.78899 +** the key space, though not uniformly, and which include samples with
 1.78900 +** largest possible nEq values.
 1.78901 +*/
 1.78902 +#ifndef SQLITE_OMIT_ANALYZE
 1.78903 +
 1.78904 +/*
 1.78905 +** This routine generates code that opens the sqlite_stat1 table for
 1.78906 +** writing with cursor iStatCur. If the library was built with the
 1.78907 +** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
 1.78908 +** opened for writing using cursor (iStatCur+1)
 1.78909 +**
 1.78910 +** If the sqlite_stat1 tables does not previously exist, it is created.
 1.78911 +** Similarly, if the sqlite_stat3 table does not exist and the library
 1.78912 +** is compiled with SQLITE_ENABLE_STAT3 defined, it is created. 
 1.78913 +**
 1.78914 +** Argument zWhere may be a pointer to a buffer containing a table name,
 1.78915 +** or it may be a NULL pointer. If it is not NULL, then all entries in
 1.78916 +** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
 1.78917 +** with the named table are deleted. If zWhere==0, then code is generated
 1.78918 +** to delete all stat table entries.
 1.78919 +*/
 1.78920 +static void openStatTable(
 1.78921 +  Parse *pParse,          /* Parsing context */
 1.78922 +  int iDb,                /* The database we are looking in */
 1.78923 +  int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
 1.78924 +  const char *zWhere,     /* Delete entries for this table or index */
 1.78925 +  const char *zWhereType  /* Either "tbl" or "idx" */
 1.78926 +){
 1.78927 +  static const struct {
 1.78928 +    const char *zName;
 1.78929 +    const char *zCols;
 1.78930 +  } aTable[] = {
 1.78931 +    { "sqlite_stat1", "tbl,idx,stat" },
 1.78932 +#ifdef SQLITE_ENABLE_STAT3
 1.78933 +    { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
 1.78934 +#endif
 1.78935 +  };
 1.78936 +
 1.78937 +  int aRoot[] = {0, 0};
 1.78938 +  u8 aCreateTbl[] = {0, 0};
 1.78939 +
 1.78940 +  int i;
 1.78941 +  sqlite3 *db = pParse->db;
 1.78942 +  Db *pDb;
 1.78943 +  Vdbe *v = sqlite3GetVdbe(pParse);
 1.78944 +  if( v==0 ) return;
 1.78945 +  assert( sqlite3BtreeHoldsAllMutexes(db) );
 1.78946 +  assert( sqlite3VdbeDb(v)==db );
 1.78947 +  pDb = &db->aDb[iDb];
 1.78948 +
 1.78949 +  /* Create new statistic tables if they do not exist, or clear them
 1.78950 +  ** if they do already exist.
 1.78951 +  */
 1.78952 +  for(i=0; i<ArraySize(aTable); i++){
 1.78953 +    const char *zTab = aTable[i].zName;
 1.78954 +    Table *pStat;
 1.78955 +    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
 1.78956 +      /* The sqlite_stat[12] table does not exist. Create it. Note that a 
 1.78957 +      ** side-effect of the CREATE TABLE statement is to leave the rootpage 
 1.78958 +      ** of the new table in register pParse->regRoot. This is important 
 1.78959 +      ** because the OpenWrite opcode below will be needing it. */
 1.78960 +      sqlite3NestedParse(pParse,
 1.78961 +          "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
 1.78962 +      );
 1.78963 +      aRoot[i] = pParse->regRoot;
 1.78964 +      aCreateTbl[i] = OPFLAG_P2ISREG;
 1.78965 +    }else{
 1.78966 +      /* The table already exists. If zWhere is not NULL, delete all entries 
 1.78967 +      ** associated with the table zWhere. If zWhere is NULL, delete the
 1.78968 +      ** entire contents of the table. */
 1.78969 +      aRoot[i] = pStat->tnum;
 1.78970 +      sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 1.78971 +      if( zWhere ){
 1.78972 +        sqlite3NestedParse(pParse,
 1.78973 +           "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
 1.78974 +        );
 1.78975 +      }else{
 1.78976 +        /* The sqlite_stat[12] table already exists.  Delete all rows. */
 1.78977 +        sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 1.78978 +      }
 1.78979 +    }
 1.78980 +  }
 1.78981 +
 1.78982 +  /* Open the sqlite_stat[13] tables for writing. */
 1.78983 +  for(i=0; i<ArraySize(aTable); i++){
 1.78984 +    sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
 1.78985 +    sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
 1.78986 +    sqlite3VdbeChangeP5(v, aCreateTbl[i]);
 1.78987 +  }
 1.78988 +}
 1.78989 +
 1.78990 +/*
 1.78991 +** Recommended number of samples for sqlite_stat3
 1.78992 +*/
 1.78993 +#ifndef SQLITE_STAT3_SAMPLES
 1.78994 +# define SQLITE_STAT3_SAMPLES 24
 1.78995 +#endif
 1.78996 +
 1.78997 +/*
 1.78998 +** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
 1.78999 +** share an instance of the following structure to hold their state
 1.79000 +** information.
 1.79001 +*/
 1.79002 +typedef struct Stat3Accum Stat3Accum;
 1.79003 +struct Stat3Accum {
 1.79004 +  tRowcnt nRow;             /* Number of rows in the entire table */
 1.79005 +  tRowcnt nPSample;         /* How often to do a periodic sample */
 1.79006 +  int iMin;                 /* Index of entry with minimum nEq and hash */
 1.79007 +  int mxSample;             /* Maximum number of samples to accumulate */
 1.79008 +  int nSample;              /* Current number of samples */
 1.79009 +  u32 iPrn;                 /* Pseudo-random number used for sampling */
 1.79010 +  struct Stat3Sample {
 1.79011 +    i64 iRowid;                /* Rowid in main table of the key */
 1.79012 +    tRowcnt nEq;               /* sqlite_stat3.nEq */
 1.79013 +    tRowcnt nLt;               /* sqlite_stat3.nLt */
 1.79014 +    tRowcnt nDLt;              /* sqlite_stat3.nDLt */
 1.79015 +    u8 isPSample;              /* True if a periodic sample */
 1.79016 +    u32 iHash;                 /* Tiebreaker hash */
 1.79017 +  } *a;                     /* An array of samples */
 1.79018 +};
 1.79019 +
 1.79020 +#ifdef SQLITE_ENABLE_STAT3
 1.79021 +/*
 1.79022 +** Implementation of the stat3_init(C,S) SQL function.  The two parameters
 1.79023 +** are the number of rows in the table or index (C) and the number of samples
 1.79024 +** to accumulate (S).
 1.79025 +**
 1.79026 +** This routine allocates the Stat3Accum object.
 1.79027 +**
 1.79028 +** The return value is the Stat3Accum object (P).
 1.79029 +*/
 1.79030 +static void stat3Init(
 1.79031 +  sqlite3_context *context,
 1.79032 +  int argc,
 1.79033 +  sqlite3_value **argv
 1.79034 +){
 1.79035 +  Stat3Accum *p;
 1.79036 +  tRowcnt nRow;
 1.79037 +  int mxSample;
 1.79038 +  int n;
 1.79039 +
 1.79040 +  UNUSED_PARAMETER(argc);
 1.79041 +  nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
 1.79042 +  mxSample = sqlite3_value_int(argv[1]);
 1.79043 +  n = sizeof(*p) + sizeof(p->a[0])*mxSample;
 1.79044 +  p = sqlite3MallocZero( n );
 1.79045 +  if( p==0 ){
 1.79046 +    sqlite3_result_error_nomem(context);
 1.79047 +    return;
 1.79048 +  }
 1.79049 +  p->a = (struct Stat3Sample*)&p[1];
 1.79050 +  p->nRow = nRow;
 1.79051 +  p->mxSample = mxSample;
 1.79052 +  p->nPSample = p->nRow/(mxSample/3+1) + 1;
 1.79053 +  sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
 1.79054 +  sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
 1.79055 +}
 1.79056 +static const FuncDef stat3InitFuncdef = {
 1.79057 +  2,                /* nArg */
 1.79058 +  SQLITE_UTF8,      /* iPrefEnc */
 1.79059 +  0,                /* flags */
 1.79060 +  0,                /* pUserData */
 1.79061 +  0,                /* pNext */
 1.79062 +  stat3Init,        /* xFunc */
 1.79063 +  0,                /* xStep */
 1.79064 +  0,                /* xFinalize */
 1.79065 +  "stat3_init",     /* zName */
 1.79066 +  0,                /* pHash */
 1.79067 +  0                 /* pDestructor */
 1.79068 +};
 1.79069 +
 1.79070 +
 1.79071 +/*
 1.79072 +** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function.  The
 1.79073 +** arguments describe a single key instance.  This routine makes the 
 1.79074 +** decision about whether or not to retain this key for the sqlite_stat3
 1.79075 +** table.
 1.79076 +**
 1.79077 +** The return value is NULL.
 1.79078 +*/
 1.79079 +static void stat3Push(
 1.79080 +  sqlite3_context *context,
 1.79081 +  int argc,
 1.79082 +  sqlite3_value **argv
 1.79083 +){
 1.79084 +  Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
 1.79085 +  tRowcnt nEq = sqlite3_value_int64(argv[0]);
 1.79086 +  tRowcnt nLt = sqlite3_value_int64(argv[1]);
 1.79087 +  tRowcnt nDLt = sqlite3_value_int64(argv[2]);
 1.79088 +  i64 rowid = sqlite3_value_int64(argv[3]);
 1.79089 +  u8 isPSample = 0;
 1.79090 +  u8 doInsert = 0;
 1.79091 +  int iMin = p->iMin;
 1.79092 +  struct Stat3Sample *pSample;
 1.79093 +  int i;
 1.79094 +  u32 h;
 1.79095 +
 1.79096 +  UNUSED_PARAMETER(context);
 1.79097 +  UNUSED_PARAMETER(argc);
 1.79098 +  if( nEq==0 ) return;
 1.79099 +  h = p->iPrn = p->iPrn*1103515245 + 12345;
 1.79100 +  if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
 1.79101 +    doInsert = isPSample = 1;
 1.79102 +  }else if( p->nSample<p->mxSample ){
 1.79103 +    doInsert = 1;
 1.79104 +  }else{
 1.79105 +    if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
 1.79106 +      doInsert = 1;
 1.79107 +    }
 1.79108 +  }
 1.79109 +  if( !doInsert ) return;
 1.79110 +  if( p->nSample==p->mxSample ){
 1.79111 +    assert( p->nSample - iMin - 1 >= 0 );
 1.79112 +    memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
 1.79113 +    pSample = &p->a[p->nSample-1];
 1.79114 +  }else{
 1.79115 +    pSample = &p->a[p->nSample++];
 1.79116 +  }
 1.79117 +  pSample->iRowid = rowid;
 1.79118 +  pSample->nEq = nEq;
 1.79119 +  pSample->nLt = nLt;
 1.79120 +  pSample->nDLt = nDLt;
 1.79121 +  pSample->iHash = h;
 1.79122 +  pSample->isPSample = isPSample;
 1.79123 +
 1.79124 +  /* Find the new minimum */
 1.79125 +  if( p->nSample==p->mxSample ){
 1.79126 +    pSample = p->a;
 1.79127 +    i = 0;
 1.79128 +    while( pSample->isPSample ){
 1.79129 +      i++;
 1.79130 +      pSample++;
 1.79131 +      assert( i<p->nSample );
 1.79132 +    }
 1.79133 +    nEq = pSample->nEq;
 1.79134 +    h = pSample->iHash;
 1.79135 +    iMin = i;
 1.79136 +    for(i++, pSample++; i<p->nSample; i++, pSample++){
 1.79137 +      if( pSample->isPSample ) continue;
 1.79138 +      if( pSample->nEq<nEq
 1.79139 +       || (pSample->nEq==nEq && pSample->iHash<h)
 1.79140 +      ){
 1.79141 +        iMin = i;
 1.79142 +        nEq = pSample->nEq;
 1.79143 +        h = pSample->iHash;
 1.79144 +      }
 1.79145 +    }
 1.79146 +    p->iMin = iMin;
 1.79147 +  }
 1.79148 +}
 1.79149 +static const FuncDef stat3PushFuncdef = {
 1.79150 +  5,                /* nArg */
 1.79151 +  SQLITE_UTF8,      /* iPrefEnc */
 1.79152 +  0,                /* flags */
 1.79153 +  0,                /* pUserData */
 1.79154 +  0,                /* pNext */
 1.79155 +  stat3Push,        /* xFunc */
 1.79156 +  0,                /* xStep */
 1.79157 +  0,                /* xFinalize */
 1.79158 +  "stat3_push",     /* zName */
 1.79159 +  0,                /* pHash */
 1.79160 +  0                 /* pDestructor */
 1.79161 +};
 1.79162 +
 1.79163 +/*
 1.79164 +** Implementation of the stat3_get(P,N,...) SQL function.  This routine is
 1.79165 +** used to query the results.  Content is returned for the Nth sqlite_stat3
 1.79166 +** row where N is between 0 and S-1 and S is the number of samples.  The
 1.79167 +** value returned depends on the number of arguments.
 1.79168 +**
 1.79169 +**   argc==2    result:  rowid
 1.79170 +**   argc==3    result:  nEq
 1.79171 +**   argc==4    result:  nLt
 1.79172 +**   argc==5    result:  nDLt
 1.79173 +*/
 1.79174 +static void stat3Get(
 1.79175 +  sqlite3_context *context,
 1.79176 +  int argc,
 1.79177 +  sqlite3_value **argv
 1.79178 +){
 1.79179 +  int n = sqlite3_value_int(argv[1]);
 1.79180 +  Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
 1.79181 +
 1.79182 +  assert( p!=0 );
 1.79183 +  if( p->nSample<=n ) return;
 1.79184 +  switch( argc ){
 1.79185 +    case 2:  sqlite3_result_int64(context, p->a[n].iRowid); break;
 1.79186 +    case 3:  sqlite3_result_int64(context, p->a[n].nEq);    break;
 1.79187 +    case 4:  sqlite3_result_int64(context, p->a[n].nLt);    break;
 1.79188 +    default: sqlite3_result_int64(context, p->a[n].nDLt);   break;
 1.79189 +  }
 1.79190 +}
 1.79191 +static const FuncDef stat3GetFuncdef = {
 1.79192 +  -1,               /* nArg */
 1.79193 +  SQLITE_UTF8,      /* iPrefEnc */
 1.79194 +  0,                /* flags */
 1.79195 +  0,                /* pUserData */
 1.79196 +  0,                /* pNext */
 1.79197 +  stat3Get,         /* xFunc */
 1.79198 +  0,                /* xStep */
 1.79199 +  0,                /* xFinalize */
 1.79200 +  "stat3_get",     /* zName */
 1.79201 +  0,                /* pHash */
 1.79202 +  0                 /* pDestructor */
 1.79203 +};
 1.79204 +#endif /* SQLITE_ENABLE_STAT3 */
 1.79205 +
 1.79206 +
 1.79207 +
 1.79208 +
 1.79209 +/*
 1.79210 +** Generate code to do an analysis of all indices associated with
 1.79211 +** a single table.
 1.79212 +*/
 1.79213 +static void analyzeOneTable(
 1.79214 +  Parse *pParse,   /* Parser context */
 1.79215 +  Table *pTab,     /* Table whose indices are to be analyzed */
 1.79216 +  Index *pOnlyIdx, /* If not NULL, only analyze this one index */
 1.79217 +  int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
 1.79218 +  int iMem         /* Available memory locations begin here */
 1.79219 +){
 1.79220 +  sqlite3 *db = pParse->db;    /* Database handle */
 1.79221 +  Index *pIdx;                 /* An index to being analyzed */
 1.79222 +  int iIdxCur;                 /* Cursor open on index being analyzed */
 1.79223 +  Vdbe *v;                     /* The virtual machine being built up */
 1.79224 +  int i;                       /* Loop counter */
 1.79225 +  int topOfLoop;               /* The top of the loop */
 1.79226 +  int endOfLoop;               /* The end of the loop */
 1.79227 +  int jZeroRows = -1;          /* Jump from here if number of rows is zero */
 1.79228 +  int iDb;                     /* Index of database containing pTab */
 1.79229 +  int regTabname = iMem++;     /* Register containing table name */
 1.79230 +  int regIdxname = iMem++;     /* Register containing index name */
 1.79231 +  int regStat1 = iMem++;       /* The stat column of sqlite_stat1 */
 1.79232 +#ifdef SQLITE_ENABLE_STAT3
 1.79233 +  int regNumEq = regStat1;     /* Number of instances.  Same as regStat1 */
 1.79234 +  int regNumLt = iMem++;       /* Number of keys less than regSample */
 1.79235 +  int regNumDLt = iMem++;      /* Number of distinct keys less than regSample */
 1.79236 +  int regSample = iMem++;      /* The next sample value */
 1.79237 +  int regRowid = regSample;    /* Rowid of a sample */
 1.79238 +  int regAccum = iMem++;       /* Register to hold Stat3Accum object */
 1.79239 +  int regLoop = iMem++;        /* Loop counter */
 1.79240 +  int regCount = iMem++;       /* Number of rows in the table or index */
 1.79241 +  int regTemp1 = iMem++;       /* Intermediate register */
 1.79242 +  int regTemp2 = iMem++;       /* Intermediate register */
 1.79243 +  int once = 1;                /* One-time initialization */
 1.79244 +  int shortJump = 0;           /* Instruction address */
 1.79245 +  int iTabCur = pParse->nTab++; /* Table cursor */
 1.79246 +#endif
 1.79247 +  int regCol = iMem++;         /* Content of a column in analyzed table */
 1.79248 +  int regRec = iMem++;         /* Register holding completed record */
 1.79249 +  int regTemp = iMem++;        /* Temporary use register */
 1.79250 +  int regNewRowid = iMem++;    /* Rowid for the inserted record */
 1.79251 +
 1.79252 +
 1.79253 +  v = sqlite3GetVdbe(pParse);
 1.79254 +  if( v==0 || NEVER(pTab==0) ){
 1.79255 +    return;
 1.79256 +  }
 1.79257 +  if( pTab->tnum==0 ){
 1.79258 +    /* Do not gather statistics on views or virtual tables */
 1.79259 +    return;
 1.79260 +  }
 1.79261 +  if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
 1.79262 +    /* Do not gather statistics on system tables */
 1.79263 +    return;
 1.79264 +  }
 1.79265 +  assert( sqlite3BtreeHoldsAllMutexes(db) );
 1.79266 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.79267 +  assert( iDb>=0 );
 1.79268 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.79269 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.79270 +  if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
 1.79271 +      db->aDb[iDb].zName ) ){
 1.79272 +    return;
 1.79273 +  }
 1.79274 +#endif
 1.79275 +
 1.79276 +  /* Establish a read-lock on the table at the shared-cache level. */
 1.79277 +  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 1.79278 +
 1.79279 +  iIdxCur = pParse->nTab++;
 1.79280 +  sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
 1.79281 +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.79282 +    int nCol;
 1.79283 +    KeyInfo *pKey;
 1.79284 +    int addrIfNot = 0;           /* address of OP_IfNot */
 1.79285 +    int *aChngAddr;              /* Array of jump instruction addresses */
 1.79286 +
 1.79287 +    if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
 1.79288 +    VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
 1.79289 +    nCol = pIdx->nColumn;
 1.79290 +    aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
 1.79291 +    if( aChngAddr==0 ) continue;
 1.79292 +    pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 1.79293 +    if( iMem+1+(nCol*2)>pParse->nMem ){
 1.79294 +      pParse->nMem = iMem+1+(nCol*2);
 1.79295 +    }
 1.79296 +
 1.79297 +    /* Open a cursor to the index to be analyzed. */
 1.79298 +    assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
 1.79299 +    sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
 1.79300 +        (char *)pKey, P4_KEYINFO_HANDOFF);
 1.79301 +    VdbeComment((v, "%s", pIdx->zName));
 1.79302 +
 1.79303 +    /* Populate the register containing the index name. */
 1.79304 +    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
 1.79305 +
 1.79306 +#ifdef SQLITE_ENABLE_STAT3
 1.79307 +    if( once ){
 1.79308 +      once = 0;
 1.79309 +      sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
 1.79310 +    }
 1.79311 +    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
 1.79312 +    sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
 1.79313 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
 1.79314 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
 1.79315 +    sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
 1.79316 +    sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
 1.79317 +    sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
 1.79318 +                      (char*)&stat3InitFuncdef, P4_FUNCDEF);
 1.79319 +    sqlite3VdbeChangeP5(v, 2);
 1.79320 +#endif /* SQLITE_ENABLE_STAT3 */
 1.79321 +
 1.79322 +    /* The block of memory cells initialized here is used as follows.
 1.79323 +    **
 1.79324 +    **    iMem:                
 1.79325 +    **        The total number of rows in the table.
 1.79326 +    **
 1.79327 +    **    iMem+1 .. iMem+nCol: 
 1.79328 +    **        Number of distinct entries in index considering the 
 1.79329 +    **        left-most N columns only, where N is between 1 and nCol, 
 1.79330 +    **        inclusive.
 1.79331 +    **
 1.79332 +    **    iMem+nCol+1 .. Mem+2*nCol:  
 1.79333 +    **        Previous value of indexed columns, from left to right.
 1.79334 +    **
 1.79335 +    ** Cells iMem through iMem+nCol are initialized to 0. The others are 
 1.79336 +    ** initialized to contain an SQL NULL.
 1.79337 +    */
 1.79338 +    for(i=0; i<=nCol; i++){
 1.79339 +      sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
 1.79340 +    }
 1.79341 +    for(i=0; i<nCol; i++){
 1.79342 +      sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
 1.79343 +    }
 1.79344 +
 1.79345 +    /* Start the analysis loop. This loop runs through all the entries in
 1.79346 +    ** the index b-tree.  */
 1.79347 +    endOfLoop = sqlite3VdbeMakeLabel(v);
 1.79348 +    sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
 1.79349 +    topOfLoop = sqlite3VdbeCurrentAddr(v);
 1.79350 +    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);  /* Increment row counter */
 1.79351 +
 1.79352 +    for(i=0; i<nCol; i++){
 1.79353 +      CollSeq *pColl;
 1.79354 +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
 1.79355 +      if( i==0 ){
 1.79356 +        /* Always record the very first row */
 1.79357 +        addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
 1.79358 +      }
 1.79359 +      assert( pIdx->azColl!=0 );
 1.79360 +      assert( pIdx->azColl[i]!=0 );
 1.79361 +      pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
 1.79362 +      aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
 1.79363 +                                      (char*)pColl, P4_COLLSEQ);
 1.79364 +      sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 1.79365 +      VdbeComment((v, "jump if column %d changed", i));
 1.79366 +#ifdef SQLITE_ENABLE_STAT3
 1.79367 +      if( i==0 ){
 1.79368 +        sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
 1.79369 +        VdbeComment((v, "incr repeat count"));
 1.79370 +      }
 1.79371 +#endif
 1.79372 +    }
 1.79373 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
 1.79374 +    for(i=0; i<nCol; i++){
 1.79375 +      sqlite3VdbeJumpHere(v, aChngAddr[i]);  /* Set jump dest for the OP_Ne */
 1.79376 +      if( i==0 ){
 1.79377 +        sqlite3VdbeJumpHere(v, addrIfNot);   /* Jump dest for OP_IfNot */
 1.79378 +#ifdef SQLITE_ENABLE_STAT3
 1.79379 +        sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
 1.79380 +                          (char*)&stat3PushFuncdef, P4_FUNCDEF);
 1.79381 +        sqlite3VdbeChangeP5(v, 5);
 1.79382 +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
 1.79383 +        sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
 1.79384 +        sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
 1.79385 +        sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
 1.79386 +#endif        
 1.79387 +      }
 1.79388 +      sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
 1.79389 +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
 1.79390 +    }
 1.79391 +    sqlite3DbFree(db, aChngAddr);
 1.79392 +
 1.79393 +    /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
 1.79394 +    sqlite3VdbeResolveLabel(v, endOfLoop);
 1.79395 +
 1.79396 +    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
 1.79397 +    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 1.79398 +#ifdef SQLITE_ENABLE_STAT3
 1.79399 +    sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
 1.79400 +                      (char*)&stat3PushFuncdef, P4_FUNCDEF);
 1.79401 +    sqlite3VdbeChangeP5(v, 5);
 1.79402 +    sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
 1.79403 +    shortJump = 
 1.79404 +    sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
 1.79405 +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
 1.79406 +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
 1.79407 +    sqlite3VdbeChangeP5(v, 2);
 1.79408 +    sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
 1.79409 +    sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
 1.79410 +    sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
 1.79411 +    sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
 1.79412 +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
 1.79413 +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
 1.79414 +    sqlite3VdbeChangeP5(v, 3);
 1.79415 +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
 1.79416 +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
 1.79417 +    sqlite3VdbeChangeP5(v, 4);
 1.79418 +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
 1.79419 +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
 1.79420 +    sqlite3VdbeChangeP5(v, 5);
 1.79421 +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
 1.79422 +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
 1.79423 +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
 1.79424 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
 1.79425 +    sqlite3VdbeJumpHere(v, shortJump+2);
 1.79426 +#endif        
 1.79427 +
 1.79428 +    /* Store the results in sqlite_stat1.
 1.79429 +    **
 1.79430 +    ** The result is a single row of the sqlite_stat1 table.  The first
 1.79431 +    ** two columns are the names of the table and index.  The third column
 1.79432 +    ** is a string composed of a list of integer statistics about the
 1.79433 +    ** index.  The first integer in the list is the total number of entries
 1.79434 +    ** in the index.  There is one additional integer in the list for each
 1.79435 +    ** column of the table.  This additional integer is a guess of how many
 1.79436 +    ** rows of the table the index will select.  If D is the count of distinct
 1.79437 +    ** values and K is the total number of rows, then the integer is computed
 1.79438 +    ** as:
 1.79439 +    **
 1.79440 +    **        I = (K+D-1)/D
 1.79441 +    **
 1.79442 +    ** If K==0 then no entry is made into the sqlite_stat1 table.  
 1.79443 +    ** If K>0 then it is always the case the D>0 so division by zero
 1.79444 +    ** is never possible.
 1.79445 +    */
 1.79446 +    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
 1.79447 +    if( jZeroRows<0 ){
 1.79448 +      jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
 1.79449 +    }
 1.79450 +    for(i=0; i<nCol; i++){
 1.79451 +      sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
 1.79452 +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
 1.79453 +      sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
 1.79454 +      sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
 1.79455 +      sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
 1.79456 +      sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
 1.79457 +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
 1.79458 +    }
 1.79459 +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
 1.79460 +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 1.79461 +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
 1.79462 +    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.79463 +  }
 1.79464 +
 1.79465 +  /* If the table has no indices, create a single sqlite_stat1 entry
 1.79466 +  ** containing NULL as the index name and the row count as the content.
 1.79467 +  */
 1.79468 +  if( pTab->pIndex==0 ){
 1.79469 +    sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
 1.79470 +    VdbeComment((v, "%s", pTab->zName));
 1.79471 +    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
 1.79472 +    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 1.79473 +    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
 1.79474 +  }else{
 1.79475 +    sqlite3VdbeJumpHere(v, jZeroRows);
 1.79476 +    jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
 1.79477 +  }
 1.79478 +  sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
 1.79479 +  sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
 1.79480 +  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 1.79481 +  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
 1.79482 +  sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.79483 +  if( pParse->nMem<regRec ) pParse->nMem = regRec;
 1.79484 +  sqlite3VdbeJumpHere(v, jZeroRows);
 1.79485 +}
 1.79486 +
 1.79487 +
 1.79488 +/*
 1.79489 +** Generate code that will cause the most recent index analysis to
 1.79490 +** be loaded into internal hash tables where is can be used.
 1.79491 +*/
 1.79492 +static void loadAnalysis(Parse *pParse, int iDb){
 1.79493 +  Vdbe *v = sqlite3GetVdbe(pParse);
 1.79494 +  if( v ){
 1.79495 +    sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
 1.79496 +  }
 1.79497 +}
 1.79498 +
 1.79499 +/*
 1.79500 +** Generate code that will do an analysis of an entire database
 1.79501 +*/
 1.79502 +static void analyzeDatabase(Parse *pParse, int iDb){
 1.79503 +  sqlite3 *db = pParse->db;
 1.79504 +  Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
 1.79505 +  HashElem *k;
 1.79506 +  int iStatCur;
 1.79507 +  int iMem;
 1.79508 +
 1.79509 +  sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.79510 +  iStatCur = pParse->nTab;
 1.79511 +  pParse->nTab += 3;
 1.79512 +  openStatTable(pParse, iDb, iStatCur, 0, 0);
 1.79513 +  iMem = pParse->nMem+1;
 1.79514 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.79515 +  for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
 1.79516 +    Table *pTab = (Table*)sqliteHashData(k);
 1.79517 +    analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
 1.79518 +  }
 1.79519 +  loadAnalysis(pParse, iDb);
 1.79520 +}
 1.79521 +
 1.79522 +/*
 1.79523 +** Generate code that will do an analysis of a single table in
 1.79524 +** a database.  If pOnlyIdx is not NULL then it is a single index
 1.79525 +** in pTab that should be analyzed.
 1.79526 +*/
 1.79527 +static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
 1.79528 +  int iDb;
 1.79529 +  int iStatCur;
 1.79530 +
 1.79531 +  assert( pTab!=0 );
 1.79532 +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 1.79533 +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.79534 +  sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.79535 +  iStatCur = pParse->nTab;
 1.79536 +  pParse->nTab += 3;
 1.79537 +  if( pOnlyIdx ){
 1.79538 +    openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
 1.79539 +  }else{
 1.79540 +    openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
 1.79541 +  }
 1.79542 +  analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
 1.79543 +  loadAnalysis(pParse, iDb);
 1.79544 +}
 1.79545 +
 1.79546 +/*
 1.79547 +** Generate code for the ANALYZE command.  The parser calls this routine
 1.79548 +** when it recognizes an ANALYZE command.
 1.79549 +**
 1.79550 +**        ANALYZE                            -- 1
 1.79551 +**        ANALYZE  <database>                -- 2
 1.79552 +**        ANALYZE  ?<database>.?<tablename>  -- 3
 1.79553 +**
 1.79554 +** Form 1 causes all indices in all attached databases to be analyzed.
 1.79555 +** Form 2 analyzes all indices the single database named.
 1.79556 +** Form 3 analyzes all indices associated with the named table.
 1.79557 +*/
 1.79558 +SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
 1.79559 +  sqlite3 *db = pParse->db;
 1.79560 +  int iDb;
 1.79561 +  int i;
 1.79562 +  char *z, *zDb;
 1.79563 +  Table *pTab;
 1.79564 +  Index *pIdx;
 1.79565 +  Token *pTableName;
 1.79566 +
 1.79567 +  /* Read the database schema. If an error occurs, leave an error message
 1.79568 +  ** and code in pParse and return NULL. */
 1.79569 +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 1.79570 +  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 1.79571 +    return;
 1.79572 +  }
 1.79573 +
 1.79574 +  assert( pName2!=0 || pName1==0 );
 1.79575 +  if( pName1==0 ){
 1.79576 +    /* Form 1:  Analyze everything */
 1.79577 +    for(i=0; i<db->nDb; i++){
 1.79578 +      if( i==1 ) continue;  /* Do not analyze the TEMP database */
 1.79579 +      analyzeDatabase(pParse, i);
 1.79580 +    }
 1.79581 +  }else if( pName2->n==0 ){
 1.79582 +    /* Form 2:  Analyze the database or table named */
 1.79583 +    iDb = sqlite3FindDb(db, pName1);
 1.79584 +    if( iDb>=0 ){
 1.79585 +      analyzeDatabase(pParse, iDb);
 1.79586 +    }else{
 1.79587 +      z = sqlite3NameFromToken(db, pName1);
 1.79588 +      if( z ){
 1.79589 +        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
 1.79590 +          analyzeTable(pParse, pIdx->pTable, pIdx);
 1.79591 +        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
 1.79592 +          analyzeTable(pParse, pTab, 0);
 1.79593 +        }
 1.79594 +        sqlite3DbFree(db, z);
 1.79595 +      }
 1.79596 +    }
 1.79597 +  }else{
 1.79598 +    /* Form 3: Analyze the fully qualified table name */
 1.79599 +    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
 1.79600 +    if( iDb>=0 ){
 1.79601 +      zDb = db->aDb[iDb].zName;
 1.79602 +      z = sqlite3NameFromToken(db, pTableName);
 1.79603 +      if( z ){
 1.79604 +        if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
 1.79605 +          analyzeTable(pParse, pIdx->pTable, pIdx);
 1.79606 +        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
 1.79607 +          analyzeTable(pParse, pTab, 0);
 1.79608 +        }
 1.79609 +        sqlite3DbFree(db, z);
 1.79610 +      }
 1.79611 +    }   
 1.79612 +  }
 1.79613 +}
 1.79614 +
 1.79615 +/*
 1.79616 +** Used to pass information from the analyzer reader through to the
 1.79617 +** callback routine.
 1.79618 +*/
 1.79619 +typedef struct analysisInfo analysisInfo;
 1.79620 +struct analysisInfo {
 1.79621 +  sqlite3 *db;
 1.79622 +  const char *zDatabase;
 1.79623 +};
 1.79624 +
 1.79625 +/*
 1.79626 +** This callback is invoked once for each index when reading the
 1.79627 +** sqlite_stat1 table.  
 1.79628 +**
 1.79629 +**     argv[0] = name of the table
 1.79630 +**     argv[1] = name of the index (might be NULL)
 1.79631 +**     argv[2] = results of analysis - on integer for each column
 1.79632 +**
 1.79633 +** Entries for which argv[1]==NULL simply record the number of rows in
 1.79634 +** the table.
 1.79635 +*/
 1.79636 +static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
 1.79637 +  analysisInfo *pInfo = (analysisInfo*)pData;
 1.79638 +  Index *pIndex;
 1.79639 +  Table *pTable;
 1.79640 +  int i, c, n;
 1.79641 +  tRowcnt v;
 1.79642 +  const char *z;
 1.79643 +
 1.79644 +  assert( argc==3 );
 1.79645 +  UNUSED_PARAMETER2(NotUsed, argc);
 1.79646 +
 1.79647 +  if( argv==0 || argv[0]==0 || argv[2]==0 ){
 1.79648 +    return 0;
 1.79649 +  }
 1.79650 +  pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
 1.79651 +  if( pTable==0 ){
 1.79652 +    return 0;
 1.79653 +  }
 1.79654 +  if( argv[1] ){
 1.79655 +    pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
 1.79656 +  }else{
 1.79657 +    pIndex = 0;
 1.79658 +  }
 1.79659 +  n = pIndex ? pIndex->nColumn : 0;
 1.79660 +  z = argv[2];
 1.79661 +  for(i=0; *z && i<=n; i++){
 1.79662 +    v = 0;
 1.79663 +    while( (c=z[0])>='0' && c<='9' ){
 1.79664 +      v = v*10 + c - '0';
 1.79665 +      z++;
 1.79666 +    }
 1.79667 +    if( i==0 ) pTable->nRowEst = v;
 1.79668 +    if( pIndex==0 ) break;
 1.79669 +    pIndex->aiRowEst[i] = v;
 1.79670 +    if( *z==' ' ) z++;
 1.79671 +    if( memcmp(z, "unordered", 10)==0 ){
 1.79672 +      pIndex->bUnordered = 1;
 1.79673 +      break;
 1.79674 +    }
 1.79675 +  }
 1.79676 +  return 0;
 1.79677 +}
 1.79678 +
 1.79679 +/*
 1.79680 +** If the Index.aSample variable is not NULL, delete the aSample[] array
 1.79681 +** and its contents.
 1.79682 +*/
 1.79683 +SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
 1.79684 +#ifdef SQLITE_ENABLE_STAT3
 1.79685 +  if( pIdx->aSample ){
 1.79686 +    int j;
 1.79687 +    for(j=0; j<pIdx->nSample; j++){
 1.79688 +      IndexSample *p = &pIdx->aSample[j];
 1.79689 +      if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
 1.79690 +        sqlite3DbFree(db, p->u.z);
 1.79691 +      }
 1.79692 +    }
 1.79693 +    sqlite3DbFree(db, pIdx->aSample);
 1.79694 +  }
 1.79695 +  if( db && db->pnBytesFreed==0 ){
 1.79696 +    pIdx->nSample = 0;
 1.79697 +    pIdx->aSample = 0;
 1.79698 +  }
 1.79699 +#else
 1.79700 +  UNUSED_PARAMETER(db);
 1.79701 +  UNUSED_PARAMETER(pIdx);
 1.79702 +#endif
 1.79703 +}
 1.79704 +
 1.79705 +#ifdef SQLITE_ENABLE_STAT3
 1.79706 +/*
 1.79707 +** Load content from the sqlite_stat3 table into the Index.aSample[]
 1.79708 +** arrays of all indices.
 1.79709 +*/
 1.79710 +static int loadStat3(sqlite3 *db, const char *zDb){
 1.79711 +  int rc;                       /* Result codes from subroutines */
 1.79712 +  sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
 1.79713 +  char *zSql;                   /* Text of the SQL statement */
 1.79714 +  Index *pPrevIdx = 0;          /* Previous index in the loop */
 1.79715 +  int idx = 0;                  /* slot in pIdx->aSample[] for next sample */
 1.79716 +  int eType;                    /* Datatype of a sample */
 1.79717 +  IndexSample *pSample;         /* A slot in pIdx->aSample[] */
 1.79718 +
 1.79719 +  assert( db->lookaside.bEnabled==0 );
 1.79720 +  if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
 1.79721 +    return SQLITE_OK;
 1.79722 +  }
 1.79723 +
 1.79724 +  zSql = sqlite3MPrintf(db, 
 1.79725 +      "SELECT idx,count(*) FROM %Q.sqlite_stat3"
 1.79726 +      " GROUP BY idx", zDb);
 1.79727 +  if( !zSql ){
 1.79728 +    return SQLITE_NOMEM;
 1.79729 +  }
 1.79730 +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 1.79731 +  sqlite3DbFree(db, zSql);
 1.79732 +  if( rc ) return rc;
 1.79733 +
 1.79734 +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
 1.79735 +    char *zIndex;   /* Index name */
 1.79736 +    Index *pIdx;    /* Pointer to the index object */
 1.79737 +    int nSample;    /* Number of samples */
 1.79738 +
 1.79739 +    zIndex = (char *)sqlite3_column_text(pStmt, 0);
 1.79740 +    if( zIndex==0 ) continue;
 1.79741 +    nSample = sqlite3_column_int(pStmt, 1);
 1.79742 +    pIdx = sqlite3FindIndex(db, zIndex, zDb);
 1.79743 +    if( pIdx==0 ) continue;
 1.79744 +    assert( pIdx->nSample==0 );
 1.79745 +    pIdx->nSample = nSample;
 1.79746 +    pIdx->aSample = sqlite3DbMallocZero(db, nSample*sizeof(IndexSample));
 1.79747 +    pIdx->avgEq = pIdx->aiRowEst[1];
 1.79748 +    if( pIdx->aSample==0 ){
 1.79749 +      db->mallocFailed = 1;
 1.79750 +      sqlite3_finalize(pStmt);
 1.79751 +      return SQLITE_NOMEM;
 1.79752 +    }
 1.79753 +  }
 1.79754 +  rc = sqlite3_finalize(pStmt);
 1.79755 +  if( rc ) return rc;
 1.79756 +
 1.79757 +  zSql = sqlite3MPrintf(db, 
 1.79758 +      "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
 1.79759 +  if( !zSql ){
 1.79760 +    return SQLITE_NOMEM;
 1.79761 +  }
 1.79762 +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 1.79763 +  sqlite3DbFree(db, zSql);
 1.79764 +  if( rc ) return rc;
 1.79765 +
 1.79766 +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
 1.79767 +    char *zIndex;   /* Index name */
 1.79768 +    Index *pIdx;    /* Pointer to the index object */
 1.79769 +    int i;          /* Loop counter */
 1.79770 +    tRowcnt sumEq;  /* Sum of the nEq values */
 1.79771 +
 1.79772 +    zIndex = (char *)sqlite3_column_text(pStmt, 0);
 1.79773 +    if( zIndex==0 ) continue;
 1.79774 +    pIdx = sqlite3FindIndex(db, zIndex, zDb);
 1.79775 +    if( pIdx==0 ) continue;
 1.79776 +    if( pIdx==pPrevIdx ){
 1.79777 +      idx++;
 1.79778 +    }else{
 1.79779 +      pPrevIdx = pIdx;
 1.79780 +      idx = 0;
 1.79781 +    }
 1.79782 +    assert( idx<pIdx->nSample );
 1.79783 +    pSample = &pIdx->aSample[idx];
 1.79784 +    pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
 1.79785 +    pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
 1.79786 +    pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
 1.79787 +    if( idx==pIdx->nSample-1 ){
 1.79788 +      if( pSample->nDLt>0 ){
 1.79789 +        for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
 1.79790 +        pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
 1.79791 +      }
 1.79792 +      if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
 1.79793 +    }
 1.79794 +    eType = sqlite3_column_type(pStmt, 4);
 1.79795 +    pSample->eType = (u8)eType;
 1.79796 +    switch( eType ){
 1.79797 +      case SQLITE_INTEGER: {
 1.79798 +        pSample->u.i = sqlite3_column_int64(pStmt, 4);
 1.79799 +        break;
 1.79800 +      }
 1.79801 +      case SQLITE_FLOAT: {
 1.79802 +        pSample->u.r = sqlite3_column_double(pStmt, 4);
 1.79803 +        break;
 1.79804 +      }
 1.79805 +      case SQLITE_NULL: {
 1.79806 +        break;
 1.79807 +      }
 1.79808 +      default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
 1.79809 +        const char *z = (const char *)(
 1.79810 +              (eType==SQLITE_BLOB) ?
 1.79811 +              sqlite3_column_blob(pStmt, 4):
 1.79812 +              sqlite3_column_text(pStmt, 4)
 1.79813 +           );
 1.79814 +        int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
 1.79815 +        pSample->nByte = n;
 1.79816 +        if( n < 1){
 1.79817 +          pSample->u.z = 0;
 1.79818 +        }else{
 1.79819 +          pSample->u.z = sqlite3DbMallocRaw(db, n);
 1.79820 +          if( pSample->u.z==0 ){
 1.79821 +            db->mallocFailed = 1;
 1.79822 +            sqlite3_finalize(pStmt);
 1.79823 +            return SQLITE_NOMEM;
 1.79824 +          }
 1.79825 +          memcpy(pSample->u.z, z, n);
 1.79826 +        }
 1.79827 +      }
 1.79828 +    }
 1.79829 +  }
 1.79830 +  return sqlite3_finalize(pStmt);
 1.79831 +}
 1.79832 +#endif /* SQLITE_ENABLE_STAT3 */
 1.79833 +
 1.79834 +/*
 1.79835 +** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
 1.79836 +** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
 1.79837 +** arrays. The contents of sqlite_stat3 are used to populate the
 1.79838 +** Index.aSample[] arrays.
 1.79839 +**
 1.79840 +** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
 1.79841 +** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined 
 1.79842 +** during compilation and the sqlite_stat3 table is present, no data is 
 1.79843 +** read from it.
 1.79844 +**
 1.79845 +** If SQLITE_ENABLE_STAT3 was defined during compilation and the 
 1.79846 +** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
 1.79847 +** returned. However, in this case, data is read from the sqlite_stat1
 1.79848 +** table (if it is present) before returning.
 1.79849 +**
 1.79850 +** If an OOM error occurs, this function always sets db->mallocFailed.
 1.79851 +** This means if the caller does not care about other errors, the return
 1.79852 +** code may be ignored.
 1.79853 +*/
 1.79854 +SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
 1.79855 +  analysisInfo sInfo;
 1.79856 +  HashElem *i;
 1.79857 +  char *zSql;
 1.79858 +  int rc;
 1.79859 +
 1.79860 +  assert( iDb>=0 && iDb<db->nDb );
 1.79861 +  assert( db->aDb[iDb].pBt!=0 );
 1.79862 +
 1.79863 +  /* Clear any prior statistics */
 1.79864 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.79865 +  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
 1.79866 +    Index *pIdx = sqliteHashData(i);
 1.79867 +    sqlite3DefaultRowEst(pIdx);
 1.79868 +#ifdef SQLITE_ENABLE_STAT3
 1.79869 +    sqlite3DeleteIndexSamples(db, pIdx);
 1.79870 +    pIdx->aSample = 0;
 1.79871 +#endif
 1.79872 +  }
 1.79873 +
 1.79874 +  /* Check to make sure the sqlite_stat1 table exists */
 1.79875 +  sInfo.db = db;
 1.79876 +  sInfo.zDatabase = db->aDb[iDb].zName;
 1.79877 +  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
 1.79878 +    return SQLITE_ERROR;
 1.79879 +  }
 1.79880 +
 1.79881 +  /* Load new statistics out of the sqlite_stat1 table */
 1.79882 +  zSql = sqlite3MPrintf(db, 
 1.79883 +      "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
 1.79884 +  if( zSql==0 ){
 1.79885 +    rc = SQLITE_NOMEM;
 1.79886 +  }else{
 1.79887 +    rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
 1.79888 +    sqlite3DbFree(db, zSql);
 1.79889 +  }
 1.79890 +
 1.79891 +
 1.79892 +  /* Load the statistics from the sqlite_stat3 table. */
 1.79893 +#ifdef SQLITE_ENABLE_STAT3
 1.79894 +  if( rc==SQLITE_OK ){
 1.79895 +    int lookasideEnabled = db->lookaside.bEnabled;
 1.79896 +    db->lookaside.bEnabled = 0;
 1.79897 +    rc = loadStat3(db, sInfo.zDatabase);
 1.79898 +    db->lookaside.bEnabled = lookasideEnabled;
 1.79899 +  }
 1.79900 +#endif
 1.79901 +
 1.79902 +  if( rc==SQLITE_NOMEM ){
 1.79903 +    db->mallocFailed = 1;
 1.79904 +  }
 1.79905 +  return rc;
 1.79906 +}
 1.79907 +
 1.79908 +
 1.79909 +#endif /* SQLITE_OMIT_ANALYZE */
 1.79910 +
 1.79911 +/************** End of analyze.c *********************************************/
 1.79912 +/************** Begin file attach.c ******************************************/
 1.79913 +/*
 1.79914 +** 2003 April 6
 1.79915 +**
 1.79916 +** The author disclaims copyright to this source code.  In place of
 1.79917 +** a legal notice, here is a blessing:
 1.79918 +**
 1.79919 +**    May you do good and not evil.
 1.79920 +**    May you find forgiveness for yourself and forgive others.
 1.79921 +**    May you share freely, never taking more than you give.
 1.79922 +**
 1.79923 +*************************************************************************
 1.79924 +** This file contains code used to implement the ATTACH and DETACH commands.
 1.79925 +*/
 1.79926 +
 1.79927 +#ifndef SQLITE_OMIT_ATTACH
 1.79928 +/*
 1.79929 +** Resolve an expression that was part of an ATTACH or DETACH statement. This
 1.79930 +** is slightly different from resolving a normal SQL expression, because simple
 1.79931 +** identifiers are treated as strings, not possible column names or aliases.
 1.79932 +**
 1.79933 +** i.e. if the parser sees:
 1.79934 +**
 1.79935 +**     ATTACH DATABASE abc AS def
 1.79936 +**
 1.79937 +** it treats the two expressions as literal strings 'abc' and 'def' instead of
 1.79938 +** looking for columns of the same name.
 1.79939 +**
 1.79940 +** This only applies to the root node of pExpr, so the statement:
 1.79941 +**
 1.79942 +**     ATTACH DATABASE abc||def AS 'db2'
 1.79943 +**
 1.79944 +** will fail because neither abc or def can be resolved.
 1.79945 +*/
 1.79946 +static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
 1.79947 +{
 1.79948 +  int rc = SQLITE_OK;
 1.79949 +  if( pExpr ){
 1.79950 +    if( pExpr->op!=TK_ID ){
 1.79951 +      rc = sqlite3ResolveExprNames(pName, pExpr);
 1.79952 +      if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
 1.79953 +        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
 1.79954 +        return SQLITE_ERROR;
 1.79955 +      }
 1.79956 +    }else{
 1.79957 +      pExpr->op = TK_STRING;
 1.79958 +    }
 1.79959 +  }
 1.79960 +  return rc;
 1.79961 +}
 1.79962 +
 1.79963 +/*
 1.79964 +** An SQL user-function registered to do the work of an ATTACH statement. The
 1.79965 +** three arguments to the function come directly from an attach statement:
 1.79966 +**
 1.79967 +**     ATTACH DATABASE x AS y KEY z
 1.79968 +**
 1.79969 +**     SELECT sqlite_attach(x, y, z)
 1.79970 +**
 1.79971 +** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
 1.79972 +** third argument.
 1.79973 +*/
 1.79974 +static void attachFunc(
 1.79975 +  sqlite3_context *context,
 1.79976 +  int NotUsed,
 1.79977 +  sqlite3_value **argv
 1.79978 +){
 1.79979 +  int i;
 1.79980 +  int rc = 0;
 1.79981 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.79982 +  const char *zName;
 1.79983 +  const char *zFile;
 1.79984 +  char *zPath = 0;
 1.79985 +  char *zErr = 0;
 1.79986 +  unsigned int flags;
 1.79987 +  Db *aNew;
 1.79988 +  char *zErrDyn = 0;
 1.79989 +  sqlite3_vfs *pVfs;
 1.79990 +
 1.79991 +  UNUSED_PARAMETER(NotUsed);
 1.79992 +
 1.79993 +  zFile = (const char *)sqlite3_value_text(argv[0]);
 1.79994 +  zName = (const char *)sqlite3_value_text(argv[1]);
 1.79995 +  if( zFile==0 ) zFile = "";
 1.79996 +  if( zName==0 ) zName = "";
 1.79997 +
 1.79998 +  /* Check for the following errors:
 1.79999 +  **
 1.80000 +  **     * Too many attached databases,
 1.80001 +  **     * Transaction currently open
 1.80002 +  **     * Specified database name already being used.
 1.80003 +  */
 1.80004 +  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
 1.80005 +    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
 1.80006 +      db->aLimit[SQLITE_LIMIT_ATTACHED]
 1.80007 +    );
 1.80008 +    goto attach_error;
 1.80009 +  }
 1.80010 +  if( !db->autoCommit ){
 1.80011 +    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
 1.80012 +    goto attach_error;
 1.80013 +  }
 1.80014 +  for(i=0; i<db->nDb; i++){
 1.80015 +    char *z = db->aDb[i].zName;
 1.80016 +    assert( z && zName );
 1.80017 +    if( sqlite3StrICmp(z, zName)==0 ){
 1.80018 +      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
 1.80019 +      goto attach_error;
 1.80020 +    }
 1.80021 +  }
 1.80022 +
 1.80023 +  /* Allocate the new entry in the db->aDb[] array and initialise the schema
 1.80024 +  ** hash tables.
 1.80025 +  */
 1.80026 +  if( db->aDb==db->aDbStatic ){
 1.80027 +    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
 1.80028 +    if( aNew==0 ) return;
 1.80029 +    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
 1.80030 +  }else{
 1.80031 +    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
 1.80032 +    if( aNew==0 ) return;
 1.80033 +  }
 1.80034 +  db->aDb = aNew;
 1.80035 +  aNew = &db->aDb[db->nDb];
 1.80036 +  memset(aNew, 0, sizeof(*aNew));
 1.80037 +
 1.80038 +  /* Open the database file. If the btree is successfully opened, use
 1.80039 +  ** it to obtain the database schema. At this point the schema may
 1.80040 +  ** or may not be initialised.
 1.80041 +  */
 1.80042 +  flags = db->openFlags;
 1.80043 +  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
 1.80044 +  if( rc!=SQLITE_OK ){
 1.80045 +    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
 1.80046 +    sqlite3_result_error(context, zErr, -1);
 1.80047 +    sqlite3_free(zErr);
 1.80048 +    return;
 1.80049 +  }
 1.80050 +  assert( pVfs );
 1.80051 +  flags |= SQLITE_OPEN_MAIN_DB;
 1.80052 +  rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
 1.80053 +  sqlite3_free( zPath );
 1.80054 +  db->nDb++;
 1.80055 +  if( rc==SQLITE_CONSTRAINT ){
 1.80056 +    rc = SQLITE_ERROR;
 1.80057 +    zErrDyn = sqlite3MPrintf(db, "database is already attached");
 1.80058 +  }else if( rc==SQLITE_OK ){
 1.80059 +    Pager *pPager;
 1.80060 +    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
 1.80061 +    if( !aNew->pSchema ){
 1.80062 +      rc = SQLITE_NOMEM;
 1.80063 +    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
 1.80064 +      zErrDyn = sqlite3MPrintf(db, 
 1.80065 +        "attached databases must use the same text encoding as main database");
 1.80066 +      rc = SQLITE_ERROR;
 1.80067 +    }
 1.80068 +    pPager = sqlite3BtreePager(aNew->pBt);
 1.80069 +    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
 1.80070 +    sqlite3BtreeSecureDelete(aNew->pBt,
 1.80071 +                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
 1.80072 +  }
 1.80073 +  aNew->safety_level = 3;
 1.80074 +  aNew->zName = sqlite3DbStrDup(db, zName);
 1.80075 +  if( rc==SQLITE_OK && aNew->zName==0 ){
 1.80076 +    rc = SQLITE_NOMEM;
 1.80077 +  }
 1.80078 +
 1.80079 +
 1.80080 +#ifdef SQLITE_HAS_CODEC
 1.80081 +  if( rc==SQLITE_OK ){
 1.80082 +    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
 1.80083 +    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
 1.80084 +    int nKey;
 1.80085 +    char *zKey;
 1.80086 +    int t = sqlite3_value_type(argv[2]);
 1.80087 +    switch( t ){
 1.80088 +      case SQLITE_INTEGER:
 1.80089 +      case SQLITE_FLOAT:
 1.80090 +        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
 1.80091 +        rc = SQLITE_ERROR;
 1.80092 +        break;
 1.80093 +        
 1.80094 +      case SQLITE_TEXT:
 1.80095 +      case SQLITE_BLOB:
 1.80096 +        nKey = sqlite3_value_bytes(argv[2]);
 1.80097 +        zKey = (char *)sqlite3_value_blob(argv[2]);
 1.80098 +        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 1.80099 +        break;
 1.80100 +
 1.80101 +      case SQLITE_NULL:
 1.80102 +        /* No key specified.  Use the key from the main database */
 1.80103 +        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 1.80104 +        if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
 1.80105 +          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 1.80106 +        }
 1.80107 +        break;
 1.80108 +    }
 1.80109 +  }
 1.80110 +#endif
 1.80111 +
 1.80112 +  /* If the file was opened successfully, read the schema for the new database.
 1.80113 +  ** If this fails, or if opening the file failed, then close the file and 
 1.80114 +  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
 1.80115 +  ** we found it.
 1.80116 +  */
 1.80117 +  if( rc==SQLITE_OK ){
 1.80118 +    sqlite3BtreeEnterAll(db);
 1.80119 +    rc = sqlite3Init(db, &zErrDyn);
 1.80120 +    sqlite3BtreeLeaveAll(db);
 1.80121 +  }
 1.80122 +  if( rc ){
 1.80123 +    int iDb = db->nDb - 1;
 1.80124 +    assert( iDb>=2 );
 1.80125 +    if( db->aDb[iDb].pBt ){
 1.80126 +      sqlite3BtreeClose(db->aDb[iDb].pBt);
 1.80127 +      db->aDb[iDb].pBt = 0;
 1.80128 +      db->aDb[iDb].pSchema = 0;
 1.80129 +    }
 1.80130 +    sqlite3ResetAllSchemasOfConnection(db);
 1.80131 +    db->nDb = iDb;
 1.80132 +    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 1.80133 +      db->mallocFailed = 1;
 1.80134 +      sqlite3DbFree(db, zErrDyn);
 1.80135 +      zErrDyn = sqlite3MPrintf(db, "out of memory");
 1.80136 +    }else if( zErrDyn==0 ){
 1.80137 +      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
 1.80138 +    }
 1.80139 +    goto attach_error;
 1.80140 +  }
 1.80141 +  
 1.80142 +  return;
 1.80143 +
 1.80144 +attach_error:
 1.80145 +  /* Return an error if we get here */
 1.80146 +  if( zErrDyn ){
 1.80147 +    sqlite3_result_error(context, zErrDyn, -1);
 1.80148 +    sqlite3DbFree(db, zErrDyn);
 1.80149 +  }
 1.80150 +  if( rc ) sqlite3_result_error_code(context, rc);
 1.80151 +}
 1.80152 +
 1.80153 +/*
 1.80154 +** An SQL user-function registered to do the work of an DETACH statement. The
 1.80155 +** three arguments to the function come directly from a detach statement:
 1.80156 +**
 1.80157 +**     DETACH DATABASE x
 1.80158 +**
 1.80159 +**     SELECT sqlite_detach(x)
 1.80160 +*/
 1.80161 +static void detachFunc(
 1.80162 +  sqlite3_context *context,
 1.80163 +  int NotUsed,
 1.80164 +  sqlite3_value **argv
 1.80165 +){
 1.80166 +  const char *zName = (const char *)sqlite3_value_text(argv[0]);
 1.80167 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.80168 +  int i;
 1.80169 +  Db *pDb = 0;
 1.80170 +  char zErr[128];
 1.80171 +
 1.80172 +  UNUSED_PARAMETER(NotUsed);
 1.80173 +
 1.80174 +  if( zName==0 ) zName = "";
 1.80175 +  for(i=0; i<db->nDb; i++){
 1.80176 +    pDb = &db->aDb[i];
 1.80177 +    if( pDb->pBt==0 ) continue;
 1.80178 +    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
 1.80179 +  }
 1.80180 +
 1.80181 +  if( i>=db->nDb ){
 1.80182 +    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
 1.80183 +    goto detach_error;
 1.80184 +  }
 1.80185 +  if( i<2 ){
 1.80186 +    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
 1.80187 +    goto detach_error;
 1.80188 +  }
 1.80189 +  if( !db->autoCommit ){
 1.80190 +    sqlite3_snprintf(sizeof(zErr), zErr,
 1.80191 +                     "cannot DETACH database within transaction");
 1.80192 +    goto detach_error;
 1.80193 +  }
 1.80194 +  if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
 1.80195 +    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
 1.80196 +    goto detach_error;
 1.80197 +  }
 1.80198 +
 1.80199 +  sqlite3BtreeClose(pDb->pBt);
 1.80200 +  pDb->pBt = 0;
 1.80201 +  pDb->pSchema = 0;
 1.80202 +  sqlite3ResetAllSchemasOfConnection(db);
 1.80203 +  return;
 1.80204 +
 1.80205 +detach_error:
 1.80206 +  sqlite3_result_error(context, zErr, -1);
 1.80207 +}
 1.80208 +
 1.80209 +/*
 1.80210 +** This procedure generates VDBE code for a single invocation of either the
 1.80211 +** sqlite_detach() or sqlite_attach() SQL user functions.
 1.80212 +*/
 1.80213 +static void codeAttach(
 1.80214 +  Parse *pParse,       /* The parser context */
 1.80215 +  int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
 1.80216 +  FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
 1.80217 +  Expr *pAuthArg,      /* Expression to pass to authorization callback */
 1.80218 +  Expr *pFilename,     /* Name of database file */
 1.80219 +  Expr *pDbname,       /* Name of the database to use internally */
 1.80220 +  Expr *pKey           /* Database key for encryption extension */
 1.80221 +){
 1.80222 +  int rc;
 1.80223 +  NameContext sName;
 1.80224 +  Vdbe *v;
 1.80225 +  sqlite3* db = pParse->db;
 1.80226 +  int regArgs;
 1.80227 +
 1.80228 +  memset(&sName, 0, sizeof(NameContext));
 1.80229 +  sName.pParse = pParse;
 1.80230 +
 1.80231 +  if( 
 1.80232 +      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
 1.80233 +      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
 1.80234 +      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
 1.80235 +  ){
 1.80236 +    pParse->nErr++;
 1.80237 +    goto attach_end;
 1.80238 +  }
 1.80239 +
 1.80240 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.80241 +  if( pAuthArg ){
 1.80242 +    char *zAuthArg;
 1.80243 +    if( pAuthArg->op==TK_STRING ){
 1.80244 +      zAuthArg = pAuthArg->u.zToken;
 1.80245 +    }else{
 1.80246 +      zAuthArg = 0;
 1.80247 +    }
 1.80248 +    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
 1.80249 +    if(rc!=SQLITE_OK ){
 1.80250 +      goto attach_end;
 1.80251 +    }
 1.80252 +  }
 1.80253 +#endif /* SQLITE_OMIT_AUTHORIZATION */
 1.80254 +
 1.80255 +
 1.80256 +  v = sqlite3GetVdbe(pParse);
 1.80257 +  regArgs = sqlite3GetTempRange(pParse, 4);
 1.80258 +  sqlite3ExprCode(pParse, pFilename, regArgs);
 1.80259 +  sqlite3ExprCode(pParse, pDbname, regArgs+1);
 1.80260 +  sqlite3ExprCode(pParse, pKey, regArgs+2);
 1.80261 +
 1.80262 +  assert( v || db->mallocFailed );
 1.80263 +  if( v ){
 1.80264 +    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
 1.80265 +    assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
 1.80266 +    sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
 1.80267 +    sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
 1.80268 +
 1.80269 +    /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
 1.80270 +    ** statement only). For DETACH, set it to false (expire all existing
 1.80271 +    ** statements).
 1.80272 +    */
 1.80273 +    sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
 1.80274 +  }
 1.80275 +  
 1.80276 +attach_end:
 1.80277 +  sqlite3ExprDelete(db, pFilename);
 1.80278 +  sqlite3ExprDelete(db, pDbname);
 1.80279 +  sqlite3ExprDelete(db, pKey);
 1.80280 +}
 1.80281 +
 1.80282 +/*
 1.80283 +** Called by the parser to compile a DETACH statement.
 1.80284 +**
 1.80285 +**     DETACH pDbname
 1.80286 +*/
 1.80287 +SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
 1.80288 +  static const FuncDef detach_func = {
 1.80289 +    1,                /* nArg */
 1.80290 +    SQLITE_UTF8,      /* iPrefEnc */
 1.80291 +    0,                /* flags */
 1.80292 +    0,                /* pUserData */
 1.80293 +    0,                /* pNext */
 1.80294 +    detachFunc,       /* xFunc */
 1.80295 +    0,                /* xStep */
 1.80296 +    0,                /* xFinalize */
 1.80297 +    "sqlite_detach",  /* zName */
 1.80298 +    0,                /* pHash */
 1.80299 +    0                 /* pDestructor */
 1.80300 +  };
 1.80301 +  codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
 1.80302 +}
 1.80303 +
 1.80304 +/*
 1.80305 +** Called by the parser to compile an ATTACH statement.
 1.80306 +**
 1.80307 +**     ATTACH p AS pDbname KEY pKey
 1.80308 +*/
 1.80309 +SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
 1.80310 +  static const FuncDef attach_func = {
 1.80311 +    3,                /* nArg */
 1.80312 +    SQLITE_UTF8,      /* iPrefEnc */
 1.80313 +    0,                /* flags */
 1.80314 +    0,                /* pUserData */
 1.80315 +    0,                /* pNext */
 1.80316 +    attachFunc,       /* xFunc */
 1.80317 +    0,                /* xStep */
 1.80318 +    0,                /* xFinalize */
 1.80319 +    "sqlite_attach",  /* zName */
 1.80320 +    0,                /* pHash */
 1.80321 +    0                 /* pDestructor */
 1.80322 +  };
 1.80323 +  codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
 1.80324 +}
 1.80325 +#endif /* SQLITE_OMIT_ATTACH */
 1.80326 +
 1.80327 +/*
 1.80328 +** Initialize a DbFixer structure.  This routine must be called prior
 1.80329 +** to passing the structure to one of the sqliteFixAAAA() routines below.
 1.80330 +**
 1.80331 +** The return value indicates whether or not fixation is required.  TRUE
 1.80332 +** means we do need to fix the database references, FALSE means we do not.
 1.80333 +*/
 1.80334 +SQLITE_PRIVATE int sqlite3FixInit(
 1.80335 +  DbFixer *pFix,      /* The fixer to be initialized */
 1.80336 +  Parse *pParse,      /* Error messages will be written here */
 1.80337 +  int iDb,            /* This is the database that must be used */
 1.80338 +  const char *zType,  /* "view", "trigger", or "index" */
 1.80339 +  const Token *pName  /* Name of the view, trigger, or index */
 1.80340 +){
 1.80341 +  sqlite3 *db;
 1.80342 +
 1.80343 +  if( NEVER(iDb<0) || iDb==1 ) return 0;
 1.80344 +  db = pParse->db;
 1.80345 +  assert( db->nDb>iDb );
 1.80346 +  pFix->pParse = pParse;
 1.80347 +  pFix->zDb = db->aDb[iDb].zName;
 1.80348 +  pFix->pSchema = db->aDb[iDb].pSchema;
 1.80349 +  pFix->zType = zType;
 1.80350 +  pFix->pName = pName;
 1.80351 +  return 1;
 1.80352 +}
 1.80353 +
 1.80354 +/*
 1.80355 +** The following set of routines walk through the parse tree and assign
 1.80356 +** a specific database to all table references where the database name
 1.80357 +** was left unspecified in the original SQL statement.  The pFix structure
 1.80358 +** must have been initialized by a prior call to sqlite3FixInit().
 1.80359 +**
 1.80360 +** These routines are used to make sure that an index, trigger, or
 1.80361 +** view in one database does not refer to objects in a different database.
 1.80362 +** (Exception: indices, triggers, and views in the TEMP database are
 1.80363 +** allowed to refer to anything.)  If a reference is explicitly made
 1.80364 +** to an object in a different database, an error message is added to
 1.80365 +** pParse->zErrMsg and these routines return non-zero.  If everything
 1.80366 +** checks out, these routines return 0.
 1.80367 +*/
 1.80368 +SQLITE_PRIVATE int sqlite3FixSrcList(
 1.80369 +  DbFixer *pFix,       /* Context of the fixation */
 1.80370 +  SrcList *pList       /* The Source list to check and modify */
 1.80371 +){
 1.80372 +  int i;
 1.80373 +  const char *zDb;
 1.80374 +  struct SrcList_item *pItem;
 1.80375 +
 1.80376 +  if( NEVER(pList==0) ) return 0;
 1.80377 +  zDb = pFix->zDb;
 1.80378 +  for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
 1.80379 +    if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
 1.80380 +      sqlite3ErrorMsg(pFix->pParse,
 1.80381 +         "%s %T cannot reference objects in database %s",
 1.80382 +         pFix->zType, pFix->pName, pItem->zDatabase);
 1.80383 +      return 1;
 1.80384 +    }
 1.80385 +    sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
 1.80386 +    pItem->zDatabase = 0;
 1.80387 +    pItem->pSchema = pFix->pSchema;
 1.80388 +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
 1.80389 +    if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
 1.80390 +    if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
 1.80391 +#endif
 1.80392 +  }
 1.80393 +  return 0;
 1.80394 +}
 1.80395 +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
 1.80396 +SQLITE_PRIVATE int sqlite3FixSelect(
 1.80397 +  DbFixer *pFix,       /* Context of the fixation */
 1.80398 +  Select *pSelect      /* The SELECT statement to be fixed to one database */
 1.80399 +){
 1.80400 +  while( pSelect ){
 1.80401 +    if( sqlite3FixExprList(pFix, pSelect->pEList) ){
 1.80402 +      return 1;
 1.80403 +    }
 1.80404 +    if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
 1.80405 +      return 1;
 1.80406 +    }
 1.80407 +    if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
 1.80408 +      return 1;
 1.80409 +    }
 1.80410 +    if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
 1.80411 +      return 1;
 1.80412 +    }
 1.80413 +    pSelect = pSelect->pPrior;
 1.80414 +  }
 1.80415 +  return 0;
 1.80416 +}
 1.80417 +SQLITE_PRIVATE int sqlite3FixExpr(
 1.80418 +  DbFixer *pFix,     /* Context of the fixation */
 1.80419 +  Expr *pExpr        /* The expression to be fixed to one database */
 1.80420 +){
 1.80421 +  while( pExpr ){
 1.80422 +    if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
 1.80423 +    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.80424 +      if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
 1.80425 +    }else{
 1.80426 +      if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
 1.80427 +    }
 1.80428 +    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
 1.80429 +      return 1;
 1.80430 +    }
 1.80431 +    pExpr = pExpr->pLeft;
 1.80432 +  }
 1.80433 +  return 0;
 1.80434 +}
 1.80435 +SQLITE_PRIVATE int sqlite3FixExprList(
 1.80436 +  DbFixer *pFix,     /* Context of the fixation */
 1.80437 +  ExprList *pList    /* The expression to be fixed to one database */
 1.80438 +){
 1.80439 +  int i;
 1.80440 +  struct ExprList_item *pItem;
 1.80441 +  if( pList==0 ) return 0;
 1.80442 +  for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
 1.80443 +    if( sqlite3FixExpr(pFix, pItem->pExpr) ){
 1.80444 +      return 1;
 1.80445 +    }
 1.80446 +  }
 1.80447 +  return 0;
 1.80448 +}
 1.80449 +#endif
 1.80450 +
 1.80451 +#ifndef SQLITE_OMIT_TRIGGER
 1.80452 +SQLITE_PRIVATE int sqlite3FixTriggerStep(
 1.80453 +  DbFixer *pFix,     /* Context of the fixation */
 1.80454 +  TriggerStep *pStep /* The trigger step be fixed to one database */
 1.80455 +){
 1.80456 +  while( pStep ){
 1.80457 +    if( sqlite3FixSelect(pFix, pStep->pSelect) ){
 1.80458 +      return 1;
 1.80459 +    }
 1.80460 +    if( sqlite3FixExpr(pFix, pStep->pWhere) ){
 1.80461 +      return 1;
 1.80462 +    }
 1.80463 +    if( sqlite3FixExprList(pFix, pStep->pExprList) ){
 1.80464 +      return 1;
 1.80465 +    }
 1.80466 +    pStep = pStep->pNext;
 1.80467 +  }
 1.80468 +  return 0;
 1.80469 +}
 1.80470 +#endif
 1.80471 +
 1.80472 +/************** End of attach.c **********************************************/
 1.80473 +/************** Begin file auth.c ********************************************/
 1.80474 +/*
 1.80475 +** 2003 January 11
 1.80476 +**
 1.80477 +** The author disclaims copyright to this source code.  In place of
 1.80478 +** a legal notice, here is a blessing:
 1.80479 +**
 1.80480 +**    May you do good and not evil.
 1.80481 +**    May you find forgiveness for yourself and forgive others.
 1.80482 +**    May you share freely, never taking more than you give.
 1.80483 +**
 1.80484 +*************************************************************************
 1.80485 +** This file contains code used to implement the sqlite3_set_authorizer()
 1.80486 +** API.  This facility is an optional feature of the library.  Embedded
 1.80487 +** systems that do not need this facility may omit it by recompiling
 1.80488 +** the library with -DSQLITE_OMIT_AUTHORIZATION=1
 1.80489 +*/
 1.80490 +
 1.80491 +/*
 1.80492 +** All of the code in this file may be omitted by defining a single
 1.80493 +** macro.
 1.80494 +*/
 1.80495 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.80496 +
 1.80497 +/*
 1.80498 +** Set or clear the access authorization function.
 1.80499 +**
 1.80500 +** The access authorization function is be called during the compilation
 1.80501 +** phase to verify that the user has read and/or write access permission on
 1.80502 +** various fields of the database.  The first argument to the auth function
 1.80503 +** is a copy of the 3rd argument to this routine.  The second argument
 1.80504 +** to the auth function is one of these constants:
 1.80505 +**
 1.80506 +**       SQLITE_CREATE_INDEX
 1.80507 +**       SQLITE_CREATE_TABLE
 1.80508 +**       SQLITE_CREATE_TEMP_INDEX
 1.80509 +**       SQLITE_CREATE_TEMP_TABLE
 1.80510 +**       SQLITE_CREATE_TEMP_TRIGGER
 1.80511 +**       SQLITE_CREATE_TEMP_VIEW
 1.80512 +**       SQLITE_CREATE_TRIGGER
 1.80513 +**       SQLITE_CREATE_VIEW
 1.80514 +**       SQLITE_DELETE
 1.80515 +**       SQLITE_DROP_INDEX
 1.80516 +**       SQLITE_DROP_TABLE
 1.80517 +**       SQLITE_DROP_TEMP_INDEX
 1.80518 +**       SQLITE_DROP_TEMP_TABLE
 1.80519 +**       SQLITE_DROP_TEMP_TRIGGER
 1.80520 +**       SQLITE_DROP_TEMP_VIEW
 1.80521 +**       SQLITE_DROP_TRIGGER
 1.80522 +**       SQLITE_DROP_VIEW
 1.80523 +**       SQLITE_INSERT
 1.80524 +**       SQLITE_PRAGMA
 1.80525 +**       SQLITE_READ
 1.80526 +**       SQLITE_SELECT
 1.80527 +**       SQLITE_TRANSACTION
 1.80528 +**       SQLITE_UPDATE
 1.80529 +**
 1.80530 +** The third and fourth arguments to the auth function are the name of
 1.80531 +** the table and the column that are being accessed.  The auth function
 1.80532 +** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
 1.80533 +** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
 1.80534 +** means that the SQL statement will never-run - the sqlite3_exec() call
 1.80535 +** will return with an error.  SQLITE_IGNORE means that the SQL statement
 1.80536 +** should run but attempts to read the specified column will return NULL
 1.80537 +** and attempts to write the column will be ignored.
 1.80538 +**
 1.80539 +** Setting the auth function to NULL disables this hook.  The default
 1.80540 +** setting of the auth function is NULL.
 1.80541 +*/
 1.80542 +SQLITE_API int sqlite3_set_authorizer(
 1.80543 +  sqlite3 *db,
 1.80544 +  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
 1.80545 +  void *pArg
 1.80546 +){
 1.80547 +  sqlite3_mutex_enter(db->mutex);
 1.80548 +  db->xAuth = xAuth;
 1.80549 +  db->pAuthArg = pArg;
 1.80550 +  sqlite3ExpirePreparedStatements(db);
 1.80551 +  sqlite3_mutex_leave(db->mutex);
 1.80552 +  return SQLITE_OK;
 1.80553 +}
 1.80554 +
 1.80555 +/*
 1.80556 +** Write an error message into pParse->zErrMsg that explains that the
 1.80557 +** user-supplied authorization function returned an illegal value.
 1.80558 +*/
 1.80559 +static void sqliteAuthBadReturnCode(Parse *pParse){
 1.80560 +  sqlite3ErrorMsg(pParse, "authorizer malfunction");
 1.80561 +  pParse->rc = SQLITE_ERROR;
 1.80562 +}
 1.80563 +
 1.80564 +/*
 1.80565 +** Invoke the authorization callback for permission to read column zCol from
 1.80566 +** table zTab in database zDb. This function assumes that an authorization
 1.80567 +** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
 1.80568 +**
 1.80569 +** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
 1.80570 +** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
 1.80571 +** is treated as SQLITE_DENY. In this case an error is left in pParse.
 1.80572 +*/
 1.80573 +SQLITE_PRIVATE int sqlite3AuthReadCol(
 1.80574 +  Parse *pParse,                  /* The parser context */
 1.80575 +  const char *zTab,               /* Table name */
 1.80576 +  const char *zCol,               /* Column name */
 1.80577 +  int iDb                         /* Index of containing database. */
 1.80578 +){
 1.80579 +  sqlite3 *db = pParse->db;       /* Database handle */
 1.80580 +  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
 1.80581 +  int rc;                         /* Auth callback return code */
 1.80582 +
 1.80583 +  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
 1.80584 +  if( rc==SQLITE_DENY ){
 1.80585 +    if( db->nDb>2 || iDb!=0 ){
 1.80586 +      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
 1.80587 +    }else{
 1.80588 +      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
 1.80589 +    }
 1.80590 +    pParse->rc = SQLITE_AUTH;
 1.80591 +  }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
 1.80592 +    sqliteAuthBadReturnCode(pParse);
 1.80593 +  }
 1.80594 +  return rc;
 1.80595 +}
 1.80596 +
 1.80597 +/*
 1.80598 +** The pExpr should be a TK_COLUMN expression.  The table referred to
 1.80599 +** is in pTabList or else it is the NEW or OLD table of a trigger.  
 1.80600 +** Check to see if it is OK to read this particular column.
 1.80601 +**
 1.80602 +** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
 1.80603 +** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
 1.80604 +** then generate an error.
 1.80605 +*/
 1.80606 +SQLITE_PRIVATE void sqlite3AuthRead(
 1.80607 +  Parse *pParse,        /* The parser context */
 1.80608 +  Expr *pExpr,          /* The expression to check authorization on */
 1.80609 +  Schema *pSchema,      /* The schema of the expression */
 1.80610 +  SrcList *pTabList     /* All table that pExpr might refer to */
 1.80611 +){
 1.80612 +  sqlite3 *db = pParse->db;
 1.80613 +  Table *pTab = 0;      /* The table being read */
 1.80614 +  const char *zCol;     /* Name of the column of the table */
 1.80615 +  int iSrc;             /* Index in pTabList->a[] of table being read */
 1.80616 +  int iDb;              /* The index of the database the expression refers to */
 1.80617 +  int iCol;             /* Index of column in table */
 1.80618 +
 1.80619 +  if( db->xAuth==0 ) return;
 1.80620 +  iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
 1.80621 +  if( iDb<0 ){
 1.80622 +    /* An attempt to read a column out of a subquery or other
 1.80623 +    ** temporary table. */
 1.80624 +    return;
 1.80625 +  }
 1.80626 +
 1.80627 +  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
 1.80628 +  if( pExpr->op==TK_TRIGGER ){
 1.80629 +    pTab = pParse->pTriggerTab;
 1.80630 +  }else{
 1.80631 +    assert( pTabList );
 1.80632 +    for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
 1.80633 +      if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
 1.80634 +        pTab = pTabList->a[iSrc].pTab;
 1.80635 +        break;
 1.80636 +      }
 1.80637 +    }
 1.80638 +  }
 1.80639 +  iCol = pExpr->iColumn;
 1.80640 +  if( NEVER(pTab==0) ) return;
 1.80641 +
 1.80642 +  if( iCol>=0 ){
 1.80643 +    assert( iCol<pTab->nCol );
 1.80644 +    zCol = pTab->aCol[iCol].zName;
 1.80645 +  }else if( pTab->iPKey>=0 ){
 1.80646 +    assert( pTab->iPKey<pTab->nCol );
 1.80647 +    zCol = pTab->aCol[pTab->iPKey].zName;
 1.80648 +  }else{
 1.80649 +    zCol = "ROWID";
 1.80650 +  }
 1.80651 +  assert( iDb>=0 && iDb<db->nDb );
 1.80652 +  if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
 1.80653 +    pExpr->op = TK_NULL;
 1.80654 +  }
 1.80655 +}
 1.80656 +
 1.80657 +/*
 1.80658 +** Do an authorization check using the code and arguments given.  Return
 1.80659 +** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
 1.80660 +** is returned, then the error count and error message in pParse are
 1.80661 +** modified appropriately.
 1.80662 +*/
 1.80663 +SQLITE_PRIVATE int sqlite3AuthCheck(
 1.80664 +  Parse *pParse,
 1.80665 +  int code,
 1.80666 +  const char *zArg1,
 1.80667 +  const char *zArg2,
 1.80668 +  const char *zArg3
 1.80669 +){
 1.80670 +  sqlite3 *db = pParse->db;
 1.80671 +  int rc;
 1.80672 +
 1.80673 +  /* Don't do any authorization checks if the database is initialising
 1.80674 +  ** or if the parser is being invoked from within sqlite3_declare_vtab.
 1.80675 +  */
 1.80676 +  if( db->init.busy || IN_DECLARE_VTAB ){
 1.80677 +    return SQLITE_OK;
 1.80678 +  }
 1.80679 +
 1.80680 +  if( db->xAuth==0 ){
 1.80681 +    return SQLITE_OK;
 1.80682 +  }
 1.80683 +  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
 1.80684 +  if( rc==SQLITE_DENY ){
 1.80685 +    sqlite3ErrorMsg(pParse, "not authorized");
 1.80686 +    pParse->rc = SQLITE_AUTH;
 1.80687 +  }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
 1.80688 +    rc = SQLITE_DENY;
 1.80689 +    sqliteAuthBadReturnCode(pParse);
 1.80690 +  }
 1.80691 +  return rc;
 1.80692 +}
 1.80693 +
 1.80694 +/*
 1.80695 +** Push an authorization context.  After this routine is called, the
 1.80696 +** zArg3 argument to authorization callbacks will be zContext until
 1.80697 +** popped.  Or if pParse==0, this routine is a no-op.
 1.80698 +*/
 1.80699 +SQLITE_PRIVATE void sqlite3AuthContextPush(
 1.80700 +  Parse *pParse,
 1.80701 +  AuthContext *pContext, 
 1.80702 +  const char *zContext
 1.80703 +){
 1.80704 +  assert( pParse );
 1.80705 +  pContext->pParse = pParse;
 1.80706 +  pContext->zAuthContext = pParse->zAuthContext;
 1.80707 +  pParse->zAuthContext = zContext;
 1.80708 +}
 1.80709 +
 1.80710 +/*
 1.80711 +** Pop an authorization context that was previously pushed
 1.80712 +** by sqlite3AuthContextPush
 1.80713 +*/
 1.80714 +SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
 1.80715 +  if( pContext->pParse ){
 1.80716 +    pContext->pParse->zAuthContext = pContext->zAuthContext;
 1.80717 +    pContext->pParse = 0;
 1.80718 +  }
 1.80719 +}
 1.80720 +
 1.80721 +#endif /* SQLITE_OMIT_AUTHORIZATION */
 1.80722 +
 1.80723 +/************** End of auth.c ************************************************/
 1.80724 +/************** Begin file build.c *******************************************/
 1.80725 +/*
 1.80726 +** 2001 September 15
 1.80727 +**
 1.80728 +** The author disclaims copyright to this source code.  In place of
 1.80729 +** a legal notice, here is a blessing:
 1.80730 +**
 1.80731 +**    May you do good and not evil.
 1.80732 +**    May you find forgiveness for yourself and forgive others.
 1.80733 +**    May you share freely, never taking more than you give.
 1.80734 +**
 1.80735 +*************************************************************************
 1.80736 +** This file contains C code routines that are called by the SQLite parser
 1.80737 +** when syntax rules are reduced.  The routines in this file handle the
 1.80738 +** following kinds of SQL syntax:
 1.80739 +**
 1.80740 +**     CREATE TABLE
 1.80741 +**     DROP TABLE
 1.80742 +**     CREATE INDEX
 1.80743 +**     DROP INDEX
 1.80744 +**     creating ID lists
 1.80745 +**     BEGIN TRANSACTION
 1.80746 +**     COMMIT
 1.80747 +**     ROLLBACK
 1.80748 +*/
 1.80749 +
 1.80750 +/*
 1.80751 +** This routine is called when a new SQL statement is beginning to
 1.80752 +** be parsed.  Initialize the pParse structure as needed.
 1.80753 +*/
 1.80754 +SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
 1.80755 +  pParse->explain = (u8)explainFlag;
 1.80756 +  pParse->nVar = 0;
 1.80757 +}
 1.80758 +
 1.80759 +#ifndef SQLITE_OMIT_SHARED_CACHE
 1.80760 +/*
 1.80761 +** The TableLock structure is only used by the sqlite3TableLock() and
 1.80762 +** codeTableLocks() functions.
 1.80763 +*/
 1.80764 +struct TableLock {
 1.80765 +  int iDb;             /* The database containing the table to be locked */
 1.80766 +  int iTab;            /* The root page of the table to be locked */
 1.80767 +  u8 isWriteLock;      /* True for write lock.  False for a read lock */
 1.80768 +  const char *zName;   /* Name of the table */
 1.80769 +};
 1.80770 +
 1.80771 +/*
 1.80772 +** Record the fact that we want to lock a table at run-time.  
 1.80773 +**
 1.80774 +** The table to be locked has root page iTab and is found in database iDb.
 1.80775 +** A read or a write lock can be taken depending on isWritelock.
 1.80776 +**
 1.80777 +** This routine just records the fact that the lock is desired.  The
 1.80778 +** code to make the lock occur is generated by a later call to
 1.80779 +** codeTableLocks() which occurs during sqlite3FinishCoding().
 1.80780 +*/
 1.80781 +SQLITE_PRIVATE void sqlite3TableLock(
 1.80782 +  Parse *pParse,     /* Parsing context */
 1.80783 +  int iDb,           /* Index of the database containing the table to lock */
 1.80784 +  int iTab,          /* Root page number of the table to be locked */
 1.80785 +  u8 isWriteLock,    /* True for a write lock */
 1.80786 +  const char *zName  /* Name of the table to be locked */
 1.80787 +){
 1.80788 +  Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.80789 +  int i;
 1.80790 +  int nBytes;
 1.80791 +  TableLock *p;
 1.80792 +  assert( iDb>=0 );
 1.80793 +
 1.80794 +  for(i=0; i<pToplevel->nTableLock; i++){
 1.80795 +    p = &pToplevel->aTableLock[i];
 1.80796 +    if( p->iDb==iDb && p->iTab==iTab ){
 1.80797 +      p->isWriteLock = (p->isWriteLock || isWriteLock);
 1.80798 +      return;
 1.80799 +    }
 1.80800 +  }
 1.80801 +
 1.80802 +  nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
 1.80803 +  pToplevel->aTableLock =
 1.80804 +      sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
 1.80805 +  if( pToplevel->aTableLock ){
 1.80806 +    p = &pToplevel->aTableLock[pToplevel->nTableLock++];
 1.80807 +    p->iDb = iDb;
 1.80808 +    p->iTab = iTab;
 1.80809 +    p->isWriteLock = isWriteLock;
 1.80810 +    p->zName = zName;
 1.80811 +  }else{
 1.80812 +    pToplevel->nTableLock = 0;
 1.80813 +    pToplevel->db->mallocFailed = 1;
 1.80814 +  }
 1.80815 +}
 1.80816 +
 1.80817 +/*
 1.80818 +** Code an OP_TableLock instruction for each table locked by the
 1.80819 +** statement (configured by calls to sqlite3TableLock()).
 1.80820 +*/
 1.80821 +static void codeTableLocks(Parse *pParse){
 1.80822 +  int i;
 1.80823 +  Vdbe *pVdbe; 
 1.80824 +
 1.80825 +  pVdbe = sqlite3GetVdbe(pParse);
 1.80826 +  assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
 1.80827 +
 1.80828 +  for(i=0; i<pParse->nTableLock; i++){
 1.80829 +    TableLock *p = &pParse->aTableLock[i];
 1.80830 +    int p1 = p->iDb;
 1.80831 +    sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
 1.80832 +                      p->zName, P4_STATIC);
 1.80833 +  }
 1.80834 +}
 1.80835 +#else
 1.80836 +  #define codeTableLocks(x)
 1.80837 +#endif
 1.80838 +
 1.80839 +/*
 1.80840 +** This routine is called after a single SQL statement has been
 1.80841 +** parsed and a VDBE program to execute that statement has been
 1.80842 +** prepared.  This routine puts the finishing touches on the
 1.80843 +** VDBE program and resets the pParse structure for the next
 1.80844 +** parse.
 1.80845 +**
 1.80846 +** Note that if an error occurred, it might be the case that
 1.80847 +** no VDBE code was generated.
 1.80848 +*/
 1.80849 +SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
 1.80850 +  sqlite3 *db;
 1.80851 +  Vdbe *v;
 1.80852 +
 1.80853 +  assert( pParse->pToplevel==0 );
 1.80854 +  db = pParse->db;
 1.80855 +  if( db->mallocFailed ) return;
 1.80856 +  if( pParse->nested ) return;
 1.80857 +  if( pParse->nErr ) return;
 1.80858 +
 1.80859 +  /* Begin by generating some termination code at the end of the
 1.80860 +  ** vdbe program
 1.80861 +  */
 1.80862 +  v = sqlite3GetVdbe(pParse);
 1.80863 +  assert( !pParse->isMultiWrite 
 1.80864 +       || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
 1.80865 +  if( v ){
 1.80866 +    sqlite3VdbeAddOp0(v, OP_Halt);
 1.80867 +
 1.80868 +    /* The cookie mask contains one bit for each database file open.
 1.80869 +    ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
 1.80870 +    ** set for each database that is used.  Generate code to start a
 1.80871 +    ** transaction on each used database and to verify the schema cookie
 1.80872 +    ** on each used database.
 1.80873 +    */
 1.80874 +    if( pParse->cookieGoto>0 ){
 1.80875 +      yDbMask mask;
 1.80876 +      int iDb;
 1.80877 +      sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
 1.80878 +      for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
 1.80879 +        if( (mask & pParse->cookieMask)==0 ) continue;
 1.80880 +        sqlite3VdbeUsesBtree(v, iDb);
 1.80881 +        sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
 1.80882 +        if( db->init.busy==0 ){
 1.80883 +          assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.80884 +          sqlite3VdbeAddOp3(v, OP_VerifyCookie,
 1.80885 +                            iDb, pParse->cookieValue[iDb],
 1.80886 +                            db->aDb[iDb].pSchema->iGeneration);
 1.80887 +        }
 1.80888 +      }
 1.80889 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.80890 +      {
 1.80891 +        int i;
 1.80892 +        for(i=0; i<pParse->nVtabLock; i++){
 1.80893 +          char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
 1.80894 +          sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
 1.80895 +        }
 1.80896 +        pParse->nVtabLock = 0;
 1.80897 +      }
 1.80898 +#endif
 1.80899 +
 1.80900 +      /* Once all the cookies have been verified and transactions opened, 
 1.80901 +      ** obtain the required table-locks. This is a no-op unless the 
 1.80902 +      ** shared-cache feature is enabled.
 1.80903 +      */
 1.80904 +      codeTableLocks(pParse);
 1.80905 +
 1.80906 +      /* Initialize any AUTOINCREMENT data structures required.
 1.80907 +      */
 1.80908 +      sqlite3AutoincrementBegin(pParse);
 1.80909 +
 1.80910 +      /* Finally, jump back to the beginning of the executable code. */
 1.80911 +      sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
 1.80912 +    }
 1.80913 +  }
 1.80914 +
 1.80915 +
 1.80916 +  /* Get the VDBE program ready for execution
 1.80917 +  */
 1.80918 +  if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
 1.80919 +#ifdef SQLITE_DEBUG
 1.80920 +    FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
 1.80921 +    sqlite3VdbeTrace(v, trace);
 1.80922 +#endif
 1.80923 +    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
 1.80924 +    /* A minimum of one cursor is required if autoincrement is used
 1.80925 +    *  See ticket [a696379c1f08866] */
 1.80926 +    if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
 1.80927 +    sqlite3VdbeMakeReady(v, pParse);
 1.80928 +    pParse->rc = SQLITE_DONE;
 1.80929 +    pParse->colNamesSet = 0;
 1.80930 +  }else{
 1.80931 +    pParse->rc = SQLITE_ERROR;
 1.80932 +  }
 1.80933 +  pParse->nTab = 0;
 1.80934 +  pParse->nMem = 0;
 1.80935 +  pParse->nSet = 0;
 1.80936 +  pParse->nVar = 0;
 1.80937 +  pParse->cookieMask = 0;
 1.80938 +  pParse->cookieGoto = 0;
 1.80939 +}
 1.80940 +
 1.80941 +/*
 1.80942 +** Run the parser and code generator recursively in order to generate
 1.80943 +** code for the SQL statement given onto the end of the pParse context
 1.80944 +** currently under construction.  When the parser is run recursively
 1.80945 +** this way, the final OP_Halt is not appended and other initialization
 1.80946 +** and finalization steps are omitted because those are handling by the
 1.80947 +** outermost parser.
 1.80948 +**
 1.80949 +** Not everything is nestable.  This facility is designed to permit
 1.80950 +** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
 1.80951 +** care if you decide to try to use this routine for some other purposes.
 1.80952 +*/
 1.80953 +SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
 1.80954 +  va_list ap;
 1.80955 +  char *zSql;
 1.80956 +  char *zErrMsg = 0;
 1.80957 +  sqlite3 *db = pParse->db;
 1.80958 +# define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
 1.80959 +  char saveBuf[SAVE_SZ];
 1.80960 +
 1.80961 +  if( pParse->nErr ) return;
 1.80962 +  assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
 1.80963 +  va_start(ap, zFormat);
 1.80964 +  zSql = sqlite3VMPrintf(db, zFormat, ap);
 1.80965 +  va_end(ap);
 1.80966 +  if( zSql==0 ){
 1.80967 +    return;   /* A malloc must have failed */
 1.80968 +  }
 1.80969 +  pParse->nested++;
 1.80970 +  memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
 1.80971 +  memset(&pParse->nVar, 0, SAVE_SZ);
 1.80972 +  sqlite3RunParser(pParse, zSql, &zErrMsg);
 1.80973 +  sqlite3DbFree(db, zErrMsg);
 1.80974 +  sqlite3DbFree(db, zSql);
 1.80975 +  memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
 1.80976 +  pParse->nested--;
 1.80977 +}
 1.80978 +
 1.80979 +/*
 1.80980 +** Locate the in-memory structure that describes a particular database
 1.80981 +** table given the name of that table and (optionally) the name of the
 1.80982 +** database containing the table.  Return NULL if not found.
 1.80983 +**
 1.80984 +** If zDatabase is 0, all databases are searched for the table and the
 1.80985 +** first matching table is returned.  (No checking for duplicate table
 1.80986 +** names is done.)  The search order is TEMP first, then MAIN, then any
 1.80987 +** auxiliary databases added using the ATTACH command.
 1.80988 +**
 1.80989 +** See also sqlite3LocateTable().
 1.80990 +*/
 1.80991 +SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
 1.80992 +  Table *p = 0;
 1.80993 +  int i;
 1.80994 +  int nName;
 1.80995 +  assert( zName!=0 );
 1.80996 +  nName = sqlite3Strlen30(zName);
 1.80997 +  /* All mutexes are required for schema access.  Make sure we hold them. */
 1.80998 +  assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 1.80999 +  for(i=OMIT_TEMPDB; i<db->nDb; i++){
 1.81000 +    int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
 1.81001 +    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
 1.81002 +    assert( sqlite3SchemaMutexHeld(db, j, 0) );
 1.81003 +    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
 1.81004 +    if( p ) break;
 1.81005 +  }
 1.81006 +  return p;
 1.81007 +}
 1.81008 +
 1.81009 +/*
 1.81010 +** Locate the in-memory structure that describes a particular database
 1.81011 +** table given the name of that table and (optionally) the name of the
 1.81012 +** database containing the table.  Return NULL if not found.  Also leave an
 1.81013 +** error message in pParse->zErrMsg.
 1.81014 +**
 1.81015 +** The difference between this routine and sqlite3FindTable() is that this
 1.81016 +** routine leaves an error message in pParse->zErrMsg where
 1.81017 +** sqlite3FindTable() does not.
 1.81018 +*/
 1.81019 +SQLITE_PRIVATE Table *sqlite3LocateTable(
 1.81020 +  Parse *pParse,         /* context in which to report errors */
 1.81021 +  int isView,            /* True if looking for a VIEW rather than a TABLE */
 1.81022 +  const char *zName,     /* Name of the table we are looking for */
 1.81023 +  const char *zDbase     /* Name of the database.  Might be NULL */
 1.81024 +){
 1.81025 +  Table *p;
 1.81026 +
 1.81027 +  /* Read the database schema. If an error occurs, leave an error message
 1.81028 +  ** and code in pParse and return NULL. */
 1.81029 +  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 1.81030 +    return 0;
 1.81031 +  }
 1.81032 +
 1.81033 +  p = sqlite3FindTable(pParse->db, zName, zDbase);
 1.81034 +  if( p==0 ){
 1.81035 +    const char *zMsg = isView ? "no such view" : "no such table";
 1.81036 +    if( zDbase ){
 1.81037 +      sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
 1.81038 +    }else{
 1.81039 +      sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
 1.81040 +    }
 1.81041 +    pParse->checkSchema = 1;
 1.81042 +  }
 1.81043 +  return p;
 1.81044 +}
 1.81045 +
 1.81046 +/*
 1.81047 +** Locate the table identified by *p.
 1.81048 +**
 1.81049 +** This is a wrapper around sqlite3LocateTable(). The difference between
 1.81050 +** sqlite3LocateTable() and this function is that this function restricts
 1.81051 +** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
 1.81052 +** non-NULL if it is part of a view or trigger program definition. See
 1.81053 +** sqlite3FixSrcList() for details.
 1.81054 +*/
 1.81055 +SQLITE_PRIVATE Table *sqlite3LocateTableItem(
 1.81056 +  Parse *pParse, 
 1.81057 +  int isView, 
 1.81058 +  struct SrcList_item *p
 1.81059 +){
 1.81060 +  const char *zDb;
 1.81061 +  assert( p->pSchema==0 || p->zDatabase==0 );
 1.81062 +  if( p->pSchema ){
 1.81063 +    int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
 1.81064 +    zDb = pParse->db->aDb[iDb].zName;
 1.81065 +  }else{
 1.81066 +    zDb = p->zDatabase;
 1.81067 +  }
 1.81068 +  return sqlite3LocateTable(pParse, isView, p->zName, zDb);
 1.81069 +}
 1.81070 +
 1.81071 +/*
 1.81072 +** Locate the in-memory structure that describes 
 1.81073 +** a particular index given the name of that index
 1.81074 +** and the name of the database that contains the index.
 1.81075 +** Return NULL if not found.
 1.81076 +**
 1.81077 +** If zDatabase is 0, all databases are searched for the
 1.81078 +** table and the first matching index is returned.  (No checking
 1.81079 +** for duplicate index names is done.)  The search order is
 1.81080 +** TEMP first, then MAIN, then any auxiliary databases added
 1.81081 +** using the ATTACH command.
 1.81082 +*/
 1.81083 +SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
 1.81084 +  Index *p = 0;
 1.81085 +  int i;
 1.81086 +  int nName = sqlite3Strlen30(zName);
 1.81087 +  /* All mutexes are required for schema access.  Make sure we hold them. */
 1.81088 +  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 1.81089 +  for(i=OMIT_TEMPDB; i<db->nDb; i++){
 1.81090 +    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 1.81091 +    Schema *pSchema = db->aDb[j].pSchema;
 1.81092 +    assert( pSchema );
 1.81093 +    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
 1.81094 +    assert( sqlite3SchemaMutexHeld(db, j, 0) );
 1.81095 +    p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
 1.81096 +    if( p ) break;
 1.81097 +  }
 1.81098 +  return p;
 1.81099 +}
 1.81100 +
 1.81101 +/*
 1.81102 +** Reclaim the memory used by an index
 1.81103 +*/
 1.81104 +static void freeIndex(sqlite3 *db, Index *p){
 1.81105 +#ifndef SQLITE_OMIT_ANALYZE
 1.81106 +  sqlite3DeleteIndexSamples(db, p);
 1.81107 +#endif
 1.81108 +  sqlite3DbFree(db, p->zColAff);
 1.81109 +  sqlite3DbFree(db, p);
 1.81110 +}
 1.81111 +
 1.81112 +/*
 1.81113 +** For the index called zIdxName which is found in the database iDb,
 1.81114 +** unlike that index from its Table then remove the index from
 1.81115 +** the index hash table and free all memory structures associated
 1.81116 +** with the index.
 1.81117 +*/
 1.81118 +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
 1.81119 +  Index *pIndex;
 1.81120 +  int len;
 1.81121 +  Hash *pHash;
 1.81122 +
 1.81123 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.81124 +  pHash = &db->aDb[iDb].pSchema->idxHash;
 1.81125 +  len = sqlite3Strlen30(zIdxName);
 1.81126 +  pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
 1.81127 +  if( ALWAYS(pIndex) ){
 1.81128 +    if( pIndex->pTable->pIndex==pIndex ){
 1.81129 +      pIndex->pTable->pIndex = pIndex->pNext;
 1.81130 +    }else{
 1.81131 +      Index *p;
 1.81132 +      /* Justification of ALWAYS();  The index must be on the list of
 1.81133 +      ** indices. */
 1.81134 +      p = pIndex->pTable->pIndex;
 1.81135 +      while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
 1.81136 +      if( ALWAYS(p && p->pNext==pIndex) ){
 1.81137 +        p->pNext = pIndex->pNext;
 1.81138 +      }
 1.81139 +    }
 1.81140 +    freeIndex(db, pIndex);
 1.81141 +  }
 1.81142 +  db->flags |= SQLITE_InternChanges;
 1.81143 +}
 1.81144 +
 1.81145 +/*
 1.81146 +** Look through the list of open database files in db->aDb[] and if
 1.81147 +** any have been closed, remove them from the list.  Reallocate the
 1.81148 +** db->aDb[] structure to a smaller size, if possible.
 1.81149 +**
 1.81150 +** Entry 0 (the "main" database) and entry 1 (the "temp" database)
 1.81151 +** are never candidates for being collapsed.
 1.81152 +*/
 1.81153 +SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
 1.81154 +  int i, j;
 1.81155 +  for(i=j=2; i<db->nDb; i++){
 1.81156 +    struct Db *pDb = &db->aDb[i];
 1.81157 +    if( pDb->pBt==0 ){
 1.81158 +      sqlite3DbFree(db, pDb->zName);
 1.81159 +      pDb->zName = 0;
 1.81160 +      continue;
 1.81161 +    }
 1.81162 +    if( j<i ){
 1.81163 +      db->aDb[j] = db->aDb[i];
 1.81164 +    }
 1.81165 +    j++;
 1.81166 +  }
 1.81167 +  memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
 1.81168 +  db->nDb = j;
 1.81169 +  if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
 1.81170 +    memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
 1.81171 +    sqlite3DbFree(db, db->aDb);
 1.81172 +    db->aDb = db->aDbStatic;
 1.81173 +  }
 1.81174 +}
 1.81175 +
 1.81176 +/*
 1.81177 +** Reset the schema for the database at index iDb.  Also reset the
 1.81178 +** TEMP schema.
 1.81179 +*/
 1.81180 +SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
 1.81181 +  Db *pDb;
 1.81182 +  assert( iDb<db->nDb );
 1.81183 +
 1.81184 +  /* Case 1:  Reset the single schema identified by iDb */
 1.81185 +  pDb = &db->aDb[iDb];
 1.81186 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.81187 +  assert( pDb->pSchema!=0 );
 1.81188 +  sqlite3SchemaClear(pDb->pSchema);
 1.81189 +
 1.81190 +  /* If any database other than TEMP is reset, then also reset TEMP
 1.81191 +  ** since TEMP might be holding triggers that reference tables in the
 1.81192 +  ** other database.
 1.81193 +  */
 1.81194 +  if( iDb!=1 ){
 1.81195 +    pDb = &db->aDb[1];
 1.81196 +    assert( pDb->pSchema!=0 );
 1.81197 +    sqlite3SchemaClear(pDb->pSchema);
 1.81198 +  }
 1.81199 +  return;
 1.81200 +}
 1.81201 +
 1.81202 +/*
 1.81203 +** Erase all schema information from all attached databases (including
 1.81204 +** "main" and "temp") for a single database connection.
 1.81205 +*/
 1.81206 +SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
 1.81207 +  int i;
 1.81208 +  sqlite3BtreeEnterAll(db);
 1.81209 +  for(i=0; i<db->nDb; i++){
 1.81210 +    Db *pDb = &db->aDb[i];
 1.81211 +    if( pDb->pSchema ){
 1.81212 +      sqlite3SchemaClear(pDb->pSchema);
 1.81213 +    }
 1.81214 +  }
 1.81215 +  db->flags &= ~SQLITE_InternChanges;
 1.81216 +  sqlite3VtabUnlockList(db);
 1.81217 +  sqlite3BtreeLeaveAll(db);
 1.81218 +  sqlite3CollapseDatabaseArray(db);
 1.81219 +}
 1.81220 +
 1.81221 +/*
 1.81222 +** This routine is called when a commit occurs.
 1.81223 +*/
 1.81224 +SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
 1.81225 +  db->flags &= ~SQLITE_InternChanges;
 1.81226 +}
 1.81227 +
 1.81228 +/*
 1.81229 +** Delete memory allocated for the column names of a table or view (the
 1.81230 +** Table.aCol[] array).
 1.81231 +*/
 1.81232 +static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
 1.81233 +  int i;
 1.81234 +  Column *pCol;
 1.81235 +  assert( pTable!=0 );
 1.81236 +  if( (pCol = pTable->aCol)!=0 ){
 1.81237 +    for(i=0; i<pTable->nCol; i++, pCol++){
 1.81238 +      sqlite3DbFree(db, pCol->zName);
 1.81239 +      sqlite3ExprDelete(db, pCol->pDflt);
 1.81240 +      sqlite3DbFree(db, pCol->zDflt);
 1.81241 +      sqlite3DbFree(db, pCol->zType);
 1.81242 +      sqlite3DbFree(db, pCol->zColl);
 1.81243 +    }
 1.81244 +    sqlite3DbFree(db, pTable->aCol);
 1.81245 +  }
 1.81246 +}
 1.81247 +
 1.81248 +/*
 1.81249 +** Remove the memory data structures associated with the given
 1.81250 +** Table.  No changes are made to disk by this routine.
 1.81251 +**
 1.81252 +** This routine just deletes the data structure.  It does not unlink
 1.81253 +** the table data structure from the hash table.  But it does destroy
 1.81254 +** memory structures of the indices and foreign keys associated with 
 1.81255 +** the table.
 1.81256 +**
 1.81257 +** The db parameter is optional.  It is needed if the Table object 
 1.81258 +** contains lookaside memory.  (Table objects in the schema do not use
 1.81259 +** lookaside memory, but some ephemeral Table objects do.)  Or the
 1.81260 +** db parameter can be used with db->pnBytesFreed to measure the memory
 1.81261 +** used by the Table object.
 1.81262 +*/
 1.81263 +SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
 1.81264 +  Index *pIndex, *pNext;
 1.81265 +  TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
 1.81266 +
 1.81267 +  assert( !pTable || pTable->nRef>0 );
 1.81268 +
 1.81269 +  /* Do not delete the table until the reference count reaches zero. */
 1.81270 +  if( !pTable ) return;
 1.81271 +  if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
 1.81272 +
 1.81273 +  /* Record the number of outstanding lookaside allocations in schema Tables
 1.81274 +  ** prior to doing any free() operations.  Since schema Tables do not use
 1.81275 +  ** lookaside, this number should not change. */
 1.81276 +  TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
 1.81277 +                         db->lookaside.nOut : 0 );
 1.81278 +
 1.81279 +  /* Delete all indices associated with this table. */
 1.81280 +  for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
 1.81281 +    pNext = pIndex->pNext;
 1.81282 +    assert( pIndex->pSchema==pTable->pSchema );
 1.81283 +    if( !db || db->pnBytesFreed==0 ){
 1.81284 +      char *zName = pIndex->zName; 
 1.81285 +      TESTONLY ( Index *pOld = ) sqlite3HashInsert(
 1.81286 +         &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
 1.81287 +      );
 1.81288 +      assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 1.81289 +      assert( pOld==pIndex || pOld==0 );
 1.81290 +    }
 1.81291 +    freeIndex(db, pIndex);
 1.81292 +  }
 1.81293 +
 1.81294 +  /* Delete any foreign keys attached to this table. */
 1.81295 +  sqlite3FkDelete(db, pTable);
 1.81296 +
 1.81297 +  /* Delete the Table structure itself.
 1.81298 +  */
 1.81299 +  sqliteDeleteColumnNames(db, pTable);
 1.81300 +  sqlite3DbFree(db, pTable->zName);
 1.81301 +  sqlite3DbFree(db, pTable->zColAff);
 1.81302 +  sqlite3SelectDelete(db, pTable->pSelect);
 1.81303 +#ifndef SQLITE_OMIT_CHECK
 1.81304 +  sqlite3ExprListDelete(db, pTable->pCheck);
 1.81305 +#endif
 1.81306 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.81307 +  sqlite3VtabClear(db, pTable);
 1.81308 +#endif
 1.81309 +  sqlite3DbFree(db, pTable);
 1.81310 +
 1.81311 +  /* Verify that no lookaside memory was used by schema tables */
 1.81312 +  assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
 1.81313 +}
 1.81314 +
 1.81315 +/*
 1.81316 +** Unlink the given table from the hash tables and the delete the
 1.81317 +** table structure with all its indices and foreign keys.
 1.81318 +*/
 1.81319 +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
 1.81320 +  Table *p;
 1.81321 +  Db *pDb;
 1.81322 +
 1.81323 +  assert( db!=0 );
 1.81324 +  assert( iDb>=0 && iDb<db->nDb );
 1.81325 +  assert( zTabName );
 1.81326 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.81327 +  testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
 1.81328 +  pDb = &db->aDb[iDb];
 1.81329 +  p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
 1.81330 +                        sqlite3Strlen30(zTabName),0);
 1.81331 +  sqlite3DeleteTable(db, p);
 1.81332 +  db->flags |= SQLITE_InternChanges;
 1.81333 +}
 1.81334 +
 1.81335 +/*
 1.81336 +** Given a token, return a string that consists of the text of that
 1.81337 +** token.  Space to hold the returned string
 1.81338 +** is obtained from sqliteMalloc() and must be freed by the calling
 1.81339 +** function.
 1.81340 +**
 1.81341 +** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
 1.81342 +** surround the body of the token are removed.
 1.81343 +**
 1.81344 +** Tokens are often just pointers into the original SQL text and so
 1.81345 +** are not \000 terminated and are not persistent.  The returned string
 1.81346 +** is \000 terminated and is persistent.
 1.81347 +*/
 1.81348 +SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
 1.81349 +  char *zName;
 1.81350 +  if( pName ){
 1.81351 +    zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
 1.81352 +    sqlite3Dequote(zName);
 1.81353 +  }else{
 1.81354 +    zName = 0;
 1.81355 +  }
 1.81356 +  return zName;
 1.81357 +}
 1.81358 +
 1.81359 +/*
 1.81360 +** Open the sqlite_master table stored in database number iDb for
 1.81361 +** writing. The table is opened using cursor 0.
 1.81362 +*/
 1.81363 +SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
 1.81364 +  Vdbe *v = sqlite3GetVdbe(p);
 1.81365 +  sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
 1.81366 +  sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
 1.81367 +  sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
 1.81368 +  if( p->nTab==0 ){
 1.81369 +    p->nTab = 1;
 1.81370 +  }
 1.81371 +}
 1.81372 +
 1.81373 +/*
 1.81374 +** Parameter zName points to a nul-terminated buffer containing the name
 1.81375 +** of a database ("main", "temp" or the name of an attached db). This
 1.81376 +** function returns the index of the named database in db->aDb[], or
 1.81377 +** -1 if the named db cannot be found.
 1.81378 +*/
 1.81379 +SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
 1.81380 +  int i = -1;         /* Database number */
 1.81381 +  if( zName ){
 1.81382 +    Db *pDb;
 1.81383 +    int n = sqlite3Strlen30(zName);
 1.81384 +    for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
 1.81385 +      if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && 
 1.81386 +          0==sqlite3StrICmp(pDb->zName, zName) ){
 1.81387 +        break;
 1.81388 +      }
 1.81389 +    }
 1.81390 +  }
 1.81391 +  return i;
 1.81392 +}
 1.81393 +
 1.81394 +/*
 1.81395 +** The token *pName contains the name of a database (either "main" or
 1.81396 +** "temp" or the name of an attached db). This routine returns the
 1.81397 +** index of the named database in db->aDb[], or -1 if the named db 
 1.81398 +** does not exist.
 1.81399 +*/
 1.81400 +SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
 1.81401 +  int i;                               /* Database number */
 1.81402 +  char *zName;                         /* Name we are searching for */
 1.81403 +  zName = sqlite3NameFromToken(db, pName);
 1.81404 +  i = sqlite3FindDbName(db, zName);
 1.81405 +  sqlite3DbFree(db, zName);
 1.81406 +  return i;
 1.81407 +}
 1.81408 +
 1.81409 +/* The table or view or trigger name is passed to this routine via tokens
 1.81410 +** pName1 and pName2. If the table name was fully qualified, for example:
 1.81411 +**
 1.81412 +** CREATE TABLE xxx.yyy (...);
 1.81413 +** 
 1.81414 +** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
 1.81415 +** the table name is not fully qualified, i.e.:
 1.81416 +**
 1.81417 +** CREATE TABLE yyy(...);
 1.81418 +**
 1.81419 +** Then pName1 is set to "yyy" and pName2 is "".
 1.81420 +**
 1.81421 +** This routine sets the *ppUnqual pointer to point at the token (pName1 or
 1.81422 +** pName2) that stores the unqualified table name.  The index of the
 1.81423 +** database "xxx" is returned.
 1.81424 +*/
 1.81425 +SQLITE_PRIVATE int sqlite3TwoPartName(
 1.81426 +  Parse *pParse,      /* Parsing and code generating context */
 1.81427 +  Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
 1.81428 +  Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
 1.81429 +  Token **pUnqual     /* Write the unqualified object name here */
 1.81430 +){
 1.81431 +  int iDb;                    /* Database holding the object */
 1.81432 +  sqlite3 *db = pParse->db;
 1.81433 +
 1.81434 +  if( ALWAYS(pName2!=0) && pName2->n>0 ){
 1.81435 +    if( db->init.busy ) {
 1.81436 +      sqlite3ErrorMsg(pParse, "corrupt database");
 1.81437 +      pParse->nErr++;
 1.81438 +      return -1;
 1.81439 +    }
 1.81440 +    *pUnqual = pName2;
 1.81441 +    iDb = sqlite3FindDb(db, pName1);
 1.81442 +    if( iDb<0 ){
 1.81443 +      sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
 1.81444 +      pParse->nErr++;
 1.81445 +      return -1;
 1.81446 +    }
 1.81447 +  }else{
 1.81448 +    assert( db->init.iDb==0 || db->init.busy );
 1.81449 +    iDb = db->init.iDb;
 1.81450 +    *pUnqual = pName1;
 1.81451 +  }
 1.81452 +  return iDb;
 1.81453 +}
 1.81454 +
 1.81455 +/*
 1.81456 +** This routine is used to check if the UTF-8 string zName is a legal
 1.81457 +** unqualified name for a new schema object (table, index, view or
 1.81458 +** trigger). All names are legal except those that begin with the string
 1.81459 +** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
 1.81460 +** is reserved for internal use.
 1.81461 +*/
 1.81462 +SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
 1.81463 +  if( !pParse->db->init.busy && pParse->nested==0 
 1.81464 +          && (pParse->db->flags & SQLITE_WriteSchema)==0
 1.81465 +          && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
 1.81466 +    sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
 1.81467 +    return SQLITE_ERROR;
 1.81468 +  }
 1.81469 +  return SQLITE_OK;
 1.81470 +}
 1.81471 +
 1.81472 +/*
 1.81473 +** Begin constructing a new table representation in memory.  This is
 1.81474 +** the first of several action routines that get called in response
 1.81475 +** to a CREATE TABLE statement.  In particular, this routine is called
 1.81476 +** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
 1.81477 +** flag is true if the table should be stored in the auxiliary database
 1.81478 +** file instead of in the main database file.  This is normally the case
 1.81479 +** when the "TEMP" or "TEMPORARY" keyword occurs in between
 1.81480 +** CREATE and TABLE.
 1.81481 +**
 1.81482 +** The new table record is initialized and put in pParse->pNewTable.
 1.81483 +** As more of the CREATE TABLE statement is parsed, additional action
 1.81484 +** routines will be called to add more information to this record.
 1.81485 +** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
 1.81486 +** is called to complete the construction of the new table record.
 1.81487 +*/
 1.81488 +SQLITE_PRIVATE void sqlite3StartTable(
 1.81489 +  Parse *pParse,   /* Parser context */
 1.81490 +  Token *pName1,   /* First part of the name of the table or view */
 1.81491 +  Token *pName2,   /* Second part of the name of the table or view */
 1.81492 +  int isTemp,      /* True if this is a TEMP table */
 1.81493 +  int isView,      /* True if this is a VIEW */
 1.81494 +  int isVirtual,   /* True if this is a VIRTUAL table */
 1.81495 +  int noErr        /* Do nothing if table already exists */
 1.81496 +){
 1.81497 +  Table *pTable;
 1.81498 +  char *zName = 0; /* The name of the new table */
 1.81499 +  sqlite3 *db = pParse->db;
 1.81500 +  Vdbe *v;
 1.81501 +  int iDb;         /* Database number to create the table in */
 1.81502 +  Token *pName;    /* Unqualified name of the table to create */
 1.81503 +
 1.81504 +  /* The table or view name to create is passed to this routine via tokens
 1.81505 +  ** pName1 and pName2. If the table name was fully qualified, for example:
 1.81506 +  **
 1.81507 +  ** CREATE TABLE xxx.yyy (...);
 1.81508 +  ** 
 1.81509 +  ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
 1.81510 +  ** the table name is not fully qualified, i.e.:
 1.81511 +  **
 1.81512 +  ** CREATE TABLE yyy(...);
 1.81513 +  **
 1.81514 +  ** Then pName1 is set to "yyy" and pName2 is "".
 1.81515 +  **
 1.81516 +  ** The call below sets the pName pointer to point at the token (pName1 or
 1.81517 +  ** pName2) that stores the unqualified table name. The variable iDb is
 1.81518 +  ** set to the index of the database that the table or view is to be
 1.81519 +  ** created in.
 1.81520 +  */
 1.81521 +  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 1.81522 +  if( iDb<0 ) return;
 1.81523 +  if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
 1.81524 +    /* If creating a temp table, the name may not be qualified. Unless 
 1.81525 +    ** the database name is "temp" anyway.  */
 1.81526 +    sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
 1.81527 +    return;
 1.81528 +  }
 1.81529 +  if( !OMIT_TEMPDB && isTemp ) iDb = 1;
 1.81530 +
 1.81531 +  pParse->sNameToken = *pName;
 1.81532 +  zName = sqlite3NameFromToken(db, pName);
 1.81533 +  if( zName==0 ) return;
 1.81534 +  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 1.81535 +    goto begin_table_error;
 1.81536 +  }
 1.81537 +  if( db->init.iDb==1 ) isTemp = 1;
 1.81538 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.81539 +  assert( (isTemp & 1)==isTemp );
 1.81540 +  {
 1.81541 +    int code;
 1.81542 +    char *zDb = db->aDb[iDb].zName;
 1.81543 +    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
 1.81544 +      goto begin_table_error;
 1.81545 +    }
 1.81546 +    if( isView ){
 1.81547 +      if( !OMIT_TEMPDB && isTemp ){
 1.81548 +        code = SQLITE_CREATE_TEMP_VIEW;
 1.81549 +      }else{
 1.81550 +        code = SQLITE_CREATE_VIEW;
 1.81551 +      }
 1.81552 +    }else{
 1.81553 +      if( !OMIT_TEMPDB && isTemp ){
 1.81554 +        code = SQLITE_CREATE_TEMP_TABLE;
 1.81555 +      }else{
 1.81556 +        code = SQLITE_CREATE_TABLE;
 1.81557 +      }
 1.81558 +    }
 1.81559 +    if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
 1.81560 +      goto begin_table_error;
 1.81561 +    }
 1.81562 +  }
 1.81563 +#endif
 1.81564 +
 1.81565 +  /* Make sure the new table name does not collide with an existing
 1.81566 +  ** index or table name in the same database.  Issue an error message if
 1.81567 +  ** it does. The exception is if the statement being parsed was passed
 1.81568 +  ** to an sqlite3_declare_vtab() call. In that case only the column names
 1.81569 +  ** and types will be used, so there is no need to test for namespace
 1.81570 +  ** collisions.
 1.81571 +  */
 1.81572 +  if( !IN_DECLARE_VTAB ){
 1.81573 +    char *zDb = db->aDb[iDb].zName;
 1.81574 +    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 1.81575 +      goto begin_table_error;
 1.81576 +    }
 1.81577 +    pTable = sqlite3FindTable(db, zName, zDb);
 1.81578 +    if( pTable ){
 1.81579 +      if( !noErr ){
 1.81580 +        sqlite3ErrorMsg(pParse, "table %T already exists", pName);
 1.81581 +      }else{
 1.81582 +        assert( !db->init.busy );
 1.81583 +        sqlite3CodeVerifySchema(pParse, iDb);
 1.81584 +      }
 1.81585 +      goto begin_table_error;
 1.81586 +    }
 1.81587 +    if( sqlite3FindIndex(db, zName, zDb)!=0 ){
 1.81588 +      sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
 1.81589 +      goto begin_table_error;
 1.81590 +    }
 1.81591 +  }
 1.81592 +
 1.81593 +  pTable = sqlite3DbMallocZero(db, sizeof(Table));
 1.81594 +  if( pTable==0 ){
 1.81595 +    db->mallocFailed = 1;
 1.81596 +    pParse->rc = SQLITE_NOMEM;
 1.81597 +    pParse->nErr++;
 1.81598 +    goto begin_table_error;
 1.81599 +  }
 1.81600 +  pTable->zName = zName;
 1.81601 +  pTable->iPKey = -1;
 1.81602 +  pTable->pSchema = db->aDb[iDb].pSchema;
 1.81603 +  pTable->nRef = 1;
 1.81604 +  pTable->nRowEst = 1000000;
 1.81605 +  assert( pParse->pNewTable==0 );
 1.81606 +  pParse->pNewTable = pTable;
 1.81607 +
 1.81608 +  /* If this is the magic sqlite_sequence table used by autoincrement,
 1.81609 +  ** then record a pointer to this table in the main database structure
 1.81610 +  ** so that INSERT can find the table easily.
 1.81611 +  */
 1.81612 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.81613 +  if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
 1.81614 +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.81615 +    pTable->pSchema->pSeqTab = pTable;
 1.81616 +  }
 1.81617 +#endif
 1.81618 +
 1.81619 +  /* Begin generating the code that will insert the table record into
 1.81620 +  ** the SQLITE_MASTER table.  Note in particular that we must go ahead
 1.81621 +  ** and allocate the record number for the table entry now.  Before any
 1.81622 +  ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
 1.81623 +  ** indices to be created and the table record must come before the 
 1.81624 +  ** indices.  Hence, the record number for the table must be allocated
 1.81625 +  ** now.
 1.81626 +  */
 1.81627 +  if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
 1.81628 +    int j1;
 1.81629 +    int fileFormat;
 1.81630 +    int reg1, reg2, reg3;
 1.81631 +    sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.81632 +
 1.81633 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.81634 +    if( isVirtual ){
 1.81635 +      sqlite3VdbeAddOp0(v, OP_VBegin);
 1.81636 +    }
 1.81637 +#endif
 1.81638 +
 1.81639 +    /* If the file format and encoding in the database have not been set, 
 1.81640 +    ** set them now.
 1.81641 +    */
 1.81642 +    reg1 = pParse->regRowid = ++pParse->nMem;
 1.81643 +    reg2 = pParse->regRoot = ++pParse->nMem;
 1.81644 +    reg3 = ++pParse->nMem;
 1.81645 +    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
 1.81646 +    sqlite3VdbeUsesBtree(v, iDb);
 1.81647 +    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
 1.81648 +    fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
 1.81649 +                  1 : SQLITE_MAX_FILE_FORMAT;
 1.81650 +    sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
 1.81651 +    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
 1.81652 +    sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
 1.81653 +    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
 1.81654 +    sqlite3VdbeJumpHere(v, j1);
 1.81655 +
 1.81656 +    /* This just creates a place-holder record in the sqlite_master table.
 1.81657 +    ** The record created does not contain anything yet.  It will be replaced
 1.81658 +    ** by the real entry in code generated at sqlite3EndTable().
 1.81659 +    **
 1.81660 +    ** The rowid for the new entry is left in register pParse->regRowid.
 1.81661 +    ** The root page number of the new table is left in reg pParse->regRoot.
 1.81662 +    ** The rowid and root page number values are needed by the code that
 1.81663 +    ** sqlite3EndTable will generate.
 1.81664 +    */
 1.81665 +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 1.81666 +    if( isView || isVirtual ){
 1.81667 +      sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
 1.81668 +    }else
 1.81669 +#endif
 1.81670 +    {
 1.81671 +      sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
 1.81672 +    }
 1.81673 +    sqlite3OpenMasterTable(pParse, iDb);
 1.81674 +    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
 1.81675 +    sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
 1.81676 +    sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
 1.81677 +    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.81678 +    sqlite3VdbeAddOp0(v, OP_Close);
 1.81679 +  }
 1.81680 +
 1.81681 +  /* Normal (non-error) return. */
 1.81682 +  return;
 1.81683 +
 1.81684 +  /* If an error occurs, we jump here */
 1.81685 +begin_table_error:
 1.81686 +  sqlite3DbFree(db, zName);
 1.81687 +  return;
 1.81688 +}
 1.81689 +
 1.81690 +/*
 1.81691 +** This macro is used to compare two strings in a case-insensitive manner.
 1.81692 +** It is slightly faster than calling sqlite3StrICmp() directly, but
 1.81693 +** produces larger code.
 1.81694 +**
 1.81695 +** WARNING: This macro is not compatible with the strcmp() family. It
 1.81696 +** returns true if the two strings are equal, otherwise false.
 1.81697 +*/
 1.81698 +#define STRICMP(x, y) (\
 1.81699 +sqlite3UpperToLower[*(unsigned char *)(x)]==   \
 1.81700 +sqlite3UpperToLower[*(unsigned char *)(y)]     \
 1.81701 +&& sqlite3StrICmp((x)+1,(y)+1)==0 )
 1.81702 +
 1.81703 +/*
 1.81704 +** Add a new column to the table currently being constructed.
 1.81705 +**
 1.81706 +** The parser calls this routine once for each column declaration
 1.81707 +** in a CREATE TABLE statement.  sqlite3StartTable() gets called
 1.81708 +** first to get things going.  Then this routine is called for each
 1.81709 +** column.
 1.81710 +*/
 1.81711 +SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
 1.81712 +  Table *p;
 1.81713 +  int i;
 1.81714 +  char *z;
 1.81715 +  Column *pCol;
 1.81716 +  sqlite3 *db = pParse->db;
 1.81717 +  if( (p = pParse->pNewTable)==0 ) return;
 1.81718 +#if SQLITE_MAX_COLUMN
 1.81719 +  if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 1.81720 +    sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
 1.81721 +    return;
 1.81722 +  }
 1.81723 +#endif
 1.81724 +  z = sqlite3NameFromToken(db, pName);
 1.81725 +  if( z==0 ) return;
 1.81726 +  for(i=0; i<p->nCol; i++){
 1.81727 +    if( STRICMP(z, p->aCol[i].zName) ){
 1.81728 +      sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
 1.81729 +      sqlite3DbFree(db, z);
 1.81730 +      return;
 1.81731 +    }
 1.81732 +  }
 1.81733 +  if( (p->nCol & 0x7)==0 ){
 1.81734 +    Column *aNew;
 1.81735 +    aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
 1.81736 +    if( aNew==0 ){
 1.81737 +      sqlite3DbFree(db, z);
 1.81738 +      return;
 1.81739 +    }
 1.81740 +    p->aCol = aNew;
 1.81741 +  }
 1.81742 +  pCol = &p->aCol[p->nCol];
 1.81743 +  memset(pCol, 0, sizeof(p->aCol[0]));
 1.81744 +  pCol->zName = z;
 1.81745 + 
 1.81746 +  /* If there is no type specified, columns have the default affinity
 1.81747 +  ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
 1.81748 +  ** be called next to set pCol->affinity correctly.
 1.81749 +  */
 1.81750 +  pCol->affinity = SQLITE_AFF_NONE;
 1.81751 +  p->nCol++;
 1.81752 +}
 1.81753 +
 1.81754 +/*
 1.81755 +** This routine is called by the parser while in the middle of
 1.81756 +** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
 1.81757 +** been seen on a column.  This routine sets the notNull flag on
 1.81758 +** the column currently under construction.
 1.81759 +*/
 1.81760 +SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
 1.81761 +  Table *p;
 1.81762 +  p = pParse->pNewTable;
 1.81763 +  if( p==0 || NEVER(p->nCol<1) ) return;
 1.81764 +  p->aCol[p->nCol-1].notNull = (u8)onError;
 1.81765 +}
 1.81766 +
 1.81767 +/*
 1.81768 +** Scan the column type name zType (length nType) and return the
 1.81769 +** associated affinity type.
 1.81770 +**
 1.81771 +** This routine does a case-independent search of zType for the 
 1.81772 +** substrings in the following table. If one of the substrings is
 1.81773 +** found, the corresponding affinity is returned. If zType contains
 1.81774 +** more than one of the substrings, entries toward the top of 
 1.81775 +** the table take priority. For example, if zType is 'BLOBINT', 
 1.81776 +** SQLITE_AFF_INTEGER is returned.
 1.81777 +**
 1.81778 +** Substring     | Affinity
 1.81779 +** --------------------------------
 1.81780 +** 'INT'         | SQLITE_AFF_INTEGER
 1.81781 +** 'CHAR'        | SQLITE_AFF_TEXT
 1.81782 +** 'CLOB'        | SQLITE_AFF_TEXT
 1.81783 +** 'TEXT'        | SQLITE_AFF_TEXT
 1.81784 +** 'BLOB'        | SQLITE_AFF_NONE
 1.81785 +** 'REAL'        | SQLITE_AFF_REAL
 1.81786 +** 'FLOA'        | SQLITE_AFF_REAL
 1.81787 +** 'DOUB'        | SQLITE_AFF_REAL
 1.81788 +**
 1.81789 +** If none of the substrings in the above table are found,
 1.81790 +** SQLITE_AFF_NUMERIC is returned.
 1.81791 +*/
 1.81792 +SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
 1.81793 +  u32 h = 0;
 1.81794 +  char aff = SQLITE_AFF_NUMERIC;
 1.81795 +
 1.81796 +  if( zIn ) while( zIn[0] ){
 1.81797 +    h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
 1.81798 +    zIn++;
 1.81799 +    if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
 1.81800 +      aff = SQLITE_AFF_TEXT; 
 1.81801 +    }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
 1.81802 +      aff = SQLITE_AFF_TEXT;
 1.81803 +    }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
 1.81804 +      aff = SQLITE_AFF_TEXT;
 1.81805 +    }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
 1.81806 +        && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
 1.81807 +      aff = SQLITE_AFF_NONE;
 1.81808 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.81809 +    }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
 1.81810 +        && aff==SQLITE_AFF_NUMERIC ){
 1.81811 +      aff = SQLITE_AFF_REAL;
 1.81812 +    }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
 1.81813 +        && aff==SQLITE_AFF_NUMERIC ){
 1.81814 +      aff = SQLITE_AFF_REAL;
 1.81815 +    }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
 1.81816 +        && aff==SQLITE_AFF_NUMERIC ){
 1.81817 +      aff = SQLITE_AFF_REAL;
 1.81818 +#endif
 1.81819 +    }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
 1.81820 +      aff = SQLITE_AFF_INTEGER;
 1.81821 +      break;
 1.81822 +    }
 1.81823 +  }
 1.81824 +
 1.81825 +  return aff;
 1.81826 +}
 1.81827 +
 1.81828 +/*
 1.81829 +** This routine is called by the parser while in the middle of
 1.81830 +** parsing a CREATE TABLE statement.  The pFirst token is the first
 1.81831 +** token in the sequence of tokens that describe the type of the
 1.81832 +** column currently under construction.   pLast is the last token
 1.81833 +** in the sequence.  Use this information to construct a string
 1.81834 +** that contains the typename of the column and store that string
 1.81835 +** in zType.
 1.81836 +*/ 
 1.81837 +SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
 1.81838 +  Table *p;
 1.81839 +  Column *pCol;
 1.81840 +
 1.81841 +  p = pParse->pNewTable;
 1.81842 +  if( p==0 || NEVER(p->nCol<1) ) return;
 1.81843 +  pCol = &p->aCol[p->nCol-1];
 1.81844 +  assert( pCol->zType==0 );
 1.81845 +  pCol->zType = sqlite3NameFromToken(pParse->db, pType);
 1.81846 +  pCol->affinity = sqlite3AffinityType(pCol->zType);
 1.81847 +}
 1.81848 +
 1.81849 +/*
 1.81850 +** The expression is the default value for the most recently added column
 1.81851 +** of the table currently under construction.
 1.81852 +**
 1.81853 +** Default value expressions must be constant.  Raise an exception if this
 1.81854 +** is not the case.
 1.81855 +**
 1.81856 +** This routine is called by the parser while in the middle of
 1.81857 +** parsing a CREATE TABLE statement.
 1.81858 +*/
 1.81859 +SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
 1.81860 +  Table *p;
 1.81861 +  Column *pCol;
 1.81862 +  sqlite3 *db = pParse->db;
 1.81863 +  p = pParse->pNewTable;
 1.81864 +  if( p!=0 ){
 1.81865 +    pCol = &(p->aCol[p->nCol-1]);
 1.81866 +    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
 1.81867 +      sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
 1.81868 +          pCol->zName);
 1.81869 +    }else{
 1.81870 +      /* A copy of pExpr is used instead of the original, as pExpr contains
 1.81871 +      ** tokens that point to volatile memory. The 'span' of the expression
 1.81872 +      ** is required by pragma table_info.
 1.81873 +      */
 1.81874 +      sqlite3ExprDelete(db, pCol->pDflt);
 1.81875 +      pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
 1.81876 +      sqlite3DbFree(db, pCol->zDflt);
 1.81877 +      pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
 1.81878 +                                     (int)(pSpan->zEnd - pSpan->zStart));
 1.81879 +    }
 1.81880 +  }
 1.81881 +  sqlite3ExprDelete(db, pSpan->pExpr);
 1.81882 +}
 1.81883 +
 1.81884 +/*
 1.81885 +** Designate the PRIMARY KEY for the table.  pList is a list of names 
 1.81886 +** of columns that form the primary key.  If pList is NULL, then the
 1.81887 +** most recently added column of the table is the primary key.
 1.81888 +**
 1.81889 +** A table can have at most one primary key.  If the table already has
 1.81890 +** a primary key (and this is the second primary key) then create an
 1.81891 +** error.
 1.81892 +**
 1.81893 +** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
 1.81894 +** then we will try to use that column as the rowid.  Set the Table.iPKey
 1.81895 +** field of the table under construction to be the index of the
 1.81896 +** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
 1.81897 +** no INTEGER PRIMARY KEY.
 1.81898 +**
 1.81899 +** If the key is not an INTEGER PRIMARY KEY, then create a unique
 1.81900 +** index for the key.  No index is created for INTEGER PRIMARY KEYs.
 1.81901 +*/
 1.81902 +SQLITE_PRIVATE void sqlite3AddPrimaryKey(
 1.81903 +  Parse *pParse,    /* Parsing context */
 1.81904 +  ExprList *pList,  /* List of field names to be indexed */
 1.81905 +  int onError,      /* What to do with a uniqueness conflict */
 1.81906 +  int autoInc,      /* True if the AUTOINCREMENT keyword is present */
 1.81907 +  int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
 1.81908 +){
 1.81909 +  Table *pTab = pParse->pNewTable;
 1.81910 +  char *zType = 0;
 1.81911 +  int iCol = -1, i;
 1.81912 +  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
 1.81913 +  if( pTab->tabFlags & TF_HasPrimaryKey ){
 1.81914 +    sqlite3ErrorMsg(pParse, 
 1.81915 +      "table \"%s\" has more than one primary key", pTab->zName);
 1.81916 +    goto primary_key_exit;
 1.81917 +  }
 1.81918 +  pTab->tabFlags |= TF_HasPrimaryKey;
 1.81919 +  if( pList==0 ){
 1.81920 +    iCol = pTab->nCol - 1;
 1.81921 +    pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 1.81922 +  }else{
 1.81923 +    for(i=0; i<pList->nExpr; i++){
 1.81924 +      for(iCol=0; iCol<pTab->nCol; iCol++){
 1.81925 +        if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
 1.81926 +          break;
 1.81927 +        }
 1.81928 +      }
 1.81929 +      if( iCol<pTab->nCol ){
 1.81930 +        pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 1.81931 +      }
 1.81932 +    }
 1.81933 +    if( pList->nExpr>1 ) iCol = -1;
 1.81934 +  }
 1.81935 +  if( iCol>=0 && iCol<pTab->nCol ){
 1.81936 +    zType = pTab->aCol[iCol].zType;
 1.81937 +  }
 1.81938 +  if( zType && sqlite3StrICmp(zType, "INTEGER")==0
 1.81939 +        && sortOrder==SQLITE_SO_ASC ){
 1.81940 +    pTab->iPKey = iCol;
 1.81941 +    pTab->keyConf = (u8)onError;
 1.81942 +    assert( autoInc==0 || autoInc==1 );
 1.81943 +    pTab->tabFlags |= autoInc*TF_Autoincrement;
 1.81944 +  }else if( autoInc ){
 1.81945 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.81946 +    sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
 1.81947 +       "INTEGER PRIMARY KEY");
 1.81948 +#endif
 1.81949 +  }else{
 1.81950 +    Index *p;
 1.81951 +    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
 1.81952 +    if( p ){
 1.81953 +      p->autoIndex = 2;
 1.81954 +    }
 1.81955 +    pList = 0;
 1.81956 +  }
 1.81957 +
 1.81958 +primary_key_exit:
 1.81959 +  sqlite3ExprListDelete(pParse->db, pList);
 1.81960 +  return;
 1.81961 +}
 1.81962 +
 1.81963 +/*
 1.81964 +** Add a new CHECK constraint to the table currently under construction.
 1.81965 +*/
 1.81966 +SQLITE_PRIVATE void sqlite3AddCheckConstraint(
 1.81967 +  Parse *pParse,    /* Parsing context */
 1.81968 +  Expr *pCheckExpr  /* The check expression */
 1.81969 +){
 1.81970 +#ifndef SQLITE_OMIT_CHECK
 1.81971 +  Table *pTab = pParse->pNewTable;
 1.81972 +  if( pTab && !IN_DECLARE_VTAB ){
 1.81973 +    pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
 1.81974 +    if( pParse->constraintName.n ){
 1.81975 +      sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
 1.81976 +    }
 1.81977 +  }else
 1.81978 +#endif
 1.81979 +  {
 1.81980 +    sqlite3ExprDelete(pParse->db, pCheckExpr);
 1.81981 +  }
 1.81982 +}
 1.81983 +
 1.81984 +/*
 1.81985 +** Set the collation function of the most recently parsed table column
 1.81986 +** to the CollSeq given.
 1.81987 +*/
 1.81988 +SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
 1.81989 +  Table *p;
 1.81990 +  int i;
 1.81991 +  char *zColl;              /* Dequoted name of collation sequence */
 1.81992 +  sqlite3 *db;
 1.81993 +
 1.81994 +  if( (p = pParse->pNewTable)==0 ) return;
 1.81995 +  i = p->nCol-1;
 1.81996 +  db = pParse->db;
 1.81997 +  zColl = sqlite3NameFromToken(db, pToken);
 1.81998 +  if( !zColl ) return;
 1.81999 +
 1.82000 +  if( sqlite3LocateCollSeq(pParse, zColl) ){
 1.82001 +    Index *pIdx;
 1.82002 +    p->aCol[i].zColl = zColl;
 1.82003 +  
 1.82004 +    /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
 1.82005 +    ** then an index may have been created on this column before the
 1.82006 +    ** collation type was added. Correct this if it is the case.
 1.82007 +    */
 1.82008 +    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
 1.82009 +      assert( pIdx->nColumn==1 );
 1.82010 +      if( pIdx->aiColumn[0]==i ){
 1.82011 +        pIdx->azColl[0] = p->aCol[i].zColl;
 1.82012 +      }
 1.82013 +    }
 1.82014 +  }else{
 1.82015 +    sqlite3DbFree(db, zColl);
 1.82016 +  }
 1.82017 +}
 1.82018 +
 1.82019 +/*
 1.82020 +** This function returns the collation sequence for database native text
 1.82021 +** encoding identified by the string zName, length nName.
 1.82022 +**
 1.82023 +** If the requested collation sequence is not available, or not available
 1.82024 +** in the database native encoding, the collation factory is invoked to
 1.82025 +** request it. If the collation factory does not supply such a sequence,
 1.82026 +** and the sequence is available in another text encoding, then that is
 1.82027 +** returned instead.
 1.82028 +**
 1.82029 +** If no versions of the requested collations sequence are available, or
 1.82030 +** another error occurs, NULL is returned and an error message written into
 1.82031 +** pParse.
 1.82032 +**
 1.82033 +** This routine is a wrapper around sqlite3FindCollSeq().  This routine
 1.82034 +** invokes the collation factory if the named collation cannot be found
 1.82035 +** and generates an error message.
 1.82036 +**
 1.82037 +** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
 1.82038 +*/
 1.82039 +SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
 1.82040 +  sqlite3 *db = pParse->db;
 1.82041 +  u8 enc = ENC(db);
 1.82042 +  u8 initbusy = db->init.busy;
 1.82043 +  CollSeq *pColl;
 1.82044 +
 1.82045 +  pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
 1.82046 +  if( !initbusy && (!pColl || !pColl->xCmp) ){
 1.82047 +    pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
 1.82048 +  }
 1.82049 +
 1.82050 +  return pColl;
 1.82051 +}
 1.82052 +
 1.82053 +
 1.82054 +/*
 1.82055 +** Generate code that will increment the schema cookie.
 1.82056 +**
 1.82057 +** The schema cookie is used to determine when the schema for the
 1.82058 +** database changes.  After each schema change, the cookie value
 1.82059 +** changes.  When a process first reads the schema it records the
 1.82060 +** cookie.  Thereafter, whenever it goes to access the database,
 1.82061 +** it checks the cookie to make sure the schema has not changed
 1.82062 +** since it was last read.
 1.82063 +**
 1.82064 +** This plan is not completely bullet-proof.  It is possible for
 1.82065 +** the schema to change multiple times and for the cookie to be
 1.82066 +** set back to prior value.  But schema changes are infrequent
 1.82067 +** and the probability of hitting the same cookie value is only
 1.82068 +** 1 chance in 2^32.  So we're safe enough.
 1.82069 +*/
 1.82070 +SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
 1.82071 +  int r1 = sqlite3GetTempReg(pParse);
 1.82072 +  sqlite3 *db = pParse->db;
 1.82073 +  Vdbe *v = pParse->pVdbe;
 1.82074 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.82075 +  sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
 1.82076 +  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
 1.82077 +  sqlite3ReleaseTempReg(pParse, r1);
 1.82078 +}
 1.82079 +
 1.82080 +/*
 1.82081 +** Measure the number of characters needed to output the given
 1.82082 +** identifier.  The number returned includes any quotes used
 1.82083 +** but does not include the null terminator.
 1.82084 +**
 1.82085 +** The estimate is conservative.  It might be larger that what is
 1.82086 +** really needed.
 1.82087 +*/
 1.82088 +static int identLength(const char *z){
 1.82089 +  int n;
 1.82090 +  for(n=0; *z; n++, z++){
 1.82091 +    if( *z=='"' ){ n++; }
 1.82092 +  }
 1.82093 +  return n + 2;
 1.82094 +}
 1.82095 +
 1.82096 +/*
 1.82097 +** The first parameter is a pointer to an output buffer. The second 
 1.82098 +** parameter is a pointer to an integer that contains the offset at
 1.82099 +** which to write into the output buffer. This function copies the
 1.82100 +** nul-terminated string pointed to by the third parameter, zSignedIdent,
 1.82101 +** to the specified offset in the buffer and updates *pIdx to refer
 1.82102 +** to the first byte after the last byte written before returning.
 1.82103 +** 
 1.82104 +** If the string zSignedIdent consists entirely of alpha-numeric
 1.82105 +** characters, does not begin with a digit and is not an SQL keyword,
 1.82106 +** then it is copied to the output buffer exactly as it is. Otherwise,
 1.82107 +** it is quoted using double-quotes.
 1.82108 +*/
 1.82109 +static void identPut(char *z, int *pIdx, char *zSignedIdent){
 1.82110 +  unsigned char *zIdent = (unsigned char*)zSignedIdent;
 1.82111 +  int i, j, needQuote;
 1.82112 +  i = *pIdx;
 1.82113 +
 1.82114 +  for(j=0; zIdent[j]; j++){
 1.82115 +    if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
 1.82116 +  }
 1.82117 +  needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
 1.82118 +  if( !needQuote ){
 1.82119 +    needQuote = zIdent[j];
 1.82120 +  }
 1.82121 +
 1.82122 +  if( needQuote ) z[i++] = '"';
 1.82123 +  for(j=0; zIdent[j]; j++){
 1.82124 +    z[i++] = zIdent[j];
 1.82125 +    if( zIdent[j]=='"' ) z[i++] = '"';
 1.82126 +  }
 1.82127 +  if( needQuote ) z[i++] = '"';
 1.82128 +  z[i] = 0;
 1.82129 +  *pIdx = i;
 1.82130 +}
 1.82131 +
 1.82132 +/*
 1.82133 +** Generate a CREATE TABLE statement appropriate for the given
 1.82134 +** table.  Memory to hold the text of the statement is obtained
 1.82135 +** from sqliteMalloc() and must be freed by the calling function.
 1.82136 +*/
 1.82137 +static char *createTableStmt(sqlite3 *db, Table *p){
 1.82138 +  int i, k, n;
 1.82139 +  char *zStmt;
 1.82140 +  char *zSep, *zSep2, *zEnd;
 1.82141 +  Column *pCol;
 1.82142 +  n = 0;
 1.82143 +  for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
 1.82144 +    n += identLength(pCol->zName) + 5;
 1.82145 +  }
 1.82146 +  n += identLength(p->zName);
 1.82147 +  if( n<50 ){ 
 1.82148 +    zSep = "";
 1.82149 +    zSep2 = ",";
 1.82150 +    zEnd = ")";
 1.82151 +  }else{
 1.82152 +    zSep = "\n  ";
 1.82153 +    zSep2 = ",\n  ";
 1.82154 +    zEnd = "\n)";
 1.82155 +  }
 1.82156 +  n += 35 + 6*p->nCol;
 1.82157 +  zStmt = sqlite3DbMallocRaw(0, n);
 1.82158 +  if( zStmt==0 ){
 1.82159 +    db->mallocFailed = 1;
 1.82160 +    return 0;
 1.82161 +  }
 1.82162 +  sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
 1.82163 +  k = sqlite3Strlen30(zStmt);
 1.82164 +  identPut(zStmt, &k, p->zName);
 1.82165 +  zStmt[k++] = '(';
 1.82166 +  for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
 1.82167 +    static const char * const azType[] = {
 1.82168 +        /* SQLITE_AFF_TEXT    */ " TEXT",
 1.82169 +        /* SQLITE_AFF_NONE    */ "",
 1.82170 +        /* SQLITE_AFF_NUMERIC */ " NUM",
 1.82171 +        /* SQLITE_AFF_INTEGER */ " INT",
 1.82172 +        /* SQLITE_AFF_REAL    */ " REAL"
 1.82173 +    };
 1.82174 +    int len;
 1.82175 +    const char *zType;
 1.82176 +
 1.82177 +    sqlite3_snprintf(n-k, &zStmt[k], zSep);
 1.82178 +    k += sqlite3Strlen30(&zStmt[k]);
 1.82179 +    zSep = zSep2;
 1.82180 +    identPut(zStmt, &k, pCol->zName);
 1.82181 +    assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
 1.82182 +    assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
 1.82183 +    testcase( pCol->affinity==SQLITE_AFF_TEXT );
 1.82184 +    testcase( pCol->affinity==SQLITE_AFF_NONE );
 1.82185 +    testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
 1.82186 +    testcase( pCol->affinity==SQLITE_AFF_INTEGER );
 1.82187 +    testcase( pCol->affinity==SQLITE_AFF_REAL );
 1.82188 +    
 1.82189 +    zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
 1.82190 +    len = sqlite3Strlen30(zType);
 1.82191 +    assert( pCol->affinity==SQLITE_AFF_NONE 
 1.82192 +            || pCol->affinity==sqlite3AffinityType(zType) );
 1.82193 +    memcpy(&zStmt[k], zType, len);
 1.82194 +    k += len;
 1.82195 +    assert( k<=n );
 1.82196 +  }
 1.82197 +  sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
 1.82198 +  return zStmt;
 1.82199 +}
 1.82200 +
 1.82201 +/*
 1.82202 +** This routine is called to report the final ")" that terminates
 1.82203 +** a CREATE TABLE statement.
 1.82204 +**
 1.82205 +** The table structure that other action routines have been building
 1.82206 +** is added to the internal hash tables, assuming no errors have
 1.82207 +** occurred.
 1.82208 +**
 1.82209 +** An entry for the table is made in the master table on disk, unless
 1.82210 +** this is a temporary table or db->init.busy==1.  When db->init.busy==1
 1.82211 +** it means we are reading the sqlite_master table because we just
 1.82212 +** connected to the database or because the sqlite_master table has
 1.82213 +** recently changed, so the entry for this table already exists in
 1.82214 +** the sqlite_master table.  We do not want to create it again.
 1.82215 +**
 1.82216 +** If the pSelect argument is not NULL, it means that this routine
 1.82217 +** was called to create a table generated from a 
 1.82218 +** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
 1.82219 +** the new table will match the result set of the SELECT.
 1.82220 +*/
 1.82221 +SQLITE_PRIVATE void sqlite3EndTable(
 1.82222 +  Parse *pParse,          /* Parse context */
 1.82223 +  Token *pCons,           /* The ',' token after the last column defn. */
 1.82224 +  Token *pEnd,            /* The final ')' token in the CREATE TABLE */
 1.82225 +  Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
 1.82226 +){
 1.82227 +  Table *p;
 1.82228 +  sqlite3 *db = pParse->db;
 1.82229 +  int iDb;
 1.82230 +
 1.82231 +  if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
 1.82232 +    return;
 1.82233 +  }
 1.82234 +  p = pParse->pNewTable;
 1.82235 +  if( p==0 ) return;
 1.82236 +
 1.82237 +  assert( !db->init.busy || !pSelect );
 1.82238 +
 1.82239 +  iDb = sqlite3SchemaToIndex(db, p->pSchema);
 1.82240 +
 1.82241 +#ifndef SQLITE_OMIT_CHECK
 1.82242 +  /* Resolve names in all CHECK constraint expressions.
 1.82243 +  */
 1.82244 +  if( p->pCheck ){
 1.82245 +    SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
 1.82246 +    NameContext sNC;                /* Name context for pParse->pNewTable */
 1.82247 +    ExprList *pList;                /* List of all CHECK constraints */
 1.82248 +    int i;                          /* Loop counter */
 1.82249 +
 1.82250 +    memset(&sNC, 0, sizeof(sNC));
 1.82251 +    memset(&sSrc, 0, sizeof(sSrc));
 1.82252 +    sSrc.nSrc = 1;
 1.82253 +    sSrc.a[0].zName = p->zName;
 1.82254 +    sSrc.a[0].pTab = p;
 1.82255 +    sSrc.a[0].iCursor = -1;
 1.82256 +    sNC.pParse = pParse;
 1.82257 +    sNC.pSrcList = &sSrc;
 1.82258 +    sNC.ncFlags = NC_IsCheck;
 1.82259 +    pList = p->pCheck;
 1.82260 +    for(i=0; i<pList->nExpr; i++){
 1.82261 +      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
 1.82262 +        return;
 1.82263 +      }
 1.82264 +    }
 1.82265 +  }
 1.82266 +#endif /* !defined(SQLITE_OMIT_CHECK) */
 1.82267 +
 1.82268 +  /* If the db->init.busy is 1 it means we are reading the SQL off the
 1.82269 +  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
 1.82270 +  ** So do not write to the disk again.  Extract the root page number
 1.82271 +  ** for the table from the db->init.newTnum field.  (The page number
 1.82272 +  ** should have been put there by the sqliteOpenCb routine.)
 1.82273 +  */
 1.82274 +  if( db->init.busy ){
 1.82275 +    p->tnum = db->init.newTnum;
 1.82276 +  }
 1.82277 +
 1.82278 +  /* If not initializing, then create a record for the new table
 1.82279 +  ** in the SQLITE_MASTER table of the database.
 1.82280 +  **
 1.82281 +  ** If this is a TEMPORARY table, write the entry into the auxiliary
 1.82282 +  ** file instead of into the main database file.
 1.82283 +  */
 1.82284 +  if( !db->init.busy ){
 1.82285 +    int n;
 1.82286 +    Vdbe *v;
 1.82287 +    char *zType;    /* "view" or "table" */
 1.82288 +    char *zType2;   /* "VIEW" or "TABLE" */
 1.82289 +    char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
 1.82290 +
 1.82291 +    v = sqlite3GetVdbe(pParse);
 1.82292 +    if( NEVER(v==0) ) return;
 1.82293 +
 1.82294 +    sqlite3VdbeAddOp1(v, OP_Close, 0);
 1.82295 +
 1.82296 +    /* 
 1.82297 +    ** Initialize zType for the new view or table.
 1.82298 +    */
 1.82299 +    if( p->pSelect==0 ){
 1.82300 +      /* A regular table */
 1.82301 +      zType = "table";
 1.82302 +      zType2 = "TABLE";
 1.82303 +#ifndef SQLITE_OMIT_VIEW
 1.82304 +    }else{
 1.82305 +      /* A view */
 1.82306 +      zType = "view";
 1.82307 +      zType2 = "VIEW";
 1.82308 +#endif
 1.82309 +    }
 1.82310 +
 1.82311 +    /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
 1.82312 +    ** statement to populate the new table. The root-page number for the
 1.82313 +    ** new table is in register pParse->regRoot.
 1.82314 +    **
 1.82315 +    ** Once the SELECT has been coded by sqlite3Select(), it is in a
 1.82316 +    ** suitable state to query for the column names and types to be used
 1.82317 +    ** by the new table.
 1.82318 +    **
 1.82319 +    ** A shared-cache write-lock is not required to write to the new table,
 1.82320 +    ** as a schema-lock must have already been obtained to create it. Since
 1.82321 +    ** a schema-lock excludes all other database users, the write-lock would
 1.82322 +    ** be redundant.
 1.82323 +    */
 1.82324 +    if( pSelect ){
 1.82325 +      SelectDest dest;
 1.82326 +      Table *pSelTab;
 1.82327 +
 1.82328 +      assert(pParse->nTab==1);
 1.82329 +      sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
 1.82330 +      sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
 1.82331 +      pParse->nTab = 2;
 1.82332 +      sqlite3SelectDestInit(&dest, SRT_Table, 1);
 1.82333 +      sqlite3Select(pParse, pSelect, &dest);
 1.82334 +      sqlite3VdbeAddOp1(v, OP_Close, 1);
 1.82335 +      if( pParse->nErr==0 ){
 1.82336 +        pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
 1.82337 +        if( pSelTab==0 ) return;
 1.82338 +        assert( p->aCol==0 );
 1.82339 +        p->nCol = pSelTab->nCol;
 1.82340 +        p->aCol = pSelTab->aCol;
 1.82341 +        pSelTab->nCol = 0;
 1.82342 +        pSelTab->aCol = 0;
 1.82343 +        sqlite3DeleteTable(db, pSelTab);
 1.82344 +      }
 1.82345 +    }
 1.82346 +
 1.82347 +    /* Compute the complete text of the CREATE statement */
 1.82348 +    if( pSelect ){
 1.82349 +      zStmt = createTableStmt(db, p);
 1.82350 +    }else{
 1.82351 +      n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
 1.82352 +      zStmt = sqlite3MPrintf(db, 
 1.82353 +          "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
 1.82354 +      );
 1.82355 +    }
 1.82356 +
 1.82357 +    /* A slot for the record has already been allocated in the 
 1.82358 +    ** SQLITE_MASTER table.  We just need to update that slot with all
 1.82359 +    ** the information we've collected.
 1.82360 +    */
 1.82361 +    sqlite3NestedParse(pParse,
 1.82362 +      "UPDATE %Q.%s "
 1.82363 +         "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
 1.82364 +       "WHERE rowid=#%d",
 1.82365 +      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 1.82366 +      zType,
 1.82367 +      p->zName,
 1.82368 +      p->zName,
 1.82369 +      pParse->regRoot,
 1.82370 +      zStmt,
 1.82371 +      pParse->regRowid
 1.82372 +    );
 1.82373 +    sqlite3DbFree(db, zStmt);
 1.82374 +    sqlite3ChangeCookie(pParse, iDb);
 1.82375 +
 1.82376 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.82377 +    /* Check to see if we need to create an sqlite_sequence table for
 1.82378 +    ** keeping track of autoincrement keys.
 1.82379 +    */
 1.82380 +    if( p->tabFlags & TF_Autoincrement ){
 1.82381 +      Db *pDb = &db->aDb[iDb];
 1.82382 +      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.82383 +      if( pDb->pSchema->pSeqTab==0 ){
 1.82384 +        sqlite3NestedParse(pParse,
 1.82385 +          "CREATE TABLE %Q.sqlite_sequence(name,seq)",
 1.82386 +          pDb->zName
 1.82387 +        );
 1.82388 +      }
 1.82389 +    }
 1.82390 +#endif
 1.82391 +
 1.82392 +    /* Reparse everything to update our internal data structures */
 1.82393 +    sqlite3VdbeAddParseSchemaOp(v, iDb,
 1.82394 +               sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
 1.82395 +  }
 1.82396 +
 1.82397 +
 1.82398 +  /* Add the table to the in-memory representation of the database.
 1.82399 +  */
 1.82400 +  if( db->init.busy ){
 1.82401 +    Table *pOld;
 1.82402 +    Schema *pSchema = p->pSchema;
 1.82403 +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.82404 +    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
 1.82405 +                             sqlite3Strlen30(p->zName),p);
 1.82406 +    if( pOld ){
 1.82407 +      assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
 1.82408 +      db->mallocFailed = 1;
 1.82409 +      return;
 1.82410 +    }
 1.82411 +    pParse->pNewTable = 0;
 1.82412 +    db->flags |= SQLITE_InternChanges;
 1.82413 +
 1.82414 +#ifndef SQLITE_OMIT_ALTERTABLE
 1.82415 +    if( !p->pSelect ){
 1.82416 +      const char *zName = (const char *)pParse->sNameToken.z;
 1.82417 +      int nName;
 1.82418 +      assert( !pSelect && pCons && pEnd );
 1.82419 +      if( pCons->z==0 ){
 1.82420 +        pCons = pEnd;
 1.82421 +      }
 1.82422 +      nName = (int)((const char *)pCons->z - zName);
 1.82423 +      p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
 1.82424 +    }
 1.82425 +#endif
 1.82426 +  }
 1.82427 +}
 1.82428 +
 1.82429 +#ifndef SQLITE_OMIT_VIEW
 1.82430 +/*
 1.82431 +** The parser calls this routine in order to create a new VIEW
 1.82432 +*/
 1.82433 +SQLITE_PRIVATE void sqlite3CreateView(
 1.82434 +  Parse *pParse,     /* The parsing context */
 1.82435 +  Token *pBegin,     /* The CREATE token that begins the statement */
 1.82436 +  Token *pName1,     /* The token that holds the name of the view */
 1.82437 +  Token *pName2,     /* The token that holds the name of the view */
 1.82438 +  Select *pSelect,   /* A SELECT statement that will become the new view */
 1.82439 +  int isTemp,        /* TRUE for a TEMPORARY view */
 1.82440 +  int noErr          /* Suppress error messages if VIEW already exists */
 1.82441 +){
 1.82442 +  Table *p;
 1.82443 +  int n;
 1.82444 +  const char *z;
 1.82445 +  Token sEnd;
 1.82446 +  DbFixer sFix;
 1.82447 +  Token *pName = 0;
 1.82448 +  int iDb;
 1.82449 +  sqlite3 *db = pParse->db;
 1.82450 +
 1.82451 +  if( pParse->nVar>0 ){
 1.82452 +    sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
 1.82453 +    sqlite3SelectDelete(db, pSelect);
 1.82454 +    return;
 1.82455 +  }
 1.82456 +  sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
 1.82457 +  p = pParse->pNewTable;
 1.82458 +  if( p==0 || pParse->nErr ){
 1.82459 +    sqlite3SelectDelete(db, pSelect);
 1.82460 +    return;
 1.82461 +  }
 1.82462 +  sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 1.82463 +  iDb = sqlite3SchemaToIndex(db, p->pSchema);
 1.82464 +  if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
 1.82465 +    && sqlite3FixSelect(&sFix, pSelect)
 1.82466 +  ){
 1.82467 +    sqlite3SelectDelete(db, pSelect);
 1.82468 +    return;
 1.82469 +  }
 1.82470 +
 1.82471 +  /* Make a copy of the entire SELECT statement that defines the view.
 1.82472 +  ** This will force all the Expr.token.z values to be dynamically
 1.82473 +  ** allocated rather than point to the input string - which means that
 1.82474 +  ** they will persist after the current sqlite3_exec() call returns.
 1.82475 +  */
 1.82476 +  p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 1.82477 +  sqlite3SelectDelete(db, pSelect);
 1.82478 +  if( db->mallocFailed ){
 1.82479 +    return;
 1.82480 +  }
 1.82481 +  if( !db->init.busy ){
 1.82482 +    sqlite3ViewGetColumnNames(pParse, p);
 1.82483 +  }
 1.82484 +
 1.82485 +  /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
 1.82486 +  ** the end.
 1.82487 +  */
 1.82488 +  sEnd = pParse->sLastToken;
 1.82489 +  if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
 1.82490 +    sEnd.z += sEnd.n;
 1.82491 +  }
 1.82492 +  sEnd.n = 0;
 1.82493 +  n = (int)(sEnd.z - pBegin->z);
 1.82494 +  z = pBegin->z;
 1.82495 +  while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
 1.82496 +  sEnd.z = &z[n-1];
 1.82497 +  sEnd.n = 1;
 1.82498 +
 1.82499 +  /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
 1.82500 +  sqlite3EndTable(pParse, 0, &sEnd, 0);
 1.82501 +  return;
 1.82502 +}
 1.82503 +#endif /* SQLITE_OMIT_VIEW */
 1.82504 +
 1.82505 +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 1.82506 +/*
 1.82507 +** The Table structure pTable is really a VIEW.  Fill in the names of
 1.82508 +** the columns of the view in the pTable structure.  Return the number
 1.82509 +** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
 1.82510 +*/
 1.82511 +SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
 1.82512 +  Table *pSelTab;   /* A fake table from which we get the result set */
 1.82513 +  Select *pSel;     /* Copy of the SELECT that implements the view */
 1.82514 +  int nErr = 0;     /* Number of errors encountered */
 1.82515 +  int n;            /* Temporarily holds the number of cursors assigned */
 1.82516 +  sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
 1.82517 +  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
 1.82518 +
 1.82519 +  assert( pTable );
 1.82520 +
 1.82521 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.82522 +  if( sqlite3VtabCallConnect(pParse, pTable) ){
 1.82523 +    return SQLITE_ERROR;
 1.82524 +  }
 1.82525 +  if( IsVirtual(pTable) ) return 0;
 1.82526 +#endif
 1.82527 +
 1.82528 +#ifndef SQLITE_OMIT_VIEW
 1.82529 +  /* A positive nCol means the columns names for this view are
 1.82530 +  ** already known.
 1.82531 +  */
 1.82532 +  if( pTable->nCol>0 ) return 0;
 1.82533 +
 1.82534 +  /* A negative nCol is a special marker meaning that we are currently
 1.82535 +  ** trying to compute the column names.  If we enter this routine with
 1.82536 +  ** a negative nCol, it means two or more views form a loop, like this:
 1.82537 +  **
 1.82538 +  **     CREATE VIEW one AS SELECT * FROM two;
 1.82539 +  **     CREATE VIEW two AS SELECT * FROM one;
 1.82540 +  **
 1.82541 +  ** Actually, the error above is now caught prior to reaching this point.
 1.82542 +  ** But the following test is still important as it does come up
 1.82543 +  ** in the following:
 1.82544 +  ** 
 1.82545 +  **     CREATE TABLE main.ex1(a);
 1.82546 +  **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
 1.82547 +  **     SELECT * FROM temp.ex1;
 1.82548 +  */
 1.82549 +  if( pTable->nCol<0 ){
 1.82550 +    sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
 1.82551 +    return 1;
 1.82552 +  }
 1.82553 +  assert( pTable->nCol>=0 );
 1.82554 +
 1.82555 +  /* If we get this far, it means we need to compute the table names.
 1.82556 +  ** Note that the call to sqlite3ResultSetOfSelect() will expand any
 1.82557 +  ** "*" elements in the results set of the view and will assign cursors
 1.82558 +  ** to the elements of the FROM clause.  But we do not want these changes
 1.82559 +  ** to be permanent.  So the computation is done on a copy of the SELECT
 1.82560 +  ** statement that defines the view.
 1.82561 +  */
 1.82562 +  assert( pTable->pSelect );
 1.82563 +  pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
 1.82564 +  if( pSel ){
 1.82565 +    u8 enableLookaside = db->lookaside.bEnabled;
 1.82566 +    n = pParse->nTab;
 1.82567 +    sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
 1.82568 +    pTable->nCol = -1;
 1.82569 +    db->lookaside.bEnabled = 0;
 1.82570 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.82571 +    xAuth = db->xAuth;
 1.82572 +    db->xAuth = 0;
 1.82573 +    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
 1.82574 +    db->xAuth = xAuth;
 1.82575 +#else
 1.82576 +    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
 1.82577 +#endif
 1.82578 +    db->lookaside.bEnabled = enableLookaside;
 1.82579 +    pParse->nTab = n;
 1.82580 +    if( pSelTab ){
 1.82581 +      assert( pTable->aCol==0 );
 1.82582 +      pTable->nCol = pSelTab->nCol;
 1.82583 +      pTable->aCol = pSelTab->aCol;
 1.82584 +      pSelTab->nCol = 0;
 1.82585 +      pSelTab->aCol = 0;
 1.82586 +      sqlite3DeleteTable(db, pSelTab);
 1.82587 +      assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
 1.82588 +      pTable->pSchema->flags |= DB_UnresetViews;
 1.82589 +    }else{
 1.82590 +      pTable->nCol = 0;
 1.82591 +      nErr++;
 1.82592 +    }
 1.82593 +    sqlite3SelectDelete(db, pSel);
 1.82594 +  } else {
 1.82595 +    nErr++;
 1.82596 +  }
 1.82597 +#endif /* SQLITE_OMIT_VIEW */
 1.82598 +  return nErr;  
 1.82599 +}
 1.82600 +#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
 1.82601 +
 1.82602 +#ifndef SQLITE_OMIT_VIEW
 1.82603 +/*
 1.82604 +** Clear the column names from every VIEW in database idx.
 1.82605 +*/
 1.82606 +static void sqliteViewResetAll(sqlite3 *db, int idx){
 1.82607 +  HashElem *i;
 1.82608 +  assert( sqlite3SchemaMutexHeld(db, idx, 0) );
 1.82609 +  if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
 1.82610 +  for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
 1.82611 +    Table *pTab = sqliteHashData(i);
 1.82612 +    if( pTab->pSelect ){
 1.82613 +      sqliteDeleteColumnNames(db, pTab);
 1.82614 +      pTab->aCol = 0;
 1.82615 +      pTab->nCol = 0;
 1.82616 +    }
 1.82617 +  }
 1.82618 +  DbClearProperty(db, idx, DB_UnresetViews);
 1.82619 +}
 1.82620 +#else
 1.82621 +# define sqliteViewResetAll(A,B)
 1.82622 +#endif /* SQLITE_OMIT_VIEW */
 1.82623 +
 1.82624 +/*
 1.82625 +** This function is called by the VDBE to adjust the internal schema
 1.82626 +** used by SQLite when the btree layer moves a table root page. The
 1.82627 +** root-page of a table or index in database iDb has changed from iFrom
 1.82628 +** to iTo.
 1.82629 +**
 1.82630 +** Ticket #1728:  The symbol table might still contain information
 1.82631 +** on tables and/or indices that are the process of being deleted.
 1.82632 +** If you are unlucky, one of those deleted indices or tables might
 1.82633 +** have the same rootpage number as the real table or index that is
 1.82634 +** being moved.  So we cannot stop searching after the first match 
 1.82635 +** because the first match might be for one of the deleted indices
 1.82636 +** or tables and not the table/index that is actually being moved.
 1.82637 +** We must continue looping until all tables and indices with
 1.82638 +** rootpage==iFrom have been converted to have a rootpage of iTo
 1.82639 +** in order to be certain that we got the right one.
 1.82640 +*/
 1.82641 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.82642 +SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
 1.82643 +  HashElem *pElem;
 1.82644 +  Hash *pHash;
 1.82645 +  Db *pDb;
 1.82646 +
 1.82647 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.82648 +  pDb = &db->aDb[iDb];
 1.82649 +  pHash = &pDb->pSchema->tblHash;
 1.82650 +  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
 1.82651 +    Table *pTab = sqliteHashData(pElem);
 1.82652 +    if( pTab->tnum==iFrom ){
 1.82653 +      pTab->tnum = iTo;
 1.82654 +    }
 1.82655 +  }
 1.82656 +  pHash = &pDb->pSchema->idxHash;
 1.82657 +  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
 1.82658 +    Index *pIdx = sqliteHashData(pElem);
 1.82659 +    if( pIdx->tnum==iFrom ){
 1.82660 +      pIdx->tnum = iTo;
 1.82661 +    }
 1.82662 +  }
 1.82663 +}
 1.82664 +#endif
 1.82665 +
 1.82666 +/*
 1.82667 +** Write code to erase the table with root-page iTable from database iDb.
 1.82668 +** Also write code to modify the sqlite_master table and internal schema
 1.82669 +** if a root-page of another table is moved by the btree-layer whilst
 1.82670 +** erasing iTable (this can happen with an auto-vacuum database).
 1.82671 +*/ 
 1.82672 +static void destroyRootPage(Parse *pParse, int iTable, int iDb){
 1.82673 +  Vdbe *v = sqlite3GetVdbe(pParse);
 1.82674 +  int r1 = sqlite3GetTempReg(pParse);
 1.82675 +  sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
 1.82676 +  sqlite3MayAbort(pParse);
 1.82677 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.82678 +  /* OP_Destroy stores an in integer r1. If this integer
 1.82679 +  ** is non-zero, then it is the root page number of a table moved to
 1.82680 +  ** location iTable. The following code modifies the sqlite_master table to
 1.82681 +  ** reflect this.
 1.82682 +  **
 1.82683 +  ** The "#NNN" in the SQL is a special constant that means whatever value
 1.82684 +  ** is in register NNN.  See grammar rules associated with the TK_REGISTER
 1.82685 +  ** token for additional information.
 1.82686 +  */
 1.82687 +  sqlite3NestedParse(pParse, 
 1.82688 +     "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
 1.82689 +     pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
 1.82690 +#endif
 1.82691 +  sqlite3ReleaseTempReg(pParse, r1);
 1.82692 +}
 1.82693 +
 1.82694 +/*
 1.82695 +** Write VDBE code to erase table pTab and all associated indices on disk.
 1.82696 +** Code to update the sqlite_master tables and internal schema definitions
 1.82697 +** in case a root-page belonging to another table is moved by the btree layer
 1.82698 +** is also added (this can happen with an auto-vacuum database).
 1.82699 +*/
 1.82700 +static void destroyTable(Parse *pParse, Table *pTab){
 1.82701 +#ifdef SQLITE_OMIT_AUTOVACUUM
 1.82702 +  Index *pIdx;
 1.82703 +  int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.82704 +  destroyRootPage(pParse, pTab->tnum, iDb);
 1.82705 +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.82706 +    destroyRootPage(pParse, pIdx->tnum, iDb);
 1.82707 +  }
 1.82708 +#else
 1.82709 +  /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
 1.82710 +  ** is not defined), then it is important to call OP_Destroy on the
 1.82711 +  ** table and index root-pages in order, starting with the numerically 
 1.82712 +  ** largest root-page number. This guarantees that none of the root-pages
 1.82713 +  ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
 1.82714 +  ** following were coded:
 1.82715 +  **
 1.82716 +  ** OP_Destroy 4 0
 1.82717 +  ** ...
 1.82718 +  ** OP_Destroy 5 0
 1.82719 +  **
 1.82720 +  ** and root page 5 happened to be the largest root-page number in the
 1.82721 +  ** database, then root page 5 would be moved to page 4 by the 
 1.82722 +  ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
 1.82723 +  ** a free-list page.
 1.82724 +  */
 1.82725 +  int iTab = pTab->tnum;
 1.82726 +  int iDestroyed = 0;
 1.82727 +
 1.82728 +  while( 1 ){
 1.82729 +    Index *pIdx;
 1.82730 +    int iLargest = 0;
 1.82731 +
 1.82732 +    if( iDestroyed==0 || iTab<iDestroyed ){
 1.82733 +      iLargest = iTab;
 1.82734 +    }
 1.82735 +    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.82736 +      int iIdx = pIdx->tnum;
 1.82737 +      assert( pIdx->pSchema==pTab->pSchema );
 1.82738 +      if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
 1.82739 +        iLargest = iIdx;
 1.82740 +      }
 1.82741 +    }
 1.82742 +    if( iLargest==0 ){
 1.82743 +      return;
 1.82744 +    }else{
 1.82745 +      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.82746 +      assert( iDb>=0 && iDb<pParse->db->nDb );
 1.82747 +      destroyRootPage(pParse, iLargest, iDb);
 1.82748 +      iDestroyed = iLargest;
 1.82749 +    }
 1.82750 +  }
 1.82751 +#endif
 1.82752 +}
 1.82753 +
 1.82754 +/*
 1.82755 +** Remove entries from the sqlite_statN tables (for N in (1,2,3))
 1.82756 +** after a DROP INDEX or DROP TABLE command.
 1.82757 +*/
 1.82758 +static void sqlite3ClearStatTables(
 1.82759 +  Parse *pParse,         /* The parsing context */
 1.82760 +  int iDb,               /* The database number */
 1.82761 +  const char *zType,     /* "idx" or "tbl" */
 1.82762 +  const char *zName      /* Name of index or table */
 1.82763 +){
 1.82764 +  int i;
 1.82765 +  const char *zDbName = pParse->db->aDb[iDb].zName;
 1.82766 +  for(i=1; i<=3; i++){
 1.82767 +    char zTab[24];
 1.82768 +    sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
 1.82769 +    if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
 1.82770 +      sqlite3NestedParse(pParse,
 1.82771 +        "DELETE FROM %Q.%s WHERE %s=%Q",
 1.82772 +        zDbName, zTab, zType, zName
 1.82773 +      );
 1.82774 +    }
 1.82775 +  }
 1.82776 +}
 1.82777 +
 1.82778 +/*
 1.82779 +** Generate code to drop a table.
 1.82780 +*/
 1.82781 +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
 1.82782 +  Vdbe *v;
 1.82783 +  sqlite3 *db = pParse->db;
 1.82784 +  Trigger *pTrigger;
 1.82785 +  Db *pDb = &db->aDb[iDb];
 1.82786 +
 1.82787 +  v = sqlite3GetVdbe(pParse);
 1.82788 +  assert( v!=0 );
 1.82789 +  sqlite3BeginWriteOperation(pParse, 1, iDb);
 1.82790 +
 1.82791 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.82792 +  if( IsVirtual(pTab) ){
 1.82793 +    sqlite3VdbeAddOp0(v, OP_VBegin);
 1.82794 +  }
 1.82795 +#endif
 1.82796 +
 1.82797 +  /* Drop all triggers associated with the table being dropped. Code
 1.82798 +  ** is generated to remove entries from sqlite_master and/or
 1.82799 +  ** sqlite_temp_master if required.
 1.82800 +  */
 1.82801 +  pTrigger = sqlite3TriggerList(pParse, pTab);
 1.82802 +  while( pTrigger ){
 1.82803 +    assert( pTrigger->pSchema==pTab->pSchema || 
 1.82804 +        pTrigger->pSchema==db->aDb[1].pSchema );
 1.82805 +    sqlite3DropTriggerPtr(pParse, pTrigger);
 1.82806 +    pTrigger = pTrigger->pNext;
 1.82807 +  }
 1.82808 +
 1.82809 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.82810 +  /* Remove any entries of the sqlite_sequence table associated with
 1.82811 +  ** the table being dropped. This is done before the table is dropped
 1.82812 +  ** at the btree level, in case the sqlite_sequence table needs to
 1.82813 +  ** move as a result of the drop (can happen in auto-vacuum mode).
 1.82814 +  */
 1.82815 +  if( pTab->tabFlags & TF_Autoincrement ){
 1.82816 +    sqlite3NestedParse(pParse,
 1.82817 +      "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
 1.82818 +      pDb->zName, pTab->zName
 1.82819 +    );
 1.82820 +  }
 1.82821 +#endif
 1.82822 +
 1.82823 +  /* Drop all SQLITE_MASTER table and index entries that refer to the
 1.82824 +  ** table. The program name loops through the master table and deletes
 1.82825 +  ** every row that refers to a table of the same name as the one being
 1.82826 +  ** dropped. Triggers are handled seperately because a trigger can be
 1.82827 +  ** created in the temp database that refers to a table in another
 1.82828 +  ** database.
 1.82829 +  */
 1.82830 +  sqlite3NestedParse(pParse, 
 1.82831 +      "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
 1.82832 +      pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
 1.82833 +  if( !isView && !IsVirtual(pTab) ){
 1.82834 +    destroyTable(pParse, pTab);
 1.82835 +  }
 1.82836 +
 1.82837 +  /* Remove the table entry from SQLite's internal schema and modify
 1.82838 +  ** the schema cookie.
 1.82839 +  */
 1.82840 +  if( IsVirtual(pTab) ){
 1.82841 +    sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
 1.82842 +  }
 1.82843 +  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 1.82844 +  sqlite3ChangeCookie(pParse, iDb);
 1.82845 +  sqliteViewResetAll(db, iDb);
 1.82846 +}
 1.82847 +
 1.82848 +/*
 1.82849 +** This routine is called to do the work of a DROP TABLE statement.
 1.82850 +** pName is the name of the table to be dropped.
 1.82851 +*/
 1.82852 +SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
 1.82853 +  Table *pTab;
 1.82854 +  Vdbe *v;
 1.82855 +  sqlite3 *db = pParse->db;
 1.82856 +  int iDb;
 1.82857 +
 1.82858 +  if( db->mallocFailed ){
 1.82859 +    goto exit_drop_table;
 1.82860 +  }
 1.82861 +  assert( pParse->nErr==0 );
 1.82862 +  assert( pName->nSrc==1 );
 1.82863 +  if( noErr ) db->suppressErr++;
 1.82864 +  pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
 1.82865 +  if( noErr ) db->suppressErr--;
 1.82866 +
 1.82867 +  if( pTab==0 ){
 1.82868 +    if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 1.82869 +    goto exit_drop_table;
 1.82870 +  }
 1.82871 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.82872 +  assert( iDb>=0 && iDb<db->nDb );
 1.82873 +
 1.82874 +  /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
 1.82875 +  ** it is initialized.
 1.82876 +  */
 1.82877 +  if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
 1.82878 +    goto exit_drop_table;
 1.82879 +  }
 1.82880 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.82881 +  {
 1.82882 +    int code;
 1.82883 +    const char *zTab = SCHEMA_TABLE(iDb);
 1.82884 +    const char *zDb = db->aDb[iDb].zName;
 1.82885 +    const char *zArg2 = 0;
 1.82886 +    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
 1.82887 +      goto exit_drop_table;
 1.82888 +    }
 1.82889 +    if( isView ){
 1.82890 +      if( !OMIT_TEMPDB && iDb==1 ){
 1.82891 +        code = SQLITE_DROP_TEMP_VIEW;
 1.82892 +      }else{
 1.82893 +        code = SQLITE_DROP_VIEW;
 1.82894 +      }
 1.82895 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.82896 +    }else if( IsVirtual(pTab) ){
 1.82897 +      code = SQLITE_DROP_VTABLE;
 1.82898 +      zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
 1.82899 +#endif
 1.82900 +    }else{
 1.82901 +      if( !OMIT_TEMPDB && iDb==1 ){
 1.82902 +        code = SQLITE_DROP_TEMP_TABLE;
 1.82903 +      }else{
 1.82904 +        code = SQLITE_DROP_TABLE;
 1.82905 +      }
 1.82906 +    }
 1.82907 +    if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
 1.82908 +      goto exit_drop_table;
 1.82909 +    }
 1.82910 +    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
 1.82911 +      goto exit_drop_table;
 1.82912 +    }
 1.82913 +  }
 1.82914 +#endif
 1.82915 +  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
 1.82916 +    && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
 1.82917 +    sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
 1.82918 +    goto exit_drop_table;
 1.82919 +  }
 1.82920 +
 1.82921 +#ifndef SQLITE_OMIT_VIEW
 1.82922 +  /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
 1.82923 +  ** on a table.
 1.82924 +  */
 1.82925 +  if( isView && pTab->pSelect==0 ){
 1.82926 +    sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
 1.82927 +    goto exit_drop_table;
 1.82928 +  }
 1.82929 +  if( !isView && pTab->pSelect ){
 1.82930 +    sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
 1.82931 +    goto exit_drop_table;
 1.82932 +  }
 1.82933 +#endif
 1.82934 +
 1.82935 +  /* Generate code to remove the table from the master table
 1.82936 +  ** on disk.
 1.82937 +  */
 1.82938 +  v = sqlite3GetVdbe(pParse);
 1.82939 +  if( v ){
 1.82940 +    sqlite3BeginWriteOperation(pParse, 1, iDb);
 1.82941 +    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 1.82942 +    sqlite3FkDropTable(pParse, pName, pTab);
 1.82943 +    sqlite3CodeDropTable(pParse, pTab, iDb, isView);
 1.82944 +  }
 1.82945 +
 1.82946 +exit_drop_table:
 1.82947 +  sqlite3SrcListDelete(db, pName);
 1.82948 +}
 1.82949 +
 1.82950 +/*
 1.82951 +** This routine is called to create a new foreign key on the table
 1.82952 +** currently under construction.  pFromCol determines which columns
 1.82953 +** in the current table point to the foreign key.  If pFromCol==0 then
 1.82954 +** connect the key to the last column inserted.  pTo is the name of
 1.82955 +** the table referred to.  pToCol is a list of tables in the other
 1.82956 +** pTo table that the foreign key points to.  flags contains all
 1.82957 +** information about the conflict resolution algorithms specified
 1.82958 +** in the ON DELETE, ON UPDATE and ON INSERT clauses.
 1.82959 +**
 1.82960 +** An FKey structure is created and added to the table currently
 1.82961 +** under construction in the pParse->pNewTable field.
 1.82962 +**
 1.82963 +** The foreign key is set for IMMEDIATE processing.  A subsequent call
 1.82964 +** to sqlite3DeferForeignKey() might change this to DEFERRED.
 1.82965 +*/
 1.82966 +SQLITE_PRIVATE void sqlite3CreateForeignKey(
 1.82967 +  Parse *pParse,       /* Parsing context */
 1.82968 +  ExprList *pFromCol,  /* Columns in this table that point to other table */
 1.82969 +  Token *pTo,          /* Name of the other table */
 1.82970 +  ExprList *pToCol,    /* Columns in the other table */
 1.82971 +  int flags            /* Conflict resolution algorithms. */
 1.82972 +){
 1.82973 +  sqlite3 *db = pParse->db;
 1.82974 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.82975 +  FKey *pFKey = 0;
 1.82976 +  FKey *pNextTo;
 1.82977 +  Table *p = pParse->pNewTable;
 1.82978 +  int nByte;
 1.82979 +  int i;
 1.82980 +  int nCol;
 1.82981 +  char *z;
 1.82982 +
 1.82983 +  assert( pTo!=0 );
 1.82984 +  if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
 1.82985 +  if( pFromCol==0 ){
 1.82986 +    int iCol = p->nCol-1;
 1.82987 +    if( NEVER(iCol<0) ) goto fk_end;
 1.82988 +    if( pToCol && pToCol->nExpr!=1 ){
 1.82989 +      sqlite3ErrorMsg(pParse, "foreign key on %s"
 1.82990 +         " should reference only one column of table %T",
 1.82991 +         p->aCol[iCol].zName, pTo);
 1.82992 +      goto fk_end;
 1.82993 +    }
 1.82994 +    nCol = 1;
 1.82995 +  }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
 1.82996 +    sqlite3ErrorMsg(pParse,
 1.82997 +        "number of columns in foreign key does not match the number of "
 1.82998 +        "columns in the referenced table");
 1.82999 +    goto fk_end;
 1.83000 +  }else{
 1.83001 +    nCol = pFromCol->nExpr;
 1.83002 +  }
 1.83003 +  nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
 1.83004 +  if( pToCol ){
 1.83005 +    for(i=0; i<pToCol->nExpr; i++){
 1.83006 +      nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
 1.83007 +    }
 1.83008 +  }
 1.83009 +  pFKey = sqlite3DbMallocZero(db, nByte );
 1.83010 +  if( pFKey==0 ){
 1.83011 +    goto fk_end;
 1.83012 +  }
 1.83013 +  pFKey->pFrom = p;
 1.83014 +  pFKey->pNextFrom = p->pFKey;
 1.83015 +  z = (char*)&pFKey->aCol[nCol];
 1.83016 +  pFKey->zTo = z;
 1.83017 +  memcpy(z, pTo->z, pTo->n);
 1.83018 +  z[pTo->n] = 0;
 1.83019 +  sqlite3Dequote(z);
 1.83020 +  z += pTo->n+1;
 1.83021 +  pFKey->nCol = nCol;
 1.83022 +  if( pFromCol==0 ){
 1.83023 +    pFKey->aCol[0].iFrom = p->nCol-1;
 1.83024 +  }else{
 1.83025 +    for(i=0; i<nCol; i++){
 1.83026 +      int j;
 1.83027 +      for(j=0; j<p->nCol; j++){
 1.83028 +        if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
 1.83029 +          pFKey->aCol[i].iFrom = j;
 1.83030 +          break;
 1.83031 +        }
 1.83032 +      }
 1.83033 +      if( j>=p->nCol ){
 1.83034 +        sqlite3ErrorMsg(pParse, 
 1.83035 +          "unknown column \"%s\" in foreign key definition", 
 1.83036 +          pFromCol->a[i].zName);
 1.83037 +        goto fk_end;
 1.83038 +      }
 1.83039 +    }
 1.83040 +  }
 1.83041 +  if( pToCol ){
 1.83042 +    for(i=0; i<nCol; i++){
 1.83043 +      int n = sqlite3Strlen30(pToCol->a[i].zName);
 1.83044 +      pFKey->aCol[i].zCol = z;
 1.83045 +      memcpy(z, pToCol->a[i].zName, n);
 1.83046 +      z[n] = 0;
 1.83047 +      z += n+1;
 1.83048 +    }
 1.83049 +  }
 1.83050 +  pFKey->isDeferred = 0;
 1.83051 +  pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
 1.83052 +  pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
 1.83053 +
 1.83054 +  assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
 1.83055 +  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
 1.83056 +      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
 1.83057 +  );
 1.83058 +  if( pNextTo==pFKey ){
 1.83059 +    db->mallocFailed = 1;
 1.83060 +    goto fk_end;
 1.83061 +  }
 1.83062 +  if( pNextTo ){
 1.83063 +    assert( pNextTo->pPrevTo==0 );
 1.83064 +    pFKey->pNextTo = pNextTo;
 1.83065 +    pNextTo->pPrevTo = pFKey;
 1.83066 +  }
 1.83067 +
 1.83068 +  /* Link the foreign key to the table as the last step.
 1.83069 +  */
 1.83070 +  p->pFKey = pFKey;
 1.83071 +  pFKey = 0;
 1.83072 +
 1.83073 +fk_end:
 1.83074 +  sqlite3DbFree(db, pFKey);
 1.83075 +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 1.83076 +  sqlite3ExprListDelete(db, pFromCol);
 1.83077 +  sqlite3ExprListDelete(db, pToCol);
 1.83078 +}
 1.83079 +
 1.83080 +/*
 1.83081 +** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
 1.83082 +** clause is seen as part of a foreign key definition.  The isDeferred
 1.83083 +** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
 1.83084 +** The behavior of the most recently created foreign key is adjusted
 1.83085 +** accordingly.
 1.83086 +*/
 1.83087 +SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
 1.83088 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.83089 +  Table *pTab;
 1.83090 +  FKey *pFKey;
 1.83091 +  if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
 1.83092 +  assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
 1.83093 +  pFKey->isDeferred = (u8)isDeferred;
 1.83094 +#endif
 1.83095 +}
 1.83096 +
 1.83097 +/*
 1.83098 +** Generate code that will erase and refill index *pIdx.  This is
 1.83099 +** used to initialize a newly created index or to recompute the
 1.83100 +** content of an index in response to a REINDEX command.
 1.83101 +**
 1.83102 +** if memRootPage is not negative, it means that the index is newly
 1.83103 +** created.  The register specified by memRootPage contains the
 1.83104 +** root page number of the index.  If memRootPage is negative, then
 1.83105 +** the index already exists and must be cleared before being refilled and
 1.83106 +** the root page number of the index is taken from pIndex->tnum.
 1.83107 +*/
 1.83108 +static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
 1.83109 +  Table *pTab = pIndex->pTable;  /* The table that is indexed */
 1.83110 +  int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
 1.83111 +  int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
 1.83112 +  int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
 1.83113 +  int addr1;                     /* Address of top of loop */
 1.83114 +  int addr2;                     /* Address to jump to for next iteration */
 1.83115 +  int tnum;                      /* Root page of index */
 1.83116 +  Vdbe *v;                       /* Generate code into this virtual machine */
 1.83117 +  KeyInfo *pKey;                 /* KeyInfo for index */
 1.83118 +#ifdef SQLITE_OMIT_MERGE_SORT
 1.83119 +  int regIdxKey;                 /* Registers containing the index key */
 1.83120 +#endif
 1.83121 +  int regRecord;                 /* Register holding assemblied index record */
 1.83122 +  sqlite3 *db = pParse->db;      /* The database connection */
 1.83123 +  int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 1.83124 +
 1.83125 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.83126 +  if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
 1.83127 +      db->aDb[iDb].zName ) ){
 1.83128 +    return;
 1.83129 +  }
 1.83130 +#endif
 1.83131 +
 1.83132 +  /* Require a write-lock on the table to perform this operation */
 1.83133 +  sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
 1.83134 +
 1.83135 +  v = sqlite3GetVdbe(pParse);
 1.83136 +  if( v==0 ) return;
 1.83137 +  if( memRootPage>=0 ){
 1.83138 +    tnum = memRootPage;
 1.83139 +  }else{
 1.83140 +    tnum = pIndex->tnum;
 1.83141 +    sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
 1.83142 +  }
 1.83143 +  pKey = sqlite3IndexKeyinfo(pParse, pIndex);
 1.83144 +  sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 1.83145 +                    (char *)pKey, P4_KEYINFO_HANDOFF);
 1.83146 +  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
 1.83147 +
 1.83148 +#ifndef SQLITE_OMIT_MERGE_SORT
 1.83149 +  /* Open the sorter cursor if we are to use one. */
 1.83150 +  iSorter = pParse->nTab++;
 1.83151 +  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
 1.83152 +#else
 1.83153 +  iSorter = iTab;
 1.83154 +#endif
 1.83155 +
 1.83156 +  /* Open the table. Loop through all rows of the table, inserting index
 1.83157 +  ** records into the sorter. */
 1.83158 +  sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 1.83159 +  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
 1.83160 +  regRecord = sqlite3GetTempReg(pParse);
 1.83161 +
 1.83162 +#ifndef SQLITE_OMIT_MERGE_SORT
 1.83163 +  sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 1.83164 +  sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
 1.83165 +  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
 1.83166 +  sqlite3VdbeJumpHere(v, addr1);
 1.83167 +  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
 1.83168 +  if( pIndex->onError!=OE_None ){
 1.83169 +    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
 1.83170 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
 1.83171 +    addr2 = sqlite3VdbeCurrentAddr(v);
 1.83172 +    sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
 1.83173 +    sqlite3HaltConstraint(
 1.83174 +        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
 1.83175 +    );
 1.83176 +  }else{
 1.83177 +    addr2 = sqlite3VdbeCurrentAddr(v);
 1.83178 +  }
 1.83179 +  sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
 1.83180 +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
 1.83181 +  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 1.83182 +#else
 1.83183 +  regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 1.83184 +  addr2 = addr1 + 1;
 1.83185 +  if( pIndex->onError!=OE_None ){
 1.83186 +    const int regRowid = regIdxKey + pIndex->nColumn;
 1.83187 +    const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
 1.83188 +    void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
 1.83189 +
 1.83190 +    /* The registers accessed by the OP_IsUnique opcode were allocated
 1.83191 +    ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
 1.83192 +    ** call above. Just before that function was freed they were released
 1.83193 +    ** (made available to the compiler for reuse) using 
 1.83194 +    ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
 1.83195 +    ** opcode use the values stored within seems dangerous. However, since
 1.83196 +    ** we can be sure that no other temp registers have been allocated
 1.83197 +    ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
 1.83198 +    */
 1.83199 +    sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
 1.83200 +    sqlite3HaltConstraint(
 1.83201 +        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
 1.83202 +  }
 1.83203 +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
 1.83204 +  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 1.83205 +#endif
 1.83206 +  sqlite3ReleaseTempReg(pParse, regRecord);
 1.83207 +  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
 1.83208 +  sqlite3VdbeJumpHere(v, addr1);
 1.83209 +
 1.83210 +  sqlite3VdbeAddOp1(v, OP_Close, iTab);
 1.83211 +  sqlite3VdbeAddOp1(v, OP_Close, iIdx);
 1.83212 +  sqlite3VdbeAddOp1(v, OP_Close, iSorter);
 1.83213 +}
 1.83214 +
 1.83215 +/*
 1.83216 +** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
 1.83217 +** and pTblList is the name of the table that is to be indexed.  Both will 
 1.83218 +** be NULL for a primary key or an index that is created to satisfy a
 1.83219 +** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
 1.83220 +** as the table to be indexed.  pParse->pNewTable is a table that is
 1.83221 +** currently being constructed by a CREATE TABLE statement.
 1.83222 +**
 1.83223 +** pList is a list of columns to be indexed.  pList will be NULL if this
 1.83224 +** is a primary key or unique-constraint on the most recent column added
 1.83225 +** to the table currently under construction.  
 1.83226 +**
 1.83227 +** If the index is created successfully, return a pointer to the new Index
 1.83228 +** structure. This is used by sqlite3AddPrimaryKey() to mark the index
 1.83229 +** as the tables primary key (Index.autoIndex==2).
 1.83230 +*/
 1.83231 +SQLITE_PRIVATE Index *sqlite3CreateIndex(
 1.83232 +  Parse *pParse,     /* All information about this parse */
 1.83233 +  Token *pName1,     /* First part of index name. May be NULL */
 1.83234 +  Token *pName2,     /* Second part of index name. May be NULL */
 1.83235 +  SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
 1.83236 +  ExprList *pList,   /* A list of columns to be indexed */
 1.83237 +  int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 1.83238 +  Token *pStart,     /* The CREATE token that begins this statement */
 1.83239 +  Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
 1.83240 +  int sortOrder,     /* Sort order of primary key when pList==NULL */
 1.83241 +  int ifNotExist     /* Omit error if index already exists */
 1.83242 +){
 1.83243 +  Index *pRet = 0;     /* Pointer to return */
 1.83244 +  Table *pTab = 0;     /* Table to be indexed */
 1.83245 +  Index *pIndex = 0;   /* The index to be created */
 1.83246 +  char *zName = 0;     /* Name of the index */
 1.83247 +  int nName;           /* Number of characters in zName */
 1.83248 +  int i, j;
 1.83249 +  Token nullId;        /* Fake token for an empty ID list */
 1.83250 +  DbFixer sFix;        /* For assigning database names to pTable */
 1.83251 +  int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
 1.83252 +  sqlite3 *db = pParse->db;
 1.83253 +  Db *pDb;             /* The specific table containing the indexed database */
 1.83254 +  int iDb;             /* Index of the database that is being written */
 1.83255 +  Token *pName = 0;    /* Unqualified name of the index to create */
 1.83256 +  struct ExprList_item *pListItem; /* For looping over pList */
 1.83257 +  int nCol;
 1.83258 +  int nExtra = 0;
 1.83259 +  char *zExtra;
 1.83260 +
 1.83261 +  assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
 1.83262 +  assert( pParse->nErr==0 );      /* Never called with prior errors */
 1.83263 +  if( db->mallocFailed || IN_DECLARE_VTAB ){
 1.83264 +    goto exit_create_index;
 1.83265 +  }
 1.83266 +  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 1.83267 +    goto exit_create_index;
 1.83268 +  }
 1.83269 +
 1.83270 +  /*
 1.83271 +  ** Find the table that is to be indexed.  Return early if not found.
 1.83272 +  */
 1.83273 +  if( pTblName!=0 ){
 1.83274 +
 1.83275 +    /* Use the two-part index name to determine the database 
 1.83276 +    ** to search for the table. 'Fix' the table name to this db
 1.83277 +    ** before looking up the table.
 1.83278 +    */
 1.83279 +    assert( pName1 && pName2 );
 1.83280 +    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 1.83281 +    if( iDb<0 ) goto exit_create_index;
 1.83282 +    assert( pName && pName->z );
 1.83283 +
 1.83284 +#ifndef SQLITE_OMIT_TEMPDB
 1.83285 +    /* If the index name was unqualified, check if the table
 1.83286 +    ** is a temp table. If so, set the database to 1. Do not do this
 1.83287 +    ** if initialising a database schema.
 1.83288 +    */
 1.83289 +    if( !db->init.busy ){
 1.83290 +      pTab = sqlite3SrcListLookup(pParse, pTblName);
 1.83291 +      if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
 1.83292 +        iDb = 1;
 1.83293 +      }
 1.83294 +    }
 1.83295 +#endif
 1.83296 +
 1.83297 +    if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
 1.83298 +        sqlite3FixSrcList(&sFix, pTblName)
 1.83299 +    ){
 1.83300 +      /* Because the parser constructs pTblName from a single identifier,
 1.83301 +      ** sqlite3FixSrcList can never fail. */
 1.83302 +      assert(0);
 1.83303 +    }
 1.83304 +    pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
 1.83305 +    assert( db->mallocFailed==0 || pTab==0 );
 1.83306 +    if( pTab==0 ) goto exit_create_index;
 1.83307 +    assert( db->aDb[iDb].pSchema==pTab->pSchema );
 1.83308 +  }else{
 1.83309 +    assert( pName==0 );
 1.83310 +    assert( pStart==0 );
 1.83311 +    pTab = pParse->pNewTable;
 1.83312 +    if( !pTab ) goto exit_create_index;
 1.83313 +    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.83314 +  }
 1.83315 +  pDb = &db->aDb[iDb];
 1.83316 +
 1.83317 +  assert( pTab!=0 );
 1.83318 +  assert( pParse->nErr==0 );
 1.83319 +  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
 1.83320 +       && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
 1.83321 +    sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
 1.83322 +    goto exit_create_index;
 1.83323 +  }
 1.83324 +#ifndef SQLITE_OMIT_VIEW
 1.83325 +  if( pTab->pSelect ){
 1.83326 +    sqlite3ErrorMsg(pParse, "views may not be indexed");
 1.83327 +    goto exit_create_index;
 1.83328 +  }
 1.83329 +#endif
 1.83330 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.83331 +  if( IsVirtual(pTab) ){
 1.83332 +    sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
 1.83333 +    goto exit_create_index;
 1.83334 +  }
 1.83335 +#endif
 1.83336 +
 1.83337 +  /*
 1.83338 +  ** Find the name of the index.  Make sure there is not already another
 1.83339 +  ** index or table with the same name.  
 1.83340 +  **
 1.83341 +  ** Exception:  If we are reading the names of permanent indices from the
 1.83342 +  ** sqlite_master table (because some other process changed the schema) and
 1.83343 +  ** one of the index names collides with the name of a temporary table or
 1.83344 +  ** index, then we will continue to process this index.
 1.83345 +  **
 1.83346 +  ** If pName==0 it means that we are
 1.83347 +  ** dealing with a primary key or UNIQUE constraint.  We have to invent our
 1.83348 +  ** own name.
 1.83349 +  */
 1.83350 +  if( pName ){
 1.83351 +    zName = sqlite3NameFromToken(db, pName);
 1.83352 +    if( zName==0 ) goto exit_create_index;
 1.83353 +    assert( pName->z!=0 );
 1.83354 +    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 1.83355 +      goto exit_create_index;
 1.83356 +    }
 1.83357 +    if( !db->init.busy ){
 1.83358 +      if( sqlite3FindTable(db, zName, 0)!=0 ){
 1.83359 +        sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
 1.83360 +        goto exit_create_index;
 1.83361 +      }
 1.83362 +    }
 1.83363 +    if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
 1.83364 +      if( !ifNotExist ){
 1.83365 +        sqlite3ErrorMsg(pParse, "index %s already exists", zName);
 1.83366 +      }else{
 1.83367 +        assert( !db->init.busy );
 1.83368 +        sqlite3CodeVerifySchema(pParse, iDb);
 1.83369 +      }
 1.83370 +      goto exit_create_index;
 1.83371 +    }
 1.83372 +  }else{
 1.83373 +    int n;
 1.83374 +    Index *pLoop;
 1.83375 +    for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
 1.83376 +    zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
 1.83377 +    if( zName==0 ){
 1.83378 +      goto exit_create_index;
 1.83379 +    }
 1.83380 +  }
 1.83381 +
 1.83382 +  /* Check for authorization to create an index.
 1.83383 +  */
 1.83384 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.83385 +  {
 1.83386 +    const char *zDb = pDb->zName;
 1.83387 +    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
 1.83388 +      goto exit_create_index;
 1.83389 +    }
 1.83390 +    i = SQLITE_CREATE_INDEX;
 1.83391 +    if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
 1.83392 +    if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
 1.83393 +      goto exit_create_index;
 1.83394 +    }
 1.83395 +  }
 1.83396 +#endif
 1.83397 +
 1.83398 +  /* If pList==0, it means this routine was called to make a primary
 1.83399 +  ** key out of the last column added to the table under construction.
 1.83400 +  ** So create a fake list to simulate this.
 1.83401 +  */
 1.83402 +  if( pList==0 ){
 1.83403 +    nullId.z = pTab->aCol[pTab->nCol-1].zName;
 1.83404 +    nullId.n = sqlite3Strlen30((char*)nullId.z);
 1.83405 +    pList = sqlite3ExprListAppend(pParse, 0, 0);
 1.83406 +    if( pList==0 ) goto exit_create_index;
 1.83407 +    sqlite3ExprListSetName(pParse, pList, &nullId, 0);
 1.83408 +    pList->a[0].sortOrder = (u8)sortOrder;
 1.83409 +  }
 1.83410 +
 1.83411 +  /* Figure out how many bytes of space are required to store explicitly
 1.83412 +  ** specified collation sequence names.
 1.83413 +  */
 1.83414 +  for(i=0; i<pList->nExpr; i++){
 1.83415 +    Expr *pExpr = pList->a[i].pExpr;
 1.83416 +    if( pExpr ){
 1.83417 +      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
 1.83418 +      if( pColl ){
 1.83419 +        nExtra += (1 + sqlite3Strlen30(pColl->zName));
 1.83420 +      }
 1.83421 +    }
 1.83422 +  }
 1.83423 +
 1.83424 +  /* 
 1.83425 +  ** Allocate the index structure. 
 1.83426 +  */
 1.83427 +  nName = sqlite3Strlen30(zName);
 1.83428 +  nCol = pList->nExpr;
 1.83429 +  pIndex = sqlite3DbMallocZero(db, 
 1.83430 +      ROUND8(sizeof(Index)) +              /* Index structure  */
 1.83431 +      ROUND8(sizeof(tRowcnt)*(nCol+1)) +   /* Index.aiRowEst   */
 1.83432 +      sizeof(char *)*nCol +                /* Index.azColl     */
 1.83433 +      sizeof(int)*nCol +                   /* Index.aiColumn   */
 1.83434 +      sizeof(u8)*nCol +                    /* Index.aSortOrder */
 1.83435 +      nName + 1 +                          /* Index.zName      */
 1.83436 +      nExtra                               /* Collation sequence names */
 1.83437 +  );
 1.83438 +  if( db->mallocFailed ){
 1.83439 +    goto exit_create_index;
 1.83440 +  }
 1.83441 +  zExtra = (char*)pIndex;
 1.83442 +  pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
 1.83443 +  pIndex->azColl = (char**)
 1.83444 +     ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
 1.83445 +  assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
 1.83446 +  assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
 1.83447 +  pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
 1.83448 +  pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
 1.83449 +  pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
 1.83450 +  zExtra = (char *)(&pIndex->zName[nName+1]);
 1.83451 +  memcpy(pIndex->zName, zName, nName+1);
 1.83452 +  pIndex->pTable = pTab;
 1.83453 +  pIndex->nColumn = pList->nExpr;
 1.83454 +  pIndex->onError = (u8)onError;
 1.83455 +  pIndex->autoIndex = (u8)(pName==0);
 1.83456 +  pIndex->pSchema = db->aDb[iDb].pSchema;
 1.83457 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.83458 +
 1.83459 +  /* Check to see if we should honor DESC requests on index columns
 1.83460 +  */
 1.83461 +  if( pDb->pSchema->file_format>=4 ){
 1.83462 +    sortOrderMask = -1;   /* Honor DESC */
 1.83463 +  }else{
 1.83464 +    sortOrderMask = 0;    /* Ignore DESC */
 1.83465 +  }
 1.83466 +
 1.83467 +  /* Scan the names of the columns of the table to be indexed and
 1.83468 +  ** load the column indices into the Index structure.  Report an error
 1.83469 +  ** if any column is not found.
 1.83470 +  **
 1.83471 +  ** TODO:  Add a test to make sure that the same column is not named
 1.83472 +  ** more than once within the same index.  Only the first instance of
 1.83473 +  ** the column will ever be used by the optimizer.  Note that using the
 1.83474 +  ** same column more than once cannot be an error because that would 
 1.83475 +  ** break backwards compatibility - it needs to be a warning.
 1.83476 +  */
 1.83477 +  for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
 1.83478 +    const char *zColName = pListItem->zName;
 1.83479 +    Column *pTabCol;
 1.83480 +    int requestedSortOrder;
 1.83481 +    CollSeq *pColl;                /* Collating sequence */
 1.83482 +    char *zColl;                   /* Collation sequence name */
 1.83483 +
 1.83484 +    for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
 1.83485 +      if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
 1.83486 +    }
 1.83487 +    if( j>=pTab->nCol ){
 1.83488 +      sqlite3ErrorMsg(pParse, "table %s has no column named %s",
 1.83489 +        pTab->zName, zColName);
 1.83490 +      pParse->checkSchema = 1;
 1.83491 +      goto exit_create_index;
 1.83492 +    }
 1.83493 +    pIndex->aiColumn[i] = j;
 1.83494 +    if( pListItem->pExpr
 1.83495 +     && (pColl = sqlite3ExprCollSeq(pParse, pListItem->pExpr))!=0
 1.83496 +    ){
 1.83497 +      int nColl;
 1.83498 +      zColl = pColl->zName;
 1.83499 +      nColl = sqlite3Strlen30(zColl) + 1;
 1.83500 +      assert( nExtra>=nColl );
 1.83501 +      memcpy(zExtra, zColl, nColl);
 1.83502 +      zColl = zExtra;
 1.83503 +      zExtra += nColl;
 1.83504 +      nExtra -= nColl;
 1.83505 +    }else{
 1.83506 +      zColl = pTab->aCol[j].zColl;
 1.83507 +      if( !zColl ){
 1.83508 +        zColl = "BINARY";
 1.83509 +      }
 1.83510 +    }
 1.83511 +    if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
 1.83512 +      goto exit_create_index;
 1.83513 +    }
 1.83514 +    pIndex->azColl[i] = zColl;
 1.83515 +    requestedSortOrder = pListItem->sortOrder & sortOrderMask;
 1.83516 +    pIndex->aSortOrder[i] = (u8)requestedSortOrder;
 1.83517 +  }
 1.83518 +  sqlite3DefaultRowEst(pIndex);
 1.83519 +
 1.83520 +  if( pTab==pParse->pNewTable ){
 1.83521 +    /* This routine has been called to create an automatic index as a
 1.83522 +    ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
 1.83523 +    ** a PRIMARY KEY or UNIQUE clause following the column definitions.
 1.83524 +    ** i.e. one of:
 1.83525 +    **
 1.83526 +    ** CREATE TABLE t(x PRIMARY KEY, y);
 1.83527 +    ** CREATE TABLE t(x, y, UNIQUE(x, y));
 1.83528 +    **
 1.83529 +    ** Either way, check to see if the table already has such an index. If
 1.83530 +    ** so, don't bother creating this one. This only applies to
 1.83531 +    ** automatically created indices. Users can do as they wish with
 1.83532 +    ** explicit indices.
 1.83533 +    **
 1.83534 +    ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
 1.83535 +    ** (and thus suppressing the second one) even if they have different
 1.83536 +    ** sort orders.
 1.83537 +    **
 1.83538 +    ** If there are different collating sequences or if the columns of
 1.83539 +    ** the constraint occur in different orders, then the constraints are
 1.83540 +    ** considered distinct and both result in separate indices.
 1.83541 +    */
 1.83542 +    Index *pIdx;
 1.83543 +    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.83544 +      int k;
 1.83545 +      assert( pIdx->onError!=OE_None );
 1.83546 +      assert( pIdx->autoIndex );
 1.83547 +      assert( pIndex->onError!=OE_None );
 1.83548 +
 1.83549 +      if( pIdx->nColumn!=pIndex->nColumn ) continue;
 1.83550 +      for(k=0; k<pIdx->nColumn; k++){
 1.83551 +        const char *z1;
 1.83552 +        const char *z2;
 1.83553 +        if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
 1.83554 +        z1 = pIdx->azColl[k];
 1.83555 +        z2 = pIndex->azColl[k];
 1.83556 +        if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
 1.83557 +      }
 1.83558 +      if( k==pIdx->nColumn ){
 1.83559 +        if( pIdx->onError!=pIndex->onError ){
 1.83560 +          /* This constraint creates the same index as a previous
 1.83561 +          ** constraint specified somewhere in the CREATE TABLE statement.
 1.83562 +          ** However the ON CONFLICT clauses are different. If both this 
 1.83563 +          ** constraint and the previous equivalent constraint have explicit
 1.83564 +          ** ON CONFLICT clauses this is an error. Otherwise, use the
 1.83565 +          ** explicitly specified behaviour for the index.
 1.83566 +          */
 1.83567 +          if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
 1.83568 +            sqlite3ErrorMsg(pParse, 
 1.83569 +                "conflicting ON CONFLICT clauses specified", 0);
 1.83570 +          }
 1.83571 +          if( pIdx->onError==OE_Default ){
 1.83572 +            pIdx->onError = pIndex->onError;
 1.83573 +          }
 1.83574 +        }
 1.83575 +        goto exit_create_index;
 1.83576 +      }
 1.83577 +    }
 1.83578 +  }
 1.83579 +
 1.83580 +  /* Link the new Index structure to its table and to the other
 1.83581 +  ** in-memory database structures. 
 1.83582 +  */
 1.83583 +  if( db->init.busy ){
 1.83584 +    Index *p;
 1.83585 +    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 1.83586 +    p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
 1.83587 +                          pIndex->zName, sqlite3Strlen30(pIndex->zName),
 1.83588 +                          pIndex);
 1.83589 +    if( p ){
 1.83590 +      assert( p==pIndex );  /* Malloc must have failed */
 1.83591 +      db->mallocFailed = 1;
 1.83592 +      goto exit_create_index;
 1.83593 +    }
 1.83594 +    db->flags |= SQLITE_InternChanges;
 1.83595 +    if( pTblName!=0 ){
 1.83596 +      pIndex->tnum = db->init.newTnum;
 1.83597 +    }
 1.83598 +  }
 1.83599 +
 1.83600 +  /* If the db->init.busy is 0 then create the index on disk.  This
 1.83601 +  ** involves writing the index into the master table and filling in the
 1.83602 +  ** index with the current table contents.
 1.83603 +  **
 1.83604 +  ** The db->init.busy is 0 when the user first enters a CREATE INDEX 
 1.83605 +  ** command.  db->init.busy is 1 when a database is opened and 
 1.83606 +  ** CREATE INDEX statements are read out of the master table.  In
 1.83607 +  ** the latter case the index already exists on disk, which is why
 1.83608 +  ** we don't want to recreate it.
 1.83609 +  **
 1.83610 +  ** If pTblName==0 it means this index is generated as a primary key
 1.83611 +  ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
 1.83612 +  ** has just been created, it contains no data and the index initialization
 1.83613 +  ** step can be skipped.
 1.83614 +  */
 1.83615 +  else{ /* if( db->init.busy==0 ) */
 1.83616 +    Vdbe *v;
 1.83617 +    char *zStmt;
 1.83618 +    int iMem = ++pParse->nMem;
 1.83619 +
 1.83620 +    v = sqlite3GetVdbe(pParse);
 1.83621 +    if( v==0 ) goto exit_create_index;
 1.83622 +
 1.83623 +
 1.83624 +    /* Create the rootpage for the index
 1.83625 +    */
 1.83626 +    sqlite3BeginWriteOperation(pParse, 1, iDb);
 1.83627 +    sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
 1.83628 +
 1.83629 +    /* Gather the complete text of the CREATE INDEX statement into
 1.83630 +    ** the zStmt variable
 1.83631 +    */
 1.83632 +    if( pStart ){
 1.83633 +      assert( pEnd!=0 );
 1.83634 +      /* A named index with an explicit CREATE INDEX statement */
 1.83635 +      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
 1.83636 +        onError==OE_None ? "" : " UNIQUE",
 1.83637 +        (int)(pEnd->z - pName->z) + 1,
 1.83638 +        pName->z);
 1.83639 +    }else{
 1.83640 +      /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
 1.83641 +      /* zStmt = sqlite3MPrintf(""); */
 1.83642 +      zStmt = 0;
 1.83643 +    }
 1.83644 +
 1.83645 +    /* Add an entry in sqlite_master for this index
 1.83646 +    */
 1.83647 +    sqlite3NestedParse(pParse, 
 1.83648 +        "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
 1.83649 +        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 1.83650 +        pIndex->zName,
 1.83651 +        pTab->zName,
 1.83652 +        iMem,
 1.83653 +        zStmt
 1.83654 +    );
 1.83655 +    sqlite3DbFree(db, zStmt);
 1.83656 +
 1.83657 +    /* Fill the index with data and reparse the schema. Code an OP_Expire
 1.83658 +    ** to invalidate all pre-compiled statements.
 1.83659 +    */
 1.83660 +    if( pTblName ){
 1.83661 +      sqlite3RefillIndex(pParse, pIndex, iMem);
 1.83662 +      sqlite3ChangeCookie(pParse, iDb);
 1.83663 +      sqlite3VdbeAddParseSchemaOp(v, iDb,
 1.83664 +         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
 1.83665 +      sqlite3VdbeAddOp1(v, OP_Expire, 0);
 1.83666 +    }
 1.83667 +  }
 1.83668 +
 1.83669 +  /* When adding an index to the list of indices for a table, make
 1.83670 +  ** sure all indices labeled OE_Replace come after all those labeled
 1.83671 +  ** OE_Ignore.  This is necessary for the correct constraint check
 1.83672 +  ** processing (in sqlite3GenerateConstraintChecks()) as part of
 1.83673 +  ** UPDATE and INSERT statements.  
 1.83674 +  */
 1.83675 +  if( db->init.busy || pTblName==0 ){
 1.83676 +    if( onError!=OE_Replace || pTab->pIndex==0
 1.83677 +         || pTab->pIndex->onError==OE_Replace){
 1.83678 +      pIndex->pNext = pTab->pIndex;
 1.83679 +      pTab->pIndex = pIndex;
 1.83680 +    }else{
 1.83681 +      Index *pOther = pTab->pIndex;
 1.83682 +      while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
 1.83683 +        pOther = pOther->pNext;
 1.83684 +      }
 1.83685 +      pIndex->pNext = pOther->pNext;
 1.83686 +      pOther->pNext = pIndex;
 1.83687 +    }
 1.83688 +    pRet = pIndex;
 1.83689 +    pIndex = 0;
 1.83690 +  }
 1.83691 +
 1.83692 +  /* Clean up before exiting */
 1.83693 +exit_create_index:
 1.83694 +  if( pIndex ){
 1.83695 +    sqlite3DbFree(db, pIndex->zColAff);
 1.83696 +    sqlite3DbFree(db, pIndex);
 1.83697 +  }
 1.83698 +  sqlite3ExprListDelete(db, pList);
 1.83699 +  sqlite3SrcListDelete(db, pTblName);
 1.83700 +  sqlite3DbFree(db, zName);
 1.83701 +  return pRet;
 1.83702 +}
 1.83703 +
 1.83704 +/*
 1.83705 +** Fill the Index.aiRowEst[] array with default information - information
 1.83706 +** to be used when we have not run the ANALYZE command.
 1.83707 +**
 1.83708 +** aiRowEst[0] is suppose to contain the number of elements in the index.
 1.83709 +** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
 1.83710 +** number of rows in the table that match any particular value of the
 1.83711 +** first column of the index.  aiRowEst[2] is an estimate of the number
 1.83712 +** of rows that match any particular combiniation of the first 2 columns
 1.83713 +** of the index.  And so forth.  It must always be the case that
 1.83714 +*
 1.83715 +**           aiRowEst[N]<=aiRowEst[N-1]
 1.83716 +**           aiRowEst[N]>=1
 1.83717 +**
 1.83718 +** Apart from that, we have little to go on besides intuition as to
 1.83719 +** how aiRowEst[] should be initialized.  The numbers generated here
 1.83720 +** are based on typical values found in actual indices.
 1.83721 +*/
 1.83722 +SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
 1.83723 +  tRowcnt *a = pIdx->aiRowEst;
 1.83724 +  int i;
 1.83725 +  tRowcnt n;
 1.83726 +  assert( a!=0 );
 1.83727 +  a[0] = pIdx->pTable->nRowEst;
 1.83728 +  if( a[0]<10 ) a[0] = 10;
 1.83729 +  n = 10;
 1.83730 +  for(i=1; i<=pIdx->nColumn; i++){
 1.83731 +    a[i] = n;
 1.83732 +    if( n>5 ) n--;
 1.83733 +  }
 1.83734 +  if( pIdx->onError!=OE_None ){
 1.83735 +    a[pIdx->nColumn] = 1;
 1.83736 +  }
 1.83737 +}
 1.83738 +
 1.83739 +/*
 1.83740 +** This routine will drop an existing named index.  This routine
 1.83741 +** implements the DROP INDEX statement.
 1.83742 +*/
 1.83743 +SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
 1.83744 +  Index *pIndex;
 1.83745 +  Vdbe *v;
 1.83746 +  sqlite3 *db = pParse->db;
 1.83747 +  int iDb;
 1.83748 +
 1.83749 +  assert( pParse->nErr==0 );   /* Never called with prior errors */
 1.83750 +  if( db->mallocFailed ){
 1.83751 +    goto exit_drop_index;
 1.83752 +  }
 1.83753 +  assert( pName->nSrc==1 );
 1.83754 +  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 1.83755 +    goto exit_drop_index;
 1.83756 +  }
 1.83757 +  pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
 1.83758 +  if( pIndex==0 ){
 1.83759 +    if( !ifExists ){
 1.83760 +      sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
 1.83761 +    }else{
 1.83762 +      sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 1.83763 +    }
 1.83764 +    pParse->checkSchema = 1;
 1.83765 +    goto exit_drop_index;
 1.83766 +  }
 1.83767 +  if( pIndex->autoIndex ){
 1.83768 +    sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
 1.83769 +      "or PRIMARY KEY constraint cannot be dropped", 0);
 1.83770 +    goto exit_drop_index;
 1.83771 +  }
 1.83772 +  iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 1.83773 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.83774 +  {
 1.83775 +    int code = SQLITE_DROP_INDEX;
 1.83776 +    Table *pTab = pIndex->pTable;
 1.83777 +    const char *zDb = db->aDb[iDb].zName;
 1.83778 +    const char *zTab = SCHEMA_TABLE(iDb);
 1.83779 +    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
 1.83780 +      goto exit_drop_index;
 1.83781 +    }
 1.83782 +    if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
 1.83783 +    if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
 1.83784 +      goto exit_drop_index;
 1.83785 +    }
 1.83786 +  }
 1.83787 +#endif
 1.83788 +
 1.83789 +  /* Generate code to remove the index and from the master table */
 1.83790 +  v = sqlite3GetVdbe(pParse);
 1.83791 +  if( v ){
 1.83792 +    sqlite3BeginWriteOperation(pParse, 1, iDb);
 1.83793 +    sqlite3NestedParse(pParse,
 1.83794 +       "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
 1.83795 +       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
 1.83796 +    );
 1.83797 +    sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
 1.83798 +    sqlite3ChangeCookie(pParse, iDb);
 1.83799 +    destroyRootPage(pParse, pIndex->tnum, iDb);
 1.83800 +    sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
 1.83801 +  }
 1.83802 +
 1.83803 +exit_drop_index:
 1.83804 +  sqlite3SrcListDelete(db, pName);
 1.83805 +}
 1.83806 +
 1.83807 +/*
 1.83808 +** pArray is a pointer to an array of objects. Each object in the
 1.83809 +** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
 1.83810 +** to extend the array so that there is space for a new object at the end.
 1.83811 +**
 1.83812 +** When this function is called, *pnEntry contains the current size of
 1.83813 +** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
 1.83814 +** in total).
 1.83815 +**
 1.83816 +** If the realloc() is successful (i.e. if no OOM condition occurs), the
 1.83817 +** space allocated for the new object is zeroed, *pnEntry updated to
 1.83818 +** reflect the new size of the array and a pointer to the new allocation
 1.83819 +** returned. *pIdx is set to the index of the new array entry in this case.
 1.83820 +**
 1.83821 +** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
 1.83822 +** unchanged and a copy of pArray returned.
 1.83823 +*/
 1.83824 +SQLITE_PRIVATE void *sqlite3ArrayAllocate(
 1.83825 +  sqlite3 *db,      /* Connection to notify of malloc failures */
 1.83826 +  void *pArray,     /* Array of objects.  Might be reallocated */
 1.83827 +  int szEntry,      /* Size of each object in the array */
 1.83828 +  int *pnEntry,     /* Number of objects currently in use */
 1.83829 +  int *pIdx         /* Write the index of a new slot here */
 1.83830 +){
 1.83831 +  char *z;
 1.83832 +  int n = *pnEntry;
 1.83833 +  if( (n & (n-1))==0 ){
 1.83834 +    int sz = (n==0) ? 1 : 2*n;
 1.83835 +    void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
 1.83836 +    if( pNew==0 ){
 1.83837 +      *pIdx = -1;
 1.83838 +      return pArray;
 1.83839 +    }
 1.83840 +    pArray = pNew;
 1.83841 +  }
 1.83842 +  z = (char*)pArray;
 1.83843 +  memset(&z[n * szEntry], 0, szEntry);
 1.83844 +  *pIdx = n;
 1.83845 +  ++*pnEntry;
 1.83846 +  return pArray;
 1.83847 +}
 1.83848 +
 1.83849 +/*
 1.83850 +** Append a new element to the given IdList.  Create a new IdList if
 1.83851 +** need be.
 1.83852 +**
 1.83853 +** A new IdList is returned, or NULL if malloc() fails.
 1.83854 +*/
 1.83855 +SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
 1.83856 +  int i;
 1.83857 +  if( pList==0 ){
 1.83858 +    pList = sqlite3DbMallocZero(db, sizeof(IdList) );
 1.83859 +    if( pList==0 ) return 0;
 1.83860 +  }
 1.83861 +  pList->a = sqlite3ArrayAllocate(
 1.83862 +      db,
 1.83863 +      pList->a,
 1.83864 +      sizeof(pList->a[0]),
 1.83865 +      &pList->nId,
 1.83866 +      &i
 1.83867 +  );
 1.83868 +  if( i<0 ){
 1.83869 +    sqlite3IdListDelete(db, pList);
 1.83870 +    return 0;
 1.83871 +  }
 1.83872 +  pList->a[i].zName = sqlite3NameFromToken(db, pToken);
 1.83873 +  return pList;
 1.83874 +}
 1.83875 +
 1.83876 +/*
 1.83877 +** Delete an IdList.
 1.83878 +*/
 1.83879 +SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
 1.83880 +  int i;
 1.83881 +  if( pList==0 ) return;
 1.83882 +  for(i=0; i<pList->nId; i++){
 1.83883 +    sqlite3DbFree(db, pList->a[i].zName);
 1.83884 +  }
 1.83885 +  sqlite3DbFree(db, pList->a);
 1.83886 +  sqlite3DbFree(db, pList);
 1.83887 +}
 1.83888 +
 1.83889 +/*
 1.83890 +** Return the index in pList of the identifier named zId.  Return -1
 1.83891 +** if not found.
 1.83892 +*/
 1.83893 +SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
 1.83894 +  int i;
 1.83895 +  if( pList==0 ) return -1;
 1.83896 +  for(i=0; i<pList->nId; i++){
 1.83897 +    if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
 1.83898 +  }
 1.83899 +  return -1;
 1.83900 +}
 1.83901 +
 1.83902 +/*
 1.83903 +** Expand the space allocated for the given SrcList object by
 1.83904 +** creating nExtra new slots beginning at iStart.  iStart is zero based.
 1.83905 +** New slots are zeroed.
 1.83906 +**
 1.83907 +** For example, suppose a SrcList initially contains two entries: A,B.
 1.83908 +** To append 3 new entries onto the end, do this:
 1.83909 +**
 1.83910 +**    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
 1.83911 +**
 1.83912 +** After the call above it would contain:  A, B, nil, nil, nil.
 1.83913 +** If the iStart argument had been 1 instead of 2, then the result
 1.83914 +** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
 1.83915 +** the iStart value would be 0.  The result then would
 1.83916 +** be: nil, nil, nil, A, B.
 1.83917 +**
 1.83918 +** If a memory allocation fails the SrcList is unchanged.  The
 1.83919 +** db->mallocFailed flag will be set to true.
 1.83920 +*/
 1.83921 +SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
 1.83922 +  sqlite3 *db,       /* Database connection to notify of OOM errors */
 1.83923 +  SrcList *pSrc,     /* The SrcList to be enlarged */
 1.83924 +  int nExtra,        /* Number of new slots to add to pSrc->a[] */
 1.83925 +  int iStart         /* Index in pSrc->a[] of first new slot */
 1.83926 +){
 1.83927 +  int i;
 1.83928 +
 1.83929 +  /* Sanity checking on calling parameters */
 1.83930 +  assert( iStart>=0 );
 1.83931 +  assert( nExtra>=1 );
 1.83932 +  assert( pSrc!=0 );
 1.83933 +  assert( iStart<=pSrc->nSrc );
 1.83934 +
 1.83935 +  /* Allocate additional space if needed */
 1.83936 +  if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
 1.83937 +    SrcList *pNew;
 1.83938 +    int nAlloc = pSrc->nSrc+nExtra;
 1.83939 +    int nGot;
 1.83940 +    pNew = sqlite3DbRealloc(db, pSrc,
 1.83941 +               sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
 1.83942 +    if( pNew==0 ){
 1.83943 +      assert( db->mallocFailed );
 1.83944 +      return pSrc;
 1.83945 +    }
 1.83946 +    pSrc = pNew;
 1.83947 +    nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
 1.83948 +    pSrc->nAlloc = (u16)nGot;
 1.83949 +  }
 1.83950 +
 1.83951 +  /* Move existing slots that come after the newly inserted slots
 1.83952 +  ** out of the way */
 1.83953 +  for(i=pSrc->nSrc-1; i>=iStart; i--){
 1.83954 +    pSrc->a[i+nExtra] = pSrc->a[i];
 1.83955 +  }
 1.83956 +  pSrc->nSrc += (i16)nExtra;
 1.83957 +
 1.83958 +  /* Zero the newly allocated slots */
 1.83959 +  memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
 1.83960 +  for(i=iStart; i<iStart+nExtra; i++){
 1.83961 +    pSrc->a[i].iCursor = -1;
 1.83962 +  }
 1.83963 +
 1.83964 +  /* Return a pointer to the enlarged SrcList */
 1.83965 +  return pSrc;
 1.83966 +}
 1.83967 +
 1.83968 +
 1.83969 +/*
 1.83970 +** Append a new table name to the given SrcList.  Create a new SrcList if
 1.83971 +** need be.  A new entry is created in the SrcList even if pTable is NULL.
 1.83972 +**
 1.83973 +** A SrcList is returned, or NULL if there is an OOM error.  The returned
 1.83974 +** SrcList might be the same as the SrcList that was input or it might be
 1.83975 +** a new one.  If an OOM error does occurs, then the prior value of pList
 1.83976 +** that is input to this routine is automatically freed.
 1.83977 +**
 1.83978 +** If pDatabase is not null, it means that the table has an optional
 1.83979 +** database name prefix.  Like this:  "database.table".  The pDatabase
 1.83980 +** points to the table name and the pTable points to the database name.
 1.83981 +** The SrcList.a[].zName field is filled with the table name which might
 1.83982 +** come from pTable (if pDatabase is NULL) or from pDatabase.  
 1.83983 +** SrcList.a[].zDatabase is filled with the database name from pTable,
 1.83984 +** or with NULL if no database is specified.
 1.83985 +**
 1.83986 +** In other words, if call like this:
 1.83987 +**
 1.83988 +**         sqlite3SrcListAppend(D,A,B,0);
 1.83989 +**
 1.83990 +** Then B is a table name and the database name is unspecified.  If called
 1.83991 +** like this:
 1.83992 +**
 1.83993 +**         sqlite3SrcListAppend(D,A,B,C);
 1.83994 +**
 1.83995 +** Then C is the table name and B is the database name.  If C is defined
 1.83996 +** then so is B.  In other words, we never have a case where:
 1.83997 +**
 1.83998 +**         sqlite3SrcListAppend(D,A,0,C);
 1.83999 +**
 1.84000 +** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
 1.84001 +** before being added to the SrcList.
 1.84002 +*/
 1.84003 +SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
 1.84004 +  sqlite3 *db,        /* Connection to notify of malloc failures */
 1.84005 +  SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
 1.84006 +  Token *pTable,      /* Table to append */
 1.84007 +  Token *pDatabase    /* Database of the table */
 1.84008 +){
 1.84009 +  struct SrcList_item *pItem;
 1.84010 +  assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
 1.84011 +  if( pList==0 ){
 1.84012 +    pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
 1.84013 +    if( pList==0 ) return 0;
 1.84014 +    pList->nAlloc = 1;
 1.84015 +  }
 1.84016 +  pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
 1.84017 +  if( db->mallocFailed ){
 1.84018 +    sqlite3SrcListDelete(db, pList);
 1.84019 +    return 0;
 1.84020 +  }
 1.84021 +  pItem = &pList->a[pList->nSrc-1];
 1.84022 +  if( pDatabase && pDatabase->z==0 ){
 1.84023 +    pDatabase = 0;
 1.84024 +  }
 1.84025 +  if( pDatabase ){
 1.84026 +    Token *pTemp = pDatabase;
 1.84027 +    pDatabase = pTable;
 1.84028 +    pTable = pTemp;
 1.84029 +  }
 1.84030 +  pItem->zName = sqlite3NameFromToken(db, pTable);
 1.84031 +  pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
 1.84032 +  return pList;
 1.84033 +}
 1.84034 +
 1.84035 +/*
 1.84036 +** Assign VdbeCursor index numbers to all tables in a SrcList
 1.84037 +*/
 1.84038 +SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
 1.84039 +  int i;
 1.84040 +  struct SrcList_item *pItem;
 1.84041 +  assert(pList || pParse->db->mallocFailed );
 1.84042 +  if( pList ){
 1.84043 +    for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
 1.84044 +      if( pItem->iCursor>=0 ) break;
 1.84045 +      pItem->iCursor = pParse->nTab++;
 1.84046 +      if( pItem->pSelect ){
 1.84047 +        sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
 1.84048 +      }
 1.84049 +    }
 1.84050 +  }
 1.84051 +}
 1.84052 +
 1.84053 +/*
 1.84054 +** Delete an entire SrcList including all its substructure.
 1.84055 +*/
 1.84056 +SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
 1.84057 +  int i;
 1.84058 +  struct SrcList_item *pItem;
 1.84059 +  if( pList==0 ) return;
 1.84060 +  for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
 1.84061 +    sqlite3DbFree(db, pItem->zDatabase);
 1.84062 +    sqlite3DbFree(db, pItem->zName);
 1.84063 +    sqlite3DbFree(db, pItem->zAlias);
 1.84064 +    sqlite3DbFree(db, pItem->zIndex);
 1.84065 +    sqlite3DeleteTable(db, pItem->pTab);
 1.84066 +    sqlite3SelectDelete(db, pItem->pSelect);
 1.84067 +    sqlite3ExprDelete(db, pItem->pOn);
 1.84068 +    sqlite3IdListDelete(db, pItem->pUsing);
 1.84069 +  }
 1.84070 +  sqlite3DbFree(db, pList);
 1.84071 +}
 1.84072 +
 1.84073 +/*
 1.84074 +** This routine is called by the parser to add a new term to the
 1.84075 +** end of a growing FROM clause.  The "p" parameter is the part of
 1.84076 +** the FROM clause that has already been constructed.  "p" is NULL
 1.84077 +** if this is the first term of the FROM clause.  pTable and pDatabase
 1.84078 +** are the name of the table and database named in the FROM clause term.
 1.84079 +** pDatabase is NULL if the database name qualifier is missing - the
 1.84080 +** usual case.  If the term has a alias, then pAlias points to the
 1.84081 +** alias token.  If the term is a subquery, then pSubquery is the
 1.84082 +** SELECT statement that the subquery encodes.  The pTable and
 1.84083 +** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
 1.84084 +** parameters are the content of the ON and USING clauses.
 1.84085 +**
 1.84086 +** Return a new SrcList which encodes is the FROM with the new
 1.84087 +** term added.
 1.84088 +*/
 1.84089 +SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
 1.84090 +  Parse *pParse,          /* Parsing context */
 1.84091 +  SrcList *p,             /* The left part of the FROM clause already seen */
 1.84092 +  Token *pTable,          /* Name of the table to add to the FROM clause */
 1.84093 +  Token *pDatabase,       /* Name of the database containing pTable */
 1.84094 +  Token *pAlias,          /* The right-hand side of the AS subexpression */
 1.84095 +  Select *pSubquery,      /* A subquery used in place of a table name */
 1.84096 +  Expr *pOn,              /* The ON clause of a join */
 1.84097 +  IdList *pUsing          /* The USING clause of a join */
 1.84098 +){
 1.84099 +  struct SrcList_item *pItem;
 1.84100 +  sqlite3 *db = pParse->db;
 1.84101 +  if( !p && (pOn || pUsing) ){
 1.84102 +    sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
 1.84103 +      (pOn ? "ON" : "USING")
 1.84104 +    );
 1.84105 +    goto append_from_error;
 1.84106 +  }
 1.84107 +  p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
 1.84108 +  if( p==0 || NEVER(p->nSrc==0) ){
 1.84109 +    goto append_from_error;
 1.84110 +  }
 1.84111 +  pItem = &p->a[p->nSrc-1];
 1.84112 +  assert( pAlias!=0 );
 1.84113 +  if( pAlias->n ){
 1.84114 +    pItem->zAlias = sqlite3NameFromToken(db, pAlias);
 1.84115 +  }
 1.84116 +  pItem->pSelect = pSubquery;
 1.84117 +  pItem->pOn = pOn;
 1.84118 +  pItem->pUsing = pUsing;
 1.84119 +  return p;
 1.84120 +
 1.84121 + append_from_error:
 1.84122 +  assert( p==0 );
 1.84123 +  sqlite3ExprDelete(db, pOn);
 1.84124 +  sqlite3IdListDelete(db, pUsing);
 1.84125 +  sqlite3SelectDelete(db, pSubquery);
 1.84126 +  return 0;
 1.84127 +}
 1.84128 +
 1.84129 +/*
 1.84130 +** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
 1.84131 +** element of the source-list passed as the second argument.
 1.84132 +*/
 1.84133 +SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
 1.84134 +  assert( pIndexedBy!=0 );
 1.84135 +  if( p && ALWAYS(p->nSrc>0) ){
 1.84136 +    struct SrcList_item *pItem = &p->a[p->nSrc-1];
 1.84137 +    assert( pItem->notIndexed==0 && pItem->zIndex==0 );
 1.84138 +    if( pIndexedBy->n==1 && !pIndexedBy->z ){
 1.84139 +      /* A "NOT INDEXED" clause was supplied. See parse.y 
 1.84140 +      ** construct "indexed_opt" for details. */
 1.84141 +      pItem->notIndexed = 1;
 1.84142 +    }else{
 1.84143 +      pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
 1.84144 +    }
 1.84145 +  }
 1.84146 +}
 1.84147 +
 1.84148 +/*
 1.84149 +** When building up a FROM clause in the parser, the join operator
 1.84150 +** is initially attached to the left operand.  But the code generator
 1.84151 +** expects the join operator to be on the right operand.  This routine
 1.84152 +** Shifts all join operators from left to right for an entire FROM
 1.84153 +** clause.
 1.84154 +**
 1.84155 +** Example: Suppose the join is like this:
 1.84156 +**
 1.84157 +**           A natural cross join B
 1.84158 +**
 1.84159 +** The operator is "natural cross join".  The A and B operands are stored
 1.84160 +** in p->a[0] and p->a[1], respectively.  The parser initially stores the
 1.84161 +** operator with A.  This routine shifts that operator over to B.
 1.84162 +*/
 1.84163 +SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
 1.84164 +  if( p ){
 1.84165 +    int i;
 1.84166 +    assert( p->a || p->nSrc==0 );
 1.84167 +    for(i=p->nSrc-1; i>0; i--){
 1.84168 +      p->a[i].jointype = p->a[i-1].jointype;
 1.84169 +    }
 1.84170 +    p->a[0].jointype = 0;
 1.84171 +  }
 1.84172 +}
 1.84173 +
 1.84174 +/*
 1.84175 +** Begin a transaction
 1.84176 +*/
 1.84177 +SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
 1.84178 +  sqlite3 *db;
 1.84179 +  Vdbe *v;
 1.84180 +  int i;
 1.84181 +
 1.84182 +  assert( pParse!=0 );
 1.84183 +  db = pParse->db;
 1.84184 +  assert( db!=0 );
 1.84185 +/*  if( db->aDb[0].pBt==0 ) return; */
 1.84186 +  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
 1.84187 +    return;
 1.84188 +  }
 1.84189 +  v = sqlite3GetVdbe(pParse);
 1.84190 +  if( !v ) return;
 1.84191 +  if( type!=TK_DEFERRED ){
 1.84192 +    for(i=0; i<db->nDb; i++){
 1.84193 +      sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
 1.84194 +      sqlite3VdbeUsesBtree(v, i);
 1.84195 +    }
 1.84196 +  }
 1.84197 +  sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
 1.84198 +}
 1.84199 +
 1.84200 +/*
 1.84201 +** Commit a transaction
 1.84202 +*/
 1.84203 +SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
 1.84204 +  Vdbe *v;
 1.84205 +
 1.84206 +  assert( pParse!=0 );
 1.84207 +  assert( pParse->db!=0 );
 1.84208 +  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
 1.84209 +    return;
 1.84210 +  }
 1.84211 +  v = sqlite3GetVdbe(pParse);
 1.84212 +  if( v ){
 1.84213 +    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
 1.84214 +  }
 1.84215 +}
 1.84216 +
 1.84217 +/*
 1.84218 +** Rollback a transaction
 1.84219 +*/
 1.84220 +SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
 1.84221 +  Vdbe *v;
 1.84222 +
 1.84223 +  assert( pParse!=0 );
 1.84224 +  assert( pParse->db!=0 );
 1.84225 +  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
 1.84226 +    return;
 1.84227 +  }
 1.84228 +  v = sqlite3GetVdbe(pParse);
 1.84229 +  if( v ){
 1.84230 +    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
 1.84231 +  }
 1.84232 +}
 1.84233 +
 1.84234 +/*
 1.84235 +** This function is called by the parser when it parses a command to create,
 1.84236 +** release or rollback an SQL savepoint. 
 1.84237 +*/
 1.84238 +SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
 1.84239 +  char *zName = sqlite3NameFromToken(pParse->db, pName);
 1.84240 +  if( zName ){
 1.84241 +    Vdbe *v = sqlite3GetVdbe(pParse);
 1.84242 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.84243 +    static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
 1.84244 +    assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
 1.84245 +#endif
 1.84246 +    if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
 1.84247 +      sqlite3DbFree(pParse->db, zName);
 1.84248 +      return;
 1.84249 +    }
 1.84250 +    sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
 1.84251 +  }
 1.84252 +}
 1.84253 +
 1.84254 +/*
 1.84255 +** Make sure the TEMP database is open and available for use.  Return
 1.84256 +** the number of errors.  Leave any error messages in the pParse structure.
 1.84257 +*/
 1.84258 +SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
 1.84259 +  sqlite3 *db = pParse->db;
 1.84260 +  if( db->aDb[1].pBt==0 && !pParse->explain ){
 1.84261 +    int rc;
 1.84262 +    Btree *pBt;
 1.84263 +    static const int flags = 
 1.84264 +          SQLITE_OPEN_READWRITE |
 1.84265 +          SQLITE_OPEN_CREATE |
 1.84266 +          SQLITE_OPEN_EXCLUSIVE |
 1.84267 +          SQLITE_OPEN_DELETEONCLOSE |
 1.84268 +          SQLITE_OPEN_TEMP_DB;
 1.84269 +
 1.84270 +    rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
 1.84271 +    if( rc!=SQLITE_OK ){
 1.84272 +      sqlite3ErrorMsg(pParse, "unable to open a temporary database "
 1.84273 +        "file for storing temporary tables");
 1.84274 +      pParse->rc = rc;
 1.84275 +      return 1;
 1.84276 +    }
 1.84277 +    db->aDb[1].pBt = pBt;
 1.84278 +    assert( db->aDb[1].pSchema );
 1.84279 +    if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
 1.84280 +      db->mallocFailed = 1;
 1.84281 +      return 1;
 1.84282 +    }
 1.84283 +  }
 1.84284 +  return 0;
 1.84285 +}
 1.84286 +
 1.84287 +/*
 1.84288 +** Generate VDBE code that will verify the schema cookie and start
 1.84289 +** a read-transaction for all named database files.
 1.84290 +**
 1.84291 +** It is important that all schema cookies be verified and all
 1.84292 +** read transactions be started before anything else happens in
 1.84293 +** the VDBE program.  But this routine can be called after much other
 1.84294 +** code has been generated.  So here is what we do:
 1.84295 +**
 1.84296 +** The first time this routine is called, we code an OP_Goto that
 1.84297 +** will jump to a subroutine at the end of the program.  Then we
 1.84298 +** record every database that needs its schema verified in the
 1.84299 +** pParse->cookieMask field.  Later, after all other code has been
 1.84300 +** generated, the subroutine that does the cookie verifications and
 1.84301 +** starts the transactions will be coded and the OP_Goto P2 value
 1.84302 +** will be made to point to that subroutine.  The generation of the
 1.84303 +** cookie verification subroutine code happens in sqlite3FinishCoding().
 1.84304 +**
 1.84305 +** If iDb<0 then code the OP_Goto only - don't set flag to verify the
 1.84306 +** schema on any databases.  This can be used to position the OP_Goto
 1.84307 +** early in the code, before we know if any database tables will be used.
 1.84308 +*/
 1.84309 +SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
 1.84310 +  Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.84311 +
 1.84312 +#ifndef SQLITE_OMIT_TRIGGER
 1.84313 +  if( pToplevel!=pParse ){
 1.84314 +    /* This branch is taken if a trigger is currently being coded. In this
 1.84315 +    ** case, set cookieGoto to a non-zero value to show that this function
 1.84316 +    ** has been called. This is used by the sqlite3ExprCodeConstants()
 1.84317 +    ** function. */
 1.84318 +    pParse->cookieGoto = -1;
 1.84319 +  }
 1.84320 +#endif
 1.84321 +  if( pToplevel->cookieGoto==0 ){
 1.84322 +    Vdbe *v = sqlite3GetVdbe(pToplevel);
 1.84323 +    if( v==0 ) return;  /* This only happens if there was a prior error */
 1.84324 +    pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
 1.84325 +  }
 1.84326 +  if( iDb>=0 ){
 1.84327 +    sqlite3 *db = pToplevel->db;
 1.84328 +    yDbMask mask;
 1.84329 +
 1.84330 +    assert( iDb<db->nDb );
 1.84331 +    assert( db->aDb[iDb].pBt!=0 || iDb==1 );
 1.84332 +    assert( iDb<SQLITE_MAX_ATTACHED+2 );
 1.84333 +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.84334 +    mask = ((yDbMask)1)<<iDb;
 1.84335 +    if( (pToplevel->cookieMask & mask)==0 ){
 1.84336 +      pToplevel->cookieMask |= mask;
 1.84337 +      pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
 1.84338 +      if( !OMIT_TEMPDB && iDb==1 ){
 1.84339 +        sqlite3OpenTempDatabase(pToplevel);
 1.84340 +      }
 1.84341 +    }
 1.84342 +  }
 1.84343 +}
 1.84344 +
 1.84345 +/*
 1.84346 +** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
 1.84347 +** attached database. Otherwise, invoke it for the database named zDb only.
 1.84348 +*/
 1.84349 +SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
 1.84350 +  sqlite3 *db = pParse->db;
 1.84351 +  int i;
 1.84352 +  for(i=0; i<db->nDb; i++){
 1.84353 +    Db *pDb = &db->aDb[i];
 1.84354 +    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
 1.84355 +      sqlite3CodeVerifySchema(pParse, i);
 1.84356 +    }
 1.84357 +  }
 1.84358 +}
 1.84359 +
 1.84360 +/*
 1.84361 +** Generate VDBE code that prepares for doing an operation that
 1.84362 +** might change the database.
 1.84363 +**
 1.84364 +** This routine starts a new transaction if we are not already within
 1.84365 +** a transaction.  If we are already within a transaction, then a checkpoint
 1.84366 +** is set if the setStatement parameter is true.  A checkpoint should
 1.84367 +** be set for operations that might fail (due to a constraint) part of
 1.84368 +** the way through and which will need to undo some writes without having to
 1.84369 +** rollback the whole transaction.  For operations where all constraints
 1.84370 +** can be checked before any changes are made to the database, it is never
 1.84371 +** necessary to undo a write and the checkpoint should not be set.
 1.84372 +*/
 1.84373 +SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
 1.84374 +  Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.84375 +  sqlite3CodeVerifySchema(pParse, iDb);
 1.84376 +  pToplevel->writeMask |= ((yDbMask)1)<<iDb;
 1.84377 +  pToplevel->isMultiWrite |= setStatement;
 1.84378 +}
 1.84379 +
 1.84380 +/*
 1.84381 +** Indicate that the statement currently under construction might write
 1.84382 +** more than one entry (example: deleting one row then inserting another,
 1.84383 +** inserting multiple rows in a table, or inserting a row and index entries.)
 1.84384 +** If an abort occurs after some of these writes have completed, then it will
 1.84385 +** be necessary to undo the completed writes.
 1.84386 +*/
 1.84387 +SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
 1.84388 +  Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.84389 +  pToplevel->isMultiWrite = 1;
 1.84390 +}
 1.84391 +
 1.84392 +/* 
 1.84393 +** The code generator calls this routine if is discovers that it is
 1.84394 +** possible to abort a statement prior to completion.  In order to 
 1.84395 +** perform this abort without corrupting the database, we need to make
 1.84396 +** sure that the statement is protected by a statement transaction.
 1.84397 +**
 1.84398 +** Technically, we only need to set the mayAbort flag if the
 1.84399 +** isMultiWrite flag was previously set.  There is a time dependency
 1.84400 +** such that the abort must occur after the multiwrite.  This makes
 1.84401 +** some statements involving the REPLACE conflict resolution algorithm
 1.84402 +** go a little faster.  But taking advantage of this time dependency
 1.84403 +** makes it more difficult to prove that the code is correct (in 
 1.84404 +** particular, it prevents us from writing an effective
 1.84405 +** implementation of sqlite3AssertMayAbort()) and so we have chosen
 1.84406 +** to take the safe route and skip the optimization.
 1.84407 +*/
 1.84408 +SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
 1.84409 +  Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.84410 +  pToplevel->mayAbort = 1;
 1.84411 +}
 1.84412 +
 1.84413 +/*
 1.84414 +** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
 1.84415 +** error. The onError parameter determines which (if any) of the statement
 1.84416 +** and/or current transaction is rolled back.
 1.84417 +*/
 1.84418 +SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
 1.84419 +  Vdbe *v = sqlite3GetVdbe(pParse);
 1.84420 +  if( onError==OE_Abort ){
 1.84421 +    sqlite3MayAbort(pParse);
 1.84422 +  }
 1.84423 +  sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
 1.84424 +}
 1.84425 +
 1.84426 +/*
 1.84427 +** Check to see if pIndex uses the collating sequence pColl.  Return
 1.84428 +** true if it does and false if it does not.
 1.84429 +*/
 1.84430 +#ifndef SQLITE_OMIT_REINDEX
 1.84431 +static int collationMatch(const char *zColl, Index *pIndex){
 1.84432 +  int i;
 1.84433 +  assert( zColl!=0 );
 1.84434 +  for(i=0; i<pIndex->nColumn; i++){
 1.84435 +    const char *z = pIndex->azColl[i];
 1.84436 +    assert( z!=0 );
 1.84437 +    if( 0==sqlite3StrICmp(z, zColl) ){
 1.84438 +      return 1;
 1.84439 +    }
 1.84440 +  }
 1.84441 +  return 0;
 1.84442 +}
 1.84443 +#endif
 1.84444 +
 1.84445 +/*
 1.84446 +** Recompute all indices of pTab that use the collating sequence pColl.
 1.84447 +** If pColl==0 then recompute all indices of pTab.
 1.84448 +*/
 1.84449 +#ifndef SQLITE_OMIT_REINDEX
 1.84450 +static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
 1.84451 +  Index *pIndex;              /* An index associated with pTab */
 1.84452 +
 1.84453 +  for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
 1.84454 +    if( zColl==0 || collationMatch(zColl, pIndex) ){
 1.84455 +      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.84456 +      sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.84457 +      sqlite3RefillIndex(pParse, pIndex, -1);
 1.84458 +    }
 1.84459 +  }
 1.84460 +}
 1.84461 +#endif
 1.84462 +
 1.84463 +/*
 1.84464 +** Recompute all indices of all tables in all databases where the
 1.84465 +** indices use the collating sequence pColl.  If pColl==0 then recompute
 1.84466 +** all indices everywhere.
 1.84467 +*/
 1.84468 +#ifndef SQLITE_OMIT_REINDEX
 1.84469 +static void reindexDatabases(Parse *pParse, char const *zColl){
 1.84470 +  Db *pDb;                    /* A single database */
 1.84471 +  int iDb;                    /* The database index number */
 1.84472 +  sqlite3 *db = pParse->db;   /* The database connection */
 1.84473 +  HashElem *k;                /* For looping over tables in pDb */
 1.84474 +  Table *pTab;                /* A table in the database */
 1.84475 +
 1.84476 +  assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
 1.84477 +  for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
 1.84478 +    assert( pDb!=0 );
 1.84479 +    for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
 1.84480 +      pTab = (Table*)sqliteHashData(k);
 1.84481 +      reindexTable(pParse, pTab, zColl);
 1.84482 +    }
 1.84483 +  }
 1.84484 +}
 1.84485 +#endif
 1.84486 +
 1.84487 +/*
 1.84488 +** Generate code for the REINDEX command.
 1.84489 +**
 1.84490 +**        REINDEX                            -- 1
 1.84491 +**        REINDEX  <collation>               -- 2
 1.84492 +**        REINDEX  ?<database>.?<tablename>  -- 3
 1.84493 +**        REINDEX  ?<database>.?<indexname>  -- 4
 1.84494 +**
 1.84495 +** Form 1 causes all indices in all attached databases to be rebuilt.
 1.84496 +** Form 2 rebuilds all indices in all databases that use the named
 1.84497 +** collating function.  Forms 3 and 4 rebuild the named index or all
 1.84498 +** indices associated with the named table.
 1.84499 +*/
 1.84500 +#ifndef SQLITE_OMIT_REINDEX
 1.84501 +SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
 1.84502 +  CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
 1.84503 +  char *z;                    /* Name of a table or index */
 1.84504 +  const char *zDb;            /* Name of the database */
 1.84505 +  Table *pTab;                /* A table in the database */
 1.84506 +  Index *pIndex;              /* An index associated with pTab */
 1.84507 +  int iDb;                    /* The database index number */
 1.84508 +  sqlite3 *db = pParse->db;   /* The database connection */
 1.84509 +  Token *pObjName;            /* Name of the table or index to be reindexed */
 1.84510 +
 1.84511 +  /* Read the database schema. If an error occurs, leave an error message
 1.84512 +  ** and code in pParse and return NULL. */
 1.84513 +  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 1.84514 +    return;
 1.84515 +  }
 1.84516 +
 1.84517 +  if( pName1==0 ){
 1.84518 +    reindexDatabases(pParse, 0);
 1.84519 +    return;
 1.84520 +  }else if( NEVER(pName2==0) || pName2->z==0 ){
 1.84521 +    char *zColl;
 1.84522 +    assert( pName1->z );
 1.84523 +    zColl = sqlite3NameFromToken(pParse->db, pName1);
 1.84524 +    if( !zColl ) return;
 1.84525 +    pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 1.84526 +    if( pColl ){
 1.84527 +      reindexDatabases(pParse, zColl);
 1.84528 +      sqlite3DbFree(db, zColl);
 1.84529 +      return;
 1.84530 +    }
 1.84531 +    sqlite3DbFree(db, zColl);
 1.84532 +  }
 1.84533 +  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
 1.84534 +  if( iDb<0 ) return;
 1.84535 +  z = sqlite3NameFromToken(db, pObjName);
 1.84536 +  if( z==0 ) return;
 1.84537 +  zDb = db->aDb[iDb].zName;
 1.84538 +  pTab = sqlite3FindTable(db, z, zDb);
 1.84539 +  if( pTab ){
 1.84540 +    reindexTable(pParse, pTab, 0);
 1.84541 +    sqlite3DbFree(db, z);
 1.84542 +    return;
 1.84543 +  }
 1.84544 +  pIndex = sqlite3FindIndex(db, z, zDb);
 1.84545 +  sqlite3DbFree(db, z);
 1.84546 +  if( pIndex ){
 1.84547 +    sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.84548 +    sqlite3RefillIndex(pParse, pIndex, -1);
 1.84549 +    return;
 1.84550 +  }
 1.84551 +  sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
 1.84552 +}
 1.84553 +#endif
 1.84554 +
 1.84555 +/*
 1.84556 +** Return a dynamicly allocated KeyInfo structure that can be used
 1.84557 +** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
 1.84558 +**
 1.84559 +** If successful, a pointer to the new structure is returned. In this case
 1.84560 +** the caller is responsible for calling sqlite3DbFree(db, ) on the returned 
 1.84561 +** pointer. If an error occurs (out of memory or missing collation 
 1.84562 +** sequence), NULL is returned and the state of pParse updated to reflect
 1.84563 +** the error.
 1.84564 +*/
 1.84565 +SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
 1.84566 +  int i;
 1.84567 +  int nCol = pIdx->nColumn;
 1.84568 +  int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
 1.84569 +  sqlite3 *db = pParse->db;
 1.84570 +  KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
 1.84571 +
 1.84572 +  if( pKey ){
 1.84573 +    pKey->db = pParse->db;
 1.84574 +    pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
 1.84575 +    assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
 1.84576 +    for(i=0; i<nCol; i++){
 1.84577 +      char *zColl = pIdx->azColl[i];
 1.84578 +      assert( zColl );
 1.84579 +      pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
 1.84580 +      pKey->aSortOrder[i] = pIdx->aSortOrder[i];
 1.84581 +    }
 1.84582 +    pKey->nField = (u16)nCol;
 1.84583 +  }
 1.84584 +
 1.84585 +  if( pParse->nErr ){
 1.84586 +    sqlite3DbFree(db, pKey);
 1.84587 +    pKey = 0;
 1.84588 +  }
 1.84589 +  return pKey;
 1.84590 +}
 1.84591 +
 1.84592 +/************** End of build.c ***********************************************/
 1.84593 +/************** Begin file callback.c ****************************************/
 1.84594 +/*
 1.84595 +** 2005 May 23 
 1.84596 +**
 1.84597 +** The author disclaims copyright to this source code.  In place of
 1.84598 +** a legal notice, here is a blessing:
 1.84599 +**
 1.84600 +**    May you do good and not evil.
 1.84601 +**    May you find forgiveness for yourself and forgive others.
 1.84602 +**    May you share freely, never taking more than you give.
 1.84603 +**
 1.84604 +*************************************************************************
 1.84605 +**
 1.84606 +** This file contains functions used to access the internal hash tables
 1.84607 +** of user defined functions and collation sequences.
 1.84608 +*/
 1.84609 +
 1.84610 +
 1.84611 +/*
 1.84612 +** Invoke the 'collation needed' callback to request a collation sequence
 1.84613 +** in the encoding enc of name zName, length nName.
 1.84614 +*/
 1.84615 +static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
 1.84616 +  assert( !db->xCollNeeded || !db->xCollNeeded16 );
 1.84617 +  if( db->xCollNeeded ){
 1.84618 +    char *zExternal = sqlite3DbStrDup(db, zName);
 1.84619 +    if( !zExternal ) return;
 1.84620 +    db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
 1.84621 +    sqlite3DbFree(db, zExternal);
 1.84622 +  }
 1.84623 +#ifndef SQLITE_OMIT_UTF16
 1.84624 +  if( db->xCollNeeded16 ){
 1.84625 +    char const *zExternal;
 1.84626 +    sqlite3_value *pTmp = sqlite3ValueNew(db);
 1.84627 +    sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
 1.84628 +    zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
 1.84629 +    if( zExternal ){
 1.84630 +      db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
 1.84631 +    }
 1.84632 +    sqlite3ValueFree(pTmp);
 1.84633 +  }
 1.84634 +#endif
 1.84635 +}
 1.84636 +
 1.84637 +/*
 1.84638 +** This routine is called if the collation factory fails to deliver a
 1.84639 +** collation function in the best encoding but there may be other versions
 1.84640 +** of this collation function (for other text encodings) available. Use one
 1.84641 +** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
 1.84642 +** possible.
 1.84643 +*/
 1.84644 +static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
 1.84645 +  CollSeq *pColl2;
 1.84646 +  char *z = pColl->zName;
 1.84647 +  int i;
 1.84648 +  static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
 1.84649 +  for(i=0; i<3; i++){
 1.84650 +    pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
 1.84651 +    if( pColl2->xCmp!=0 ){
 1.84652 +      memcpy(pColl, pColl2, sizeof(CollSeq));
 1.84653 +      pColl->xDel = 0;         /* Do not copy the destructor */
 1.84654 +      return SQLITE_OK;
 1.84655 +    }
 1.84656 +  }
 1.84657 +  return SQLITE_ERROR;
 1.84658 +}
 1.84659 +
 1.84660 +/*
 1.84661 +** This function is responsible for invoking the collation factory callback
 1.84662 +** or substituting a collation sequence of a different encoding when the
 1.84663 +** requested collation sequence is not available in the desired encoding.
 1.84664 +** 
 1.84665 +** If it is not NULL, then pColl must point to the database native encoding 
 1.84666 +** collation sequence with name zName, length nName.
 1.84667 +**
 1.84668 +** The return value is either the collation sequence to be used in database
 1.84669 +** db for collation type name zName, length nName, or NULL, if no collation
 1.84670 +** sequence can be found.  If no collation is found, leave an error message.
 1.84671 +**
 1.84672 +** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
 1.84673 +*/
 1.84674 +SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
 1.84675 +  Parse *pParse,        /* Parsing context */
 1.84676 +  u8 enc,               /* The desired encoding for the collating sequence */
 1.84677 +  CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
 1.84678 +  const char *zName     /* Collating sequence name */
 1.84679 +){
 1.84680 +  CollSeq *p;
 1.84681 +  sqlite3 *db = pParse->db;
 1.84682 +
 1.84683 +  p = pColl;
 1.84684 +  if( !p ){
 1.84685 +    p = sqlite3FindCollSeq(db, enc, zName, 0);
 1.84686 +  }
 1.84687 +  if( !p || !p->xCmp ){
 1.84688 +    /* No collation sequence of this type for this encoding is registered.
 1.84689 +    ** Call the collation factory to see if it can supply us with one.
 1.84690 +    */
 1.84691 +    callCollNeeded(db, enc, zName);
 1.84692 +    p = sqlite3FindCollSeq(db, enc, zName, 0);
 1.84693 +  }
 1.84694 +  if( p && !p->xCmp && synthCollSeq(db, p) ){
 1.84695 +    p = 0;
 1.84696 +  }
 1.84697 +  assert( !p || p->xCmp );
 1.84698 +  if( p==0 ){
 1.84699 +    sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 1.84700 +  }
 1.84701 +  return p;
 1.84702 +}
 1.84703 +
 1.84704 +/*
 1.84705 +** This routine is called on a collation sequence before it is used to
 1.84706 +** check that it is defined. An undefined collation sequence exists when
 1.84707 +** a database is loaded that contains references to collation sequences
 1.84708 +** that have not been defined by sqlite3_create_collation() etc.
 1.84709 +**
 1.84710 +** If required, this routine calls the 'collation needed' callback to
 1.84711 +** request a definition of the collating sequence. If this doesn't work, 
 1.84712 +** an equivalent collating sequence that uses a text encoding different
 1.84713 +** from the main database is substituted, if one is available.
 1.84714 +*/
 1.84715 +SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
 1.84716 +  if( pColl ){
 1.84717 +    const char *zName = pColl->zName;
 1.84718 +    sqlite3 *db = pParse->db;
 1.84719 +    CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
 1.84720 +    if( !p ){
 1.84721 +      return SQLITE_ERROR;
 1.84722 +    }
 1.84723 +    assert( p==pColl );
 1.84724 +  }
 1.84725 +  return SQLITE_OK;
 1.84726 +}
 1.84727 +
 1.84728 +
 1.84729 +
 1.84730 +/*
 1.84731 +** Locate and return an entry from the db.aCollSeq hash table. If the entry
 1.84732 +** specified by zName and nName is not found and parameter 'create' is
 1.84733 +** true, then create a new entry. Otherwise return NULL.
 1.84734 +**
 1.84735 +** Each pointer stored in the sqlite3.aCollSeq hash table contains an
 1.84736 +** array of three CollSeq structures. The first is the collation sequence
 1.84737 +** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
 1.84738 +**
 1.84739 +** Stored immediately after the three collation sequences is a copy of
 1.84740 +** the collation sequence name. A pointer to this string is stored in
 1.84741 +** each collation sequence structure.
 1.84742 +*/
 1.84743 +static CollSeq *findCollSeqEntry(
 1.84744 +  sqlite3 *db,          /* Database connection */
 1.84745 +  const char *zName,    /* Name of the collating sequence */
 1.84746 +  int create            /* Create a new entry if true */
 1.84747 +){
 1.84748 +  CollSeq *pColl;
 1.84749 +  int nName = sqlite3Strlen30(zName);
 1.84750 +  pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
 1.84751 +
 1.84752 +  if( 0==pColl && create ){
 1.84753 +    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
 1.84754 +    if( pColl ){
 1.84755 +      CollSeq *pDel = 0;
 1.84756 +      pColl[0].zName = (char*)&pColl[3];
 1.84757 +      pColl[0].enc = SQLITE_UTF8;
 1.84758 +      pColl[1].zName = (char*)&pColl[3];
 1.84759 +      pColl[1].enc = SQLITE_UTF16LE;
 1.84760 +      pColl[2].zName = (char*)&pColl[3];
 1.84761 +      pColl[2].enc = SQLITE_UTF16BE;
 1.84762 +      memcpy(pColl[0].zName, zName, nName);
 1.84763 +      pColl[0].zName[nName] = 0;
 1.84764 +      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
 1.84765 +
 1.84766 +      /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
 1.84767 +      ** return the pColl pointer to be deleted (because it wasn't added
 1.84768 +      ** to the hash table).
 1.84769 +      */
 1.84770 +      assert( pDel==0 || pDel==pColl );
 1.84771 +      if( pDel!=0 ){
 1.84772 +        db->mallocFailed = 1;
 1.84773 +        sqlite3DbFree(db, pDel);
 1.84774 +        pColl = 0;
 1.84775 +      }
 1.84776 +    }
 1.84777 +  }
 1.84778 +  return pColl;
 1.84779 +}
 1.84780 +
 1.84781 +/*
 1.84782 +** Parameter zName points to a UTF-8 encoded string nName bytes long.
 1.84783 +** Return the CollSeq* pointer for the collation sequence named zName
 1.84784 +** for the encoding 'enc' from the database 'db'.
 1.84785 +**
 1.84786 +** If the entry specified is not found and 'create' is true, then create a
 1.84787 +** new entry.  Otherwise return NULL.
 1.84788 +**
 1.84789 +** A separate function sqlite3LocateCollSeq() is a wrapper around
 1.84790 +** this routine.  sqlite3LocateCollSeq() invokes the collation factory
 1.84791 +** if necessary and generates an error message if the collating sequence
 1.84792 +** cannot be found.
 1.84793 +**
 1.84794 +** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
 1.84795 +*/
 1.84796 +SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
 1.84797 +  sqlite3 *db,
 1.84798 +  u8 enc,
 1.84799 +  const char *zName,
 1.84800 +  int create
 1.84801 +){
 1.84802 +  CollSeq *pColl;
 1.84803 +  if( zName ){
 1.84804 +    pColl = findCollSeqEntry(db, zName, create);
 1.84805 +  }else{
 1.84806 +    pColl = db->pDfltColl;
 1.84807 +  }
 1.84808 +  assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
 1.84809 +  assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
 1.84810 +  if( pColl ) pColl += enc-1;
 1.84811 +  return pColl;
 1.84812 +}
 1.84813 +
 1.84814 +/* During the search for the best function definition, this procedure
 1.84815 +** is called to test how well the function passed as the first argument
 1.84816 +** matches the request for a function with nArg arguments in a system
 1.84817 +** that uses encoding enc. The value returned indicates how well the
 1.84818 +** request is matched. A higher value indicates a better match.
 1.84819 +**
 1.84820 +** If nArg is -1 that means to only return a match (non-zero) if p->nArg
 1.84821 +** is also -1.  In other words, we are searching for a function that
 1.84822 +** takes a variable number of arguments.
 1.84823 +**
 1.84824 +** If nArg is -2 that means that we are searching for any function 
 1.84825 +** regardless of the number of arguments it uses, so return a positive
 1.84826 +** match score for any
 1.84827 +**
 1.84828 +** The returned value is always between 0 and 6, as follows:
 1.84829 +**
 1.84830 +** 0: Not a match.
 1.84831 +** 1: UTF8/16 conversion required and function takes any number of arguments.
 1.84832 +** 2: UTF16 byte order change required and function takes any number of args.
 1.84833 +** 3: encoding matches and function takes any number of arguments
 1.84834 +** 4: UTF8/16 conversion required - argument count matches exactly
 1.84835 +** 5: UTF16 byte order conversion required - argument count matches exactly
 1.84836 +** 6: Perfect match:  encoding and argument count match exactly.
 1.84837 +**
 1.84838 +** If nArg==(-2) then any function with a non-null xStep or xFunc is
 1.84839 +** a perfect match and any function with both xStep and xFunc NULL is
 1.84840 +** a non-match.
 1.84841 +*/
 1.84842 +#define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
 1.84843 +static int matchQuality(
 1.84844 +  FuncDef *p,     /* The function we are evaluating for match quality */
 1.84845 +  int nArg,       /* Desired number of arguments.  (-1)==any */
 1.84846 +  u8 enc          /* Desired text encoding */
 1.84847 +){
 1.84848 +  int match;
 1.84849 +
 1.84850 +  /* nArg of -2 is a special case */
 1.84851 +  if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
 1.84852 +
 1.84853 +  /* Wrong number of arguments means "no match" */
 1.84854 +  if( p->nArg!=nArg && p->nArg>=0 ) return 0;
 1.84855 +
 1.84856 +  /* Give a better score to a function with a specific number of arguments
 1.84857 +  ** than to function that accepts any number of arguments. */
 1.84858 +  if( p->nArg==nArg ){
 1.84859 +    match = 4;
 1.84860 +  }else{
 1.84861 +    match = 1;
 1.84862 +  }
 1.84863 +
 1.84864 +  /* Bonus points if the text encoding matches */
 1.84865 +  if( enc==p->iPrefEnc ){
 1.84866 +    match += 2;  /* Exact encoding match */
 1.84867 +  }else if( (enc & p->iPrefEnc & 2)!=0 ){
 1.84868 +    match += 1;  /* Both are UTF16, but with different byte orders */
 1.84869 +  }
 1.84870 +
 1.84871 +  return match;
 1.84872 +}
 1.84873 +
 1.84874 +/*
 1.84875 +** Search a FuncDefHash for a function with the given name.  Return
 1.84876 +** a pointer to the matching FuncDef if found, or 0 if there is no match.
 1.84877 +*/
 1.84878 +static FuncDef *functionSearch(
 1.84879 +  FuncDefHash *pHash,  /* Hash table to search */
 1.84880 +  int h,               /* Hash of the name */
 1.84881 +  const char *zFunc,   /* Name of function */
 1.84882 +  int nFunc            /* Number of bytes in zFunc */
 1.84883 +){
 1.84884 +  FuncDef *p;
 1.84885 +  for(p=pHash->a[h]; p; p=p->pHash){
 1.84886 +    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
 1.84887 +      return p;
 1.84888 +    }
 1.84889 +  }
 1.84890 +  return 0;
 1.84891 +}
 1.84892 +
 1.84893 +/*
 1.84894 +** Insert a new FuncDef into a FuncDefHash hash table.
 1.84895 +*/
 1.84896 +SQLITE_PRIVATE void sqlite3FuncDefInsert(
 1.84897 +  FuncDefHash *pHash,  /* The hash table into which to insert */
 1.84898 +  FuncDef *pDef        /* The function definition to insert */
 1.84899 +){
 1.84900 +  FuncDef *pOther;
 1.84901 +  int nName = sqlite3Strlen30(pDef->zName);
 1.84902 +  u8 c1 = (u8)pDef->zName[0];
 1.84903 +  int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
 1.84904 +  pOther = functionSearch(pHash, h, pDef->zName, nName);
 1.84905 +  if( pOther ){
 1.84906 +    assert( pOther!=pDef && pOther->pNext!=pDef );
 1.84907 +    pDef->pNext = pOther->pNext;
 1.84908 +    pOther->pNext = pDef;
 1.84909 +  }else{
 1.84910 +    pDef->pNext = 0;
 1.84911 +    pDef->pHash = pHash->a[h];
 1.84912 +    pHash->a[h] = pDef;
 1.84913 +  }
 1.84914 +}
 1.84915 +  
 1.84916 +  
 1.84917 +
 1.84918 +/*
 1.84919 +** Locate a user function given a name, a number of arguments and a flag
 1.84920 +** indicating whether the function prefers UTF-16 over UTF-8.  Return a
 1.84921 +** pointer to the FuncDef structure that defines that function, or return
 1.84922 +** NULL if the function does not exist.
 1.84923 +**
 1.84924 +** If the createFlag argument is true, then a new (blank) FuncDef
 1.84925 +** structure is created and liked into the "db" structure if a
 1.84926 +** no matching function previously existed.
 1.84927 +**
 1.84928 +** If nArg is -2, then the first valid function found is returned.  A
 1.84929 +** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
 1.84930 +** case is used to see if zName is a valid function name for some number
 1.84931 +** of arguments.  If nArg is -2, then createFlag must be 0.
 1.84932 +**
 1.84933 +** If createFlag is false, then a function with the required name and
 1.84934 +** number of arguments may be returned even if the eTextRep flag does not
 1.84935 +** match that requested.
 1.84936 +*/
 1.84937 +SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
 1.84938 +  sqlite3 *db,       /* An open database */
 1.84939 +  const char *zName, /* Name of the function.  Not null-terminated */
 1.84940 +  int nName,         /* Number of characters in the name */
 1.84941 +  int nArg,          /* Number of arguments.  -1 means any number */
 1.84942 +  u8 enc,            /* Preferred text encoding */
 1.84943 +  u8 createFlag      /* Create new entry if true and does not otherwise exist */
 1.84944 +){
 1.84945 +  FuncDef *p;         /* Iterator variable */
 1.84946 +  FuncDef *pBest = 0; /* Best match found so far */
 1.84947 +  int bestScore = 0;  /* Score of best match */
 1.84948 +  int h;              /* Hash value */
 1.84949 +
 1.84950 +  assert( nArg>=(-2) );
 1.84951 +  assert( nArg>=(-1) || createFlag==0 );
 1.84952 +  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
 1.84953 +  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
 1.84954 +
 1.84955 +  /* First search for a match amongst the application-defined functions.
 1.84956 +  */
 1.84957 +  p = functionSearch(&db->aFunc, h, zName, nName);
 1.84958 +  while( p ){
 1.84959 +    int score = matchQuality(p, nArg, enc);
 1.84960 +    if( score>bestScore ){
 1.84961 +      pBest = p;
 1.84962 +      bestScore = score;
 1.84963 +    }
 1.84964 +    p = p->pNext;
 1.84965 +  }
 1.84966 +
 1.84967 +  /* If no match is found, search the built-in functions.
 1.84968 +  **
 1.84969 +  ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
 1.84970 +  ** functions even if a prior app-defined function was found.  And give
 1.84971 +  ** priority to built-in functions.
 1.84972 +  **
 1.84973 +  ** Except, if createFlag is true, that means that we are trying to
 1.84974 +  ** install a new function.  Whatever FuncDef structure is returned it will
 1.84975 +  ** have fields overwritten with new information appropriate for the
 1.84976 +  ** new function.  But the FuncDefs for built-in functions are read-only.
 1.84977 +  ** So we must not search for built-ins when creating a new function.
 1.84978 +  */ 
 1.84979 +  if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
 1.84980 +    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 1.84981 +    bestScore = 0;
 1.84982 +    p = functionSearch(pHash, h, zName, nName);
 1.84983 +    while( p ){
 1.84984 +      int score = matchQuality(p, nArg, enc);
 1.84985 +      if( score>bestScore ){
 1.84986 +        pBest = p;
 1.84987 +        bestScore = score;
 1.84988 +      }
 1.84989 +      p = p->pNext;
 1.84990 +    }
 1.84991 +  }
 1.84992 +
 1.84993 +  /* If the createFlag parameter is true and the search did not reveal an
 1.84994 +  ** exact match for the name, number of arguments and encoding, then add a
 1.84995 +  ** new entry to the hash table and return it.
 1.84996 +  */
 1.84997 +  if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
 1.84998 +      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
 1.84999 +    pBest->zName = (char *)&pBest[1];
 1.85000 +    pBest->nArg = (u16)nArg;
 1.85001 +    pBest->iPrefEnc = enc;
 1.85002 +    memcpy(pBest->zName, zName, nName);
 1.85003 +    pBest->zName[nName] = 0;
 1.85004 +    sqlite3FuncDefInsert(&db->aFunc, pBest);
 1.85005 +  }
 1.85006 +
 1.85007 +  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
 1.85008 +    return pBest;
 1.85009 +  }
 1.85010 +  return 0;
 1.85011 +}
 1.85012 +
 1.85013 +/*
 1.85014 +** Free all resources held by the schema structure. The void* argument points
 1.85015 +** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 
 1.85016 +** pointer itself, it just cleans up subsidiary resources (i.e. the contents
 1.85017 +** of the schema hash tables).
 1.85018 +**
 1.85019 +** The Schema.cache_size variable is not cleared.
 1.85020 +*/
 1.85021 +SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
 1.85022 +  Hash temp1;
 1.85023 +  Hash temp2;
 1.85024 +  HashElem *pElem;
 1.85025 +  Schema *pSchema = (Schema *)p;
 1.85026 +
 1.85027 +  temp1 = pSchema->tblHash;
 1.85028 +  temp2 = pSchema->trigHash;
 1.85029 +  sqlite3HashInit(&pSchema->trigHash);
 1.85030 +  sqlite3HashClear(&pSchema->idxHash);
 1.85031 +  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
 1.85032 +    sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
 1.85033 +  }
 1.85034 +  sqlite3HashClear(&temp2);
 1.85035 +  sqlite3HashInit(&pSchema->tblHash);
 1.85036 +  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
 1.85037 +    Table *pTab = sqliteHashData(pElem);
 1.85038 +    sqlite3DeleteTable(0, pTab);
 1.85039 +  }
 1.85040 +  sqlite3HashClear(&temp1);
 1.85041 +  sqlite3HashClear(&pSchema->fkeyHash);
 1.85042 +  pSchema->pSeqTab = 0;
 1.85043 +  if( pSchema->flags & DB_SchemaLoaded ){
 1.85044 +    pSchema->iGeneration++;
 1.85045 +    pSchema->flags &= ~DB_SchemaLoaded;
 1.85046 +  }
 1.85047 +}
 1.85048 +
 1.85049 +/*
 1.85050 +** Find and return the schema associated with a BTree.  Create
 1.85051 +** a new one if necessary.
 1.85052 +*/
 1.85053 +SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
 1.85054 +  Schema * p;
 1.85055 +  if( pBt ){
 1.85056 +    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
 1.85057 +  }else{
 1.85058 +    p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
 1.85059 +  }
 1.85060 +  if( !p ){
 1.85061 +    db->mallocFailed = 1;
 1.85062 +  }else if ( 0==p->file_format ){
 1.85063 +    sqlite3HashInit(&p->tblHash);
 1.85064 +    sqlite3HashInit(&p->idxHash);
 1.85065 +    sqlite3HashInit(&p->trigHash);
 1.85066 +    sqlite3HashInit(&p->fkeyHash);
 1.85067 +    p->enc = SQLITE_UTF8;
 1.85068 +  }
 1.85069 +  return p;
 1.85070 +}
 1.85071 +
 1.85072 +/************** End of callback.c ********************************************/
 1.85073 +/************** Begin file delete.c ******************************************/
 1.85074 +/*
 1.85075 +** 2001 September 15
 1.85076 +**
 1.85077 +** The author disclaims copyright to this source code.  In place of
 1.85078 +** a legal notice, here is a blessing:
 1.85079 +**
 1.85080 +**    May you do good and not evil.
 1.85081 +**    May you find forgiveness for yourself and forgive others.
 1.85082 +**    May you share freely, never taking more than you give.
 1.85083 +**
 1.85084 +*************************************************************************
 1.85085 +** This file contains C code routines that are called by the parser
 1.85086 +** in order to generate code for DELETE FROM statements.
 1.85087 +*/
 1.85088 +
 1.85089 +/*
 1.85090 +** While a SrcList can in general represent multiple tables and subqueries
 1.85091 +** (as in the FROM clause of a SELECT statement) in this case it contains
 1.85092 +** the name of a single table, as one might find in an INSERT, DELETE,
 1.85093 +** or UPDATE statement.  Look up that table in the symbol table and
 1.85094 +** return a pointer.  Set an error message and return NULL if the table 
 1.85095 +** name is not found or if any other error occurs.
 1.85096 +**
 1.85097 +** The following fields are initialized appropriate in pSrc:
 1.85098 +**
 1.85099 +**    pSrc->a[0].pTab       Pointer to the Table object
 1.85100 +**    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
 1.85101 +**
 1.85102 +*/
 1.85103 +SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
 1.85104 +  struct SrcList_item *pItem = pSrc->a;
 1.85105 +  Table *pTab;
 1.85106 +  assert( pItem && pSrc->nSrc==1 );
 1.85107 +  pTab = sqlite3LocateTableItem(pParse, 0, pItem);
 1.85108 +  sqlite3DeleteTable(pParse->db, pItem->pTab);
 1.85109 +  pItem->pTab = pTab;
 1.85110 +  if( pTab ){
 1.85111 +    pTab->nRef++;
 1.85112 +  }
 1.85113 +  if( sqlite3IndexedByLookup(pParse, pItem) ){
 1.85114 +    pTab = 0;
 1.85115 +  }
 1.85116 +  return pTab;
 1.85117 +}
 1.85118 +
 1.85119 +/*
 1.85120 +** Check to make sure the given table is writable.  If it is not
 1.85121 +** writable, generate an error message and return 1.  If it is
 1.85122 +** writable return 0;
 1.85123 +*/
 1.85124 +SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
 1.85125 +  /* A table is not writable under the following circumstances:
 1.85126 +  **
 1.85127 +  **   1) It is a virtual table and no implementation of the xUpdate method
 1.85128 +  **      has been provided, or
 1.85129 +  **   2) It is a system table (i.e. sqlite_master), this call is not
 1.85130 +  **      part of a nested parse and writable_schema pragma has not 
 1.85131 +  **      been specified.
 1.85132 +  **
 1.85133 +  ** In either case leave an error message in pParse and return non-zero.
 1.85134 +  */
 1.85135 +  if( ( IsVirtual(pTab) 
 1.85136 +     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
 1.85137 +   || ( (pTab->tabFlags & TF_Readonly)!=0
 1.85138 +     && (pParse->db->flags & SQLITE_WriteSchema)==0
 1.85139 +     && pParse->nested==0 )
 1.85140 +  ){
 1.85141 +    sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
 1.85142 +    return 1;
 1.85143 +  }
 1.85144 +
 1.85145 +#ifndef SQLITE_OMIT_VIEW
 1.85146 +  if( !viewOk && pTab->pSelect ){
 1.85147 +    sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
 1.85148 +    return 1;
 1.85149 +  }
 1.85150 +#endif
 1.85151 +  return 0;
 1.85152 +}
 1.85153 +
 1.85154 +
 1.85155 +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 1.85156 +/*
 1.85157 +** Evaluate a view and store its result in an ephemeral table.  The
 1.85158 +** pWhere argument is an optional WHERE clause that restricts the
 1.85159 +** set of rows in the view that are to be added to the ephemeral table.
 1.85160 +*/
 1.85161 +SQLITE_PRIVATE void sqlite3MaterializeView(
 1.85162 +  Parse *pParse,       /* Parsing context */
 1.85163 +  Table *pView,        /* View definition */
 1.85164 +  Expr *pWhere,        /* Optional WHERE clause to be added */
 1.85165 +  int iCur             /* Cursor number for ephemerial table */
 1.85166 +){
 1.85167 +  SelectDest dest;
 1.85168 +  Select *pDup;
 1.85169 +  sqlite3 *db = pParse->db;
 1.85170 +
 1.85171 +  pDup = sqlite3SelectDup(db, pView->pSelect, 0);
 1.85172 +  if( pWhere ){
 1.85173 +    SrcList *pFrom;
 1.85174 +    
 1.85175 +    pWhere = sqlite3ExprDup(db, pWhere, 0);
 1.85176 +    pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
 1.85177 +    if( pFrom ){
 1.85178 +      assert( pFrom->nSrc==1 );
 1.85179 +      pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
 1.85180 +      pFrom->a[0].pSelect = pDup;
 1.85181 +      assert( pFrom->a[0].pOn==0 );
 1.85182 +      assert( pFrom->a[0].pUsing==0 );
 1.85183 +    }else{
 1.85184 +      sqlite3SelectDelete(db, pDup);
 1.85185 +    }
 1.85186 +    pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
 1.85187 +    if( pDup ) pDup->selFlags |= SF_Materialize;
 1.85188 +  }
 1.85189 +  sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
 1.85190 +  sqlite3Select(pParse, pDup, &dest);
 1.85191 +  sqlite3SelectDelete(db, pDup);
 1.85192 +}
 1.85193 +#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
 1.85194 +
 1.85195 +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 1.85196 +/*
 1.85197 +** Generate an expression tree to implement the WHERE, ORDER BY,
 1.85198 +** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
 1.85199 +**
 1.85200 +**     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
 1.85201 +**                            \__________________________/
 1.85202 +**                               pLimitWhere (pInClause)
 1.85203 +*/
 1.85204 +SQLITE_PRIVATE Expr *sqlite3LimitWhere(
 1.85205 +  Parse *pParse,               /* The parser context */
 1.85206 +  SrcList *pSrc,               /* the FROM clause -- which tables to scan */
 1.85207 +  Expr *pWhere,                /* The WHERE clause.  May be null */
 1.85208 +  ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
 1.85209 +  Expr *pLimit,                /* The LIMIT clause.  May be null */
 1.85210 +  Expr *pOffset,               /* The OFFSET clause.  May be null */
 1.85211 +  char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
 1.85212 +){
 1.85213 +  Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
 1.85214 +  Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
 1.85215 +  Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
 1.85216 +  ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
 1.85217 +  SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
 1.85218 +  Select *pSelect = NULL;      /* Complete SELECT tree */
 1.85219 +
 1.85220 +  /* Check that there isn't an ORDER BY without a LIMIT clause.
 1.85221 +  */
 1.85222 +  if( pOrderBy && (pLimit == 0) ) {
 1.85223 +    sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
 1.85224 +    goto limit_where_cleanup_2;
 1.85225 +  }
 1.85226 +
 1.85227 +  /* We only need to generate a select expression if there
 1.85228 +  ** is a limit/offset term to enforce.
 1.85229 +  */
 1.85230 +  if( pLimit == 0 ) {
 1.85231 +    /* if pLimit is null, pOffset will always be null as well. */
 1.85232 +    assert( pOffset == 0 );
 1.85233 +    return pWhere;
 1.85234 +  }
 1.85235 +
 1.85236 +  /* Generate a select expression tree to enforce the limit/offset 
 1.85237 +  ** term for the DELETE or UPDATE statement.  For example:
 1.85238 +  **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
 1.85239 +  ** becomes:
 1.85240 +  **   DELETE FROM table_a WHERE rowid IN ( 
 1.85241 +  **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
 1.85242 +  **   );
 1.85243 +  */
 1.85244 +
 1.85245 +  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
 1.85246 +  if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
 1.85247 +  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
 1.85248 +  if( pEList == 0 ) goto limit_where_cleanup_2;
 1.85249 +
 1.85250 +  /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
 1.85251 +  ** and the SELECT subtree. */
 1.85252 +  pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
 1.85253 +  if( pSelectSrc == 0 ) {
 1.85254 +    sqlite3ExprListDelete(pParse->db, pEList);
 1.85255 +    goto limit_where_cleanup_2;
 1.85256 +  }
 1.85257 +
 1.85258 +  /* generate the SELECT expression tree. */
 1.85259 +  pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
 1.85260 +                             pOrderBy,0,pLimit,pOffset);
 1.85261 +  if( pSelect == 0 ) return 0;
 1.85262 +
 1.85263 +  /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
 1.85264 +  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
 1.85265 +  if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
 1.85266 +  pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
 1.85267 +  if( pInClause == 0 ) goto limit_where_cleanup_1;
 1.85268 +
 1.85269 +  pInClause->x.pSelect = pSelect;
 1.85270 +  pInClause->flags |= EP_xIsSelect;
 1.85271 +  sqlite3ExprSetHeight(pParse, pInClause);
 1.85272 +  return pInClause;
 1.85273 +
 1.85274 +  /* something went wrong. clean up anything allocated. */
 1.85275 +limit_where_cleanup_1:
 1.85276 +  sqlite3SelectDelete(pParse->db, pSelect);
 1.85277 +  return 0;
 1.85278 +
 1.85279 +limit_where_cleanup_2:
 1.85280 +  sqlite3ExprDelete(pParse->db, pWhere);
 1.85281 +  sqlite3ExprListDelete(pParse->db, pOrderBy);
 1.85282 +  sqlite3ExprDelete(pParse->db, pLimit);
 1.85283 +  sqlite3ExprDelete(pParse->db, pOffset);
 1.85284 +  return 0;
 1.85285 +}
 1.85286 +#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
 1.85287 +
 1.85288 +/*
 1.85289 +** Generate code for a DELETE FROM statement.
 1.85290 +**
 1.85291 +**     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
 1.85292 +**                 \________/       \________________/
 1.85293 +**                  pTabList              pWhere
 1.85294 +*/
 1.85295 +SQLITE_PRIVATE void sqlite3DeleteFrom(
 1.85296 +  Parse *pParse,         /* The parser context */
 1.85297 +  SrcList *pTabList,     /* The table from which we should delete things */
 1.85298 +  Expr *pWhere           /* The WHERE clause.  May be null */
 1.85299 +){
 1.85300 +  Vdbe *v;               /* The virtual database engine */
 1.85301 +  Table *pTab;           /* The table from which records will be deleted */
 1.85302 +  const char *zDb;       /* Name of database holding pTab */
 1.85303 +  int end, addr = 0;     /* A couple addresses of generated code */
 1.85304 +  int i;                 /* Loop counter */
 1.85305 +  WhereInfo *pWInfo;     /* Information about the WHERE clause */
 1.85306 +  Index *pIdx;           /* For looping over indices of the table */
 1.85307 +  int iCur;              /* VDBE Cursor number for pTab */
 1.85308 +  sqlite3 *db;           /* Main database structure */
 1.85309 +  AuthContext sContext;  /* Authorization context */
 1.85310 +  NameContext sNC;       /* Name context to resolve expressions in */
 1.85311 +  int iDb;               /* Database number */
 1.85312 +  int memCnt = -1;       /* Memory cell used for change counting */
 1.85313 +  int rcauth;            /* Value returned by authorization callback */
 1.85314 +
 1.85315 +#ifndef SQLITE_OMIT_TRIGGER
 1.85316 +  int isView;                  /* True if attempting to delete from a view */
 1.85317 +  Trigger *pTrigger;           /* List of table triggers, if required */
 1.85318 +#endif
 1.85319 +
 1.85320 +  memset(&sContext, 0, sizeof(sContext));
 1.85321 +  db = pParse->db;
 1.85322 +  if( pParse->nErr || db->mallocFailed ){
 1.85323 +    goto delete_from_cleanup;
 1.85324 +  }
 1.85325 +  assert( pTabList->nSrc==1 );
 1.85326 +
 1.85327 +  /* Locate the table which we want to delete.  This table has to be
 1.85328 +  ** put in an SrcList structure because some of the subroutines we
 1.85329 +  ** will be calling are designed to work with multiple tables and expect
 1.85330 +  ** an SrcList* parameter instead of just a Table* parameter.
 1.85331 +  */
 1.85332 +  pTab = sqlite3SrcListLookup(pParse, pTabList);
 1.85333 +  if( pTab==0 )  goto delete_from_cleanup;
 1.85334 +
 1.85335 +  /* Figure out if we have any triggers and if the table being
 1.85336 +  ** deleted from is a view
 1.85337 +  */
 1.85338 +#ifndef SQLITE_OMIT_TRIGGER
 1.85339 +  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 1.85340 +  isView = pTab->pSelect!=0;
 1.85341 +#else
 1.85342 +# define pTrigger 0
 1.85343 +# define isView 0
 1.85344 +#endif
 1.85345 +#ifdef SQLITE_OMIT_VIEW
 1.85346 +# undef isView
 1.85347 +# define isView 0
 1.85348 +#endif
 1.85349 +
 1.85350 +  /* If pTab is really a view, make sure it has been initialized.
 1.85351 +  */
 1.85352 +  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 1.85353 +    goto delete_from_cleanup;
 1.85354 +  }
 1.85355 +
 1.85356 +  if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
 1.85357 +    goto delete_from_cleanup;
 1.85358 +  }
 1.85359 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.85360 +  assert( iDb<db->nDb );
 1.85361 +  zDb = db->aDb[iDb].zName;
 1.85362 +  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
 1.85363 +  assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
 1.85364 +  if( rcauth==SQLITE_DENY ){
 1.85365 +    goto delete_from_cleanup;
 1.85366 +  }
 1.85367 +  assert(!isView || pTrigger);
 1.85368 +
 1.85369 +  /* Assign  cursor number to the table and all its indices.
 1.85370 +  */
 1.85371 +  assert( pTabList->nSrc==1 );
 1.85372 +  iCur = pTabList->a[0].iCursor = pParse->nTab++;
 1.85373 +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.85374 +    pParse->nTab++;
 1.85375 +  }
 1.85376 +
 1.85377 +  /* Start the view context
 1.85378 +  */
 1.85379 +  if( isView ){
 1.85380 +    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
 1.85381 +  }
 1.85382 +
 1.85383 +  /* Begin generating code.
 1.85384 +  */
 1.85385 +  v = sqlite3GetVdbe(pParse);
 1.85386 +  if( v==0 ){
 1.85387 +    goto delete_from_cleanup;
 1.85388 +  }
 1.85389 +  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 1.85390 +  sqlite3BeginWriteOperation(pParse, 1, iDb);
 1.85391 +
 1.85392 +  /* If we are trying to delete from a view, realize that view into
 1.85393 +  ** a ephemeral table.
 1.85394 +  */
 1.85395 +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 1.85396 +  if( isView ){
 1.85397 +    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
 1.85398 +  }
 1.85399 +#endif
 1.85400 +
 1.85401 +  /* Resolve the column names in the WHERE clause.
 1.85402 +  */
 1.85403 +  memset(&sNC, 0, sizeof(sNC));
 1.85404 +  sNC.pParse = pParse;
 1.85405 +  sNC.pSrcList = pTabList;
 1.85406 +  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
 1.85407 +    goto delete_from_cleanup;
 1.85408 +  }
 1.85409 +
 1.85410 +  /* Initialize the counter of the number of rows deleted, if
 1.85411 +  ** we are counting rows.
 1.85412 +  */
 1.85413 +  if( db->flags & SQLITE_CountRows ){
 1.85414 +    memCnt = ++pParse->nMem;
 1.85415 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
 1.85416 +  }
 1.85417 +
 1.85418 +#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 1.85419 +  /* Special case: A DELETE without a WHERE clause deletes everything.
 1.85420 +  ** It is easier just to erase the whole table. Prior to version 3.6.5,
 1.85421 +  ** this optimization caused the row change count (the value returned by 
 1.85422 +  ** API function sqlite3_count_changes) to be set incorrectly.  */
 1.85423 +  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
 1.85424 +   && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
 1.85425 +  ){
 1.85426 +    assert( !isView );
 1.85427 +    sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
 1.85428 +                      pTab->zName, P4_STATIC);
 1.85429 +    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.85430 +      assert( pIdx->pSchema==pTab->pSchema );
 1.85431 +      sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
 1.85432 +    }
 1.85433 +  }else
 1.85434 +#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
 1.85435 +  /* The usual case: There is a WHERE clause so we have to scan through
 1.85436 +  ** the table and pick which records to delete.
 1.85437 +  */
 1.85438 +  {
 1.85439 +    int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
 1.85440 +    int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
 1.85441 +    int regRowid;                   /* Actual register containing rowids */
 1.85442 +
 1.85443 +    /* Collect rowids of every row to be deleted.
 1.85444 +    */
 1.85445 +    sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
 1.85446 +    pWInfo = sqlite3WhereBegin(
 1.85447 +        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
 1.85448 +    );
 1.85449 +    if( pWInfo==0 ) goto delete_from_cleanup;
 1.85450 +    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
 1.85451 +    sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
 1.85452 +    if( db->flags & SQLITE_CountRows ){
 1.85453 +      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
 1.85454 +    }
 1.85455 +    sqlite3WhereEnd(pWInfo);
 1.85456 +
 1.85457 +    /* Delete every item whose key was written to the list during the
 1.85458 +    ** database scan.  We have to delete items after the scan is complete
 1.85459 +    ** because deleting an item can change the scan order.  */
 1.85460 +    end = sqlite3VdbeMakeLabel(v);
 1.85461 +
 1.85462 +    /* Unless this is a view, open cursors for the table we are 
 1.85463 +    ** deleting from and all its indices. If this is a view, then the
 1.85464 +    ** only effect this statement has is to fire the INSTEAD OF 
 1.85465 +    ** triggers.  */
 1.85466 +    if( !isView ){
 1.85467 +      sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
 1.85468 +    }
 1.85469 +
 1.85470 +    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
 1.85471 +
 1.85472 +    /* Delete the row */
 1.85473 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.85474 +    if( IsVirtual(pTab) ){
 1.85475 +      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 1.85476 +      sqlite3VtabMakeWritable(pParse, pTab);
 1.85477 +      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
 1.85478 +      sqlite3VdbeChangeP5(v, OE_Abort);
 1.85479 +      sqlite3MayAbort(pParse);
 1.85480 +    }else
 1.85481 +#endif
 1.85482 +    {
 1.85483 +      int count = (pParse->nested==0);    /* True to count changes */
 1.85484 +      sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
 1.85485 +    }
 1.85486 +
 1.85487 +    /* End of the delete loop */
 1.85488 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
 1.85489 +    sqlite3VdbeResolveLabel(v, end);
 1.85490 +
 1.85491 +    /* Close the cursors open on the table and its indexes. */
 1.85492 +    if( !isView && !IsVirtual(pTab) ){
 1.85493 +      for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
 1.85494 +        sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
 1.85495 +      }
 1.85496 +      sqlite3VdbeAddOp1(v, OP_Close, iCur);
 1.85497 +    }
 1.85498 +  }
 1.85499 +
 1.85500 +  /* Update the sqlite_sequence table by storing the content of the
 1.85501 +  ** maximum rowid counter values recorded while inserting into
 1.85502 +  ** autoincrement tables.
 1.85503 +  */
 1.85504 +  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 1.85505 +    sqlite3AutoincrementEnd(pParse);
 1.85506 +  }
 1.85507 +
 1.85508 +  /* Return the number of rows that were deleted. If this routine is 
 1.85509 +  ** generating code because of a call to sqlite3NestedParse(), do not
 1.85510 +  ** invoke the callback function.
 1.85511 +  */
 1.85512 +  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
 1.85513 +    sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
 1.85514 +    sqlite3VdbeSetNumCols(v, 1);
 1.85515 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
 1.85516 +  }
 1.85517 +
 1.85518 +delete_from_cleanup:
 1.85519 +  sqlite3AuthContextPop(&sContext);
 1.85520 +  sqlite3SrcListDelete(db, pTabList);
 1.85521 +  sqlite3ExprDelete(db, pWhere);
 1.85522 +  return;
 1.85523 +}
 1.85524 +/* Make sure "isView" and other macros defined above are undefined. Otherwise
 1.85525 +** thely may interfere with compilation of other functions in this file
 1.85526 +** (or in another file, if this file becomes part of the amalgamation).  */
 1.85527 +#ifdef isView
 1.85528 + #undef isView
 1.85529 +#endif
 1.85530 +#ifdef pTrigger
 1.85531 + #undef pTrigger
 1.85532 +#endif
 1.85533 +
 1.85534 +/*
 1.85535 +** This routine generates VDBE code that causes a single row of a
 1.85536 +** single table to be deleted.
 1.85537 +**
 1.85538 +** The VDBE must be in a particular state when this routine is called.
 1.85539 +** These are the requirements:
 1.85540 +**
 1.85541 +**   1.  A read/write cursor pointing to pTab, the table containing the row
 1.85542 +**       to be deleted, must be opened as cursor number $iCur.
 1.85543 +**
 1.85544 +**   2.  Read/write cursors for all indices of pTab must be open as
 1.85545 +**       cursor number base+i for the i-th index.
 1.85546 +**
 1.85547 +**   3.  The record number of the row to be deleted must be stored in
 1.85548 +**       memory cell iRowid.
 1.85549 +**
 1.85550 +** This routine generates code to remove both the table record and all 
 1.85551 +** index entries that point to that record.
 1.85552 +*/
 1.85553 +SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 1.85554 +  Parse *pParse,     /* Parsing context */
 1.85555 +  Table *pTab,       /* Table containing the row to be deleted */
 1.85556 +  int iCur,          /* Cursor number for the table */
 1.85557 +  int iRowid,        /* Memory cell that contains the rowid to delete */
 1.85558 +  int count,         /* If non-zero, increment the row change counter */
 1.85559 +  Trigger *pTrigger, /* List of triggers to (potentially) fire */
 1.85560 +  int onconf         /* Default ON CONFLICT policy for triggers */
 1.85561 +){
 1.85562 +  Vdbe *v = pParse->pVdbe;        /* Vdbe */
 1.85563 +  int iOld = 0;                   /* First register in OLD.* array */
 1.85564 +  int iLabel;                     /* Label resolved to end of generated code */
 1.85565 +
 1.85566 +  /* Vdbe is guaranteed to have been allocated by this stage. */
 1.85567 +  assert( v );
 1.85568 +
 1.85569 +  /* Seek cursor iCur to the row to delete. If this row no longer exists 
 1.85570 +  ** (this can happen if a trigger program has already deleted it), do
 1.85571 +  ** not attempt to delete it or fire any DELETE triggers.  */
 1.85572 +  iLabel = sqlite3VdbeMakeLabel(v);
 1.85573 +  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
 1.85574 + 
 1.85575 +  /* If there are any triggers to fire, allocate a range of registers to
 1.85576 +  ** use for the old.* references in the triggers.  */
 1.85577 +  if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
 1.85578 +    u32 mask;                     /* Mask of OLD.* columns in use */
 1.85579 +    int iCol;                     /* Iterator used while populating OLD.* */
 1.85580 +
 1.85581 +    /* TODO: Could use temporary registers here. Also could attempt to
 1.85582 +    ** avoid copying the contents of the rowid register.  */
 1.85583 +    mask = sqlite3TriggerColmask(
 1.85584 +        pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
 1.85585 +    );
 1.85586 +    mask |= sqlite3FkOldmask(pParse, pTab);
 1.85587 +    iOld = pParse->nMem+1;
 1.85588 +    pParse->nMem += (1 + pTab->nCol);
 1.85589 +
 1.85590 +    /* Populate the OLD.* pseudo-table register array. These values will be 
 1.85591 +    ** used by any BEFORE and AFTER triggers that exist.  */
 1.85592 +    sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
 1.85593 +    for(iCol=0; iCol<pTab->nCol; iCol++){
 1.85594 +      if( mask==0xffffffff || mask&(1<<iCol) ){
 1.85595 +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
 1.85596 +      }
 1.85597 +    }
 1.85598 +
 1.85599 +    /* Invoke BEFORE DELETE trigger programs. */
 1.85600 +    sqlite3CodeRowTrigger(pParse, pTrigger, 
 1.85601 +        TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
 1.85602 +    );
 1.85603 +
 1.85604 +    /* Seek the cursor to the row to be deleted again. It may be that
 1.85605 +    ** the BEFORE triggers coded above have already removed the row
 1.85606 +    ** being deleted. Do not attempt to delete the row a second time, and 
 1.85607 +    ** do not fire AFTER triggers.  */
 1.85608 +    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
 1.85609 +
 1.85610 +    /* Do FK processing. This call checks that any FK constraints that
 1.85611 +    ** refer to this table (i.e. constraints attached to other tables) 
 1.85612 +    ** are not violated by deleting this row.  */
 1.85613 +    sqlite3FkCheck(pParse, pTab, iOld, 0);
 1.85614 +  }
 1.85615 +
 1.85616 +  /* Delete the index and table entries. Skip this step if pTab is really
 1.85617 +  ** a view (in which case the only effect of the DELETE statement is to
 1.85618 +  ** fire the INSTEAD OF triggers).  */ 
 1.85619 +  if( pTab->pSelect==0 ){
 1.85620 +    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
 1.85621 +    sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
 1.85622 +    if( count ){
 1.85623 +      sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 1.85624 +    }
 1.85625 +  }
 1.85626 +
 1.85627 +  /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
 1.85628 +  ** handle rows (possibly in other tables) that refer via a foreign key
 1.85629 +  ** to the row just deleted. */ 
 1.85630 +  sqlite3FkActions(pParse, pTab, 0, iOld);
 1.85631 +
 1.85632 +  /* Invoke AFTER DELETE trigger programs. */
 1.85633 +  sqlite3CodeRowTrigger(pParse, pTrigger, 
 1.85634 +      TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
 1.85635 +  );
 1.85636 +
 1.85637 +  /* Jump here if the row had already been deleted before any BEFORE
 1.85638 +  ** trigger programs were invoked. Or if a trigger program throws a 
 1.85639 +  ** RAISE(IGNORE) exception.  */
 1.85640 +  sqlite3VdbeResolveLabel(v, iLabel);
 1.85641 +}
 1.85642 +
 1.85643 +/*
 1.85644 +** This routine generates VDBE code that causes the deletion of all
 1.85645 +** index entries associated with a single row of a single table.
 1.85646 +**
 1.85647 +** The VDBE must be in a particular state when this routine is called.
 1.85648 +** These are the requirements:
 1.85649 +**
 1.85650 +**   1.  A read/write cursor pointing to pTab, the table containing the row
 1.85651 +**       to be deleted, must be opened as cursor number "iCur".
 1.85652 +**
 1.85653 +**   2.  Read/write cursors for all indices of pTab must be open as
 1.85654 +**       cursor number iCur+i for the i-th index.
 1.85655 +**
 1.85656 +**   3.  The "iCur" cursor must be pointing to the row that is to be
 1.85657 +**       deleted.
 1.85658 +*/
 1.85659 +SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
 1.85660 +  Parse *pParse,     /* Parsing and code generating context */
 1.85661 +  Table *pTab,       /* Table containing the row to be deleted */
 1.85662 +  int iCur,          /* Cursor number for the table */
 1.85663 +  int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
 1.85664 +){
 1.85665 +  int i;
 1.85666 +  Index *pIdx;
 1.85667 +  int r1;
 1.85668 +
 1.85669 +  for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
 1.85670 +    if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
 1.85671 +    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
 1.85672 +    sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
 1.85673 +  }
 1.85674 +}
 1.85675 +
 1.85676 +/*
 1.85677 +** Generate code that will assemble an index key and put it in register
 1.85678 +** regOut.  The key with be for index pIdx which is an index on pTab.
 1.85679 +** iCur is the index of a cursor open on the pTab table and pointing to
 1.85680 +** the entry that needs indexing.
 1.85681 +**
 1.85682 +** Return a register number which is the first in a block of
 1.85683 +** registers that holds the elements of the index key.  The
 1.85684 +** block of registers has already been deallocated by the time
 1.85685 +** this routine returns.
 1.85686 +*/
 1.85687 +SQLITE_PRIVATE int sqlite3GenerateIndexKey(
 1.85688 +  Parse *pParse,     /* Parsing context */
 1.85689 +  Index *pIdx,       /* The index for which to generate a key */
 1.85690 +  int iCur,          /* Cursor number for the pIdx->pTable table */
 1.85691 +  int regOut,        /* Write the new index key to this register */
 1.85692 +  int doMakeRec      /* Run the OP_MakeRecord instruction if true */
 1.85693 +){
 1.85694 +  Vdbe *v = pParse->pVdbe;
 1.85695 +  int j;
 1.85696 +  Table *pTab = pIdx->pTable;
 1.85697 +  int regBase;
 1.85698 +  int nCol;
 1.85699 +
 1.85700 +  nCol = pIdx->nColumn;
 1.85701 +  regBase = sqlite3GetTempRange(pParse, nCol+1);
 1.85702 +  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
 1.85703 +  for(j=0; j<nCol; j++){
 1.85704 +    int idx = pIdx->aiColumn[j];
 1.85705 +    if( idx==pTab->iPKey ){
 1.85706 +      sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
 1.85707 +    }else{
 1.85708 +      sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
 1.85709 +      sqlite3ColumnDefault(v, pTab, idx, -1);
 1.85710 +    }
 1.85711 +  }
 1.85712 +  if( doMakeRec ){
 1.85713 +    const char *zAff;
 1.85714 +    if( pTab->pSelect
 1.85715 +     || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
 1.85716 +    ){
 1.85717 +      zAff = 0;
 1.85718 +    }else{
 1.85719 +      zAff = sqlite3IndexAffinityStr(v, pIdx);
 1.85720 +    }
 1.85721 +    sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
 1.85722 +    sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
 1.85723 +  }
 1.85724 +  sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
 1.85725 +  return regBase;
 1.85726 +}
 1.85727 +
 1.85728 +/************** End of delete.c **********************************************/
 1.85729 +/************** Begin file func.c ********************************************/
 1.85730 +/*
 1.85731 +** 2002 February 23
 1.85732 +**
 1.85733 +** The author disclaims copyright to this source code.  In place of
 1.85734 +** a legal notice, here is a blessing:
 1.85735 +**
 1.85736 +**    May you do good and not evil.
 1.85737 +**    May you find forgiveness for yourself and forgive others.
 1.85738 +**    May you share freely, never taking more than you give.
 1.85739 +**
 1.85740 +*************************************************************************
 1.85741 +** This file contains the C functions that implement various SQL
 1.85742 +** functions of SQLite.  
 1.85743 +**
 1.85744 +** There is only one exported symbol in this file - the function
 1.85745 +** sqliteRegisterBuildinFunctions() found at the bottom of the file.
 1.85746 +** All other code has file scope.
 1.85747 +*/
 1.85748 +/* #include <stdlib.h> */
 1.85749 +/* #include <assert.h> */
 1.85750 +
 1.85751 +/*
 1.85752 +** Return the collating function associated with a function.
 1.85753 +*/
 1.85754 +static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
 1.85755 +  return context->pColl;
 1.85756 +}
 1.85757 +
 1.85758 +/*
 1.85759 +** Indicate that the accumulator load should be skipped on this
 1.85760 +** iteration of the aggregate loop.
 1.85761 +*/
 1.85762 +static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
 1.85763 +  context->skipFlag = 1;
 1.85764 +}
 1.85765 +
 1.85766 +/*
 1.85767 +** Implementation of the non-aggregate min() and max() functions
 1.85768 +*/
 1.85769 +static void minmaxFunc(
 1.85770 +  sqlite3_context *context,
 1.85771 +  int argc,
 1.85772 +  sqlite3_value **argv
 1.85773 +){
 1.85774 +  int i;
 1.85775 +  int mask;    /* 0 for min() or 0xffffffff for max() */
 1.85776 +  int iBest;
 1.85777 +  CollSeq *pColl;
 1.85778 +
 1.85779 +  assert( argc>1 );
 1.85780 +  mask = sqlite3_user_data(context)==0 ? 0 : -1;
 1.85781 +  pColl = sqlite3GetFuncCollSeq(context);
 1.85782 +  assert( pColl );
 1.85783 +  assert( mask==-1 || mask==0 );
 1.85784 +  iBest = 0;
 1.85785 +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 1.85786 +  for(i=1; i<argc; i++){
 1.85787 +    if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
 1.85788 +    if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
 1.85789 +      testcase( mask==0 );
 1.85790 +      iBest = i;
 1.85791 +    }
 1.85792 +  }
 1.85793 +  sqlite3_result_value(context, argv[iBest]);
 1.85794 +}
 1.85795 +
 1.85796 +/*
 1.85797 +** Return the type of the argument.
 1.85798 +*/
 1.85799 +static void typeofFunc(
 1.85800 +  sqlite3_context *context,
 1.85801 +  int NotUsed,
 1.85802 +  sqlite3_value **argv
 1.85803 +){
 1.85804 +  const char *z = 0;
 1.85805 +  UNUSED_PARAMETER(NotUsed);
 1.85806 +  switch( sqlite3_value_type(argv[0]) ){
 1.85807 +    case SQLITE_INTEGER: z = "integer"; break;
 1.85808 +    case SQLITE_TEXT:    z = "text";    break;
 1.85809 +    case SQLITE_FLOAT:   z = "real";    break;
 1.85810 +    case SQLITE_BLOB:    z = "blob";    break;
 1.85811 +    default:             z = "null";    break;
 1.85812 +  }
 1.85813 +  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
 1.85814 +}
 1.85815 +
 1.85816 +
 1.85817 +/*
 1.85818 +** Implementation of the length() function
 1.85819 +*/
 1.85820 +static void lengthFunc(
 1.85821 +  sqlite3_context *context,
 1.85822 +  int argc,
 1.85823 +  sqlite3_value **argv
 1.85824 +){
 1.85825 +  int len;
 1.85826 +
 1.85827 +  assert( argc==1 );
 1.85828 +  UNUSED_PARAMETER(argc);
 1.85829 +  switch( sqlite3_value_type(argv[0]) ){
 1.85830 +    case SQLITE_BLOB:
 1.85831 +    case SQLITE_INTEGER:
 1.85832 +    case SQLITE_FLOAT: {
 1.85833 +      sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
 1.85834 +      break;
 1.85835 +    }
 1.85836 +    case SQLITE_TEXT: {
 1.85837 +      const unsigned char *z = sqlite3_value_text(argv[0]);
 1.85838 +      if( z==0 ) return;
 1.85839 +      len = 0;
 1.85840 +      while( *z ){
 1.85841 +        len++;
 1.85842 +        SQLITE_SKIP_UTF8(z);
 1.85843 +      }
 1.85844 +      sqlite3_result_int(context, len);
 1.85845 +      break;
 1.85846 +    }
 1.85847 +    default: {
 1.85848 +      sqlite3_result_null(context);
 1.85849 +      break;
 1.85850 +    }
 1.85851 +  }
 1.85852 +}
 1.85853 +
 1.85854 +/*
 1.85855 +** Implementation of the abs() function.
 1.85856 +**
 1.85857 +** IMP: R-23979-26855 The abs(X) function returns the absolute value of
 1.85858 +** the numeric argument X. 
 1.85859 +*/
 1.85860 +static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.85861 +  assert( argc==1 );
 1.85862 +  UNUSED_PARAMETER(argc);
 1.85863 +  switch( sqlite3_value_type(argv[0]) ){
 1.85864 +    case SQLITE_INTEGER: {
 1.85865 +      i64 iVal = sqlite3_value_int64(argv[0]);
 1.85866 +      if( iVal<0 ){
 1.85867 +        if( (iVal<<1)==0 ){
 1.85868 +          /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
 1.85869 +          ** abs(X) throws an integer overflow error since there is no
 1.85870 +          ** equivalent positive 64-bit two complement value. */
 1.85871 +          sqlite3_result_error(context, "integer overflow", -1);
 1.85872 +          return;
 1.85873 +        }
 1.85874 +        iVal = -iVal;
 1.85875 +      } 
 1.85876 +      sqlite3_result_int64(context, iVal);
 1.85877 +      break;
 1.85878 +    }
 1.85879 +    case SQLITE_NULL: {
 1.85880 +      /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
 1.85881 +      sqlite3_result_null(context);
 1.85882 +      break;
 1.85883 +    }
 1.85884 +    default: {
 1.85885 +      /* Because sqlite3_value_double() returns 0.0 if the argument is not
 1.85886 +      ** something that can be converted into a number, we have:
 1.85887 +      ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
 1.85888 +      ** cannot be converted to a numeric value. 
 1.85889 +      */
 1.85890 +      double rVal = sqlite3_value_double(argv[0]);
 1.85891 +      if( rVal<0 ) rVal = -rVal;
 1.85892 +      sqlite3_result_double(context, rVal);
 1.85893 +      break;
 1.85894 +    }
 1.85895 +  }
 1.85896 +}
 1.85897 +
 1.85898 +/*
 1.85899 +** Implementation of the instr() function.
 1.85900 +**
 1.85901 +** instr(haystack,needle) finds the first occurrence of needle
 1.85902 +** in haystack and returns the number of previous characters plus 1,
 1.85903 +** or 0 if needle does not occur within haystack.
 1.85904 +**
 1.85905 +** If both haystack and needle are BLOBs, then the result is one more than
 1.85906 +** the number of bytes in haystack prior to the first occurrence of needle,
 1.85907 +** or 0 if needle never occurs in haystack.
 1.85908 +*/
 1.85909 +static void instrFunc(
 1.85910 +  sqlite3_context *context,
 1.85911 +  int argc,
 1.85912 +  sqlite3_value **argv
 1.85913 +){
 1.85914 +  const unsigned char *zHaystack;
 1.85915 +  const unsigned char *zNeedle;
 1.85916 +  int nHaystack;
 1.85917 +  int nNeedle;
 1.85918 +  int typeHaystack, typeNeedle;
 1.85919 +  int N = 1;
 1.85920 +  int isText;
 1.85921 +
 1.85922 +  UNUSED_PARAMETER(argc);
 1.85923 +  typeHaystack = sqlite3_value_type(argv[0]);
 1.85924 +  typeNeedle = sqlite3_value_type(argv[1]);
 1.85925 +  if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
 1.85926 +  nHaystack = sqlite3_value_bytes(argv[0]);
 1.85927 +  nNeedle = sqlite3_value_bytes(argv[1]);
 1.85928 +  if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
 1.85929 +    zHaystack = sqlite3_value_blob(argv[0]);
 1.85930 +    zNeedle = sqlite3_value_blob(argv[1]);
 1.85931 +    isText = 0;
 1.85932 +  }else{
 1.85933 +    zHaystack = sqlite3_value_text(argv[0]);
 1.85934 +    zNeedle = sqlite3_value_text(argv[1]);
 1.85935 +    isText = 1;
 1.85936 +  }
 1.85937 +  while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
 1.85938 +    N++;
 1.85939 +    do{
 1.85940 +      nHaystack--;
 1.85941 +      zHaystack++;
 1.85942 +    }while( isText && (zHaystack[0]&0xc0)==0x80 );
 1.85943 +  }
 1.85944 +  if( nNeedle>nHaystack ) N = 0;
 1.85945 +  sqlite3_result_int(context, N);
 1.85946 +}
 1.85947 +
 1.85948 +/*
 1.85949 +** Implementation of the substr() function.
 1.85950 +**
 1.85951 +** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
 1.85952 +** p1 is 1-indexed.  So substr(x,1,1) returns the first character
 1.85953 +** of x.  If x is text, then we actually count UTF-8 characters.
 1.85954 +** If x is a blob, then we count bytes.
 1.85955 +**
 1.85956 +** If p1 is negative, then we begin abs(p1) from the end of x[].
 1.85957 +**
 1.85958 +** If p2 is negative, return the p2 characters preceeding p1.
 1.85959 +*/
 1.85960 +static void substrFunc(
 1.85961 +  sqlite3_context *context,
 1.85962 +  int argc,
 1.85963 +  sqlite3_value **argv
 1.85964 +){
 1.85965 +  const unsigned char *z;
 1.85966 +  const unsigned char *z2;
 1.85967 +  int len;
 1.85968 +  int p0type;
 1.85969 +  i64 p1, p2;
 1.85970 +  int negP2 = 0;
 1.85971 +
 1.85972 +  assert( argc==3 || argc==2 );
 1.85973 +  if( sqlite3_value_type(argv[1])==SQLITE_NULL
 1.85974 +   || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
 1.85975 +  ){
 1.85976 +    return;
 1.85977 +  }
 1.85978 +  p0type = sqlite3_value_type(argv[0]);
 1.85979 +  p1 = sqlite3_value_int(argv[1]);
 1.85980 +  if( p0type==SQLITE_BLOB ){
 1.85981 +    len = sqlite3_value_bytes(argv[0]);
 1.85982 +    z = sqlite3_value_blob(argv[0]);
 1.85983 +    if( z==0 ) return;
 1.85984 +    assert( len==sqlite3_value_bytes(argv[0]) );
 1.85985 +  }else{
 1.85986 +    z = sqlite3_value_text(argv[0]);
 1.85987 +    if( z==0 ) return;
 1.85988 +    len = 0;
 1.85989 +    if( p1<0 ){
 1.85990 +      for(z2=z; *z2; len++){
 1.85991 +        SQLITE_SKIP_UTF8(z2);
 1.85992 +      }
 1.85993 +    }
 1.85994 +  }
 1.85995 +  if( argc==3 ){
 1.85996 +    p2 = sqlite3_value_int(argv[2]);
 1.85997 +    if( p2<0 ){
 1.85998 +      p2 = -p2;
 1.85999 +      negP2 = 1;
 1.86000 +    }
 1.86001 +  }else{
 1.86002 +    p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
 1.86003 +  }
 1.86004 +  if( p1<0 ){
 1.86005 +    p1 += len;
 1.86006 +    if( p1<0 ){
 1.86007 +      p2 += p1;
 1.86008 +      if( p2<0 ) p2 = 0;
 1.86009 +      p1 = 0;
 1.86010 +    }
 1.86011 +  }else if( p1>0 ){
 1.86012 +    p1--;
 1.86013 +  }else if( p2>0 ){
 1.86014 +    p2--;
 1.86015 +  }
 1.86016 +  if( negP2 ){
 1.86017 +    p1 -= p2;
 1.86018 +    if( p1<0 ){
 1.86019 +      p2 += p1;
 1.86020 +      p1 = 0;
 1.86021 +    }
 1.86022 +  }
 1.86023 +  assert( p1>=0 && p2>=0 );
 1.86024 +  if( p0type!=SQLITE_BLOB ){
 1.86025 +    while( *z && p1 ){
 1.86026 +      SQLITE_SKIP_UTF8(z);
 1.86027 +      p1--;
 1.86028 +    }
 1.86029 +    for(z2=z; *z2 && p2; p2--){
 1.86030 +      SQLITE_SKIP_UTF8(z2);
 1.86031 +    }
 1.86032 +    sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
 1.86033 +  }else{
 1.86034 +    if( p1+p2>len ){
 1.86035 +      p2 = len-p1;
 1.86036 +      if( p2<0 ) p2 = 0;
 1.86037 +    }
 1.86038 +    sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
 1.86039 +  }
 1.86040 +}
 1.86041 +
 1.86042 +/*
 1.86043 +** Implementation of the round() function
 1.86044 +*/
 1.86045 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.86046 +static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.86047 +  int n = 0;
 1.86048 +  double r;
 1.86049 +  char *zBuf;
 1.86050 +  assert( argc==1 || argc==2 );
 1.86051 +  if( argc==2 ){
 1.86052 +    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
 1.86053 +    n = sqlite3_value_int(argv[1]);
 1.86054 +    if( n>30 ) n = 30;
 1.86055 +    if( n<0 ) n = 0;
 1.86056 +  }
 1.86057 +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 1.86058 +  r = sqlite3_value_double(argv[0]);
 1.86059 +  /* If Y==0 and X will fit in a 64-bit int,
 1.86060 +  ** handle the rounding directly,
 1.86061 +  ** otherwise use printf.
 1.86062 +  */
 1.86063 +  if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
 1.86064 +    r = (double)((sqlite_int64)(r+0.5));
 1.86065 +  }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
 1.86066 +    r = -(double)((sqlite_int64)((-r)+0.5));
 1.86067 +  }else{
 1.86068 +    zBuf = sqlite3_mprintf("%.*f",n,r);
 1.86069 +    if( zBuf==0 ){
 1.86070 +      sqlite3_result_error_nomem(context);
 1.86071 +      return;
 1.86072 +    }
 1.86073 +    sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
 1.86074 +    sqlite3_free(zBuf);
 1.86075 +  }
 1.86076 +  sqlite3_result_double(context, r);
 1.86077 +}
 1.86078 +#endif
 1.86079 +
 1.86080 +/*
 1.86081 +** Allocate nByte bytes of space using sqlite3_malloc(). If the
 1.86082 +** allocation fails, call sqlite3_result_error_nomem() to notify
 1.86083 +** the database handle that malloc() has failed and return NULL.
 1.86084 +** If nByte is larger than the maximum string or blob length, then
 1.86085 +** raise an SQLITE_TOOBIG exception and return NULL.
 1.86086 +*/
 1.86087 +static void *contextMalloc(sqlite3_context *context, i64 nByte){
 1.86088 +  char *z;
 1.86089 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.86090 +  assert( nByte>0 );
 1.86091 +  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
 1.86092 +  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 1.86093 +  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.86094 +    sqlite3_result_error_toobig(context);
 1.86095 +    z = 0;
 1.86096 +  }else{
 1.86097 +    z = sqlite3Malloc((int)nByte);
 1.86098 +    if( !z ){
 1.86099 +      sqlite3_result_error_nomem(context);
 1.86100 +    }
 1.86101 +  }
 1.86102 +  return z;
 1.86103 +}
 1.86104 +
 1.86105 +/*
 1.86106 +** Implementation of the upper() and lower() SQL functions.
 1.86107 +*/
 1.86108 +static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.86109 +  char *z1;
 1.86110 +  const char *z2;
 1.86111 +  int i, n;
 1.86112 +  UNUSED_PARAMETER(argc);
 1.86113 +  z2 = (char*)sqlite3_value_text(argv[0]);
 1.86114 +  n = sqlite3_value_bytes(argv[0]);
 1.86115 +  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 1.86116 +  assert( z2==(char*)sqlite3_value_text(argv[0]) );
 1.86117 +  if( z2 ){
 1.86118 +    z1 = contextMalloc(context, ((i64)n)+1);
 1.86119 +    if( z1 ){
 1.86120 +      for(i=0; i<n; i++){
 1.86121 +        z1[i] = (char)sqlite3Toupper(z2[i]);
 1.86122 +      }
 1.86123 +      sqlite3_result_text(context, z1, n, sqlite3_free);
 1.86124 +    }
 1.86125 +  }
 1.86126 +}
 1.86127 +static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.86128 +  char *z1;
 1.86129 +  const char *z2;
 1.86130 +  int i, n;
 1.86131 +  UNUSED_PARAMETER(argc);
 1.86132 +  z2 = (char*)sqlite3_value_text(argv[0]);
 1.86133 +  n = sqlite3_value_bytes(argv[0]);
 1.86134 +  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 1.86135 +  assert( z2==(char*)sqlite3_value_text(argv[0]) );
 1.86136 +  if( z2 ){
 1.86137 +    z1 = contextMalloc(context, ((i64)n)+1);
 1.86138 +    if( z1 ){
 1.86139 +      for(i=0; i<n; i++){
 1.86140 +        z1[i] = sqlite3Tolower(z2[i]);
 1.86141 +      }
 1.86142 +      sqlite3_result_text(context, z1, n, sqlite3_free);
 1.86143 +    }
 1.86144 +  }
 1.86145 +}
 1.86146 +
 1.86147 +/*
 1.86148 +** The COALESCE() and IFNULL() functions are implemented as VDBE code so
 1.86149 +** that unused argument values do not have to be computed.  However, we
 1.86150 +** still need some kind of function implementation for this routines in
 1.86151 +** the function table.  That function implementation will never be called
 1.86152 +** so it doesn't matter what the implementation is.  We might as well use
 1.86153 +** the "version()" function as a substitute.
 1.86154 +*/
 1.86155 +#define ifnullFunc versionFunc   /* Substitute function - never called */
 1.86156 +
 1.86157 +/*
 1.86158 +** Implementation of random().  Return a random integer.  
 1.86159 +*/
 1.86160 +static void randomFunc(
 1.86161 +  sqlite3_context *context,
 1.86162 +  int NotUsed,
 1.86163 +  sqlite3_value **NotUsed2
 1.86164 +){
 1.86165 +  sqlite_int64 r;
 1.86166 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.86167 +  sqlite3_randomness(sizeof(r), &r);
 1.86168 +  if( r<0 ){
 1.86169 +    /* We need to prevent a random number of 0x8000000000000000 
 1.86170 +    ** (or -9223372036854775808) since when you do abs() of that
 1.86171 +    ** number of you get the same value back again.  To do this
 1.86172 +    ** in a way that is testable, mask the sign bit off of negative
 1.86173 +    ** values, resulting in a positive value.  Then take the 
 1.86174 +    ** 2s complement of that positive value.  The end result can
 1.86175 +    ** therefore be no less than -9223372036854775807.
 1.86176 +    */
 1.86177 +    r = -(r & LARGEST_INT64);
 1.86178 +  }
 1.86179 +  sqlite3_result_int64(context, r);
 1.86180 +}
 1.86181 +
 1.86182 +/*
 1.86183 +** Implementation of randomblob(N).  Return a random blob
 1.86184 +** that is N bytes long.
 1.86185 +*/
 1.86186 +static void randomBlob(
 1.86187 +  sqlite3_context *context,
 1.86188 +  int argc,
 1.86189 +  sqlite3_value **argv
 1.86190 +){
 1.86191 +  int n;
 1.86192 +  unsigned char *p;
 1.86193 +  assert( argc==1 );
 1.86194 +  UNUSED_PARAMETER(argc);
 1.86195 +  n = sqlite3_value_int(argv[0]);
 1.86196 +  if( n<1 ){
 1.86197 +    n = 1;
 1.86198 +  }
 1.86199 +  p = contextMalloc(context, n);
 1.86200 +  if( p ){
 1.86201 +    sqlite3_randomness(n, p);
 1.86202 +    sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
 1.86203 +  }
 1.86204 +}
 1.86205 +
 1.86206 +/*
 1.86207 +** Implementation of the last_insert_rowid() SQL function.  The return
 1.86208 +** value is the same as the sqlite3_last_insert_rowid() API function.
 1.86209 +*/
 1.86210 +static void last_insert_rowid(
 1.86211 +  sqlite3_context *context, 
 1.86212 +  int NotUsed, 
 1.86213 +  sqlite3_value **NotUsed2
 1.86214 +){
 1.86215 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.86216 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.86217 +  /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
 1.86218 +  ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
 1.86219 +  ** function. */
 1.86220 +  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
 1.86221 +}
 1.86222 +
 1.86223 +/*
 1.86224 +** Implementation of the changes() SQL function.
 1.86225 +**
 1.86226 +** IMP: R-62073-11209 The changes() SQL function is a wrapper
 1.86227 +** around the sqlite3_changes() C/C++ function and hence follows the same
 1.86228 +** rules for counting changes.
 1.86229 +*/
 1.86230 +static void changes(
 1.86231 +  sqlite3_context *context,
 1.86232 +  int NotUsed,
 1.86233 +  sqlite3_value **NotUsed2
 1.86234 +){
 1.86235 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.86236 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.86237 +  sqlite3_result_int(context, sqlite3_changes(db));
 1.86238 +}
 1.86239 +
 1.86240 +/*
 1.86241 +** Implementation of the total_changes() SQL function.  The return value is
 1.86242 +** the same as the sqlite3_total_changes() API function.
 1.86243 +*/
 1.86244 +static void total_changes(
 1.86245 +  sqlite3_context *context,
 1.86246 +  int NotUsed,
 1.86247 +  sqlite3_value **NotUsed2
 1.86248 +){
 1.86249 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.86250 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.86251 +  /* IMP: R-52756-41993 This function is a wrapper around the
 1.86252 +  ** sqlite3_total_changes() C/C++ interface. */
 1.86253 +  sqlite3_result_int(context, sqlite3_total_changes(db));
 1.86254 +}
 1.86255 +
 1.86256 +/*
 1.86257 +** A structure defining how to do GLOB-style comparisons.
 1.86258 +*/
 1.86259 +struct compareInfo {
 1.86260 +  u8 matchAll;
 1.86261 +  u8 matchOne;
 1.86262 +  u8 matchSet;
 1.86263 +  u8 noCase;
 1.86264 +};
 1.86265 +
 1.86266 +/*
 1.86267 +** For LIKE and GLOB matching on EBCDIC machines, assume that every
 1.86268 +** character is exactly one byte in size.  Also, all characters are
 1.86269 +** able to participate in upper-case-to-lower-case mappings in EBCDIC
 1.86270 +** whereas only characters less than 0x80 do in ASCII.
 1.86271 +*/
 1.86272 +#if defined(SQLITE_EBCDIC)
 1.86273 +# define sqlite3Utf8Read(A)    (*((*A)++))
 1.86274 +# define GlogUpperToLower(A)   A = sqlite3UpperToLower[A]
 1.86275 +#else
 1.86276 +# define GlogUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
 1.86277 +#endif
 1.86278 +
 1.86279 +static const struct compareInfo globInfo = { '*', '?', '[', 0 };
 1.86280 +/* The correct SQL-92 behavior is for the LIKE operator to ignore
 1.86281 +** case.  Thus  'a' LIKE 'A' would be true. */
 1.86282 +static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
 1.86283 +/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
 1.86284 +** is case sensitive causing 'a' LIKE 'A' to be false */
 1.86285 +static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
 1.86286 +
 1.86287 +/*
 1.86288 +** Compare two UTF-8 strings for equality where the first string can
 1.86289 +** potentially be a "glob" expression.  Return true (1) if they
 1.86290 +** are the same and false (0) if they are different.
 1.86291 +**
 1.86292 +** Globbing rules:
 1.86293 +**
 1.86294 +**      '*'       Matches any sequence of zero or more characters.
 1.86295 +**
 1.86296 +**      '?'       Matches exactly one character.
 1.86297 +**
 1.86298 +**     [...]      Matches one character from the enclosed list of
 1.86299 +**                characters.
 1.86300 +**
 1.86301 +**     [^...]     Matches one character not in the enclosed list.
 1.86302 +**
 1.86303 +** With the [...] and [^...] matching, a ']' character can be included
 1.86304 +** in the list by making it the first character after '[' or '^'.  A
 1.86305 +** range of characters can be specified using '-'.  Example:
 1.86306 +** "[a-z]" matches any single lower-case letter.  To match a '-', make
 1.86307 +** it the last character in the list.
 1.86308 +**
 1.86309 +** This routine is usually quick, but can be N**2 in the worst case.
 1.86310 +**
 1.86311 +** Hints: to match '*' or '?', put them in "[]".  Like this:
 1.86312 +**
 1.86313 +**         abc[*]xyz        Matches "abc*xyz" only
 1.86314 +*/
 1.86315 +static int patternCompare(
 1.86316 +  const u8 *zPattern,              /* The glob pattern */
 1.86317 +  const u8 *zString,               /* The string to compare against the glob */
 1.86318 +  const struct compareInfo *pInfo, /* Information about how to do the compare */
 1.86319 +  u32 esc                          /* The escape character */
 1.86320 +){
 1.86321 +  u32 c, c2;
 1.86322 +  int invert;
 1.86323 +  int seen;
 1.86324 +  u8 matchOne = pInfo->matchOne;
 1.86325 +  u8 matchAll = pInfo->matchAll;
 1.86326 +  u8 matchSet = pInfo->matchSet;
 1.86327 +  u8 noCase = pInfo->noCase; 
 1.86328 +  int prevEscape = 0;     /* True if the previous character was 'escape' */
 1.86329 +
 1.86330 +  while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
 1.86331 +    if( c==matchAll && !prevEscape ){
 1.86332 +      while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
 1.86333 +               || c == matchOne ){
 1.86334 +        if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
 1.86335 +          return 0;
 1.86336 +        }
 1.86337 +      }
 1.86338 +      if( c==0 ){
 1.86339 +        return 1;
 1.86340 +      }else if( c==esc ){
 1.86341 +        c = sqlite3Utf8Read(&zPattern);
 1.86342 +        if( c==0 ){
 1.86343 +          return 0;
 1.86344 +        }
 1.86345 +      }else if( c==matchSet ){
 1.86346 +        assert( esc==0 );         /* This is GLOB, not LIKE */
 1.86347 +        assert( matchSet<0x80 );  /* '[' is a single-byte character */
 1.86348 +        while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
 1.86349 +          SQLITE_SKIP_UTF8(zString);
 1.86350 +        }
 1.86351 +        return *zString!=0;
 1.86352 +      }
 1.86353 +      while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
 1.86354 +        if( noCase ){
 1.86355 +          GlogUpperToLower(c2);
 1.86356 +          GlogUpperToLower(c);
 1.86357 +          while( c2 != 0 && c2 != c ){
 1.86358 +            c2 = sqlite3Utf8Read(&zString);
 1.86359 +            GlogUpperToLower(c2);
 1.86360 +          }
 1.86361 +        }else{
 1.86362 +          while( c2 != 0 && c2 != c ){
 1.86363 +            c2 = sqlite3Utf8Read(&zString);
 1.86364 +          }
 1.86365 +        }
 1.86366 +        if( c2==0 ) return 0;
 1.86367 +        if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
 1.86368 +      }
 1.86369 +      return 0;
 1.86370 +    }else if( c==matchOne && !prevEscape ){
 1.86371 +      if( sqlite3Utf8Read(&zString)==0 ){
 1.86372 +        return 0;
 1.86373 +      }
 1.86374 +    }else if( c==matchSet ){
 1.86375 +      u32 prior_c = 0;
 1.86376 +      assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
 1.86377 +      seen = 0;
 1.86378 +      invert = 0;
 1.86379 +      c = sqlite3Utf8Read(&zString);
 1.86380 +      if( c==0 ) return 0;
 1.86381 +      c2 = sqlite3Utf8Read(&zPattern);
 1.86382 +      if( c2=='^' ){
 1.86383 +        invert = 1;
 1.86384 +        c2 = sqlite3Utf8Read(&zPattern);
 1.86385 +      }
 1.86386 +      if( c2==']' ){
 1.86387 +        if( c==']' ) seen = 1;
 1.86388 +        c2 = sqlite3Utf8Read(&zPattern);
 1.86389 +      }
 1.86390 +      while( c2 && c2!=']' ){
 1.86391 +        if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
 1.86392 +          c2 = sqlite3Utf8Read(&zPattern);
 1.86393 +          if( c>=prior_c && c<=c2 ) seen = 1;
 1.86394 +          prior_c = 0;
 1.86395 +        }else{
 1.86396 +          if( c==c2 ){
 1.86397 +            seen = 1;
 1.86398 +          }
 1.86399 +          prior_c = c2;
 1.86400 +        }
 1.86401 +        c2 = sqlite3Utf8Read(&zPattern);
 1.86402 +      }
 1.86403 +      if( c2==0 || (seen ^ invert)==0 ){
 1.86404 +        return 0;
 1.86405 +      }
 1.86406 +    }else if( esc==c && !prevEscape ){
 1.86407 +      prevEscape = 1;
 1.86408 +    }else{
 1.86409 +      c2 = sqlite3Utf8Read(&zString);
 1.86410 +      if( noCase ){
 1.86411 +        GlogUpperToLower(c);
 1.86412 +        GlogUpperToLower(c2);
 1.86413 +      }
 1.86414 +      if( c!=c2 ){
 1.86415 +        return 0;
 1.86416 +      }
 1.86417 +      prevEscape = 0;
 1.86418 +    }
 1.86419 +  }
 1.86420 +  return *zString==0;
 1.86421 +}
 1.86422 +
 1.86423 +/*
 1.86424 +** Count the number of times that the LIKE operator (or GLOB which is
 1.86425 +** just a variation of LIKE) gets called.  This is used for testing
 1.86426 +** only.
 1.86427 +*/
 1.86428 +#ifdef SQLITE_TEST
 1.86429 +SQLITE_API int sqlite3_like_count = 0;
 1.86430 +#endif
 1.86431 +
 1.86432 +
 1.86433 +/*
 1.86434 +** Implementation of the like() SQL function.  This function implements
 1.86435 +** the build-in LIKE operator.  The first argument to the function is the
 1.86436 +** pattern and the second argument is the string.  So, the SQL statements:
 1.86437 +**
 1.86438 +**       A LIKE B
 1.86439 +**
 1.86440 +** is implemented as like(B,A).
 1.86441 +**
 1.86442 +** This same function (with a different compareInfo structure) computes
 1.86443 +** the GLOB operator.
 1.86444 +*/
 1.86445 +static void likeFunc(
 1.86446 +  sqlite3_context *context, 
 1.86447 +  int argc, 
 1.86448 +  sqlite3_value **argv
 1.86449 +){
 1.86450 +  const unsigned char *zA, *zB;
 1.86451 +  u32 escape = 0;
 1.86452 +  int nPat;
 1.86453 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.86454 +
 1.86455 +  zB = sqlite3_value_text(argv[0]);
 1.86456 +  zA = sqlite3_value_text(argv[1]);
 1.86457 +
 1.86458 +  /* Limit the length of the LIKE or GLOB pattern to avoid problems
 1.86459 +  ** of deep recursion and N*N behavior in patternCompare().
 1.86460 +  */
 1.86461 +  nPat = sqlite3_value_bytes(argv[0]);
 1.86462 +  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
 1.86463 +  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
 1.86464 +  if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
 1.86465 +    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
 1.86466 +    return;
 1.86467 +  }
 1.86468 +  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
 1.86469 +
 1.86470 +  if( argc==3 ){
 1.86471 +    /* The escape character string must consist of a single UTF-8 character.
 1.86472 +    ** Otherwise, return an error.
 1.86473 +    */
 1.86474 +    const unsigned char *zEsc = sqlite3_value_text(argv[2]);
 1.86475 +    if( zEsc==0 ) return;
 1.86476 +    if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
 1.86477 +      sqlite3_result_error(context, 
 1.86478 +          "ESCAPE expression must be a single character", -1);
 1.86479 +      return;
 1.86480 +    }
 1.86481 +    escape = sqlite3Utf8Read(&zEsc);
 1.86482 +  }
 1.86483 +  if( zA && zB ){
 1.86484 +    struct compareInfo *pInfo = sqlite3_user_data(context);
 1.86485 +#ifdef SQLITE_TEST
 1.86486 +    sqlite3_like_count++;
 1.86487 +#endif
 1.86488 +    
 1.86489 +    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
 1.86490 +  }
 1.86491 +}
 1.86492 +
 1.86493 +/*
 1.86494 +** Implementation of the NULLIF(x,y) function.  The result is the first
 1.86495 +** argument if the arguments are different.  The result is NULL if the
 1.86496 +** arguments are equal to each other.
 1.86497 +*/
 1.86498 +static void nullifFunc(
 1.86499 +  sqlite3_context *context,
 1.86500 +  int NotUsed,
 1.86501 +  sqlite3_value **argv
 1.86502 +){
 1.86503 +  CollSeq *pColl = sqlite3GetFuncCollSeq(context);
 1.86504 +  UNUSED_PARAMETER(NotUsed);
 1.86505 +  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
 1.86506 +    sqlite3_result_value(context, argv[0]);
 1.86507 +  }
 1.86508 +}
 1.86509 +
 1.86510 +/*
 1.86511 +** Implementation of the sqlite_version() function.  The result is the version
 1.86512 +** of the SQLite library that is running.
 1.86513 +*/
 1.86514 +static void versionFunc(
 1.86515 +  sqlite3_context *context,
 1.86516 +  int NotUsed,
 1.86517 +  sqlite3_value **NotUsed2
 1.86518 +){
 1.86519 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.86520 +  /* IMP: R-48699-48617 This function is an SQL wrapper around the
 1.86521 +  ** sqlite3_libversion() C-interface. */
 1.86522 +  sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
 1.86523 +}
 1.86524 +
 1.86525 +/*
 1.86526 +** Implementation of the sqlite_source_id() function. The result is a string
 1.86527 +** that identifies the particular version of the source code used to build
 1.86528 +** SQLite.
 1.86529 +*/
 1.86530 +static void sourceidFunc(
 1.86531 +  sqlite3_context *context,
 1.86532 +  int NotUsed,
 1.86533 +  sqlite3_value **NotUsed2
 1.86534 +){
 1.86535 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.86536 +  /* IMP: R-24470-31136 This function is an SQL wrapper around the
 1.86537 +  ** sqlite3_sourceid() C interface. */
 1.86538 +  sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
 1.86539 +}
 1.86540 +
 1.86541 +/*
 1.86542 +** Implementation of the sqlite_log() function.  This is a wrapper around
 1.86543 +** sqlite3_log().  The return value is NULL.  The function exists purely for
 1.86544 +** its side-effects.
 1.86545 +*/
 1.86546 +static void errlogFunc(
 1.86547 +  sqlite3_context *context,
 1.86548 +  int argc,
 1.86549 +  sqlite3_value **argv
 1.86550 +){
 1.86551 +  UNUSED_PARAMETER(argc);
 1.86552 +  UNUSED_PARAMETER(context);
 1.86553 +  sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
 1.86554 +}
 1.86555 +
 1.86556 +/*
 1.86557 +** Implementation of the sqlite_compileoption_used() function.
 1.86558 +** The result is an integer that identifies if the compiler option
 1.86559 +** was used to build SQLite.
 1.86560 +*/
 1.86561 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 1.86562 +static void compileoptionusedFunc(
 1.86563 +  sqlite3_context *context,
 1.86564 +  int argc,
 1.86565 +  sqlite3_value **argv
 1.86566 +){
 1.86567 +  const char *zOptName;
 1.86568 +  assert( argc==1 );
 1.86569 +  UNUSED_PARAMETER(argc);
 1.86570 +  /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
 1.86571 +  ** function is a wrapper around the sqlite3_compileoption_used() C/C++
 1.86572 +  ** function.
 1.86573 +  */
 1.86574 +  if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
 1.86575 +    sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
 1.86576 +  }
 1.86577 +}
 1.86578 +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 1.86579 +
 1.86580 +/*
 1.86581 +** Implementation of the sqlite_compileoption_get() function. 
 1.86582 +** The result is a string that identifies the compiler options 
 1.86583 +** used to build SQLite.
 1.86584 +*/
 1.86585 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 1.86586 +static void compileoptiongetFunc(
 1.86587 +  sqlite3_context *context,
 1.86588 +  int argc,
 1.86589 +  sqlite3_value **argv
 1.86590 +){
 1.86591 +  int n;
 1.86592 +  assert( argc==1 );
 1.86593 +  UNUSED_PARAMETER(argc);
 1.86594 +  /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
 1.86595 +  ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
 1.86596 +  */
 1.86597 +  n = sqlite3_value_int(argv[0]);
 1.86598 +  sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
 1.86599 +}
 1.86600 +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 1.86601 +
 1.86602 +/* Array for converting from half-bytes (nybbles) into ASCII hex
 1.86603 +** digits. */
 1.86604 +static const char hexdigits[] = {
 1.86605 +  '0', '1', '2', '3', '4', '5', '6', '7',
 1.86606 +  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
 1.86607 +};
 1.86608 +
 1.86609 +/*
 1.86610 +** EXPERIMENTAL - This is not an official function.  The interface may
 1.86611 +** change.  This function may disappear.  Do not write code that depends
 1.86612 +** on this function.
 1.86613 +**
 1.86614 +** Implementation of the QUOTE() function.  This function takes a single
 1.86615 +** argument.  If the argument is numeric, the return value is the same as
 1.86616 +** the argument.  If the argument is NULL, the return value is the string
 1.86617 +** "NULL".  Otherwise, the argument is enclosed in single quotes with
 1.86618 +** single-quote escapes.
 1.86619 +*/
 1.86620 +static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.86621 +  assert( argc==1 );
 1.86622 +  UNUSED_PARAMETER(argc);
 1.86623 +  switch( sqlite3_value_type(argv[0]) ){
 1.86624 +    case SQLITE_FLOAT: {
 1.86625 +      double r1, r2;
 1.86626 +      char zBuf[50];
 1.86627 +      r1 = sqlite3_value_double(argv[0]);
 1.86628 +      sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
 1.86629 +      sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
 1.86630 +      if( r1!=r2 ){
 1.86631 +        sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
 1.86632 +      }
 1.86633 +      sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 1.86634 +      break;
 1.86635 +    }
 1.86636 +    case SQLITE_INTEGER: {
 1.86637 +      sqlite3_result_value(context, argv[0]);
 1.86638 +      break;
 1.86639 +    }
 1.86640 +    case SQLITE_BLOB: {
 1.86641 +      char *zText = 0;
 1.86642 +      char const *zBlob = sqlite3_value_blob(argv[0]);
 1.86643 +      int nBlob = sqlite3_value_bytes(argv[0]);
 1.86644 +      assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
 1.86645 +      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
 1.86646 +      if( zText ){
 1.86647 +        int i;
 1.86648 +        for(i=0; i<nBlob; i++){
 1.86649 +          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
 1.86650 +          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
 1.86651 +        }
 1.86652 +        zText[(nBlob*2)+2] = '\'';
 1.86653 +        zText[(nBlob*2)+3] = '\0';
 1.86654 +        zText[0] = 'X';
 1.86655 +        zText[1] = '\'';
 1.86656 +        sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
 1.86657 +        sqlite3_free(zText);
 1.86658 +      }
 1.86659 +      break;
 1.86660 +    }
 1.86661 +    case SQLITE_TEXT: {
 1.86662 +      int i,j;
 1.86663 +      u64 n;
 1.86664 +      const unsigned char *zArg = sqlite3_value_text(argv[0]);
 1.86665 +      char *z;
 1.86666 +
 1.86667 +      if( zArg==0 ) return;
 1.86668 +      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
 1.86669 +      z = contextMalloc(context, ((i64)i)+((i64)n)+3);
 1.86670 +      if( z ){
 1.86671 +        z[0] = '\'';
 1.86672 +        for(i=0, j=1; zArg[i]; i++){
 1.86673 +          z[j++] = zArg[i];
 1.86674 +          if( zArg[i]=='\'' ){
 1.86675 +            z[j++] = '\'';
 1.86676 +          }
 1.86677 +        }
 1.86678 +        z[j++] = '\'';
 1.86679 +        z[j] = 0;
 1.86680 +        sqlite3_result_text(context, z, j, sqlite3_free);
 1.86681 +      }
 1.86682 +      break;
 1.86683 +    }
 1.86684 +    default: {
 1.86685 +      assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
 1.86686 +      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
 1.86687 +      break;
 1.86688 +    }
 1.86689 +  }
 1.86690 +}
 1.86691 +
 1.86692 +/*
 1.86693 +** The hex() function.  Interpret the argument as a blob.  Return
 1.86694 +** a hexadecimal rendering as text.
 1.86695 +*/
 1.86696 +static void hexFunc(
 1.86697 +  sqlite3_context *context,
 1.86698 +  int argc,
 1.86699 +  sqlite3_value **argv
 1.86700 +){
 1.86701 +  int i, n;
 1.86702 +  const unsigned char *pBlob;
 1.86703 +  char *zHex, *z;
 1.86704 +  assert( argc==1 );
 1.86705 +  UNUSED_PARAMETER(argc);
 1.86706 +  pBlob = sqlite3_value_blob(argv[0]);
 1.86707 +  n = sqlite3_value_bytes(argv[0]);
 1.86708 +  assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
 1.86709 +  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
 1.86710 +  if( zHex ){
 1.86711 +    for(i=0; i<n; i++, pBlob++){
 1.86712 +      unsigned char c = *pBlob;
 1.86713 +      *(z++) = hexdigits[(c>>4)&0xf];
 1.86714 +      *(z++) = hexdigits[c&0xf];
 1.86715 +    }
 1.86716 +    *z = 0;
 1.86717 +    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
 1.86718 +  }
 1.86719 +}
 1.86720 +
 1.86721 +/*
 1.86722 +** The zeroblob(N) function returns a zero-filled blob of size N bytes.
 1.86723 +*/
 1.86724 +static void zeroblobFunc(
 1.86725 +  sqlite3_context *context,
 1.86726 +  int argc,
 1.86727 +  sqlite3_value **argv
 1.86728 +){
 1.86729 +  i64 n;
 1.86730 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.86731 +  assert( argc==1 );
 1.86732 +  UNUSED_PARAMETER(argc);
 1.86733 +  n = sqlite3_value_int64(argv[0]);
 1.86734 +  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
 1.86735 +  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 1.86736 +  if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.86737 +    sqlite3_result_error_toobig(context);
 1.86738 +  }else{
 1.86739 +    sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
 1.86740 +  }
 1.86741 +}
 1.86742 +
 1.86743 +/*
 1.86744 +** The replace() function.  Three arguments are all strings: call
 1.86745 +** them A, B, and C. The result is also a string which is derived
 1.86746 +** from A by replacing every occurance of B with C.  The match
 1.86747 +** must be exact.  Collating sequences are not used.
 1.86748 +*/
 1.86749 +static void replaceFunc(
 1.86750 +  sqlite3_context *context,
 1.86751 +  int argc,
 1.86752 +  sqlite3_value **argv
 1.86753 +){
 1.86754 +  const unsigned char *zStr;        /* The input string A */
 1.86755 +  const unsigned char *zPattern;    /* The pattern string B */
 1.86756 +  const unsigned char *zRep;        /* The replacement string C */
 1.86757 +  unsigned char *zOut;              /* The output */
 1.86758 +  int nStr;                /* Size of zStr */
 1.86759 +  int nPattern;            /* Size of zPattern */
 1.86760 +  int nRep;                /* Size of zRep */
 1.86761 +  i64 nOut;                /* Maximum size of zOut */
 1.86762 +  int loopLimit;           /* Last zStr[] that might match zPattern[] */
 1.86763 +  int i, j;                /* Loop counters */
 1.86764 +
 1.86765 +  assert( argc==3 );
 1.86766 +  UNUSED_PARAMETER(argc);
 1.86767 +  zStr = sqlite3_value_text(argv[0]);
 1.86768 +  if( zStr==0 ) return;
 1.86769 +  nStr = sqlite3_value_bytes(argv[0]);
 1.86770 +  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
 1.86771 +  zPattern = sqlite3_value_text(argv[1]);
 1.86772 +  if( zPattern==0 ){
 1.86773 +    assert( sqlite3_value_type(argv[1])==SQLITE_NULL
 1.86774 +            || sqlite3_context_db_handle(context)->mallocFailed );
 1.86775 +    return;
 1.86776 +  }
 1.86777 +  if( zPattern[0]==0 ){
 1.86778 +    assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
 1.86779 +    sqlite3_result_value(context, argv[0]);
 1.86780 +    return;
 1.86781 +  }
 1.86782 +  nPattern = sqlite3_value_bytes(argv[1]);
 1.86783 +  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
 1.86784 +  zRep = sqlite3_value_text(argv[2]);
 1.86785 +  if( zRep==0 ) return;
 1.86786 +  nRep = sqlite3_value_bytes(argv[2]);
 1.86787 +  assert( zRep==sqlite3_value_text(argv[2]) );
 1.86788 +  nOut = nStr + 1;
 1.86789 +  assert( nOut<SQLITE_MAX_LENGTH );
 1.86790 +  zOut = contextMalloc(context, (i64)nOut);
 1.86791 +  if( zOut==0 ){
 1.86792 +    return;
 1.86793 +  }
 1.86794 +  loopLimit = nStr - nPattern;  
 1.86795 +  for(i=j=0; i<=loopLimit; i++){
 1.86796 +    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
 1.86797 +      zOut[j++] = zStr[i];
 1.86798 +    }else{
 1.86799 +      u8 *zOld;
 1.86800 +      sqlite3 *db = sqlite3_context_db_handle(context);
 1.86801 +      nOut += nRep - nPattern;
 1.86802 +      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
 1.86803 +      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
 1.86804 +      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 1.86805 +        sqlite3_result_error_toobig(context);
 1.86806 +        sqlite3_free(zOut);
 1.86807 +        return;
 1.86808 +      }
 1.86809 +      zOld = zOut;
 1.86810 +      zOut = sqlite3_realloc(zOut, (int)nOut);
 1.86811 +      if( zOut==0 ){
 1.86812 +        sqlite3_result_error_nomem(context);
 1.86813 +        sqlite3_free(zOld);
 1.86814 +        return;
 1.86815 +      }
 1.86816 +      memcpy(&zOut[j], zRep, nRep);
 1.86817 +      j += nRep;
 1.86818 +      i += nPattern-1;
 1.86819 +    }
 1.86820 +  }
 1.86821 +  assert( j+nStr-i+1==nOut );
 1.86822 +  memcpy(&zOut[j], &zStr[i], nStr-i);
 1.86823 +  j += nStr - i;
 1.86824 +  assert( j<=nOut );
 1.86825 +  zOut[j] = 0;
 1.86826 +  sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
 1.86827 +}
 1.86828 +
 1.86829 +/*
 1.86830 +** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
 1.86831 +** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
 1.86832 +*/
 1.86833 +static void trimFunc(
 1.86834 +  sqlite3_context *context,
 1.86835 +  int argc,
 1.86836 +  sqlite3_value **argv
 1.86837 +){
 1.86838 +  const unsigned char *zIn;         /* Input string */
 1.86839 +  const unsigned char *zCharSet;    /* Set of characters to trim */
 1.86840 +  int nIn;                          /* Number of bytes in input */
 1.86841 +  int flags;                        /* 1: trimleft  2: trimright  3: trim */
 1.86842 +  int i;                            /* Loop counter */
 1.86843 +  unsigned char *aLen = 0;          /* Length of each character in zCharSet */
 1.86844 +  unsigned char **azChar = 0;       /* Individual characters in zCharSet */
 1.86845 +  int nChar;                        /* Number of characters in zCharSet */
 1.86846 +
 1.86847 +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
 1.86848 +    return;
 1.86849 +  }
 1.86850 +  zIn = sqlite3_value_text(argv[0]);
 1.86851 +  if( zIn==0 ) return;
 1.86852 +  nIn = sqlite3_value_bytes(argv[0]);
 1.86853 +  assert( zIn==sqlite3_value_text(argv[0]) );
 1.86854 +  if( argc==1 ){
 1.86855 +    static const unsigned char lenOne[] = { 1 };
 1.86856 +    static unsigned char * const azOne[] = { (u8*)" " };
 1.86857 +    nChar = 1;
 1.86858 +    aLen = (u8*)lenOne;
 1.86859 +    azChar = (unsigned char **)azOne;
 1.86860 +    zCharSet = 0;
 1.86861 +  }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
 1.86862 +    return;
 1.86863 +  }else{
 1.86864 +    const unsigned char *z;
 1.86865 +    for(z=zCharSet, nChar=0; *z; nChar++){
 1.86866 +      SQLITE_SKIP_UTF8(z);
 1.86867 +    }
 1.86868 +    if( nChar>0 ){
 1.86869 +      azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
 1.86870 +      if( azChar==0 ){
 1.86871 +        return;
 1.86872 +      }
 1.86873 +      aLen = (unsigned char*)&azChar[nChar];
 1.86874 +      for(z=zCharSet, nChar=0; *z; nChar++){
 1.86875 +        azChar[nChar] = (unsigned char *)z;
 1.86876 +        SQLITE_SKIP_UTF8(z);
 1.86877 +        aLen[nChar] = (u8)(z - azChar[nChar]);
 1.86878 +      }
 1.86879 +    }
 1.86880 +  }
 1.86881 +  if( nChar>0 ){
 1.86882 +    flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
 1.86883 +    if( flags & 1 ){
 1.86884 +      while( nIn>0 ){
 1.86885 +        int len = 0;
 1.86886 +        for(i=0; i<nChar; i++){
 1.86887 +          len = aLen[i];
 1.86888 +          if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
 1.86889 +        }
 1.86890 +        if( i>=nChar ) break;
 1.86891 +        zIn += len;
 1.86892 +        nIn -= len;
 1.86893 +      }
 1.86894 +    }
 1.86895 +    if( flags & 2 ){
 1.86896 +      while( nIn>0 ){
 1.86897 +        int len = 0;
 1.86898 +        for(i=0; i<nChar; i++){
 1.86899 +          len = aLen[i];
 1.86900 +          if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
 1.86901 +        }
 1.86902 +        if( i>=nChar ) break;
 1.86903 +        nIn -= len;
 1.86904 +      }
 1.86905 +    }
 1.86906 +    if( zCharSet ){
 1.86907 +      sqlite3_free(azChar);
 1.86908 +    }
 1.86909 +  }
 1.86910 +  sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
 1.86911 +}
 1.86912 +
 1.86913 +
 1.86914 +/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
 1.86915 +** is only available if the SQLITE_SOUNDEX compile-time option is used
 1.86916 +** when SQLite is built.
 1.86917 +*/
 1.86918 +#ifdef SQLITE_SOUNDEX
 1.86919 +/*
 1.86920 +** Compute the soundex encoding of a word.
 1.86921 +**
 1.86922 +** IMP: R-59782-00072 The soundex(X) function returns a string that is the
 1.86923 +** soundex encoding of the string X. 
 1.86924 +*/
 1.86925 +static void soundexFunc(
 1.86926 +  sqlite3_context *context,
 1.86927 +  int argc,
 1.86928 +  sqlite3_value **argv
 1.86929 +){
 1.86930 +  char zResult[8];
 1.86931 +  const u8 *zIn;
 1.86932 +  int i, j;
 1.86933 +  static const unsigned char iCode[] = {
 1.86934 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 1.86935 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 1.86936 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 1.86937 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 1.86938 +    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
 1.86939 +    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
 1.86940 +    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
 1.86941 +    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
 1.86942 +  };
 1.86943 +  assert( argc==1 );
 1.86944 +  zIn = (u8*)sqlite3_value_text(argv[0]);
 1.86945 +  if( zIn==0 ) zIn = (u8*)"";
 1.86946 +  for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
 1.86947 +  if( zIn[i] ){
 1.86948 +    u8 prevcode = iCode[zIn[i]&0x7f];
 1.86949 +    zResult[0] = sqlite3Toupper(zIn[i]);
 1.86950 +    for(j=1; j<4 && zIn[i]; i++){
 1.86951 +      int code = iCode[zIn[i]&0x7f];
 1.86952 +      if( code>0 ){
 1.86953 +        if( code!=prevcode ){
 1.86954 +          prevcode = code;
 1.86955 +          zResult[j++] = code + '0';
 1.86956 +        }
 1.86957 +      }else{
 1.86958 +        prevcode = 0;
 1.86959 +      }
 1.86960 +    }
 1.86961 +    while( j<4 ){
 1.86962 +      zResult[j++] = '0';
 1.86963 +    }
 1.86964 +    zResult[j] = 0;
 1.86965 +    sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
 1.86966 +  }else{
 1.86967 +    /* IMP: R-64894-50321 The string "?000" is returned if the argument
 1.86968 +    ** is NULL or contains no ASCII alphabetic characters. */
 1.86969 +    sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
 1.86970 +  }
 1.86971 +}
 1.86972 +#endif /* SQLITE_SOUNDEX */
 1.86973 +
 1.86974 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
 1.86975 +/*
 1.86976 +** A function that loads a shared-library extension then returns NULL.
 1.86977 +*/
 1.86978 +static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.86979 +  const char *zFile = (const char *)sqlite3_value_text(argv[0]);
 1.86980 +  const char *zProc;
 1.86981 +  sqlite3 *db = sqlite3_context_db_handle(context);
 1.86982 +  char *zErrMsg = 0;
 1.86983 +
 1.86984 +  if( argc==2 ){
 1.86985 +    zProc = (const char *)sqlite3_value_text(argv[1]);
 1.86986 +  }else{
 1.86987 +    zProc = 0;
 1.86988 +  }
 1.86989 +  if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
 1.86990 +    sqlite3_result_error(context, zErrMsg, -1);
 1.86991 +    sqlite3_free(zErrMsg);
 1.86992 +  }
 1.86993 +}
 1.86994 +#endif
 1.86995 +
 1.86996 +
 1.86997 +/*
 1.86998 +** An instance of the following structure holds the context of a
 1.86999 +** sum() or avg() aggregate computation.
 1.87000 +*/
 1.87001 +typedef struct SumCtx SumCtx;
 1.87002 +struct SumCtx {
 1.87003 +  double rSum;      /* Floating point sum */
 1.87004 +  i64 iSum;         /* Integer sum */   
 1.87005 +  i64 cnt;          /* Number of elements summed */
 1.87006 +  u8 overflow;      /* True if integer overflow seen */
 1.87007 +  u8 approx;        /* True if non-integer value was input to the sum */
 1.87008 +};
 1.87009 +
 1.87010 +/*
 1.87011 +** Routines used to compute the sum, average, and total.
 1.87012 +**
 1.87013 +** The SUM() function follows the (broken) SQL standard which means
 1.87014 +** that it returns NULL if it sums over no inputs.  TOTAL returns
 1.87015 +** 0.0 in that case.  In addition, TOTAL always returns a float where
 1.87016 +** SUM might return an integer if it never encounters a floating point
 1.87017 +** value.  TOTAL never fails, but SUM might through an exception if
 1.87018 +** it overflows an integer.
 1.87019 +*/
 1.87020 +static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.87021 +  SumCtx *p;
 1.87022 +  int type;
 1.87023 +  assert( argc==1 );
 1.87024 +  UNUSED_PARAMETER(argc);
 1.87025 +  p = sqlite3_aggregate_context(context, sizeof(*p));
 1.87026 +  type = sqlite3_value_numeric_type(argv[0]);
 1.87027 +  if( p && type!=SQLITE_NULL ){
 1.87028 +    p->cnt++;
 1.87029 +    if( type==SQLITE_INTEGER ){
 1.87030 +      i64 v = sqlite3_value_int64(argv[0]);
 1.87031 +      p->rSum += v;
 1.87032 +      if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
 1.87033 +        p->overflow = 1;
 1.87034 +      }
 1.87035 +    }else{
 1.87036 +      p->rSum += sqlite3_value_double(argv[0]);
 1.87037 +      p->approx = 1;
 1.87038 +    }
 1.87039 +  }
 1.87040 +}
 1.87041 +static void sumFinalize(sqlite3_context *context){
 1.87042 +  SumCtx *p;
 1.87043 +  p = sqlite3_aggregate_context(context, 0);
 1.87044 +  if( p && p->cnt>0 ){
 1.87045 +    if( p->overflow ){
 1.87046 +      sqlite3_result_error(context,"integer overflow",-1);
 1.87047 +    }else if( p->approx ){
 1.87048 +      sqlite3_result_double(context, p->rSum);
 1.87049 +    }else{
 1.87050 +      sqlite3_result_int64(context, p->iSum);
 1.87051 +    }
 1.87052 +  }
 1.87053 +}
 1.87054 +static void avgFinalize(sqlite3_context *context){
 1.87055 +  SumCtx *p;
 1.87056 +  p = sqlite3_aggregate_context(context, 0);
 1.87057 +  if( p && p->cnt>0 ){
 1.87058 +    sqlite3_result_double(context, p->rSum/(double)p->cnt);
 1.87059 +  }
 1.87060 +}
 1.87061 +static void totalFinalize(sqlite3_context *context){
 1.87062 +  SumCtx *p;
 1.87063 +  p = sqlite3_aggregate_context(context, 0);
 1.87064 +  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 1.87065 +  sqlite3_result_double(context, p ? p->rSum : (double)0);
 1.87066 +}
 1.87067 +
 1.87068 +/*
 1.87069 +** The following structure keeps track of state information for the
 1.87070 +** count() aggregate function.
 1.87071 +*/
 1.87072 +typedef struct CountCtx CountCtx;
 1.87073 +struct CountCtx {
 1.87074 +  i64 n;
 1.87075 +};
 1.87076 +
 1.87077 +/*
 1.87078 +** Routines to implement the count() aggregate function.
 1.87079 +*/
 1.87080 +static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
 1.87081 +  CountCtx *p;
 1.87082 +  p = sqlite3_aggregate_context(context, sizeof(*p));
 1.87083 +  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
 1.87084 +    p->n++;
 1.87085 +  }
 1.87086 +
 1.87087 +#ifndef SQLITE_OMIT_DEPRECATED
 1.87088 +  /* The sqlite3_aggregate_count() function is deprecated.  But just to make
 1.87089 +  ** sure it still operates correctly, verify that its count agrees with our 
 1.87090 +  ** internal count when using count(*) and when the total count can be
 1.87091 +  ** expressed as a 32-bit integer. */
 1.87092 +  assert( argc==1 || p==0 || p->n>0x7fffffff
 1.87093 +          || p->n==sqlite3_aggregate_count(context) );
 1.87094 +#endif
 1.87095 +}   
 1.87096 +static void countFinalize(sqlite3_context *context){
 1.87097 +  CountCtx *p;
 1.87098 +  p = sqlite3_aggregate_context(context, 0);
 1.87099 +  sqlite3_result_int64(context, p ? p->n : 0);
 1.87100 +}
 1.87101 +
 1.87102 +/*
 1.87103 +** Routines to implement min() and max() aggregate functions.
 1.87104 +*/
 1.87105 +static void minmaxStep(
 1.87106 +  sqlite3_context *context, 
 1.87107 +  int NotUsed, 
 1.87108 +  sqlite3_value **argv
 1.87109 +){
 1.87110 +  Mem *pArg  = (Mem *)argv[0];
 1.87111 +  Mem *pBest;
 1.87112 +  UNUSED_PARAMETER(NotUsed);
 1.87113 +
 1.87114 +  pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
 1.87115 +  if( !pBest ) return;
 1.87116 +
 1.87117 +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
 1.87118 +    if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
 1.87119 +  }else if( pBest->flags ){
 1.87120 +    int max;
 1.87121 +    int cmp;
 1.87122 +    CollSeq *pColl = sqlite3GetFuncCollSeq(context);
 1.87123 +    /* This step function is used for both the min() and max() aggregates,
 1.87124 +    ** the only difference between the two being that the sense of the
 1.87125 +    ** comparison is inverted. For the max() aggregate, the
 1.87126 +    ** sqlite3_user_data() function returns (void *)-1. For min() it
 1.87127 +    ** returns (void *)db, where db is the sqlite3* database pointer.
 1.87128 +    ** Therefore the next statement sets variable 'max' to 1 for the max()
 1.87129 +    ** aggregate, or 0 for min().
 1.87130 +    */
 1.87131 +    max = sqlite3_user_data(context)!=0;
 1.87132 +    cmp = sqlite3MemCompare(pBest, pArg, pColl);
 1.87133 +    if( (max && cmp<0) || (!max && cmp>0) ){
 1.87134 +      sqlite3VdbeMemCopy(pBest, pArg);
 1.87135 +    }else{
 1.87136 +      sqlite3SkipAccumulatorLoad(context);
 1.87137 +    }
 1.87138 +  }else{
 1.87139 +    sqlite3VdbeMemCopy(pBest, pArg);
 1.87140 +  }
 1.87141 +}
 1.87142 +static void minMaxFinalize(sqlite3_context *context){
 1.87143 +  sqlite3_value *pRes;
 1.87144 +  pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
 1.87145 +  if( pRes ){
 1.87146 +    if( pRes->flags ){
 1.87147 +      sqlite3_result_value(context, pRes);
 1.87148 +    }
 1.87149 +    sqlite3VdbeMemRelease(pRes);
 1.87150 +  }
 1.87151 +}
 1.87152 +
 1.87153 +/*
 1.87154 +** group_concat(EXPR, ?SEPARATOR?)
 1.87155 +*/
 1.87156 +static void groupConcatStep(
 1.87157 +  sqlite3_context *context,
 1.87158 +  int argc,
 1.87159 +  sqlite3_value **argv
 1.87160 +){
 1.87161 +  const char *zVal;
 1.87162 +  StrAccum *pAccum;
 1.87163 +  const char *zSep;
 1.87164 +  int nVal, nSep;
 1.87165 +  assert( argc==1 || argc==2 );
 1.87166 +  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 1.87167 +  pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
 1.87168 +
 1.87169 +  if( pAccum ){
 1.87170 +    sqlite3 *db = sqlite3_context_db_handle(context);
 1.87171 +    int firstTerm = pAccum->useMalloc==0;
 1.87172 +    pAccum->useMalloc = 2;
 1.87173 +    pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
 1.87174 +    if( !firstTerm ){
 1.87175 +      if( argc==2 ){
 1.87176 +        zSep = (char*)sqlite3_value_text(argv[1]);
 1.87177 +        nSep = sqlite3_value_bytes(argv[1]);
 1.87178 +      }else{
 1.87179 +        zSep = ",";
 1.87180 +        nSep = 1;
 1.87181 +      }
 1.87182 +      sqlite3StrAccumAppend(pAccum, zSep, nSep);
 1.87183 +    }
 1.87184 +    zVal = (char*)sqlite3_value_text(argv[0]);
 1.87185 +    nVal = sqlite3_value_bytes(argv[0]);
 1.87186 +    sqlite3StrAccumAppend(pAccum, zVal, nVal);
 1.87187 +  }
 1.87188 +}
 1.87189 +static void groupConcatFinalize(sqlite3_context *context){
 1.87190 +  StrAccum *pAccum;
 1.87191 +  pAccum = sqlite3_aggregate_context(context, 0);
 1.87192 +  if( pAccum ){
 1.87193 +    if( pAccum->tooBig ){
 1.87194 +      sqlite3_result_error_toobig(context);
 1.87195 +    }else if( pAccum->mallocFailed ){
 1.87196 +      sqlite3_result_error_nomem(context);
 1.87197 +    }else{    
 1.87198 +      sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
 1.87199 +                          sqlite3_free);
 1.87200 +    }
 1.87201 +  }
 1.87202 +}
 1.87203 +
 1.87204 +/*
 1.87205 +** This routine does per-connection function registration.  Most
 1.87206 +** of the built-in functions above are part of the global function set.
 1.87207 +** This routine only deals with those that are not global.
 1.87208 +*/
 1.87209 +SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
 1.87210 +  int rc = sqlite3_overload_function(db, "MATCH", 2);
 1.87211 +  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
 1.87212 +  if( rc==SQLITE_NOMEM ){
 1.87213 +    db->mallocFailed = 1;
 1.87214 +  }
 1.87215 +}
 1.87216 +
 1.87217 +/*
 1.87218 +** Set the LIKEOPT flag on the 2-argument function with the given name.
 1.87219 +*/
 1.87220 +static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
 1.87221 +  FuncDef *pDef;
 1.87222 +  pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
 1.87223 +                             2, SQLITE_UTF8, 0);
 1.87224 +  if( ALWAYS(pDef) ){
 1.87225 +    pDef->flags = flagVal;
 1.87226 +  }
 1.87227 +}
 1.87228 +
 1.87229 +/*
 1.87230 +** Register the built-in LIKE and GLOB functions.  The caseSensitive
 1.87231 +** parameter determines whether or not the LIKE operator is case
 1.87232 +** sensitive.  GLOB is always case sensitive.
 1.87233 +*/
 1.87234 +SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
 1.87235 +  struct compareInfo *pInfo;
 1.87236 +  if( caseSensitive ){
 1.87237 +    pInfo = (struct compareInfo*)&likeInfoAlt;
 1.87238 +  }else{
 1.87239 +    pInfo = (struct compareInfo*)&likeInfoNorm;
 1.87240 +  }
 1.87241 +  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
 1.87242 +  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
 1.87243 +  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
 1.87244 +      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
 1.87245 +  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
 1.87246 +  setLikeOptFlag(db, "like", 
 1.87247 +      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
 1.87248 +}
 1.87249 +
 1.87250 +/*
 1.87251 +** pExpr points to an expression which implements a function.  If
 1.87252 +** it is appropriate to apply the LIKE optimization to that function
 1.87253 +** then set aWc[0] through aWc[2] to the wildcard characters and
 1.87254 +** return TRUE.  If the function is not a LIKE-style function then
 1.87255 +** return FALSE.
 1.87256 +*/
 1.87257 +SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
 1.87258 +  FuncDef *pDef;
 1.87259 +  if( pExpr->op!=TK_FUNCTION 
 1.87260 +   || !pExpr->x.pList 
 1.87261 +   || pExpr->x.pList->nExpr!=2
 1.87262 +  ){
 1.87263 +    return 0;
 1.87264 +  }
 1.87265 +  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 1.87266 +  pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
 1.87267 +                             sqlite3Strlen30(pExpr->u.zToken),
 1.87268 +                             2, SQLITE_UTF8, 0);
 1.87269 +  if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
 1.87270 +    return 0;
 1.87271 +  }
 1.87272 +
 1.87273 +  /* The memcpy() statement assumes that the wildcard characters are
 1.87274 +  ** the first three statements in the compareInfo structure.  The
 1.87275 +  ** asserts() that follow verify that assumption
 1.87276 +  */
 1.87277 +  memcpy(aWc, pDef->pUserData, 3);
 1.87278 +  assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
 1.87279 +  assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
 1.87280 +  assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
 1.87281 +  *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
 1.87282 +  return 1;
 1.87283 +}
 1.87284 +
 1.87285 +/*
 1.87286 +** All all of the FuncDef structures in the aBuiltinFunc[] array above
 1.87287 +** to the global function hash table.  This occurs at start-time (as
 1.87288 +** a consequence of calling sqlite3_initialize()).
 1.87289 +**
 1.87290 +** After this routine runs
 1.87291 +*/
 1.87292 +SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
 1.87293 +  /*
 1.87294 +  ** The following array holds FuncDef structures for all of the functions
 1.87295 +  ** defined in this file.
 1.87296 +  **
 1.87297 +  ** The array cannot be constant since changes are made to the
 1.87298 +  ** FuncDef.pHash elements at start-time.  The elements of this array
 1.87299 +  ** are read-only after initialization is complete.
 1.87300 +  */
 1.87301 +  static SQLITE_WSD FuncDef aBuiltinFunc[] = {
 1.87302 +    FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
 1.87303 +    FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
 1.87304 +    FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
 1.87305 +    FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
 1.87306 +    FUNCTION(trim,               1, 3, 0, trimFunc         ),
 1.87307 +    FUNCTION(trim,               2, 3, 0, trimFunc         ),
 1.87308 +    FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
 1.87309 +    FUNCTION(min,                0, 0, 1, 0                ),
 1.87310 +    AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
 1.87311 +    FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
 1.87312 +    FUNCTION(max,                0, 1, 1, 0                ),
 1.87313 +    AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
 1.87314 +    FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
 1.87315 +    FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
 1.87316 +    FUNCTION(instr,              2, 0, 0, instrFunc        ),
 1.87317 +    FUNCTION(substr,             2, 0, 0, substrFunc       ),
 1.87318 +    FUNCTION(substr,             3, 0, 0, substrFunc       ),
 1.87319 +    FUNCTION(abs,                1, 0, 0, absFunc          ),
 1.87320 +#ifndef SQLITE_OMIT_FLOATING_POINT
 1.87321 +    FUNCTION(round,              1, 0, 0, roundFunc        ),
 1.87322 +    FUNCTION(round,              2, 0, 0, roundFunc        ),
 1.87323 +#endif
 1.87324 +    FUNCTION(upper,              1, 0, 0, upperFunc        ),
 1.87325 +    FUNCTION(lower,              1, 0, 0, lowerFunc        ),
 1.87326 +    FUNCTION(coalesce,           1, 0, 0, 0                ),
 1.87327 +    FUNCTION(coalesce,           0, 0, 0, 0                ),
 1.87328 +    FUNCTION2(coalesce,         -1, 0, 0, ifnullFunc,  SQLITE_FUNC_COALESCE),
 1.87329 +    FUNCTION(hex,                1, 0, 0, hexFunc          ),
 1.87330 +    FUNCTION2(ifnull,            2, 0, 0, ifnullFunc,  SQLITE_FUNC_COALESCE),
 1.87331 +    FUNCTION(random,             0, 0, 0, randomFunc       ),
 1.87332 +    FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
 1.87333 +    FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
 1.87334 +    FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
 1.87335 +    FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
 1.87336 +    FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
 1.87337 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 1.87338 +    FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
 1.87339 +    FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
 1.87340 +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 1.87341 +    FUNCTION(quote,              1, 0, 0, quoteFunc        ),
 1.87342 +    FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
 1.87343 +    FUNCTION(changes,            0, 0, 0, changes          ),
 1.87344 +    FUNCTION(total_changes,      0, 0, 0, total_changes    ),
 1.87345 +    FUNCTION(replace,            3, 0, 0, replaceFunc      ),
 1.87346 +    FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
 1.87347 +  #ifdef SQLITE_SOUNDEX
 1.87348 +    FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
 1.87349 +  #endif
 1.87350 +  #ifndef SQLITE_OMIT_LOAD_EXTENSION
 1.87351 +    FUNCTION(load_extension,     1, 0, 0, loadExt          ),
 1.87352 +    FUNCTION(load_extension,     2, 0, 0, loadExt          ),
 1.87353 +  #endif
 1.87354 +    AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
 1.87355 +    AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
 1.87356 +    AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
 1.87357 + /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
 1.87358 +    {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
 1.87359 +    AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
 1.87360 +    AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
 1.87361 +    AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
 1.87362 +  
 1.87363 +    LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 1.87364 +  #ifdef SQLITE_CASE_SENSITIVE_LIKE
 1.87365 +    LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 1.87366 +    LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 1.87367 +  #else
 1.87368 +    LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
 1.87369 +    LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
 1.87370 +  #endif
 1.87371 +  };
 1.87372 +
 1.87373 +  int i;
 1.87374 +  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 1.87375 +  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
 1.87376 +
 1.87377 +  for(i=0; i<ArraySize(aBuiltinFunc); i++){
 1.87378 +    sqlite3FuncDefInsert(pHash, &aFunc[i]);
 1.87379 +  }
 1.87380 +  sqlite3RegisterDateTimeFunctions();
 1.87381 +#ifndef SQLITE_OMIT_ALTERTABLE
 1.87382 +  sqlite3AlterFunctions();
 1.87383 +#endif
 1.87384 +}
 1.87385 +
 1.87386 +/************** End of func.c ************************************************/
 1.87387 +/************** Begin file fkey.c ********************************************/
 1.87388 +/*
 1.87389 +**
 1.87390 +** The author disclaims copyright to this source code.  In place of
 1.87391 +** a legal notice, here is a blessing:
 1.87392 +**
 1.87393 +**    May you do good and not evil.
 1.87394 +**    May you find forgiveness for yourself and forgive others.
 1.87395 +**    May you share freely, never taking more than you give.
 1.87396 +**
 1.87397 +*************************************************************************
 1.87398 +** This file contains code used by the compiler to add foreign key
 1.87399 +** support to compiled SQL statements.
 1.87400 +*/
 1.87401 +
 1.87402 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.87403 +#ifndef SQLITE_OMIT_TRIGGER
 1.87404 +
 1.87405 +/*
 1.87406 +** Deferred and Immediate FKs
 1.87407 +** --------------------------
 1.87408 +**
 1.87409 +** Foreign keys in SQLite come in two flavours: deferred and immediate.
 1.87410 +** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
 1.87411 +** is returned and the current statement transaction rolled back. If a 
 1.87412 +** deferred foreign key constraint is violated, no action is taken 
 1.87413 +** immediately. However if the application attempts to commit the 
 1.87414 +** transaction before fixing the constraint violation, the attempt fails.
 1.87415 +**
 1.87416 +** Deferred constraints are implemented using a simple counter associated
 1.87417 +** with the database handle. The counter is set to zero each time a 
 1.87418 +** database transaction is opened. Each time a statement is executed 
 1.87419 +** that causes a foreign key violation, the counter is incremented. Each
 1.87420 +** time a statement is executed that removes an existing violation from
 1.87421 +** the database, the counter is decremented. When the transaction is
 1.87422 +** committed, the commit fails if the current value of the counter is
 1.87423 +** greater than zero. This scheme has two big drawbacks:
 1.87424 +**
 1.87425 +**   * When a commit fails due to a deferred foreign key constraint, 
 1.87426 +**     there is no way to tell which foreign constraint is not satisfied,
 1.87427 +**     or which row it is not satisfied for.
 1.87428 +**
 1.87429 +**   * If the database contains foreign key violations when the 
 1.87430 +**     transaction is opened, this may cause the mechanism to malfunction.
 1.87431 +**
 1.87432 +** Despite these problems, this approach is adopted as it seems simpler
 1.87433 +** than the alternatives.
 1.87434 +**
 1.87435 +** INSERT operations:
 1.87436 +**
 1.87437 +**   I.1) For each FK for which the table is the child table, search
 1.87438 +**        the parent table for a match. If none is found increment the
 1.87439 +**        constraint counter.
 1.87440 +**
 1.87441 +**   I.2) For each FK for which the table is the parent table, 
 1.87442 +**        search the child table for rows that correspond to the new
 1.87443 +**        row in the parent table. Decrement the counter for each row
 1.87444 +**        found (as the constraint is now satisfied).
 1.87445 +**
 1.87446 +** DELETE operations:
 1.87447 +**
 1.87448 +**   D.1) For each FK for which the table is the child table, 
 1.87449 +**        search the parent table for a row that corresponds to the 
 1.87450 +**        deleted row in the child table. If such a row is not found, 
 1.87451 +**        decrement the counter.
 1.87452 +**
 1.87453 +**   D.2) For each FK for which the table is the parent table, search 
 1.87454 +**        the child table for rows that correspond to the deleted row 
 1.87455 +**        in the parent table. For each found increment the counter.
 1.87456 +**
 1.87457 +** UPDATE operations:
 1.87458 +**
 1.87459 +**   An UPDATE command requires that all 4 steps above are taken, but only
 1.87460 +**   for FK constraints for which the affected columns are actually 
 1.87461 +**   modified (values must be compared at runtime).
 1.87462 +**
 1.87463 +** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
 1.87464 +** This simplifies the implementation a bit.
 1.87465 +**
 1.87466 +** For the purposes of immediate FK constraints, the OR REPLACE conflict
 1.87467 +** resolution is considered to delete rows before the new row is inserted.
 1.87468 +** If a delete caused by OR REPLACE violates an FK constraint, an exception
 1.87469 +** is thrown, even if the FK constraint would be satisfied after the new 
 1.87470 +** row is inserted.
 1.87471 +**
 1.87472 +** Immediate constraints are usually handled similarly. The only difference 
 1.87473 +** is that the counter used is stored as part of each individual statement
 1.87474 +** object (struct Vdbe). If, after the statement has run, its immediate
 1.87475 +** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
 1.87476 +** and the statement transaction is rolled back. An exception is an INSERT
 1.87477 +** statement that inserts a single row only (no triggers). In this case,
 1.87478 +** instead of using a counter, an exception is thrown immediately if the
 1.87479 +** INSERT violates a foreign key constraint. This is necessary as such
 1.87480 +** an INSERT does not open a statement transaction.
 1.87481 +**
 1.87482 +** TODO: How should dropping a table be handled? How should renaming a 
 1.87483 +** table be handled?
 1.87484 +**
 1.87485 +**
 1.87486 +** Query API Notes
 1.87487 +** ---------------
 1.87488 +**
 1.87489 +** Before coding an UPDATE or DELETE row operation, the code-generator
 1.87490 +** for those two operations needs to know whether or not the operation
 1.87491 +** requires any FK processing and, if so, which columns of the original
 1.87492 +** row are required by the FK processing VDBE code (i.e. if FKs were
 1.87493 +** implemented using triggers, which of the old.* columns would be 
 1.87494 +** accessed). No information is required by the code-generator before
 1.87495 +** coding an INSERT operation. The functions used by the UPDATE/DELETE
 1.87496 +** generation code to query for this information are:
 1.87497 +**
 1.87498 +**   sqlite3FkRequired() - Test to see if FK processing is required.
 1.87499 +**   sqlite3FkOldmask()  - Query for the set of required old.* columns.
 1.87500 +**
 1.87501 +**
 1.87502 +** Externally accessible module functions
 1.87503 +** --------------------------------------
 1.87504 +**
 1.87505 +**   sqlite3FkCheck()    - Check for foreign key violations.
 1.87506 +**   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
 1.87507 +**   sqlite3FkDelete()   - Delete an FKey structure.
 1.87508 +*/
 1.87509 +
 1.87510 +/*
 1.87511 +** VDBE Calling Convention
 1.87512 +** -----------------------
 1.87513 +**
 1.87514 +** Example:
 1.87515 +**
 1.87516 +**   For the following INSERT statement:
 1.87517 +**
 1.87518 +**     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
 1.87519 +**     INSERT INTO t1 VALUES(1, 2, 3.1);
 1.87520 +**
 1.87521 +**   Register (x):        2    (type integer)
 1.87522 +**   Register (x+1):      1    (type integer)
 1.87523 +**   Register (x+2):      NULL (type NULL)
 1.87524 +**   Register (x+3):      3.1  (type real)
 1.87525 +*/
 1.87526 +
 1.87527 +/*
 1.87528 +** A foreign key constraint requires that the key columns in the parent
 1.87529 +** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
 1.87530 +** Given that pParent is the parent table for foreign key constraint pFKey, 
 1.87531 +** search the schema a unique index on the parent key columns. 
 1.87532 +**
 1.87533 +** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
 1.87534 +** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
 1.87535 +** is set to point to the unique index. 
 1.87536 +** 
 1.87537 +** If the parent key consists of a single column (the foreign key constraint
 1.87538 +** is not a composite foreign key), output variable *paiCol is set to NULL.
 1.87539 +** Otherwise, it is set to point to an allocated array of size N, where
 1.87540 +** N is the number of columns in the parent key. The first element of the
 1.87541 +** array is the index of the child table column that is mapped by the FK
 1.87542 +** constraint to the parent table column stored in the left-most column
 1.87543 +** of index *ppIdx. The second element of the array is the index of the
 1.87544 +** child table column that corresponds to the second left-most column of
 1.87545 +** *ppIdx, and so on.
 1.87546 +**
 1.87547 +** If the required index cannot be found, either because:
 1.87548 +**
 1.87549 +**   1) The named parent key columns do not exist, or
 1.87550 +**
 1.87551 +**   2) The named parent key columns do exist, but are not subject to a
 1.87552 +**      UNIQUE or PRIMARY KEY constraint, or
 1.87553 +**
 1.87554 +**   3) No parent key columns were provided explicitly as part of the
 1.87555 +**      foreign key definition, and the parent table does not have a
 1.87556 +**      PRIMARY KEY, or
 1.87557 +**
 1.87558 +**   4) No parent key columns were provided explicitly as part of the
 1.87559 +**      foreign key definition, and the PRIMARY KEY of the parent table 
 1.87560 +**      consists of a a different number of columns to the child key in 
 1.87561 +**      the child table.
 1.87562 +**
 1.87563 +** then non-zero is returned, and a "foreign key mismatch" error loaded
 1.87564 +** into pParse. If an OOM error occurs, non-zero is returned and the
 1.87565 +** pParse->db->mallocFailed flag is set.
 1.87566 +*/
 1.87567 +static int locateFkeyIndex(
 1.87568 +  Parse *pParse,                  /* Parse context to store any error in */
 1.87569 +  Table *pParent,                 /* Parent table of FK constraint pFKey */
 1.87570 +  FKey *pFKey,                    /* Foreign key to find index for */
 1.87571 +  Index **ppIdx,                  /* OUT: Unique index on parent table */
 1.87572 +  int **paiCol                    /* OUT: Map of index columns in pFKey */
 1.87573 +){
 1.87574 +  Index *pIdx = 0;                    /* Value to return via *ppIdx */
 1.87575 +  int *aiCol = 0;                     /* Value to return via *paiCol */
 1.87576 +  int nCol = pFKey->nCol;             /* Number of columns in parent key */
 1.87577 +  char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
 1.87578 +
 1.87579 +  /* The caller is responsible for zeroing output parameters. */
 1.87580 +  assert( ppIdx && *ppIdx==0 );
 1.87581 +  assert( !paiCol || *paiCol==0 );
 1.87582 +  assert( pParse );
 1.87583 +
 1.87584 +  /* If this is a non-composite (single column) foreign key, check if it 
 1.87585 +  ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx 
 1.87586 +  ** and *paiCol set to zero and return early. 
 1.87587 +  **
 1.87588 +  ** Otherwise, for a composite foreign key (more than one column), allocate
 1.87589 +  ** space for the aiCol array (returned via output parameter *paiCol).
 1.87590 +  ** Non-composite foreign keys do not require the aiCol array.
 1.87591 +  */
 1.87592 +  if( nCol==1 ){
 1.87593 +    /* The FK maps to the IPK if any of the following are true:
 1.87594 +    **
 1.87595 +    **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly 
 1.87596 +    **      mapped to the primary key of table pParent, or
 1.87597 +    **   2) The FK is explicitly mapped to a column declared as INTEGER
 1.87598 +    **      PRIMARY KEY.
 1.87599 +    */
 1.87600 +    if( pParent->iPKey>=0 ){
 1.87601 +      if( !zKey ) return 0;
 1.87602 +      if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
 1.87603 +    }
 1.87604 +  }else if( paiCol ){
 1.87605 +    assert( nCol>1 );
 1.87606 +    aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
 1.87607 +    if( !aiCol ) return 1;
 1.87608 +    *paiCol = aiCol;
 1.87609 +  }
 1.87610 +
 1.87611 +  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
 1.87612 +    if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
 1.87613 +      /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
 1.87614 +      ** of columns. If each indexed column corresponds to a foreign key
 1.87615 +      ** column of pFKey, then this index is a winner.  */
 1.87616 +
 1.87617 +      if( zKey==0 ){
 1.87618 +        /* If zKey is NULL, then this foreign key is implicitly mapped to 
 1.87619 +        ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
 1.87620 +        ** identified by the test (Index.autoIndex==2).  */
 1.87621 +        if( pIdx->autoIndex==2 ){
 1.87622 +          if( aiCol ){
 1.87623 +            int i;
 1.87624 +            for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
 1.87625 +          }
 1.87626 +          break;
 1.87627 +        }
 1.87628 +      }else{
 1.87629 +        /* If zKey is non-NULL, then this foreign key was declared to
 1.87630 +        ** map to an explicit list of columns in table pParent. Check if this
 1.87631 +        ** index matches those columns. Also, check that the index uses
 1.87632 +        ** the default collation sequences for each column. */
 1.87633 +        int i, j;
 1.87634 +        for(i=0; i<nCol; i++){
 1.87635 +          int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
 1.87636 +          char *zDfltColl;                  /* Def. collation for column */
 1.87637 +          char *zIdxCol;                    /* Name of indexed column */
 1.87638 +
 1.87639 +          /* If the index uses a collation sequence that is different from
 1.87640 +          ** the default collation sequence for the column, this index is
 1.87641 +          ** unusable. Bail out early in this case.  */
 1.87642 +          zDfltColl = pParent->aCol[iCol].zColl;
 1.87643 +          if( !zDfltColl ){
 1.87644 +            zDfltColl = "BINARY";
 1.87645 +          }
 1.87646 +          if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
 1.87647 +
 1.87648 +          zIdxCol = pParent->aCol[iCol].zName;
 1.87649 +          for(j=0; j<nCol; j++){
 1.87650 +            if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
 1.87651 +              if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
 1.87652 +              break;
 1.87653 +            }
 1.87654 +          }
 1.87655 +          if( j==nCol ) break;
 1.87656 +        }
 1.87657 +        if( i==nCol ) break;      /* pIdx is usable */
 1.87658 +      }
 1.87659 +    }
 1.87660 +  }
 1.87661 +
 1.87662 +  if( !pIdx ){
 1.87663 +    if( !pParse->disableTriggers ){
 1.87664 +      sqlite3ErrorMsg(pParse, "foreign key mismatch");
 1.87665 +    }
 1.87666 +    sqlite3DbFree(pParse->db, aiCol);
 1.87667 +    return 1;
 1.87668 +  }
 1.87669 +
 1.87670 +  *ppIdx = pIdx;
 1.87671 +  return 0;
 1.87672 +}
 1.87673 +
 1.87674 +/*
 1.87675 +** This function is called when a row is inserted into or deleted from the 
 1.87676 +** child table of foreign key constraint pFKey. If an SQL UPDATE is executed 
 1.87677 +** on the child table of pFKey, this function is invoked twice for each row
 1.87678 +** affected - once to "delete" the old row, and then again to "insert" the
 1.87679 +** new row.
 1.87680 +**
 1.87681 +** Each time it is called, this function generates VDBE code to locate the
 1.87682 +** row in the parent table that corresponds to the row being inserted into 
 1.87683 +** or deleted from the child table. If the parent row can be found, no 
 1.87684 +** special action is taken. Otherwise, if the parent row can *not* be
 1.87685 +** found in the parent table:
 1.87686 +**
 1.87687 +**   Operation | FK type   | Action taken
 1.87688 +**   --------------------------------------------------------------------------
 1.87689 +**   INSERT      immediate   Increment the "immediate constraint counter".
 1.87690 +**
 1.87691 +**   DELETE      immediate   Decrement the "immediate constraint counter".
 1.87692 +**
 1.87693 +**   INSERT      deferred    Increment the "deferred constraint counter".
 1.87694 +**
 1.87695 +**   DELETE      deferred    Decrement the "deferred constraint counter".
 1.87696 +**
 1.87697 +** These operations are identified in the comment at the top of this file 
 1.87698 +** (fkey.c) as "I.1" and "D.1".
 1.87699 +*/
 1.87700 +static void fkLookupParent(
 1.87701 +  Parse *pParse,        /* Parse context */
 1.87702 +  int iDb,              /* Index of database housing pTab */
 1.87703 +  Table *pTab,          /* Parent table of FK pFKey */
 1.87704 +  Index *pIdx,          /* Unique index on parent key columns in pTab */
 1.87705 +  FKey *pFKey,          /* Foreign key constraint */
 1.87706 +  int *aiCol,           /* Map from parent key columns to child table columns */
 1.87707 +  int regData,          /* Address of array containing child table row */
 1.87708 +  int nIncr,            /* Increment constraint counter by this */
 1.87709 +  int isIgnore          /* If true, pretend pTab contains all NULL values */
 1.87710 +){
 1.87711 +  int i;                                    /* Iterator variable */
 1.87712 +  Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
 1.87713 +  int iCur = pParse->nTab - 1;              /* Cursor number to use */
 1.87714 +  int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
 1.87715 +
 1.87716 +  /* If nIncr is less than zero, then check at runtime if there are any
 1.87717 +  ** outstanding constraints to resolve. If there are not, there is no need
 1.87718 +  ** to check if deleting this row resolves any outstanding violations.
 1.87719 +  **
 1.87720 +  ** Check if any of the key columns in the child table row are NULL. If 
 1.87721 +  ** any are, then the constraint is considered satisfied. No need to 
 1.87722 +  ** search for a matching row in the parent table.  */
 1.87723 +  if( nIncr<0 ){
 1.87724 +    sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
 1.87725 +  }
 1.87726 +  for(i=0; i<pFKey->nCol; i++){
 1.87727 +    int iReg = aiCol[i] + regData + 1;
 1.87728 +    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
 1.87729 +  }
 1.87730 +
 1.87731 +  if( isIgnore==0 ){
 1.87732 +    if( pIdx==0 ){
 1.87733 +      /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
 1.87734 +      ** column of the parent table (table pTab).  */
 1.87735 +      int iMustBeInt;               /* Address of MustBeInt instruction */
 1.87736 +      int regTemp = sqlite3GetTempReg(pParse);
 1.87737 +  
 1.87738 +      /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
 1.87739 +      ** apply the affinity of the parent key). If this fails, then there
 1.87740 +      ** is no matching parent key. Before using MustBeInt, make a copy of
 1.87741 +      ** the value. Otherwise, the value inserted into the child key column
 1.87742 +      ** will have INTEGER affinity applied to it, which may not be correct.  */
 1.87743 +      sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
 1.87744 +      iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
 1.87745 +  
 1.87746 +      /* If the parent table is the same as the child table, and we are about
 1.87747 +      ** to increment the constraint-counter (i.e. this is an INSERT operation),
 1.87748 +      ** then check if the row being inserted matches itself. If so, do not
 1.87749 +      ** increment the constraint-counter.  */
 1.87750 +      if( pTab==pFKey->pFrom && nIncr==1 ){
 1.87751 +        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
 1.87752 +      }
 1.87753 +  
 1.87754 +      sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
 1.87755 +      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
 1.87756 +      sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 1.87757 +      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
 1.87758 +      sqlite3VdbeJumpHere(v, iMustBeInt);
 1.87759 +      sqlite3ReleaseTempReg(pParse, regTemp);
 1.87760 +    }else{
 1.87761 +      int nCol = pFKey->nCol;
 1.87762 +      int regTemp = sqlite3GetTempRange(pParse, nCol);
 1.87763 +      int regRec = sqlite3GetTempReg(pParse);
 1.87764 +      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 1.87765 +  
 1.87766 +      sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
 1.87767 +      sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
 1.87768 +      for(i=0; i<nCol; i++){
 1.87769 +        sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
 1.87770 +      }
 1.87771 +  
 1.87772 +      /* If the parent table is the same as the child table, and we are about
 1.87773 +      ** to increment the constraint-counter (i.e. this is an INSERT operation),
 1.87774 +      ** then check if the row being inserted matches itself. If so, do not
 1.87775 +      ** increment the constraint-counter. 
 1.87776 +      **
 1.87777 +      ** If any of the parent-key values are NULL, then the row cannot match 
 1.87778 +      ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
 1.87779 +      ** of the parent-key values are NULL (at this point it is known that
 1.87780 +      ** none of the child key values are).
 1.87781 +      */
 1.87782 +      if( pTab==pFKey->pFrom && nIncr==1 ){
 1.87783 +        int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
 1.87784 +        for(i=0; i<nCol; i++){
 1.87785 +          int iChild = aiCol[i]+1+regData;
 1.87786 +          int iParent = pIdx->aiColumn[i]+1+regData;
 1.87787 +          assert( aiCol[i]!=pTab->iPKey );
 1.87788 +          if( pIdx->aiColumn[i]==pTab->iPKey ){
 1.87789 +            /* The parent key is a composite key that includes the IPK column */
 1.87790 +            iParent = regData;
 1.87791 +          }
 1.87792 +          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
 1.87793 +          sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 1.87794 +        }
 1.87795 +        sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 1.87796 +      }
 1.87797 +  
 1.87798 +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
 1.87799 +      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
 1.87800 +      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
 1.87801 +  
 1.87802 +      sqlite3ReleaseTempReg(pParse, regRec);
 1.87803 +      sqlite3ReleaseTempRange(pParse, regTemp, nCol);
 1.87804 +    }
 1.87805 +  }
 1.87806 +
 1.87807 +  if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
 1.87808 +    /* Special case: If this is an INSERT statement that will insert exactly
 1.87809 +    ** one row into the table, raise a constraint immediately instead of
 1.87810 +    ** incrementing a counter. This is necessary as the VM code is being
 1.87811 +    ** generated for will not open a statement transaction.  */
 1.87812 +    assert( nIncr==1 );
 1.87813 +    sqlite3HaltConstraint(
 1.87814 +        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
 1.87815 +    );
 1.87816 +  }else{
 1.87817 +    if( nIncr>0 && pFKey->isDeferred==0 ){
 1.87818 +      sqlite3ParseToplevel(pParse)->mayAbort = 1;
 1.87819 +    }
 1.87820 +    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
 1.87821 +  }
 1.87822 +
 1.87823 +  sqlite3VdbeResolveLabel(v, iOk);
 1.87824 +  sqlite3VdbeAddOp1(v, OP_Close, iCur);
 1.87825 +}
 1.87826 +
 1.87827 +/*
 1.87828 +** This function is called to generate code executed when a row is deleted
 1.87829 +** from the parent table of foreign key constraint pFKey and, if pFKey is 
 1.87830 +** deferred, when a row is inserted into the same table. When generating
 1.87831 +** code for an SQL UPDATE operation, this function may be called twice -
 1.87832 +** once to "delete" the old row and once to "insert" the new row.
 1.87833 +**
 1.87834 +** The code generated by this function scans through the rows in the child
 1.87835 +** table that correspond to the parent table row being deleted or inserted.
 1.87836 +** For each child row found, one of the following actions is taken:
 1.87837 +**
 1.87838 +**   Operation | FK type   | Action taken
 1.87839 +**   --------------------------------------------------------------------------
 1.87840 +**   DELETE      immediate   Increment the "immediate constraint counter".
 1.87841 +**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
 1.87842 +**                           throw a "foreign key constraint failed" exception.
 1.87843 +**
 1.87844 +**   INSERT      immediate   Decrement the "immediate constraint counter".
 1.87845 +**
 1.87846 +**   DELETE      deferred    Increment the "deferred constraint counter".
 1.87847 +**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
 1.87848 +**                           throw a "foreign key constraint failed" exception.
 1.87849 +**
 1.87850 +**   INSERT      deferred    Decrement the "deferred constraint counter".
 1.87851 +**
 1.87852 +** These operations are identified in the comment at the top of this file 
 1.87853 +** (fkey.c) as "I.2" and "D.2".
 1.87854 +*/
 1.87855 +static void fkScanChildren(
 1.87856 +  Parse *pParse,                  /* Parse context */
 1.87857 +  SrcList *pSrc,                  /* SrcList containing the table to scan */
 1.87858 +  Table *pTab,
 1.87859 +  Index *pIdx,                    /* Foreign key index */
 1.87860 +  FKey *pFKey,                    /* Foreign key relationship */
 1.87861 +  int *aiCol,                     /* Map from pIdx cols to child table cols */
 1.87862 +  int regData,                    /* Referenced table data starts here */
 1.87863 +  int nIncr                       /* Amount to increment deferred counter by */
 1.87864 +){
 1.87865 +  sqlite3 *db = pParse->db;       /* Database handle */
 1.87866 +  int i;                          /* Iterator variable */
 1.87867 +  Expr *pWhere = 0;               /* WHERE clause to scan with */
 1.87868 +  NameContext sNameContext;       /* Context used to resolve WHERE clause */
 1.87869 +  WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
 1.87870 +  int iFkIfZero = 0;              /* Address of OP_FkIfZero */
 1.87871 +  Vdbe *v = sqlite3GetVdbe(pParse);
 1.87872 +
 1.87873 +  assert( !pIdx || pIdx->pTable==pTab );
 1.87874 +
 1.87875 +  if( nIncr<0 ){
 1.87876 +    iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
 1.87877 +  }
 1.87878 +
 1.87879 +  /* Create an Expr object representing an SQL expression like:
 1.87880 +  **
 1.87881 +  **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
 1.87882 +  **
 1.87883 +  ** The collation sequence used for the comparison should be that of
 1.87884 +  ** the parent key columns. The affinity of the parent key column should
 1.87885 +  ** be applied to each child key value before the comparison takes place.
 1.87886 +  */
 1.87887 +  for(i=0; i<pFKey->nCol; i++){
 1.87888 +    Expr *pLeft;                  /* Value from parent table row */
 1.87889 +    Expr *pRight;                 /* Column ref to child table */
 1.87890 +    Expr *pEq;                    /* Expression (pLeft = pRight) */
 1.87891 +    int iCol;                     /* Index of column in child table */ 
 1.87892 +    const char *zCol;             /* Name of column in child table */
 1.87893 +
 1.87894 +    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
 1.87895 +    if( pLeft ){
 1.87896 +      /* Set the collation sequence and affinity of the LHS of each TK_EQ
 1.87897 +      ** expression to the parent key column defaults.  */
 1.87898 +      if( pIdx ){
 1.87899 +        Column *pCol;
 1.87900 +        const char *zColl;
 1.87901 +        iCol = pIdx->aiColumn[i];
 1.87902 +        pCol = &pTab->aCol[iCol];
 1.87903 +        if( pTab->iPKey==iCol ) iCol = -1;
 1.87904 +        pLeft->iTable = regData+iCol+1;
 1.87905 +        pLeft->affinity = pCol->affinity;
 1.87906 +        zColl = pCol->zColl;
 1.87907 +        if( zColl==0 ) zColl = db->pDfltColl->zName;
 1.87908 +        pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
 1.87909 +      }else{
 1.87910 +        pLeft->iTable = regData;
 1.87911 +        pLeft->affinity = SQLITE_AFF_INTEGER;
 1.87912 +      }
 1.87913 +    }
 1.87914 +    iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 1.87915 +    assert( iCol>=0 );
 1.87916 +    zCol = pFKey->pFrom->aCol[iCol].zName;
 1.87917 +    pRight = sqlite3Expr(db, TK_ID, zCol);
 1.87918 +    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
 1.87919 +    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 1.87920 +  }
 1.87921 +
 1.87922 +  /* If the child table is the same as the parent table, and this scan
 1.87923 +  ** is taking place as part of a DELETE operation (operation D.2), omit the
 1.87924 +  ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
 1.87925 +  ** clause, where $rowid is the rowid of the row being deleted.  */
 1.87926 +  if( pTab==pFKey->pFrom && nIncr>0 ){
 1.87927 +    Expr *pEq;                    /* Expression (pLeft = pRight) */
 1.87928 +    Expr *pLeft;                  /* Value from parent table row */
 1.87929 +    Expr *pRight;                 /* Column ref to child table */
 1.87930 +    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
 1.87931 +    pRight = sqlite3Expr(db, TK_COLUMN, 0);
 1.87932 +    if( pLeft && pRight ){
 1.87933 +      pLeft->iTable = regData;
 1.87934 +      pLeft->affinity = SQLITE_AFF_INTEGER;
 1.87935 +      pRight->iTable = pSrc->a[0].iCursor;
 1.87936 +      pRight->iColumn = -1;
 1.87937 +    }
 1.87938 +    pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
 1.87939 +    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 1.87940 +  }
 1.87941 +
 1.87942 +  /* Resolve the references in the WHERE clause. */
 1.87943 +  memset(&sNameContext, 0, sizeof(NameContext));
 1.87944 +  sNameContext.pSrcList = pSrc;
 1.87945 +  sNameContext.pParse = pParse;
 1.87946 +  sqlite3ResolveExprNames(&sNameContext, pWhere);
 1.87947 +
 1.87948 +  /* Create VDBE to loop through the entries in pSrc that match the WHERE
 1.87949 +  ** clause. If the constraint is not deferred, throw an exception for
 1.87950 +  ** each row found. Otherwise, for deferred constraints, increment the
 1.87951 +  ** deferred constraint counter by nIncr for each row selected.  */
 1.87952 +  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
 1.87953 +  if( nIncr>0 && pFKey->isDeferred==0 ){
 1.87954 +    sqlite3ParseToplevel(pParse)->mayAbort = 1;
 1.87955 +  }
 1.87956 +  sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
 1.87957 +  if( pWInfo ){
 1.87958 +    sqlite3WhereEnd(pWInfo);
 1.87959 +  }
 1.87960 +
 1.87961 +  /* Clean up the WHERE clause constructed above. */
 1.87962 +  sqlite3ExprDelete(db, pWhere);
 1.87963 +  if( iFkIfZero ){
 1.87964 +    sqlite3VdbeJumpHere(v, iFkIfZero);
 1.87965 +  }
 1.87966 +}
 1.87967 +
 1.87968 +/*
 1.87969 +** This function returns a pointer to the head of a linked list of FK
 1.87970 +** constraints for which table pTab is the parent table. For example,
 1.87971 +** given the following schema:
 1.87972 +**
 1.87973 +**   CREATE TABLE t1(a PRIMARY KEY);
 1.87974 +**   CREATE TABLE t2(b REFERENCES t1(a);
 1.87975 +**
 1.87976 +** Calling this function with table "t1" as an argument returns a pointer
 1.87977 +** to the FKey structure representing the foreign key constraint on table
 1.87978 +** "t2". Calling this function with "t2" as the argument would return a
 1.87979 +** NULL pointer (as there are no FK constraints for which t2 is the parent
 1.87980 +** table).
 1.87981 +*/
 1.87982 +SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
 1.87983 +  int nName = sqlite3Strlen30(pTab->zName);
 1.87984 +  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
 1.87985 +}
 1.87986 +
 1.87987 +/*
 1.87988 +** The second argument is a Trigger structure allocated by the 
 1.87989 +** fkActionTrigger() routine. This function deletes the Trigger structure
 1.87990 +** and all of its sub-components.
 1.87991 +**
 1.87992 +** The Trigger structure or any of its sub-components may be allocated from
 1.87993 +** the lookaside buffer belonging to database handle dbMem.
 1.87994 +*/
 1.87995 +static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
 1.87996 +  if( p ){
 1.87997 +    TriggerStep *pStep = p->step_list;
 1.87998 +    sqlite3ExprDelete(dbMem, pStep->pWhere);
 1.87999 +    sqlite3ExprListDelete(dbMem, pStep->pExprList);
 1.88000 +    sqlite3SelectDelete(dbMem, pStep->pSelect);
 1.88001 +    sqlite3ExprDelete(dbMem, p->pWhen);
 1.88002 +    sqlite3DbFree(dbMem, p);
 1.88003 +  }
 1.88004 +}
 1.88005 +
 1.88006 +/*
 1.88007 +** This function is called to generate code that runs when table pTab is
 1.88008 +** being dropped from the database. The SrcList passed as the second argument
 1.88009 +** to this function contains a single entry guaranteed to resolve to
 1.88010 +** table pTab.
 1.88011 +**
 1.88012 +** Normally, no code is required. However, if either
 1.88013 +**
 1.88014 +**   (a) The table is the parent table of a FK constraint, or
 1.88015 +**   (b) The table is the child table of a deferred FK constraint and it is
 1.88016 +**       determined at runtime that there are outstanding deferred FK 
 1.88017 +**       constraint violations in the database,
 1.88018 +**
 1.88019 +** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
 1.88020 +** the table from the database. Triggers are disabled while running this
 1.88021 +** DELETE, but foreign key actions are not.
 1.88022 +*/
 1.88023 +SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
 1.88024 +  sqlite3 *db = pParse->db;
 1.88025 +  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
 1.88026 +    int iSkip = 0;
 1.88027 +    Vdbe *v = sqlite3GetVdbe(pParse);
 1.88028 +
 1.88029 +    assert( v );                  /* VDBE has already been allocated */
 1.88030 +    if( sqlite3FkReferences(pTab)==0 ){
 1.88031 +      /* Search for a deferred foreign key constraint for which this table
 1.88032 +      ** is the child table. If one cannot be found, return without 
 1.88033 +      ** generating any VDBE code. If one can be found, then jump over
 1.88034 +      ** the entire DELETE if there are no outstanding deferred constraints
 1.88035 +      ** when this statement is run.  */
 1.88036 +      FKey *p;
 1.88037 +      for(p=pTab->pFKey; p; p=p->pNextFrom){
 1.88038 +        if( p->isDeferred ) break;
 1.88039 +      }
 1.88040 +      if( !p ) return;
 1.88041 +      iSkip = sqlite3VdbeMakeLabel(v);
 1.88042 +      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
 1.88043 +    }
 1.88044 +
 1.88045 +    pParse->disableTriggers = 1;
 1.88046 +    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
 1.88047 +    pParse->disableTriggers = 0;
 1.88048 +
 1.88049 +    /* If the DELETE has generated immediate foreign key constraint 
 1.88050 +    ** violations, halt the VDBE and return an error at this point, before
 1.88051 +    ** any modifications to the schema are made. This is because statement
 1.88052 +    ** transactions are not able to rollback schema changes.  */
 1.88053 +    sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
 1.88054 +    sqlite3HaltConstraint(
 1.88055 +        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
 1.88056 +    );
 1.88057 +
 1.88058 +    if( iSkip ){
 1.88059 +      sqlite3VdbeResolveLabel(v, iSkip);
 1.88060 +    }
 1.88061 +  }
 1.88062 +}
 1.88063 +
 1.88064 +/*
 1.88065 +** This function is called when inserting, deleting or updating a row of
 1.88066 +** table pTab to generate VDBE code to perform foreign key constraint 
 1.88067 +** processing for the operation.
 1.88068 +**
 1.88069 +** For a DELETE operation, parameter regOld is passed the index of the
 1.88070 +** first register in an array of (pTab->nCol+1) registers containing the
 1.88071 +** rowid of the row being deleted, followed by each of the column values
 1.88072 +** of the row being deleted, from left to right. Parameter regNew is passed
 1.88073 +** zero in this case.
 1.88074 +**
 1.88075 +** For an INSERT operation, regOld is passed zero and regNew is passed the
 1.88076 +** first register of an array of (pTab->nCol+1) registers containing the new
 1.88077 +** row data.
 1.88078 +**
 1.88079 +** For an UPDATE operation, this function is called twice. Once before
 1.88080 +** the original record is deleted from the table using the calling convention
 1.88081 +** described for DELETE. Then again after the original record is deleted
 1.88082 +** but before the new record is inserted using the INSERT convention. 
 1.88083 +*/
 1.88084 +SQLITE_PRIVATE void sqlite3FkCheck(
 1.88085 +  Parse *pParse,                  /* Parse context */
 1.88086 +  Table *pTab,                    /* Row is being deleted from this table */ 
 1.88087 +  int regOld,                     /* Previous row data is stored here */
 1.88088 +  int regNew                      /* New row data is stored here */
 1.88089 +){
 1.88090 +  sqlite3 *db = pParse->db;       /* Database handle */
 1.88091 +  FKey *pFKey;                    /* Used to iterate through FKs */
 1.88092 +  int iDb;                        /* Index of database containing pTab */
 1.88093 +  const char *zDb;                /* Name of database containing pTab */
 1.88094 +  int isIgnoreErrors = pParse->disableTriggers;
 1.88095 +
 1.88096 +  /* Exactly one of regOld and regNew should be non-zero. */
 1.88097 +  assert( (regOld==0)!=(regNew==0) );
 1.88098 +
 1.88099 +  /* If foreign-keys are disabled, this function is a no-op. */
 1.88100 +  if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
 1.88101 +
 1.88102 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.88103 +  zDb = db->aDb[iDb].zName;
 1.88104 +
 1.88105 +  /* Loop through all the foreign key constraints for which pTab is the
 1.88106 +  ** child table (the table that the foreign key definition is part of).  */
 1.88107 +  for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
 1.88108 +    Table *pTo;                   /* Parent table of foreign key pFKey */
 1.88109 +    Index *pIdx = 0;              /* Index on key columns in pTo */
 1.88110 +    int *aiFree = 0;
 1.88111 +    int *aiCol;
 1.88112 +    int iCol;
 1.88113 +    int i;
 1.88114 +    int isIgnore = 0;
 1.88115 +
 1.88116 +    /* Find the parent table of this foreign key. Also find a unique index 
 1.88117 +    ** on the parent key columns in the parent table. If either of these 
 1.88118 +    ** schema items cannot be located, set an error in pParse and return 
 1.88119 +    ** early.  */
 1.88120 +    if( pParse->disableTriggers ){
 1.88121 +      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
 1.88122 +    }else{
 1.88123 +      pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
 1.88124 +    }
 1.88125 +    if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
 1.88126 +      assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
 1.88127 +      if( !isIgnoreErrors || db->mallocFailed ) return;
 1.88128 +      if( pTo==0 ){
 1.88129 +        /* If isIgnoreErrors is true, then a table is being dropped. In this
 1.88130 +        ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
 1.88131 +        ** before actually dropping it in order to check FK constraints.
 1.88132 +        ** If the parent table of an FK constraint on the current table is
 1.88133 +        ** missing, behave as if it is empty. i.e. decrement the relevant
 1.88134 +        ** FK counter for each row of the current table with non-NULL keys.
 1.88135 +        */
 1.88136 +        Vdbe *v = sqlite3GetVdbe(pParse);
 1.88137 +        int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
 1.88138 +        for(i=0; i<pFKey->nCol; i++){
 1.88139 +          int iReg = pFKey->aCol[i].iFrom + regOld + 1;
 1.88140 +          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
 1.88141 +        }
 1.88142 +        sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
 1.88143 +      }
 1.88144 +      continue;
 1.88145 +    }
 1.88146 +    assert( pFKey->nCol==1 || (aiFree && pIdx) );
 1.88147 +
 1.88148 +    if( aiFree ){
 1.88149 +      aiCol = aiFree;
 1.88150 +    }else{
 1.88151 +      iCol = pFKey->aCol[0].iFrom;
 1.88152 +      aiCol = &iCol;
 1.88153 +    }
 1.88154 +    for(i=0; i<pFKey->nCol; i++){
 1.88155 +      if( aiCol[i]==pTab->iPKey ){
 1.88156 +        aiCol[i] = -1;
 1.88157 +      }
 1.88158 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.88159 +      /* Request permission to read the parent key columns. If the 
 1.88160 +      ** authorization callback returns SQLITE_IGNORE, behave as if any
 1.88161 +      ** values read from the parent table are NULL. */
 1.88162 +      if( db->xAuth ){
 1.88163 +        int rcauth;
 1.88164 +        char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
 1.88165 +        rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
 1.88166 +        isIgnore = (rcauth==SQLITE_IGNORE);
 1.88167 +      }
 1.88168 +#endif
 1.88169 +    }
 1.88170 +
 1.88171 +    /* Take a shared-cache advisory read-lock on the parent table. Allocate 
 1.88172 +    ** a cursor to use to search the unique index on the parent key columns 
 1.88173 +    ** in the parent table.  */
 1.88174 +    sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
 1.88175 +    pParse->nTab++;
 1.88176 +
 1.88177 +    if( regOld!=0 ){
 1.88178 +      /* A row is being removed from the child table. Search for the parent.
 1.88179 +      ** If the parent does not exist, removing the child row resolves an 
 1.88180 +      ** outstanding foreign key constraint violation. */
 1.88181 +      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
 1.88182 +    }
 1.88183 +    if( regNew!=0 ){
 1.88184 +      /* A row is being added to the child table. If a parent row cannot
 1.88185 +      ** be found, adding the child row has violated the FK constraint. */ 
 1.88186 +      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
 1.88187 +    }
 1.88188 +
 1.88189 +    sqlite3DbFree(db, aiFree);
 1.88190 +  }
 1.88191 +
 1.88192 +  /* Loop through all the foreign key constraints that refer to this table */
 1.88193 +  for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
 1.88194 +    Index *pIdx = 0;              /* Foreign key index for pFKey */
 1.88195 +    SrcList *pSrc;
 1.88196 +    int *aiCol = 0;
 1.88197 +
 1.88198 +    if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
 1.88199 +      assert( regOld==0 && regNew!=0 );
 1.88200 +      /* Inserting a single row into a parent table cannot cause an immediate
 1.88201 +      ** foreign key violation. So do nothing in this case.  */
 1.88202 +      continue;
 1.88203 +    }
 1.88204 +
 1.88205 +    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
 1.88206 +      if( !isIgnoreErrors || db->mallocFailed ) return;
 1.88207 +      continue;
 1.88208 +    }
 1.88209 +    assert( aiCol || pFKey->nCol==1 );
 1.88210 +
 1.88211 +    /* Create a SrcList structure containing a single table (the table 
 1.88212 +    ** the foreign key that refers to this table is attached to). This
 1.88213 +    ** is required for the sqlite3WhereXXX() interface.  */
 1.88214 +    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
 1.88215 +    if( pSrc ){
 1.88216 +      struct SrcList_item *pItem = pSrc->a;
 1.88217 +      pItem->pTab = pFKey->pFrom;
 1.88218 +      pItem->zName = pFKey->pFrom->zName;
 1.88219 +      pItem->pTab->nRef++;
 1.88220 +      pItem->iCursor = pParse->nTab++;
 1.88221 +  
 1.88222 +      if( regNew!=0 ){
 1.88223 +        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
 1.88224 +      }
 1.88225 +      if( regOld!=0 ){
 1.88226 +        /* If there is a RESTRICT action configured for the current operation
 1.88227 +        ** on the parent table of this FK, then throw an exception 
 1.88228 +        ** immediately if the FK constraint is violated, even if this is a
 1.88229 +        ** deferred trigger. That's what RESTRICT means. To defer checking
 1.88230 +        ** the constraint, the FK should specify NO ACTION (represented
 1.88231 +        ** using OE_None). NO ACTION is the default.  */
 1.88232 +        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
 1.88233 +      }
 1.88234 +      pItem->zName = 0;
 1.88235 +      sqlite3SrcListDelete(db, pSrc);
 1.88236 +    }
 1.88237 +    sqlite3DbFree(db, aiCol);
 1.88238 +  }
 1.88239 +}
 1.88240 +
 1.88241 +#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
 1.88242 +
 1.88243 +/*
 1.88244 +** This function is called before generating code to update or delete a 
 1.88245 +** row contained in table pTab.
 1.88246 +*/
 1.88247 +SQLITE_PRIVATE u32 sqlite3FkOldmask(
 1.88248 +  Parse *pParse,                  /* Parse context */
 1.88249 +  Table *pTab                     /* Table being modified */
 1.88250 +){
 1.88251 +  u32 mask = 0;
 1.88252 +  if( pParse->db->flags&SQLITE_ForeignKeys ){
 1.88253 +    FKey *p;
 1.88254 +    int i;
 1.88255 +    for(p=pTab->pFKey; p; p=p->pNextFrom){
 1.88256 +      for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
 1.88257 +    }
 1.88258 +    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 1.88259 +      Index *pIdx = 0;
 1.88260 +      locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
 1.88261 +      if( pIdx ){
 1.88262 +        for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
 1.88263 +      }
 1.88264 +    }
 1.88265 +  }
 1.88266 +  return mask;
 1.88267 +}
 1.88268 +
 1.88269 +/*
 1.88270 +** This function is called before generating code to update or delete a 
 1.88271 +** row contained in table pTab. If the operation is a DELETE, then
 1.88272 +** parameter aChange is passed a NULL value. For an UPDATE, aChange points
 1.88273 +** to an array of size N, where N is the number of columns in table pTab.
 1.88274 +** If the i'th column is not modified by the UPDATE, then the corresponding 
 1.88275 +** entry in the aChange[] array is set to -1. If the column is modified,
 1.88276 +** the value is 0 or greater. Parameter chngRowid is set to true if the
 1.88277 +** UPDATE statement modifies the rowid fields of the table.
 1.88278 +**
 1.88279 +** If any foreign key processing will be required, this function returns
 1.88280 +** true. If there is no foreign key related processing, this function 
 1.88281 +** returns false.
 1.88282 +*/
 1.88283 +SQLITE_PRIVATE int sqlite3FkRequired(
 1.88284 +  Parse *pParse,                  /* Parse context */
 1.88285 +  Table *pTab,                    /* Table being modified */
 1.88286 +  int *aChange,                   /* Non-NULL for UPDATE operations */
 1.88287 +  int chngRowid                   /* True for UPDATE that affects rowid */
 1.88288 +){
 1.88289 +  if( pParse->db->flags&SQLITE_ForeignKeys ){
 1.88290 +    if( !aChange ){
 1.88291 +      /* A DELETE operation. Foreign key processing is required if the 
 1.88292 +      ** table in question is either the child or parent table for any 
 1.88293 +      ** foreign key constraint.  */
 1.88294 +      return (sqlite3FkReferences(pTab) || pTab->pFKey);
 1.88295 +    }else{
 1.88296 +      /* This is an UPDATE. Foreign key processing is only required if the
 1.88297 +      ** operation modifies one or more child or parent key columns. */
 1.88298 +      int i;
 1.88299 +      FKey *p;
 1.88300 +
 1.88301 +      /* Check if any child key columns are being modified. */
 1.88302 +      for(p=pTab->pFKey; p; p=p->pNextFrom){
 1.88303 +        for(i=0; i<p->nCol; i++){
 1.88304 +          int iChildKey = p->aCol[i].iFrom;
 1.88305 +          if( aChange[iChildKey]>=0 ) return 1;
 1.88306 +          if( iChildKey==pTab->iPKey && chngRowid ) return 1;
 1.88307 +        }
 1.88308 +      }
 1.88309 +
 1.88310 +      /* Check if any parent key columns are being modified. */
 1.88311 +      for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 1.88312 +        for(i=0; i<p->nCol; i++){
 1.88313 +          char *zKey = p->aCol[i].zCol;
 1.88314 +          int iKey;
 1.88315 +          for(iKey=0; iKey<pTab->nCol; iKey++){
 1.88316 +            Column *pCol = &pTab->aCol[iKey];
 1.88317 +            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey)
 1.88318 +                      : (pCol->colFlags & COLFLAG_PRIMKEY)!=0) ){
 1.88319 +              if( aChange[iKey]>=0 ) return 1;
 1.88320 +              if( iKey==pTab->iPKey && chngRowid ) return 1;
 1.88321 +            }
 1.88322 +          }
 1.88323 +        }
 1.88324 +      }
 1.88325 +    }
 1.88326 +  }
 1.88327 +  return 0;
 1.88328 +}
 1.88329 +
 1.88330 +/*
 1.88331 +** This function is called when an UPDATE or DELETE operation is being 
 1.88332 +** compiled on table pTab, which is the parent table of foreign-key pFKey.
 1.88333 +** If the current operation is an UPDATE, then the pChanges parameter is
 1.88334 +** passed a pointer to the list of columns being modified. If it is a
 1.88335 +** DELETE, pChanges is passed a NULL pointer.
 1.88336 +**
 1.88337 +** It returns a pointer to a Trigger structure containing a trigger
 1.88338 +** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
 1.88339 +** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
 1.88340 +** returned (these actions require no special handling by the triggers
 1.88341 +** sub-system, code for them is created by fkScanChildren()).
 1.88342 +**
 1.88343 +** For example, if pFKey is the foreign key and pTab is table "p" in 
 1.88344 +** the following schema:
 1.88345 +**
 1.88346 +**   CREATE TABLE p(pk PRIMARY KEY);
 1.88347 +**   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
 1.88348 +**
 1.88349 +** then the returned trigger structure is equivalent to:
 1.88350 +**
 1.88351 +**   CREATE TRIGGER ... DELETE ON p BEGIN
 1.88352 +**     DELETE FROM c WHERE ck = old.pk;
 1.88353 +**   END;
 1.88354 +**
 1.88355 +** The returned pointer is cached as part of the foreign key object. It
 1.88356 +** is eventually freed along with the rest of the foreign key object by 
 1.88357 +** sqlite3FkDelete().
 1.88358 +*/
 1.88359 +static Trigger *fkActionTrigger(
 1.88360 +  Parse *pParse,                  /* Parse context */
 1.88361 +  Table *pTab,                    /* Table being updated or deleted from */
 1.88362 +  FKey *pFKey,                    /* Foreign key to get action for */
 1.88363 +  ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
 1.88364 +){
 1.88365 +  sqlite3 *db = pParse->db;       /* Database handle */
 1.88366 +  int action;                     /* One of OE_None, OE_Cascade etc. */
 1.88367 +  Trigger *pTrigger;              /* Trigger definition to return */
 1.88368 +  int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
 1.88369 +
 1.88370 +  action = pFKey->aAction[iAction];
 1.88371 +  pTrigger = pFKey->apTrigger[iAction];
 1.88372 +
 1.88373 +  if( action!=OE_None && !pTrigger ){
 1.88374 +    u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
 1.88375 +    char const *zFrom;            /* Name of child table */
 1.88376 +    int nFrom;                    /* Length in bytes of zFrom */
 1.88377 +    Index *pIdx = 0;              /* Parent key index for this FK */
 1.88378 +    int *aiCol = 0;               /* child table cols -> parent key cols */
 1.88379 +    TriggerStep *pStep = 0;        /* First (only) step of trigger program */
 1.88380 +    Expr *pWhere = 0;             /* WHERE clause of trigger step */
 1.88381 +    ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
 1.88382 +    Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
 1.88383 +    int i;                        /* Iterator variable */
 1.88384 +    Expr *pWhen = 0;              /* WHEN clause for the trigger */
 1.88385 +
 1.88386 +    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
 1.88387 +    assert( aiCol || pFKey->nCol==1 );
 1.88388 +
 1.88389 +    for(i=0; i<pFKey->nCol; i++){
 1.88390 +      Token tOld = { "old", 3 };  /* Literal "old" token */
 1.88391 +      Token tNew = { "new", 3 };  /* Literal "new" token */
 1.88392 +      Token tFromCol;             /* Name of column in child table */
 1.88393 +      Token tToCol;               /* Name of column in parent table */
 1.88394 +      int iFromCol;               /* Idx of column in child table */
 1.88395 +      Expr *pEq;                  /* tFromCol = OLD.tToCol */
 1.88396 +
 1.88397 +      iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 1.88398 +      assert( iFromCol>=0 );
 1.88399 +      tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
 1.88400 +      tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
 1.88401 +
 1.88402 +      tToCol.n = sqlite3Strlen30(tToCol.z);
 1.88403 +      tFromCol.n = sqlite3Strlen30(tFromCol.z);
 1.88404 +
 1.88405 +      /* Create the expression "OLD.zToCol = zFromCol". It is important
 1.88406 +      ** that the "OLD.zToCol" term is on the LHS of the = operator, so
 1.88407 +      ** that the affinity and collation sequence associated with the
 1.88408 +      ** parent table are used for the comparison. */
 1.88409 +      pEq = sqlite3PExpr(pParse, TK_EQ,
 1.88410 +          sqlite3PExpr(pParse, TK_DOT, 
 1.88411 +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 1.88412 +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
 1.88413 +          , 0),
 1.88414 +          sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
 1.88415 +      , 0);
 1.88416 +      pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 1.88417 +
 1.88418 +      /* For ON UPDATE, construct the next term of the WHEN clause.
 1.88419 +      ** The final WHEN clause will be like this:
 1.88420 +      **
 1.88421 +      **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
 1.88422 +      */
 1.88423 +      if( pChanges ){
 1.88424 +        pEq = sqlite3PExpr(pParse, TK_IS,
 1.88425 +            sqlite3PExpr(pParse, TK_DOT, 
 1.88426 +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 1.88427 +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
 1.88428 +              0),
 1.88429 +            sqlite3PExpr(pParse, TK_DOT, 
 1.88430 +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 1.88431 +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
 1.88432 +              0),
 1.88433 +            0);
 1.88434 +        pWhen = sqlite3ExprAnd(db, pWhen, pEq);
 1.88435 +      }
 1.88436 +  
 1.88437 +      if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
 1.88438 +        Expr *pNew;
 1.88439 +        if( action==OE_Cascade ){
 1.88440 +          pNew = sqlite3PExpr(pParse, TK_DOT, 
 1.88441 +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 1.88442 +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
 1.88443 +          , 0);
 1.88444 +        }else if( action==OE_SetDflt ){
 1.88445 +          Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
 1.88446 +          if( pDflt ){
 1.88447 +            pNew = sqlite3ExprDup(db, pDflt, 0);
 1.88448 +          }else{
 1.88449 +            pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
 1.88450 +          }
 1.88451 +        }else{
 1.88452 +          pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
 1.88453 +        }
 1.88454 +        pList = sqlite3ExprListAppend(pParse, pList, pNew);
 1.88455 +        sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
 1.88456 +      }
 1.88457 +    }
 1.88458 +    sqlite3DbFree(db, aiCol);
 1.88459 +
 1.88460 +    zFrom = pFKey->pFrom->zName;
 1.88461 +    nFrom = sqlite3Strlen30(zFrom);
 1.88462 +
 1.88463 +    if( action==OE_Restrict ){
 1.88464 +      Token tFrom;
 1.88465 +      Expr *pRaise; 
 1.88466 +
 1.88467 +      tFrom.z = zFrom;
 1.88468 +      tFrom.n = nFrom;
 1.88469 +      pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
 1.88470 +      if( pRaise ){
 1.88471 +        pRaise->affinity = OE_Abort;
 1.88472 +      }
 1.88473 +      pSelect = sqlite3SelectNew(pParse, 
 1.88474 +          sqlite3ExprListAppend(pParse, 0, pRaise),
 1.88475 +          sqlite3SrcListAppend(db, 0, &tFrom, 0),
 1.88476 +          pWhere,
 1.88477 +          0, 0, 0, 0, 0, 0
 1.88478 +      );
 1.88479 +      pWhere = 0;
 1.88480 +    }
 1.88481 +
 1.88482 +    /* Disable lookaside memory allocation */
 1.88483 +    enableLookaside = db->lookaside.bEnabled;
 1.88484 +    db->lookaside.bEnabled = 0;
 1.88485 +
 1.88486 +    pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
 1.88487 +        sizeof(Trigger) +         /* struct Trigger */
 1.88488 +        sizeof(TriggerStep) +     /* Single step in trigger program */
 1.88489 +        nFrom + 1                 /* Space for pStep->target.z */
 1.88490 +    );
 1.88491 +    if( pTrigger ){
 1.88492 +      pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
 1.88493 +      pStep->target.z = (char *)&pStep[1];
 1.88494 +      pStep->target.n = nFrom;
 1.88495 +      memcpy((char *)pStep->target.z, zFrom, nFrom);
 1.88496 +  
 1.88497 +      pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 1.88498 +      pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
 1.88499 +      pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 1.88500 +      if( pWhen ){
 1.88501 +        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
 1.88502 +        pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
 1.88503 +      }
 1.88504 +    }
 1.88505 +
 1.88506 +    /* Re-enable the lookaside buffer, if it was disabled earlier. */
 1.88507 +    db->lookaside.bEnabled = enableLookaside;
 1.88508 +
 1.88509 +    sqlite3ExprDelete(db, pWhere);
 1.88510 +    sqlite3ExprDelete(db, pWhen);
 1.88511 +    sqlite3ExprListDelete(db, pList);
 1.88512 +    sqlite3SelectDelete(db, pSelect);
 1.88513 +    if( db->mallocFailed==1 ){
 1.88514 +      fkTriggerDelete(db, pTrigger);
 1.88515 +      return 0;
 1.88516 +    }
 1.88517 +    assert( pStep!=0 );
 1.88518 +
 1.88519 +    switch( action ){
 1.88520 +      case OE_Restrict:
 1.88521 +        pStep->op = TK_SELECT; 
 1.88522 +        break;
 1.88523 +      case OE_Cascade: 
 1.88524 +        if( !pChanges ){ 
 1.88525 +          pStep->op = TK_DELETE; 
 1.88526 +          break; 
 1.88527 +        }
 1.88528 +      default:
 1.88529 +        pStep->op = TK_UPDATE;
 1.88530 +    }
 1.88531 +    pStep->pTrig = pTrigger;
 1.88532 +    pTrigger->pSchema = pTab->pSchema;
 1.88533 +    pTrigger->pTabSchema = pTab->pSchema;
 1.88534 +    pFKey->apTrigger[iAction] = pTrigger;
 1.88535 +    pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
 1.88536 +  }
 1.88537 +
 1.88538 +  return pTrigger;
 1.88539 +}
 1.88540 +
 1.88541 +/*
 1.88542 +** This function is called when deleting or updating a row to implement
 1.88543 +** any required CASCADE, SET NULL or SET DEFAULT actions.
 1.88544 +*/
 1.88545 +SQLITE_PRIVATE void sqlite3FkActions(
 1.88546 +  Parse *pParse,                  /* Parse context */
 1.88547 +  Table *pTab,                    /* Table being updated or deleted from */
 1.88548 +  ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
 1.88549 +  int regOld                      /* Address of array containing old row */
 1.88550 +){
 1.88551 +  /* If foreign-key support is enabled, iterate through all FKs that 
 1.88552 +  ** refer to table pTab. If there is an action associated with the FK 
 1.88553 +  ** for this operation (either update or delete), invoke the associated 
 1.88554 +  ** trigger sub-program.  */
 1.88555 +  if( pParse->db->flags&SQLITE_ForeignKeys ){
 1.88556 +    FKey *pFKey;                  /* Iterator variable */
 1.88557 +    for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
 1.88558 +      Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
 1.88559 +      if( pAction ){
 1.88560 +        sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
 1.88561 +      }
 1.88562 +    }
 1.88563 +  }
 1.88564 +}
 1.88565 +
 1.88566 +#endif /* ifndef SQLITE_OMIT_TRIGGER */
 1.88567 +
 1.88568 +/*
 1.88569 +** Free all memory associated with foreign key definitions attached to
 1.88570 +** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
 1.88571 +** hash table.
 1.88572 +*/
 1.88573 +SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
 1.88574 +  FKey *pFKey;                    /* Iterator variable */
 1.88575 +  FKey *pNext;                    /* Copy of pFKey->pNextFrom */
 1.88576 +
 1.88577 +  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
 1.88578 +  for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
 1.88579 +
 1.88580 +    /* Remove the FK from the fkeyHash hash table. */
 1.88581 +    if( !db || db->pnBytesFreed==0 ){
 1.88582 +      if( pFKey->pPrevTo ){
 1.88583 +        pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
 1.88584 +      }else{
 1.88585 +        void *p = (void *)pFKey->pNextTo;
 1.88586 +        const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
 1.88587 +        sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
 1.88588 +      }
 1.88589 +      if( pFKey->pNextTo ){
 1.88590 +        pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
 1.88591 +      }
 1.88592 +    }
 1.88593 +
 1.88594 +    /* EV: R-30323-21917 Each foreign key constraint in SQLite is
 1.88595 +    ** classified as either immediate or deferred.
 1.88596 +    */
 1.88597 +    assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
 1.88598 +
 1.88599 +    /* Delete any triggers created to implement actions for this FK. */
 1.88600 +#ifndef SQLITE_OMIT_TRIGGER
 1.88601 +    fkTriggerDelete(db, pFKey->apTrigger[0]);
 1.88602 +    fkTriggerDelete(db, pFKey->apTrigger[1]);
 1.88603 +#endif
 1.88604 +
 1.88605 +    pNext = pFKey->pNextFrom;
 1.88606 +    sqlite3DbFree(db, pFKey);
 1.88607 +  }
 1.88608 +}
 1.88609 +#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
 1.88610 +
 1.88611 +/************** End of fkey.c ************************************************/
 1.88612 +/************** Begin file insert.c ******************************************/
 1.88613 +/*
 1.88614 +** 2001 September 15
 1.88615 +**
 1.88616 +** The author disclaims copyright to this source code.  In place of
 1.88617 +** a legal notice, here is a blessing:
 1.88618 +**
 1.88619 +**    May you do good and not evil.
 1.88620 +**    May you find forgiveness for yourself and forgive others.
 1.88621 +**    May you share freely, never taking more than you give.
 1.88622 +**
 1.88623 +*************************************************************************
 1.88624 +** This file contains C code routines that are called by the parser
 1.88625 +** to handle INSERT statements in SQLite.
 1.88626 +*/
 1.88627 +
 1.88628 +/*
 1.88629 +** Generate code that will open a table for reading.
 1.88630 +*/
 1.88631 +SQLITE_PRIVATE void sqlite3OpenTable(
 1.88632 +  Parse *p,       /* Generate code into this VDBE */
 1.88633 +  int iCur,       /* The cursor number of the table */
 1.88634 +  int iDb,        /* The database index in sqlite3.aDb[] */
 1.88635 +  Table *pTab,    /* The table to be opened */
 1.88636 +  int opcode      /* OP_OpenRead or OP_OpenWrite */
 1.88637 +){
 1.88638 +  Vdbe *v;
 1.88639 +  assert( !IsVirtual(pTab) );
 1.88640 +  v = sqlite3GetVdbe(p);
 1.88641 +  assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
 1.88642 +  sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
 1.88643 +  sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
 1.88644 +  sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
 1.88645 +  VdbeComment((v, "%s", pTab->zName));
 1.88646 +}
 1.88647 +
 1.88648 +/*
 1.88649 +** Return a pointer to the column affinity string associated with index
 1.88650 +** pIdx. A column affinity string has one character for each column in 
 1.88651 +** the table, according to the affinity of the column:
 1.88652 +**
 1.88653 +**  Character      Column affinity
 1.88654 +**  ------------------------------
 1.88655 +**  'a'            TEXT
 1.88656 +**  'b'            NONE
 1.88657 +**  'c'            NUMERIC
 1.88658 +**  'd'            INTEGER
 1.88659 +**  'e'            REAL
 1.88660 +**
 1.88661 +** An extra 'd' is appended to the end of the string to cover the
 1.88662 +** rowid that appears as the last column in every index.
 1.88663 +**
 1.88664 +** Memory for the buffer containing the column index affinity string
 1.88665 +** is managed along with the rest of the Index structure. It will be
 1.88666 +** released when sqlite3DeleteIndex() is called.
 1.88667 +*/
 1.88668 +SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
 1.88669 +  if( !pIdx->zColAff ){
 1.88670 +    /* The first time a column affinity string for a particular index is
 1.88671 +    ** required, it is allocated and populated here. It is then stored as
 1.88672 +    ** a member of the Index structure for subsequent use.
 1.88673 +    **
 1.88674 +    ** The column affinity string will eventually be deleted by
 1.88675 +    ** sqliteDeleteIndex() when the Index structure itself is cleaned
 1.88676 +    ** up.
 1.88677 +    */
 1.88678 +    int n;
 1.88679 +    Table *pTab = pIdx->pTable;
 1.88680 +    sqlite3 *db = sqlite3VdbeDb(v);
 1.88681 +    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
 1.88682 +    if( !pIdx->zColAff ){
 1.88683 +      db->mallocFailed = 1;
 1.88684 +      return 0;
 1.88685 +    }
 1.88686 +    for(n=0; n<pIdx->nColumn; n++){
 1.88687 +      pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
 1.88688 +    }
 1.88689 +    pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
 1.88690 +    pIdx->zColAff[n] = 0;
 1.88691 +  }
 1.88692 + 
 1.88693 +  return pIdx->zColAff;
 1.88694 +}
 1.88695 +
 1.88696 +/*
 1.88697 +** Set P4 of the most recently inserted opcode to a column affinity
 1.88698 +** string for table pTab. A column affinity string has one character
 1.88699 +** for each column indexed by the index, according to the affinity of the
 1.88700 +** column:
 1.88701 +**
 1.88702 +**  Character      Column affinity
 1.88703 +**  ------------------------------
 1.88704 +**  'a'            TEXT
 1.88705 +**  'b'            NONE
 1.88706 +**  'c'            NUMERIC
 1.88707 +**  'd'            INTEGER
 1.88708 +**  'e'            REAL
 1.88709 +*/
 1.88710 +SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
 1.88711 +  /* The first time a column affinity string for a particular table
 1.88712 +  ** is required, it is allocated and populated here. It is then 
 1.88713 +  ** stored as a member of the Table structure for subsequent use.
 1.88714 +  **
 1.88715 +  ** The column affinity string will eventually be deleted by
 1.88716 +  ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
 1.88717 +  */
 1.88718 +  if( !pTab->zColAff ){
 1.88719 +    char *zColAff;
 1.88720 +    int i;
 1.88721 +    sqlite3 *db = sqlite3VdbeDb(v);
 1.88722 +
 1.88723 +    zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
 1.88724 +    if( !zColAff ){
 1.88725 +      db->mallocFailed = 1;
 1.88726 +      return;
 1.88727 +    }
 1.88728 +
 1.88729 +    for(i=0; i<pTab->nCol; i++){
 1.88730 +      zColAff[i] = pTab->aCol[i].affinity;
 1.88731 +    }
 1.88732 +    zColAff[pTab->nCol] = '\0';
 1.88733 +
 1.88734 +    pTab->zColAff = zColAff;
 1.88735 +  }
 1.88736 +
 1.88737 +  sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
 1.88738 +}
 1.88739 +
 1.88740 +/*
 1.88741 +** Return non-zero if the table pTab in database iDb or any of its indices
 1.88742 +** have been opened at any point in the VDBE program beginning at location
 1.88743 +** iStartAddr throught the end of the program.  This is used to see if 
 1.88744 +** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
 1.88745 +** run without using temporary table for the results of the SELECT. 
 1.88746 +*/
 1.88747 +static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
 1.88748 +  Vdbe *v = sqlite3GetVdbe(p);
 1.88749 +  int i;
 1.88750 +  int iEnd = sqlite3VdbeCurrentAddr(v);
 1.88751 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.88752 +  VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
 1.88753 +#endif
 1.88754 +
 1.88755 +  for(i=iStartAddr; i<iEnd; i++){
 1.88756 +    VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
 1.88757 +    assert( pOp!=0 );
 1.88758 +    if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
 1.88759 +      Index *pIndex;
 1.88760 +      int tnum = pOp->p2;
 1.88761 +      if( tnum==pTab->tnum ){
 1.88762 +        return 1;
 1.88763 +      }
 1.88764 +      for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
 1.88765 +        if( tnum==pIndex->tnum ){
 1.88766 +          return 1;
 1.88767 +        }
 1.88768 +      }
 1.88769 +    }
 1.88770 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.88771 +    if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
 1.88772 +      assert( pOp->p4.pVtab!=0 );
 1.88773 +      assert( pOp->p4type==P4_VTAB );
 1.88774 +      return 1;
 1.88775 +    }
 1.88776 +#endif
 1.88777 +  }
 1.88778 +  return 0;
 1.88779 +}
 1.88780 +
 1.88781 +#ifndef SQLITE_OMIT_AUTOINCREMENT
 1.88782 +/*
 1.88783 +** Locate or create an AutoincInfo structure associated with table pTab
 1.88784 +** which is in database iDb.  Return the register number for the register
 1.88785 +** that holds the maximum rowid.
 1.88786 +**
 1.88787 +** There is at most one AutoincInfo structure per table even if the
 1.88788 +** same table is autoincremented multiple times due to inserts within
 1.88789 +** triggers.  A new AutoincInfo structure is created if this is the
 1.88790 +** first use of table pTab.  On 2nd and subsequent uses, the original
 1.88791 +** AutoincInfo structure is used.
 1.88792 +**
 1.88793 +** Three memory locations are allocated:
 1.88794 +**
 1.88795 +**   (1)  Register to hold the name of the pTab table.
 1.88796 +**   (2)  Register to hold the maximum ROWID of pTab.
 1.88797 +**   (3)  Register to hold the rowid in sqlite_sequence of pTab
 1.88798 +**
 1.88799 +** The 2nd register is the one that is returned.  That is all the
 1.88800 +** insert routine needs to know about.
 1.88801 +*/
 1.88802 +static int autoIncBegin(
 1.88803 +  Parse *pParse,      /* Parsing context */
 1.88804 +  int iDb,            /* Index of the database holding pTab */
 1.88805 +  Table *pTab         /* The table we are writing to */
 1.88806 +){
 1.88807 +  int memId = 0;      /* Register holding maximum rowid */
 1.88808 +  if( pTab->tabFlags & TF_Autoincrement ){
 1.88809 +    Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.88810 +    AutoincInfo *pInfo;
 1.88811 +
 1.88812 +    pInfo = pToplevel->pAinc;
 1.88813 +    while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
 1.88814 +    if( pInfo==0 ){
 1.88815 +      pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
 1.88816 +      if( pInfo==0 ) return 0;
 1.88817 +      pInfo->pNext = pToplevel->pAinc;
 1.88818 +      pToplevel->pAinc = pInfo;
 1.88819 +      pInfo->pTab = pTab;
 1.88820 +      pInfo->iDb = iDb;
 1.88821 +      pToplevel->nMem++;                  /* Register to hold name of table */
 1.88822 +      pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
 1.88823 +      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
 1.88824 +    }
 1.88825 +    memId = pInfo->regCtr;
 1.88826 +  }
 1.88827 +  return memId;
 1.88828 +}
 1.88829 +
 1.88830 +/*
 1.88831 +** This routine generates code that will initialize all of the
 1.88832 +** register used by the autoincrement tracker.  
 1.88833 +*/
 1.88834 +SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
 1.88835 +  AutoincInfo *p;            /* Information about an AUTOINCREMENT */
 1.88836 +  sqlite3 *db = pParse->db;  /* The database connection */
 1.88837 +  Db *pDb;                   /* Database only autoinc table */
 1.88838 +  int memId;                 /* Register holding max rowid */
 1.88839 +  int addr;                  /* A VDBE address */
 1.88840 +  Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
 1.88841 +
 1.88842 +  /* This routine is never called during trigger-generation.  It is
 1.88843 +  ** only called from the top-level */
 1.88844 +  assert( pParse->pTriggerTab==0 );
 1.88845 +  assert( pParse==sqlite3ParseToplevel(pParse) );
 1.88846 +
 1.88847 +  assert( v );   /* We failed long ago if this is not so */
 1.88848 +  for(p = pParse->pAinc; p; p = p->pNext){
 1.88849 +    pDb = &db->aDb[p->iDb];
 1.88850 +    memId = p->regCtr;
 1.88851 +    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 1.88852 +    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
 1.88853 +    sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
 1.88854 +    addr = sqlite3VdbeCurrentAddr(v);
 1.88855 +    sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
 1.88856 +    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
 1.88857 +    sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
 1.88858 +    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
 1.88859 +    sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 1.88860 +    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
 1.88861 +    sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
 1.88862 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
 1.88863 +    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
 1.88864 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
 1.88865 +    sqlite3VdbeAddOp0(v, OP_Close);
 1.88866 +  }
 1.88867 +}
 1.88868 +
 1.88869 +/*
 1.88870 +** Update the maximum rowid for an autoincrement calculation.
 1.88871 +**
 1.88872 +** This routine should be called when the top of the stack holds a
 1.88873 +** new rowid that is about to be inserted.  If that new rowid is
 1.88874 +** larger than the maximum rowid in the memId memory cell, then the
 1.88875 +** memory cell is updated.  The stack is unchanged.
 1.88876 +*/
 1.88877 +static void autoIncStep(Parse *pParse, int memId, int regRowid){
 1.88878 +  if( memId>0 ){
 1.88879 +    sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
 1.88880 +  }
 1.88881 +}
 1.88882 +
 1.88883 +/*
 1.88884 +** This routine generates the code needed to write autoincrement
 1.88885 +** maximum rowid values back into the sqlite_sequence register.
 1.88886 +** Every statement that might do an INSERT into an autoincrement
 1.88887 +** table (either directly or through triggers) needs to call this
 1.88888 +** routine just before the "exit" code.
 1.88889 +*/
 1.88890 +SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
 1.88891 +  AutoincInfo *p;
 1.88892 +  Vdbe *v = pParse->pVdbe;
 1.88893 +  sqlite3 *db = pParse->db;
 1.88894 +
 1.88895 +  assert( v );
 1.88896 +  for(p = pParse->pAinc; p; p = p->pNext){
 1.88897 +    Db *pDb = &db->aDb[p->iDb];
 1.88898 +    int j1, j2, j3, j4, j5;
 1.88899 +    int iRec;
 1.88900 +    int memId = p->regCtr;
 1.88901 +
 1.88902 +    iRec = sqlite3GetTempReg(pParse);
 1.88903 +    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 1.88904 +    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
 1.88905 +    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
 1.88906 +    j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
 1.88907 +    j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
 1.88908 +    j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
 1.88909 +    sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
 1.88910 +    sqlite3VdbeJumpHere(v, j2);
 1.88911 +    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
 1.88912 +    j5 = sqlite3VdbeAddOp0(v, OP_Goto);
 1.88913 +    sqlite3VdbeJumpHere(v, j4);
 1.88914 +    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
 1.88915 +    sqlite3VdbeJumpHere(v, j1);
 1.88916 +    sqlite3VdbeJumpHere(v, j5);
 1.88917 +    sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
 1.88918 +    sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
 1.88919 +    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.88920 +    sqlite3VdbeAddOp0(v, OP_Close);
 1.88921 +    sqlite3ReleaseTempReg(pParse, iRec);
 1.88922 +  }
 1.88923 +}
 1.88924 +#else
 1.88925 +/*
 1.88926 +** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
 1.88927 +** above are all no-ops
 1.88928 +*/
 1.88929 +# define autoIncBegin(A,B,C) (0)
 1.88930 +# define autoIncStep(A,B,C)
 1.88931 +#endif /* SQLITE_OMIT_AUTOINCREMENT */
 1.88932 +
 1.88933 +
 1.88934 +/*
 1.88935 +** Generate code for a co-routine that will evaluate a subquery one
 1.88936 +** row at a time.
 1.88937 +**
 1.88938 +** The pSelect parameter is the subquery that the co-routine will evaluation.
 1.88939 +** Information about the location of co-routine and the registers it will use
 1.88940 +** is returned by filling in the pDest object.
 1.88941 +**
 1.88942 +** Registers are allocated as follows:
 1.88943 +**
 1.88944 +**   pDest->iSDParm      The register holding the next entry-point of the
 1.88945 +**                       co-routine.  Run the co-routine to its next breakpoint
 1.88946 +**                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
 1.88947 +**
 1.88948 +**   pDest->iSDParm+1    The register holding the "completed" flag for the
 1.88949 +**                       co-routine. This register is 0 if the previous Yield
 1.88950 +**                       generated a new result row, or 1 if the subquery
 1.88951 +**                       has completed.  If the Yield is called again
 1.88952 +**                       after this register becomes 1, then the VDBE will
 1.88953 +**                       halt with an SQLITE_INTERNAL error.
 1.88954 +**
 1.88955 +**   pDest->iSdst        First result register.
 1.88956 +**
 1.88957 +**   pDest->nSdst        Number of result registers.
 1.88958 +**
 1.88959 +** This routine handles all of the register allocation and fills in the
 1.88960 +** pDest structure appropriately.
 1.88961 +**
 1.88962 +** Here is a schematic of the generated code assuming that X is the 
 1.88963 +** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
 1.88964 +** completed flag reg[pDest->iSDParm+1], and R and S are the range of
 1.88965 +** registers that hold the result set, reg[pDest->iSdst] through
 1.88966 +** reg[pDest->iSdst+pDest->nSdst-1]:
 1.88967 +**
 1.88968 +**         X <- A
 1.88969 +**         EOF <- 0
 1.88970 +**         goto B
 1.88971 +**      A: setup for the SELECT
 1.88972 +**         loop rows in the SELECT
 1.88973 +**           load results into registers R..S
 1.88974 +**           yield X
 1.88975 +**         end loop
 1.88976 +**         cleanup after the SELECT
 1.88977 +**         EOF <- 1
 1.88978 +**         yield X
 1.88979 +**         halt-error
 1.88980 +**      B:
 1.88981 +**
 1.88982 +** To use this subroutine, the caller generates code as follows:
 1.88983 +**
 1.88984 +**         [ Co-routine generated by this subroutine, shown above ]
 1.88985 +**      S: yield X
 1.88986 +**         if EOF goto E
 1.88987 +**         if skip this row, goto C
 1.88988 +**         if terminate loop, goto E
 1.88989 +**         deal with this row
 1.88990 +**      C: goto S
 1.88991 +**      E:
 1.88992 +*/
 1.88993 +SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
 1.88994 +  int regYield;       /* Register holding co-routine entry-point */
 1.88995 +  int regEof;         /* Register holding co-routine completion flag */
 1.88996 +  int addrTop;        /* Top of the co-routine */
 1.88997 +  int j1;             /* Jump instruction */
 1.88998 +  int rc;             /* Result code */
 1.88999 +  Vdbe *v;            /* VDBE under construction */
 1.89000 +
 1.89001 +  regYield = ++pParse->nMem;
 1.89002 +  regEof = ++pParse->nMem;
 1.89003 +  v = sqlite3GetVdbe(pParse);
 1.89004 +  addrTop = sqlite3VdbeCurrentAddr(v);
 1.89005 +  sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
 1.89006 +  VdbeComment((v, "Co-routine entry point"));
 1.89007 +  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
 1.89008 +  VdbeComment((v, "Co-routine completion flag"));
 1.89009 +  sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
 1.89010 +  j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 1.89011 +  rc = sqlite3Select(pParse, pSelect, pDest);
 1.89012 +  assert( pParse->nErr==0 || rc );
 1.89013 +  if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
 1.89014 +  if( rc ) return rc;
 1.89015 +  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
 1.89016 +  sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
 1.89017 +  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
 1.89018 +  VdbeComment((v, "End of coroutine"));
 1.89019 +  sqlite3VdbeJumpHere(v, j1);                             /* label B: */
 1.89020 +  return rc;
 1.89021 +}
 1.89022 +
 1.89023 +
 1.89024 +
 1.89025 +/* Forward declaration */
 1.89026 +static int xferOptimization(
 1.89027 +  Parse *pParse,        /* Parser context */
 1.89028 +  Table *pDest,         /* The table we are inserting into */
 1.89029 +  Select *pSelect,      /* A SELECT statement to use as the data source */
 1.89030 +  int onError,          /* How to handle constraint errors */
 1.89031 +  int iDbDest           /* The database of pDest */
 1.89032 +);
 1.89033 +
 1.89034 +/*
 1.89035 +** This routine is call to handle SQL of the following forms:
 1.89036 +**
 1.89037 +**    insert into TABLE (IDLIST) values(EXPRLIST)
 1.89038 +**    insert into TABLE (IDLIST) select
 1.89039 +**
 1.89040 +** The IDLIST following the table name is always optional.  If omitted,
 1.89041 +** then a list of all columns for the table is substituted.  The IDLIST
 1.89042 +** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
 1.89043 +**
 1.89044 +** The pList parameter holds EXPRLIST in the first form of the INSERT
 1.89045 +** statement above, and pSelect is NULL.  For the second form, pList is
 1.89046 +** NULL and pSelect is a pointer to the select statement used to generate
 1.89047 +** data for the insert.
 1.89048 +**
 1.89049 +** The code generated follows one of four templates.  For a simple
 1.89050 +** select with data coming from a VALUES clause, the code executes
 1.89051 +** once straight down through.  Pseudo-code follows (we call this
 1.89052 +** the "1st template"):
 1.89053 +**
 1.89054 +**         open write cursor to <table> and its indices
 1.89055 +**         puts VALUES clause expressions onto the stack
 1.89056 +**         write the resulting record into <table>
 1.89057 +**         cleanup
 1.89058 +**
 1.89059 +** The three remaining templates assume the statement is of the form
 1.89060 +**
 1.89061 +**   INSERT INTO <table> SELECT ...
 1.89062 +**
 1.89063 +** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
 1.89064 +** in other words if the SELECT pulls all columns from a single table
 1.89065 +** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
 1.89066 +** if <table2> and <table1> are distinct tables but have identical
 1.89067 +** schemas, including all the same indices, then a special optimization
 1.89068 +** is invoked that copies raw records from <table2> over to <table1>.
 1.89069 +** See the xferOptimization() function for the implementation of this
 1.89070 +** template.  This is the 2nd template.
 1.89071 +**
 1.89072 +**         open a write cursor to <table>
 1.89073 +**         open read cursor on <table2>
 1.89074 +**         transfer all records in <table2> over to <table>
 1.89075 +**         close cursors
 1.89076 +**         foreach index on <table>
 1.89077 +**           open a write cursor on the <table> index
 1.89078 +**           open a read cursor on the corresponding <table2> index
 1.89079 +**           transfer all records from the read to the write cursors
 1.89080 +**           close cursors
 1.89081 +**         end foreach
 1.89082 +**
 1.89083 +** The 3rd template is for when the second template does not apply
 1.89084 +** and the SELECT clause does not read from <table> at any time.
 1.89085 +** The generated code follows this template:
 1.89086 +**
 1.89087 +**         EOF <- 0
 1.89088 +**         X <- A
 1.89089 +**         goto B
 1.89090 +**      A: setup for the SELECT
 1.89091 +**         loop over the rows in the SELECT
 1.89092 +**           load values into registers R..R+n
 1.89093 +**           yield X
 1.89094 +**         end loop
 1.89095 +**         cleanup after the SELECT
 1.89096 +**         EOF <- 1
 1.89097 +**         yield X
 1.89098 +**         goto A
 1.89099 +**      B: open write cursor to <table> and its indices
 1.89100 +**      C: yield X
 1.89101 +**         if EOF goto D
 1.89102 +**         insert the select result into <table> from R..R+n
 1.89103 +**         goto C
 1.89104 +**      D: cleanup
 1.89105 +**
 1.89106 +** The 4th template is used if the insert statement takes its
 1.89107 +** values from a SELECT but the data is being inserted into a table
 1.89108 +** that is also read as part of the SELECT.  In the third form,
 1.89109 +** we have to use a intermediate table to store the results of
 1.89110 +** the select.  The template is like this:
 1.89111 +**
 1.89112 +**         EOF <- 0
 1.89113 +**         X <- A
 1.89114 +**         goto B
 1.89115 +**      A: setup for the SELECT
 1.89116 +**         loop over the tables in the SELECT
 1.89117 +**           load value into register R..R+n
 1.89118 +**           yield X
 1.89119 +**         end loop
 1.89120 +**         cleanup after the SELECT
 1.89121 +**         EOF <- 1
 1.89122 +**         yield X
 1.89123 +**         halt-error
 1.89124 +**      B: open temp table
 1.89125 +**      L: yield X
 1.89126 +**         if EOF goto M
 1.89127 +**         insert row from R..R+n into temp table
 1.89128 +**         goto L
 1.89129 +**      M: open write cursor to <table> and its indices
 1.89130 +**         rewind temp table
 1.89131 +**      C: loop over rows of intermediate table
 1.89132 +**           transfer values form intermediate table into <table>
 1.89133 +**         end loop
 1.89134 +**      D: cleanup
 1.89135 +*/
 1.89136 +SQLITE_PRIVATE void sqlite3Insert(
 1.89137 +  Parse *pParse,        /* Parser context */
 1.89138 +  SrcList *pTabList,    /* Name of table into which we are inserting */
 1.89139 +  ExprList *pList,      /* List of values to be inserted */
 1.89140 +  Select *pSelect,      /* A SELECT statement to use as the data source */
 1.89141 +  IdList *pColumn,      /* Column names corresponding to IDLIST. */
 1.89142 +  int onError           /* How to handle constraint errors */
 1.89143 +){
 1.89144 +  sqlite3 *db;          /* The main database structure */
 1.89145 +  Table *pTab;          /* The table to insert into.  aka TABLE */
 1.89146 +  char *zTab;           /* Name of the table into which we are inserting */
 1.89147 +  const char *zDb;      /* Name of the database holding this table */
 1.89148 +  int i, j, idx;        /* Loop counters */
 1.89149 +  Vdbe *v;              /* Generate code into this virtual machine */
 1.89150 +  Index *pIdx;          /* For looping over indices of the table */
 1.89151 +  int nColumn;          /* Number of columns in the data */
 1.89152 +  int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
 1.89153 +  int baseCur = 0;      /* VDBE Cursor number for pTab */
 1.89154 +  int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
 1.89155 +  int endOfLoop;        /* Label for the end of the insertion loop */
 1.89156 +  int useTempTable = 0; /* Store SELECT results in intermediate table */
 1.89157 +  int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
 1.89158 +  int addrInsTop = 0;   /* Jump to label "D" */
 1.89159 +  int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
 1.89160 +  int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
 1.89161 +  SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
 1.89162 +  int iDb;              /* Index of database holding TABLE */
 1.89163 +  Db *pDb;              /* The database containing table being inserted into */
 1.89164 +  int appendFlag = 0;   /* True if the insert is likely to be an append */
 1.89165 +
 1.89166 +  /* Register allocations */
 1.89167 +  int regFromSelect = 0;/* Base register for data coming from SELECT */
 1.89168 +  int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
 1.89169 +  int regRowCount = 0;  /* Memory cell used for the row counter */
 1.89170 +  int regIns;           /* Block of regs holding rowid+data being inserted */
 1.89171 +  int regRowid;         /* registers holding insert rowid */
 1.89172 +  int regData;          /* register holding first column to insert */
 1.89173 +  int regEof = 0;       /* Register recording end of SELECT data */
 1.89174 +  int *aRegIdx = 0;     /* One register allocated to each index */
 1.89175 +
 1.89176 +#ifndef SQLITE_OMIT_TRIGGER
 1.89177 +  int isView;                 /* True if attempting to insert into a view */
 1.89178 +  Trigger *pTrigger;          /* List of triggers on pTab, if required */
 1.89179 +  int tmask;                  /* Mask of trigger times */
 1.89180 +#endif
 1.89181 +
 1.89182 +  db = pParse->db;
 1.89183 +  memset(&dest, 0, sizeof(dest));
 1.89184 +  if( pParse->nErr || db->mallocFailed ){
 1.89185 +    goto insert_cleanup;
 1.89186 +  }
 1.89187 +
 1.89188 +  /* Locate the table into which we will be inserting new information.
 1.89189 +  */
 1.89190 +  assert( pTabList->nSrc==1 );
 1.89191 +  zTab = pTabList->a[0].zName;
 1.89192 +  if( NEVER(zTab==0) ) goto insert_cleanup;
 1.89193 +  pTab = sqlite3SrcListLookup(pParse, pTabList);
 1.89194 +  if( pTab==0 ){
 1.89195 +    goto insert_cleanup;
 1.89196 +  }
 1.89197 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.89198 +  assert( iDb<db->nDb );
 1.89199 +  pDb = &db->aDb[iDb];
 1.89200 +  zDb = pDb->zName;
 1.89201 +  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
 1.89202 +    goto insert_cleanup;
 1.89203 +  }
 1.89204 +
 1.89205 +  /* Figure out if we have any triggers and if the table being
 1.89206 +  ** inserted into is a view
 1.89207 +  */
 1.89208 +#ifndef SQLITE_OMIT_TRIGGER
 1.89209 +  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
 1.89210 +  isView = pTab->pSelect!=0;
 1.89211 +#else
 1.89212 +# define pTrigger 0
 1.89213 +# define tmask 0
 1.89214 +# define isView 0
 1.89215 +#endif
 1.89216 +#ifdef SQLITE_OMIT_VIEW
 1.89217 +# undef isView
 1.89218 +# define isView 0
 1.89219 +#endif
 1.89220 +  assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
 1.89221 +
 1.89222 +  /* If pTab is really a view, make sure it has been initialized.
 1.89223 +  ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 
 1.89224 +  ** module table).
 1.89225 +  */
 1.89226 +  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 1.89227 +    goto insert_cleanup;
 1.89228 +  }
 1.89229 +
 1.89230 +  /* Ensure that:
 1.89231 +  *  (a) the table is not read-only, 
 1.89232 +  *  (b) that if it is a view then ON INSERT triggers exist
 1.89233 +  */
 1.89234 +  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
 1.89235 +    goto insert_cleanup;
 1.89236 +  }
 1.89237 +
 1.89238 +  /* Allocate a VDBE
 1.89239 +  */
 1.89240 +  v = sqlite3GetVdbe(pParse);
 1.89241 +  if( v==0 ) goto insert_cleanup;
 1.89242 +  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 1.89243 +  sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
 1.89244 +
 1.89245 +#ifndef SQLITE_OMIT_XFER_OPT
 1.89246 +  /* If the statement is of the form
 1.89247 +  **
 1.89248 +  **       INSERT INTO <table1> SELECT * FROM <table2>;
 1.89249 +  **
 1.89250 +  ** Then special optimizations can be applied that make the transfer
 1.89251 +  ** very fast and which reduce fragmentation of indices.
 1.89252 +  **
 1.89253 +  ** This is the 2nd template.
 1.89254 +  */
 1.89255 +  if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
 1.89256 +    assert( !pTrigger );
 1.89257 +    assert( pList==0 );
 1.89258 +    goto insert_end;
 1.89259 +  }
 1.89260 +#endif /* SQLITE_OMIT_XFER_OPT */
 1.89261 +
 1.89262 +  /* If this is an AUTOINCREMENT table, look up the sequence number in the
 1.89263 +  ** sqlite_sequence table and store it in memory cell regAutoinc.
 1.89264 +  */
 1.89265 +  regAutoinc = autoIncBegin(pParse, iDb, pTab);
 1.89266 +
 1.89267 +  /* Figure out how many columns of data are supplied.  If the data
 1.89268 +  ** is coming from a SELECT statement, then generate a co-routine that
 1.89269 +  ** produces a single row of the SELECT on each invocation.  The
 1.89270 +  ** co-routine is the common header to the 3rd and 4th templates.
 1.89271 +  */
 1.89272 +  if( pSelect ){
 1.89273 +    /* Data is coming from a SELECT.  Generate a co-routine to run that
 1.89274 +    ** SELECT. */
 1.89275 +    int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
 1.89276 +    if( rc ) goto insert_cleanup;
 1.89277 +
 1.89278 +    regEof = dest.iSDParm + 1;
 1.89279 +    regFromSelect = dest.iSdst;
 1.89280 +    assert( pSelect->pEList );
 1.89281 +    nColumn = pSelect->pEList->nExpr;
 1.89282 +    assert( dest.nSdst==nColumn );
 1.89283 +
 1.89284 +    /* Set useTempTable to TRUE if the result of the SELECT statement
 1.89285 +    ** should be written into a temporary table (template 4).  Set to
 1.89286 +    ** FALSE if each* row of the SELECT can be written directly into
 1.89287 +    ** the destination table (template 3).
 1.89288 +    **
 1.89289 +    ** A temp table must be used if the table being updated is also one
 1.89290 +    ** of the tables being read by the SELECT statement.  Also use a 
 1.89291 +    ** temp table in the case of row triggers.
 1.89292 +    */
 1.89293 +    if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
 1.89294 +      useTempTable = 1;
 1.89295 +    }
 1.89296 +
 1.89297 +    if( useTempTable ){
 1.89298 +      /* Invoke the coroutine to extract information from the SELECT
 1.89299 +      ** and add it to a transient table srcTab.  The code generated
 1.89300 +      ** here is from the 4th template:
 1.89301 +      **
 1.89302 +      **      B: open temp table
 1.89303 +      **      L: yield X
 1.89304 +      **         if EOF goto M
 1.89305 +      **         insert row from R..R+n into temp table
 1.89306 +      **         goto L
 1.89307 +      **      M: ...
 1.89308 +      */
 1.89309 +      int regRec;          /* Register to hold packed record */
 1.89310 +      int regTempRowid;    /* Register to hold temp table ROWID */
 1.89311 +      int addrTop;         /* Label "L" */
 1.89312 +      int addrIf;          /* Address of jump to M */
 1.89313 +
 1.89314 +      srcTab = pParse->nTab++;
 1.89315 +      regRec = sqlite3GetTempReg(pParse);
 1.89316 +      regTempRowid = sqlite3GetTempReg(pParse);
 1.89317 +      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
 1.89318 +      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 1.89319 +      addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
 1.89320 +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
 1.89321 +      sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
 1.89322 +      sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
 1.89323 +      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
 1.89324 +      sqlite3VdbeJumpHere(v, addrIf);
 1.89325 +      sqlite3ReleaseTempReg(pParse, regRec);
 1.89326 +      sqlite3ReleaseTempReg(pParse, regTempRowid);
 1.89327 +    }
 1.89328 +  }else{
 1.89329 +    /* This is the case if the data for the INSERT is coming from a VALUES
 1.89330 +    ** clause
 1.89331 +    */
 1.89332 +    NameContext sNC;
 1.89333 +    memset(&sNC, 0, sizeof(sNC));
 1.89334 +    sNC.pParse = pParse;
 1.89335 +    srcTab = -1;
 1.89336 +    assert( useTempTable==0 );
 1.89337 +    nColumn = pList ? pList->nExpr : 0;
 1.89338 +    for(i=0; i<nColumn; i++){
 1.89339 +      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
 1.89340 +        goto insert_cleanup;
 1.89341 +      }
 1.89342 +    }
 1.89343 +  }
 1.89344 +
 1.89345 +  /* Make sure the number of columns in the source data matches the number
 1.89346 +  ** of columns to be inserted into the table.
 1.89347 +  */
 1.89348 +  if( IsVirtual(pTab) ){
 1.89349 +    for(i=0; i<pTab->nCol; i++){
 1.89350 +      nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
 1.89351 +    }
 1.89352 +  }
 1.89353 +  if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
 1.89354 +    sqlite3ErrorMsg(pParse, 
 1.89355 +       "table %S has %d columns but %d values were supplied",
 1.89356 +       pTabList, 0, pTab->nCol-nHidden, nColumn);
 1.89357 +    goto insert_cleanup;
 1.89358 +  }
 1.89359 +  if( pColumn!=0 && nColumn!=pColumn->nId ){
 1.89360 +    sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
 1.89361 +    goto insert_cleanup;
 1.89362 +  }
 1.89363 +
 1.89364 +  /* If the INSERT statement included an IDLIST term, then make sure
 1.89365 +  ** all elements of the IDLIST really are columns of the table and 
 1.89366 +  ** remember the column indices.
 1.89367 +  **
 1.89368 +  ** If the table has an INTEGER PRIMARY KEY column and that column
 1.89369 +  ** is named in the IDLIST, then record in the keyColumn variable
 1.89370 +  ** the index into IDLIST of the primary key column.  keyColumn is
 1.89371 +  ** the index of the primary key as it appears in IDLIST, not as
 1.89372 +  ** is appears in the original table.  (The index of the primary
 1.89373 +  ** key in the original table is pTab->iPKey.)
 1.89374 +  */
 1.89375 +  if( pColumn ){
 1.89376 +    for(i=0; i<pColumn->nId; i++){
 1.89377 +      pColumn->a[i].idx = -1;
 1.89378 +    }
 1.89379 +    for(i=0; i<pColumn->nId; i++){
 1.89380 +      for(j=0; j<pTab->nCol; j++){
 1.89381 +        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
 1.89382 +          pColumn->a[i].idx = j;
 1.89383 +          if( j==pTab->iPKey ){
 1.89384 +            keyColumn = i;
 1.89385 +          }
 1.89386 +          break;
 1.89387 +        }
 1.89388 +      }
 1.89389 +      if( j>=pTab->nCol ){
 1.89390 +        if( sqlite3IsRowid(pColumn->a[i].zName) ){
 1.89391 +          keyColumn = i;
 1.89392 +        }else{
 1.89393 +          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
 1.89394 +              pTabList, 0, pColumn->a[i].zName);
 1.89395 +          pParse->checkSchema = 1;
 1.89396 +          goto insert_cleanup;
 1.89397 +        }
 1.89398 +      }
 1.89399 +    }
 1.89400 +  }
 1.89401 +
 1.89402 +  /* If there is no IDLIST term but the table has an integer primary
 1.89403 +  ** key, the set the keyColumn variable to the primary key column index
 1.89404 +  ** in the original table definition.
 1.89405 +  */
 1.89406 +  if( pColumn==0 && nColumn>0 ){
 1.89407 +    keyColumn = pTab->iPKey;
 1.89408 +  }
 1.89409 +    
 1.89410 +  /* Initialize the count of rows to be inserted
 1.89411 +  */
 1.89412 +  if( db->flags & SQLITE_CountRows ){
 1.89413 +    regRowCount = ++pParse->nMem;
 1.89414 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
 1.89415 +  }
 1.89416 +
 1.89417 +  /* If this is not a view, open the table and and all indices */
 1.89418 +  if( !isView ){
 1.89419 +    int nIdx;
 1.89420 +
 1.89421 +    baseCur = pParse->nTab;
 1.89422 +    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
 1.89423 +    aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
 1.89424 +    if( aRegIdx==0 ){
 1.89425 +      goto insert_cleanup;
 1.89426 +    }
 1.89427 +    for(i=0; i<nIdx; i++){
 1.89428 +      aRegIdx[i] = ++pParse->nMem;
 1.89429 +    }
 1.89430 +  }
 1.89431 +
 1.89432 +  /* This is the top of the main insertion loop */
 1.89433 +  if( useTempTable ){
 1.89434 +    /* This block codes the top of loop only.  The complete loop is the
 1.89435 +    ** following pseudocode (template 4):
 1.89436 +    **
 1.89437 +    **         rewind temp table
 1.89438 +    **      C: loop over rows of intermediate table
 1.89439 +    **           transfer values form intermediate table into <table>
 1.89440 +    **         end loop
 1.89441 +    **      D: ...
 1.89442 +    */
 1.89443 +    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
 1.89444 +    addrCont = sqlite3VdbeCurrentAddr(v);
 1.89445 +  }else if( pSelect ){
 1.89446 +    /* This block codes the top of loop only.  The complete loop is the
 1.89447 +    ** following pseudocode (template 3):
 1.89448 +    **
 1.89449 +    **      C: yield X
 1.89450 +    **         if EOF goto D
 1.89451 +    **         insert the select result into <table> from R..R+n
 1.89452 +    **         goto C
 1.89453 +    **      D: ...
 1.89454 +    */
 1.89455 +    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 1.89456 +    addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
 1.89457 +  }
 1.89458 +
 1.89459 +  /* Allocate registers for holding the rowid of the new row,
 1.89460 +  ** the content of the new row, and the assemblied row record.
 1.89461 +  */
 1.89462 +  regRowid = regIns = pParse->nMem+1;
 1.89463 +  pParse->nMem += pTab->nCol + 1;
 1.89464 +  if( IsVirtual(pTab) ){
 1.89465 +    regRowid++;
 1.89466 +    pParse->nMem++;
 1.89467 +  }
 1.89468 +  regData = regRowid+1;
 1.89469 +
 1.89470 +  /* Run the BEFORE and INSTEAD OF triggers, if there are any
 1.89471 +  */
 1.89472 +  endOfLoop = sqlite3VdbeMakeLabel(v);
 1.89473 +  if( tmask & TRIGGER_BEFORE ){
 1.89474 +    int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
 1.89475 +
 1.89476 +    /* build the NEW.* reference row.  Note that if there is an INTEGER
 1.89477 +    ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
 1.89478 +    ** translated into a unique ID for the row.  But on a BEFORE trigger,
 1.89479 +    ** we do not know what the unique ID will be (because the insert has
 1.89480 +    ** not happened yet) so we substitute a rowid of -1
 1.89481 +    */
 1.89482 +    if( keyColumn<0 ){
 1.89483 +      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
 1.89484 +    }else{
 1.89485 +      int j1;
 1.89486 +      if( useTempTable ){
 1.89487 +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
 1.89488 +      }else{
 1.89489 +        assert( pSelect==0 );  /* Otherwise useTempTable is true */
 1.89490 +        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
 1.89491 +      }
 1.89492 +      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
 1.89493 +      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
 1.89494 +      sqlite3VdbeJumpHere(v, j1);
 1.89495 +      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
 1.89496 +    }
 1.89497 +
 1.89498 +    /* Cannot have triggers on a virtual table. If it were possible,
 1.89499 +    ** this block would have to account for hidden column.
 1.89500 +    */
 1.89501 +    assert( !IsVirtual(pTab) );
 1.89502 +
 1.89503 +    /* Create the new column data
 1.89504 +    */
 1.89505 +    for(i=0; i<pTab->nCol; i++){
 1.89506 +      if( pColumn==0 ){
 1.89507 +        j = i;
 1.89508 +      }else{
 1.89509 +        for(j=0; j<pColumn->nId; j++){
 1.89510 +          if( pColumn->a[j].idx==i ) break;
 1.89511 +        }
 1.89512 +      }
 1.89513 +      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
 1.89514 +        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
 1.89515 +      }else if( useTempTable ){
 1.89516 +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
 1.89517 +      }else{
 1.89518 +        assert( pSelect==0 ); /* Otherwise useTempTable is true */
 1.89519 +        sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
 1.89520 +      }
 1.89521 +    }
 1.89522 +
 1.89523 +    /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
 1.89524 +    ** do not attempt any conversions before assembling the record.
 1.89525 +    ** If this is a real table, attempt conversions as required by the
 1.89526 +    ** table column affinities.
 1.89527 +    */
 1.89528 +    if( !isView ){
 1.89529 +      sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
 1.89530 +      sqlite3TableAffinityStr(v, pTab);
 1.89531 +    }
 1.89532 +
 1.89533 +    /* Fire BEFORE or INSTEAD OF triggers */
 1.89534 +    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
 1.89535 +        pTab, regCols-pTab->nCol-1, onError, endOfLoop);
 1.89536 +
 1.89537 +    sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
 1.89538 +  }
 1.89539 +
 1.89540 +  /* Push the record number for the new entry onto the stack.  The
 1.89541 +  ** record number is a randomly generate integer created by NewRowid
 1.89542 +  ** except when the table has an INTEGER PRIMARY KEY column, in which
 1.89543 +  ** case the record number is the same as that column. 
 1.89544 +  */
 1.89545 +  if( !isView ){
 1.89546 +    if( IsVirtual(pTab) ){
 1.89547 +      /* The row that the VUpdate opcode will delete: none */
 1.89548 +      sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
 1.89549 +    }
 1.89550 +    if( keyColumn>=0 ){
 1.89551 +      if( useTempTable ){
 1.89552 +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
 1.89553 +      }else if( pSelect ){
 1.89554 +        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
 1.89555 +      }else{
 1.89556 +        VdbeOp *pOp;
 1.89557 +        sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
 1.89558 +        pOp = sqlite3VdbeGetOp(v, -1);
 1.89559 +        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
 1.89560 +          appendFlag = 1;
 1.89561 +          pOp->opcode = OP_NewRowid;
 1.89562 +          pOp->p1 = baseCur;
 1.89563 +          pOp->p2 = regRowid;
 1.89564 +          pOp->p3 = regAutoinc;
 1.89565 +        }
 1.89566 +      }
 1.89567 +      /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
 1.89568 +      ** to generate a unique primary key value.
 1.89569 +      */
 1.89570 +      if( !appendFlag ){
 1.89571 +        int j1;
 1.89572 +        if( !IsVirtual(pTab) ){
 1.89573 +          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
 1.89574 +          sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
 1.89575 +          sqlite3VdbeJumpHere(v, j1);
 1.89576 +        }else{
 1.89577 +          j1 = sqlite3VdbeCurrentAddr(v);
 1.89578 +          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
 1.89579 +        }
 1.89580 +        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
 1.89581 +      }
 1.89582 +    }else if( IsVirtual(pTab) ){
 1.89583 +      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
 1.89584 +    }else{
 1.89585 +      sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
 1.89586 +      appendFlag = 1;
 1.89587 +    }
 1.89588 +    autoIncStep(pParse, regAutoinc, regRowid);
 1.89589 +
 1.89590 +    /* Push onto the stack, data for all columns of the new entry, beginning
 1.89591 +    ** with the first column.
 1.89592 +    */
 1.89593 +    nHidden = 0;
 1.89594 +    for(i=0; i<pTab->nCol; i++){
 1.89595 +      int iRegStore = regRowid+1+i;
 1.89596 +      if( i==pTab->iPKey ){
 1.89597 +        /* The value of the INTEGER PRIMARY KEY column is always a NULL.
 1.89598 +        ** Whenever this column is read, the record number will be substituted
 1.89599 +        ** in its place.  So will fill this column with a NULL to avoid
 1.89600 +        ** taking up data space with information that will never be used. */
 1.89601 +        sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
 1.89602 +        continue;
 1.89603 +      }
 1.89604 +      if( pColumn==0 ){
 1.89605 +        if( IsHiddenColumn(&pTab->aCol[i]) ){
 1.89606 +          assert( IsVirtual(pTab) );
 1.89607 +          j = -1;
 1.89608 +          nHidden++;
 1.89609 +        }else{
 1.89610 +          j = i - nHidden;
 1.89611 +        }
 1.89612 +      }else{
 1.89613 +        for(j=0; j<pColumn->nId; j++){
 1.89614 +          if( pColumn->a[j].idx==i ) break;
 1.89615 +        }
 1.89616 +      }
 1.89617 +      if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
 1.89618 +        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
 1.89619 +      }else if( useTempTable ){
 1.89620 +        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
 1.89621 +      }else if( pSelect ){
 1.89622 +        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
 1.89623 +      }else{
 1.89624 +        sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
 1.89625 +      }
 1.89626 +    }
 1.89627 +
 1.89628 +    /* Generate code to check constraints and generate index keys and
 1.89629 +    ** do the insertion.
 1.89630 +    */
 1.89631 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.89632 +    if( IsVirtual(pTab) ){
 1.89633 +      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 1.89634 +      sqlite3VtabMakeWritable(pParse, pTab);
 1.89635 +      sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
 1.89636 +      sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
 1.89637 +      sqlite3MayAbort(pParse);
 1.89638 +    }else
 1.89639 +#endif
 1.89640 +    {
 1.89641 +      int isReplace;    /* Set to true if constraints may cause a replace */
 1.89642 +      sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
 1.89643 +          keyColumn>=0, 0, onError, endOfLoop, &isReplace
 1.89644 +      );
 1.89645 +      sqlite3FkCheck(pParse, pTab, 0, regIns);
 1.89646 +      sqlite3CompleteInsertion(
 1.89647 +          pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
 1.89648 +      );
 1.89649 +    }
 1.89650 +  }
 1.89651 +
 1.89652 +  /* Update the count of rows that are inserted
 1.89653 +  */
 1.89654 +  if( (db->flags & SQLITE_CountRows)!=0 ){
 1.89655 +    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
 1.89656 +  }
 1.89657 +
 1.89658 +  if( pTrigger ){
 1.89659 +    /* Code AFTER triggers */
 1.89660 +    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
 1.89661 +        pTab, regData-2-pTab->nCol, onError, endOfLoop);
 1.89662 +  }
 1.89663 +
 1.89664 +  /* The bottom of the main insertion loop, if the data source
 1.89665 +  ** is a SELECT statement.
 1.89666 +  */
 1.89667 +  sqlite3VdbeResolveLabel(v, endOfLoop);
 1.89668 +  if( useTempTable ){
 1.89669 +    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
 1.89670 +    sqlite3VdbeJumpHere(v, addrInsTop);
 1.89671 +    sqlite3VdbeAddOp1(v, OP_Close, srcTab);
 1.89672 +  }else if( pSelect ){
 1.89673 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
 1.89674 +    sqlite3VdbeJumpHere(v, addrInsTop);
 1.89675 +  }
 1.89676 +
 1.89677 +  if( !IsVirtual(pTab) && !isView ){
 1.89678 +    /* Close all tables opened */
 1.89679 +    sqlite3VdbeAddOp1(v, OP_Close, baseCur);
 1.89680 +    for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
 1.89681 +      sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
 1.89682 +    }
 1.89683 +  }
 1.89684 +
 1.89685 +insert_end:
 1.89686 +  /* Update the sqlite_sequence table by storing the content of the
 1.89687 +  ** maximum rowid counter values recorded while inserting into
 1.89688 +  ** autoincrement tables.
 1.89689 +  */
 1.89690 +  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 1.89691 +    sqlite3AutoincrementEnd(pParse);
 1.89692 +  }
 1.89693 +
 1.89694 +  /*
 1.89695 +  ** Return the number of rows inserted. If this routine is 
 1.89696 +  ** generating code because of a call to sqlite3NestedParse(), do not
 1.89697 +  ** invoke the callback function.
 1.89698 +  */
 1.89699 +  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
 1.89700 +    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
 1.89701 +    sqlite3VdbeSetNumCols(v, 1);
 1.89702 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
 1.89703 +  }
 1.89704 +
 1.89705 +insert_cleanup:
 1.89706 +  sqlite3SrcListDelete(db, pTabList);
 1.89707 +  sqlite3ExprListDelete(db, pList);
 1.89708 +  sqlite3SelectDelete(db, pSelect);
 1.89709 +  sqlite3IdListDelete(db, pColumn);
 1.89710 +  sqlite3DbFree(db, aRegIdx);
 1.89711 +}
 1.89712 +
 1.89713 +/* Make sure "isView" and other macros defined above are undefined. Otherwise
 1.89714 +** thely may interfere with compilation of other functions in this file
 1.89715 +** (or in another file, if this file becomes part of the amalgamation).  */
 1.89716 +#ifdef isView
 1.89717 + #undef isView
 1.89718 +#endif
 1.89719 +#ifdef pTrigger
 1.89720 + #undef pTrigger
 1.89721 +#endif
 1.89722 +#ifdef tmask
 1.89723 + #undef tmask
 1.89724 +#endif
 1.89725 +
 1.89726 +
 1.89727 +/*
 1.89728 +** Generate code to do constraint checks prior to an INSERT or an UPDATE.
 1.89729 +**
 1.89730 +** The input is a range of consecutive registers as follows:
 1.89731 +**
 1.89732 +**    1.  The rowid of the row after the update.
 1.89733 +**
 1.89734 +**    2.  The data in the first column of the entry after the update.
 1.89735 +**
 1.89736 +**    i.  Data from middle columns...
 1.89737 +**
 1.89738 +**    N.  The data in the last column of the entry after the update.
 1.89739 +**
 1.89740 +** The regRowid parameter is the index of the register containing (1).
 1.89741 +**
 1.89742 +** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
 1.89743 +** the address of a register containing the rowid before the update takes
 1.89744 +** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
 1.89745 +** is false, indicating an INSERT statement, then a non-zero rowidChng 
 1.89746 +** indicates that the rowid was explicitly specified as part of the
 1.89747 +** INSERT statement. If rowidChng is false, it means that  the rowid is
 1.89748 +** computed automatically in an insert or that the rowid value is not 
 1.89749 +** modified by an update.
 1.89750 +**
 1.89751 +** The code generated by this routine store new index entries into
 1.89752 +** registers identified by aRegIdx[].  No index entry is created for
 1.89753 +** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
 1.89754 +** the same as the order of indices on the linked list of indices
 1.89755 +** attached to the table.
 1.89756 +**
 1.89757 +** This routine also generates code to check constraints.  NOT NULL,
 1.89758 +** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
 1.89759 +** then the appropriate action is performed.  There are five possible
 1.89760 +** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
 1.89761 +**
 1.89762 +**  Constraint type  Action       What Happens
 1.89763 +**  ---------------  ----------   ----------------------------------------
 1.89764 +**  any              ROLLBACK     The current transaction is rolled back and
 1.89765 +**                                sqlite3_exec() returns immediately with a
 1.89766 +**                                return code of SQLITE_CONSTRAINT.
 1.89767 +**
 1.89768 +**  any              ABORT        Back out changes from the current command
 1.89769 +**                                only (do not do a complete rollback) then
 1.89770 +**                                cause sqlite3_exec() to return immediately
 1.89771 +**                                with SQLITE_CONSTRAINT.
 1.89772 +**
 1.89773 +**  any              FAIL         Sqlite3_exec() returns immediately with a
 1.89774 +**                                return code of SQLITE_CONSTRAINT.  The
 1.89775 +**                                transaction is not rolled back and any
 1.89776 +**                                prior changes are retained.
 1.89777 +**
 1.89778 +**  any              IGNORE       The record number and data is popped from
 1.89779 +**                                the stack and there is an immediate jump
 1.89780 +**                                to label ignoreDest.
 1.89781 +**
 1.89782 +**  NOT NULL         REPLACE      The NULL value is replace by the default
 1.89783 +**                                value for that column.  If the default value
 1.89784 +**                                is NULL, the action is the same as ABORT.
 1.89785 +**
 1.89786 +**  UNIQUE           REPLACE      The other row that conflicts with the row
 1.89787 +**                                being inserted is removed.
 1.89788 +**
 1.89789 +**  CHECK            REPLACE      Illegal.  The results in an exception.
 1.89790 +**
 1.89791 +** Which action to take is determined by the overrideError parameter.
 1.89792 +** Or if overrideError==OE_Default, then the pParse->onError parameter
 1.89793 +** is used.  Or if pParse->onError==OE_Default then the onError value
 1.89794 +** for the constraint is used.
 1.89795 +**
 1.89796 +** The calling routine must open a read/write cursor for pTab with
 1.89797 +** cursor number "baseCur".  All indices of pTab must also have open
 1.89798 +** read/write cursors with cursor number baseCur+i for the i-th cursor.
 1.89799 +** Except, if there is no possibility of a REPLACE action then
 1.89800 +** cursors do not need to be open for indices where aRegIdx[i]==0.
 1.89801 +*/
 1.89802 +SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
 1.89803 +  Parse *pParse,      /* The parser context */
 1.89804 +  Table *pTab,        /* the table into which we are inserting */
 1.89805 +  int baseCur,        /* Index of a read/write cursor pointing at pTab */
 1.89806 +  int regRowid,       /* Index of the range of input registers */
 1.89807 +  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
 1.89808 +  int rowidChng,      /* True if the rowid might collide with existing entry */
 1.89809 +  int isUpdate,       /* True for UPDATE, False for INSERT */
 1.89810 +  int overrideError,  /* Override onError to this if not OE_Default */
 1.89811 +  int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
 1.89812 +  int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
 1.89813 +){
 1.89814 +  int i;              /* loop counter */
 1.89815 +  Vdbe *v;            /* VDBE under constrution */
 1.89816 +  int nCol;           /* Number of columns */
 1.89817 +  int onError;        /* Conflict resolution strategy */
 1.89818 +  int j1;             /* Addresss of jump instruction */
 1.89819 +  int j2 = 0, j3;     /* Addresses of jump instructions */
 1.89820 +  int regData;        /* Register containing first data column */
 1.89821 +  int iCur;           /* Table cursor number */
 1.89822 +  Index *pIdx;         /* Pointer to one of the indices */
 1.89823 +  sqlite3 *db;         /* Database connection */
 1.89824 +  int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
 1.89825 +  int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
 1.89826 +
 1.89827 +  db = pParse->db;
 1.89828 +  v = sqlite3GetVdbe(pParse);
 1.89829 +  assert( v!=0 );
 1.89830 +  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 1.89831 +  nCol = pTab->nCol;
 1.89832 +  regData = regRowid + 1;
 1.89833 +
 1.89834 +  /* Test all NOT NULL constraints.
 1.89835 +  */
 1.89836 +  for(i=0; i<nCol; i++){
 1.89837 +    if( i==pTab->iPKey ){
 1.89838 +      continue;
 1.89839 +    }
 1.89840 +    onError = pTab->aCol[i].notNull;
 1.89841 +    if( onError==OE_None ) continue;
 1.89842 +    if( overrideError!=OE_Default ){
 1.89843 +      onError = overrideError;
 1.89844 +    }else if( onError==OE_Default ){
 1.89845 +      onError = OE_Abort;
 1.89846 +    }
 1.89847 +    if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
 1.89848 +      onError = OE_Abort;
 1.89849 +    }
 1.89850 +    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
 1.89851 +        || onError==OE_Ignore || onError==OE_Replace );
 1.89852 +    switch( onError ){
 1.89853 +      case OE_Abort:
 1.89854 +        sqlite3MayAbort(pParse);
 1.89855 +      case OE_Rollback:
 1.89856 +      case OE_Fail: {
 1.89857 +        char *zMsg;
 1.89858 +        sqlite3VdbeAddOp3(v, OP_HaltIfNull,
 1.89859 +                                  SQLITE_CONSTRAINT, onError, regData+i);
 1.89860 +        zMsg = sqlite3MPrintf(db, "%s.%s may not be NULL",
 1.89861 +                              pTab->zName, pTab->aCol[i].zName);
 1.89862 +        sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
 1.89863 +        break;
 1.89864 +      }
 1.89865 +      case OE_Ignore: {
 1.89866 +        sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
 1.89867 +        break;
 1.89868 +      }
 1.89869 +      default: {
 1.89870 +        assert( onError==OE_Replace );
 1.89871 +        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
 1.89872 +        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
 1.89873 +        sqlite3VdbeJumpHere(v, j1);
 1.89874 +        break;
 1.89875 +      }
 1.89876 +    }
 1.89877 +  }
 1.89878 +
 1.89879 +  /* Test all CHECK constraints
 1.89880 +  */
 1.89881 +#ifndef SQLITE_OMIT_CHECK
 1.89882 +  if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
 1.89883 +    ExprList *pCheck = pTab->pCheck;
 1.89884 +    pParse->ckBase = regData;
 1.89885 +    onError = overrideError!=OE_Default ? overrideError : OE_Abort;
 1.89886 +    for(i=0; i<pCheck->nExpr; i++){
 1.89887 +      int allOk = sqlite3VdbeMakeLabel(v);
 1.89888 +      sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
 1.89889 +      if( onError==OE_Ignore ){
 1.89890 +        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 1.89891 +      }else{
 1.89892 +        char *zConsName = pCheck->a[i].zName;
 1.89893 +        if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
 1.89894 +        if( zConsName ){
 1.89895 +          zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
 1.89896 +        }else{
 1.89897 +          zConsName = 0;
 1.89898 +        }
 1.89899 +        sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
 1.89900 +      }
 1.89901 +      sqlite3VdbeResolveLabel(v, allOk);
 1.89902 +    }
 1.89903 +  }
 1.89904 +#endif /* !defined(SQLITE_OMIT_CHECK) */
 1.89905 +
 1.89906 +  /* If we have an INTEGER PRIMARY KEY, make sure the primary key
 1.89907 +  ** of the new record does not previously exist.  Except, if this
 1.89908 +  ** is an UPDATE and the primary key is not changing, that is OK.
 1.89909 +  */
 1.89910 +  if( rowidChng ){
 1.89911 +    onError = pTab->keyConf;
 1.89912 +    if( overrideError!=OE_Default ){
 1.89913 +      onError = overrideError;
 1.89914 +    }else if( onError==OE_Default ){
 1.89915 +      onError = OE_Abort;
 1.89916 +    }
 1.89917 +    
 1.89918 +    if( isUpdate ){
 1.89919 +      j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
 1.89920 +    }
 1.89921 +    j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
 1.89922 +    switch( onError ){
 1.89923 +      default: {
 1.89924 +        onError = OE_Abort;
 1.89925 +        /* Fall thru into the next case */
 1.89926 +      }
 1.89927 +      case OE_Rollback:
 1.89928 +      case OE_Abort:
 1.89929 +      case OE_Fail: {
 1.89930 +        sqlite3HaltConstraint(
 1.89931 +          pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
 1.89932 +        break;
 1.89933 +      }
 1.89934 +      case OE_Replace: {
 1.89935 +        /* If there are DELETE triggers on this table and the
 1.89936 +        ** recursive-triggers flag is set, call GenerateRowDelete() to
 1.89937 +        ** remove the conflicting row from the table. This will fire
 1.89938 +        ** the triggers and remove both the table and index b-tree entries.
 1.89939 +        **
 1.89940 +        ** Otherwise, if there are no triggers or the recursive-triggers
 1.89941 +        ** flag is not set, but the table has one or more indexes, call 
 1.89942 +        ** GenerateRowIndexDelete(). This removes the index b-tree entries 
 1.89943 +        ** only. The table b-tree entry will be replaced by the new entry 
 1.89944 +        ** when it is inserted.  
 1.89945 +        **
 1.89946 +        ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
 1.89947 +        ** also invoke MultiWrite() to indicate that this VDBE may require
 1.89948 +        ** statement rollback (if the statement is aborted after the delete
 1.89949 +        ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
 1.89950 +        ** but being more selective here allows statements like:
 1.89951 +        **
 1.89952 +        **   REPLACE INTO t(rowid) VALUES($newrowid)
 1.89953 +        **
 1.89954 +        ** to run without a statement journal if there are no indexes on the
 1.89955 +        ** table.
 1.89956 +        */
 1.89957 +        Trigger *pTrigger = 0;
 1.89958 +        if( db->flags&SQLITE_RecTriggers ){
 1.89959 +          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 1.89960 +        }
 1.89961 +        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
 1.89962 +          sqlite3MultiWrite(pParse);
 1.89963 +          sqlite3GenerateRowDelete(
 1.89964 +              pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
 1.89965 +          );
 1.89966 +        }else if( pTab->pIndex ){
 1.89967 +          sqlite3MultiWrite(pParse);
 1.89968 +          sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
 1.89969 +        }
 1.89970 +        seenReplace = 1;
 1.89971 +        break;
 1.89972 +      }
 1.89973 +      case OE_Ignore: {
 1.89974 +        assert( seenReplace==0 );
 1.89975 +        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 1.89976 +        break;
 1.89977 +      }
 1.89978 +    }
 1.89979 +    sqlite3VdbeJumpHere(v, j3);
 1.89980 +    if( isUpdate ){
 1.89981 +      sqlite3VdbeJumpHere(v, j2);
 1.89982 +    }
 1.89983 +  }
 1.89984 +
 1.89985 +  /* Test all UNIQUE constraints by creating entries for each UNIQUE
 1.89986 +  ** index and making sure that duplicate entries do not already exist.
 1.89987 +  ** Add the new records to the indices as we go.
 1.89988 +  */
 1.89989 +  for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
 1.89990 +    int regIdx;
 1.89991 +    int regR;
 1.89992 +
 1.89993 +    if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
 1.89994 +
 1.89995 +    /* Create a key for accessing the index entry */
 1.89996 +    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
 1.89997 +    for(i=0; i<pIdx->nColumn; i++){
 1.89998 +      int idx = pIdx->aiColumn[i];
 1.89999 +      if( idx==pTab->iPKey ){
 1.90000 +        sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
 1.90001 +      }else{
 1.90002 +        sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
 1.90003 +      }
 1.90004 +    }
 1.90005 +    sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
 1.90006 +    sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
 1.90007 +    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
 1.90008 +    sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
 1.90009 +
 1.90010 +    /* Find out what action to take in case there is an indexing conflict */
 1.90011 +    onError = pIdx->onError;
 1.90012 +    if( onError==OE_None ){ 
 1.90013 +      sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
 1.90014 +      continue;  /* pIdx is not a UNIQUE index */
 1.90015 +    }
 1.90016 +    if( overrideError!=OE_Default ){
 1.90017 +      onError = overrideError;
 1.90018 +    }else if( onError==OE_Default ){
 1.90019 +      onError = OE_Abort;
 1.90020 +    }
 1.90021 +    if( seenReplace ){
 1.90022 +      if( onError==OE_Ignore ) onError = OE_Replace;
 1.90023 +      else if( onError==OE_Fail ) onError = OE_Abort;
 1.90024 +    }
 1.90025 +    
 1.90026 +    /* Check to see if the new index entry will be unique */
 1.90027 +    regR = sqlite3GetTempReg(pParse);
 1.90028 +    sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
 1.90029 +    j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
 1.90030 +                           regR, SQLITE_INT_TO_PTR(regIdx),
 1.90031 +                           P4_INT32);
 1.90032 +    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
 1.90033 +
 1.90034 +    /* Generate code that executes if the new index entry is not unique */
 1.90035 +    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
 1.90036 +        || onError==OE_Ignore || onError==OE_Replace );
 1.90037 +    switch( onError ){
 1.90038 +      case OE_Rollback:
 1.90039 +      case OE_Abort:
 1.90040 +      case OE_Fail: {
 1.90041 +        int j;
 1.90042 +        StrAccum errMsg;
 1.90043 +        const char *zSep;
 1.90044 +        char *zErr;
 1.90045 +
 1.90046 +        sqlite3StrAccumInit(&errMsg, 0, 0, 200);
 1.90047 +        errMsg.db = db;
 1.90048 +        zSep = pIdx->nColumn>1 ? "columns " : "column ";
 1.90049 +        for(j=0; j<pIdx->nColumn; j++){
 1.90050 +          char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
 1.90051 +          sqlite3StrAccumAppend(&errMsg, zSep, -1);
 1.90052 +          zSep = ", ";
 1.90053 +          sqlite3StrAccumAppend(&errMsg, zCol, -1);
 1.90054 +        }
 1.90055 +        sqlite3StrAccumAppend(&errMsg,
 1.90056 +            pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
 1.90057 +        zErr = sqlite3StrAccumFinish(&errMsg);
 1.90058 +        sqlite3HaltConstraint(pParse, onError, zErr, 0);
 1.90059 +        sqlite3DbFree(errMsg.db, zErr);
 1.90060 +        break;
 1.90061 +      }
 1.90062 +      case OE_Ignore: {
 1.90063 +        assert( seenReplace==0 );
 1.90064 +        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 1.90065 +        break;
 1.90066 +      }
 1.90067 +      default: {
 1.90068 +        Trigger *pTrigger = 0;
 1.90069 +        assert( onError==OE_Replace );
 1.90070 +        sqlite3MultiWrite(pParse);
 1.90071 +        if( db->flags&SQLITE_RecTriggers ){
 1.90072 +          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 1.90073 +        }
 1.90074 +        sqlite3GenerateRowDelete(
 1.90075 +            pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
 1.90076 +        );
 1.90077 +        seenReplace = 1;
 1.90078 +        break;
 1.90079 +      }
 1.90080 +    }
 1.90081 +    sqlite3VdbeJumpHere(v, j3);
 1.90082 +    sqlite3ReleaseTempReg(pParse, regR);
 1.90083 +  }
 1.90084 +  
 1.90085 +  if( pbMayReplace ){
 1.90086 +    *pbMayReplace = seenReplace;
 1.90087 +  }
 1.90088 +}
 1.90089 +
 1.90090 +/*
 1.90091 +** This routine generates code to finish the INSERT or UPDATE operation
 1.90092 +** that was started by a prior call to sqlite3GenerateConstraintChecks.
 1.90093 +** A consecutive range of registers starting at regRowid contains the
 1.90094 +** rowid and the content to be inserted.
 1.90095 +**
 1.90096 +** The arguments to this routine should be the same as the first six
 1.90097 +** arguments to sqlite3GenerateConstraintChecks.
 1.90098 +*/
 1.90099 +SQLITE_PRIVATE void sqlite3CompleteInsertion(
 1.90100 +  Parse *pParse,      /* The parser context */
 1.90101 +  Table *pTab,        /* the table into which we are inserting */
 1.90102 +  int baseCur,        /* Index of a read/write cursor pointing at pTab */
 1.90103 +  int regRowid,       /* Range of content */
 1.90104 +  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
 1.90105 +  int isUpdate,       /* True for UPDATE, False for INSERT */
 1.90106 +  int appendBias,     /* True if this is likely to be an append */
 1.90107 +  int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
 1.90108 +){
 1.90109 +  int i;
 1.90110 +  Vdbe *v;
 1.90111 +  int nIdx;
 1.90112 +  Index *pIdx;
 1.90113 +  u8 pik_flags;
 1.90114 +  int regData;
 1.90115 +  int regRec;
 1.90116 +
 1.90117 +  v = sqlite3GetVdbe(pParse);
 1.90118 +  assert( v!=0 );
 1.90119 +  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 1.90120 +  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
 1.90121 +  for(i=nIdx-1; i>=0; i--){
 1.90122 +    if( aRegIdx[i]==0 ) continue;
 1.90123 +    sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
 1.90124 +    if( useSeekResult ){
 1.90125 +      sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 1.90126 +    }
 1.90127 +  }
 1.90128 +  regData = regRowid + 1;
 1.90129 +  regRec = sqlite3GetTempReg(pParse);
 1.90130 +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
 1.90131 +  sqlite3TableAffinityStr(v, pTab);
 1.90132 +  sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
 1.90133 +  if( pParse->nested ){
 1.90134 +    pik_flags = 0;
 1.90135 +  }else{
 1.90136 +    pik_flags = OPFLAG_NCHANGE;
 1.90137 +    pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
 1.90138 +  }
 1.90139 +  if( appendBias ){
 1.90140 +    pik_flags |= OPFLAG_APPEND;
 1.90141 +  }
 1.90142 +  if( useSeekResult ){
 1.90143 +    pik_flags |= OPFLAG_USESEEKRESULT;
 1.90144 +  }
 1.90145 +  sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
 1.90146 +  if( !pParse->nested ){
 1.90147 +    sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 1.90148 +  }
 1.90149 +  sqlite3VdbeChangeP5(v, pik_flags);
 1.90150 +}
 1.90151 +
 1.90152 +/*
 1.90153 +** Generate code that will open cursors for a table and for all
 1.90154 +** indices of that table.  The "baseCur" parameter is the cursor number used
 1.90155 +** for the table.  Indices are opened on subsequent cursors.
 1.90156 +**
 1.90157 +** Return the number of indices on the table.
 1.90158 +*/
 1.90159 +SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
 1.90160 +  Parse *pParse,   /* Parsing context */
 1.90161 +  Table *pTab,     /* Table to be opened */
 1.90162 +  int baseCur,     /* Cursor number assigned to the table */
 1.90163 +  int op           /* OP_OpenRead or OP_OpenWrite */
 1.90164 +){
 1.90165 +  int i;
 1.90166 +  int iDb;
 1.90167 +  Index *pIdx;
 1.90168 +  Vdbe *v;
 1.90169 +
 1.90170 +  if( IsVirtual(pTab) ) return 0;
 1.90171 +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.90172 +  v = sqlite3GetVdbe(pParse);
 1.90173 +  assert( v!=0 );
 1.90174 +  sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
 1.90175 +  for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 1.90176 +    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 1.90177 +    assert( pIdx->pSchema==pTab->pSchema );
 1.90178 +    sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
 1.90179 +                      (char*)pKey, P4_KEYINFO_HANDOFF);
 1.90180 +    VdbeComment((v, "%s", pIdx->zName));
 1.90181 +  }
 1.90182 +  if( pParse->nTab<baseCur+i ){
 1.90183 +    pParse->nTab = baseCur+i;
 1.90184 +  }
 1.90185 +  return i-1;
 1.90186 +}
 1.90187 +
 1.90188 +
 1.90189 +#ifdef SQLITE_TEST
 1.90190 +/*
 1.90191 +** The following global variable is incremented whenever the
 1.90192 +** transfer optimization is used.  This is used for testing
 1.90193 +** purposes only - to make sure the transfer optimization really
 1.90194 +** is happening when it is suppose to.
 1.90195 +*/
 1.90196 +SQLITE_API int sqlite3_xferopt_count;
 1.90197 +#endif /* SQLITE_TEST */
 1.90198 +
 1.90199 +
 1.90200 +#ifndef SQLITE_OMIT_XFER_OPT
 1.90201 +/*
 1.90202 +** Check to collation names to see if they are compatible.
 1.90203 +*/
 1.90204 +static int xferCompatibleCollation(const char *z1, const char *z2){
 1.90205 +  if( z1==0 ){
 1.90206 +    return z2==0;
 1.90207 +  }
 1.90208 +  if( z2==0 ){
 1.90209 +    return 0;
 1.90210 +  }
 1.90211 +  return sqlite3StrICmp(z1, z2)==0;
 1.90212 +}
 1.90213 +
 1.90214 +
 1.90215 +/*
 1.90216 +** Check to see if index pSrc is compatible as a source of data
 1.90217 +** for index pDest in an insert transfer optimization.  The rules
 1.90218 +** for a compatible index:
 1.90219 +**
 1.90220 +**    *   The index is over the same set of columns
 1.90221 +**    *   The same DESC and ASC markings occurs on all columns
 1.90222 +**    *   The same onError processing (OE_Abort, OE_Ignore, etc)
 1.90223 +**    *   The same collating sequence on each column
 1.90224 +*/
 1.90225 +static int xferCompatibleIndex(Index *pDest, Index *pSrc){
 1.90226 +  int i;
 1.90227 +  assert( pDest && pSrc );
 1.90228 +  assert( pDest->pTable!=pSrc->pTable );
 1.90229 +  if( pDest->nColumn!=pSrc->nColumn ){
 1.90230 +    return 0;   /* Different number of columns */
 1.90231 +  }
 1.90232 +  if( pDest->onError!=pSrc->onError ){
 1.90233 +    return 0;   /* Different conflict resolution strategies */
 1.90234 +  }
 1.90235 +  for(i=0; i<pSrc->nColumn; i++){
 1.90236 +    if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
 1.90237 +      return 0;   /* Different columns indexed */
 1.90238 +    }
 1.90239 +    if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
 1.90240 +      return 0;   /* Different sort orders */
 1.90241 +    }
 1.90242 +    if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
 1.90243 +      return 0;   /* Different collating sequences */
 1.90244 +    }
 1.90245 +  }
 1.90246 +
 1.90247 +  /* If no test above fails then the indices must be compatible */
 1.90248 +  return 1;
 1.90249 +}
 1.90250 +
 1.90251 +/*
 1.90252 +** Attempt the transfer optimization on INSERTs of the form
 1.90253 +**
 1.90254 +**     INSERT INTO tab1 SELECT * FROM tab2;
 1.90255 +**
 1.90256 +** The xfer optimization transfers raw records from tab2 over to tab1.  
 1.90257 +** Columns are not decoded and reassemblied, which greatly improves
 1.90258 +** performance.  Raw index records are transferred in the same way.
 1.90259 +**
 1.90260 +** The xfer optimization is only attempted if tab1 and tab2 are compatible.
 1.90261 +** There are lots of rules for determining compatibility - see comments
 1.90262 +** embedded in the code for details.
 1.90263 +**
 1.90264 +** This routine returns TRUE if the optimization is guaranteed to be used.
 1.90265 +** Sometimes the xfer optimization will only work if the destination table
 1.90266 +** is empty - a factor that can only be determined at run-time.  In that
 1.90267 +** case, this routine generates code for the xfer optimization but also
 1.90268 +** does a test to see if the destination table is empty and jumps over the
 1.90269 +** xfer optimization code if the test fails.  In that case, this routine
 1.90270 +** returns FALSE so that the caller will know to go ahead and generate
 1.90271 +** an unoptimized transfer.  This routine also returns FALSE if there
 1.90272 +** is no chance that the xfer optimization can be applied.
 1.90273 +**
 1.90274 +** This optimization is particularly useful at making VACUUM run faster.
 1.90275 +*/
 1.90276 +static int xferOptimization(
 1.90277 +  Parse *pParse,        /* Parser context */
 1.90278 +  Table *pDest,         /* The table we are inserting into */
 1.90279 +  Select *pSelect,      /* A SELECT statement to use as the data source */
 1.90280 +  int onError,          /* How to handle constraint errors */
 1.90281 +  int iDbDest           /* The database of pDest */
 1.90282 +){
 1.90283 +  ExprList *pEList;                /* The result set of the SELECT */
 1.90284 +  Table *pSrc;                     /* The table in the FROM clause of SELECT */
 1.90285 +  Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
 1.90286 +  struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
 1.90287 +  int i;                           /* Loop counter */
 1.90288 +  int iDbSrc;                      /* The database of pSrc */
 1.90289 +  int iSrc, iDest;                 /* Cursors from source and destination */
 1.90290 +  int addr1, addr2;                /* Loop addresses */
 1.90291 +  int emptyDestTest;               /* Address of test for empty pDest */
 1.90292 +  int emptySrcTest;                /* Address of test for empty pSrc */
 1.90293 +  Vdbe *v;                         /* The VDBE we are building */
 1.90294 +  KeyInfo *pKey;                   /* Key information for an index */
 1.90295 +  int regAutoinc;                  /* Memory register used by AUTOINC */
 1.90296 +  int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
 1.90297 +  int regData, regRowid;           /* Registers holding data and rowid */
 1.90298 +
 1.90299 +  if( pSelect==0 ){
 1.90300 +    return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
 1.90301 +  }
 1.90302 +  if( sqlite3TriggerList(pParse, pDest) ){
 1.90303 +    return 0;   /* tab1 must not have triggers */
 1.90304 +  }
 1.90305 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.90306 +  if( pDest->tabFlags & TF_Virtual ){
 1.90307 +    return 0;   /* tab1 must not be a virtual table */
 1.90308 +  }
 1.90309 +#endif
 1.90310 +  if( onError==OE_Default ){
 1.90311 +    if( pDest->iPKey>=0 ) onError = pDest->keyConf;
 1.90312 +    if( onError==OE_Default ) onError = OE_Abort;
 1.90313 +  }
 1.90314 +  assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
 1.90315 +  if( pSelect->pSrc->nSrc!=1 ){
 1.90316 +    return 0;   /* FROM clause must have exactly one term */
 1.90317 +  }
 1.90318 +  if( pSelect->pSrc->a[0].pSelect ){
 1.90319 +    return 0;   /* FROM clause cannot contain a subquery */
 1.90320 +  }
 1.90321 +  if( pSelect->pWhere ){
 1.90322 +    return 0;   /* SELECT may not have a WHERE clause */
 1.90323 +  }
 1.90324 +  if( pSelect->pOrderBy ){
 1.90325 +    return 0;   /* SELECT may not have an ORDER BY clause */
 1.90326 +  }
 1.90327 +  /* Do not need to test for a HAVING clause.  If HAVING is present but
 1.90328 +  ** there is no ORDER BY, we will get an error. */
 1.90329 +  if( pSelect->pGroupBy ){
 1.90330 +    return 0;   /* SELECT may not have a GROUP BY clause */
 1.90331 +  }
 1.90332 +  if( pSelect->pLimit ){
 1.90333 +    return 0;   /* SELECT may not have a LIMIT clause */
 1.90334 +  }
 1.90335 +  assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
 1.90336 +  if( pSelect->pPrior ){
 1.90337 +    return 0;   /* SELECT may not be a compound query */
 1.90338 +  }
 1.90339 +  if( pSelect->selFlags & SF_Distinct ){
 1.90340 +    return 0;   /* SELECT may not be DISTINCT */
 1.90341 +  }
 1.90342 +  pEList = pSelect->pEList;
 1.90343 +  assert( pEList!=0 );
 1.90344 +  if( pEList->nExpr!=1 ){
 1.90345 +    return 0;   /* The result set must have exactly one column */
 1.90346 +  }
 1.90347 +  assert( pEList->a[0].pExpr );
 1.90348 +  if( pEList->a[0].pExpr->op!=TK_ALL ){
 1.90349 +    return 0;   /* The result set must be the special operator "*" */
 1.90350 +  }
 1.90351 +
 1.90352 +  /* At this point we have established that the statement is of the
 1.90353 +  ** correct syntactic form to participate in this optimization.  Now
 1.90354 +  ** we have to check the semantics.
 1.90355 +  */
 1.90356 +  pItem = pSelect->pSrc->a;
 1.90357 +  pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
 1.90358 +  if( pSrc==0 ){
 1.90359 +    return 0;   /* FROM clause does not contain a real table */
 1.90360 +  }
 1.90361 +  if( pSrc==pDest ){
 1.90362 +    return 0;   /* tab1 and tab2 may not be the same table */
 1.90363 +  }
 1.90364 +#ifndef SQLITE_OMIT_VIRTUALTABLE
 1.90365 +  if( pSrc->tabFlags & TF_Virtual ){
 1.90366 +    return 0;   /* tab2 must not be a virtual table */
 1.90367 +  }
 1.90368 +#endif
 1.90369 +  if( pSrc->pSelect ){
 1.90370 +    return 0;   /* tab2 may not be a view */
 1.90371 +  }
 1.90372 +  if( pDest->nCol!=pSrc->nCol ){
 1.90373 +    return 0;   /* Number of columns must be the same in tab1 and tab2 */
 1.90374 +  }
 1.90375 +  if( pDest->iPKey!=pSrc->iPKey ){
 1.90376 +    return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
 1.90377 +  }
 1.90378 +  for(i=0; i<pDest->nCol; i++){
 1.90379 +    if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
 1.90380 +      return 0;    /* Affinity must be the same on all columns */
 1.90381 +    }
 1.90382 +    if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
 1.90383 +      return 0;    /* Collating sequence must be the same on all columns */
 1.90384 +    }
 1.90385 +    if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
 1.90386 +      return 0;    /* tab2 must be NOT NULL if tab1 is */
 1.90387 +    }
 1.90388 +  }
 1.90389 +  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
 1.90390 +    if( pDestIdx->onError!=OE_None ){
 1.90391 +      destHasUniqueIdx = 1;
 1.90392 +    }
 1.90393 +    for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
 1.90394 +      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
 1.90395 +    }
 1.90396 +    if( pSrcIdx==0 ){
 1.90397 +      return 0;    /* pDestIdx has no corresponding index in pSrc */
 1.90398 +    }
 1.90399 +  }
 1.90400 +#ifndef SQLITE_OMIT_CHECK
 1.90401 +  if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck, pDest->pCheck) ){
 1.90402 +    return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
 1.90403 +  }
 1.90404 +#endif
 1.90405 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.90406 +  /* Disallow the transfer optimization if the destination table constains
 1.90407 +  ** any foreign key constraints.  This is more restrictive than necessary.
 1.90408 +  ** But the main beneficiary of the transfer optimization is the VACUUM 
 1.90409 +  ** command, and the VACUUM command disables foreign key constraints.  So
 1.90410 +  ** the extra complication to make this rule less restrictive is probably
 1.90411 +  ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
 1.90412 +  */
 1.90413 +  if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
 1.90414 +    return 0;
 1.90415 +  }
 1.90416 +#endif
 1.90417 +  if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
 1.90418 +    return 0;  /* xfer opt does not play well with PRAGMA count_changes */
 1.90419 +  }
 1.90420 +
 1.90421 +  /* If we get this far, it means that the xfer optimization is at
 1.90422 +  ** least a possibility, though it might only work if the destination
 1.90423 +  ** table (tab1) is initially empty.
 1.90424 +  */
 1.90425 +#ifdef SQLITE_TEST
 1.90426 +  sqlite3_xferopt_count++;
 1.90427 +#endif
 1.90428 +  iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
 1.90429 +  v = sqlite3GetVdbe(pParse);
 1.90430 +  sqlite3CodeVerifySchema(pParse, iDbSrc);
 1.90431 +  iSrc = pParse->nTab++;
 1.90432 +  iDest = pParse->nTab++;
 1.90433 +  regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
 1.90434 +  sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
 1.90435 +  if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
 1.90436 +   || destHasUniqueIdx                              /* (2) */
 1.90437 +   || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
 1.90438 +  ){
 1.90439 +    /* In some circumstances, we are able to run the xfer optimization
 1.90440 +    ** only if the destination table is initially empty.  This code makes
 1.90441 +    ** that determination.  Conditions under which the destination must
 1.90442 +    ** be empty:
 1.90443 +    **
 1.90444 +    ** (1) There is no INTEGER PRIMARY KEY but there are indices.
 1.90445 +    **     (If the destination is not initially empty, the rowid fields
 1.90446 +    **     of index entries might need to change.)
 1.90447 +    **
 1.90448 +    ** (2) The destination has a unique index.  (The xfer optimization 
 1.90449 +    **     is unable to test uniqueness.)
 1.90450 +    **
 1.90451 +    ** (3) onError is something other than OE_Abort and OE_Rollback.
 1.90452 +    */
 1.90453 +    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
 1.90454 +    emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 1.90455 +    sqlite3VdbeJumpHere(v, addr1);
 1.90456 +  }else{
 1.90457 +    emptyDestTest = 0;
 1.90458 +  }
 1.90459 +  sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
 1.90460 +  emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
 1.90461 +  regData = sqlite3GetTempReg(pParse);
 1.90462 +  regRowid = sqlite3GetTempReg(pParse);
 1.90463 +  if( pDest->iPKey>=0 ){
 1.90464 +    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 1.90465 +    addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
 1.90466 +    sqlite3HaltConstraint(
 1.90467 +        pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
 1.90468 +    sqlite3VdbeJumpHere(v, addr2);
 1.90469 +    autoIncStep(pParse, regAutoinc, regRowid);
 1.90470 +  }else if( pDest->pIndex==0 ){
 1.90471 +    addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
 1.90472 +  }else{
 1.90473 +    addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 1.90474 +    assert( (pDest->tabFlags & TF_Autoincrement)==0 );
 1.90475 +  }
 1.90476 +  sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
 1.90477 +  sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
 1.90478 +  sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
 1.90479 +  sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
 1.90480 +  sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
 1.90481 +  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
 1.90482 +    for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
 1.90483 +      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
 1.90484 +    }
 1.90485 +    assert( pSrcIdx );
 1.90486 +    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 1.90487 +    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 1.90488 +    pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
 1.90489 +    sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
 1.90490 +                      (char*)pKey, P4_KEYINFO_HANDOFF);
 1.90491 +    VdbeComment((v, "%s", pSrcIdx->zName));
 1.90492 +    pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
 1.90493 +    sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
 1.90494 +                      (char*)pKey, P4_KEYINFO_HANDOFF);
 1.90495 +    VdbeComment((v, "%s", pDestIdx->zName));
 1.90496 +    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
 1.90497 +    sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
 1.90498 +    sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
 1.90499 +    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
 1.90500 +    sqlite3VdbeJumpHere(v, addr1);
 1.90501 +  }
 1.90502 +  sqlite3VdbeJumpHere(v, emptySrcTest);
 1.90503 +  sqlite3ReleaseTempReg(pParse, regRowid);
 1.90504 +  sqlite3ReleaseTempReg(pParse, regData);
 1.90505 +  sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 1.90506 +  sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 1.90507 +  if( emptyDestTest ){
 1.90508 +    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
 1.90509 +    sqlite3VdbeJumpHere(v, emptyDestTest);
 1.90510 +    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 1.90511 +    return 0;
 1.90512 +  }else{
 1.90513 +    return 1;
 1.90514 +  }
 1.90515 +}
 1.90516 +#endif /* SQLITE_OMIT_XFER_OPT */
 1.90517 +
 1.90518 +/************** End of insert.c **********************************************/
 1.90519 +/************** Begin file legacy.c ******************************************/
 1.90520 +/*
 1.90521 +** 2001 September 15
 1.90522 +**
 1.90523 +** The author disclaims copyright to this source code.  In place of
 1.90524 +** a legal notice, here is a blessing:
 1.90525 +**
 1.90526 +**    May you do good and not evil.
 1.90527 +**    May you find forgiveness for yourself and forgive others.
 1.90528 +**    May you share freely, never taking more than you give.
 1.90529 +**
 1.90530 +*************************************************************************
 1.90531 +** Main file for the SQLite library.  The routines in this file
 1.90532 +** implement the programmer interface to the library.  Routines in
 1.90533 +** other files are for internal use by SQLite and should not be
 1.90534 +** accessed by users of the library.
 1.90535 +*/
 1.90536 +
 1.90537 +
 1.90538 +/*
 1.90539 +** Execute SQL code.  Return one of the SQLITE_ success/failure
 1.90540 +** codes.  Also write an error message into memory obtained from
 1.90541 +** malloc() and make *pzErrMsg point to that message.
 1.90542 +**
 1.90543 +** If the SQL is a query, then for each row in the query result
 1.90544 +** the xCallback() function is called.  pArg becomes the first
 1.90545 +** argument to xCallback().  If xCallback=NULL then no callback
 1.90546 +** is invoked, even for queries.
 1.90547 +*/
 1.90548 +SQLITE_API int sqlite3_exec(
 1.90549 +  sqlite3 *db,                /* The database on which the SQL executes */
 1.90550 +  const char *zSql,           /* The SQL to be executed */
 1.90551 +  sqlite3_callback xCallback, /* Invoke this callback routine */
 1.90552 +  void *pArg,                 /* First argument to xCallback() */
 1.90553 +  char **pzErrMsg             /* Write error messages here */
 1.90554 +){
 1.90555 +  int rc = SQLITE_OK;         /* Return code */
 1.90556 +  const char *zLeftover;      /* Tail of unprocessed SQL */
 1.90557 +  sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
 1.90558 +  char **azCols = 0;          /* Names of result columns */
 1.90559 +  int nRetry = 0;             /* Number of retry attempts */
 1.90560 +  int callbackIsInit;         /* True if callback data is initialized */
 1.90561 +
 1.90562 +  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
 1.90563 +  if( zSql==0 ) zSql = "";
 1.90564 +
 1.90565 +  sqlite3_mutex_enter(db->mutex);
 1.90566 +  sqlite3Error(db, SQLITE_OK, 0);
 1.90567 +  while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
 1.90568 +    int nCol;
 1.90569 +    char **azVals = 0;
 1.90570 +
 1.90571 +    pStmt = 0;
 1.90572 +    rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
 1.90573 +    assert( rc==SQLITE_OK || pStmt==0 );
 1.90574 +    if( rc!=SQLITE_OK ){
 1.90575 +      continue;
 1.90576 +    }
 1.90577 +    if( !pStmt ){
 1.90578 +      /* this happens for a comment or white-space */
 1.90579 +      zSql = zLeftover;
 1.90580 +      continue;
 1.90581 +    }
 1.90582 +
 1.90583 +    callbackIsInit = 0;
 1.90584 +    nCol = sqlite3_column_count(pStmt);
 1.90585 +
 1.90586 +    while( 1 ){
 1.90587 +      int i;
 1.90588 +      rc = sqlite3_step(pStmt);
 1.90589 +
 1.90590 +      /* Invoke the callback function if required */
 1.90591 +      if( xCallback && (SQLITE_ROW==rc || 
 1.90592 +          (SQLITE_DONE==rc && !callbackIsInit
 1.90593 +                           && db->flags&SQLITE_NullCallback)) ){
 1.90594 +        if( !callbackIsInit ){
 1.90595 +          azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
 1.90596 +          if( azCols==0 ){
 1.90597 +            goto exec_out;
 1.90598 +          }
 1.90599 +          for(i=0; i<nCol; i++){
 1.90600 +            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
 1.90601 +            /* sqlite3VdbeSetColName() installs column names as UTF8
 1.90602 +            ** strings so there is no way for sqlite3_column_name() to fail. */
 1.90603 +            assert( azCols[i]!=0 );
 1.90604 +          }
 1.90605 +          callbackIsInit = 1;
 1.90606 +        }
 1.90607 +        if( rc==SQLITE_ROW ){
 1.90608 +          azVals = &azCols[nCol];
 1.90609 +          for(i=0; i<nCol; i++){
 1.90610 +            azVals[i] = (char *)sqlite3_column_text(pStmt, i);
 1.90611 +            if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
 1.90612 +              db->mallocFailed = 1;
 1.90613 +              goto exec_out;
 1.90614 +            }
 1.90615 +          }
 1.90616 +        }
 1.90617 +        if( xCallback(pArg, nCol, azVals, azCols) ){
 1.90618 +          rc = SQLITE_ABORT;
 1.90619 +          sqlite3VdbeFinalize((Vdbe *)pStmt);
 1.90620 +          pStmt = 0;
 1.90621 +          sqlite3Error(db, SQLITE_ABORT, 0);
 1.90622 +          goto exec_out;
 1.90623 +        }
 1.90624 +      }
 1.90625 +
 1.90626 +      if( rc!=SQLITE_ROW ){
 1.90627 +        rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
 1.90628 +        pStmt = 0;
 1.90629 +        if( rc!=SQLITE_SCHEMA ){
 1.90630 +          nRetry = 0;
 1.90631 +          zSql = zLeftover;
 1.90632 +          while( sqlite3Isspace(zSql[0]) ) zSql++;
 1.90633 +        }
 1.90634 +        break;
 1.90635 +      }
 1.90636 +    }
 1.90637 +
 1.90638 +    sqlite3DbFree(db, azCols);
 1.90639 +    azCols = 0;
 1.90640 +  }
 1.90641 +
 1.90642 +exec_out:
 1.90643 +  if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
 1.90644 +  sqlite3DbFree(db, azCols);
 1.90645 +
 1.90646 +  rc = sqlite3ApiExit(db, rc);
 1.90647 +  if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
 1.90648 +    int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
 1.90649 +    *pzErrMsg = sqlite3Malloc(nErrMsg);
 1.90650 +    if( *pzErrMsg ){
 1.90651 +      memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
 1.90652 +    }else{
 1.90653 +      rc = SQLITE_NOMEM;
 1.90654 +      sqlite3Error(db, SQLITE_NOMEM, 0);
 1.90655 +    }
 1.90656 +  }else if( pzErrMsg ){
 1.90657 +    *pzErrMsg = 0;
 1.90658 +  }
 1.90659 +
 1.90660 +  assert( (rc&db->errMask)==rc );
 1.90661 +  sqlite3_mutex_leave(db->mutex);
 1.90662 +  return rc;
 1.90663 +}
 1.90664 +
 1.90665 +/************** End of legacy.c **********************************************/
 1.90666 +/************** Begin file loadext.c *****************************************/
 1.90667 +/*
 1.90668 +** 2006 June 7
 1.90669 +**
 1.90670 +** The author disclaims copyright to this source code.  In place of
 1.90671 +** a legal notice, here is a blessing:
 1.90672 +**
 1.90673 +**    May you do good and not evil.
 1.90674 +**    May you find forgiveness for yourself and forgive others.
 1.90675 +**    May you share freely, never taking more than you give.
 1.90676 +**
 1.90677 +*************************************************************************
 1.90678 +** This file contains code used to dynamically load extensions into
 1.90679 +** the SQLite library.
 1.90680 +*/
 1.90681 +
 1.90682 +#ifndef SQLITE_CORE
 1.90683 +  #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
 1.90684 +#endif
 1.90685 +/************** Include sqlite3ext.h in the middle of loadext.c **************/
 1.90686 +/************** Begin file sqlite3ext.h **************************************/
 1.90687 +/*
 1.90688 +** 2006 June 7
 1.90689 +**
 1.90690 +** The author disclaims copyright to this source code.  In place of
 1.90691 +** a legal notice, here is a blessing:
 1.90692 +**
 1.90693 +**    May you do good and not evil.
 1.90694 +**    May you find forgiveness for yourself and forgive others.
 1.90695 +**    May you share freely, never taking more than you give.
 1.90696 +**
 1.90697 +*************************************************************************
 1.90698 +** This header file defines the SQLite interface for use by
 1.90699 +** shared libraries that want to be imported as extensions into
 1.90700 +** an SQLite instance.  Shared libraries that intend to be loaded
 1.90701 +** as extensions by SQLite should #include this file instead of 
 1.90702 +** sqlite3.h.
 1.90703 +*/
 1.90704 +#ifndef _SQLITE3EXT_H_
 1.90705 +#define _SQLITE3EXT_H_
 1.90706 +
 1.90707 +typedef struct sqlite3_api_routines sqlite3_api_routines;
 1.90708 +
 1.90709 +/*
 1.90710 +** The following structure holds pointers to all of the SQLite API
 1.90711 +** routines.
 1.90712 +**
 1.90713 +** WARNING:  In order to maintain backwards compatibility, add new
 1.90714 +** interfaces to the end of this structure only.  If you insert new
 1.90715 +** interfaces in the middle of this structure, then older different
 1.90716 +** versions of SQLite will not be able to load each others' shared
 1.90717 +** libraries!
 1.90718 +*/
 1.90719 +struct sqlite3_api_routines {
 1.90720 +  void * (*aggregate_context)(sqlite3_context*,int nBytes);
 1.90721 +  int  (*aggregate_count)(sqlite3_context*);
 1.90722 +  int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
 1.90723 +  int  (*bind_double)(sqlite3_stmt*,int,double);
 1.90724 +  int  (*bind_int)(sqlite3_stmt*,int,int);
 1.90725 +  int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
 1.90726 +  int  (*bind_null)(sqlite3_stmt*,int);
 1.90727 +  int  (*bind_parameter_count)(sqlite3_stmt*);
 1.90728 +  int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
 1.90729 +  const char * (*bind_parameter_name)(sqlite3_stmt*,int);
 1.90730 +  int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
 1.90731 +  int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
 1.90732 +  int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
 1.90733 +  int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
 1.90734 +  int  (*busy_timeout)(sqlite3*,int ms);
 1.90735 +  int  (*changes)(sqlite3*);
 1.90736 +  int  (*close)(sqlite3*);
 1.90737 +  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
 1.90738 +                           int eTextRep,const char*));
 1.90739 +  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
 1.90740 +                             int eTextRep,const void*));
 1.90741 +  const void * (*column_blob)(sqlite3_stmt*,int iCol);
 1.90742 +  int  (*column_bytes)(sqlite3_stmt*,int iCol);
 1.90743 +  int  (*column_bytes16)(sqlite3_stmt*,int iCol);
 1.90744 +  int  (*column_count)(sqlite3_stmt*pStmt);
 1.90745 +  const char * (*column_database_name)(sqlite3_stmt*,int);
 1.90746 +  const void * (*column_database_name16)(sqlite3_stmt*,int);
 1.90747 +  const char * (*column_decltype)(sqlite3_stmt*,int i);
 1.90748 +  const void * (*column_decltype16)(sqlite3_stmt*,int);
 1.90749 +  double  (*column_double)(sqlite3_stmt*,int iCol);
 1.90750 +  int  (*column_int)(sqlite3_stmt*,int iCol);
 1.90751 +  sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
 1.90752 +  const char * (*column_name)(sqlite3_stmt*,int);
 1.90753 +  const void * (*column_name16)(sqlite3_stmt*,int);
 1.90754 +  const char * (*column_origin_name)(sqlite3_stmt*,int);
 1.90755 +  const void * (*column_origin_name16)(sqlite3_stmt*,int);
 1.90756 +  const char * (*column_table_name)(sqlite3_stmt*,int);
 1.90757 +  const void * (*column_table_name16)(sqlite3_stmt*,int);
 1.90758 +  const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
 1.90759 +  const void * (*column_text16)(sqlite3_stmt*,int iCol);
 1.90760 +  int  (*column_type)(sqlite3_stmt*,int iCol);
 1.90761 +  sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
 1.90762 +  void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
 1.90763 +  int  (*complete)(const char*sql);
 1.90764 +  int  (*complete16)(const void*sql);
 1.90765 +  int  (*create_collation)(sqlite3*,const char*,int,void*,
 1.90766 +                           int(*)(void*,int,const void*,int,const void*));
 1.90767 +  int  (*create_collation16)(sqlite3*,const void*,int,void*,
 1.90768 +                             int(*)(void*,int,const void*,int,const void*));
 1.90769 +  int  (*create_function)(sqlite3*,const char*,int,int,void*,
 1.90770 +                          void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 1.90771 +                          void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 1.90772 +                          void (*xFinal)(sqlite3_context*));
 1.90773 +  int  (*create_function16)(sqlite3*,const void*,int,int,void*,
 1.90774 +                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 1.90775 +                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 1.90776 +                            void (*xFinal)(sqlite3_context*));
 1.90777 +  int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
 1.90778 +  int  (*data_count)(sqlite3_stmt*pStmt);
 1.90779 +  sqlite3 * (*db_handle)(sqlite3_stmt*);
 1.90780 +  int (*declare_vtab)(sqlite3*,const char*);
 1.90781 +  int  (*enable_shared_cache)(int);
 1.90782 +  int  (*errcode)(sqlite3*db);
 1.90783 +  const char * (*errmsg)(sqlite3*);
 1.90784 +  const void * (*errmsg16)(sqlite3*);
 1.90785 +  int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
 1.90786 +  int  (*expired)(sqlite3_stmt*);
 1.90787 +  int  (*finalize)(sqlite3_stmt*pStmt);
 1.90788 +  void  (*free)(void*);
 1.90789 +  void  (*free_table)(char**result);
 1.90790 +  int  (*get_autocommit)(sqlite3*);
 1.90791 +  void * (*get_auxdata)(sqlite3_context*,int);
 1.90792 +  int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
 1.90793 +  int  (*global_recover)(void);
 1.90794 +  void  (*interruptx)(sqlite3*);
 1.90795 +  sqlite_int64  (*last_insert_rowid)(sqlite3*);
 1.90796 +  const char * (*libversion)(void);
 1.90797 +  int  (*libversion_number)(void);
 1.90798 +  void *(*malloc)(int);
 1.90799 +  char * (*mprintf)(const char*,...);
 1.90800 +  int  (*open)(const char*,sqlite3**);
 1.90801 +  int  (*open16)(const void*,sqlite3**);
 1.90802 +  int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
 1.90803 +  int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
 1.90804 +  void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
 1.90805 +  void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
 1.90806 +  void *(*realloc)(void*,int);
 1.90807 +  int  (*reset)(sqlite3_stmt*pStmt);
 1.90808 +  void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
 1.90809 +  void  (*result_double)(sqlite3_context*,double);
 1.90810 +  void  (*result_error)(sqlite3_context*,const char*,int);
 1.90811 +  void  (*result_error16)(sqlite3_context*,const void*,int);
 1.90812 +  void  (*result_int)(sqlite3_context*,int);
 1.90813 +  void  (*result_int64)(sqlite3_context*,sqlite_int64);
 1.90814 +  void  (*result_null)(sqlite3_context*);
 1.90815 +  void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
 1.90816 +  void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
 1.90817 +  void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
 1.90818 +  void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
 1.90819 +  void  (*result_value)(sqlite3_context*,sqlite3_value*);
 1.90820 +  void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
 1.90821 +  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
 1.90822 +                         const char*,const char*),void*);
 1.90823 +  void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
 1.90824 +  char * (*snprintf)(int,char*,const char*,...);
 1.90825 +  int  (*step)(sqlite3_stmt*);
 1.90826 +  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
 1.90827 +                                char const**,char const**,int*,int*,int*);
 1.90828 +  void  (*thread_cleanup)(void);
 1.90829 +  int  (*total_changes)(sqlite3*);
 1.90830 +  void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
 1.90831 +  int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
 1.90832 +  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
 1.90833 +                                         sqlite_int64),void*);
 1.90834 +  void * (*user_data)(sqlite3_context*);
 1.90835 +  const void * (*value_blob)(sqlite3_value*);
 1.90836 +  int  (*value_bytes)(sqlite3_value*);
 1.90837 +  int  (*value_bytes16)(sqlite3_value*);
 1.90838 +  double  (*value_double)(sqlite3_value*);
 1.90839 +  int  (*value_int)(sqlite3_value*);
 1.90840 +  sqlite_int64  (*value_int64)(sqlite3_value*);
 1.90841 +  int  (*value_numeric_type)(sqlite3_value*);
 1.90842 +  const unsigned char * (*value_text)(sqlite3_value*);
 1.90843 +  const void * (*value_text16)(sqlite3_value*);
 1.90844 +  const void * (*value_text16be)(sqlite3_value*);
 1.90845 +  const void * (*value_text16le)(sqlite3_value*);
 1.90846 +  int  (*value_type)(sqlite3_value*);
 1.90847 +  char *(*vmprintf)(const char*,va_list);
 1.90848 +  /* Added ??? */
 1.90849 +  int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
 1.90850 +  /* Added by 3.3.13 */
 1.90851 +  int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
 1.90852 +  int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
 1.90853 +  int (*clear_bindings)(sqlite3_stmt*);
 1.90854 +  /* Added by 3.4.1 */
 1.90855 +  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
 1.90856 +                          void (*xDestroy)(void *));
 1.90857 +  /* Added by 3.5.0 */
 1.90858 +  int (*bind_zeroblob)(sqlite3_stmt*,int,int);
 1.90859 +  int (*blob_bytes)(sqlite3_blob*);
 1.90860 +  int (*blob_close)(sqlite3_blob*);
 1.90861 +  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
 1.90862 +                   int,sqlite3_blob**);
 1.90863 +  int (*blob_read)(sqlite3_blob*,void*,int,int);
 1.90864 +  int (*blob_write)(sqlite3_blob*,const void*,int,int);
 1.90865 +  int (*create_collation_v2)(sqlite3*,const char*,int,void*,
 1.90866 +                             int(*)(void*,int,const void*,int,const void*),
 1.90867 +                             void(*)(void*));
 1.90868 +  int (*file_control)(sqlite3*,const char*,int,void*);
 1.90869 +  sqlite3_int64 (*memory_highwater)(int);
 1.90870 +  sqlite3_int64 (*memory_used)(void);
 1.90871 +  sqlite3_mutex *(*mutex_alloc)(int);
 1.90872 +  void (*mutex_enter)(sqlite3_mutex*);
 1.90873 +  void (*mutex_free)(sqlite3_mutex*);
 1.90874 +  void (*mutex_leave)(sqlite3_mutex*);
 1.90875 +  int (*mutex_try)(sqlite3_mutex*);
 1.90876 +  int (*open_v2)(const char*,sqlite3**,int,const char*);
 1.90877 +  int (*release_memory)(int);
 1.90878 +  void (*result_error_nomem)(sqlite3_context*);
 1.90879 +  void (*result_error_toobig)(sqlite3_context*);
 1.90880 +  int (*sleep)(int);
 1.90881 +  void (*soft_heap_limit)(int);
 1.90882 +  sqlite3_vfs *(*vfs_find)(const char*);
 1.90883 +  int (*vfs_register)(sqlite3_vfs*,int);
 1.90884 +  int (*vfs_unregister)(sqlite3_vfs*);
 1.90885 +  int (*xthreadsafe)(void);
 1.90886 +  void (*result_zeroblob)(sqlite3_context*,int);
 1.90887 +  void (*result_error_code)(sqlite3_context*,int);
 1.90888 +  int (*test_control)(int, ...);
 1.90889 +  void (*randomness)(int,void*);
 1.90890 +  sqlite3 *(*context_db_handle)(sqlite3_context*);
 1.90891 +  int (*extended_result_codes)(sqlite3*,int);
 1.90892 +  int (*limit)(sqlite3*,int,int);
 1.90893 +  sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
 1.90894 +  const char *(*sql)(sqlite3_stmt*);
 1.90895 +  int (*status)(int,int*,int*,int);
 1.90896 +  int (*backup_finish)(sqlite3_backup*);
 1.90897 +  sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
 1.90898 +  int (*backup_pagecount)(sqlite3_backup*);
 1.90899 +  int (*backup_remaining)(sqlite3_backup*);
 1.90900 +  int (*backup_step)(sqlite3_backup*,int);
 1.90901 +  const char *(*compileoption_get)(int);
 1.90902 +  int (*compileoption_used)(const char*);
 1.90903 +  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
 1.90904 +                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 1.90905 +                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 1.90906 +                            void (*xFinal)(sqlite3_context*),
 1.90907 +                            void(*xDestroy)(void*));
 1.90908 +  int (*db_config)(sqlite3*,int,...);
 1.90909 +  sqlite3_mutex *(*db_mutex)(sqlite3*);
 1.90910 +  int (*db_status)(sqlite3*,int,int*,int*,int);
 1.90911 +  int (*extended_errcode)(sqlite3*);
 1.90912 +  void (*log)(int,const char*,...);
 1.90913 +  sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
 1.90914 +  const char *(*sourceid)(void);
 1.90915 +  int (*stmt_status)(sqlite3_stmt*,int,int);
 1.90916 +  int (*strnicmp)(const char*,const char*,int);
 1.90917 +  int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
 1.90918 +  int (*wal_autocheckpoint)(sqlite3*,int);
 1.90919 +  int (*wal_checkpoint)(sqlite3*,const char*);
 1.90920 +  void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
 1.90921 +  int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
 1.90922 +  int (*vtab_config)(sqlite3*,int op,...);
 1.90923 +  int (*vtab_on_conflict)(sqlite3*);
 1.90924 +};
 1.90925 +
 1.90926 +/*
 1.90927 +** The following macros redefine the API routines so that they are
 1.90928 +** redirected throught the global sqlite3_api structure.
 1.90929 +**
 1.90930 +** This header file is also used by the loadext.c source file
 1.90931 +** (part of the main SQLite library - not an extension) so that
 1.90932 +** it can get access to the sqlite3_api_routines structure
 1.90933 +** definition.  But the main library does not want to redefine
 1.90934 +** the API.  So the redefinition macros are only valid if the
 1.90935 +** SQLITE_CORE macros is undefined.
 1.90936 +*/
 1.90937 +#ifndef SQLITE_CORE
 1.90938 +#define sqlite3_aggregate_context      sqlite3_api->aggregate_context
 1.90939 +#ifndef SQLITE_OMIT_DEPRECATED
 1.90940 +#define sqlite3_aggregate_count        sqlite3_api->aggregate_count
 1.90941 +#endif
 1.90942 +#define sqlite3_bind_blob              sqlite3_api->bind_blob
 1.90943 +#define sqlite3_bind_double            sqlite3_api->bind_double
 1.90944 +#define sqlite3_bind_int               sqlite3_api->bind_int
 1.90945 +#define sqlite3_bind_int64             sqlite3_api->bind_int64
 1.90946 +#define sqlite3_bind_null              sqlite3_api->bind_null
 1.90947 +#define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
 1.90948 +#define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
 1.90949 +#define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
 1.90950 +#define sqlite3_bind_text              sqlite3_api->bind_text
 1.90951 +#define sqlite3_bind_text16            sqlite3_api->bind_text16
 1.90952 +#define sqlite3_bind_value             sqlite3_api->bind_value
 1.90953 +#define sqlite3_busy_handler           sqlite3_api->busy_handler
 1.90954 +#define sqlite3_busy_timeout           sqlite3_api->busy_timeout
 1.90955 +#define sqlite3_changes                sqlite3_api->changes
 1.90956 +#define sqlite3_close                  sqlite3_api->close
 1.90957 +#define sqlite3_collation_needed       sqlite3_api->collation_needed
 1.90958 +#define sqlite3_collation_needed16     sqlite3_api->collation_needed16
 1.90959 +#define sqlite3_column_blob            sqlite3_api->column_blob
 1.90960 +#define sqlite3_column_bytes           sqlite3_api->column_bytes
 1.90961 +#define sqlite3_column_bytes16         sqlite3_api->column_bytes16
 1.90962 +#define sqlite3_column_count           sqlite3_api->column_count
 1.90963 +#define sqlite3_column_database_name   sqlite3_api->column_database_name
 1.90964 +#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
 1.90965 +#define sqlite3_column_decltype        sqlite3_api->column_decltype
 1.90966 +#define sqlite3_column_decltype16      sqlite3_api->column_decltype16
 1.90967 +#define sqlite3_column_double          sqlite3_api->column_double
 1.90968 +#define sqlite3_column_int             sqlite3_api->column_int
 1.90969 +#define sqlite3_column_int64           sqlite3_api->column_int64
 1.90970 +#define sqlite3_column_name            sqlite3_api->column_name
 1.90971 +#define sqlite3_column_name16          sqlite3_api->column_name16
 1.90972 +#define sqlite3_column_origin_name     sqlite3_api->column_origin_name
 1.90973 +#define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
 1.90974 +#define sqlite3_column_table_name      sqlite3_api->column_table_name
 1.90975 +#define sqlite3_column_table_name16    sqlite3_api->column_table_name16
 1.90976 +#define sqlite3_column_text            sqlite3_api->column_text
 1.90977 +#define sqlite3_column_text16          sqlite3_api->column_text16
 1.90978 +#define sqlite3_column_type            sqlite3_api->column_type
 1.90979 +#define sqlite3_column_value           sqlite3_api->column_value
 1.90980 +#define sqlite3_commit_hook            sqlite3_api->commit_hook
 1.90981 +#define sqlite3_complete               sqlite3_api->complete
 1.90982 +#define sqlite3_complete16             sqlite3_api->complete16
 1.90983 +#define sqlite3_create_collation       sqlite3_api->create_collation
 1.90984 +#define sqlite3_create_collation16     sqlite3_api->create_collation16
 1.90985 +#define sqlite3_create_function        sqlite3_api->create_function
 1.90986 +#define sqlite3_create_function16      sqlite3_api->create_function16
 1.90987 +#define sqlite3_create_module          sqlite3_api->create_module
 1.90988 +#define sqlite3_create_module_v2       sqlite3_api->create_module_v2
 1.90989 +#define sqlite3_data_count             sqlite3_api->data_count
 1.90990 +#define sqlite3_db_handle              sqlite3_api->db_handle
 1.90991 +#define sqlite3_declare_vtab           sqlite3_api->declare_vtab
 1.90992 +#define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
 1.90993 +#define sqlite3_errcode                sqlite3_api->errcode
 1.90994 +#define sqlite3_errmsg                 sqlite3_api->errmsg
 1.90995 +#define sqlite3_errmsg16               sqlite3_api->errmsg16
 1.90996 +#define sqlite3_exec                   sqlite3_api->exec
 1.90997 +#ifndef SQLITE_OMIT_DEPRECATED
 1.90998 +#define sqlite3_expired                sqlite3_api->expired
 1.90999 +#endif
 1.91000 +#define sqlite3_finalize               sqlite3_api->finalize
 1.91001 +#define sqlite3_free                   sqlite3_api->free
 1.91002 +#define sqlite3_free_table             sqlite3_api->free_table
 1.91003 +#define sqlite3_get_autocommit         sqlite3_api->get_autocommit
 1.91004 +#define sqlite3_get_auxdata            sqlite3_api->get_auxdata
 1.91005 +#define sqlite3_get_table              sqlite3_api->get_table
 1.91006 +#ifndef SQLITE_OMIT_DEPRECATED
 1.91007 +#define sqlite3_global_recover         sqlite3_api->global_recover
 1.91008 +#endif
 1.91009 +#define sqlite3_interrupt              sqlite3_api->interruptx
 1.91010 +#define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
 1.91011 +#define sqlite3_libversion             sqlite3_api->libversion
 1.91012 +#define sqlite3_libversion_number      sqlite3_api->libversion_number
 1.91013 +#define sqlite3_malloc                 sqlite3_api->malloc
 1.91014 +#define sqlite3_mprintf                sqlite3_api->mprintf
 1.91015 +#define sqlite3_open                   sqlite3_api->open
 1.91016 +#define sqlite3_open16                 sqlite3_api->open16
 1.91017 +#define sqlite3_prepare                sqlite3_api->prepare
 1.91018 +#define sqlite3_prepare16              sqlite3_api->prepare16
 1.91019 +#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
 1.91020 +#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
 1.91021 +#define sqlite3_profile                sqlite3_api->profile
 1.91022 +#define sqlite3_progress_handler       sqlite3_api->progress_handler
 1.91023 +#define sqlite3_realloc                sqlite3_api->realloc
 1.91024 +#define sqlite3_reset                  sqlite3_api->reset
 1.91025 +#define sqlite3_result_blob            sqlite3_api->result_blob
 1.91026 +#define sqlite3_result_double          sqlite3_api->result_double
 1.91027 +#define sqlite3_result_error           sqlite3_api->result_error
 1.91028 +#define sqlite3_result_error16         sqlite3_api->result_error16
 1.91029 +#define sqlite3_result_int             sqlite3_api->result_int
 1.91030 +#define sqlite3_result_int64           sqlite3_api->result_int64
 1.91031 +#define sqlite3_result_null            sqlite3_api->result_null
 1.91032 +#define sqlite3_result_text            sqlite3_api->result_text
 1.91033 +#define sqlite3_result_text16          sqlite3_api->result_text16
 1.91034 +#define sqlite3_result_text16be        sqlite3_api->result_text16be
 1.91035 +#define sqlite3_result_text16le        sqlite3_api->result_text16le
 1.91036 +#define sqlite3_result_value           sqlite3_api->result_value
 1.91037 +#define sqlite3_rollback_hook          sqlite3_api->rollback_hook
 1.91038 +#define sqlite3_set_authorizer         sqlite3_api->set_authorizer
 1.91039 +#define sqlite3_set_auxdata            sqlite3_api->set_auxdata
 1.91040 +#define sqlite3_snprintf               sqlite3_api->snprintf
 1.91041 +#define sqlite3_step                   sqlite3_api->step
 1.91042 +#define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
 1.91043 +#define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
 1.91044 +#define sqlite3_total_changes          sqlite3_api->total_changes
 1.91045 +#define sqlite3_trace                  sqlite3_api->trace
 1.91046 +#ifndef SQLITE_OMIT_DEPRECATED
 1.91047 +#define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
 1.91048 +#endif
 1.91049 +#define sqlite3_update_hook            sqlite3_api->update_hook
 1.91050 +#define sqlite3_user_data              sqlite3_api->user_data
 1.91051 +#define sqlite3_value_blob             sqlite3_api->value_blob
 1.91052 +#define sqlite3_value_bytes            sqlite3_api->value_bytes
 1.91053 +#define sqlite3_value_bytes16          sqlite3_api->value_bytes16
 1.91054 +#define sqlite3_value_double           sqlite3_api->value_double
 1.91055 +#define sqlite3_value_int              sqlite3_api->value_int
 1.91056 +#define sqlite3_value_int64            sqlite3_api->value_int64
 1.91057 +#define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
 1.91058 +#define sqlite3_value_text             sqlite3_api->value_text
 1.91059 +#define sqlite3_value_text16           sqlite3_api->value_text16
 1.91060 +#define sqlite3_value_text16be         sqlite3_api->value_text16be
 1.91061 +#define sqlite3_value_text16le         sqlite3_api->value_text16le
 1.91062 +#define sqlite3_value_type             sqlite3_api->value_type
 1.91063 +#define sqlite3_vmprintf               sqlite3_api->vmprintf
 1.91064 +#define sqlite3_overload_function      sqlite3_api->overload_function
 1.91065 +#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
 1.91066 +#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
 1.91067 +#define sqlite3_clear_bindings         sqlite3_api->clear_bindings
 1.91068 +#define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
 1.91069 +#define sqlite3_blob_bytes             sqlite3_api->blob_bytes
 1.91070 +#define sqlite3_blob_close             sqlite3_api->blob_close
 1.91071 +#define sqlite3_blob_open              sqlite3_api->blob_open
 1.91072 +#define sqlite3_blob_read              sqlite3_api->blob_read
 1.91073 +#define sqlite3_blob_write             sqlite3_api->blob_write
 1.91074 +#define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
 1.91075 +#define sqlite3_file_control           sqlite3_api->file_control
 1.91076 +#define sqlite3_memory_highwater       sqlite3_api->memory_highwater
 1.91077 +#define sqlite3_memory_used            sqlite3_api->memory_used
 1.91078 +#define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
 1.91079 +#define sqlite3_mutex_enter            sqlite3_api->mutex_enter
 1.91080 +#define sqlite3_mutex_free             sqlite3_api->mutex_free
 1.91081 +#define sqlite3_mutex_leave            sqlite3_api->mutex_leave
 1.91082 +#define sqlite3_mutex_try              sqlite3_api->mutex_try
 1.91083 +#define sqlite3_open_v2                sqlite3_api->open_v2
 1.91084 +#define sqlite3_release_memory         sqlite3_api->release_memory
 1.91085 +#define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
 1.91086 +#define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
 1.91087 +#define sqlite3_sleep                  sqlite3_api->sleep
 1.91088 +#define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
 1.91089 +#define sqlite3_vfs_find               sqlite3_api->vfs_find
 1.91090 +#define sqlite3_vfs_register           sqlite3_api->vfs_register
 1.91091 +#define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
 1.91092 +#define sqlite3_threadsafe             sqlite3_api->xthreadsafe
 1.91093 +#define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
 1.91094 +#define sqlite3_result_error_code      sqlite3_api->result_error_code
 1.91095 +#define sqlite3_test_control           sqlite3_api->test_control
 1.91096 +#define sqlite3_randomness             sqlite3_api->randomness
 1.91097 +#define sqlite3_context_db_handle      sqlite3_api->context_db_handle
 1.91098 +#define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
 1.91099 +#define sqlite3_limit                  sqlite3_api->limit
 1.91100 +#define sqlite3_next_stmt              sqlite3_api->next_stmt
 1.91101 +#define sqlite3_sql                    sqlite3_api->sql
 1.91102 +#define sqlite3_status                 sqlite3_api->status
 1.91103 +#define sqlite3_backup_finish          sqlite3_api->backup_finish
 1.91104 +#define sqlite3_backup_init            sqlite3_api->backup_init
 1.91105 +#define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
 1.91106 +#define sqlite3_backup_remaining       sqlite3_api->backup_remaining
 1.91107 +#define sqlite3_backup_step            sqlite3_api->backup_step
 1.91108 +#define sqlite3_compileoption_get      sqlite3_api->compileoption_get
 1.91109 +#define sqlite3_compileoption_used     sqlite3_api->compileoption_used
 1.91110 +#define sqlite3_create_function_v2     sqlite3_api->create_function_v2
 1.91111 +#define sqlite3_db_config              sqlite3_api->db_config
 1.91112 +#define sqlite3_db_mutex               sqlite3_api->db_mutex
 1.91113 +#define sqlite3_db_status              sqlite3_api->db_status
 1.91114 +#define sqlite3_extended_errcode       sqlite3_api->extended_errcode
 1.91115 +#define sqlite3_log                    sqlite3_api->log
 1.91116 +#define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
 1.91117 +#define sqlite3_sourceid               sqlite3_api->sourceid
 1.91118 +#define sqlite3_stmt_status            sqlite3_api->stmt_status
 1.91119 +#define sqlite3_strnicmp               sqlite3_api->strnicmp
 1.91120 +#define sqlite3_unlock_notify          sqlite3_api->unlock_notify
 1.91121 +#define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
 1.91122 +#define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
 1.91123 +#define sqlite3_wal_hook               sqlite3_api->wal_hook
 1.91124 +#define sqlite3_blob_reopen            sqlite3_api->blob_reopen
 1.91125 +#define sqlite3_vtab_config            sqlite3_api->vtab_config
 1.91126 +#define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
 1.91127 +#endif /* SQLITE_CORE */
 1.91128 +
 1.91129 +#define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api = 0;
 1.91130 +#define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
 1.91131 +
 1.91132 +#endif /* _SQLITE3EXT_H_ */
 1.91133 +
 1.91134 +/************** End of sqlite3ext.h ******************************************/
 1.91135 +/************** Continuing where we left off in loadext.c ********************/
 1.91136 +/* #include <string.h> */
 1.91137 +
 1.91138 +#ifndef SQLITE_OMIT_LOAD_EXTENSION
 1.91139 +
 1.91140 +/*
 1.91141 +** Some API routines are omitted when various features are
 1.91142 +** excluded from a build of SQLite.  Substitute a NULL pointer
 1.91143 +** for any missing APIs.
 1.91144 +*/
 1.91145 +#ifndef SQLITE_ENABLE_COLUMN_METADATA
 1.91146 +# define sqlite3_column_database_name   0
 1.91147 +# define sqlite3_column_database_name16 0
 1.91148 +# define sqlite3_column_table_name      0
 1.91149 +# define sqlite3_column_table_name16    0
 1.91150 +# define sqlite3_column_origin_name     0
 1.91151 +# define sqlite3_column_origin_name16   0
 1.91152 +# define sqlite3_table_column_metadata  0
 1.91153 +#endif
 1.91154 +
 1.91155 +#ifdef SQLITE_OMIT_AUTHORIZATION
 1.91156 +# define sqlite3_set_authorizer         0
 1.91157 +#endif
 1.91158 +
 1.91159 +#ifdef SQLITE_OMIT_UTF16
 1.91160 +# define sqlite3_bind_text16            0
 1.91161 +# define sqlite3_collation_needed16     0
 1.91162 +# define sqlite3_column_decltype16      0
 1.91163 +# define sqlite3_column_name16          0
 1.91164 +# define sqlite3_column_text16          0
 1.91165 +# define sqlite3_complete16             0
 1.91166 +# define sqlite3_create_collation16     0
 1.91167 +# define sqlite3_create_function16      0
 1.91168 +# define sqlite3_errmsg16               0
 1.91169 +# define sqlite3_open16                 0
 1.91170 +# define sqlite3_prepare16              0
 1.91171 +# define sqlite3_prepare16_v2           0
 1.91172 +# define sqlite3_result_error16         0
 1.91173 +# define sqlite3_result_text16          0
 1.91174 +# define sqlite3_result_text16be        0
 1.91175 +# define sqlite3_result_text16le        0
 1.91176 +# define sqlite3_value_text16           0
 1.91177 +# define sqlite3_value_text16be         0
 1.91178 +# define sqlite3_value_text16le         0
 1.91179 +# define sqlite3_column_database_name16 0
 1.91180 +# define sqlite3_column_table_name16    0
 1.91181 +# define sqlite3_column_origin_name16   0
 1.91182 +#endif
 1.91183 +
 1.91184 +#ifdef SQLITE_OMIT_COMPLETE
 1.91185 +# define sqlite3_complete 0
 1.91186 +# define sqlite3_complete16 0
 1.91187 +#endif
 1.91188 +
 1.91189 +#ifdef SQLITE_OMIT_DECLTYPE
 1.91190 +# define sqlite3_column_decltype16      0
 1.91191 +# define sqlite3_column_decltype        0
 1.91192 +#endif
 1.91193 +
 1.91194 +#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
 1.91195 +# define sqlite3_progress_handler 0
 1.91196 +#endif
 1.91197 +
 1.91198 +#ifdef SQLITE_OMIT_VIRTUALTABLE
 1.91199 +# define sqlite3_create_module 0
 1.91200 +# define sqlite3_create_module_v2 0
 1.91201 +# define sqlite3_declare_vtab 0
 1.91202 +# define sqlite3_vtab_config 0
 1.91203 +# define sqlite3_vtab_on_conflict 0
 1.91204 +#endif
 1.91205 +
 1.91206 +#ifdef SQLITE_OMIT_SHARED_CACHE
 1.91207 +# define sqlite3_enable_shared_cache 0
 1.91208 +#endif
 1.91209 +
 1.91210 +#ifdef SQLITE_OMIT_TRACE
 1.91211 +# define sqlite3_profile       0
 1.91212 +# define sqlite3_trace         0
 1.91213 +#endif
 1.91214 +
 1.91215 +#ifdef SQLITE_OMIT_GET_TABLE
 1.91216 +# define sqlite3_free_table    0
 1.91217 +# define sqlite3_get_table     0
 1.91218 +#endif
 1.91219 +
 1.91220 +#ifdef SQLITE_OMIT_INCRBLOB
 1.91221 +#define sqlite3_bind_zeroblob  0
 1.91222 +#define sqlite3_blob_bytes     0
 1.91223 +#define sqlite3_blob_close     0
 1.91224 +#define sqlite3_blob_open      0
 1.91225 +#define sqlite3_blob_read      0
 1.91226 +#define sqlite3_blob_write     0
 1.91227 +#define sqlite3_blob_reopen    0
 1.91228 +#endif
 1.91229 +
 1.91230 +/*
 1.91231 +** The following structure contains pointers to all SQLite API routines.
 1.91232 +** A pointer to this structure is passed into extensions when they are
 1.91233 +** loaded so that the extension can make calls back into the SQLite
 1.91234 +** library.
 1.91235 +**
 1.91236 +** When adding new APIs, add them to the bottom of this structure
 1.91237 +** in order to preserve backwards compatibility.
 1.91238 +**
 1.91239 +** Extensions that use newer APIs should first call the
 1.91240 +** sqlite3_libversion_number() to make sure that the API they
 1.91241 +** intend to use is supported by the library.  Extensions should
 1.91242 +** also check to make sure that the pointer to the function is
 1.91243 +** not NULL before calling it.
 1.91244 +*/
 1.91245 +static const sqlite3_api_routines sqlite3Apis = {
 1.91246 +  sqlite3_aggregate_context,
 1.91247 +#ifndef SQLITE_OMIT_DEPRECATED
 1.91248 +  sqlite3_aggregate_count,
 1.91249 +#else
 1.91250 +  0,
 1.91251 +#endif
 1.91252 +  sqlite3_bind_blob,
 1.91253 +  sqlite3_bind_double,
 1.91254 +  sqlite3_bind_int,
 1.91255 +  sqlite3_bind_int64,
 1.91256 +  sqlite3_bind_null,
 1.91257 +  sqlite3_bind_parameter_count,
 1.91258 +  sqlite3_bind_parameter_index,
 1.91259 +  sqlite3_bind_parameter_name,
 1.91260 +  sqlite3_bind_text,
 1.91261 +  sqlite3_bind_text16,
 1.91262 +  sqlite3_bind_value,
 1.91263 +  sqlite3_busy_handler,
 1.91264 +  sqlite3_busy_timeout,
 1.91265 +  sqlite3_changes,
 1.91266 +  sqlite3_close,
 1.91267 +  sqlite3_collation_needed,
 1.91268 +  sqlite3_collation_needed16,
 1.91269 +  sqlite3_column_blob,
 1.91270 +  sqlite3_column_bytes,
 1.91271 +  sqlite3_column_bytes16,
 1.91272 +  sqlite3_column_count,
 1.91273 +  sqlite3_column_database_name,
 1.91274 +  sqlite3_column_database_name16,
 1.91275 +  sqlite3_column_decltype,
 1.91276 +  sqlite3_column_decltype16,
 1.91277 +  sqlite3_column_double,
 1.91278 +  sqlite3_column_int,
 1.91279 +  sqlite3_column_int64,
 1.91280 +  sqlite3_column_name,
 1.91281 +  sqlite3_column_name16,
 1.91282 +  sqlite3_column_origin_name,
 1.91283 +  sqlite3_column_origin_name16,
 1.91284 +  sqlite3_column_table_name,
 1.91285 +  sqlite3_column_table_name16,
 1.91286 +  sqlite3_column_text,
 1.91287 +  sqlite3_column_text16,
 1.91288 +  sqlite3_column_type,
 1.91289 +  sqlite3_column_value,
 1.91290 +  sqlite3_commit_hook,
 1.91291 +  sqlite3_complete,
 1.91292 +  sqlite3_complete16,
 1.91293 +  sqlite3_create_collation,
 1.91294 +  sqlite3_create_collation16,
 1.91295 +  sqlite3_create_function,
 1.91296 +  sqlite3_create_function16,
 1.91297 +  sqlite3_create_module,
 1.91298 +  sqlite3_data_count,
 1.91299 +  sqlite3_db_handle,
 1.91300 +  sqlite3_declare_vtab,
 1.91301 +  sqlite3_enable_shared_cache,
 1.91302 +  sqlite3_errcode,
 1.91303 +  sqlite3_errmsg,
 1.91304 +  sqlite3_errmsg16,
 1.91305 +  sqlite3_exec,
 1.91306 +#ifndef SQLITE_OMIT_DEPRECATED
 1.91307 +  sqlite3_expired,
 1.91308 +#else
 1.91309 +  0,
 1.91310 +#endif
 1.91311 +  sqlite3_finalize,
 1.91312 +  sqlite3_free,
 1.91313 +  sqlite3_free_table,
 1.91314 +  sqlite3_get_autocommit,
 1.91315 +  sqlite3_get_auxdata,
 1.91316 +  sqlite3_get_table,
 1.91317 +  0,     /* Was sqlite3_global_recover(), but that function is deprecated */
 1.91318 +  sqlite3_interrupt,
 1.91319 +  sqlite3_last_insert_rowid,
 1.91320 +  sqlite3_libversion,
 1.91321 +  sqlite3_libversion_number,
 1.91322 +  sqlite3_malloc,
 1.91323 +  sqlite3_mprintf,
 1.91324 +  sqlite3_open,
 1.91325 +  sqlite3_open16,
 1.91326 +  sqlite3_prepare,
 1.91327 +  sqlite3_prepare16,
 1.91328 +  sqlite3_profile,
 1.91329 +  sqlite3_progress_handler,
 1.91330 +  sqlite3_realloc,
 1.91331 +  sqlite3_reset,
 1.91332 +  sqlite3_result_blob,
 1.91333 +  sqlite3_result_double,
 1.91334 +  sqlite3_result_error,
 1.91335 +  sqlite3_result_error16,
 1.91336 +  sqlite3_result_int,
 1.91337 +  sqlite3_result_int64,
 1.91338 +  sqlite3_result_null,
 1.91339 +  sqlite3_result_text,
 1.91340 +  sqlite3_result_text16,
 1.91341 +  sqlite3_result_text16be,
 1.91342 +  sqlite3_result_text16le,
 1.91343 +  sqlite3_result_value,
 1.91344 +  sqlite3_rollback_hook,
 1.91345 +  sqlite3_set_authorizer,
 1.91346 +  sqlite3_set_auxdata,
 1.91347 +  sqlite3_snprintf,
 1.91348 +  sqlite3_step,
 1.91349 +  sqlite3_table_column_metadata,
 1.91350 +#ifndef SQLITE_OMIT_DEPRECATED
 1.91351 +  sqlite3_thread_cleanup,
 1.91352 +#else
 1.91353 +  0,
 1.91354 +#endif
 1.91355 +  sqlite3_total_changes,
 1.91356 +  sqlite3_trace,
 1.91357 +#ifndef SQLITE_OMIT_DEPRECATED
 1.91358 +  sqlite3_transfer_bindings,
 1.91359 +#else
 1.91360 +  0,
 1.91361 +#endif
 1.91362 +  sqlite3_update_hook,
 1.91363 +  sqlite3_user_data,
 1.91364 +  sqlite3_value_blob,
 1.91365 +  sqlite3_value_bytes,
 1.91366 +  sqlite3_value_bytes16,
 1.91367 +  sqlite3_value_double,
 1.91368 +  sqlite3_value_int,
 1.91369 +  sqlite3_value_int64,
 1.91370 +  sqlite3_value_numeric_type,
 1.91371 +  sqlite3_value_text,
 1.91372 +  sqlite3_value_text16,
 1.91373 +  sqlite3_value_text16be,
 1.91374 +  sqlite3_value_text16le,
 1.91375 +  sqlite3_value_type,
 1.91376 +  sqlite3_vmprintf,
 1.91377 +  /*
 1.91378 +  ** The original API set ends here.  All extensions can call any
 1.91379 +  ** of the APIs above provided that the pointer is not NULL.  But
 1.91380 +  ** before calling APIs that follow, extension should check the
 1.91381 +  ** sqlite3_libversion_number() to make sure they are dealing with
 1.91382 +  ** a library that is new enough to support that API.
 1.91383 +  *************************************************************************
 1.91384 +  */
 1.91385 +  sqlite3_overload_function,
 1.91386 +
 1.91387 +  /*
 1.91388 +  ** Added after 3.3.13
 1.91389 +  */
 1.91390 +  sqlite3_prepare_v2,
 1.91391 +  sqlite3_prepare16_v2,
 1.91392 +  sqlite3_clear_bindings,
 1.91393 +
 1.91394 +  /*
 1.91395 +  ** Added for 3.4.1
 1.91396 +  */
 1.91397 +  sqlite3_create_module_v2,
 1.91398 +
 1.91399 +  /*
 1.91400 +  ** Added for 3.5.0
 1.91401 +  */
 1.91402 +  sqlite3_bind_zeroblob,
 1.91403 +  sqlite3_blob_bytes,
 1.91404 +  sqlite3_blob_close,
 1.91405 +  sqlite3_blob_open,
 1.91406 +  sqlite3_blob_read,
 1.91407 +  sqlite3_blob_write,
 1.91408 +  sqlite3_create_collation_v2,
 1.91409 +  sqlite3_file_control,
 1.91410 +  sqlite3_memory_highwater,
 1.91411 +  sqlite3_memory_used,
 1.91412 +#ifdef SQLITE_MUTEX_OMIT
 1.91413 +  0, 
 1.91414 +  0, 
 1.91415 +  0,
 1.91416 +  0,
 1.91417 +  0,
 1.91418 +#else
 1.91419 +  sqlite3_mutex_alloc,
 1.91420 +  sqlite3_mutex_enter,
 1.91421 +  sqlite3_mutex_free,
 1.91422 +  sqlite3_mutex_leave,
 1.91423 +  sqlite3_mutex_try,
 1.91424 +#endif
 1.91425 +  sqlite3_open_v2,
 1.91426 +  sqlite3_release_memory,
 1.91427 +  sqlite3_result_error_nomem,
 1.91428 +  sqlite3_result_error_toobig,
 1.91429 +  sqlite3_sleep,
 1.91430 +  sqlite3_soft_heap_limit,
 1.91431 +  sqlite3_vfs_find,
 1.91432 +  sqlite3_vfs_register,
 1.91433 +  sqlite3_vfs_unregister,
 1.91434 +
 1.91435 +  /*
 1.91436 +  ** Added for 3.5.8
 1.91437 +  */
 1.91438 +  sqlite3_threadsafe,
 1.91439 +  sqlite3_result_zeroblob,
 1.91440 +  sqlite3_result_error_code,
 1.91441 +  sqlite3_test_control,
 1.91442 +  sqlite3_randomness,
 1.91443 +  sqlite3_context_db_handle,
 1.91444 +
 1.91445 +  /*
 1.91446 +  ** Added for 3.6.0
 1.91447 +  */
 1.91448 +  sqlite3_extended_result_codes,
 1.91449 +  sqlite3_limit,
 1.91450 +  sqlite3_next_stmt,
 1.91451 +  sqlite3_sql,
 1.91452 +  sqlite3_status,
 1.91453 +
 1.91454 +  /*
 1.91455 +  ** Added for 3.7.4
 1.91456 +  */
 1.91457 +  sqlite3_backup_finish,
 1.91458 +  sqlite3_backup_init,
 1.91459 +  sqlite3_backup_pagecount,
 1.91460 +  sqlite3_backup_remaining,
 1.91461 +  sqlite3_backup_step,
 1.91462 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 1.91463 +  sqlite3_compileoption_get,
 1.91464 +  sqlite3_compileoption_used,
 1.91465 +#else
 1.91466 +  0,
 1.91467 +  0,
 1.91468 +#endif
 1.91469 +  sqlite3_create_function_v2,
 1.91470 +  sqlite3_db_config,
 1.91471 +  sqlite3_db_mutex,
 1.91472 +  sqlite3_db_status,
 1.91473 +  sqlite3_extended_errcode,
 1.91474 +  sqlite3_log,
 1.91475 +  sqlite3_soft_heap_limit64,
 1.91476 +  sqlite3_sourceid,
 1.91477 +  sqlite3_stmt_status,
 1.91478 +  sqlite3_strnicmp,
 1.91479 +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 1.91480 +  sqlite3_unlock_notify,
 1.91481 +#else
 1.91482 +  0,
 1.91483 +#endif
 1.91484 +#ifndef SQLITE_OMIT_WAL
 1.91485 +  sqlite3_wal_autocheckpoint,
 1.91486 +  sqlite3_wal_checkpoint,
 1.91487 +  sqlite3_wal_hook,
 1.91488 +#else
 1.91489 +  0,
 1.91490 +  0,
 1.91491 +  0,
 1.91492 +#endif
 1.91493 +  sqlite3_blob_reopen,
 1.91494 +  sqlite3_vtab_config,
 1.91495 +  sqlite3_vtab_on_conflict,
 1.91496 +};
 1.91497 +
 1.91498 +/*
 1.91499 +** Attempt to load an SQLite extension library contained in the file
 1.91500 +** zFile.  The entry point is zProc.  zProc may be 0 in which case a
 1.91501 +** default entry point name (sqlite3_extension_init) is used.  Use
 1.91502 +** of the default name is recommended.
 1.91503 +**
 1.91504 +** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
 1.91505 +**
 1.91506 +** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
 1.91507 +** error message text.  The calling function should free this memory
 1.91508 +** by calling sqlite3DbFree(db, ).
 1.91509 +*/
 1.91510 +static int sqlite3LoadExtension(
 1.91511 +  sqlite3 *db,          /* Load the extension into this database connection */
 1.91512 +  const char *zFile,    /* Name of the shared library containing extension */
 1.91513 +  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
 1.91514 +  char **pzErrMsg       /* Put error message here if not 0 */
 1.91515 +){
 1.91516 +  sqlite3_vfs *pVfs = db->pVfs;
 1.91517 +  void *handle;
 1.91518 +  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 1.91519 +  char *zErrmsg = 0;
 1.91520 +  void **aHandle;
 1.91521 +  int nMsg = 300 + sqlite3Strlen30(zFile);
 1.91522 +
 1.91523 +  if( pzErrMsg ) *pzErrMsg = 0;
 1.91524 +
 1.91525 +  /* Ticket #1863.  To avoid a creating security problems for older
 1.91526 +  ** applications that relink against newer versions of SQLite, the
 1.91527 +  ** ability to run load_extension is turned off by default.  One
 1.91528 +  ** must call sqlite3_enable_load_extension() to turn on extension
 1.91529 +  ** loading.  Otherwise you get the following error.
 1.91530 +  */
 1.91531 +  if( (db->flags & SQLITE_LoadExtension)==0 ){
 1.91532 +    if( pzErrMsg ){
 1.91533 +      *pzErrMsg = sqlite3_mprintf("not authorized");
 1.91534 +    }
 1.91535 +    return SQLITE_ERROR;
 1.91536 +  }
 1.91537 +
 1.91538 +  if( zProc==0 ){
 1.91539 +    zProc = "sqlite3_extension_init";
 1.91540 +  }
 1.91541 +
 1.91542 +  handle = sqlite3OsDlOpen(pVfs, zFile);
 1.91543 +  if( handle==0 ){
 1.91544 +    if( pzErrMsg ){
 1.91545 +      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
 1.91546 +      if( zErrmsg ){
 1.91547 +        sqlite3_snprintf(nMsg, zErrmsg, 
 1.91548 +            "unable to open shared library [%s]", zFile);
 1.91549 +        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
 1.91550 +      }
 1.91551 +    }
 1.91552 +    return SQLITE_ERROR;
 1.91553 +  }
 1.91554 +  xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 1.91555 +                   sqlite3OsDlSym(pVfs, handle, zProc);
 1.91556 +  if( xInit==0 ){
 1.91557 +    if( pzErrMsg ){
 1.91558 +      nMsg += sqlite3Strlen30(zProc);
 1.91559 +      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
 1.91560 +      if( zErrmsg ){
 1.91561 +        sqlite3_snprintf(nMsg, zErrmsg,
 1.91562 +            "no entry point [%s] in shared library [%s]", zProc,zFile);
 1.91563 +        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
 1.91564 +      }
 1.91565 +      sqlite3OsDlClose(pVfs, handle);
 1.91566 +    }
 1.91567 +    return SQLITE_ERROR;
 1.91568 +  }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
 1.91569 +    if( pzErrMsg ){
 1.91570 +      *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
 1.91571 +    }
 1.91572 +    sqlite3_free(zErrmsg);
 1.91573 +    sqlite3OsDlClose(pVfs, handle);
 1.91574 +    return SQLITE_ERROR;
 1.91575 +  }
 1.91576 +
 1.91577 +  /* Append the new shared library handle to the db->aExtension array. */
 1.91578 +  aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
 1.91579 +  if( aHandle==0 ){
 1.91580 +    return SQLITE_NOMEM;
 1.91581 +  }
 1.91582 +  if( db->nExtension>0 ){
 1.91583 +    memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
 1.91584 +  }
 1.91585 +  sqlite3DbFree(db, db->aExtension);
 1.91586 +  db->aExtension = aHandle;
 1.91587 +
 1.91588 +  db->aExtension[db->nExtension++] = handle;
 1.91589 +  return SQLITE_OK;
 1.91590 +}
 1.91591 +SQLITE_API int sqlite3_load_extension(
 1.91592 +  sqlite3 *db,          /* Load the extension into this database connection */
 1.91593 +  const char *zFile,    /* Name of the shared library containing extension */
 1.91594 +  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
 1.91595 +  char **pzErrMsg       /* Put error message here if not 0 */
 1.91596 +){
 1.91597 +  int rc;
 1.91598 +  sqlite3_mutex_enter(db->mutex);
 1.91599 +  rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
 1.91600 +  rc = sqlite3ApiExit(db, rc);
 1.91601 +  sqlite3_mutex_leave(db->mutex);
 1.91602 +  return rc;
 1.91603 +}
 1.91604 +
 1.91605 +/*
 1.91606 +** Call this routine when the database connection is closing in order
 1.91607 +** to clean up loaded extensions
 1.91608 +*/
 1.91609 +SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
 1.91610 +  int i;
 1.91611 +  assert( sqlite3_mutex_held(db->mutex) );
 1.91612 +  for(i=0; i<db->nExtension; i++){
 1.91613 +    sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
 1.91614 +  }
 1.91615 +  sqlite3DbFree(db, db->aExtension);
 1.91616 +}
 1.91617 +
 1.91618 +/*
 1.91619 +** Enable or disable extension loading.  Extension loading is disabled by
 1.91620 +** default so as not to open security holes in older applications.
 1.91621 +*/
 1.91622 +SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
 1.91623 +  sqlite3_mutex_enter(db->mutex);
 1.91624 +  if( onoff ){
 1.91625 +    db->flags |= SQLITE_LoadExtension;
 1.91626 +  }else{
 1.91627 +    db->flags &= ~SQLITE_LoadExtension;
 1.91628 +  }
 1.91629 +  sqlite3_mutex_leave(db->mutex);
 1.91630 +  return SQLITE_OK;
 1.91631 +}
 1.91632 +
 1.91633 +#endif /* SQLITE_OMIT_LOAD_EXTENSION */
 1.91634 +
 1.91635 +/*
 1.91636 +** The auto-extension code added regardless of whether or not extension
 1.91637 +** loading is supported.  We need a dummy sqlite3Apis pointer for that
 1.91638 +** code if regular extension loading is not available.  This is that
 1.91639 +** dummy pointer.
 1.91640 +*/
 1.91641 +#ifdef SQLITE_OMIT_LOAD_EXTENSION
 1.91642 +static const sqlite3_api_routines sqlite3Apis = { 0 };
 1.91643 +#endif
 1.91644 +
 1.91645 +
 1.91646 +/*
 1.91647 +** The following object holds the list of automatically loaded
 1.91648 +** extensions.
 1.91649 +**
 1.91650 +** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
 1.91651 +** mutex must be held while accessing this list.
 1.91652 +*/
 1.91653 +typedef struct sqlite3AutoExtList sqlite3AutoExtList;
 1.91654 +static SQLITE_WSD struct sqlite3AutoExtList {
 1.91655 +  int nExt;              /* Number of entries in aExt[] */          
 1.91656 +  void (**aExt)(void);   /* Pointers to the extension init functions */
 1.91657 +} sqlite3Autoext = { 0, 0 };
 1.91658 +
 1.91659 +/* The "wsdAutoext" macro will resolve to the autoextension
 1.91660 +** state vector.  If writable static data is unsupported on the target,
 1.91661 +** we have to locate the state vector at run-time.  In the more common
 1.91662 +** case where writable static data is supported, wsdStat can refer directly
 1.91663 +** to the "sqlite3Autoext" state vector declared above.
 1.91664 +*/
 1.91665 +#ifdef SQLITE_OMIT_WSD
 1.91666 +# define wsdAutoextInit \
 1.91667 +  sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
 1.91668 +# define wsdAutoext x[0]
 1.91669 +#else
 1.91670 +# define wsdAutoextInit
 1.91671 +# define wsdAutoext sqlite3Autoext
 1.91672 +#endif
 1.91673 +
 1.91674 +
 1.91675 +/*
 1.91676 +** Register a statically linked extension that is automatically
 1.91677 +** loaded by every new database connection.
 1.91678 +*/
 1.91679 +SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
 1.91680 +  int rc = SQLITE_OK;
 1.91681 +#ifndef SQLITE_OMIT_AUTOINIT
 1.91682 +  rc = sqlite3_initialize();
 1.91683 +  if( rc ){
 1.91684 +    return rc;
 1.91685 +  }else
 1.91686 +#endif
 1.91687 +  {
 1.91688 +    int i;
 1.91689 +#if SQLITE_THREADSAFE
 1.91690 +    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 1.91691 +#endif
 1.91692 +    wsdAutoextInit;
 1.91693 +    sqlite3_mutex_enter(mutex);
 1.91694 +    for(i=0; i<wsdAutoext.nExt; i++){
 1.91695 +      if( wsdAutoext.aExt[i]==xInit ) break;
 1.91696 +    }
 1.91697 +    if( i==wsdAutoext.nExt ){
 1.91698 +      int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
 1.91699 +      void (**aNew)(void);
 1.91700 +      aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
 1.91701 +      if( aNew==0 ){
 1.91702 +        rc = SQLITE_NOMEM;
 1.91703 +      }else{
 1.91704 +        wsdAutoext.aExt = aNew;
 1.91705 +        wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
 1.91706 +        wsdAutoext.nExt++;
 1.91707 +      }
 1.91708 +    }
 1.91709 +    sqlite3_mutex_leave(mutex);
 1.91710 +    assert( (rc&0xff)==rc );
 1.91711 +    return rc;
 1.91712 +  }
 1.91713 +}
 1.91714 +
 1.91715 +/*
 1.91716 +** Reset the automatic extension loading mechanism.
 1.91717 +*/
 1.91718 +SQLITE_API void sqlite3_reset_auto_extension(void){
 1.91719 +#ifndef SQLITE_OMIT_AUTOINIT
 1.91720 +  if( sqlite3_initialize()==SQLITE_OK )
 1.91721 +#endif
 1.91722 +  {
 1.91723 +#if SQLITE_THREADSAFE
 1.91724 +    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 1.91725 +#endif
 1.91726 +    wsdAutoextInit;
 1.91727 +    sqlite3_mutex_enter(mutex);
 1.91728 +    sqlite3_free(wsdAutoext.aExt);
 1.91729 +    wsdAutoext.aExt = 0;
 1.91730 +    wsdAutoext.nExt = 0;
 1.91731 +    sqlite3_mutex_leave(mutex);
 1.91732 +  }
 1.91733 +}
 1.91734 +
 1.91735 +/*
 1.91736 +** Load all automatic extensions.
 1.91737 +**
 1.91738 +** If anything goes wrong, set an error in the database connection.
 1.91739 +*/
 1.91740 +SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
 1.91741 +  int i;
 1.91742 +  int go = 1;
 1.91743 +  int rc;
 1.91744 +  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 1.91745 +
 1.91746 +  wsdAutoextInit;
 1.91747 +  if( wsdAutoext.nExt==0 ){
 1.91748 +    /* Common case: early out without every having to acquire a mutex */
 1.91749 +    return;
 1.91750 +  }
 1.91751 +  for(i=0; go; i++){
 1.91752 +    char *zErrmsg;
 1.91753 +#if SQLITE_THREADSAFE
 1.91754 +    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 1.91755 +#endif
 1.91756 +    sqlite3_mutex_enter(mutex);
 1.91757 +    if( i>=wsdAutoext.nExt ){
 1.91758 +      xInit = 0;
 1.91759 +      go = 0;
 1.91760 +    }else{
 1.91761 +      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 1.91762 +              wsdAutoext.aExt[i];
 1.91763 +    }
 1.91764 +    sqlite3_mutex_leave(mutex);
 1.91765 +    zErrmsg = 0;
 1.91766 +    if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
 1.91767 +      sqlite3Error(db, rc,
 1.91768 +            "automatic extension loading failed: %s", zErrmsg);
 1.91769 +      go = 0;
 1.91770 +    }
 1.91771 +    sqlite3_free(zErrmsg);
 1.91772 +  }
 1.91773 +}
 1.91774 +
 1.91775 +/************** End of loadext.c *********************************************/
 1.91776 +/************** Begin file pragma.c ******************************************/
 1.91777 +/*
 1.91778 +** 2003 April 6
 1.91779 +**
 1.91780 +** The author disclaims copyright to this source code.  In place of
 1.91781 +** a legal notice, here is a blessing:
 1.91782 +**
 1.91783 +**    May you do good and not evil.
 1.91784 +**    May you find forgiveness for yourself and forgive others.
 1.91785 +**    May you share freely, never taking more than you give.
 1.91786 +**
 1.91787 +*************************************************************************
 1.91788 +** This file contains code used to implement the PRAGMA command.
 1.91789 +*/
 1.91790 +
 1.91791 +/*
 1.91792 +** Interpret the given string as a safety level.  Return 0 for OFF,
 1.91793 +** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
 1.91794 +** unrecognized string argument.  The FULL option is disallowed
 1.91795 +** if the omitFull parameter it 1.
 1.91796 +**
 1.91797 +** Note that the values returned are one less that the values that
 1.91798 +** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
 1.91799 +** to support legacy SQL code.  The safety level used to be boolean
 1.91800 +** and older scripts may have used numbers 0 for OFF and 1 for ON.
 1.91801 +*/
 1.91802 +static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
 1.91803 +                             /* 123456789 123456789 */
 1.91804 +  static const char zText[] = "onoffalseyestruefull";
 1.91805 +  static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
 1.91806 +  static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
 1.91807 +  static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
 1.91808 +  int i, n;
 1.91809 +  if( sqlite3Isdigit(*z) ){
 1.91810 +    return (u8)sqlite3Atoi(z);
 1.91811 +  }
 1.91812 +  n = sqlite3Strlen30(z);
 1.91813 +  for(i=0; i<ArraySize(iLength)-omitFull; i++){
 1.91814 +    if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
 1.91815 +      return iValue[i];
 1.91816 +    }
 1.91817 +  }
 1.91818 +  return dflt;
 1.91819 +}
 1.91820 +
 1.91821 +/*
 1.91822 +** Interpret the given string as a boolean value.
 1.91823 +*/
 1.91824 +SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, int dflt){
 1.91825 +  return getSafetyLevel(z,1,dflt)!=0;
 1.91826 +}
 1.91827 +
 1.91828 +/* The sqlite3GetBoolean() function is used by other modules but the
 1.91829 +** remainder of this file is specific to PRAGMA processing.  So omit
 1.91830 +** the rest of the file if PRAGMAs are omitted from the build.
 1.91831 +*/
 1.91832 +#if !defined(SQLITE_OMIT_PRAGMA)
 1.91833 +
 1.91834 +/*
 1.91835 +** Interpret the given string as a locking mode value.
 1.91836 +*/
 1.91837 +static int getLockingMode(const char *z){
 1.91838 +  if( z ){
 1.91839 +    if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
 1.91840 +    if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
 1.91841 +  }
 1.91842 +  return PAGER_LOCKINGMODE_QUERY;
 1.91843 +}
 1.91844 +
 1.91845 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.91846 +/*
 1.91847 +** Interpret the given string as an auto-vacuum mode value.
 1.91848 +**
 1.91849 +** The following strings, "none", "full" and "incremental" are 
 1.91850 +** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
 1.91851 +*/
 1.91852 +static int getAutoVacuum(const char *z){
 1.91853 +  int i;
 1.91854 +  if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
 1.91855 +  if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
 1.91856 +  if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
 1.91857 +  i = sqlite3Atoi(z);
 1.91858 +  return (u8)((i>=0&&i<=2)?i:0);
 1.91859 +}
 1.91860 +#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
 1.91861 +
 1.91862 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
 1.91863 +/*
 1.91864 +** Interpret the given string as a temp db location. Return 1 for file
 1.91865 +** backed temporary databases, 2 for the Red-Black tree in memory database
 1.91866 +** and 0 to use the compile-time default.
 1.91867 +*/
 1.91868 +static int getTempStore(const char *z){
 1.91869 +  if( z[0]>='0' && z[0]<='2' ){
 1.91870 +    return z[0] - '0';
 1.91871 +  }else if( sqlite3StrICmp(z, "file")==0 ){
 1.91872 +    return 1;
 1.91873 +  }else if( sqlite3StrICmp(z, "memory")==0 ){
 1.91874 +    return 2;
 1.91875 +  }else{
 1.91876 +    return 0;
 1.91877 +  }
 1.91878 +}
 1.91879 +#endif /* SQLITE_PAGER_PRAGMAS */
 1.91880 +
 1.91881 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
 1.91882 +/*
 1.91883 +** Invalidate temp storage, either when the temp storage is changed
 1.91884 +** from default, or when 'file' and the temp_store_directory has changed
 1.91885 +*/
 1.91886 +static int invalidateTempStorage(Parse *pParse){
 1.91887 +  sqlite3 *db = pParse->db;
 1.91888 +  if( db->aDb[1].pBt!=0 ){
 1.91889 +    if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
 1.91890 +      sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
 1.91891 +        "from within a transaction");
 1.91892 +      return SQLITE_ERROR;
 1.91893 +    }
 1.91894 +    sqlite3BtreeClose(db->aDb[1].pBt);
 1.91895 +    db->aDb[1].pBt = 0;
 1.91896 +    sqlite3ResetAllSchemasOfConnection(db);
 1.91897 +  }
 1.91898 +  return SQLITE_OK;
 1.91899 +}
 1.91900 +#endif /* SQLITE_PAGER_PRAGMAS */
 1.91901 +
 1.91902 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
 1.91903 +/*
 1.91904 +** If the TEMP database is open, close it and mark the database schema
 1.91905 +** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
 1.91906 +** or DEFAULT_TEMP_STORE pragmas.
 1.91907 +*/
 1.91908 +static int changeTempStorage(Parse *pParse, const char *zStorageType){
 1.91909 +  int ts = getTempStore(zStorageType);
 1.91910 +  sqlite3 *db = pParse->db;
 1.91911 +  if( db->temp_store==ts ) return SQLITE_OK;
 1.91912 +  if( invalidateTempStorage( pParse ) != SQLITE_OK ){
 1.91913 +    return SQLITE_ERROR;
 1.91914 +  }
 1.91915 +  db->temp_store = (u8)ts;
 1.91916 +  return SQLITE_OK;
 1.91917 +}
 1.91918 +#endif /* SQLITE_PAGER_PRAGMAS */
 1.91919 +
 1.91920 +/*
 1.91921 +** Generate code to return a single integer value.
 1.91922 +*/
 1.91923 +static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
 1.91924 +  Vdbe *v = sqlite3GetVdbe(pParse);
 1.91925 +  int mem = ++pParse->nMem;
 1.91926 +  i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
 1.91927 +  if( pI64 ){
 1.91928 +    memcpy(pI64, &value, sizeof(value));
 1.91929 +  }
 1.91930 +  sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
 1.91931 +  sqlite3VdbeSetNumCols(v, 1);
 1.91932 +  sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
 1.91933 +  sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
 1.91934 +}
 1.91935 +
 1.91936 +#ifndef SQLITE_OMIT_FLAG_PRAGMAS
 1.91937 +/*
 1.91938 +** Check to see if zRight and zLeft refer to a pragma that queries
 1.91939 +** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
 1.91940 +** Also, implement the pragma.
 1.91941 +*/
 1.91942 +static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
 1.91943 +  static const struct sPragmaType {
 1.91944 +    const char *zName;  /* Name of the pragma */
 1.91945 +    int mask;           /* Mask for the db->flags value */
 1.91946 +  } aPragma[] = {
 1.91947 +    { "full_column_names",        SQLITE_FullColNames  },
 1.91948 +    { "short_column_names",       SQLITE_ShortColNames },
 1.91949 +    { "count_changes",            SQLITE_CountRows     },
 1.91950 +    { "empty_result_callbacks",   SQLITE_NullCallback  },
 1.91951 +    { "legacy_file_format",       SQLITE_LegacyFileFmt },
 1.91952 +    { "fullfsync",                SQLITE_FullFSync     },
 1.91953 +    { "checkpoint_fullfsync",     SQLITE_CkptFullFSync },
 1.91954 +    { "reverse_unordered_selects", SQLITE_ReverseOrder  },
 1.91955 +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 1.91956 +    { "automatic_index",          SQLITE_AutoIndex     },
 1.91957 +#endif
 1.91958 +#ifdef SQLITE_DEBUG
 1.91959 +    { "sql_trace",                SQLITE_SqlTrace      },
 1.91960 +    { "vdbe_listing",             SQLITE_VdbeListing   },
 1.91961 +    { "vdbe_trace",               SQLITE_VdbeTrace     },
 1.91962 +#endif
 1.91963 +#ifndef SQLITE_OMIT_CHECK
 1.91964 +    { "ignore_check_constraints", SQLITE_IgnoreChecks  },
 1.91965 +#endif
 1.91966 +    /* The following is VERY experimental */
 1.91967 +    { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
 1.91968 +
 1.91969 +    /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
 1.91970 +    ** flag if there are any active statements. */
 1.91971 +    { "read_uncommitted",         SQLITE_ReadUncommitted },
 1.91972 +    { "recursive_triggers",       SQLITE_RecTriggers },
 1.91973 +
 1.91974 +    /* This flag may only be set if both foreign-key and trigger support
 1.91975 +    ** are present in the build.  */
 1.91976 +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 1.91977 +    { "foreign_keys",             SQLITE_ForeignKeys },
 1.91978 +#endif
 1.91979 +  };
 1.91980 +  int i;
 1.91981 +  const struct sPragmaType *p;
 1.91982 +  for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
 1.91983 +    if( sqlite3StrICmp(zLeft, p->zName)==0 ){
 1.91984 +      sqlite3 *db = pParse->db;
 1.91985 +      Vdbe *v;
 1.91986 +      v = sqlite3GetVdbe(pParse);
 1.91987 +      assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
 1.91988 +      if( ALWAYS(v) ){
 1.91989 +        if( zRight==0 ){
 1.91990 +          returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
 1.91991 +        }else{
 1.91992 +          int mask = p->mask;          /* Mask of bits to set or clear. */
 1.91993 +          if( db->autoCommit==0 ){
 1.91994 +            /* Foreign key support may not be enabled or disabled while not
 1.91995 +            ** in auto-commit mode.  */
 1.91996 +            mask &= ~(SQLITE_ForeignKeys);
 1.91997 +          }
 1.91998 +
 1.91999 +          if( sqlite3GetBoolean(zRight, 0) ){
 1.92000 +            db->flags |= mask;
 1.92001 +          }else{
 1.92002 +            db->flags &= ~mask;
 1.92003 +          }
 1.92004 +
 1.92005 +          /* Many of the flag-pragmas modify the code generated by the SQL 
 1.92006 +          ** compiler (eg. count_changes). So add an opcode to expire all
 1.92007 +          ** compiled SQL statements after modifying a pragma value.
 1.92008 +          */
 1.92009 +          sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
 1.92010 +        }
 1.92011 +      }
 1.92012 +
 1.92013 +      return 1;
 1.92014 +    }
 1.92015 +  }
 1.92016 +  return 0;
 1.92017 +}
 1.92018 +#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
 1.92019 +
 1.92020 +/*
 1.92021 +** Return a human-readable name for a constraint resolution action.
 1.92022 +*/
 1.92023 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.92024 +static const char *actionName(u8 action){
 1.92025 +  const char *zName;
 1.92026 +  switch( action ){
 1.92027 +    case OE_SetNull:  zName = "SET NULL";        break;
 1.92028 +    case OE_SetDflt:  zName = "SET DEFAULT";     break;
 1.92029 +    case OE_Cascade:  zName = "CASCADE";         break;
 1.92030 +    case OE_Restrict: zName = "RESTRICT";        break;
 1.92031 +    default:          zName = "NO ACTION";  
 1.92032 +                      assert( action==OE_None ); break;
 1.92033 +  }
 1.92034 +  return zName;
 1.92035 +}
 1.92036 +#endif
 1.92037 +
 1.92038 +
 1.92039 +/*
 1.92040 +** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
 1.92041 +** defined in pager.h. This function returns the associated lowercase
 1.92042 +** journal-mode name.
 1.92043 +*/
 1.92044 +SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
 1.92045 +  static char * const azModeName[] = {
 1.92046 +    "delete", "persist", "off", "truncate", "memory"
 1.92047 +#ifndef SQLITE_OMIT_WAL
 1.92048 +     , "wal"
 1.92049 +#endif
 1.92050 +  };
 1.92051 +  assert( PAGER_JOURNALMODE_DELETE==0 );
 1.92052 +  assert( PAGER_JOURNALMODE_PERSIST==1 );
 1.92053 +  assert( PAGER_JOURNALMODE_OFF==2 );
 1.92054 +  assert( PAGER_JOURNALMODE_TRUNCATE==3 );
 1.92055 +  assert( PAGER_JOURNALMODE_MEMORY==4 );
 1.92056 +  assert( PAGER_JOURNALMODE_WAL==5 );
 1.92057 +  assert( eMode>=0 && eMode<=ArraySize(azModeName) );
 1.92058 +
 1.92059 +  if( eMode==ArraySize(azModeName) ) return 0;
 1.92060 +  return azModeName[eMode];
 1.92061 +}
 1.92062 +
 1.92063 +/*
 1.92064 +** Process a pragma statement.  
 1.92065 +**
 1.92066 +** Pragmas are of this form:
 1.92067 +**
 1.92068 +**      PRAGMA [database.]id [= value]
 1.92069 +**
 1.92070 +** The identifier might also be a string.  The value is a string, and
 1.92071 +** identifier, or a number.  If minusFlag is true, then the value is
 1.92072 +** a number that was preceded by a minus sign.
 1.92073 +**
 1.92074 +** If the left side is "database.id" then pId1 is the database name
 1.92075 +** and pId2 is the id.  If the left side is just "id" then pId1 is the
 1.92076 +** id and pId2 is any empty string.
 1.92077 +*/
 1.92078 +SQLITE_PRIVATE void sqlite3Pragma(
 1.92079 +  Parse *pParse, 
 1.92080 +  Token *pId1,        /* First part of [database.]id field */
 1.92081 +  Token *pId2,        /* Second part of [database.]id field, or NULL */
 1.92082 +  Token *pValue,      /* Token for <value>, or NULL */
 1.92083 +  int minusFlag       /* True if a '-' sign preceded <value> */
 1.92084 +){
 1.92085 +  char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
 1.92086 +  char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
 1.92087 +  const char *zDb = 0;   /* The database name */
 1.92088 +  Token *pId;            /* Pointer to <id> token */
 1.92089 +  int iDb;               /* Database index for <database> */
 1.92090 +  char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
 1.92091 +  int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
 1.92092 +  sqlite3 *db = pParse->db;    /* The database connection */
 1.92093 +  Db *pDb;                     /* The specific database being pragmaed */
 1.92094 +  Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);  /* Prepared statement */
 1.92095 +
 1.92096 +  if( v==0 ) return;
 1.92097 +  sqlite3VdbeRunOnlyOnce(v);
 1.92098 +  pParse->nMem = 2;
 1.92099 +
 1.92100 +  /* Interpret the [database.] part of the pragma statement. iDb is the
 1.92101 +  ** index of the database this pragma is being applied to in db.aDb[]. */
 1.92102 +  iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
 1.92103 +  if( iDb<0 ) return;
 1.92104 +  pDb = &db->aDb[iDb];
 1.92105 +
 1.92106 +  /* If the temp database has been explicitly named as part of the 
 1.92107 +  ** pragma, make sure it is open. 
 1.92108 +  */
 1.92109 +  if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
 1.92110 +    return;
 1.92111 +  }
 1.92112 +
 1.92113 +  zLeft = sqlite3NameFromToken(db, pId);
 1.92114 +  if( !zLeft ) return;
 1.92115 +  if( minusFlag ){
 1.92116 +    zRight = sqlite3MPrintf(db, "-%T", pValue);
 1.92117 +  }else{
 1.92118 +    zRight = sqlite3NameFromToken(db, pValue);
 1.92119 +  }
 1.92120 +
 1.92121 +  assert( pId2 );
 1.92122 +  zDb = pId2->n>0 ? pDb->zName : 0;
 1.92123 +  if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
 1.92124 +    goto pragma_out;
 1.92125 +  }
 1.92126 +
 1.92127 +  /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
 1.92128 +  ** connection.  If it returns SQLITE_OK, then assume that the VFS
 1.92129 +  ** handled the pragma and generate a no-op prepared statement.
 1.92130 +  */
 1.92131 +  aFcntl[0] = 0;
 1.92132 +  aFcntl[1] = zLeft;
 1.92133 +  aFcntl[2] = zRight;
 1.92134 +  aFcntl[3] = 0;
 1.92135 +  db->busyHandler.nBusy = 0;
 1.92136 +  rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
 1.92137 +  if( rc==SQLITE_OK ){
 1.92138 +    if( aFcntl[0] ){
 1.92139 +      int mem = ++pParse->nMem;
 1.92140 +      sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
 1.92141 +      sqlite3VdbeSetNumCols(v, 1);
 1.92142 +      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
 1.92143 +      sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
 1.92144 +      sqlite3_free(aFcntl[0]);
 1.92145 +    }
 1.92146 +  }else if( rc!=SQLITE_NOTFOUND ){
 1.92147 +    if( aFcntl[0] ){
 1.92148 +      sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
 1.92149 +      sqlite3_free(aFcntl[0]);
 1.92150 +    }
 1.92151 +    pParse->nErr++;
 1.92152 +    pParse->rc = rc;
 1.92153 +  }else
 1.92154 +                            
 1.92155 + 
 1.92156 +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
 1.92157 +  /*
 1.92158 +  **  PRAGMA [database.]default_cache_size
 1.92159 +  **  PRAGMA [database.]default_cache_size=N
 1.92160 +  **
 1.92161 +  ** The first form reports the current persistent setting for the
 1.92162 +  ** page cache size.  The value returned is the maximum number of
 1.92163 +  ** pages in the page cache.  The second form sets both the current
 1.92164 +  ** page cache size value and the persistent page cache size value
 1.92165 +  ** stored in the database file.
 1.92166 +  **
 1.92167 +  ** Older versions of SQLite would set the default cache size to a
 1.92168 +  ** negative number to indicate synchronous=OFF.  These days, synchronous
 1.92169 +  ** is always on by default regardless of the sign of the default cache
 1.92170 +  ** size.  But continue to take the absolute value of the default cache
 1.92171 +  ** size of historical compatibility.
 1.92172 +  */
 1.92173 +  if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
 1.92174 +    static const VdbeOpList getCacheSize[] = {
 1.92175 +      { OP_Transaction, 0, 0,        0},                         /* 0 */
 1.92176 +      { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
 1.92177 +      { OP_IfPos,       1, 7,        0},
 1.92178 +      { OP_Integer,     0, 2,        0},
 1.92179 +      { OP_Subtract,    1, 2,        1},
 1.92180 +      { OP_IfPos,       1, 7,        0},
 1.92181 +      { OP_Integer,     0, 1,        0},                         /* 6 */
 1.92182 +      { OP_ResultRow,   1, 1,        0},
 1.92183 +    };
 1.92184 +    int addr;
 1.92185 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92186 +    sqlite3VdbeUsesBtree(v, iDb);
 1.92187 +    if( !zRight ){
 1.92188 +      sqlite3VdbeSetNumCols(v, 1);
 1.92189 +      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
 1.92190 +      pParse->nMem += 2;
 1.92191 +      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
 1.92192 +      sqlite3VdbeChangeP1(v, addr, iDb);
 1.92193 +      sqlite3VdbeChangeP1(v, addr+1, iDb);
 1.92194 +      sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
 1.92195 +    }else{
 1.92196 +      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
 1.92197 +      sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.92198 +      sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
 1.92199 +      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
 1.92200 +      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.92201 +      pDb->pSchema->cache_size = size;
 1.92202 +      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 1.92203 +    }
 1.92204 +  }else
 1.92205 +#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
 1.92206 +
 1.92207 +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 1.92208 +  /*
 1.92209 +  **  PRAGMA [database.]page_size
 1.92210 +  **  PRAGMA [database.]page_size=N
 1.92211 +  **
 1.92212 +  ** The first form reports the current setting for the
 1.92213 +  ** database page size in bytes.  The second form sets the
 1.92214 +  ** database page size value.  The value can only be set if
 1.92215 +  ** the database has not yet been created.
 1.92216 +  */
 1.92217 +  if( sqlite3StrICmp(zLeft,"page_size")==0 ){
 1.92218 +    Btree *pBt = pDb->pBt;
 1.92219 +    assert( pBt!=0 );
 1.92220 +    if( !zRight ){
 1.92221 +      int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
 1.92222 +      returnSingleInt(pParse, "page_size", size);
 1.92223 +    }else{
 1.92224 +      /* Malloc may fail when setting the page-size, as there is an internal
 1.92225 +      ** buffer that the pager module resizes using sqlite3_realloc().
 1.92226 +      */
 1.92227 +      db->nextPagesize = sqlite3Atoi(zRight);
 1.92228 +      if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
 1.92229 +        db->mallocFailed = 1;
 1.92230 +      }
 1.92231 +    }
 1.92232 +  }else
 1.92233 +
 1.92234 +  /*
 1.92235 +  **  PRAGMA [database.]secure_delete
 1.92236 +  **  PRAGMA [database.]secure_delete=ON/OFF
 1.92237 +  **
 1.92238 +  ** The first form reports the current setting for the
 1.92239 +  ** secure_delete flag.  The second form changes the secure_delete
 1.92240 +  ** flag setting and reports thenew value.
 1.92241 +  */
 1.92242 +  if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
 1.92243 +    Btree *pBt = pDb->pBt;
 1.92244 +    int b = -1;
 1.92245 +    assert( pBt!=0 );
 1.92246 +    if( zRight ){
 1.92247 +      b = sqlite3GetBoolean(zRight, 0);
 1.92248 +    }
 1.92249 +    if( pId2->n==0 && b>=0 ){
 1.92250 +      int ii;
 1.92251 +      for(ii=0; ii<db->nDb; ii++){
 1.92252 +        sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
 1.92253 +      }
 1.92254 +    }
 1.92255 +    b = sqlite3BtreeSecureDelete(pBt, b);
 1.92256 +    returnSingleInt(pParse, "secure_delete", b);
 1.92257 +  }else
 1.92258 +
 1.92259 +  /*
 1.92260 +  **  PRAGMA [database.]max_page_count
 1.92261 +  **  PRAGMA [database.]max_page_count=N
 1.92262 +  **
 1.92263 +  ** The first form reports the current setting for the
 1.92264 +  ** maximum number of pages in the database file.  The 
 1.92265 +  ** second form attempts to change this setting.  Both
 1.92266 +  ** forms return the current setting.
 1.92267 +  **
 1.92268 +  ** The absolute value of N is used.  This is undocumented and might
 1.92269 +  ** change.  The only purpose is to provide an easy way to test
 1.92270 +  ** the sqlite3AbsInt32() function.
 1.92271 +  **
 1.92272 +  **  PRAGMA [database.]page_count
 1.92273 +  **
 1.92274 +  ** Return the number of pages in the specified database.
 1.92275 +  */
 1.92276 +  if( sqlite3StrICmp(zLeft,"page_count")==0
 1.92277 +   || sqlite3StrICmp(zLeft,"max_page_count")==0
 1.92278 +  ){
 1.92279 +    int iReg;
 1.92280 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92281 +    sqlite3CodeVerifySchema(pParse, iDb);
 1.92282 +    iReg = ++pParse->nMem;
 1.92283 +    if( sqlite3Tolower(zLeft[0])=='p' ){
 1.92284 +      sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
 1.92285 +    }else{
 1.92286 +      sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, 
 1.92287 +                        sqlite3AbsInt32(sqlite3Atoi(zRight)));
 1.92288 +    }
 1.92289 +    sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
 1.92290 +    sqlite3VdbeSetNumCols(v, 1);
 1.92291 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
 1.92292 +  }else
 1.92293 +
 1.92294 +  /*
 1.92295 +  **  PRAGMA [database.]locking_mode
 1.92296 +  **  PRAGMA [database.]locking_mode = (normal|exclusive)
 1.92297 +  */
 1.92298 +  if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
 1.92299 +    const char *zRet = "normal";
 1.92300 +    int eMode = getLockingMode(zRight);
 1.92301 +
 1.92302 +    if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
 1.92303 +      /* Simple "PRAGMA locking_mode;" statement. This is a query for
 1.92304 +      ** the current default locking mode (which may be different to
 1.92305 +      ** the locking-mode of the main database).
 1.92306 +      */
 1.92307 +      eMode = db->dfltLockMode;
 1.92308 +    }else{
 1.92309 +      Pager *pPager;
 1.92310 +      if( pId2->n==0 ){
 1.92311 +        /* This indicates that no database name was specified as part
 1.92312 +        ** of the PRAGMA command. In this case the locking-mode must be
 1.92313 +        ** set on all attached databases, as well as the main db file.
 1.92314 +        **
 1.92315 +        ** Also, the sqlite3.dfltLockMode variable is set so that
 1.92316 +        ** any subsequently attached databases also use the specified
 1.92317 +        ** locking mode.
 1.92318 +        */
 1.92319 +        int ii;
 1.92320 +        assert(pDb==&db->aDb[0]);
 1.92321 +        for(ii=2; ii<db->nDb; ii++){
 1.92322 +          pPager = sqlite3BtreePager(db->aDb[ii].pBt);
 1.92323 +          sqlite3PagerLockingMode(pPager, eMode);
 1.92324 +        }
 1.92325 +        db->dfltLockMode = (u8)eMode;
 1.92326 +      }
 1.92327 +      pPager = sqlite3BtreePager(pDb->pBt);
 1.92328 +      eMode = sqlite3PagerLockingMode(pPager, eMode);
 1.92329 +    }
 1.92330 +
 1.92331 +    assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
 1.92332 +    if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
 1.92333 +      zRet = "exclusive";
 1.92334 +    }
 1.92335 +    sqlite3VdbeSetNumCols(v, 1);
 1.92336 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
 1.92337 +    sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
 1.92338 +    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 1.92339 +  }else
 1.92340 +
 1.92341 +  /*
 1.92342 +  **  PRAGMA [database.]journal_mode
 1.92343 +  **  PRAGMA [database.]journal_mode =
 1.92344 +  **                      (delete|persist|off|truncate|memory|wal|off)
 1.92345 +  */
 1.92346 +  if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
 1.92347 +    int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
 1.92348 +    int ii;           /* Loop counter */
 1.92349 +
 1.92350 +    /* Force the schema to be loaded on all databases.  This causes all
 1.92351 +    ** database files to be opened and the journal_modes set.  This is
 1.92352 +    ** necessary because subsequent processing must know if the databases
 1.92353 +    ** are in WAL mode. */
 1.92354 +    if( sqlite3ReadSchema(pParse) ){
 1.92355 +      goto pragma_out;
 1.92356 +    }
 1.92357 +
 1.92358 +    sqlite3VdbeSetNumCols(v, 1);
 1.92359 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
 1.92360 +
 1.92361 +    if( zRight==0 ){
 1.92362 +      /* If there is no "=MODE" part of the pragma, do a query for the
 1.92363 +      ** current mode */
 1.92364 +      eMode = PAGER_JOURNALMODE_QUERY;
 1.92365 +    }else{
 1.92366 +      const char *zMode;
 1.92367 +      int n = sqlite3Strlen30(zRight);
 1.92368 +      for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
 1.92369 +        if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
 1.92370 +      }
 1.92371 +      if( !zMode ){
 1.92372 +        /* If the "=MODE" part does not match any known journal mode,
 1.92373 +        ** then do a query */
 1.92374 +        eMode = PAGER_JOURNALMODE_QUERY;
 1.92375 +      }
 1.92376 +    }
 1.92377 +    if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
 1.92378 +      /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
 1.92379 +      iDb = 0;
 1.92380 +      pId2->n = 1;
 1.92381 +    }
 1.92382 +    for(ii=db->nDb-1; ii>=0; ii--){
 1.92383 +      if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
 1.92384 +        sqlite3VdbeUsesBtree(v, ii);
 1.92385 +        sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
 1.92386 +      }
 1.92387 +    }
 1.92388 +    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 1.92389 +  }else
 1.92390 +
 1.92391 +  /*
 1.92392 +  **  PRAGMA [database.]journal_size_limit
 1.92393 +  **  PRAGMA [database.]journal_size_limit=N
 1.92394 +  **
 1.92395 +  ** Get or set the size limit on rollback journal files.
 1.92396 +  */
 1.92397 +  if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
 1.92398 +    Pager *pPager = sqlite3BtreePager(pDb->pBt);
 1.92399 +    i64 iLimit = -2;
 1.92400 +    if( zRight ){
 1.92401 +      sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
 1.92402 +      if( iLimit<-1 ) iLimit = -1;
 1.92403 +    }
 1.92404 +    iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
 1.92405 +    returnSingleInt(pParse, "journal_size_limit", iLimit);
 1.92406 +  }else
 1.92407 +
 1.92408 +#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
 1.92409 +
 1.92410 +  /*
 1.92411 +  **  PRAGMA [database.]auto_vacuum
 1.92412 +  **  PRAGMA [database.]auto_vacuum=N
 1.92413 +  **
 1.92414 +  ** Get or set the value of the database 'auto-vacuum' parameter.
 1.92415 +  ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
 1.92416 +  */
 1.92417 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.92418 +  if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
 1.92419 +    Btree *pBt = pDb->pBt;
 1.92420 +    assert( pBt!=0 );
 1.92421 +    if( sqlite3ReadSchema(pParse) ){
 1.92422 +      goto pragma_out;
 1.92423 +    }
 1.92424 +    if( !zRight ){
 1.92425 +      int auto_vacuum;
 1.92426 +      if( ALWAYS(pBt) ){
 1.92427 +         auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
 1.92428 +      }else{
 1.92429 +         auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
 1.92430 +      }
 1.92431 +      returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
 1.92432 +    }else{
 1.92433 +      int eAuto = getAutoVacuum(zRight);
 1.92434 +      assert( eAuto>=0 && eAuto<=2 );
 1.92435 +      db->nextAutovac = (u8)eAuto;
 1.92436 +      if( ALWAYS(eAuto>=0) ){
 1.92437 +        /* Call SetAutoVacuum() to set initialize the internal auto and
 1.92438 +        ** incr-vacuum flags. This is required in case this connection
 1.92439 +        ** creates the database file. It is important that it is created
 1.92440 +        ** as an auto-vacuum capable db.
 1.92441 +        */
 1.92442 +        rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
 1.92443 +        if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
 1.92444 +          /* When setting the auto_vacuum mode to either "full" or 
 1.92445 +          ** "incremental", write the value of meta[6] in the database
 1.92446 +          ** file. Before writing to meta[6], check that meta[3] indicates
 1.92447 +          ** that this really is an auto-vacuum capable database.
 1.92448 +          */
 1.92449 +          static const VdbeOpList setMeta6[] = {
 1.92450 +            { OP_Transaction,    0,         1,                 0},    /* 0 */
 1.92451 +            { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
 1.92452 +            { OP_If,             1,         0,                 0},    /* 2 */
 1.92453 +            { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
 1.92454 +            { OP_Integer,        0,         1,                 0},    /* 4 */
 1.92455 +            { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
 1.92456 +          };
 1.92457 +          int iAddr;
 1.92458 +          iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
 1.92459 +          sqlite3VdbeChangeP1(v, iAddr, iDb);
 1.92460 +          sqlite3VdbeChangeP1(v, iAddr+1, iDb);
 1.92461 +          sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
 1.92462 +          sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
 1.92463 +          sqlite3VdbeChangeP1(v, iAddr+5, iDb);
 1.92464 +          sqlite3VdbeUsesBtree(v, iDb);
 1.92465 +        }
 1.92466 +      }
 1.92467 +    }
 1.92468 +  }else
 1.92469 +#endif
 1.92470 +
 1.92471 +  /*
 1.92472 +  **  PRAGMA [database.]incremental_vacuum(N)
 1.92473 +  **
 1.92474 +  ** Do N steps of incremental vacuuming on a database.
 1.92475 +  */
 1.92476 +#ifndef SQLITE_OMIT_AUTOVACUUM
 1.92477 +  if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
 1.92478 +    int iLimit, addr;
 1.92479 +    if( sqlite3ReadSchema(pParse) ){
 1.92480 +      goto pragma_out;
 1.92481 +    }
 1.92482 +    if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
 1.92483 +      iLimit = 0x7fffffff;
 1.92484 +    }
 1.92485 +    sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.92486 +    sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
 1.92487 +    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
 1.92488 +    sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
 1.92489 +    sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
 1.92490 +    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
 1.92491 +    sqlite3VdbeJumpHere(v, addr);
 1.92492 +  }else
 1.92493 +#endif
 1.92494 +
 1.92495 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
 1.92496 +  /*
 1.92497 +  **  PRAGMA [database.]cache_size
 1.92498 +  **  PRAGMA [database.]cache_size=N
 1.92499 +  **
 1.92500 +  ** The first form reports the current local setting for the
 1.92501 +  ** page cache size. The second form sets the local
 1.92502 +  ** page cache size value.  If N is positive then that is the
 1.92503 +  ** number of pages in the cache.  If N is negative, then the
 1.92504 +  ** number of pages is adjusted so that the cache uses -N kibibytes
 1.92505 +  ** of memory.
 1.92506 +  */
 1.92507 +  if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
 1.92508 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92509 +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.92510 +    if( !zRight ){
 1.92511 +      returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
 1.92512 +    }else{
 1.92513 +      int size = sqlite3Atoi(zRight);
 1.92514 +      pDb->pSchema->cache_size = size;
 1.92515 +      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 1.92516 +    }
 1.92517 +  }else
 1.92518 +
 1.92519 +  /*
 1.92520 +  **   PRAGMA temp_store
 1.92521 +  **   PRAGMA temp_store = "default"|"memory"|"file"
 1.92522 +  **
 1.92523 +  ** Return or set the local value of the temp_store flag.  Changing
 1.92524 +  ** the local value does not make changes to the disk file and the default
 1.92525 +  ** value will be restored the next time the database is opened.
 1.92526 +  **
 1.92527 +  ** Note that it is possible for the library compile-time options to
 1.92528 +  ** override this setting
 1.92529 +  */
 1.92530 +  if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
 1.92531 +    if( !zRight ){
 1.92532 +      returnSingleInt(pParse, "temp_store", db->temp_store);
 1.92533 +    }else{
 1.92534 +      changeTempStorage(pParse, zRight);
 1.92535 +    }
 1.92536 +  }else
 1.92537 +
 1.92538 +  /*
 1.92539 +  **   PRAGMA temp_store_directory
 1.92540 +  **   PRAGMA temp_store_directory = ""|"directory_name"
 1.92541 +  **
 1.92542 +  ** Return or set the local value of the temp_store_directory flag.  Changing
 1.92543 +  ** the value sets a specific directory to be used for temporary files.
 1.92544 +  ** Setting to a null string reverts to the default temporary directory search.
 1.92545 +  ** If temporary directory is changed, then invalidateTempStorage.
 1.92546 +  **
 1.92547 +  */
 1.92548 +  if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
 1.92549 +    if( !zRight ){
 1.92550 +      if( sqlite3_temp_directory ){
 1.92551 +        sqlite3VdbeSetNumCols(v, 1);
 1.92552 +        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 1.92553 +            "temp_store_directory", SQLITE_STATIC);
 1.92554 +        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
 1.92555 +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 1.92556 +      }
 1.92557 +    }else{
 1.92558 +#ifndef SQLITE_OMIT_WSD
 1.92559 +      if( zRight[0] ){
 1.92560 +        int res;
 1.92561 +        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
 1.92562 +        if( rc!=SQLITE_OK || res==0 ){
 1.92563 +          sqlite3ErrorMsg(pParse, "not a writable directory");
 1.92564 +          goto pragma_out;
 1.92565 +        }
 1.92566 +      }
 1.92567 +      if( SQLITE_TEMP_STORE==0
 1.92568 +       || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
 1.92569 +       || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
 1.92570 +      ){
 1.92571 +        invalidateTempStorage(pParse);
 1.92572 +      }
 1.92573 +      sqlite3_free(sqlite3_temp_directory);
 1.92574 +      if( zRight[0] ){
 1.92575 +        sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
 1.92576 +      }else{
 1.92577 +        sqlite3_temp_directory = 0;
 1.92578 +      }
 1.92579 +#endif /* SQLITE_OMIT_WSD */
 1.92580 +    }
 1.92581 +  }else
 1.92582 +
 1.92583 +#if SQLITE_OS_WIN
 1.92584 +  /*
 1.92585 +  **   PRAGMA data_store_directory
 1.92586 +  **   PRAGMA data_store_directory = ""|"directory_name"
 1.92587 +  **
 1.92588 +  ** Return or set the local value of the data_store_directory flag.  Changing
 1.92589 +  ** the value sets a specific directory to be used for database files that
 1.92590 +  ** were specified with a relative pathname.  Setting to a null string reverts
 1.92591 +  ** to the default database directory, which for database files specified with
 1.92592 +  ** a relative path will probably be based on the current directory for the
 1.92593 +  ** process.  Database file specified with an absolute path are not impacted
 1.92594 +  ** by this setting, regardless of its value.
 1.92595 +  **
 1.92596 +  */
 1.92597 +  if( sqlite3StrICmp(zLeft, "data_store_directory")==0 ){
 1.92598 +    if( !zRight ){
 1.92599 +      if( sqlite3_data_directory ){
 1.92600 +        sqlite3VdbeSetNumCols(v, 1);
 1.92601 +        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 1.92602 +            "data_store_directory", SQLITE_STATIC);
 1.92603 +        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
 1.92604 +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 1.92605 +      }
 1.92606 +    }else{
 1.92607 +#ifndef SQLITE_OMIT_WSD
 1.92608 +      if( zRight[0] ){
 1.92609 +        int res;
 1.92610 +        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
 1.92611 +        if( rc!=SQLITE_OK || res==0 ){
 1.92612 +          sqlite3ErrorMsg(pParse, "not a writable directory");
 1.92613 +          goto pragma_out;
 1.92614 +        }
 1.92615 +      }
 1.92616 +      sqlite3_free(sqlite3_data_directory);
 1.92617 +      if( zRight[0] ){
 1.92618 +        sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
 1.92619 +      }else{
 1.92620 +        sqlite3_data_directory = 0;
 1.92621 +      }
 1.92622 +#endif /* SQLITE_OMIT_WSD */
 1.92623 +    }
 1.92624 +  }else
 1.92625 +#endif
 1.92626 +
 1.92627 +#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
 1.92628 +#  if defined(__APPLE__)
 1.92629 +#    define SQLITE_ENABLE_LOCKING_STYLE 1
 1.92630 +#  else
 1.92631 +#    define SQLITE_ENABLE_LOCKING_STYLE 0
 1.92632 +#  endif
 1.92633 +#endif
 1.92634 +#if SQLITE_ENABLE_LOCKING_STYLE
 1.92635 +  /*
 1.92636 +   **   PRAGMA [database.]lock_proxy_file
 1.92637 +   **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
 1.92638 +   **
 1.92639 +   ** Return or set the value of the lock_proxy_file flag.  Changing
 1.92640 +   ** the value sets a specific file to be used for database access locks.
 1.92641 +   **
 1.92642 +   */
 1.92643 +  if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
 1.92644 +    if( !zRight ){
 1.92645 +      Pager *pPager = sqlite3BtreePager(pDb->pBt);
 1.92646 +      char *proxy_file_path = NULL;
 1.92647 +      sqlite3_file *pFile = sqlite3PagerFile(pPager);
 1.92648 +      sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 
 1.92649 +                           &proxy_file_path);
 1.92650 +      
 1.92651 +      if( proxy_file_path ){
 1.92652 +        sqlite3VdbeSetNumCols(v, 1);
 1.92653 +        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 1.92654 +                              "lock_proxy_file", SQLITE_STATIC);
 1.92655 +        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
 1.92656 +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 1.92657 +      }
 1.92658 +    }else{
 1.92659 +      Pager *pPager = sqlite3BtreePager(pDb->pBt);
 1.92660 +      sqlite3_file *pFile = sqlite3PagerFile(pPager);
 1.92661 +      int res;
 1.92662 +      if( zRight[0] ){
 1.92663 +        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
 1.92664 +                                     zRight);
 1.92665 +      } else {
 1.92666 +        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
 1.92667 +                                     NULL);
 1.92668 +      }
 1.92669 +      if( res!=SQLITE_OK ){
 1.92670 +        sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
 1.92671 +        goto pragma_out;
 1.92672 +      }
 1.92673 +    }
 1.92674 +  }else
 1.92675 +#endif /* SQLITE_ENABLE_LOCKING_STYLE */      
 1.92676 +    
 1.92677 +  /*
 1.92678 +  **   PRAGMA [database.]synchronous
 1.92679 +  **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
 1.92680 +  **
 1.92681 +  ** Return or set the local value of the synchronous flag.  Changing
 1.92682 +  ** the local value does not make changes to the disk file and the
 1.92683 +  ** default value will be restored the next time the database is
 1.92684 +  ** opened.
 1.92685 +  */
 1.92686 +  if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
 1.92687 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92688 +    if( !zRight ){
 1.92689 +      returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
 1.92690 +    }else{
 1.92691 +      if( !db->autoCommit ){
 1.92692 +        sqlite3ErrorMsg(pParse, 
 1.92693 +            "Safety level may not be changed inside a transaction");
 1.92694 +      }else{
 1.92695 +        pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
 1.92696 +      }
 1.92697 +    }
 1.92698 +  }else
 1.92699 +#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
 1.92700 +
 1.92701 +#ifndef SQLITE_OMIT_FLAG_PRAGMAS
 1.92702 +  if( flagPragma(pParse, zLeft, zRight) ){
 1.92703 +    /* The flagPragma() subroutine also generates any necessary code
 1.92704 +    ** there is nothing more to do here */
 1.92705 +  }else
 1.92706 +#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
 1.92707 +
 1.92708 +#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
 1.92709 +  /*
 1.92710 +  **   PRAGMA table_info(<table>)
 1.92711 +  **
 1.92712 +  ** Return a single row for each column of the named table. The columns of
 1.92713 +  ** the returned data set are:
 1.92714 +  **
 1.92715 +  ** cid:        Column id (numbered from left to right, starting at 0)
 1.92716 +  ** name:       Column name
 1.92717 +  ** type:       Column declaration type.
 1.92718 +  ** notnull:    True if 'NOT NULL' is part of column declaration
 1.92719 +  ** dflt_value: The default value for the column, if any.
 1.92720 +  */
 1.92721 +  if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
 1.92722 +    Table *pTab;
 1.92723 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92724 +    pTab = sqlite3FindTable(db, zRight, zDb);
 1.92725 +    if( pTab ){
 1.92726 +      int i;
 1.92727 +      int nHidden = 0;
 1.92728 +      Column *pCol;
 1.92729 +      sqlite3VdbeSetNumCols(v, 6);
 1.92730 +      pParse->nMem = 6;
 1.92731 +      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
 1.92732 +      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 1.92733 +      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
 1.92734 +      sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
 1.92735 +      sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
 1.92736 +      sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
 1.92737 +      sqlite3ViewGetColumnNames(pParse, pTab);
 1.92738 +      for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
 1.92739 +        if( IsHiddenColumn(pCol) ){
 1.92740 +          nHidden++;
 1.92741 +          continue;
 1.92742 +        }
 1.92743 +        sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
 1.92744 +        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
 1.92745 +        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 1.92746 +           pCol->zType ? pCol->zType : "", 0);
 1.92747 +        sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
 1.92748 +        if( pCol->zDflt ){
 1.92749 +          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
 1.92750 +        }else{
 1.92751 +          sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
 1.92752 +        }
 1.92753 +        sqlite3VdbeAddOp2(v, OP_Integer,
 1.92754 +                            (pCol->colFlags&COLFLAG_PRIMKEY)!=0, 6);
 1.92755 +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
 1.92756 +      }
 1.92757 +    }
 1.92758 +  }else
 1.92759 +
 1.92760 +  if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
 1.92761 +    Index *pIdx;
 1.92762 +    Table *pTab;
 1.92763 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92764 +    pIdx = sqlite3FindIndex(db, zRight, zDb);
 1.92765 +    if( pIdx ){
 1.92766 +      int i;
 1.92767 +      pTab = pIdx->pTable;
 1.92768 +      sqlite3VdbeSetNumCols(v, 3);
 1.92769 +      pParse->nMem = 3;
 1.92770 +      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
 1.92771 +      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
 1.92772 +      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
 1.92773 +      for(i=0; i<pIdx->nColumn; i++){
 1.92774 +        int cnum = pIdx->aiColumn[i];
 1.92775 +        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 1.92776 +        sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
 1.92777 +        assert( pTab->nCol>cnum );
 1.92778 +        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
 1.92779 +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 1.92780 +      }
 1.92781 +    }
 1.92782 +  }else
 1.92783 +
 1.92784 +  if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
 1.92785 +    Index *pIdx;
 1.92786 +    Table *pTab;
 1.92787 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92788 +    pTab = sqlite3FindTable(db, zRight, zDb);
 1.92789 +    if( pTab ){
 1.92790 +      v = sqlite3GetVdbe(pParse);
 1.92791 +      pIdx = pTab->pIndex;
 1.92792 +      if( pIdx ){
 1.92793 +        int i = 0; 
 1.92794 +        sqlite3VdbeSetNumCols(v, 3);
 1.92795 +        pParse->nMem = 3;
 1.92796 +        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 1.92797 +        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 1.92798 +        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
 1.92799 +        while(pIdx){
 1.92800 +          sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 1.92801 +          sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
 1.92802 +          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
 1.92803 +          sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 1.92804 +          ++i;
 1.92805 +          pIdx = pIdx->pNext;
 1.92806 +        }
 1.92807 +      }
 1.92808 +    }
 1.92809 +  }else
 1.92810 +
 1.92811 +  if( sqlite3StrICmp(zLeft, "database_list")==0 ){
 1.92812 +    int i;
 1.92813 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92814 +    sqlite3VdbeSetNumCols(v, 3);
 1.92815 +    pParse->nMem = 3;
 1.92816 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 1.92817 +    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 1.92818 +    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
 1.92819 +    for(i=0; i<db->nDb; i++){
 1.92820 +      if( db->aDb[i].pBt==0 ) continue;
 1.92821 +      assert( db->aDb[i].zName!=0 );
 1.92822 +      sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 1.92823 +      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
 1.92824 +      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 1.92825 +           sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
 1.92826 +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 1.92827 +    }
 1.92828 +  }else
 1.92829 +
 1.92830 +  if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
 1.92831 +    int i = 0;
 1.92832 +    HashElem *p;
 1.92833 +    sqlite3VdbeSetNumCols(v, 2);
 1.92834 +    pParse->nMem = 2;
 1.92835 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 1.92836 +    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 1.92837 +    for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
 1.92838 +      CollSeq *pColl = (CollSeq *)sqliteHashData(p);
 1.92839 +      sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
 1.92840 +      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
 1.92841 +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
 1.92842 +    }
 1.92843 +  }else
 1.92844 +#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
 1.92845 +
 1.92846 +#ifndef SQLITE_OMIT_FOREIGN_KEY
 1.92847 +  if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
 1.92848 +    FKey *pFK;
 1.92849 +    Table *pTab;
 1.92850 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92851 +    pTab = sqlite3FindTable(db, zRight, zDb);
 1.92852 +    if( pTab ){
 1.92853 +      v = sqlite3GetVdbe(pParse);
 1.92854 +      pFK = pTab->pFKey;
 1.92855 +      if( pFK ){
 1.92856 +        int i = 0; 
 1.92857 +        sqlite3VdbeSetNumCols(v, 8);
 1.92858 +        pParse->nMem = 8;
 1.92859 +        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
 1.92860 +        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
 1.92861 +        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
 1.92862 +        sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
 1.92863 +        sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
 1.92864 +        sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
 1.92865 +        sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
 1.92866 +        sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
 1.92867 +        while(pFK){
 1.92868 +          int j;
 1.92869 +          for(j=0; j<pFK->nCol; j++){
 1.92870 +            char *zCol = pFK->aCol[j].zCol;
 1.92871 +            char *zOnDelete = (char *)actionName(pFK->aAction[0]);
 1.92872 +            char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
 1.92873 +            sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 1.92874 +            sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
 1.92875 +            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
 1.92876 +            sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
 1.92877 +                              pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
 1.92878 +            sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
 1.92879 +            sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
 1.92880 +            sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
 1.92881 +            sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
 1.92882 +            sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
 1.92883 +          }
 1.92884 +          ++i;
 1.92885 +          pFK = pFK->pNextFrom;
 1.92886 +        }
 1.92887 +      }
 1.92888 +    }
 1.92889 +  }else
 1.92890 +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 1.92891 +
 1.92892 +#ifndef NDEBUG
 1.92893 +  if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
 1.92894 +    if( zRight ){
 1.92895 +      if( sqlite3GetBoolean(zRight, 0) ){
 1.92896 +        sqlite3ParserTrace(stderr, "parser: ");
 1.92897 +      }else{
 1.92898 +        sqlite3ParserTrace(0, 0);
 1.92899 +      }
 1.92900 +    }
 1.92901 +  }else
 1.92902 +#endif
 1.92903 +
 1.92904 +  /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
 1.92905 +  ** used will be case sensitive or not depending on the RHS.
 1.92906 +  */
 1.92907 +  if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
 1.92908 +    if( zRight ){
 1.92909 +      sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
 1.92910 +    }
 1.92911 +  }else
 1.92912 +
 1.92913 +#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
 1.92914 +# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
 1.92915 +#endif
 1.92916 +
 1.92917 +#ifndef SQLITE_OMIT_INTEGRITY_CHECK
 1.92918 +  /* Pragma "quick_check" is an experimental reduced version of 
 1.92919 +  ** integrity_check designed to detect most database corruption
 1.92920 +  ** without most of the overhead of a full integrity-check.
 1.92921 +  */
 1.92922 +  if( sqlite3StrICmp(zLeft, "integrity_check")==0
 1.92923 +   || sqlite3StrICmp(zLeft, "quick_check")==0 
 1.92924 +  ){
 1.92925 +    int i, j, addr, mxErr;
 1.92926 +
 1.92927 +    /* Code that appears at the end of the integrity check.  If no error
 1.92928 +    ** messages have been generated, output OK.  Otherwise output the
 1.92929 +    ** error message
 1.92930 +    */
 1.92931 +    static const VdbeOpList endCode[] = {
 1.92932 +      { OP_AddImm,      1, 0,        0},    /* 0 */
 1.92933 +      { OP_IfNeg,       1, 0,        0},    /* 1 */
 1.92934 +      { OP_String8,     0, 3,        0},    /* 2 */
 1.92935 +      { OP_ResultRow,   3, 1,        0},
 1.92936 +    };
 1.92937 +
 1.92938 +    int isQuick = (sqlite3Tolower(zLeft[0])=='q');
 1.92939 +
 1.92940 +    /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
 1.92941 +    ** then iDb is set to the index of the database identified by <db>.
 1.92942 +    ** In this case, the integrity of database iDb only is verified by
 1.92943 +    ** the VDBE created below.
 1.92944 +    **
 1.92945 +    ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
 1.92946 +    ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
 1.92947 +    ** to -1 here, to indicate that the VDBE should verify the integrity
 1.92948 +    ** of all attached databases.  */
 1.92949 +    assert( iDb>=0 );
 1.92950 +    assert( iDb==0 || pId2->z );
 1.92951 +    if( pId2->z==0 ) iDb = -1;
 1.92952 +
 1.92953 +    /* Initialize the VDBE program */
 1.92954 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.92955 +    pParse->nMem = 6;
 1.92956 +    sqlite3VdbeSetNumCols(v, 1);
 1.92957 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
 1.92958 +
 1.92959 +    /* Set the maximum error count */
 1.92960 +    mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
 1.92961 +    if( zRight ){
 1.92962 +      sqlite3GetInt32(zRight, &mxErr);
 1.92963 +      if( mxErr<=0 ){
 1.92964 +        mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
 1.92965 +      }
 1.92966 +    }
 1.92967 +    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
 1.92968 +
 1.92969 +    /* Do an integrity check on each database file */
 1.92970 +    for(i=0; i<db->nDb; i++){
 1.92971 +      HashElem *x;
 1.92972 +      Hash *pTbls;
 1.92973 +      int cnt = 0;
 1.92974 +
 1.92975 +      if( OMIT_TEMPDB && i==1 ) continue;
 1.92976 +      if( iDb>=0 && i!=iDb ) continue;
 1.92977 +
 1.92978 +      sqlite3CodeVerifySchema(pParse, i);
 1.92979 +      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
 1.92980 +      sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 1.92981 +      sqlite3VdbeJumpHere(v, addr);
 1.92982 +
 1.92983 +      /* Do an integrity check of the B-Tree
 1.92984 +      **
 1.92985 +      ** Begin by filling registers 2, 3, ... with the root pages numbers
 1.92986 +      ** for all tables and indices in the database.
 1.92987 +      */
 1.92988 +      assert( sqlite3SchemaMutexHeld(db, i, 0) );
 1.92989 +      pTbls = &db->aDb[i].pSchema->tblHash;
 1.92990 +      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
 1.92991 +        Table *pTab = sqliteHashData(x);
 1.92992 +        Index *pIdx;
 1.92993 +        sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
 1.92994 +        cnt++;
 1.92995 +        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.92996 +          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
 1.92997 +          cnt++;
 1.92998 +        }
 1.92999 +      }
 1.93000 +
 1.93001 +      /* Make sure sufficient number of registers have been allocated */
 1.93002 +      if( pParse->nMem < cnt+4 ){
 1.93003 +        pParse->nMem = cnt+4;
 1.93004 +      }
 1.93005 +
 1.93006 +      /* Do the b-tree integrity checks */
 1.93007 +      sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
 1.93008 +      sqlite3VdbeChangeP5(v, (u8)i);
 1.93009 +      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
 1.93010 +      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 1.93011 +         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
 1.93012 +         P4_DYNAMIC);
 1.93013 +      sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
 1.93014 +      sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
 1.93015 +      sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
 1.93016 +      sqlite3VdbeJumpHere(v, addr);
 1.93017 +
 1.93018 +      /* Make sure all the indices are constructed correctly.
 1.93019 +      */
 1.93020 +      for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
 1.93021 +        Table *pTab = sqliteHashData(x);
 1.93022 +        Index *pIdx;
 1.93023 +        int loopTop;
 1.93024 +
 1.93025 +        if( pTab->pIndex==0 ) continue;
 1.93026 +        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
 1.93027 +        sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 1.93028 +        sqlite3VdbeJumpHere(v, addr);
 1.93029 +        sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
 1.93030 +        sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
 1.93031 +        loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
 1.93032 +        sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
 1.93033 +        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 1.93034 +          int jmp2;
 1.93035 +          int r1;
 1.93036 +          static const VdbeOpList idxErr[] = {
 1.93037 +            { OP_AddImm,      1, -1,  0},
 1.93038 +            { OP_String8,     0,  3,  0},    /* 1 */
 1.93039 +            { OP_Rowid,       1,  4,  0},
 1.93040 +            { OP_String8,     0,  5,  0},    /* 3 */
 1.93041 +            { OP_String8,     0,  6,  0},    /* 4 */
 1.93042 +            { OP_Concat,      4,  3,  3},
 1.93043 +            { OP_Concat,      5,  3,  3},
 1.93044 +            { OP_Concat,      6,  3,  3},
 1.93045 +            { OP_ResultRow,   3,  1,  0},
 1.93046 +            { OP_IfPos,       1,  0,  0},    /* 9 */
 1.93047 +            { OP_Halt,        0,  0,  0},
 1.93048 +          };
 1.93049 +          r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
 1.93050 +          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
 1.93051 +          addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
 1.93052 +          sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
 1.93053 +          sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
 1.93054 +          sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
 1.93055 +          sqlite3VdbeJumpHere(v, addr+9);
 1.93056 +          sqlite3VdbeJumpHere(v, jmp2);
 1.93057 +        }
 1.93058 +        sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
 1.93059 +        sqlite3VdbeJumpHere(v, loopTop);
 1.93060 +        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 1.93061 +          static const VdbeOpList cntIdx[] = {
 1.93062 +             { OP_Integer,      0,  3,  0},
 1.93063 +             { OP_Rewind,       0,  0,  0},  /* 1 */
 1.93064 +             { OP_AddImm,       3,  1,  0},
 1.93065 +             { OP_Next,         0,  0,  0},  /* 3 */
 1.93066 +             { OP_Eq,           2,  0,  3},  /* 4 */
 1.93067 +             { OP_AddImm,       1, -1,  0},
 1.93068 +             { OP_String8,      0,  2,  0},  /* 6 */
 1.93069 +             { OP_String8,      0,  3,  0},  /* 7 */
 1.93070 +             { OP_Concat,       3,  2,  2},
 1.93071 +             { OP_ResultRow,    2,  1,  0},
 1.93072 +          };
 1.93073 +          addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
 1.93074 +          sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 1.93075 +          sqlite3VdbeJumpHere(v, addr);
 1.93076 +          addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
 1.93077 +          sqlite3VdbeChangeP1(v, addr+1, j+2);
 1.93078 +          sqlite3VdbeChangeP2(v, addr+1, addr+4);
 1.93079 +          sqlite3VdbeChangeP1(v, addr+3, j+2);
 1.93080 +          sqlite3VdbeChangeP2(v, addr+3, addr+2);
 1.93081 +          sqlite3VdbeJumpHere(v, addr+4);
 1.93082 +          sqlite3VdbeChangeP4(v, addr+6, 
 1.93083 +                     "wrong # of entries in index ", P4_STATIC);
 1.93084 +          sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
 1.93085 +        }
 1.93086 +      } 
 1.93087 +    }
 1.93088 +    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
 1.93089 +    sqlite3VdbeChangeP2(v, addr, -mxErr);
 1.93090 +    sqlite3VdbeJumpHere(v, addr+1);
 1.93091 +    sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
 1.93092 +  }else
 1.93093 +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 1.93094 +
 1.93095 +#ifndef SQLITE_OMIT_UTF16
 1.93096 +  /*
 1.93097 +  **   PRAGMA encoding
 1.93098 +  **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
 1.93099 +  **
 1.93100 +  ** In its first form, this pragma returns the encoding of the main
 1.93101 +  ** database. If the database is not initialized, it is initialized now.
 1.93102 +  **
 1.93103 +  ** The second form of this pragma is a no-op if the main database file
 1.93104 +  ** has not already been initialized. In this case it sets the default
 1.93105 +  ** encoding that will be used for the main database file if a new file
 1.93106 +  ** is created. If an existing main database file is opened, then the
 1.93107 +  ** default text encoding for the existing database is used.
 1.93108 +  ** 
 1.93109 +  ** In all cases new databases created using the ATTACH command are
 1.93110 +  ** created to use the same default text encoding as the main database. If
 1.93111 +  ** the main database has not been initialized and/or created when ATTACH
 1.93112 +  ** is executed, this is done before the ATTACH operation.
 1.93113 +  **
 1.93114 +  ** In the second form this pragma sets the text encoding to be used in
 1.93115 +  ** new database files created using this database handle. It is only
 1.93116 +  ** useful if invoked immediately after the main database i
 1.93117 +  */
 1.93118 +  if( sqlite3StrICmp(zLeft, "encoding")==0 ){
 1.93119 +    static const struct EncName {
 1.93120 +      char *zName;
 1.93121 +      u8 enc;
 1.93122 +    } encnames[] = {
 1.93123 +      { "UTF8",     SQLITE_UTF8        },
 1.93124 +      { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
 1.93125 +      { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
 1.93126 +      { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
 1.93127 +      { "UTF16le",  SQLITE_UTF16LE     },
 1.93128 +      { "UTF16be",  SQLITE_UTF16BE     },
 1.93129 +      { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
 1.93130 +      { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
 1.93131 +      { 0, 0 }
 1.93132 +    };
 1.93133 +    const struct EncName *pEnc;
 1.93134 +    if( !zRight ){    /* "PRAGMA encoding" */
 1.93135 +      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.93136 +      sqlite3VdbeSetNumCols(v, 1);
 1.93137 +      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
 1.93138 +      sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
 1.93139 +      assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
 1.93140 +      assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
 1.93141 +      assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
 1.93142 +      sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
 1.93143 +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 1.93144 +    }else{                        /* "PRAGMA encoding = XXX" */
 1.93145 +      /* Only change the value of sqlite.enc if the database handle is not
 1.93146 +      ** initialized. If the main database exists, the new sqlite.enc value
 1.93147 +      ** will be overwritten when the schema is next loaded. If it does not
 1.93148 +      ** already exists, it will be created to use the new encoding value.
 1.93149 +      */
 1.93150 +      if( 
 1.93151 +        !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
 1.93152 +        DbHasProperty(db, 0, DB_Empty) 
 1.93153 +      ){
 1.93154 +        for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
 1.93155 +          if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
 1.93156 +            ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
 1.93157 +            break;
 1.93158 +          }
 1.93159 +        }
 1.93160 +        if( !pEnc->zName ){
 1.93161 +          sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
 1.93162 +        }
 1.93163 +      }
 1.93164 +    }
 1.93165 +  }else
 1.93166 +#endif /* SQLITE_OMIT_UTF16 */
 1.93167 +
 1.93168 +#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
 1.93169 +  /*
 1.93170 +  **   PRAGMA [database.]schema_version
 1.93171 +  **   PRAGMA [database.]schema_version = <integer>
 1.93172 +  **
 1.93173 +  **   PRAGMA [database.]user_version
 1.93174 +  **   PRAGMA [database.]user_version = <integer>
 1.93175 +  **
 1.93176 +  ** The pragma's schema_version and user_version are used to set or get
 1.93177 +  ** the value of the schema-version and user-version, respectively. Both
 1.93178 +  ** the schema-version and the user-version are 32-bit signed integers
 1.93179 +  ** stored in the database header.
 1.93180 +  **
 1.93181 +  ** The schema-cookie is usually only manipulated internally by SQLite. It
 1.93182 +  ** is incremented by SQLite whenever the database schema is modified (by
 1.93183 +  ** creating or dropping a table or index). The schema version is used by
 1.93184 +  ** SQLite each time a query is executed to ensure that the internal cache
 1.93185 +  ** of the schema used when compiling the SQL query matches the schema of
 1.93186 +  ** the database against which the compiled query is actually executed.
 1.93187 +  ** Subverting this mechanism by using "PRAGMA schema_version" to modify
 1.93188 +  ** the schema-version is potentially dangerous and may lead to program
 1.93189 +  ** crashes or database corruption. Use with caution!
 1.93190 +  **
 1.93191 +  ** The user-version is not used internally by SQLite. It may be used by
 1.93192 +  ** applications for any purpose.
 1.93193 +  */
 1.93194 +  if( sqlite3StrICmp(zLeft, "schema_version")==0 
 1.93195 +   || sqlite3StrICmp(zLeft, "user_version")==0 
 1.93196 +   || sqlite3StrICmp(zLeft, "freelist_count")==0 
 1.93197 +  ){
 1.93198 +    int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
 1.93199 +    sqlite3VdbeUsesBtree(v, iDb);
 1.93200 +    switch( zLeft[0] ){
 1.93201 +      case 'f': case 'F':
 1.93202 +        iCookie = BTREE_FREE_PAGE_COUNT;
 1.93203 +        break;
 1.93204 +      case 's': case 'S':
 1.93205 +        iCookie = BTREE_SCHEMA_VERSION;
 1.93206 +        break;
 1.93207 +      default:
 1.93208 +        iCookie = BTREE_USER_VERSION;
 1.93209 +        break;
 1.93210 +    }
 1.93211 +
 1.93212 +    if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
 1.93213 +      /* Write the specified cookie value */
 1.93214 +      static const VdbeOpList setCookie[] = {
 1.93215 +        { OP_Transaction,    0,  1,  0},    /* 0 */
 1.93216 +        { OP_Integer,        0,  1,  0},    /* 1 */
 1.93217 +        { OP_SetCookie,      0,  0,  1},    /* 2 */
 1.93218 +      };
 1.93219 +      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
 1.93220 +      sqlite3VdbeChangeP1(v, addr, iDb);
 1.93221 +      sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
 1.93222 +      sqlite3VdbeChangeP1(v, addr+2, iDb);
 1.93223 +      sqlite3VdbeChangeP2(v, addr+2, iCookie);
 1.93224 +    }else{
 1.93225 +      /* Read the specified cookie value */
 1.93226 +      static const VdbeOpList readCookie[] = {
 1.93227 +        { OP_Transaction,     0,  0,  0},    /* 0 */
 1.93228 +        { OP_ReadCookie,      0,  1,  0},    /* 1 */
 1.93229 +        { OP_ResultRow,       1,  1,  0}
 1.93230 +      };
 1.93231 +      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
 1.93232 +      sqlite3VdbeChangeP1(v, addr, iDb);
 1.93233 +      sqlite3VdbeChangeP1(v, addr+1, iDb);
 1.93234 +      sqlite3VdbeChangeP3(v, addr+1, iCookie);
 1.93235 +      sqlite3VdbeSetNumCols(v, 1);
 1.93236 +      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
 1.93237 +    }
 1.93238 +  }else
 1.93239 +#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
 1.93240 +
 1.93241 +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 1.93242 +  /*
 1.93243 +  **   PRAGMA compile_options
 1.93244 +  **
 1.93245 +  ** Return the names of all compile-time options used in this build,
 1.93246 +  ** one option per row.
 1.93247 +  */
 1.93248 +  if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
 1.93249 +    int i = 0;
 1.93250 +    const char *zOpt;
 1.93251 +    sqlite3VdbeSetNumCols(v, 1);
 1.93252 +    pParse->nMem = 1;
 1.93253 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
 1.93254 +    while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
 1.93255 +      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
 1.93256 +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 1.93257 +    }
 1.93258 +  }else
 1.93259 +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 1.93260 +
 1.93261 +#ifndef SQLITE_OMIT_WAL
 1.93262 +  /*
 1.93263 +  **   PRAGMA [database.]wal_checkpoint = passive|full|restart
 1.93264 +  **
 1.93265 +  ** Checkpoint the database.
 1.93266 +  */
 1.93267 +  if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
 1.93268 +    int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
 1.93269 +    int eMode = SQLITE_CHECKPOINT_PASSIVE;
 1.93270 +    if( zRight ){
 1.93271 +      if( sqlite3StrICmp(zRight, "full")==0 ){
 1.93272 +        eMode = SQLITE_CHECKPOINT_FULL;
 1.93273 +      }else if( sqlite3StrICmp(zRight, "restart")==0 ){
 1.93274 +        eMode = SQLITE_CHECKPOINT_RESTART;
 1.93275 +      }
 1.93276 +    }
 1.93277 +    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 1.93278 +    sqlite3VdbeSetNumCols(v, 3);
 1.93279 +    pParse->nMem = 3;
 1.93280 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
 1.93281 +    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
 1.93282 +    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
 1.93283 +
 1.93284 +    sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
 1.93285 +    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 1.93286 +  }else
 1.93287 +
 1.93288 +  /*
 1.93289 +  **   PRAGMA wal_autocheckpoint
 1.93290 +  **   PRAGMA wal_autocheckpoint = N
 1.93291 +  **
 1.93292 +  ** Configure a database connection to automatically checkpoint a database
 1.93293 +  ** after accumulating N frames in the log. Or query for the current value
 1.93294 +  ** of N.
 1.93295 +  */
 1.93296 +  if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
 1.93297 +    if( zRight ){
 1.93298 +      sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
 1.93299 +    }
 1.93300 +    returnSingleInt(pParse, "wal_autocheckpoint", 
 1.93301 +       db->xWalCallback==sqlite3WalDefaultHook ? 
 1.93302 +           SQLITE_PTR_TO_INT(db->pWalArg) : 0);
 1.93303 +  }else
 1.93304 +#endif
 1.93305 +
 1.93306 +  /*
 1.93307 +  **  PRAGMA shrink_memory
 1.93308 +  **
 1.93309 +  ** This pragma attempts to free as much memory as possible from the
 1.93310 +  ** current database connection.
 1.93311 +  */
 1.93312 +  if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
 1.93313 +    sqlite3_db_release_memory(db);
 1.93314 +  }else
 1.93315 +
 1.93316 +  /*
 1.93317 +  **   PRAGMA busy_timeout
 1.93318 +  **   PRAGMA busy_timeout = N
 1.93319 +  **
 1.93320 +  ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
 1.93321 +  ** if one is set.  If no busy handler or a different busy handler is set
 1.93322 +  ** then 0 is returned.  Setting the busy_timeout to 0 or negative
 1.93323 +  ** disables the timeout.
 1.93324 +  */
 1.93325 +  if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
 1.93326 +    if( zRight ){
 1.93327 +      sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
 1.93328 +    }
 1.93329 +    returnSingleInt(pParse, "timeout",  db->busyTimeout);
 1.93330 +  }else
 1.93331 +
 1.93332 +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
 1.93333 +  /*
 1.93334 +  ** Report the current state of file logs for all databases
 1.93335 +  */
 1.93336 +  if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
 1.93337 +    static const char *const azLockName[] = {
 1.93338 +      "unlocked", "shared", "reserved", "pending", "exclusive"
 1.93339 +    };
 1.93340 +    int i;
 1.93341 +    sqlite3VdbeSetNumCols(v, 2);
 1.93342 +    pParse->nMem = 2;
 1.93343 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
 1.93344 +    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
 1.93345 +    for(i=0; i<db->nDb; i++){
 1.93346 +      Btree *pBt;
 1.93347 +      const char *zState = "unknown";
 1.93348 +      int j;
 1.93349 +      if( db->aDb[i].zName==0 ) continue;
 1.93350 +      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
 1.93351 +      pBt = db->aDb[i].pBt;
 1.93352 +      if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
 1.93353 +        zState = "closed";
 1.93354 +      }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
 1.93355 +                                     SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
 1.93356 +         zState = azLockName[j];
 1.93357 +      }
 1.93358 +      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
 1.93359 +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
 1.93360 +    }
 1.93361 +
 1.93362 +  }else
 1.93363 +#endif
 1.93364 +
 1.93365 +#ifdef SQLITE_HAS_CODEC
 1.93366 +  if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
 1.93367 +    sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
 1.93368 +  }else
 1.93369 +  if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
 1.93370 +    sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
 1.93371 +  }else
 1.93372 +  if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
 1.93373 +                 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
 1.93374 +    int i, h1, h2;
 1.93375 +    char zKey[40];
 1.93376 +    for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
 1.93377 +      h1 += 9*(1&(h1>>6));
 1.93378 +      h2 += 9*(1&(h2>>6));
 1.93379 +      zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
 1.93380 +    }
 1.93381 +    if( (zLeft[3] & 0xf)==0xb ){
 1.93382 +      sqlite3_key(db, zKey, i/2);
 1.93383 +    }else{
 1.93384 +      sqlite3_rekey(db, zKey, i/2);
 1.93385 +    }
 1.93386 +  }else
 1.93387 +#endif
 1.93388 +#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
 1.93389 +  if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
 1.93390 +#ifdef SQLITE_HAS_CODEC
 1.93391 +    if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
 1.93392 +      sqlite3_activate_see(&zRight[4]);
 1.93393 +    }
 1.93394 +#endif
 1.93395 +#ifdef SQLITE_ENABLE_CEROD
 1.93396 +    if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
 1.93397 +      sqlite3_activate_cerod(&zRight[6]);
 1.93398 +    }
 1.93399 +#endif
 1.93400 +  }else
 1.93401 +#endif
 1.93402 +
 1.93403 + 
 1.93404 +  {/* Empty ELSE clause */}
 1.93405 +
 1.93406 +  /*
 1.93407 +  ** Reset the safety level, in case the fullfsync flag or synchronous
 1.93408 +  ** setting changed.
 1.93409 +  */
 1.93410 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
 1.93411 +  if( db->autoCommit ){
 1.93412 +    sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
 1.93413 +               (db->flags&SQLITE_FullFSync)!=0,
 1.93414 +               (db->flags&SQLITE_CkptFullFSync)!=0);
 1.93415 +  }
 1.93416 +#endif
 1.93417 +pragma_out:
 1.93418 +  sqlite3DbFree(db, zLeft);
 1.93419 +  sqlite3DbFree(db, zRight);
 1.93420 +}
 1.93421 +
 1.93422 +#endif /* SQLITE_OMIT_PRAGMA */
 1.93423 +
 1.93424 +/************** End of pragma.c **********************************************/
 1.93425 +/************** Begin file prepare.c *****************************************/
 1.93426 +/*
 1.93427 +** 2005 May 25
 1.93428 +**
 1.93429 +** The author disclaims copyright to this source code.  In place of
 1.93430 +** a legal notice, here is a blessing:
 1.93431 +**
 1.93432 +**    May you do good and not evil.
 1.93433 +**    May you find forgiveness for yourself and forgive others.
 1.93434 +**    May you share freely, never taking more than you give.
 1.93435 +**
 1.93436 +*************************************************************************
 1.93437 +** This file contains the implementation of the sqlite3_prepare()
 1.93438 +** interface, and routines that contribute to loading the database schema
 1.93439 +** from disk.
 1.93440 +*/
 1.93441 +
 1.93442 +/*
 1.93443 +** Fill the InitData structure with an error message that indicates
 1.93444 +** that the database is corrupt.
 1.93445 +*/
 1.93446 +static void corruptSchema(
 1.93447 +  InitData *pData,     /* Initialization context */
 1.93448 +  const char *zObj,    /* Object being parsed at the point of error */
 1.93449 +  const char *zExtra   /* Error information */
 1.93450 +){
 1.93451 +  sqlite3 *db = pData->db;
 1.93452 +  if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
 1.93453 +    if( zObj==0 ) zObj = "?";
 1.93454 +    sqlite3SetString(pData->pzErrMsg, db,
 1.93455 +      "malformed database schema (%s)", zObj);
 1.93456 +    if( zExtra ){
 1.93457 +      *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
 1.93458 +                                 "%s - %s", *pData->pzErrMsg, zExtra);
 1.93459 +    }
 1.93460 +  }
 1.93461 +  pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
 1.93462 +}
 1.93463 +
 1.93464 +/*
 1.93465 +** This is the callback routine for the code that initializes the
 1.93466 +** database.  See sqlite3Init() below for additional information.
 1.93467 +** This routine is also called from the OP_ParseSchema opcode of the VDBE.
 1.93468 +**
 1.93469 +** Each callback contains the following information:
 1.93470 +**
 1.93471 +**     argv[0] = name of thing being created
 1.93472 +**     argv[1] = root page number for table or index. 0 for trigger or view.
 1.93473 +**     argv[2] = SQL text for the CREATE statement.
 1.93474 +**
 1.93475 +*/
 1.93476 +SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
 1.93477 +  InitData *pData = (InitData*)pInit;
 1.93478 +  sqlite3 *db = pData->db;
 1.93479 +  int iDb = pData->iDb;
 1.93480 +
 1.93481 +  assert( argc==3 );
 1.93482 +  UNUSED_PARAMETER2(NotUsed, argc);
 1.93483 +  assert( sqlite3_mutex_held(db->mutex) );
 1.93484 +  DbClearProperty(db, iDb, DB_Empty);
 1.93485 +  if( db->mallocFailed ){
 1.93486 +    corruptSchema(pData, argv[0], 0);
 1.93487 +    return 1;
 1.93488 +  }
 1.93489 +
 1.93490 +  assert( iDb>=0 && iDb<db->nDb );
 1.93491 +  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
 1.93492 +  if( argv[1]==0 ){
 1.93493 +    corruptSchema(pData, argv[0], 0);
 1.93494 +  }else if( argv[2] && argv[2][0] ){
 1.93495 +    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
 1.93496 +    ** But because db->init.busy is set to 1, no VDBE code is generated
 1.93497 +    ** or executed.  All the parser does is build the internal data
 1.93498 +    ** structures that describe the table, index, or view.
 1.93499 +    */
 1.93500 +    int rc;
 1.93501 +    sqlite3_stmt *pStmt;
 1.93502 +    TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
 1.93503 +
 1.93504 +    assert( db->init.busy );
 1.93505 +    db->init.iDb = iDb;
 1.93506 +    db->init.newTnum = sqlite3Atoi(argv[1]);
 1.93507 +    db->init.orphanTrigger = 0;
 1.93508 +    TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
 1.93509 +    rc = db->errCode;
 1.93510 +    assert( (rc&0xFF)==(rcp&0xFF) );
 1.93511 +    db->init.iDb = 0;
 1.93512 +    if( SQLITE_OK!=rc ){
 1.93513 +      if( db->init.orphanTrigger ){
 1.93514 +        assert( iDb==1 );
 1.93515 +      }else{
 1.93516 +        pData->rc = rc;
 1.93517 +        if( rc==SQLITE_NOMEM ){
 1.93518 +          db->mallocFailed = 1;
 1.93519 +        }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
 1.93520 +          corruptSchema(pData, argv[0], sqlite3_errmsg(db));
 1.93521 +        }
 1.93522 +      }
 1.93523 +    }
 1.93524 +    sqlite3_finalize(pStmt);
 1.93525 +  }else if( argv[0]==0 ){
 1.93526 +    corruptSchema(pData, 0, 0);
 1.93527 +  }else{
 1.93528 +    /* If the SQL column is blank it means this is an index that
 1.93529 +    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
 1.93530 +    ** constraint for a CREATE TABLE.  The index should have already
 1.93531 +    ** been created when we processed the CREATE TABLE.  All we have
 1.93532 +    ** to do here is record the root page number for that index.
 1.93533 +    */
 1.93534 +    Index *pIndex;
 1.93535 +    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
 1.93536 +    if( pIndex==0 ){
 1.93537 +      /* This can occur if there exists an index on a TEMP table which
 1.93538 +      ** has the same name as another index on a permanent index.  Since
 1.93539 +      ** the permanent table is hidden by the TEMP table, we can also
 1.93540 +      ** safely ignore the index on the permanent table.
 1.93541 +      */
 1.93542 +      /* Do Nothing */;
 1.93543 +    }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
 1.93544 +      corruptSchema(pData, argv[0], "invalid rootpage");
 1.93545 +    }
 1.93546 +  }
 1.93547 +  return 0;
 1.93548 +}
 1.93549 +
 1.93550 +/*
 1.93551 +** Attempt to read the database schema and initialize internal
 1.93552 +** data structures for a single database file.  The index of the
 1.93553 +** database file is given by iDb.  iDb==0 is used for the main
 1.93554 +** database.  iDb==1 should never be used.  iDb>=2 is used for
 1.93555 +** auxiliary databases.  Return one of the SQLITE_ error codes to
 1.93556 +** indicate success or failure.
 1.93557 +*/
 1.93558 +static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
 1.93559 +  int rc;
 1.93560 +  int i;
 1.93561 +#ifndef SQLITE_OMIT_DEPRECATED
 1.93562 +  int size;
 1.93563 +#endif
 1.93564 +  Table *pTab;
 1.93565 +  Db *pDb;
 1.93566 +  char const *azArg[4];
 1.93567 +  int meta[5];
 1.93568 +  InitData initData;
 1.93569 +  char const *zMasterSchema;
 1.93570 +  char const *zMasterName;
 1.93571 +  int openedTransaction = 0;
 1.93572 +
 1.93573 +  /*
 1.93574 +  ** The master database table has a structure like this
 1.93575 +  */
 1.93576 +  static const char master_schema[] = 
 1.93577 +     "CREATE TABLE sqlite_master(\n"
 1.93578 +     "  type text,\n"
 1.93579 +     "  name text,\n"
 1.93580 +     "  tbl_name text,\n"
 1.93581 +     "  rootpage integer,\n"
 1.93582 +     "  sql text\n"
 1.93583 +     ")"
 1.93584 +  ;
 1.93585 +#ifndef SQLITE_OMIT_TEMPDB
 1.93586 +  static const char temp_master_schema[] = 
 1.93587 +     "CREATE TEMP TABLE sqlite_temp_master(\n"
 1.93588 +     "  type text,\n"
 1.93589 +     "  name text,\n"
 1.93590 +     "  tbl_name text,\n"
 1.93591 +     "  rootpage integer,\n"
 1.93592 +     "  sql text\n"
 1.93593 +     ")"
 1.93594 +  ;
 1.93595 +#else
 1.93596 +  #define temp_master_schema 0
 1.93597 +#endif
 1.93598 +
 1.93599 +  assert( iDb>=0 && iDb<db->nDb );
 1.93600 +  assert( db->aDb[iDb].pSchema );
 1.93601 +  assert( sqlite3_mutex_held(db->mutex) );
 1.93602 +  assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
 1.93603 +
 1.93604 +  /* zMasterSchema and zInitScript are set to point at the master schema
 1.93605 +  ** and initialisation script appropriate for the database being
 1.93606 +  ** initialised. zMasterName is the name of the master table.
 1.93607 +  */
 1.93608 +  if( !OMIT_TEMPDB && iDb==1 ){
 1.93609 +    zMasterSchema = temp_master_schema;
 1.93610 +  }else{
 1.93611 +    zMasterSchema = master_schema;
 1.93612 +  }
 1.93613 +  zMasterName = SCHEMA_TABLE(iDb);
 1.93614 +
 1.93615 +  /* Construct the schema tables.  */
 1.93616 +  azArg[0] = zMasterName;
 1.93617 +  azArg[1] = "1";
 1.93618 +  azArg[2] = zMasterSchema;
 1.93619 +  azArg[3] = 0;
 1.93620 +  initData.db = db;
 1.93621 +  initData.iDb = iDb;
 1.93622 +  initData.rc = SQLITE_OK;
 1.93623 +  initData.pzErrMsg = pzErrMsg;
 1.93624 +  sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
 1.93625 +  if( initData.rc ){
 1.93626 +    rc = initData.rc;
 1.93627 +    goto error_out;
 1.93628 +  }
 1.93629 +  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
 1.93630 +  if( ALWAYS(pTab) ){
 1.93631 +    pTab->tabFlags |= TF_Readonly;
 1.93632 +  }
 1.93633 +
 1.93634 +  /* Create a cursor to hold the database open
 1.93635 +  */
 1.93636 +  pDb = &db->aDb[iDb];
 1.93637 +  if( pDb->pBt==0 ){
 1.93638 +    if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
 1.93639 +      DbSetProperty(db, 1, DB_SchemaLoaded);
 1.93640 +    }
 1.93641 +    return SQLITE_OK;
 1.93642 +  }
 1.93643 +
 1.93644 +  /* If there is not already a read-only (or read-write) transaction opened
 1.93645 +  ** on the b-tree database, open one now. If a transaction is opened, it 
 1.93646 +  ** will be closed before this function returns.  */
 1.93647 +  sqlite3BtreeEnter(pDb->pBt);
 1.93648 +  if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
 1.93649 +    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
 1.93650 +    if( rc!=SQLITE_OK ){
 1.93651 +      sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
 1.93652 +      goto initone_error_out;
 1.93653 +    }
 1.93654 +    openedTransaction = 1;
 1.93655 +  }
 1.93656 +
 1.93657 +  /* Get the database meta information.
 1.93658 +  **
 1.93659 +  ** Meta values are as follows:
 1.93660 +  **    meta[0]   Schema cookie.  Changes with each schema change.
 1.93661 +  **    meta[1]   File format of schema layer.
 1.93662 +  **    meta[2]   Size of the page cache.
 1.93663 +  **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
 1.93664 +  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
 1.93665 +  **    meta[5]   User version
 1.93666 +  **    meta[6]   Incremental vacuum mode
 1.93667 +  **    meta[7]   unused
 1.93668 +  **    meta[8]   unused
 1.93669 +  **    meta[9]   unused
 1.93670 +  **
 1.93671 +  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
 1.93672 +  ** the possible values of meta[4].
 1.93673 +  */
 1.93674 +  for(i=0; i<ArraySize(meta); i++){
 1.93675 +    sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
 1.93676 +  }
 1.93677 +  pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
 1.93678 +
 1.93679 +  /* If opening a non-empty database, check the text encoding. For the
 1.93680 +  ** main database, set sqlite3.enc to the encoding of the main database.
 1.93681 +  ** For an attached db, it is an error if the encoding is not the same
 1.93682 +  ** as sqlite3.enc.
 1.93683 +  */
 1.93684 +  if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
 1.93685 +    if( iDb==0 ){
 1.93686 +      u8 encoding;
 1.93687 +      /* If opening the main database, set ENC(db). */
 1.93688 +      encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
 1.93689 +      if( encoding==0 ) encoding = SQLITE_UTF8;
 1.93690 +      ENC(db) = encoding;
 1.93691 +    }else{
 1.93692 +      /* If opening an attached database, the encoding much match ENC(db) */
 1.93693 +      if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
 1.93694 +        sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
 1.93695 +            " text encoding as main database");
 1.93696 +        rc = SQLITE_ERROR;
 1.93697 +        goto initone_error_out;
 1.93698 +      }
 1.93699 +    }
 1.93700 +  }else{
 1.93701 +    DbSetProperty(db, iDb, DB_Empty);
 1.93702 +  }
 1.93703 +  pDb->pSchema->enc = ENC(db);
 1.93704 +
 1.93705 +  if( pDb->pSchema->cache_size==0 ){
 1.93706 +#ifndef SQLITE_OMIT_DEPRECATED
 1.93707 +    size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
 1.93708 +    if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
 1.93709 +    pDb->pSchema->cache_size = size;
 1.93710 +#else
 1.93711 +    pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
 1.93712 +#endif
 1.93713 +    sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 1.93714 +  }
 1.93715 +
 1.93716 +  /*
 1.93717 +  ** file_format==1    Version 3.0.0.
 1.93718 +  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
 1.93719 +  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
 1.93720 +  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
 1.93721 +  */
 1.93722 +  pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
 1.93723 +  if( pDb->pSchema->file_format==0 ){
 1.93724 +    pDb->pSchema->file_format = 1;
 1.93725 +  }
 1.93726 +  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
 1.93727 +    sqlite3SetString(pzErrMsg, db, "unsupported file format");
 1.93728 +    rc = SQLITE_ERROR;
 1.93729 +    goto initone_error_out;
 1.93730 +  }
 1.93731 +
 1.93732 +  /* Ticket #2804:  When we open a database in the newer file format,
 1.93733 +  ** clear the legacy_file_format pragma flag so that a VACUUM will
 1.93734 +  ** not downgrade the database and thus invalidate any descending
 1.93735 +  ** indices that the user might have created.
 1.93736 +  */
 1.93737 +  if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
 1.93738 +    db->flags &= ~SQLITE_LegacyFileFmt;
 1.93739 +  }
 1.93740 +
 1.93741 +  /* Read the schema information out of the schema tables
 1.93742 +  */
 1.93743 +  assert( db->init.busy );
 1.93744 +  {
 1.93745 +    char *zSql;
 1.93746 +    zSql = sqlite3MPrintf(db, 
 1.93747 +        "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
 1.93748 +        db->aDb[iDb].zName, zMasterName);
 1.93749 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.93750 +    {
 1.93751 +      int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
 1.93752 +      xAuth = db->xAuth;
 1.93753 +      db->xAuth = 0;
 1.93754 +#endif
 1.93755 +      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
 1.93756 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.93757 +      db->xAuth = xAuth;
 1.93758 +    }
 1.93759 +#endif
 1.93760 +    if( rc==SQLITE_OK ) rc = initData.rc;
 1.93761 +    sqlite3DbFree(db, zSql);
 1.93762 +#ifndef SQLITE_OMIT_ANALYZE
 1.93763 +    if( rc==SQLITE_OK ){
 1.93764 +      sqlite3AnalysisLoad(db, iDb);
 1.93765 +    }
 1.93766 +#endif
 1.93767 +  }
 1.93768 +  if( db->mallocFailed ){
 1.93769 +    rc = SQLITE_NOMEM;
 1.93770 +    sqlite3ResetAllSchemasOfConnection(db);
 1.93771 +  }
 1.93772 +  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
 1.93773 +    /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
 1.93774 +    ** the schema loaded, even if errors occurred. In this situation the 
 1.93775 +    ** current sqlite3_prepare() operation will fail, but the following one
 1.93776 +    ** will attempt to compile the supplied statement against whatever subset
 1.93777 +    ** of the schema was loaded before the error occurred. The primary
 1.93778 +    ** purpose of this is to allow access to the sqlite_master table
 1.93779 +    ** even when its contents have been corrupted.
 1.93780 +    */
 1.93781 +    DbSetProperty(db, iDb, DB_SchemaLoaded);
 1.93782 +    rc = SQLITE_OK;
 1.93783 +  }
 1.93784 +
 1.93785 +  /* Jump here for an error that occurs after successfully allocating
 1.93786 +  ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
 1.93787 +  ** before that point, jump to error_out.
 1.93788 +  */
 1.93789 +initone_error_out:
 1.93790 +  if( openedTransaction ){
 1.93791 +    sqlite3BtreeCommit(pDb->pBt);
 1.93792 +  }
 1.93793 +  sqlite3BtreeLeave(pDb->pBt);
 1.93794 +
 1.93795 +error_out:
 1.93796 +  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 1.93797 +    db->mallocFailed = 1;
 1.93798 +  }
 1.93799 +  return rc;
 1.93800 +}
 1.93801 +
 1.93802 +/*
 1.93803 +** Initialize all database files - the main database file, the file
 1.93804 +** used to store temporary tables, and any additional database files
 1.93805 +** created using ATTACH statements.  Return a success code.  If an
 1.93806 +** error occurs, write an error message into *pzErrMsg.
 1.93807 +**
 1.93808 +** After a database is initialized, the DB_SchemaLoaded bit is set
 1.93809 +** bit is set in the flags field of the Db structure. If the database
 1.93810 +** file was of zero-length, then the DB_Empty flag is also set.
 1.93811 +*/
 1.93812 +SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
 1.93813 +  int i, rc;
 1.93814 +  int commit_internal = !(db->flags&SQLITE_InternChanges);
 1.93815 +  
 1.93816 +  assert( sqlite3_mutex_held(db->mutex) );
 1.93817 +  rc = SQLITE_OK;
 1.93818 +  db->init.busy = 1;
 1.93819 +  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 1.93820 +    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
 1.93821 +    rc = sqlite3InitOne(db, i, pzErrMsg);
 1.93822 +    if( rc ){
 1.93823 +      sqlite3ResetOneSchema(db, i);
 1.93824 +    }
 1.93825 +  }
 1.93826 +
 1.93827 +  /* Once all the other databases have been initialised, load the schema
 1.93828 +  ** for the TEMP database. This is loaded last, as the TEMP database
 1.93829 +  ** schema may contain references to objects in other databases.
 1.93830 +  */
 1.93831 +#ifndef SQLITE_OMIT_TEMPDB
 1.93832 +  if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
 1.93833 +                    && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
 1.93834 +    rc = sqlite3InitOne(db, 1, pzErrMsg);
 1.93835 +    if( rc ){
 1.93836 +      sqlite3ResetOneSchema(db, 1);
 1.93837 +    }
 1.93838 +  }
 1.93839 +#endif
 1.93840 +
 1.93841 +  db->init.busy = 0;
 1.93842 +  if( rc==SQLITE_OK && commit_internal ){
 1.93843 +    sqlite3CommitInternalChanges(db);
 1.93844 +  }
 1.93845 +
 1.93846 +  return rc; 
 1.93847 +}
 1.93848 +
 1.93849 +/*
 1.93850 +** This routine is a no-op if the database schema is already initialised.
 1.93851 +** Otherwise, the schema is loaded. An error code is returned.
 1.93852 +*/
 1.93853 +SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
 1.93854 +  int rc = SQLITE_OK;
 1.93855 +  sqlite3 *db = pParse->db;
 1.93856 +  assert( sqlite3_mutex_held(db->mutex) );
 1.93857 +  if( !db->init.busy ){
 1.93858 +    rc = sqlite3Init(db, &pParse->zErrMsg);
 1.93859 +  }
 1.93860 +  if( rc!=SQLITE_OK ){
 1.93861 +    pParse->rc = rc;
 1.93862 +    pParse->nErr++;
 1.93863 +  }
 1.93864 +  return rc;
 1.93865 +}
 1.93866 +
 1.93867 +
 1.93868 +/*
 1.93869 +** Check schema cookies in all databases.  If any cookie is out
 1.93870 +** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
 1.93871 +** make no changes to pParse->rc.
 1.93872 +*/
 1.93873 +static void schemaIsValid(Parse *pParse){
 1.93874 +  sqlite3 *db = pParse->db;
 1.93875 +  int iDb;
 1.93876 +  int rc;
 1.93877 +  int cookie;
 1.93878 +
 1.93879 +  assert( pParse->checkSchema );
 1.93880 +  assert( sqlite3_mutex_held(db->mutex) );
 1.93881 +  for(iDb=0; iDb<db->nDb; iDb++){
 1.93882 +    int openedTransaction = 0;         /* True if a transaction is opened */
 1.93883 +    Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
 1.93884 +    if( pBt==0 ) continue;
 1.93885 +
 1.93886 +    /* If there is not already a read-only (or read-write) transaction opened
 1.93887 +    ** on the b-tree database, open one now. If a transaction is opened, it 
 1.93888 +    ** will be closed immediately after reading the meta-value. */
 1.93889 +    if( !sqlite3BtreeIsInReadTrans(pBt) ){
 1.93890 +      rc = sqlite3BtreeBeginTrans(pBt, 0);
 1.93891 +      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 1.93892 +        db->mallocFailed = 1;
 1.93893 +      }
 1.93894 +      if( rc!=SQLITE_OK ) return;
 1.93895 +      openedTransaction = 1;
 1.93896 +    }
 1.93897 +
 1.93898 +    /* Read the schema cookie from the database. If it does not match the 
 1.93899 +    ** value stored as part of the in-memory schema representation,
 1.93900 +    ** set Parse.rc to SQLITE_SCHEMA. */
 1.93901 +    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
 1.93902 +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.93903 +    if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
 1.93904 +      sqlite3ResetOneSchema(db, iDb);
 1.93905 +      pParse->rc = SQLITE_SCHEMA;
 1.93906 +    }
 1.93907 +
 1.93908 +    /* Close the transaction, if one was opened. */
 1.93909 +    if( openedTransaction ){
 1.93910 +      sqlite3BtreeCommit(pBt);
 1.93911 +    }
 1.93912 +  }
 1.93913 +}
 1.93914 +
 1.93915 +/*
 1.93916 +** Convert a schema pointer into the iDb index that indicates
 1.93917 +** which database file in db->aDb[] the schema refers to.
 1.93918 +**
 1.93919 +** If the same database is attached more than once, the first
 1.93920 +** attached database is returned.
 1.93921 +*/
 1.93922 +SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
 1.93923 +  int i = -1000000;
 1.93924 +
 1.93925 +  /* If pSchema is NULL, then return -1000000. This happens when code in 
 1.93926 +  ** expr.c is trying to resolve a reference to a transient table (i.e. one
 1.93927 +  ** created by a sub-select). In this case the return value of this 
 1.93928 +  ** function should never be used.
 1.93929 +  **
 1.93930 +  ** We return -1000000 instead of the more usual -1 simply because using
 1.93931 +  ** -1000000 as the incorrect index into db->aDb[] is much 
 1.93932 +  ** more likely to cause a segfault than -1 (of course there are assert()
 1.93933 +  ** statements too, but it never hurts to play the odds).
 1.93934 +  */
 1.93935 +  assert( sqlite3_mutex_held(db->mutex) );
 1.93936 +  if( pSchema ){
 1.93937 +    for(i=0; ALWAYS(i<db->nDb); i++){
 1.93938 +      if( db->aDb[i].pSchema==pSchema ){
 1.93939 +        break;
 1.93940 +      }
 1.93941 +    }
 1.93942 +    assert( i>=0 && i<db->nDb );
 1.93943 +  }
 1.93944 +  return i;
 1.93945 +}
 1.93946 +
 1.93947 +/*
 1.93948 +** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
 1.93949 +*/
 1.93950 +static int sqlite3Prepare(
 1.93951 +  sqlite3 *db,              /* Database handle. */
 1.93952 +  const char *zSql,         /* UTF-8 encoded SQL statement. */
 1.93953 +  int nBytes,               /* Length of zSql in bytes. */
 1.93954 +  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
 1.93955 +  Vdbe *pReprepare,         /* VM being reprepared */
 1.93956 +  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 1.93957 +  const char **pzTail       /* OUT: End of parsed string */
 1.93958 +){
 1.93959 +  Parse *pParse;            /* Parsing context */
 1.93960 +  char *zErrMsg = 0;        /* Error message */
 1.93961 +  int rc = SQLITE_OK;       /* Result code */
 1.93962 +  int i;                    /* Loop counter */
 1.93963 +
 1.93964 +  /* Allocate the parsing context */
 1.93965 +  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
 1.93966 +  if( pParse==0 ){
 1.93967 +    rc = SQLITE_NOMEM;
 1.93968 +    goto end_prepare;
 1.93969 +  }
 1.93970 +  pParse->pReprepare = pReprepare;
 1.93971 +  assert( ppStmt && *ppStmt==0 );
 1.93972 +  assert( !db->mallocFailed );
 1.93973 +  assert( sqlite3_mutex_held(db->mutex) );
 1.93974 +
 1.93975 +  /* Check to verify that it is possible to get a read lock on all
 1.93976 +  ** database schemas.  The inability to get a read lock indicates that
 1.93977 +  ** some other database connection is holding a write-lock, which in
 1.93978 +  ** turn means that the other connection has made uncommitted changes
 1.93979 +  ** to the schema.
 1.93980 +  **
 1.93981 +  ** Were we to proceed and prepare the statement against the uncommitted
 1.93982 +  ** schema changes and if those schema changes are subsequently rolled
 1.93983 +  ** back and different changes are made in their place, then when this
 1.93984 +  ** prepared statement goes to run the schema cookie would fail to detect
 1.93985 +  ** the schema change.  Disaster would follow.
 1.93986 +  **
 1.93987 +  ** This thread is currently holding mutexes on all Btrees (because
 1.93988 +  ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
 1.93989 +  ** is not possible for another thread to start a new schema change
 1.93990 +  ** while this routine is running.  Hence, we do not need to hold 
 1.93991 +  ** locks on the schema, we just need to make sure nobody else is 
 1.93992 +  ** holding them.
 1.93993 +  **
 1.93994 +  ** Note that setting READ_UNCOMMITTED overrides most lock detection,
 1.93995 +  ** but it does *not* override schema lock detection, so this all still
 1.93996 +  ** works even if READ_UNCOMMITTED is set.
 1.93997 +  */
 1.93998 +  for(i=0; i<db->nDb; i++) {
 1.93999 +    Btree *pBt = db->aDb[i].pBt;
 1.94000 +    if( pBt ){
 1.94001 +      assert( sqlite3BtreeHoldsMutex(pBt) );
 1.94002 +      rc = sqlite3BtreeSchemaLocked(pBt);
 1.94003 +      if( rc ){
 1.94004 +        const char *zDb = db->aDb[i].zName;
 1.94005 +        sqlite3Error(db, rc, "database schema is locked: %s", zDb);
 1.94006 +        testcase( db->flags & SQLITE_ReadUncommitted );
 1.94007 +        goto end_prepare;
 1.94008 +      }
 1.94009 +    }
 1.94010 +  }
 1.94011 +
 1.94012 +  sqlite3VtabUnlockList(db);
 1.94013 +
 1.94014 +  pParse->db = db;
 1.94015 +  pParse->nQueryLoop = (double)1;
 1.94016 +  if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
 1.94017 +    char *zSqlCopy;
 1.94018 +    int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
 1.94019 +    testcase( nBytes==mxLen );
 1.94020 +    testcase( nBytes==mxLen+1 );
 1.94021 +    if( nBytes>mxLen ){
 1.94022 +      sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
 1.94023 +      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
 1.94024 +      goto end_prepare;
 1.94025 +    }
 1.94026 +    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
 1.94027 +    if( zSqlCopy ){
 1.94028 +      sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
 1.94029 +      sqlite3DbFree(db, zSqlCopy);
 1.94030 +      pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
 1.94031 +    }else{
 1.94032 +      pParse->zTail = &zSql[nBytes];
 1.94033 +    }
 1.94034 +  }else{
 1.94035 +    sqlite3RunParser(pParse, zSql, &zErrMsg);
 1.94036 +  }
 1.94037 +  assert( 1==(int)pParse->nQueryLoop );
 1.94038 +
 1.94039 +  if( db->mallocFailed ){
 1.94040 +    pParse->rc = SQLITE_NOMEM;
 1.94041 +  }
 1.94042 +  if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
 1.94043 +  if( pParse->checkSchema ){
 1.94044 +    schemaIsValid(pParse);
 1.94045 +  }
 1.94046 +  if( db->mallocFailed ){
 1.94047 +    pParse->rc = SQLITE_NOMEM;
 1.94048 +  }
 1.94049 +  if( pzTail ){
 1.94050 +    *pzTail = pParse->zTail;
 1.94051 +  }
 1.94052 +  rc = pParse->rc;
 1.94053 +
 1.94054 +#ifndef SQLITE_OMIT_EXPLAIN
 1.94055 +  if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
 1.94056 +    static const char * const azColName[] = {
 1.94057 +       "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
 1.94058 +       "selectid", "order", "from", "detail"
 1.94059 +    };
 1.94060 +    int iFirst, mx;
 1.94061 +    if( pParse->explain==2 ){
 1.94062 +      sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
 1.94063 +      iFirst = 8;
 1.94064 +      mx = 12;
 1.94065 +    }else{
 1.94066 +      sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
 1.94067 +      iFirst = 0;
 1.94068 +      mx = 8;
 1.94069 +    }
 1.94070 +    for(i=iFirst; i<mx; i++){
 1.94071 +      sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
 1.94072 +                            azColName[i], SQLITE_STATIC);
 1.94073 +    }
 1.94074 +  }
 1.94075 +#endif
 1.94076 +
 1.94077 +  assert( db->init.busy==0 || saveSqlFlag==0 );
 1.94078 +  if( db->init.busy==0 ){
 1.94079 +    Vdbe *pVdbe = pParse->pVdbe;
 1.94080 +    sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
 1.94081 +  }
 1.94082 +  if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
 1.94083 +    sqlite3VdbeFinalize(pParse->pVdbe);
 1.94084 +    assert(!(*ppStmt));
 1.94085 +  }else{
 1.94086 +    *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
 1.94087 +  }
 1.94088 +
 1.94089 +  if( zErrMsg ){
 1.94090 +    sqlite3Error(db, rc, "%s", zErrMsg);
 1.94091 +    sqlite3DbFree(db, zErrMsg);
 1.94092 +  }else{
 1.94093 +    sqlite3Error(db, rc, 0);
 1.94094 +  }
 1.94095 +
 1.94096 +  /* Delete any TriggerPrg structures allocated while parsing this statement. */
 1.94097 +  while( pParse->pTriggerPrg ){
 1.94098 +    TriggerPrg *pT = pParse->pTriggerPrg;
 1.94099 +    pParse->pTriggerPrg = pT->pNext;
 1.94100 +    sqlite3DbFree(db, pT);
 1.94101 +  }
 1.94102 +
 1.94103 +end_prepare:
 1.94104 +
 1.94105 +  sqlite3StackFree(db, pParse);
 1.94106 +  rc = sqlite3ApiExit(db, rc);
 1.94107 +  assert( (rc&db->errMask)==rc );
 1.94108 +  return rc;
 1.94109 +}
 1.94110 +static int sqlite3LockAndPrepare(
 1.94111 +  sqlite3 *db,              /* Database handle. */
 1.94112 +  const char *zSql,         /* UTF-8 encoded SQL statement. */
 1.94113 +  int nBytes,               /* Length of zSql in bytes. */
 1.94114 +  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
 1.94115 +  Vdbe *pOld,               /* VM being reprepared */
 1.94116 +  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 1.94117 +  const char **pzTail       /* OUT: End of parsed string */
 1.94118 +){
 1.94119 +  int rc;
 1.94120 +  assert( ppStmt!=0 );
 1.94121 +  *ppStmt = 0;
 1.94122 +  if( !sqlite3SafetyCheckOk(db) ){
 1.94123 +    return SQLITE_MISUSE_BKPT;
 1.94124 +  }
 1.94125 +  sqlite3_mutex_enter(db->mutex);
 1.94126 +  sqlite3BtreeEnterAll(db);
 1.94127 +  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
 1.94128 +  if( rc==SQLITE_SCHEMA ){
 1.94129 +    sqlite3_finalize(*ppStmt);
 1.94130 +    rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
 1.94131 +  }
 1.94132 +  sqlite3BtreeLeaveAll(db);
 1.94133 +  sqlite3_mutex_leave(db->mutex);
 1.94134 +  assert( rc==SQLITE_OK || *ppStmt==0 );
 1.94135 +  return rc;
 1.94136 +}
 1.94137 +
 1.94138 +/*
 1.94139 +** Rerun the compilation of a statement after a schema change.
 1.94140 +**
 1.94141 +** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
 1.94142 +** if the statement cannot be recompiled because another connection has
 1.94143 +** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
 1.94144 +** occurs, return SQLITE_SCHEMA.
 1.94145 +*/
 1.94146 +SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
 1.94147 +  int rc;
 1.94148 +  sqlite3_stmt *pNew;
 1.94149 +  const char *zSql;
 1.94150 +  sqlite3 *db;
 1.94151 +
 1.94152 +  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
 1.94153 +  zSql = sqlite3_sql((sqlite3_stmt *)p);
 1.94154 +  assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
 1.94155 +  db = sqlite3VdbeDb(p);
 1.94156 +  assert( sqlite3_mutex_held(db->mutex) );
 1.94157 +  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
 1.94158 +  if( rc ){
 1.94159 +    if( rc==SQLITE_NOMEM ){
 1.94160 +      db->mallocFailed = 1;
 1.94161 +    }
 1.94162 +    assert( pNew==0 );
 1.94163 +    return rc;
 1.94164 +  }else{
 1.94165 +    assert( pNew!=0 );
 1.94166 +  }
 1.94167 +  sqlite3VdbeSwap((Vdbe*)pNew, p);
 1.94168 +  sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
 1.94169 +  sqlite3VdbeResetStepResult((Vdbe*)pNew);
 1.94170 +  sqlite3VdbeFinalize((Vdbe*)pNew);
 1.94171 +  return SQLITE_OK;
 1.94172 +}
 1.94173 +
 1.94174 +
 1.94175 +/*
 1.94176 +** Two versions of the official API.  Legacy and new use.  In the legacy
 1.94177 +** version, the original SQL text is not saved in the prepared statement
 1.94178 +** and so if a schema change occurs, SQLITE_SCHEMA is returned by
 1.94179 +** sqlite3_step().  In the new version, the original SQL text is retained
 1.94180 +** and the statement is automatically recompiled if an schema change
 1.94181 +** occurs.
 1.94182 +*/
 1.94183 +SQLITE_API int sqlite3_prepare(
 1.94184 +  sqlite3 *db,              /* Database handle. */
 1.94185 +  const char *zSql,         /* UTF-8 encoded SQL statement. */
 1.94186 +  int nBytes,               /* Length of zSql in bytes. */
 1.94187 +  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 1.94188 +  const char **pzTail       /* OUT: End of parsed string */
 1.94189 +){
 1.94190 +  int rc;
 1.94191 +  rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
 1.94192 +  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 1.94193 +  return rc;
 1.94194 +}
 1.94195 +SQLITE_API int sqlite3_prepare_v2(
 1.94196 +  sqlite3 *db,              /* Database handle. */
 1.94197 +  const char *zSql,         /* UTF-8 encoded SQL statement. */
 1.94198 +  int nBytes,               /* Length of zSql in bytes. */
 1.94199 +  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 1.94200 +  const char **pzTail       /* OUT: End of parsed string */
 1.94201 +){
 1.94202 +  int rc;
 1.94203 +  rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
 1.94204 +  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 1.94205 +  return rc;
 1.94206 +}
 1.94207 +
 1.94208 +
 1.94209 +#ifndef SQLITE_OMIT_UTF16
 1.94210 +/*
 1.94211 +** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
 1.94212 +*/
 1.94213 +static int sqlite3Prepare16(
 1.94214 +  sqlite3 *db,              /* Database handle. */ 
 1.94215 +  const void *zSql,         /* UTF-16 encoded SQL statement. */
 1.94216 +  int nBytes,               /* Length of zSql in bytes. */
 1.94217 +  int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
 1.94218 +  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 1.94219 +  const void **pzTail       /* OUT: End of parsed string */
 1.94220 +){
 1.94221 +  /* This function currently works by first transforming the UTF-16
 1.94222 +  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
 1.94223 +  ** tricky bit is figuring out the pointer to return in *pzTail.
 1.94224 +  */
 1.94225 +  char *zSql8;
 1.94226 +  const char *zTail8 = 0;
 1.94227 +  int rc = SQLITE_OK;
 1.94228 +
 1.94229 +  assert( ppStmt );
 1.94230 +  *ppStmt = 0;
 1.94231 +  if( !sqlite3SafetyCheckOk(db) ){
 1.94232 +    return SQLITE_MISUSE_BKPT;
 1.94233 +  }
 1.94234 +  sqlite3_mutex_enter(db->mutex);
 1.94235 +  zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
 1.94236 +  if( zSql8 ){
 1.94237 +    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
 1.94238 +  }
 1.94239 +
 1.94240 +  if( zTail8 && pzTail ){
 1.94241 +    /* If sqlite3_prepare returns a tail pointer, we calculate the
 1.94242 +    ** equivalent pointer into the UTF-16 string by counting the unicode
 1.94243 +    ** characters between zSql8 and zTail8, and then returning a pointer
 1.94244 +    ** the same number of characters into the UTF-16 string.
 1.94245 +    */
 1.94246 +    int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
 1.94247 +    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
 1.94248 +  }
 1.94249 +  sqlite3DbFree(db, zSql8); 
 1.94250 +  rc = sqlite3ApiExit(db, rc);
 1.94251 +  sqlite3_mutex_leave(db->mutex);
 1.94252 +  return rc;
 1.94253 +}
 1.94254 +
 1.94255 +/*
 1.94256 +** Two versions of the official API.  Legacy and new use.  In the legacy
 1.94257 +** version, the original SQL text is not saved in the prepared statement
 1.94258 +** and so if a schema change occurs, SQLITE_SCHEMA is returned by
 1.94259 +** sqlite3_step().  In the new version, the original SQL text is retained
 1.94260 +** and the statement is automatically recompiled if an schema change
 1.94261 +** occurs.
 1.94262 +*/
 1.94263 +SQLITE_API int sqlite3_prepare16(
 1.94264 +  sqlite3 *db,              /* Database handle. */ 
 1.94265 +  const void *zSql,         /* UTF-16 encoded SQL statement. */
 1.94266 +  int nBytes,               /* Length of zSql in bytes. */
 1.94267 +  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 1.94268 +  const void **pzTail       /* OUT: End of parsed string */
 1.94269 +){
 1.94270 +  int rc;
 1.94271 +  rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
 1.94272 +  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 1.94273 +  return rc;
 1.94274 +}
 1.94275 +SQLITE_API int sqlite3_prepare16_v2(
 1.94276 +  sqlite3 *db,              /* Database handle. */ 
 1.94277 +  const void *zSql,         /* UTF-16 encoded SQL statement. */
 1.94278 +  int nBytes,               /* Length of zSql in bytes. */
 1.94279 +  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 1.94280 +  const void **pzTail       /* OUT: End of parsed string */
 1.94281 +){
 1.94282 +  int rc;
 1.94283 +  rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
 1.94284 +  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 1.94285 +  return rc;
 1.94286 +}
 1.94287 +
 1.94288 +#endif /* SQLITE_OMIT_UTF16 */
 1.94289 +
 1.94290 +/************** End of prepare.c *********************************************/
 1.94291 +/************** Begin file select.c ******************************************/
 1.94292 +/*
 1.94293 +** 2001 September 15
 1.94294 +**
 1.94295 +** The author disclaims copyright to this source code.  In place of
 1.94296 +** a legal notice, here is a blessing:
 1.94297 +**
 1.94298 +**    May you do good and not evil.
 1.94299 +**    May you find forgiveness for yourself and forgive others.
 1.94300 +**    May you share freely, never taking more than you give.
 1.94301 +**
 1.94302 +*************************************************************************
 1.94303 +** This file contains C code routines that are called by the parser
 1.94304 +** to handle SELECT statements in SQLite.
 1.94305 +*/
 1.94306 +
 1.94307 +
 1.94308 +/*
 1.94309 +** Delete all the content of a Select structure but do not deallocate
 1.94310 +** the select structure itself.
 1.94311 +*/
 1.94312 +static void clearSelect(sqlite3 *db, Select *p){
 1.94313 +  sqlite3ExprListDelete(db, p->pEList);
 1.94314 +  sqlite3SrcListDelete(db, p->pSrc);
 1.94315 +  sqlite3ExprDelete(db, p->pWhere);
 1.94316 +  sqlite3ExprListDelete(db, p->pGroupBy);
 1.94317 +  sqlite3ExprDelete(db, p->pHaving);
 1.94318 +  sqlite3ExprListDelete(db, p->pOrderBy);
 1.94319 +  sqlite3SelectDelete(db, p->pPrior);
 1.94320 +  sqlite3ExprDelete(db, p->pLimit);
 1.94321 +  sqlite3ExprDelete(db, p->pOffset);
 1.94322 +}
 1.94323 +
 1.94324 +/*
 1.94325 +** Initialize a SelectDest structure.
 1.94326 +*/
 1.94327 +SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
 1.94328 +  pDest->eDest = (u8)eDest;
 1.94329 +  pDest->iSDParm = iParm;
 1.94330 +  pDest->affSdst = 0;
 1.94331 +  pDest->iSdst = 0;
 1.94332 +  pDest->nSdst = 0;
 1.94333 +}
 1.94334 +
 1.94335 +
 1.94336 +/*
 1.94337 +** Allocate a new Select structure and return a pointer to that
 1.94338 +** structure.
 1.94339 +*/
 1.94340 +SQLITE_PRIVATE Select *sqlite3SelectNew(
 1.94341 +  Parse *pParse,        /* Parsing context */
 1.94342 +  ExprList *pEList,     /* which columns to include in the result */
 1.94343 +  SrcList *pSrc,        /* the FROM clause -- which tables to scan */
 1.94344 +  Expr *pWhere,         /* the WHERE clause */
 1.94345 +  ExprList *pGroupBy,   /* the GROUP BY clause */
 1.94346 +  Expr *pHaving,        /* the HAVING clause */
 1.94347 +  ExprList *pOrderBy,   /* the ORDER BY clause */
 1.94348 +  int isDistinct,       /* true if the DISTINCT keyword is present */
 1.94349 +  Expr *pLimit,         /* LIMIT value.  NULL means not used */
 1.94350 +  Expr *pOffset         /* OFFSET value.  NULL means no offset */
 1.94351 +){
 1.94352 +  Select *pNew;
 1.94353 +  Select standin;
 1.94354 +  sqlite3 *db = pParse->db;
 1.94355 +  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
 1.94356 +  assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
 1.94357 +  if( pNew==0 ){
 1.94358 +    assert( db->mallocFailed );
 1.94359 +    pNew = &standin;
 1.94360 +    memset(pNew, 0, sizeof(*pNew));
 1.94361 +  }
 1.94362 +  if( pEList==0 ){
 1.94363 +    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
 1.94364 +  }
 1.94365 +  pNew->pEList = pEList;
 1.94366 +  if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
 1.94367 +  pNew->pSrc = pSrc;
 1.94368 +  pNew->pWhere = pWhere;
 1.94369 +  pNew->pGroupBy = pGroupBy;
 1.94370 +  pNew->pHaving = pHaving;
 1.94371 +  pNew->pOrderBy = pOrderBy;
 1.94372 +  pNew->selFlags = isDistinct ? SF_Distinct : 0;
 1.94373 +  pNew->op = TK_SELECT;
 1.94374 +  pNew->pLimit = pLimit;
 1.94375 +  pNew->pOffset = pOffset;
 1.94376 +  assert( pOffset==0 || pLimit!=0 );
 1.94377 +  pNew->addrOpenEphm[0] = -1;
 1.94378 +  pNew->addrOpenEphm[1] = -1;
 1.94379 +  pNew->addrOpenEphm[2] = -1;
 1.94380 +  if( db->mallocFailed ) {
 1.94381 +    clearSelect(db, pNew);
 1.94382 +    if( pNew!=&standin ) sqlite3DbFree(db, pNew);
 1.94383 +    pNew = 0;
 1.94384 +  }else{
 1.94385 +    assert( pNew->pSrc!=0 || pParse->nErr>0 );
 1.94386 +  }
 1.94387 +  assert( pNew!=&standin );
 1.94388 +  return pNew;
 1.94389 +}
 1.94390 +
 1.94391 +/*
 1.94392 +** Delete the given Select structure and all of its substructures.
 1.94393 +*/
 1.94394 +SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
 1.94395 +  if( p ){
 1.94396 +    clearSelect(db, p);
 1.94397 +    sqlite3DbFree(db, p);
 1.94398 +  }
 1.94399 +}
 1.94400 +
 1.94401 +/*
 1.94402 +** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
 1.94403 +** type of join.  Return an integer constant that expresses that type
 1.94404 +** in terms of the following bit values:
 1.94405 +**
 1.94406 +**     JT_INNER
 1.94407 +**     JT_CROSS
 1.94408 +**     JT_OUTER
 1.94409 +**     JT_NATURAL
 1.94410 +**     JT_LEFT
 1.94411 +**     JT_RIGHT
 1.94412 +**
 1.94413 +** A full outer join is the combination of JT_LEFT and JT_RIGHT.
 1.94414 +**
 1.94415 +** If an illegal or unsupported join type is seen, then still return
 1.94416 +** a join type, but put an error in the pParse structure.
 1.94417 +*/
 1.94418 +SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
 1.94419 +  int jointype = 0;
 1.94420 +  Token *apAll[3];
 1.94421 +  Token *p;
 1.94422 +                             /*   0123456789 123456789 123456789 123 */
 1.94423 +  static const char zKeyText[] = "naturaleftouterightfullinnercross";
 1.94424 +  static const struct {
 1.94425 +    u8 i;        /* Beginning of keyword text in zKeyText[] */
 1.94426 +    u8 nChar;    /* Length of the keyword in characters */
 1.94427 +    u8 code;     /* Join type mask */
 1.94428 +  } aKeyword[] = {
 1.94429 +    /* natural */ { 0,  7, JT_NATURAL                },
 1.94430 +    /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
 1.94431 +    /* outer   */ { 10, 5, JT_OUTER                  },
 1.94432 +    /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
 1.94433 +    /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
 1.94434 +    /* inner   */ { 23, 5, JT_INNER                  },
 1.94435 +    /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
 1.94436 +  };
 1.94437 +  int i, j;
 1.94438 +  apAll[0] = pA;
 1.94439 +  apAll[1] = pB;
 1.94440 +  apAll[2] = pC;
 1.94441 +  for(i=0; i<3 && apAll[i]; i++){
 1.94442 +    p = apAll[i];
 1.94443 +    for(j=0; j<ArraySize(aKeyword); j++){
 1.94444 +      if( p->n==aKeyword[j].nChar 
 1.94445 +          && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
 1.94446 +        jointype |= aKeyword[j].code;
 1.94447 +        break;
 1.94448 +      }
 1.94449 +    }
 1.94450 +    testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
 1.94451 +    if( j>=ArraySize(aKeyword) ){
 1.94452 +      jointype |= JT_ERROR;
 1.94453 +      break;
 1.94454 +    }
 1.94455 +  }
 1.94456 +  if(
 1.94457 +     (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
 1.94458 +     (jointype & JT_ERROR)!=0
 1.94459 +  ){
 1.94460 +    const char *zSp = " ";
 1.94461 +    assert( pB!=0 );
 1.94462 +    if( pC==0 ){ zSp++; }
 1.94463 +    sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
 1.94464 +       "%T %T%s%T", pA, pB, zSp, pC);
 1.94465 +    jointype = JT_INNER;
 1.94466 +  }else if( (jointype & JT_OUTER)!=0 
 1.94467 +         && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
 1.94468 +    sqlite3ErrorMsg(pParse, 
 1.94469 +      "RIGHT and FULL OUTER JOINs are not currently supported");
 1.94470 +    jointype = JT_INNER;
 1.94471 +  }
 1.94472 +  return jointype;
 1.94473 +}
 1.94474 +
 1.94475 +/*
 1.94476 +** Return the index of a column in a table.  Return -1 if the column
 1.94477 +** is not contained in the table.
 1.94478 +*/
 1.94479 +static int columnIndex(Table *pTab, const char *zCol){
 1.94480 +  int i;
 1.94481 +  for(i=0; i<pTab->nCol; i++){
 1.94482 +    if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
 1.94483 +  }
 1.94484 +  return -1;
 1.94485 +}
 1.94486 +
 1.94487 +/*
 1.94488 +** Search the first N tables in pSrc, from left to right, looking for a
 1.94489 +** table that has a column named zCol.  
 1.94490 +**
 1.94491 +** When found, set *piTab and *piCol to the table index and column index
 1.94492 +** of the matching column and return TRUE.
 1.94493 +**
 1.94494 +** If not found, return FALSE.
 1.94495 +*/
 1.94496 +static int tableAndColumnIndex(
 1.94497 +  SrcList *pSrc,       /* Array of tables to search */
 1.94498 +  int N,               /* Number of tables in pSrc->a[] to search */
 1.94499 +  const char *zCol,    /* Name of the column we are looking for */
 1.94500 +  int *piTab,          /* Write index of pSrc->a[] here */
 1.94501 +  int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
 1.94502 +){
 1.94503 +  int i;               /* For looping over tables in pSrc */
 1.94504 +  int iCol;            /* Index of column matching zCol */
 1.94505 +
 1.94506 +  assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
 1.94507 +  for(i=0; i<N; i++){
 1.94508 +    iCol = columnIndex(pSrc->a[i].pTab, zCol);
 1.94509 +    if( iCol>=0 ){
 1.94510 +      if( piTab ){
 1.94511 +        *piTab = i;
 1.94512 +        *piCol = iCol;
 1.94513 +      }
 1.94514 +      return 1;
 1.94515 +    }
 1.94516 +  }
 1.94517 +  return 0;
 1.94518 +}
 1.94519 +
 1.94520 +/*
 1.94521 +** This function is used to add terms implied by JOIN syntax to the
 1.94522 +** WHERE clause expression of a SELECT statement. The new term, which
 1.94523 +** is ANDed with the existing WHERE clause, is of the form:
 1.94524 +**
 1.94525 +**    (tab1.col1 = tab2.col2)
 1.94526 +**
 1.94527 +** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the 
 1.94528 +** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
 1.94529 +** column iColRight of tab2.
 1.94530 +*/
 1.94531 +static void addWhereTerm(
 1.94532 +  Parse *pParse,                  /* Parsing context */
 1.94533 +  SrcList *pSrc,                  /* List of tables in FROM clause */
 1.94534 +  int iLeft,                      /* Index of first table to join in pSrc */
 1.94535 +  int iColLeft,                   /* Index of column in first table */
 1.94536 +  int iRight,                     /* Index of second table in pSrc */
 1.94537 +  int iColRight,                  /* Index of column in second table */
 1.94538 +  int isOuterJoin,                /* True if this is an OUTER join */
 1.94539 +  Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
 1.94540 +){
 1.94541 +  sqlite3 *db = pParse->db;
 1.94542 +  Expr *pE1;
 1.94543 +  Expr *pE2;
 1.94544 +  Expr *pEq;
 1.94545 +
 1.94546 +  assert( iLeft<iRight );
 1.94547 +  assert( pSrc->nSrc>iRight );
 1.94548 +  assert( pSrc->a[iLeft].pTab );
 1.94549 +  assert( pSrc->a[iRight].pTab );
 1.94550 +
 1.94551 +  pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
 1.94552 +  pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
 1.94553 +
 1.94554 +  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
 1.94555 +  if( pEq && isOuterJoin ){
 1.94556 +    ExprSetProperty(pEq, EP_FromJoin);
 1.94557 +    assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
 1.94558 +    ExprSetIrreducible(pEq);
 1.94559 +    pEq->iRightJoinTable = (i16)pE2->iTable;
 1.94560 +  }
 1.94561 +  *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
 1.94562 +}
 1.94563 +
 1.94564 +/*
 1.94565 +** Set the EP_FromJoin property on all terms of the given expression.
 1.94566 +** And set the Expr.iRightJoinTable to iTable for every term in the
 1.94567 +** expression.
 1.94568 +**
 1.94569 +** The EP_FromJoin property is used on terms of an expression to tell
 1.94570 +** the LEFT OUTER JOIN processing logic that this term is part of the
 1.94571 +** join restriction specified in the ON or USING clause and not a part
 1.94572 +** of the more general WHERE clause.  These terms are moved over to the
 1.94573 +** WHERE clause during join processing but we need to remember that they
 1.94574 +** originated in the ON or USING clause.
 1.94575 +**
 1.94576 +** The Expr.iRightJoinTable tells the WHERE clause processing that the
 1.94577 +** expression depends on table iRightJoinTable even if that table is not
 1.94578 +** explicitly mentioned in the expression.  That information is needed
 1.94579 +** for cases like this:
 1.94580 +**
 1.94581 +**    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
 1.94582 +**
 1.94583 +** The where clause needs to defer the handling of the t1.x=5
 1.94584 +** term until after the t2 loop of the join.  In that way, a
 1.94585 +** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
 1.94586 +** defer the handling of t1.x=5, it will be processed immediately
 1.94587 +** after the t1 loop and rows with t1.x!=5 will never appear in
 1.94588 +** the output, which is incorrect.
 1.94589 +*/
 1.94590 +static void setJoinExpr(Expr *p, int iTable){
 1.94591 +  while( p ){
 1.94592 +    ExprSetProperty(p, EP_FromJoin);
 1.94593 +    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
 1.94594 +    ExprSetIrreducible(p);
 1.94595 +    p->iRightJoinTable = (i16)iTable;
 1.94596 +    setJoinExpr(p->pLeft, iTable);
 1.94597 +    p = p->pRight;
 1.94598 +  } 
 1.94599 +}
 1.94600 +
 1.94601 +/*
 1.94602 +** This routine processes the join information for a SELECT statement.
 1.94603 +** ON and USING clauses are converted into extra terms of the WHERE clause.
 1.94604 +** NATURAL joins also create extra WHERE clause terms.
 1.94605 +**
 1.94606 +** The terms of a FROM clause are contained in the Select.pSrc structure.
 1.94607 +** The left most table is the first entry in Select.pSrc.  The right-most
 1.94608 +** table is the last entry.  The join operator is held in the entry to
 1.94609 +** the left.  Thus entry 0 contains the join operator for the join between
 1.94610 +** entries 0 and 1.  Any ON or USING clauses associated with the join are
 1.94611 +** also attached to the left entry.
 1.94612 +**
 1.94613 +** This routine returns the number of errors encountered.
 1.94614 +*/
 1.94615 +static int sqliteProcessJoin(Parse *pParse, Select *p){
 1.94616 +  SrcList *pSrc;                  /* All tables in the FROM clause */
 1.94617 +  int i, j;                       /* Loop counters */
 1.94618 +  struct SrcList_item *pLeft;     /* Left table being joined */
 1.94619 +  struct SrcList_item *pRight;    /* Right table being joined */
 1.94620 +
 1.94621 +  pSrc = p->pSrc;
 1.94622 +  pLeft = &pSrc->a[0];
 1.94623 +  pRight = &pLeft[1];
 1.94624 +  for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
 1.94625 +    Table *pLeftTab = pLeft->pTab;
 1.94626 +    Table *pRightTab = pRight->pTab;
 1.94627 +    int isOuter;
 1.94628 +
 1.94629 +    if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
 1.94630 +    isOuter = (pRight->jointype & JT_OUTER)!=0;
 1.94631 +
 1.94632 +    /* When the NATURAL keyword is present, add WHERE clause terms for
 1.94633 +    ** every column that the two tables have in common.
 1.94634 +    */
 1.94635 +    if( pRight->jointype & JT_NATURAL ){
 1.94636 +      if( pRight->pOn || pRight->pUsing ){
 1.94637 +        sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
 1.94638 +           "an ON or USING clause", 0);
 1.94639 +        return 1;
 1.94640 +      }
 1.94641 +      for(j=0; j<pRightTab->nCol; j++){
 1.94642 +        char *zName;   /* Name of column in the right table */
 1.94643 +        int iLeft;     /* Matching left table */
 1.94644 +        int iLeftCol;  /* Matching column in the left table */
 1.94645 +
 1.94646 +        zName = pRightTab->aCol[j].zName;
 1.94647 +        if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
 1.94648 +          addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
 1.94649 +                       isOuter, &p->pWhere);
 1.94650 +        }
 1.94651 +      }
 1.94652 +    }
 1.94653 +
 1.94654 +    /* Disallow both ON and USING clauses in the same join
 1.94655 +    */
 1.94656 +    if( pRight->pOn && pRight->pUsing ){
 1.94657 +      sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
 1.94658 +        "clauses in the same join");
 1.94659 +      return 1;
 1.94660 +    }
 1.94661 +
 1.94662 +    /* Add the ON clause to the end of the WHERE clause, connected by
 1.94663 +    ** an AND operator.
 1.94664 +    */
 1.94665 +    if( pRight->pOn ){
 1.94666 +      if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
 1.94667 +      p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
 1.94668 +      pRight->pOn = 0;
 1.94669 +    }
 1.94670 +
 1.94671 +    /* Create extra terms on the WHERE clause for each column named
 1.94672 +    ** in the USING clause.  Example: If the two tables to be joined are 
 1.94673 +    ** A and B and the USING clause names X, Y, and Z, then add this
 1.94674 +    ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
 1.94675 +    ** Report an error if any column mentioned in the USING clause is
 1.94676 +    ** not contained in both tables to be joined.
 1.94677 +    */
 1.94678 +    if( pRight->pUsing ){
 1.94679 +      IdList *pList = pRight->pUsing;
 1.94680 +      for(j=0; j<pList->nId; j++){
 1.94681 +        char *zName;     /* Name of the term in the USING clause */
 1.94682 +        int iLeft;       /* Table on the left with matching column name */
 1.94683 +        int iLeftCol;    /* Column number of matching column on the left */
 1.94684 +        int iRightCol;   /* Column number of matching column on the right */
 1.94685 +
 1.94686 +        zName = pList->a[j].zName;
 1.94687 +        iRightCol = columnIndex(pRightTab, zName);
 1.94688 +        if( iRightCol<0
 1.94689 +         || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
 1.94690 +        ){
 1.94691 +          sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
 1.94692 +            "not present in both tables", zName);
 1.94693 +          return 1;
 1.94694 +        }
 1.94695 +        addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
 1.94696 +                     isOuter, &p->pWhere);
 1.94697 +      }
 1.94698 +    }
 1.94699 +  }
 1.94700 +  return 0;
 1.94701 +}
 1.94702 +
 1.94703 +/*
 1.94704 +** Insert code into "v" that will push the record on the top of the
 1.94705 +** stack into the sorter.
 1.94706 +*/
 1.94707 +static void pushOntoSorter(
 1.94708 +  Parse *pParse,         /* Parser context */
 1.94709 +  ExprList *pOrderBy,    /* The ORDER BY clause */
 1.94710 +  Select *pSelect,       /* The whole SELECT statement */
 1.94711 +  int regData            /* Register holding data to be sorted */
 1.94712 +){
 1.94713 +  Vdbe *v = pParse->pVdbe;
 1.94714 +  int nExpr = pOrderBy->nExpr;
 1.94715 +  int regBase = sqlite3GetTempRange(pParse, nExpr+2);
 1.94716 +  int regRecord = sqlite3GetTempReg(pParse);
 1.94717 +  int op;
 1.94718 +  sqlite3ExprCacheClear(pParse);
 1.94719 +  sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
 1.94720 +  sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
 1.94721 +  sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
 1.94722 +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
 1.94723 +  if( pSelect->selFlags & SF_UseSorter ){
 1.94724 +    op = OP_SorterInsert;
 1.94725 +  }else{
 1.94726 +    op = OP_IdxInsert;
 1.94727 +  }
 1.94728 +  sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
 1.94729 +  sqlite3ReleaseTempReg(pParse, regRecord);
 1.94730 +  sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
 1.94731 +  if( pSelect->iLimit ){
 1.94732 +    int addr1, addr2;
 1.94733 +    int iLimit;
 1.94734 +    if( pSelect->iOffset ){
 1.94735 +      iLimit = pSelect->iOffset+1;
 1.94736 +    }else{
 1.94737 +      iLimit = pSelect->iLimit;
 1.94738 +    }
 1.94739 +    addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
 1.94740 +    sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
 1.94741 +    addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
 1.94742 +    sqlite3VdbeJumpHere(v, addr1);
 1.94743 +    sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
 1.94744 +    sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
 1.94745 +    sqlite3VdbeJumpHere(v, addr2);
 1.94746 +  }
 1.94747 +}
 1.94748 +
 1.94749 +/*
 1.94750 +** Add code to implement the OFFSET
 1.94751 +*/
 1.94752 +static void codeOffset(
 1.94753 +  Vdbe *v,          /* Generate code into this VM */
 1.94754 +  Select *p,        /* The SELECT statement being coded */
 1.94755 +  int iContinue     /* Jump here to skip the current record */
 1.94756 +){
 1.94757 +  if( p->iOffset && iContinue!=0 ){
 1.94758 +    int addr;
 1.94759 +    sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
 1.94760 +    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
 1.94761 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
 1.94762 +    VdbeComment((v, "skip OFFSET records"));
 1.94763 +    sqlite3VdbeJumpHere(v, addr);
 1.94764 +  }
 1.94765 +}
 1.94766 +
 1.94767 +/*
 1.94768 +** Add code that will check to make sure the N registers starting at iMem
 1.94769 +** form a distinct entry.  iTab is a sorting index that holds previously
 1.94770 +** seen combinations of the N values.  A new entry is made in iTab
 1.94771 +** if the current N values are new.
 1.94772 +**
 1.94773 +** A jump to addrRepeat is made and the N+1 values are popped from the
 1.94774 +** stack if the top N elements are not distinct.
 1.94775 +*/
 1.94776 +static void codeDistinct(
 1.94777 +  Parse *pParse,     /* Parsing and code generating context */
 1.94778 +  int iTab,          /* A sorting index used to test for distinctness */
 1.94779 +  int addrRepeat,    /* Jump to here if not distinct */
 1.94780 +  int N,             /* Number of elements */
 1.94781 +  int iMem           /* First element */
 1.94782 +){
 1.94783 +  Vdbe *v;
 1.94784 +  int r1;
 1.94785 +
 1.94786 +  v = pParse->pVdbe;
 1.94787 +  r1 = sqlite3GetTempReg(pParse);
 1.94788 +  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
 1.94789 +  sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
 1.94790 +  sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
 1.94791 +  sqlite3ReleaseTempReg(pParse, r1);
 1.94792 +}
 1.94793 +
 1.94794 +#ifndef SQLITE_OMIT_SUBQUERY
 1.94795 +/*
 1.94796 +** Generate an error message when a SELECT is used within a subexpression
 1.94797 +** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
 1.94798 +** column.  We do this in a subroutine because the error used to occur
 1.94799 +** in multiple places.  (The error only occurs in one place now, but we
 1.94800 +** retain the subroutine to minimize code disruption.)
 1.94801 +*/
 1.94802 +static int checkForMultiColumnSelectError(
 1.94803 +  Parse *pParse,       /* Parse context. */
 1.94804 +  SelectDest *pDest,   /* Destination of SELECT results */
 1.94805 +  int nExpr            /* Number of result columns returned by SELECT */
 1.94806 +){
 1.94807 +  int eDest = pDest->eDest;
 1.94808 +  if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
 1.94809 +    sqlite3ErrorMsg(pParse, "only a single result allowed for "
 1.94810 +       "a SELECT that is part of an expression");
 1.94811 +    return 1;
 1.94812 +  }else{
 1.94813 +    return 0;
 1.94814 +  }
 1.94815 +}
 1.94816 +#endif
 1.94817 +
 1.94818 +/*
 1.94819 +** An instance of the following object is used to record information about
 1.94820 +** how to process the DISTINCT keyword, to simplify passing that information
 1.94821 +** into the selectInnerLoop() routine.
 1.94822 +*/
 1.94823 +typedef struct DistinctCtx DistinctCtx;
 1.94824 +struct DistinctCtx {
 1.94825 +  u8 isTnct;      /* True if the DISTINCT keyword is present */
 1.94826 +  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
 1.94827 +  int tabTnct;    /* Ephemeral table used for DISTINCT processing */
 1.94828 +  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
 1.94829 +};
 1.94830 +
 1.94831 +/*
 1.94832 +** This routine generates the code for the inside of the inner loop
 1.94833 +** of a SELECT.
 1.94834 +**
 1.94835 +** If srcTab and nColumn are both zero, then the pEList expressions
 1.94836 +** are evaluated in order to get the data for this row.  If nColumn>0
 1.94837 +** then data is pulled from srcTab and pEList is used only to get the
 1.94838 +** datatypes for each column.
 1.94839 +*/
 1.94840 +static void selectInnerLoop(
 1.94841 +  Parse *pParse,          /* The parser context */
 1.94842 +  Select *p,              /* The complete select statement being coded */
 1.94843 +  ExprList *pEList,       /* List of values being extracted */
 1.94844 +  int srcTab,             /* Pull data from this table */
 1.94845 +  int nColumn,            /* Number of columns in the source table */
 1.94846 +  ExprList *pOrderBy,     /* If not NULL, sort results using this key */
 1.94847 +  DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
 1.94848 +  SelectDest *pDest,      /* How to dispose of the results */
 1.94849 +  int iContinue,          /* Jump here to continue with next row */
 1.94850 +  int iBreak              /* Jump here to break out of the inner loop */
 1.94851 +){
 1.94852 +  Vdbe *v = pParse->pVdbe;
 1.94853 +  int i;
 1.94854 +  int hasDistinct;        /* True if the DISTINCT keyword is present */
 1.94855 +  int regResult;              /* Start of memory holding result set */
 1.94856 +  int eDest = pDest->eDest;   /* How to dispose of results */
 1.94857 +  int iParm = pDest->iSDParm; /* First argument to disposal method */
 1.94858 +  int nResultCol;             /* Number of result columns */
 1.94859 +
 1.94860 +  assert( v );
 1.94861 +  if( NEVER(v==0) ) return;
 1.94862 +  assert( pEList!=0 );
 1.94863 +  hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
 1.94864 +  if( pOrderBy==0 && !hasDistinct ){
 1.94865 +    codeOffset(v, p, iContinue);
 1.94866 +  }
 1.94867 +
 1.94868 +  /* Pull the requested columns.
 1.94869 +  */
 1.94870 +  if( nColumn>0 ){
 1.94871 +    nResultCol = nColumn;
 1.94872 +  }else{
 1.94873 +    nResultCol = pEList->nExpr;
 1.94874 +  }
 1.94875 +  if( pDest->iSdst==0 ){
 1.94876 +    pDest->iSdst = pParse->nMem+1;
 1.94877 +    pDest->nSdst = nResultCol;
 1.94878 +    pParse->nMem += nResultCol;
 1.94879 +  }else{ 
 1.94880 +    assert( pDest->nSdst==nResultCol );
 1.94881 +  }
 1.94882 +  regResult = pDest->iSdst;
 1.94883 +  if( nColumn>0 ){
 1.94884 +    for(i=0; i<nColumn; i++){
 1.94885 +      sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
 1.94886 +    }
 1.94887 +  }else if( eDest!=SRT_Exists ){
 1.94888 +    /* If the destination is an EXISTS(...) expression, the actual
 1.94889 +    ** values returned by the SELECT are not required.
 1.94890 +    */
 1.94891 +    sqlite3ExprCacheClear(pParse);
 1.94892 +    sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
 1.94893 +  }
 1.94894 +  nColumn = nResultCol;
 1.94895 +
 1.94896 +  /* If the DISTINCT keyword was present on the SELECT statement
 1.94897 +  ** and this row has been seen before, then do not make this row
 1.94898 +  ** part of the result.
 1.94899 +  */
 1.94900 +  if( hasDistinct ){
 1.94901 +    assert( pEList!=0 );
 1.94902 +    assert( pEList->nExpr==nColumn );
 1.94903 +    switch( pDistinct->eTnctType ){
 1.94904 +      case WHERE_DISTINCT_ORDERED: {
 1.94905 +        VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
 1.94906 +        int iJump;              /* Jump destination */
 1.94907 +        int regPrev;            /* Previous row content */
 1.94908 +
 1.94909 +        /* Allocate space for the previous row */
 1.94910 +        regPrev = pParse->nMem+1;
 1.94911 +        pParse->nMem += nColumn;
 1.94912 +
 1.94913 +        /* Change the OP_OpenEphemeral coded earlier to an OP_Null
 1.94914 +        ** sets the MEM_Cleared bit on the first register of the
 1.94915 +        ** previous value.  This will cause the OP_Ne below to always
 1.94916 +        ** fail on the first iteration of the loop even if the first
 1.94917 +        ** row is all NULLs.
 1.94918 +        */
 1.94919 +        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 1.94920 +        pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
 1.94921 +        pOp->opcode = OP_Null;
 1.94922 +        pOp->p1 = 1;
 1.94923 +        pOp->p2 = regPrev;
 1.94924 +
 1.94925 +        iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
 1.94926 +        for(i=0; i<nColumn; i++){
 1.94927 +          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
 1.94928 +          if( i<nColumn-1 ){
 1.94929 +            sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
 1.94930 +          }else{
 1.94931 +            sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
 1.94932 +          }
 1.94933 +          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
 1.94934 +          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 1.94935 +        }
 1.94936 +        assert( sqlite3VdbeCurrentAddr(v)==iJump );
 1.94937 +        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
 1.94938 +        break;
 1.94939 +      }
 1.94940 +
 1.94941 +      case WHERE_DISTINCT_UNIQUE: {
 1.94942 +        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 1.94943 +        break;
 1.94944 +      }
 1.94945 +
 1.94946 +      default: {
 1.94947 +        assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
 1.94948 +        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
 1.94949 +        break;
 1.94950 +      }
 1.94951 +    }
 1.94952 +    if( pOrderBy==0 ){
 1.94953 +      codeOffset(v, p, iContinue);
 1.94954 +    }
 1.94955 +  }
 1.94956 +
 1.94957 +  switch( eDest ){
 1.94958 +    /* In this mode, write each query result to the key of the temporary
 1.94959 +    ** table iParm.
 1.94960 +    */
 1.94961 +#ifndef SQLITE_OMIT_COMPOUND_SELECT
 1.94962 +    case SRT_Union: {
 1.94963 +      int r1;
 1.94964 +      r1 = sqlite3GetTempReg(pParse);
 1.94965 +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
 1.94966 +      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 1.94967 +      sqlite3ReleaseTempReg(pParse, r1);
 1.94968 +      break;
 1.94969 +    }
 1.94970 +
 1.94971 +    /* Construct a record from the query result, but instead of
 1.94972 +    ** saving that record, use it as a key to delete elements from
 1.94973 +    ** the temporary table iParm.
 1.94974 +    */
 1.94975 +    case SRT_Except: {
 1.94976 +      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
 1.94977 +      break;
 1.94978 +    }
 1.94979 +#endif
 1.94980 +
 1.94981 +    /* Store the result as data using a unique key.
 1.94982 +    */
 1.94983 +    case SRT_Table:
 1.94984 +    case SRT_EphemTab: {
 1.94985 +      int r1 = sqlite3GetTempReg(pParse);
 1.94986 +      testcase( eDest==SRT_Table );
 1.94987 +      testcase( eDest==SRT_EphemTab );
 1.94988 +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
 1.94989 +      if( pOrderBy ){
 1.94990 +        pushOntoSorter(pParse, pOrderBy, p, r1);
 1.94991 +      }else{
 1.94992 +        int r2 = sqlite3GetTempReg(pParse);
 1.94993 +        sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
 1.94994 +        sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
 1.94995 +        sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.94996 +        sqlite3ReleaseTempReg(pParse, r2);
 1.94997 +      }
 1.94998 +      sqlite3ReleaseTempReg(pParse, r1);
 1.94999 +      break;
 1.95000 +    }
 1.95001 +
 1.95002 +#ifndef SQLITE_OMIT_SUBQUERY
 1.95003 +    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 1.95004 +    ** then there should be a single item on the stack.  Write this
 1.95005 +    ** item into the set table with bogus data.
 1.95006 +    */
 1.95007 +    case SRT_Set: {
 1.95008 +      assert( nColumn==1 );
 1.95009 +      pDest->affSdst =
 1.95010 +                  sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
 1.95011 +      if( pOrderBy ){
 1.95012 +        /* At first glance you would think we could optimize out the
 1.95013 +        ** ORDER BY in this case since the order of entries in the set
 1.95014 +        ** does not matter.  But there might be a LIMIT clause, in which
 1.95015 +        ** case the order does matter */
 1.95016 +        pushOntoSorter(pParse, pOrderBy, p, regResult);
 1.95017 +      }else{
 1.95018 +        int r1 = sqlite3GetTempReg(pParse);
 1.95019 +        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
 1.95020 +        sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
 1.95021 +        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 1.95022 +        sqlite3ReleaseTempReg(pParse, r1);
 1.95023 +      }
 1.95024 +      break;
 1.95025 +    }
 1.95026 +
 1.95027 +    /* If any row exist in the result set, record that fact and abort.
 1.95028 +    */
 1.95029 +    case SRT_Exists: {
 1.95030 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
 1.95031 +      /* The LIMIT clause will terminate the loop for us */
 1.95032 +      break;
 1.95033 +    }
 1.95034 +
 1.95035 +    /* If this is a scalar select that is part of an expression, then
 1.95036 +    ** store the results in the appropriate memory cell and break out
 1.95037 +    ** of the scan loop.
 1.95038 +    */
 1.95039 +    case SRT_Mem: {
 1.95040 +      assert( nColumn==1 );
 1.95041 +      if( pOrderBy ){
 1.95042 +        pushOntoSorter(pParse, pOrderBy, p, regResult);
 1.95043 +      }else{
 1.95044 +        sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
 1.95045 +        /* The LIMIT clause will jump out of the loop for us */
 1.95046 +      }
 1.95047 +      break;
 1.95048 +    }
 1.95049 +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 1.95050 +
 1.95051 +    /* Send the data to the callback function or to a subroutine.  In the
 1.95052 +    ** case of a subroutine, the subroutine itself is responsible for
 1.95053 +    ** popping the data from the stack.
 1.95054 +    */
 1.95055 +    case SRT_Coroutine:
 1.95056 +    case SRT_Output: {
 1.95057 +      testcase( eDest==SRT_Coroutine );
 1.95058 +      testcase( eDest==SRT_Output );
 1.95059 +      if( pOrderBy ){
 1.95060 +        int r1 = sqlite3GetTempReg(pParse);
 1.95061 +        sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
 1.95062 +        pushOntoSorter(pParse, pOrderBy, p, r1);
 1.95063 +        sqlite3ReleaseTempReg(pParse, r1);
 1.95064 +      }else if( eDest==SRT_Coroutine ){
 1.95065 +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 1.95066 +      }else{
 1.95067 +        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
 1.95068 +        sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
 1.95069 +      }
 1.95070 +      break;
 1.95071 +    }
 1.95072 +
 1.95073 +#if !defined(SQLITE_OMIT_TRIGGER)
 1.95074 +    /* Discard the results.  This is used for SELECT statements inside
 1.95075 +    ** the body of a TRIGGER.  The purpose of such selects is to call
 1.95076 +    ** user-defined functions that have side effects.  We do not care
 1.95077 +    ** about the actual results of the select.
 1.95078 +    */
 1.95079 +    default: {
 1.95080 +      assert( eDest==SRT_Discard );
 1.95081 +      break;
 1.95082 +    }
 1.95083 +#endif
 1.95084 +  }
 1.95085 +
 1.95086 +  /* Jump to the end of the loop if the LIMIT is reached.  Except, if
 1.95087 +  ** there is a sorter, in which case the sorter has already limited
 1.95088 +  ** the output for us.
 1.95089 +  */
 1.95090 +  if( pOrderBy==0 && p->iLimit ){
 1.95091 +    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
 1.95092 +  }
 1.95093 +}
 1.95094 +
 1.95095 +/*
 1.95096 +** Given an expression list, generate a KeyInfo structure that records
 1.95097 +** the collating sequence for each expression in that expression list.
 1.95098 +**
 1.95099 +** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
 1.95100 +** KeyInfo structure is appropriate for initializing a virtual index to
 1.95101 +** implement that clause.  If the ExprList is the result set of a SELECT
 1.95102 +** then the KeyInfo structure is appropriate for initializing a virtual
 1.95103 +** index to implement a DISTINCT test.
 1.95104 +**
 1.95105 +** Space to hold the KeyInfo structure is obtain from malloc.  The calling
 1.95106 +** function is responsible for seeing that this structure is eventually
 1.95107 +** freed.  Add the KeyInfo structure to the P4 field of an opcode using
 1.95108 +** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
 1.95109 +*/
 1.95110 +static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
 1.95111 +  sqlite3 *db = pParse->db;
 1.95112 +  int nExpr;
 1.95113 +  KeyInfo *pInfo;
 1.95114 +  struct ExprList_item *pItem;
 1.95115 +  int i;
 1.95116 +
 1.95117 +  nExpr = pList->nExpr;
 1.95118 +  pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
 1.95119 +  if( pInfo ){
 1.95120 +    pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
 1.95121 +    pInfo->nField = (u16)nExpr;
 1.95122 +    pInfo->enc = ENC(db);
 1.95123 +    pInfo->db = db;
 1.95124 +    for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
 1.95125 +      CollSeq *pColl;
 1.95126 +      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
 1.95127 +      if( !pColl ){
 1.95128 +        pColl = db->pDfltColl;
 1.95129 +      }
 1.95130 +      pInfo->aColl[i] = pColl;
 1.95131 +      pInfo->aSortOrder[i] = pItem->sortOrder;
 1.95132 +    }
 1.95133 +  }
 1.95134 +  return pInfo;
 1.95135 +}
 1.95136 +
 1.95137 +#ifndef SQLITE_OMIT_COMPOUND_SELECT
 1.95138 +/*
 1.95139 +** Name of the connection operator, used for error messages.
 1.95140 +*/
 1.95141 +static const char *selectOpName(int id){
 1.95142 +  char *z;
 1.95143 +  switch( id ){
 1.95144 +    case TK_ALL:       z = "UNION ALL";   break;
 1.95145 +    case TK_INTERSECT: z = "INTERSECT";   break;
 1.95146 +    case TK_EXCEPT:    z = "EXCEPT";      break;
 1.95147 +    default:           z = "UNION";       break;
 1.95148 +  }
 1.95149 +  return z;
 1.95150 +}
 1.95151 +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
 1.95152 +
 1.95153 +#ifndef SQLITE_OMIT_EXPLAIN
 1.95154 +/*
 1.95155 +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
 1.95156 +** is a no-op. Otherwise, it adds a single row of output to the EQP result,
 1.95157 +** where the caption is of the form:
 1.95158 +**
 1.95159 +**   "USE TEMP B-TREE FOR xxx"
 1.95160 +**
 1.95161 +** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
 1.95162 +** is determined by the zUsage argument.
 1.95163 +*/
 1.95164 +static void explainTempTable(Parse *pParse, const char *zUsage){
 1.95165 +  if( pParse->explain==2 ){
 1.95166 +    Vdbe *v = pParse->pVdbe;
 1.95167 +    char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
 1.95168 +    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 1.95169 +  }
 1.95170 +}
 1.95171 +
 1.95172 +/*
 1.95173 +** Assign expression b to lvalue a. A second, no-op, version of this macro
 1.95174 +** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
 1.95175 +** in sqlite3Select() to assign values to structure member variables that
 1.95176 +** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
 1.95177 +** code with #ifndef directives.
 1.95178 +*/
 1.95179 +# define explainSetInteger(a, b) a = b
 1.95180 +
 1.95181 +#else
 1.95182 +/* No-op versions of the explainXXX() functions and macros. */
 1.95183 +# define explainTempTable(y,z)
 1.95184 +# define explainSetInteger(y,z)
 1.95185 +#endif
 1.95186 +
 1.95187 +#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
 1.95188 +/*
 1.95189 +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
 1.95190 +** is a no-op. Otherwise, it adds a single row of output to the EQP result,
 1.95191 +** where the caption is of one of the two forms:
 1.95192 +**
 1.95193 +**   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
 1.95194 +**   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
 1.95195 +**
 1.95196 +** where iSub1 and iSub2 are the integers passed as the corresponding
 1.95197 +** function parameters, and op is the text representation of the parameter
 1.95198 +** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
 1.95199 +** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
 1.95200 +** false, or the second form if it is true.
 1.95201 +*/
 1.95202 +static void explainComposite(
 1.95203 +  Parse *pParse,                  /* Parse context */
 1.95204 +  int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
 1.95205 +  int iSub1,                      /* Subquery id 1 */
 1.95206 +  int iSub2,                      /* Subquery id 2 */
 1.95207 +  int bUseTmp                     /* True if a temp table was used */
 1.95208 +){
 1.95209 +  assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
 1.95210 +  if( pParse->explain==2 ){
 1.95211 +    Vdbe *v = pParse->pVdbe;
 1.95212 +    char *zMsg = sqlite3MPrintf(
 1.95213 +        pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
 1.95214 +        bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
 1.95215 +    );
 1.95216 +    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 1.95217 +  }
 1.95218 +}
 1.95219 +#else
 1.95220 +/* No-op versions of the explainXXX() functions and macros. */
 1.95221 +# define explainComposite(v,w,x,y,z)
 1.95222 +#endif
 1.95223 +
 1.95224 +/*
 1.95225 +** If the inner loop was generated using a non-null pOrderBy argument,
 1.95226 +** then the results were placed in a sorter.  After the loop is terminated
 1.95227 +** we need to run the sorter and output the results.  The following
 1.95228 +** routine generates the code needed to do that.
 1.95229 +*/
 1.95230 +static void generateSortTail(
 1.95231 +  Parse *pParse,    /* Parsing context */
 1.95232 +  Select *p,        /* The SELECT statement */
 1.95233 +  Vdbe *v,          /* Generate code into this VDBE */
 1.95234 +  int nColumn,      /* Number of columns of data */
 1.95235 +  SelectDest *pDest /* Write the sorted results here */
 1.95236 +){
 1.95237 +  int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
 1.95238 +  int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
 1.95239 +  int addr;
 1.95240 +  int iTab;
 1.95241 +  int pseudoTab = 0;
 1.95242 +  ExprList *pOrderBy = p->pOrderBy;
 1.95243 +
 1.95244 +  int eDest = pDest->eDest;
 1.95245 +  int iParm = pDest->iSDParm;
 1.95246 +
 1.95247 +  int regRow;
 1.95248 +  int regRowid;
 1.95249 +
 1.95250 +  iTab = pOrderBy->iECursor;
 1.95251 +  regRow = sqlite3GetTempReg(pParse);
 1.95252 +  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 1.95253 +    pseudoTab = pParse->nTab++;
 1.95254 +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
 1.95255 +    regRowid = 0;
 1.95256 +  }else{
 1.95257 +    regRowid = sqlite3GetTempReg(pParse);
 1.95258 +  }
 1.95259 +  if( p->selFlags & SF_UseSorter ){
 1.95260 +    int regSortOut = ++pParse->nMem;
 1.95261 +    int ptab2 = pParse->nTab++;
 1.95262 +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
 1.95263 +    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
 1.95264 +    codeOffset(v, p, addrContinue);
 1.95265 +    sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
 1.95266 +    sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
 1.95267 +    sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 1.95268 +  }else{
 1.95269 +    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
 1.95270 +    codeOffset(v, p, addrContinue);
 1.95271 +    sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
 1.95272 +  }
 1.95273 +  switch( eDest ){
 1.95274 +    case SRT_Table:
 1.95275 +    case SRT_EphemTab: {
 1.95276 +      testcase( eDest==SRT_Table );
 1.95277 +      testcase( eDest==SRT_EphemTab );
 1.95278 +      sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
 1.95279 +      sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
 1.95280 +      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.95281 +      break;
 1.95282 +    }
 1.95283 +#ifndef SQLITE_OMIT_SUBQUERY
 1.95284 +    case SRT_Set: {
 1.95285 +      assert( nColumn==1 );
 1.95286 +      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
 1.95287 +                        &pDest->affSdst, 1);
 1.95288 +      sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
 1.95289 +      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
 1.95290 +      break;
 1.95291 +    }
 1.95292 +    case SRT_Mem: {
 1.95293 +      assert( nColumn==1 );
 1.95294 +      sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
 1.95295 +      /* The LIMIT clause will terminate the loop for us */
 1.95296 +      break;
 1.95297 +    }
 1.95298 +#endif
 1.95299 +    default: {
 1.95300 +      int i;
 1.95301 +      assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
 1.95302 +      testcase( eDest==SRT_Output );
 1.95303 +      testcase( eDest==SRT_Coroutine );
 1.95304 +      for(i=0; i<nColumn; i++){
 1.95305 +        assert( regRow!=pDest->iSdst+i );
 1.95306 +        sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
 1.95307 +        if( i==0 ){
 1.95308 +          sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 1.95309 +        }
 1.95310 +      }
 1.95311 +      if( eDest==SRT_Output ){
 1.95312 +        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
 1.95313 +        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
 1.95314 +      }else{
 1.95315 +        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 1.95316 +      }
 1.95317 +      break;
 1.95318 +    }
 1.95319 +  }
 1.95320 +  sqlite3ReleaseTempReg(pParse, regRow);
 1.95321 +  sqlite3ReleaseTempReg(pParse, regRowid);
 1.95322 +
 1.95323 +  /* The bottom of the loop
 1.95324 +  */
 1.95325 +  sqlite3VdbeResolveLabel(v, addrContinue);
 1.95326 +  if( p->selFlags & SF_UseSorter ){
 1.95327 +    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
 1.95328 +  }else{
 1.95329 +    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
 1.95330 +  }
 1.95331 +  sqlite3VdbeResolveLabel(v, addrBreak);
 1.95332 +  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 1.95333 +    sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
 1.95334 +  }
 1.95335 +}
 1.95336 +
 1.95337 +/*
 1.95338 +** Return a pointer to a string containing the 'declaration type' of the
 1.95339 +** expression pExpr. The string may be treated as static by the caller.
 1.95340 +**
 1.95341 +** The declaration type is the exact datatype definition extracted from the
 1.95342 +** original CREATE TABLE statement if the expression is a column. The
 1.95343 +** declaration type for a ROWID field is INTEGER. Exactly when an expression
 1.95344 +** is considered a column can be complex in the presence of subqueries. The
 1.95345 +** result-set expression in all of the following SELECT statements is 
 1.95346 +** considered a column by this function.
 1.95347 +**
 1.95348 +**   SELECT col FROM tbl;
 1.95349 +**   SELECT (SELECT col FROM tbl;
 1.95350 +**   SELECT (SELECT col FROM tbl);
 1.95351 +**   SELECT abc FROM (SELECT col AS abc FROM tbl);
 1.95352 +** 
 1.95353 +** The declaration type for any expression other than a column is NULL.
 1.95354 +*/
 1.95355 +static const char *columnType(
 1.95356 +  NameContext *pNC, 
 1.95357 +  Expr *pExpr,
 1.95358 +  const char **pzOriginDb,
 1.95359 +  const char **pzOriginTab,
 1.95360 +  const char **pzOriginCol
 1.95361 +){
 1.95362 +  char const *zType = 0;
 1.95363 +  char const *zOriginDb = 0;
 1.95364 +  char const *zOriginTab = 0;
 1.95365 +  char const *zOriginCol = 0;
 1.95366 +  int j;
 1.95367 +  if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
 1.95368 +
 1.95369 +  switch( pExpr->op ){
 1.95370 +    case TK_AGG_COLUMN:
 1.95371 +    case TK_COLUMN: {
 1.95372 +      /* The expression is a column. Locate the table the column is being
 1.95373 +      ** extracted from in NameContext.pSrcList. This table may be real
 1.95374 +      ** database table or a subquery.
 1.95375 +      */
 1.95376 +      Table *pTab = 0;            /* Table structure column is extracted from */
 1.95377 +      Select *pS = 0;             /* Select the column is extracted from */
 1.95378 +      int iCol = pExpr->iColumn;  /* Index of column in pTab */
 1.95379 +      testcase( pExpr->op==TK_AGG_COLUMN );
 1.95380 +      testcase( pExpr->op==TK_COLUMN );
 1.95381 +      while( pNC && !pTab ){
 1.95382 +        SrcList *pTabList = pNC->pSrcList;
 1.95383 +        for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
 1.95384 +        if( j<pTabList->nSrc ){
 1.95385 +          pTab = pTabList->a[j].pTab;
 1.95386 +          pS = pTabList->a[j].pSelect;
 1.95387 +        }else{
 1.95388 +          pNC = pNC->pNext;
 1.95389 +        }
 1.95390 +      }
 1.95391 +
 1.95392 +      if( pTab==0 ){
 1.95393 +        /* At one time, code such as "SELECT new.x" within a trigger would
 1.95394 +        ** cause this condition to run.  Since then, we have restructured how
 1.95395 +        ** trigger code is generated and so this condition is no longer 
 1.95396 +        ** possible. However, it can still be true for statements like
 1.95397 +        ** the following:
 1.95398 +        **
 1.95399 +        **   CREATE TABLE t1(col INTEGER);
 1.95400 +        **   SELECT (SELECT t1.col) FROM FROM t1;
 1.95401 +        **
 1.95402 +        ** when columnType() is called on the expression "t1.col" in the 
 1.95403 +        ** sub-select. In this case, set the column type to NULL, even
 1.95404 +        ** though it should really be "INTEGER".
 1.95405 +        **
 1.95406 +        ** This is not a problem, as the column type of "t1.col" is never
 1.95407 +        ** used. When columnType() is called on the expression 
 1.95408 +        ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
 1.95409 +        ** branch below.  */
 1.95410 +        break;
 1.95411 +      }
 1.95412 +
 1.95413 +      assert( pTab && pExpr->pTab==pTab );
 1.95414 +      if( pS ){
 1.95415 +        /* The "table" is actually a sub-select or a view in the FROM clause
 1.95416 +        ** of the SELECT statement. Return the declaration type and origin
 1.95417 +        ** data for the result-set column of the sub-select.
 1.95418 +        */
 1.95419 +        if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
 1.95420 +          /* If iCol is less than zero, then the expression requests the
 1.95421 +          ** rowid of the sub-select or view. This expression is legal (see 
 1.95422 +          ** test case misc2.2.2) - it always evaluates to NULL.
 1.95423 +          */
 1.95424 +          NameContext sNC;
 1.95425 +          Expr *p = pS->pEList->a[iCol].pExpr;
 1.95426 +          sNC.pSrcList = pS->pSrc;
 1.95427 +          sNC.pNext = pNC;
 1.95428 +          sNC.pParse = pNC->pParse;
 1.95429 +          zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
 1.95430 +        }
 1.95431 +      }else if( ALWAYS(pTab->pSchema) ){
 1.95432 +        /* A real table */
 1.95433 +        assert( !pS );
 1.95434 +        if( iCol<0 ) iCol = pTab->iPKey;
 1.95435 +        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
 1.95436 +        if( iCol<0 ){
 1.95437 +          zType = "INTEGER";
 1.95438 +          zOriginCol = "rowid";
 1.95439 +        }else{
 1.95440 +          zType = pTab->aCol[iCol].zType;
 1.95441 +          zOriginCol = pTab->aCol[iCol].zName;
 1.95442 +        }
 1.95443 +        zOriginTab = pTab->zName;
 1.95444 +        if( pNC->pParse ){
 1.95445 +          int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
 1.95446 +          zOriginDb = pNC->pParse->db->aDb[iDb].zName;
 1.95447 +        }
 1.95448 +      }
 1.95449 +      break;
 1.95450 +    }
 1.95451 +#ifndef SQLITE_OMIT_SUBQUERY
 1.95452 +    case TK_SELECT: {
 1.95453 +      /* The expression is a sub-select. Return the declaration type and
 1.95454 +      ** origin info for the single column in the result set of the SELECT
 1.95455 +      ** statement.
 1.95456 +      */
 1.95457 +      NameContext sNC;
 1.95458 +      Select *pS = pExpr->x.pSelect;
 1.95459 +      Expr *p = pS->pEList->a[0].pExpr;
 1.95460 +      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 1.95461 +      sNC.pSrcList = pS->pSrc;
 1.95462 +      sNC.pNext = pNC;
 1.95463 +      sNC.pParse = pNC->pParse;
 1.95464 +      zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
 1.95465 +      break;
 1.95466 +    }
 1.95467 +#endif
 1.95468 +  }
 1.95469 +  
 1.95470 +  if( pzOriginDb ){
 1.95471 +    assert( pzOriginTab && pzOriginCol );
 1.95472 +    *pzOriginDb = zOriginDb;
 1.95473 +    *pzOriginTab = zOriginTab;
 1.95474 +    *pzOriginCol = zOriginCol;
 1.95475 +  }
 1.95476 +  return zType;
 1.95477 +}
 1.95478 +
 1.95479 +/*
 1.95480 +** Generate code that will tell the VDBE the declaration types of columns
 1.95481 +** in the result set.
 1.95482 +*/
 1.95483 +static void generateColumnTypes(
 1.95484 +  Parse *pParse,      /* Parser context */
 1.95485 +  SrcList *pTabList,  /* List of tables */
 1.95486 +  ExprList *pEList    /* Expressions defining the result set */
 1.95487 +){
 1.95488 +#ifndef SQLITE_OMIT_DECLTYPE
 1.95489 +  Vdbe *v = pParse->pVdbe;
 1.95490 +  int i;
 1.95491 +  NameContext sNC;
 1.95492 +  sNC.pSrcList = pTabList;
 1.95493 +  sNC.pParse = pParse;
 1.95494 +  for(i=0; i<pEList->nExpr; i++){
 1.95495 +    Expr *p = pEList->a[i].pExpr;
 1.95496 +    const char *zType;
 1.95497 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
 1.95498 +    const char *zOrigDb = 0;
 1.95499 +    const char *zOrigTab = 0;
 1.95500 +    const char *zOrigCol = 0;
 1.95501 +    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
 1.95502 +
 1.95503 +    /* The vdbe must make its own copy of the column-type and other 
 1.95504 +    ** column specific strings, in case the schema is reset before this
 1.95505 +    ** virtual machine is deleted.
 1.95506 +    */
 1.95507 +    sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
 1.95508 +    sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
 1.95509 +    sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
 1.95510 +#else
 1.95511 +    zType = columnType(&sNC, p, 0, 0, 0);
 1.95512 +#endif
 1.95513 +    sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
 1.95514 +  }
 1.95515 +#endif /* SQLITE_OMIT_DECLTYPE */
 1.95516 +}
 1.95517 +
 1.95518 +/*
 1.95519 +** Generate code that will tell the VDBE the names of columns
 1.95520 +** in the result set.  This information is used to provide the
 1.95521 +** azCol[] values in the callback.
 1.95522 +*/
 1.95523 +static void generateColumnNames(
 1.95524 +  Parse *pParse,      /* Parser context */
 1.95525 +  SrcList *pTabList,  /* List of tables */
 1.95526 +  ExprList *pEList    /* Expressions defining the result set */
 1.95527 +){
 1.95528 +  Vdbe *v = pParse->pVdbe;
 1.95529 +  int i, j;
 1.95530 +  sqlite3 *db = pParse->db;
 1.95531 +  int fullNames, shortNames;
 1.95532 +
 1.95533 +#ifndef SQLITE_OMIT_EXPLAIN
 1.95534 +  /* If this is an EXPLAIN, skip this step */
 1.95535 +  if( pParse->explain ){
 1.95536 +    return;
 1.95537 +  }
 1.95538 +#endif
 1.95539 +
 1.95540 +  if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
 1.95541 +  pParse->colNamesSet = 1;
 1.95542 +  fullNames = (db->flags & SQLITE_FullColNames)!=0;
 1.95543 +  shortNames = (db->flags & SQLITE_ShortColNames)!=0;
 1.95544 +  sqlite3VdbeSetNumCols(v, pEList->nExpr);
 1.95545 +  for(i=0; i<pEList->nExpr; i++){
 1.95546 +    Expr *p;
 1.95547 +    p = pEList->a[i].pExpr;
 1.95548 +    if( NEVER(p==0) ) continue;
 1.95549 +    if( pEList->a[i].zName ){
 1.95550 +      char *zName = pEList->a[i].zName;
 1.95551 +      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
 1.95552 +    }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
 1.95553 +      Table *pTab;
 1.95554 +      char *zCol;
 1.95555 +      int iCol = p->iColumn;
 1.95556 +      for(j=0; ALWAYS(j<pTabList->nSrc); j++){
 1.95557 +        if( pTabList->a[j].iCursor==p->iTable ) break;
 1.95558 +      }
 1.95559 +      assert( j<pTabList->nSrc );
 1.95560 +      pTab = pTabList->a[j].pTab;
 1.95561 +      if( iCol<0 ) iCol = pTab->iPKey;
 1.95562 +      assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
 1.95563 +      if( iCol<0 ){
 1.95564 +        zCol = "rowid";
 1.95565 +      }else{
 1.95566 +        zCol = pTab->aCol[iCol].zName;
 1.95567 +      }
 1.95568 +      if( !shortNames && !fullNames ){
 1.95569 +        sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
 1.95570 +            sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
 1.95571 +      }else if( fullNames ){
 1.95572 +        char *zName = 0;
 1.95573 +        zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
 1.95574 +        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
 1.95575 +      }else{
 1.95576 +        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
 1.95577 +      }
 1.95578 +    }else{
 1.95579 +      sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
 1.95580 +          sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
 1.95581 +    }
 1.95582 +  }
 1.95583 +  generateColumnTypes(pParse, pTabList, pEList);
 1.95584 +}
 1.95585 +
 1.95586 +/*
 1.95587 +** Given a an expression list (which is really the list of expressions
 1.95588 +** that form the result set of a SELECT statement) compute appropriate
 1.95589 +** column names for a table that would hold the expression list.
 1.95590 +**
 1.95591 +** All column names will be unique.
 1.95592 +**
 1.95593 +** Only the column names are computed.  Column.zType, Column.zColl,
 1.95594 +** and other fields of Column are zeroed.
 1.95595 +**
 1.95596 +** Return SQLITE_OK on success.  If a memory allocation error occurs,
 1.95597 +** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
 1.95598 +*/
 1.95599 +static int selectColumnsFromExprList(
 1.95600 +  Parse *pParse,          /* Parsing context */
 1.95601 +  ExprList *pEList,       /* Expr list from which to derive column names */
 1.95602 +  i16 *pnCol,             /* Write the number of columns here */
 1.95603 +  Column **paCol          /* Write the new column list here */
 1.95604 +){
 1.95605 +  sqlite3 *db = pParse->db;   /* Database connection */
 1.95606 +  int i, j;                   /* Loop counters */
 1.95607 +  int cnt;                    /* Index added to make the name unique */
 1.95608 +  Column *aCol, *pCol;        /* For looping over result columns */
 1.95609 +  int nCol;                   /* Number of columns in the result set */
 1.95610 +  Expr *p;                    /* Expression for a single result column */
 1.95611 +  char *zName;                /* Column name */
 1.95612 +  int nName;                  /* Size of name in zName[] */
 1.95613 +
 1.95614 +  if( pEList ){
 1.95615 +    nCol = pEList->nExpr;
 1.95616 +    aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
 1.95617 +    testcase( aCol==0 );
 1.95618 +  }else{
 1.95619 +    nCol = 0;
 1.95620 +    aCol = 0;
 1.95621 +  }
 1.95622 +  *pnCol = nCol;
 1.95623 +  *paCol = aCol;
 1.95624 +
 1.95625 +  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 1.95626 +    /* Get an appropriate name for the column
 1.95627 +    */
 1.95628 +    p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
 1.95629 +    assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
 1.95630 +               || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
 1.95631 +    if( (zName = pEList->a[i].zName)!=0 ){
 1.95632 +      /* If the column contains an "AS <name>" phrase, use <name> as the name */
 1.95633 +      zName = sqlite3DbStrDup(db, zName);
 1.95634 +    }else{
 1.95635 +      Expr *pColExpr = p;  /* The expression that is the result column name */
 1.95636 +      Table *pTab;         /* Table associated with this expression */
 1.95637 +      while( pColExpr->op==TK_DOT ){
 1.95638 +        pColExpr = pColExpr->pRight;
 1.95639 +        assert( pColExpr!=0 );
 1.95640 +      }
 1.95641 +      if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
 1.95642 +        /* For columns use the column name name */
 1.95643 +        int iCol = pColExpr->iColumn;
 1.95644 +        pTab = pColExpr->pTab;
 1.95645 +        if( iCol<0 ) iCol = pTab->iPKey;
 1.95646 +        zName = sqlite3MPrintf(db, "%s",
 1.95647 +                 iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
 1.95648 +      }else if( pColExpr->op==TK_ID ){
 1.95649 +        assert( !ExprHasProperty(pColExpr, EP_IntValue) );
 1.95650 +        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
 1.95651 +      }else{
 1.95652 +        /* Use the original text of the column expression as its name */
 1.95653 +        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
 1.95654 +      }
 1.95655 +    }
 1.95656 +    if( db->mallocFailed ){
 1.95657 +      sqlite3DbFree(db, zName);
 1.95658 +      break;
 1.95659 +    }
 1.95660 +
 1.95661 +    /* Make sure the column name is unique.  If the name is not unique,
 1.95662 +    ** append a integer to the name so that it becomes unique.
 1.95663 +    */
 1.95664 +    nName = sqlite3Strlen30(zName);
 1.95665 +    for(j=cnt=0; j<i; j++){
 1.95666 +      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
 1.95667 +        char *zNewName;
 1.95668 +        zName[nName] = 0;
 1.95669 +        zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
 1.95670 +        sqlite3DbFree(db, zName);
 1.95671 +        zName = zNewName;
 1.95672 +        j = -1;
 1.95673 +        if( zName==0 ) break;
 1.95674 +      }
 1.95675 +    }
 1.95676 +    pCol->zName = zName;
 1.95677 +  }
 1.95678 +  if( db->mallocFailed ){
 1.95679 +    for(j=0; j<i; j++){
 1.95680 +      sqlite3DbFree(db, aCol[j].zName);
 1.95681 +    }
 1.95682 +    sqlite3DbFree(db, aCol);
 1.95683 +    *paCol = 0;
 1.95684 +    *pnCol = 0;
 1.95685 +    return SQLITE_NOMEM;
 1.95686 +  }
 1.95687 +  return SQLITE_OK;
 1.95688 +}
 1.95689 +
 1.95690 +/*
 1.95691 +** Add type and collation information to a column list based on
 1.95692 +** a SELECT statement.
 1.95693 +** 
 1.95694 +** The column list presumably came from selectColumnNamesFromExprList().
 1.95695 +** The column list has only names, not types or collations.  This
 1.95696 +** routine goes through and adds the types and collations.
 1.95697 +**
 1.95698 +** This routine requires that all identifiers in the SELECT
 1.95699 +** statement be resolved.
 1.95700 +*/
 1.95701 +static void selectAddColumnTypeAndCollation(
 1.95702 +  Parse *pParse,        /* Parsing contexts */
 1.95703 +  int nCol,             /* Number of columns */
 1.95704 +  Column *aCol,         /* List of columns */
 1.95705 +  Select *pSelect       /* SELECT used to determine types and collations */
 1.95706 +){
 1.95707 +  sqlite3 *db = pParse->db;
 1.95708 +  NameContext sNC;
 1.95709 +  Column *pCol;
 1.95710 +  CollSeq *pColl;
 1.95711 +  int i;
 1.95712 +  Expr *p;
 1.95713 +  struct ExprList_item *a;
 1.95714 +
 1.95715 +  assert( pSelect!=0 );
 1.95716 +  assert( (pSelect->selFlags & SF_Resolved)!=0 );
 1.95717 +  assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
 1.95718 +  if( db->mallocFailed ) return;
 1.95719 +  memset(&sNC, 0, sizeof(sNC));
 1.95720 +  sNC.pSrcList = pSelect->pSrc;
 1.95721 +  a = pSelect->pEList->a;
 1.95722 +  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 1.95723 +    p = a[i].pExpr;
 1.95724 +    pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
 1.95725 +    pCol->affinity = sqlite3ExprAffinity(p);
 1.95726 +    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
 1.95727 +    pColl = sqlite3ExprCollSeq(pParse, p);
 1.95728 +    if( pColl ){
 1.95729 +      pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
 1.95730 +    }
 1.95731 +  }
 1.95732 +}
 1.95733 +
 1.95734 +/*
 1.95735 +** Given a SELECT statement, generate a Table structure that describes
 1.95736 +** the result set of that SELECT.
 1.95737 +*/
 1.95738 +SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
 1.95739 +  Table *pTab;
 1.95740 +  sqlite3 *db = pParse->db;
 1.95741 +  int savedFlags;
 1.95742 +
 1.95743 +  savedFlags = db->flags;
 1.95744 +  db->flags &= ~SQLITE_FullColNames;
 1.95745 +  db->flags |= SQLITE_ShortColNames;
 1.95746 +  sqlite3SelectPrep(pParse, pSelect, 0);
 1.95747 +  if( pParse->nErr ) return 0;
 1.95748 +  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
 1.95749 +  db->flags = savedFlags;
 1.95750 +  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
 1.95751 +  if( pTab==0 ){
 1.95752 +    return 0;
 1.95753 +  }
 1.95754 +  /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
 1.95755 +  ** is disabled */
 1.95756 +  assert( db->lookaside.bEnabled==0 );
 1.95757 +  pTab->nRef = 1;
 1.95758 +  pTab->zName = 0;
 1.95759 +  pTab->nRowEst = 1000000;
 1.95760 +  selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
 1.95761 +  selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
 1.95762 +  pTab->iPKey = -1;
 1.95763 +  if( db->mallocFailed ){
 1.95764 +    sqlite3DeleteTable(db, pTab);
 1.95765 +    return 0;
 1.95766 +  }
 1.95767 +  return pTab;
 1.95768 +}
 1.95769 +
 1.95770 +/*
 1.95771 +** Get a VDBE for the given parser context.  Create a new one if necessary.
 1.95772 +** If an error occurs, return NULL and leave a message in pParse.
 1.95773 +*/
 1.95774 +SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
 1.95775 +  Vdbe *v = pParse->pVdbe;
 1.95776 +  if( v==0 ){
 1.95777 +    v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
 1.95778 +#ifndef SQLITE_OMIT_TRACE
 1.95779 +    if( v ){
 1.95780 +      sqlite3VdbeAddOp0(v, OP_Trace);
 1.95781 +    }
 1.95782 +#endif
 1.95783 +  }
 1.95784 +  return v;
 1.95785 +}
 1.95786 +
 1.95787 +
 1.95788 +/*
 1.95789 +** Compute the iLimit and iOffset fields of the SELECT based on the
 1.95790 +** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
 1.95791 +** that appear in the original SQL statement after the LIMIT and OFFSET
 1.95792 +** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
 1.95793 +** are the integer memory register numbers for counters used to compute 
 1.95794 +** the limit and offset.  If there is no limit and/or offset, then 
 1.95795 +** iLimit and iOffset are negative.
 1.95796 +**
 1.95797 +** This routine changes the values of iLimit and iOffset only if
 1.95798 +** a limit or offset is defined by pLimit and pOffset.  iLimit and
 1.95799 +** iOffset should have been preset to appropriate default values
 1.95800 +** (usually but not always -1) prior to calling this routine.
 1.95801 +** Only if pLimit!=0 or pOffset!=0 do the limit registers get
 1.95802 +** redefined.  The UNION ALL operator uses this property to force
 1.95803 +** the reuse of the same limit and offset registers across multiple
 1.95804 +** SELECT statements.
 1.95805 +*/
 1.95806 +static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
 1.95807 +  Vdbe *v = 0;
 1.95808 +  int iLimit = 0;
 1.95809 +  int iOffset;
 1.95810 +  int addr1, n;
 1.95811 +  if( p->iLimit ) return;
 1.95812 +
 1.95813 +  /* 
 1.95814 +  ** "LIMIT -1" always shows all rows.  There is some
 1.95815 +  ** contraversy about what the correct behavior should be.
 1.95816 +  ** The current implementation interprets "LIMIT 0" to mean
 1.95817 +  ** no rows.
 1.95818 +  */
 1.95819 +  sqlite3ExprCacheClear(pParse);
 1.95820 +  assert( p->pOffset==0 || p->pLimit!=0 );
 1.95821 +  if( p->pLimit ){
 1.95822 +    p->iLimit = iLimit = ++pParse->nMem;
 1.95823 +    v = sqlite3GetVdbe(pParse);
 1.95824 +    if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
 1.95825 +    if( sqlite3ExprIsInteger(p->pLimit, &n) ){
 1.95826 +      sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
 1.95827 +      VdbeComment((v, "LIMIT counter"));
 1.95828 +      if( n==0 ){
 1.95829 +        sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
 1.95830 +      }else{
 1.95831 +        if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
 1.95832 +      }
 1.95833 +    }else{
 1.95834 +      sqlite3ExprCode(pParse, p->pLimit, iLimit);
 1.95835 +      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
 1.95836 +      VdbeComment((v, "LIMIT counter"));
 1.95837 +      sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
 1.95838 +    }
 1.95839 +    if( p->pOffset ){
 1.95840 +      p->iOffset = iOffset = ++pParse->nMem;
 1.95841 +      pParse->nMem++;   /* Allocate an extra register for limit+offset */
 1.95842 +      sqlite3ExprCode(pParse, p->pOffset, iOffset);
 1.95843 +      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
 1.95844 +      VdbeComment((v, "OFFSET counter"));
 1.95845 +      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
 1.95846 +      sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
 1.95847 +      sqlite3VdbeJumpHere(v, addr1);
 1.95848 +      sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
 1.95849 +      VdbeComment((v, "LIMIT+OFFSET"));
 1.95850 +      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
 1.95851 +      sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
 1.95852 +      sqlite3VdbeJumpHere(v, addr1);
 1.95853 +    }
 1.95854 +  }
 1.95855 +}
 1.95856 +
 1.95857 +#ifndef SQLITE_OMIT_COMPOUND_SELECT
 1.95858 +/*
 1.95859 +** Return the appropriate collating sequence for the iCol-th column of
 1.95860 +** the result set for the compound-select statement "p".  Return NULL if
 1.95861 +** the column has no default collating sequence.
 1.95862 +**
 1.95863 +** The collating sequence for the compound select is taken from the
 1.95864 +** left-most term of the select that has a collating sequence.
 1.95865 +*/
 1.95866 +static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
 1.95867 +  CollSeq *pRet;
 1.95868 +  if( p->pPrior ){
 1.95869 +    pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
 1.95870 +  }else{
 1.95871 +    pRet = 0;
 1.95872 +  }
 1.95873 +  assert( iCol>=0 );
 1.95874 +  if( pRet==0 && iCol<p->pEList->nExpr ){
 1.95875 +    pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
 1.95876 +  }
 1.95877 +  return pRet;
 1.95878 +}
 1.95879 +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
 1.95880 +
 1.95881 +/* Forward reference */
 1.95882 +static int multiSelectOrderBy(
 1.95883 +  Parse *pParse,        /* Parsing context */
 1.95884 +  Select *p,            /* The right-most of SELECTs to be coded */
 1.95885 +  SelectDest *pDest     /* What to do with query results */
 1.95886 +);
 1.95887 +
 1.95888 +
 1.95889 +#ifndef SQLITE_OMIT_COMPOUND_SELECT
 1.95890 +/*
 1.95891 +** This routine is called to process a compound query form from
 1.95892 +** two or more separate queries using UNION, UNION ALL, EXCEPT, or
 1.95893 +** INTERSECT
 1.95894 +**
 1.95895 +** "p" points to the right-most of the two queries.  the query on the
 1.95896 +** left is p->pPrior.  The left query could also be a compound query
 1.95897 +** in which case this routine will be called recursively. 
 1.95898 +**
 1.95899 +** The results of the total query are to be written into a destination
 1.95900 +** of type eDest with parameter iParm.
 1.95901 +**
 1.95902 +** Example 1:  Consider a three-way compound SQL statement.
 1.95903 +**
 1.95904 +**     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
 1.95905 +**
 1.95906 +** This statement is parsed up as follows:
 1.95907 +**
 1.95908 +**     SELECT c FROM t3
 1.95909 +**      |
 1.95910 +**      `----->  SELECT b FROM t2
 1.95911 +**                |
 1.95912 +**                `------>  SELECT a FROM t1
 1.95913 +**
 1.95914 +** The arrows in the diagram above represent the Select.pPrior pointer.
 1.95915 +** So if this routine is called with p equal to the t3 query, then
 1.95916 +** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
 1.95917 +**
 1.95918 +** Notice that because of the way SQLite parses compound SELECTs, the
 1.95919 +** individual selects always group from left to right.
 1.95920 +*/
 1.95921 +static int multiSelect(
 1.95922 +  Parse *pParse,        /* Parsing context */
 1.95923 +  Select *p,            /* The right-most of SELECTs to be coded */
 1.95924 +  SelectDest *pDest     /* What to do with query results */
 1.95925 +){
 1.95926 +  int rc = SQLITE_OK;   /* Success code from a subroutine */
 1.95927 +  Select *pPrior;       /* Another SELECT immediately to our left */
 1.95928 +  Vdbe *v;              /* Generate code to this VDBE */
 1.95929 +  SelectDest dest;      /* Alternative data destination */
 1.95930 +  Select *pDelete = 0;  /* Chain of simple selects to delete */
 1.95931 +  sqlite3 *db;          /* Database connection */
 1.95932 +#ifndef SQLITE_OMIT_EXPLAIN
 1.95933 +  int iSub1;            /* EQP id of left-hand query */
 1.95934 +  int iSub2;            /* EQP id of right-hand query */
 1.95935 +#endif
 1.95936 +
 1.95937 +  /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
 1.95938 +  ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
 1.95939 +  */
 1.95940 +  assert( p && p->pPrior );  /* Calling function guarantees this much */
 1.95941 +  db = pParse->db;
 1.95942 +  pPrior = p->pPrior;
 1.95943 +  assert( pPrior->pRightmost!=pPrior );
 1.95944 +  assert( pPrior->pRightmost==p->pRightmost );
 1.95945 +  dest = *pDest;
 1.95946 +  if( pPrior->pOrderBy ){
 1.95947 +    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
 1.95948 +      selectOpName(p->op));
 1.95949 +    rc = 1;
 1.95950 +    goto multi_select_end;
 1.95951 +  }
 1.95952 +  if( pPrior->pLimit ){
 1.95953 +    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
 1.95954 +      selectOpName(p->op));
 1.95955 +    rc = 1;
 1.95956 +    goto multi_select_end;
 1.95957 +  }
 1.95958 +
 1.95959 +  v = sqlite3GetVdbe(pParse);
 1.95960 +  assert( v!=0 );  /* The VDBE already created by calling function */
 1.95961 +
 1.95962 +  /* Create the destination temporary table if necessary
 1.95963 +  */
 1.95964 +  if( dest.eDest==SRT_EphemTab ){
 1.95965 +    assert( p->pEList );
 1.95966 +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
 1.95967 +    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 1.95968 +    dest.eDest = SRT_Table;
 1.95969 +  }
 1.95970 +
 1.95971 +  /* Make sure all SELECTs in the statement have the same number of elements
 1.95972 +  ** in their result sets.
 1.95973 +  */
 1.95974 +  assert( p->pEList && pPrior->pEList );
 1.95975 +  if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
 1.95976 +    if( p->selFlags & SF_Values ){
 1.95977 +      sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
 1.95978 +    }else{
 1.95979 +      sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
 1.95980 +        " do not have the same number of result columns", selectOpName(p->op));
 1.95981 +    }
 1.95982 +    rc = 1;
 1.95983 +    goto multi_select_end;
 1.95984 +  }
 1.95985 +
 1.95986 +  /* Compound SELECTs that have an ORDER BY clause are handled separately.
 1.95987 +  */
 1.95988 +  if( p->pOrderBy ){
 1.95989 +    return multiSelectOrderBy(pParse, p, pDest);
 1.95990 +  }
 1.95991 +
 1.95992 +  /* Generate code for the left and right SELECT statements.
 1.95993 +  */
 1.95994 +  switch( p->op ){
 1.95995 +    case TK_ALL: {
 1.95996 +      int addr = 0;
 1.95997 +      int nLimit;
 1.95998 +      assert( !pPrior->pLimit );
 1.95999 +      pPrior->pLimit = p->pLimit;
 1.96000 +      pPrior->pOffset = p->pOffset;
 1.96001 +      explainSetInteger(iSub1, pParse->iNextSelectId);
 1.96002 +      rc = sqlite3Select(pParse, pPrior, &dest);
 1.96003 +      p->pLimit = 0;
 1.96004 +      p->pOffset = 0;
 1.96005 +      if( rc ){
 1.96006 +        goto multi_select_end;
 1.96007 +      }
 1.96008 +      p->pPrior = 0;
 1.96009 +      p->iLimit = pPrior->iLimit;
 1.96010 +      p->iOffset = pPrior->iOffset;
 1.96011 +      if( p->iLimit ){
 1.96012 +        addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
 1.96013 +        VdbeComment((v, "Jump ahead if LIMIT reached"));
 1.96014 +      }
 1.96015 +      explainSetInteger(iSub2, pParse->iNextSelectId);
 1.96016 +      rc = sqlite3Select(pParse, p, &dest);
 1.96017 +      testcase( rc!=SQLITE_OK );
 1.96018 +      pDelete = p->pPrior;
 1.96019 +      p->pPrior = pPrior;
 1.96020 +      p->nSelectRow += pPrior->nSelectRow;
 1.96021 +      if( pPrior->pLimit
 1.96022 +       && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
 1.96023 +       && p->nSelectRow > (double)nLimit 
 1.96024 +      ){
 1.96025 +        p->nSelectRow = (double)nLimit;
 1.96026 +      }
 1.96027 +      if( addr ){
 1.96028 +        sqlite3VdbeJumpHere(v, addr);
 1.96029 +      }
 1.96030 +      break;
 1.96031 +    }
 1.96032 +    case TK_EXCEPT:
 1.96033 +    case TK_UNION: {
 1.96034 +      int unionTab;    /* Cursor number of the temporary table holding result */
 1.96035 +      u8 op = 0;       /* One of the SRT_ operations to apply to self */
 1.96036 +      int priorOp;     /* The SRT_ operation to apply to prior selects */
 1.96037 +      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
 1.96038 +      int addr;
 1.96039 +      SelectDest uniondest;
 1.96040 +
 1.96041 +      testcase( p->op==TK_EXCEPT );
 1.96042 +      testcase( p->op==TK_UNION );
 1.96043 +      priorOp = SRT_Union;
 1.96044 +      if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
 1.96045 +        /* We can reuse a temporary table generated by a SELECT to our
 1.96046 +        ** right.
 1.96047 +        */
 1.96048 +        assert( p->pRightmost!=p );  /* Can only happen for leftward elements
 1.96049 +                                     ** of a 3-way or more compound */
 1.96050 +        assert( p->pLimit==0 );      /* Not allowed on leftward elements */
 1.96051 +        assert( p->pOffset==0 );     /* Not allowed on leftward elements */
 1.96052 +        unionTab = dest.iSDParm;
 1.96053 +      }else{
 1.96054 +        /* We will need to create our own temporary table to hold the
 1.96055 +        ** intermediate results.
 1.96056 +        */
 1.96057 +        unionTab = pParse->nTab++;
 1.96058 +        assert( p->pOrderBy==0 );
 1.96059 +        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
 1.96060 +        assert( p->addrOpenEphm[0] == -1 );
 1.96061 +        p->addrOpenEphm[0] = addr;
 1.96062 +        p->pRightmost->selFlags |= SF_UsesEphemeral;
 1.96063 +        assert( p->pEList );
 1.96064 +      }
 1.96065 +
 1.96066 +      /* Code the SELECT statements to our left
 1.96067 +      */
 1.96068 +      assert( !pPrior->pOrderBy );
 1.96069 +      sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
 1.96070 +      explainSetInteger(iSub1, pParse->iNextSelectId);
 1.96071 +      rc = sqlite3Select(pParse, pPrior, &uniondest);
 1.96072 +      if( rc ){
 1.96073 +        goto multi_select_end;
 1.96074 +      }
 1.96075 +
 1.96076 +      /* Code the current SELECT statement
 1.96077 +      */
 1.96078 +      if( p->op==TK_EXCEPT ){
 1.96079 +        op = SRT_Except;
 1.96080 +      }else{
 1.96081 +        assert( p->op==TK_UNION );
 1.96082 +        op = SRT_Union;
 1.96083 +      }
 1.96084 +      p->pPrior = 0;
 1.96085 +      pLimit = p->pLimit;
 1.96086 +      p->pLimit = 0;
 1.96087 +      pOffset = p->pOffset;
 1.96088 +      p->pOffset = 0;
 1.96089 +      uniondest.eDest = op;
 1.96090 +      explainSetInteger(iSub2, pParse->iNextSelectId);
 1.96091 +      rc = sqlite3Select(pParse, p, &uniondest);
 1.96092 +      testcase( rc!=SQLITE_OK );
 1.96093 +      /* Query flattening in sqlite3Select() might refill p->pOrderBy.
 1.96094 +      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
 1.96095 +      sqlite3ExprListDelete(db, p->pOrderBy);
 1.96096 +      pDelete = p->pPrior;
 1.96097 +      p->pPrior = pPrior;
 1.96098 +      p->pOrderBy = 0;
 1.96099 +      if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
 1.96100 +      sqlite3ExprDelete(db, p->pLimit);
 1.96101 +      p->pLimit = pLimit;
 1.96102 +      p->pOffset = pOffset;
 1.96103 +      p->iLimit = 0;
 1.96104 +      p->iOffset = 0;
 1.96105 +
 1.96106 +      /* Convert the data in the temporary table into whatever form
 1.96107 +      ** it is that we currently need.
 1.96108 +      */
 1.96109 +      assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
 1.96110 +      if( dest.eDest!=priorOp ){
 1.96111 +        int iCont, iBreak, iStart;
 1.96112 +        assert( p->pEList );
 1.96113 +        if( dest.eDest==SRT_Output ){
 1.96114 +          Select *pFirst = p;
 1.96115 +          while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 1.96116 +          generateColumnNames(pParse, 0, pFirst->pEList);
 1.96117 +        }
 1.96118 +        iBreak = sqlite3VdbeMakeLabel(v);
 1.96119 +        iCont = sqlite3VdbeMakeLabel(v);
 1.96120 +        computeLimitRegisters(pParse, p, iBreak);
 1.96121 +        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
 1.96122 +        iStart = sqlite3VdbeCurrentAddr(v);
 1.96123 +        selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
 1.96124 +                        0, 0, &dest, iCont, iBreak);
 1.96125 +        sqlite3VdbeResolveLabel(v, iCont);
 1.96126 +        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
 1.96127 +        sqlite3VdbeResolveLabel(v, iBreak);
 1.96128 +        sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
 1.96129 +      }
 1.96130 +      break;
 1.96131 +    }
 1.96132 +    default: assert( p->op==TK_INTERSECT ); {
 1.96133 +      int tab1, tab2;
 1.96134 +      int iCont, iBreak, iStart;
 1.96135 +      Expr *pLimit, *pOffset;
 1.96136 +      int addr;
 1.96137 +      SelectDest intersectdest;
 1.96138 +      int r1;
 1.96139 +
 1.96140 +      /* INTERSECT is different from the others since it requires
 1.96141 +      ** two temporary tables.  Hence it has its own case.  Begin
 1.96142 +      ** by allocating the tables we will need.
 1.96143 +      */
 1.96144 +      tab1 = pParse->nTab++;
 1.96145 +      tab2 = pParse->nTab++;
 1.96146 +      assert( p->pOrderBy==0 );
 1.96147 +
 1.96148 +      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
 1.96149 +      assert( p->addrOpenEphm[0] == -1 );
 1.96150 +      p->addrOpenEphm[0] = addr;
 1.96151 +      p->pRightmost->selFlags |= SF_UsesEphemeral;
 1.96152 +      assert( p->pEList );
 1.96153 +
 1.96154 +      /* Code the SELECTs to our left into temporary table "tab1".
 1.96155 +      */
 1.96156 +      sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
 1.96157 +      explainSetInteger(iSub1, pParse->iNextSelectId);
 1.96158 +      rc = sqlite3Select(pParse, pPrior, &intersectdest);
 1.96159 +      if( rc ){
 1.96160 +        goto multi_select_end;
 1.96161 +      }
 1.96162 +
 1.96163 +      /* Code the current SELECT into temporary table "tab2"
 1.96164 +      */
 1.96165 +      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
 1.96166 +      assert( p->addrOpenEphm[1] == -1 );
 1.96167 +      p->addrOpenEphm[1] = addr;
 1.96168 +      p->pPrior = 0;
 1.96169 +      pLimit = p->pLimit;
 1.96170 +      p->pLimit = 0;
 1.96171 +      pOffset = p->pOffset;
 1.96172 +      p->pOffset = 0;
 1.96173 +      intersectdest.iSDParm = tab2;
 1.96174 +      explainSetInteger(iSub2, pParse->iNextSelectId);
 1.96175 +      rc = sqlite3Select(pParse, p, &intersectdest);
 1.96176 +      testcase( rc!=SQLITE_OK );
 1.96177 +      pDelete = p->pPrior;
 1.96178 +      p->pPrior = pPrior;
 1.96179 +      if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
 1.96180 +      sqlite3ExprDelete(db, p->pLimit);
 1.96181 +      p->pLimit = pLimit;
 1.96182 +      p->pOffset = pOffset;
 1.96183 +
 1.96184 +      /* Generate code to take the intersection of the two temporary
 1.96185 +      ** tables.
 1.96186 +      */
 1.96187 +      assert( p->pEList );
 1.96188 +      if( dest.eDest==SRT_Output ){
 1.96189 +        Select *pFirst = p;
 1.96190 +        while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 1.96191 +        generateColumnNames(pParse, 0, pFirst->pEList);
 1.96192 +      }
 1.96193 +      iBreak = sqlite3VdbeMakeLabel(v);
 1.96194 +      iCont = sqlite3VdbeMakeLabel(v);
 1.96195 +      computeLimitRegisters(pParse, p, iBreak);
 1.96196 +      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
 1.96197 +      r1 = sqlite3GetTempReg(pParse);
 1.96198 +      iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
 1.96199 +      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
 1.96200 +      sqlite3ReleaseTempReg(pParse, r1);
 1.96201 +      selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
 1.96202 +                      0, 0, &dest, iCont, iBreak);
 1.96203 +      sqlite3VdbeResolveLabel(v, iCont);
 1.96204 +      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
 1.96205 +      sqlite3VdbeResolveLabel(v, iBreak);
 1.96206 +      sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
 1.96207 +      sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
 1.96208 +      break;
 1.96209 +    }
 1.96210 +  }
 1.96211 +
 1.96212 +  explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
 1.96213 +
 1.96214 +  /* Compute collating sequences used by 
 1.96215 +  ** temporary tables needed to implement the compound select.
 1.96216 +  ** Attach the KeyInfo structure to all temporary tables.
 1.96217 +  **
 1.96218 +  ** This section is run by the right-most SELECT statement only.
 1.96219 +  ** SELECT statements to the left always skip this part.  The right-most
 1.96220 +  ** SELECT might also skip this part if it has no ORDER BY clause and
 1.96221 +  ** no temp tables are required.
 1.96222 +  */
 1.96223 +  if( p->selFlags & SF_UsesEphemeral ){
 1.96224 +    int i;                        /* Loop counter */
 1.96225 +    KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
 1.96226 +    Select *pLoop;                /* For looping through SELECT statements */
 1.96227 +    CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
 1.96228 +    int nCol;                     /* Number of columns in result set */
 1.96229 +
 1.96230 +    assert( p->pRightmost==p );
 1.96231 +    nCol = p->pEList->nExpr;
 1.96232 +    pKeyInfo = sqlite3DbMallocZero(db,
 1.96233 +                       sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
 1.96234 +    if( !pKeyInfo ){
 1.96235 +      rc = SQLITE_NOMEM;
 1.96236 +      goto multi_select_end;
 1.96237 +    }
 1.96238 +
 1.96239 +    pKeyInfo->enc = ENC(db);
 1.96240 +    pKeyInfo->nField = (u16)nCol;
 1.96241 +
 1.96242 +    for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
 1.96243 +      *apColl = multiSelectCollSeq(pParse, p, i);
 1.96244 +      if( 0==*apColl ){
 1.96245 +        *apColl = db->pDfltColl;
 1.96246 +      }
 1.96247 +    }
 1.96248 +    pKeyInfo->aSortOrder = (u8*)apColl;
 1.96249 +
 1.96250 +    for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
 1.96251 +      for(i=0; i<2; i++){
 1.96252 +        int addr = pLoop->addrOpenEphm[i];
 1.96253 +        if( addr<0 ){
 1.96254 +          /* If [0] is unused then [1] is also unused.  So we can
 1.96255 +          ** always safely abort as soon as the first unused slot is found */
 1.96256 +          assert( pLoop->addrOpenEphm[1]<0 );
 1.96257 +          break;
 1.96258 +        }
 1.96259 +        sqlite3VdbeChangeP2(v, addr, nCol);
 1.96260 +        sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
 1.96261 +        pLoop->addrOpenEphm[i] = -1;
 1.96262 +      }
 1.96263 +    }
 1.96264 +    sqlite3DbFree(db, pKeyInfo);
 1.96265 +  }
 1.96266 +
 1.96267 +multi_select_end:
 1.96268 +  pDest->iSdst = dest.iSdst;
 1.96269 +  pDest->nSdst = dest.nSdst;
 1.96270 +  sqlite3SelectDelete(db, pDelete);
 1.96271 +  return rc;
 1.96272 +}
 1.96273 +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
 1.96274 +
 1.96275 +/*
 1.96276 +** Code an output subroutine for a coroutine implementation of a
 1.96277 +** SELECT statment.
 1.96278 +**
 1.96279 +** The data to be output is contained in pIn->iSdst.  There are
 1.96280 +** pIn->nSdst columns to be output.  pDest is where the output should
 1.96281 +** be sent.
 1.96282 +**
 1.96283 +** regReturn is the number of the register holding the subroutine
 1.96284 +** return address.
 1.96285 +**
 1.96286 +** If regPrev>0 then it is the first register in a vector that
 1.96287 +** records the previous output.  mem[regPrev] is a flag that is false
 1.96288 +** if there has been no previous output.  If regPrev>0 then code is
 1.96289 +** generated to suppress duplicates.  pKeyInfo is used for comparing
 1.96290 +** keys.
 1.96291 +**
 1.96292 +** If the LIMIT found in p->iLimit is reached, jump immediately to
 1.96293 +** iBreak.
 1.96294 +*/
 1.96295 +static int generateOutputSubroutine(
 1.96296 +  Parse *pParse,          /* Parsing context */
 1.96297 +  Select *p,              /* The SELECT statement */
 1.96298 +  SelectDest *pIn,        /* Coroutine supplying data */
 1.96299 +  SelectDest *pDest,      /* Where to send the data */
 1.96300 +  int regReturn,          /* The return address register */
 1.96301 +  int regPrev,            /* Previous result register.  No uniqueness if 0 */
 1.96302 +  KeyInfo *pKeyInfo,      /* For comparing with previous entry */
 1.96303 +  int p4type,             /* The p4 type for pKeyInfo */
 1.96304 +  int iBreak              /* Jump here if we hit the LIMIT */
 1.96305 +){
 1.96306 +  Vdbe *v = pParse->pVdbe;
 1.96307 +  int iContinue;
 1.96308 +  int addr;
 1.96309 +
 1.96310 +  addr = sqlite3VdbeCurrentAddr(v);
 1.96311 +  iContinue = sqlite3VdbeMakeLabel(v);
 1.96312 +
 1.96313 +  /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
 1.96314 +  */
 1.96315 +  if( regPrev ){
 1.96316 +    int j1, j2;
 1.96317 +    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
 1.96318 +    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
 1.96319 +                              (char*)pKeyInfo, p4type);
 1.96320 +    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
 1.96321 +    sqlite3VdbeJumpHere(v, j1);
 1.96322 +    sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
 1.96323 +    sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
 1.96324 +  }
 1.96325 +  if( pParse->db->mallocFailed ) return 0;
 1.96326 +
 1.96327 +  /* Suppress the first OFFSET entries if there is an OFFSET clause
 1.96328 +  */
 1.96329 +  codeOffset(v, p, iContinue);
 1.96330 +
 1.96331 +  switch( pDest->eDest ){
 1.96332 +    /* Store the result as data using a unique key.
 1.96333 +    */
 1.96334 +    case SRT_Table:
 1.96335 +    case SRT_EphemTab: {
 1.96336 +      int r1 = sqlite3GetTempReg(pParse);
 1.96337 +      int r2 = sqlite3GetTempReg(pParse);
 1.96338 +      testcase( pDest->eDest==SRT_Table );
 1.96339 +      testcase( pDest->eDest==SRT_EphemTab );
 1.96340 +      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
 1.96341 +      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
 1.96342 +      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
 1.96343 +      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 1.96344 +      sqlite3ReleaseTempReg(pParse, r2);
 1.96345 +      sqlite3ReleaseTempReg(pParse, r1);
 1.96346 +      break;
 1.96347 +    }
 1.96348 +
 1.96349 +#ifndef SQLITE_OMIT_SUBQUERY
 1.96350 +    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 1.96351 +    ** then there should be a single item on the stack.  Write this
 1.96352 +    ** item into the set table with bogus data.
 1.96353 +    */
 1.96354 +    case SRT_Set: {
 1.96355 +      int r1;
 1.96356 +      assert( pIn->nSdst==1 );
 1.96357 +      pDest->affSdst = 
 1.96358 +         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
 1.96359 +      r1 = sqlite3GetTempReg(pParse);
 1.96360 +      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
 1.96361 +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
 1.96362 +      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
 1.96363 +      sqlite3ReleaseTempReg(pParse, r1);
 1.96364 +      break;
 1.96365 +    }
 1.96366 +
 1.96367 +#if 0  /* Never occurs on an ORDER BY query */
 1.96368 +    /* If any row exist in the result set, record that fact and abort.
 1.96369 +    */
 1.96370 +    case SRT_Exists: {
 1.96371 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
 1.96372 +      /* The LIMIT clause will terminate the loop for us */
 1.96373 +      break;
 1.96374 +    }
 1.96375 +#endif
 1.96376 +
 1.96377 +    /* If this is a scalar select that is part of an expression, then
 1.96378 +    ** store the results in the appropriate memory cell and break out
 1.96379 +    ** of the scan loop.
 1.96380 +    */
 1.96381 +    case SRT_Mem: {
 1.96382 +      assert( pIn->nSdst==1 );
 1.96383 +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
 1.96384 +      /* The LIMIT clause will jump out of the loop for us */
 1.96385 +      break;
 1.96386 +    }
 1.96387 +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 1.96388 +
 1.96389 +    /* The results are stored in a sequence of registers
 1.96390 +    ** starting at pDest->iSdst.  Then the co-routine yields.
 1.96391 +    */
 1.96392 +    case SRT_Coroutine: {
 1.96393 +      if( pDest->iSdst==0 ){
 1.96394 +        pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
 1.96395 +        pDest->nSdst = pIn->nSdst;
 1.96396 +      }
 1.96397 +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
 1.96398 +      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 1.96399 +      break;
 1.96400 +    }
 1.96401 +
 1.96402 +    /* If none of the above, then the result destination must be
 1.96403 +    ** SRT_Output.  This routine is never called with any other
 1.96404 +    ** destination other than the ones handled above or SRT_Output.
 1.96405 +    **
 1.96406 +    ** For SRT_Output, results are stored in a sequence of registers.  
 1.96407 +    ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
 1.96408 +    ** return the next row of result.
 1.96409 +    */
 1.96410 +    default: {
 1.96411 +      assert( pDest->eDest==SRT_Output );
 1.96412 +      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
 1.96413 +      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
 1.96414 +      break;
 1.96415 +    }
 1.96416 +  }
 1.96417 +
 1.96418 +  /* Jump to the end of the loop if the LIMIT is reached.
 1.96419 +  */
 1.96420 +  if( p->iLimit ){
 1.96421 +    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
 1.96422 +  }
 1.96423 +
 1.96424 +  /* Generate the subroutine return
 1.96425 +  */
 1.96426 +  sqlite3VdbeResolveLabel(v, iContinue);
 1.96427 +  sqlite3VdbeAddOp1(v, OP_Return, regReturn);
 1.96428 +
 1.96429 +  return addr;
 1.96430 +}
 1.96431 +
 1.96432 +/*
 1.96433 +** Alternative compound select code generator for cases when there
 1.96434 +** is an ORDER BY clause.
 1.96435 +**
 1.96436 +** We assume a query of the following form:
 1.96437 +**
 1.96438 +**      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
 1.96439 +**
 1.96440 +** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
 1.96441 +** is to code both <selectA> and <selectB> with the ORDER BY clause as
 1.96442 +** co-routines.  Then run the co-routines in parallel and merge the results
 1.96443 +** into the output.  In addition to the two coroutines (called selectA and
 1.96444 +** selectB) there are 7 subroutines:
 1.96445 +**
 1.96446 +**    outA:    Move the output of the selectA coroutine into the output
 1.96447 +**             of the compound query.
 1.96448 +**
 1.96449 +**    outB:    Move the output of the selectB coroutine into the output
 1.96450 +**             of the compound query.  (Only generated for UNION and
 1.96451 +**             UNION ALL.  EXCEPT and INSERTSECT never output a row that
 1.96452 +**             appears only in B.)
 1.96453 +**
 1.96454 +**    AltB:    Called when there is data from both coroutines and A<B.
 1.96455 +**
 1.96456 +**    AeqB:    Called when there is data from both coroutines and A==B.
 1.96457 +**
 1.96458 +**    AgtB:    Called when there is data from both coroutines and A>B.
 1.96459 +**
 1.96460 +**    EofA:    Called when data is exhausted from selectA.
 1.96461 +**
 1.96462 +**    EofB:    Called when data is exhausted from selectB.
 1.96463 +**
 1.96464 +** The implementation of the latter five subroutines depend on which 
 1.96465 +** <operator> is used:
 1.96466 +**
 1.96467 +**
 1.96468 +**             UNION ALL         UNION            EXCEPT          INTERSECT
 1.96469 +**          -------------  -----------------  --------------  -----------------
 1.96470 +**   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
 1.96471 +**
 1.96472 +**   AeqB:   outA, nextA         nextA             nextA         outA, nextA
 1.96473 +**
 1.96474 +**   AgtB:   outB, nextB      outB, nextB          nextB            nextB
 1.96475 +**
 1.96476 +**   EofA:   outB, nextB      outB, nextB          halt             halt
 1.96477 +**
 1.96478 +**   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
 1.96479 +**
 1.96480 +** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
 1.96481 +** causes an immediate jump to EofA and an EOF on B following nextB causes
 1.96482 +** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
 1.96483 +** following nextX causes a jump to the end of the select processing.
 1.96484 +**
 1.96485 +** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
 1.96486 +** within the output subroutine.  The regPrev register set holds the previously
 1.96487 +** output value.  A comparison is made against this value and the output
 1.96488 +** is skipped if the next results would be the same as the previous.
 1.96489 +**
 1.96490 +** The implementation plan is to implement the two coroutines and seven
 1.96491 +** subroutines first, then put the control logic at the bottom.  Like this:
 1.96492 +**
 1.96493 +**          goto Init
 1.96494 +**     coA: coroutine for left query (A)
 1.96495 +**     coB: coroutine for right query (B)
 1.96496 +**    outA: output one row of A
 1.96497 +**    outB: output one row of B (UNION and UNION ALL only)
 1.96498 +**    EofA: ...
 1.96499 +**    EofB: ...
 1.96500 +**    AltB: ...
 1.96501 +**    AeqB: ...
 1.96502 +**    AgtB: ...
 1.96503 +**    Init: initialize coroutine registers
 1.96504 +**          yield coA
 1.96505 +**          if eof(A) goto EofA
 1.96506 +**          yield coB
 1.96507 +**          if eof(B) goto EofB
 1.96508 +**    Cmpr: Compare A, B
 1.96509 +**          Jump AltB, AeqB, AgtB
 1.96510 +**     End: ...
 1.96511 +**
 1.96512 +** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
 1.96513 +** actually called using Gosub and they do not Return.  EofA and EofB loop
 1.96514 +** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
 1.96515 +** and AgtB jump to either L2 or to one of EofA or EofB.
 1.96516 +*/
 1.96517 +#ifndef SQLITE_OMIT_COMPOUND_SELECT
 1.96518 +static int multiSelectOrderBy(
 1.96519 +  Parse *pParse,        /* Parsing context */
 1.96520 +  Select *p,            /* The right-most of SELECTs to be coded */
 1.96521 +  SelectDest *pDest     /* What to do with query results */
 1.96522 +){
 1.96523 +  int i, j;             /* Loop counters */
 1.96524 +  Select *pPrior;       /* Another SELECT immediately to our left */
 1.96525 +  Vdbe *v;              /* Generate code to this VDBE */
 1.96526 +  SelectDest destA;     /* Destination for coroutine A */
 1.96527 +  SelectDest destB;     /* Destination for coroutine B */
 1.96528 +  int regAddrA;         /* Address register for select-A coroutine */
 1.96529 +  int regEofA;          /* Flag to indicate when select-A is complete */
 1.96530 +  int regAddrB;         /* Address register for select-B coroutine */
 1.96531 +  int regEofB;          /* Flag to indicate when select-B is complete */
 1.96532 +  int addrSelectA;      /* Address of the select-A coroutine */
 1.96533 +  int addrSelectB;      /* Address of the select-B coroutine */
 1.96534 +  int regOutA;          /* Address register for the output-A subroutine */
 1.96535 +  int regOutB;          /* Address register for the output-B subroutine */
 1.96536 +  int addrOutA;         /* Address of the output-A subroutine */
 1.96537 +  int addrOutB = 0;     /* Address of the output-B subroutine */
 1.96538 +  int addrEofA;         /* Address of the select-A-exhausted subroutine */
 1.96539 +  int addrEofB;         /* Address of the select-B-exhausted subroutine */
 1.96540 +  int addrAltB;         /* Address of the A<B subroutine */
 1.96541 +  int addrAeqB;         /* Address of the A==B subroutine */
 1.96542 +  int addrAgtB;         /* Address of the A>B subroutine */
 1.96543 +  int regLimitA;        /* Limit register for select-A */
 1.96544 +  int regLimitB;        /* Limit register for select-A */
 1.96545 +  int regPrev;          /* A range of registers to hold previous output */
 1.96546 +  int savedLimit;       /* Saved value of p->iLimit */
 1.96547 +  int savedOffset;      /* Saved value of p->iOffset */
 1.96548 +  int labelCmpr;        /* Label for the start of the merge algorithm */
 1.96549 +  int labelEnd;         /* Label for the end of the overall SELECT stmt */
 1.96550 +  int j1;               /* Jump instructions that get retargetted */
 1.96551 +  int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
 1.96552 +  KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
 1.96553 +  KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
 1.96554 +  sqlite3 *db;          /* Database connection */
 1.96555 +  ExprList *pOrderBy;   /* The ORDER BY clause */
 1.96556 +  int nOrderBy;         /* Number of terms in the ORDER BY clause */
 1.96557 +  int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
 1.96558 +#ifndef SQLITE_OMIT_EXPLAIN
 1.96559 +  int iSub1;            /* EQP id of left-hand query */
 1.96560 +  int iSub2;            /* EQP id of right-hand query */
 1.96561 +#endif
 1.96562 +
 1.96563 +  assert( p->pOrderBy!=0 );
 1.96564 +  assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
 1.96565 +  db = pParse->db;
 1.96566 +  v = pParse->pVdbe;
 1.96567 +  assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
 1.96568 +  labelEnd = sqlite3VdbeMakeLabel(v);
 1.96569 +  labelCmpr = sqlite3VdbeMakeLabel(v);
 1.96570 +
 1.96571 +
 1.96572 +  /* Patch up the ORDER BY clause
 1.96573 +  */
 1.96574 +  op = p->op;  
 1.96575 +  pPrior = p->pPrior;
 1.96576 +  assert( pPrior->pOrderBy==0 );
 1.96577 +  pOrderBy = p->pOrderBy;
 1.96578 +  assert( pOrderBy );
 1.96579 +  nOrderBy = pOrderBy->nExpr;
 1.96580 +
 1.96581 +  /* For operators other than UNION ALL we have to make sure that
 1.96582 +  ** the ORDER BY clause covers every term of the result set.  Add
 1.96583 +  ** terms to the ORDER BY clause as necessary.
 1.96584 +  */
 1.96585 +  if( op!=TK_ALL ){
 1.96586 +    for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
 1.96587 +      struct ExprList_item *pItem;
 1.96588 +      for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
 1.96589 +        assert( pItem->iOrderByCol>0 );
 1.96590 +        if( pItem->iOrderByCol==i ) break;
 1.96591 +      }
 1.96592 +      if( j==nOrderBy ){
 1.96593 +        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 1.96594 +        if( pNew==0 ) return SQLITE_NOMEM;
 1.96595 +        pNew->flags |= EP_IntValue;
 1.96596 +        pNew->u.iValue = i;
 1.96597 +        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
 1.96598 +        if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
 1.96599 +      }
 1.96600 +    }
 1.96601 +  }
 1.96602 +
 1.96603 +  /* Compute the comparison permutation and keyinfo that is used with
 1.96604 +  ** the permutation used to determine if the next
 1.96605 +  ** row of results comes from selectA or selectB.  Also add explicit
 1.96606 +  ** collations to the ORDER BY clause terms so that when the subqueries
 1.96607 +  ** to the right and the left are evaluated, they use the correct
 1.96608 +  ** collation.
 1.96609 +  */
 1.96610 +  aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
 1.96611 +  if( aPermute ){
 1.96612 +    struct ExprList_item *pItem;
 1.96613 +    for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
 1.96614 +      assert( pItem->iOrderByCol>0  && pItem->iOrderByCol<=p->pEList->nExpr );
 1.96615 +      aPermute[i] = pItem->iOrderByCol - 1;
 1.96616 +    }
 1.96617 +    pKeyMerge =
 1.96618 +      sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
 1.96619 +    if( pKeyMerge ){
 1.96620 +      pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
 1.96621 +      pKeyMerge->nField = (u16)nOrderBy;
 1.96622 +      pKeyMerge->enc = ENC(db);
 1.96623 +      for(i=0; i<nOrderBy; i++){
 1.96624 +        CollSeq *pColl;
 1.96625 +        Expr *pTerm = pOrderBy->a[i].pExpr;
 1.96626 +        if( pTerm->flags & EP_Collate ){
 1.96627 +          pColl = sqlite3ExprCollSeq(pParse, pTerm);
 1.96628 +        }else{
 1.96629 +          pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
 1.96630 +          if( pColl==0 ) pColl = db->pDfltColl;
 1.96631 +          pOrderBy->a[i].pExpr =
 1.96632 +             sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
 1.96633 +        }
 1.96634 +        pKeyMerge->aColl[i] = pColl;
 1.96635 +        pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
 1.96636 +      }
 1.96637 +    }
 1.96638 +  }else{
 1.96639 +    pKeyMerge = 0;
 1.96640 +  }
 1.96641 +
 1.96642 +  /* Reattach the ORDER BY clause to the query.
 1.96643 +  */
 1.96644 +  p->pOrderBy = pOrderBy;
 1.96645 +  pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
 1.96646 +
 1.96647 +  /* Allocate a range of temporary registers and the KeyInfo needed
 1.96648 +  ** for the logic that removes duplicate result rows when the
 1.96649 +  ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
 1.96650 +  */
 1.96651 +  if( op==TK_ALL ){
 1.96652 +    regPrev = 0;
 1.96653 +  }else{
 1.96654 +    int nExpr = p->pEList->nExpr;
 1.96655 +    assert( nOrderBy>=nExpr || db->mallocFailed );
 1.96656 +    regPrev = sqlite3GetTempRange(pParse, nExpr+1);
 1.96657 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
 1.96658 +    pKeyDup = sqlite3DbMallocZero(db,
 1.96659 +                  sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
 1.96660 +    if( pKeyDup ){
 1.96661 +      pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
 1.96662 +      pKeyDup->nField = (u16)nExpr;
 1.96663 +      pKeyDup->enc = ENC(db);
 1.96664 +      for(i=0; i<nExpr; i++){
 1.96665 +        pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
 1.96666 +        pKeyDup->aSortOrder[i] = 0;
 1.96667 +      }
 1.96668 +    }
 1.96669 +  }
 1.96670 + 
 1.96671 +  /* Separate the left and the right query from one another
 1.96672 +  */
 1.96673 +  p->pPrior = 0;
 1.96674 +  sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
 1.96675 +  if( pPrior->pPrior==0 ){
 1.96676 +    sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
 1.96677 +  }
 1.96678 +
 1.96679 +  /* Compute the limit registers */
 1.96680 +  computeLimitRegisters(pParse, p, labelEnd);
 1.96681 +  if( p->iLimit && op==TK_ALL ){
 1.96682 +    regLimitA = ++pParse->nMem;
 1.96683 +    regLimitB = ++pParse->nMem;
 1.96684 +    sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
 1.96685 +                                  regLimitA);
 1.96686 +    sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
 1.96687 +  }else{
 1.96688 +    regLimitA = regLimitB = 0;
 1.96689 +  }
 1.96690 +  sqlite3ExprDelete(db, p->pLimit);
 1.96691 +  p->pLimit = 0;
 1.96692 +  sqlite3ExprDelete(db, p->pOffset);
 1.96693 +  p->pOffset = 0;
 1.96694 +
 1.96695 +  regAddrA = ++pParse->nMem;
 1.96696 +  regEofA = ++pParse->nMem;
 1.96697 +  regAddrB = ++pParse->nMem;
 1.96698 +  regEofB = ++pParse->nMem;
 1.96699 +  regOutA = ++pParse->nMem;
 1.96700 +  regOutB = ++pParse->nMem;
 1.96701 +  sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
 1.96702 +  sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
 1.96703 +
 1.96704 +  /* Jump past the various subroutines and coroutines to the main
 1.96705 +  ** merge loop
 1.96706 +  */
 1.96707 +  j1 = sqlite3VdbeAddOp0(v, OP_Goto);
 1.96708 +  addrSelectA = sqlite3VdbeCurrentAddr(v);
 1.96709 +
 1.96710 +
 1.96711 +  /* Generate a coroutine to evaluate the SELECT statement to the
 1.96712 +  ** left of the compound operator - the "A" select.
 1.96713 +  */
 1.96714 +  VdbeNoopComment((v, "Begin coroutine for left SELECT"));
 1.96715 +  pPrior->iLimit = regLimitA;
 1.96716 +  explainSetInteger(iSub1, pParse->iNextSelectId);
 1.96717 +  sqlite3Select(pParse, pPrior, &destA);
 1.96718 +  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
 1.96719 +  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 1.96720 +  VdbeNoopComment((v, "End coroutine for left SELECT"));
 1.96721 +
 1.96722 +  /* Generate a coroutine to evaluate the SELECT statement on 
 1.96723 +  ** the right - the "B" select
 1.96724 +  */
 1.96725 +  addrSelectB = sqlite3VdbeCurrentAddr(v);
 1.96726 +  VdbeNoopComment((v, "Begin coroutine for right SELECT"));
 1.96727 +  savedLimit = p->iLimit;
 1.96728 +  savedOffset = p->iOffset;
 1.96729 +  p->iLimit = regLimitB;
 1.96730 +  p->iOffset = 0;  
 1.96731 +  explainSetInteger(iSub2, pParse->iNextSelectId);
 1.96732 +  sqlite3Select(pParse, p, &destB);
 1.96733 +  p->iLimit = savedLimit;
 1.96734 +  p->iOffset = savedOffset;
 1.96735 +  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
 1.96736 +  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
 1.96737 +  VdbeNoopComment((v, "End coroutine for right SELECT"));
 1.96738 +
 1.96739 +  /* Generate a subroutine that outputs the current row of the A
 1.96740 +  ** select as the next output row of the compound select.
 1.96741 +  */
 1.96742 +  VdbeNoopComment((v, "Output routine for A"));
 1.96743 +  addrOutA = generateOutputSubroutine(pParse,
 1.96744 +                 p, &destA, pDest, regOutA,
 1.96745 +                 regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
 1.96746 +  
 1.96747 +  /* Generate a subroutine that outputs the current row of the B
 1.96748 +  ** select as the next output row of the compound select.
 1.96749 +  */
 1.96750 +  if( op==TK_ALL || op==TK_UNION ){
 1.96751 +    VdbeNoopComment((v, "Output routine for B"));
 1.96752 +    addrOutB = generateOutputSubroutine(pParse,
 1.96753 +                 p, &destB, pDest, regOutB,
 1.96754 +                 regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
 1.96755 +  }
 1.96756 +
 1.96757 +  /* Generate a subroutine to run when the results from select A
 1.96758 +  ** are exhausted and only data in select B remains.
 1.96759 +  */
 1.96760 +  VdbeNoopComment((v, "eof-A subroutine"));
 1.96761 +  if( op==TK_EXCEPT || op==TK_INTERSECT ){
 1.96762 +    addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
 1.96763 +  }else{  
 1.96764 +    addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
 1.96765 +    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 1.96766 +    sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
 1.96767 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
 1.96768 +    p->nSelectRow += pPrior->nSelectRow;
 1.96769 +  }
 1.96770 +
 1.96771 +  /* Generate a subroutine to run when the results from select B
 1.96772 +  ** are exhausted and only data in select A remains.
 1.96773 +  */
 1.96774 +  if( op==TK_INTERSECT ){
 1.96775 +    addrEofB = addrEofA;
 1.96776 +    if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
 1.96777 +  }else{  
 1.96778 +    VdbeNoopComment((v, "eof-B subroutine"));
 1.96779 +    addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
 1.96780 +    sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 1.96781 +    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 1.96782 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
 1.96783 +  }
 1.96784 +
 1.96785 +  /* Generate code to handle the case of A<B
 1.96786 +  */
 1.96787 +  VdbeNoopComment((v, "A-lt-B subroutine"));
 1.96788 +  addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 1.96789 +  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 1.96790 +  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
 1.96791 +  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 1.96792 +
 1.96793 +  /* Generate code to handle the case of A==B
 1.96794 +  */
 1.96795 +  if( op==TK_ALL ){
 1.96796 +    addrAeqB = addrAltB;
 1.96797 +  }else if( op==TK_INTERSECT ){
 1.96798 +    addrAeqB = addrAltB;
 1.96799 +    addrAltB++;
 1.96800 +  }else{
 1.96801 +    VdbeNoopComment((v, "A-eq-B subroutine"));
 1.96802 +    addrAeqB =
 1.96803 +    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
 1.96804 +    sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
 1.96805 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 1.96806 +  }
 1.96807 +
 1.96808 +  /* Generate code to handle the case of A>B
 1.96809 +  */
 1.96810 +  VdbeNoopComment((v, "A-gt-B subroutine"));
 1.96811 +  addrAgtB = sqlite3VdbeCurrentAddr(v);
 1.96812 +  if( op==TK_ALL || op==TK_UNION ){
 1.96813 +    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 1.96814 +  }
 1.96815 +  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
 1.96816 +  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
 1.96817 +  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 1.96818 +
 1.96819 +  /* This code runs once to initialize everything.
 1.96820 +  */
 1.96821 +  sqlite3VdbeJumpHere(v, j1);
 1.96822 +  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
 1.96823 +  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
 1.96824 +  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
 1.96825 +  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
 1.96826 +  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
 1.96827 +  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
 1.96828 +
 1.96829 +  /* Implement the main merge loop
 1.96830 +  */
 1.96831 +  sqlite3VdbeResolveLabel(v, labelCmpr);
 1.96832 +  sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
 1.96833 +  sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
 1.96834 +                         (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
 1.96835 +  sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
 1.96836 +  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
 1.96837 +
 1.96838 +  /* Release temporary registers
 1.96839 +  */
 1.96840 +  if( regPrev ){
 1.96841 +    sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
 1.96842 +  }
 1.96843 +
 1.96844 +  /* Jump to the this point in order to terminate the query.
 1.96845 +  */
 1.96846 +  sqlite3VdbeResolveLabel(v, labelEnd);
 1.96847 +
 1.96848 +  /* Set the number of output columns
 1.96849 +  */
 1.96850 +  if( pDest->eDest==SRT_Output ){
 1.96851 +    Select *pFirst = pPrior;
 1.96852 +    while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 1.96853 +    generateColumnNames(pParse, 0, pFirst->pEList);
 1.96854 +  }
 1.96855 +
 1.96856 +  /* Reassembly the compound query so that it will be freed correctly
 1.96857 +  ** by the calling function */
 1.96858 +  if( p->pPrior ){
 1.96859 +    sqlite3SelectDelete(db, p->pPrior);
 1.96860 +  }
 1.96861 +  p->pPrior = pPrior;
 1.96862 +
 1.96863 +  /*** TBD:  Insert subroutine calls to close cursors on incomplete
 1.96864 +  **** subqueries ****/
 1.96865 +  explainComposite(pParse, p->op, iSub1, iSub2, 0);
 1.96866 +  return SQLITE_OK;
 1.96867 +}
 1.96868 +#endif
 1.96869 +
 1.96870 +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 1.96871 +/* Forward Declarations */
 1.96872 +static void substExprList(sqlite3*, ExprList*, int, ExprList*);
 1.96873 +static void substSelect(sqlite3*, Select *, int, ExprList *);
 1.96874 +
 1.96875 +/*
 1.96876 +** Scan through the expression pExpr.  Replace every reference to
 1.96877 +** a column in table number iTable with a copy of the iColumn-th
 1.96878 +** entry in pEList.  (But leave references to the ROWID column 
 1.96879 +** unchanged.)
 1.96880 +**
 1.96881 +** This routine is part of the flattening procedure.  A subquery
 1.96882 +** whose result set is defined by pEList appears as entry in the
 1.96883 +** FROM clause of a SELECT such that the VDBE cursor assigned to that
 1.96884 +** FORM clause entry is iTable.  This routine make the necessary 
 1.96885 +** changes to pExpr so that it refers directly to the source table
 1.96886 +** of the subquery rather the result set of the subquery.
 1.96887 +*/
 1.96888 +static Expr *substExpr(
 1.96889 +  sqlite3 *db,        /* Report malloc errors to this connection */
 1.96890 +  Expr *pExpr,        /* Expr in which substitution occurs */
 1.96891 +  int iTable,         /* Table to be substituted */
 1.96892 +  ExprList *pEList    /* Substitute expressions */
 1.96893 +){
 1.96894 +  if( pExpr==0 ) return 0;
 1.96895 +  if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
 1.96896 +    if( pExpr->iColumn<0 ){
 1.96897 +      pExpr->op = TK_NULL;
 1.96898 +    }else{
 1.96899 +      Expr *pNew;
 1.96900 +      assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
 1.96901 +      assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 1.96902 +      pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
 1.96903 +      sqlite3ExprDelete(db, pExpr);
 1.96904 +      pExpr = pNew;
 1.96905 +    }
 1.96906 +  }else{
 1.96907 +    pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
 1.96908 +    pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
 1.96909 +    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 1.96910 +      substSelect(db, pExpr->x.pSelect, iTable, pEList);
 1.96911 +    }else{
 1.96912 +      substExprList(db, pExpr->x.pList, iTable, pEList);
 1.96913 +    }
 1.96914 +  }
 1.96915 +  return pExpr;
 1.96916 +}
 1.96917 +static void substExprList(
 1.96918 +  sqlite3 *db,         /* Report malloc errors here */
 1.96919 +  ExprList *pList,     /* List to scan and in which to make substitutes */
 1.96920 +  int iTable,          /* Table to be substituted */
 1.96921 +  ExprList *pEList     /* Substitute values */
 1.96922 +){
 1.96923 +  int i;
 1.96924 +  if( pList==0 ) return;
 1.96925 +  for(i=0; i<pList->nExpr; i++){
 1.96926 +    pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
 1.96927 +  }
 1.96928 +}
 1.96929 +static void substSelect(
 1.96930 +  sqlite3 *db,         /* Report malloc errors here */
 1.96931 +  Select *p,           /* SELECT statement in which to make substitutions */
 1.96932 +  int iTable,          /* Table to be replaced */
 1.96933 +  ExprList *pEList     /* Substitute values */
 1.96934 +){
 1.96935 +  SrcList *pSrc;
 1.96936 +  struct SrcList_item *pItem;
 1.96937 +  int i;
 1.96938 +  if( !p ) return;
 1.96939 +  substExprList(db, p->pEList, iTable, pEList);
 1.96940 +  substExprList(db, p->pGroupBy, iTable, pEList);
 1.96941 +  substExprList(db, p->pOrderBy, iTable, pEList);
 1.96942 +  p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
 1.96943 +  p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
 1.96944 +  substSelect(db, p->pPrior, iTable, pEList);
 1.96945 +  pSrc = p->pSrc;
 1.96946 +  assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
 1.96947 +  if( ALWAYS(pSrc) ){
 1.96948 +    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
 1.96949 +      substSelect(db, pItem->pSelect, iTable, pEList);
 1.96950 +    }
 1.96951 +  }
 1.96952 +}
 1.96953 +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 1.96954 +
 1.96955 +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 1.96956 +/*
 1.96957 +** This routine attempts to flatten subqueries as a performance optimization.
 1.96958 +** This routine returns 1 if it makes changes and 0 if no flattening occurs.
 1.96959 +**
 1.96960 +** To understand the concept of flattening, consider the following
 1.96961 +** query:
 1.96962 +**
 1.96963 +**     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
 1.96964 +**
 1.96965 +** The default way of implementing this query is to execute the
 1.96966 +** subquery first and store the results in a temporary table, then
 1.96967 +** run the outer query on that temporary table.  This requires two
 1.96968 +** passes over the data.  Furthermore, because the temporary table
 1.96969 +** has no indices, the WHERE clause on the outer query cannot be
 1.96970 +** optimized.
 1.96971 +**
 1.96972 +** This routine attempts to rewrite queries such as the above into
 1.96973 +** a single flat select, like this:
 1.96974 +**
 1.96975 +**     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
 1.96976 +**
 1.96977 +** The code generated for this simpification gives the same result
 1.96978 +** but only has to scan the data once.  And because indices might 
 1.96979 +** exist on the table t1, a complete scan of the data might be
 1.96980 +** avoided.
 1.96981 +**
 1.96982 +** Flattening is only attempted if all of the following are true:
 1.96983 +**
 1.96984 +**   (1)  The subquery and the outer query do not both use aggregates.
 1.96985 +**
 1.96986 +**   (2)  The subquery is not an aggregate or the outer query is not a join.
 1.96987 +**
 1.96988 +**   (3)  The subquery is not the right operand of a left outer join
 1.96989 +**        (Originally ticket #306.  Strengthened by ticket #3300)
 1.96990 +**
 1.96991 +**   (4)  The subquery is not DISTINCT.
 1.96992 +**
 1.96993 +**  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
 1.96994 +**        sub-queries that were excluded from this optimization. Restriction 
 1.96995 +**        (4) has since been expanded to exclude all DISTINCT subqueries.
 1.96996 +**
 1.96997 +**   (6)  The subquery does not use aggregates or the outer query is not
 1.96998 +**        DISTINCT.
 1.96999 +**
 1.97000 +**   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
 1.97001 +**        A FROM clause, consider adding a FROM close with the special
 1.97002 +**        table sqlite_once that consists of a single row containing a
 1.97003 +**        single NULL.
 1.97004 +**
 1.97005 +**   (8)  The subquery does not use LIMIT or the outer query is not a join.
 1.97006 +**
 1.97007 +**   (9)  The subquery does not use LIMIT or the outer query does not use
 1.97008 +**        aggregates.
 1.97009 +**
 1.97010 +**  (10)  The subquery does not use aggregates or the outer query does not
 1.97011 +**        use LIMIT.
 1.97012 +**
 1.97013 +**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
 1.97014 +**
 1.97015 +**  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
 1.97016 +**        a separate restriction deriving from ticket #350.
 1.97017 +**
 1.97018 +**  (13)  The subquery and outer query do not both use LIMIT.
 1.97019 +**
 1.97020 +**  (14)  The subquery does not use OFFSET.
 1.97021 +**
 1.97022 +**  (15)  The outer query is not part of a compound select or the
 1.97023 +**        subquery does not have a LIMIT clause.
 1.97024 +**        (See ticket #2339 and ticket [02a8e81d44]).
 1.97025 +**
 1.97026 +**  (16)  The outer query is not an aggregate or the subquery does
 1.97027 +**        not contain ORDER BY.  (Ticket #2942)  This used to not matter
 1.97028 +**        until we introduced the group_concat() function.  
 1.97029 +**
 1.97030 +**  (17)  The sub-query is not a compound select, or it is a UNION ALL 
 1.97031 +**        compound clause made up entirely of non-aggregate queries, and 
 1.97032 +**        the parent query:
 1.97033 +**
 1.97034 +**          * is not itself part of a compound select,
 1.97035 +**          * is not an aggregate or DISTINCT query, and
 1.97036 +**          * is not a join
 1.97037 +**
 1.97038 +**        The parent and sub-query may contain WHERE clauses. Subject to
 1.97039 +**        rules (11), (13) and (14), they may also contain ORDER BY,
 1.97040 +**        LIMIT and OFFSET clauses.  The subquery cannot use any compound
 1.97041 +**        operator other than UNION ALL because all the other compound
 1.97042 +**        operators have an implied DISTINCT which is disallowed by
 1.97043 +**        restriction (4).
 1.97044 +**
 1.97045 +**        Also, each component of the sub-query must return the same number
 1.97046 +**        of result columns. This is actually a requirement for any compound
 1.97047 +**        SELECT statement, but all the code here does is make sure that no
 1.97048 +**        such (illegal) sub-query is flattened. The caller will detect the
 1.97049 +**        syntax error and return a detailed message.
 1.97050 +**
 1.97051 +**  (18)  If the sub-query is a compound select, then all terms of the
 1.97052 +**        ORDER by clause of the parent must be simple references to 
 1.97053 +**        columns of the sub-query.
 1.97054 +**
 1.97055 +**  (19)  The subquery does not use LIMIT or the outer query does not
 1.97056 +**        have a WHERE clause.
 1.97057 +**
 1.97058 +**  (20)  If the sub-query is a compound select, then it must not use
 1.97059 +**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
 1.97060 +**        somewhat by saying that the terms of the ORDER BY clause must
 1.97061 +**        appear as unmodified result columns in the outer query.  But we
 1.97062 +**        have other optimizations in mind to deal with that case.
 1.97063 +**
 1.97064 +**  (21)  The subquery does not use LIMIT or the outer query is not
 1.97065 +**        DISTINCT.  (See ticket [752e1646fc]).
 1.97066 +**
 1.97067 +** In this routine, the "p" parameter is a pointer to the outer query.
 1.97068 +** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
 1.97069 +** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
 1.97070 +**
 1.97071 +** If flattening is not attempted, this routine is a no-op and returns 0.
 1.97072 +** If flattening is attempted this routine returns 1.
 1.97073 +**
 1.97074 +** All of the expression analysis must occur on both the outer query and
 1.97075 +** the subquery before this routine runs.
 1.97076 +*/
 1.97077 +static int flattenSubquery(
 1.97078 +  Parse *pParse,       /* Parsing context */
 1.97079 +  Select *p,           /* The parent or outer SELECT statement */
 1.97080 +  int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
 1.97081 +  int isAgg,           /* True if outer SELECT uses aggregate functions */
 1.97082 +  int subqueryIsAgg    /* True if the subquery uses aggregate functions */
 1.97083 +){
 1.97084 +  const char *zSavedAuthContext = pParse->zAuthContext;
 1.97085 +  Select *pParent;
 1.97086 +  Select *pSub;       /* The inner query or "subquery" */
 1.97087 +  Select *pSub1;      /* Pointer to the rightmost select in sub-query */
 1.97088 +  SrcList *pSrc;      /* The FROM clause of the outer query */
 1.97089 +  SrcList *pSubSrc;   /* The FROM clause of the subquery */
 1.97090 +  ExprList *pList;    /* The result set of the outer query */
 1.97091 +  int iParent;        /* VDBE cursor number of the pSub result set temp table */
 1.97092 +  int i;              /* Loop counter */
 1.97093 +  Expr *pWhere;                    /* The WHERE clause */
 1.97094 +  struct SrcList_item *pSubitem;   /* The subquery */
 1.97095 +  sqlite3 *db = pParse->db;
 1.97096 +
 1.97097 +  /* Check to see if flattening is permitted.  Return 0 if not.
 1.97098 +  */
 1.97099 +  assert( p!=0 );
 1.97100 +  assert( p->pPrior==0 );  /* Unable to flatten compound queries */
 1.97101 +  if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
 1.97102 +  pSrc = p->pSrc;
 1.97103 +  assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
 1.97104 +  pSubitem = &pSrc->a[iFrom];
 1.97105 +  iParent = pSubitem->iCursor;
 1.97106 +  pSub = pSubitem->pSelect;
 1.97107 +  assert( pSub!=0 );
 1.97108 +  if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
 1.97109 +  if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
 1.97110 +  pSubSrc = pSub->pSrc;
 1.97111 +  assert( pSubSrc );
 1.97112 +  /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
 1.97113 +  ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
 1.97114 +  ** because they could be computed at compile-time.  But when LIMIT and OFFSET
 1.97115 +  ** became arbitrary expressions, we were forced to add restrictions (13)
 1.97116 +  ** and (14). */
 1.97117 +  if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
 1.97118 +  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
 1.97119 +  if( p->pRightmost && pSub->pLimit ){
 1.97120 +    return 0;                                            /* Restriction (15) */
 1.97121 +  }
 1.97122 +  if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
 1.97123 +  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
 1.97124 +  if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
 1.97125 +     return 0;         /* Restrictions (8)(9) */
 1.97126 +  }
 1.97127 +  if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
 1.97128 +     return 0;         /* Restriction (6)  */
 1.97129 +  }
 1.97130 +  if( p->pOrderBy && pSub->pOrderBy ){
 1.97131 +     return 0;                                           /* Restriction (11) */
 1.97132 +  }
 1.97133 +  if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
 1.97134 +  if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
 1.97135 +  if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
 1.97136 +     return 0;         /* Restriction (21) */
 1.97137 +  }
 1.97138 +
 1.97139 +  /* OBSOLETE COMMENT 1:
 1.97140 +  ** Restriction 3:  If the subquery is a join, make sure the subquery is 
 1.97141 +  ** not used as the right operand of an outer join.  Examples of why this
 1.97142 +  ** is not allowed:
 1.97143 +  **
 1.97144 +  **         t1 LEFT OUTER JOIN (t2 JOIN t3)
 1.97145 +  **
 1.97146 +  ** If we flatten the above, we would get
 1.97147 +  **
 1.97148 +  **         (t1 LEFT OUTER JOIN t2) JOIN t3
 1.97149 +  **
 1.97150 +  ** which is not at all the same thing.
 1.97151 +  **
 1.97152 +  ** OBSOLETE COMMENT 2:
 1.97153 +  ** Restriction 12:  If the subquery is the right operand of a left outer
 1.97154 +  ** join, make sure the subquery has no WHERE clause.
 1.97155 +  ** An examples of why this is not allowed:
 1.97156 +  **
 1.97157 +  **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
 1.97158 +  **
 1.97159 +  ** If we flatten the above, we would get
 1.97160 +  **
 1.97161 +  **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
 1.97162 +  **
 1.97163 +  ** But the t2.x>0 test will always fail on a NULL row of t2, which
 1.97164 +  ** effectively converts the OUTER JOIN into an INNER JOIN.
 1.97165 +  **
 1.97166 +  ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
 1.97167 +  ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
 1.97168 +  ** is fraught with danger.  Best to avoid the whole thing.  If the
 1.97169 +  ** subquery is the right term of a LEFT JOIN, then do not flatten.
 1.97170 +  */
 1.97171 +  if( (pSubitem->jointype & JT_OUTER)!=0 ){
 1.97172 +    return 0;
 1.97173 +  }
 1.97174 +
 1.97175 +  /* Restriction 17: If the sub-query is a compound SELECT, then it must
 1.97176 +  ** use only the UNION ALL operator. And none of the simple select queries
 1.97177 +  ** that make up the compound SELECT are allowed to be aggregate or distinct
 1.97178 +  ** queries.
 1.97179 +  */
 1.97180 +  if( pSub->pPrior ){
 1.97181 +    if( pSub->pOrderBy ){
 1.97182 +      return 0;  /* Restriction 20 */
 1.97183 +    }
 1.97184 +    if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
 1.97185 +      return 0;
 1.97186 +    }
 1.97187 +    for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
 1.97188 +      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 1.97189 +      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 1.97190 +      assert( pSub->pSrc!=0 );
 1.97191 +      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
 1.97192 +       || (pSub1->pPrior && pSub1->op!=TK_ALL) 
 1.97193 +       || pSub1->pSrc->nSrc<1
 1.97194 +       || pSub->pEList->nExpr!=pSub1->pEList->nExpr
 1.97195 +      ){
 1.97196 +        return 0;
 1.97197 +      }
 1.97198 +      testcase( pSub1->pSrc->nSrc>1 );
 1.97199 +    }
 1.97200 +
 1.97201 +    /* Restriction 18. */
 1.97202 +    if( p->pOrderBy ){
 1.97203 +      int ii;
 1.97204 +      for(ii=0; ii<p->pOrderBy->nExpr; ii++){
 1.97205 +        if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
 1.97206 +      }
 1.97207 +    }
 1.97208 +  }
 1.97209 +
 1.97210 +  /***** If we reach this point, flattening is permitted. *****/
 1.97211 +
 1.97212 +  /* Authorize the subquery */
 1.97213 +  pParse->zAuthContext = pSubitem->zName;
 1.97214 +  TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
 1.97215 +  testcase( i==SQLITE_DENY );
 1.97216 +  pParse->zAuthContext = zSavedAuthContext;
 1.97217 +
 1.97218 +  /* If the sub-query is a compound SELECT statement, then (by restrictions
 1.97219 +  ** 17 and 18 above) it must be a UNION ALL and the parent query must 
 1.97220 +  ** be of the form:
 1.97221 +  **
 1.97222 +  **     SELECT <expr-list> FROM (<sub-query>) <where-clause> 
 1.97223 +  **
 1.97224 +  ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
 1.97225 +  ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or 
 1.97226 +  ** OFFSET clauses and joins them to the left-hand-side of the original
 1.97227 +  ** using UNION ALL operators. In this case N is the number of simple
 1.97228 +  ** select statements in the compound sub-query.
 1.97229 +  **
 1.97230 +  ** Example:
 1.97231 +  **
 1.97232 +  **     SELECT a+1 FROM (
 1.97233 +  **        SELECT x FROM tab
 1.97234 +  **        UNION ALL
 1.97235 +  **        SELECT y FROM tab
 1.97236 +  **        UNION ALL
 1.97237 +  **        SELECT abs(z*2) FROM tab2
 1.97238 +  **     ) WHERE a!=5 ORDER BY 1
 1.97239 +  **
 1.97240 +  ** Transformed into:
 1.97241 +  **
 1.97242 +  **     SELECT x+1 FROM tab WHERE x+1!=5
 1.97243 +  **     UNION ALL
 1.97244 +  **     SELECT y+1 FROM tab WHERE y+1!=5
 1.97245 +  **     UNION ALL
 1.97246 +  **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
 1.97247 +  **     ORDER BY 1
 1.97248 +  **
 1.97249 +  ** We call this the "compound-subquery flattening".
 1.97250 +  */
 1.97251 +  for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
 1.97252 +    Select *pNew;
 1.97253 +    ExprList *pOrderBy = p->pOrderBy;
 1.97254 +    Expr *pLimit = p->pLimit;
 1.97255 +    Select *pPrior = p->pPrior;
 1.97256 +    p->pOrderBy = 0;
 1.97257 +    p->pSrc = 0;
 1.97258 +    p->pPrior = 0;
 1.97259 +    p->pLimit = 0;
 1.97260 +    pNew = sqlite3SelectDup(db, p, 0);
 1.97261 +    p->pLimit = pLimit;
 1.97262 +    p->pOrderBy = pOrderBy;
 1.97263 +    p->pSrc = pSrc;
 1.97264 +    p->op = TK_ALL;
 1.97265 +    p->pRightmost = 0;
 1.97266 +    if( pNew==0 ){
 1.97267 +      pNew = pPrior;
 1.97268 +    }else{
 1.97269 +      pNew->pPrior = pPrior;
 1.97270 +      pNew->pRightmost = 0;
 1.97271 +    }
 1.97272 +    p->pPrior = pNew;
 1.97273 +    if( db->mallocFailed ) return 1;
 1.97274 +  }
 1.97275 +
 1.97276 +  /* Begin flattening the iFrom-th entry of the FROM clause 
 1.97277 +  ** in the outer query.
 1.97278 +  */
 1.97279 +  pSub = pSub1 = pSubitem->pSelect;
 1.97280 +
 1.97281 +  /* Delete the transient table structure associated with the
 1.97282 +  ** subquery
 1.97283 +  */
 1.97284 +  sqlite3DbFree(db, pSubitem->zDatabase);
 1.97285 +  sqlite3DbFree(db, pSubitem->zName);
 1.97286 +  sqlite3DbFree(db, pSubitem->zAlias);
 1.97287 +  pSubitem->zDatabase = 0;
 1.97288 +  pSubitem->zName = 0;
 1.97289 +  pSubitem->zAlias = 0;
 1.97290 +  pSubitem->pSelect = 0;
 1.97291 +
 1.97292 +  /* Defer deleting the Table object associated with the
 1.97293 +  ** subquery until code generation is
 1.97294 +  ** complete, since there may still exist Expr.pTab entries that
 1.97295 +  ** refer to the subquery even after flattening.  Ticket #3346.
 1.97296 +  **
 1.97297 +  ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
 1.97298 +  */
 1.97299 +  if( ALWAYS(pSubitem->pTab!=0) ){
 1.97300 +    Table *pTabToDel = pSubitem->pTab;
 1.97301 +    if( pTabToDel->nRef==1 ){
 1.97302 +      Parse *pToplevel = sqlite3ParseToplevel(pParse);
 1.97303 +      pTabToDel->pNextZombie = pToplevel->pZombieTab;
 1.97304 +      pToplevel->pZombieTab = pTabToDel;
 1.97305 +    }else{
 1.97306 +      pTabToDel->nRef--;
 1.97307 +    }
 1.97308 +    pSubitem->pTab = 0;
 1.97309 +  }
 1.97310 +
 1.97311 +  /* The following loop runs once for each term in a compound-subquery
 1.97312 +  ** flattening (as described above).  If we are doing a different kind
 1.97313 +  ** of flattening - a flattening other than a compound-subquery flattening -
 1.97314 +  ** then this loop only runs once.
 1.97315 +  **
 1.97316 +  ** This loop moves all of the FROM elements of the subquery into the
 1.97317 +  ** the FROM clause of the outer query.  Before doing this, remember
 1.97318 +  ** the cursor number for the original outer query FROM element in
 1.97319 +  ** iParent.  The iParent cursor will never be used.  Subsequent code
 1.97320 +  ** will scan expressions looking for iParent references and replace
 1.97321 +  ** those references with expressions that resolve to the subquery FROM
 1.97322 +  ** elements we are now copying in.
 1.97323 +  */
 1.97324 +  for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
 1.97325 +    int nSubSrc;
 1.97326 +    u8 jointype = 0;
 1.97327 +    pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
 1.97328 +    nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
 1.97329 +    pSrc = pParent->pSrc;     /* FROM clause of the outer query */
 1.97330 +
 1.97331 +    if( pSrc ){
 1.97332 +      assert( pParent==p );  /* First time through the loop */
 1.97333 +      jointype = pSubitem->jointype;
 1.97334 +    }else{
 1.97335 +      assert( pParent!=p );  /* 2nd and subsequent times through the loop */
 1.97336 +      pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
 1.97337 +      if( pSrc==0 ){
 1.97338 +        assert( db->mallocFailed );
 1.97339 +        break;
 1.97340 +      }
 1.97341 +    }
 1.97342 +
 1.97343 +    /* The subquery uses a single slot of the FROM clause of the outer
 1.97344 +    ** query.  If the subquery has more than one element in its FROM clause,
 1.97345 +    ** then expand the outer query to make space for it to hold all elements
 1.97346 +    ** of the subquery.
 1.97347 +    **
 1.97348 +    ** Example:
 1.97349 +    **
 1.97350 +    **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
 1.97351 +    **
 1.97352 +    ** The outer query has 3 slots in its FROM clause.  One slot of the
 1.97353 +    ** outer query (the middle slot) is used by the subquery.  The next
 1.97354 +    ** block of code will expand the out query to 4 slots.  The middle
 1.97355 +    ** slot is expanded to two slots in order to make space for the
 1.97356 +    ** two elements in the FROM clause of the subquery.
 1.97357 +    */
 1.97358 +    if( nSubSrc>1 ){
 1.97359 +      pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
 1.97360 +      if( db->mallocFailed ){
 1.97361 +        break;
 1.97362 +      }
 1.97363 +    }
 1.97364 +
 1.97365 +    /* Transfer the FROM clause terms from the subquery into the
 1.97366 +    ** outer query.
 1.97367 +    */
 1.97368 +    for(i=0; i<nSubSrc; i++){
 1.97369 +      sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
 1.97370 +      pSrc->a[i+iFrom] = pSubSrc->a[i];
 1.97371 +      memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
 1.97372 +    }
 1.97373 +    pSrc->a[iFrom].jointype = jointype;
 1.97374 +  
 1.97375 +    /* Now begin substituting subquery result set expressions for 
 1.97376 +    ** references to the iParent in the outer query.
 1.97377 +    ** 
 1.97378 +    ** Example:
 1.97379 +    **
 1.97380 +    **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
 1.97381 +    **   \                     \_____________ subquery __________/          /
 1.97382 +    **    \_____________________ outer query ______________________________/
 1.97383 +    **
 1.97384 +    ** We look at every expression in the outer query and every place we see
 1.97385 +    ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
 1.97386 +    */
 1.97387 +    pList = pParent->pEList;
 1.97388 +    for(i=0; i<pList->nExpr; i++){
 1.97389 +      if( pList->a[i].zName==0 ){
 1.97390 +        char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
 1.97391 +        sqlite3Dequote(zName);
 1.97392 +        pList->a[i].zName = zName;
 1.97393 +      }
 1.97394 +    }
 1.97395 +    substExprList(db, pParent->pEList, iParent, pSub->pEList);
 1.97396 +    if( isAgg ){
 1.97397 +      substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
 1.97398 +      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
 1.97399 +    }
 1.97400 +    if( pSub->pOrderBy ){
 1.97401 +      assert( pParent->pOrderBy==0 );
 1.97402 +      pParent->pOrderBy = pSub->pOrderBy;
 1.97403 +      pSub->pOrderBy = 0;
 1.97404 +    }else if( pParent->pOrderBy ){
 1.97405 +      substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
 1.97406 +    }
 1.97407 +    if( pSub->pWhere ){
 1.97408 +      pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
 1.97409 +    }else{
 1.97410 +      pWhere = 0;
 1.97411 +    }
 1.97412 +    if( subqueryIsAgg ){
 1.97413 +      assert( pParent->pHaving==0 );
 1.97414 +      pParent->pHaving = pParent->pWhere;
 1.97415 +      pParent->pWhere = pWhere;
 1.97416 +      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
 1.97417 +      pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
 1.97418 +                                  sqlite3ExprDup(db, pSub->pHaving, 0));
 1.97419 +      assert( pParent->pGroupBy==0 );
 1.97420 +      pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
 1.97421 +    }else{
 1.97422 +      pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
 1.97423 +      pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
 1.97424 +    }
 1.97425 +  
 1.97426 +    /* The flattened query is distinct if either the inner or the
 1.97427 +    ** outer query is distinct. 
 1.97428 +    */
 1.97429 +    pParent->selFlags |= pSub->selFlags & SF_Distinct;
 1.97430 +  
 1.97431 +    /*
 1.97432 +    ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
 1.97433 +    **
 1.97434 +    ** One is tempted to try to add a and b to combine the limits.  But this
 1.97435 +    ** does not work if either limit is negative.
 1.97436 +    */
 1.97437 +    if( pSub->pLimit ){
 1.97438 +      pParent->pLimit = pSub->pLimit;
 1.97439 +      pSub->pLimit = 0;
 1.97440 +    }
 1.97441 +  }
 1.97442 +
 1.97443 +  /* Finially, delete what is left of the subquery and return
 1.97444 +  ** success.
 1.97445 +  */
 1.97446 +  sqlite3SelectDelete(db, pSub1);
 1.97447 +
 1.97448 +  return 1;
 1.97449 +}
 1.97450 +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 1.97451 +
 1.97452 +/*
 1.97453 +** Analyze the SELECT statement passed as an argument to see if it
 1.97454 +** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if 
 1.97455 +** it is, or 0 otherwise. At present, a query is considered to be
 1.97456 +** a min()/max() query if:
 1.97457 +**
 1.97458 +**   1. There is a single object in the FROM clause.
 1.97459 +**
 1.97460 +**   2. There is a single expression in the result set, and it is
 1.97461 +**      either min(x) or max(x), where x is a column reference.
 1.97462 +*/
 1.97463 +static u8 minMaxQuery(Select *p){
 1.97464 +  Expr *pExpr;
 1.97465 +  ExprList *pEList = p->pEList;
 1.97466 +
 1.97467 +  if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
 1.97468 +  pExpr = pEList->a[0].pExpr;
 1.97469 +  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
 1.97470 +  if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
 1.97471 +  pEList = pExpr->x.pList;
 1.97472 +  if( pEList==0 || pEList->nExpr!=1 ) return 0;
 1.97473 +  if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
 1.97474 +  assert( !ExprHasProperty(pExpr, EP_IntValue) );
 1.97475 +  if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
 1.97476 +    return WHERE_ORDERBY_MIN;
 1.97477 +  }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
 1.97478 +    return WHERE_ORDERBY_MAX;
 1.97479 +  }
 1.97480 +  return WHERE_ORDERBY_NORMAL;
 1.97481 +}
 1.97482 +
 1.97483 +/*
 1.97484 +** The select statement passed as the first argument is an aggregate query.
 1.97485 +** The second argment is the associated aggregate-info object. This 
 1.97486 +** function tests if the SELECT is of the form:
 1.97487 +**
 1.97488 +**   SELECT count(*) FROM <tbl>
 1.97489 +**
 1.97490 +** where table is a database table, not a sub-select or view. If the query
 1.97491 +** does match this pattern, then a pointer to the Table object representing
 1.97492 +** <tbl> is returned. Otherwise, 0 is returned.
 1.97493 +*/
 1.97494 +static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
 1.97495 +  Table *pTab;
 1.97496 +  Expr *pExpr;
 1.97497 +
 1.97498 +  assert( !p->pGroupBy );
 1.97499 +
 1.97500 +  if( p->pWhere || p->pEList->nExpr!=1 
 1.97501 +   || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
 1.97502 +  ){
 1.97503 +    return 0;
 1.97504 +  }
 1.97505 +  pTab = p->pSrc->a[0].pTab;
 1.97506 +  pExpr = p->pEList->a[0].pExpr;
 1.97507 +  assert( pTab && !pTab->pSelect && pExpr );
 1.97508 +
 1.97509 +  if( IsVirtual(pTab) ) return 0;
 1.97510 +  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
 1.97511 +  if( NEVER(pAggInfo->nFunc==0) ) return 0;
 1.97512 +  if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
 1.97513 +  if( pExpr->flags&EP_Distinct ) return 0;
 1.97514 +
 1.97515 +  return pTab;
 1.97516 +}
 1.97517 +
 1.97518 +/*
 1.97519 +** If the source-list item passed as an argument was augmented with an
 1.97520 +** INDEXED BY clause, then try to locate the specified index. If there
 1.97521 +** was such a clause and the named index cannot be found, return 
 1.97522 +** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
 1.97523 +** pFrom->pIndex and return SQLITE_OK.
 1.97524 +*/
 1.97525 +SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
 1.97526 +  if( pFrom->pTab && pFrom->zIndex ){
 1.97527 +    Table *pTab = pFrom->pTab;
 1.97528 +    char *zIndex = pFrom->zIndex;
 1.97529 +    Index *pIdx;
 1.97530 +    for(pIdx=pTab->pIndex; 
 1.97531 +        pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
 1.97532 +        pIdx=pIdx->pNext
 1.97533 +    );
 1.97534 +    if( !pIdx ){
 1.97535 +      sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
 1.97536 +      pParse->checkSchema = 1;
 1.97537 +      return SQLITE_ERROR;
 1.97538 +    }
 1.97539 +    pFrom->pIndex = pIdx;
 1.97540 +  }
 1.97541 +  return SQLITE_OK;
 1.97542 +}
 1.97543 +
 1.97544 +/*
 1.97545 +** This routine is a Walker callback for "expanding" a SELECT statement.
 1.97546 +** "Expanding" means to do the following:
 1.97547 +**
 1.97548 +**    (1)  Make sure VDBE cursor numbers have been assigned to every
 1.97549 +**         element of the FROM clause.
 1.97550 +**
 1.97551 +**    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
 1.97552 +**         defines FROM clause.  When views appear in the FROM clause,
 1.97553 +**         fill pTabList->a[].pSelect with a copy of the SELECT statement
 1.97554 +**         that implements the view.  A copy is made of the view's SELECT
 1.97555 +**         statement so that we can freely modify or delete that statement
 1.97556 +**         without worrying about messing up the presistent representation
 1.97557 +**         of the view.
 1.97558 +**
 1.97559 +**    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
 1.97560 +**         on joins and the ON and USING clause of joins.
 1.97561 +**
 1.97562 +**    (4)  Scan the list of columns in the result set (pEList) looking
 1.97563 +**         for instances of the "*" operator or the TABLE.* operator.
 1.97564 +**         If found, expand each "*" to be every column in every table
 1.97565 +**         and TABLE.* to be every column in TABLE.
 1.97566 +**
 1.97567 +*/
 1.97568 +static int selectExpander(Walker *pWalker, Select *p){
 1.97569 +  Parse *pParse = pWalker->pParse;
 1.97570 +  int i, j, k;
 1.97571 +  SrcList *pTabList;
 1.97572 +  ExprList *pEList;
 1.97573 +  struct SrcList_item *pFrom;
 1.97574 +  sqlite3 *db = pParse->db;
 1.97575 +
 1.97576 +  if( db->mallocFailed  ){
 1.97577 +    return WRC_Abort;
 1.97578 +  }
 1.97579 +  if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
 1.97580 +    return WRC_Prune;
 1.97581 +  }
 1.97582 +  p->selFlags |= SF_Expanded;
 1.97583 +  pTabList = p->pSrc;
 1.97584 +  pEList = p->pEList;
 1.97585 +
 1.97586 +  /* Make sure cursor numbers have been assigned to all entries in
 1.97587 +  ** the FROM clause of the SELECT statement.
 1.97588 +  */
 1.97589 +  sqlite3SrcListAssignCursors(pParse, pTabList);
 1.97590 +
 1.97591 +  /* Look up every table named in the FROM clause of the select.  If
 1.97592 +  ** an entry of the FROM clause is a subquery instead of a table or view,
 1.97593 +  ** then create a transient table structure to describe the subquery.
 1.97594 +  */
 1.97595 +  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 1.97596 +    Table *pTab;
 1.97597 +    if( pFrom->pTab!=0 ){
 1.97598 +      /* This statement has already been prepared.  There is no need
 1.97599 +      ** to go further. */
 1.97600 +      assert( i==0 );
 1.97601 +      return WRC_Prune;
 1.97602 +    }
 1.97603 +    if( pFrom->zName==0 ){
 1.97604 +#ifndef SQLITE_OMIT_SUBQUERY
 1.97605 +      Select *pSel = pFrom->pSelect;
 1.97606 +      /* A sub-query in the FROM clause of a SELECT */
 1.97607 +      assert( pSel!=0 );
 1.97608 +      assert( pFrom->pTab==0 );
 1.97609 +      sqlite3WalkSelect(pWalker, pSel);
 1.97610 +      pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
 1.97611 +      if( pTab==0 ) return WRC_Abort;
 1.97612 +      pTab->nRef = 1;
 1.97613 +      pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
 1.97614 +      while( pSel->pPrior ){ pSel = pSel->pPrior; }
 1.97615 +      selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
 1.97616 +      pTab->iPKey = -1;
 1.97617 +      pTab->nRowEst = 1000000;
 1.97618 +      pTab->tabFlags |= TF_Ephemeral;
 1.97619 +#endif
 1.97620 +    }else{
 1.97621 +      /* An ordinary table or view name in the FROM clause */
 1.97622 +      assert( pFrom->pTab==0 );
 1.97623 +      pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
 1.97624 +      if( pTab==0 ) return WRC_Abort;
 1.97625 +      pTab->nRef++;
 1.97626 +#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
 1.97627 +      if( pTab->pSelect || IsVirtual(pTab) ){
 1.97628 +        /* We reach here if the named table is a really a view */
 1.97629 +        if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
 1.97630 +        assert( pFrom->pSelect==0 );
 1.97631 +        pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
 1.97632 +        sqlite3WalkSelect(pWalker, pFrom->pSelect);
 1.97633 +      }
 1.97634 +#endif
 1.97635 +    }
 1.97636 +
 1.97637 +    /* Locate the index named by the INDEXED BY clause, if any. */
 1.97638 +    if( sqlite3IndexedByLookup(pParse, pFrom) ){
 1.97639 +      return WRC_Abort;
 1.97640 +    }
 1.97641 +  }
 1.97642 +
 1.97643 +  /* Process NATURAL keywords, and ON and USING clauses of joins.
 1.97644 +  */
 1.97645 +  if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
 1.97646 +    return WRC_Abort;
 1.97647 +  }
 1.97648 +
 1.97649 +  /* For every "*" that occurs in the column list, insert the names of
 1.97650 +  ** all columns in all tables.  And for every TABLE.* insert the names
 1.97651 +  ** of all columns in TABLE.  The parser inserted a special expression
 1.97652 +  ** with the TK_ALL operator for each "*" that it found in the column list.
 1.97653 +  ** The following code just has to locate the TK_ALL expressions and expand
 1.97654 +  ** each one to the list of all columns in all tables.
 1.97655 +  **
 1.97656 +  ** The first loop just checks to see if there are any "*" operators
 1.97657 +  ** that need expanding.
 1.97658 +  */
 1.97659 +  for(k=0; k<pEList->nExpr; k++){
 1.97660 +    Expr *pE = pEList->a[k].pExpr;
 1.97661 +    if( pE->op==TK_ALL ) break;
 1.97662 +    assert( pE->op!=TK_DOT || pE->pRight!=0 );
 1.97663 +    assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
 1.97664 +    if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
 1.97665 +  }
 1.97666 +  if( k<pEList->nExpr ){
 1.97667 +    /*
 1.97668 +    ** If we get here it means the result set contains one or more "*"
 1.97669 +    ** operators that need to be expanded.  Loop through each expression
 1.97670 +    ** in the result set and expand them one by one.
 1.97671 +    */
 1.97672 +    struct ExprList_item *a = pEList->a;
 1.97673 +    ExprList *pNew = 0;
 1.97674 +    int flags = pParse->db->flags;
 1.97675 +    int longNames = (flags & SQLITE_FullColNames)!=0
 1.97676 +                      && (flags & SQLITE_ShortColNames)==0;
 1.97677 +
 1.97678 +    for(k=0; k<pEList->nExpr; k++){
 1.97679 +      Expr *pE = a[k].pExpr;
 1.97680 +      assert( pE->op!=TK_DOT || pE->pRight!=0 );
 1.97681 +      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
 1.97682 +        /* This particular expression does not need to be expanded.
 1.97683 +        */
 1.97684 +        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
 1.97685 +        if( pNew ){
 1.97686 +          pNew->a[pNew->nExpr-1].zName = a[k].zName;
 1.97687 +          pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
 1.97688 +          a[k].zName = 0;
 1.97689 +          a[k].zSpan = 0;
 1.97690 +        }
 1.97691 +        a[k].pExpr = 0;
 1.97692 +      }else{
 1.97693 +        /* This expression is a "*" or a "TABLE.*" and needs to be
 1.97694 +        ** expanded. */
 1.97695 +        int tableSeen = 0;      /* Set to 1 when TABLE matches */
 1.97696 +        char *zTName;            /* text of name of TABLE */
 1.97697 +        if( pE->op==TK_DOT ){
 1.97698 +          assert( pE->pLeft!=0 );
 1.97699 +          assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
 1.97700 +          zTName = pE->pLeft->u.zToken;
 1.97701 +        }else{
 1.97702 +          zTName = 0;
 1.97703 +        }
 1.97704 +        for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 1.97705 +          Table *pTab = pFrom->pTab;
 1.97706 +          char *zTabName = pFrom->zAlias;
 1.97707 +          if( zTabName==0 ){
 1.97708 +            zTabName = pTab->zName;
 1.97709 +          }
 1.97710 +          if( db->mallocFailed ) break;
 1.97711 +          if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
 1.97712 +            continue;
 1.97713 +          }
 1.97714 +          tableSeen = 1;
 1.97715 +          for(j=0; j<pTab->nCol; j++){
 1.97716 +            Expr *pExpr, *pRight;
 1.97717 +            char *zName = pTab->aCol[j].zName;
 1.97718 +            char *zColname;  /* The computed column name */
 1.97719 +            char *zToFree;   /* Malloced string that needs to be freed */
 1.97720 +            Token sColname;  /* Computed column name as a token */
 1.97721 +
 1.97722 +            /* If a column is marked as 'hidden' (currently only possible
 1.97723 +            ** for virtual tables), do not include it in the expanded
 1.97724 +            ** result-set list.
 1.97725 +            */
 1.97726 +            if( IsHiddenColumn(&pTab->aCol[j]) ){
 1.97727 +              assert(IsVirtual(pTab));
 1.97728 +              continue;
 1.97729 +            }
 1.97730 +
 1.97731 +            if( i>0 && zTName==0 ){
 1.97732 +              if( (pFrom->jointype & JT_NATURAL)!=0
 1.97733 +                && tableAndColumnIndex(pTabList, i, zName, 0, 0)
 1.97734 +              ){
 1.97735 +                /* In a NATURAL join, omit the join columns from the 
 1.97736 +                ** table to the right of the join */
 1.97737 +                continue;
 1.97738 +              }
 1.97739 +              if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
 1.97740 +                /* In a join with a USING clause, omit columns in the
 1.97741 +                ** using clause from the table on the right. */
 1.97742 +                continue;
 1.97743 +              }
 1.97744 +            }
 1.97745 +            pRight = sqlite3Expr(db, TK_ID, zName);
 1.97746 +            zColname = zName;
 1.97747 +            zToFree = 0;
 1.97748 +            if( longNames || pTabList->nSrc>1 ){
 1.97749 +              Expr *pLeft;
 1.97750 +              pLeft = sqlite3Expr(db, TK_ID, zTabName);
 1.97751 +              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
 1.97752 +              if( longNames ){
 1.97753 +                zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
 1.97754 +                zToFree = zColname;
 1.97755 +              }
 1.97756 +            }else{
 1.97757 +              pExpr = pRight;
 1.97758 +            }
 1.97759 +            pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
 1.97760 +            sColname.z = zColname;
 1.97761 +            sColname.n = sqlite3Strlen30(zColname);
 1.97762 +            sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
 1.97763 +            sqlite3DbFree(db, zToFree);
 1.97764 +          }
 1.97765 +        }
 1.97766 +        if( !tableSeen ){
 1.97767 +          if( zTName ){
 1.97768 +            sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
 1.97769 +          }else{
 1.97770 +            sqlite3ErrorMsg(pParse, "no tables specified");
 1.97771 +          }
 1.97772 +        }
 1.97773 +      }
 1.97774 +    }
 1.97775 +    sqlite3ExprListDelete(db, pEList);
 1.97776 +    p->pEList = pNew;
 1.97777 +  }
 1.97778 +#if SQLITE_MAX_COLUMN
 1.97779 +  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 1.97780 +    sqlite3ErrorMsg(pParse, "too many columns in result set");
 1.97781 +  }
 1.97782 +#endif
 1.97783 +  return WRC_Continue;
 1.97784 +}
 1.97785 +
 1.97786 +/*
 1.97787 +** No-op routine for the parse-tree walker.
 1.97788 +**
 1.97789 +** When this routine is the Walker.xExprCallback then expression trees
 1.97790 +** are walked without any actions being taken at each node.  Presumably,
 1.97791 +** when this routine is used for Walker.xExprCallback then 
 1.97792 +** Walker.xSelectCallback is set to do something useful for every 
 1.97793 +** subquery in the parser tree.
 1.97794 +*/
 1.97795 +static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
 1.97796 +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 1.97797 +  return WRC_Continue;
 1.97798 +}
 1.97799 +
 1.97800 +/*
 1.97801 +** This routine "expands" a SELECT statement and all of its subqueries.
 1.97802 +** For additional information on what it means to "expand" a SELECT
 1.97803 +** statement, see the comment on the selectExpand worker callback above.
 1.97804 +**
 1.97805 +** Expanding a SELECT statement is the first step in processing a
 1.97806 +** SELECT statement.  The SELECT statement must be expanded before
 1.97807 +** name resolution is performed.
 1.97808 +**
 1.97809 +** If anything goes wrong, an error message is written into pParse.
 1.97810 +** The calling function can detect the problem by looking at pParse->nErr
 1.97811 +** and/or pParse->db->mallocFailed.
 1.97812 +*/
 1.97813 +static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
 1.97814 +  Walker w;
 1.97815 +  w.xSelectCallback = selectExpander;
 1.97816 +  w.xExprCallback = exprWalkNoop;
 1.97817 +  w.pParse = pParse;
 1.97818 +  sqlite3WalkSelect(&w, pSelect);
 1.97819 +}
 1.97820 +
 1.97821 +
 1.97822 +#ifndef SQLITE_OMIT_SUBQUERY
 1.97823 +/*
 1.97824 +** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
 1.97825 +** interface.
 1.97826 +**
 1.97827 +** For each FROM-clause subquery, add Column.zType and Column.zColl
 1.97828 +** information to the Table structure that represents the result set
 1.97829 +** of that subquery.
 1.97830 +**
 1.97831 +** The Table structure that represents the result set was constructed
 1.97832 +** by selectExpander() but the type and collation information was omitted
 1.97833 +** at that point because identifiers had not yet been resolved.  This
 1.97834 +** routine is called after identifier resolution.
 1.97835 +*/
 1.97836 +static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
 1.97837 +  Parse *pParse;
 1.97838 +  int i;
 1.97839 +  SrcList *pTabList;
 1.97840 +  struct SrcList_item *pFrom;
 1.97841 +
 1.97842 +  assert( p->selFlags & SF_Resolved );
 1.97843 +  if( (p->selFlags & SF_HasTypeInfo)==0 ){
 1.97844 +    p->selFlags |= SF_HasTypeInfo;
 1.97845 +    pParse = pWalker->pParse;
 1.97846 +    pTabList = p->pSrc;
 1.97847 +    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 1.97848 +      Table *pTab = pFrom->pTab;
 1.97849 +      if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
 1.97850 +        /* A sub-query in the FROM clause of a SELECT */
 1.97851 +        Select *pSel = pFrom->pSelect;
 1.97852 +        assert( pSel );
 1.97853 +        while( pSel->pPrior ) pSel = pSel->pPrior;
 1.97854 +        selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
 1.97855 +      }
 1.97856 +    }
 1.97857 +  }
 1.97858 +  return WRC_Continue;
 1.97859 +}
 1.97860 +#endif
 1.97861 +
 1.97862 +
 1.97863 +/*
 1.97864 +** This routine adds datatype and collating sequence information to
 1.97865 +** the Table structures of all FROM-clause subqueries in a
 1.97866 +** SELECT statement.
 1.97867 +**
 1.97868 +** Use this routine after name resolution.
 1.97869 +*/
 1.97870 +static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
 1.97871 +#ifndef SQLITE_OMIT_SUBQUERY
 1.97872 +  Walker w;
 1.97873 +  w.xSelectCallback = selectAddSubqueryTypeInfo;
 1.97874 +  w.xExprCallback = exprWalkNoop;
 1.97875 +  w.pParse = pParse;
 1.97876 +  sqlite3WalkSelect(&w, pSelect);
 1.97877 +#endif
 1.97878 +}
 1.97879 +
 1.97880 +
 1.97881 +/*
 1.97882 +** This routine sets up a SELECT statement for processing.  The
 1.97883 +** following is accomplished:
 1.97884 +**
 1.97885 +**     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
 1.97886 +**     *  Ephemeral Table objects are created for all FROM-clause subqueries.
 1.97887 +**     *  ON and USING clauses are shifted into WHERE statements
 1.97888 +**     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
 1.97889 +**     *  Identifiers in expression are matched to tables.
 1.97890 +**
 1.97891 +** This routine acts recursively on all subqueries within the SELECT.
 1.97892 +*/
 1.97893 +SQLITE_PRIVATE void sqlite3SelectPrep(
 1.97894 +  Parse *pParse,         /* The parser context */
 1.97895 +  Select *p,             /* The SELECT statement being coded. */
 1.97896 +  NameContext *pOuterNC  /* Name context for container */
 1.97897 +){
 1.97898 +  sqlite3 *db;
 1.97899 +  if( NEVER(p==0) ) return;
 1.97900 +  db = pParse->db;
 1.97901 +  if( p->selFlags & SF_HasTypeInfo ) return;
 1.97902 +  sqlite3SelectExpand(pParse, p);
 1.97903 +  if( pParse->nErr || db->mallocFailed ) return;
 1.97904 +  sqlite3ResolveSelectNames(pParse, p, pOuterNC);
 1.97905 +  if( pParse->nErr || db->mallocFailed ) return;
 1.97906 +  sqlite3SelectAddTypeInfo(pParse, p);
 1.97907 +}
 1.97908 +
 1.97909 +/*
 1.97910 +** Reset the aggregate accumulator.
 1.97911 +**
 1.97912 +** The aggregate accumulator is a set of memory cells that hold
 1.97913 +** intermediate results while calculating an aggregate.  This
 1.97914 +** routine generates code that stores NULLs in all of those memory
 1.97915 +** cells.
 1.97916 +*/
 1.97917 +static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
 1.97918 +  Vdbe *v = pParse->pVdbe;
 1.97919 +  int i;
 1.97920 +  struct AggInfo_func *pFunc;
 1.97921 +  if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
 1.97922 +    return;
 1.97923 +  }
 1.97924 +  for(i=0; i<pAggInfo->nColumn; i++){
 1.97925 +    sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
 1.97926 +  }
 1.97927 +  for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
 1.97928 +    sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
 1.97929 +    if( pFunc->iDistinct>=0 ){
 1.97930 +      Expr *pE = pFunc->pExpr;
 1.97931 +      assert( !ExprHasProperty(pE, EP_xIsSelect) );
 1.97932 +      if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
 1.97933 +        sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
 1.97934 +           "argument");
 1.97935 +        pFunc->iDistinct = -1;
 1.97936 +      }else{
 1.97937 +        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
 1.97938 +        sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
 1.97939 +                          (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 1.97940 +      }
 1.97941 +    }
 1.97942 +  }
 1.97943 +}
 1.97944 +
 1.97945 +/*
 1.97946 +** Invoke the OP_AggFinalize opcode for every aggregate function
 1.97947 +** in the AggInfo structure.
 1.97948 +*/
 1.97949 +static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
 1.97950 +  Vdbe *v = pParse->pVdbe;
 1.97951 +  int i;
 1.97952 +  struct AggInfo_func *pF;
 1.97953 +  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
 1.97954 +    ExprList *pList = pF->pExpr->x.pList;
 1.97955 +    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
 1.97956 +    sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
 1.97957 +                      (void*)pF->pFunc, P4_FUNCDEF);
 1.97958 +  }
 1.97959 +}
 1.97960 +
 1.97961 +/*
 1.97962 +** Update the accumulator memory cells for an aggregate based on
 1.97963 +** the current cursor position.
 1.97964 +*/
 1.97965 +static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
 1.97966 +  Vdbe *v = pParse->pVdbe;
 1.97967 +  int i;
 1.97968 +  int regHit = 0;
 1.97969 +  int addrHitTest = 0;
 1.97970 +  struct AggInfo_func *pF;
 1.97971 +  struct AggInfo_col *pC;
 1.97972 +
 1.97973 +  pAggInfo->directMode = 1;
 1.97974 +  sqlite3ExprCacheClear(pParse);
 1.97975 +  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
 1.97976 +    int nArg;
 1.97977 +    int addrNext = 0;
 1.97978 +    int regAgg;
 1.97979 +    ExprList *pList = pF->pExpr->x.pList;
 1.97980 +    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
 1.97981 +    if( pList ){
 1.97982 +      nArg = pList->nExpr;
 1.97983 +      regAgg = sqlite3GetTempRange(pParse, nArg);
 1.97984 +      sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
 1.97985 +    }else{
 1.97986 +      nArg = 0;
 1.97987 +      regAgg = 0;
 1.97988 +    }
 1.97989 +    if( pF->iDistinct>=0 ){
 1.97990 +      addrNext = sqlite3VdbeMakeLabel(v);
 1.97991 +      assert( nArg==1 );
 1.97992 +      codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
 1.97993 +    }
 1.97994 +    if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 1.97995 +      CollSeq *pColl = 0;
 1.97996 +      struct ExprList_item *pItem;
 1.97997 +      int j;
 1.97998 +      assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
 1.97999 +      for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
 1.98000 +        pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
 1.98001 +      }
 1.98002 +      if( !pColl ){
 1.98003 +        pColl = pParse->db->pDfltColl;
 1.98004 +      }
 1.98005 +      if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
 1.98006 +      sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
 1.98007 +    }
 1.98008 +    sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
 1.98009 +                      (void*)pF->pFunc, P4_FUNCDEF);
 1.98010 +    sqlite3VdbeChangeP5(v, (u8)nArg);
 1.98011 +    sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
 1.98012 +    sqlite3ReleaseTempRange(pParse, regAgg, nArg);
 1.98013 +    if( addrNext ){
 1.98014 +      sqlite3VdbeResolveLabel(v, addrNext);
 1.98015 +      sqlite3ExprCacheClear(pParse);
 1.98016 +    }
 1.98017 +  }
 1.98018 +
 1.98019 +  /* Before populating the accumulator registers, clear the column cache.
 1.98020 +  ** Otherwise, if any of the required column values are already present 
 1.98021 +  ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
 1.98022 +  ** to pC->iMem. But by the time the value is used, the original register
 1.98023 +  ** may have been used, invalidating the underlying buffer holding the
 1.98024 +  ** text or blob value. See ticket [883034dcb5].
 1.98025 +  **
 1.98026 +  ** Another solution would be to change the OP_SCopy used to copy cached
 1.98027 +  ** values to an OP_Copy.
 1.98028 +  */
 1.98029 +  if( regHit ){
 1.98030 +    addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit);
 1.98031 +  }
 1.98032 +  sqlite3ExprCacheClear(pParse);
 1.98033 +  for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
 1.98034 +    sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
 1.98035 +  }
 1.98036 +  pAggInfo->directMode = 0;
 1.98037 +  sqlite3ExprCacheClear(pParse);
 1.98038 +  if( addrHitTest ){
 1.98039 +    sqlite3VdbeJumpHere(v, addrHitTest);
 1.98040 +  }
 1.98041 +}
 1.98042 +
 1.98043 +/*
 1.98044 +** Add a single OP_Explain instruction to the VDBE to explain a simple
 1.98045 +** count(*) query ("SELECT count(*) FROM pTab").
 1.98046 +*/
 1.98047 +#ifndef SQLITE_OMIT_EXPLAIN
 1.98048 +static void explainSimpleCount(
 1.98049 +  Parse *pParse,                  /* Parse context */
 1.98050 +  Table *pTab,                    /* Table being queried */
 1.98051 +  Index *pIdx                     /* Index used to optimize scan, or NULL */
 1.98052 +){
 1.98053 +  if( pParse->explain==2 ){
 1.98054 +    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
 1.98055 +        pTab->zName, 
 1.98056 +        pIdx ? "USING COVERING INDEX " : "",
 1.98057 +        pIdx ? pIdx->zName : "",
 1.98058 +        pTab->nRowEst
 1.98059 +    );
 1.98060 +    sqlite3VdbeAddOp4(
 1.98061 +        pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
 1.98062 +    );
 1.98063 +  }
 1.98064 +}
 1.98065 +#else
 1.98066 +# define explainSimpleCount(a,b,c)
 1.98067 +#endif
 1.98068 +
 1.98069 +/*
 1.98070 +** Generate code for the SELECT statement given in the p argument.  
 1.98071 +**
 1.98072 +** The results are distributed in various ways depending on the
 1.98073 +** contents of the SelectDest structure pointed to by argument pDest
 1.98074 +** as follows:
 1.98075 +**
 1.98076 +**     pDest->eDest    Result
 1.98077 +**     ------------    -------------------------------------------
 1.98078 +**     SRT_Output      Generate a row of output (using the OP_ResultRow
 1.98079 +**                     opcode) for each row in the result set.
 1.98080 +**
 1.98081 +**     SRT_Mem         Only valid if the result is a single column.
 1.98082 +**                     Store the first column of the first result row
 1.98083 +**                     in register pDest->iSDParm then abandon the rest
 1.98084 +**                     of the query.  This destination implies "LIMIT 1".
 1.98085 +**
 1.98086 +**     SRT_Set         The result must be a single column.  Store each
 1.98087 +**                     row of result as the key in table pDest->iSDParm. 
 1.98088 +**                     Apply the affinity pDest->affSdst before storing
 1.98089 +**                     results.  Used to implement "IN (SELECT ...)".
 1.98090 +**
 1.98091 +**     SRT_Union       Store results as a key in a temporary table 
 1.98092 +**                     identified by pDest->iSDParm.
 1.98093 +**
 1.98094 +**     SRT_Except      Remove results from the temporary table pDest->iSDParm.
 1.98095 +**
 1.98096 +**     SRT_Table       Store results in temporary table pDest->iSDParm.
 1.98097 +**                     This is like SRT_EphemTab except that the table
 1.98098 +**                     is assumed to already be open.
 1.98099 +**
 1.98100 +**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
 1.98101 +**                     the result there. The cursor is left open after
 1.98102 +**                     returning.  This is like SRT_Table except that
 1.98103 +**                     this destination uses OP_OpenEphemeral to create
 1.98104 +**                     the table first.
 1.98105 +**
 1.98106 +**     SRT_Coroutine   Generate a co-routine that returns a new row of
 1.98107 +**                     results each time it is invoked.  The entry point
 1.98108 +**                     of the co-routine is stored in register pDest->iSDParm.
 1.98109 +**
 1.98110 +**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
 1.98111 +**                     set is not empty.
 1.98112 +**
 1.98113 +**     SRT_Discard     Throw the results away.  This is used by SELECT
 1.98114 +**                     statements within triggers whose only purpose is
 1.98115 +**                     the side-effects of functions.
 1.98116 +**
 1.98117 +** This routine returns the number of errors.  If any errors are
 1.98118 +** encountered, then an appropriate error message is left in
 1.98119 +** pParse->zErrMsg.
 1.98120 +**
 1.98121 +** This routine does NOT free the Select structure passed in.  The
 1.98122 +** calling function needs to do that.
 1.98123 +*/
 1.98124 +SQLITE_PRIVATE int sqlite3Select(
 1.98125 +  Parse *pParse,         /* The parser context */
 1.98126 +  Select *p,             /* The SELECT statement being coded. */
 1.98127 +  SelectDest *pDest      /* What to do with the query results */
 1.98128 +){
 1.98129 +  int i, j;              /* Loop counters */
 1.98130 +  WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
 1.98131 +  Vdbe *v;               /* The virtual machine under construction */
 1.98132 +  int isAgg;             /* True for select lists like "count(*)" */
 1.98133 +  ExprList *pEList;      /* List of columns to extract. */
 1.98134 +  SrcList *pTabList;     /* List of tables to select from */
 1.98135 +  Expr *pWhere;          /* The WHERE clause.  May be NULL */
 1.98136 +  ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
 1.98137 +  ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
 1.98138 +  Expr *pHaving;         /* The HAVING clause.  May be NULL */
 1.98139 +  int rc = 1;            /* Value to return from this function */
 1.98140 +  int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
 1.98141 +  DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
 1.98142 +  AggInfo sAggInfo;      /* Information used by aggregate queries */
 1.98143 +  int iEnd;              /* Address of the end of the query */
 1.98144 +  sqlite3 *db;           /* The database connection */
 1.98145 +
 1.98146 +#ifndef SQLITE_OMIT_EXPLAIN
 1.98147 +  int iRestoreSelectId = pParse->iSelectId;
 1.98148 +  pParse->iSelectId = pParse->iNextSelectId++;
 1.98149 +#endif
 1.98150 +
 1.98151 +  db = pParse->db;
 1.98152 +  if( p==0 || db->mallocFailed || pParse->nErr ){
 1.98153 +    return 1;
 1.98154 +  }
 1.98155 +  if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
 1.98156 +  memset(&sAggInfo, 0, sizeof(sAggInfo));
 1.98157 +
 1.98158 +  if( IgnorableOrderby(pDest) ){
 1.98159 +    assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
 1.98160 +           pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
 1.98161 +    /* If ORDER BY makes no difference in the output then neither does
 1.98162 +    ** DISTINCT so it can be removed too. */
 1.98163 +    sqlite3ExprListDelete(db, p->pOrderBy);
 1.98164 +    p->pOrderBy = 0;
 1.98165 +    p->selFlags &= ~SF_Distinct;
 1.98166 +  }
 1.98167 +  sqlite3SelectPrep(pParse, p, 0);
 1.98168 +  pOrderBy = p->pOrderBy;
 1.98169 +  pTabList = p->pSrc;
 1.98170 +  pEList = p->pEList;
 1.98171 +  if( pParse->nErr || db->mallocFailed ){
 1.98172 +    goto select_end;
 1.98173 +  }
 1.98174 +  isAgg = (p->selFlags & SF_Aggregate)!=0;
 1.98175 +  assert( pEList!=0 );
 1.98176 +
 1.98177 +  /* Begin generating code.
 1.98178 +  */
 1.98179 +  v = sqlite3GetVdbe(pParse);
 1.98180 +  if( v==0 ) goto select_end;
 1.98181 +
 1.98182 +  /* If writing to memory or generating a set
 1.98183 +  ** only a single column may be output.
 1.98184 +  */
 1.98185 +#ifndef SQLITE_OMIT_SUBQUERY
 1.98186 +  if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
 1.98187 +    goto select_end;
 1.98188 +  }
 1.98189 +#endif
 1.98190 +
 1.98191 +  /* Generate code for all sub-queries in the FROM clause
 1.98192 +  */
 1.98193 +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 1.98194 +  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
 1.98195 +    struct SrcList_item *pItem = &pTabList->a[i];
 1.98196 +    SelectDest dest;
 1.98197 +    Select *pSub = pItem->pSelect;
 1.98198 +    int isAggSub;
 1.98199 +
 1.98200 +    if( pSub==0 ) continue;
 1.98201 +
 1.98202 +    /* Sometimes the code for a subquery will be generated more than
 1.98203 +    ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
 1.98204 +    ** for example.  In that case, do not regenerate the code to manifest
 1.98205 +    ** a view or the co-routine to implement a view.  The first instance
 1.98206 +    ** is sufficient, though the subroutine to manifest the view does need
 1.98207 +    ** to be invoked again. */
 1.98208 +    if( pItem->addrFillSub ){
 1.98209 +      if( pItem->viaCoroutine==0 ){
 1.98210 +        sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
 1.98211 +      }
 1.98212 +      continue;
 1.98213 +    }
 1.98214 +
 1.98215 +    /* Increment Parse.nHeight by the height of the largest expression
 1.98216 +    ** tree refered to by this, the parent select. The child select
 1.98217 +    ** may contain expression trees of at most
 1.98218 +    ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
 1.98219 +    ** more conservative than necessary, but much easier than enforcing
 1.98220 +    ** an exact limit.
 1.98221 +    */
 1.98222 +    pParse->nHeight += sqlite3SelectExprHeight(p);
 1.98223 +
 1.98224 +    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
 1.98225 +    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
 1.98226 +      /* This subquery can be absorbed into its parent. */
 1.98227 +      if( isAggSub ){
 1.98228 +        isAgg = 1;
 1.98229 +        p->selFlags |= SF_Aggregate;
 1.98230 +      }
 1.98231 +      i = -1;
 1.98232 +    }else if( pTabList->nSrc==1 && (p->selFlags & SF_Materialize)==0
 1.98233 +      && OptimizationEnabled(db, SQLITE_SubqCoroutine)
 1.98234 +    ){
 1.98235 +      /* Implement a co-routine that will return a single row of the result
 1.98236 +      ** set on each invocation.
 1.98237 +      */
 1.98238 +      int addrTop;
 1.98239 +      int addrEof;
 1.98240 +      pItem->regReturn = ++pParse->nMem;
 1.98241 +      addrEof = ++pParse->nMem;
 1.98242 +      /* Before coding the OP_Goto to jump to the start of the main routine,
 1.98243 +      ** ensure that the jump to the verify-schema routine has already
 1.98244 +      ** been coded. Otherwise, the verify-schema would likely be coded as 
 1.98245 +      ** part of the co-routine. If the main routine then accessed the 
 1.98246 +      ** database before invoking the co-routine for the first time (for 
 1.98247 +      ** example to initialize a LIMIT register from a sub-select), it would 
 1.98248 +      ** be doing so without having verified the schema version and obtained 
 1.98249 +      ** the required db locks. See ticket d6b36be38.  */
 1.98250 +      sqlite3CodeVerifySchema(pParse, -1);
 1.98251 +      sqlite3VdbeAddOp0(v, OP_Goto);
 1.98252 +      addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
 1.98253 +      sqlite3VdbeChangeP5(v, 1);
 1.98254 +      VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
 1.98255 +      pItem->addrFillSub = addrTop;
 1.98256 +      sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
 1.98257 +      sqlite3VdbeChangeP5(v, 1);
 1.98258 +      sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
 1.98259 +      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 1.98260 +      sqlite3Select(pParse, pSub, &dest);
 1.98261 +      pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
 1.98262 +      pItem->viaCoroutine = 1;
 1.98263 +      sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
 1.98264 +      sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
 1.98265 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
 1.98266 +      sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
 1.98267 +      VdbeComment((v, "end %s", pItem->pTab->zName));
 1.98268 +      sqlite3VdbeJumpHere(v, addrTop-1);
 1.98269 +      sqlite3ClearTempRegCache(pParse);
 1.98270 +    }else{
 1.98271 +      /* Generate a subroutine that will fill an ephemeral table with
 1.98272 +      ** the content of this subquery.  pItem->addrFillSub will point
 1.98273 +      ** to the address of the generated subroutine.  pItem->regReturn
 1.98274 +      ** is a register allocated to hold the subroutine return address
 1.98275 +      */
 1.98276 +      int topAddr;
 1.98277 +      int onceAddr = 0;
 1.98278 +      int retAddr;
 1.98279 +      assert( pItem->addrFillSub==0 );
 1.98280 +      pItem->regReturn = ++pParse->nMem;
 1.98281 +      topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
 1.98282 +      pItem->addrFillSub = topAddr+1;
 1.98283 +      VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
 1.98284 +      if( pItem->isCorrelated==0 ){
 1.98285 +        /* If the subquery is no correlated and if we are not inside of
 1.98286 +        ** a trigger, then we only need to compute the value of the subquery
 1.98287 +        ** once. */
 1.98288 +        onceAddr = sqlite3CodeOnce(pParse);
 1.98289 +      }
 1.98290 +      sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
 1.98291 +      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 1.98292 +      sqlite3Select(pParse, pSub, &dest);
 1.98293 +      pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
 1.98294 +      if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
 1.98295 +      retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
 1.98296 +      VdbeComment((v, "end %s", pItem->pTab->zName));
 1.98297 +      sqlite3VdbeChangeP1(v, topAddr, retAddr);
 1.98298 +      sqlite3ClearTempRegCache(pParse);
 1.98299 +    }
 1.98300 +    if( /*pParse->nErr ||*/ db->mallocFailed ){
 1.98301 +      goto select_end;
 1.98302 +    }
 1.98303 +    pParse->nHeight -= sqlite3SelectExprHeight(p);
 1.98304 +    pTabList = p->pSrc;
 1.98305 +    if( !IgnorableOrderby(pDest) ){
 1.98306 +      pOrderBy = p->pOrderBy;
 1.98307 +    }
 1.98308 +  }
 1.98309 +  pEList = p->pEList;
 1.98310 +#endif
 1.98311 +  pWhere = p->pWhere;
 1.98312 +  pGroupBy = p->pGroupBy;
 1.98313 +  pHaving = p->pHaving;
 1.98314 +  sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
 1.98315 +
 1.98316 +#ifndef SQLITE_OMIT_COMPOUND_SELECT
 1.98317 +  /* If there is are a sequence of queries, do the earlier ones first.
 1.98318 +  */
 1.98319 +  if( p->pPrior ){
 1.98320 +    if( p->pRightmost==0 ){
 1.98321 +      Select *pLoop, *pRight = 0;
 1.98322 +      int cnt = 0;
 1.98323 +      int mxSelect;
 1.98324 +      for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
 1.98325 +        pLoop->pRightmost = p;
 1.98326 +        pLoop->pNext = pRight;
 1.98327 +        pRight = pLoop;
 1.98328 +      }
 1.98329 +      mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
 1.98330 +      if( mxSelect && cnt>mxSelect ){
 1.98331 +        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
 1.98332 +        goto select_end;
 1.98333 +      }
 1.98334 +    }
 1.98335 +    rc = multiSelect(pParse, p, pDest);
 1.98336 +    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
 1.98337 +    return rc;
 1.98338 +  }
 1.98339 +#endif
 1.98340 +
 1.98341 +  /* If there is both a GROUP BY and an ORDER BY clause and they are
 1.98342 +  ** identical, then disable the ORDER BY clause since the GROUP BY
 1.98343 +  ** will cause elements to come out in the correct order.  This is
 1.98344 +  ** an optimization - the correct answer should result regardless.
 1.98345 +  ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
 1.98346 +  ** to disable this optimization for testing purposes.
 1.98347 +  */
 1.98348 +  if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
 1.98349 +         && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
 1.98350 +    pOrderBy = 0;
 1.98351 +  }
 1.98352 +
 1.98353 +  /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
 1.98354 +  ** if the select-list is the same as the ORDER BY list, then this query
 1.98355 +  ** can be rewritten as a GROUP BY. In other words, this:
 1.98356 +  **
 1.98357 +  **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
 1.98358 +  **
 1.98359 +  ** is transformed to:
 1.98360 +  **
 1.98361 +  **     SELECT xyz FROM ... GROUP BY xyz
 1.98362 +  **
 1.98363 +  ** The second form is preferred as a single index (or temp-table) may be 
 1.98364 +  ** used for both the ORDER BY and DISTINCT processing. As originally 
 1.98365 +  ** written the query must use a temp-table for at least one of the ORDER 
 1.98366 +  ** BY and DISTINCT, and an index or separate temp-table for the other.
 1.98367 +  */
 1.98368 +  if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
 1.98369 +   && sqlite3ExprListCompare(pOrderBy, p->pEList)==0
 1.98370 +  ){
 1.98371 +    p->selFlags &= ~SF_Distinct;
 1.98372 +    p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
 1.98373 +    pGroupBy = p->pGroupBy;
 1.98374 +    pOrderBy = 0;
 1.98375 +    /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
 1.98376 +    ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
 1.98377 +    ** original setting of the SF_Distinct flag, not the current setting */
 1.98378 +    assert( sDistinct.isTnct );
 1.98379 +  }
 1.98380 +
 1.98381 +  /* If there is an ORDER BY clause, then this sorting
 1.98382 +  ** index might end up being unused if the data can be 
 1.98383 +  ** extracted in pre-sorted order.  If that is the case, then the
 1.98384 +  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
 1.98385 +  ** we figure out that the sorting index is not needed.  The addrSortIndex
 1.98386 +  ** variable is used to facilitate that change.
 1.98387 +  */
 1.98388 +  if( pOrderBy ){
 1.98389 +    KeyInfo *pKeyInfo;
 1.98390 +    pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
 1.98391 +    pOrderBy->iECursor = pParse->nTab++;
 1.98392 +    p->addrOpenEphm[2] = addrSortIndex =
 1.98393 +      sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
 1.98394 +                           pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
 1.98395 +                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 1.98396 +  }else{
 1.98397 +    addrSortIndex = -1;
 1.98398 +  }
 1.98399 +
 1.98400 +  /* If the output is destined for a temporary table, open that table.
 1.98401 +  */
 1.98402 +  if( pDest->eDest==SRT_EphemTab ){
 1.98403 +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
 1.98404 +  }
 1.98405 +
 1.98406 +  /* Set the limiter.
 1.98407 +  */
 1.98408 +  iEnd = sqlite3VdbeMakeLabel(v);
 1.98409 +  p->nSelectRow = (double)LARGEST_INT64;
 1.98410 +  computeLimitRegisters(pParse, p, iEnd);
 1.98411 +  if( p->iLimit==0 && addrSortIndex>=0 ){
 1.98412 +    sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
 1.98413 +    p->selFlags |= SF_UseSorter;
 1.98414 +  }
 1.98415 +
 1.98416 +  /* Open a virtual index to use for the distinct set.
 1.98417 +  */
 1.98418 +  if( p->selFlags & SF_Distinct ){
 1.98419 +    sDistinct.tabTnct = pParse->nTab++;
 1.98420 +    sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
 1.98421 +                                sDistinct.tabTnct, 0, 0,
 1.98422 +                                (char*)keyInfoFromExprList(pParse, p->pEList),
 1.98423 +                                P4_KEYINFO_HANDOFF);
 1.98424 +    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 1.98425 +    sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
 1.98426 +  }else{
 1.98427 +    sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
 1.98428 +  }
 1.98429 +
 1.98430 +  if( !isAgg && pGroupBy==0 ){
 1.98431 +    /* No aggregate functions and no GROUP BY clause */
 1.98432 +    ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
 1.98433 +
 1.98434 +    /* Begin the database scan. */
 1.98435 +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
 1.98436 +    if( pWInfo==0 ) goto select_end;
 1.98437 +    if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
 1.98438 +    if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
 1.98439 +    if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
 1.98440 +
 1.98441 +    /* If sorting index that was created by a prior OP_OpenEphemeral 
 1.98442 +    ** instruction ended up not being needed, then change the OP_OpenEphemeral
 1.98443 +    ** into an OP_Noop.
 1.98444 +    */
 1.98445 +    if( addrSortIndex>=0 && pOrderBy==0 ){
 1.98446 +      sqlite3VdbeChangeToNoop(v, addrSortIndex);
 1.98447 +      p->addrOpenEphm[2] = -1;
 1.98448 +    }
 1.98449 +
 1.98450 +    /* Use the standard inner loop. */
 1.98451 +    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
 1.98452 +                    pWInfo->iContinue, pWInfo->iBreak);
 1.98453 +
 1.98454 +    /* End the database scan loop.
 1.98455 +    */
 1.98456 +    sqlite3WhereEnd(pWInfo);
 1.98457 +  }else{
 1.98458 +    /* This case when there exist aggregate functions or a GROUP BY clause
 1.98459 +    ** or both */
 1.98460 +    NameContext sNC;    /* Name context for processing aggregate information */
 1.98461 +    int iAMem;          /* First Mem address for storing current GROUP BY */
 1.98462 +    int iBMem;          /* First Mem address for previous GROUP BY */
 1.98463 +    int iUseFlag;       /* Mem address holding flag indicating that at least
 1.98464 +                        ** one row of the input to the aggregator has been
 1.98465 +                        ** processed */
 1.98466 +    int iAbortFlag;     /* Mem address which causes query abort if positive */
 1.98467 +    int groupBySort;    /* Rows come from source in GROUP BY order */
 1.98468 +    int addrEnd;        /* End of processing for this SELECT */
 1.98469 +    int sortPTab = 0;   /* Pseudotable used to decode sorting results */
 1.98470 +    int sortOut = 0;    /* Output register from the sorter */
 1.98471 +
 1.98472 +    /* Remove any and all aliases between the result set and the
 1.98473 +    ** GROUP BY clause.
 1.98474 +    */
 1.98475 +    if( pGroupBy ){
 1.98476 +      int k;                        /* Loop counter */
 1.98477 +      struct ExprList_item *pItem;  /* For looping over expression in a list */
 1.98478 +
 1.98479 +      for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
 1.98480 +        pItem->iAlias = 0;
 1.98481 +      }
 1.98482 +      for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
 1.98483 +        pItem->iAlias = 0;
 1.98484 +      }
 1.98485 +      if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
 1.98486 +    }else{
 1.98487 +      p->nSelectRow = (double)1;
 1.98488 +    }
 1.98489 +
 1.98490 + 
 1.98491 +    /* Create a label to jump to when we want to abort the query */
 1.98492 +    addrEnd = sqlite3VdbeMakeLabel(v);
 1.98493 +
 1.98494 +    /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
 1.98495 +    ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
 1.98496 +    ** SELECT statement.
 1.98497 +    */
 1.98498 +    memset(&sNC, 0, sizeof(sNC));
 1.98499 +    sNC.pParse = pParse;
 1.98500 +    sNC.pSrcList = pTabList;
 1.98501 +    sNC.pAggInfo = &sAggInfo;
 1.98502 +    sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
 1.98503 +    sAggInfo.pGroupBy = pGroupBy;
 1.98504 +    sqlite3ExprAnalyzeAggList(&sNC, pEList);
 1.98505 +    sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
 1.98506 +    if( pHaving ){
 1.98507 +      sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
 1.98508 +    }
 1.98509 +    sAggInfo.nAccumulator = sAggInfo.nColumn;
 1.98510 +    for(i=0; i<sAggInfo.nFunc; i++){
 1.98511 +      assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
 1.98512 +      sNC.ncFlags |= NC_InAggFunc;
 1.98513 +      sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
 1.98514 +      sNC.ncFlags &= ~NC_InAggFunc;
 1.98515 +    }
 1.98516 +    if( db->mallocFailed ) goto select_end;
 1.98517 +
 1.98518 +    /* Processing for aggregates with GROUP BY is very different and
 1.98519 +    ** much more complex than aggregates without a GROUP BY.
 1.98520 +    */
 1.98521 +    if( pGroupBy ){
 1.98522 +      KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
 1.98523 +      int j1;             /* A-vs-B comparision jump */
 1.98524 +      int addrOutputRow;  /* Start of subroutine that outputs a result row */
 1.98525 +      int regOutputRow;   /* Return address register for output subroutine */
 1.98526 +      int addrSetAbort;   /* Set the abort flag and return */
 1.98527 +      int addrTopOfLoop;  /* Top of the input loop */
 1.98528 +      int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
 1.98529 +      int addrReset;      /* Subroutine for resetting the accumulator */
 1.98530 +      int regReset;       /* Return address register for reset subroutine */
 1.98531 +
 1.98532 +      /* If there is a GROUP BY clause we might need a sorting index to
 1.98533 +      ** implement it.  Allocate that sorting index now.  If it turns out
 1.98534 +      ** that we do not need it after all, the OP_SorterOpen instruction
 1.98535 +      ** will be converted into a Noop.  
 1.98536 +      */
 1.98537 +      sAggInfo.sortingIdx = pParse->nTab++;
 1.98538 +      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
 1.98539 +      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
 1.98540 +          sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
 1.98541 +          0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 1.98542 +
 1.98543 +      /* Initialize memory locations used by GROUP BY aggregate processing
 1.98544 +      */
 1.98545 +      iUseFlag = ++pParse->nMem;
 1.98546 +      iAbortFlag = ++pParse->nMem;
 1.98547 +      regOutputRow = ++pParse->nMem;
 1.98548 +      addrOutputRow = sqlite3VdbeMakeLabel(v);
 1.98549 +      regReset = ++pParse->nMem;
 1.98550 +      addrReset = sqlite3VdbeMakeLabel(v);
 1.98551 +      iAMem = pParse->nMem + 1;
 1.98552 +      pParse->nMem += pGroupBy->nExpr;
 1.98553 +      iBMem = pParse->nMem + 1;
 1.98554 +      pParse->nMem += pGroupBy->nExpr;
 1.98555 +      sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
 1.98556 +      VdbeComment((v, "clear abort flag"));
 1.98557 +      sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
 1.98558 +      VdbeComment((v, "indicate accumulator empty"));
 1.98559 +      sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
 1.98560 +
 1.98561 +      /* Begin a loop that will extract all source rows in GROUP BY order.
 1.98562 +      ** This might involve two separate loops with an OP_Sort in between, or
 1.98563 +      ** it might be a single loop that uses an index to extract information
 1.98564 +      ** in the right order to begin with.
 1.98565 +      */
 1.98566 +      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
 1.98567 +      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
 1.98568 +      if( pWInfo==0 ) goto select_end;
 1.98569 +      if( pWInfo->nOBSat==pGroupBy->nExpr ){
 1.98570 +        /* The optimizer is able to deliver rows in group by order so
 1.98571 +        ** we do not have to sort.  The OP_OpenEphemeral table will be
 1.98572 +        ** cancelled later because we still need to use the pKeyInfo
 1.98573 +        */
 1.98574 +        groupBySort = 0;
 1.98575 +      }else{
 1.98576 +        /* Rows are coming out in undetermined order.  We have to push
 1.98577 +        ** each row into a sorting index, terminate the first loop,
 1.98578 +        ** then loop over the sorting index in order to get the output
 1.98579 +        ** in sorted order
 1.98580 +        */
 1.98581 +        int regBase;
 1.98582 +        int regRecord;
 1.98583 +        int nCol;
 1.98584 +        int nGroupBy;
 1.98585 +
 1.98586 +        explainTempTable(pParse, 
 1.98587 +            (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
 1.98588 +                    "DISTINCT" : "GROUP BY");
 1.98589 +
 1.98590 +        groupBySort = 1;
 1.98591 +        nGroupBy = pGroupBy->nExpr;
 1.98592 +        nCol = nGroupBy + 1;
 1.98593 +        j = nGroupBy+1;
 1.98594 +        for(i=0; i<sAggInfo.nColumn; i++){
 1.98595 +          if( sAggInfo.aCol[i].iSorterColumn>=j ){
 1.98596 +            nCol++;
 1.98597 +            j++;
 1.98598 +          }
 1.98599 +        }
 1.98600 +        regBase = sqlite3GetTempRange(pParse, nCol);
 1.98601 +        sqlite3ExprCacheClear(pParse);
 1.98602 +        sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
 1.98603 +        sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
 1.98604 +        j = nGroupBy+1;
 1.98605 +        for(i=0; i<sAggInfo.nColumn; i++){
 1.98606 +          struct AggInfo_col *pCol = &sAggInfo.aCol[i];
 1.98607 +          if( pCol->iSorterColumn>=j ){
 1.98608 +            int r1 = j + regBase;
 1.98609 +            int r2;
 1.98610 +
 1.98611 +            r2 = sqlite3ExprCodeGetColumn(pParse, 
 1.98612 +                               pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
 1.98613 +            if( r1!=r2 ){
 1.98614 +              sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
 1.98615 +            }
 1.98616 +            j++;
 1.98617 +          }
 1.98618 +        }
 1.98619 +        regRecord = sqlite3GetTempReg(pParse);
 1.98620 +        sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
 1.98621 +        sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
 1.98622 +        sqlite3ReleaseTempReg(pParse, regRecord);
 1.98623 +        sqlite3ReleaseTempRange(pParse, regBase, nCol);
 1.98624 +        sqlite3WhereEnd(pWInfo);
 1.98625 +        sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
 1.98626 +        sortOut = sqlite3GetTempReg(pParse);
 1.98627 +        sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
 1.98628 +        sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
 1.98629 +        VdbeComment((v, "GROUP BY sort"));
 1.98630 +        sAggInfo.useSortingIdx = 1;
 1.98631 +        sqlite3ExprCacheClear(pParse);
 1.98632 +      }
 1.98633 +
 1.98634 +      /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
 1.98635 +      ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
 1.98636 +      ** Then compare the current GROUP BY terms against the GROUP BY terms
 1.98637 +      ** from the previous row currently stored in a0, a1, a2...
 1.98638 +      */
 1.98639 +      addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
 1.98640 +      sqlite3ExprCacheClear(pParse);
 1.98641 +      if( groupBySort ){
 1.98642 +        sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
 1.98643 +      }
 1.98644 +      for(j=0; j<pGroupBy->nExpr; j++){
 1.98645 +        if( groupBySort ){
 1.98646 +          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
 1.98647 +          if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 1.98648 +        }else{
 1.98649 +          sAggInfo.directMode = 1;
 1.98650 +          sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
 1.98651 +        }
 1.98652 +      }
 1.98653 +      sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
 1.98654 +                          (char*)pKeyInfo, P4_KEYINFO);
 1.98655 +      j1 = sqlite3VdbeCurrentAddr(v);
 1.98656 +      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
 1.98657 +
 1.98658 +      /* Generate code that runs whenever the GROUP BY changes.
 1.98659 +      ** Changes in the GROUP BY are detected by the previous code
 1.98660 +      ** block.  If there were no changes, this block is skipped.
 1.98661 +      **
 1.98662 +      ** This code copies current group by terms in b0,b1,b2,...
 1.98663 +      ** over to a0,a1,a2.  It then calls the output subroutine
 1.98664 +      ** and resets the aggregate accumulator registers in preparation
 1.98665 +      ** for the next GROUP BY batch.
 1.98666 +      */
 1.98667 +      sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
 1.98668 +      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
 1.98669 +      VdbeComment((v, "output one row"));
 1.98670 +      sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
 1.98671 +      VdbeComment((v, "check abort flag"));
 1.98672 +      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
 1.98673 +      VdbeComment((v, "reset accumulator"));
 1.98674 +
 1.98675 +      /* Update the aggregate accumulators based on the content of
 1.98676 +      ** the current row
 1.98677 +      */
 1.98678 +      sqlite3VdbeJumpHere(v, j1);
 1.98679 +      updateAccumulator(pParse, &sAggInfo);
 1.98680 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
 1.98681 +      VdbeComment((v, "indicate data in accumulator"));
 1.98682 +
 1.98683 +      /* End of the loop
 1.98684 +      */
 1.98685 +      if( groupBySort ){
 1.98686 +        sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
 1.98687 +      }else{
 1.98688 +        sqlite3WhereEnd(pWInfo);
 1.98689 +        sqlite3VdbeChangeToNoop(v, addrSortingIdx);
 1.98690 +      }
 1.98691 +
 1.98692 +      /* Output the final row of result
 1.98693 +      */
 1.98694 +      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
 1.98695 +      VdbeComment((v, "output final row"));
 1.98696 +
 1.98697 +      /* Jump over the subroutines
 1.98698 +      */
 1.98699 +      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
 1.98700 +
 1.98701 +      /* Generate a subroutine that outputs a single row of the result
 1.98702 +      ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
 1.98703 +      ** is less than or equal to zero, the subroutine is a no-op.  If
 1.98704 +      ** the processing calls for the query to abort, this subroutine
 1.98705 +      ** increments the iAbortFlag memory location before returning in
 1.98706 +      ** order to signal the caller to abort.
 1.98707 +      */
 1.98708 +      addrSetAbort = sqlite3VdbeCurrentAddr(v);
 1.98709 +      sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
 1.98710 +      VdbeComment((v, "set abort flag"));
 1.98711 +      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 1.98712 +      sqlite3VdbeResolveLabel(v, addrOutputRow);
 1.98713 +      addrOutputRow = sqlite3VdbeCurrentAddr(v);
 1.98714 +      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
 1.98715 +      VdbeComment((v, "Groupby result generator entry point"));
 1.98716 +      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 1.98717 +      finalizeAggFunctions(pParse, &sAggInfo);
 1.98718 +      sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
 1.98719 +      selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
 1.98720 +                      &sDistinct, pDest,
 1.98721 +                      addrOutputRow+1, addrSetAbort);
 1.98722 +      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 1.98723 +      VdbeComment((v, "end groupby result generator"));
 1.98724 +
 1.98725 +      /* Generate a subroutine that will reset the group-by accumulator
 1.98726 +      */
 1.98727 +      sqlite3VdbeResolveLabel(v, addrReset);
 1.98728 +      resetAccumulator(pParse, &sAggInfo);
 1.98729 +      sqlite3VdbeAddOp1(v, OP_Return, regReset);
 1.98730 +     
 1.98731 +    } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
 1.98732 +    else {
 1.98733 +      ExprList *pDel = 0;
 1.98734 +#ifndef SQLITE_OMIT_BTREECOUNT
 1.98735 +      Table *pTab;
 1.98736 +      if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
 1.98737 +        /* If isSimpleCount() returns a pointer to a Table structure, then
 1.98738 +        ** the SQL statement is of the form:
 1.98739 +        **
 1.98740 +        **   SELECT count(*) FROM <tbl>
 1.98741 +        **
 1.98742 +        ** where the Table structure returned represents table <tbl>.
 1.98743 +        **
 1.98744 +        ** This statement is so common that it is optimized specially. The
 1.98745 +        ** OP_Count instruction is executed either on the intkey table that
 1.98746 +        ** contains the data for table <tbl> or on one of its indexes. It
 1.98747 +        ** is better to execute the op on an index, as indexes are almost
 1.98748 +        ** always spread across less pages than their corresponding tables.
 1.98749 +        */
 1.98750 +        const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 1.98751 +        const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
 1.98752 +        Index *pIdx;                         /* Iterator variable */
 1.98753 +        KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
 1.98754 +        Index *pBest = 0;                    /* Best index found so far */
 1.98755 +        int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
 1.98756 +
 1.98757 +        sqlite3CodeVerifySchema(pParse, iDb);
 1.98758 +        sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 1.98759 +
 1.98760 +        /* Search for the index that has the least amount of columns. If
 1.98761 +        ** there is such an index, and it has less columns than the table
 1.98762 +        ** does, then we can assume that it consumes less space on disk and
 1.98763 +        ** will therefore be cheaper to scan to determine the query result.
 1.98764 +        ** In this case set iRoot to the root page number of the index b-tree
 1.98765 +        ** and pKeyInfo to the KeyInfo structure required to navigate the
 1.98766 +        ** index.
 1.98767 +        **
 1.98768 +        ** (2011-04-15) Do not do a full scan of an unordered index.
 1.98769 +        **
 1.98770 +        ** In practice the KeyInfo structure will not be used. It is only 
 1.98771 +        ** passed to keep OP_OpenRead happy.
 1.98772 +        */
 1.98773 +        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 1.98774 +          if( pIdx->bUnordered==0 && (!pBest || pIdx->nColumn<pBest->nColumn) ){
 1.98775 +            pBest = pIdx;
 1.98776 +          }
 1.98777 +        }
 1.98778 +        if( pBest && pBest->nColumn<pTab->nCol ){
 1.98779 +          iRoot = pBest->tnum;
 1.98780 +          pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
 1.98781 +        }
 1.98782 +
 1.98783 +        /* Open a read-only cursor, execute the OP_Count, close the cursor. */
 1.98784 +        sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
 1.98785 +        if( pKeyInfo ){
 1.98786 +          sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
 1.98787 +        }
 1.98788 +        sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
 1.98789 +        sqlite3VdbeAddOp1(v, OP_Close, iCsr);
 1.98790 +        explainSimpleCount(pParse, pTab, pBest);
 1.98791 +      }else
 1.98792 +#endif /* SQLITE_OMIT_BTREECOUNT */
 1.98793 +      {
 1.98794 +        /* Check if the query is of one of the following forms:
 1.98795 +        **
 1.98796 +        **   SELECT min(x) FROM ...
 1.98797 +        **   SELECT max(x) FROM ...
 1.98798 +        **
 1.98799 +        ** If it is, then ask the code in where.c to attempt to sort results
 1.98800 +        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
 1.98801 +        ** If where.c is able to produce results sorted in this order, then
 1.98802 +        ** add vdbe code to break out of the processing loop after the 
 1.98803 +        ** first iteration (since the first iteration of the loop is 
 1.98804 +        ** guaranteed to operate on the row with the minimum or maximum 
 1.98805 +        ** value of x, the only row required).
 1.98806 +        **
 1.98807 +        ** A special flag must be passed to sqlite3WhereBegin() to slightly
 1.98808 +        ** modify behaviour as follows:
 1.98809 +        **
 1.98810 +        **   + If the query is a "SELECT min(x)", then the loop coded by
 1.98811 +        **     where.c should not iterate over any values with a NULL value
 1.98812 +        **     for x.
 1.98813 +        **
 1.98814 +        **   + The optimizer code in where.c (the thing that decides which
 1.98815 +        **     index or indices to use) should place a different priority on 
 1.98816 +        **     satisfying the 'ORDER BY' clause than it does in other cases.
 1.98817 +        **     Refer to code and comments in where.c for details.
 1.98818 +        */
 1.98819 +        ExprList *pMinMax = 0;
 1.98820 +        u8 flag = minMaxQuery(p);
 1.98821 +        if( flag ){
 1.98822 +          assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
 1.98823 +          assert( p->pEList->a[0].pExpr->x.pList->nExpr==1 );
 1.98824 +          pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
 1.98825 +          pDel = pMinMax;
 1.98826 +          if( pMinMax && !db->mallocFailed ){
 1.98827 +            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
 1.98828 +            pMinMax->a[0].pExpr->op = TK_COLUMN;
 1.98829 +          }
 1.98830 +        }
 1.98831 +  
 1.98832 +        /* This case runs if the aggregate has no GROUP BY clause.  The
 1.98833 +        ** processing is much simpler since there is only a single row
 1.98834 +        ** of output.
 1.98835 +        */
 1.98836 +        resetAccumulator(pParse, &sAggInfo);
 1.98837 +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
 1.98838 +        if( pWInfo==0 ){
 1.98839 +          sqlite3ExprListDelete(db, pDel);
 1.98840 +          goto select_end;
 1.98841 +        }
 1.98842 +        updateAccumulator(pParse, &sAggInfo);
 1.98843 +        assert( pMinMax==0 || pMinMax->nExpr==1 );
 1.98844 +        if( pWInfo->nOBSat>0 ){
 1.98845 +          sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
 1.98846 +          VdbeComment((v, "%s() by index",
 1.98847 +                (flag==WHERE_ORDERBY_MIN?"min":"max")));
 1.98848 +        }
 1.98849 +        sqlite3WhereEnd(pWInfo);
 1.98850 +        finalizeAggFunctions(pParse, &sAggInfo);
 1.98851 +      }
 1.98852 +
 1.98853 +      pOrderBy = 0;
 1.98854 +      sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
 1.98855 +      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0, 
 1.98856 +                      pDest, addrEnd, addrEnd);
 1.98857 +      sqlite3ExprListDelete(db, pDel);
 1.98858 +    }
 1.98859 +    sqlite3VdbeResolveLabel(v, addrEnd);
 1.98860 +    
 1.98861 +  } /* endif aggregate query */
 1.98862 +
 1.98863 +  if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
 1.98864 +    explainTempTable(pParse, "DISTINCT");
 1.98865 +  }
 1.98866 +
 1.98867 +  /* If there is an ORDER BY clause, then we need to sort the results
 1.98868 +  ** and send them to the callback one by one.
 1.98869 +  */
 1.98870 +  if( pOrderBy ){
 1.98871 +    explainTempTable(pParse, "ORDER BY");
 1.98872 +    generateSortTail(pParse, p, v, pEList->nExpr, pDest);
 1.98873 +  }
 1.98874 +
 1.98875 +  /* Jump here to skip this query
 1.98876 +  */
 1.98877 +  sqlite3VdbeResolveLabel(v, iEnd);
 1.98878 +
 1.98879 +  /* The SELECT was successfully coded.   Set the return code to 0
 1.98880 +  ** to indicate no errors.
 1.98881 +  */
 1.98882 +  rc = 0;
 1.98883 +
 1.98884 +  /* Control jumps to here if an error is encountered above, or upon
 1.98885 +  ** successful coding of the SELECT.
 1.98886 +  */
 1.98887 +select_end:
 1.98888 +  explainSetInteger(pParse->iSelectId, iRestoreSelectId);
 1.98889 +
 1.98890 +  /* Identify column names if results of the SELECT are to be output.
 1.98891 +  */
 1.98892 +  if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
 1.98893 +    generateColumnNames(pParse, pTabList, pEList);
 1.98894 +  }
 1.98895 +
 1.98896 +  sqlite3DbFree(db, sAggInfo.aCol);
 1.98897 +  sqlite3DbFree(db, sAggInfo.aFunc);
 1.98898 +  return rc;
 1.98899 +}
 1.98900 +
 1.98901 +#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 1.98902 +/*
 1.98903 +** Generate a human-readable description of a the Select object.
 1.98904 +*/
 1.98905 +static void explainOneSelect(Vdbe *pVdbe, Select *p){
 1.98906 +  sqlite3ExplainPrintf(pVdbe, "SELECT ");
 1.98907 +  if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 1.98908 +    if( p->selFlags & SF_Distinct ){
 1.98909 +      sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
 1.98910 +    }
 1.98911 +    if( p->selFlags & SF_Aggregate ){
 1.98912 +      sqlite3ExplainPrintf(pVdbe, "agg_flag ");
 1.98913 +    }
 1.98914 +    sqlite3ExplainNL(pVdbe);
 1.98915 +    sqlite3ExplainPrintf(pVdbe, "   ");
 1.98916 +  }
 1.98917 +  sqlite3ExplainExprList(pVdbe, p->pEList);
 1.98918 +  sqlite3ExplainNL(pVdbe);
 1.98919 +  if( p->pSrc && p->pSrc->nSrc ){
 1.98920 +    int i;
 1.98921 +    sqlite3ExplainPrintf(pVdbe, "FROM ");
 1.98922 +    sqlite3ExplainPush(pVdbe);
 1.98923 +    for(i=0; i<p->pSrc->nSrc; i++){
 1.98924 +      struct SrcList_item *pItem = &p->pSrc->a[i];
 1.98925 +      sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
 1.98926 +      if( pItem->pSelect ){
 1.98927 +        sqlite3ExplainSelect(pVdbe, pItem->pSelect);
 1.98928 +        if( pItem->pTab ){
 1.98929 +          sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
 1.98930 +        }
 1.98931 +      }else if( pItem->zName ){
 1.98932 +        sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
 1.98933 +      }
 1.98934 +      if( pItem->zAlias ){
 1.98935 +        sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
 1.98936 +      }
 1.98937 +      if( pItem->jointype & JT_LEFT ){
 1.98938 +        sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
 1.98939 +      }
 1.98940 +      sqlite3ExplainNL(pVdbe);
 1.98941 +    }
 1.98942 +    sqlite3ExplainPop(pVdbe);
 1.98943 +  }
 1.98944 +  if( p->pWhere ){
 1.98945 +    sqlite3ExplainPrintf(pVdbe, "WHERE ");
 1.98946 +    sqlite3ExplainExpr(pVdbe, p->pWhere);
 1.98947 +    sqlite3ExplainNL(pVdbe);
 1.98948 +  }
 1.98949 +  if( p->pGroupBy ){
 1.98950 +    sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
 1.98951 +    sqlite3ExplainExprList(pVdbe, p->pGroupBy);
 1.98952 +    sqlite3ExplainNL(pVdbe);
 1.98953 +  }
 1.98954 +  if( p->pHaving ){
 1.98955 +    sqlite3ExplainPrintf(pVdbe, "HAVING ");
 1.98956 +    sqlite3ExplainExpr(pVdbe, p->pHaving);
 1.98957 +    sqlite3ExplainNL(pVdbe);
 1.98958 +  }
 1.98959 +  if( p->pOrderBy ){
 1.98960 +    sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
 1.98961 +    sqlite3ExplainExprList(pVdbe, p->pOrderBy);
 1.98962 +    sqlite3ExplainNL(pVdbe);
 1.98963 +  }
 1.98964 +  if( p->pLimit ){
 1.98965 +    sqlite3ExplainPrintf(pVdbe, "LIMIT ");
 1.98966 +    sqlite3ExplainExpr(pVdbe, p->pLimit);
 1.98967 +    sqlite3ExplainNL(pVdbe);
 1.98968 +  }
 1.98969 +  if( p->pOffset ){
 1.98970 +    sqlite3ExplainPrintf(pVdbe, "OFFSET ");
 1.98971 +    sqlite3ExplainExpr(pVdbe, p->pOffset);
 1.98972 +    sqlite3ExplainNL(pVdbe);
 1.98973 +  }
 1.98974 +}
 1.98975 +SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
 1.98976 +  if( p==0 ){
 1.98977 +    sqlite3ExplainPrintf(pVdbe, "(null-select)");
 1.98978 +    return;
 1.98979 +  }
 1.98980 +  while( p->pPrior ) p = p->pPrior;
 1.98981 +  sqlite3ExplainPush(pVdbe);
 1.98982 +  while( p ){
 1.98983 +    explainOneSelect(pVdbe, p);
 1.98984 +    p = p->pNext;
 1.98985 +    if( p==0 ) break;
 1.98986 +    sqlite3ExplainNL(pVdbe);
 1.98987 +    sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
 1.98988 +  }
 1.98989 +  sqlite3ExplainPrintf(pVdbe, "END");
 1.98990 +  sqlite3ExplainPop(pVdbe);
 1.98991 +}
 1.98992 +
 1.98993 +/* End of the structure debug printing code
 1.98994 +*****************************************************************************/
 1.98995 +#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
 1.98996 +
 1.98997 +/************** End of select.c **********************************************/
 1.98998 +/************** Begin file table.c *******************************************/
 1.98999 +/*
 1.99000 +** 2001 September 15
 1.99001 +**
 1.99002 +** The author disclaims copyright to this source code.  In place of
 1.99003 +** a legal notice, here is a blessing:
 1.99004 +**
 1.99005 +**    May you do good and not evil.
 1.99006 +**    May you find forgiveness for yourself and forgive others.
 1.99007 +**    May you share freely, never taking more than you give.
 1.99008 +**
 1.99009 +*************************************************************************
 1.99010 +** This file contains the sqlite3_get_table() and sqlite3_free_table()
 1.99011 +** interface routines.  These are just wrappers around the main
 1.99012 +** interface routine of sqlite3_exec().
 1.99013 +**
 1.99014 +** These routines are in a separate files so that they will not be linked
 1.99015 +** if they are not used.
 1.99016 +*/
 1.99017 +/* #include <stdlib.h> */
 1.99018 +/* #include <string.h> */
 1.99019 +
 1.99020 +#ifndef SQLITE_OMIT_GET_TABLE
 1.99021 +
 1.99022 +/*
 1.99023 +** This structure is used to pass data from sqlite3_get_table() through
 1.99024 +** to the callback function is uses to build the result.
 1.99025 +*/
 1.99026 +typedef struct TabResult {
 1.99027 +  char **azResult;   /* Accumulated output */
 1.99028 +  char *zErrMsg;     /* Error message text, if an error occurs */
 1.99029 +  int nAlloc;        /* Slots allocated for azResult[] */
 1.99030 +  int nRow;          /* Number of rows in the result */
 1.99031 +  int nColumn;       /* Number of columns in the result */
 1.99032 +  int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
 1.99033 +  int rc;            /* Return code from sqlite3_exec() */
 1.99034 +} TabResult;
 1.99035 +
 1.99036 +/*
 1.99037 +** This routine is called once for each row in the result table.  Its job
 1.99038 +** is to fill in the TabResult structure appropriately, allocating new
 1.99039 +** memory as necessary.
 1.99040 +*/
 1.99041 +static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
 1.99042 +  TabResult *p = (TabResult*)pArg;  /* Result accumulator */
 1.99043 +  int need;                         /* Slots needed in p->azResult[] */
 1.99044 +  int i;                            /* Loop counter */
 1.99045 +  char *z;                          /* A single column of result */
 1.99046 +
 1.99047 +  /* Make sure there is enough space in p->azResult to hold everything
 1.99048 +  ** we need to remember from this invocation of the callback.
 1.99049 +  */
 1.99050 +  if( p->nRow==0 && argv!=0 ){
 1.99051 +    need = nCol*2;
 1.99052 +  }else{
 1.99053 +    need = nCol;
 1.99054 +  }
 1.99055 +  if( p->nData + need > p->nAlloc ){
 1.99056 +    char **azNew;
 1.99057 +    p->nAlloc = p->nAlloc*2 + need;
 1.99058 +    azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
 1.99059 +    if( azNew==0 ) goto malloc_failed;
 1.99060 +    p->azResult = azNew;
 1.99061 +  }
 1.99062 +
 1.99063 +  /* If this is the first row, then generate an extra row containing
 1.99064 +  ** the names of all columns.
 1.99065 +  */
 1.99066 +  if( p->nRow==0 ){
 1.99067 +    p->nColumn = nCol;
 1.99068 +    for(i=0; i<nCol; i++){
 1.99069 +      z = sqlite3_mprintf("%s", colv[i]);
 1.99070 +      if( z==0 ) goto malloc_failed;
 1.99071 +      p->azResult[p->nData++] = z;
 1.99072 +    }
 1.99073 +  }else if( p->nColumn!=nCol ){
 1.99074 +    sqlite3_free(p->zErrMsg);
 1.99075 +    p->zErrMsg = sqlite3_mprintf(
 1.99076 +       "sqlite3_get_table() called with two or more incompatible queries"
 1.99077 +    );
 1.99078 +    p->rc = SQLITE_ERROR;
 1.99079 +    return 1;
 1.99080 +  }
 1.99081 +
 1.99082 +  /* Copy over the row data
 1.99083 +  */
 1.99084 +  if( argv!=0 ){
 1.99085 +    for(i=0; i<nCol; i++){
 1.99086 +      if( argv[i]==0 ){
 1.99087 +        z = 0;
 1.99088 +      }else{
 1.99089 +        int n = sqlite3Strlen30(argv[i])+1;
 1.99090 +        z = sqlite3_malloc( n );
 1.99091 +        if( z==0 ) goto malloc_failed;
 1.99092 +        memcpy(z, argv[i], n);
 1.99093 +      }
 1.99094 +      p->azResult[p->nData++] = z;
 1.99095 +    }
 1.99096 +    p->nRow++;
 1.99097 +  }
 1.99098 +  return 0;
 1.99099 +
 1.99100 +malloc_failed:
 1.99101 +  p->rc = SQLITE_NOMEM;
 1.99102 +  return 1;
 1.99103 +}
 1.99104 +
 1.99105 +/*
 1.99106 +** Query the database.  But instead of invoking a callback for each row,
 1.99107 +** malloc() for space to hold the result and return the entire results
 1.99108 +** at the conclusion of the call.
 1.99109 +**
 1.99110 +** The result that is written to ***pazResult is held in memory obtained
 1.99111 +** from malloc().  But the caller cannot free this memory directly.  
 1.99112 +** Instead, the entire table should be passed to sqlite3_free_table() when
 1.99113 +** the calling procedure is finished using it.
 1.99114 +*/
 1.99115 +SQLITE_API int sqlite3_get_table(
 1.99116 +  sqlite3 *db,                /* The database on which the SQL executes */
 1.99117 +  const char *zSql,           /* The SQL to be executed */
 1.99118 +  char ***pazResult,          /* Write the result table here */
 1.99119 +  int *pnRow,                 /* Write the number of rows in the result here */
 1.99120 +  int *pnColumn,              /* Write the number of columns of result here */
 1.99121 +  char **pzErrMsg             /* Write error messages here */
 1.99122 +){
 1.99123 +  int rc;
 1.99124 +  TabResult res;
 1.99125 +
 1.99126 +  *pazResult = 0;
 1.99127 +  if( pnColumn ) *pnColumn = 0;
 1.99128 +  if( pnRow ) *pnRow = 0;
 1.99129 +  if( pzErrMsg ) *pzErrMsg = 0;
 1.99130 +  res.zErrMsg = 0;
 1.99131 +  res.nRow = 0;
 1.99132 +  res.nColumn = 0;
 1.99133 +  res.nData = 1;
 1.99134 +  res.nAlloc = 20;
 1.99135 +  res.rc = SQLITE_OK;
 1.99136 +  res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
 1.99137 +  if( res.azResult==0 ){
 1.99138 +     db->errCode = SQLITE_NOMEM;
 1.99139 +     return SQLITE_NOMEM;
 1.99140 +  }
 1.99141 +  res.azResult[0] = 0;
 1.99142 +  rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
 1.99143 +  assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
 1.99144 +  res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
 1.99145 +  if( (rc&0xff)==SQLITE_ABORT ){
 1.99146 +    sqlite3_free_table(&res.azResult[1]);
 1.99147 +    if( res.zErrMsg ){
 1.99148 +      if( pzErrMsg ){
 1.99149 +        sqlite3_free(*pzErrMsg);
 1.99150 +        *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
 1.99151 +      }
 1.99152 +      sqlite3_free(res.zErrMsg);
 1.99153 +    }
 1.99154 +    db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
 1.99155 +    return res.rc;
 1.99156 +  }
 1.99157 +  sqlite3_free(res.zErrMsg);
 1.99158 +  if( rc!=SQLITE_OK ){
 1.99159 +    sqlite3_free_table(&res.azResult[1]);
 1.99160 +    return rc;
 1.99161 +  }
 1.99162 +  if( res.nAlloc>res.nData ){
 1.99163 +    char **azNew;
 1.99164 +    azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
 1.99165 +    if( azNew==0 ){
 1.99166 +      sqlite3_free_table(&res.azResult[1]);
 1.99167 +      db->errCode = SQLITE_NOMEM;
 1.99168 +      return SQLITE_NOMEM;
 1.99169 +    }
 1.99170 +    res.azResult = azNew;
 1.99171 +  }
 1.99172 +  *pazResult = &res.azResult[1];
 1.99173 +  if( pnColumn ) *pnColumn = res.nColumn;
 1.99174 +  if( pnRow ) *pnRow = res.nRow;
 1.99175 +  return rc;
 1.99176 +}
 1.99177 +
 1.99178 +/*
 1.99179 +** This routine frees the space the sqlite3_get_table() malloced.
 1.99180 +*/
 1.99181 +SQLITE_API void sqlite3_free_table(
 1.99182 +  char **azResult            /* Result returned from from sqlite3_get_table() */
 1.99183 +){
 1.99184 +  if( azResult ){
 1.99185 +    int i, n;
 1.99186 +    azResult--;
 1.99187 +    assert( azResult!=0 );
 1.99188 +    n = SQLITE_PTR_TO_INT(azResult[0]);
 1.99189 +    for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
 1.99190 +    sqlite3_free(azResult);
 1.99191 +  }
 1.99192 +}
 1.99193 +
 1.99194 +#endif /* SQLITE_OMIT_GET_TABLE */
 1.99195 +
 1.99196 +/************** End of table.c ***********************************************/
 1.99197 +/************** Begin file trigger.c *****************************************/
 1.99198 +/*
 1.99199 +**
 1.99200 +** The author disclaims copyright to this source code.  In place of
 1.99201 +** a legal notice, here is a blessing:
 1.99202 +**
 1.99203 +**    May you do good and not evil.
 1.99204 +**    May you find forgiveness for yourself and forgive others.
 1.99205 +**    May you share freely, never taking more than you give.
 1.99206 +**
 1.99207 +*************************************************************************
 1.99208 +** This file contains the implementation for TRIGGERs
 1.99209 +*/
 1.99210 +
 1.99211 +#ifndef SQLITE_OMIT_TRIGGER
 1.99212 +/*
 1.99213 +** Delete a linked list of TriggerStep structures.
 1.99214 +*/
 1.99215 +SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
 1.99216 +  while( pTriggerStep ){
 1.99217 +    TriggerStep * pTmp = pTriggerStep;
 1.99218 +    pTriggerStep = pTriggerStep->pNext;
 1.99219 +
 1.99220 +    sqlite3ExprDelete(db, pTmp->pWhere);
 1.99221 +    sqlite3ExprListDelete(db, pTmp->pExprList);
 1.99222 +    sqlite3SelectDelete(db, pTmp->pSelect);
 1.99223 +    sqlite3IdListDelete(db, pTmp->pIdList);
 1.99224 +
 1.99225 +    sqlite3DbFree(db, pTmp);
 1.99226 +  }
 1.99227 +}
 1.99228 +
 1.99229 +/*
 1.99230 +** Given table pTab, return a list of all the triggers attached to 
 1.99231 +** the table. The list is connected by Trigger.pNext pointers.
 1.99232 +**
 1.99233 +** All of the triggers on pTab that are in the same database as pTab
 1.99234 +** are already attached to pTab->pTrigger.  But there might be additional
 1.99235 +** triggers on pTab in the TEMP schema.  This routine prepends all
 1.99236 +** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
 1.99237 +** and returns the combined list.
 1.99238 +**
 1.99239 +** To state it another way:  This routine returns a list of all triggers
 1.99240 +** that fire off of pTab.  The list will include any TEMP triggers on
 1.99241 +** pTab as well as the triggers lised in pTab->pTrigger.
 1.99242 +*/
 1.99243 +SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
 1.99244 +  Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
 1.99245 +  Trigger *pList = 0;                  /* List of triggers to return */
 1.99246 +
 1.99247 +  if( pParse->disableTriggers ){
 1.99248 +    return 0;
 1.99249 +  }
 1.99250 +
 1.99251 +  if( pTmpSchema!=pTab->pSchema ){
 1.99252 +    HashElem *p;
 1.99253 +    assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
 1.99254 +    for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
 1.99255 +      Trigger *pTrig = (Trigger *)sqliteHashData(p);
 1.99256 +      if( pTrig->pTabSchema==pTab->pSchema
 1.99257 +       && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 
 1.99258 +      ){
 1.99259 +        pTrig->pNext = (pList ? pList : pTab->pTrigger);
 1.99260 +        pList = pTrig;
 1.99261 +      }
 1.99262 +    }
 1.99263 +  }
 1.99264 +
 1.99265 +  return (pList ? pList : pTab->pTrigger);
 1.99266 +}
 1.99267 +
 1.99268 +/*
 1.99269 +** This is called by the parser when it sees a CREATE TRIGGER statement
 1.99270 +** up to the point of the BEGIN before the trigger actions.  A Trigger
 1.99271 +** structure is generated based on the information available and stored
 1.99272 +** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
 1.99273 +** sqlite3FinishTrigger() function is called to complete the trigger
 1.99274 +** construction process.
 1.99275 +*/
 1.99276 +SQLITE_PRIVATE void sqlite3BeginTrigger(
 1.99277 +  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
 1.99278 +  Token *pName1,      /* The name of the trigger */
 1.99279 +  Token *pName2,      /* The name of the trigger */
 1.99280 +  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
 1.99281 +  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
 1.99282 +  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
 1.99283 +  SrcList *pTableName,/* The name of the table/view the trigger applies to */
 1.99284 +  Expr *pWhen,        /* WHEN clause */
 1.99285 +  int isTemp,         /* True if the TEMPORARY keyword is present */
 1.99286 +  int noErr           /* Suppress errors if the trigger already exists */
 1.99287 +){
 1.99288 +  Trigger *pTrigger = 0;  /* The new trigger */
 1.99289 +  Table *pTab;            /* Table that the trigger fires off of */
 1.99290 +  char *zName = 0;        /* Name of the trigger */
 1.99291 +  sqlite3 *db = pParse->db;  /* The database connection */
 1.99292 +  int iDb;                /* The database to store the trigger in */
 1.99293 +  Token *pName;           /* The unqualified db name */
 1.99294 +  DbFixer sFix;           /* State vector for the DB fixer */
 1.99295 +  int iTabDb;             /* Index of the database holding pTab */
 1.99296 +
 1.99297 +  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
 1.99298 +  assert( pName2!=0 );
 1.99299 +  assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
 1.99300 +  assert( op>0 && op<0xff );
 1.99301 +  if( isTemp ){
 1.99302 +    /* If TEMP was specified, then the trigger name may not be qualified. */
 1.99303 +    if( pName2->n>0 ){
 1.99304 +      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
 1.99305 +      goto trigger_cleanup;
 1.99306 +    }
 1.99307 +    iDb = 1;
 1.99308 +    pName = pName1;
 1.99309 +  }else{
 1.99310 +    /* Figure out the db that the trigger will be created in */
 1.99311 +    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 1.99312 +    if( iDb<0 ){
 1.99313 +      goto trigger_cleanup;
 1.99314 +    }
 1.99315 +  }
 1.99316 +  if( !pTableName || db->mallocFailed ){
 1.99317 +    goto trigger_cleanup;
 1.99318 +  }
 1.99319 +
 1.99320 +  /* A long-standing parser bug is that this syntax was allowed:
 1.99321 +  **
 1.99322 +  **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
 1.99323 +  **                                                 ^^^^^^^^
 1.99324 +  **
 1.99325 +  ** To maintain backwards compatibility, ignore the database
 1.99326 +  ** name on pTableName if we are reparsing our of SQLITE_MASTER.
 1.99327 +  */
 1.99328 +  if( db->init.busy && iDb!=1 ){
 1.99329 +    sqlite3DbFree(db, pTableName->a[0].zDatabase);
 1.99330 +    pTableName->a[0].zDatabase = 0;
 1.99331 +  }
 1.99332 +
 1.99333 +  /* If the trigger name was unqualified, and the table is a temp table,
 1.99334 +  ** then set iDb to 1 to create the trigger in the temporary database.
 1.99335 +  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
 1.99336 +  ** exist, the error is caught by the block below.
 1.99337 +  */
 1.99338 +  pTab = sqlite3SrcListLookup(pParse, pTableName);
 1.99339 +  if( db->init.busy==0 && pName2->n==0 && pTab
 1.99340 +        && pTab->pSchema==db->aDb[1].pSchema ){
 1.99341 +    iDb = 1;
 1.99342 +  }
 1.99343 +
 1.99344 +  /* Ensure the table name matches database name and that the table exists */
 1.99345 +  if( db->mallocFailed ) goto trigger_cleanup;
 1.99346 +  assert( pTableName->nSrc==1 );
 1.99347 +  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
 1.99348 +      sqlite3FixSrcList(&sFix, pTableName) ){
 1.99349 +    goto trigger_cleanup;
 1.99350 +  }
 1.99351 +  pTab = sqlite3SrcListLookup(pParse, pTableName);
 1.99352 +  if( !pTab ){
 1.99353 +    /* The table does not exist. */
 1.99354 +    if( db->init.iDb==1 ){
 1.99355 +      /* Ticket #3810.
 1.99356 +      ** Normally, whenever a table is dropped, all associated triggers are
 1.99357 +      ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
 1.99358 +      ** and the table is dropped by a different database connection, the
 1.99359 +      ** trigger is not visible to the database connection that does the
 1.99360 +      ** drop so the trigger cannot be dropped.  This results in an
 1.99361 +      ** "orphaned trigger" - a trigger whose associated table is missing.
 1.99362 +      */
 1.99363 +      db->init.orphanTrigger = 1;
 1.99364 +    }
 1.99365 +    goto trigger_cleanup;
 1.99366 +  }
 1.99367 +  if( IsVirtual(pTab) ){
 1.99368 +    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
 1.99369 +    goto trigger_cleanup;
 1.99370 +  }
 1.99371 +
 1.99372 +  /* Check that the trigger name is not reserved and that no trigger of the
 1.99373 +  ** specified name exists */
 1.99374 +  zName = sqlite3NameFromToken(db, pName);
 1.99375 +  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 1.99376 +    goto trigger_cleanup;
 1.99377 +  }
 1.99378 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.99379 +  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
 1.99380 +                      zName, sqlite3Strlen30(zName)) ){
 1.99381 +    if( !noErr ){
 1.99382 +      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
 1.99383 +    }else{
 1.99384 +      assert( !db->init.busy );
 1.99385 +      sqlite3CodeVerifySchema(pParse, iDb);
 1.99386 +    }
 1.99387 +    goto trigger_cleanup;
 1.99388 +  }
 1.99389 +
 1.99390 +  /* Do not create a trigger on a system table */
 1.99391 +  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
 1.99392 +    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
 1.99393 +    pParse->nErr++;
 1.99394 +    goto trigger_cleanup;
 1.99395 +  }
 1.99396 +
 1.99397 +  /* INSTEAD of triggers are only for views and views only support INSTEAD
 1.99398 +  ** of triggers.
 1.99399 +  */
 1.99400 +  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
 1.99401 +    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
 1.99402 +        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
 1.99403 +    goto trigger_cleanup;
 1.99404 +  }
 1.99405 +  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
 1.99406 +    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
 1.99407 +        " trigger on table: %S", pTableName, 0);
 1.99408 +    goto trigger_cleanup;
 1.99409 +  }
 1.99410 +  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 1.99411 +
 1.99412 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.99413 +  {
 1.99414 +    int code = SQLITE_CREATE_TRIGGER;
 1.99415 +    const char *zDb = db->aDb[iTabDb].zName;
 1.99416 +    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
 1.99417 +    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
 1.99418 +    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
 1.99419 +      goto trigger_cleanup;
 1.99420 +    }
 1.99421 +    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
 1.99422 +      goto trigger_cleanup;
 1.99423 +    }
 1.99424 +  }
 1.99425 +#endif
 1.99426 +
 1.99427 +  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
 1.99428 +  ** cannot appear on views.  So we might as well translate every
 1.99429 +  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
 1.99430 +  ** elsewhere.
 1.99431 +  */
 1.99432 +  if (tr_tm == TK_INSTEAD){
 1.99433 +    tr_tm = TK_BEFORE;
 1.99434 +  }
 1.99435 +
 1.99436 +  /* Build the Trigger object */
 1.99437 +  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
 1.99438 +  if( pTrigger==0 ) goto trigger_cleanup;
 1.99439 +  pTrigger->zName = zName;
 1.99440 +  zName = 0;
 1.99441 +  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
 1.99442 +  pTrigger->pSchema = db->aDb[iDb].pSchema;
 1.99443 +  pTrigger->pTabSchema = pTab->pSchema;
 1.99444 +  pTrigger->op = (u8)op;
 1.99445 +  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
 1.99446 +  pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
 1.99447 +  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
 1.99448 +  assert( pParse->pNewTrigger==0 );
 1.99449 +  pParse->pNewTrigger = pTrigger;
 1.99450 +
 1.99451 +trigger_cleanup:
 1.99452 +  sqlite3DbFree(db, zName);
 1.99453 +  sqlite3SrcListDelete(db, pTableName);
 1.99454 +  sqlite3IdListDelete(db, pColumns);
 1.99455 +  sqlite3ExprDelete(db, pWhen);
 1.99456 +  if( !pParse->pNewTrigger ){
 1.99457 +    sqlite3DeleteTrigger(db, pTrigger);
 1.99458 +  }else{
 1.99459 +    assert( pParse->pNewTrigger==pTrigger );
 1.99460 +  }
 1.99461 +}
 1.99462 +
 1.99463 +/*
 1.99464 +** This routine is called after all of the trigger actions have been parsed
 1.99465 +** in order to complete the process of building the trigger.
 1.99466 +*/
 1.99467 +SQLITE_PRIVATE void sqlite3FinishTrigger(
 1.99468 +  Parse *pParse,          /* Parser context */
 1.99469 +  TriggerStep *pStepList, /* The triggered program */
 1.99470 +  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
 1.99471 +){
 1.99472 +  Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
 1.99473 +  char *zName;                            /* Name of trigger */
 1.99474 +  sqlite3 *db = pParse->db;               /* The database */
 1.99475 +  DbFixer sFix;                           /* Fixer object */
 1.99476 +  int iDb;                                /* Database containing the trigger */
 1.99477 +  Token nameToken;                        /* Trigger name for error reporting */
 1.99478 +
 1.99479 +  pParse->pNewTrigger = 0;
 1.99480 +  if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
 1.99481 +  zName = pTrig->zName;
 1.99482 +  iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
 1.99483 +  pTrig->step_list = pStepList;
 1.99484 +  while( pStepList ){
 1.99485 +    pStepList->pTrig = pTrig;
 1.99486 +    pStepList = pStepList->pNext;
 1.99487 +  }
 1.99488 +  nameToken.z = pTrig->zName;
 1.99489 +  nameToken.n = sqlite3Strlen30(nameToken.z);
 1.99490 +  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 
 1.99491 +          && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
 1.99492 +    goto triggerfinish_cleanup;
 1.99493 +  }
 1.99494 +
 1.99495 +  /* if we are not initializing,
 1.99496 +  ** build the sqlite_master entry
 1.99497 +  */
 1.99498 +  if( !db->init.busy ){
 1.99499 +    Vdbe *v;
 1.99500 +    char *z;
 1.99501 +
 1.99502 +    /* Make an entry in the sqlite_master table */
 1.99503 +    v = sqlite3GetVdbe(pParse);
 1.99504 +    if( v==0 ) goto triggerfinish_cleanup;
 1.99505 +    sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.99506 +    z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
 1.99507 +    sqlite3NestedParse(pParse,
 1.99508 +       "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
 1.99509 +       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
 1.99510 +       pTrig->table, z);
 1.99511 +    sqlite3DbFree(db, z);
 1.99512 +    sqlite3ChangeCookie(pParse, iDb);
 1.99513 +    sqlite3VdbeAddParseSchemaOp(v, iDb,
 1.99514 +        sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
 1.99515 +  }
 1.99516 +
 1.99517 +  if( db->init.busy ){
 1.99518 +    Trigger *pLink = pTrig;
 1.99519 +    Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
 1.99520 +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.99521 +    pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
 1.99522 +    if( pTrig ){
 1.99523 +      db->mallocFailed = 1;
 1.99524 +    }else if( pLink->pSchema==pLink->pTabSchema ){
 1.99525 +      Table *pTab;
 1.99526 +      int n = sqlite3Strlen30(pLink->table);
 1.99527 +      pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
 1.99528 +      assert( pTab!=0 );
 1.99529 +      pLink->pNext = pTab->pTrigger;
 1.99530 +      pTab->pTrigger = pLink;
 1.99531 +    }
 1.99532 +  }
 1.99533 +
 1.99534 +triggerfinish_cleanup:
 1.99535 +  sqlite3DeleteTrigger(db, pTrig);
 1.99536 +  assert( !pParse->pNewTrigger );
 1.99537 +  sqlite3DeleteTriggerStep(db, pStepList);
 1.99538 +}
 1.99539 +
 1.99540 +/*
 1.99541 +** Turn a SELECT statement (that the pSelect parameter points to) into
 1.99542 +** a trigger step.  Return a pointer to a TriggerStep structure.
 1.99543 +**
 1.99544 +** The parser calls this routine when it finds a SELECT statement in
 1.99545 +** body of a TRIGGER.  
 1.99546 +*/
 1.99547 +SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
 1.99548 +  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
 1.99549 +  if( pTriggerStep==0 ) {
 1.99550 +    sqlite3SelectDelete(db, pSelect);
 1.99551 +    return 0;
 1.99552 +  }
 1.99553 +  pTriggerStep->op = TK_SELECT;
 1.99554 +  pTriggerStep->pSelect = pSelect;
 1.99555 +  pTriggerStep->orconf = OE_Default;
 1.99556 +  return pTriggerStep;
 1.99557 +}
 1.99558 +
 1.99559 +/*
 1.99560 +** Allocate space to hold a new trigger step.  The allocated space
 1.99561 +** holds both the TriggerStep object and the TriggerStep.target.z string.
 1.99562 +**
 1.99563 +** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
 1.99564 +*/
 1.99565 +static TriggerStep *triggerStepAllocate(
 1.99566 +  sqlite3 *db,                /* Database connection */
 1.99567 +  u8 op,                      /* Trigger opcode */
 1.99568 +  Token *pName                /* The target name */
 1.99569 +){
 1.99570 +  TriggerStep *pTriggerStep;
 1.99571 +
 1.99572 +  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
 1.99573 +  if( pTriggerStep ){
 1.99574 +    char *z = (char*)&pTriggerStep[1];
 1.99575 +    memcpy(z, pName->z, pName->n);
 1.99576 +    pTriggerStep->target.z = z;
 1.99577 +    pTriggerStep->target.n = pName->n;
 1.99578 +    pTriggerStep->op = op;
 1.99579 +  }
 1.99580 +  return pTriggerStep;
 1.99581 +}
 1.99582 +
 1.99583 +/*
 1.99584 +** Build a trigger step out of an INSERT statement.  Return a pointer
 1.99585 +** to the new trigger step.
 1.99586 +**
 1.99587 +** The parser calls this routine when it sees an INSERT inside the
 1.99588 +** body of a trigger.
 1.99589 +*/
 1.99590 +SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
 1.99591 +  sqlite3 *db,        /* The database connection */
 1.99592 +  Token *pTableName,  /* Name of the table into which we insert */
 1.99593 +  IdList *pColumn,    /* List of columns in pTableName to insert into */
 1.99594 +  ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
 1.99595 +  Select *pSelect,    /* A SELECT statement that supplies values */
 1.99596 +  u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
 1.99597 +){
 1.99598 +  TriggerStep *pTriggerStep;
 1.99599 +
 1.99600 +  assert(pEList == 0 || pSelect == 0);
 1.99601 +  assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
 1.99602 +
 1.99603 +  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
 1.99604 +  if( pTriggerStep ){
 1.99605 +    pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 1.99606 +    pTriggerStep->pIdList = pColumn;
 1.99607 +    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
 1.99608 +    pTriggerStep->orconf = orconf;
 1.99609 +  }else{
 1.99610 +    sqlite3IdListDelete(db, pColumn);
 1.99611 +  }
 1.99612 +  sqlite3ExprListDelete(db, pEList);
 1.99613 +  sqlite3SelectDelete(db, pSelect);
 1.99614 +
 1.99615 +  return pTriggerStep;
 1.99616 +}
 1.99617 +
 1.99618 +/*
 1.99619 +** Construct a trigger step that implements an UPDATE statement and return
 1.99620 +** a pointer to that trigger step.  The parser calls this routine when it
 1.99621 +** sees an UPDATE statement inside the body of a CREATE TRIGGER.
 1.99622 +*/
 1.99623 +SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
 1.99624 +  sqlite3 *db,         /* The database connection */
 1.99625 +  Token *pTableName,   /* Name of the table to be updated */
 1.99626 +  ExprList *pEList,    /* The SET clause: list of column and new values */
 1.99627 +  Expr *pWhere,        /* The WHERE clause */
 1.99628 +  u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
 1.99629 +){
 1.99630 +  TriggerStep *pTriggerStep;
 1.99631 +
 1.99632 +  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
 1.99633 +  if( pTriggerStep ){
 1.99634 +    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
 1.99635 +    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 1.99636 +    pTriggerStep->orconf = orconf;
 1.99637 +  }
 1.99638 +  sqlite3ExprListDelete(db, pEList);
 1.99639 +  sqlite3ExprDelete(db, pWhere);
 1.99640 +  return pTriggerStep;
 1.99641 +}
 1.99642 +
 1.99643 +/*
 1.99644 +** Construct a trigger step that implements a DELETE statement and return
 1.99645 +** a pointer to that trigger step.  The parser calls this routine when it
 1.99646 +** sees a DELETE statement inside the body of a CREATE TRIGGER.
 1.99647 +*/
 1.99648 +SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
 1.99649 +  sqlite3 *db,            /* Database connection */
 1.99650 +  Token *pTableName,      /* The table from which rows are deleted */
 1.99651 +  Expr *pWhere            /* The WHERE clause */
 1.99652 +){
 1.99653 +  TriggerStep *pTriggerStep;
 1.99654 +
 1.99655 +  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
 1.99656 +  if( pTriggerStep ){
 1.99657 +    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 1.99658 +    pTriggerStep->orconf = OE_Default;
 1.99659 +  }
 1.99660 +  sqlite3ExprDelete(db, pWhere);
 1.99661 +  return pTriggerStep;
 1.99662 +}
 1.99663 +
 1.99664 +/* 
 1.99665 +** Recursively delete a Trigger structure
 1.99666 +*/
 1.99667 +SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
 1.99668 +  if( pTrigger==0 ) return;
 1.99669 +  sqlite3DeleteTriggerStep(db, pTrigger->step_list);
 1.99670 +  sqlite3DbFree(db, pTrigger->zName);
 1.99671 +  sqlite3DbFree(db, pTrigger->table);
 1.99672 +  sqlite3ExprDelete(db, pTrigger->pWhen);
 1.99673 +  sqlite3IdListDelete(db, pTrigger->pColumns);
 1.99674 +  sqlite3DbFree(db, pTrigger);
 1.99675 +}
 1.99676 +
 1.99677 +/*
 1.99678 +** This function is called to drop a trigger from the database schema. 
 1.99679 +**
 1.99680 +** This may be called directly from the parser and therefore identifies
 1.99681 +** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
 1.99682 +** same job as this routine except it takes a pointer to the trigger
 1.99683 +** instead of the trigger name.
 1.99684 +**/
 1.99685 +SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
 1.99686 +  Trigger *pTrigger = 0;
 1.99687 +  int i;
 1.99688 +  const char *zDb;
 1.99689 +  const char *zName;
 1.99690 +  int nName;
 1.99691 +  sqlite3 *db = pParse->db;
 1.99692 +
 1.99693 +  if( db->mallocFailed ) goto drop_trigger_cleanup;
 1.99694 +  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 1.99695 +    goto drop_trigger_cleanup;
 1.99696 +  }
 1.99697 +
 1.99698 +  assert( pName->nSrc==1 );
 1.99699 +  zDb = pName->a[0].zDatabase;
 1.99700 +  zName = pName->a[0].zName;
 1.99701 +  nName = sqlite3Strlen30(zName);
 1.99702 +  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 1.99703 +  for(i=OMIT_TEMPDB; i<db->nDb; i++){
 1.99704 +    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 1.99705 +    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
 1.99706 +    assert( sqlite3SchemaMutexHeld(db, j, 0) );
 1.99707 +    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
 1.99708 +    if( pTrigger ) break;
 1.99709 +  }
 1.99710 +  if( !pTrigger ){
 1.99711 +    if( !noErr ){
 1.99712 +      sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
 1.99713 +    }else{
 1.99714 +      sqlite3CodeVerifyNamedSchema(pParse, zDb);
 1.99715 +    }
 1.99716 +    pParse->checkSchema = 1;
 1.99717 +    goto drop_trigger_cleanup;
 1.99718 +  }
 1.99719 +  sqlite3DropTriggerPtr(pParse, pTrigger);
 1.99720 +
 1.99721 +drop_trigger_cleanup:
 1.99722 +  sqlite3SrcListDelete(db, pName);
 1.99723 +}
 1.99724 +
 1.99725 +/*
 1.99726 +** Return a pointer to the Table structure for the table that a trigger
 1.99727 +** is set on.
 1.99728 +*/
 1.99729 +static Table *tableOfTrigger(Trigger *pTrigger){
 1.99730 +  int n = sqlite3Strlen30(pTrigger->table);
 1.99731 +  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
 1.99732 +}
 1.99733 +
 1.99734 +
 1.99735 +/*
 1.99736 +** Drop a trigger given a pointer to that trigger. 
 1.99737 +*/
 1.99738 +SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
 1.99739 +  Table   *pTable;
 1.99740 +  Vdbe *v;
 1.99741 +  sqlite3 *db = pParse->db;
 1.99742 +  int iDb;
 1.99743 +
 1.99744 +  iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
 1.99745 +  assert( iDb>=0 && iDb<db->nDb );
 1.99746 +  pTable = tableOfTrigger(pTrigger);
 1.99747 +  assert( pTable );
 1.99748 +  assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
 1.99749 +#ifndef SQLITE_OMIT_AUTHORIZATION
 1.99750 +  {
 1.99751 +    int code = SQLITE_DROP_TRIGGER;
 1.99752 +    const char *zDb = db->aDb[iDb].zName;
 1.99753 +    const char *zTab = SCHEMA_TABLE(iDb);
 1.99754 +    if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
 1.99755 +    if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
 1.99756 +      sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
 1.99757 +      return;
 1.99758 +    }
 1.99759 +  }
 1.99760 +#endif
 1.99761 +
 1.99762 +  /* Generate code to destroy the database record of the trigger.
 1.99763 +  */
 1.99764 +  assert( pTable!=0 );
 1.99765 +  if( (v = sqlite3GetVdbe(pParse))!=0 ){
 1.99766 +    int base;
 1.99767 +    static const VdbeOpList dropTrigger[] = {
 1.99768 +      { OP_Rewind,     0, ADDR(9),  0},
 1.99769 +      { OP_String8,    0, 1,        0}, /* 1 */
 1.99770 +      { OP_Column,     0, 1,        2},
 1.99771 +      { OP_Ne,         2, ADDR(8),  1},
 1.99772 +      { OP_String8,    0, 1,        0}, /* 4: "trigger" */
 1.99773 +      { OP_Column,     0, 0,        2},
 1.99774 +      { OP_Ne,         2, ADDR(8),  1},
 1.99775 +      { OP_Delete,     0, 0,        0},
 1.99776 +      { OP_Next,       0, ADDR(1),  0}, /* 8 */
 1.99777 +    };
 1.99778 +
 1.99779 +    sqlite3BeginWriteOperation(pParse, 0, iDb);
 1.99780 +    sqlite3OpenMasterTable(pParse, iDb);
 1.99781 +    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
 1.99782 +    sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
 1.99783 +    sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
 1.99784 +    sqlite3ChangeCookie(pParse, iDb);
 1.99785 +    sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
 1.99786 +    sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
 1.99787 +    if( pParse->nMem<3 ){
 1.99788 +      pParse->nMem = 3;
 1.99789 +    }
 1.99790 +  }
 1.99791 +}
 1.99792 +
 1.99793 +/*
 1.99794 +** Remove a trigger from the hash tables of the sqlite* pointer.
 1.99795 +*/
 1.99796 +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
 1.99797 +  Trigger *pTrigger;
 1.99798 +  Hash *pHash;
 1.99799 +
 1.99800 +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 1.99801 +  pHash = &(db->aDb[iDb].pSchema->trigHash);
 1.99802 +  pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
 1.99803 +  if( ALWAYS(pTrigger) ){
 1.99804 +    if( pTrigger->pSchema==pTrigger->pTabSchema ){
 1.99805 +      Table *pTab = tableOfTrigger(pTrigger);
 1.99806 +      Trigger **pp;
 1.99807 +      for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
 1.99808 +      *pp = (*pp)->pNext;
 1.99809 +    }
 1.99810 +    sqlite3DeleteTrigger(db, pTrigger);
 1.99811 +    db->flags |= SQLITE_InternChanges;
 1.99812 +  }
 1.99813 +}
 1.99814 +
 1.99815 +/*
 1.99816 +** pEList is the SET clause of an UPDATE statement.  Each entry
 1.99817 +** in pEList is of the format <id>=<expr>.  If any of the entries
 1.99818 +** in pEList have an <id> which matches an identifier in pIdList,
 1.99819 +** then return TRUE.  If pIdList==NULL, then it is considered a
 1.99820 +** wildcard that matches anything.  Likewise if pEList==NULL then
 1.99821 +** it matches anything so always return true.  Return false only
 1.99822 +** if there is no match.
 1.99823 +*/
 1.99824 +static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
 1.99825 +  int e;
 1.99826 +  if( pIdList==0 || NEVER(pEList==0) ) return 1;
 1.99827 +  for(e=0; e<pEList->nExpr; e++){
 1.99828 +    if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
 1.99829 +  }
 1.99830 +  return 0; 
 1.99831 +}
 1.99832 +
 1.99833 +/*
 1.99834 +** Return a list of all triggers on table pTab if there exists at least
 1.99835 +** one trigger that must be fired when an operation of type 'op' is 
 1.99836 +** performed on the table, and, if that operation is an UPDATE, if at
 1.99837 +** least one of the columns in pChanges is being modified.
 1.99838 +*/
 1.99839 +SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
 1.99840 +  Parse *pParse,          /* Parse context */
 1.99841 +  Table *pTab,            /* The table the contains the triggers */
 1.99842 +  int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
 1.99843 +  ExprList *pChanges,     /* Columns that change in an UPDATE statement */
 1.99844 +  int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
 1.99845 +){
 1.99846 +  int mask = 0;
 1.99847 +  Trigger *pList = 0;
 1.99848 +  Trigger *p;
 1.99849 +
 1.99850 +  if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
 1.99851 +    pList = sqlite3TriggerList(pParse, pTab);
 1.99852 +  }
 1.99853 +  assert( pList==0 || IsVirtual(pTab)==0 );
 1.99854 +  for(p=pList; p; p=p->pNext){
 1.99855 +    if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
 1.99856 +      mask |= p->tr_tm;
 1.99857 +    }
 1.99858 +  }
 1.99859 +  if( pMask ){
 1.99860 +    *pMask = mask;
 1.99861 +  }
 1.99862 +  return (mask ? pList : 0);
 1.99863 +}
 1.99864 +
 1.99865 +/*
 1.99866 +** Convert the pStep->target token into a SrcList and return a pointer
 1.99867 +** to that SrcList.
 1.99868 +**
 1.99869 +** This routine adds a specific database name, if needed, to the target when
 1.99870 +** forming the SrcList.  This prevents a trigger in one database from
 1.99871 +** referring to a target in another database.  An exception is when the
 1.99872 +** trigger is in TEMP in which case it can refer to any other database it
 1.99873 +** wants.
 1.99874 +*/
 1.99875 +static SrcList *targetSrcList(
 1.99876 +  Parse *pParse,       /* The parsing context */
 1.99877 +  TriggerStep *pStep   /* The trigger containing the target token */
 1.99878 +){
 1.99879 +  int iDb;             /* Index of the database to use */
 1.99880 +  SrcList *pSrc;       /* SrcList to be returned */
 1.99881 +
 1.99882 +  pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
 1.99883 +  if( pSrc ){
 1.99884 +    assert( pSrc->nSrc>0 );
 1.99885 +    assert( pSrc->a!=0 );
 1.99886 +    iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
 1.99887 +    if( iDb==0 || iDb>=2 ){
 1.99888 +      sqlite3 *db = pParse->db;
 1.99889 +      assert( iDb<pParse->db->nDb );
 1.99890 +      pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
 1.99891 +    }
 1.99892 +  }
 1.99893 +  return pSrc;
 1.99894 +}
 1.99895 +
 1.99896 +/*
 1.99897 +** Generate VDBE code for the statements inside the body of a single 
 1.99898 +** trigger.
 1.99899 +*/
 1.99900 +static int codeTriggerProgram(
 1.99901 +  Parse *pParse,            /* The parser context */
 1.99902 +  TriggerStep *pStepList,   /* List of statements inside the trigger body */
 1.99903 +  int orconf                /* Conflict algorithm. (OE_Abort, etc) */  
 1.99904 +){
 1.99905 +  TriggerStep *pStep;
 1.99906 +  Vdbe *v = pParse->pVdbe;
 1.99907 +  sqlite3 *db = pParse->db;
 1.99908 +
 1.99909 +  assert( pParse->pTriggerTab && pParse->pToplevel );
 1.99910 +  assert( pStepList );
 1.99911 +  assert( v!=0 );
 1.99912 +  for(pStep=pStepList; pStep; pStep=pStep->pNext){
 1.99913 +    /* Figure out the ON CONFLICT policy that will be used for this step
 1.99914 +    ** of the trigger program. If the statement that caused this trigger
 1.99915 +    ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
 1.99916 +    ** the ON CONFLICT policy that was specified as part of the trigger
 1.99917 +    ** step statement. Example:
 1.99918 +    **
 1.99919 +    **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
 1.99920 +    **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
 1.99921 +    **   END;
 1.99922 +    **
 1.99923 +    **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
 1.99924 +    **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
 1.99925 +    */
 1.99926 +    pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
 1.99927 +
 1.99928 +    /* Clear the cookieGoto flag. When coding triggers, the cookieGoto 
 1.99929 +    ** variable is used as a flag to indicate to sqlite3ExprCodeConstants()
 1.99930 +    ** that it is not safe to refactor constants (this happens after the
 1.99931 +    ** start of the first loop in the SQL statement is coded - at that 
 1.99932 +    ** point code may be conditionally executed, so it is no longer safe to 
 1.99933 +    ** initialize constant register values).  */
 1.99934 +    assert( pParse->cookieGoto==0 || pParse->cookieGoto==-1 );
 1.99935 +    pParse->cookieGoto = 0;
 1.99936 +
 1.99937 +    switch( pStep->op ){
 1.99938 +      case TK_UPDATE: {
 1.99939 +        sqlite3Update(pParse, 
 1.99940 +          targetSrcList(pParse, pStep),
 1.99941 +          sqlite3ExprListDup(db, pStep->pExprList, 0), 
 1.99942 +          sqlite3ExprDup(db, pStep->pWhere, 0), 
 1.99943 +          pParse->eOrconf
 1.99944 +        );
 1.99945 +        break;
 1.99946 +      }
 1.99947 +      case TK_INSERT: {
 1.99948 +        sqlite3Insert(pParse, 
 1.99949 +          targetSrcList(pParse, pStep),
 1.99950 +          sqlite3ExprListDup(db, pStep->pExprList, 0), 
 1.99951 +          sqlite3SelectDup(db, pStep->pSelect, 0), 
 1.99952 +          sqlite3IdListDup(db, pStep->pIdList), 
 1.99953 +          pParse->eOrconf
 1.99954 +        );
 1.99955 +        break;
 1.99956 +      }
 1.99957 +      case TK_DELETE: {
 1.99958 +        sqlite3DeleteFrom(pParse, 
 1.99959 +          targetSrcList(pParse, pStep),
 1.99960 +          sqlite3ExprDup(db, pStep->pWhere, 0)
 1.99961 +        );
 1.99962 +        break;
 1.99963 +      }
 1.99964 +      default: assert( pStep->op==TK_SELECT ); {
 1.99965 +        SelectDest sDest;
 1.99966 +        Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
 1.99967 +        sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
 1.99968 +        sqlite3Select(pParse, pSelect, &sDest);
 1.99969 +        sqlite3SelectDelete(db, pSelect);
 1.99970 +        break;
 1.99971 +      }
 1.99972 +    } 
 1.99973 +    if( pStep->op!=TK_SELECT ){
 1.99974 +      sqlite3VdbeAddOp0(v, OP_ResetCount);
 1.99975 +    }
 1.99976 +  }
 1.99977 +
 1.99978 +  return 0;
 1.99979 +}
 1.99980 +
 1.99981 +#ifdef SQLITE_DEBUG
 1.99982 +/*
 1.99983 +** This function is used to add VdbeComment() annotations to a VDBE
 1.99984 +** program. It is not used in production code, only for debugging.
 1.99985 +*/
 1.99986 +static const char *onErrorText(int onError){
 1.99987 +  switch( onError ){
 1.99988 +    case OE_Abort:    return "abort";
 1.99989 +    case OE_Rollback: return "rollback";
 1.99990 +    case OE_Fail:     return "fail";
 1.99991 +    case OE_Replace:  return "replace";
 1.99992 +    case OE_Ignore:   return "ignore";
 1.99993 +    case OE_Default:  return "default";
 1.99994 +  }
 1.99995 +  return "n/a";
 1.99996 +}
 1.99997 +#endif
 1.99998 +
 1.99999 +/*
1.100000 +** Parse context structure pFrom has just been used to create a sub-vdbe
1.100001 +** (trigger program). If an error has occurred, transfer error information
1.100002 +** from pFrom to pTo.
1.100003 +*/
1.100004 +static void transferParseError(Parse *pTo, Parse *pFrom){
1.100005 +  assert( pFrom->zErrMsg==0 || pFrom->nErr );
1.100006 +  assert( pTo->zErrMsg==0 || pTo->nErr );
1.100007 +  if( pTo->nErr==0 ){
1.100008 +    pTo->zErrMsg = pFrom->zErrMsg;
1.100009 +    pTo->nErr = pFrom->nErr;
1.100010 +  }else{
1.100011 +    sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
1.100012 +  }
1.100013 +}
1.100014 +
1.100015 +/*
1.100016 +** Create and populate a new TriggerPrg object with a sub-program 
1.100017 +** implementing trigger pTrigger with ON CONFLICT policy orconf.
1.100018 +*/
1.100019 +static TriggerPrg *codeRowTrigger(
1.100020 +  Parse *pParse,       /* Current parse context */
1.100021 +  Trigger *pTrigger,   /* Trigger to code */
1.100022 +  Table *pTab,         /* The table pTrigger is attached to */
1.100023 +  int orconf           /* ON CONFLICT policy to code trigger program with */
1.100024 +){
1.100025 +  Parse *pTop = sqlite3ParseToplevel(pParse);
1.100026 +  sqlite3 *db = pParse->db;   /* Database handle */
1.100027 +  TriggerPrg *pPrg;           /* Value to return */
1.100028 +  Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
1.100029 +  Vdbe *v;                    /* Temporary VM */
1.100030 +  NameContext sNC;            /* Name context for sub-vdbe */
1.100031 +  SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
1.100032 +  Parse *pSubParse;           /* Parse context for sub-vdbe */
1.100033 +  int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
1.100034 +
1.100035 +  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
1.100036 +  assert( pTop->pVdbe );
1.100037 +
1.100038 +  /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
1.100039 +  ** are freed if an error occurs, link them into the Parse.pTriggerPrg 
1.100040 +  ** list of the top-level Parse object sooner rather than later.  */
1.100041 +  pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
1.100042 +  if( !pPrg ) return 0;
1.100043 +  pPrg->pNext = pTop->pTriggerPrg;
1.100044 +  pTop->pTriggerPrg = pPrg;
1.100045 +  pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
1.100046 +  if( !pProgram ) return 0;
1.100047 +  sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
1.100048 +  pPrg->pTrigger = pTrigger;
1.100049 +  pPrg->orconf = orconf;
1.100050 +  pPrg->aColmask[0] = 0xffffffff;
1.100051 +  pPrg->aColmask[1] = 0xffffffff;
1.100052 +
1.100053 +  /* Allocate and populate a new Parse context to use for coding the 
1.100054 +  ** trigger sub-program.  */
1.100055 +  pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
1.100056 +  if( !pSubParse ) return 0;
1.100057 +  memset(&sNC, 0, sizeof(sNC));
1.100058 +  sNC.pParse = pSubParse;
1.100059 +  pSubParse->db = db;
1.100060 +  pSubParse->pTriggerTab = pTab;
1.100061 +  pSubParse->pToplevel = pTop;
1.100062 +  pSubParse->zAuthContext = pTrigger->zName;
1.100063 +  pSubParse->eTriggerOp = pTrigger->op;
1.100064 +  pSubParse->nQueryLoop = pParse->nQueryLoop;
1.100065 +
1.100066 +  v = sqlite3GetVdbe(pSubParse);
1.100067 +  if( v ){
1.100068 +    VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 
1.100069 +      pTrigger->zName, onErrorText(orconf),
1.100070 +      (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
1.100071 +        (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
1.100072 +        (pTrigger->op==TK_INSERT ? "INSERT" : ""),
1.100073 +        (pTrigger->op==TK_DELETE ? "DELETE" : ""),
1.100074 +      pTab->zName
1.100075 +    ));
1.100076 +#ifndef SQLITE_OMIT_TRACE
1.100077 +    sqlite3VdbeChangeP4(v, -1, 
1.100078 +      sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
1.100079 +    );
1.100080 +#endif
1.100081 +
1.100082 +    /* If one was specified, code the WHEN clause. If it evaluates to false
1.100083 +    ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
1.100084 +    ** OP_Halt inserted at the end of the program.  */
1.100085 +    if( pTrigger->pWhen ){
1.100086 +      pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
1.100087 +      if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 
1.100088 +       && db->mallocFailed==0 
1.100089 +      ){
1.100090 +        iEndTrigger = sqlite3VdbeMakeLabel(v);
1.100091 +        sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
1.100092 +      }
1.100093 +      sqlite3ExprDelete(db, pWhen);
1.100094 +    }
1.100095 +
1.100096 +    /* Code the trigger program into the sub-vdbe. */
1.100097 +    codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
1.100098 +
1.100099 +    /* Insert an OP_Halt at the end of the sub-program. */
1.100100 +    if( iEndTrigger ){
1.100101 +      sqlite3VdbeResolveLabel(v, iEndTrigger);
1.100102 +    }
1.100103 +    sqlite3VdbeAddOp0(v, OP_Halt);
1.100104 +    VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
1.100105 +
1.100106 +    transferParseError(pParse, pSubParse);
1.100107 +    if( db->mallocFailed==0 ){
1.100108 +      pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
1.100109 +    }
1.100110 +    pProgram->nMem = pSubParse->nMem;
1.100111 +    pProgram->nCsr = pSubParse->nTab;
1.100112 +    pProgram->nOnce = pSubParse->nOnce;
1.100113 +    pProgram->token = (void *)pTrigger;
1.100114 +    pPrg->aColmask[0] = pSubParse->oldmask;
1.100115 +    pPrg->aColmask[1] = pSubParse->newmask;
1.100116 +    sqlite3VdbeDelete(v);
1.100117 +  }
1.100118 +
1.100119 +  assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
1.100120 +  assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
1.100121 +  sqlite3StackFree(db, pSubParse);
1.100122 +
1.100123 +  return pPrg;
1.100124 +}
1.100125 +    
1.100126 +/*
1.100127 +** Return a pointer to a TriggerPrg object containing the sub-program for
1.100128 +** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
1.100129 +** TriggerPrg object exists, a new object is allocated and populated before
1.100130 +** being returned.
1.100131 +*/
1.100132 +static TriggerPrg *getRowTrigger(
1.100133 +  Parse *pParse,       /* Current parse context */
1.100134 +  Trigger *pTrigger,   /* Trigger to code */
1.100135 +  Table *pTab,         /* The table trigger pTrigger is attached to */
1.100136 +  int orconf           /* ON CONFLICT algorithm. */
1.100137 +){
1.100138 +  Parse *pRoot = sqlite3ParseToplevel(pParse);
1.100139 +  TriggerPrg *pPrg;
1.100140 +
1.100141 +  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
1.100142 +
1.100143 +  /* It may be that this trigger has already been coded (or is in the
1.100144 +  ** process of being coded). If this is the case, then an entry with
1.100145 +  ** a matching TriggerPrg.pTrigger field will be present somewhere
1.100146 +  ** in the Parse.pTriggerPrg list. Search for such an entry.  */
1.100147 +  for(pPrg=pRoot->pTriggerPrg; 
1.100148 +      pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 
1.100149 +      pPrg=pPrg->pNext
1.100150 +  );
1.100151 +
1.100152 +  /* If an existing TriggerPrg could not be located, create a new one. */
1.100153 +  if( !pPrg ){
1.100154 +    pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
1.100155 +  }
1.100156 +
1.100157 +  return pPrg;
1.100158 +}
1.100159 +
1.100160 +/*
1.100161 +** Generate code for the trigger program associated with trigger p on 
1.100162 +** table pTab. The reg, orconf and ignoreJump parameters passed to this
1.100163 +** function are the same as those described in the header function for
1.100164 +** sqlite3CodeRowTrigger()
1.100165 +*/
1.100166 +SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
1.100167 +  Parse *pParse,       /* Parse context */
1.100168 +  Trigger *p,          /* Trigger to code */
1.100169 +  Table *pTab,         /* The table to code triggers from */
1.100170 +  int reg,             /* Reg array containing OLD.* and NEW.* values */
1.100171 +  int orconf,          /* ON CONFLICT policy */
1.100172 +  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
1.100173 +){
1.100174 +  Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
1.100175 +  TriggerPrg *pPrg;
1.100176 +  pPrg = getRowTrigger(pParse, p, pTab, orconf);
1.100177 +  assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
1.100178 +
1.100179 +  /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
1.100180 +  ** is a pointer to the sub-vdbe containing the trigger program.  */
1.100181 +  if( pPrg ){
1.100182 +    int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
1.100183 +
1.100184 +    sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
1.100185 +    sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
1.100186 +    VdbeComment(
1.100187 +        (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
1.100188 +
1.100189 +    /* Set the P5 operand of the OP_Program instruction to non-zero if
1.100190 +    ** recursive invocation of this trigger program is disallowed. Recursive
1.100191 +    ** invocation is disallowed if (a) the sub-program is really a trigger,
1.100192 +    ** not a foreign key action, and (b) the flag to enable recursive triggers
1.100193 +    ** is clear.  */
1.100194 +    sqlite3VdbeChangeP5(v, (u8)bRecursive);
1.100195 +  }
1.100196 +}
1.100197 +
1.100198 +/*
1.100199 +** This is called to code the required FOR EACH ROW triggers for an operation
1.100200 +** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
1.100201 +** is given by the op paramater. The tr_tm parameter determines whether the
1.100202 +** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
1.100203 +** parameter pChanges is passed the list of columns being modified.
1.100204 +**
1.100205 +** If there are no triggers that fire at the specified time for the specified
1.100206 +** operation on pTab, this function is a no-op.
1.100207 +**
1.100208 +** The reg argument is the address of the first in an array of registers 
1.100209 +** that contain the values substituted for the new.* and old.* references
1.100210 +** in the trigger program. If N is the number of columns in table pTab
1.100211 +** (a copy of pTab->nCol), then registers are populated as follows:
1.100212 +**
1.100213 +**   Register       Contains
1.100214 +**   ------------------------------------------------------
1.100215 +**   reg+0          OLD.rowid
1.100216 +**   reg+1          OLD.* value of left-most column of pTab
1.100217 +**   ...            ...
1.100218 +**   reg+N          OLD.* value of right-most column of pTab
1.100219 +**   reg+N+1        NEW.rowid
1.100220 +**   reg+N+2        OLD.* value of left-most column of pTab
1.100221 +**   ...            ...
1.100222 +**   reg+N+N+1      NEW.* value of right-most column of pTab
1.100223 +**
1.100224 +** For ON DELETE triggers, the registers containing the NEW.* values will
1.100225 +** never be accessed by the trigger program, so they are not allocated or 
1.100226 +** populated by the caller (there is no data to populate them with anyway). 
1.100227 +** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
1.100228 +** are never accessed, and so are not allocated by the caller. So, for an
1.100229 +** ON INSERT trigger, the value passed to this function as parameter reg
1.100230 +** is not a readable register, although registers (reg+N) through 
1.100231 +** (reg+N+N+1) are.
1.100232 +**
1.100233 +** Parameter orconf is the default conflict resolution algorithm for the
1.100234 +** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
1.100235 +** is the instruction that control should jump to if a trigger program
1.100236 +** raises an IGNORE exception.
1.100237 +*/
1.100238 +SQLITE_PRIVATE void sqlite3CodeRowTrigger(
1.100239 +  Parse *pParse,       /* Parse context */
1.100240 +  Trigger *pTrigger,   /* List of triggers on table pTab */
1.100241 +  int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
1.100242 +  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
1.100243 +  int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
1.100244 +  Table *pTab,         /* The table to code triggers from */
1.100245 +  int reg,             /* The first in an array of registers (see above) */
1.100246 +  int orconf,          /* ON CONFLICT policy */
1.100247 +  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
1.100248 +){
1.100249 +  Trigger *p;          /* Used to iterate through pTrigger list */
1.100250 +
1.100251 +  assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
1.100252 +  assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
1.100253 +  assert( (op==TK_UPDATE)==(pChanges!=0) );
1.100254 +
1.100255 +  for(p=pTrigger; p; p=p->pNext){
1.100256 +
1.100257 +    /* Sanity checking:  The schema for the trigger and for the table are
1.100258 +    ** always defined.  The trigger must be in the same schema as the table
1.100259 +    ** or else it must be a TEMP trigger. */
1.100260 +    assert( p->pSchema!=0 );
1.100261 +    assert( p->pTabSchema!=0 );
1.100262 +    assert( p->pSchema==p->pTabSchema 
1.100263 +         || p->pSchema==pParse->db->aDb[1].pSchema );
1.100264 +
1.100265 +    /* Determine whether we should code this trigger */
1.100266 +    if( p->op==op 
1.100267 +     && p->tr_tm==tr_tm 
1.100268 +     && checkColumnOverlap(p->pColumns, pChanges)
1.100269 +    ){
1.100270 +      sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
1.100271 +    }
1.100272 +  }
1.100273 +}
1.100274 +
1.100275 +/*
1.100276 +** Triggers may access values stored in the old.* or new.* pseudo-table. 
1.100277 +** This function returns a 32-bit bitmask indicating which columns of the 
1.100278 +** old.* or new.* tables actually are used by triggers. This information 
1.100279 +** may be used by the caller, for example, to avoid having to load the entire
1.100280 +** old.* record into memory when executing an UPDATE or DELETE command.
1.100281 +**
1.100282 +** Bit 0 of the returned mask is set if the left-most column of the
1.100283 +** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
1.100284 +** the second leftmost column value is required, and so on. If there
1.100285 +** are more than 32 columns in the table, and at least one of the columns
1.100286 +** with an index greater than 32 may be accessed, 0xffffffff is returned.
1.100287 +**
1.100288 +** It is not possible to determine if the old.rowid or new.rowid column is 
1.100289 +** accessed by triggers. The caller must always assume that it is.
1.100290 +**
1.100291 +** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
1.100292 +** applies to the old.* table. If 1, the new.* table.
1.100293 +**
1.100294 +** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
1.100295 +** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
1.100296 +** included in the returned mask if the TRIGGER_BEFORE bit is set in the
1.100297 +** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
1.100298 +** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
1.100299 +*/
1.100300 +SQLITE_PRIVATE u32 sqlite3TriggerColmask(
1.100301 +  Parse *pParse,       /* Parse context */
1.100302 +  Trigger *pTrigger,   /* List of triggers on table pTab */
1.100303 +  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
1.100304 +  int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
1.100305 +  int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
1.100306 +  Table *pTab,         /* The table to code triggers from */
1.100307 +  int orconf           /* Default ON CONFLICT policy for trigger steps */
1.100308 +){
1.100309 +  const int op = pChanges ? TK_UPDATE : TK_DELETE;
1.100310 +  u32 mask = 0;
1.100311 +  Trigger *p;
1.100312 +
1.100313 +  assert( isNew==1 || isNew==0 );
1.100314 +  for(p=pTrigger; p; p=p->pNext){
1.100315 +    if( p->op==op && (tr_tm&p->tr_tm)
1.100316 +     && checkColumnOverlap(p->pColumns,pChanges)
1.100317 +    ){
1.100318 +      TriggerPrg *pPrg;
1.100319 +      pPrg = getRowTrigger(pParse, p, pTab, orconf);
1.100320 +      if( pPrg ){
1.100321 +        mask |= pPrg->aColmask[isNew];
1.100322 +      }
1.100323 +    }
1.100324 +  }
1.100325 +
1.100326 +  return mask;
1.100327 +}
1.100328 +
1.100329 +#endif /* !defined(SQLITE_OMIT_TRIGGER) */
1.100330 +
1.100331 +/************** End of trigger.c *********************************************/
1.100332 +/************** Begin file update.c ******************************************/
1.100333 +/*
1.100334 +** 2001 September 15
1.100335 +**
1.100336 +** The author disclaims copyright to this source code.  In place of
1.100337 +** a legal notice, here is a blessing:
1.100338 +**
1.100339 +**    May you do good and not evil.
1.100340 +**    May you find forgiveness for yourself and forgive others.
1.100341 +**    May you share freely, never taking more than you give.
1.100342 +**
1.100343 +*************************************************************************
1.100344 +** This file contains C code routines that are called by the parser
1.100345 +** to handle UPDATE statements.
1.100346 +*/
1.100347 +
1.100348 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.100349 +/* Forward declaration */
1.100350 +static void updateVirtualTable(
1.100351 +  Parse *pParse,       /* The parsing context */
1.100352 +  SrcList *pSrc,       /* The virtual table to be modified */
1.100353 +  Table *pTab,         /* The virtual table */
1.100354 +  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
1.100355 +  Expr *pRowidExpr,    /* Expression used to recompute the rowid */
1.100356 +  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
1.100357 +  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
1.100358 +  int onError          /* ON CONFLICT strategy */
1.100359 +);
1.100360 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.100361 +
1.100362 +/*
1.100363 +** The most recently coded instruction was an OP_Column to retrieve the
1.100364 +** i-th column of table pTab. This routine sets the P4 parameter of the 
1.100365 +** OP_Column to the default value, if any.
1.100366 +**
1.100367 +** The default value of a column is specified by a DEFAULT clause in the 
1.100368 +** column definition. This was either supplied by the user when the table
1.100369 +** was created, or added later to the table definition by an ALTER TABLE
1.100370 +** command. If the latter, then the row-records in the table btree on disk
1.100371 +** may not contain a value for the column and the default value, taken
1.100372 +** from the P4 parameter of the OP_Column instruction, is returned instead.
1.100373 +** If the former, then all row-records are guaranteed to include a value
1.100374 +** for the column and the P4 value is not required.
1.100375 +**
1.100376 +** Column definitions created by an ALTER TABLE command may only have 
1.100377 +** literal default values specified: a number, null or a string. (If a more
1.100378 +** complicated default expression value was provided, it is evaluated 
1.100379 +** when the ALTER TABLE is executed and one of the literal values written
1.100380 +** into the sqlite_master table.)
1.100381 +**
1.100382 +** Therefore, the P4 parameter is only required if the default value for
1.100383 +** the column is a literal number, string or null. The sqlite3ValueFromExpr()
1.100384 +** function is capable of transforming these types of expressions into
1.100385 +** sqlite3_value objects.
1.100386 +**
1.100387 +** If parameter iReg is not negative, code an OP_RealAffinity instruction
1.100388 +** on register iReg. This is used when an equivalent integer value is 
1.100389 +** stored in place of an 8-byte floating point value in order to save 
1.100390 +** space.
1.100391 +*/
1.100392 +SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
1.100393 +  assert( pTab!=0 );
1.100394 +  if( !pTab->pSelect ){
1.100395 +    sqlite3_value *pValue;
1.100396 +    u8 enc = ENC(sqlite3VdbeDb(v));
1.100397 +    Column *pCol = &pTab->aCol[i];
1.100398 +    VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
1.100399 +    assert( i<pTab->nCol );
1.100400 +    sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
1.100401 +                         pCol->affinity, &pValue);
1.100402 +    if( pValue ){
1.100403 +      sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
1.100404 +    }
1.100405 +#ifndef SQLITE_OMIT_FLOATING_POINT
1.100406 +    if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
1.100407 +      sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
1.100408 +    }
1.100409 +#endif
1.100410 +  }
1.100411 +}
1.100412 +
1.100413 +/*
1.100414 +** Process an UPDATE statement.
1.100415 +**
1.100416 +**   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
1.100417 +**          \_______/ \________/     \______/       \________________/
1.100418 +*            onError   pTabList      pChanges             pWhere
1.100419 +*/
1.100420 +SQLITE_PRIVATE void sqlite3Update(
1.100421 +  Parse *pParse,         /* The parser context */
1.100422 +  SrcList *pTabList,     /* The table in which we should change things */
1.100423 +  ExprList *pChanges,    /* Things to be changed */
1.100424 +  Expr *pWhere,          /* The WHERE clause.  May be null */
1.100425 +  int onError            /* How to handle constraint errors */
1.100426 +){
1.100427 +  int i, j;              /* Loop counters */
1.100428 +  Table *pTab;           /* The table to be updated */
1.100429 +  int addr = 0;          /* VDBE instruction address of the start of the loop */
1.100430 +  WhereInfo *pWInfo;     /* Information about the WHERE clause */
1.100431 +  Vdbe *v;               /* The virtual database engine */
1.100432 +  Index *pIdx;           /* For looping over indices */
1.100433 +  int nIdx;              /* Number of indices that need updating */
1.100434 +  int iCur;              /* VDBE Cursor number of pTab */
1.100435 +  sqlite3 *db;           /* The database structure */
1.100436 +  int *aRegIdx = 0;      /* One register assigned to each index to be updated */
1.100437 +  int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
1.100438 +                         ** an expression for the i-th column of the table.
1.100439 +                         ** aXRef[i]==-1 if the i-th column is not changed. */
1.100440 +  int chngRowid;         /* True if the record number is being changed */
1.100441 +  Expr *pRowidExpr = 0;  /* Expression defining the new record number */
1.100442 +  int openAll = 0;       /* True if all indices need to be opened */
1.100443 +  AuthContext sContext;  /* The authorization context */
1.100444 +  NameContext sNC;       /* The name-context to resolve expressions in */
1.100445 +  int iDb;               /* Database containing the table being updated */
1.100446 +  int okOnePass;         /* True for one-pass algorithm without the FIFO */
1.100447 +  int hasFK;             /* True if foreign key processing is required */
1.100448 +
1.100449 +#ifndef SQLITE_OMIT_TRIGGER
1.100450 +  int isView;            /* True when updating a view (INSTEAD OF trigger) */
1.100451 +  Trigger *pTrigger;     /* List of triggers on pTab, if required */
1.100452 +  int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
1.100453 +#endif
1.100454 +  int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
1.100455 +
1.100456 +  /* Register Allocations */
1.100457 +  int regRowCount = 0;   /* A count of rows changed */
1.100458 +  int regOldRowid;       /* The old rowid */
1.100459 +  int regNewRowid;       /* The new rowid */
1.100460 +  int regNew;            /* Content of the NEW.* table in triggers */
1.100461 +  int regOld = 0;        /* Content of OLD.* table in triggers */
1.100462 +  int regRowSet = 0;     /* Rowset of rows to be updated */
1.100463 +
1.100464 +  memset(&sContext, 0, sizeof(sContext));
1.100465 +  db = pParse->db;
1.100466 +  if( pParse->nErr || db->mallocFailed ){
1.100467 +    goto update_cleanup;
1.100468 +  }
1.100469 +  assert( pTabList->nSrc==1 );
1.100470 +
1.100471 +  /* Locate the table which we want to update. 
1.100472 +  */
1.100473 +  pTab = sqlite3SrcListLookup(pParse, pTabList);
1.100474 +  if( pTab==0 ) goto update_cleanup;
1.100475 +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1.100476 +
1.100477 +  /* Figure out if we have any triggers and if the table being
1.100478 +  ** updated is a view.
1.100479 +  */
1.100480 +#ifndef SQLITE_OMIT_TRIGGER
1.100481 +  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
1.100482 +  isView = pTab->pSelect!=0;
1.100483 +  assert( pTrigger || tmask==0 );
1.100484 +#else
1.100485 +# define pTrigger 0
1.100486 +# define isView 0
1.100487 +# define tmask 0
1.100488 +#endif
1.100489 +#ifdef SQLITE_OMIT_VIEW
1.100490 +# undef isView
1.100491 +# define isView 0
1.100492 +#endif
1.100493 +
1.100494 +  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
1.100495 +    goto update_cleanup;
1.100496 +  }
1.100497 +  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
1.100498 +    goto update_cleanup;
1.100499 +  }
1.100500 +  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
1.100501 +  if( aXRef==0 ) goto update_cleanup;
1.100502 +  for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
1.100503 +
1.100504 +  /* Allocate a cursors for the main database table and for all indices.
1.100505 +  ** The index cursors might not be used, but if they are used they
1.100506 +  ** need to occur right after the database cursor.  So go ahead and
1.100507 +  ** allocate enough space, just in case.
1.100508 +  */
1.100509 +  pTabList->a[0].iCursor = iCur = pParse->nTab++;
1.100510 +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1.100511 +    pParse->nTab++;
1.100512 +  }
1.100513 +
1.100514 +  /* Initialize the name-context */
1.100515 +  memset(&sNC, 0, sizeof(sNC));
1.100516 +  sNC.pParse = pParse;
1.100517 +  sNC.pSrcList = pTabList;
1.100518 +
1.100519 +  /* Resolve the column names in all the expressions of the
1.100520 +  ** of the UPDATE statement.  Also find the column index
1.100521 +  ** for each column to be updated in the pChanges array.  For each
1.100522 +  ** column to be updated, make sure we have authorization to change
1.100523 +  ** that column.
1.100524 +  */
1.100525 +  chngRowid = 0;
1.100526 +  for(i=0; i<pChanges->nExpr; i++){
1.100527 +    if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
1.100528 +      goto update_cleanup;
1.100529 +    }
1.100530 +    for(j=0; j<pTab->nCol; j++){
1.100531 +      if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
1.100532 +        if( j==pTab->iPKey ){
1.100533 +          chngRowid = 1;
1.100534 +          pRowidExpr = pChanges->a[i].pExpr;
1.100535 +        }
1.100536 +        aXRef[j] = i;
1.100537 +        break;
1.100538 +      }
1.100539 +    }
1.100540 +    if( j>=pTab->nCol ){
1.100541 +      if( sqlite3IsRowid(pChanges->a[i].zName) ){
1.100542 +        chngRowid = 1;
1.100543 +        pRowidExpr = pChanges->a[i].pExpr;
1.100544 +      }else{
1.100545 +        sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
1.100546 +        pParse->checkSchema = 1;
1.100547 +        goto update_cleanup;
1.100548 +      }
1.100549 +    }
1.100550 +#ifndef SQLITE_OMIT_AUTHORIZATION
1.100551 +    {
1.100552 +      int rc;
1.100553 +      rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
1.100554 +                           pTab->aCol[j].zName, db->aDb[iDb].zName);
1.100555 +      if( rc==SQLITE_DENY ){
1.100556 +        goto update_cleanup;
1.100557 +      }else if( rc==SQLITE_IGNORE ){
1.100558 +        aXRef[j] = -1;
1.100559 +      }
1.100560 +    }
1.100561 +#endif
1.100562 +  }
1.100563 +
1.100564 +  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
1.100565 +
1.100566 +  /* Allocate memory for the array aRegIdx[].  There is one entry in the
1.100567 +  ** array for each index associated with table being updated.  Fill in
1.100568 +  ** the value with a register number for indices that are to be used
1.100569 +  ** and with zero for unused indices.
1.100570 +  */
1.100571 +  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
1.100572 +  if( nIdx>0 ){
1.100573 +    aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
1.100574 +    if( aRegIdx==0 ) goto update_cleanup;
1.100575 +  }
1.100576 +  for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1.100577 +    int reg;
1.100578 +    if( hasFK || chngRowid ){
1.100579 +      reg = ++pParse->nMem;
1.100580 +    }else{
1.100581 +      reg = 0;
1.100582 +      for(i=0; i<pIdx->nColumn; i++){
1.100583 +        if( aXRef[pIdx->aiColumn[i]]>=0 ){
1.100584 +          reg = ++pParse->nMem;
1.100585 +          break;
1.100586 +        }
1.100587 +      }
1.100588 +    }
1.100589 +    aRegIdx[j] = reg;
1.100590 +  }
1.100591 +
1.100592 +  /* Begin generating code. */
1.100593 +  v = sqlite3GetVdbe(pParse);
1.100594 +  if( v==0 ) goto update_cleanup;
1.100595 +  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
1.100596 +  sqlite3BeginWriteOperation(pParse, 1, iDb);
1.100597 +
1.100598 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.100599 +  /* Virtual tables must be handled separately */
1.100600 +  if( IsVirtual(pTab) ){
1.100601 +    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
1.100602 +                       pWhere, onError);
1.100603 +    pWhere = 0;
1.100604 +    pTabList = 0;
1.100605 +    goto update_cleanup;
1.100606 +  }
1.100607 +#endif
1.100608 +
1.100609 +  /* Allocate required registers. */
1.100610 +  regRowSet = ++pParse->nMem;
1.100611 +  regOldRowid = regNewRowid = ++pParse->nMem;
1.100612 +  if( pTrigger || hasFK ){
1.100613 +    regOld = pParse->nMem + 1;
1.100614 +    pParse->nMem += pTab->nCol;
1.100615 +  }
1.100616 +  if( chngRowid || pTrigger || hasFK ){
1.100617 +    regNewRowid = ++pParse->nMem;
1.100618 +  }
1.100619 +  regNew = pParse->nMem + 1;
1.100620 +  pParse->nMem += pTab->nCol;
1.100621 +
1.100622 +  /* Start the view context. */
1.100623 +  if( isView ){
1.100624 +    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
1.100625 +  }
1.100626 +
1.100627 +  /* If we are trying to update a view, realize that view into
1.100628 +  ** a ephemeral table.
1.100629 +  */
1.100630 +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
1.100631 +  if( isView ){
1.100632 +    sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
1.100633 +  }
1.100634 +#endif
1.100635 +
1.100636 +  /* Resolve the column names in all the expressions in the
1.100637 +  ** WHERE clause.
1.100638 +  */
1.100639 +  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
1.100640 +    goto update_cleanup;
1.100641 +  }
1.100642 +
1.100643 +  /* Begin the database scan
1.100644 +  */
1.100645 +  sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
1.100646 +  pWInfo = sqlite3WhereBegin(
1.100647 +      pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0
1.100648 +  );
1.100649 +  if( pWInfo==0 ) goto update_cleanup;
1.100650 +  okOnePass = pWInfo->okOnePass;
1.100651 +
1.100652 +  /* Remember the rowid of every item to be updated.
1.100653 +  */
1.100654 +  sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
1.100655 +  if( !okOnePass ){
1.100656 +    sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
1.100657 +  }
1.100658 +
1.100659 +  /* End the database scan loop.
1.100660 +  */
1.100661 +  sqlite3WhereEnd(pWInfo);
1.100662 +
1.100663 +  /* Initialize the count of updated rows
1.100664 +  */
1.100665 +  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
1.100666 +    regRowCount = ++pParse->nMem;
1.100667 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
1.100668 +  }
1.100669 +
1.100670 +  if( !isView ){
1.100671 +    /* 
1.100672 +    ** Open every index that needs updating.  Note that if any
1.100673 +    ** index could potentially invoke a REPLACE conflict resolution 
1.100674 +    ** action, then we need to open all indices because we might need
1.100675 +    ** to be deleting some records.
1.100676 +    */
1.100677 +    if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 
1.100678 +    if( onError==OE_Replace ){
1.100679 +      openAll = 1;
1.100680 +    }else{
1.100681 +      openAll = 0;
1.100682 +      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1.100683 +        if( pIdx->onError==OE_Replace ){
1.100684 +          openAll = 1;
1.100685 +          break;
1.100686 +        }
1.100687 +      }
1.100688 +    }
1.100689 +    for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1.100690 +      assert( aRegIdx );
1.100691 +      if( openAll || aRegIdx[i]>0 ){
1.100692 +        KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
1.100693 +        sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
1.100694 +                       (char*)pKey, P4_KEYINFO_HANDOFF);
1.100695 +        assert( pParse->nTab>iCur+i+1 );
1.100696 +      }
1.100697 +    }
1.100698 +  }
1.100699 +
1.100700 +  /* Top of the update loop */
1.100701 +  if( okOnePass ){
1.100702 +    int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
1.100703 +    addr = sqlite3VdbeAddOp0(v, OP_Goto);
1.100704 +    sqlite3VdbeJumpHere(v, a1);
1.100705 +  }else{
1.100706 +    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
1.100707 +  }
1.100708 +
1.100709 +  /* Make cursor iCur point to the record that is being updated. If
1.100710 +  ** this record does not exist for some reason (deleted by a trigger,
1.100711 +  ** for example, then jump to the next iteration of the RowSet loop.  */
1.100712 +  sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
1.100713 +
1.100714 +  /* If the record number will change, set register regNewRowid to
1.100715 +  ** contain the new value. If the record number is not being modified,
1.100716 +  ** then regNewRowid is the same register as regOldRowid, which is
1.100717 +  ** already populated.  */
1.100718 +  assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
1.100719 +  if( chngRowid ){
1.100720 +    sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
1.100721 +    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
1.100722 +  }
1.100723 +
1.100724 +  /* If there are triggers on this table, populate an array of registers 
1.100725 +  ** with the required old.* column data.  */
1.100726 +  if( hasFK || pTrigger ){
1.100727 +    u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
1.100728 +    oldmask |= sqlite3TriggerColmask(pParse, 
1.100729 +        pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
1.100730 +    );
1.100731 +    for(i=0; i<pTab->nCol; i++){
1.100732 +      if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
1.100733 +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
1.100734 +      }else{
1.100735 +        sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
1.100736 +      }
1.100737 +    }
1.100738 +    if( chngRowid==0 ){
1.100739 +      sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
1.100740 +    }
1.100741 +  }
1.100742 +
1.100743 +  /* Populate the array of registers beginning at regNew with the new
1.100744 +  ** row data. This array is used to check constaints, create the new
1.100745 +  ** table and index records, and as the values for any new.* references
1.100746 +  ** made by triggers.
1.100747 +  **
1.100748 +  ** If there are one or more BEFORE triggers, then do not populate the
1.100749 +  ** registers associated with columns that are (a) not modified by
1.100750 +  ** this UPDATE statement and (b) not accessed by new.* references. The
1.100751 +  ** values for registers not modified by the UPDATE must be reloaded from 
1.100752 +  ** the database after the BEFORE triggers are fired anyway (as the trigger 
1.100753 +  ** may have modified them). So not loading those that are not going to
1.100754 +  ** be used eliminates some redundant opcodes.
1.100755 +  */
1.100756 +  newmask = sqlite3TriggerColmask(
1.100757 +      pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
1.100758 +  );
1.100759 +  sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);
1.100760 +  for(i=0; i<pTab->nCol; i++){
1.100761 +    if( i==pTab->iPKey ){
1.100762 +      /*sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);*/
1.100763 +    }else{
1.100764 +      j = aXRef[i];
1.100765 +      if( j>=0 ){
1.100766 +        sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
1.100767 +      }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
1.100768 +        /* This branch loads the value of a column that will not be changed 
1.100769 +        ** into a register. This is done if there are no BEFORE triggers, or
1.100770 +        ** if there are one or more BEFORE triggers that use this value via
1.100771 +        ** a new.* reference in a trigger program.
1.100772 +        */
1.100773 +        testcase( i==31 );
1.100774 +        testcase( i==32 );
1.100775 +        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
1.100776 +        sqlite3ColumnDefault(v, pTab, i, regNew+i);
1.100777 +      }
1.100778 +    }
1.100779 +  }
1.100780 +
1.100781 +  /* Fire any BEFORE UPDATE triggers. This happens before constraints are
1.100782 +  ** verified. One could argue that this is wrong.
1.100783 +  */
1.100784 +  if( tmask&TRIGGER_BEFORE ){
1.100785 +    sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
1.100786 +    sqlite3TableAffinityStr(v, pTab);
1.100787 +    sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
1.100788 +        TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
1.100789 +
1.100790 +    /* The row-trigger may have deleted the row being updated. In this
1.100791 +    ** case, jump to the next row. No updates or AFTER triggers are 
1.100792 +    ** required. This behaviour - what happens when the row being updated
1.100793 +    ** is deleted or renamed by a BEFORE trigger - is left undefined in the
1.100794 +    ** documentation.
1.100795 +    */
1.100796 +    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
1.100797 +
1.100798 +    /* If it did not delete it, the row-trigger may still have modified 
1.100799 +    ** some of the columns of the row being updated. Load the values for 
1.100800 +    ** all columns not modified by the update statement into their 
1.100801 +    ** registers in case this has happened.
1.100802 +    */
1.100803 +    for(i=0; i<pTab->nCol; i++){
1.100804 +      if( aXRef[i]<0 && i!=pTab->iPKey ){
1.100805 +        sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
1.100806 +        sqlite3ColumnDefault(v, pTab, i, regNew+i);
1.100807 +      }
1.100808 +    }
1.100809 +  }
1.100810 +
1.100811 +  if( !isView ){
1.100812 +    int j1;                       /* Address of jump instruction */
1.100813 +
1.100814 +    /* Do constraint checks. */
1.100815 +    sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
1.100816 +        aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
1.100817 +
1.100818 +    /* Do FK constraint checks. */
1.100819 +    if( hasFK ){
1.100820 +      sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
1.100821 +    }
1.100822 +
1.100823 +    /* Delete the index entries associated with the current record.  */
1.100824 +    j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
1.100825 +    sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
1.100826 +  
1.100827 +    /* If changing the record number, delete the old record.  */
1.100828 +    if( hasFK || chngRowid ){
1.100829 +      sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
1.100830 +    }
1.100831 +    sqlite3VdbeJumpHere(v, j1);
1.100832 +
1.100833 +    if( hasFK ){
1.100834 +      sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
1.100835 +    }
1.100836 +  
1.100837 +    /* Insert the new index entries and the new record. */
1.100838 +    sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
1.100839 +
1.100840 +    /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
1.100841 +    ** handle rows (possibly in other tables) that refer via a foreign key
1.100842 +    ** to the row just updated. */ 
1.100843 +    if( hasFK ){
1.100844 +      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
1.100845 +    }
1.100846 +  }
1.100847 +
1.100848 +  /* Increment the row counter 
1.100849 +  */
1.100850 +  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
1.100851 +    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
1.100852 +  }
1.100853 +
1.100854 +  sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
1.100855 +      TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
1.100856 +
1.100857 +  /* Repeat the above with the next record to be updated, until
1.100858 +  ** all record selected by the WHERE clause have been updated.
1.100859 +  */
1.100860 +  sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
1.100861 +  sqlite3VdbeJumpHere(v, addr);
1.100862 +
1.100863 +  /* Close all tables */
1.100864 +  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1.100865 +    assert( aRegIdx );
1.100866 +    if( openAll || aRegIdx[i]>0 ){
1.100867 +      sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
1.100868 +    }
1.100869 +  }
1.100870 +  sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
1.100871 +
1.100872 +  /* Update the sqlite_sequence table by storing the content of the
1.100873 +  ** maximum rowid counter values recorded while inserting into
1.100874 +  ** autoincrement tables.
1.100875 +  */
1.100876 +  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
1.100877 +    sqlite3AutoincrementEnd(pParse);
1.100878 +  }
1.100879 +
1.100880 +  /*
1.100881 +  ** Return the number of rows that were changed. If this routine is 
1.100882 +  ** generating code because of a call to sqlite3NestedParse(), do not
1.100883 +  ** invoke the callback function.
1.100884 +  */
1.100885 +  if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
1.100886 +    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
1.100887 +    sqlite3VdbeSetNumCols(v, 1);
1.100888 +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
1.100889 +  }
1.100890 +
1.100891 +update_cleanup:
1.100892 +  sqlite3AuthContextPop(&sContext);
1.100893 +  sqlite3DbFree(db, aRegIdx);
1.100894 +  sqlite3DbFree(db, aXRef);
1.100895 +  sqlite3SrcListDelete(db, pTabList);
1.100896 +  sqlite3ExprListDelete(db, pChanges);
1.100897 +  sqlite3ExprDelete(db, pWhere);
1.100898 +  return;
1.100899 +}
1.100900 +/* Make sure "isView" and other macros defined above are undefined. Otherwise
1.100901 +** thely may interfere with compilation of other functions in this file
1.100902 +** (or in another file, if this file becomes part of the amalgamation).  */
1.100903 +#ifdef isView
1.100904 + #undef isView
1.100905 +#endif
1.100906 +#ifdef pTrigger
1.100907 + #undef pTrigger
1.100908 +#endif
1.100909 +
1.100910 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.100911 +/*
1.100912 +** Generate code for an UPDATE of a virtual table.
1.100913 +**
1.100914 +** The strategy is that we create an ephemerial table that contains
1.100915 +** for each row to be changed:
1.100916 +**
1.100917 +**   (A)  The original rowid of that row.
1.100918 +**   (B)  The revised rowid for the row. (note1)
1.100919 +**   (C)  The content of every column in the row.
1.100920 +**
1.100921 +** Then we loop over this ephemeral table and for each row in
1.100922 +** the ephermeral table call VUpdate.
1.100923 +**
1.100924 +** When finished, drop the ephemeral table.
1.100925 +**
1.100926 +** (note1) Actually, if we know in advance that (A) is always the same
1.100927 +** as (B) we only store (A), then duplicate (A) when pulling
1.100928 +** it out of the ephemeral table before calling VUpdate.
1.100929 +*/
1.100930 +static void updateVirtualTable(
1.100931 +  Parse *pParse,       /* The parsing context */
1.100932 +  SrcList *pSrc,       /* The virtual table to be modified */
1.100933 +  Table *pTab,         /* The virtual table */
1.100934 +  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
1.100935 +  Expr *pRowid,        /* Expression used to recompute the rowid */
1.100936 +  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
1.100937 +  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
1.100938 +  int onError          /* ON CONFLICT strategy */
1.100939 +){
1.100940 +  Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
1.100941 +  ExprList *pEList = 0;     /* The result set of the SELECT statement */
1.100942 +  Select *pSelect = 0;      /* The SELECT statement */
1.100943 +  Expr *pExpr;              /* Temporary expression */
1.100944 +  int ephemTab;             /* Table holding the result of the SELECT */
1.100945 +  int i;                    /* Loop counter */
1.100946 +  int addr;                 /* Address of top of loop */
1.100947 +  int iReg;                 /* First register in set passed to OP_VUpdate */
1.100948 +  sqlite3 *db = pParse->db; /* Database connection */
1.100949 +  const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
1.100950 +  SelectDest dest;
1.100951 +
1.100952 +  /* Construct the SELECT statement that will find the new values for
1.100953 +  ** all updated rows. 
1.100954 +  */
1.100955 +  pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
1.100956 +  if( pRowid ){
1.100957 +    pEList = sqlite3ExprListAppend(pParse, pEList,
1.100958 +                                   sqlite3ExprDup(db, pRowid, 0));
1.100959 +  }
1.100960 +  assert( pTab->iPKey<0 );
1.100961 +  for(i=0; i<pTab->nCol; i++){
1.100962 +    if( aXRef[i]>=0 ){
1.100963 +      pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
1.100964 +    }else{
1.100965 +      pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
1.100966 +    }
1.100967 +    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
1.100968 +  }
1.100969 +  pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
1.100970 +  
1.100971 +  /* Create the ephemeral table into which the update results will
1.100972 +  ** be stored.
1.100973 +  */
1.100974 +  assert( v );
1.100975 +  ephemTab = pParse->nTab++;
1.100976 +  sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
1.100977 +  sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
1.100978 +
1.100979 +  /* fill the ephemeral table 
1.100980 +  */
1.100981 +  sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
1.100982 +  sqlite3Select(pParse, pSelect, &dest);
1.100983 +
1.100984 +  /* Generate code to scan the ephemeral table and call VUpdate. */
1.100985 +  iReg = ++pParse->nMem;
1.100986 +  pParse->nMem += pTab->nCol+1;
1.100987 +  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
1.100988 +  sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
1.100989 +  sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
1.100990 +  for(i=0; i<pTab->nCol; i++){
1.100991 +    sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
1.100992 +  }
1.100993 +  sqlite3VtabMakeWritable(pParse, pTab);
1.100994 +  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
1.100995 +  sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
1.100996 +  sqlite3MayAbort(pParse);
1.100997 +  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
1.100998 +  sqlite3VdbeJumpHere(v, addr);
1.100999 +  sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
1.101000 +
1.101001 +  /* Cleanup */
1.101002 +  sqlite3SelectDelete(db, pSelect);  
1.101003 +}
1.101004 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.101005 +
1.101006 +/************** End of update.c **********************************************/
1.101007 +/************** Begin file vacuum.c ******************************************/
1.101008 +/*
1.101009 +** 2003 April 6
1.101010 +**
1.101011 +** The author disclaims copyright to this source code.  In place of
1.101012 +** a legal notice, here is a blessing:
1.101013 +**
1.101014 +**    May you do good and not evil.
1.101015 +**    May you find forgiveness for yourself and forgive others.
1.101016 +**    May you share freely, never taking more than you give.
1.101017 +**
1.101018 +*************************************************************************
1.101019 +** This file contains code used to implement the VACUUM command.
1.101020 +**
1.101021 +** Most of the code in this file may be omitted by defining the
1.101022 +** SQLITE_OMIT_VACUUM macro.
1.101023 +*/
1.101024 +
1.101025 +#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
1.101026 +/*
1.101027 +** Finalize a prepared statement.  If there was an error, store the
1.101028 +** text of the error message in *pzErrMsg.  Return the result code.
1.101029 +*/
1.101030 +static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
1.101031 +  int rc;
1.101032 +  rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
1.101033 +  if( rc ){
1.101034 +    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
1.101035 +  }
1.101036 +  return rc;
1.101037 +}
1.101038 +
1.101039 +/*
1.101040 +** Execute zSql on database db. Return an error code.
1.101041 +*/
1.101042 +static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
1.101043 +  sqlite3_stmt *pStmt;
1.101044 +  VVA_ONLY( int rc; )
1.101045 +  if( !zSql ){
1.101046 +    return SQLITE_NOMEM;
1.101047 +  }
1.101048 +  if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
1.101049 +    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
1.101050 +    return sqlite3_errcode(db);
1.101051 +  }
1.101052 +  VVA_ONLY( rc = ) sqlite3_step(pStmt);
1.101053 +  assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
1.101054 +  return vacuumFinalize(db, pStmt, pzErrMsg);
1.101055 +}
1.101056 +
1.101057 +/*
1.101058 +** Execute zSql on database db. The statement returns exactly
1.101059 +** one column. Execute this as SQL on the same database.
1.101060 +*/
1.101061 +static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
1.101062 +  sqlite3_stmt *pStmt;
1.101063 +  int rc;
1.101064 +
1.101065 +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
1.101066 +  if( rc!=SQLITE_OK ) return rc;
1.101067 +
1.101068 +  while( SQLITE_ROW==sqlite3_step(pStmt) ){
1.101069 +    rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
1.101070 +    if( rc!=SQLITE_OK ){
1.101071 +      vacuumFinalize(db, pStmt, pzErrMsg);
1.101072 +      return rc;
1.101073 +    }
1.101074 +  }
1.101075 +
1.101076 +  return vacuumFinalize(db, pStmt, pzErrMsg);
1.101077 +}
1.101078 +
1.101079 +/*
1.101080 +** The non-standard VACUUM command is used to clean up the database,
1.101081 +** collapse free space, etc.  It is modelled after the VACUUM command
1.101082 +** in PostgreSQL.
1.101083 +**
1.101084 +** In version 1.0.x of SQLite, the VACUUM command would call
1.101085 +** gdbm_reorganize() on all the database tables.  But beginning
1.101086 +** with 2.0.0, SQLite no longer uses GDBM so this command has
1.101087 +** become a no-op.
1.101088 +*/
1.101089 +SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
1.101090 +  Vdbe *v = sqlite3GetVdbe(pParse);
1.101091 +  if( v ){
1.101092 +    sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
1.101093 +    sqlite3VdbeUsesBtree(v, 0);
1.101094 +  }
1.101095 +  return;
1.101096 +}
1.101097 +
1.101098 +/*
1.101099 +** This routine implements the OP_Vacuum opcode of the VDBE.
1.101100 +*/
1.101101 +SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
1.101102 +  int rc = SQLITE_OK;     /* Return code from service routines */
1.101103 +  Btree *pMain;           /* The database being vacuumed */
1.101104 +  Btree *pTemp;           /* The temporary database we vacuum into */
1.101105 +  char *zSql = 0;         /* SQL statements */
1.101106 +  int saved_flags;        /* Saved value of the db->flags */
1.101107 +  int saved_nChange;      /* Saved value of db->nChange */
1.101108 +  int saved_nTotalChange; /* Saved value of db->nTotalChange */
1.101109 +  void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
1.101110 +  Db *pDb = 0;            /* Database to detach at end of vacuum */
1.101111 +  int isMemDb;            /* True if vacuuming a :memory: database */
1.101112 +  int nRes;               /* Bytes of reserved space at the end of each page */
1.101113 +  int nDb;                /* Number of attached databases */
1.101114 +
1.101115 +  if( !db->autoCommit ){
1.101116 +    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
1.101117 +    return SQLITE_ERROR;
1.101118 +  }
1.101119 +  if( db->activeVdbeCnt>1 ){
1.101120 +    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
1.101121 +    return SQLITE_ERROR;
1.101122 +  }
1.101123 +
1.101124 +  /* Save the current value of the database flags so that it can be 
1.101125 +  ** restored before returning. Then set the writable-schema flag, and
1.101126 +  ** disable CHECK and foreign key constraints.  */
1.101127 +  saved_flags = db->flags;
1.101128 +  saved_nChange = db->nChange;
1.101129 +  saved_nTotalChange = db->nTotalChange;
1.101130 +  saved_xTrace = db->xTrace;
1.101131 +  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
1.101132 +  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
1.101133 +  db->xTrace = 0;
1.101134 +
1.101135 +  pMain = db->aDb[0].pBt;
1.101136 +  isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
1.101137 +
1.101138 +  /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
1.101139 +  ** can be set to 'off' for this file, as it is not recovered if a crash
1.101140 +  ** occurs anyway. The integrity of the database is maintained by a
1.101141 +  ** (possibly synchronous) transaction opened on the main database before
1.101142 +  ** sqlite3BtreeCopyFile() is called.
1.101143 +  **
1.101144 +  ** An optimisation would be to use a non-journaled pager.
1.101145 +  ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
1.101146 +  ** that actually made the VACUUM run slower.  Very little journalling
1.101147 +  ** actually occurs when doing a vacuum since the vacuum_db is initially
1.101148 +  ** empty.  Only the journal header is written.  Apparently it takes more
1.101149 +  ** time to parse and run the PRAGMA to turn journalling off than it does
1.101150 +  ** to write the journal header file.
1.101151 +  */
1.101152 +  nDb = db->nDb;
1.101153 +  if( sqlite3TempInMemory(db) ){
1.101154 +    zSql = "ATTACH ':memory:' AS vacuum_db;";
1.101155 +  }else{
1.101156 +    zSql = "ATTACH '' AS vacuum_db;";
1.101157 +  }
1.101158 +  rc = execSql(db, pzErrMsg, zSql);
1.101159 +  if( db->nDb>nDb ){
1.101160 +    pDb = &db->aDb[db->nDb-1];
1.101161 +    assert( strcmp(pDb->zName,"vacuum_db")==0 );
1.101162 +  }
1.101163 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101164 +  pTemp = db->aDb[db->nDb-1].pBt;
1.101165 +
1.101166 +  /* The call to execSql() to attach the temp database has left the file
1.101167 +  ** locked (as there was more than one active statement when the transaction
1.101168 +  ** to read the schema was concluded. Unlock it here so that this doesn't
1.101169 +  ** cause problems for the call to BtreeSetPageSize() below.  */
1.101170 +  sqlite3BtreeCommit(pTemp);
1.101171 +
1.101172 +  nRes = sqlite3BtreeGetReserve(pMain);
1.101173 +
1.101174 +  /* A VACUUM cannot change the pagesize of an encrypted database. */
1.101175 +#ifdef SQLITE_HAS_CODEC
1.101176 +  if( db->nextPagesize ){
1.101177 +    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
1.101178 +    int nKey;
1.101179 +    char *zKey;
1.101180 +    sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
1.101181 +    if( nKey ) db->nextPagesize = 0;
1.101182 +  }
1.101183 +#endif
1.101184 +
1.101185 +  rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
1.101186 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101187 +
1.101188 +  /* Begin a transaction and take an exclusive lock on the main database
1.101189 +  ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
1.101190 +  ** to ensure that we do not try to change the page-size on a WAL database.
1.101191 +  */
1.101192 +  rc = execSql(db, pzErrMsg, "BEGIN;");
1.101193 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101194 +  rc = sqlite3BtreeBeginTrans(pMain, 2);
1.101195 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101196 +
1.101197 +  /* Do not attempt to change the page size for a WAL database */
1.101198 +  if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
1.101199 +                                               ==PAGER_JOURNALMODE_WAL ){
1.101200 +    db->nextPagesize = 0;
1.101201 +  }
1.101202 +
1.101203 +  if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
1.101204 +   || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
1.101205 +   || NEVER(db->mallocFailed)
1.101206 +  ){
1.101207 +    rc = SQLITE_NOMEM;
1.101208 +    goto end_of_vacuum;
1.101209 +  }
1.101210 +
1.101211 +#ifndef SQLITE_OMIT_AUTOVACUUM
1.101212 +  sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
1.101213 +                                           sqlite3BtreeGetAutoVacuum(pMain));
1.101214 +#endif
1.101215 +
1.101216 +  /* Query the schema of the main database. Create a mirror schema
1.101217 +  ** in the temporary database.
1.101218 +  */
1.101219 +  rc = execExecSql(db, pzErrMsg,
1.101220 +      "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
1.101221 +      "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
1.101222 +      "   AND rootpage>0"
1.101223 +  );
1.101224 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101225 +  rc = execExecSql(db, pzErrMsg,
1.101226 +      "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
1.101227 +      "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
1.101228 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101229 +  rc = execExecSql(db, pzErrMsg,
1.101230 +      "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
1.101231 +      "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
1.101232 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101233 +
1.101234 +  /* Loop through the tables in the main database. For each, do
1.101235 +  ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
1.101236 +  ** the contents to the temporary database.
1.101237 +  */
1.101238 +  rc = execExecSql(db, pzErrMsg,
1.101239 +      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
1.101240 +      "|| ' SELECT * FROM main.' || quote(name) || ';'"
1.101241 +      "FROM main.sqlite_master "
1.101242 +      "WHERE type = 'table' AND name!='sqlite_sequence' "
1.101243 +      "  AND rootpage>0"
1.101244 +  );
1.101245 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101246 +
1.101247 +  /* Copy over the sequence table
1.101248 +  */
1.101249 +  rc = execExecSql(db, pzErrMsg,
1.101250 +      "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
1.101251 +      "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
1.101252 +  );
1.101253 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101254 +  rc = execExecSql(db, pzErrMsg,
1.101255 +      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
1.101256 +      "|| ' SELECT * FROM main.' || quote(name) || ';' "
1.101257 +      "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
1.101258 +  );
1.101259 +  if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101260 +
1.101261 +
1.101262 +  /* Copy the triggers, views, and virtual tables from the main database
1.101263 +  ** over to the temporary database.  None of these objects has any
1.101264 +  ** associated storage, so all we have to do is copy their entries
1.101265 +  ** from the SQLITE_MASTER table.
1.101266 +  */
1.101267 +  rc = execSql(db, pzErrMsg,
1.101268 +      "INSERT INTO vacuum_db.sqlite_master "
1.101269 +      "  SELECT type, name, tbl_name, rootpage, sql"
1.101270 +      "    FROM main.sqlite_master"
1.101271 +      "   WHERE type='view' OR type='trigger'"
1.101272 +      "      OR (type='table' AND rootpage=0)"
1.101273 +  );
1.101274 +  if( rc ) goto end_of_vacuum;
1.101275 +
1.101276 +  /* At this point, there is a write transaction open on both the 
1.101277 +  ** vacuum database and the main database. Assuming no error occurs,
1.101278 +  ** both transactions are closed by this block - the main database
1.101279 +  ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
1.101280 +  ** call to sqlite3BtreeCommit().
1.101281 +  */
1.101282 +  {
1.101283 +    u32 meta;
1.101284 +    int i;
1.101285 +
1.101286 +    /* This array determines which meta meta values are preserved in the
1.101287 +    ** vacuum.  Even entries are the meta value number and odd entries
1.101288 +    ** are an increment to apply to the meta value after the vacuum.
1.101289 +    ** The increment is used to increase the schema cookie so that other
1.101290 +    ** connections to the same database will know to reread the schema.
1.101291 +    */
1.101292 +    static const unsigned char aCopy[] = {
1.101293 +       BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
1.101294 +       BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
1.101295 +       BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
1.101296 +       BTREE_USER_VERSION,       0,  /* Preserve the user version */
1.101297 +    };
1.101298 +
1.101299 +    assert( 1==sqlite3BtreeIsInTrans(pTemp) );
1.101300 +    assert( 1==sqlite3BtreeIsInTrans(pMain) );
1.101301 +
1.101302 +    /* Copy Btree meta values */
1.101303 +    for(i=0; i<ArraySize(aCopy); i+=2){
1.101304 +      /* GetMeta() and UpdateMeta() cannot fail in this context because
1.101305 +      ** we already have page 1 loaded into cache and marked dirty. */
1.101306 +      sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
1.101307 +      rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
1.101308 +      if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
1.101309 +    }
1.101310 +
1.101311 +    rc = sqlite3BtreeCopyFile(pMain, pTemp);
1.101312 +    if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101313 +    rc = sqlite3BtreeCommit(pTemp);
1.101314 +    if( rc!=SQLITE_OK ) goto end_of_vacuum;
1.101315 +#ifndef SQLITE_OMIT_AUTOVACUUM
1.101316 +    sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
1.101317 +#endif
1.101318 +  }
1.101319 +
1.101320 +  assert( rc==SQLITE_OK );
1.101321 +  rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
1.101322 +
1.101323 +end_of_vacuum:
1.101324 +  /* Restore the original value of db->flags */
1.101325 +  db->flags = saved_flags;
1.101326 +  db->nChange = saved_nChange;
1.101327 +  db->nTotalChange = saved_nTotalChange;
1.101328 +  db->xTrace = saved_xTrace;
1.101329 +  sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
1.101330 +
1.101331 +  /* Currently there is an SQL level transaction open on the vacuum
1.101332 +  ** database. No locks are held on any other files (since the main file
1.101333 +  ** was committed at the btree level). So it safe to end the transaction
1.101334 +  ** by manually setting the autoCommit flag to true and detaching the
1.101335 +  ** vacuum database. The vacuum_db journal file is deleted when the pager
1.101336 +  ** is closed by the DETACH.
1.101337 +  */
1.101338 +  db->autoCommit = 1;
1.101339 +
1.101340 +  if( pDb ){
1.101341 +    sqlite3BtreeClose(pDb->pBt);
1.101342 +    pDb->pBt = 0;
1.101343 +    pDb->pSchema = 0;
1.101344 +  }
1.101345 +
1.101346 +  /* This both clears the schemas and reduces the size of the db->aDb[]
1.101347 +  ** array. */ 
1.101348 +  sqlite3ResetAllSchemasOfConnection(db);
1.101349 +
1.101350 +  return rc;
1.101351 +}
1.101352 +
1.101353 +#endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
1.101354 +
1.101355 +/************** End of vacuum.c **********************************************/
1.101356 +/************** Begin file vtab.c ********************************************/
1.101357 +/*
1.101358 +** 2006 June 10
1.101359 +**
1.101360 +** The author disclaims copyright to this source code.  In place of
1.101361 +** a legal notice, here is a blessing:
1.101362 +**
1.101363 +**    May you do good and not evil.
1.101364 +**    May you find forgiveness for yourself and forgive others.
1.101365 +**    May you share freely, never taking more than you give.
1.101366 +**
1.101367 +*************************************************************************
1.101368 +** This file contains code used to help implement virtual tables.
1.101369 +*/
1.101370 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.101371 +
1.101372 +/*
1.101373 +** Before a virtual table xCreate() or xConnect() method is invoked, the
1.101374 +** sqlite3.pVtabCtx member variable is set to point to an instance of
1.101375 +** this struct allocated on the stack. It is used by the implementation of 
1.101376 +** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
1.101377 +** are invoked only from within xCreate and xConnect methods.
1.101378 +*/
1.101379 +struct VtabCtx {
1.101380 +  VTable *pVTable;    /* The virtual table being constructed */
1.101381 +  Table *pTab;        /* The Table object to which the virtual table belongs */
1.101382 +};
1.101383 +
1.101384 +/*
1.101385 +** The actual function that does the work of creating a new module.
1.101386 +** This function implements the sqlite3_create_module() and
1.101387 +** sqlite3_create_module_v2() interfaces.
1.101388 +*/
1.101389 +static int createModule(
1.101390 +  sqlite3 *db,                    /* Database in which module is registered */
1.101391 +  const char *zName,              /* Name assigned to this module */
1.101392 +  const sqlite3_module *pModule,  /* The definition of the module */
1.101393 +  void *pAux,                     /* Context pointer for xCreate/xConnect */
1.101394 +  void (*xDestroy)(void *)        /* Module destructor function */
1.101395 +){
1.101396 +  int rc = SQLITE_OK;
1.101397 +  int nName;
1.101398 +
1.101399 +  sqlite3_mutex_enter(db->mutex);
1.101400 +  nName = sqlite3Strlen30(zName);
1.101401 +  if( sqlite3HashFind(&db->aModule, zName, nName) ){
1.101402 +    rc = SQLITE_MISUSE_BKPT;
1.101403 +  }else{
1.101404 +    Module *pMod;
1.101405 +    pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
1.101406 +    if( pMod ){
1.101407 +      Module *pDel;
1.101408 +      char *zCopy = (char *)(&pMod[1]);
1.101409 +      memcpy(zCopy, zName, nName+1);
1.101410 +      pMod->zName = zCopy;
1.101411 +      pMod->pModule = pModule;
1.101412 +      pMod->pAux = pAux;
1.101413 +      pMod->xDestroy = xDestroy;
1.101414 +      pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,nName,(void*)pMod);
1.101415 +      assert( pDel==0 || pDel==pMod );
1.101416 +      if( pDel ){
1.101417 +        db->mallocFailed = 1;
1.101418 +        sqlite3DbFree(db, pDel);
1.101419 +      }
1.101420 +    }
1.101421 +  }
1.101422 +  rc = sqlite3ApiExit(db, rc);
1.101423 +  if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
1.101424 +
1.101425 +  sqlite3_mutex_leave(db->mutex);
1.101426 +  return rc;
1.101427 +}
1.101428 +
1.101429 +
1.101430 +/*
1.101431 +** External API function used to create a new virtual-table module.
1.101432 +*/
1.101433 +SQLITE_API int sqlite3_create_module(
1.101434 +  sqlite3 *db,                    /* Database in which module is registered */
1.101435 +  const char *zName,              /* Name assigned to this module */
1.101436 +  const sqlite3_module *pModule,  /* The definition of the module */
1.101437 +  void *pAux                      /* Context pointer for xCreate/xConnect */
1.101438 +){
1.101439 +  return createModule(db, zName, pModule, pAux, 0);
1.101440 +}
1.101441 +
1.101442 +/*
1.101443 +** External API function used to create a new virtual-table module.
1.101444 +*/
1.101445 +SQLITE_API int sqlite3_create_module_v2(
1.101446 +  sqlite3 *db,                    /* Database in which module is registered */
1.101447 +  const char *zName,              /* Name assigned to this module */
1.101448 +  const sqlite3_module *pModule,  /* The definition of the module */
1.101449 +  void *pAux,                     /* Context pointer for xCreate/xConnect */
1.101450 +  void (*xDestroy)(void *)        /* Module destructor function */
1.101451 +){
1.101452 +  return createModule(db, zName, pModule, pAux, xDestroy);
1.101453 +}
1.101454 +
1.101455 +/*
1.101456 +** Lock the virtual table so that it cannot be disconnected.
1.101457 +** Locks nest.  Every lock should have a corresponding unlock.
1.101458 +** If an unlock is omitted, resources leaks will occur.  
1.101459 +**
1.101460 +** If a disconnect is attempted while a virtual table is locked,
1.101461 +** the disconnect is deferred until all locks have been removed.
1.101462 +*/
1.101463 +SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
1.101464 +  pVTab->nRef++;
1.101465 +}
1.101466 +
1.101467 +
1.101468 +/*
1.101469 +** pTab is a pointer to a Table structure representing a virtual-table.
1.101470 +** Return a pointer to the VTable object used by connection db to access 
1.101471 +** this virtual-table, if one has been created, or NULL otherwise.
1.101472 +*/
1.101473 +SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
1.101474 +  VTable *pVtab;
1.101475 +  assert( IsVirtual(pTab) );
1.101476 +  for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
1.101477 +  return pVtab;
1.101478 +}
1.101479 +
1.101480 +/*
1.101481 +** Decrement the ref-count on a virtual table object. When the ref-count
1.101482 +** reaches zero, call the xDisconnect() method to delete the object.
1.101483 +*/
1.101484 +SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
1.101485 +  sqlite3 *db = pVTab->db;
1.101486 +
1.101487 +  assert( db );
1.101488 +  assert( pVTab->nRef>0 );
1.101489 +  assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
1.101490 +
1.101491 +  pVTab->nRef--;
1.101492 +  if( pVTab->nRef==0 ){
1.101493 +    sqlite3_vtab *p = pVTab->pVtab;
1.101494 +    if( p ){
1.101495 +      p->pModule->xDisconnect(p);
1.101496 +    }
1.101497 +    sqlite3DbFree(db, pVTab);
1.101498 +  }
1.101499 +}
1.101500 +
1.101501 +/*
1.101502 +** Table p is a virtual table. This function moves all elements in the
1.101503 +** p->pVTable list to the sqlite3.pDisconnect lists of their associated
1.101504 +** database connections to be disconnected at the next opportunity. 
1.101505 +** Except, if argument db is not NULL, then the entry associated with
1.101506 +** connection db is left in the p->pVTable list.
1.101507 +*/
1.101508 +static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
1.101509 +  VTable *pRet = 0;
1.101510 +  VTable *pVTable = p->pVTable;
1.101511 +  p->pVTable = 0;
1.101512 +
1.101513 +  /* Assert that the mutex (if any) associated with the BtShared database 
1.101514 +  ** that contains table p is held by the caller. See header comments 
1.101515 +  ** above function sqlite3VtabUnlockList() for an explanation of why
1.101516 +  ** this makes it safe to access the sqlite3.pDisconnect list of any
1.101517 +  ** database connection that may have an entry in the p->pVTable list.
1.101518 +  */
1.101519 +  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
1.101520 +
1.101521 +  while( pVTable ){
1.101522 +    sqlite3 *db2 = pVTable->db;
1.101523 +    VTable *pNext = pVTable->pNext;
1.101524 +    assert( db2 );
1.101525 +    if( db2==db ){
1.101526 +      pRet = pVTable;
1.101527 +      p->pVTable = pRet;
1.101528 +      pRet->pNext = 0;
1.101529 +    }else{
1.101530 +      pVTable->pNext = db2->pDisconnect;
1.101531 +      db2->pDisconnect = pVTable;
1.101532 +    }
1.101533 +    pVTable = pNext;
1.101534 +  }
1.101535 +
1.101536 +  assert( !db || pRet );
1.101537 +  return pRet;
1.101538 +}
1.101539 +
1.101540 +/*
1.101541 +** Table *p is a virtual table. This function removes the VTable object
1.101542 +** for table *p associated with database connection db from the linked
1.101543 +** list in p->pVTab. It also decrements the VTable ref count. This is
1.101544 +** used when closing database connection db to free all of its VTable
1.101545 +** objects without disturbing the rest of the Schema object (which may
1.101546 +** be being used by other shared-cache connections).
1.101547 +*/
1.101548 +SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
1.101549 +  VTable **ppVTab;
1.101550 +
1.101551 +  assert( IsVirtual(p) );
1.101552 +  assert( sqlite3BtreeHoldsAllMutexes(db) );
1.101553 +  assert( sqlite3_mutex_held(db->mutex) );
1.101554 +
1.101555 +  for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
1.101556 +    if( (*ppVTab)->db==db  ){
1.101557 +      VTable *pVTab = *ppVTab;
1.101558 +      *ppVTab = pVTab->pNext;
1.101559 +      sqlite3VtabUnlock(pVTab);
1.101560 +      break;
1.101561 +    }
1.101562 +  }
1.101563 +}
1.101564 +
1.101565 +
1.101566 +/*
1.101567 +** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
1.101568 +**
1.101569 +** This function may only be called when the mutexes associated with all
1.101570 +** shared b-tree databases opened using connection db are held by the 
1.101571 +** caller. This is done to protect the sqlite3.pDisconnect list. The
1.101572 +** sqlite3.pDisconnect list is accessed only as follows:
1.101573 +**
1.101574 +**   1) By this function. In this case, all BtShared mutexes and the mutex
1.101575 +**      associated with the database handle itself must be held.
1.101576 +**
1.101577 +**   2) By function vtabDisconnectAll(), when it adds a VTable entry to
1.101578 +**      the sqlite3.pDisconnect list. In this case either the BtShared mutex
1.101579 +**      associated with the database the virtual table is stored in is held
1.101580 +**      or, if the virtual table is stored in a non-sharable database, then
1.101581 +**      the database handle mutex is held.
1.101582 +**
1.101583 +** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously 
1.101584 +** by multiple threads. It is thread-safe.
1.101585 +*/
1.101586 +SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
1.101587 +  VTable *p = db->pDisconnect;
1.101588 +  db->pDisconnect = 0;
1.101589 +
1.101590 +  assert( sqlite3BtreeHoldsAllMutexes(db) );
1.101591 +  assert( sqlite3_mutex_held(db->mutex) );
1.101592 +
1.101593 +  if( p ){
1.101594 +    sqlite3ExpirePreparedStatements(db);
1.101595 +    do {
1.101596 +      VTable *pNext = p->pNext;
1.101597 +      sqlite3VtabUnlock(p);
1.101598 +      p = pNext;
1.101599 +    }while( p );
1.101600 +  }
1.101601 +}
1.101602 +
1.101603 +/*
1.101604 +** Clear any and all virtual-table information from the Table record.
1.101605 +** This routine is called, for example, just before deleting the Table
1.101606 +** record.
1.101607 +**
1.101608 +** Since it is a virtual-table, the Table structure contains a pointer
1.101609 +** to the head of a linked list of VTable structures. Each VTable 
1.101610 +** structure is associated with a single sqlite3* user of the schema.
1.101611 +** The reference count of the VTable structure associated with database 
1.101612 +** connection db is decremented immediately (which may lead to the 
1.101613 +** structure being xDisconnected and free). Any other VTable structures
1.101614 +** in the list are moved to the sqlite3.pDisconnect list of the associated 
1.101615 +** database connection.
1.101616 +*/
1.101617 +SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
1.101618 +  if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
1.101619 +  if( p->azModuleArg ){
1.101620 +    int i;
1.101621 +    for(i=0; i<p->nModuleArg; i++){
1.101622 +      if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
1.101623 +    }
1.101624 +    sqlite3DbFree(db, p->azModuleArg);
1.101625 +  }
1.101626 +}
1.101627 +
1.101628 +/*
1.101629 +** Add a new module argument to pTable->azModuleArg[].
1.101630 +** The string is not copied - the pointer is stored.  The
1.101631 +** string will be freed automatically when the table is
1.101632 +** deleted.
1.101633 +*/
1.101634 +static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
1.101635 +  int i = pTable->nModuleArg++;
1.101636 +  int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
1.101637 +  char **azModuleArg;
1.101638 +  azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
1.101639 +  if( azModuleArg==0 ){
1.101640 +    int j;
1.101641 +    for(j=0; j<i; j++){
1.101642 +      sqlite3DbFree(db, pTable->azModuleArg[j]);
1.101643 +    }
1.101644 +    sqlite3DbFree(db, zArg);
1.101645 +    sqlite3DbFree(db, pTable->azModuleArg);
1.101646 +    pTable->nModuleArg = 0;
1.101647 +  }else{
1.101648 +    azModuleArg[i] = zArg;
1.101649 +    azModuleArg[i+1] = 0;
1.101650 +  }
1.101651 +  pTable->azModuleArg = azModuleArg;
1.101652 +}
1.101653 +
1.101654 +/*
1.101655 +** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
1.101656 +** statement.  The module name has been parsed, but the optional list
1.101657 +** of parameters that follow the module name are still pending.
1.101658 +*/
1.101659 +SQLITE_PRIVATE void sqlite3VtabBeginParse(
1.101660 +  Parse *pParse,        /* Parsing context */
1.101661 +  Token *pName1,        /* Name of new table, or database name */
1.101662 +  Token *pName2,        /* Name of new table or NULL */
1.101663 +  Token *pModuleName,   /* Name of the module for the virtual table */
1.101664 +  int ifNotExists       /* No error if the table already exists */
1.101665 +){
1.101666 +  int iDb;              /* The database the table is being created in */
1.101667 +  Table *pTable;        /* The new virtual table */
1.101668 +  sqlite3 *db;          /* Database connection */
1.101669 +
1.101670 +  sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
1.101671 +  pTable = pParse->pNewTable;
1.101672 +  if( pTable==0 ) return;
1.101673 +  assert( 0==pTable->pIndex );
1.101674 +
1.101675 +  db = pParse->db;
1.101676 +  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
1.101677 +  assert( iDb>=0 );
1.101678 +
1.101679 +  pTable->tabFlags |= TF_Virtual;
1.101680 +  pTable->nModuleArg = 0;
1.101681 +  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
1.101682 +  addModuleArgument(db, pTable, 0);
1.101683 +  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
1.101684 +  pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
1.101685 +
1.101686 +#ifndef SQLITE_OMIT_AUTHORIZATION
1.101687 +  /* Creating a virtual table invokes the authorization callback twice.
1.101688 +  ** The first invocation, to obtain permission to INSERT a row into the
1.101689 +  ** sqlite_master table, has already been made by sqlite3StartTable().
1.101690 +  ** The second call, to obtain permission to create the table, is made now.
1.101691 +  */
1.101692 +  if( pTable->azModuleArg ){
1.101693 +    sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
1.101694 +            pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
1.101695 +  }
1.101696 +#endif
1.101697 +}
1.101698 +
1.101699 +/*
1.101700 +** This routine takes the module argument that has been accumulating
1.101701 +** in pParse->zArg[] and appends it to the list of arguments on the
1.101702 +** virtual table currently under construction in pParse->pTable.
1.101703 +*/
1.101704 +static void addArgumentToVtab(Parse *pParse){
1.101705 +  if( pParse->sArg.z && pParse->pNewTable ){
1.101706 +    const char *z = (const char*)pParse->sArg.z;
1.101707 +    int n = pParse->sArg.n;
1.101708 +    sqlite3 *db = pParse->db;
1.101709 +    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
1.101710 +  }
1.101711 +}
1.101712 +
1.101713 +/*
1.101714 +** The parser calls this routine after the CREATE VIRTUAL TABLE statement
1.101715 +** has been completely parsed.
1.101716 +*/
1.101717 +SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
1.101718 +  Table *pTab = pParse->pNewTable;  /* The table being constructed */
1.101719 +  sqlite3 *db = pParse->db;         /* The database connection */
1.101720 +
1.101721 +  if( pTab==0 ) return;
1.101722 +  addArgumentToVtab(pParse);
1.101723 +  pParse->sArg.z = 0;
1.101724 +  if( pTab->nModuleArg<1 ) return;
1.101725 +  
1.101726 +  /* If the CREATE VIRTUAL TABLE statement is being entered for the
1.101727 +  ** first time (in other words if the virtual table is actually being
1.101728 +  ** created now instead of just being read out of sqlite_master) then
1.101729 +  ** do additional initialization work and store the statement text
1.101730 +  ** in the sqlite_master table.
1.101731 +  */
1.101732 +  if( !db->init.busy ){
1.101733 +    char *zStmt;
1.101734 +    char *zWhere;
1.101735 +    int iDb;
1.101736 +    Vdbe *v;
1.101737 +
1.101738 +    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
1.101739 +    if( pEnd ){
1.101740 +      pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
1.101741 +    }
1.101742 +    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
1.101743 +
1.101744 +    /* A slot for the record has already been allocated in the 
1.101745 +    ** SQLITE_MASTER table.  We just need to update that slot with all
1.101746 +    ** the information we've collected.  
1.101747 +    **
1.101748 +    ** The VM register number pParse->regRowid holds the rowid of an
1.101749 +    ** entry in the sqlite_master table tht was created for this vtab
1.101750 +    ** by sqlite3StartTable().
1.101751 +    */
1.101752 +    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1.101753 +    sqlite3NestedParse(pParse,
1.101754 +      "UPDATE %Q.%s "
1.101755 +         "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
1.101756 +       "WHERE rowid=#%d",
1.101757 +      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
1.101758 +      pTab->zName,
1.101759 +      pTab->zName,
1.101760 +      zStmt,
1.101761 +      pParse->regRowid
1.101762 +    );
1.101763 +    sqlite3DbFree(db, zStmt);
1.101764 +    v = sqlite3GetVdbe(pParse);
1.101765 +    sqlite3ChangeCookie(pParse, iDb);
1.101766 +
1.101767 +    sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1.101768 +    zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
1.101769 +    sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
1.101770 +    sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
1.101771 +                         pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
1.101772 +  }
1.101773 +
1.101774 +  /* If we are rereading the sqlite_master table create the in-memory
1.101775 +  ** record of the table. The xConnect() method is not called until
1.101776 +  ** the first time the virtual table is used in an SQL statement. This
1.101777 +  ** allows a schema that contains virtual tables to be loaded before
1.101778 +  ** the required virtual table implementations are registered.  */
1.101779 +  else {
1.101780 +    Table *pOld;
1.101781 +    Schema *pSchema = pTab->pSchema;
1.101782 +    const char *zName = pTab->zName;
1.101783 +    int nName = sqlite3Strlen30(zName);
1.101784 +    assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
1.101785 +    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
1.101786 +    if( pOld ){
1.101787 +      db->mallocFailed = 1;
1.101788 +      assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
1.101789 +      return;
1.101790 +    }
1.101791 +    pParse->pNewTable = 0;
1.101792 +  }
1.101793 +}
1.101794 +
1.101795 +/*
1.101796 +** The parser calls this routine when it sees the first token
1.101797 +** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
1.101798 +*/
1.101799 +SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
1.101800 +  addArgumentToVtab(pParse);
1.101801 +  pParse->sArg.z = 0;
1.101802 +  pParse->sArg.n = 0;
1.101803 +}
1.101804 +
1.101805 +/*
1.101806 +** The parser calls this routine for each token after the first token
1.101807 +** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
1.101808 +*/
1.101809 +SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
1.101810 +  Token *pArg = &pParse->sArg;
1.101811 +  if( pArg->z==0 ){
1.101812 +    pArg->z = p->z;
1.101813 +    pArg->n = p->n;
1.101814 +  }else{
1.101815 +    assert(pArg->z < p->z);
1.101816 +    pArg->n = (int)(&p->z[p->n] - pArg->z);
1.101817 +  }
1.101818 +}
1.101819 +
1.101820 +/*
1.101821 +** Invoke a virtual table constructor (either xCreate or xConnect). The
1.101822 +** pointer to the function to invoke is passed as the fourth parameter
1.101823 +** to this procedure.
1.101824 +*/
1.101825 +static int vtabCallConstructor(
1.101826 +  sqlite3 *db, 
1.101827 +  Table *pTab,
1.101828 +  Module *pMod,
1.101829 +  int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
1.101830 +  char **pzErr
1.101831 +){
1.101832 +  VtabCtx sCtx, *pPriorCtx;
1.101833 +  VTable *pVTable;
1.101834 +  int rc;
1.101835 +  const char *const*azArg = (const char *const*)pTab->azModuleArg;
1.101836 +  int nArg = pTab->nModuleArg;
1.101837 +  char *zErr = 0;
1.101838 +  char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
1.101839 +  int iDb;
1.101840 +
1.101841 +  if( !zModuleName ){
1.101842 +    return SQLITE_NOMEM;
1.101843 +  }
1.101844 +
1.101845 +  pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
1.101846 +  if( !pVTable ){
1.101847 +    sqlite3DbFree(db, zModuleName);
1.101848 +    return SQLITE_NOMEM;
1.101849 +  }
1.101850 +  pVTable->db = db;
1.101851 +  pVTable->pMod = pMod;
1.101852 +
1.101853 +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1.101854 +  pTab->azModuleArg[1] = db->aDb[iDb].zName;
1.101855 +
1.101856 +  /* Invoke the virtual table constructor */
1.101857 +  assert( &db->pVtabCtx );
1.101858 +  assert( xConstruct );
1.101859 +  sCtx.pTab = pTab;
1.101860 +  sCtx.pVTable = pVTable;
1.101861 +  pPriorCtx = db->pVtabCtx;
1.101862 +  db->pVtabCtx = &sCtx;
1.101863 +  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
1.101864 +  db->pVtabCtx = pPriorCtx;
1.101865 +  if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
1.101866 +
1.101867 +  if( SQLITE_OK!=rc ){
1.101868 +    if( zErr==0 ){
1.101869 +      *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
1.101870 +    }else {
1.101871 +      *pzErr = sqlite3MPrintf(db, "%s", zErr);
1.101872 +      sqlite3_free(zErr);
1.101873 +    }
1.101874 +    sqlite3DbFree(db, pVTable);
1.101875 +  }else if( ALWAYS(pVTable->pVtab) ){
1.101876 +    /* Justification of ALWAYS():  A correct vtab constructor must allocate
1.101877 +    ** the sqlite3_vtab object if successful.  */
1.101878 +    pVTable->pVtab->pModule = pMod->pModule;
1.101879 +    pVTable->nRef = 1;
1.101880 +    if( sCtx.pTab ){
1.101881 +      const char *zFormat = "vtable constructor did not declare schema: %s";
1.101882 +      *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
1.101883 +      sqlite3VtabUnlock(pVTable);
1.101884 +      rc = SQLITE_ERROR;
1.101885 +    }else{
1.101886 +      int iCol;
1.101887 +      /* If everything went according to plan, link the new VTable structure
1.101888 +      ** into the linked list headed by pTab->pVTable. Then loop through the 
1.101889 +      ** columns of the table to see if any of them contain the token "hidden".
1.101890 +      ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
1.101891 +      ** the type string.  */
1.101892 +      pVTable->pNext = pTab->pVTable;
1.101893 +      pTab->pVTable = pVTable;
1.101894 +
1.101895 +      for(iCol=0; iCol<pTab->nCol; iCol++){
1.101896 +        char *zType = pTab->aCol[iCol].zType;
1.101897 +        int nType;
1.101898 +        int i = 0;
1.101899 +        if( !zType ) continue;
1.101900 +        nType = sqlite3Strlen30(zType);
1.101901 +        if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
1.101902 +          for(i=0; i<nType; i++){
1.101903 +            if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
1.101904 +             && (zType[i+7]=='\0' || zType[i+7]==' ')
1.101905 +            ){
1.101906 +              i++;
1.101907 +              break;
1.101908 +            }
1.101909 +          }
1.101910 +        }
1.101911 +        if( i<nType ){
1.101912 +          int j;
1.101913 +          int nDel = 6 + (zType[i+6] ? 1 : 0);
1.101914 +          for(j=i; (j+nDel)<=nType; j++){
1.101915 +            zType[j] = zType[j+nDel];
1.101916 +          }
1.101917 +          if( zType[i]=='\0' && i>0 ){
1.101918 +            assert(zType[i-1]==' ');
1.101919 +            zType[i-1] = '\0';
1.101920 +          }
1.101921 +          pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
1.101922 +        }
1.101923 +      }
1.101924 +    }
1.101925 +  }
1.101926 +
1.101927 +  sqlite3DbFree(db, zModuleName);
1.101928 +  return rc;
1.101929 +}
1.101930 +
1.101931 +/*
1.101932 +** This function is invoked by the parser to call the xConnect() method
1.101933 +** of the virtual table pTab. If an error occurs, an error code is returned 
1.101934 +** and an error left in pParse.
1.101935 +**
1.101936 +** This call is a no-op if table pTab is not a virtual table.
1.101937 +*/
1.101938 +SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
1.101939 +  sqlite3 *db = pParse->db;
1.101940 +  const char *zMod;
1.101941 +  Module *pMod;
1.101942 +  int rc;
1.101943 +
1.101944 +  assert( pTab );
1.101945 +  if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
1.101946 +    return SQLITE_OK;
1.101947 +  }
1.101948 +
1.101949 +  /* Locate the required virtual table module */
1.101950 +  zMod = pTab->azModuleArg[0];
1.101951 +  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
1.101952 +
1.101953 +  if( !pMod ){
1.101954 +    const char *zModule = pTab->azModuleArg[0];
1.101955 +    sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
1.101956 +    rc = SQLITE_ERROR;
1.101957 +  }else{
1.101958 +    char *zErr = 0;
1.101959 +    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
1.101960 +    if( rc!=SQLITE_OK ){
1.101961 +      sqlite3ErrorMsg(pParse, "%s", zErr);
1.101962 +    }
1.101963 +    sqlite3DbFree(db, zErr);
1.101964 +  }
1.101965 +
1.101966 +  return rc;
1.101967 +}
1.101968 +/*
1.101969 +** Grow the db->aVTrans[] array so that there is room for at least one
1.101970 +** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
1.101971 +*/
1.101972 +static int growVTrans(sqlite3 *db){
1.101973 +  const int ARRAY_INCR = 5;
1.101974 +
1.101975 +  /* Grow the sqlite3.aVTrans array if required */
1.101976 +  if( (db->nVTrans%ARRAY_INCR)==0 ){
1.101977 +    VTable **aVTrans;
1.101978 +    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
1.101979 +    aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
1.101980 +    if( !aVTrans ){
1.101981 +      return SQLITE_NOMEM;
1.101982 +    }
1.101983 +    memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
1.101984 +    db->aVTrans = aVTrans;
1.101985 +  }
1.101986 +
1.101987 +  return SQLITE_OK;
1.101988 +}
1.101989 +
1.101990 +/*
1.101991 +** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
1.101992 +** have already been reserved using growVTrans().
1.101993 +*/
1.101994 +static void addToVTrans(sqlite3 *db, VTable *pVTab){
1.101995 +  /* Add pVtab to the end of sqlite3.aVTrans */
1.101996 +  db->aVTrans[db->nVTrans++] = pVTab;
1.101997 +  sqlite3VtabLock(pVTab);
1.101998 +}
1.101999 +
1.102000 +/*
1.102001 +** This function is invoked by the vdbe to call the xCreate method
1.102002 +** of the virtual table named zTab in database iDb. 
1.102003 +**
1.102004 +** If an error occurs, *pzErr is set to point an an English language
1.102005 +** description of the error and an SQLITE_XXX error code is returned.
1.102006 +** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
1.102007 +*/
1.102008 +SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
1.102009 +  int rc = SQLITE_OK;
1.102010 +  Table *pTab;
1.102011 +  Module *pMod;
1.102012 +  const char *zMod;
1.102013 +
1.102014 +  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
1.102015 +  assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
1.102016 +
1.102017 +  /* Locate the required virtual table module */
1.102018 +  zMod = pTab->azModuleArg[0];
1.102019 +  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
1.102020 +
1.102021 +  /* If the module has been registered and includes a Create method, 
1.102022 +  ** invoke it now. If the module has not been registered, return an 
1.102023 +  ** error. Otherwise, do nothing.
1.102024 +  */
1.102025 +  if( !pMod ){
1.102026 +    *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
1.102027 +    rc = SQLITE_ERROR;
1.102028 +  }else{
1.102029 +    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
1.102030 +  }
1.102031 +
1.102032 +  /* Justification of ALWAYS():  The xConstructor method is required to
1.102033 +  ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
1.102034 +  if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
1.102035 +    rc = growVTrans(db);
1.102036 +    if( rc==SQLITE_OK ){
1.102037 +      addToVTrans(db, sqlite3GetVTable(db, pTab));
1.102038 +    }
1.102039 +  }
1.102040 +
1.102041 +  return rc;
1.102042 +}
1.102043 +
1.102044 +/*
1.102045 +** This function is used to set the schema of a virtual table.  It is only
1.102046 +** valid to call this function from within the xCreate() or xConnect() of a
1.102047 +** virtual table module.
1.102048 +*/
1.102049 +SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
1.102050 +  Parse *pParse;
1.102051 +
1.102052 +  int rc = SQLITE_OK;
1.102053 +  Table *pTab;
1.102054 +  char *zErr = 0;
1.102055 +
1.102056 +  sqlite3_mutex_enter(db->mutex);
1.102057 +  if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
1.102058 +    sqlite3Error(db, SQLITE_MISUSE, 0);
1.102059 +    sqlite3_mutex_leave(db->mutex);
1.102060 +    return SQLITE_MISUSE_BKPT;
1.102061 +  }
1.102062 +  assert( (pTab->tabFlags & TF_Virtual)!=0 );
1.102063 +
1.102064 +  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
1.102065 +  if( pParse==0 ){
1.102066 +    rc = SQLITE_NOMEM;
1.102067 +  }else{
1.102068 +    pParse->declareVtab = 1;
1.102069 +    pParse->db = db;
1.102070 +    pParse->nQueryLoop = 1;
1.102071 +  
1.102072 +    if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
1.102073 +     && pParse->pNewTable
1.102074 +     && !db->mallocFailed
1.102075 +     && !pParse->pNewTable->pSelect
1.102076 +     && (pParse->pNewTable->tabFlags & TF_Virtual)==0
1.102077 +    ){
1.102078 +      if( !pTab->aCol ){
1.102079 +        pTab->aCol = pParse->pNewTable->aCol;
1.102080 +        pTab->nCol = pParse->pNewTable->nCol;
1.102081 +        pParse->pNewTable->nCol = 0;
1.102082 +        pParse->pNewTable->aCol = 0;
1.102083 +      }
1.102084 +      db->pVtabCtx->pTab = 0;
1.102085 +    }else{
1.102086 +      sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
1.102087 +      sqlite3DbFree(db, zErr);
1.102088 +      rc = SQLITE_ERROR;
1.102089 +    }
1.102090 +    pParse->declareVtab = 0;
1.102091 +  
1.102092 +    if( pParse->pVdbe ){
1.102093 +      sqlite3VdbeFinalize(pParse->pVdbe);
1.102094 +    }
1.102095 +    sqlite3DeleteTable(db, pParse->pNewTable);
1.102096 +    sqlite3StackFree(db, pParse);
1.102097 +  }
1.102098 +
1.102099 +  assert( (rc&0xff)==rc );
1.102100 +  rc = sqlite3ApiExit(db, rc);
1.102101 +  sqlite3_mutex_leave(db->mutex);
1.102102 +  return rc;
1.102103 +}
1.102104 +
1.102105 +/*
1.102106 +** This function is invoked by the vdbe to call the xDestroy method
1.102107 +** of the virtual table named zTab in database iDb. This occurs
1.102108 +** when a DROP TABLE is mentioned.
1.102109 +**
1.102110 +** This call is a no-op if zTab is not a virtual table.
1.102111 +*/
1.102112 +SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
1.102113 +  int rc = SQLITE_OK;
1.102114 +  Table *pTab;
1.102115 +
1.102116 +  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
1.102117 +  if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
1.102118 +    VTable *p = vtabDisconnectAll(db, pTab);
1.102119 +
1.102120 +    assert( rc==SQLITE_OK );
1.102121 +    rc = p->pMod->pModule->xDestroy(p->pVtab);
1.102122 +
1.102123 +    /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
1.102124 +    if( rc==SQLITE_OK ){
1.102125 +      assert( pTab->pVTable==p && p->pNext==0 );
1.102126 +      p->pVtab = 0;
1.102127 +      pTab->pVTable = 0;
1.102128 +      sqlite3VtabUnlock(p);
1.102129 +    }
1.102130 +  }
1.102131 +
1.102132 +  return rc;
1.102133 +}
1.102134 +
1.102135 +/*
1.102136 +** This function invokes either the xRollback or xCommit method
1.102137 +** of each of the virtual tables in the sqlite3.aVTrans array. The method
1.102138 +** called is identified by the second argument, "offset", which is
1.102139 +** the offset of the method to call in the sqlite3_module structure.
1.102140 +**
1.102141 +** The array is cleared after invoking the callbacks. 
1.102142 +*/
1.102143 +static void callFinaliser(sqlite3 *db, int offset){
1.102144 +  int i;
1.102145 +  if( db->aVTrans ){
1.102146 +    for(i=0; i<db->nVTrans; i++){
1.102147 +      VTable *pVTab = db->aVTrans[i];
1.102148 +      sqlite3_vtab *p = pVTab->pVtab;
1.102149 +      if( p ){
1.102150 +        int (*x)(sqlite3_vtab *);
1.102151 +        x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
1.102152 +        if( x ) x(p);
1.102153 +      }
1.102154 +      pVTab->iSavepoint = 0;
1.102155 +      sqlite3VtabUnlock(pVTab);
1.102156 +    }
1.102157 +    sqlite3DbFree(db, db->aVTrans);
1.102158 +    db->nVTrans = 0;
1.102159 +    db->aVTrans = 0;
1.102160 +  }
1.102161 +}
1.102162 +
1.102163 +/*
1.102164 +** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
1.102165 +** array. Return the error code for the first error that occurs, or
1.102166 +** SQLITE_OK if all xSync operations are successful.
1.102167 +**
1.102168 +** Set *pzErrmsg to point to a buffer that should be released using 
1.102169 +** sqlite3DbFree() containing an error message, if one is available.
1.102170 +*/
1.102171 +SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
1.102172 +  int i;
1.102173 +  int rc = SQLITE_OK;
1.102174 +  VTable **aVTrans = db->aVTrans;
1.102175 +
1.102176 +  db->aVTrans = 0;
1.102177 +  for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
1.102178 +    int (*x)(sqlite3_vtab *);
1.102179 +    sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
1.102180 +    if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
1.102181 +      rc = x(pVtab);
1.102182 +      sqlite3DbFree(db, *pzErrmsg);
1.102183 +      *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
1.102184 +      sqlite3_free(pVtab->zErrMsg);
1.102185 +    }
1.102186 +  }
1.102187 +  db->aVTrans = aVTrans;
1.102188 +  return rc;
1.102189 +}
1.102190 +
1.102191 +/*
1.102192 +** Invoke the xRollback method of all virtual tables in the 
1.102193 +** sqlite3.aVTrans array. Then clear the array itself.
1.102194 +*/
1.102195 +SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
1.102196 +  callFinaliser(db, offsetof(sqlite3_module,xRollback));
1.102197 +  return SQLITE_OK;
1.102198 +}
1.102199 +
1.102200 +/*
1.102201 +** Invoke the xCommit method of all virtual tables in the 
1.102202 +** sqlite3.aVTrans array. Then clear the array itself.
1.102203 +*/
1.102204 +SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
1.102205 +  callFinaliser(db, offsetof(sqlite3_module,xCommit));
1.102206 +  return SQLITE_OK;
1.102207 +}
1.102208 +
1.102209 +/*
1.102210 +** If the virtual table pVtab supports the transaction interface
1.102211 +** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
1.102212 +** not currently open, invoke the xBegin method now.
1.102213 +**
1.102214 +** If the xBegin call is successful, place the sqlite3_vtab pointer
1.102215 +** in the sqlite3.aVTrans array.
1.102216 +*/
1.102217 +SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
1.102218 +  int rc = SQLITE_OK;
1.102219 +  const sqlite3_module *pModule;
1.102220 +
1.102221 +  /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
1.102222 +  ** than zero, then this function is being called from within a
1.102223 +  ** virtual module xSync() callback. It is illegal to write to 
1.102224 +  ** virtual module tables in this case, so return SQLITE_LOCKED.
1.102225 +  */
1.102226 +  if( sqlite3VtabInSync(db) ){
1.102227 +    return SQLITE_LOCKED;
1.102228 +  }
1.102229 +  if( !pVTab ){
1.102230 +    return SQLITE_OK;
1.102231 +  } 
1.102232 +  pModule = pVTab->pVtab->pModule;
1.102233 +
1.102234 +  if( pModule->xBegin ){
1.102235 +    int i;
1.102236 +
1.102237 +    /* If pVtab is already in the aVTrans array, return early */
1.102238 +    for(i=0; i<db->nVTrans; i++){
1.102239 +      if( db->aVTrans[i]==pVTab ){
1.102240 +        return SQLITE_OK;
1.102241 +      }
1.102242 +    }
1.102243 +
1.102244 +    /* Invoke the xBegin method. If successful, add the vtab to the 
1.102245 +    ** sqlite3.aVTrans[] array. */
1.102246 +    rc = growVTrans(db);
1.102247 +    if( rc==SQLITE_OK ){
1.102248 +      rc = pModule->xBegin(pVTab->pVtab);
1.102249 +      if( rc==SQLITE_OK ){
1.102250 +        addToVTrans(db, pVTab);
1.102251 +      }
1.102252 +    }
1.102253 +  }
1.102254 +  return rc;
1.102255 +}
1.102256 +
1.102257 +/*
1.102258 +** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
1.102259 +** virtual tables that currently have an open transaction. Pass iSavepoint
1.102260 +** as the second argument to the virtual table method invoked.
1.102261 +**
1.102262 +** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
1.102263 +** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is 
1.102264 +** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
1.102265 +** an open transaction is invoked.
1.102266 +**
1.102267 +** If any virtual table method returns an error code other than SQLITE_OK, 
1.102268 +** processing is abandoned and the error returned to the caller of this
1.102269 +** function immediately. If all calls to virtual table methods are successful,
1.102270 +** SQLITE_OK is returned.
1.102271 +*/
1.102272 +SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
1.102273 +  int rc = SQLITE_OK;
1.102274 +
1.102275 +  assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
1.102276 +  assert( iSavepoint>=0 );
1.102277 +  if( db->aVTrans ){
1.102278 +    int i;
1.102279 +    for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
1.102280 +      VTable *pVTab = db->aVTrans[i];
1.102281 +      const sqlite3_module *pMod = pVTab->pMod->pModule;
1.102282 +      if( pVTab->pVtab && pMod->iVersion>=2 ){
1.102283 +        int (*xMethod)(sqlite3_vtab *, int);
1.102284 +        switch( op ){
1.102285 +          case SAVEPOINT_BEGIN:
1.102286 +            xMethod = pMod->xSavepoint;
1.102287 +            pVTab->iSavepoint = iSavepoint+1;
1.102288 +            break;
1.102289 +          case SAVEPOINT_ROLLBACK:
1.102290 +            xMethod = pMod->xRollbackTo;
1.102291 +            break;
1.102292 +          default:
1.102293 +            xMethod = pMod->xRelease;
1.102294 +            break;
1.102295 +        }
1.102296 +        if( xMethod && pVTab->iSavepoint>iSavepoint ){
1.102297 +          rc = xMethod(pVTab->pVtab, iSavepoint);
1.102298 +        }
1.102299 +      }
1.102300 +    }
1.102301 +  }
1.102302 +  return rc;
1.102303 +}
1.102304 +
1.102305 +/*
1.102306 +** The first parameter (pDef) is a function implementation.  The
1.102307 +** second parameter (pExpr) is the first argument to this function.
1.102308 +** If pExpr is a column in a virtual table, then let the virtual
1.102309 +** table implementation have an opportunity to overload the function.
1.102310 +**
1.102311 +** This routine is used to allow virtual table implementations to
1.102312 +** overload MATCH, LIKE, GLOB, and REGEXP operators.
1.102313 +**
1.102314 +** Return either the pDef argument (indicating no change) or a 
1.102315 +** new FuncDef structure that is marked as ephemeral using the
1.102316 +** SQLITE_FUNC_EPHEM flag.
1.102317 +*/
1.102318 +SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
1.102319 +  sqlite3 *db,    /* Database connection for reporting malloc problems */
1.102320 +  FuncDef *pDef,  /* Function to possibly overload */
1.102321 +  int nArg,       /* Number of arguments to the function */
1.102322 +  Expr *pExpr     /* First argument to the function */
1.102323 +){
1.102324 +  Table *pTab;
1.102325 +  sqlite3_vtab *pVtab;
1.102326 +  sqlite3_module *pMod;
1.102327 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
1.102328 +  void *pArg = 0;
1.102329 +  FuncDef *pNew;
1.102330 +  int rc = 0;
1.102331 +  char *zLowerName;
1.102332 +  unsigned char *z;
1.102333 +
1.102334 +
1.102335 +  /* Check to see the left operand is a column in a virtual table */
1.102336 +  if( NEVER(pExpr==0) ) return pDef;
1.102337 +  if( pExpr->op!=TK_COLUMN ) return pDef;
1.102338 +  pTab = pExpr->pTab;
1.102339 +  if( NEVER(pTab==0) ) return pDef;
1.102340 +  if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
1.102341 +  pVtab = sqlite3GetVTable(db, pTab)->pVtab;
1.102342 +  assert( pVtab!=0 );
1.102343 +  assert( pVtab->pModule!=0 );
1.102344 +  pMod = (sqlite3_module *)pVtab->pModule;
1.102345 +  if( pMod->xFindFunction==0 ) return pDef;
1.102346 + 
1.102347 +  /* Call the xFindFunction method on the virtual table implementation
1.102348 +  ** to see if the implementation wants to overload this function 
1.102349 +  */
1.102350 +  zLowerName = sqlite3DbStrDup(db, pDef->zName);
1.102351 +  if( zLowerName ){
1.102352 +    for(z=(unsigned char*)zLowerName; *z; z++){
1.102353 +      *z = sqlite3UpperToLower[*z];
1.102354 +    }
1.102355 +    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
1.102356 +    sqlite3DbFree(db, zLowerName);
1.102357 +  }
1.102358 +  if( rc==0 ){
1.102359 +    return pDef;
1.102360 +  }
1.102361 +
1.102362 +  /* Create a new ephemeral function definition for the overloaded
1.102363 +  ** function */
1.102364 +  pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
1.102365 +                             + sqlite3Strlen30(pDef->zName) + 1);
1.102366 +  if( pNew==0 ){
1.102367 +    return pDef;
1.102368 +  }
1.102369 +  *pNew = *pDef;
1.102370 +  pNew->zName = (char *)&pNew[1];
1.102371 +  memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
1.102372 +  pNew->xFunc = xFunc;
1.102373 +  pNew->pUserData = pArg;
1.102374 +  pNew->flags |= SQLITE_FUNC_EPHEM;
1.102375 +  return pNew;
1.102376 +}
1.102377 +
1.102378 +/*
1.102379 +** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
1.102380 +** array so that an OP_VBegin will get generated for it.  Add pTab to the
1.102381 +** array if it is missing.  If pTab is already in the array, this routine
1.102382 +** is a no-op.
1.102383 +*/
1.102384 +SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
1.102385 +  Parse *pToplevel = sqlite3ParseToplevel(pParse);
1.102386 +  int i, n;
1.102387 +  Table **apVtabLock;
1.102388 +
1.102389 +  assert( IsVirtual(pTab) );
1.102390 +  for(i=0; i<pToplevel->nVtabLock; i++){
1.102391 +    if( pTab==pToplevel->apVtabLock[i] ) return;
1.102392 +  }
1.102393 +  n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
1.102394 +  apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
1.102395 +  if( apVtabLock ){
1.102396 +    pToplevel->apVtabLock = apVtabLock;
1.102397 +    pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
1.102398 +  }else{
1.102399 +    pToplevel->db->mallocFailed = 1;
1.102400 +  }
1.102401 +}
1.102402 +
1.102403 +/*
1.102404 +** Return the ON CONFLICT resolution mode in effect for the virtual
1.102405 +** table update operation currently in progress.
1.102406 +**
1.102407 +** The results of this routine are undefined unless it is called from
1.102408 +** within an xUpdate method.
1.102409 +*/
1.102410 +SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
1.102411 +  static const unsigned char aMap[] = { 
1.102412 +    SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE 
1.102413 +  };
1.102414 +  assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
1.102415 +  assert( OE_Ignore==4 && OE_Replace==5 );
1.102416 +  assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
1.102417 +  return (int)aMap[db->vtabOnConflict-1];
1.102418 +}
1.102419 +
1.102420 +/*
1.102421 +** Call from within the xCreate() or xConnect() methods to provide 
1.102422 +** the SQLite core with additional information about the behavior
1.102423 +** of the virtual table being implemented.
1.102424 +*/
1.102425 +SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
1.102426 +  va_list ap;
1.102427 +  int rc = SQLITE_OK;
1.102428 +
1.102429 +  sqlite3_mutex_enter(db->mutex);
1.102430 +
1.102431 +  va_start(ap, op);
1.102432 +  switch( op ){
1.102433 +    case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
1.102434 +      VtabCtx *p = db->pVtabCtx;
1.102435 +      if( !p ){
1.102436 +        rc = SQLITE_MISUSE_BKPT;
1.102437 +      }else{
1.102438 +        assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
1.102439 +        p->pVTable->bConstraint = (u8)va_arg(ap, int);
1.102440 +      }
1.102441 +      break;
1.102442 +    }
1.102443 +    default:
1.102444 +      rc = SQLITE_MISUSE_BKPT;
1.102445 +      break;
1.102446 +  }
1.102447 +  va_end(ap);
1.102448 +
1.102449 +  if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
1.102450 +  sqlite3_mutex_leave(db->mutex);
1.102451 +  return rc;
1.102452 +}
1.102453 +
1.102454 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.102455 +
1.102456 +/************** End of vtab.c ************************************************/
1.102457 +/************** Begin file where.c *******************************************/
1.102458 +/*
1.102459 +** 2001 September 15
1.102460 +**
1.102461 +** The author disclaims copyright to this source code.  In place of
1.102462 +** a legal notice, here is a blessing:
1.102463 +**
1.102464 +**    May you do good and not evil.
1.102465 +**    May you find forgiveness for yourself and forgive others.
1.102466 +**    May you share freely, never taking more than you give.
1.102467 +**
1.102468 +*************************************************************************
1.102469 +** This module contains C code that generates VDBE code used to process
1.102470 +** the WHERE clause of SQL statements.  This module is responsible for
1.102471 +** generating the code that loops through a table looking for applicable
1.102472 +** rows.  Indices are selected and used to speed the search when doing
1.102473 +** so is applicable.  Because this module is responsible for selecting
1.102474 +** indices, you might also think of this module as the "query optimizer".
1.102475 +*/
1.102476 +
1.102477 +
1.102478 +/*
1.102479 +** Trace output macros
1.102480 +*/
1.102481 +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
1.102482 +/***/ int sqlite3WhereTrace = 0;
1.102483 +#endif
1.102484 +#if defined(SQLITE_DEBUG) \
1.102485 +    && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
1.102486 +# define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
1.102487 +#else
1.102488 +# define WHERETRACE(X)
1.102489 +#endif
1.102490 +
1.102491 +/* Forward reference
1.102492 +*/
1.102493 +typedef struct WhereClause WhereClause;
1.102494 +typedef struct WhereMaskSet WhereMaskSet;
1.102495 +typedef struct WhereOrInfo WhereOrInfo;
1.102496 +typedef struct WhereAndInfo WhereAndInfo;
1.102497 +typedef struct WhereCost WhereCost;
1.102498 +
1.102499 +/*
1.102500 +** The query generator uses an array of instances of this structure to
1.102501 +** help it analyze the subexpressions of the WHERE clause.  Each WHERE
1.102502 +** clause subexpression is separated from the others by AND operators,
1.102503 +** usually, or sometimes subexpressions separated by OR.
1.102504 +**
1.102505 +** All WhereTerms are collected into a single WhereClause structure.  
1.102506 +** The following identity holds:
1.102507 +**
1.102508 +**        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
1.102509 +**
1.102510 +** When a term is of the form:
1.102511 +**
1.102512 +**              X <op> <expr>
1.102513 +**
1.102514 +** where X is a column name and <op> is one of certain operators,
1.102515 +** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
1.102516 +** cursor number and column number for X.  WhereTerm.eOperator records
1.102517 +** the <op> using a bitmask encoding defined by WO_xxx below.  The
1.102518 +** use of a bitmask encoding for the operator allows us to search
1.102519 +** quickly for terms that match any of several different operators.
1.102520 +**
1.102521 +** A WhereTerm might also be two or more subterms connected by OR:
1.102522 +**
1.102523 +**         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
1.102524 +**
1.102525 +** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
1.102526 +** and the WhereTerm.u.pOrInfo field points to auxiliary information that
1.102527 +** is collected about the
1.102528 +**
1.102529 +** If a term in the WHERE clause does not match either of the two previous
1.102530 +** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
1.102531 +** to the original subexpression content and wtFlags is set up appropriately
1.102532 +** but no other fields in the WhereTerm object are meaningful.
1.102533 +**
1.102534 +** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
1.102535 +** but they do so indirectly.  A single WhereMaskSet structure translates
1.102536 +** cursor number into bits and the translated bit is stored in the prereq
1.102537 +** fields.  The translation is used in order to maximize the number of
1.102538 +** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
1.102539 +** spread out over the non-negative integers.  For example, the cursor
1.102540 +** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
1.102541 +** translates these sparse cursor numbers into consecutive integers
1.102542 +** beginning with 0 in order to make the best possible use of the available
1.102543 +** bits in the Bitmask.  So, in the example above, the cursor numbers
1.102544 +** would be mapped into integers 0 through 7.
1.102545 +**
1.102546 +** The number of terms in a join is limited by the number of bits
1.102547 +** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
1.102548 +** is only able to process joins with 64 or fewer tables.
1.102549 +*/
1.102550 +typedef struct WhereTerm WhereTerm;
1.102551 +struct WhereTerm {
1.102552 +  Expr *pExpr;            /* Pointer to the subexpression that is this term */
1.102553 +  int iParent;            /* Disable pWC->a[iParent] when this term disabled */
1.102554 +  int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
1.102555 +  union {
1.102556 +    int leftColumn;         /* Column number of X in "X <op> <expr>" */
1.102557 +    WhereOrInfo *pOrInfo;   /* Extra information if eOperator==WO_OR */
1.102558 +    WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
1.102559 +  } u;
1.102560 +  u16 eOperator;          /* A WO_xx value describing <op> */
1.102561 +  u8 wtFlags;             /* TERM_xxx bit flags.  See below */
1.102562 +  u8 nChild;              /* Number of children that must disable us */
1.102563 +  WhereClause *pWC;       /* The clause this term is part of */
1.102564 +  Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
1.102565 +  Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
1.102566 +};
1.102567 +
1.102568 +/*
1.102569 +** Allowed values of WhereTerm.wtFlags
1.102570 +*/
1.102571 +#define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
1.102572 +#define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
1.102573 +#define TERM_CODED      0x04   /* This term is already coded */
1.102574 +#define TERM_COPIED     0x08   /* Has a child */
1.102575 +#define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
1.102576 +#define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
1.102577 +#define TERM_OR_OK      0x40   /* Used during OR-clause processing */
1.102578 +#ifdef SQLITE_ENABLE_STAT3
1.102579 +#  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
1.102580 +#else
1.102581 +#  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
1.102582 +#endif
1.102583 +
1.102584 +/*
1.102585 +** An instance of the following structure holds all information about a
1.102586 +** WHERE clause.  Mostly this is a container for one or more WhereTerms.
1.102587 +**
1.102588 +** Explanation of pOuter:  For a WHERE clause of the form
1.102589 +**
1.102590 +**           a AND ((b AND c) OR (d AND e)) AND f
1.102591 +**
1.102592 +** There are separate WhereClause objects for the whole clause and for
1.102593 +** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
1.102594 +** subclauses points to the WhereClause object for the whole clause.
1.102595 +*/
1.102596 +struct WhereClause {
1.102597 +  Parse *pParse;           /* The parser context */
1.102598 +  WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
1.102599 +  Bitmask vmask;           /* Bitmask identifying virtual table cursors */
1.102600 +  WhereClause *pOuter;     /* Outer conjunction */
1.102601 +  u8 op;                   /* Split operator.  TK_AND or TK_OR */
1.102602 +  u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
1.102603 +  int nTerm;               /* Number of terms */
1.102604 +  int nSlot;               /* Number of entries in a[] */
1.102605 +  WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
1.102606 +#if defined(SQLITE_SMALL_STACK)
1.102607 +  WhereTerm aStatic[1];    /* Initial static space for a[] */
1.102608 +#else
1.102609 +  WhereTerm aStatic[8];    /* Initial static space for a[] */
1.102610 +#endif
1.102611 +};
1.102612 +
1.102613 +/*
1.102614 +** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
1.102615 +** a dynamically allocated instance of the following structure.
1.102616 +*/
1.102617 +struct WhereOrInfo {
1.102618 +  WhereClause wc;          /* Decomposition into subterms */
1.102619 +  Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
1.102620 +};
1.102621 +
1.102622 +/*
1.102623 +** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
1.102624 +** a dynamically allocated instance of the following structure.
1.102625 +*/
1.102626 +struct WhereAndInfo {
1.102627 +  WhereClause wc;          /* The subexpression broken out */
1.102628 +};
1.102629 +
1.102630 +/*
1.102631 +** An instance of the following structure keeps track of a mapping
1.102632 +** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
1.102633 +**
1.102634 +** The VDBE cursor numbers are small integers contained in 
1.102635 +** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE 
1.102636 +** clause, the cursor numbers might not begin with 0 and they might
1.102637 +** contain gaps in the numbering sequence.  But we want to make maximum
1.102638 +** use of the bits in our bitmasks.  This structure provides a mapping
1.102639 +** from the sparse cursor numbers into consecutive integers beginning
1.102640 +** with 0.
1.102641 +**
1.102642 +** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
1.102643 +** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
1.102644 +**
1.102645 +** For example, if the WHERE clause expression used these VDBE
1.102646 +** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
1.102647 +** would map those cursor numbers into bits 0 through 5.
1.102648 +**
1.102649 +** Note that the mapping is not necessarily ordered.  In the example
1.102650 +** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
1.102651 +** 57->5, 73->4.  Or one of 719 other combinations might be used. It
1.102652 +** does not really matter.  What is important is that sparse cursor
1.102653 +** numbers all get mapped into bit numbers that begin with 0 and contain
1.102654 +** no gaps.
1.102655 +*/
1.102656 +struct WhereMaskSet {
1.102657 +  int n;                        /* Number of assigned cursor values */
1.102658 +  int ix[BMS];                  /* Cursor assigned to each bit */
1.102659 +};
1.102660 +
1.102661 +/*
1.102662 +** A WhereCost object records a lookup strategy and the estimated
1.102663 +** cost of pursuing that strategy.
1.102664 +*/
1.102665 +struct WhereCost {
1.102666 +  WherePlan plan;    /* The lookup strategy */
1.102667 +  double rCost;      /* Overall cost of pursuing this search strategy */
1.102668 +  Bitmask used;      /* Bitmask of cursors used by this plan */
1.102669 +};
1.102670 +
1.102671 +/*
1.102672 +** Bitmasks for the operators that indices are able to exploit.  An
1.102673 +** OR-ed combination of these values can be used when searching for
1.102674 +** terms in the where clause.
1.102675 +*/
1.102676 +#define WO_IN     0x001
1.102677 +#define WO_EQ     0x002
1.102678 +#define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
1.102679 +#define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
1.102680 +#define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
1.102681 +#define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
1.102682 +#define WO_MATCH  0x040
1.102683 +#define WO_ISNULL 0x080
1.102684 +#define WO_OR     0x100       /* Two or more OR-connected terms */
1.102685 +#define WO_AND    0x200       /* Two or more AND-connected terms */
1.102686 +#define WO_NOOP   0x800       /* This term does not restrict search space */
1.102687 +
1.102688 +#define WO_ALL    0xfff       /* Mask of all possible WO_* values */
1.102689 +#define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
1.102690 +
1.102691 +/*
1.102692 +** Value for wsFlags returned by bestIndex() and stored in
1.102693 +** WhereLevel.wsFlags.  These flags determine which search
1.102694 +** strategies are appropriate.
1.102695 +**
1.102696 +** The least significant 12 bits is reserved as a mask for WO_ values above.
1.102697 +** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
1.102698 +** But if the table is the right table of a left join, WhereLevel.wsFlags
1.102699 +** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
1.102700 +** the "op" parameter to findTerm when we are resolving equality constraints.
1.102701 +** ISNULL constraints will then not be used on the right table of a left
1.102702 +** join.  Tickets #2177 and #2189.
1.102703 +*/
1.102704 +#define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
1.102705 +#define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
1.102706 +#define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
1.102707 +#define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
1.102708 +#define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
1.102709 +#define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
1.102710 +#define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
1.102711 +#define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
1.102712 +#define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
1.102713 +#define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
1.102714 +#define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
1.102715 +#define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
1.102716 +#define WHERE_IDX_ONLY     0x00400000  /* Use index only - omit table */
1.102717 +#define WHERE_ORDERED      0x00800000  /* Output will appear in correct order */
1.102718 +#define WHERE_REVERSE      0x01000000  /* Scan in reverse order */
1.102719 +#define WHERE_UNIQUE       0x02000000  /* Selects no more than one row */
1.102720 +#define WHERE_ALL_UNIQUE   0x04000000  /* This and all prior have one row */
1.102721 +#define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
1.102722 +#define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
1.102723 +#define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
1.102724 +#define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
1.102725 +#define WHERE_COVER_SCAN   0x80000000  /* Full scan of a covering index */
1.102726 +
1.102727 +/*
1.102728 +** This module contains many separate subroutines that work together to
1.102729 +** find the best indices to use for accessing a particular table in a query.
1.102730 +** An instance of the following structure holds context information about the
1.102731 +** index search so that it can be more easily passed between the various
1.102732 +** routines.
1.102733 +*/
1.102734 +typedef struct WhereBestIdx WhereBestIdx;
1.102735 +struct WhereBestIdx {
1.102736 +  Parse *pParse;                  /* Parser context */
1.102737 +  WhereClause *pWC;               /* The WHERE clause */
1.102738 +  struct SrcList_item *pSrc;      /* The FROM clause term to search */
1.102739 +  Bitmask notReady;               /* Mask of cursors not available */
1.102740 +  Bitmask notValid;               /* Cursors not available for any purpose */
1.102741 +  ExprList *pOrderBy;             /* The ORDER BY clause */
1.102742 +  ExprList *pDistinct;            /* The select-list if query is DISTINCT */
1.102743 +  sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
1.102744 +  int i, n;                       /* Which loop is being coded; # of loops */
1.102745 +  WhereLevel *aLevel;             /* Info about outer loops */
1.102746 +  WhereCost cost;                 /* Lowest cost query plan */
1.102747 +};
1.102748 +
1.102749 +/*
1.102750 +** Return TRUE if the probe cost is less than the baseline cost
1.102751 +*/
1.102752 +static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
1.102753 +  if( pProbe->rCost<pBaseline->rCost ) return 1;
1.102754 +  if( pProbe->rCost>pBaseline->rCost ) return 0;
1.102755 +  if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
1.102756 +  if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
1.102757 +  return 0;
1.102758 +}
1.102759 +
1.102760 +/*
1.102761 +** Initialize a preallocated WhereClause structure.
1.102762 +*/
1.102763 +static void whereClauseInit(
1.102764 +  WhereClause *pWC,        /* The WhereClause to be initialized */
1.102765 +  Parse *pParse,           /* The parsing context */
1.102766 +  WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
1.102767 +  u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
1.102768 +){
1.102769 +  pWC->pParse = pParse;
1.102770 +  pWC->pMaskSet = pMaskSet;
1.102771 +  pWC->pOuter = 0;
1.102772 +  pWC->nTerm = 0;
1.102773 +  pWC->nSlot = ArraySize(pWC->aStatic);
1.102774 +  pWC->a = pWC->aStatic;
1.102775 +  pWC->vmask = 0;
1.102776 +  pWC->wctrlFlags = wctrlFlags;
1.102777 +}
1.102778 +
1.102779 +/* Forward reference */
1.102780 +static void whereClauseClear(WhereClause*);
1.102781 +
1.102782 +/*
1.102783 +** Deallocate all memory associated with a WhereOrInfo object.
1.102784 +*/
1.102785 +static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
1.102786 +  whereClauseClear(&p->wc);
1.102787 +  sqlite3DbFree(db, p);
1.102788 +}
1.102789 +
1.102790 +/*
1.102791 +** Deallocate all memory associated with a WhereAndInfo object.
1.102792 +*/
1.102793 +static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
1.102794 +  whereClauseClear(&p->wc);
1.102795 +  sqlite3DbFree(db, p);
1.102796 +}
1.102797 +
1.102798 +/*
1.102799 +** Deallocate a WhereClause structure.  The WhereClause structure
1.102800 +** itself is not freed.  This routine is the inverse of whereClauseInit().
1.102801 +*/
1.102802 +static void whereClauseClear(WhereClause *pWC){
1.102803 +  int i;
1.102804 +  WhereTerm *a;
1.102805 +  sqlite3 *db = pWC->pParse->db;
1.102806 +  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
1.102807 +    if( a->wtFlags & TERM_DYNAMIC ){
1.102808 +      sqlite3ExprDelete(db, a->pExpr);
1.102809 +    }
1.102810 +    if( a->wtFlags & TERM_ORINFO ){
1.102811 +      whereOrInfoDelete(db, a->u.pOrInfo);
1.102812 +    }else if( a->wtFlags & TERM_ANDINFO ){
1.102813 +      whereAndInfoDelete(db, a->u.pAndInfo);
1.102814 +    }
1.102815 +  }
1.102816 +  if( pWC->a!=pWC->aStatic ){
1.102817 +    sqlite3DbFree(db, pWC->a);
1.102818 +  }
1.102819 +}
1.102820 +
1.102821 +/*
1.102822 +** Add a single new WhereTerm entry to the WhereClause object pWC.
1.102823 +** The new WhereTerm object is constructed from Expr p and with wtFlags.
1.102824 +** The index in pWC->a[] of the new WhereTerm is returned on success.
1.102825 +** 0 is returned if the new WhereTerm could not be added due to a memory
1.102826 +** allocation error.  The memory allocation failure will be recorded in
1.102827 +** the db->mallocFailed flag so that higher-level functions can detect it.
1.102828 +**
1.102829 +** This routine will increase the size of the pWC->a[] array as necessary.
1.102830 +**
1.102831 +** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
1.102832 +** for freeing the expression p is assumed by the WhereClause object pWC.
1.102833 +** This is true even if this routine fails to allocate a new WhereTerm.
1.102834 +**
1.102835 +** WARNING:  This routine might reallocate the space used to store
1.102836 +** WhereTerms.  All pointers to WhereTerms should be invalidated after
1.102837 +** calling this routine.  Such pointers may be reinitialized by referencing
1.102838 +** the pWC->a[] array.
1.102839 +*/
1.102840 +static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
1.102841 +  WhereTerm *pTerm;
1.102842 +  int idx;
1.102843 +  testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
1.102844 +  if( pWC->nTerm>=pWC->nSlot ){
1.102845 +    WhereTerm *pOld = pWC->a;
1.102846 +    sqlite3 *db = pWC->pParse->db;
1.102847 +    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
1.102848 +    if( pWC->a==0 ){
1.102849 +      if( wtFlags & TERM_DYNAMIC ){
1.102850 +        sqlite3ExprDelete(db, p);
1.102851 +      }
1.102852 +      pWC->a = pOld;
1.102853 +      return 0;
1.102854 +    }
1.102855 +    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
1.102856 +    if( pOld!=pWC->aStatic ){
1.102857 +      sqlite3DbFree(db, pOld);
1.102858 +    }
1.102859 +    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
1.102860 +  }
1.102861 +  pTerm = &pWC->a[idx = pWC->nTerm++];
1.102862 +  pTerm->pExpr = p;
1.102863 +  pTerm->wtFlags = wtFlags;
1.102864 +  pTerm->pWC = pWC;
1.102865 +  pTerm->iParent = -1;
1.102866 +  return idx;
1.102867 +}
1.102868 +
1.102869 +/*
1.102870 +** This routine identifies subexpressions in the WHERE clause where
1.102871 +** each subexpression is separated by the AND operator or some other
1.102872 +** operator specified in the op parameter.  The WhereClause structure
1.102873 +** is filled with pointers to subexpressions.  For example:
1.102874 +**
1.102875 +**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
1.102876 +**           \________/     \_______________/     \________________/
1.102877 +**            slot[0]            slot[1]               slot[2]
1.102878 +**
1.102879 +** The original WHERE clause in pExpr is unaltered.  All this routine
1.102880 +** does is make slot[] entries point to substructure within pExpr.
1.102881 +**
1.102882 +** In the previous sentence and in the diagram, "slot[]" refers to
1.102883 +** the WhereClause.a[] array.  The slot[] array grows as needed to contain
1.102884 +** all terms of the WHERE clause.
1.102885 +*/
1.102886 +static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
1.102887 +  pWC->op = (u8)op;
1.102888 +  if( pExpr==0 ) return;
1.102889 +  if( pExpr->op!=op ){
1.102890 +    whereClauseInsert(pWC, pExpr, 0);
1.102891 +  }else{
1.102892 +    whereSplit(pWC, pExpr->pLeft, op);
1.102893 +    whereSplit(pWC, pExpr->pRight, op);
1.102894 +  }
1.102895 +}
1.102896 +
1.102897 +/*
1.102898 +** Initialize an expression mask set (a WhereMaskSet object)
1.102899 +*/
1.102900 +#define initMaskSet(P)  memset(P, 0, sizeof(*P))
1.102901 +
1.102902 +/*
1.102903 +** Return the bitmask for the given cursor number.  Return 0 if
1.102904 +** iCursor is not in the set.
1.102905 +*/
1.102906 +static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
1.102907 +  int i;
1.102908 +  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
1.102909 +  for(i=0; i<pMaskSet->n; i++){
1.102910 +    if( pMaskSet->ix[i]==iCursor ){
1.102911 +      return ((Bitmask)1)<<i;
1.102912 +    }
1.102913 +  }
1.102914 +  return 0;
1.102915 +}
1.102916 +
1.102917 +/*
1.102918 +** Create a new mask for cursor iCursor.
1.102919 +**
1.102920 +** There is one cursor per table in the FROM clause.  The number of
1.102921 +** tables in the FROM clause is limited by a test early in the
1.102922 +** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
1.102923 +** array will never overflow.
1.102924 +*/
1.102925 +static void createMask(WhereMaskSet *pMaskSet, int iCursor){
1.102926 +  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
1.102927 +  pMaskSet->ix[pMaskSet->n++] = iCursor;
1.102928 +}
1.102929 +
1.102930 +/*
1.102931 +** This routine walks (recursively) an expression tree and generates
1.102932 +** a bitmask indicating which tables are used in that expression
1.102933 +** tree.
1.102934 +**
1.102935 +** In order for this routine to work, the calling function must have
1.102936 +** previously invoked sqlite3ResolveExprNames() on the expression.  See
1.102937 +** the header comment on that routine for additional information.
1.102938 +** The sqlite3ResolveExprNames() routines looks for column names and
1.102939 +** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
1.102940 +** the VDBE cursor number of the table.  This routine just has to
1.102941 +** translate the cursor numbers into bitmask values and OR all
1.102942 +** the bitmasks together.
1.102943 +*/
1.102944 +static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
1.102945 +static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
1.102946 +static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
1.102947 +  Bitmask mask = 0;
1.102948 +  if( p==0 ) return 0;
1.102949 +  if( p->op==TK_COLUMN ){
1.102950 +    mask = getMask(pMaskSet, p->iTable);
1.102951 +    return mask;
1.102952 +  }
1.102953 +  mask = exprTableUsage(pMaskSet, p->pRight);
1.102954 +  mask |= exprTableUsage(pMaskSet, p->pLeft);
1.102955 +  if( ExprHasProperty(p, EP_xIsSelect) ){
1.102956 +    mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
1.102957 +  }else{
1.102958 +    mask |= exprListTableUsage(pMaskSet, p->x.pList);
1.102959 +  }
1.102960 +  return mask;
1.102961 +}
1.102962 +static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
1.102963 +  int i;
1.102964 +  Bitmask mask = 0;
1.102965 +  if( pList ){
1.102966 +    for(i=0; i<pList->nExpr; i++){
1.102967 +      mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
1.102968 +    }
1.102969 +  }
1.102970 +  return mask;
1.102971 +}
1.102972 +static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
1.102973 +  Bitmask mask = 0;
1.102974 +  while( pS ){
1.102975 +    SrcList *pSrc = pS->pSrc;
1.102976 +    mask |= exprListTableUsage(pMaskSet, pS->pEList);
1.102977 +    mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
1.102978 +    mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
1.102979 +    mask |= exprTableUsage(pMaskSet, pS->pWhere);
1.102980 +    mask |= exprTableUsage(pMaskSet, pS->pHaving);
1.102981 +    if( ALWAYS(pSrc!=0) ){
1.102982 +      int i;
1.102983 +      for(i=0; i<pSrc->nSrc; i++){
1.102984 +        mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
1.102985 +        mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
1.102986 +      }
1.102987 +    }
1.102988 +    pS = pS->pPrior;
1.102989 +  }
1.102990 +  return mask;
1.102991 +}
1.102992 +
1.102993 +/*
1.102994 +** Return TRUE if the given operator is one of the operators that is
1.102995 +** allowed for an indexable WHERE clause term.  The allowed operators are
1.102996 +** "=", "<", ">", "<=", ">=", and "IN".
1.102997 +**
1.102998 +** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
1.102999 +** of one of the following forms: column = expression column > expression
1.103000 +** column >= expression column < expression column <= expression
1.103001 +** expression = column expression > column expression >= column
1.103002 +** expression < column expression <= column column IN
1.103003 +** (expression-list) column IN (subquery) column IS NULL
1.103004 +*/
1.103005 +static int allowedOp(int op){
1.103006 +  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
1.103007 +  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
1.103008 +  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
1.103009 +  assert( TK_GE==TK_EQ+4 );
1.103010 +  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
1.103011 +}
1.103012 +
1.103013 +/*
1.103014 +** Swap two objects of type TYPE.
1.103015 +*/
1.103016 +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
1.103017 +
1.103018 +/*
1.103019 +** Commute a comparison operator.  Expressions of the form "X op Y"
1.103020 +** are converted into "Y op X".
1.103021 +**
1.103022 +** If left/right precendence rules come into play when determining the
1.103023 +** collating
1.103024 +** side of the comparison, it remains associated with the same side after
1.103025 +** the commutation. So "Y collate NOCASE op X" becomes 
1.103026 +** "X op Y". This is because any collation sequence on
1.103027 +** the left hand side of a comparison overrides any collation sequence 
1.103028 +** attached to the right. For the same reason the EP_Collate flag
1.103029 +** is not commuted.
1.103030 +*/
1.103031 +static void exprCommute(Parse *pParse, Expr *pExpr){
1.103032 +  u16 expRight = (pExpr->pRight->flags & EP_Collate);
1.103033 +  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
1.103034 +  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
1.103035 +  if( expRight==expLeft ){
1.103036 +    /* Either X and Y both have COLLATE operator or neither do */
1.103037 +    if( expRight ){
1.103038 +      /* Both X and Y have COLLATE operators.  Make sure X is always
1.103039 +      ** used by clearing the EP_Collate flag from Y. */
1.103040 +      pExpr->pRight->flags &= ~EP_Collate;
1.103041 +    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
1.103042 +      /* Neither X nor Y have COLLATE operators, but X has a non-default
1.103043 +      ** collating sequence.  So add the EP_Collate marker on X to cause
1.103044 +      ** it to be searched first. */
1.103045 +      pExpr->pLeft->flags |= EP_Collate;
1.103046 +    }
1.103047 +  }
1.103048 +  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
1.103049 +  if( pExpr->op>=TK_GT ){
1.103050 +    assert( TK_LT==TK_GT+2 );
1.103051 +    assert( TK_GE==TK_LE+2 );
1.103052 +    assert( TK_GT>TK_EQ );
1.103053 +    assert( TK_GT<TK_LE );
1.103054 +    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
1.103055 +    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
1.103056 +  }
1.103057 +}
1.103058 +
1.103059 +/*
1.103060 +** Translate from TK_xx operator to WO_xx bitmask.
1.103061 +*/
1.103062 +static u16 operatorMask(int op){
1.103063 +  u16 c;
1.103064 +  assert( allowedOp(op) );
1.103065 +  if( op==TK_IN ){
1.103066 +    c = WO_IN;
1.103067 +  }else if( op==TK_ISNULL ){
1.103068 +    c = WO_ISNULL;
1.103069 +  }else{
1.103070 +    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
1.103071 +    c = (u16)(WO_EQ<<(op-TK_EQ));
1.103072 +  }
1.103073 +  assert( op!=TK_ISNULL || c==WO_ISNULL );
1.103074 +  assert( op!=TK_IN || c==WO_IN );
1.103075 +  assert( op!=TK_EQ || c==WO_EQ );
1.103076 +  assert( op!=TK_LT || c==WO_LT );
1.103077 +  assert( op!=TK_LE || c==WO_LE );
1.103078 +  assert( op!=TK_GT || c==WO_GT );
1.103079 +  assert( op!=TK_GE || c==WO_GE );
1.103080 +  return c;
1.103081 +}
1.103082 +
1.103083 +/*
1.103084 +** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
1.103085 +** where X is a reference to the iColumn of table iCur and <op> is one of
1.103086 +** the WO_xx operator codes specified by the op parameter.
1.103087 +** Return a pointer to the term.  Return 0 if not found.
1.103088 +*/
1.103089 +static WhereTerm *findTerm(
1.103090 +  WhereClause *pWC,     /* The WHERE clause to be searched */
1.103091 +  int iCur,             /* Cursor number of LHS */
1.103092 +  int iColumn,          /* Column number of LHS */
1.103093 +  Bitmask notReady,     /* RHS must not overlap with this mask */
1.103094 +  u32 op,               /* Mask of WO_xx values describing operator */
1.103095 +  Index *pIdx           /* Must be compatible with this index, if not NULL */
1.103096 +){
1.103097 +  WhereTerm *pTerm;
1.103098 +  int k;
1.103099 +  assert( iCur>=0 );
1.103100 +  op &= WO_ALL;
1.103101 +  for(; pWC; pWC=pWC->pOuter){
1.103102 +    for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
1.103103 +      if( pTerm->leftCursor==iCur
1.103104 +         && (pTerm->prereqRight & notReady)==0
1.103105 +         && pTerm->u.leftColumn==iColumn
1.103106 +         && (pTerm->eOperator & op)!=0
1.103107 +      ){
1.103108 +        if( iColumn>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
1.103109 +          Expr *pX = pTerm->pExpr;
1.103110 +          CollSeq *pColl;
1.103111 +          char idxaff;
1.103112 +          int j;
1.103113 +          Parse *pParse = pWC->pParse;
1.103114 +  
1.103115 +          idxaff = pIdx->pTable->aCol[iColumn].affinity;
1.103116 +          if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
1.103117 +  
1.103118 +          /* Figure out the collation sequence required from an index for
1.103119 +          ** it to be useful for optimising expression pX. Store this
1.103120 +          ** value in variable pColl.
1.103121 +          */
1.103122 +          assert(pX->pLeft);
1.103123 +          pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
1.103124 +          if( pColl==0 ) pColl = pParse->db->pDfltColl;
1.103125 +  
1.103126 +          for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
1.103127 +            if( NEVER(j>=pIdx->nColumn) ) return 0;
1.103128 +          }
1.103129 +          if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
1.103130 +        }
1.103131 +        return pTerm;
1.103132 +      }
1.103133 +    }
1.103134 +  }
1.103135 +  return 0;
1.103136 +}
1.103137 +
1.103138 +/* Forward reference */
1.103139 +static void exprAnalyze(SrcList*, WhereClause*, int);
1.103140 +
1.103141 +/*
1.103142 +** Call exprAnalyze on all terms in a WHERE clause.  
1.103143 +**
1.103144 +**
1.103145 +*/
1.103146 +static void exprAnalyzeAll(
1.103147 +  SrcList *pTabList,       /* the FROM clause */
1.103148 +  WhereClause *pWC         /* the WHERE clause to be analyzed */
1.103149 +){
1.103150 +  int i;
1.103151 +  for(i=pWC->nTerm-1; i>=0; i--){
1.103152 +    exprAnalyze(pTabList, pWC, i);
1.103153 +  }
1.103154 +}
1.103155 +
1.103156 +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
1.103157 +/*
1.103158 +** Check to see if the given expression is a LIKE or GLOB operator that
1.103159 +** can be optimized using inequality constraints.  Return TRUE if it is
1.103160 +** so and false if not.
1.103161 +**
1.103162 +** In order for the operator to be optimizible, the RHS must be a string
1.103163 +** literal that does not begin with a wildcard.  
1.103164 +*/
1.103165 +static int isLikeOrGlob(
1.103166 +  Parse *pParse,    /* Parsing and code generating context */
1.103167 +  Expr *pExpr,      /* Test this expression */
1.103168 +  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
1.103169 +  int *pisComplete, /* True if the only wildcard is % in the last character */
1.103170 +  int *pnoCase      /* True if uppercase is equivalent to lowercase */
1.103171 +){
1.103172 +  const char *z = 0;         /* String on RHS of LIKE operator */
1.103173 +  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
1.103174 +  ExprList *pList;           /* List of operands to the LIKE operator */
1.103175 +  int c;                     /* One character in z[] */
1.103176 +  int cnt;                   /* Number of non-wildcard prefix characters */
1.103177 +  char wc[3];                /* Wildcard characters */
1.103178 +  sqlite3 *db = pParse->db;  /* Database connection */
1.103179 +  sqlite3_value *pVal = 0;
1.103180 +  int op;                    /* Opcode of pRight */
1.103181 +
1.103182 +  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
1.103183 +    return 0;
1.103184 +  }
1.103185 +#ifdef SQLITE_EBCDIC
1.103186 +  if( *pnoCase ) return 0;
1.103187 +#endif
1.103188 +  pList = pExpr->x.pList;
1.103189 +  pLeft = pList->a[1].pExpr;
1.103190 +  if( pLeft->op!=TK_COLUMN 
1.103191 +   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
1.103192 +   || IsVirtual(pLeft->pTab)
1.103193 +  ){
1.103194 +    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
1.103195 +    ** be the name of an indexed column with TEXT affinity. */
1.103196 +    return 0;
1.103197 +  }
1.103198 +  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
1.103199 +
1.103200 +  pRight = pList->a[0].pExpr;
1.103201 +  op = pRight->op;
1.103202 +  if( op==TK_REGISTER ){
1.103203 +    op = pRight->op2;
1.103204 +  }
1.103205 +  if( op==TK_VARIABLE ){
1.103206 +    Vdbe *pReprepare = pParse->pReprepare;
1.103207 +    int iCol = pRight->iColumn;
1.103208 +    pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
1.103209 +    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
1.103210 +      z = (char *)sqlite3_value_text(pVal);
1.103211 +    }
1.103212 +    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
1.103213 +    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
1.103214 +  }else if( op==TK_STRING ){
1.103215 +    z = pRight->u.zToken;
1.103216 +  }
1.103217 +  if( z ){
1.103218 +    cnt = 0;
1.103219 +    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
1.103220 +      cnt++;
1.103221 +    }
1.103222 +    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
1.103223 +      Expr *pPrefix;
1.103224 +      *pisComplete = c==wc[0] && z[cnt+1]==0;
1.103225 +      pPrefix = sqlite3Expr(db, TK_STRING, z);
1.103226 +      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
1.103227 +      *ppPrefix = pPrefix;
1.103228 +      if( op==TK_VARIABLE ){
1.103229 +        Vdbe *v = pParse->pVdbe;
1.103230 +        sqlite3VdbeSetVarmask(v, pRight->iColumn);
1.103231 +        if( *pisComplete && pRight->u.zToken[1] ){
1.103232 +          /* If the rhs of the LIKE expression is a variable, and the current
1.103233 +          ** value of the variable means there is no need to invoke the LIKE
1.103234 +          ** function, then no OP_Variable will be added to the program.
1.103235 +          ** This causes problems for the sqlite3_bind_parameter_name()
1.103236 +          ** API. To workaround them, add a dummy OP_Variable here.
1.103237 +          */ 
1.103238 +          int r1 = sqlite3GetTempReg(pParse);
1.103239 +          sqlite3ExprCodeTarget(pParse, pRight, r1);
1.103240 +          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
1.103241 +          sqlite3ReleaseTempReg(pParse, r1);
1.103242 +        }
1.103243 +      }
1.103244 +    }else{
1.103245 +      z = 0;
1.103246 +    }
1.103247 +  }
1.103248 +
1.103249 +  sqlite3ValueFree(pVal);
1.103250 +  return (z!=0);
1.103251 +}
1.103252 +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
1.103253 +
1.103254 +
1.103255 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.103256 +/*
1.103257 +** Check to see if the given expression is of the form
1.103258 +**
1.103259 +**         column MATCH expr
1.103260 +**
1.103261 +** If it is then return TRUE.  If not, return FALSE.
1.103262 +*/
1.103263 +static int isMatchOfColumn(
1.103264 +  Expr *pExpr      /* Test this expression */
1.103265 +){
1.103266 +  ExprList *pList;
1.103267 +
1.103268 +  if( pExpr->op!=TK_FUNCTION ){
1.103269 +    return 0;
1.103270 +  }
1.103271 +  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
1.103272 +    return 0;
1.103273 +  }
1.103274 +  pList = pExpr->x.pList;
1.103275 +  if( pList->nExpr!=2 ){
1.103276 +    return 0;
1.103277 +  }
1.103278 +  if( pList->a[1].pExpr->op != TK_COLUMN ){
1.103279 +    return 0;
1.103280 +  }
1.103281 +  return 1;
1.103282 +}
1.103283 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.103284 +
1.103285 +/*
1.103286 +** If the pBase expression originated in the ON or USING clause of
1.103287 +** a join, then transfer the appropriate markings over to derived.
1.103288 +*/
1.103289 +static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
1.103290 +  pDerived->flags |= pBase->flags & EP_FromJoin;
1.103291 +  pDerived->iRightJoinTable = pBase->iRightJoinTable;
1.103292 +}
1.103293 +
1.103294 +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
1.103295 +/*
1.103296 +** Analyze a term that consists of two or more OR-connected
1.103297 +** subterms.  So in:
1.103298 +**
1.103299 +**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
1.103300 +**                          ^^^^^^^^^^^^^^^^^^^^
1.103301 +**
1.103302 +** This routine analyzes terms such as the middle term in the above example.
1.103303 +** A WhereOrTerm object is computed and attached to the term under
1.103304 +** analysis, regardless of the outcome of the analysis.  Hence:
1.103305 +**
1.103306 +**     WhereTerm.wtFlags   |=  TERM_ORINFO
1.103307 +**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
1.103308 +**
1.103309 +** The term being analyzed must have two or more of OR-connected subterms.
1.103310 +** A single subterm might be a set of AND-connected sub-subterms.
1.103311 +** Examples of terms under analysis:
1.103312 +**
1.103313 +**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
1.103314 +**     (B)     x=expr1 OR expr2=x OR x=expr3
1.103315 +**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
1.103316 +**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
1.103317 +**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
1.103318 +**
1.103319 +** CASE 1:
1.103320 +**
1.103321 +** If all subterms are of the form T.C=expr for some single column of C
1.103322 +** a single table T (as shown in example B above) then create a new virtual
1.103323 +** term that is an equivalent IN expression.  In other words, if the term
1.103324 +** being analyzed is:
1.103325 +**
1.103326 +**      x = expr1  OR  expr2 = x  OR  x = expr3
1.103327 +**
1.103328 +** then create a new virtual term like this:
1.103329 +**
1.103330 +**      x IN (expr1,expr2,expr3)
1.103331 +**
1.103332 +** CASE 2:
1.103333 +**
1.103334 +** If all subterms are indexable by a single table T, then set
1.103335 +**
1.103336 +**     WhereTerm.eOperator              =  WO_OR
1.103337 +**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
1.103338 +**
1.103339 +** A subterm is "indexable" if it is of the form
1.103340 +** "T.C <op> <expr>" where C is any column of table T and 
1.103341 +** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
1.103342 +** A subterm is also indexable if it is an AND of two or more
1.103343 +** subsubterms at least one of which is indexable.  Indexable AND 
1.103344 +** subterms have their eOperator set to WO_AND and they have
1.103345 +** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
1.103346 +**
1.103347 +** From another point of view, "indexable" means that the subterm could
1.103348 +** potentially be used with an index if an appropriate index exists.
1.103349 +** This analysis does not consider whether or not the index exists; that
1.103350 +** is something the bestIndex() routine will determine.  This analysis
1.103351 +** only looks at whether subterms appropriate for indexing exist.
1.103352 +**
1.103353 +** All examples A through E above all satisfy case 2.  But if a term
1.103354 +** also statisfies case 1 (such as B) we know that the optimizer will
1.103355 +** always prefer case 1, so in that case we pretend that case 2 is not
1.103356 +** satisfied.
1.103357 +**
1.103358 +** It might be the case that multiple tables are indexable.  For example,
1.103359 +** (E) above is indexable on tables P, Q, and R.
1.103360 +**
1.103361 +** Terms that satisfy case 2 are candidates for lookup by using
1.103362 +** separate indices to find rowids for each subterm and composing
1.103363 +** the union of all rowids using a RowSet object.  This is similar
1.103364 +** to "bitmap indices" in other database engines.
1.103365 +**
1.103366 +** OTHERWISE:
1.103367 +**
1.103368 +** If neither case 1 nor case 2 apply, then leave the eOperator set to
1.103369 +** zero.  This term is not useful for search.
1.103370 +*/
1.103371 +static void exprAnalyzeOrTerm(
1.103372 +  SrcList *pSrc,            /* the FROM clause */
1.103373 +  WhereClause *pWC,         /* the complete WHERE clause */
1.103374 +  int idxTerm               /* Index of the OR-term to be analyzed */
1.103375 +){
1.103376 +  Parse *pParse = pWC->pParse;            /* Parser context */
1.103377 +  sqlite3 *db = pParse->db;               /* Database connection */
1.103378 +  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
1.103379 +  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
1.103380 +  WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
1.103381 +  int i;                                  /* Loop counters */
1.103382 +  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
1.103383 +  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
1.103384 +  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
1.103385 +  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
1.103386 +  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
1.103387 +
1.103388 +  /*
1.103389 +  ** Break the OR clause into its separate subterms.  The subterms are
1.103390 +  ** stored in a WhereClause structure containing within the WhereOrInfo
1.103391 +  ** object that is attached to the original OR clause term.
1.103392 +  */
1.103393 +  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
1.103394 +  assert( pExpr->op==TK_OR );
1.103395 +  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
1.103396 +  if( pOrInfo==0 ) return;
1.103397 +  pTerm->wtFlags |= TERM_ORINFO;
1.103398 +  pOrWc = &pOrInfo->wc;
1.103399 +  whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
1.103400 +  whereSplit(pOrWc, pExpr, TK_OR);
1.103401 +  exprAnalyzeAll(pSrc, pOrWc);
1.103402 +  if( db->mallocFailed ) return;
1.103403 +  assert( pOrWc->nTerm>=2 );
1.103404 +
1.103405 +  /*
1.103406 +  ** Compute the set of tables that might satisfy cases 1 or 2.
1.103407 +  */
1.103408 +  indexable = ~(Bitmask)0;
1.103409 +  chngToIN = ~(pWC->vmask);
1.103410 +  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
1.103411 +    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
1.103412 +      WhereAndInfo *pAndInfo;
1.103413 +      assert( pOrTerm->eOperator==0 );
1.103414 +      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
1.103415 +      chngToIN = 0;
1.103416 +      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
1.103417 +      if( pAndInfo ){
1.103418 +        WhereClause *pAndWC;
1.103419 +        WhereTerm *pAndTerm;
1.103420 +        int j;
1.103421 +        Bitmask b = 0;
1.103422 +        pOrTerm->u.pAndInfo = pAndInfo;
1.103423 +        pOrTerm->wtFlags |= TERM_ANDINFO;
1.103424 +        pOrTerm->eOperator = WO_AND;
1.103425 +        pAndWC = &pAndInfo->wc;
1.103426 +        whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
1.103427 +        whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
1.103428 +        exprAnalyzeAll(pSrc, pAndWC);
1.103429 +        pAndWC->pOuter = pWC;
1.103430 +        testcase( db->mallocFailed );
1.103431 +        if( !db->mallocFailed ){
1.103432 +          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
1.103433 +            assert( pAndTerm->pExpr );
1.103434 +            if( allowedOp(pAndTerm->pExpr->op) ){
1.103435 +              b |= getMask(pMaskSet, pAndTerm->leftCursor);
1.103436 +            }
1.103437 +          }
1.103438 +        }
1.103439 +        indexable &= b;
1.103440 +      }
1.103441 +    }else if( pOrTerm->wtFlags & TERM_COPIED ){
1.103442 +      /* Skip this term for now.  We revisit it when we process the
1.103443 +      ** corresponding TERM_VIRTUAL term */
1.103444 +    }else{
1.103445 +      Bitmask b;
1.103446 +      b = getMask(pMaskSet, pOrTerm->leftCursor);
1.103447 +      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
1.103448 +        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
1.103449 +        b |= getMask(pMaskSet, pOther->leftCursor);
1.103450 +      }
1.103451 +      indexable &= b;
1.103452 +      if( pOrTerm->eOperator!=WO_EQ ){
1.103453 +        chngToIN = 0;
1.103454 +      }else{
1.103455 +        chngToIN &= b;
1.103456 +      }
1.103457 +    }
1.103458 +  }
1.103459 +
1.103460 +  /*
1.103461 +  ** Record the set of tables that satisfy case 2.  The set might be
1.103462 +  ** empty.
1.103463 +  */
1.103464 +  pOrInfo->indexable = indexable;
1.103465 +  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
1.103466 +
1.103467 +  /*
1.103468 +  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
1.103469 +  ** we have to do some additional checking to see if case 1 really
1.103470 +  ** is satisfied.
1.103471 +  **
1.103472 +  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
1.103473 +  ** that there is no possibility of transforming the OR clause into an
1.103474 +  ** IN operator because one or more terms in the OR clause contain
1.103475 +  ** something other than == on a column in the single table.  The 1-bit
1.103476 +  ** case means that every term of the OR clause is of the form
1.103477 +  ** "table.column=expr" for some single table.  The one bit that is set
1.103478 +  ** will correspond to the common table.  We still need to check to make
1.103479 +  ** sure the same column is used on all terms.  The 2-bit case is when
1.103480 +  ** the all terms are of the form "table1.column=table2.column".  It
1.103481 +  ** might be possible to form an IN operator with either table1.column
1.103482 +  ** or table2.column as the LHS if either is common to every term of
1.103483 +  ** the OR clause.
1.103484 +  **
1.103485 +  ** Note that terms of the form "table.column1=table.column2" (the
1.103486 +  ** same table on both sizes of the ==) cannot be optimized.
1.103487 +  */
1.103488 +  if( chngToIN ){
1.103489 +    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
1.103490 +    int iColumn = -1;         /* Column index on lhs of IN operator */
1.103491 +    int iCursor = -1;         /* Table cursor common to all terms */
1.103492 +    int j = 0;                /* Loop counter */
1.103493 +
1.103494 +    /* Search for a table and column that appears on one side or the
1.103495 +    ** other of the == operator in every subterm.  That table and column
1.103496 +    ** will be recorded in iCursor and iColumn.  There might not be any
1.103497 +    ** such table and column.  Set okToChngToIN if an appropriate table
1.103498 +    ** and column is found but leave okToChngToIN false if not found.
1.103499 +    */
1.103500 +    for(j=0; j<2 && !okToChngToIN; j++){
1.103501 +      pOrTerm = pOrWc->a;
1.103502 +      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
1.103503 +        assert( pOrTerm->eOperator==WO_EQ );
1.103504 +        pOrTerm->wtFlags &= ~TERM_OR_OK;
1.103505 +        if( pOrTerm->leftCursor==iCursor ){
1.103506 +          /* This is the 2-bit case and we are on the second iteration and
1.103507 +          ** current term is from the first iteration.  So skip this term. */
1.103508 +          assert( j==1 );
1.103509 +          continue;
1.103510 +        }
1.103511 +        if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
1.103512 +          /* This term must be of the form t1.a==t2.b where t2 is in the
1.103513 +          ** chngToIN set but t1 is not.  This term will be either preceeded
1.103514 +          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
1.103515 +          ** and use its inversion. */
1.103516 +          testcase( pOrTerm->wtFlags & TERM_COPIED );
1.103517 +          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
1.103518 +          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
1.103519 +          continue;
1.103520 +        }
1.103521 +        iColumn = pOrTerm->u.leftColumn;
1.103522 +        iCursor = pOrTerm->leftCursor;
1.103523 +        break;
1.103524 +      }
1.103525 +      if( i<0 ){
1.103526 +        /* No candidate table+column was found.  This can only occur
1.103527 +        ** on the second iteration */
1.103528 +        assert( j==1 );
1.103529 +        assert( (chngToIN&(chngToIN-1))==0 );
1.103530 +        assert( chngToIN==getMask(pMaskSet, iCursor) );
1.103531 +        break;
1.103532 +      }
1.103533 +      testcase( j==1 );
1.103534 +
1.103535 +      /* We have found a candidate table and column.  Check to see if that
1.103536 +      ** table and column is common to every term in the OR clause */
1.103537 +      okToChngToIN = 1;
1.103538 +      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
1.103539 +        assert( pOrTerm->eOperator==WO_EQ );
1.103540 +        if( pOrTerm->leftCursor!=iCursor ){
1.103541 +          pOrTerm->wtFlags &= ~TERM_OR_OK;
1.103542 +        }else if( pOrTerm->u.leftColumn!=iColumn ){
1.103543 +          okToChngToIN = 0;
1.103544 +        }else{
1.103545 +          int affLeft, affRight;
1.103546 +          /* If the right-hand side is also a column, then the affinities
1.103547 +          ** of both right and left sides must be such that no type
1.103548 +          ** conversions are required on the right.  (Ticket #2249)
1.103549 +          */
1.103550 +          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
1.103551 +          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
1.103552 +          if( affRight!=0 && affRight!=affLeft ){
1.103553 +            okToChngToIN = 0;
1.103554 +          }else{
1.103555 +            pOrTerm->wtFlags |= TERM_OR_OK;
1.103556 +          }
1.103557 +        }
1.103558 +      }
1.103559 +    }
1.103560 +
1.103561 +    /* At this point, okToChngToIN is true if original pTerm satisfies
1.103562 +    ** case 1.  In that case, construct a new virtual term that is 
1.103563 +    ** pTerm converted into an IN operator.
1.103564 +    **
1.103565 +    ** EV: R-00211-15100
1.103566 +    */
1.103567 +    if( okToChngToIN ){
1.103568 +      Expr *pDup;            /* A transient duplicate expression */
1.103569 +      ExprList *pList = 0;   /* The RHS of the IN operator */
1.103570 +      Expr *pLeft = 0;       /* The LHS of the IN operator */
1.103571 +      Expr *pNew;            /* The complete IN operator */
1.103572 +
1.103573 +      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
1.103574 +        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
1.103575 +        assert( pOrTerm->eOperator==WO_EQ );
1.103576 +        assert( pOrTerm->leftCursor==iCursor );
1.103577 +        assert( pOrTerm->u.leftColumn==iColumn );
1.103578 +        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
1.103579 +        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
1.103580 +        pLeft = pOrTerm->pExpr->pLeft;
1.103581 +      }
1.103582 +      assert( pLeft!=0 );
1.103583 +      pDup = sqlite3ExprDup(db, pLeft, 0);
1.103584 +      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
1.103585 +      if( pNew ){
1.103586 +        int idxNew;
1.103587 +        transferJoinMarkings(pNew, pExpr);
1.103588 +        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
1.103589 +        pNew->x.pList = pList;
1.103590 +        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
1.103591 +        testcase( idxNew==0 );
1.103592 +        exprAnalyze(pSrc, pWC, idxNew);
1.103593 +        pTerm = &pWC->a[idxTerm];
1.103594 +        pWC->a[idxNew].iParent = idxTerm;
1.103595 +        pTerm->nChild = 1;
1.103596 +      }else{
1.103597 +        sqlite3ExprListDelete(db, pList);
1.103598 +      }
1.103599 +      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
1.103600 +    }
1.103601 +  }
1.103602 +}
1.103603 +#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
1.103604 +
1.103605 +
1.103606 +/*
1.103607 +** The input to this routine is an WhereTerm structure with only the
1.103608 +** "pExpr" field filled in.  The job of this routine is to analyze the
1.103609 +** subexpression and populate all the other fields of the WhereTerm
1.103610 +** structure.
1.103611 +**
1.103612 +** If the expression is of the form "<expr> <op> X" it gets commuted
1.103613 +** to the standard form of "X <op> <expr>".
1.103614 +**
1.103615 +** If the expression is of the form "X <op> Y" where both X and Y are
1.103616 +** columns, then the original expression is unchanged and a new virtual
1.103617 +** term of the form "Y <op> X" is added to the WHERE clause and
1.103618 +** analyzed separately.  The original term is marked with TERM_COPIED
1.103619 +** and the new term is marked with TERM_DYNAMIC (because it's pExpr
1.103620 +** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
1.103621 +** is a commuted copy of a prior term.)  The original term has nChild=1
1.103622 +** and the copy has idxParent set to the index of the original term.
1.103623 +*/
1.103624 +static void exprAnalyze(
1.103625 +  SrcList *pSrc,            /* the FROM clause */
1.103626 +  WhereClause *pWC,         /* the WHERE clause */
1.103627 +  int idxTerm               /* Index of the term to be analyzed */
1.103628 +){
1.103629 +  WhereTerm *pTerm;                /* The term to be analyzed */
1.103630 +  WhereMaskSet *pMaskSet;          /* Set of table index masks */
1.103631 +  Expr *pExpr;                     /* The expression to be analyzed */
1.103632 +  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
1.103633 +  Bitmask prereqAll;               /* Prerequesites of pExpr */
1.103634 +  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
1.103635 +  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
1.103636 +  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
1.103637 +  int noCase = 0;                  /* LIKE/GLOB distinguishes case */
1.103638 +  int op;                          /* Top-level operator.  pExpr->op */
1.103639 +  Parse *pParse = pWC->pParse;     /* Parsing context */
1.103640 +  sqlite3 *db = pParse->db;        /* Database connection */
1.103641 +
1.103642 +  if( db->mallocFailed ){
1.103643 +    return;
1.103644 +  }
1.103645 +  pTerm = &pWC->a[idxTerm];
1.103646 +  pMaskSet = pWC->pMaskSet;
1.103647 +  pExpr = sqlite3ExprSkipCollate(pTerm->pExpr);
1.103648 +  prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
1.103649 +  op = pExpr->op;
1.103650 +  if( op==TK_IN ){
1.103651 +    assert( pExpr->pRight==0 );
1.103652 +    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
1.103653 +      pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
1.103654 +    }else{
1.103655 +      pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
1.103656 +    }
1.103657 +  }else if( op==TK_ISNULL ){
1.103658 +    pTerm->prereqRight = 0;
1.103659 +  }else{
1.103660 +    pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
1.103661 +  }
1.103662 +  prereqAll = exprTableUsage(pMaskSet, pExpr);
1.103663 +  if( ExprHasProperty(pExpr, EP_FromJoin) ){
1.103664 +    Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
1.103665 +    prereqAll |= x;
1.103666 +    extraRight = x-1;  /* ON clause terms may not be used with an index
1.103667 +                       ** on left table of a LEFT JOIN.  Ticket #3015 */
1.103668 +  }
1.103669 +  pTerm->prereqAll = prereqAll;
1.103670 +  pTerm->leftCursor = -1;
1.103671 +  pTerm->iParent = -1;
1.103672 +  pTerm->eOperator = 0;
1.103673 +  if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
1.103674 +    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
1.103675 +    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
1.103676 +    if( pLeft->op==TK_COLUMN ){
1.103677 +      pTerm->leftCursor = pLeft->iTable;
1.103678 +      pTerm->u.leftColumn = pLeft->iColumn;
1.103679 +      pTerm->eOperator = operatorMask(op);
1.103680 +    }
1.103681 +    if( pRight && pRight->op==TK_COLUMN ){
1.103682 +      WhereTerm *pNew;
1.103683 +      Expr *pDup;
1.103684 +      if( pTerm->leftCursor>=0 ){
1.103685 +        int idxNew;
1.103686 +        pDup = sqlite3ExprDup(db, pExpr, 0);
1.103687 +        if( db->mallocFailed ){
1.103688 +          sqlite3ExprDelete(db, pDup);
1.103689 +          return;
1.103690 +        }
1.103691 +        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
1.103692 +        if( idxNew==0 ) return;
1.103693 +        pNew = &pWC->a[idxNew];
1.103694 +        pNew->iParent = idxTerm;
1.103695 +        pTerm = &pWC->a[idxTerm];
1.103696 +        pTerm->nChild = 1;
1.103697 +        pTerm->wtFlags |= TERM_COPIED;
1.103698 +      }else{
1.103699 +        pDup = pExpr;
1.103700 +        pNew = pTerm;
1.103701 +      }
1.103702 +      exprCommute(pParse, pDup);
1.103703 +      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
1.103704 +      pNew->leftCursor = pLeft->iTable;
1.103705 +      pNew->u.leftColumn = pLeft->iColumn;
1.103706 +      testcase( (prereqLeft | extraRight) != prereqLeft );
1.103707 +      pNew->prereqRight = prereqLeft | extraRight;
1.103708 +      pNew->prereqAll = prereqAll;
1.103709 +      pNew->eOperator = operatorMask(pDup->op);
1.103710 +    }
1.103711 +  }
1.103712 +
1.103713 +#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
1.103714 +  /* If a term is the BETWEEN operator, create two new virtual terms
1.103715 +  ** that define the range that the BETWEEN implements.  For example:
1.103716 +  **
1.103717 +  **      a BETWEEN b AND c
1.103718 +  **
1.103719 +  ** is converted into:
1.103720 +  **
1.103721 +  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
1.103722 +  **
1.103723 +  ** The two new terms are added onto the end of the WhereClause object.
1.103724 +  ** The new terms are "dynamic" and are children of the original BETWEEN
1.103725 +  ** term.  That means that if the BETWEEN term is coded, the children are
1.103726 +  ** skipped.  Or, if the children are satisfied by an index, the original
1.103727 +  ** BETWEEN term is skipped.
1.103728 +  */
1.103729 +  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
1.103730 +    ExprList *pList = pExpr->x.pList;
1.103731 +    int i;
1.103732 +    static const u8 ops[] = {TK_GE, TK_LE};
1.103733 +    assert( pList!=0 );
1.103734 +    assert( pList->nExpr==2 );
1.103735 +    for(i=0; i<2; i++){
1.103736 +      Expr *pNewExpr;
1.103737 +      int idxNew;
1.103738 +      pNewExpr = sqlite3PExpr(pParse, ops[i], 
1.103739 +                             sqlite3ExprDup(db, pExpr->pLeft, 0),
1.103740 +                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
1.103741 +      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1.103742 +      testcase( idxNew==0 );
1.103743 +      exprAnalyze(pSrc, pWC, idxNew);
1.103744 +      pTerm = &pWC->a[idxTerm];
1.103745 +      pWC->a[idxNew].iParent = idxTerm;
1.103746 +    }
1.103747 +    pTerm->nChild = 2;
1.103748 +  }
1.103749 +#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
1.103750 +
1.103751 +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
1.103752 +  /* Analyze a term that is composed of two or more subterms connected by
1.103753 +  ** an OR operator.
1.103754 +  */
1.103755 +  else if( pExpr->op==TK_OR ){
1.103756 +    assert( pWC->op==TK_AND );
1.103757 +    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
1.103758 +    pTerm = &pWC->a[idxTerm];
1.103759 +  }
1.103760 +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
1.103761 +
1.103762 +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
1.103763 +  /* Add constraints to reduce the search space on a LIKE or GLOB
1.103764 +  ** operator.
1.103765 +  **
1.103766 +  ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
1.103767 +  **
1.103768 +  **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
1.103769 +  **
1.103770 +  ** The last character of the prefix "abc" is incremented to form the
1.103771 +  ** termination condition "abd".
1.103772 +  */
1.103773 +  if( pWC->op==TK_AND 
1.103774 +   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
1.103775 +  ){
1.103776 +    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
1.103777 +    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
1.103778 +    Expr *pNewExpr1;
1.103779 +    Expr *pNewExpr2;
1.103780 +    int idxNew1;
1.103781 +    int idxNew2;
1.103782 +    Token sCollSeqName;  /* Name of collating sequence */
1.103783 +
1.103784 +    pLeft = pExpr->x.pList->a[1].pExpr;
1.103785 +    pStr2 = sqlite3ExprDup(db, pStr1, 0);
1.103786 +    if( !db->mallocFailed ){
1.103787 +      u8 c, *pC;       /* Last character before the first wildcard */
1.103788 +      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
1.103789 +      c = *pC;
1.103790 +      if( noCase ){
1.103791 +        /* The point is to increment the last character before the first
1.103792 +        ** wildcard.  But if we increment '@', that will push it into the
1.103793 +        ** alphabetic range where case conversions will mess up the 
1.103794 +        ** inequality.  To avoid this, make sure to also run the full
1.103795 +        ** LIKE on all candidate expressions by clearing the isComplete flag
1.103796 +        */
1.103797 +        if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
1.103798 +
1.103799 +
1.103800 +        c = sqlite3UpperToLower[c];
1.103801 +      }
1.103802 +      *pC = c + 1;
1.103803 +    }
1.103804 +    sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
1.103805 +    sCollSeqName.n = 6;
1.103806 +    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
1.103807 +    pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
1.103808 +           sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
1.103809 +           pStr1, 0);
1.103810 +    idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
1.103811 +    testcase( idxNew1==0 );
1.103812 +    exprAnalyze(pSrc, pWC, idxNew1);
1.103813 +    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
1.103814 +    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
1.103815 +           sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
1.103816 +           pStr2, 0);
1.103817 +    idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
1.103818 +    testcase( idxNew2==0 );
1.103819 +    exprAnalyze(pSrc, pWC, idxNew2);
1.103820 +    pTerm = &pWC->a[idxTerm];
1.103821 +    if( isComplete ){
1.103822 +      pWC->a[idxNew1].iParent = idxTerm;
1.103823 +      pWC->a[idxNew2].iParent = idxTerm;
1.103824 +      pTerm->nChild = 2;
1.103825 +    }
1.103826 +  }
1.103827 +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
1.103828 +
1.103829 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.103830 +  /* Add a WO_MATCH auxiliary term to the constraint set if the
1.103831 +  ** current expression is of the form:  column MATCH expr.
1.103832 +  ** This information is used by the xBestIndex methods of
1.103833 +  ** virtual tables.  The native query optimizer does not attempt
1.103834 +  ** to do anything with MATCH functions.
1.103835 +  */
1.103836 +  if( isMatchOfColumn(pExpr) ){
1.103837 +    int idxNew;
1.103838 +    Expr *pRight, *pLeft;
1.103839 +    WhereTerm *pNewTerm;
1.103840 +    Bitmask prereqColumn, prereqExpr;
1.103841 +
1.103842 +    pRight = pExpr->x.pList->a[0].pExpr;
1.103843 +    pLeft = pExpr->x.pList->a[1].pExpr;
1.103844 +    prereqExpr = exprTableUsage(pMaskSet, pRight);
1.103845 +    prereqColumn = exprTableUsage(pMaskSet, pLeft);
1.103846 +    if( (prereqExpr & prereqColumn)==0 ){
1.103847 +      Expr *pNewExpr;
1.103848 +      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
1.103849 +                              0, sqlite3ExprDup(db, pRight, 0), 0);
1.103850 +      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1.103851 +      testcase( idxNew==0 );
1.103852 +      pNewTerm = &pWC->a[idxNew];
1.103853 +      pNewTerm->prereqRight = prereqExpr;
1.103854 +      pNewTerm->leftCursor = pLeft->iTable;
1.103855 +      pNewTerm->u.leftColumn = pLeft->iColumn;
1.103856 +      pNewTerm->eOperator = WO_MATCH;
1.103857 +      pNewTerm->iParent = idxTerm;
1.103858 +      pTerm = &pWC->a[idxTerm];
1.103859 +      pTerm->nChild = 1;
1.103860 +      pTerm->wtFlags |= TERM_COPIED;
1.103861 +      pNewTerm->prereqAll = pTerm->prereqAll;
1.103862 +    }
1.103863 +  }
1.103864 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.103865 +
1.103866 +#ifdef SQLITE_ENABLE_STAT3
1.103867 +  /* When sqlite_stat3 histogram data is available an operator of the
1.103868 +  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
1.103869 +  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
1.103870 +  ** virtual term of that form.
1.103871 +  **
1.103872 +  ** Note that the virtual term must be tagged with TERM_VNULL.  This
1.103873 +  ** TERM_VNULL tag will suppress the not-null check at the beginning
1.103874 +  ** of the loop.  Without the TERM_VNULL flag, the not-null check at
1.103875 +  ** the start of the loop will prevent any results from being returned.
1.103876 +  */
1.103877 +  if( pExpr->op==TK_NOTNULL
1.103878 +   && pExpr->pLeft->op==TK_COLUMN
1.103879 +   && pExpr->pLeft->iColumn>=0
1.103880 +  ){
1.103881 +    Expr *pNewExpr;
1.103882 +    Expr *pLeft = pExpr->pLeft;
1.103883 +    int idxNew;
1.103884 +    WhereTerm *pNewTerm;
1.103885 +
1.103886 +    pNewExpr = sqlite3PExpr(pParse, TK_GT,
1.103887 +                            sqlite3ExprDup(db, pLeft, 0),
1.103888 +                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
1.103889 +
1.103890 +    idxNew = whereClauseInsert(pWC, pNewExpr,
1.103891 +                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
1.103892 +    if( idxNew ){
1.103893 +      pNewTerm = &pWC->a[idxNew];
1.103894 +      pNewTerm->prereqRight = 0;
1.103895 +      pNewTerm->leftCursor = pLeft->iTable;
1.103896 +      pNewTerm->u.leftColumn = pLeft->iColumn;
1.103897 +      pNewTerm->eOperator = WO_GT;
1.103898 +      pNewTerm->iParent = idxTerm;
1.103899 +      pTerm = &pWC->a[idxTerm];
1.103900 +      pTerm->nChild = 1;
1.103901 +      pTerm->wtFlags |= TERM_COPIED;
1.103902 +      pNewTerm->prereqAll = pTerm->prereqAll;
1.103903 +    }
1.103904 +  }
1.103905 +#endif /* SQLITE_ENABLE_STAT */
1.103906 +
1.103907 +  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
1.103908 +  ** an index for tables to the left of the join.
1.103909 +  */
1.103910 +  pTerm->prereqRight |= extraRight;
1.103911 +}
1.103912 +
1.103913 +/*
1.103914 +** This function searches the expression list passed as the second argument
1.103915 +** for an expression of type TK_COLUMN that refers to the same column and
1.103916 +** uses the same collation sequence as the iCol'th column of index pIdx.
1.103917 +** Argument iBase is the cursor number used for the table that pIdx refers
1.103918 +** to.
1.103919 +**
1.103920 +** If such an expression is found, its index in pList->a[] is returned. If
1.103921 +** no expression is found, -1 is returned.
1.103922 +*/
1.103923 +static int findIndexCol(
1.103924 +  Parse *pParse,                  /* Parse context */
1.103925 +  ExprList *pList,                /* Expression list to search */
1.103926 +  int iBase,                      /* Cursor for table associated with pIdx */
1.103927 +  Index *pIdx,                    /* Index to match column of */
1.103928 +  int iCol                        /* Column of index to match */
1.103929 +){
1.103930 +  int i;
1.103931 +  const char *zColl = pIdx->azColl[iCol];
1.103932 +
1.103933 +  for(i=0; i<pList->nExpr; i++){
1.103934 +    Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
1.103935 +    if( p->op==TK_COLUMN
1.103936 +     && p->iColumn==pIdx->aiColumn[iCol]
1.103937 +     && p->iTable==iBase
1.103938 +    ){
1.103939 +      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1.103940 +      if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
1.103941 +        return i;
1.103942 +      }
1.103943 +    }
1.103944 +  }
1.103945 +
1.103946 +  return -1;
1.103947 +}
1.103948 +
1.103949 +/*
1.103950 +** This routine determines if pIdx can be used to assist in processing a
1.103951 +** DISTINCT qualifier. In other words, it tests whether or not using this
1.103952 +** index for the outer loop guarantees that rows with equal values for
1.103953 +** all expressions in the pDistinct list are delivered grouped together.
1.103954 +**
1.103955 +** For example, the query 
1.103956 +**
1.103957 +**   SELECT DISTINCT a, b, c FROM tbl WHERE a = ?
1.103958 +**
1.103959 +** can benefit from any index on columns "b" and "c".
1.103960 +*/
1.103961 +static int isDistinctIndex(
1.103962 +  Parse *pParse,                  /* Parsing context */
1.103963 +  WhereClause *pWC,               /* The WHERE clause */
1.103964 +  Index *pIdx,                    /* The index being considered */
1.103965 +  int base,                       /* Cursor number for the table pIdx is on */
1.103966 +  ExprList *pDistinct,            /* The DISTINCT expressions */
1.103967 +  int nEqCol                      /* Number of index columns with == */
1.103968 +){
1.103969 +  Bitmask mask = 0;               /* Mask of unaccounted for pDistinct exprs */
1.103970 +  int i;                          /* Iterator variable */
1.103971 +
1.103972 +  assert( pDistinct!=0 );
1.103973 +  if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
1.103974 +  testcase( pDistinct->nExpr==BMS-1 );
1.103975 +
1.103976 +  /* Loop through all the expressions in the distinct list. If any of them
1.103977 +  ** are not simple column references, return early. Otherwise, test if the
1.103978 +  ** WHERE clause contains a "col=X" clause. If it does, the expression
1.103979 +  ** can be ignored. If it does not, and the column does not belong to the
1.103980 +  ** same table as index pIdx, return early. Finally, if there is no
1.103981 +  ** matching "col=X" expression and the column is on the same table as pIdx,
1.103982 +  ** set the corresponding bit in variable mask.
1.103983 +  */
1.103984 +  for(i=0; i<pDistinct->nExpr; i++){
1.103985 +    WhereTerm *pTerm;
1.103986 +    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
1.103987 +    if( p->op!=TK_COLUMN ) return 0;
1.103988 +    pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
1.103989 +    if( pTerm ){
1.103990 +      Expr *pX = pTerm->pExpr;
1.103991 +      CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
1.103992 +      CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
1.103993 +      if( p1==p2 ) continue;
1.103994 +    }
1.103995 +    if( p->iTable!=base ) return 0;
1.103996 +    mask |= (((Bitmask)1) << i);
1.103997 +  }
1.103998 +
1.103999 +  for(i=nEqCol; mask && i<pIdx->nColumn; i++){
1.104000 +    int iExpr = findIndexCol(pParse, pDistinct, base, pIdx, i);
1.104001 +    if( iExpr<0 ) break;
1.104002 +    mask &= ~(((Bitmask)1) << iExpr);
1.104003 +  }
1.104004 +
1.104005 +  return (mask==0);
1.104006 +}
1.104007 +
1.104008 +
1.104009 +/*
1.104010 +** Return true if the DISTINCT expression-list passed as the third argument
1.104011 +** is redundant. A DISTINCT list is redundant if the database contains a
1.104012 +** UNIQUE index that guarantees that the result of the query will be distinct
1.104013 +** anyway.
1.104014 +*/
1.104015 +static int isDistinctRedundant(
1.104016 +  Parse *pParse,
1.104017 +  SrcList *pTabList,
1.104018 +  WhereClause *pWC,
1.104019 +  ExprList *pDistinct
1.104020 +){
1.104021 +  Table *pTab;
1.104022 +  Index *pIdx;
1.104023 +  int i;                          
1.104024 +  int iBase;
1.104025 +
1.104026 +  /* If there is more than one table or sub-select in the FROM clause of
1.104027 +  ** this query, then it will not be possible to show that the DISTINCT 
1.104028 +  ** clause is redundant. */
1.104029 +  if( pTabList->nSrc!=1 ) return 0;
1.104030 +  iBase = pTabList->a[0].iCursor;
1.104031 +  pTab = pTabList->a[0].pTab;
1.104032 +
1.104033 +  /* If any of the expressions is an IPK column on table iBase, then return 
1.104034 +  ** true. Note: The (p->iTable==iBase) part of this test may be false if the
1.104035 +  ** current SELECT is a correlated sub-query.
1.104036 +  */
1.104037 +  for(i=0; i<pDistinct->nExpr; i++){
1.104038 +    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
1.104039 +    if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
1.104040 +  }
1.104041 +
1.104042 +  /* Loop through all indices on the table, checking each to see if it makes
1.104043 +  ** the DISTINCT qualifier redundant. It does so if:
1.104044 +  **
1.104045 +  **   1. The index is itself UNIQUE, and
1.104046 +  **
1.104047 +  **   2. All of the columns in the index are either part of the pDistinct
1.104048 +  **      list, or else the WHERE clause contains a term of the form "col=X",
1.104049 +  **      where X is a constant value. The collation sequences of the
1.104050 +  **      comparison and select-list expressions must match those of the index.
1.104051 +  **
1.104052 +  **   3. All of those index columns for which the WHERE clause does not
1.104053 +  **      contain a "col=X" term are subject to a NOT NULL constraint.
1.104054 +  */
1.104055 +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1.104056 +    if( pIdx->onError==OE_None ) continue;
1.104057 +    for(i=0; i<pIdx->nColumn; i++){
1.104058 +      int iCol = pIdx->aiColumn[i];
1.104059 +      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
1.104060 +        int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
1.104061 +        if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){
1.104062 +          break;
1.104063 +        }
1.104064 +      }
1.104065 +    }
1.104066 +    if( i==pIdx->nColumn ){
1.104067 +      /* This index implies that the DISTINCT qualifier is redundant. */
1.104068 +      return 1;
1.104069 +    }
1.104070 +  }
1.104071 +
1.104072 +  return 0;
1.104073 +}
1.104074 +
1.104075 +/*
1.104076 +** Prepare a crude estimate of the logarithm of the input value.
1.104077 +** The results need not be exact.  This is only used for estimating
1.104078 +** the total cost of performing operations with O(logN) or O(NlogN)
1.104079 +** complexity.  Because N is just a guess, it is no great tragedy if
1.104080 +** logN is a little off.
1.104081 +*/
1.104082 +static double estLog(double N){
1.104083 +  double logN = 1;
1.104084 +  double x = 10;
1.104085 +  while( N>x ){
1.104086 +    logN += 1;
1.104087 +    x *= 10;
1.104088 +  }
1.104089 +  return logN;
1.104090 +}
1.104091 +
1.104092 +/*
1.104093 +** Two routines for printing the content of an sqlite3_index_info
1.104094 +** structure.  Used for testing and debugging only.  If neither
1.104095 +** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
1.104096 +** are no-ops.
1.104097 +*/
1.104098 +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
1.104099 +static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
1.104100 +  int i;
1.104101 +  if( !sqlite3WhereTrace ) return;
1.104102 +  for(i=0; i<p->nConstraint; i++){
1.104103 +    sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
1.104104 +       i,
1.104105 +       p->aConstraint[i].iColumn,
1.104106 +       p->aConstraint[i].iTermOffset,
1.104107 +       p->aConstraint[i].op,
1.104108 +       p->aConstraint[i].usable);
1.104109 +  }
1.104110 +  for(i=0; i<p->nOrderBy; i++){
1.104111 +    sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
1.104112 +       i,
1.104113 +       p->aOrderBy[i].iColumn,
1.104114 +       p->aOrderBy[i].desc);
1.104115 +  }
1.104116 +}
1.104117 +static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
1.104118 +  int i;
1.104119 +  if( !sqlite3WhereTrace ) return;
1.104120 +  for(i=0; i<p->nConstraint; i++){
1.104121 +    sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
1.104122 +       i,
1.104123 +       p->aConstraintUsage[i].argvIndex,
1.104124 +       p->aConstraintUsage[i].omit);
1.104125 +  }
1.104126 +  sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
1.104127 +  sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
1.104128 +  sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
1.104129 +  sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
1.104130 +}
1.104131 +#else
1.104132 +#define TRACE_IDX_INPUTS(A)
1.104133 +#define TRACE_IDX_OUTPUTS(A)
1.104134 +#endif
1.104135 +
1.104136 +/* 
1.104137 +** Required because bestIndex() is called by bestOrClauseIndex() 
1.104138 +*/
1.104139 +static void bestIndex(WhereBestIdx*);
1.104140 +
1.104141 +/*
1.104142 +** This routine attempts to find an scanning strategy that can be used 
1.104143 +** to optimize an 'OR' expression that is part of a WHERE clause. 
1.104144 +**
1.104145 +** The table associated with FROM clause term pSrc may be either a
1.104146 +** regular B-Tree table or a virtual table.
1.104147 +*/
1.104148 +static void bestOrClauseIndex(WhereBestIdx *p){
1.104149 +#ifndef SQLITE_OMIT_OR_OPTIMIZATION
1.104150 +  WhereClause *pWC = p->pWC;           /* The WHERE clause */
1.104151 +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
1.104152 +  const int iCur = pSrc->iCursor;      /* The cursor of the table  */
1.104153 +  const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
1.104154 +  WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
1.104155 +  WhereTerm *pTerm;                    /* A single term of the WHERE clause */
1.104156 +
1.104157 +  /* The OR-clause optimization is disallowed if the INDEXED BY or
1.104158 +  ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
1.104159 +  if( pSrc->notIndexed || pSrc->pIndex!=0 ){
1.104160 +    return;
1.104161 +  }
1.104162 +  if( pWC->wctrlFlags & WHERE_AND_ONLY ){
1.104163 +    return;
1.104164 +  }
1.104165 +
1.104166 +  /* Search the WHERE clause terms for a usable WO_OR term. */
1.104167 +  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
1.104168 +    if( pTerm->eOperator==WO_OR 
1.104169 +     && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
1.104170 +     && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
1.104171 +    ){
1.104172 +      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
1.104173 +      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
1.104174 +      WhereTerm *pOrTerm;
1.104175 +      int flags = WHERE_MULTI_OR;
1.104176 +      double rTotal = 0;
1.104177 +      double nRow = 0;
1.104178 +      Bitmask used = 0;
1.104179 +      WhereBestIdx sBOI;
1.104180 +
1.104181 +      sBOI = *p;
1.104182 +      sBOI.pOrderBy = 0;
1.104183 +      sBOI.pDistinct = 0;
1.104184 +      sBOI.ppIdxInfo = 0;
1.104185 +      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
1.104186 +        WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
1.104187 +          (pOrTerm - pOrWC->a), (pTerm - pWC->a)
1.104188 +        ));
1.104189 +        if( pOrTerm->eOperator==WO_AND ){
1.104190 +          sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
1.104191 +          bestIndex(&sBOI);
1.104192 +        }else if( pOrTerm->leftCursor==iCur ){
1.104193 +          WhereClause tempWC;
1.104194 +          tempWC.pParse = pWC->pParse;
1.104195 +          tempWC.pMaskSet = pWC->pMaskSet;
1.104196 +          tempWC.pOuter = pWC;
1.104197 +          tempWC.op = TK_AND;
1.104198 +          tempWC.a = pOrTerm;
1.104199 +          tempWC.wctrlFlags = 0;
1.104200 +          tempWC.nTerm = 1;
1.104201 +          sBOI.pWC = &tempWC;
1.104202 +          bestIndex(&sBOI);
1.104203 +        }else{
1.104204 +          continue;
1.104205 +        }
1.104206 +        rTotal += sBOI.cost.rCost;
1.104207 +        nRow += sBOI.cost.plan.nRow;
1.104208 +        used |= sBOI.cost.used;
1.104209 +        if( rTotal>=p->cost.rCost ) break;
1.104210 +      }
1.104211 +
1.104212 +      /* If there is an ORDER BY clause, increase the scan cost to account 
1.104213 +      ** for the cost of the sort. */
1.104214 +      if( p->pOrderBy!=0 ){
1.104215 +        WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
1.104216 +                    rTotal, rTotal+nRow*estLog(nRow)));
1.104217 +        rTotal += nRow*estLog(nRow);
1.104218 +      }
1.104219 +
1.104220 +      /* If the cost of scanning using this OR term for optimization is
1.104221 +      ** less than the current cost stored in pCost, replace the contents
1.104222 +      ** of pCost. */
1.104223 +      WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
1.104224 +      if( rTotal<p->cost.rCost ){
1.104225 +        p->cost.rCost = rTotal;
1.104226 +        p->cost.used = used;
1.104227 +        p->cost.plan.nRow = nRow;
1.104228 +        p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
1.104229 +        p->cost.plan.wsFlags = flags;
1.104230 +        p->cost.plan.u.pTerm = pTerm;
1.104231 +      }
1.104232 +    }
1.104233 +  }
1.104234 +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
1.104235 +}
1.104236 +
1.104237 +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
1.104238 +/*
1.104239 +** Return TRUE if the WHERE clause term pTerm is of a form where it
1.104240 +** could be used with an index to access pSrc, assuming an appropriate
1.104241 +** index existed.
1.104242 +*/
1.104243 +static int termCanDriveIndex(
1.104244 +  WhereTerm *pTerm,              /* WHERE clause term to check */
1.104245 +  struct SrcList_item *pSrc,     /* Table we are trying to access */
1.104246 +  Bitmask notReady               /* Tables in outer loops of the join */
1.104247 +){
1.104248 +  char aff;
1.104249 +  if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
1.104250 +  if( pTerm->eOperator!=WO_EQ ) return 0;
1.104251 +  if( (pTerm->prereqRight & notReady)!=0 ) return 0;
1.104252 +  aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
1.104253 +  if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
1.104254 +  return 1;
1.104255 +}
1.104256 +#endif
1.104257 +
1.104258 +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
1.104259 +/*
1.104260 +** If the query plan for pSrc specified in pCost is a full table scan
1.104261 +** and indexing is allows (if there is no NOT INDEXED clause) and it
1.104262 +** possible to construct a transient index that would perform better
1.104263 +** than a full table scan even when the cost of constructing the index
1.104264 +** is taken into account, then alter the query plan to use the
1.104265 +** transient index.
1.104266 +*/
1.104267 +static void bestAutomaticIndex(WhereBestIdx *p){
1.104268 +  Parse *pParse = p->pParse;            /* The parsing context */
1.104269 +  WhereClause *pWC = p->pWC;            /* The WHERE clause */
1.104270 +  struct SrcList_item *pSrc = p->pSrc;  /* The FROM clause term to search */
1.104271 +  double nTableRow;                     /* Rows in the input table */
1.104272 +  double logN;                          /* log(nTableRow) */
1.104273 +  double costTempIdx;         /* per-query cost of the transient index */
1.104274 +  WhereTerm *pTerm;           /* A single term of the WHERE clause */
1.104275 +  WhereTerm *pWCEnd;          /* End of pWC->a[] */
1.104276 +  Table *pTable;              /* Table tht might be indexed */
1.104277 +
1.104278 +  if( pParse->nQueryLoop<=(double)1 ){
1.104279 +    /* There is no point in building an automatic index for a single scan */
1.104280 +    return;
1.104281 +  }
1.104282 +  if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
1.104283 +    /* Automatic indices are disabled at run-time */
1.104284 +    return;
1.104285 +  }
1.104286 +  if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0
1.104287 +   && (p->cost.plan.wsFlags & WHERE_COVER_SCAN)==0
1.104288 +  ){
1.104289 +    /* We already have some kind of index in use for this query. */
1.104290 +    return;
1.104291 +  }
1.104292 +  if( pSrc->viaCoroutine ){
1.104293 +    /* Cannot index a co-routine */
1.104294 +    return;
1.104295 +  }
1.104296 +  if( pSrc->notIndexed ){
1.104297 +    /* The NOT INDEXED clause appears in the SQL. */
1.104298 +    return;
1.104299 +  }
1.104300 +  if( pSrc->isCorrelated ){
1.104301 +    /* The source is a correlated sub-query. No point in indexing it. */
1.104302 +    return;
1.104303 +  }
1.104304 +
1.104305 +  assert( pParse->nQueryLoop >= (double)1 );
1.104306 +  pTable = pSrc->pTab;
1.104307 +  nTableRow = pTable->nRowEst;
1.104308 +  logN = estLog(nTableRow);
1.104309 +  costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
1.104310 +  if( costTempIdx>=p->cost.rCost ){
1.104311 +    /* The cost of creating the transient table would be greater than
1.104312 +    ** doing the full table scan */
1.104313 +    return;
1.104314 +  }
1.104315 +
1.104316 +  /* Search for any equality comparison term */
1.104317 +  pWCEnd = &pWC->a[pWC->nTerm];
1.104318 +  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
1.104319 +    if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
1.104320 +      WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
1.104321 +                    p->cost.rCost, costTempIdx));
1.104322 +      p->cost.rCost = costTempIdx;
1.104323 +      p->cost.plan.nRow = logN + 1;
1.104324 +      p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
1.104325 +      p->cost.used = pTerm->prereqRight;
1.104326 +      break;
1.104327 +    }
1.104328 +  }
1.104329 +}
1.104330 +#else
1.104331 +# define bestAutomaticIndex(A)  /* no-op */
1.104332 +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
1.104333 +
1.104334 +
1.104335 +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
1.104336 +/*
1.104337 +** Generate code to construct the Index object for an automatic index
1.104338 +** and to set up the WhereLevel object pLevel so that the code generator
1.104339 +** makes use of the automatic index.
1.104340 +*/
1.104341 +static void constructAutomaticIndex(
1.104342 +  Parse *pParse,              /* The parsing context */
1.104343 +  WhereClause *pWC,           /* The WHERE clause */
1.104344 +  struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
1.104345 +  Bitmask notReady,           /* Mask of cursors that are not available */
1.104346 +  WhereLevel *pLevel          /* Write new index here */
1.104347 +){
1.104348 +  int nColumn;                /* Number of columns in the constructed index */
1.104349 +  WhereTerm *pTerm;           /* A single term of the WHERE clause */
1.104350 +  WhereTerm *pWCEnd;          /* End of pWC->a[] */
1.104351 +  int nByte;                  /* Byte of memory needed for pIdx */
1.104352 +  Index *pIdx;                /* Object describing the transient index */
1.104353 +  Vdbe *v;                    /* Prepared statement under construction */
1.104354 +  int addrInit;               /* Address of the initialization bypass jump */
1.104355 +  Table *pTable;              /* The table being indexed */
1.104356 +  KeyInfo *pKeyinfo;          /* Key information for the index */   
1.104357 +  int addrTop;                /* Top of the index fill loop */
1.104358 +  int regRecord;              /* Register holding an index record */
1.104359 +  int n;                      /* Column counter */
1.104360 +  int i;                      /* Loop counter */
1.104361 +  int mxBitCol;               /* Maximum column in pSrc->colUsed */
1.104362 +  CollSeq *pColl;             /* Collating sequence to on a column */
1.104363 +  Bitmask idxCols;            /* Bitmap of columns used for indexing */
1.104364 +  Bitmask extraCols;          /* Bitmap of additional columns */
1.104365 +
1.104366 +  /* Generate code to skip over the creation and initialization of the
1.104367 +  ** transient index on 2nd and subsequent iterations of the loop. */
1.104368 +  v = pParse->pVdbe;
1.104369 +  assert( v!=0 );
1.104370 +  addrInit = sqlite3CodeOnce(pParse);
1.104371 +
1.104372 +  /* Count the number of columns that will be added to the index
1.104373 +  ** and used to match WHERE clause constraints */
1.104374 +  nColumn = 0;
1.104375 +  pTable = pSrc->pTab;
1.104376 +  pWCEnd = &pWC->a[pWC->nTerm];
1.104377 +  idxCols = 0;
1.104378 +  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
1.104379 +    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
1.104380 +      int iCol = pTerm->u.leftColumn;
1.104381 +      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
1.104382 +      testcase( iCol==BMS );
1.104383 +      testcase( iCol==BMS-1 );
1.104384 +      if( (idxCols & cMask)==0 ){
1.104385 +        nColumn++;
1.104386 +        idxCols |= cMask;
1.104387 +      }
1.104388 +    }
1.104389 +  }
1.104390 +  assert( nColumn>0 );
1.104391 +  pLevel->plan.nEq = nColumn;
1.104392 +
1.104393 +  /* Count the number of additional columns needed to create a
1.104394 +  ** covering index.  A "covering index" is an index that contains all
1.104395 +  ** columns that are needed by the query.  With a covering index, the
1.104396 +  ** original table never needs to be accessed.  Automatic indices must
1.104397 +  ** be a covering index because the index will not be updated if the
1.104398 +  ** original table changes and the index and table cannot both be used
1.104399 +  ** if they go out of sync.
1.104400 +  */
1.104401 +  extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
1.104402 +  mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
1.104403 +  testcase( pTable->nCol==BMS-1 );
1.104404 +  testcase( pTable->nCol==BMS-2 );
1.104405 +  for(i=0; i<mxBitCol; i++){
1.104406 +    if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
1.104407 +  }
1.104408 +  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
1.104409 +    nColumn += pTable->nCol - BMS + 1;
1.104410 +  }
1.104411 +  pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
1.104412 +
1.104413 +  /* Construct the Index object to describe this index */
1.104414 +  nByte = sizeof(Index);
1.104415 +  nByte += nColumn*sizeof(int);     /* Index.aiColumn */
1.104416 +  nByte += nColumn*sizeof(char*);   /* Index.azColl */
1.104417 +  nByte += nColumn;                 /* Index.aSortOrder */
1.104418 +  pIdx = sqlite3DbMallocZero(pParse->db, nByte);
1.104419 +  if( pIdx==0 ) return;
1.104420 +  pLevel->plan.u.pIdx = pIdx;
1.104421 +  pIdx->azColl = (char**)&pIdx[1];
1.104422 +  pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
1.104423 +  pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
1.104424 +  pIdx->zName = "auto-index";
1.104425 +  pIdx->nColumn = nColumn;
1.104426 +  pIdx->pTable = pTable;
1.104427 +  n = 0;
1.104428 +  idxCols = 0;
1.104429 +  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
1.104430 +    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
1.104431 +      int iCol = pTerm->u.leftColumn;
1.104432 +      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
1.104433 +      if( (idxCols & cMask)==0 ){
1.104434 +        Expr *pX = pTerm->pExpr;
1.104435 +        idxCols |= cMask;
1.104436 +        pIdx->aiColumn[n] = pTerm->u.leftColumn;
1.104437 +        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
1.104438 +        pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
1.104439 +        n++;
1.104440 +      }
1.104441 +    }
1.104442 +  }
1.104443 +  assert( (u32)n==pLevel->plan.nEq );
1.104444 +
1.104445 +  /* Add additional columns needed to make the automatic index into
1.104446 +  ** a covering index */
1.104447 +  for(i=0; i<mxBitCol; i++){
1.104448 +    if( extraCols & (((Bitmask)1)<<i) ){
1.104449 +      pIdx->aiColumn[n] = i;
1.104450 +      pIdx->azColl[n] = "BINARY";
1.104451 +      n++;
1.104452 +    }
1.104453 +  }
1.104454 +  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
1.104455 +    for(i=BMS-1; i<pTable->nCol; i++){
1.104456 +      pIdx->aiColumn[n] = i;
1.104457 +      pIdx->azColl[n] = "BINARY";
1.104458 +      n++;
1.104459 +    }
1.104460 +  }
1.104461 +  assert( n==nColumn );
1.104462 +
1.104463 +  /* Create the automatic index */
1.104464 +  pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
1.104465 +  assert( pLevel->iIdxCur>=0 );
1.104466 +  sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
1.104467 +                    (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
1.104468 +  VdbeComment((v, "for %s", pTable->zName));
1.104469 +
1.104470 +  /* Fill the automatic index with content */
1.104471 +  addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
1.104472 +  regRecord = sqlite3GetTempReg(pParse);
1.104473 +  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
1.104474 +  sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
1.104475 +  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
1.104476 +  sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
1.104477 +  sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
1.104478 +  sqlite3VdbeJumpHere(v, addrTop);
1.104479 +  sqlite3ReleaseTempReg(pParse, regRecord);
1.104480 +  
1.104481 +  /* Jump here when skipping the initialization */
1.104482 +  sqlite3VdbeJumpHere(v, addrInit);
1.104483 +}
1.104484 +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
1.104485 +
1.104486 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.104487 +/*
1.104488 +** Allocate and populate an sqlite3_index_info structure. It is the 
1.104489 +** responsibility of the caller to eventually release the structure
1.104490 +** by passing the pointer returned by this function to sqlite3_free().
1.104491 +*/
1.104492 +static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
1.104493 +  Parse *pParse = p->pParse; 
1.104494 +  WhereClause *pWC = p->pWC;
1.104495 +  struct SrcList_item *pSrc = p->pSrc;
1.104496 +  ExprList *pOrderBy = p->pOrderBy;
1.104497 +  int i, j;
1.104498 +  int nTerm;
1.104499 +  struct sqlite3_index_constraint *pIdxCons;
1.104500 +  struct sqlite3_index_orderby *pIdxOrderBy;
1.104501 +  struct sqlite3_index_constraint_usage *pUsage;
1.104502 +  WhereTerm *pTerm;
1.104503 +  int nOrderBy;
1.104504 +  sqlite3_index_info *pIdxInfo;
1.104505 +
1.104506 +  WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
1.104507 +
1.104508 +  /* Count the number of possible WHERE clause constraints referring
1.104509 +  ** to this virtual table */
1.104510 +  for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
1.104511 +    if( pTerm->leftCursor != pSrc->iCursor ) continue;
1.104512 +    assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
1.104513 +    testcase( pTerm->eOperator==WO_IN );
1.104514 +    testcase( pTerm->eOperator==WO_ISNULL );
1.104515 +    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
1.104516 +    if( pTerm->wtFlags & TERM_VNULL ) continue;
1.104517 +    nTerm++;
1.104518 +  }
1.104519 +
1.104520 +  /* If the ORDER BY clause contains only columns in the current 
1.104521 +  ** virtual table then allocate space for the aOrderBy part of
1.104522 +  ** the sqlite3_index_info structure.
1.104523 +  */
1.104524 +  nOrderBy = 0;
1.104525 +  if( pOrderBy ){
1.104526 +    int n = pOrderBy->nExpr;
1.104527 +    for(i=0; i<n; i++){
1.104528 +      Expr *pExpr = pOrderBy->a[i].pExpr;
1.104529 +      if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
1.104530 +    }
1.104531 +    if( i==n){
1.104532 +      nOrderBy = n;
1.104533 +    }
1.104534 +  }
1.104535 +
1.104536 +  /* Allocate the sqlite3_index_info structure
1.104537 +  */
1.104538 +  pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
1.104539 +                           + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
1.104540 +                           + sizeof(*pIdxOrderBy)*nOrderBy );
1.104541 +  if( pIdxInfo==0 ){
1.104542 +    sqlite3ErrorMsg(pParse, "out of memory");
1.104543 +    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
1.104544 +    return 0;
1.104545 +  }
1.104546 +
1.104547 +  /* Initialize the structure.  The sqlite3_index_info structure contains
1.104548 +  ** many fields that are declared "const" to prevent xBestIndex from
1.104549 +  ** changing them.  We have to do some funky casting in order to
1.104550 +  ** initialize those fields.
1.104551 +  */
1.104552 +  pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
1.104553 +  pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
1.104554 +  pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
1.104555 +  *(int*)&pIdxInfo->nConstraint = nTerm;
1.104556 +  *(int*)&pIdxInfo->nOrderBy = nOrderBy;
1.104557 +  *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
1.104558 +  *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
1.104559 +  *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
1.104560 +                                                                   pUsage;
1.104561 +
1.104562 +  for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
1.104563 +    if( pTerm->leftCursor != pSrc->iCursor ) continue;
1.104564 +    assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
1.104565 +    testcase( pTerm->eOperator==WO_IN );
1.104566 +    testcase( pTerm->eOperator==WO_ISNULL );
1.104567 +    if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
1.104568 +    if( pTerm->wtFlags & TERM_VNULL ) continue;
1.104569 +    pIdxCons[j].iColumn = pTerm->u.leftColumn;
1.104570 +    pIdxCons[j].iTermOffset = i;
1.104571 +    pIdxCons[j].op = (u8)pTerm->eOperator;
1.104572 +    /* The direct assignment in the previous line is possible only because
1.104573 +    ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
1.104574 +    ** following asserts verify this fact. */
1.104575 +    assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
1.104576 +    assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
1.104577 +    assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
1.104578 +    assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
1.104579 +    assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
1.104580 +    assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
1.104581 +    assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
1.104582 +    j++;
1.104583 +  }
1.104584 +  for(i=0; i<nOrderBy; i++){
1.104585 +    Expr *pExpr = pOrderBy->a[i].pExpr;
1.104586 +    pIdxOrderBy[i].iColumn = pExpr->iColumn;
1.104587 +    pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
1.104588 +  }
1.104589 +
1.104590 +  return pIdxInfo;
1.104591 +}
1.104592 +
1.104593 +/*
1.104594 +** The table object reference passed as the second argument to this function
1.104595 +** must represent a virtual table. This function invokes the xBestIndex()
1.104596 +** method of the virtual table with the sqlite3_index_info pointer passed
1.104597 +** as the argument.
1.104598 +**
1.104599 +** If an error occurs, pParse is populated with an error message and a
1.104600 +** non-zero value is returned. Otherwise, 0 is returned and the output
1.104601 +** part of the sqlite3_index_info structure is left populated.
1.104602 +**
1.104603 +** Whether or not an error is returned, it is the responsibility of the
1.104604 +** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
1.104605 +** that this is required.
1.104606 +*/
1.104607 +static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
1.104608 +  sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
1.104609 +  int i;
1.104610 +  int rc;
1.104611 +
1.104612 +  WHERETRACE(("xBestIndex for %s\n", pTab->zName));
1.104613 +  TRACE_IDX_INPUTS(p);
1.104614 +  rc = pVtab->pModule->xBestIndex(pVtab, p);
1.104615 +  TRACE_IDX_OUTPUTS(p);
1.104616 +
1.104617 +  if( rc!=SQLITE_OK ){
1.104618 +    if( rc==SQLITE_NOMEM ){
1.104619 +      pParse->db->mallocFailed = 1;
1.104620 +    }else if( !pVtab->zErrMsg ){
1.104621 +      sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
1.104622 +    }else{
1.104623 +      sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
1.104624 +    }
1.104625 +  }
1.104626 +  sqlite3_free(pVtab->zErrMsg);
1.104627 +  pVtab->zErrMsg = 0;
1.104628 +
1.104629 +  for(i=0; i<p->nConstraint; i++){
1.104630 +    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
1.104631 +      sqlite3ErrorMsg(pParse, 
1.104632 +          "table %s: xBestIndex returned an invalid plan", pTab->zName);
1.104633 +    }
1.104634 +  }
1.104635 +
1.104636 +  return pParse->nErr;
1.104637 +}
1.104638 +
1.104639 +
1.104640 +/*
1.104641 +** Compute the best index for a virtual table.
1.104642 +**
1.104643 +** The best index is computed by the xBestIndex method of the virtual
1.104644 +** table module.  This routine is really just a wrapper that sets up
1.104645 +** the sqlite3_index_info structure that is used to communicate with
1.104646 +** xBestIndex.
1.104647 +**
1.104648 +** In a join, this routine might be called multiple times for the
1.104649 +** same virtual table.  The sqlite3_index_info structure is created
1.104650 +** and initialized on the first invocation and reused on all subsequent
1.104651 +** invocations.  The sqlite3_index_info structure is also used when
1.104652 +** code is generated to access the virtual table.  The whereInfoDelete() 
1.104653 +** routine takes care of freeing the sqlite3_index_info structure after
1.104654 +** everybody has finished with it.
1.104655 +*/
1.104656 +static void bestVirtualIndex(WhereBestIdx *p){
1.104657 +  Parse *pParse = p->pParse;      /* The parsing context */
1.104658 +  WhereClause *pWC = p->pWC;      /* The WHERE clause */
1.104659 +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
1.104660 +  Table *pTab = pSrc->pTab;
1.104661 +  sqlite3_index_info *pIdxInfo;
1.104662 +  struct sqlite3_index_constraint *pIdxCons;
1.104663 +  struct sqlite3_index_constraint_usage *pUsage;
1.104664 +  WhereTerm *pTerm;
1.104665 +  int i, j;
1.104666 +  int nOrderBy;
1.104667 +  double rCost;
1.104668 +
1.104669 +  /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
1.104670 +  ** malloc in allocateIndexInfo() fails and this function returns leaving
1.104671 +  ** wsFlags in an uninitialized state, the caller may behave unpredictably.
1.104672 +  */
1.104673 +  memset(&p->cost, 0, sizeof(p->cost));
1.104674 +  p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
1.104675 +
1.104676 +  /* If the sqlite3_index_info structure has not been previously
1.104677 +  ** allocated and initialized, then allocate and initialize it now.
1.104678 +  */
1.104679 +  pIdxInfo = *p->ppIdxInfo;
1.104680 +  if( pIdxInfo==0 ){
1.104681 +    *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
1.104682 +  }
1.104683 +  if( pIdxInfo==0 ){
1.104684 +    return;
1.104685 +  }
1.104686 +
1.104687 +  /* At this point, the sqlite3_index_info structure that pIdxInfo points
1.104688 +  ** to will have been initialized, either during the current invocation or
1.104689 +  ** during some prior invocation.  Now we just have to customize the
1.104690 +  ** details of pIdxInfo for the current invocation and pass it to
1.104691 +  ** xBestIndex.
1.104692 +  */
1.104693 +
1.104694 +  /* The module name must be defined. Also, by this point there must
1.104695 +  ** be a pointer to an sqlite3_vtab structure. Otherwise
1.104696 +  ** sqlite3ViewGetColumnNames() would have picked up the error. 
1.104697 +  */
1.104698 +  assert( pTab->azModuleArg && pTab->azModuleArg[0] );
1.104699 +  assert( sqlite3GetVTable(pParse->db, pTab) );
1.104700 +
1.104701 +  /* Set the aConstraint[].usable fields and initialize all 
1.104702 +  ** output variables to zero.
1.104703 +  **
1.104704 +  ** aConstraint[].usable is true for constraints where the right-hand
1.104705 +  ** side contains only references to tables to the left of the current
1.104706 +  ** table.  In other words, if the constraint is of the form:
1.104707 +  **
1.104708 +  **           column = expr
1.104709 +  **
1.104710 +  ** and we are evaluating a join, then the constraint on column is 
1.104711 +  ** only valid if all tables referenced in expr occur to the left
1.104712 +  ** of the table containing column.
1.104713 +  **
1.104714 +  ** The aConstraints[] array contains entries for all constraints
1.104715 +  ** on the current table.  That way we only have to compute it once
1.104716 +  ** even though we might try to pick the best index multiple times.
1.104717 +  ** For each attempt at picking an index, the order of tables in the
1.104718 +  ** join might be different so we have to recompute the usable flag
1.104719 +  ** each time.
1.104720 +  */
1.104721 +  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
1.104722 +  pUsage = pIdxInfo->aConstraintUsage;
1.104723 +  for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
1.104724 +    j = pIdxCons->iTermOffset;
1.104725 +    pTerm = &pWC->a[j];
1.104726 +    pIdxCons->usable = (pTerm->prereqRight&p->notReady) ? 0 : 1;
1.104727 +  }
1.104728 +  memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
1.104729 +  if( pIdxInfo->needToFreeIdxStr ){
1.104730 +    sqlite3_free(pIdxInfo->idxStr);
1.104731 +  }
1.104732 +  pIdxInfo->idxStr = 0;
1.104733 +  pIdxInfo->idxNum = 0;
1.104734 +  pIdxInfo->needToFreeIdxStr = 0;
1.104735 +  pIdxInfo->orderByConsumed = 0;
1.104736 +  /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
1.104737 +  pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
1.104738 +  nOrderBy = pIdxInfo->nOrderBy;
1.104739 +  if( !p->pOrderBy ){
1.104740 +    pIdxInfo->nOrderBy = 0;
1.104741 +  }
1.104742 +
1.104743 +  if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
1.104744 +    return;
1.104745 +  }
1.104746 +
1.104747 +  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
1.104748 +  for(i=0; i<pIdxInfo->nConstraint; i++){
1.104749 +    if( pUsage[i].argvIndex>0 ){
1.104750 +      p->cost.used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
1.104751 +    }
1.104752 +  }
1.104753 +
1.104754 +  /* If there is an ORDER BY clause, and the selected virtual table index
1.104755 +  ** does not satisfy it, increase the cost of the scan accordingly. This
1.104756 +  ** matches the processing for non-virtual tables in bestBtreeIndex().
1.104757 +  */
1.104758 +  rCost = pIdxInfo->estimatedCost;
1.104759 +  if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
1.104760 +    rCost += estLog(rCost)*rCost;
1.104761 +  }
1.104762 +
1.104763 +  /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
1.104764 +  ** inital value of lowestCost in this loop. If it is, then the
1.104765 +  ** (cost<lowestCost) test below will never be true.
1.104766 +  ** 
1.104767 +  ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
1.104768 +  ** is defined.
1.104769 +  */
1.104770 +  if( (SQLITE_BIG_DBL/((double)2))<rCost ){
1.104771 +    p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
1.104772 +  }else{
1.104773 +    p->cost.rCost = rCost;
1.104774 +  }
1.104775 +  p->cost.plan.u.pVtabIdx = pIdxInfo;
1.104776 +  if( pIdxInfo->orderByConsumed ){
1.104777 +    p->cost.plan.wsFlags |= WHERE_ORDERED;
1.104778 +    p->cost.plan.nOBSat = nOrderBy;
1.104779 +  }else{
1.104780 +    p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
1.104781 +  }
1.104782 +  p->cost.plan.nEq = 0;
1.104783 +  pIdxInfo->nOrderBy = nOrderBy;
1.104784 +
1.104785 +  /* Try to find a more efficient access pattern by using multiple indexes
1.104786 +  ** to optimize an OR expression within the WHERE clause. 
1.104787 +  */
1.104788 +  bestOrClauseIndex(p);
1.104789 +}
1.104790 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.104791 +
1.104792 +#ifdef SQLITE_ENABLE_STAT3
1.104793 +/*
1.104794 +** Estimate the location of a particular key among all keys in an
1.104795 +** index.  Store the results in aStat as follows:
1.104796 +**
1.104797 +**    aStat[0]      Est. number of rows less than pVal
1.104798 +**    aStat[1]      Est. number of rows equal to pVal
1.104799 +**
1.104800 +** Return SQLITE_OK on success.
1.104801 +*/
1.104802 +static int whereKeyStats(
1.104803 +  Parse *pParse,              /* Database connection */
1.104804 +  Index *pIdx,                /* Index to consider domain of */
1.104805 +  sqlite3_value *pVal,        /* Value to consider */
1.104806 +  int roundUp,                /* Round up if true.  Round down if false */
1.104807 +  tRowcnt *aStat              /* OUT: stats written here */
1.104808 +){
1.104809 +  tRowcnt n;
1.104810 +  IndexSample *aSample;
1.104811 +  int i, eType;
1.104812 +  int isEq = 0;
1.104813 +  i64 v;
1.104814 +  double r, rS;
1.104815 +
1.104816 +  assert( roundUp==0 || roundUp==1 );
1.104817 +  assert( pIdx->nSample>0 );
1.104818 +  if( pVal==0 ) return SQLITE_ERROR;
1.104819 +  n = pIdx->aiRowEst[0];
1.104820 +  aSample = pIdx->aSample;
1.104821 +  eType = sqlite3_value_type(pVal);
1.104822 +
1.104823 +  if( eType==SQLITE_INTEGER ){
1.104824 +    v = sqlite3_value_int64(pVal);
1.104825 +    r = (i64)v;
1.104826 +    for(i=0; i<pIdx->nSample; i++){
1.104827 +      if( aSample[i].eType==SQLITE_NULL ) continue;
1.104828 +      if( aSample[i].eType>=SQLITE_TEXT ) break;
1.104829 +      if( aSample[i].eType==SQLITE_INTEGER ){
1.104830 +        if( aSample[i].u.i>=v ){
1.104831 +          isEq = aSample[i].u.i==v;
1.104832 +          break;
1.104833 +        }
1.104834 +      }else{
1.104835 +        assert( aSample[i].eType==SQLITE_FLOAT );
1.104836 +        if( aSample[i].u.r>=r ){
1.104837 +          isEq = aSample[i].u.r==r;
1.104838 +          break;
1.104839 +        }
1.104840 +      }
1.104841 +    }
1.104842 +  }else if( eType==SQLITE_FLOAT ){
1.104843 +    r = sqlite3_value_double(pVal);
1.104844 +    for(i=0; i<pIdx->nSample; i++){
1.104845 +      if( aSample[i].eType==SQLITE_NULL ) continue;
1.104846 +      if( aSample[i].eType>=SQLITE_TEXT ) break;
1.104847 +      if( aSample[i].eType==SQLITE_FLOAT ){
1.104848 +        rS = aSample[i].u.r;
1.104849 +      }else{
1.104850 +        rS = aSample[i].u.i;
1.104851 +      }
1.104852 +      if( rS>=r ){
1.104853 +        isEq = rS==r;
1.104854 +        break;
1.104855 +      }
1.104856 +    }
1.104857 +  }else if( eType==SQLITE_NULL ){
1.104858 +    i = 0;
1.104859 +    if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
1.104860 +  }else{
1.104861 +    assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
1.104862 +    for(i=0; i<pIdx->nSample; i++){
1.104863 +      if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
1.104864 +        break;
1.104865 +      }
1.104866 +    }
1.104867 +    if( i<pIdx->nSample ){      
1.104868 +      sqlite3 *db = pParse->db;
1.104869 +      CollSeq *pColl;
1.104870 +      const u8 *z;
1.104871 +      if( eType==SQLITE_BLOB ){
1.104872 +        z = (const u8 *)sqlite3_value_blob(pVal);
1.104873 +        pColl = db->pDfltColl;
1.104874 +        assert( pColl->enc==SQLITE_UTF8 );
1.104875 +      }else{
1.104876 +        pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
1.104877 +        if( pColl==0 ){
1.104878 +          return SQLITE_ERROR;
1.104879 +        }
1.104880 +        z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
1.104881 +        if( !z ){
1.104882 +          return SQLITE_NOMEM;
1.104883 +        }
1.104884 +        assert( z && pColl && pColl->xCmp );
1.104885 +      }
1.104886 +      n = sqlite3ValueBytes(pVal, pColl->enc);
1.104887 +  
1.104888 +      for(; i<pIdx->nSample; i++){
1.104889 +        int c;
1.104890 +        int eSampletype = aSample[i].eType;
1.104891 +        if( eSampletype<eType ) continue;
1.104892 +        if( eSampletype!=eType ) break;
1.104893 +#ifndef SQLITE_OMIT_UTF16
1.104894 +        if( pColl->enc!=SQLITE_UTF8 ){
1.104895 +          int nSample;
1.104896 +          char *zSample = sqlite3Utf8to16(
1.104897 +              db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
1.104898 +          );
1.104899 +          if( !zSample ){
1.104900 +            assert( db->mallocFailed );
1.104901 +            return SQLITE_NOMEM;
1.104902 +          }
1.104903 +          c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
1.104904 +          sqlite3DbFree(db, zSample);
1.104905 +        }else
1.104906 +#endif
1.104907 +        {
1.104908 +          c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
1.104909 +        }
1.104910 +        if( c>=0 ){
1.104911 +          if( c==0 ) isEq = 1;
1.104912 +          break;
1.104913 +        }
1.104914 +      }
1.104915 +    }
1.104916 +  }
1.104917 +
1.104918 +  /* At this point, aSample[i] is the first sample that is greater than
1.104919 +  ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
1.104920 +  ** than pVal.  If aSample[i]==pVal, then isEq==1.
1.104921 +  */
1.104922 +  if( isEq ){
1.104923 +    assert( i<pIdx->nSample );
1.104924 +    aStat[0] = aSample[i].nLt;
1.104925 +    aStat[1] = aSample[i].nEq;
1.104926 +  }else{
1.104927 +    tRowcnt iLower, iUpper, iGap;
1.104928 +    if( i==0 ){
1.104929 +      iLower = 0;
1.104930 +      iUpper = aSample[0].nLt;
1.104931 +    }else{
1.104932 +      iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
1.104933 +      iLower = aSample[i-1].nEq + aSample[i-1].nLt;
1.104934 +    }
1.104935 +    aStat[1] = pIdx->avgEq;
1.104936 +    if( iLower>=iUpper ){
1.104937 +      iGap = 0;
1.104938 +    }else{
1.104939 +      iGap = iUpper - iLower;
1.104940 +    }
1.104941 +    if( roundUp ){
1.104942 +      iGap = (iGap*2)/3;
1.104943 +    }else{
1.104944 +      iGap = iGap/3;
1.104945 +    }
1.104946 +    aStat[0] = iLower + iGap;
1.104947 +  }
1.104948 +  return SQLITE_OK;
1.104949 +}
1.104950 +#endif /* SQLITE_ENABLE_STAT3 */
1.104951 +
1.104952 +/*
1.104953 +** If expression pExpr represents a literal value, set *pp to point to
1.104954 +** an sqlite3_value structure containing the same value, with affinity
1.104955 +** aff applied to it, before returning. It is the responsibility of the 
1.104956 +** caller to eventually release this structure by passing it to 
1.104957 +** sqlite3ValueFree().
1.104958 +**
1.104959 +** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
1.104960 +** is an SQL variable that currently has a non-NULL value bound to it,
1.104961 +** create an sqlite3_value structure containing this value, again with
1.104962 +** affinity aff applied to it, instead.
1.104963 +**
1.104964 +** If neither of the above apply, set *pp to NULL.
1.104965 +**
1.104966 +** If an error occurs, return an error code. Otherwise, SQLITE_OK.
1.104967 +*/
1.104968 +#ifdef SQLITE_ENABLE_STAT3
1.104969 +static int valueFromExpr(
1.104970 +  Parse *pParse, 
1.104971 +  Expr *pExpr, 
1.104972 +  u8 aff, 
1.104973 +  sqlite3_value **pp
1.104974 +){
1.104975 +  if( pExpr->op==TK_VARIABLE
1.104976 +   || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
1.104977 +  ){
1.104978 +    int iVar = pExpr->iColumn;
1.104979 +    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
1.104980 +    *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
1.104981 +    return SQLITE_OK;
1.104982 +  }
1.104983 +  return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
1.104984 +}
1.104985 +#endif
1.104986 +
1.104987 +/*
1.104988 +** This function is used to estimate the number of rows that will be visited
1.104989 +** by scanning an index for a range of values. The range may have an upper
1.104990 +** bound, a lower bound, or both. The WHERE clause terms that set the upper
1.104991 +** and lower bounds are represented by pLower and pUpper respectively. For
1.104992 +** example, assuming that index p is on t1(a):
1.104993 +**
1.104994 +**   ... FROM t1 WHERE a > ? AND a < ? ...
1.104995 +**                    |_____|   |_____|
1.104996 +**                       |         |
1.104997 +**                     pLower    pUpper
1.104998 +**
1.104999 +** If either of the upper or lower bound is not present, then NULL is passed in
1.105000 +** place of the corresponding WhereTerm.
1.105001 +**
1.105002 +** The nEq parameter is passed the index of the index column subject to the
1.105003 +** range constraint. Or, equivalently, the number of equality constraints
1.105004 +** optimized by the proposed index scan. For example, assuming index p is
1.105005 +** on t1(a, b), and the SQL query is:
1.105006 +**
1.105007 +**   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
1.105008 +**
1.105009 +** then nEq should be passed the value 1 (as the range restricted column,
1.105010 +** b, is the second left-most column of the index). Or, if the query is:
1.105011 +**
1.105012 +**   ... FROM t1 WHERE a > ? AND a < ? ...
1.105013 +**
1.105014 +** then nEq should be passed 0.
1.105015 +**
1.105016 +** The returned value is an integer divisor to reduce the estimated
1.105017 +** search space.  A return value of 1 means that range constraints are
1.105018 +** no help at all.  A return value of 2 means range constraints are
1.105019 +** expected to reduce the search space by half.  And so forth...
1.105020 +**
1.105021 +** In the absence of sqlite_stat3 ANALYZE data, each range inequality
1.105022 +** reduces the search space by a factor of 4.  Hence a single constraint (x>?)
1.105023 +** results in a return of 4 and a range constraint (x>? AND x<?) results
1.105024 +** in a return of 16.
1.105025 +*/
1.105026 +static int whereRangeScanEst(
1.105027 +  Parse *pParse,       /* Parsing & code generating context */
1.105028 +  Index *p,            /* The index containing the range-compared column; "x" */
1.105029 +  int nEq,             /* index into p->aCol[] of the range-compared column */
1.105030 +  WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
1.105031 +  WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
1.105032 +  double *pRangeDiv   /* OUT: Reduce search space by this divisor */
1.105033 +){
1.105034 +  int rc = SQLITE_OK;
1.105035 +
1.105036 +#ifdef SQLITE_ENABLE_STAT3
1.105037 +
1.105038 +  if( nEq==0 && p->nSample ){
1.105039 +    sqlite3_value *pRangeVal;
1.105040 +    tRowcnt iLower = 0;
1.105041 +    tRowcnt iUpper = p->aiRowEst[0];
1.105042 +    tRowcnt a[2];
1.105043 +    u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
1.105044 +
1.105045 +    if( pLower ){
1.105046 +      Expr *pExpr = pLower->pExpr->pRight;
1.105047 +      rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
1.105048 +      assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
1.105049 +      if( rc==SQLITE_OK
1.105050 +       && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
1.105051 +      ){
1.105052 +        iLower = a[0];
1.105053 +        if( pLower->eOperator==WO_GT ) iLower += a[1];
1.105054 +      }
1.105055 +      sqlite3ValueFree(pRangeVal);
1.105056 +    }
1.105057 +    if( rc==SQLITE_OK && pUpper ){
1.105058 +      Expr *pExpr = pUpper->pExpr->pRight;
1.105059 +      rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
1.105060 +      assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
1.105061 +      if( rc==SQLITE_OK
1.105062 +       && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
1.105063 +      ){
1.105064 +        iUpper = a[0];
1.105065 +        if( pUpper->eOperator==WO_LE ) iUpper += a[1];
1.105066 +      }
1.105067 +      sqlite3ValueFree(pRangeVal);
1.105068 +    }
1.105069 +    if( rc==SQLITE_OK ){
1.105070 +      if( iUpper<=iLower ){
1.105071 +        *pRangeDiv = (double)p->aiRowEst[0];
1.105072 +      }else{
1.105073 +        *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
1.105074 +      }
1.105075 +      WHERETRACE(("range scan regions: %u..%u  div=%g\n",
1.105076 +                  (u32)iLower, (u32)iUpper, *pRangeDiv));
1.105077 +      return SQLITE_OK;
1.105078 +    }
1.105079 +  }
1.105080 +#else
1.105081 +  UNUSED_PARAMETER(pParse);
1.105082 +  UNUSED_PARAMETER(p);
1.105083 +  UNUSED_PARAMETER(nEq);
1.105084 +#endif
1.105085 +  assert( pLower || pUpper );
1.105086 +  *pRangeDiv = (double)1;
1.105087 +  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
1.105088 +  if( pUpper ) *pRangeDiv *= (double)4;
1.105089 +  return rc;
1.105090 +}
1.105091 +
1.105092 +#ifdef SQLITE_ENABLE_STAT3
1.105093 +/*
1.105094 +** Estimate the number of rows that will be returned based on
1.105095 +** an equality constraint x=VALUE and where that VALUE occurs in
1.105096 +** the histogram data.  This only works when x is the left-most
1.105097 +** column of an index and sqlite_stat3 histogram data is available
1.105098 +** for that index.  When pExpr==NULL that means the constraint is
1.105099 +** "x IS NULL" instead of "x=VALUE".
1.105100 +**
1.105101 +** Write the estimated row count into *pnRow and return SQLITE_OK. 
1.105102 +** If unable to make an estimate, leave *pnRow unchanged and return
1.105103 +** non-zero.
1.105104 +**
1.105105 +** This routine can fail if it is unable to load a collating sequence
1.105106 +** required for string comparison, or if unable to allocate memory
1.105107 +** for a UTF conversion required for comparison.  The error is stored
1.105108 +** in the pParse structure.
1.105109 +*/
1.105110 +static int whereEqualScanEst(
1.105111 +  Parse *pParse,       /* Parsing & code generating context */
1.105112 +  Index *p,            /* The index whose left-most column is pTerm */
1.105113 +  Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
1.105114 +  double *pnRow        /* Write the revised row estimate here */
1.105115 +){
1.105116 +  sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
1.105117 +  u8 aff;                   /* Column affinity */
1.105118 +  int rc;                   /* Subfunction return code */
1.105119 +  tRowcnt a[2];             /* Statistics */
1.105120 +
1.105121 +  assert( p->aSample!=0 );
1.105122 +  assert( p->nSample>0 );
1.105123 +  aff = p->pTable->aCol[p->aiColumn[0]].affinity;
1.105124 +  if( pExpr ){
1.105125 +    rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
1.105126 +    if( rc ) goto whereEqualScanEst_cancel;
1.105127 +  }else{
1.105128 +    pRhs = sqlite3ValueNew(pParse->db);
1.105129 +  }
1.105130 +  if( pRhs==0 ) return SQLITE_NOTFOUND;
1.105131 +  rc = whereKeyStats(pParse, p, pRhs, 0, a);
1.105132 +  if( rc==SQLITE_OK ){
1.105133 +    WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
1.105134 +    *pnRow = a[1];
1.105135 +  }
1.105136 +whereEqualScanEst_cancel:
1.105137 +  sqlite3ValueFree(pRhs);
1.105138 +  return rc;
1.105139 +}
1.105140 +#endif /* defined(SQLITE_ENABLE_STAT3) */
1.105141 +
1.105142 +#ifdef SQLITE_ENABLE_STAT3
1.105143 +/*
1.105144 +** Estimate the number of rows that will be returned based on
1.105145 +** an IN constraint where the right-hand side of the IN operator
1.105146 +** is a list of values.  Example:
1.105147 +**
1.105148 +**        WHERE x IN (1,2,3,4)
1.105149 +**
1.105150 +** Write the estimated row count into *pnRow and return SQLITE_OK. 
1.105151 +** If unable to make an estimate, leave *pnRow unchanged and return
1.105152 +** non-zero.
1.105153 +**
1.105154 +** This routine can fail if it is unable to load a collating sequence
1.105155 +** required for string comparison, or if unable to allocate memory
1.105156 +** for a UTF conversion required for comparison.  The error is stored
1.105157 +** in the pParse structure.
1.105158 +*/
1.105159 +static int whereInScanEst(
1.105160 +  Parse *pParse,       /* Parsing & code generating context */
1.105161 +  Index *p,            /* The index whose left-most column is pTerm */
1.105162 +  ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
1.105163 +  double *pnRow        /* Write the revised row estimate here */
1.105164 +){
1.105165 +  int rc = SQLITE_OK;         /* Subfunction return code */
1.105166 +  double nEst;                /* Number of rows for a single term */
1.105167 +  double nRowEst = (double)0; /* New estimate of the number of rows */
1.105168 +  int i;                      /* Loop counter */
1.105169 +
1.105170 +  assert( p->aSample!=0 );
1.105171 +  for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
1.105172 +    nEst = p->aiRowEst[0];
1.105173 +    rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
1.105174 +    nRowEst += nEst;
1.105175 +  }
1.105176 +  if( rc==SQLITE_OK ){
1.105177 +    if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
1.105178 +    *pnRow = nRowEst;
1.105179 +    WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
1.105180 +  }
1.105181 +  return rc;
1.105182 +}
1.105183 +#endif /* defined(SQLITE_ENABLE_STAT3) */
1.105184 +
1.105185 +/*
1.105186 +** Check to see if column iCol of the table with cursor iTab will appear
1.105187 +** in sorted order according to the current query plan.
1.105188 +**
1.105189 +** Return values:
1.105190 +**
1.105191 +**    0   iCol is not ordered
1.105192 +**    1   iCol has only a single value
1.105193 +**    2   iCol is in ASC order
1.105194 +**    3   iCol is in DESC order
1.105195 +*/
1.105196 +static int isOrderedColumn(
1.105197 +  WhereBestIdx *p,
1.105198 +  int iTab,
1.105199 +  int iCol
1.105200 +){
1.105201 +  int i, j;
1.105202 +  WhereLevel *pLevel = &p->aLevel[p->i-1];
1.105203 +  Index *pIdx;
1.105204 +  u8 sortOrder;
1.105205 +  for(i=p->i-1; i>=0; i--, pLevel--){
1.105206 +    if( pLevel->iTabCur!=iTab ) continue;
1.105207 +    if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
1.105208 +      return 1;
1.105209 +    }
1.105210 +    assert( (pLevel->plan.wsFlags & WHERE_ORDERED)!=0 );
1.105211 +    if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
1.105212 +      if( iCol<0 ){
1.105213 +        sortOrder = 0;
1.105214 +        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
1.105215 +      }else{
1.105216 +        int n = pIdx->nColumn;
1.105217 +        for(j=0; j<n; j++){
1.105218 +          if( iCol==pIdx->aiColumn[j] ) break;
1.105219 +        }
1.105220 +        if( j>=n ) return 0;
1.105221 +        sortOrder = pIdx->aSortOrder[j];
1.105222 +        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
1.105223 +      }
1.105224 +    }else{
1.105225 +      if( iCol!=(-1) ) return 0;
1.105226 +      sortOrder = 0;
1.105227 +      testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
1.105228 +    }
1.105229 +    if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
1.105230 +      assert( sortOrder==0 || sortOrder==1 );
1.105231 +      testcase( sortOrder==1 );
1.105232 +      sortOrder = 1 - sortOrder;
1.105233 +    }
1.105234 +    return sortOrder+2;
1.105235 +  }
1.105236 +  return 0;
1.105237 +}
1.105238 +
1.105239 +/*
1.105240 +** This routine decides if pIdx can be used to satisfy the ORDER BY
1.105241 +** clause, either in whole or in part.  The return value is the 
1.105242 +** cumulative number of terms in the ORDER BY clause that are satisfied
1.105243 +** by the index pIdx and other indices in outer loops.
1.105244 +**
1.105245 +** The table being queried has a cursor number of "base".  pIdx is the
1.105246 +** index that is postulated for use to access the table.
1.105247 +**
1.105248 +** The *pbRev value is set to 0 order 1 depending on whether or not
1.105249 +** pIdx should be run in the forward order or in reverse order.
1.105250 +*/
1.105251 +static int isSortingIndex(
1.105252 +  WhereBestIdx *p,    /* Best index search context */
1.105253 +  Index *pIdx,        /* The index we are testing */
1.105254 +  int base,           /* Cursor number for the table to be sorted */
1.105255 +  int *pbRev          /* Set to 1 for reverse-order scan of pIdx */
1.105256 +){
1.105257 +  int i;                        /* Number of pIdx terms used */
1.105258 +  int j;                        /* Number of ORDER BY terms satisfied */
1.105259 +  int sortOrder = 2;            /* 0: forward.  1: backward.  2: unknown */
1.105260 +  int nTerm;                    /* Number of ORDER BY terms */
1.105261 +  struct ExprList_item *pOBItem;/* A term of the ORDER BY clause */
1.105262 +  Table *pTab = pIdx->pTable;   /* Table that owns index pIdx */
1.105263 +  ExprList *pOrderBy;           /* The ORDER BY clause */
1.105264 +  Parse *pParse = p->pParse;    /* Parser context */
1.105265 +  sqlite3 *db = pParse->db;     /* Database connection */
1.105266 +  int nPriorSat;                /* ORDER BY terms satisfied by outer loops */
1.105267 +  int seenRowid = 0;            /* True if an ORDER BY rowid term is seen */
1.105268 +  int uniqueNotNull;            /* pIdx is UNIQUE with all terms are NOT NULL */
1.105269 +
1.105270 +  if( p->i==0 ){
1.105271 +    nPriorSat = 0;
1.105272 +  }else{
1.105273 +    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
1.105274 +    if( (p->aLevel[p->i-1].plan.wsFlags & WHERE_ORDERED)==0 ){
1.105275 +      /* This loop cannot be ordered unless the next outer loop is
1.105276 +      ** also ordered */
1.105277 +      return nPriorSat;
1.105278 +    }
1.105279 +    if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){
1.105280 +      /* Only look at the outer-most loop if the OrderByIdxJoin
1.105281 +      ** optimization is disabled */
1.105282 +      return nPriorSat;
1.105283 +    }
1.105284 +  }
1.105285 +  pOrderBy = p->pOrderBy;
1.105286 +  assert( pOrderBy!=0 );
1.105287 +  if( pIdx->bUnordered ){
1.105288 +    /* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot
1.105289 +    ** be used for sorting */
1.105290 +    return nPriorSat;
1.105291 +  }
1.105292 +  nTerm = pOrderBy->nExpr;
1.105293 +  uniqueNotNull = pIdx->onError!=OE_None;
1.105294 +  assert( nTerm>0 );
1.105295 +
1.105296 +  /* Argument pIdx must either point to a 'real' named index structure, 
1.105297 +  ** or an index structure allocated on the stack by bestBtreeIndex() to
1.105298 +  ** represent the rowid index that is part of every table.  */
1.105299 +  assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
1.105300 +
1.105301 +  /* Match terms of the ORDER BY clause against columns of
1.105302 +  ** the index.
1.105303 +  **
1.105304 +  ** Note that indices have pIdx->nColumn regular columns plus
1.105305 +  ** one additional column containing the rowid.  The rowid column
1.105306 +  ** of the index is also allowed to match against the ORDER BY
1.105307 +  ** clause.
1.105308 +  */
1.105309 +  j = nPriorSat;
1.105310 +  for(i=0,pOBItem=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
1.105311 +    Expr *pOBExpr;          /* The expression of the ORDER BY pOBItem */
1.105312 +    CollSeq *pColl;         /* The collating sequence of pOBExpr */
1.105313 +    int termSortOrder;      /* Sort order for this term */
1.105314 +    int iColumn;            /* The i-th column of the index.  -1 for rowid */
1.105315 +    int iSortOrder;         /* 1 for DESC, 0 for ASC on the i-th index term */
1.105316 +    int isEq;               /* Subject to an == or IS NULL constraint */
1.105317 +    int isMatch;            /* ORDER BY term matches the index term */
1.105318 +    const char *zColl;      /* Name of collating sequence for i-th index term */
1.105319 +    WhereTerm *pConstraint; /* A constraint in the WHERE clause */
1.105320 +
1.105321 +    /* If the next term of the ORDER BY clause refers to anything other than
1.105322 +    ** a column in the "base" table, then this index will not be of any
1.105323 +    ** further use in handling the ORDER BY. */
1.105324 +    pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
1.105325 +    if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
1.105326 +      break;
1.105327 +    }
1.105328 +
1.105329 +    /* Find column number and collating sequence for the next entry
1.105330 +    ** in the index */
1.105331 +    if( pIdx->zName && i<pIdx->nColumn ){
1.105332 +      iColumn = pIdx->aiColumn[i];
1.105333 +      if( iColumn==pIdx->pTable->iPKey ){
1.105334 +        iColumn = -1;
1.105335 +      }
1.105336 +      iSortOrder = pIdx->aSortOrder[i];
1.105337 +      zColl = pIdx->azColl[i];
1.105338 +      assert( zColl!=0 );
1.105339 +    }else{
1.105340 +      iColumn = -1;
1.105341 +      iSortOrder = 0;
1.105342 +      zColl = 0;
1.105343 +    }
1.105344 +
1.105345 +    /* Check to see if the column number and collating sequence of the
1.105346 +    ** index match the column number and collating sequence of the ORDER BY
1.105347 +    ** clause entry.  Set isMatch to 1 if they both match. */
1.105348 +    if( pOBExpr->iColumn==iColumn ){
1.105349 +      if( zColl ){
1.105350 +        pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
1.105351 +        if( !pColl ) pColl = db->pDfltColl;
1.105352 +        isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
1.105353 +      }else{
1.105354 +        isMatch = 1;
1.105355 +      }
1.105356 +    }else{
1.105357 +      isMatch = 0;
1.105358 +    }
1.105359 +
1.105360 +    /* termSortOrder is 0 or 1 for whether or not the access loop should
1.105361 +    ** run forward or backwards (respectively) in order to satisfy this 
1.105362 +    ** term of the ORDER BY clause. */
1.105363 +    assert( pOBItem->sortOrder==0 || pOBItem->sortOrder==1 );
1.105364 +    assert( iSortOrder==0 || iSortOrder==1 );
1.105365 +    termSortOrder = iSortOrder ^ pOBItem->sortOrder;
1.105366 +
1.105367 +    /* If X is the column in the index and ORDER BY clause, check to see
1.105368 +    ** if there are any X= or X IS NULL constraints in the WHERE clause. */
1.105369 +    pConstraint = findTerm(p->pWC, base, iColumn, p->notReady,
1.105370 +                           WO_EQ|WO_ISNULL|WO_IN, pIdx);
1.105371 +    if( pConstraint==0 ){
1.105372 +      isEq = 0;
1.105373 +    }else if( pConstraint->eOperator==WO_IN ){
1.105374 +      /* Constraints of the form: "X IN ..." cannot be used with an ORDER BY
1.105375 +      ** because we do not know in what order the values on the RHS of the IN
1.105376 +      ** operator will occur. */
1.105377 +      break;
1.105378 +    }else if( pConstraint->eOperator==WO_ISNULL ){
1.105379 +      uniqueNotNull = 0;
1.105380 +      isEq = 1;  /* "X IS NULL" means X has only a single value */
1.105381 +    }else if( pConstraint->prereqRight==0 ){
1.105382 +      isEq = 1;  /* Constraint "X=constant" means X has only a single value */
1.105383 +    }else{
1.105384 +      Expr *pRight = pConstraint->pExpr->pRight;
1.105385 +      if( pRight->op==TK_COLUMN ){
1.105386 +        WHERETRACE(("       .. isOrderedColumn(tab=%d,col=%d)",
1.105387 +                    pRight->iTable, pRight->iColumn));
1.105388 +        isEq = isOrderedColumn(p, pRight->iTable, pRight->iColumn);
1.105389 +        WHERETRACE((" -> isEq=%d\n", isEq));
1.105390 +
1.105391 +        /* If the constraint is of the form X=Y where Y is an ordered value
1.105392 +        ** in an outer loop, then make sure the sort order of Y matches the
1.105393 +        ** sort order required for X. */
1.105394 +        if( isMatch && isEq>=2 && isEq!=pOBItem->sortOrder+2 ){
1.105395 +          testcase( isEq==2 );
1.105396 +          testcase( isEq==3 );
1.105397 +          break;
1.105398 +        }
1.105399 +      }else{
1.105400 +        isEq = 0;  /* "X=expr" places no ordering constraints on X */
1.105401 +      }
1.105402 +    }
1.105403 +    if( !isMatch ){
1.105404 +      if( isEq==0 ){
1.105405 +        break;
1.105406 +      }else{
1.105407 +        continue;
1.105408 +      }
1.105409 +    }else if( isEq!=1 ){
1.105410 +      if( sortOrder==2 ){
1.105411 +        sortOrder = termSortOrder;
1.105412 +      }else if( termSortOrder!=sortOrder ){
1.105413 +        break;
1.105414 +      }
1.105415 +    }
1.105416 +    j++;
1.105417 +    pOBItem++;
1.105418 +    if( iColumn<0 ){
1.105419 +      seenRowid = 1;
1.105420 +      break;
1.105421 +    }else if( pTab->aCol[iColumn].notNull==0 && isEq!=1 ){
1.105422 +      testcase( isEq==0 );
1.105423 +      testcase( isEq==2 );
1.105424 +      testcase( isEq==3 );
1.105425 +      uniqueNotNull = 0;
1.105426 +    }
1.105427 +  }
1.105428 +
1.105429 +  /* If we have not found at least one ORDER BY term that matches the
1.105430 +  ** index, then show no progress. */
1.105431 +  if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat;
1.105432 +
1.105433 +  /* Return the necessary scan order back to the caller */
1.105434 +  *pbRev = sortOrder & 1;
1.105435 +
1.105436 +  /* If there was an "ORDER BY rowid" term that matched, or it is only
1.105437 +  ** possible for a single row from this table to match, then skip over
1.105438 +  ** any additional ORDER BY terms dealing with this table.
1.105439 +  */
1.105440 +  if( seenRowid || (uniqueNotNull && i>=pIdx->nColumn) ){
1.105441 +    /* Advance j over additional ORDER BY terms associated with base */
1.105442 +    WhereMaskSet *pMS = p->pWC->pMaskSet;
1.105443 +    Bitmask m = ~getMask(pMS, base);
1.105444 +    while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
1.105445 +      j++;
1.105446 +    }
1.105447 +  }
1.105448 +  return j;
1.105449 +}
1.105450 +
1.105451 +/*
1.105452 +** Find the best query plan for accessing a particular table.  Write the
1.105453 +** best query plan and its cost into the p->cost.
1.105454 +**
1.105455 +** The lowest cost plan wins.  The cost is an estimate of the amount of
1.105456 +** CPU and disk I/O needed to process the requested result.
1.105457 +** Factors that influence cost include:
1.105458 +**
1.105459 +**    *  The estimated number of rows that will be retrieved.  (The
1.105460 +**       fewer the better.)
1.105461 +**
1.105462 +**    *  Whether or not sorting must occur.
1.105463 +**
1.105464 +**    *  Whether or not there must be separate lookups in the
1.105465 +**       index and in the main table.
1.105466 +**
1.105467 +** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
1.105468 +** the SQL statement, then this function only considers plans using the 
1.105469 +** named index. If no such plan is found, then the returned cost is
1.105470 +** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
1.105471 +** then the cost is calculated in the usual way.
1.105472 +**
1.105473 +** If a NOT INDEXED clause was attached to the table 
1.105474 +** in the SELECT statement, then no indexes are considered. However, the 
1.105475 +** selected plan may still take advantage of the built-in rowid primary key
1.105476 +** index.
1.105477 +*/
1.105478 +static void bestBtreeIndex(WhereBestIdx *p){
1.105479 +  Parse *pParse = p->pParse;  /* The parsing context */
1.105480 +  WhereClause *pWC = p->pWC;  /* The WHERE clause */
1.105481 +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
1.105482 +  int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
1.105483 +  Index *pProbe;              /* An index we are evaluating */
1.105484 +  Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
1.105485 +  int eqTermMask;             /* Current mask of valid equality operators */
1.105486 +  int idxEqTermMask;          /* Index mask of valid equality operators */
1.105487 +  Index sPk;                  /* A fake index object for the primary key */
1.105488 +  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
1.105489 +  int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
1.105490 +  int wsFlagMask;             /* Allowed flags in p->cost.plan.wsFlag */
1.105491 +  int nPriorSat;              /* ORDER BY terms satisfied by outer loops */
1.105492 +  int nOrderBy;               /* Number of ORDER BY terms */
1.105493 +  char bSortInit;             /* Initializer for bSort in inner loop */
1.105494 +  char bDistInit;             /* Initializer for bDist in inner loop */
1.105495 +
1.105496 +
1.105497 +  /* Initialize the cost to a worst-case value */
1.105498 +  memset(&p->cost, 0, sizeof(p->cost));
1.105499 +  p->cost.rCost = SQLITE_BIG_DBL;
1.105500 +
1.105501 +  /* If the pSrc table is the right table of a LEFT JOIN then we may not
1.105502 +  ** use an index to satisfy IS NULL constraints on that table.  This is
1.105503 +  ** because columns might end up being NULL if the table does not match -
1.105504 +  ** a circumstance which the index cannot help us discover.  Ticket #2177.
1.105505 +  */
1.105506 +  if( pSrc->jointype & JT_LEFT ){
1.105507 +    idxEqTermMask = WO_EQ|WO_IN;
1.105508 +  }else{
1.105509 +    idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
1.105510 +  }
1.105511 +
1.105512 +  if( pSrc->pIndex ){
1.105513 +    /* An INDEXED BY clause specifies a particular index to use */
1.105514 +    pIdx = pProbe = pSrc->pIndex;
1.105515 +    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
1.105516 +    eqTermMask = idxEqTermMask;
1.105517 +  }else{
1.105518 +    /* There is no INDEXED BY clause.  Create a fake Index object in local
1.105519 +    ** variable sPk to represent the rowid primary key index.  Make this
1.105520 +    ** fake index the first in a chain of Index objects with all of the real
1.105521 +    ** indices to follow */
1.105522 +    Index *pFirst;                  /* First of real indices on the table */
1.105523 +    memset(&sPk, 0, sizeof(Index));
1.105524 +    sPk.nColumn = 1;
1.105525 +    sPk.aiColumn = &aiColumnPk;
1.105526 +    sPk.aiRowEst = aiRowEstPk;
1.105527 +    sPk.onError = OE_Replace;
1.105528 +    sPk.pTable = pSrc->pTab;
1.105529 +    aiRowEstPk[0] = pSrc->pTab->nRowEst;
1.105530 +    aiRowEstPk[1] = 1;
1.105531 +    pFirst = pSrc->pTab->pIndex;
1.105532 +    if( pSrc->notIndexed==0 ){
1.105533 +      /* The real indices of the table are only considered if the
1.105534 +      ** NOT INDEXED qualifier is omitted from the FROM clause */
1.105535 +      sPk.pNext = pFirst;
1.105536 +    }
1.105537 +    pProbe = &sPk;
1.105538 +    wsFlagMask = ~(
1.105539 +        WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
1.105540 +    );
1.105541 +    eqTermMask = WO_EQ|WO_IN;
1.105542 +    pIdx = 0;
1.105543 +  }
1.105544 +
1.105545 +  nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
1.105546 +  if( p->i ){
1.105547 +    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
1.105548 +    bSortInit = nPriorSat<nOrderBy;
1.105549 +    bDistInit = 0;
1.105550 +  }else{
1.105551 +    nPriorSat = 0;
1.105552 +    bSortInit = nOrderBy>0;
1.105553 +    bDistInit = p->pDistinct!=0;
1.105554 +  }
1.105555 +
1.105556 +  /* Loop over all indices looking for the best one to use
1.105557 +  */
1.105558 +  for(; pProbe; pIdx=pProbe=pProbe->pNext){
1.105559 +    const tRowcnt * const aiRowEst = pProbe->aiRowEst;
1.105560 +    WhereCost pc;               /* Cost of using pProbe */
1.105561 +    double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
1.105562 +
1.105563 +    /* The following variables are populated based on the properties of
1.105564 +    ** index being evaluated. They are then used to determine the expected
1.105565 +    ** cost and number of rows returned.
1.105566 +    **
1.105567 +    **  pc.plan.nEq: 
1.105568 +    **    Number of equality terms that can be implemented using the index.
1.105569 +    **    In other words, the number of initial fields in the index that
1.105570 +    **    are used in == or IN or NOT NULL constraints of the WHERE clause.
1.105571 +    **
1.105572 +    **  nInMul:  
1.105573 +    **    The "in-multiplier". This is an estimate of how many seek operations 
1.105574 +    **    SQLite must perform on the index in question. For example, if the 
1.105575 +    **    WHERE clause is:
1.105576 +    **
1.105577 +    **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
1.105578 +    **
1.105579 +    **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is 
1.105580 +    **    set to 9. Given the same schema and either of the following WHERE 
1.105581 +    **    clauses:
1.105582 +    **
1.105583 +    **      WHERE a =  1
1.105584 +    **      WHERE a >= 2
1.105585 +    **
1.105586 +    **    nInMul is set to 1.
1.105587 +    **
1.105588 +    **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
1.105589 +    **    the sub-select is assumed to return 25 rows for the purposes of 
1.105590 +    **    determining nInMul.
1.105591 +    **
1.105592 +    **  bInEst:  
1.105593 +    **    Set to true if there was at least one "x IN (SELECT ...)" term used 
1.105594 +    **    in determining the value of nInMul.  Note that the RHS of the
1.105595 +    **    IN operator must be a SELECT, not a value list, for this variable
1.105596 +    **    to be true.
1.105597 +    **
1.105598 +    **  rangeDiv:
1.105599 +    **    An estimate of a divisor by which to reduce the search space due
1.105600 +    **    to inequality constraints.  In the absence of sqlite_stat3 ANALYZE
1.105601 +    **    data, a single inequality reduces the search space to 1/4rd its
1.105602 +    **    original size (rangeDiv==4).  Two inequalities reduce the search
1.105603 +    **    space to 1/16th of its original size (rangeDiv==16).
1.105604 +    **
1.105605 +    **  bSort:   
1.105606 +    **    Boolean. True if there is an ORDER BY clause that will require an 
1.105607 +    **    external sort (i.e. scanning the index being evaluated will not 
1.105608 +    **    correctly order records).
1.105609 +    **
1.105610 +    **  bDist:
1.105611 +    **    Boolean. True if there is a DISTINCT clause that will require an 
1.105612 +    **    external btree.
1.105613 +    **
1.105614 +    **  bLookup: 
1.105615 +    **    Boolean. True if a table lookup is required for each index entry
1.105616 +    **    visited.  In other words, true if this is not a covering index.
1.105617 +    **    This is always false for the rowid primary key index of a table.
1.105618 +    **    For other indexes, it is true unless all the columns of the table
1.105619 +    **    used by the SELECT statement are present in the index (such an
1.105620 +    **    index is sometimes described as a covering index).
1.105621 +    **    For example, given the index on (a, b), the second of the following 
1.105622 +    **    two queries requires table b-tree lookups in order to find the value
1.105623 +    **    of column c, but the first does not because columns a and b are
1.105624 +    **    both available in the index.
1.105625 +    **
1.105626 +    **             SELECT a, b    FROM tbl WHERE a = 1;
1.105627 +    **             SELECT a, b, c FROM tbl WHERE a = 1;
1.105628 +    */
1.105629 +    int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
1.105630 +    int nInMul = 1;               /* Number of distinct equalities to lookup */
1.105631 +    double rangeDiv = (double)1;  /* Estimated reduction in search space */
1.105632 +    int nBound = 0;               /* Number of range constraints seen */
1.105633 +    char bSort = bSortInit;       /* True if external sort required */
1.105634 +    char bDist = bDistInit;       /* True if index cannot help with DISTINCT */
1.105635 +    char bLookup = 0;             /* True if not a covering index */
1.105636 +    WhereTerm *pTerm;             /* A single term of the WHERE clause */
1.105637 +#ifdef SQLITE_ENABLE_STAT3
1.105638 +    WhereTerm *pFirstTerm = 0;    /* First term matching the index */
1.105639 +#endif
1.105640 +
1.105641 +    WHERETRACE((
1.105642 +      "   %s(%s):\n",
1.105643 +      pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk")
1.105644 +    ));
1.105645 +    memset(&pc, 0, sizeof(pc));
1.105646 +    pc.plan.nOBSat = nPriorSat;
1.105647 +
1.105648 +    /* Determine the values of pc.plan.nEq and nInMul */
1.105649 +    for(pc.plan.nEq=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
1.105650 +      int j = pProbe->aiColumn[pc.plan.nEq];
1.105651 +      pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
1.105652 +      if( pTerm==0 ) break;
1.105653 +      pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
1.105654 +      testcase( pTerm->pWC!=pWC );
1.105655 +      if( pTerm->eOperator & WO_IN ){
1.105656 +        Expr *pExpr = pTerm->pExpr;
1.105657 +        pc.plan.wsFlags |= WHERE_COLUMN_IN;
1.105658 +        if( ExprHasProperty(pExpr, EP_xIsSelect) ){
1.105659 +          /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
1.105660 +          nInMul *= 25;
1.105661 +          bInEst = 1;
1.105662 +        }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
1.105663 +          /* "x IN (value, value, ...)" */
1.105664 +          nInMul *= pExpr->x.pList->nExpr;
1.105665 +        }
1.105666 +      }else if( pTerm->eOperator & WO_ISNULL ){
1.105667 +        pc.plan.wsFlags |= WHERE_COLUMN_NULL;
1.105668 +      }
1.105669 +#ifdef SQLITE_ENABLE_STAT3
1.105670 +      if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
1.105671 +#endif
1.105672 +      pc.used |= pTerm->prereqRight;
1.105673 +    }
1.105674 + 
1.105675 +    /* If the index being considered is UNIQUE, and there is an equality 
1.105676 +    ** constraint for all columns in the index, then this search will find
1.105677 +    ** at most a single row. In this case set the WHERE_UNIQUE flag to 
1.105678 +    ** indicate this to the caller.
1.105679 +    **
1.105680 +    ** Otherwise, if the search may find more than one row, test to see if
1.105681 +    ** there is a range constraint on indexed column (pc.plan.nEq+1) that can be 
1.105682 +    ** optimized using the index. 
1.105683 +    */
1.105684 +    if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
1.105685 +      testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
1.105686 +      testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
1.105687 +      if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
1.105688 +        pc.plan.wsFlags |= WHERE_UNIQUE;
1.105689 +        if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
1.105690 +          pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
1.105691 +        }
1.105692 +      }
1.105693 +    }else if( pProbe->bUnordered==0 ){
1.105694 +      int j;
1.105695 +      j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
1.105696 +      if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
1.105697 +        WhereTerm *pTop, *pBtm;
1.105698 +        pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
1.105699 +        pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
1.105700 +        whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
1.105701 +        if( pTop ){
1.105702 +          nBound = 1;
1.105703 +          pc.plan.wsFlags |= WHERE_TOP_LIMIT;
1.105704 +          pc.used |= pTop->prereqRight;
1.105705 +          testcase( pTop->pWC!=pWC );
1.105706 +        }
1.105707 +        if( pBtm ){
1.105708 +          nBound++;
1.105709 +          pc.plan.wsFlags |= WHERE_BTM_LIMIT;
1.105710 +          pc.used |= pBtm->prereqRight;
1.105711 +          testcase( pBtm->pWC!=pWC );
1.105712 +        }
1.105713 +        pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
1.105714 +      }
1.105715 +    }
1.105716 +
1.105717 +    /* If there is an ORDER BY clause and the index being considered will
1.105718 +    ** naturally scan rows in the required order, set the appropriate flags
1.105719 +    ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
1.105720 +    ** the index will scan rows in a different order, set the bSort
1.105721 +    ** variable.  */
1.105722 +    if( bSort && (pSrc->jointype & JT_LEFT)==0 ){
1.105723 +      int bRev = 2;
1.105724 +      WHERETRACE(("      --> before isSortingIndex: nPriorSat=%d\n",nPriorSat));
1.105725 +      pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, &bRev);
1.105726 +      WHERETRACE(("      --> after  isSortingIndex: bRev=%d nOBSat=%d\n",
1.105727 +                  bRev, pc.plan.nOBSat));
1.105728 +      if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_UNIQUE)!=0 ){
1.105729 +        pc.plan.wsFlags |= WHERE_ORDERED;
1.105730 +      }
1.105731 +      if( nOrderBy==pc.plan.nOBSat ){
1.105732 +        bSort = 0;
1.105733 +        pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
1.105734 +      }
1.105735 +      if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
1.105736 +    }
1.105737 +
1.105738 +    /* If there is a DISTINCT qualifier and this index will scan rows in
1.105739 +    ** order of the DISTINCT expressions, clear bDist and set the appropriate
1.105740 +    ** flags in pc.plan.wsFlags. */
1.105741 +    if( bDist
1.105742 +     && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
1.105743 +     && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
1.105744 +    ){
1.105745 +      bDist = 0;
1.105746 +      pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
1.105747 +    }
1.105748 +
1.105749 +    /* If currently calculating the cost of using an index (not the IPK
1.105750 +    ** index), determine if all required column data may be obtained without 
1.105751 +    ** using the main table (i.e. if the index is a covering
1.105752 +    ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
1.105753 +    ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true.  */
1.105754 +    if( pIdx ){
1.105755 +      Bitmask m = pSrc->colUsed;
1.105756 +      int j;
1.105757 +      for(j=0; j<pIdx->nColumn; j++){
1.105758 +        int x = pIdx->aiColumn[j];
1.105759 +        if( x<BMS-1 ){
1.105760 +          m &= ~(((Bitmask)1)<<x);
1.105761 +        }
1.105762 +      }
1.105763 +      if( m==0 ){
1.105764 +        pc.plan.wsFlags |= WHERE_IDX_ONLY;
1.105765 +      }else{
1.105766 +        bLookup = 1;
1.105767 +      }
1.105768 +    }
1.105769 +
1.105770 +    /*
1.105771 +    ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
1.105772 +    ** constraint, do not let the estimate exceed half the rows in the table.
1.105773 +    */
1.105774 +    pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
1.105775 +    if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
1.105776 +      pc.plan.nRow = aiRowEst[0]/2;
1.105777 +      nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
1.105778 +    }
1.105779 +
1.105780 +#ifdef SQLITE_ENABLE_STAT3
1.105781 +    /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
1.105782 +    ** and we do not think that values of x are unique and if histogram
1.105783 +    ** data is available for column x, then it might be possible
1.105784 +    ** to get a better estimate on the number of rows based on
1.105785 +    ** VALUE and how common that value is according to the histogram.
1.105786 +    */
1.105787 +    if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
1.105788 +     && pFirstTerm!=0 && aiRowEst[1]>1 ){
1.105789 +      assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
1.105790 +      if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
1.105791 +        testcase( pFirstTerm->eOperator==WO_EQ );
1.105792 +        testcase( pFirstTerm->eOperator==WO_ISNULL );
1.105793 +        whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
1.105794 +                          &pc.plan.nRow);
1.105795 +      }else if( bInEst==0 ){
1.105796 +        assert( pFirstTerm->eOperator==WO_IN );
1.105797 +        whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
1.105798 +                       &pc.plan.nRow);
1.105799 +      }
1.105800 +    }
1.105801 +#endif /* SQLITE_ENABLE_STAT3 */
1.105802 +
1.105803 +    /* Adjust the number of output rows and downward to reflect rows
1.105804 +    ** that are excluded by range constraints.
1.105805 +    */
1.105806 +    pc.plan.nRow = pc.plan.nRow/rangeDiv;
1.105807 +    if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
1.105808 +
1.105809 +    /* Experiments run on real SQLite databases show that the time needed
1.105810 +    ** to do a binary search to locate a row in a table or index is roughly
1.105811 +    ** log10(N) times the time to move from one row to the next row within
1.105812 +    ** a table or index.  The actual times can vary, with the size of
1.105813 +    ** records being an important factor.  Both moves and searches are
1.105814 +    ** slower with larger records, presumably because fewer records fit
1.105815 +    ** on one page and hence more pages have to be fetched.
1.105816 +    **
1.105817 +    ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
1.105818 +    ** not give us data on the relative sizes of table and index records.
1.105819 +    ** So this computation assumes table records are about twice as big
1.105820 +    ** as index records
1.105821 +    */
1.105822 +    if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED))==WHERE_IDX_ONLY
1.105823 +     && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
1.105824 +     && sqlite3GlobalConfig.bUseCis
1.105825 +     && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
1.105826 +    ){
1.105827 +      /* This index is not useful for indexing, but it is a covering index.
1.105828 +      ** A full-scan of the index might be a little faster than a full-scan
1.105829 +      ** of the table, so give this case a cost slightly less than a table
1.105830 +      ** scan. */
1.105831 +      pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
1.105832 +      pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
1.105833 +    }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
1.105834 +      /* The cost of a full table scan is a number of move operations equal
1.105835 +      ** to the number of rows in the table.
1.105836 +      **
1.105837 +      ** We add an additional 4x penalty to full table scans.  This causes
1.105838 +      ** the cost function to err on the side of choosing an index over
1.105839 +      ** choosing a full scan.  This 4x full-scan penalty is an arguable
1.105840 +      ** decision and one which we expect to revisit in the future.  But
1.105841 +      ** it seems to be working well enough at the moment.
1.105842 +      */
1.105843 +      pc.rCost = aiRowEst[0]*4;
1.105844 +      pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
1.105845 +      if( pIdx ){
1.105846 +        pc.plan.wsFlags &= ~WHERE_ORDERED;
1.105847 +        pc.plan.nOBSat = nPriorSat;
1.105848 +      }
1.105849 +    }else{
1.105850 +      log10N = estLog(aiRowEst[0]);
1.105851 +      pc.rCost = pc.plan.nRow;
1.105852 +      if( pIdx ){
1.105853 +        if( bLookup ){
1.105854 +          /* For an index lookup followed by a table lookup:
1.105855 +          **    nInMul index searches to find the start of each index range
1.105856 +          **  + nRow steps through the index
1.105857 +          **  + nRow table searches to lookup the table entry using the rowid
1.105858 +          */
1.105859 +          pc.rCost += (nInMul + pc.plan.nRow)*log10N;
1.105860 +        }else{
1.105861 +          /* For a covering index:
1.105862 +          **     nInMul index searches to find the initial entry 
1.105863 +          **   + nRow steps through the index
1.105864 +          */
1.105865 +          pc.rCost += nInMul*log10N;
1.105866 +        }
1.105867 +      }else{
1.105868 +        /* For a rowid primary key lookup:
1.105869 +        **    nInMult table searches to find the initial entry for each range
1.105870 +        **  + nRow steps through the table
1.105871 +        */
1.105872 +        pc.rCost += nInMul*log10N;
1.105873 +      }
1.105874 +    }
1.105875 +
1.105876 +    /* Add in the estimated cost of sorting the result.  Actual experimental
1.105877 +    ** measurements of sorting performance in SQLite show that sorting time
1.105878 +    ** adds C*N*log10(N) to the cost, where N is the number of rows to be 
1.105879 +    ** sorted and C is a factor between 1.95 and 4.3.  We will split the
1.105880 +    ** difference and select C of 3.0.
1.105881 +    */
1.105882 +    if( bSort ){
1.105883 +      double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
1.105884 +      m *= (double)(pc.plan.nOBSat ? 2 : 3);
1.105885 +      pc.rCost += pc.plan.nRow*m;
1.105886 +    }
1.105887 +    if( bDist ){
1.105888 +      pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
1.105889 +    }
1.105890 +
1.105891 +    /**** Cost of using this index has now been computed ****/
1.105892 +
1.105893 +    /* If there are additional constraints on this table that cannot
1.105894 +    ** be used with the current index, but which might lower the number
1.105895 +    ** of output rows, adjust the nRow value accordingly.  This only 
1.105896 +    ** matters if the current index is the least costly, so do not bother
1.105897 +    ** with this step if we already know this index will not be chosen.
1.105898 +    ** Also, never reduce the output row count below 2 using this step.
1.105899 +    **
1.105900 +    ** It is critical that the notValid mask be used here instead of
1.105901 +    ** the notReady mask.  When computing an "optimal" index, the notReady
1.105902 +    ** mask will only have one bit set - the bit for the current table.
1.105903 +    ** The notValid mask, on the other hand, always has all bits set for
1.105904 +    ** tables that are not in outer loops.  If notReady is used here instead
1.105905 +    ** of notValid, then a optimal index that depends on inner joins loops
1.105906 +    ** might be selected even when there exists an optimal index that has
1.105907 +    ** no such dependency.
1.105908 +    */
1.105909 +    if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
1.105910 +      int k;                       /* Loop counter */
1.105911 +      int nSkipEq = pc.plan.nEq;   /* Number of == constraints to skip */
1.105912 +      int nSkipRange = nBound;     /* Number of < constraints to skip */
1.105913 +      Bitmask thisTab;             /* Bitmap for pSrc */
1.105914 +
1.105915 +      thisTab = getMask(pWC->pMaskSet, iCur);
1.105916 +      for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
1.105917 +        if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
1.105918 +        if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
1.105919 +        if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
1.105920 +          if( nSkipEq ){
1.105921 +            /* Ignore the first pc.plan.nEq equality matches since the index
1.105922 +            ** has already accounted for these */
1.105923 +            nSkipEq--;
1.105924 +          }else{
1.105925 +            /* Assume each additional equality match reduces the result
1.105926 +            ** set size by a factor of 10 */
1.105927 +            pc.plan.nRow /= 10;
1.105928 +          }
1.105929 +        }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
1.105930 +          if( nSkipRange ){
1.105931 +            /* Ignore the first nSkipRange range constraints since the index
1.105932 +            ** has already accounted for these */
1.105933 +            nSkipRange--;
1.105934 +          }else{
1.105935 +            /* Assume each additional range constraint reduces the result
1.105936 +            ** set size by a factor of 3.  Indexed range constraints reduce
1.105937 +            ** the search space by a larger factor: 4.  We make indexed range
1.105938 +            ** more selective intentionally because of the subjective 
1.105939 +            ** observation that indexed range constraints really are more
1.105940 +            ** selective in practice, on average. */
1.105941 +            pc.plan.nRow /= 3;
1.105942 +          }
1.105943 +        }else if( pTerm->eOperator!=WO_NOOP ){
1.105944 +          /* Any other expression lowers the output row count by half */
1.105945 +          pc.plan.nRow /= 2;
1.105946 +        }
1.105947 +      }
1.105948 +      if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
1.105949 +    }
1.105950 +
1.105951 +
1.105952 +    WHERETRACE((
1.105953 +      "      nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
1.105954 +      "      notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
1.105955 +      "      used=0x%llx nOBSat=%d\n",
1.105956 +      pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
1.105957 +      p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used,
1.105958 +      pc.plan.nOBSat
1.105959 +    ));
1.105960 +
1.105961 +    /* If this index is the best we have seen so far, then record this
1.105962 +    ** index and its cost in the p->cost structure.
1.105963 +    */
1.105964 +    if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
1.105965 +      p->cost = pc;
1.105966 +      p->cost.plan.wsFlags &= wsFlagMask;
1.105967 +      p->cost.plan.u.pIdx = pIdx;
1.105968 +    }
1.105969 +
1.105970 +    /* If there was an INDEXED BY clause, then only that one index is
1.105971 +    ** considered. */
1.105972 +    if( pSrc->pIndex ) break;
1.105973 +
1.105974 +    /* Reset masks for the next index in the loop */
1.105975 +    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
1.105976 +    eqTermMask = idxEqTermMask;
1.105977 +  }
1.105978 +
1.105979 +  /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
1.105980 +  ** is set, then reverse the order that the index will be scanned
1.105981 +  ** in. This is used for application testing, to help find cases
1.105982 +  ** where application behaviour depends on the (undefined) order that
1.105983 +  ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
1.105984 +  if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
1.105985 +    p->cost.plan.wsFlags |= WHERE_REVERSE;
1.105986 +  }
1.105987 +
1.105988 +  assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
1.105989 +  assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
1.105990 +  assert( pSrc->pIndex==0 
1.105991 +       || p->cost.plan.u.pIdx==0 
1.105992 +       || p->cost.plan.u.pIdx==pSrc->pIndex 
1.105993 +  );
1.105994 +
1.105995 +  WHERETRACE(("   best index is: %s\n",
1.105996 +         p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk"));
1.105997 +  
1.105998 +  bestOrClauseIndex(p);
1.105999 +  bestAutomaticIndex(p);
1.106000 +  p->cost.plan.wsFlags |= eqTermMask;
1.106001 +}
1.106002 +
1.106003 +/*
1.106004 +** Find the query plan for accessing table pSrc->pTab. Write the
1.106005 +** best query plan and its cost into the WhereCost object supplied 
1.106006 +** as the last parameter. This function may calculate the cost of
1.106007 +** both real and virtual table scans.
1.106008 +**
1.106009 +** This function does not take ORDER BY or DISTINCT into account.  Nor
1.106010 +** does it remember the virtual table query plan.  All it does is compute
1.106011 +** the cost while determining if an OR optimization is applicable.  The
1.106012 +** details will be reconsidered later if the optimization is found to be
1.106013 +** applicable.
1.106014 +*/
1.106015 +static void bestIndex(WhereBestIdx *p){
1.106016 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.106017 +  if( IsVirtual(p->pSrc->pTab) ){
1.106018 +    sqlite3_index_info *pIdxInfo = 0;
1.106019 +    p->ppIdxInfo = &pIdxInfo;
1.106020 +    bestVirtualIndex(p);
1.106021 +    if( pIdxInfo->needToFreeIdxStr ){
1.106022 +      sqlite3_free(pIdxInfo->idxStr);
1.106023 +    }
1.106024 +    sqlite3DbFree(p->pParse->db, pIdxInfo);
1.106025 +  }else
1.106026 +#endif
1.106027 +  {
1.106028 +    bestBtreeIndex(p);
1.106029 +  }
1.106030 +}
1.106031 +
1.106032 +/*
1.106033 +** Disable a term in the WHERE clause.  Except, do not disable the term
1.106034 +** if it controls a LEFT OUTER JOIN and it did not originate in the ON
1.106035 +** or USING clause of that join.
1.106036 +**
1.106037 +** Consider the term t2.z='ok' in the following queries:
1.106038 +**
1.106039 +**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
1.106040 +**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
1.106041 +**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
1.106042 +**
1.106043 +** The t2.z='ok' is disabled in the in (2) because it originates
1.106044 +** in the ON clause.  The term is disabled in (3) because it is not part
1.106045 +** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
1.106046 +**
1.106047 +** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
1.106048 +** completely satisfied by indices.
1.106049 +**
1.106050 +** Disabling a term causes that term to not be tested in the inner loop
1.106051 +** of the join.  Disabling is an optimization.  When terms are satisfied
1.106052 +** by indices, we disable them to prevent redundant tests in the inner
1.106053 +** loop.  We would get the correct results if nothing were ever disabled,
1.106054 +** but joins might run a little slower.  The trick is to disable as much
1.106055 +** as we can without disabling too much.  If we disabled in (1), we'd get
1.106056 +** the wrong answer.  See ticket #813.
1.106057 +*/
1.106058 +static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
1.106059 +  if( pTerm
1.106060 +      && (pTerm->wtFlags & TERM_CODED)==0
1.106061 +      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
1.106062 +  ){
1.106063 +    pTerm->wtFlags |= TERM_CODED;
1.106064 +    if( pTerm->iParent>=0 ){
1.106065 +      WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
1.106066 +      if( (--pOther->nChild)==0 ){
1.106067 +        disableTerm(pLevel, pOther);
1.106068 +      }
1.106069 +    }
1.106070 +  }
1.106071 +}
1.106072 +
1.106073 +/*
1.106074 +** Code an OP_Affinity opcode to apply the column affinity string zAff
1.106075 +** to the n registers starting at base. 
1.106076 +**
1.106077 +** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
1.106078 +** beginning and end of zAff are ignored.  If all entries in zAff are
1.106079 +** SQLITE_AFF_NONE, then no code gets generated.
1.106080 +**
1.106081 +** This routine makes its own copy of zAff so that the caller is free
1.106082 +** to modify zAff after this routine returns.
1.106083 +*/
1.106084 +static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
1.106085 +  Vdbe *v = pParse->pVdbe;
1.106086 +  if( zAff==0 ){
1.106087 +    assert( pParse->db->mallocFailed );
1.106088 +    return;
1.106089 +  }
1.106090 +  assert( v!=0 );
1.106091 +
1.106092 +  /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
1.106093 +  ** and end of the affinity string.
1.106094 +  */
1.106095 +  while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
1.106096 +    n--;
1.106097 +    base++;
1.106098 +    zAff++;
1.106099 +  }
1.106100 +  while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
1.106101 +    n--;
1.106102 +  }
1.106103 +
1.106104 +  /* Code the OP_Affinity opcode if there is anything left to do. */
1.106105 +  if( n>0 ){
1.106106 +    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
1.106107 +    sqlite3VdbeChangeP4(v, -1, zAff, n);
1.106108 +    sqlite3ExprCacheAffinityChange(pParse, base, n);
1.106109 +  }
1.106110 +}
1.106111 +
1.106112 +
1.106113 +/*
1.106114 +** Generate code for a single equality term of the WHERE clause.  An equality
1.106115 +** term can be either X=expr or X IN (...).   pTerm is the term to be 
1.106116 +** coded.
1.106117 +**
1.106118 +** The current value for the constraint is left in register iReg.
1.106119 +**
1.106120 +** For a constraint of the form X=expr, the expression is evaluated and its
1.106121 +** result is left on the stack.  For constraints of the form X IN (...)
1.106122 +** this routine sets up a loop that will iterate over all values of X.
1.106123 +*/
1.106124 +static int codeEqualityTerm(
1.106125 +  Parse *pParse,      /* The parsing context */
1.106126 +  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
1.106127 +  WhereLevel *pLevel, /* When level of the FROM clause we are working on */
1.106128 +  int iTarget         /* Attempt to leave results in this register */
1.106129 +){
1.106130 +  Expr *pX = pTerm->pExpr;
1.106131 +  Vdbe *v = pParse->pVdbe;
1.106132 +  int iReg;                  /* Register holding results */
1.106133 +
1.106134 +  assert( iTarget>0 );
1.106135 +  if( pX->op==TK_EQ ){
1.106136 +    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
1.106137 +  }else if( pX->op==TK_ISNULL ){
1.106138 +    iReg = iTarget;
1.106139 +    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
1.106140 +#ifndef SQLITE_OMIT_SUBQUERY
1.106141 +  }else{
1.106142 +    int eType;
1.106143 +    int iTab;
1.106144 +    struct InLoop *pIn;
1.106145 +
1.106146 +    assert( pX->op==TK_IN );
1.106147 +    iReg = iTarget;
1.106148 +    eType = sqlite3FindInIndex(pParse, pX, 0);
1.106149 +    iTab = pX->iTable;
1.106150 +    sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
1.106151 +    assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
1.106152 +    if( pLevel->u.in.nIn==0 ){
1.106153 +      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
1.106154 +    }
1.106155 +    pLevel->u.in.nIn++;
1.106156 +    pLevel->u.in.aInLoop =
1.106157 +       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
1.106158 +                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
1.106159 +    pIn = pLevel->u.in.aInLoop;
1.106160 +    if( pIn ){
1.106161 +      pIn += pLevel->u.in.nIn - 1;
1.106162 +      pIn->iCur = iTab;
1.106163 +      if( eType==IN_INDEX_ROWID ){
1.106164 +        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
1.106165 +      }else{
1.106166 +        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
1.106167 +      }
1.106168 +      sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
1.106169 +    }else{
1.106170 +      pLevel->u.in.nIn = 0;
1.106171 +    }
1.106172 +#endif
1.106173 +  }
1.106174 +  disableTerm(pLevel, pTerm);
1.106175 +  return iReg;
1.106176 +}
1.106177 +
1.106178 +/*
1.106179 +** Generate code that will evaluate all == and IN constraints for an
1.106180 +** index.
1.106181 +**
1.106182 +** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
1.106183 +** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
1.106184 +** The index has as many as three equality constraints, but in this
1.106185 +** example, the third "c" value is an inequality.  So only two 
1.106186 +** constraints are coded.  This routine will generate code to evaluate
1.106187 +** a==5 and b IN (1,2,3).  The current values for a and b will be stored
1.106188 +** in consecutive registers and the index of the first register is returned.
1.106189 +**
1.106190 +** In the example above nEq==2.  But this subroutine works for any value
1.106191 +** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
1.106192 +** The only thing it does is allocate the pLevel->iMem memory cell and
1.106193 +** compute the affinity string.
1.106194 +**
1.106195 +** This routine always allocates at least one memory cell and returns
1.106196 +** the index of that memory cell. The code that
1.106197 +** calls this routine will use that memory cell to store the termination
1.106198 +** key value of the loop.  If one or more IN operators appear, then
1.106199 +** this routine allocates an additional nEq memory cells for internal
1.106200 +** use.
1.106201 +**
1.106202 +** Before returning, *pzAff is set to point to a buffer containing a
1.106203 +** copy of the column affinity string of the index allocated using
1.106204 +** sqlite3DbMalloc(). Except, entries in the copy of the string associated
1.106205 +** with equality constraints that use NONE affinity are set to
1.106206 +** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
1.106207 +**
1.106208 +**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
1.106209 +**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
1.106210 +**
1.106211 +** In the example above, the index on t1(a) has TEXT affinity. But since
1.106212 +** the right hand side of the equality constraint (t2.b) has NONE affinity,
1.106213 +** no conversion should be attempted before using a t2.b value as part of
1.106214 +** a key to search the index. Hence the first byte in the returned affinity
1.106215 +** string in this example would be set to SQLITE_AFF_NONE.
1.106216 +*/
1.106217 +static int codeAllEqualityTerms(
1.106218 +  Parse *pParse,        /* Parsing context */
1.106219 +  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
1.106220 +  WhereClause *pWC,     /* The WHERE clause */
1.106221 +  Bitmask notReady,     /* Which parts of FROM have not yet been coded */
1.106222 +  int nExtraReg,        /* Number of extra registers to allocate */
1.106223 +  char **pzAff          /* OUT: Set to point to affinity string */
1.106224 +){
1.106225 +  int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
1.106226 +  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
1.106227 +  Index *pIdx;                  /* The index being used for this loop */
1.106228 +  int iCur = pLevel->iTabCur;   /* The cursor of the table */
1.106229 +  WhereTerm *pTerm;             /* A single constraint term */
1.106230 +  int j;                        /* Loop counter */
1.106231 +  int regBase;                  /* Base register */
1.106232 +  int nReg;                     /* Number of registers to allocate */
1.106233 +  char *zAff;                   /* Affinity string to return */
1.106234 +
1.106235 +  /* This module is only called on query plans that use an index. */
1.106236 +  assert( pLevel->plan.wsFlags & WHERE_INDEXED );
1.106237 +  pIdx = pLevel->plan.u.pIdx;
1.106238 +
1.106239 +  /* Figure out how many memory cells we will need then allocate them.
1.106240 +  */
1.106241 +  regBase = pParse->nMem + 1;
1.106242 +  nReg = pLevel->plan.nEq + nExtraReg;
1.106243 +  pParse->nMem += nReg;
1.106244 +
1.106245 +  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
1.106246 +  if( !zAff ){
1.106247 +    pParse->db->mallocFailed = 1;
1.106248 +  }
1.106249 +
1.106250 +  /* Evaluate the equality constraints
1.106251 +  */
1.106252 +  assert( pIdx->nColumn>=nEq );
1.106253 +  for(j=0; j<nEq; j++){
1.106254 +    int r1;
1.106255 +    int k = pIdx->aiColumn[j];
1.106256 +    pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
1.106257 +    if( pTerm==0 ) break;
1.106258 +    /* The following true for indices with redundant columns. 
1.106259 +    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
1.106260 +    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
1.106261 +    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
1.106262 +    r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
1.106263 +    if( r1!=regBase+j ){
1.106264 +      if( nReg==1 ){
1.106265 +        sqlite3ReleaseTempReg(pParse, regBase);
1.106266 +        regBase = r1;
1.106267 +      }else{
1.106268 +        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
1.106269 +      }
1.106270 +    }
1.106271 +    testcase( pTerm->eOperator & WO_ISNULL );
1.106272 +    testcase( pTerm->eOperator & WO_IN );
1.106273 +    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
1.106274 +      Expr *pRight = pTerm->pExpr->pRight;
1.106275 +      sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
1.106276 +      if( zAff ){
1.106277 +        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
1.106278 +          zAff[j] = SQLITE_AFF_NONE;
1.106279 +        }
1.106280 +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
1.106281 +          zAff[j] = SQLITE_AFF_NONE;
1.106282 +        }
1.106283 +      }
1.106284 +    }
1.106285 +  }
1.106286 +  *pzAff = zAff;
1.106287 +  return regBase;
1.106288 +}
1.106289 +
1.106290 +#ifndef SQLITE_OMIT_EXPLAIN
1.106291 +/*
1.106292 +** This routine is a helper for explainIndexRange() below
1.106293 +**
1.106294 +** pStr holds the text of an expression that we are building up one term
1.106295 +** at a time.  This routine adds a new term to the end of the expression.
1.106296 +** Terms are separated by AND so add the "AND" text for second and subsequent
1.106297 +** terms only.
1.106298 +*/
1.106299 +static void explainAppendTerm(
1.106300 +  StrAccum *pStr,             /* The text expression being built */
1.106301 +  int iTerm,                  /* Index of this term.  First is zero */
1.106302 +  const char *zColumn,        /* Name of the column */
1.106303 +  const char *zOp             /* Name of the operator */
1.106304 +){
1.106305 +  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
1.106306 +  sqlite3StrAccumAppend(pStr, zColumn, -1);
1.106307 +  sqlite3StrAccumAppend(pStr, zOp, 1);
1.106308 +  sqlite3StrAccumAppend(pStr, "?", 1);
1.106309 +}
1.106310 +
1.106311 +/*
1.106312 +** Argument pLevel describes a strategy for scanning table pTab. This 
1.106313 +** function returns a pointer to a string buffer containing a description
1.106314 +** of the subset of table rows scanned by the strategy in the form of an
1.106315 +** SQL expression. Or, if all rows are scanned, NULL is returned.
1.106316 +**
1.106317 +** For example, if the query:
1.106318 +**
1.106319 +**   SELECT * FROM t1 WHERE a=1 AND b>2;
1.106320 +**
1.106321 +** is run and there is an index on (a, b), then this function returns a
1.106322 +** string similar to:
1.106323 +**
1.106324 +**   "a=? AND b>?"
1.106325 +**
1.106326 +** The returned pointer points to memory obtained from sqlite3DbMalloc().
1.106327 +** It is the responsibility of the caller to free the buffer when it is
1.106328 +** no longer required.
1.106329 +*/
1.106330 +static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
1.106331 +  WherePlan *pPlan = &pLevel->plan;
1.106332 +  Index *pIndex = pPlan->u.pIdx;
1.106333 +  int nEq = pPlan->nEq;
1.106334 +  int i, j;
1.106335 +  Column *aCol = pTab->aCol;
1.106336 +  int *aiColumn = pIndex->aiColumn;
1.106337 +  StrAccum txt;
1.106338 +
1.106339 +  if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
1.106340 +    return 0;
1.106341 +  }
1.106342 +  sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
1.106343 +  txt.db = db;
1.106344 +  sqlite3StrAccumAppend(&txt, " (", 2);
1.106345 +  for(i=0; i<nEq; i++){
1.106346 +    explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
1.106347 +  }
1.106348 +
1.106349 +  j = i;
1.106350 +  if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
1.106351 +    char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
1.106352 +    explainAppendTerm(&txt, i++, z, ">");
1.106353 +  }
1.106354 +  if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
1.106355 +    char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
1.106356 +    explainAppendTerm(&txt, i, z, "<");
1.106357 +  }
1.106358 +  sqlite3StrAccumAppend(&txt, ")", 1);
1.106359 +  return sqlite3StrAccumFinish(&txt);
1.106360 +}
1.106361 +
1.106362 +/*
1.106363 +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
1.106364 +** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
1.106365 +** record is added to the output to describe the table scan strategy in 
1.106366 +** pLevel.
1.106367 +*/
1.106368 +static void explainOneScan(
1.106369 +  Parse *pParse,                  /* Parse context */
1.106370 +  SrcList *pTabList,              /* Table list this loop refers to */
1.106371 +  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
1.106372 +  int iLevel,                     /* Value for "level" column of output */
1.106373 +  int iFrom,                      /* Value for "from" column of output */
1.106374 +  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
1.106375 +){
1.106376 +  if( pParse->explain==2 ){
1.106377 +    u32 flags = pLevel->plan.wsFlags;
1.106378 +    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
1.106379 +    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
1.106380 +    sqlite3 *db = pParse->db;     /* Database handle */
1.106381 +    char *zMsg;                   /* Text to add to EQP output */
1.106382 +    sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
1.106383 +    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
1.106384 +    int isSearch;                 /* True for a SEARCH. False for SCAN. */
1.106385 +
1.106386 +    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
1.106387 +
1.106388 +    isSearch = (pLevel->plan.nEq>0)
1.106389 +             || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
1.106390 +             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
1.106391 +
1.106392 +    zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
1.106393 +    if( pItem->pSelect ){
1.106394 +      zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
1.106395 +    }else{
1.106396 +      zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
1.106397 +    }
1.106398 +
1.106399 +    if( pItem->zAlias ){
1.106400 +      zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
1.106401 +    }
1.106402 +    if( (flags & WHERE_INDEXED)!=0 ){
1.106403 +      char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
1.106404 +      zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, 
1.106405 +          ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
1.106406 +          ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
1.106407 +          ((flags & WHERE_TEMP_INDEX)?"":" "),
1.106408 +          ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
1.106409 +          zWhere
1.106410 +      );
1.106411 +      sqlite3DbFree(db, zWhere);
1.106412 +    }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
1.106413 +      zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
1.106414 +
1.106415 +      if( flags&WHERE_ROWID_EQ ){
1.106416 +        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
1.106417 +      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
1.106418 +        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
1.106419 +      }else if( flags&WHERE_BTM_LIMIT ){
1.106420 +        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
1.106421 +      }else if( flags&WHERE_TOP_LIMIT ){
1.106422 +        zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
1.106423 +      }
1.106424 +    }
1.106425 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.106426 +    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
1.106427 +      sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
1.106428 +      zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
1.106429 +                  pVtabIdx->idxNum, pVtabIdx->idxStr);
1.106430 +    }
1.106431 +#endif
1.106432 +    if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
1.106433 +      testcase( wctrlFlags & WHERE_ORDERBY_MIN );
1.106434 +      nRow = 1;
1.106435 +    }else{
1.106436 +      nRow = (sqlite3_int64)pLevel->plan.nRow;
1.106437 +    }
1.106438 +    zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
1.106439 +    sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
1.106440 +  }
1.106441 +}
1.106442 +#else
1.106443 +# define explainOneScan(u,v,w,x,y,z)
1.106444 +#endif /* SQLITE_OMIT_EXPLAIN */
1.106445 +
1.106446 +
1.106447 +/*
1.106448 +** Generate code for the start of the iLevel-th loop in the WHERE clause
1.106449 +** implementation described by pWInfo.
1.106450 +*/
1.106451 +static Bitmask codeOneLoopStart(
1.106452 +  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
1.106453 +  int iLevel,          /* Which level of pWInfo->a[] should be coded */
1.106454 +  u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
1.106455 +  Bitmask notReady     /* Which tables are currently available */
1.106456 +){
1.106457 +  int j, k;            /* Loop counters */
1.106458 +  int iCur;            /* The VDBE cursor for the table */
1.106459 +  int addrNxt;         /* Where to jump to continue with the next IN case */
1.106460 +  int omitTable;       /* True if we use the index only */
1.106461 +  int bRev;            /* True if we need to scan in reverse order */
1.106462 +  WhereLevel *pLevel;  /* The where level to be coded */
1.106463 +  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
1.106464 +  WhereTerm *pTerm;               /* A WHERE clause term */
1.106465 +  Parse *pParse;                  /* Parsing context */
1.106466 +  Vdbe *v;                        /* The prepared stmt under constructions */
1.106467 +  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
1.106468 +  int addrBrk;                    /* Jump here to break out of the loop */
1.106469 +  int addrCont;                   /* Jump here to continue with next cycle */
1.106470 +  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
1.106471 +  int iReleaseReg = 0;      /* Temp register to free before returning */
1.106472 +
1.106473 +  pParse = pWInfo->pParse;
1.106474 +  v = pParse->pVdbe;
1.106475 +  pWC = pWInfo->pWC;
1.106476 +  pLevel = &pWInfo->a[iLevel];
1.106477 +  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
1.106478 +  iCur = pTabItem->iCursor;
1.106479 +  bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
1.106480 +  omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 
1.106481 +           && (wctrlFlags & WHERE_FORCE_TABLE)==0;
1.106482 +
1.106483 +  /* Create labels for the "break" and "continue" instructions
1.106484 +  ** for the current loop.  Jump to addrBrk to break out of a loop.
1.106485 +  ** Jump to cont to go immediately to the next iteration of the
1.106486 +  ** loop.
1.106487 +  **
1.106488 +  ** When there is an IN operator, we also have a "addrNxt" label that
1.106489 +  ** means to continue with the next IN value combination.  When
1.106490 +  ** there are no IN operators in the constraints, the "addrNxt" label
1.106491 +  ** is the same as "addrBrk".
1.106492 +  */
1.106493 +  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
1.106494 +  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
1.106495 +
1.106496 +  /* If this is the right table of a LEFT OUTER JOIN, allocate and
1.106497 +  ** initialize a memory cell that records if this table matches any
1.106498 +  ** row of the left table of the join.
1.106499 +  */
1.106500 +  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
1.106501 +    pLevel->iLeftJoin = ++pParse->nMem;
1.106502 +    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
1.106503 +    VdbeComment((v, "init LEFT JOIN no-match flag"));
1.106504 +  }
1.106505 +
1.106506 +  /* Special case of a FROM clause subquery implemented as a co-routine */
1.106507 +  if( pTabItem->viaCoroutine ){
1.106508 +    int regYield = pTabItem->regReturn;
1.106509 +    sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield);
1.106510 +    pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
1.106511 +    VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
1.106512 +    sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
1.106513 +    pLevel->op = OP_Goto;
1.106514 +  }else
1.106515 +
1.106516 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.106517 +  if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
1.106518 +    /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
1.106519 +    **          to access the data.
1.106520 +    */
1.106521 +    int iReg;   /* P3 Value for OP_VFilter */
1.106522 +    sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
1.106523 +    int nConstraint = pVtabIdx->nConstraint;
1.106524 +    struct sqlite3_index_constraint_usage *aUsage =
1.106525 +                                                pVtabIdx->aConstraintUsage;
1.106526 +    const struct sqlite3_index_constraint *aConstraint =
1.106527 +                                                pVtabIdx->aConstraint;
1.106528 +
1.106529 +    sqlite3ExprCachePush(pParse);
1.106530 +    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
1.106531 +    for(j=1; j<=nConstraint; j++){
1.106532 +      for(k=0; k<nConstraint; k++){
1.106533 +        if( aUsage[k].argvIndex==j ){
1.106534 +          int iTerm = aConstraint[k].iTermOffset;
1.106535 +          sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
1.106536 +          break;
1.106537 +        }
1.106538 +      }
1.106539 +      if( k==nConstraint ) break;
1.106540 +    }
1.106541 +    sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
1.106542 +    sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
1.106543 +    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
1.106544 +                      pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
1.106545 +    pVtabIdx->needToFreeIdxStr = 0;
1.106546 +    for(j=0; j<nConstraint; j++){
1.106547 +      if( aUsage[j].omit ){
1.106548 +        int iTerm = aConstraint[j].iTermOffset;
1.106549 +        disableTerm(pLevel, &pWC->a[iTerm]);
1.106550 +      }
1.106551 +    }
1.106552 +    pLevel->op = OP_VNext;
1.106553 +    pLevel->p1 = iCur;
1.106554 +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
1.106555 +    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
1.106556 +    sqlite3ExprCachePop(pParse, 1);
1.106557 +  }else
1.106558 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.106559 +
1.106560 +  if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
1.106561 +    /* Case 1:  We can directly reference a single row using an
1.106562 +    **          equality comparison against the ROWID field.  Or
1.106563 +    **          we reference multiple rows using a "rowid IN (...)"
1.106564 +    **          construct.
1.106565 +    */
1.106566 +    iReleaseReg = sqlite3GetTempReg(pParse);
1.106567 +    pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
1.106568 +    assert( pTerm!=0 );
1.106569 +    assert( pTerm->pExpr!=0 );
1.106570 +    assert( pTerm->leftCursor==iCur );
1.106571 +    assert( omitTable==0 );
1.106572 +    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
1.106573 +    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
1.106574 +    addrNxt = pLevel->addrNxt;
1.106575 +    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
1.106576 +    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
1.106577 +    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
1.106578 +    VdbeComment((v, "pk"));
1.106579 +    pLevel->op = OP_Noop;
1.106580 +  }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
1.106581 +    /* Case 2:  We have an inequality comparison against the ROWID field.
1.106582 +    */
1.106583 +    int testOp = OP_Noop;
1.106584 +    int start;
1.106585 +    int memEndValue = 0;
1.106586 +    WhereTerm *pStart, *pEnd;
1.106587 +
1.106588 +    assert( omitTable==0 );
1.106589 +    pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
1.106590 +    pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
1.106591 +    if( bRev ){
1.106592 +      pTerm = pStart;
1.106593 +      pStart = pEnd;
1.106594 +      pEnd = pTerm;
1.106595 +    }
1.106596 +    if( pStart ){
1.106597 +      Expr *pX;             /* The expression that defines the start bound */
1.106598 +      int r1, rTemp;        /* Registers for holding the start boundary */
1.106599 +
1.106600 +      /* The following constant maps TK_xx codes into corresponding 
1.106601 +      ** seek opcodes.  It depends on a particular ordering of TK_xx
1.106602 +      */
1.106603 +      const u8 aMoveOp[] = {
1.106604 +           /* TK_GT */  OP_SeekGt,
1.106605 +           /* TK_LE */  OP_SeekLe,
1.106606 +           /* TK_LT */  OP_SeekLt,
1.106607 +           /* TK_GE */  OP_SeekGe
1.106608 +      };
1.106609 +      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
1.106610 +      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
1.106611 +      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
1.106612 +
1.106613 +      testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
1.106614 +      pX = pStart->pExpr;
1.106615 +      assert( pX!=0 );
1.106616 +      assert( pStart->leftCursor==iCur );
1.106617 +      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
1.106618 +      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
1.106619 +      VdbeComment((v, "pk"));
1.106620 +      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
1.106621 +      sqlite3ReleaseTempReg(pParse, rTemp);
1.106622 +      disableTerm(pLevel, pStart);
1.106623 +    }else{
1.106624 +      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
1.106625 +    }
1.106626 +    if( pEnd ){
1.106627 +      Expr *pX;
1.106628 +      pX = pEnd->pExpr;
1.106629 +      assert( pX!=0 );
1.106630 +      assert( pEnd->leftCursor==iCur );
1.106631 +      testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
1.106632 +      memEndValue = ++pParse->nMem;
1.106633 +      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
1.106634 +      if( pX->op==TK_LT || pX->op==TK_GT ){
1.106635 +        testOp = bRev ? OP_Le : OP_Ge;
1.106636 +      }else{
1.106637 +        testOp = bRev ? OP_Lt : OP_Gt;
1.106638 +      }
1.106639 +      disableTerm(pLevel, pEnd);
1.106640 +    }
1.106641 +    start = sqlite3VdbeCurrentAddr(v);
1.106642 +    pLevel->op = bRev ? OP_Prev : OP_Next;
1.106643 +    pLevel->p1 = iCur;
1.106644 +    pLevel->p2 = start;
1.106645 +    if( pStart==0 && pEnd==0 ){
1.106646 +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
1.106647 +    }else{
1.106648 +      assert( pLevel->p5==0 );
1.106649 +    }
1.106650 +    if( testOp!=OP_Noop ){
1.106651 +      iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
1.106652 +      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
1.106653 +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
1.106654 +      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
1.106655 +      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
1.106656 +    }
1.106657 +  }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
1.106658 +    /* Case 3: A scan using an index.
1.106659 +    **
1.106660 +    **         The WHERE clause may contain zero or more equality 
1.106661 +    **         terms ("==" or "IN" operators) that refer to the N
1.106662 +    **         left-most columns of the index. It may also contain
1.106663 +    **         inequality constraints (>, <, >= or <=) on the indexed
1.106664 +    **         column that immediately follows the N equalities. Only 
1.106665 +    **         the right-most column can be an inequality - the rest must
1.106666 +    **         use the "==" and "IN" operators. For example, if the 
1.106667 +    **         index is on (x,y,z), then the following clauses are all 
1.106668 +    **         optimized:
1.106669 +    **
1.106670 +    **            x=5
1.106671 +    **            x=5 AND y=10
1.106672 +    **            x=5 AND y<10
1.106673 +    **            x=5 AND y>5 AND y<10
1.106674 +    **            x=5 AND y=5 AND z<=10
1.106675 +    **
1.106676 +    **         The z<10 term of the following cannot be used, only
1.106677 +    **         the x=5 term:
1.106678 +    **
1.106679 +    **            x=5 AND z<10
1.106680 +    **
1.106681 +    **         N may be zero if there are inequality constraints.
1.106682 +    **         If there are no inequality constraints, then N is at
1.106683 +    **         least one.
1.106684 +    **
1.106685 +    **         This case is also used when there are no WHERE clause
1.106686 +    **         constraints but an index is selected anyway, in order
1.106687 +    **         to force the output order to conform to an ORDER BY.
1.106688 +    */  
1.106689 +    static const u8 aStartOp[] = {
1.106690 +      0,
1.106691 +      0,
1.106692 +      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
1.106693 +      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
1.106694 +      OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
1.106695 +      OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
1.106696 +      OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
1.106697 +      OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
1.106698 +    };
1.106699 +    static const u8 aEndOp[] = {
1.106700 +      OP_Noop,             /* 0: (!end_constraints) */
1.106701 +      OP_IdxGE,            /* 1: (end_constraints && !bRev) */
1.106702 +      OP_IdxLT             /* 2: (end_constraints && bRev) */
1.106703 +    };
1.106704 +    int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
1.106705 +    int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
1.106706 +    int regBase;                 /* Base register holding constraint values */
1.106707 +    int r1;                      /* Temp register */
1.106708 +    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
1.106709 +    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
1.106710 +    int startEq;                 /* True if range start uses ==, >= or <= */
1.106711 +    int endEq;                   /* True if range end uses ==, >= or <= */
1.106712 +    int start_constraints;       /* Start of range is constrained */
1.106713 +    int nConstraint;             /* Number of constraint terms */
1.106714 +    Index *pIdx;                 /* The index we will be using */
1.106715 +    int iIdxCur;                 /* The VDBE cursor for the index */
1.106716 +    int nExtraReg = 0;           /* Number of extra registers needed */
1.106717 +    int op;                      /* Instruction opcode */
1.106718 +    char *zStartAff;             /* Affinity for start of range constraint */
1.106719 +    char *zEndAff;               /* Affinity for end of range constraint */
1.106720 +
1.106721 +    pIdx = pLevel->plan.u.pIdx;
1.106722 +    iIdxCur = pLevel->iIdxCur;
1.106723 +    k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
1.106724 +
1.106725 +    /* If this loop satisfies a sort order (pOrderBy) request that 
1.106726 +    ** was passed to this function to implement a "SELECT min(x) ..." 
1.106727 +    ** query, then the caller will only allow the loop to run for
1.106728 +    ** a single iteration. This means that the first row returned
1.106729 +    ** should not have a NULL value stored in 'x'. If column 'x' is
1.106730 +    ** the first one after the nEq equality constraints in the index,
1.106731 +    ** this requires some special handling.
1.106732 +    */
1.106733 +    if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
1.106734 +     && (pLevel->plan.wsFlags&WHERE_ORDERED)
1.106735 +     && (pIdx->nColumn>nEq)
1.106736 +    ){
1.106737 +      /* assert( pOrderBy->nExpr==1 ); */
1.106738 +      /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
1.106739 +      isMinQuery = 1;
1.106740 +      nExtraReg = 1;
1.106741 +    }
1.106742 +
1.106743 +    /* Find any inequality constraint terms for the start and end 
1.106744 +    ** of the range. 
1.106745 +    */
1.106746 +    if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
1.106747 +      pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
1.106748 +      nExtraReg = 1;
1.106749 +    }
1.106750 +    if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
1.106751 +      pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
1.106752 +      nExtraReg = 1;
1.106753 +    }
1.106754 +
1.106755 +    /* Generate code to evaluate all constraint terms using == or IN
1.106756 +    ** and store the values of those terms in an array of registers
1.106757 +    ** starting at regBase.
1.106758 +    */
1.106759 +    regBase = codeAllEqualityTerms(
1.106760 +        pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
1.106761 +    );
1.106762 +    zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
1.106763 +    addrNxt = pLevel->addrNxt;
1.106764 +
1.106765 +    /* If we are doing a reverse order scan on an ascending index, or
1.106766 +    ** a forward order scan on a descending index, interchange the 
1.106767 +    ** start and end terms (pRangeStart and pRangeEnd).
1.106768 +    */
1.106769 +    if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
1.106770 +     || (bRev && pIdx->nColumn==nEq)
1.106771 +    ){
1.106772 +      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
1.106773 +    }
1.106774 +
1.106775 +    testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
1.106776 +    testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
1.106777 +    testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
1.106778 +    testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
1.106779 +    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
1.106780 +    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
1.106781 +    start_constraints = pRangeStart || nEq>0;
1.106782 +
1.106783 +    /* Seek the index cursor to the start of the range. */
1.106784 +    nConstraint = nEq;
1.106785 +    if( pRangeStart ){
1.106786 +      Expr *pRight = pRangeStart->pExpr->pRight;
1.106787 +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
1.106788 +      if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
1.106789 +        sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
1.106790 +      }
1.106791 +      if( zStartAff ){
1.106792 +        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
1.106793 +          /* Since the comparison is to be performed with no conversions
1.106794 +          ** applied to the operands, set the affinity to apply to pRight to 
1.106795 +          ** SQLITE_AFF_NONE.  */
1.106796 +          zStartAff[nEq] = SQLITE_AFF_NONE;
1.106797 +        }
1.106798 +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
1.106799 +          zStartAff[nEq] = SQLITE_AFF_NONE;
1.106800 +        }
1.106801 +      }  
1.106802 +      nConstraint++;
1.106803 +      testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
1.106804 +    }else if( isMinQuery ){
1.106805 +      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
1.106806 +      nConstraint++;
1.106807 +      startEq = 0;
1.106808 +      start_constraints = 1;
1.106809 +    }
1.106810 +    codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
1.106811 +    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
1.106812 +    assert( op!=0 );
1.106813 +    testcase( op==OP_Rewind );
1.106814 +    testcase( op==OP_Last );
1.106815 +    testcase( op==OP_SeekGt );
1.106816 +    testcase( op==OP_SeekGe );
1.106817 +    testcase( op==OP_SeekLe );
1.106818 +    testcase( op==OP_SeekLt );
1.106819 +    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
1.106820 +
1.106821 +    /* Load the value for the inequality constraint at the end of the
1.106822 +    ** range (if any).
1.106823 +    */
1.106824 +    nConstraint = nEq;
1.106825 +    if( pRangeEnd ){
1.106826 +      Expr *pRight = pRangeEnd->pExpr->pRight;
1.106827 +      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
1.106828 +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
1.106829 +      if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
1.106830 +        sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
1.106831 +      }
1.106832 +      if( zEndAff ){
1.106833 +        if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
1.106834 +          /* Since the comparison is to be performed with no conversions
1.106835 +          ** applied to the operands, set the affinity to apply to pRight to 
1.106836 +          ** SQLITE_AFF_NONE.  */
1.106837 +          zEndAff[nEq] = SQLITE_AFF_NONE;
1.106838 +        }
1.106839 +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
1.106840 +          zEndAff[nEq] = SQLITE_AFF_NONE;
1.106841 +        }
1.106842 +      }  
1.106843 +      codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
1.106844 +      nConstraint++;
1.106845 +      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
1.106846 +    }
1.106847 +    sqlite3DbFree(pParse->db, zStartAff);
1.106848 +    sqlite3DbFree(pParse->db, zEndAff);
1.106849 +
1.106850 +    /* Top of the loop body */
1.106851 +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
1.106852 +
1.106853 +    /* Check if the index cursor is past the end of the range. */
1.106854 +    op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
1.106855 +    testcase( op==OP_Noop );
1.106856 +    testcase( op==OP_IdxGE );
1.106857 +    testcase( op==OP_IdxLT );
1.106858 +    if( op!=OP_Noop ){
1.106859 +      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
1.106860 +      sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
1.106861 +    }
1.106862 +
1.106863 +    /* If there are inequality constraints, check that the value
1.106864 +    ** of the table column that the inequality contrains is not NULL.
1.106865 +    ** If it is, jump to the next iteration of the loop.
1.106866 +    */
1.106867 +    r1 = sqlite3GetTempReg(pParse);
1.106868 +    testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
1.106869 +    testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
1.106870 +    if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
1.106871 +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
1.106872 +      sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
1.106873 +    }
1.106874 +    sqlite3ReleaseTempReg(pParse, r1);
1.106875 +
1.106876 +    /* Seek the table cursor, if required */
1.106877 +    disableTerm(pLevel, pRangeStart);
1.106878 +    disableTerm(pLevel, pRangeEnd);
1.106879 +    if( !omitTable ){
1.106880 +      iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
1.106881 +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
1.106882 +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
1.106883 +      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
1.106884 +    }
1.106885 +
1.106886 +    /* Record the instruction used to terminate the loop. Disable 
1.106887 +    ** WHERE clause terms made redundant by the index range scan.
1.106888 +    */
1.106889 +    if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
1.106890 +      pLevel->op = OP_Noop;
1.106891 +    }else if( bRev ){
1.106892 +      pLevel->op = OP_Prev;
1.106893 +    }else{
1.106894 +      pLevel->op = OP_Next;
1.106895 +    }
1.106896 +    pLevel->p1 = iIdxCur;
1.106897 +    if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
1.106898 +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
1.106899 +    }else{
1.106900 +      assert( pLevel->p5==0 );
1.106901 +    }
1.106902 +  }else
1.106903 +
1.106904 +#ifndef SQLITE_OMIT_OR_OPTIMIZATION
1.106905 +  if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
1.106906 +    /* Case 4:  Two or more separately indexed terms connected by OR
1.106907 +    **
1.106908 +    ** Example:
1.106909 +    **
1.106910 +    **   CREATE TABLE t1(a,b,c,d);
1.106911 +    **   CREATE INDEX i1 ON t1(a);
1.106912 +    **   CREATE INDEX i2 ON t1(b);
1.106913 +    **   CREATE INDEX i3 ON t1(c);
1.106914 +    **
1.106915 +    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
1.106916 +    **
1.106917 +    ** In the example, there are three indexed terms connected by OR.
1.106918 +    ** The top of the loop looks like this:
1.106919 +    **
1.106920 +    **          Null       1                # Zero the rowset in reg 1
1.106921 +    **
1.106922 +    ** Then, for each indexed term, the following. The arguments to
1.106923 +    ** RowSetTest are such that the rowid of the current row is inserted
1.106924 +    ** into the RowSet. If it is already present, control skips the
1.106925 +    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
1.106926 +    **
1.106927 +    **        sqlite3WhereBegin(<term>)
1.106928 +    **          RowSetTest                  # Insert rowid into rowset
1.106929 +    **          Gosub      2 A
1.106930 +    **        sqlite3WhereEnd()
1.106931 +    **
1.106932 +    ** Following the above, code to terminate the loop. Label A, the target
1.106933 +    ** of the Gosub above, jumps to the instruction right after the Goto.
1.106934 +    **
1.106935 +    **          Null       1                # Zero the rowset in reg 1
1.106936 +    **          Goto       B                # The loop is finished.
1.106937 +    **
1.106938 +    **       A: <loop body>                 # Return data, whatever.
1.106939 +    **
1.106940 +    **          Return     2                # Jump back to the Gosub
1.106941 +    **
1.106942 +    **       B: <after the loop>
1.106943 +    **
1.106944 +    */
1.106945 +    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
1.106946 +    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
1.106947 +    Index *pCov = 0;             /* Potential covering index (or NULL) */
1.106948 +    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
1.106949 +
1.106950 +    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
1.106951 +    int regRowset = 0;                        /* Register for RowSet object */
1.106952 +    int regRowid = 0;                         /* Register holding rowid */
1.106953 +    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
1.106954 +    int iRetInit;                             /* Address of regReturn init */
1.106955 +    int untestedTerms = 0;             /* Some terms not completely tested */
1.106956 +    int ii;                            /* Loop counter */
1.106957 +    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
1.106958 +   
1.106959 +    pTerm = pLevel->plan.u.pTerm;
1.106960 +    assert( pTerm!=0 );
1.106961 +    assert( pTerm->eOperator==WO_OR );
1.106962 +    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
1.106963 +    pOrWc = &pTerm->u.pOrInfo->wc;
1.106964 +    pLevel->op = OP_Return;
1.106965 +    pLevel->p1 = regReturn;
1.106966 +
1.106967 +    /* Set up a new SrcList in pOrTab containing the table being scanned
1.106968 +    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
1.106969 +    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
1.106970 +    */
1.106971 +    if( pWInfo->nLevel>1 ){
1.106972 +      int nNotReady;                 /* The number of notReady tables */
1.106973 +      struct SrcList_item *origSrc;     /* Original list of tables */
1.106974 +      nNotReady = pWInfo->nLevel - iLevel - 1;
1.106975 +      pOrTab = sqlite3StackAllocRaw(pParse->db,
1.106976 +                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
1.106977 +      if( pOrTab==0 ) return notReady;
1.106978 +      pOrTab->nAlloc = (i16)(nNotReady + 1);
1.106979 +      pOrTab->nSrc = pOrTab->nAlloc;
1.106980 +      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
1.106981 +      origSrc = pWInfo->pTabList->a;
1.106982 +      for(k=1; k<=nNotReady; k++){
1.106983 +        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
1.106984 +      }
1.106985 +    }else{
1.106986 +      pOrTab = pWInfo->pTabList;
1.106987 +    }
1.106988 +
1.106989 +    /* Initialize the rowset register to contain NULL. An SQL NULL is 
1.106990 +    ** equivalent to an empty rowset.
1.106991 +    **
1.106992 +    ** Also initialize regReturn to contain the address of the instruction 
1.106993 +    ** immediately following the OP_Return at the bottom of the loop. This
1.106994 +    ** is required in a few obscure LEFT JOIN cases where control jumps
1.106995 +    ** over the top of the loop into the body of it. In this case the 
1.106996 +    ** correct response for the end-of-loop code (the OP_Return) is to 
1.106997 +    ** fall through to the next instruction, just as an OP_Next does if
1.106998 +    ** called on an uninitialized cursor.
1.106999 +    */
1.107000 +    if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
1.107001 +      regRowset = ++pParse->nMem;
1.107002 +      regRowid = ++pParse->nMem;
1.107003 +      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
1.107004 +    }
1.107005 +    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
1.107006 +
1.107007 +    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
1.107008 +    ** Then for every term xN, evaluate as the subexpression: xN AND z
1.107009 +    ** That way, terms in y that are factored into the disjunction will
1.107010 +    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
1.107011 +    **
1.107012 +    ** Actually, each subexpression is converted to "xN AND w" where w is
1.107013 +    ** the "interesting" terms of z - terms that did not originate in the
1.107014 +    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
1.107015 +    ** indices.
1.107016 +    */
1.107017 +    if( pWC->nTerm>1 ){
1.107018 +      int iTerm;
1.107019 +      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
1.107020 +        Expr *pExpr = pWC->a[iTerm].pExpr;
1.107021 +        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
1.107022 +        if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue;
1.107023 +        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
1.107024 +        pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
1.107025 +        pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr);
1.107026 +      }
1.107027 +      if( pAndExpr ){
1.107028 +        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
1.107029 +      }
1.107030 +    }
1.107031 +
1.107032 +    for(ii=0; ii<pOrWc->nTerm; ii++){
1.107033 +      WhereTerm *pOrTerm = &pOrWc->a[ii];
1.107034 +      if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
1.107035 +        WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
1.107036 +        Expr *pOrExpr = pOrTerm->pExpr;
1.107037 +        if( pAndExpr ){
1.107038 +          pAndExpr->pLeft = pOrExpr;
1.107039 +          pOrExpr = pAndExpr;
1.107040 +        }
1.107041 +        /* Loop through table entries that match term pOrTerm. */
1.107042 +        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
1.107043 +                        WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
1.107044 +                        WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
1.107045 +        assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
1.107046 +        if( pSubWInfo ){
1.107047 +          WhereLevel *pLvl;
1.107048 +          explainOneScan(
1.107049 +              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
1.107050 +          );
1.107051 +          if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
1.107052 +            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
1.107053 +            int r;
1.107054 +            r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
1.107055 +                                         regRowid, 0);
1.107056 +            sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
1.107057 +                                 sqlite3VdbeCurrentAddr(v)+2, r, iSet);
1.107058 +          }
1.107059 +          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
1.107060 +
1.107061 +          /* The pSubWInfo->untestedTerms flag means that this OR term
1.107062 +          ** contained one or more AND term from a notReady table.  The
1.107063 +          ** terms from the notReady table could not be tested and will
1.107064 +          ** need to be tested later.
1.107065 +          */
1.107066 +          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
1.107067 +
1.107068 +          /* If all of the OR-connected terms are optimized using the same
1.107069 +          ** index, and the index is opened using the same cursor number
1.107070 +          ** by each call to sqlite3WhereBegin() made by this loop, it may
1.107071 +          ** be possible to use that index as a covering index.
1.107072 +          **
1.107073 +          ** If the call to sqlite3WhereBegin() above resulted in a scan that
1.107074 +          ** uses an index, and this is either the first OR-connected term
1.107075 +          ** processed or the index is the same as that used by all previous
1.107076 +          ** terms, set pCov to the candidate covering index. Otherwise, set 
1.107077 +          ** pCov to NULL to indicate that no candidate covering index will 
1.107078 +          ** be available.
1.107079 +          */
1.107080 +          pLvl = &pSubWInfo->a[0];
1.107081 +          if( (pLvl->plan.wsFlags & WHERE_INDEXED)!=0
1.107082 +           && (pLvl->plan.wsFlags & WHERE_TEMP_INDEX)==0
1.107083 +           && (ii==0 || pLvl->plan.u.pIdx==pCov)
1.107084 +          ){
1.107085 +            assert( pLvl->iIdxCur==iCovCur );
1.107086 +            pCov = pLvl->plan.u.pIdx;
1.107087 +          }else{
1.107088 +            pCov = 0;
1.107089 +          }
1.107090 +
1.107091 +          /* Finish the loop through table entries that match term pOrTerm. */
1.107092 +          sqlite3WhereEnd(pSubWInfo);
1.107093 +        }
1.107094 +      }
1.107095 +    }
1.107096 +    pLevel->u.pCovidx = pCov;
1.107097 +    if( pCov ) pLevel->iIdxCur = iCovCur;
1.107098 +    if( pAndExpr ){
1.107099 +      pAndExpr->pLeft = 0;
1.107100 +      sqlite3ExprDelete(pParse->db, pAndExpr);
1.107101 +    }
1.107102 +    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
1.107103 +    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
1.107104 +    sqlite3VdbeResolveLabel(v, iLoopBody);
1.107105 +
1.107106 +    if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
1.107107 +    if( !untestedTerms ) disableTerm(pLevel, pTerm);
1.107108 +  }else
1.107109 +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
1.107110 +
1.107111 +  {
1.107112 +    /* Case 5:  There is no usable index.  We must do a complete
1.107113 +    **          scan of the entire table.
1.107114 +    */
1.107115 +    static const u8 aStep[] = { OP_Next, OP_Prev };
1.107116 +    static const u8 aStart[] = { OP_Rewind, OP_Last };
1.107117 +    assert( bRev==0 || bRev==1 );
1.107118 +    assert( omitTable==0 );
1.107119 +    pLevel->op = aStep[bRev];
1.107120 +    pLevel->p1 = iCur;
1.107121 +    pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
1.107122 +    pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
1.107123 +  }
1.107124 +  notReady &= ~getMask(pWC->pMaskSet, iCur);
1.107125 +
1.107126 +  /* Insert code to test every subexpression that can be completely
1.107127 +  ** computed using the current set of tables.
1.107128 +  **
1.107129 +  ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
1.107130 +  ** the use of indices become tests that are evaluated against each row of
1.107131 +  ** the relevant input tables.
1.107132 +  */
1.107133 +  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
1.107134 +    Expr *pE;
1.107135 +    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
1.107136 +    testcase( pTerm->wtFlags & TERM_CODED );
1.107137 +    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
1.107138 +    if( (pTerm->prereqAll & notReady)!=0 ){
1.107139 +      testcase( pWInfo->untestedTerms==0
1.107140 +               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
1.107141 +      pWInfo->untestedTerms = 1;
1.107142 +      continue;
1.107143 +    }
1.107144 +    pE = pTerm->pExpr;
1.107145 +    assert( pE!=0 );
1.107146 +    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
1.107147 +      continue;
1.107148 +    }
1.107149 +    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
1.107150 +    pTerm->wtFlags |= TERM_CODED;
1.107151 +  }
1.107152 +
1.107153 +  /* For a LEFT OUTER JOIN, generate code that will record the fact that
1.107154 +  ** at least one row of the right table has matched the left table.  
1.107155 +  */
1.107156 +  if( pLevel->iLeftJoin ){
1.107157 +    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
1.107158 +    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
1.107159 +    VdbeComment((v, "record LEFT JOIN hit"));
1.107160 +    sqlite3ExprCacheClear(pParse);
1.107161 +    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
1.107162 +      testcase( pTerm->wtFlags & TERM_VIRTUAL );  /* IMP: R-30575-11662 */
1.107163 +      testcase( pTerm->wtFlags & TERM_CODED );
1.107164 +      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
1.107165 +      if( (pTerm->prereqAll & notReady)!=0 ){
1.107166 +        assert( pWInfo->untestedTerms );
1.107167 +        continue;
1.107168 +      }
1.107169 +      assert( pTerm->pExpr );
1.107170 +      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
1.107171 +      pTerm->wtFlags |= TERM_CODED;
1.107172 +    }
1.107173 +  }
1.107174 +  sqlite3ReleaseTempReg(pParse, iReleaseReg);
1.107175 +
1.107176 +  return notReady;
1.107177 +}
1.107178 +
1.107179 +#if defined(SQLITE_TEST)
1.107180 +/*
1.107181 +** The following variable holds a text description of query plan generated
1.107182 +** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
1.107183 +** overwrites the previous.  This information is used for testing and
1.107184 +** analysis only.
1.107185 +*/
1.107186 +SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
1.107187 +static int nQPlan = 0;              /* Next free slow in _query_plan[] */
1.107188 +
1.107189 +#endif /* SQLITE_TEST */
1.107190 +
1.107191 +
1.107192 +/*
1.107193 +** Free a WhereInfo structure
1.107194 +*/
1.107195 +static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
1.107196 +  if( ALWAYS(pWInfo) ){
1.107197 +    int i;
1.107198 +    for(i=0; i<pWInfo->nLevel; i++){
1.107199 +      sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
1.107200 +      if( pInfo ){
1.107201 +        /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
1.107202 +        if( pInfo->needToFreeIdxStr ){
1.107203 +          sqlite3_free(pInfo->idxStr);
1.107204 +        }
1.107205 +        sqlite3DbFree(db, pInfo);
1.107206 +      }
1.107207 +      if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
1.107208 +        Index *pIdx = pWInfo->a[i].plan.u.pIdx;
1.107209 +        if( pIdx ){
1.107210 +          sqlite3DbFree(db, pIdx->zColAff);
1.107211 +          sqlite3DbFree(db, pIdx);
1.107212 +        }
1.107213 +      }
1.107214 +    }
1.107215 +    whereClauseClear(pWInfo->pWC);
1.107216 +    sqlite3DbFree(db, pWInfo);
1.107217 +  }
1.107218 +}
1.107219 +
1.107220 +
1.107221 +/*
1.107222 +** Generate the beginning of the loop used for WHERE clause processing.
1.107223 +** The return value is a pointer to an opaque structure that contains
1.107224 +** information needed to terminate the loop.  Later, the calling routine
1.107225 +** should invoke sqlite3WhereEnd() with the return value of this function
1.107226 +** in order to complete the WHERE clause processing.
1.107227 +**
1.107228 +** If an error occurs, this routine returns NULL.
1.107229 +**
1.107230 +** The basic idea is to do a nested loop, one loop for each table in
1.107231 +** the FROM clause of a select.  (INSERT and UPDATE statements are the
1.107232 +** same as a SELECT with only a single table in the FROM clause.)  For
1.107233 +** example, if the SQL is this:
1.107234 +**
1.107235 +**       SELECT * FROM t1, t2, t3 WHERE ...;
1.107236 +**
1.107237 +** Then the code generated is conceptually like the following:
1.107238 +**
1.107239 +**      foreach row1 in t1 do       \    Code generated
1.107240 +**        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
1.107241 +**          foreach row3 in t3 do   /
1.107242 +**            ...
1.107243 +**          end                     \    Code generated
1.107244 +**        end                        |-- by sqlite3WhereEnd()
1.107245 +**      end                         /
1.107246 +**
1.107247 +** Note that the loops might not be nested in the order in which they
1.107248 +** appear in the FROM clause if a different order is better able to make
1.107249 +** use of indices.  Note also that when the IN operator appears in
1.107250 +** the WHERE clause, it might result in additional nested loops for
1.107251 +** scanning through all values on the right-hand side of the IN.
1.107252 +**
1.107253 +** There are Btree cursors associated with each table.  t1 uses cursor
1.107254 +** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
1.107255 +** And so forth.  This routine generates code to open those VDBE cursors
1.107256 +** and sqlite3WhereEnd() generates the code to close them.
1.107257 +**
1.107258 +** The code that sqlite3WhereBegin() generates leaves the cursors named
1.107259 +** in pTabList pointing at their appropriate entries.  The [...] code
1.107260 +** can use OP_Column and OP_Rowid opcodes on these cursors to extract
1.107261 +** data from the various tables of the loop.
1.107262 +**
1.107263 +** If the WHERE clause is empty, the foreach loops must each scan their
1.107264 +** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
1.107265 +** the tables have indices and there are terms in the WHERE clause that
1.107266 +** refer to those indices, a complete table scan can be avoided and the
1.107267 +** code will run much faster.  Most of the work of this routine is checking
1.107268 +** to see if there are indices that can be used to speed up the loop.
1.107269 +**
1.107270 +** Terms of the WHERE clause are also used to limit which rows actually
1.107271 +** make it to the "..." in the middle of the loop.  After each "foreach",
1.107272 +** terms of the WHERE clause that use only terms in that loop and outer
1.107273 +** loops are evaluated and if false a jump is made around all subsequent
1.107274 +** inner loops (or around the "..." if the test occurs within the inner-
1.107275 +** most loop)
1.107276 +**
1.107277 +** OUTER JOINS
1.107278 +**
1.107279 +** An outer join of tables t1 and t2 is conceptally coded as follows:
1.107280 +**
1.107281 +**    foreach row1 in t1 do
1.107282 +**      flag = 0
1.107283 +**      foreach row2 in t2 do
1.107284 +**        start:
1.107285 +**          ...
1.107286 +**          flag = 1
1.107287 +**      end
1.107288 +**      if flag==0 then
1.107289 +**        move the row2 cursor to a null row
1.107290 +**        goto start
1.107291 +**      fi
1.107292 +**    end
1.107293 +**
1.107294 +** ORDER BY CLAUSE PROCESSING
1.107295 +**
1.107296 +** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
1.107297 +** if there is one.  If there is no ORDER BY clause or if this routine
1.107298 +** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
1.107299 +**
1.107300 +** If an index can be used so that the natural output order of the table
1.107301 +** scan is correct for the ORDER BY clause, then that index is used and
1.107302 +** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr.  This
1.107303 +** is an optimization that prevents an unnecessary sort of the result set
1.107304 +** if an index appropriate for the ORDER BY clause already exists.
1.107305 +**
1.107306 +** If the where clause loops cannot be arranged to provide the correct
1.107307 +** output order, then WhereInfo.nOBSat is 0.
1.107308 +*/
1.107309 +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
1.107310 +  Parse *pParse,        /* The parser context */
1.107311 +  SrcList *pTabList,    /* A list of all tables to be scanned */
1.107312 +  Expr *pWhere,         /* The WHERE clause */
1.107313 +  ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
1.107314 +  ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
1.107315 +  u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
1.107316 +  int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
1.107317 +){
1.107318 +  int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
1.107319 +  int nTabList;              /* Number of elements in pTabList */
1.107320 +  WhereInfo *pWInfo;         /* Will become the return value of this function */
1.107321 +  Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
1.107322 +  Bitmask notReady;          /* Cursors that are not yet positioned */
1.107323 +  WhereBestIdx sWBI;         /* Best index search context */
1.107324 +  WhereMaskSet *pMaskSet;    /* The expression mask set */
1.107325 +  WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
1.107326 +  int iFrom;                 /* First unused FROM clause element */
1.107327 +  int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
1.107328 +  int ii;                    /* Loop counter */
1.107329 +  sqlite3 *db;               /* Database connection */
1.107330 +
1.107331 +
1.107332 +  /* Variable initialization */
1.107333 +  memset(&sWBI, 0, sizeof(sWBI));
1.107334 +  sWBI.pParse = pParse;
1.107335 +
1.107336 +  /* The number of tables in the FROM clause is limited by the number of
1.107337 +  ** bits in a Bitmask 
1.107338 +  */
1.107339 +  testcase( pTabList->nSrc==BMS );
1.107340 +  if( pTabList->nSrc>BMS ){
1.107341 +    sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
1.107342 +    return 0;
1.107343 +  }
1.107344 +
1.107345 +  /* This function normally generates a nested loop for all tables in 
1.107346 +  ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
1.107347 +  ** only generate code for the first table in pTabList and assume that
1.107348 +  ** any cursors associated with subsequent tables are uninitialized.
1.107349 +  */
1.107350 +  nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
1.107351 +
1.107352 +  /* Allocate and initialize the WhereInfo structure that will become the
1.107353 +  ** return value. A single allocation is used to store the WhereInfo
1.107354 +  ** struct, the contents of WhereInfo.a[], the WhereClause structure
1.107355 +  ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
1.107356 +  ** field (type Bitmask) it must be aligned on an 8-byte boundary on
1.107357 +  ** some architectures. Hence the ROUND8() below.
1.107358 +  */
1.107359 +  db = pParse->db;
1.107360 +  nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
1.107361 +  pWInfo = sqlite3DbMallocZero(db, 
1.107362 +      nByteWInfo + 
1.107363 +      sizeof(WhereClause) +
1.107364 +      sizeof(WhereMaskSet)
1.107365 +  );
1.107366 +  if( db->mallocFailed ){
1.107367 +    sqlite3DbFree(db, pWInfo);
1.107368 +    pWInfo = 0;
1.107369 +    goto whereBeginError;
1.107370 +  }
1.107371 +  pWInfo->nLevel = nTabList;
1.107372 +  pWInfo->pParse = pParse;
1.107373 +  pWInfo->pTabList = pTabList;
1.107374 +  pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
1.107375 +  pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
1.107376 +  pWInfo->wctrlFlags = wctrlFlags;
1.107377 +  pWInfo->savedNQueryLoop = pParse->nQueryLoop;
1.107378 +  pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
1.107379 +  sWBI.aLevel = pWInfo->a;
1.107380 +
1.107381 +  /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
1.107382 +  ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
1.107383 +  if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
1.107384 +
1.107385 +  /* Split the WHERE clause into separate subexpressions where each
1.107386 +  ** subexpression is separated by an AND operator.
1.107387 +  */
1.107388 +  initMaskSet(pMaskSet);
1.107389 +  whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
1.107390 +  sqlite3ExprCodeConstants(pParse, pWhere);
1.107391 +  whereSplit(sWBI.pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
1.107392 +    
1.107393 +  /* Special case: a WHERE clause that is constant.  Evaluate the
1.107394 +  ** expression and either jump over all of the code or fall thru.
1.107395 +  */
1.107396 +  if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
1.107397 +    sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
1.107398 +    pWhere = 0;
1.107399 +  }
1.107400 +
1.107401 +  /* Assign a bit from the bitmask to every term in the FROM clause.
1.107402 +  **
1.107403 +  ** When assigning bitmask values to FROM clause cursors, it must be
1.107404 +  ** the case that if X is the bitmask for the N-th FROM clause term then
1.107405 +  ** the bitmask for all FROM clause terms to the left of the N-th term
1.107406 +  ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
1.107407 +  ** its Expr.iRightJoinTable value to find the bitmask of the right table
1.107408 +  ** of the join.  Subtracting one from the right table bitmask gives a
1.107409 +  ** bitmask for all tables to the left of the join.  Knowing the bitmask
1.107410 +  ** for all tables to the left of a left join is important.  Ticket #3015.
1.107411 +  **
1.107412 +  ** Configure the WhereClause.vmask variable so that bits that correspond
1.107413 +  ** to virtual table cursors are set. This is used to selectively disable 
1.107414 +  ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful 
1.107415 +  ** with virtual tables.
1.107416 +  **
1.107417 +  ** Note that bitmasks are created for all pTabList->nSrc tables in
1.107418 +  ** pTabList, not just the first nTabList tables.  nTabList is normally
1.107419 +  ** equal to pTabList->nSrc but might be shortened to 1 if the
1.107420 +  ** WHERE_ONETABLE_ONLY flag is set.
1.107421 +  */
1.107422 +  assert( sWBI.pWC->vmask==0 && pMaskSet->n==0 );
1.107423 +  for(ii=0; ii<pTabList->nSrc; ii++){
1.107424 +    createMask(pMaskSet, pTabList->a[ii].iCursor);
1.107425 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.107426 +    if( ALWAYS(pTabList->a[ii].pTab) && IsVirtual(pTabList->a[ii].pTab) ){
1.107427 +      sWBI.pWC->vmask |= ((Bitmask)1 << ii);
1.107428 +    }
1.107429 +#endif
1.107430 +  }
1.107431 +#ifndef NDEBUG
1.107432 +  {
1.107433 +    Bitmask toTheLeft = 0;
1.107434 +    for(ii=0; ii<pTabList->nSrc; ii++){
1.107435 +      Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
1.107436 +      assert( (m-1)==toTheLeft );
1.107437 +      toTheLeft |= m;
1.107438 +    }
1.107439 +  }
1.107440 +#endif
1.107441 +
1.107442 +  /* Analyze all of the subexpressions.  Note that exprAnalyze() might
1.107443 +  ** add new virtual terms onto the end of the WHERE clause.  We do not
1.107444 +  ** want to analyze these virtual terms, so start analyzing at the end
1.107445 +  ** and work forward so that the added virtual terms are never processed.
1.107446 +  */
1.107447 +  exprAnalyzeAll(pTabList, sWBI.pWC);
1.107448 +  if( db->mallocFailed ){
1.107449 +    goto whereBeginError;
1.107450 +  }
1.107451 +
1.107452 +  /* Check if the DISTINCT qualifier, if there is one, is redundant. 
1.107453 +  ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
1.107454 +  ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
1.107455 +  */
1.107456 +  if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
1.107457 +    pDistinct = 0;
1.107458 +    pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
1.107459 +  }
1.107460 +
1.107461 +  /* Chose the best index to use for each table in the FROM clause.
1.107462 +  **
1.107463 +  ** This loop fills in the following fields:
1.107464 +  **
1.107465 +  **   pWInfo->a[].pIdx      The index to use for this level of the loop.
1.107466 +  **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
1.107467 +  **   pWInfo->a[].nEq       The number of == and IN constraints
1.107468 +  **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
1.107469 +  **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
1.107470 +  **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
1.107471 +  **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
1.107472 +  **
1.107473 +  ** This loop also figures out the nesting order of tables in the FROM
1.107474 +  ** clause.
1.107475 +  */
1.107476 +  sWBI.notValid = ~(Bitmask)0;
1.107477 +  sWBI.pOrderBy = pOrderBy;
1.107478 +  sWBI.n = nTabList;
1.107479 +  sWBI.pDistinct = pDistinct;
1.107480 +  andFlags = ~0;
1.107481 +  WHERETRACE(("*** Optimizer Start ***\n"));
1.107482 +  for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
1.107483 +    WhereCost bestPlan;         /* Most efficient plan seen so far */
1.107484 +    Index *pIdx;                /* Index for FROM table at pTabItem */
1.107485 +    int j;                      /* For looping over FROM tables */
1.107486 +    int bestJ = -1;             /* The value of j */
1.107487 +    Bitmask m;                  /* Bitmask value for j or bestJ */
1.107488 +    int isOptimal;              /* Iterator for optimal/non-optimal search */
1.107489 +    int nUnconstrained;         /* Number tables without INDEXED BY */
1.107490 +    Bitmask notIndexed;         /* Mask of tables that cannot use an index */
1.107491 +
1.107492 +    memset(&bestPlan, 0, sizeof(bestPlan));
1.107493 +    bestPlan.rCost = SQLITE_BIG_DBL;
1.107494 +    WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
1.107495 +
1.107496 +    /* Loop through the remaining entries in the FROM clause to find the
1.107497 +    ** next nested loop. The loop tests all FROM clause entries
1.107498 +    ** either once or twice. 
1.107499 +    **
1.107500 +    ** The first test is always performed if there are two or more entries
1.107501 +    ** remaining and never performed if there is only one FROM clause entry
1.107502 +    ** to choose from.  The first test looks for an "optimal" scan.  In
1.107503 +    ** this context an optimal scan is one that uses the same strategy
1.107504 +    ** for the given FROM clause entry as would be selected if the entry
1.107505 +    ** were used as the innermost nested loop.  In other words, a table
1.107506 +    ** is chosen such that the cost of running that table cannot be reduced
1.107507 +    ** by waiting for other tables to run first.  This "optimal" test works
1.107508 +    ** by first assuming that the FROM clause is on the inner loop and finding
1.107509 +    ** its query plan, then checking to see if that query plan uses any
1.107510 +    ** other FROM clause terms that are sWBI.notValid.  If no notValid terms
1.107511 +    ** are used then the "optimal" query plan works.
1.107512 +    **
1.107513 +    ** Note that the WhereCost.nRow parameter for an optimal scan might
1.107514 +    ** not be as small as it would be if the table really were the innermost
1.107515 +    ** join.  The nRow value can be reduced by WHERE clause constraints
1.107516 +    ** that do not use indices.  But this nRow reduction only happens if the
1.107517 +    ** table really is the innermost join.  
1.107518 +    **
1.107519 +    ** The second loop iteration is only performed if no optimal scan
1.107520 +    ** strategies were found by the first iteration. This second iteration
1.107521 +    ** is used to search for the lowest cost scan overall.
1.107522 +    **
1.107523 +    ** Previous versions of SQLite performed only the second iteration -
1.107524 +    ** the next outermost loop was always that with the lowest overall
1.107525 +    ** cost. However, this meant that SQLite could select the wrong plan
1.107526 +    ** for scripts such as the following:
1.107527 +    **   
1.107528 +    **   CREATE TABLE t1(a, b); 
1.107529 +    **   CREATE TABLE t2(c, d);
1.107530 +    **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
1.107531 +    **
1.107532 +    ** The best strategy is to iterate through table t1 first. However it
1.107533 +    ** is not possible to determine this with a simple greedy algorithm.
1.107534 +    ** Since the cost of a linear scan through table t2 is the same 
1.107535 +    ** as the cost of a linear scan through table t1, a simple greedy 
1.107536 +    ** algorithm may choose to use t2 for the outer loop, which is a much
1.107537 +    ** costlier approach.
1.107538 +    */
1.107539 +    nUnconstrained = 0;
1.107540 +    notIndexed = 0;
1.107541 +    for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
1.107542 +      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
1.107543 +        int doNotReorder;    /* True if this table should not be reordered */
1.107544 +  
1.107545 +        doNotReorder =  (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0;
1.107546 +        if( j!=iFrom && doNotReorder ) break;
1.107547 +        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
1.107548 +        if( (m & sWBI.notValid)==0 ){
1.107549 +          if( j==iFrom ) iFrom++;
1.107550 +          continue;
1.107551 +        }
1.107552 +        sWBI.notReady = (isOptimal ? m : sWBI.notValid);
1.107553 +        if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
1.107554 +  
1.107555 +        WHERETRACE(("   === trying table %d (%s) with isOptimal=%d ===\n",
1.107556 +                    j, sWBI.pSrc->pTab->zName, isOptimal));
1.107557 +        assert( sWBI.pSrc->pTab );
1.107558 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.107559 +        if( IsVirtual(sWBI.pSrc->pTab) ){
1.107560 +          sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
1.107561 +          bestVirtualIndex(&sWBI);
1.107562 +        }else 
1.107563 +#endif
1.107564 +        {
1.107565 +          bestBtreeIndex(&sWBI);
1.107566 +        }
1.107567 +        assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
1.107568 +
1.107569 +        /* If an INDEXED BY clause is present, then the plan must use that
1.107570 +        ** index if it uses any index at all */
1.107571 +        assert( sWBI.pSrc->pIndex==0 
1.107572 +                  || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
1.107573 +                  || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
1.107574 +
1.107575 +        if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
1.107576 +          notIndexed |= m;
1.107577 +        }
1.107578 +        if( isOptimal ){
1.107579 +          pWInfo->a[j].rOptCost = sWBI.cost.rCost;
1.107580 +        }else if( iFrom<nTabList-1 ){
1.107581 +          /* If two or more tables have nearly the same outer loop cost,
1.107582 +          ** very different inner loop (optimal) cost, we want to choose
1.107583 +          ** for the outer loop that table which benefits the least from
1.107584 +          ** being in the inner loop.  The following code scales the 
1.107585 +          ** outer loop cost estimate to accomplish that. */
1.107586 +          WHERETRACE(("   scaling cost from %.1f to %.1f\n",
1.107587 +                      sWBI.cost.rCost,
1.107588 +                      sWBI.cost.rCost/pWInfo->a[j].rOptCost));
1.107589 +          sWBI.cost.rCost /= pWInfo->a[j].rOptCost;
1.107590 +        }
1.107591 +
1.107592 +        /* Conditions under which this table becomes the best so far:
1.107593 +        **
1.107594 +        **   (1) The table must not depend on other tables that have not
1.107595 +        **       yet run.  (In other words, it must not depend on tables
1.107596 +        **       in inner loops.)
1.107597 +        **
1.107598 +        **   (2) (This rule was removed on 2012-11-09.  The scaling of the
1.107599 +        **       cost using the optimal scan cost made this rule obsolete.)
1.107600 +        **
1.107601 +        **   (3) All tables have an INDEXED BY clause or this table lacks an
1.107602 +        **       INDEXED BY clause or this table uses the specific
1.107603 +        **       index specified by its INDEXED BY clause.  This rule ensures
1.107604 +        **       that a best-so-far is always selected even if an impossible
1.107605 +        **       combination of INDEXED BY clauses are given.  The error
1.107606 +        **       will be detected and relayed back to the application later.
1.107607 +        **       The NEVER() comes about because rule (2) above prevents
1.107608 +        **       An indexable full-table-scan from reaching rule (3).
1.107609 +        **
1.107610 +        **   (4) The plan cost must be lower than prior plans, where "cost"
1.107611 +        **       is defined by the compareCost() function above. 
1.107612 +        */
1.107613 +        if( (sWBI.cost.used&sWBI.notValid)==0                    /* (1) */
1.107614 +            && (nUnconstrained==0 || sWBI.pSrc->pIndex==0        /* (3) */
1.107615 +                || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
1.107616 +            && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan))   /* (4) */
1.107617 +        ){
1.107618 +          WHERETRACE(("   === table %d (%s) is best so far\n"
1.107619 +                      "       cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
1.107620 +                      j, sWBI.pSrc->pTab->zName,
1.107621 +                      sWBI.cost.rCost, sWBI.cost.plan.nRow,
1.107622 +                      sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
1.107623 +          bestPlan = sWBI.cost;
1.107624 +          bestJ = j;
1.107625 +        }
1.107626 +        if( doNotReorder ) break;
1.107627 +      }
1.107628 +    }
1.107629 +    assert( bestJ>=0 );
1.107630 +    assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
1.107631 +    WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
1.107632 +                "    cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
1.107633 +                bestJ, pTabList->a[bestJ].pTab->zName,
1.107634 +                pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
1.107635 +                bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
1.107636 +    if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
1.107637 +      assert( pWInfo->eDistinct==0 );
1.107638 +      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
1.107639 +    }
1.107640 +    andFlags &= bestPlan.plan.wsFlags;
1.107641 +    pLevel->plan = bestPlan.plan;
1.107642 +    pLevel->iTabCur = pTabList->a[bestJ].iCursor;
1.107643 +    testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
1.107644 +    testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
1.107645 +    if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
1.107646 +      if( (wctrlFlags & WHERE_ONETABLE_ONLY) 
1.107647 +       && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0 
1.107648 +      ){
1.107649 +        pLevel->iIdxCur = iIdxCur;
1.107650 +      }else{
1.107651 +        pLevel->iIdxCur = pParse->nTab++;
1.107652 +      }
1.107653 +    }else{
1.107654 +      pLevel->iIdxCur = -1;
1.107655 +    }
1.107656 +    sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
1.107657 +    pLevel->iFrom = (u8)bestJ;
1.107658 +    if( bestPlan.plan.nRow>=(double)1 ){
1.107659 +      pParse->nQueryLoop *= bestPlan.plan.nRow;
1.107660 +    }
1.107661 +
1.107662 +    /* Check that if the table scanned by this loop iteration had an
1.107663 +    ** INDEXED BY clause attached to it, that the named index is being
1.107664 +    ** used for the scan. If not, then query compilation has failed.
1.107665 +    ** Return an error.
1.107666 +    */
1.107667 +    pIdx = pTabList->a[bestJ].pIndex;
1.107668 +    if( pIdx ){
1.107669 +      if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
1.107670 +        sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
1.107671 +        goto whereBeginError;
1.107672 +      }else{
1.107673 +        /* If an INDEXED BY clause is used, the bestIndex() function is
1.107674 +        ** guaranteed to find the index specified in the INDEXED BY clause
1.107675 +        ** if it find an index at all. */
1.107676 +        assert( bestPlan.plan.u.pIdx==pIdx );
1.107677 +      }
1.107678 +    }
1.107679 +  }
1.107680 +  WHERETRACE(("*** Optimizer Finished ***\n"));
1.107681 +  if( pParse->nErr || db->mallocFailed ){
1.107682 +    goto whereBeginError;
1.107683 +  }
1.107684 +  if( nTabList ){
1.107685 +    pLevel--;
1.107686 +    pWInfo->nOBSat = pLevel->plan.nOBSat;
1.107687 +  }else{
1.107688 +    pWInfo->nOBSat = 0;
1.107689 +  }
1.107690 +
1.107691 +  /* If the total query only selects a single row, then the ORDER BY
1.107692 +  ** clause is irrelevant.
1.107693 +  */
1.107694 +  if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
1.107695 +    assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
1.107696 +    pWInfo->nOBSat = pOrderBy->nExpr;
1.107697 +  }
1.107698 +
1.107699 +  /* If the caller is an UPDATE or DELETE statement that is requesting
1.107700 +  ** to use a one-pass algorithm, determine if this is appropriate.
1.107701 +  ** The one-pass algorithm only works if the WHERE clause constraints
1.107702 +  ** the statement to update a single row.
1.107703 +  */
1.107704 +  assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
1.107705 +  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
1.107706 +    pWInfo->okOnePass = 1;
1.107707 +    pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
1.107708 +  }
1.107709 +
1.107710 +  /* Open all tables in the pTabList and any indices selected for
1.107711 +  ** searching those tables.
1.107712 +  */
1.107713 +  sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
1.107714 +  notReady = ~(Bitmask)0;
1.107715 +  pWInfo->nRowOut = (double)1;
1.107716 +  for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
1.107717 +    Table *pTab;     /* Table to open */
1.107718 +    int iDb;         /* Index of database containing table/index */
1.107719 +    struct SrcList_item *pTabItem;
1.107720 +
1.107721 +    pTabItem = &pTabList->a[pLevel->iFrom];
1.107722 +    pTab = pTabItem->pTab;
1.107723 +    pWInfo->nRowOut *= pLevel->plan.nRow;
1.107724 +    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1.107725 +    if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
1.107726 +      /* Do nothing */
1.107727 +    }else
1.107728 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.107729 +    if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
1.107730 +      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
1.107731 +      int iCur = pTabItem->iCursor;
1.107732 +      sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
1.107733 +    }else if( IsVirtual(pTab) ){
1.107734 +      /* noop */
1.107735 +    }else
1.107736 +#endif
1.107737 +    if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
1.107738 +         && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
1.107739 +      int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
1.107740 +      sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
1.107741 +      testcase( pTab->nCol==BMS-1 );
1.107742 +      testcase( pTab->nCol==BMS );
1.107743 +      if( !pWInfo->okOnePass && pTab->nCol<BMS ){
1.107744 +        Bitmask b = pTabItem->colUsed;
1.107745 +        int n = 0;
1.107746 +        for(; b; b=b>>1, n++){}
1.107747 +        sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
1.107748 +                            SQLITE_INT_TO_PTR(n), P4_INT32);
1.107749 +        assert( n<=pTab->nCol );
1.107750 +      }
1.107751 +    }else{
1.107752 +      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
1.107753 +    }
1.107754 +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
1.107755 +    if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
1.107756 +      constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
1.107757 +    }else
1.107758 +#endif
1.107759 +    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
1.107760 +      Index *pIx = pLevel->plan.u.pIdx;
1.107761 +      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
1.107762 +      int iIndexCur = pLevel->iIdxCur;
1.107763 +      assert( pIx->pSchema==pTab->pSchema );
1.107764 +      assert( iIndexCur>=0 );
1.107765 +      sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
1.107766 +                        (char*)pKey, P4_KEYINFO_HANDOFF);
1.107767 +      VdbeComment((v, "%s", pIx->zName));
1.107768 +    }
1.107769 +    sqlite3CodeVerifySchema(pParse, iDb);
1.107770 +    notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
1.107771 +  }
1.107772 +  pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
1.107773 +  if( db->mallocFailed ) goto whereBeginError;
1.107774 +
1.107775 +  /* Generate the code to do the search.  Each iteration of the for
1.107776 +  ** loop below generates code for a single nested loop of the VM
1.107777 +  ** program.
1.107778 +  */
1.107779 +  notReady = ~(Bitmask)0;
1.107780 +  for(ii=0; ii<nTabList; ii++){
1.107781 +    pLevel = &pWInfo->a[ii];
1.107782 +    explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
1.107783 +    notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
1.107784 +    pWInfo->iContinue = pLevel->addrCont;
1.107785 +  }
1.107786 +
1.107787 +#ifdef SQLITE_TEST  /* For testing and debugging use only */
1.107788 +  /* Record in the query plan information about the current table
1.107789 +  ** and the index used to access it (if any).  If the table itself
1.107790 +  ** is not used, its name is just '{}'.  If no index is used
1.107791 +  ** the index is listed as "{}".  If the primary key is used the
1.107792 +  ** index name is '*'.
1.107793 +  */
1.107794 +  for(ii=0; ii<nTabList; ii++){
1.107795 +    char *z;
1.107796 +    int n;
1.107797 +    int w;
1.107798 +    struct SrcList_item *pTabItem;
1.107799 +
1.107800 +    pLevel = &pWInfo->a[ii];
1.107801 +    w = pLevel->plan.wsFlags;
1.107802 +    pTabItem = &pTabList->a[pLevel->iFrom];
1.107803 +    z = pTabItem->zAlias;
1.107804 +    if( z==0 ) z = pTabItem->pTab->zName;
1.107805 +    n = sqlite3Strlen30(z);
1.107806 +    if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
1.107807 +      if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
1.107808 +        memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
1.107809 +        nQPlan += 2;
1.107810 +      }else{
1.107811 +        memcpy(&sqlite3_query_plan[nQPlan], z, n);
1.107812 +        nQPlan += n;
1.107813 +      }
1.107814 +      sqlite3_query_plan[nQPlan++] = ' ';
1.107815 +    }
1.107816 +    testcase( w & WHERE_ROWID_EQ );
1.107817 +    testcase( w & WHERE_ROWID_RANGE );
1.107818 +    if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
1.107819 +      memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
1.107820 +      nQPlan += 2;
1.107821 +    }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
1.107822 +      n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
1.107823 +      if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
1.107824 +        memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
1.107825 +        nQPlan += n;
1.107826 +        sqlite3_query_plan[nQPlan++] = ' ';
1.107827 +      }
1.107828 +    }else{
1.107829 +      memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
1.107830 +      nQPlan += 3;
1.107831 +    }
1.107832 +  }
1.107833 +  while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
1.107834 +    sqlite3_query_plan[--nQPlan] = 0;
1.107835 +  }
1.107836 +  sqlite3_query_plan[nQPlan] = 0;
1.107837 +  nQPlan = 0;
1.107838 +#endif /* SQLITE_TEST // Testing and debugging use only */
1.107839 +
1.107840 +  /* Record the continuation address in the WhereInfo structure.  Then
1.107841 +  ** clean up and return.
1.107842 +  */
1.107843 +  return pWInfo;
1.107844 +
1.107845 +  /* Jump here if malloc fails */
1.107846 +whereBeginError:
1.107847 +  if( pWInfo ){
1.107848 +    pParse->nQueryLoop = pWInfo->savedNQueryLoop;
1.107849 +    whereInfoFree(db, pWInfo);
1.107850 +  }
1.107851 +  return 0;
1.107852 +}
1.107853 +
1.107854 +/*
1.107855 +** Generate the end of the WHERE loop.  See comments on 
1.107856 +** sqlite3WhereBegin() for additional information.
1.107857 +*/
1.107858 +SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
1.107859 +  Parse *pParse = pWInfo->pParse;
1.107860 +  Vdbe *v = pParse->pVdbe;
1.107861 +  int i;
1.107862 +  WhereLevel *pLevel;
1.107863 +  SrcList *pTabList = pWInfo->pTabList;
1.107864 +  sqlite3 *db = pParse->db;
1.107865 +
1.107866 +  /* Generate loop termination code.
1.107867 +  */
1.107868 +  sqlite3ExprCacheClear(pParse);
1.107869 +  for(i=pWInfo->nLevel-1; i>=0; i--){
1.107870 +    pLevel = &pWInfo->a[i];
1.107871 +    sqlite3VdbeResolveLabel(v, pLevel->addrCont);
1.107872 +    if( pLevel->op!=OP_Noop ){
1.107873 +      sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
1.107874 +      sqlite3VdbeChangeP5(v, pLevel->p5);
1.107875 +    }
1.107876 +    if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
1.107877 +      struct InLoop *pIn;
1.107878 +      int j;
1.107879 +      sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
1.107880 +      for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
1.107881 +        sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
1.107882 +        sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
1.107883 +        sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
1.107884 +      }
1.107885 +      sqlite3DbFree(db, pLevel->u.in.aInLoop);
1.107886 +    }
1.107887 +    sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
1.107888 +    if( pLevel->iLeftJoin ){
1.107889 +      int addr;
1.107890 +      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
1.107891 +      assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
1.107892 +           || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
1.107893 +      if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
1.107894 +        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
1.107895 +      }
1.107896 +      if( pLevel->iIdxCur>=0 ){
1.107897 +        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
1.107898 +      }
1.107899 +      if( pLevel->op==OP_Return ){
1.107900 +        sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
1.107901 +      }else{
1.107902 +        sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
1.107903 +      }
1.107904 +      sqlite3VdbeJumpHere(v, addr);
1.107905 +    }
1.107906 +  }
1.107907 +
1.107908 +  /* The "break" point is here, just past the end of the outer loop.
1.107909 +  ** Set it.
1.107910 +  */
1.107911 +  sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
1.107912 +
1.107913 +  /* Close all of the cursors that were opened by sqlite3WhereBegin.
1.107914 +  */
1.107915 +  assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
1.107916 +  for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
1.107917 +    Index *pIdx = 0;
1.107918 +    struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
1.107919 +    Table *pTab = pTabItem->pTab;
1.107920 +    assert( pTab!=0 );
1.107921 +    if( (pTab->tabFlags & TF_Ephemeral)==0
1.107922 +     && pTab->pSelect==0
1.107923 +     && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
1.107924 +    ){
1.107925 +      int ws = pLevel->plan.wsFlags;
1.107926 +      if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
1.107927 +        sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
1.107928 +      }
1.107929 +      if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
1.107930 +        sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
1.107931 +      }
1.107932 +    }
1.107933 +
1.107934 +    /* If this scan uses an index, make code substitutions to read data
1.107935 +    ** from the index in preference to the table. Sometimes, this means
1.107936 +    ** the table need never be read from. This is a performance boost,
1.107937 +    ** as the vdbe level waits until the table is read before actually
1.107938 +    ** seeking the table cursor to the record corresponding to the current
1.107939 +    ** position in the index.
1.107940 +    ** 
1.107941 +    ** Calls to the code generator in between sqlite3WhereBegin and
1.107942 +    ** sqlite3WhereEnd will have created code that references the table
1.107943 +    ** directly.  This loop scans all that code looking for opcodes
1.107944 +    ** that reference the table and converts them into opcodes that
1.107945 +    ** reference the index.
1.107946 +    */
1.107947 +    if( pLevel->plan.wsFlags & WHERE_INDEXED ){
1.107948 +      pIdx = pLevel->plan.u.pIdx;
1.107949 +    }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
1.107950 +      pIdx = pLevel->u.pCovidx;
1.107951 +    }
1.107952 +    if( pIdx && !db->mallocFailed){
1.107953 +      int k, j, last;
1.107954 +      VdbeOp *pOp;
1.107955 +
1.107956 +      pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
1.107957 +      last = sqlite3VdbeCurrentAddr(v);
1.107958 +      for(k=pWInfo->iTop; k<last; k++, pOp++){
1.107959 +        if( pOp->p1!=pLevel->iTabCur ) continue;
1.107960 +        if( pOp->opcode==OP_Column ){
1.107961 +          for(j=0; j<pIdx->nColumn; j++){
1.107962 +            if( pOp->p2==pIdx->aiColumn[j] ){
1.107963 +              pOp->p2 = j;
1.107964 +              pOp->p1 = pLevel->iIdxCur;
1.107965 +              break;
1.107966 +            }
1.107967 +          }
1.107968 +          assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
1.107969 +               || j<pIdx->nColumn );
1.107970 +        }else if( pOp->opcode==OP_Rowid ){
1.107971 +          pOp->p1 = pLevel->iIdxCur;
1.107972 +          pOp->opcode = OP_IdxRowid;
1.107973 +        }
1.107974 +      }
1.107975 +    }
1.107976 +  }
1.107977 +
1.107978 +  /* Final cleanup
1.107979 +  */
1.107980 +  pParse->nQueryLoop = pWInfo->savedNQueryLoop;
1.107981 +  whereInfoFree(db, pWInfo);
1.107982 +  return;
1.107983 +}
1.107984 +
1.107985 +/************** End of where.c ***********************************************/
1.107986 +/************** Begin file parse.c *******************************************/
1.107987 +/* Driver template for the LEMON parser generator.
1.107988 +** The author disclaims copyright to this source code.
1.107989 +**
1.107990 +** This version of "lempar.c" is modified, slightly, for use by SQLite.
1.107991 +** The only modifications are the addition of a couple of NEVER()
1.107992 +** macros to disable tests that are needed in the case of a general
1.107993 +** LALR(1) grammar but which are always false in the
1.107994 +** specific grammar used by SQLite.
1.107995 +*/
1.107996 +/* First off, code is included that follows the "include" declaration
1.107997 +** in the input grammar file. */
1.107998 +/* #include <stdio.h> */
1.107999 +
1.108000 +
1.108001 +/*
1.108002 +** Disable all error recovery processing in the parser push-down
1.108003 +** automaton.
1.108004 +*/
1.108005 +#define YYNOERRORRECOVERY 1
1.108006 +
1.108007 +/*
1.108008 +** Make yytestcase() the same as testcase()
1.108009 +*/
1.108010 +#define yytestcase(X) testcase(X)
1.108011 +
1.108012 +/*
1.108013 +** An instance of this structure holds information about the
1.108014 +** LIMIT clause of a SELECT statement.
1.108015 +*/
1.108016 +struct LimitVal {
1.108017 +  Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
1.108018 +  Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
1.108019 +};
1.108020 +
1.108021 +/*
1.108022 +** An instance of this structure is used to store the LIKE,
1.108023 +** GLOB, NOT LIKE, and NOT GLOB operators.
1.108024 +*/
1.108025 +struct LikeOp {
1.108026 +  Token eOperator;  /* "like" or "glob" or "regexp" */
1.108027 +  int bNot;         /* True if the NOT keyword is present */
1.108028 +};
1.108029 +
1.108030 +/*
1.108031 +** An instance of the following structure describes the event of a
1.108032 +** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
1.108033 +** TK_DELETE, or TK_INSTEAD.  If the event is of the form
1.108034 +**
1.108035 +**      UPDATE ON (a,b,c)
1.108036 +**
1.108037 +** Then the "b" IdList records the list "a,b,c".
1.108038 +*/
1.108039 +struct TrigEvent { int a; IdList * b; };
1.108040 +
1.108041 +/*
1.108042 +** An instance of this structure holds the ATTACH key and the key type.
1.108043 +*/
1.108044 +struct AttachKey { int type;  Token key; };
1.108045 +
1.108046 +/*
1.108047 +** One or more VALUES claues
1.108048 +*/
1.108049 +struct ValueList {
1.108050 +  ExprList *pList;
1.108051 +  Select *pSelect;
1.108052 +};
1.108053 +
1.108054 +
1.108055 +  /* This is a utility routine used to set the ExprSpan.zStart and
1.108056 +  ** ExprSpan.zEnd values of pOut so that the span covers the complete
1.108057 +  ** range of text beginning with pStart and going to the end of pEnd.
1.108058 +  */
1.108059 +  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
1.108060 +    pOut->zStart = pStart->z;
1.108061 +    pOut->zEnd = &pEnd->z[pEnd->n];
1.108062 +  }
1.108063 +
1.108064 +  /* Construct a new Expr object from a single identifier.  Use the
1.108065 +  ** new Expr to populate pOut.  Set the span of pOut to be the identifier
1.108066 +  ** that created the expression.
1.108067 +  */
1.108068 +  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
1.108069 +    pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
1.108070 +    pOut->zStart = pValue->z;
1.108071 +    pOut->zEnd = &pValue->z[pValue->n];
1.108072 +  }
1.108073 +
1.108074 +  /* This routine constructs a binary expression node out of two ExprSpan
1.108075 +  ** objects and uses the result to populate a new ExprSpan object.
1.108076 +  */
1.108077 +  static void spanBinaryExpr(
1.108078 +    ExprSpan *pOut,     /* Write the result here */
1.108079 +    Parse *pParse,      /* The parsing context.  Errors accumulate here */
1.108080 +    int op,             /* The binary operation */
1.108081 +    ExprSpan *pLeft,    /* The left operand */
1.108082 +    ExprSpan *pRight    /* The right operand */
1.108083 +  ){
1.108084 +    pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
1.108085 +    pOut->zStart = pLeft->zStart;
1.108086 +    pOut->zEnd = pRight->zEnd;
1.108087 +  }
1.108088 +
1.108089 +  /* Construct an expression node for a unary postfix operator
1.108090 +  */
1.108091 +  static void spanUnaryPostfix(
1.108092 +    ExprSpan *pOut,        /* Write the new expression node here */
1.108093 +    Parse *pParse,         /* Parsing context to record errors */
1.108094 +    int op,                /* The operator */
1.108095 +    ExprSpan *pOperand,    /* The operand */
1.108096 +    Token *pPostOp         /* The operand token for setting the span */
1.108097 +  ){
1.108098 +    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
1.108099 +    pOut->zStart = pOperand->zStart;
1.108100 +    pOut->zEnd = &pPostOp->z[pPostOp->n];
1.108101 +  }                           
1.108102 +
1.108103 +  /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1.108104 +  ** unary TK_ISNULL or TK_NOTNULL expression. */
1.108105 +  static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1.108106 +    sqlite3 *db = pParse->db;
1.108107 +    if( db->mallocFailed==0 && pY->op==TK_NULL ){
1.108108 +      pA->op = (u8)op;
1.108109 +      sqlite3ExprDelete(db, pA->pRight);
1.108110 +      pA->pRight = 0;
1.108111 +    }
1.108112 +  }
1.108113 +
1.108114 +  /* Construct an expression node for a unary prefix operator
1.108115 +  */
1.108116 +  static void spanUnaryPrefix(
1.108117 +    ExprSpan *pOut,        /* Write the new expression node here */
1.108118 +    Parse *pParse,         /* Parsing context to record errors */
1.108119 +    int op,                /* The operator */
1.108120 +    ExprSpan *pOperand,    /* The operand */
1.108121 +    Token *pPreOp         /* The operand token for setting the span */
1.108122 +  ){
1.108123 +    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
1.108124 +    pOut->zStart = pPreOp->z;
1.108125 +    pOut->zEnd = pOperand->zEnd;
1.108126 +  }
1.108127 +/* Next is all token values, in a form suitable for use by makeheaders.
1.108128 +** This section will be null unless lemon is run with the -m switch.
1.108129 +*/
1.108130 +/* 
1.108131 +** These constants (all generated automatically by the parser generator)
1.108132 +** specify the various kinds of tokens (terminals) that the parser
1.108133 +** understands. 
1.108134 +**
1.108135 +** Each symbol here is a terminal symbol in the grammar.
1.108136 +*/
1.108137 +/* Make sure the INTERFACE macro is defined.
1.108138 +*/
1.108139 +#ifndef INTERFACE
1.108140 +# define INTERFACE 1
1.108141 +#endif
1.108142 +/* The next thing included is series of defines which control
1.108143 +** various aspects of the generated parser.
1.108144 +**    YYCODETYPE         is the data type used for storing terminal
1.108145 +**                       and nonterminal numbers.  "unsigned char" is
1.108146 +**                       used if there are fewer than 250 terminals
1.108147 +**                       and nonterminals.  "int" is used otherwise.
1.108148 +**    YYNOCODE           is a number of type YYCODETYPE which corresponds
1.108149 +**                       to no legal terminal or nonterminal number.  This
1.108150 +**                       number is used to fill in empty slots of the hash 
1.108151 +**                       table.
1.108152 +**    YYFALLBACK         If defined, this indicates that one or more tokens
1.108153 +**                       have fall-back values which should be used if the
1.108154 +**                       original value of the token will not parse.
1.108155 +**    YYACTIONTYPE       is the data type used for storing terminal
1.108156 +**                       and nonterminal numbers.  "unsigned char" is
1.108157 +**                       used if there are fewer than 250 rules and
1.108158 +**                       states combined.  "int" is used otherwise.
1.108159 +**    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given 
1.108160 +**                       directly to the parser from the tokenizer.
1.108161 +**    YYMINORTYPE        is the data type used for all minor tokens.
1.108162 +**                       This is typically a union of many types, one of
1.108163 +**                       which is sqlite3ParserTOKENTYPE.  The entry in the union
1.108164 +**                       for base tokens is called "yy0".
1.108165 +**    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
1.108166 +**                       zero the stack is dynamically sized using realloc()
1.108167 +**    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
1.108168 +**    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
1.108169 +**    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
1.108170 +**    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
1.108171 +**    YYNSTATE           the combined number of states.
1.108172 +**    YYNRULE            the number of rules in the grammar
1.108173 +**    YYERRORSYMBOL      is the code number of the error symbol.  If not
1.108174 +**                       defined, then do no error processing.
1.108175 +*/
1.108176 +#define YYCODETYPE unsigned char
1.108177 +#define YYNOCODE 251
1.108178 +#define YYACTIONTYPE unsigned short int
1.108179 +#define YYWILDCARD 67
1.108180 +#define sqlite3ParserTOKENTYPE Token
1.108181 +typedef union {
1.108182 +  int yyinit;
1.108183 +  sqlite3ParserTOKENTYPE yy0;
1.108184 +  struct LimitVal yy64;
1.108185 +  Expr* yy122;
1.108186 +  Select* yy159;
1.108187 +  IdList* yy180;
1.108188 +  struct {int value; int mask;} yy207;
1.108189 +  u8 yy258;
1.108190 +  struct LikeOp yy318;
1.108191 +  TriggerStep* yy327;
1.108192 +  ExprSpan yy342;
1.108193 +  SrcList* yy347;
1.108194 +  int yy392;
1.108195 +  struct TrigEvent yy410;
1.108196 +  ExprList* yy442;
1.108197 +  struct ValueList yy487;
1.108198 +} YYMINORTYPE;
1.108199 +#ifndef YYSTACKDEPTH
1.108200 +#define YYSTACKDEPTH 100
1.108201 +#endif
1.108202 +#define sqlite3ParserARG_SDECL Parse *pParse;
1.108203 +#define sqlite3ParserARG_PDECL ,Parse *pParse
1.108204 +#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
1.108205 +#define sqlite3ParserARG_STORE yypParser->pParse = pParse
1.108206 +#define YYNSTATE 627
1.108207 +#define YYNRULE 327
1.108208 +#define YYFALLBACK 1
1.108209 +#define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
1.108210 +#define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
1.108211 +#define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
1.108212 +
1.108213 +/* The yyzerominor constant is used to initialize instances of
1.108214 +** YYMINORTYPE objects to zero. */
1.108215 +static const YYMINORTYPE yyzerominor = { 0 };
1.108216 +
1.108217 +/* Define the yytestcase() macro to be a no-op if is not already defined
1.108218 +** otherwise.
1.108219 +**
1.108220 +** Applications can choose to define yytestcase() in the %include section
1.108221 +** to a macro that can assist in verifying code coverage.  For production
1.108222 +** code the yytestcase() macro should be turned off.  But it is useful
1.108223 +** for testing.
1.108224 +*/
1.108225 +#ifndef yytestcase
1.108226 +# define yytestcase(X)
1.108227 +#endif
1.108228 +
1.108229 +
1.108230 +/* Next are the tables used to determine what action to take based on the
1.108231 +** current state and lookahead token.  These tables are used to implement
1.108232 +** functions that take a state number and lookahead value and return an
1.108233 +** action integer.  
1.108234 +**
1.108235 +** Suppose the action integer is N.  Then the action is determined as
1.108236 +** follows
1.108237 +**
1.108238 +**   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
1.108239 +**                                      token onto the stack and goto state N.
1.108240 +**
1.108241 +**   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
1.108242 +**
1.108243 +**   N == YYNSTATE+YYNRULE              A syntax error has occurred.
1.108244 +**
1.108245 +**   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
1.108246 +**
1.108247 +**   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
1.108248 +**                                      slots in the yy_action[] table.
1.108249 +**
1.108250 +** The action table is constructed as a single large table named yy_action[].
1.108251 +** Given state S and lookahead X, the action is computed as
1.108252 +**
1.108253 +**      yy_action[ yy_shift_ofst[S] + X ]
1.108254 +**
1.108255 +** If the index value yy_shift_ofst[S]+X is out of range or if the value
1.108256 +** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
1.108257 +** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
1.108258 +** and that yy_default[S] should be used instead.  
1.108259 +**
1.108260 +** The formula above is for computing the action when the lookahead is
1.108261 +** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
1.108262 +** a reduce action) then the yy_reduce_ofst[] array is used in place of
1.108263 +** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
1.108264 +** YY_SHIFT_USE_DFLT.
1.108265 +**
1.108266 +** The following are the tables generated in this section:
1.108267 +**
1.108268 +**  yy_action[]        A single table containing all actions.
1.108269 +**  yy_lookahead[]     A table containing the lookahead for each entry in
1.108270 +**                     yy_action.  Used to detect hash collisions.
1.108271 +**  yy_shift_ofst[]    For each state, the offset into yy_action for
1.108272 +**                     shifting terminals.
1.108273 +**  yy_reduce_ofst[]   For each state, the offset into yy_action for
1.108274 +**                     shifting non-terminals after a reduce.
1.108275 +**  yy_default[]       Default action for each state.
1.108276 +*/
1.108277 +#define YY_ACTTAB_COUNT (1564)
1.108278 +static const YYACTIONTYPE yy_action[] = {
1.108279 + /*     0 */   309,  955,  184,  417,    2,  171,  624,  594,   56,   56,
1.108280 + /*    10 */    56,   56,   49,   54,   54,   54,   54,   53,   53,   52,
1.108281 + /*    20 */    52,   52,   51,  233,  620,  619,  298,  620,  619,  234,
1.108282 + /*    30 */   587,  581,   56,   56,   56,   56,   19,   54,   54,   54,
1.108283 + /*    40 */    54,   53,   53,   52,   52,   52,   51,  233,  605,   57,
1.108284 + /*    50 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
1.108285 + /*    60 */    56,   56,  541,   54,   54,   54,   54,   53,   53,   52,
1.108286 + /*    70 */    52,   52,   51,  233,  309,  594,  325,  196,  195,  194,
1.108287 + /*    80 */    33,   54,   54,   54,   54,   53,   53,   52,   52,   52,
1.108288 + /*    90 */    51,  233,  617,  616,  165,  617,  616,  380,  377,  376,
1.108289 + /*   100 */   407,  532,  576,  576,  587,  581,  303,  422,  375,   59,
1.108290 + /*   110 */    53,   53,   52,   52,   52,   51,  233,   50,   47,  146,
1.108291 + /*   120 */   574,  545,   65,   57,   58,   48,  579,  578,  580,  580,
1.108292 + /*   130 */    55,   55,   56,   56,   56,   56,  213,   54,   54,   54,
1.108293 + /*   140 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  223,
1.108294 + /*   150 */   539,  420,  170,  176,  138,  280,  383,  275,  382,  168,
1.108295 + /*   160 */   489,  551,  409,  668,  620,  619,  271,  438,  409,  438,
1.108296 + /*   170 */   550,  604,   67,  482,  507,  618,  599,  412,  587,  581,
1.108297 + /*   180 */   600,  483,  618,  412,  618,  598,   91,  439,  440,  439,
1.108298 + /*   190 */   335,  598,   73,  669,  222,  266,  480,   57,   58,   48,
1.108299 + /*   200 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
1.108300 + /*   210 */   670,   54,   54,   54,   54,   53,   53,   52,   52,   52,
1.108301 + /*   220 */    51,  233,  309,  279,  232,  231,    1,  132,  200,  385,
1.108302 + /*   230 */   620,  619,  617,  616,  278,  435,  289,  563,  175,  262,
1.108303 + /*   240 */   409,  264,  437,  497,  436,  166,  441,  568,  336,  568,
1.108304 + /*   250 */   201,  537,  587,  581,  599,  412,  165,  594,  600,  380,
1.108305 + /*   260 */   377,  376,  597,  598,   92,  523,  618,  569,  569,  592,
1.108306 + /*   270 */   375,   57,   58,   48,  579,  578,  580,  580,   55,   55,
1.108307 + /*   280 */    56,   56,   56,   56,  597,   54,   54,   54,   54,   53,
1.108308 + /*   290 */    53,   52,   52,   52,   51,  233,  309,  463,  617,  616,
1.108309 + /*   300 */   590,  590,  590,  174,  272,  396,  409,  272,  409,  548,
1.108310 + /*   310 */   397,  620,  619,   68,  326,  620,  619,  620,  619,  618,
1.108311 + /*   320 */   546,  412,  618,  412,  471,  594,  587,  581,  472,  598,
1.108312 + /*   330 */    92,  598,   92,   52,   52,   52,   51,  233,  513,  512,
1.108313 + /*   340 */   206,  322,  363,  464,  221,   57,   58,   48,  579,  578,
1.108314 + /*   350 */   580,  580,   55,   55,   56,   56,   56,   56,  529,   54,
1.108315 + /*   360 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
1.108316 + /*   370 */   309,  396,  409,  396,  597,  372,  386,  530,  347,  617,
1.108317 + /*   380 */   616,  575,  202,  617,  616,  617,  616,  412,  620,  619,
1.108318 + /*   390 */   145,  255,  346,  254,  577,  598,   74,  351,   45,  489,
1.108319 + /*   400 */   587,  581,  235,  189,  464,  544,  167,  296,  187,  469,
1.108320 + /*   410 */   479,   67,   62,   39,  618,  546,  597,  345,  573,   57,
1.108321 + /*   420 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
1.108322 + /*   430 */    56,   56,    6,   54,   54,   54,   54,   53,   53,   52,
1.108323 + /*   440 */    52,   52,   51,  233,  309,  562,  558,  407,  528,  576,
1.108324 + /*   450 */   576,  344,  255,  346,  254,  182,  617,  616,  503,  504,
1.108325 + /*   460 */   314,  409,  557,  235,  166,  271,  409,  352,  564,  181,
1.108326 + /*   470 */   407,  546,  576,  576,  587,  581,  412,  537,  556,  561,
1.108327 + /*   480 */   517,  412,  618,  249,  598,   16,    7,   36,  467,  598,
1.108328 + /*   490 */    92,  516,  618,   57,   58,   48,  579,  578,  580,  580,
1.108329 + /*   500 */    55,   55,   56,   56,   56,   56,  541,   54,   54,   54,
1.108330 + /*   510 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  327,
1.108331 + /*   520 */   572,  571,  525,  558,  560,  394,  871,  246,  409,  248,
1.108332 + /*   530 */   171,  392,  594,  219,  407,  409,  576,  576,  502,  557,
1.108333 + /*   540 */   364,  145,  510,  412,  407,  229,  576,  576,  587,  581,
1.108334 + /*   550 */   412,  598,   92,  381,  269,  556,  166,  400,  598,   69,
1.108335 + /*   560 */   501,  419,  945,  199,  945,  198,  546,   57,   58,   48,
1.108336 + /*   570 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
1.108337 + /*   580 */   568,   54,   54,   54,   54,   53,   53,   52,   52,   52,
1.108338 + /*   590 */    51,  233,  309,  317,  419,  944,  508,  944,  308,  597,
1.108339 + /*   600 */   594,  565,  490,  212,  173,  247,  423,  615,  614,  613,
1.108340 + /*   610 */   323,  197,  143,  405,  572,  571,  489,   66,   50,   47,
1.108341 + /*   620 */   146,  594,  587,  581,  232,  231,  559,  427,   67,  555,
1.108342 + /*   630 */    15,  618,  186,  543,  303,  421,   35,  206,  432,  423,
1.108343 + /*   640 */   552,   57,   58,   48,  579,  578,  580,  580,   55,   55,
1.108344 + /*   650 */    56,   56,   56,   56,  205,   54,   54,   54,   54,   53,
1.108345 + /*   660 */    53,   52,   52,   52,   51,  233,  309,  569,  569,  260,
1.108346 + /*   670 */   268,  597,   12,  373,  568,  166,  409,  313,  409,  420,
1.108347 + /*   680 */   409,  473,  473,  365,  618,   50,   47,  146,  597,  594,
1.108348 + /*   690 */   468,  412,  166,  412,  351,  412,  587,  581,   32,  598,
1.108349 + /*   700 */    94,  598,   97,  598,   95,  627,  625,  329,  142,   50,
1.108350 + /*   710 */    47,  146,  333,  349,  358,   57,   58,   48,  579,  578,
1.108351 + /*   720 */   580,  580,   55,   55,   56,   56,   56,   56,  409,   54,
1.108352 + /*   730 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
1.108353 + /*   740 */   309,  409,  388,  412,  409,   22,  565,  404,  212,  362,
1.108354 + /*   750 */   389,  598,  104,  359,  409,  156,  412,  409,  603,  412,
1.108355 + /*   760 */   537,  331,  569,  569,  598,  103,  493,  598,  105,  412,
1.108356 + /*   770 */   587,  581,  412,  260,  549,  618,   11,  598,  106,  521,
1.108357 + /*   780 */   598,  133,  169,  457,  456,  170,   35,  601,  618,   57,
1.108358 + /*   790 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
1.108359 + /*   800 */    56,   56,  409,   54,   54,   54,   54,   53,   53,   52,
1.108360 + /*   810 */    52,   52,   51,  233,  309,  409,  259,  412,  409,   50,
1.108361 + /*   820 */    47,  146,  357,  318,  355,  598,  134,  527,  352,  337,
1.108362 + /*   830 */   412,  409,  356,  412,  357,  409,  357,  618,  598,   98,
1.108363 + /*   840 */   129,  598,  102,  618,  587,  581,  412,   21,  235,  618,
1.108364 + /*   850 */   412,  618,  211,  143,  598,  101,   30,  167,  598,   93,
1.108365 + /*   860 */   350,  535,  203,   57,   58,   48,  579,  578,  580,  580,
1.108366 + /*   870 */    55,   55,   56,   56,   56,   56,  409,   54,   54,   54,
1.108367 + /*   880 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  409,
1.108368 + /*   890 */   526,  412,  409,  425,  215,  305,  597,  551,  141,  598,
1.108369 + /*   900 */   100,   40,  409,   38,  412,  409,  550,  412,  409,  228,
1.108370 + /*   910 */   220,  314,  598,   77,  500,  598,   96,  412,  587,  581,
1.108371 + /*   920 */   412,  338,  253,  412,  218,  598,  137,  379,  598,  136,
1.108372 + /*   930 */    28,  598,  135,  270,  715,  210,  481,   57,   58,   48,
1.108373 + /*   940 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
1.108374 + /*   950 */   409,   54,   54,   54,   54,   53,   53,   52,   52,   52,
1.108375 + /*   960 */    51,  233,  309,  409,  272,  412,  409,  315,  147,  597,
1.108376 + /*   970 */   272,  626,    2,  598,   76,  209,  409,  127,  412,  618,
1.108377 + /*   980 */   126,  412,  409,  621,  235,  618,  598,   90,  374,  598,
1.108378 + /*   990 */    89,  412,  587,  581,   27,  260,  350,  412,  618,  598,
1.108379 + /*  1000 */    75,  321,  541,  541,  125,  598,   88,  320,  278,  597,
1.108380 + /*  1010 */   618,   57,   46,   48,  579,  578,  580,  580,   55,   55,
1.108381 + /*  1020 */    56,   56,   56,   56,  409,   54,   54,   54,   54,   53,
1.108382 + /*  1030 */    53,   52,   52,   52,   51,  233,  309,  409,  450,  412,
1.108383 + /*  1040 */   164,  284,  282,  272,  609,  424,  304,  598,   87,  370,
1.108384 + /*  1050 */   409,  477,  412,  409,  608,  409,  607,  602,  618,  618,
1.108385 + /*  1060 */   598,   99,  586,  585,  122,  412,  587,  581,  412,  618,
1.108386 + /*  1070 */   412,  618,  618,  598,   86,  366,  598,   17,  598,   85,
1.108387 + /*  1080 */   319,  185,  519,  518,  583,  582,   58,   48,  579,  578,
1.108388 + /*  1090 */   580,  580,   55,   55,   56,   56,   56,   56,  409,   54,
1.108389 + /*  1100 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
1.108390 + /*  1110 */   309,  584,  409,  412,  409,  260,  260,  260,  408,  591,
1.108391 + /*  1120 */   474,  598,   84,  170,  409,  466,  518,  412,  121,  412,
1.108392 + /*  1130 */   618,  618,  618,  618,  618,  598,   83,  598,   72,  412,
1.108393 + /*  1140 */   587,  581,   51,  233,  625,  329,  470,  598,   71,  257,
1.108394 + /*  1150 */   159,  120,   14,  462,  157,  158,  117,  260,  448,  447,
1.108395 + /*  1160 */   446,   48,  579,  578,  580,  580,   55,   55,   56,   56,
1.108396 + /*  1170 */    56,   56,  618,   54,   54,   54,   54,   53,   53,   52,
1.108397 + /*  1180 */    52,   52,   51,  233,   44,  403,  260,    3,  409,  459,
1.108398 + /*  1190 */   260,  413,  619,  118,  398,   10,   25,   24,  554,  348,
1.108399 + /*  1200 */   217,  618,  406,  412,  409,  618,    4,   44,  403,  618,
1.108400 + /*  1210 */     3,  598,   82,  618,  413,  619,  455,  542,  115,  412,
1.108401 + /*  1220 */   538,  401,  536,  274,  506,  406,  251,  598,   81,  216,
1.108402 + /*  1230 */   273,  563,  618,  243,  453,  618,  154,  618,  618,  618,
1.108403 + /*  1240 */   449,  416,  623,  110,  401,  618,  409,  236,   64,  123,
1.108404 + /*  1250 */   487,   41,   42,  531,  563,  204,  409,  267,   43,  411,
1.108405 + /*  1260 */   410,  412,  265,  592,  108,  618,  107,  434,  332,  598,
1.108406 + /*  1270 */    80,  412,  618,  263,   41,   42,  443,  618,  409,  598,
1.108407 + /*  1280 */    70,   43,  411,  410,  433,  261,  592,  149,  618,  597,
1.108408 + /*  1290 */   256,  237,  188,  412,  590,  590,  590,  589,  588,   13,
1.108409 + /*  1300 */   618,  598,   18,  328,  235,  618,   44,  403,  360,    3,
1.108410 + /*  1310 */   418,  461,  339,  413,  619,  227,  124,  590,  590,  590,
1.108411 + /*  1320 */   589,  588,   13,  618,  406,  409,  618,  409,  139,   34,
1.108412 + /*  1330 */   403,  387,    3,  148,  622,  312,  413,  619,  311,  330,
1.108413 + /*  1340 */   412,  460,  412,  401,  180,  353,  412,  406,  598,   79,
1.108414 + /*  1350 */   598,   78,  250,  563,  598,    9,  618,  612,  611,  610,
1.108415 + /*  1360 */   618,    8,  452,  442,  242,  415,  401,  618,  239,  235,
1.108416 + /*  1370 */   179,  238,  428,   41,   42,  288,  563,  618,  618,  618,
1.108417 + /*  1380 */    43,  411,  410,  618,  144,  592,  618,  618,  177,   61,
1.108418 + /*  1390 */   618,  596,  391,  620,  619,  287,   41,   42,  414,  618,
1.108419 + /*  1400 */   293,   30,  393,   43,  411,  410,  292,  618,  592,   31,
1.108420 + /*  1410 */   618,  395,  291,   60,  230,   37,  590,  590,  590,  589,
1.108421 + /*  1420 */   588,   13,  214,  553,  183,  290,  172,  301,  300,  299,
1.108422 + /*  1430 */   178,  297,  595,  563,  451,   29,  285,  390,  540,  590,
1.108423 + /*  1440 */   590,  590,  589,  588,   13,  283,  520,  534,  150,  533,
1.108424 + /*  1450 */   241,  281,  384,  192,  191,  324,  515,  514,  276,  240,
1.108425 + /*  1460 */   510,  523,  307,  511,  128,  592,  509,  225,  226,  486,
1.108426 + /*  1470 */   485,  224,  152,  491,  464,  306,  484,  163,  153,  371,
1.108427 + /*  1480 */   478,  151,  162,  258,  369,  161,  367,  208,  475,  476,
1.108428 + /*  1490 */    26,  160,  465,  140,  361,  131,  590,  590,  590,  116,
1.108429 + /*  1500 */   119,  454,  343,  155,  114,  342,  113,  112,  445,  111,
1.108430 + /*  1510 */   130,  109,  431,  316,  426,  430,   23,  429,   20,  606,
1.108431 + /*  1520 */   190,  507,  255,  341,  244,   63,  294,  593,  310,  570,
1.108432 + /*  1530 */   277,  402,  354,  235,  567,  496,  495,  492,  494,  302,
1.108433 + /*  1540 */   458,  378,  286,  245,  566,    5,  252,  547,  193,  444,
1.108434 + /*  1550 */   233,  340,  207,  524,  368,  505,  334,  522,  499,  399,
1.108435 + /*  1560 */   295,  498,  956,  488,
1.108436 +};
1.108437 +static const YYCODETYPE yy_lookahead[] = {
1.108438 + /*     0 */    19,  142,  143,  144,  145,   24,    1,   26,   77,   78,
1.108439 + /*    10 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
1.108440 + /*    20 */    89,   90,   91,   92,   26,   27,   15,   26,   27,  197,
1.108441 + /*    30 */    49,   50,   77,   78,   79,   80,  204,   82,   83,   84,
1.108442 + /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   23,   68,
1.108443 + /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
1.108444 + /*    60 */    79,   80,  166,   82,   83,   84,   85,   86,   87,   88,
1.108445 + /*    70 */    89,   90,   91,   92,   19,   94,   19,  105,  106,  107,
1.108446 + /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
1.108447 + /*    90 */    91,   92,   94,   95,   96,   94,   95,   99,  100,  101,
1.108448 + /*   100 */   112,  205,  114,  115,   49,   50,   22,   23,  110,   54,
1.108449 + /*   110 */    86,   87,   88,   89,   90,   91,   92,  221,  222,  223,
1.108450 + /*   120 */    23,  120,   25,   68,   69,   70,   71,   72,   73,   74,
1.108451 + /*   130 */    75,   76,   77,   78,   79,   80,   22,   82,   83,   84,
1.108452 + /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   92,
1.108453 + /*   150 */    23,   67,   25,   96,   97,   98,   99,  100,  101,  102,
1.108454 + /*   160 */   150,   32,  150,  118,   26,   27,  109,  150,  150,  150,
1.108455 + /*   170 */    41,  161,  162,  180,  181,  165,  113,  165,   49,   50,
1.108456 + /*   180 */   117,  188,  165,  165,  165,  173,  174,  170,  171,  170,
1.108457 + /*   190 */   171,  173,  174,  118,  184,   16,  186,   68,   69,   70,
1.108458 + /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
1.108459 + /*   210 */   118,   82,   83,   84,   85,   86,   87,   88,   89,   90,
1.108460 + /*   220 */    91,   92,   19,   98,   86,   87,   22,   24,  160,   88,
1.108461 + /*   230 */    26,   27,   94,   95,  109,   97,  224,   66,  118,   60,
1.108462 + /*   240 */   150,   62,  104,   23,  106,   25,  229,  230,  229,  230,
1.108463 + /*   250 */   160,  150,   49,   50,  113,  165,   96,   26,  117,   99,
1.108464 + /*   260 */   100,  101,  194,  173,  174,   94,  165,  129,  130,   98,
1.108465 + /*   270 */   110,   68,   69,   70,   71,   72,   73,   74,   75,   76,
1.108466 + /*   280 */    77,   78,   79,   80,  194,   82,   83,   84,   85,   86,
1.108467 + /*   290 */    87,   88,   89,   90,   91,   92,   19,   11,   94,   95,
1.108468 + /*   300 */   129,  130,  131,  118,  150,  215,  150,  150,  150,   25,
1.108469 + /*   310 */   220,   26,   27,   22,  213,   26,   27,   26,   27,  165,
1.108470 + /*   320 */    25,  165,  165,  165,   30,   94,   49,   50,   34,  173,
1.108471 + /*   330 */   174,  173,  174,   88,   89,   90,   91,   92,    7,    8,
1.108472 + /*   340 */   160,  187,   48,   57,  187,   68,   69,   70,   71,   72,
1.108473 + /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,   23,   82,
1.108474 + /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
1.108475 + /*   370 */    19,  215,  150,  215,  194,   19,  220,   88,  220,   94,
1.108476 + /*   380 */    95,   23,  160,   94,   95,   94,   95,  165,   26,   27,
1.108477 + /*   390 */    95,  105,  106,  107,  113,  173,  174,  217,   22,  150,
1.108478 + /*   400 */    49,   50,  116,  119,   57,  120,   50,  158,   22,   21,
1.108479 + /*   410 */   161,  162,  232,  136,  165,  120,  194,  237,   23,   68,
1.108480 + /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
1.108481 + /*   430 */    79,   80,   22,   82,   83,   84,   85,   86,   87,   88,
1.108482 + /*   440 */    89,   90,   91,   92,   19,   23,   12,  112,   23,  114,
1.108483 + /*   450 */   115,   63,  105,  106,  107,   23,   94,   95,   97,   98,
1.108484 + /*   460 */   104,  150,   28,  116,   25,  109,  150,  150,   23,   23,
1.108485 + /*   470 */   112,   25,  114,  115,   49,   50,  165,  150,   44,   11,
1.108486 + /*   480 */    46,  165,  165,   16,  173,  174,   76,  136,  100,  173,
1.108487 + /*   490 */   174,   57,  165,   68,   69,   70,   71,   72,   73,   74,
1.108488 + /*   500 */    75,   76,   77,   78,   79,   80,  166,   82,   83,   84,
1.108489 + /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  169,
1.108490 + /*   520 */   170,  171,   23,   12,   23,  214,  138,   60,  150,   62,
1.108491 + /*   530 */    24,  215,   26,  216,  112,  150,  114,  115,   36,   28,
1.108492 + /*   540 */   213,   95,  103,  165,  112,  205,  114,  115,   49,   50,
1.108493 + /*   550 */   165,  173,  174,   51,   23,   44,   25,   46,  173,  174,
1.108494 + /*   560 */    58,   22,   23,   22,   25,  160,  120,   68,   69,   70,
1.108495 + /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
1.108496 + /*   580 */   230,   82,   83,   84,   85,   86,   87,   88,   89,   90,
1.108497 + /*   590 */    91,   92,   19,  215,   22,   23,   23,   25,  163,  194,
1.108498 + /*   600 */    94,  166,  167,  168,   25,  138,   67,    7,    8,    9,
1.108499 + /*   610 */   108,  206,  207,  169,  170,  171,  150,   22,  221,  222,
1.108500 + /*   620 */   223,   26,   49,   50,   86,   87,   23,  161,  162,   23,
1.108501 + /*   630 */    22,  165,   24,  120,   22,   23,   25,  160,  241,   67,
1.108502 + /*   640 */   176,   68,   69,   70,   71,   72,   73,   74,   75,   76,
1.108503 + /*   650 */    77,   78,   79,   80,  160,   82,   83,   84,   85,   86,
1.108504 + /*   660 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  150,
1.108505 + /*   670 */    23,  194,   35,   23,  230,   25,  150,  155,  150,   67,
1.108506 + /*   680 */   150,  105,  106,  107,  165,  221,  222,  223,  194,   94,
1.108507 + /*   690 */    23,  165,   25,  165,  217,  165,   49,   50,   25,  173,
1.108508 + /*   700 */   174,  173,  174,  173,  174,    0,    1,    2,  118,  221,
1.108509 + /*   710 */   222,  223,  193,  219,  237,   68,   69,   70,   71,   72,
1.108510 + /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
1.108511 + /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
1.108512 + /*   740 */    19,  150,   19,  165,  150,   24,  166,  167,  168,  227,
1.108513 + /*   750 */    27,  173,  174,  231,  150,   25,  165,  150,  172,  165,
1.108514 + /*   760 */   150,  242,  129,  130,  173,  174,  180,  173,  174,  165,
1.108515 + /*   770 */    49,   50,  165,  150,  176,  165,   35,  173,  174,  165,
1.108516 + /*   780 */   173,  174,   35,   23,   23,   25,   25,  173,  165,   68,
1.108517 + /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
1.108518 + /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
1.108519 + /*   810 */    89,   90,   91,   92,   19,  150,  193,  165,  150,  221,
1.108520 + /*   820 */   222,  223,  150,  213,   19,  173,  174,   23,  150,   97,
1.108521 + /*   830 */   165,  150,   27,  165,  150,  150,  150,  165,  173,  174,
1.108522 + /*   840 */    22,  173,  174,  165,   49,   50,  165,   52,  116,  165,
1.108523 + /*   850 */   165,  165,  206,  207,  173,  174,  126,   50,  173,  174,
1.108524 + /*   860 */   128,   27,  160,   68,   69,   70,   71,   72,   73,   74,
1.108525 + /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
1.108526 + /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
1.108527 + /*   890 */    23,  165,  150,   23,  216,   25,  194,   32,   39,  173,
1.108528 + /*   900 */   174,  135,  150,  137,  165,  150,   41,  165,  150,   52,
1.108529 + /*   910 */   238,  104,  173,  174,   29,  173,  174,  165,   49,   50,
1.108530 + /*   920 */   165,  219,  238,  165,  238,  173,  174,   52,  173,  174,
1.108531 + /*   930 */    22,  173,  174,   23,   23,  160,   25,   68,   69,   70,
1.108532 + /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
1.108533 + /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
1.108534 + /*   960 */    91,   92,   19,  150,  150,  165,  150,  245,  246,  194,
1.108535 + /*   970 */   150,  144,  145,  173,  174,  160,  150,   22,  165,  165,
1.108536 + /*   980 */    22,  165,  150,  150,  116,  165,  173,  174,   52,  173,
1.108537 + /*   990 */   174,  165,   49,   50,   22,  150,  128,  165,  165,  173,
1.108538 + /*  1000 */   174,  187,  166,  166,   22,  173,  174,  187,  109,  194,
1.108539 + /*  1010 */   165,   68,   69,   70,   71,   72,   73,   74,   75,   76,
1.108540 + /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
1.108541 + /*  1030 */    87,   88,   89,   90,   91,   92,   19,  150,  193,  165,
1.108542 + /*  1040 */   102,  205,  205,  150,  150,  247,  248,  173,  174,   19,
1.108543 + /*  1050 */   150,   20,  165,  150,  150,  150,  150,  150,  165,  165,
1.108544 + /*  1060 */   173,  174,   49,   50,  104,  165,   49,   50,  165,  165,
1.108545 + /*  1070 */   165,  165,  165,  173,  174,   43,  173,  174,  173,  174,
1.108546 + /*  1080 */   187,   24,  190,  191,   71,   72,   69,   70,   71,   72,
1.108547 + /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
1.108548 + /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
1.108549 + /*  1110 */    19,   98,  150,  165,  150,  150,  150,  150,  150,  150,
1.108550 + /*  1120 */    59,  173,  174,   25,  150,  190,  191,  165,   53,  165,
1.108551 + /*  1130 */   165,  165,  165,  165,  165,  173,  174,  173,  174,  165,
1.108552 + /*  1140 */    49,   50,   91,   92,    1,    2,   53,  173,  174,  138,
1.108553 + /*  1150 */   104,   22,    5,    1,   35,  118,  127,  150,  193,  193,
1.108554 + /*  1160 */   193,   70,   71,   72,   73,   74,   75,   76,   77,   78,
1.108555 + /*  1170 */    79,   80,  165,   82,   83,   84,   85,   86,   87,   88,
1.108556 + /*  1180 */    89,   90,   91,   92,   19,   20,  150,   22,  150,   27,
1.108557 + /*  1190 */   150,   26,   27,  108,  150,   22,   76,   76,  150,   25,
1.108558 + /*  1200 */   193,  165,   37,  165,  150,  165,   22,   19,   20,  165,
1.108559 + /*  1210 */    22,  173,  174,  165,   26,   27,   23,  150,  119,  165,
1.108560 + /*  1220 */   150,   56,  150,  150,  150,   37,   16,  173,  174,  193,
1.108561 + /*  1230 */   150,   66,  165,  193,    1,  165,  121,  165,  165,  165,
1.108562 + /*  1240 */    20,  146,  147,  119,   56,  165,  150,  152,   16,  154,
1.108563 + /*  1250 */   150,   86,   87,   88,   66,  160,  150,  150,   93,   94,
1.108564 + /*  1260 */    95,  165,  150,   98,  108,  165,  127,   23,   65,  173,
1.108565 + /*  1270 */   174,  165,  165,  150,   86,   87,  128,  165,  150,  173,
1.108566 + /*  1280 */   174,   93,   94,   95,   23,  150,   98,   15,  165,  194,
1.108567 + /*  1290 */   150,  140,   22,  165,  129,  130,  131,  132,  133,  134,
1.108568 + /*  1300 */   165,  173,  174,    3,  116,  165,   19,   20,  150,   22,
1.108569 + /*  1310 */     4,  150,  217,   26,   27,  179,  179,  129,  130,  131,
1.108570 + /*  1320 */   132,  133,  134,  165,   37,  150,  165,  150,  164,   19,
1.108571 + /*  1330 */    20,  150,   22,  246,  149,  249,   26,   27,  249,  244,
1.108572 + /*  1340 */   165,  150,  165,   56,    6,  150,  165,   37,  173,  174,
1.108573 + /*  1350 */   173,  174,  150,   66,  173,  174,  165,  149,  149,   13,
1.108574 + /*  1360 */   165,   25,  150,  150,  150,  149,   56,  165,  150,  116,
1.108575 + /*  1370 */   151,  150,  150,   86,   87,  150,   66,  165,  165,  165,
1.108576 + /*  1380 */    93,   94,   95,  165,  150,   98,  165,  165,  151,   22,
1.108577 + /*  1390 */   165,  194,  150,   26,   27,  150,   86,   87,  159,  165,
1.108578 + /*  1400 */   199,  126,  123,   93,   94,   95,  200,  165,   98,  124,
1.108579 + /*  1410 */   165,  122,  201,  125,  225,  135,  129,  130,  131,  132,
1.108580 + /*  1420 */   133,  134,    5,  157,  157,  202,  118,   10,   11,   12,
1.108581 + /*  1430 */    13,   14,  203,   66,   17,  104,  210,  121,  211,  129,
1.108582 + /*  1440 */   130,  131,  132,  133,  134,  210,  175,  211,   31,  211,
1.108583 + /*  1450 */    33,  210,  104,   86,   87,   47,  175,  183,  175,   42,
1.108584 + /*  1460 */   103,   94,  178,  177,   22,   98,  175,   92,  228,  175,
1.108585 + /*  1470 */   175,  228,   55,  183,   57,  178,  175,  156,   61,   18,
1.108586 + /*  1480 */   157,   64,  156,  235,  157,  156,   45,  157,  236,  157,
1.108587 + /*  1490 */   135,  156,  189,   68,  157,  218,  129,  130,  131,   22,
1.108588 + /*  1500 */   189,  199,  157,  156,  192,   18,  192,  192,  199,  192,
1.108589 + /*  1510 */   218,  189,   40,  157,   38,  157,  240,  157,  240,  153,
1.108590 + /*  1520 */   196,  181,  105,  106,  107,  243,  198,  166,  111,  230,
1.108591 + /*  1530 */   176,  226,  239,  116,  230,  176,  166,  166,  176,  148,
1.108592 + /*  1540 */   199,  177,  209,  209,  166,  196,  239,  208,  185,  199,
1.108593 + /*  1550 */    92,  209,  233,  173,  234,  182,  139,  173,  182,  191,
1.108594 + /*  1560 */   195,  182,  250,  186,
1.108595 +};
1.108596 +#define YY_SHIFT_USE_DFLT (-70)
1.108597 +#define YY_SHIFT_COUNT (416)
1.108598 +#define YY_SHIFT_MIN   (-69)
1.108599 +#define YY_SHIFT_MAX   (1487)
1.108600 +static const short yy_shift_ofst[] = {
1.108601 + /*     0 */  1143, 1188, 1417, 1188, 1287, 1287,  138,  138,   -2,  -19,
1.108602 + /*    10 */  1287, 1287, 1287, 1287,  347,  362,  129,  129,  795, 1165,
1.108603 + /*    20 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
1.108604 + /*    30 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
1.108605 + /*    40 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1310, 1287,
1.108606 + /*    50 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
1.108607 + /*    60 */  1287, 1287,  286,  362,  362,  538,  538,  231, 1253,   55,
1.108608 + /*    70 */   721,  647,  573,  499,  425,  351,  277,  203,  869,  869,
1.108609 + /*    80 */   869,  869,  869,  869,  869,  869,  869,  869,  869,  869,
1.108610 + /*    90 */   869,  869,  869,  943,  869, 1017, 1091, 1091,  -69,  -45,
1.108611 + /*   100 */   -45,  -45,  -45,  -45,   -1,   24,  245,  362,  362,  362,
1.108612 + /*   110 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
1.108613 + /*   120 */   362,  362,  362,  388,  356,  362,  362,  362,  362,  362,
1.108614 + /*   130 */   732,  868,  231, 1051, 1458,  -70,  -70,  -70, 1367,   57,
1.108615 + /*   140 */   434,  434,  289,  291,  285,    1,  204,  572,  539,  362,
1.108616 + /*   150 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
1.108617 + /*   160 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
1.108618 + /*   170 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
1.108619 + /*   180 */   362,  506,  506,  506,  705, 1253, 1253, 1253,  -70,  -70,
1.108620 + /*   190 */   -70,  171,  171,  160,  502,  502,  502,  446,  432,  511,
1.108621 + /*   200 */   422,  358,  335,  -12,  -12,  -12,  -12,  576,  294,  -12,
1.108622 + /*   210 */   -12,  295,  595,  141,  600,  730,  723,  723,  805,  730,
1.108623 + /*   220 */   805,  439,  911,  231,  865,  231,  865,  807,  865,  723,
1.108624 + /*   230 */   766,  633,  633,  231,  284,   63,  608, 1476, 1308, 1308,
1.108625 + /*   240 */  1472, 1472, 1308, 1477, 1425, 1275, 1487, 1487, 1487, 1487,
1.108626 + /*   250 */  1308, 1461, 1275, 1477, 1425, 1425, 1308, 1461, 1355, 1441,
1.108627 + /*   260 */  1308, 1308, 1461, 1308, 1461, 1308, 1461, 1442, 1348, 1348,
1.108628 + /*   270 */  1348, 1408, 1375, 1375, 1442, 1348, 1357, 1348, 1408, 1348,
1.108629 + /*   280 */  1348, 1316, 1331, 1316, 1331, 1316, 1331, 1308, 1308, 1280,
1.108630 + /*   290 */  1288, 1289, 1285, 1279, 1275, 1253, 1336, 1346, 1346, 1338,
1.108631 + /*   300 */  1338, 1338, 1338,  -70,  -70,  -70,  -70,  -70,  -70, 1013,
1.108632 + /*   310 */   467,  612,   84,  179,  -28,  870,  410,  761,  760,  667,
1.108633 + /*   320 */   650,  531,  220,  361,  331,  125,  127,   97, 1306, 1300,
1.108634 + /*   330 */  1270, 1151, 1272, 1203, 1232, 1261, 1244, 1148, 1174, 1139,
1.108635 + /*   340 */  1156, 1124, 1220, 1115, 1210, 1233, 1099, 1193, 1184, 1174,
1.108636 + /*   350 */  1173, 1029, 1121, 1120, 1085, 1162, 1119, 1037, 1152, 1147,
1.108637 + /*   360 */  1129, 1046, 1011, 1093, 1098, 1075, 1061, 1032,  960, 1057,
1.108638 + /*   370 */  1031, 1030,  899,  938,  982,  936,  972,  958,  910,  955,
1.108639 + /*   380 */   875,  885,  908,  857,  859,  867,  804,  590,  834,  747,
1.108640 + /*   390 */   818,  513,  611,  741,  673,  637,  611,  606,  603,  579,
1.108641 + /*   400 */   501,  541,  468,  386,  445,  395,  376,  281,  185,  120,
1.108642 + /*   410 */    92,   75,   45,  114,   25,   11,    5,
1.108643 +};
1.108644 +#define YY_REDUCE_USE_DFLT (-169)
1.108645 +#define YY_REDUCE_COUNT (308)
1.108646 +#define YY_REDUCE_MIN   (-168)
1.108647 +#define YY_REDUCE_MAX   (1391)
1.108648 +static const short yy_reduce_ofst[] = {
1.108649 + /*     0 */  -141,   90, 1095,  222,  158,  156,   19,   17,   10, -104,
1.108650 + /*    10 */   378,  316,  311,   12,  180,  249,  598,  464,  397, 1181,
1.108651 + /*    20 */  1177, 1175, 1128, 1106, 1096, 1054, 1038,  974,  964,  962,
1.108652 + /*    30 */   948,  905,  903,  900,  887,  874,  832,  826,  816,  813,
1.108653 + /*    40 */   800,  758,  755,  752,  742,  739,  726,  685,  681,  668,
1.108654 + /*    50 */   665,  652,  607,  604,  594,  591,  578,  530,  528,  526,
1.108655 + /*    60 */   385,   18,  477,  466,  519,  444,  350,  435,  405,  488,
1.108656 + /*    70 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
1.108657 + /*    80 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
1.108658 + /*    90 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
1.108659 + /*   100 */   488,  488,  488,  488,  488,  488,  488, 1040,  678, 1036,
1.108660 + /*   110 */  1007,  967,  966,  965,  845,  686,  610,  684,  317,  672,
1.108661 + /*   120 */   893,  327,  623,  522,   -7,  820,  814,  157,  154,  101,
1.108662 + /*   130 */   702,  494,  580,  488,  488,  488,  488,  488,  614,  586,
1.108663 + /*   140 */   935,  892,  968, 1245, 1242, 1234, 1225,  798,  798, 1222,
1.108664 + /*   150 */  1221, 1218, 1214, 1213, 1212, 1202, 1195, 1191, 1161, 1158,
1.108665 + /*   160 */  1140, 1135, 1123, 1112, 1107, 1100, 1080, 1074, 1073, 1072,
1.108666 + /*   170 */  1070, 1067, 1048, 1044,  969,  968,  907,  906,  904,  894,
1.108667 + /*   180 */   833,  837,  836,  340,  827,  815,  775,   68,  722,  646,
1.108668 + /*   190 */  -168, 1384, 1380, 1377, 1379, 1376, 1373, 1339, 1365, 1368,
1.108669 + /*   200 */  1365, 1365, 1365, 1365, 1365, 1365, 1365, 1320, 1319, 1365,
1.108670 + /*   210 */  1365, 1339, 1378, 1349, 1391, 1350, 1342, 1334, 1307, 1341,
1.108671 + /*   220 */  1293, 1364, 1363, 1371, 1362, 1370, 1359, 1340, 1354, 1333,
1.108672 + /*   230 */  1305, 1304, 1299, 1361, 1328, 1324, 1366, 1282, 1360, 1358,
1.108673 + /*   240 */  1278, 1276, 1356, 1292, 1322, 1309, 1317, 1315, 1314, 1312,
1.108674 + /*   250 */  1345, 1347, 1302, 1277, 1311, 1303, 1337, 1335, 1252, 1248,
1.108675 + /*   260 */  1332, 1330, 1329, 1327, 1326, 1323, 1321, 1297, 1301, 1295,
1.108676 + /*   270 */  1294, 1290, 1243, 1240, 1284, 1291, 1286, 1283, 1274, 1281,
1.108677 + /*   280 */  1271, 1238, 1241, 1236, 1235, 1227, 1226, 1267, 1266, 1189,
1.108678 + /*   290 */  1229, 1223, 1211, 1206, 1201, 1197, 1239, 1237, 1219, 1216,
1.108679 + /*   300 */  1209, 1208, 1185, 1089, 1086, 1087, 1137, 1136, 1164,
1.108680 +};
1.108681 +static const YYACTIONTYPE yy_default[] = {
1.108682 + /*     0 */   632,  866,  954,  954,  866,  866,  954,  954,  954,  756,
1.108683 + /*    10 */   954,  954,  954,  864,  954,  954,  784,  784,  928,  954,
1.108684 + /*    20 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108685 + /*    30 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108686 + /*    40 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108687 + /*    50 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108688 + /*    60 */   954,  954,  954,  954,  954,  954,  954,  671,  760,  790,
1.108689 + /*    70 */   954,  954,  954,  954,  954,  954,  954,  954,  927,  929,
1.108690 + /*    80 */   798,  797,  907,  771,  795,  788,  792,  867,  860,  861,
1.108691 + /*    90 */   859,  863,  868,  954,  791,  827,  844,  826,  838,  843,
1.108692 + /*   100 */   850,  842,  839,  829,  828,  830,  831,  954,  954,  954,
1.108693 + /*   110 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108694 + /*   120 */   954,  954,  954,  658,  725,  954,  954,  954,  954,  954,
1.108695 + /*   130 */   954,  954,  954,  832,  833,  847,  846,  845,  954,  663,
1.108696 + /*   140 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108697 + /*   150 */   934,  932,  954,  879,  954,  954,  954,  954,  954,  954,
1.108698 + /*   160 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108699 + /*   170 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108700 + /*   180 */   638,  756,  756,  756,  632,  954,  954,  954,  946,  760,
1.108701 + /*   190 */   750,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108702 + /*   200 */   954,  954,  954,  800,  739,  917,  919,  954,  900,  737,
1.108703 + /*   210 */   660,  758,  673,  748,  640,  794,  773,  773,  912,  794,
1.108704 + /*   220 */   912,  696,  719,  954,  784,  954,  784,  693,  784,  773,
1.108705 + /*   230 */   862,  954,  954,  954,  757,  748,  954,  939,  764,  764,
1.108706 + /*   240 */   931,  931,  764,  806,  729,  794,  736,  736,  736,  736,
1.108707 + /*   250 */   764,  655,  794,  806,  729,  729,  764,  655,  906,  904,
1.108708 + /*   260 */   764,  764,  655,  764,  655,  764,  655,  872,  727,  727,
1.108709 + /*   270 */   727,  711,  876,  876,  872,  727,  696,  727,  711,  727,
1.108710 + /*   280 */   727,  777,  772,  777,  772,  777,  772,  764,  764,  954,
1.108711 + /*   290 */   789,  778,  787,  785,  794,  954,  714,  648,  648,  637,
1.108712 + /*   300 */   637,  637,  637,  951,  951,  946,  698,  698,  681,  954,
1.108713 + /*   310 */   954,  954,  954,  954,  954,  954,  881,  954,  954,  954,
1.108714 + /*   320 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  633,
1.108715 + /*   330 */   941,  954,  954,  938,  954,  954,  954,  954,  799,  954,
1.108716 + /*   340 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  916,
1.108717 + /*   350 */   954,  954,  954,  954,  954,  954,  954,  910,  954,  954,
1.108718 + /*   360 */   954,  954,  954,  954,  903,  902,  954,  954,  954,  954,
1.108719 + /*   370 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108720 + /*   380 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
1.108721 + /*   390 */   954,  954,  786,  954,  779,  954,  865,  954,  954,  954,
1.108722 + /*   400 */   954,  954,  954,  954,  954,  954,  954,  742,  815,  954,
1.108723 + /*   410 */   814,  818,  813,  665,  954,  646,  954,  629,  634,  950,
1.108724 + /*   420 */   953,  952,  949,  948,  947,  942,  940,  937,  936,  935,
1.108725 + /*   430 */   933,  930,  926,  885,  883,  890,  889,  888,  887,  886,
1.108726 + /*   440 */   884,  882,  880,  801,  796,  793,  925,  878,  738,  735,
1.108727 + /*   450 */   734,  654,  943,  909,  918,  805,  804,  807,  915,  914,
1.108728 + /*   460 */   913,  911,  908,  895,  803,  802,  730,  870,  869,  657,
1.108729 + /*   470 */   899,  898,  897,  901,  905,  896,  766,  656,  653,  662,
1.108730 + /*   480 */   717,  718,  726,  724,  723,  722,  721,  720,  716,  664,
1.108731 + /*   490 */   672,  710,  695,  694,  875,  877,  874,  873,  703,  702,
1.108732 + /*   500 */   708,  707,  706,  705,  704,  701,  700,  699,  692,  691,
1.108733 + /*   510 */   697,  690,  713,  712,  709,  689,  733,  732,  731,  728,
1.108734 + /*   520 */   688,  687,  686,  818,  685,  684,  824,  823,  811,  854,
1.108735 + /*   530 */   753,  752,  751,  763,  762,  775,  774,  809,  808,  776,
1.108736 + /*   540 */   761,  755,  754,  770,  769,  768,  767,  759,  749,  781,
1.108737 + /*   550 */   783,  782,  780,  856,  765,  853,  924,  923,  922,  921,
1.108738 + /*   560 */   920,  858,  857,  825,  822,  676,  677,  893,  892,  894,
1.108739 + /*   570 */   891,  679,  678,  675,  674,  855,  744,  743,  851,  848,
1.108740 + /*   580 */   840,  836,  852,  849,  841,  837,  835,  834,  820,  819,
1.108741 + /*   590 */   817,  816,  812,  821,  667,  745,  741,  740,  810,  747,
1.108742 + /*   600 */   746,  683,  682,  680,  661,  659,  652,  650,  649,  651,
1.108743 + /*   610 */   647,  645,  644,  643,  642,  641,  670,  669,  668,  666,
1.108744 + /*   620 */   665,  639,  636,  635,  631,  630,  628,
1.108745 +};
1.108746 +
1.108747 +/* The next table maps tokens into fallback tokens.  If a construct
1.108748 +** like the following:
1.108749 +** 
1.108750 +**      %fallback ID X Y Z.
1.108751 +**
1.108752 +** appears in the grammar, then ID becomes a fallback token for X, Y,
1.108753 +** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
1.108754 +** but it does not parse, the type of the token is changed to ID and
1.108755 +** the parse is retried before an error is thrown.
1.108756 +*/
1.108757 +#ifdef YYFALLBACK
1.108758 +static const YYCODETYPE yyFallback[] = {
1.108759 +    0,  /*          $ => nothing */
1.108760 +    0,  /*       SEMI => nothing */
1.108761 +   26,  /*    EXPLAIN => ID */
1.108762 +   26,  /*      QUERY => ID */
1.108763 +   26,  /*       PLAN => ID */
1.108764 +   26,  /*      BEGIN => ID */
1.108765 +    0,  /* TRANSACTION => nothing */
1.108766 +   26,  /*   DEFERRED => ID */
1.108767 +   26,  /*  IMMEDIATE => ID */
1.108768 +   26,  /*  EXCLUSIVE => ID */
1.108769 +    0,  /*     COMMIT => nothing */
1.108770 +   26,  /*        END => ID */
1.108771 +   26,  /*   ROLLBACK => ID */
1.108772 +   26,  /*  SAVEPOINT => ID */
1.108773 +   26,  /*    RELEASE => ID */
1.108774 +    0,  /*         TO => nothing */
1.108775 +    0,  /*      TABLE => nothing */
1.108776 +    0,  /*     CREATE => nothing */
1.108777 +   26,  /*         IF => ID */
1.108778 +    0,  /*        NOT => nothing */
1.108779 +    0,  /*     EXISTS => nothing */
1.108780 +   26,  /*       TEMP => ID */
1.108781 +    0,  /*         LP => nothing */
1.108782 +    0,  /*         RP => nothing */
1.108783 +    0,  /*         AS => nothing */
1.108784 +    0,  /*      COMMA => nothing */
1.108785 +    0,  /*         ID => nothing */
1.108786 +    0,  /*    INDEXED => nothing */
1.108787 +   26,  /*      ABORT => ID */
1.108788 +   26,  /*     ACTION => ID */
1.108789 +   26,  /*      AFTER => ID */
1.108790 +   26,  /*    ANALYZE => ID */
1.108791 +   26,  /*        ASC => ID */
1.108792 +   26,  /*     ATTACH => ID */
1.108793 +   26,  /*     BEFORE => ID */
1.108794 +   26,  /*         BY => ID */
1.108795 +   26,  /*    CASCADE => ID */
1.108796 +   26,  /*       CAST => ID */
1.108797 +   26,  /*   COLUMNKW => ID */
1.108798 +   26,  /*   CONFLICT => ID */
1.108799 +   26,  /*   DATABASE => ID */
1.108800 +   26,  /*       DESC => ID */
1.108801 +   26,  /*     DETACH => ID */
1.108802 +   26,  /*       EACH => ID */
1.108803 +   26,  /*       FAIL => ID */
1.108804 +   26,  /*        FOR => ID */
1.108805 +   26,  /*     IGNORE => ID */
1.108806 +   26,  /*  INITIALLY => ID */
1.108807 +   26,  /*    INSTEAD => ID */
1.108808 +   26,  /*    LIKE_KW => ID */
1.108809 +   26,  /*      MATCH => ID */
1.108810 +   26,  /*         NO => ID */
1.108811 +   26,  /*        KEY => ID */
1.108812 +   26,  /*         OF => ID */
1.108813 +   26,  /*     OFFSET => ID */
1.108814 +   26,  /*     PRAGMA => ID */
1.108815 +   26,  /*      RAISE => ID */
1.108816 +   26,  /*    REPLACE => ID */
1.108817 +   26,  /*   RESTRICT => ID */
1.108818 +   26,  /*        ROW => ID */
1.108819 +   26,  /*    TRIGGER => ID */
1.108820 +   26,  /*     VACUUM => ID */
1.108821 +   26,  /*       VIEW => ID */
1.108822 +   26,  /*    VIRTUAL => ID */
1.108823 +   26,  /*    REINDEX => ID */
1.108824 +   26,  /*     RENAME => ID */
1.108825 +   26,  /*   CTIME_KW => ID */
1.108826 +};
1.108827 +#endif /* YYFALLBACK */
1.108828 +
1.108829 +/* The following structure represents a single element of the
1.108830 +** parser's stack.  Information stored includes:
1.108831 +**
1.108832 +**   +  The state number for the parser at this level of the stack.
1.108833 +**
1.108834 +**   +  The value of the token stored at this level of the stack.
1.108835 +**      (In other words, the "major" token.)
1.108836 +**
1.108837 +**   +  The semantic value stored at this level of the stack.  This is
1.108838 +**      the information used by the action routines in the grammar.
1.108839 +**      It is sometimes called the "minor" token.
1.108840 +*/
1.108841 +struct yyStackEntry {
1.108842 +  YYACTIONTYPE stateno;  /* The state-number */
1.108843 +  YYCODETYPE major;      /* The major token value.  This is the code
1.108844 +                         ** number for the token at this stack level */
1.108845 +  YYMINORTYPE minor;     /* The user-supplied minor token value.  This
1.108846 +                         ** is the value of the token  */
1.108847 +};
1.108848 +typedef struct yyStackEntry yyStackEntry;
1.108849 +
1.108850 +/* The state of the parser is completely contained in an instance of
1.108851 +** the following structure */
1.108852 +struct yyParser {
1.108853 +  int yyidx;                    /* Index of top element in stack */
1.108854 +#ifdef YYTRACKMAXSTACKDEPTH
1.108855 +  int yyidxMax;                 /* Maximum value of yyidx */
1.108856 +#endif
1.108857 +  int yyerrcnt;                 /* Shifts left before out of the error */
1.108858 +  sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
1.108859 +#if YYSTACKDEPTH<=0
1.108860 +  int yystksz;                  /* Current side of the stack */
1.108861 +  yyStackEntry *yystack;        /* The parser's stack */
1.108862 +#else
1.108863 +  yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
1.108864 +#endif
1.108865 +};
1.108866 +typedef struct yyParser yyParser;
1.108867 +
1.108868 +#ifndef NDEBUG
1.108869 +/* #include <stdio.h> */
1.108870 +static FILE *yyTraceFILE = 0;
1.108871 +static char *yyTracePrompt = 0;
1.108872 +#endif /* NDEBUG */
1.108873 +
1.108874 +#ifndef NDEBUG
1.108875 +/* 
1.108876 +** Turn parser tracing on by giving a stream to which to write the trace
1.108877 +** and a prompt to preface each trace message.  Tracing is turned off
1.108878 +** by making either argument NULL 
1.108879 +**
1.108880 +** Inputs:
1.108881 +** <ul>
1.108882 +** <li> A FILE* to which trace output should be written.
1.108883 +**      If NULL, then tracing is turned off.
1.108884 +** <li> A prefix string written at the beginning of every
1.108885 +**      line of trace output.  If NULL, then tracing is
1.108886 +**      turned off.
1.108887 +** </ul>
1.108888 +**
1.108889 +** Outputs:
1.108890 +** None.
1.108891 +*/
1.108892 +SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
1.108893 +  yyTraceFILE = TraceFILE;
1.108894 +  yyTracePrompt = zTracePrompt;
1.108895 +  if( yyTraceFILE==0 ) yyTracePrompt = 0;
1.108896 +  else if( yyTracePrompt==0 ) yyTraceFILE = 0;
1.108897 +}
1.108898 +#endif /* NDEBUG */
1.108899 +
1.108900 +#ifndef NDEBUG
1.108901 +/* For tracing shifts, the names of all terminals and nonterminals
1.108902 +** are required.  The following table supplies these names */
1.108903 +static const char *const yyTokenName[] = { 
1.108904 +  "$",             "SEMI",          "EXPLAIN",       "QUERY",       
1.108905 +  "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
1.108906 +  "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
1.108907 +  "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
1.108908 +  "TABLE",         "CREATE",        "IF",            "NOT",         
1.108909 +  "EXISTS",        "TEMP",          "LP",            "RP",          
1.108910 +  "AS",            "COMMA",         "ID",            "INDEXED",     
1.108911 +  "ABORT",         "ACTION",        "AFTER",         "ANALYZE",     
1.108912 +  "ASC",           "ATTACH",        "BEFORE",        "BY",          
1.108913 +  "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",    
1.108914 +  "DATABASE",      "DESC",          "DETACH",        "EACH",        
1.108915 +  "FAIL",          "FOR",           "IGNORE",        "INITIALLY",   
1.108916 +  "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",          
1.108917 +  "KEY",           "OF",            "OFFSET",        "PRAGMA",      
1.108918 +  "RAISE",         "REPLACE",       "RESTRICT",      "ROW",         
1.108919 +  "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",     
1.108920 +  "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",         
1.108921 +  "OR",            "AND",           "IS",            "BETWEEN",     
1.108922 +  "IN",            "ISNULL",        "NOTNULL",       "NE",          
1.108923 +  "EQ",            "GT",            "LE",            "LT",          
1.108924 +  "GE",            "ESCAPE",        "BITAND",        "BITOR",       
1.108925 +  "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",       
1.108926 +  "STAR",          "SLASH",         "REM",           "CONCAT",      
1.108927 +  "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",     
1.108928 +  "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",     
1.108929 +  "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",    
1.108930 +  "ON",            "INSERT",        "DELETE",        "UPDATE",      
1.108931 +  "SET",           "DEFERRABLE",    "FOREIGN",       "DROP",        
1.108932 +  "UNION",         "ALL",           "EXCEPT",        "INTERSECT",   
1.108933 +  "SELECT",        "DISTINCT",      "DOT",           "FROM",        
1.108934 +  "JOIN",          "USING",         "ORDER",         "GROUP",       
1.108935 +  "HAVING",        "LIMIT",         "WHERE",         "INTO",        
1.108936 +  "VALUES",        "INTEGER",       "FLOAT",         "BLOB",        
1.108937 +  "REGISTER",      "VARIABLE",      "CASE",          "WHEN",        
1.108938 +  "THEN",          "ELSE",          "INDEX",         "ALTER",       
1.108939 +  "ADD",           "error",         "input",         "cmdlist",     
1.108940 +  "ecmd",          "explain",       "cmdx",          "cmd",         
1.108941 +  "transtype",     "trans_opt",     "nm",            "savepoint_opt",
1.108942 +  "create_table",  "create_table_args",  "createkw",      "temp",        
1.108943 +  "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
1.108944 +  "select",        "column",        "columnid",      "type",        
1.108945 +  "carglist",      "id",            "ids",           "typetoken",   
1.108946 +  "typename",      "signed",        "plus_num",      "minus_num",   
1.108947 +  "ccons",         "term",          "expr",          "onconf",      
1.108948 +  "sortorder",     "autoinc",       "idxlist_opt",   "refargs",     
1.108949 +  "defer_subclause",  "refarg",        "refact",        "init_deferred_pred_opt",
1.108950 +  "conslist",      "tconscomma",    "tcons",         "idxlist",     
1.108951 +  "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
1.108952 +  "ifexists",      "fullname",      "oneselect",     "multiselect_op",
1.108953 +  "distinct",      "selcollist",    "from",          "where_opt",   
1.108954 +  "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",   
1.108955 +  "sclp",          "as",            "seltablist",    "stl_prefix",  
1.108956 +  "joinop",        "indexed_opt",   "on_opt",        "using_opt",   
1.108957 +  "joinop2",       "inscollist",    "sortlist",      "nexprlist",   
1.108958 +  "setlist",       "insert_cmd",    "inscollist_opt",  "valuelist",   
1.108959 +  "exprlist",      "likeop",        "between_op",    "in_op",       
1.108960 +  "case_operand",  "case_exprlist",  "case_else",     "uniqueflag",  
1.108961 +  "collate",       "nmnum",         "number",        "trigger_decl",
1.108962 +  "trigger_cmd_list",  "trigger_time",  "trigger_event",  "foreach_clause",
1.108963 +  "when_clause",   "trigger_cmd",   "trnm",          "tridxby",     
1.108964 +  "database_kw_opt",  "key_opt",       "add_column_fullname",  "kwcolumn_opt",
1.108965 +  "create_vtab",   "vtabarglist",   "vtabarg",       "vtabargtoken",
1.108966 +  "lp",            "anylist",     
1.108967 +};
1.108968 +#endif /* NDEBUG */
1.108969 +
1.108970 +#ifndef NDEBUG
1.108971 +/* For tracing reduce actions, the names of all rules are required.
1.108972 +*/
1.108973 +static const char *const yyRuleName[] = {
1.108974 + /*   0 */ "input ::= cmdlist",
1.108975 + /*   1 */ "cmdlist ::= cmdlist ecmd",
1.108976 + /*   2 */ "cmdlist ::= ecmd",
1.108977 + /*   3 */ "ecmd ::= SEMI",
1.108978 + /*   4 */ "ecmd ::= explain cmdx SEMI",
1.108979 + /*   5 */ "explain ::=",
1.108980 + /*   6 */ "explain ::= EXPLAIN",
1.108981 + /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
1.108982 + /*   8 */ "cmdx ::= cmd",
1.108983 + /*   9 */ "cmd ::= BEGIN transtype trans_opt",
1.108984 + /*  10 */ "trans_opt ::=",
1.108985 + /*  11 */ "trans_opt ::= TRANSACTION",
1.108986 + /*  12 */ "trans_opt ::= TRANSACTION nm",
1.108987 + /*  13 */ "transtype ::=",
1.108988 + /*  14 */ "transtype ::= DEFERRED",
1.108989 + /*  15 */ "transtype ::= IMMEDIATE",
1.108990 + /*  16 */ "transtype ::= EXCLUSIVE",
1.108991 + /*  17 */ "cmd ::= COMMIT trans_opt",
1.108992 + /*  18 */ "cmd ::= END trans_opt",
1.108993 + /*  19 */ "cmd ::= ROLLBACK trans_opt",
1.108994 + /*  20 */ "savepoint_opt ::= SAVEPOINT",
1.108995 + /*  21 */ "savepoint_opt ::=",
1.108996 + /*  22 */ "cmd ::= SAVEPOINT nm",
1.108997 + /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
1.108998 + /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
1.108999 + /*  25 */ "cmd ::= create_table create_table_args",
1.109000 + /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
1.109001 + /*  27 */ "createkw ::= CREATE",
1.109002 + /*  28 */ "ifnotexists ::=",
1.109003 + /*  29 */ "ifnotexists ::= IF NOT EXISTS",
1.109004 + /*  30 */ "temp ::= TEMP",
1.109005 + /*  31 */ "temp ::=",
1.109006 + /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP",
1.109007 + /*  33 */ "create_table_args ::= AS select",
1.109008 + /*  34 */ "columnlist ::= columnlist COMMA column",
1.109009 + /*  35 */ "columnlist ::= column",
1.109010 + /*  36 */ "column ::= columnid type carglist",
1.109011 + /*  37 */ "columnid ::= nm",
1.109012 + /*  38 */ "id ::= ID",
1.109013 + /*  39 */ "id ::= INDEXED",
1.109014 + /*  40 */ "ids ::= ID|STRING",
1.109015 + /*  41 */ "nm ::= id",
1.109016 + /*  42 */ "nm ::= STRING",
1.109017 + /*  43 */ "nm ::= JOIN_KW",
1.109018 + /*  44 */ "type ::=",
1.109019 + /*  45 */ "type ::= typetoken",
1.109020 + /*  46 */ "typetoken ::= typename",
1.109021 + /*  47 */ "typetoken ::= typename LP signed RP",
1.109022 + /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
1.109023 + /*  49 */ "typename ::= ids",
1.109024 + /*  50 */ "typename ::= typename ids",
1.109025 + /*  51 */ "signed ::= plus_num",
1.109026 + /*  52 */ "signed ::= minus_num",
1.109027 + /*  53 */ "carglist ::= carglist ccons",
1.109028 + /*  54 */ "carglist ::=",
1.109029 + /*  55 */ "ccons ::= CONSTRAINT nm",
1.109030 + /*  56 */ "ccons ::= DEFAULT term",
1.109031 + /*  57 */ "ccons ::= DEFAULT LP expr RP",
1.109032 + /*  58 */ "ccons ::= DEFAULT PLUS term",
1.109033 + /*  59 */ "ccons ::= DEFAULT MINUS term",
1.109034 + /*  60 */ "ccons ::= DEFAULT id",
1.109035 + /*  61 */ "ccons ::= NULL onconf",
1.109036 + /*  62 */ "ccons ::= NOT NULL onconf",
1.109037 + /*  63 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
1.109038 + /*  64 */ "ccons ::= UNIQUE onconf",
1.109039 + /*  65 */ "ccons ::= CHECK LP expr RP",
1.109040 + /*  66 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
1.109041 + /*  67 */ "ccons ::= defer_subclause",
1.109042 + /*  68 */ "ccons ::= COLLATE ids",
1.109043 + /*  69 */ "autoinc ::=",
1.109044 + /*  70 */ "autoinc ::= AUTOINCR",
1.109045 + /*  71 */ "refargs ::=",
1.109046 + /*  72 */ "refargs ::= refargs refarg",
1.109047 + /*  73 */ "refarg ::= MATCH nm",
1.109048 + /*  74 */ "refarg ::= ON INSERT refact",
1.109049 + /*  75 */ "refarg ::= ON DELETE refact",
1.109050 + /*  76 */ "refarg ::= ON UPDATE refact",
1.109051 + /*  77 */ "refact ::= SET NULL",
1.109052 + /*  78 */ "refact ::= SET DEFAULT",
1.109053 + /*  79 */ "refact ::= CASCADE",
1.109054 + /*  80 */ "refact ::= RESTRICT",
1.109055 + /*  81 */ "refact ::= NO ACTION",
1.109056 + /*  82 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
1.109057 + /*  83 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
1.109058 + /*  84 */ "init_deferred_pred_opt ::=",
1.109059 + /*  85 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
1.109060 + /*  86 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
1.109061 + /*  87 */ "conslist_opt ::=",
1.109062 + /*  88 */ "conslist_opt ::= COMMA conslist",
1.109063 + /*  89 */ "conslist ::= conslist tconscomma tcons",
1.109064 + /*  90 */ "conslist ::= tcons",
1.109065 + /*  91 */ "tconscomma ::= COMMA",
1.109066 + /*  92 */ "tconscomma ::=",
1.109067 + /*  93 */ "tcons ::= CONSTRAINT nm",
1.109068 + /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
1.109069 + /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
1.109070 + /*  96 */ "tcons ::= CHECK LP expr RP onconf",
1.109071 + /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
1.109072 + /*  98 */ "defer_subclause_opt ::=",
1.109073 + /*  99 */ "defer_subclause_opt ::= defer_subclause",
1.109074 + /* 100 */ "onconf ::=",
1.109075 + /* 101 */ "onconf ::= ON CONFLICT resolvetype",
1.109076 + /* 102 */ "orconf ::=",
1.109077 + /* 103 */ "orconf ::= OR resolvetype",
1.109078 + /* 104 */ "resolvetype ::= raisetype",
1.109079 + /* 105 */ "resolvetype ::= IGNORE",
1.109080 + /* 106 */ "resolvetype ::= REPLACE",
1.109081 + /* 107 */ "cmd ::= DROP TABLE ifexists fullname",
1.109082 + /* 108 */ "ifexists ::= IF EXISTS",
1.109083 + /* 109 */ "ifexists ::=",
1.109084 + /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
1.109085 + /* 111 */ "cmd ::= DROP VIEW ifexists fullname",
1.109086 + /* 112 */ "cmd ::= select",
1.109087 + /* 113 */ "select ::= oneselect",
1.109088 + /* 114 */ "select ::= select multiselect_op oneselect",
1.109089 + /* 115 */ "multiselect_op ::= UNION",
1.109090 + /* 116 */ "multiselect_op ::= UNION ALL",
1.109091 + /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
1.109092 + /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
1.109093 + /* 119 */ "distinct ::= DISTINCT",
1.109094 + /* 120 */ "distinct ::= ALL",
1.109095 + /* 121 */ "distinct ::=",
1.109096 + /* 122 */ "sclp ::= selcollist COMMA",
1.109097 + /* 123 */ "sclp ::=",
1.109098 + /* 124 */ "selcollist ::= sclp expr as",
1.109099 + /* 125 */ "selcollist ::= sclp STAR",
1.109100 + /* 126 */ "selcollist ::= sclp nm DOT STAR",
1.109101 + /* 127 */ "as ::= AS nm",
1.109102 + /* 128 */ "as ::= ids",
1.109103 + /* 129 */ "as ::=",
1.109104 + /* 130 */ "from ::=",
1.109105 + /* 131 */ "from ::= FROM seltablist",
1.109106 + /* 132 */ "stl_prefix ::= seltablist joinop",
1.109107 + /* 133 */ "stl_prefix ::=",
1.109108 + /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
1.109109 + /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
1.109110 + /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
1.109111 + /* 137 */ "dbnm ::=",
1.109112 + /* 138 */ "dbnm ::= DOT nm",
1.109113 + /* 139 */ "fullname ::= nm dbnm",
1.109114 + /* 140 */ "joinop ::= COMMA|JOIN",
1.109115 + /* 141 */ "joinop ::= JOIN_KW JOIN",
1.109116 + /* 142 */ "joinop ::= JOIN_KW nm JOIN",
1.109117 + /* 143 */ "joinop ::= JOIN_KW nm nm JOIN",
1.109118 + /* 144 */ "on_opt ::= ON expr",
1.109119 + /* 145 */ "on_opt ::=",
1.109120 + /* 146 */ "indexed_opt ::=",
1.109121 + /* 147 */ "indexed_opt ::= INDEXED BY nm",
1.109122 + /* 148 */ "indexed_opt ::= NOT INDEXED",
1.109123 + /* 149 */ "using_opt ::= USING LP inscollist RP",
1.109124 + /* 150 */ "using_opt ::=",
1.109125 + /* 151 */ "orderby_opt ::=",
1.109126 + /* 152 */ "orderby_opt ::= ORDER BY sortlist",
1.109127 + /* 153 */ "sortlist ::= sortlist COMMA expr sortorder",
1.109128 + /* 154 */ "sortlist ::= expr sortorder",
1.109129 + /* 155 */ "sortorder ::= ASC",
1.109130 + /* 156 */ "sortorder ::= DESC",
1.109131 + /* 157 */ "sortorder ::=",
1.109132 + /* 158 */ "groupby_opt ::=",
1.109133 + /* 159 */ "groupby_opt ::= GROUP BY nexprlist",
1.109134 + /* 160 */ "having_opt ::=",
1.109135 + /* 161 */ "having_opt ::= HAVING expr",
1.109136 + /* 162 */ "limit_opt ::=",
1.109137 + /* 163 */ "limit_opt ::= LIMIT expr",
1.109138 + /* 164 */ "limit_opt ::= LIMIT expr OFFSET expr",
1.109139 + /* 165 */ "limit_opt ::= LIMIT expr COMMA expr",
1.109140 + /* 166 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
1.109141 + /* 167 */ "where_opt ::=",
1.109142 + /* 168 */ "where_opt ::= WHERE expr",
1.109143 + /* 169 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
1.109144 + /* 170 */ "setlist ::= setlist COMMA nm EQ expr",
1.109145 + /* 171 */ "setlist ::= nm EQ expr",
1.109146 + /* 172 */ "cmd ::= insert_cmd INTO fullname inscollist_opt valuelist",
1.109147 + /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
1.109148 + /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
1.109149 + /* 175 */ "insert_cmd ::= INSERT orconf",
1.109150 + /* 176 */ "insert_cmd ::= REPLACE",
1.109151 + /* 177 */ "valuelist ::= VALUES LP nexprlist RP",
1.109152 + /* 178 */ "valuelist ::= valuelist COMMA LP exprlist RP",
1.109153 + /* 179 */ "inscollist_opt ::=",
1.109154 + /* 180 */ "inscollist_opt ::= LP inscollist RP",
1.109155 + /* 181 */ "inscollist ::= inscollist COMMA nm",
1.109156 + /* 182 */ "inscollist ::= nm",
1.109157 + /* 183 */ "expr ::= term",
1.109158 + /* 184 */ "expr ::= LP expr RP",
1.109159 + /* 185 */ "term ::= NULL",
1.109160 + /* 186 */ "expr ::= id",
1.109161 + /* 187 */ "expr ::= JOIN_KW",
1.109162 + /* 188 */ "expr ::= nm DOT nm",
1.109163 + /* 189 */ "expr ::= nm DOT nm DOT nm",
1.109164 + /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
1.109165 + /* 191 */ "term ::= STRING",
1.109166 + /* 192 */ "expr ::= REGISTER",
1.109167 + /* 193 */ "expr ::= VARIABLE",
1.109168 + /* 194 */ "expr ::= expr COLLATE ids",
1.109169 + /* 195 */ "expr ::= CAST LP expr AS typetoken RP",
1.109170 + /* 196 */ "expr ::= ID LP distinct exprlist RP",
1.109171 + /* 197 */ "expr ::= ID LP STAR RP",
1.109172 + /* 198 */ "term ::= CTIME_KW",
1.109173 + /* 199 */ "expr ::= expr AND expr",
1.109174 + /* 200 */ "expr ::= expr OR expr",
1.109175 + /* 201 */ "expr ::= expr LT|GT|GE|LE expr",
1.109176 + /* 202 */ "expr ::= expr EQ|NE expr",
1.109177 + /* 203 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
1.109178 + /* 204 */ "expr ::= expr PLUS|MINUS expr",
1.109179 + /* 205 */ "expr ::= expr STAR|SLASH|REM expr",
1.109180 + /* 206 */ "expr ::= expr CONCAT expr",
1.109181 + /* 207 */ "likeop ::= LIKE_KW",
1.109182 + /* 208 */ "likeop ::= NOT LIKE_KW",
1.109183 + /* 209 */ "likeop ::= MATCH",
1.109184 + /* 210 */ "likeop ::= NOT MATCH",
1.109185 + /* 211 */ "expr ::= expr likeop expr",
1.109186 + /* 212 */ "expr ::= expr likeop expr ESCAPE expr",
1.109187 + /* 213 */ "expr ::= expr ISNULL|NOTNULL",
1.109188 + /* 214 */ "expr ::= expr NOT NULL",
1.109189 + /* 215 */ "expr ::= expr IS expr",
1.109190 + /* 216 */ "expr ::= expr IS NOT expr",
1.109191 + /* 217 */ "expr ::= NOT expr",
1.109192 + /* 218 */ "expr ::= BITNOT expr",
1.109193 + /* 219 */ "expr ::= MINUS expr",
1.109194 + /* 220 */ "expr ::= PLUS expr",
1.109195 + /* 221 */ "between_op ::= BETWEEN",
1.109196 + /* 222 */ "between_op ::= NOT BETWEEN",
1.109197 + /* 223 */ "expr ::= expr between_op expr AND expr",
1.109198 + /* 224 */ "in_op ::= IN",
1.109199 + /* 225 */ "in_op ::= NOT IN",
1.109200 + /* 226 */ "expr ::= expr in_op LP exprlist RP",
1.109201 + /* 227 */ "expr ::= LP select RP",
1.109202 + /* 228 */ "expr ::= expr in_op LP select RP",
1.109203 + /* 229 */ "expr ::= expr in_op nm dbnm",
1.109204 + /* 230 */ "expr ::= EXISTS LP select RP",
1.109205 + /* 231 */ "expr ::= CASE case_operand case_exprlist case_else END",
1.109206 + /* 232 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
1.109207 + /* 233 */ "case_exprlist ::= WHEN expr THEN expr",
1.109208 + /* 234 */ "case_else ::= ELSE expr",
1.109209 + /* 235 */ "case_else ::=",
1.109210 + /* 236 */ "case_operand ::= expr",
1.109211 + /* 237 */ "case_operand ::=",
1.109212 + /* 238 */ "exprlist ::= nexprlist",
1.109213 + /* 239 */ "exprlist ::=",
1.109214 + /* 240 */ "nexprlist ::= nexprlist COMMA expr",
1.109215 + /* 241 */ "nexprlist ::= expr",
1.109216 + /* 242 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
1.109217 + /* 243 */ "uniqueflag ::= UNIQUE",
1.109218 + /* 244 */ "uniqueflag ::=",
1.109219 + /* 245 */ "idxlist_opt ::=",
1.109220 + /* 246 */ "idxlist_opt ::= LP idxlist RP",
1.109221 + /* 247 */ "idxlist ::= idxlist COMMA nm collate sortorder",
1.109222 + /* 248 */ "idxlist ::= nm collate sortorder",
1.109223 + /* 249 */ "collate ::=",
1.109224 + /* 250 */ "collate ::= COLLATE ids",
1.109225 + /* 251 */ "cmd ::= DROP INDEX ifexists fullname",
1.109226 + /* 252 */ "cmd ::= VACUUM",
1.109227 + /* 253 */ "cmd ::= VACUUM nm",
1.109228 + /* 254 */ "cmd ::= PRAGMA nm dbnm",
1.109229 + /* 255 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
1.109230 + /* 256 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
1.109231 + /* 257 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
1.109232 + /* 258 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
1.109233 + /* 259 */ "nmnum ::= plus_num",
1.109234 + /* 260 */ "nmnum ::= nm",
1.109235 + /* 261 */ "nmnum ::= ON",
1.109236 + /* 262 */ "nmnum ::= DELETE",
1.109237 + /* 263 */ "nmnum ::= DEFAULT",
1.109238 + /* 264 */ "plus_num ::= PLUS number",
1.109239 + /* 265 */ "plus_num ::= number",
1.109240 + /* 266 */ "minus_num ::= MINUS number",
1.109241 + /* 267 */ "number ::= INTEGER|FLOAT",
1.109242 + /* 268 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
1.109243 + /* 269 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
1.109244 + /* 270 */ "trigger_time ::= BEFORE",
1.109245 + /* 271 */ "trigger_time ::= AFTER",
1.109246 + /* 272 */ "trigger_time ::= INSTEAD OF",
1.109247 + /* 273 */ "trigger_time ::=",
1.109248 + /* 274 */ "trigger_event ::= DELETE|INSERT",
1.109249 + /* 275 */ "trigger_event ::= UPDATE",
1.109250 + /* 276 */ "trigger_event ::= UPDATE OF inscollist",
1.109251 + /* 277 */ "foreach_clause ::=",
1.109252 + /* 278 */ "foreach_clause ::= FOR EACH ROW",
1.109253 + /* 279 */ "when_clause ::=",
1.109254 + /* 280 */ "when_clause ::= WHEN expr",
1.109255 + /* 281 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
1.109256 + /* 282 */ "trigger_cmd_list ::= trigger_cmd SEMI",
1.109257 + /* 283 */ "trnm ::= nm",
1.109258 + /* 284 */ "trnm ::= nm DOT nm",
1.109259 + /* 285 */ "tridxby ::=",
1.109260 + /* 286 */ "tridxby ::= INDEXED BY nm",
1.109261 + /* 287 */ "tridxby ::= NOT INDEXED",
1.109262 + /* 288 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
1.109263 + /* 289 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt valuelist",
1.109264 + /* 290 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
1.109265 + /* 291 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
1.109266 + /* 292 */ "trigger_cmd ::= select",
1.109267 + /* 293 */ "expr ::= RAISE LP IGNORE RP",
1.109268 + /* 294 */ "expr ::= RAISE LP raisetype COMMA nm RP",
1.109269 + /* 295 */ "raisetype ::= ROLLBACK",
1.109270 + /* 296 */ "raisetype ::= ABORT",
1.109271 + /* 297 */ "raisetype ::= FAIL",
1.109272 + /* 298 */ "cmd ::= DROP TRIGGER ifexists fullname",
1.109273 + /* 299 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
1.109274 + /* 300 */ "cmd ::= DETACH database_kw_opt expr",
1.109275 + /* 301 */ "key_opt ::=",
1.109276 + /* 302 */ "key_opt ::= KEY expr",
1.109277 + /* 303 */ "database_kw_opt ::= DATABASE",
1.109278 + /* 304 */ "database_kw_opt ::=",
1.109279 + /* 305 */ "cmd ::= REINDEX",
1.109280 + /* 306 */ "cmd ::= REINDEX nm dbnm",
1.109281 + /* 307 */ "cmd ::= ANALYZE",
1.109282 + /* 308 */ "cmd ::= ANALYZE nm dbnm",
1.109283 + /* 309 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
1.109284 + /* 310 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
1.109285 + /* 311 */ "add_column_fullname ::= fullname",
1.109286 + /* 312 */ "kwcolumn_opt ::=",
1.109287 + /* 313 */ "kwcolumn_opt ::= COLUMNKW",
1.109288 + /* 314 */ "cmd ::= create_vtab",
1.109289 + /* 315 */ "cmd ::= create_vtab LP vtabarglist RP",
1.109290 + /* 316 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
1.109291 + /* 317 */ "vtabarglist ::= vtabarg",
1.109292 + /* 318 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
1.109293 + /* 319 */ "vtabarg ::=",
1.109294 + /* 320 */ "vtabarg ::= vtabarg vtabargtoken",
1.109295 + /* 321 */ "vtabargtoken ::= ANY",
1.109296 + /* 322 */ "vtabargtoken ::= lp anylist RP",
1.109297 + /* 323 */ "lp ::= LP",
1.109298 + /* 324 */ "anylist ::=",
1.109299 + /* 325 */ "anylist ::= anylist LP anylist RP",
1.109300 + /* 326 */ "anylist ::= anylist ANY",
1.109301 +};
1.109302 +#endif /* NDEBUG */
1.109303 +
1.109304 +
1.109305 +#if YYSTACKDEPTH<=0
1.109306 +/*
1.109307 +** Try to increase the size of the parser stack.
1.109308 +*/
1.109309 +static void yyGrowStack(yyParser *p){
1.109310 +  int newSize;
1.109311 +  yyStackEntry *pNew;
1.109312 +
1.109313 +  newSize = p->yystksz*2 + 100;
1.109314 +  pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
1.109315 +  if( pNew ){
1.109316 +    p->yystack = pNew;
1.109317 +    p->yystksz = newSize;
1.109318 +#ifndef NDEBUG
1.109319 +    if( yyTraceFILE ){
1.109320 +      fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
1.109321 +              yyTracePrompt, p->yystksz);
1.109322 +    }
1.109323 +#endif
1.109324 +  }
1.109325 +}
1.109326 +#endif
1.109327 +
1.109328 +/* 
1.109329 +** This function allocates a new parser.
1.109330 +** The only argument is a pointer to a function which works like
1.109331 +** malloc.
1.109332 +**
1.109333 +** Inputs:
1.109334 +** A pointer to the function used to allocate memory.
1.109335 +**
1.109336 +** Outputs:
1.109337 +** A pointer to a parser.  This pointer is used in subsequent calls
1.109338 +** to sqlite3Parser and sqlite3ParserFree.
1.109339 +*/
1.109340 +SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
1.109341 +  yyParser *pParser;
1.109342 +  pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
1.109343 +  if( pParser ){
1.109344 +    pParser->yyidx = -1;
1.109345 +#ifdef YYTRACKMAXSTACKDEPTH
1.109346 +    pParser->yyidxMax = 0;
1.109347 +#endif
1.109348 +#if YYSTACKDEPTH<=0
1.109349 +    pParser->yystack = NULL;
1.109350 +    pParser->yystksz = 0;
1.109351 +    yyGrowStack(pParser);
1.109352 +#endif
1.109353 +  }
1.109354 +  return pParser;
1.109355 +}
1.109356 +
1.109357 +/* The following function deletes the value associated with a
1.109358 +** symbol.  The symbol can be either a terminal or nonterminal.
1.109359 +** "yymajor" is the symbol code, and "yypminor" is a pointer to
1.109360 +** the value.
1.109361 +*/
1.109362 +static void yy_destructor(
1.109363 +  yyParser *yypParser,    /* The parser */
1.109364 +  YYCODETYPE yymajor,     /* Type code for object to destroy */
1.109365 +  YYMINORTYPE *yypminor   /* The object to be destroyed */
1.109366 +){
1.109367 +  sqlite3ParserARG_FETCH;
1.109368 +  switch( yymajor ){
1.109369 +    /* Here is inserted the actions which take place when a
1.109370 +    ** terminal or non-terminal is destroyed.  This can happen
1.109371 +    ** when the symbol is popped from the stack during a
1.109372 +    ** reduce or during error processing or when a parser is 
1.109373 +    ** being destroyed before it is finished parsing.
1.109374 +    **
1.109375 +    ** Note: during a reduce, the only symbols destroyed are those
1.109376 +    ** which appear on the RHS of the rule, but which are not used
1.109377 +    ** inside the C code.
1.109378 +    */
1.109379 +    case 160: /* select */
1.109380 +    case 194: /* oneselect */
1.109381 +{
1.109382 +sqlite3SelectDelete(pParse->db, (yypminor->yy159));
1.109383 +}
1.109384 +      break;
1.109385 +    case 173: /* term */
1.109386 +    case 174: /* expr */
1.109387 +{
1.109388 +sqlite3ExprDelete(pParse->db, (yypminor->yy342).pExpr);
1.109389 +}
1.109390 +      break;
1.109391 +    case 178: /* idxlist_opt */
1.109392 +    case 187: /* idxlist */
1.109393 +    case 197: /* selcollist */
1.109394 +    case 200: /* groupby_opt */
1.109395 +    case 202: /* orderby_opt */
1.109396 +    case 204: /* sclp */
1.109397 +    case 214: /* sortlist */
1.109398 +    case 215: /* nexprlist */
1.109399 +    case 216: /* setlist */
1.109400 +    case 220: /* exprlist */
1.109401 +    case 225: /* case_exprlist */
1.109402 +{
1.109403 +sqlite3ExprListDelete(pParse->db, (yypminor->yy442));
1.109404 +}
1.109405 +      break;
1.109406 +    case 193: /* fullname */
1.109407 +    case 198: /* from */
1.109408 +    case 206: /* seltablist */
1.109409 +    case 207: /* stl_prefix */
1.109410 +{
1.109411 +sqlite3SrcListDelete(pParse->db, (yypminor->yy347));
1.109412 +}
1.109413 +      break;
1.109414 +    case 199: /* where_opt */
1.109415 +    case 201: /* having_opt */
1.109416 +    case 210: /* on_opt */
1.109417 +    case 224: /* case_operand */
1.109418 +    case 226: /* case_else */
1.109419 +    case 236: /* when_clause */
1.109420 +    case 241: /* key_opt */
1.109421 +{
1.109422 +sqlite3ExprDelete(pParse->db, (yypminor->yy122));
1.109423 +}
1.109424 +      break;
1.109425 +    case 211: /* using_opt */
1.109426 +    case 213: /* inscollist */
1.109427 +    case 218: /* inscollist_opt */
1.109428 +{
1.109429 +sqlite3IdListDelete(pParse->db, (yypminor->yy180));
1.109430 +}
1.109431 +      break;
1.109432 +    case 219: /* valuelist */
1.109433 +{
1.109434 +
1.109435 +  sqlite3ExprListDelete(pParse->db, (yypminor->yy487).pList);
1.109436 +  sqlite3SelectDelete(pParse->db, (yypminor->yy487).pSelect);
1.109437 +
1.109438 +}
1.109439 +      break;
1.109440 +    case 232: /* trigger_cmd_list */
1.109441 +    case 237: /* trigger_cmd */
1.109442 +{
1.109443 +sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy327));
1.109444 +}
1.109445 +      break;
1.109446 +    case 234: /* trigger_event */
1.109447 +{
1.109448 +sqlite3IdListDelete(pParse->db, (yypminor->yy410).b);
1.109449 +}
1.109450 +      break;
1.109451 +    default:  break;   /* If no destructor action specified: do nothing */
1.109452 +  }
1.109453 +}
1.109454 +
1.109455 +/*
1.109456 +** Pop the parser's stack once.
1.109457 +**
1.109458 +** If there is a destructor routine associated with the token which
1.109459 +** is popped from the stack, then call it.
1.109460 +**
1.109461 +** Return the major token number for the symbol popped.
1.109462 +*/
1.109463 +static int yy_pop_parser_stack(yyParser *pParser){
1.109464 +  YYCODETYPE yymajor;
1.109465 +  yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
1.109466 +
1.109467 +  /* There is no mechanism by which the parser stack can be popped below
1.109468 +  ** empty in SQLite.  */
1.109469 +  if( NEVER(pParser->yyidx<0) ) return 0;
1.109470 +#ifndef NDEBUG
1.109471 +  if( yyTraceFILE && pParser->yyidx>=0 ){
1.109472 +    fprintf(yyTraceFILE,"%sPopping %s\n",
1.109473 +      yyTracePrompt,
1.109474 +      yyTokenName[yytos->major]);
1.109475 +  }
1.109476 +#endif
1.109477 +  yymajor = yytos->major;
1.109478 +  yy_destructor(pParser, yymajor, &yytos->minor);
1.109479 +  pParser->yyidx--;
1.109480 +  return yymajor;
1.109481 +}
1.109482 +
1.109483 +/* 
1.109484 +** Deallocate and destroy a parser.  Destructors are all called for
1.109485 +** all stack elements before shutting the parser down.
1.109486 +**
1.109487 +** Inputs:
1.109488 +** <ul>
1.109489 +** <li>  A pointer to the parser.  This should be a pointer
1.109490 +**       obtained from sqlite3ParserAlloc.
1.109491 +** <li>  A pointer to a function used to reclaim memory obtained
1.109492 +**       from malloc.
1.109493 +** </ul>
1.109494 +*/
1.109495 +SQLITE_PRIVATE void sqlite3ParserFree(
1.109496 +  void *p,                    /* The parser to be deleted */
1.109497 +  void (*freeProc)(void*)     /* Function used to reclaim memory */
1.109498 +){
1.109499 +  yyParser *pParser = (yyParser*)p;
1.109500 +  /* In SQLite, we never try to destroy a parser that was not successfully
1.109501 +  ** created in the first place. */
1.109502 +  if( NEVER(pParser==0) ) return;
1.109503 +  while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
1.109504 +#if YYSTACKDEPTH<=0
1.109505 +  free(pParser->yystack);
1.109506 +#endif
1.109507 +  (*freeProc)((void*)pParser);
1.109508 +}
1.109509 +
1.109510 +/*
1.109511 +** Return the peak depth of the stack for a parser.
1.109512 +*/
1.109513 +#ifdef YYTRACKMAXSTACKDEPTH
1.109514 +SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
1.109515 +  yyParser *pParser = (yyParser*)p;
1.109516 +  return pParser->yyidxMax;
1.109517 +}
1.109518 +#endif
1.109519 +
1.109520 +/*
1.109521 +** Find the appropriate action for a parser given the terminal
1.109522 +** look-ahead token iLookAhead.
1.109523 +**
1.109524 +** If the look-ahead token is YYNOCODE, then check to see if the action is
1.109525 +** independent of the look-ahead.  If it is, return the action, otherwise
1.109526 +** return YY_NO_ACTION.
1.109527 +*/
1.109528 +static int yy_find_shift_action(
1.109529 +  yyParser *pParser,        /* The parser */
1.109530 +  YYCODETYPE iLookAhead     /* The look-ahead token */
1.109531 +){
1.109532 +  int i;
1.109533 +  int stateno = pParser->yystack[pParser->yyidx].stateno;
1.109534 + 
1.109535 +  if( stateno>YY_SHIFT_COUNT
1.109536 +   || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
1.109537 +    return yy_default[stateno];
1.109538 +  }
1.109539 +  assert( iLookAhead!=YYNOCODE );
1.109540 +  i += iLookAhead;
1.109541 +  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
1.109542 +    if( iLookAhead>0 ){
1.109543 +#ifdef YYFALLBACK
1.109544 +      YYCODETYPE iFallback;            /* Fallback token */
1.109545 +      if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
1.109546 +             && (iFallback = yyFallback[iLookAhead])!=0 ){
1.109547 +#ifndef NDEBUG
1.109548 +        if( yyTraceFILE ){
1.109549 +          fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
1.109550 +             yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
1.109551 +        }
1.109552 +#endif
1.109553 +        return yy_find_shift_action(pParser, iFallback);
1.109554 +      }
1.109555 +#endif
1.109556 +#ifdef YYWILDCARD
1.109557 +      {
1.109558 +        int j = i - iLookAhead + YYWILDCARD;
1.109559 +        if( 
1.109560 +#if YY_SHIFT_MIN+YYWILDCARD<0
1.109561 +          j>=0 &&
1.109562 +#endif
1.109563 +#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
1.109564 +          j<YY_ACTTAB_COUNT &&
1.109565 +#endif
1.109566 +          yy_lookahead[j]==YYWILDCARD
1.109567 +        ){
1.109568 +#ifndef NDEBUG
1.109569 +          if( yyTraceFILE ){
1.109570 +            fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
1.109571 +               yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
1.109572 +          }
1.109573 +#endif /* NDEBUG */
1.109574 +          return yy_action[j];
1.109575 +        }
1.109576 +      }
1.109577 +#endif /* YYWILDCARD */
1.109578 +    }
1.109579 +    return yy_default[stateno];
1.109580 +  }else{
1.109581 +    return yy_action[i];
1.109582 +  }
1.109583 +}
1.109584 +
1.109585 +/*
1.109586 +** Find the appropriate action for a parser given the non-terminal
1.109587 +** look-ahead token iLookAhead.
1.109588 +**
1.109589 +** If the look-ahead token is YYNOCODE, then check to see if the action is
1.109590 +** independent of the look-ahead.  If it is, return the action, otherwise
1.109591 +** return YY_NO_ACTION.
1.109592 +*/
1.109593 +static int yy_find_reduce_action(
1.109594 +  int stateno,              /* Current state number */
1.109595 +  YYCODETYPE iLookAhead     /* The look-ahead token */
1.109596 +){
1.109597 +  int i;
1.109598 +#ifdef YYERRORSYMBOL
1.109599 +  if( stateno>YY_REDUCE_COUNT ){
1.109600 +    return yy_default[stateno];
1.109601 +  }
1.109602 +#else
1.109603 +  assert( stateno<=YY_REDUCE_COUNT );
1.109604 +#endif
1.109605 +  i = yy_reduce_ofst[stateno];
1.109606 +  assert( i!=YY_REDUCE_USE_DFLT );
1.109607 +  assert( iLookAhead!=YYNOCODE );
1.109608 +  i += iLookAhead;
1.109609 +#ifdef YYERRORSYMBOL
1.109610 +  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
1.109611 +    return yy_default[stateno];
1.109612 +  }
1.109613 +#else
1.109614 +  assert( i>=0 && i<YY_ACTTAB_COUNT );
1.109615 +  assert( yy_lookahead[i]==iLookAhead );
1.109616 +#endif
1.109617 +  return yy_action[i];
1.109618 +}
1.109619 +
1.109620 +/*
1.109621 +** The following routine is called if the stack overflows.
1.109622 +*/
1.109623 +static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
1.109624 +   sqlite3ParserARG_FETCH;
1.109625 +   yypParser->yyidx--;
1.109626 +#ifndef NDEBUG
1.109627 +   if( yyTraceFILE ){
1.109628 +     fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
1.109629 +   }
1.109630 +#endif
1.109631 +   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
1.109632 +   /* Here code is inserted which will execute if the parser
1.109633 +   ** stack every overflows */
1.109634 +
1.109635 +  UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
1.109636 +  sqlite3ErrorMsg(pParse, "parser stack overflow");
1.109637 +   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
1.109638 +}
1.109639 +
1.109640 +/*
1.109641 +** Perform a shift action.
1.109642 +*/
1.109643 +static void yy_shift(
1.109644 +  yyParser *yypParser,          /* The parser to be shifted */
1.109645 +  int yyNewState,               /* The new state to shift in */
1.109646 +  int yyMajor,                  /* The major token to shift in */
1.109647 +  YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
1.109648 +){
1.109649 +  yyStackEntry *yytos;
1.109650 +  yypParser->yyidx++;
1.109651 +#ifdef YYTRACKMAXSTACKDEPTH
1.109652 +  if( yypParser->yyidx>yypParser->yyidxMax ){
1.109653 +    yypParser->yyidxMax = yypParser->yyidx;
1.109654 +  }
1.109655 +#endif
1.109656 +#if YYSTACKDEPTH>0 
1.109657 +  if( yypParser->yyidx>=YYSTACKDEPTH ){
1.109658 +    yyStackOverflow(yypParser, yypMinor);
1.109659 +    return;
1.109660 +  }
1.109661 +#else
1.109662 +  if( yypParser->yyidx>=yypParser->yystksz ){
1.109663 +    yyGrowStack(yypParser);
1.109664 +    if( yypParser->yyidx>=yypParser->yystksz ){
1.109665 +      yyStackOverflow(yypParser, yypMinor);
1.109666 +      return;
1.109667 +    }
1.109668 +  }
1.109669 +#endif
1.109670 +  yytos = &yypParser->yystack[yypParser->yyidx];
1.109671 +  yytos->stateno = (YYACTIONTYPE)yyNewState;
1.109672 +  yytos->major = (YYCODETYPE)yyMajor;
1.109673 +  yytos->minor = *yypMinor;
1.109674 +#ifndef NDEBUG
1.109675 +  if( yyTraceFILE && yypParser->yyidx>0 ){
1.109676 +    int i;
1.109677 +    fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
1.109678 +    fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
1.109679 +    for(i=1; i<=yypParser->yyidx; i++)
1.109680 +      fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
1.109681 +    fprintf(yyTraceFILE,"\n");
1.109682 +  }
1.109683 +#endif
1.109684 +}
1.109685 +
1.109686 +/* The following table contains information about every rule that
1.109687 +** is used during the reduce.
1.109688 +*/
1.109689 +static const struct {
1.109690 +  YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
1.109691 +  unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
1.109692 +} yyRuleInfo[] = {
1.109693 +  { 142, 1 },
1.109694 +  { 143, 2 },
1.109695 +  { 143, 1 },
1.109696 +  { 144, 1 },
1.109697 +  { 144, 3 },
1.109698 +  { 145, 0 },
1.109699 +  { 145, 1 },
1.109700 +  { 145, 3 },
1.109701 +  { 146, 1 },
1.109702 +  { 147, 3 },
1.109703 +  { 149, 0 },
1.109704 +  { 149, 1 },
1.109705 +  { 149, 2 },
1.109706 +  { 148, 0 },
1.109707 +  { 148, 1 },
1.109708 +  { 148, 1 },
1.109709 +  { 148, 1 },
1.109710 +  { 147, 2 },
1.109711 +  { 147, 2 },
1.109712 +  { 147, 2 },
1.109713 +  { 151, 1 },
1.109714 +  { 151, 0 },
1.109715 +  { 147, 2 },
1.109716 +  { 147, 3 },
1.109717 +  { 147, 5 },
1.109718 +  { 147, 2 },
1.109719 +  { 152, 6 },
1.109720 +  { 154, 1 },
1.109721 +  { 156, 0 },
1.109722 +  { 156, 3 },
1.109723 +  { 155, 1 },
1.109724 +  { 155, 0 },
1.109725 +  { 153, 4 },
1.109726 +  { 153, 2 },
1.109727 +  { 158, 3 },
1.109728 +  { 158, 1 },
1.109729 +  { 161, 3 },
1.109730 +  { 162, 1 },
1.109731 +  { 165, 1 },
1.109732 +  { 165, 1 },
1.109733 +  { 166, 1 },
1.109734 +  { 150, 1 },
1.109735 +  { 150, 1 },
1.109736 +  { 150, 1 },
1.109737 +  { 163, 0 },
1.109738 +  { 163, 1 },
1.109739 +  { 167, 1 },
1.109740 +  { 167, 4 },
1.109741 +  { 167, 6 },
1.109742 +  { 168, 1 },
1.109743 +  { 168, 2 },
1.109744 +  { 169, 1 },
1.109745 +  { 169, 1 },
1.109746 +  { 164, 2 },
1.109747 +  { 164, 0 },
1.109748 +  { 172, 2 },
1.109749 +  { 172, 2 },
1.109750 +  { 172, 4 },
1.109751 +  { 172, 3 },
1.109752 +  { 172, 3 },
1.109753 +  { 172, 2 },
1.109754 +  { 172, 2 },
1.109755 +  { 172, 3 },
1.109756 +  { 172, 5 },
1.109757 +  { 172, 2 },
1.109758 +  { 172, 4 },
1.109759 +  { 172, 4 },
1.109760 +  { 172, 1 },
1.109761 +  { 172, 2 },
1.109762 +  { 177, 0 },
1.109763 +  { 177, 1 },
1.109764 +  { 179, 0 },
1.109765 +  { 179, 2 },
1.109766 +  { 181, 2 },
1.109767 +  { 181, 3 },
1.109768 +  { 181, 3 },
1.109769 +  { 181, 3 },
1.109770 +  { 182, 2 },
1.109771 +  { 182, 2 },
1.109772 +  { 182, 1 },
1.109773 +  { 182, 1 },
1.109774 +  { 182, 2 },
1.109775 +  { 180, 3 },
1.109776 +  { 180, 2 },
1.109777 +  { 183, 0 },
1.109778 +  { 183, 2 },
1.109779 +  { 183, 2 },
1.109780 +  { 159, 0 },
1.109781 +  { 159, 2 },
1.109782 +  { 184, 3 },
1.109783 +  { 184, 1 },
1.109784 +  { 185, 1 },
1.109785 +  { 185, 0 },
1.109786 +  { 186, 2 },
1.109787 +  { 186, 7 },
1.109788 +  { 186, 5 },
1.109789 +  { 186, 5 },
1.109790 +  { 186, 10 },
1.109791 +  { 188, 0 },
1.109792 +  { 188, 1 },
1.109793 +  { 175, 0 },
1.109794 +  { 175, 3 },
1.109795 +  { 189, 0 },
1.109796 +  { 189, 2 },
1.109797 +  { 190, 1 },
1.109798 +  { 190, 1 },
1.109799 +  { 190, 1 },
1.109800 +  { 147, 4 },
1.109801 +  { 192, 2 },
1.109802 +  { 192, 0 },
1.109803 +  { 147, 8 },
1.109804 +  { 147, 4 },
1.109805 +  { 147, 1 },
1.109806 +  { 160, 1 },
1.109807 +  { 160, 3 },
1.109808 +  { 195, 1 },
1.109809 +  { 195, 2 },
1.109810 +  { 195, 1 },
1.109811 +  { 194, 9 },
1.109812 +  { 196, 1 },
1.109813 +  { 196, 1 },
1.109814 +  { 196, 0 },
1.109815 +  { 204, 2 },
1.109816 +  { 204, 0 },
1.109817 +  { 197, 3 },
1.109818 +  { 197, 2 },
1.109819 +  { 197, 4 },
1.109820 +  { 205, 2 },
1.109821 +  { 205, 1 },
1.109822 +  { 205, 0 },
1.109823 +  { 198, 0 },
1.109824 +  { 198, 2 },
1.109825 +  { 207, 2 },
1.109826 +  { 207, 0 },
1.109827 +  { 206, 7 },
1.109828 +  { 206, 7 },
1.109829 +  { 206, 7 },
1.109830 +  { 157, 0 },
1.109831 +  { 157, 2 },
1.109832 +  { 193, 2 },
1.109833 +  { 208, 1 },
1.109834 +  { 208, 2 },
1.109835 +  { 208, 3 },
1.109836 +  { 208, 4 },
1.109837 +  { 210, 2 },
1.109838 +  { 210, 0 },
1.109839 +  { 209, 0 },
1.109840 +  { 209, 3 },
1.109841 +  { 209, 2 },
1.109842 +  { 211, 4 },
1.109843 +  { 211, 0 },
1.109844 +  { 202, 0 },
1.109845 +  { 202, 3 },
1.109846 +  { 214, 4 },
1.109847 +  { 214, 2 },
1.109848 +  { 176, 1 },
1.109849 +  { 176, 1 },
1.109850 +  { 176, 0 },
1.109851 +  { 200, 0 },
1.109852 +  { 200, 3 },
1.109853 +  { 201, 0 },
1.109854 +  { 201, 2 },
1.109855 +  { 203, 0 },
1.109856 +  { 203, 2 },
1.109857 +  { 203, 4 },
1.109858 +  { 203, 4 },
1.109859 +  { 147, 5 },
1.109860 +  { 199, 0 },
1.109861 +  { 199, 2 },
1.109862 +  { 147, 7 },
1.109863 +  { 216, 5 },
1.109864 +  { 216, 3 },
1.109865 +  { 147, 5 },
1.109866 +  { 147, 5 },
1.109867 +  { 147, 6 },
1.109868 +  { 217, 2 },
1.109869 +  { 217, 1 },
1.109870 +  { 219, 4 },
1.109871 +  { 219, 5 },
1.109872 +  { 218, 0 },
1.109873 +  { 218, 3 },
1.109874 +  { 213, 3 },
1.109875 +  { 213, 1 },
1.109876 +  { 174, 1 },
1.109877 +  { 174, 3 },
1.109878 +  { 173, 1 },
1.109879 +  { 174, 1 },
1.109880 +  { 174, 1 },
1.109881 +  { 174, 3 },
1.109882 +  { 174, 5 },
1.109883 +  { 173, 1 },
1.109884 +  { 173, 1 },
1.109885 +  { 174, 1 },
1.109886 +  { 174, 1 },
1.109887 +  { 174, 3 },
1.109888 +  { 174, 6 },
1.109889 +  { 174, 5 },
1.109890 +  { 174, 4 },
1.109891 +  { 173, 1 },
1.109892 +  { 174, 3 },
1.109893 +  { 174, 3 },
1.109894 +  { 174, 3 },
1.109895 +  { 174, 3 },
1.109896 +  { 174, 3 },
1.109897 +  { 174, 3 },
1.109898 +  { 174, 3 },
1.109899 +  { 174, 3 },
1.109900 +  { 221, 1 },
1.109901 +  { 221, 2 },
1.109902 +  { 221, 1 },
1.109903 +  { 221, 2 },
1.109904 +  { 174, 3 },
1.109905 +  { 174, 5 },
1.109906 +  { 174, 2 },
1.109907 +  { 174, 3 },
1.109908 +  { 174, 3 },
1.109909 +  { 174, 4 },
1.109910 +  { 174, 2 },
1.109911 +  { 174, 2 },
1.109912 +  { 174, 2 },
1.109913 +  { 174, 2 },
1.109914 +  { 222, 1 },
1.109915 +  { 222, 2 },
1.109916 +  { 174, 5 },
1.109917 +  { 223, 1 },
1.109918 +  { 223, 2 },
1.109919 +  { 174, 5 },
1.109920 +  { 174, 3 },
1.109921 +  { 174, 5 },
1.109922 +  { 174, 4 },
1.109923 +  { 174, 4 },
1.109924 +  { 174, 5 },
1.109925 +  { 225, 5 },
1.109926 +  { 225, 4 },
1.109927 +  { 226, 2 },
1.109928 +  { 226, 0 },
1.109929 +  { 224, 1 },
1.109930 +  { 224, 0 },
1.109931 +  { 220, 1 },
1.109932 +  { 220, 0 },
1.109933 +  { 215, 3 },
1.109934 +  { 215, 1 },
1.109935 +  { 147, 11 },
1.109936 +  { 227, 1 },
1.109937 +  { 227, 0 },
1.109938 +  { 178, 0 },
1.109939 +  { 178, 3 },
1.109940 +  { 187, 5 },
1.109941 +  { 187, 3 },
1.109942 +  { 228, 0 },
1.109943 +  { 228, 2 },
1.109944 +  { 147, 4 },
1.109945 +  { 147, 1 },
1.109946 +  { 147, 2 },
1.109947 +  { 147, 3 },
1.109948 +  { 147, 5 },
1.109949 +  { 147, 6 },
1.109950 +  { 147, 5 },
1.109951 +  { 147, 6 },
1.109952 +  { 229, 1 },
1.109953 +  { 229, 1 },
1.109954 +  { 229, 1 },
1.109955 +  { 229, 1 },
1.109956 +  { 229, 1 },
1.109957 +  { 170, 2 },
1.109958 +  { 170, 1 },
1.109959 +  { 171, 2 },
1.109960 +  { 230, 1 },
1.109961 +  { 147, 5 },
1.109962 +  { 231, 11 },
1.109963 +  { 233, 1 },
1.109964 +  { 233, 1 },
1.109965 +  { 233, 2 },
1.109966 +  { 233, 0 },
1.109967 +  { 234, 1 },
1.109968 +  { 234, 1 },
1.109969 +  { 234, 3 },
1.109970 +  { 235, 0 },
1.109971 +  { 235, 3 },
1.109972 +  { 236, 0 },
1.109973 +  { 236, 2 },
1.109974 +  { 232, 3 },
1.109975 +  { 232, 2 },
1.109976 +  { 238, 1 },
1.109977 +  { 238, 3 },
1.109978 +  { 239, 0 },
1.109979 +  { 239, 3 },
1.109980 +  { 239, 2 },
1.109981 +  { 237, 7 },
1.109982 +  { 237, 5 },
1.109983 +  { 237, 5 },
1.109984 +  { 237, 5 },
1.109985 +  { 237, 1 },
1.109986 +  { 174, 4 },
1.109987 +  { 174, 6 },
1.109988 +  { 191, 1 },
1.109989 +  { 191, 1 },
1.109990 +  { 191, 1 },
1.109991 +  { 147, 4 },
1.109992 +  { 147, 6 },
1.109993 +  { 147, 3 },
1.109994 +  { 241, 0 },
1.109995 +  { 241, 2 },
1.109996 +  { 240, 1 },
1.109997 +  { 240, 0 },
1.109998 +  { 147, 1 },
1.109999 +  { 147, 3 },
1.110000 +  { 147, 1 },
1.110001 +  { 147, 3 },
1.110002 +  { 147, 6 },
1.110003 +  { 147, 6 },
1.110004 +  { 242, 1 },
1.110005 +  { 243, 0 },
1.110006 +  { 243, 1 },
1.110007 +  { 147, 1 },
1.110008 +  { 147, 4 },
1.110009 +  { 244, 8 },
1.110010 +  { 245, 1 },
1.110011 +  { 245, 3 },
1.110012 +  { 246, 0 },
1.110013 +  { 246, 2 },
1.110014 +  { 247, 1 },
1.110015 +  { 247, 3 },
1.110016 +  { 248, 1 },
1.110017 +  { 249, 0 },
1.110018 +  { 249, 4 },
1.110019 +  { 249, 2 },
1.110020 +};
1.110021 +
1.110022 +static void yy_accept(yyParser*);  /* Forward Declaration */
1.110023 +
1.110024 +/*
1.110025 +** Perform a reduce action and the shift that must immediately
1.110026 +** follow the reduce.
1.110027 +*/
1.110028 +static void yy_reduce(
1.110029 +  yyParser *yypParser,         /* The parser */
1.110030 +  int yyruleno                 /* Number of the rule by which to reduce */
1.110031 +){
1.110032 +  int yygoto;                     /* The next state */
1.110033 +  int yyact;                      /* The next action */
1.110034 +  YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
1.110035 +  yyStackEntry *yymsp;            /* The top of the parser's stack */
1.110036 +  int yysize;                     /* Amount to pop the stack */
1.110037 +  sqlite3ParserARG_FETCH;
1.110038 +  yymsp = &yypParser->yystack[yypParser->yyidx];
1.110039 +#ifndef NDEBUG
1.110040 +  if( yyTraceFILE && yyruleno>=0 
1.110041 +        && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
1.110042 +    fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
1.110043 +      yyRuleName[yyruleno]);
1.110044 +  }
1.110045 +#endif /* NDEBUG */
1.110046 +
1.110047 +  /* Silence complaints from purify about yygotominor being uninitialized
1.110048 +  ** in some cases when it is copied into the stack after the following
1.110049 +  ** switch.  yygotominor is uninitialized when a rule reduces that does
1.110050 +  ** not set the value of its left-hand side nonterminal.  Leaving the
1.110051 +  ** value of the nonterminal uninitialized is utterly harmless as long
1.110052 +  ** as the value is never used.  So really the only thing this code
1.110053 +  ** accomplishes is to quieten purify.  
1.110054 +  **
1.110055 +  ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
1.110056 +  ** without this code, their parser segfaults.  I'm not sure what there
1.110057 +  ** parser is doing to make this happen.  This is the second bug report
1.110058 +  ** from wireshark this week.  Clearly they are stressing Lemon in ways
1.110059 +  ** that it has not been previously stressed...  (SQLite ticket #2172)
1.110060 +  */
1.110061 +  /*memset(&yygotominor, 0, sizeof(yygotominor));*/
1.110062 +  yygotominor = yyzerominor;
1.110063 +
1.110064 +
1.110065 +  switch( yyruleno ){
1.110066 +  /* Beginning here are the reduction cases.  A typical example
1.110067 +  ** follows:
1.110068 +  **   case 0:
1.110069 +  **  #line <lineno> <grammarfile>
1.110070 +  **     { ... }           // User supplied code
1.110071 +  **  #line <lineno> <thisfile>
1.110072 +  **     break;
1.110073 +  */
1.110074 +      case 5: /* explain ::= */
1.110075 +{ sqlite3BeginParse(pParse, 0); }
1.110076 +        break;
1.110077 +      case 6: /* explain ::= EXPLAIN */
1.110078 +{ sqlite3BeginParse(pParse, 1); }
1.110079 +        break;
1.110080 +      case 7: /* explain ::= EXPLAIN QUERY PLAN */
1.110081 +{ sqlite3BeginParse(pParse, 2); }
1.110082 +        break;
1.110083 +      case 8: /* cmdx ::= cmd */
1.110084 +{ sqlite3FinishCoding(pParse); }
1.110085 +        break;
1.110086 +      case 9: /* cmd ::= BEGIN transtype trans_opt */
1.110087 +{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy392);}
1.110088 +        break;
1.110089 +      case 13: /* transtype ::= */
1.110090 +{yygotominor.yy392 = TK_DEFERRED;}
1.110091 +        break;
1.110092 +      case 14: /* transtype ::= DEFERRED */
1.110093 +      case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
1.110094 +      case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
1.110095 +      case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
1.110096 +      case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
1.110097 +{yygotominor.yy392 = yymsp[0].major;}
1.110098 +        break;
1.110099 +      case 17: /* cmd ::= COMMIT trans_opt */
1.110100 +      case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
1.110101 +{sqlite3CommitTransaction(pParse);}
1.110102 +        break;
1.110103 +      case 19: /* cmd ::= ROLLBACK trans_opt */
1.110104 +{sqlite3RollbackTransaction(pParse);}
1.110105 +        break;
1.110106 +      case 22: /* cmd ::= SAVEPOINT nm */
1.110107 +{
1.110108 +  sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
1.110109 +}
1.110110 +        break;
1.110111 +      case 23: /* cmd ::= RELEASE savepoint_opt nm */
1.110112 +{
1.110113 +  sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
1.110114 +}
1.110115 +        break;
1.110116 +      case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
1.110117 +{
1.110118 +  sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
1.110119 +}
1.110120 +        break;
1.110121 +      case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
1.110122 +{
1.110123 +   sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy392,0,0,yymsp[-2].minor.yy392);
1.110124 +}
1.110125 +        break;
1.110126 +      case 27: /* createkw ::= CREATE */
1.110127 +{
1.110128 +  pParse->db->lookaside.bEnabled = 0;
1.110129 +  yygotominor.yy0 = yymsp[0].minor.yy0;
1.110130 +}
1.110131 +        break;
1.110132 +      case 28: /* ifnotexists ::= */
1.110133 +      case 31: /* temp ::= */ yytestcase(yyruleno==31);
1.110134 +      case 69: /* autoinc ::= */ yytestcase(yyruleno==69);
1.110135 +      case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==82);
1.110136 +      case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
1.110137 +      case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
1.110138 +      case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
1.110139 +      case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
1.110140 +      case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
1.110141 +      case 121: /* distinct ::= */ yytestcase(yyruleno==121);
1.110142 +      case 221: /* between_op ::= BETWEEN */ yytestcase(yyruleno==221);
1.110143 +      case 224: /* in_op ::= IN */ yytestcase(yyruleno==224);
1.110144 +{yygotominor.yy392 = 0;}
1.110145 +        break;
1.110146 +      case 29: /* ifnotexists ::= IF NOT EXISTS */
1.110147 +      case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
1.110148 +      case 70: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==70);
1.110149 +      case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
1.110150 +      case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
1.110151 +      case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
1.110152 +      case 222: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==222);
1.110153 +      case 225: /* in_op ::= NOT IN */ yytestcase(yyruleno==225);
1.110154 +{yygotominor.yy392 = 1;}
1.110155 +        break;
1.110156 +      case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
1.110157 +{
1.110158 +  sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
1.110159 +}
1.110160 +        break;
1.110161 +      case 33: /* create_table_args ::= AS select */
1.110162 +{
1.110163 +  sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy159);
1.110164 +  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy159);
1.110165 +}
1.110166 +        break;
1.110167 +      case 36: /* column ::= columnid type carglist */
1.110168 +{
1.110169 +  yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
1.110170 +  yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
1.110171 +}
1.110172 +        break;
1.110173 +      case 37: /* columnid ::= nm */
1.110174 +{
1.110175 +  sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
1.110176 +  yygotominor.yy0 = yymsp[0].minor.yy0;
1.110177 +  pParse->constraintName.n = 0;
1.110178 +}
1.110179 +        break;
1.110180 +      case 38: /* id ::= ID */
1.110181 +      case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
1.110182 +      case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
1.110183 +      case 41: /* nm ::= id */ yytestcase(yyruleno==41);
1.110184 +      case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
1.110185 +      case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
1.110186 +      case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
1.110187 +      case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
1.110188 +      case 127: /* as ::= AS nm */ yytestcase(yyruleno==127);
1.110189 +      case 128: /* as ::= ids */ yytestcase(yyruleno==128);
1.110190 +      case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138);
1.110191 +      case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147);
1.110192 +      case 250: /* collate ::= COLLATE ids */ yytestcase(yyruleno==250);
1.110193 +      case 259: /* nmnum ::= plus_num */ yytestcase(yyruleno==259);
1.110194 +      case 260: /* nmnum ::= nm */ yytestcase(yyruleno==260);
1.110195 +      case 261: /* nmnum ::= ON */ yytestcase(yyruleno==261);
1.110196 +      case 262: /* nmnum ::= DELETE */ yytestcase(yyruleno==262);
1.110197 +      case 263: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==263);
1.110198 +      case 264: /* plus_num ::= PLUS number */ yytestcase(yyruleno==264);
1.110199 +      case 265: /* plus_num ::= number */ yytestcase(yyruleno==265);
1.110200 +      case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
1.110201 +      case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
1.110202 +      case 283: /* trnm ::= nm */ yytestcase(yyruleno==283);
1.110203 +{yygotominor.yy0 = yymsp[0].minor.yy0;}
1.110204 +        break;
1.110205 +      case 45: /* type ::= typetoken */
1.110206 +{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
1.110207 +        break;
1.110208 +      case 47: /* typetoken ::= typename LP signed RP */
1.110209 +{
1.110210 +  yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
1.110211 +  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
1.110212 +}
1.110213 +        break;
1.110214 +      case 48: /* typetoken ::= typename LP signed COMMA signed RP */
1.110215 +{
1.110216 +  yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
1.110217 +  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
1.110218 +}
1.110219 +        break;
1.110220 +      case 50: /* typename ::= typename ids */
1.110221 +{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);}
1.110222 +        break;
1.110223 +      case 55: /* ccons ::= CONSTRAINT nm */
1.110224 +      case 93: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
1.110225 +{pParse->constraintName = yymsp[0].minor.yy0;}
1.110226 +        break;
1.110227 +      case 56: /* ccons ::= DEFAULT term */
1.110228 +      case 58: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==58);
1.110229 +{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy342);}
1.110230 +        break;
1.110231 +      case 57: /* ccons ::= DEFAULT LP expr RP */
1.110232 +{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy342);}
1.110233 +        break;
1.110234 +      case 59: /* ccons ::= DEFAULT MINUS term */
1.110235 +{
1.110236 +  ExprSpan v;
1.110237 +  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy342.pExpr, 0, 0);
1.110238 +  v.zStart = yymsp[-1].minor.yy0.z;
1.110239 +  v.zEnd = yymsp[0].minor.yy342.zEnd;
1.110240 +  sqlite3AddDefaultValue(pParse,&v);
1.110241 +}
1.110242 +        break;
1.110243 +      case 60: /* ccons ::= DEFAULT id */
1.110244 +{
1.110245 +  ExprSpan v;
1.110246 +  spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
1.110247 +  sqlite3AddDefaultValue(pParse,&v);
1.110248 +}
1.110249 +        break;
1.110250 +      case 62: /* ccons ::= NOT NULL onconf */
1.110251 +{sqlite3AddNotNull(pParse, yymsp[0].minor.yy392);}
1.110252 +        break;
1.110253 +      case 63: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
1.110254 +{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy392,yymsp[0].minor.yy392,yymsp[-2].minor.yy392);}
1.110255 +        break;
1.110256 +      case 64: /* ccons ::= UNIQUE onconf */
1.110257 +{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy392,0,0,0,0);}
1.110258 +        break;
1.110259 +      case 65: /* ccons ::= CHECK LP expr RP */
1.110260 +{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy342.pExpr);}
1.110261 +        break;
1.110262 +      case 66: /* ccons ::= REFERENCES nm idxlist_opt refargs */
1.110263 +{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy442,yymsp[0].minor.yy392);}
1.110264 +        break;
1.110265 +      case 67: /* ccons ::= defer_subclause */
1.110266 +{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy392);}
1.110267 +        break;
1.110268 +      case 68: /* ccons ::= COLLATE ids */
1.110269 +{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
1.110270 +        break;
1.110271 +      case 71: /* refargs ::= */
1.110272 +{ yygotominor.yy392 = OE_None*0x0101; /* EV: R-19803-45884 */}
1.110273 +        break;
1.110274 +      case 72: /* refargs ::= refargs refarg */
1.110275 +{ yygotominor.yy392 = (yymsp[-1].minor.yy392 & ~yymsp[0].minor.yy207.mask) | yymsp[0].minor.yy207.value; }
1.110276 +        break;
1.110277 +      case 73: /* refarg ::= MATCH nm */
1.110278 +      case 74: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==74);
1.110279 +{ yygotominor.yy207.value = 0;     yygotominor.yy207.mask = 0x000000; }
1.110280 +        break;
1.110281 +      case 75: /* refarg ::= ON DELETE refact */
1.110282 +{ yygotominor.yy207.value = yymsp[0].minor.yy392;     yygotominor.yy207.mask = 0x0000ff; }
1.110283 +        break;
1.110284 +      case 76: /* refarg ::= ON UPDATE refact */
1.110285 +{ yygotominor.yy207.value = yymsp[0].minor.yy392<<8;  yygotominor.yy207.mask = 0x00ff00; }
1.110286 +        break;
1.110287 +      case 77: /* refact ::= SET NULL */
1.110288 +{ yygotominor.yy392 = OE_SetNull;  /* EV: R-33326-45252 */}
1.110289 +        break;
1.110290 +      case 78: /* refact ::= SET DEFAULT */
1.110291 +{ yygotominor.yy392 = OE_SetDflt;  /* EV: R-33326-45252 */}
1.110292 +        break;
1.110293 +      case 79: /* refact ::= CASCADE */
1.110294 +{ yygotominor.yy392 = OE_Cascade;  /* EV: R-33326-45252 */}
1.110295 +        break;
1.110296 +      case 80: /* refact ::= RESTRICT */
1.110297 +{ yygotominor.yy392 = OE_Restrict; /* EV: R-33326-45252 */}
1.110298 +        break;
1.110299 +      case 81: /* refact ::= NO ACTION */
1.110300 +{ yygotominor.yy392 = OE_None;     /* EV: R-33326-45252 */}
1.110301 +        break;
1.110302 +      case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
1.110303 +      case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
1.110304 +      case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
1.110305 +      case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
1.110306 +{yygotominor.yy392 = yymsp[0].minor.yy392;}
1.110307 +        break;
1.110308 +      case 87: /* conslist_opt ::= */
1.110309 +{yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
1.110310 +        break;
1.110311 +      case 88: /* conslist_opt ::= COMMA conslist */
1.110312 +{yygotominor.yy0 = yymsp[-1].minor.yy0;}
1.110313 +        break;
1.110314 +      case 91: /* tconscomma ::= COMMA */
1.110315 +{pParse->constraintName.n = 0;}
1.110316 +        break;
1.110317 +      case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
1.110318 +{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy442,yymsp[0].minor.yy392,yymsp[-2].minor.yy392,0);}
1.110319 +        break;
1.110320 +      case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
1.110321 +{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy442,yymsp[0].minor.yy392,0,0,0,0);}
1.110322 +        break;
1.110323 +      case 96: /* tcons ::= CHECK LP expr RP onconf */
1.110324 +{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy342.pExpr);}
1.110325 +        break;
1.110326 +      case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
1.110327 +{
1.110328 +    sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy442, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy442, yymsp[-1].minor.yy392);
1.110329 +    sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy392);
1.110330 +}
1.110331 +        break;
1.110332 +      case 100: /* onconf ::= */
1.110333 +{yygotominor.yy392 = OE_Default;}
1.110334 +        break;
1.110335 +      case 102: /* orconf ::= */
1.110336 +{yygotominor.yy258 = OE_Default;}
1.110337 +        break;
1.110338 +      case 103: /* orconf ::= OR resolvetype */
1.110339 +{yygotominor.yy258 = (u8)yymsp[0].minor.yy392;}
1.110340 +        break;
1.110341 +      case 105: /* resolvetype ::= IGNORE */
1.110342 +{yygotominor.yy392 = OE_Ignore;}
1.110343 +        break;
1.110344 +      case 106: /* resolvetype ::= REPLACE */
1.110345 +{yygotominor.yy392 = OE_Replace;}
1.110346 +        break;
1.110347 +      case 107: /* cmd ::= DROP TABLE ifexists fullname */
1.110348 +{
1.110349 +  sqlite3DropTable(pParse, yymsp[0].minor.yy347, 0, yymsp[-1].minor.yy392);
1.110350 +}
1.110351 +        break;
1.110352 +      case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
1.110353 +{
1.110354 +  sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy159, yymsp[-6].minor.yy392, yymsp[-4].minor.yy392);
1.110355 +}
1.110356 +        break;
1.110357 +      case 111: /* cmd ::= DROP VIEW ifexists fullname */
1.110358 +{
1.110359 +  sqlite3DropTable(pParse, yymsp[0].minor.yy347, 1, yymsp[-1].minor.yy392);
1.110360 +}
1.110361 +        break;
1.110362 +      case 112: /* cmd ::= select */
1.110363 +{
1.110364 +  SelectDest dest = {SRT_Output, 0, 0, 0, 0};
1.110365 +  sqlite3Select(pParse, yymsp[0].minor.yy159, &dest);
1.110366 +  sqlite3ExplainBegin(pParse->pVdbe);
1.110367 +  sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy159);
1.110368 +  sqlite3ExplainFinish(pParse->pVdbe);
1.110369 +  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy159);
1.110370 +}
1.110371 +        break;
1.110372 +      case 113: /* select ::= oneselect */
1.110373 +{yygotominor.yy159 = yymsp[0].minor.yy159;}
1.110374 +        break;
1.110375 +      case 114: /* select ::= select multiselect_op oneselect */
1.110376 +{
1.110377 +  if( yymsp[0].minor.yy159 ){
1.110378 +    yymsp[0].minor.yy159->op = (u8)yymsp[-1].minor.yy392;
1.110379 +    yymsp[0].minor.yy159->pPrior = yymsp[-2].minor.yy159;
1.110380 +  }else{
1.110381 +    sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy159);
1.110382 +  }
1.110383 +  yygotominor.yy159 = yymsp[0].minor.yy159;
1.110384 +}
1.110385 +        break;
1.110386 +      case 116: /* multiselect_op ::= UNION ALL */
1.110387 +{yygotominor.yy392 = TK_ALL;}
1.110388 +        break;
1.110389 +      case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
1.110390 +{
1.110391 +  yygotominor.yy159 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy442,yymsp[-5].minor.yy347,yymsp[-4].minor.yy122,yymsp[-3].minor.yy442,yymsp[-2].minor.yy122,yymsp[-1].minor.yy442,yymsp[-7].minor.yy392,yymsp[0].minor.yy64.pLimit,yymsp[0].minor.yy64.pOffset);
1.110392 +}
1.110393 +        break;
1.110394 +      case 122: /* sclp ::= selcollist COMMA */
1.110395 +      case 246: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==246);
1.110396 +{yygotominor.yy442 = yymsp[-1].minor.yy442;}
1.110397 +        break;
1.110398 +      case 123: /* sclp ::= */
1.110399 +      case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
1.110400 +      case 158: /* groupby_opt ::= */ yytestcase(yyruleno==158);
1.110401 +      case 239: /* exprlist ::= */ yytestcase(yyruleno==239);
1.110402 +      case 245: /* idxlist_opt ::= */ yytestcase(yyruleno==245);
1.110403 +{yygotominor.yy442 = 0;}
1.110404 +        break;
1.110405 +      case 124: /* selcollist ::= sclp expr as */
1.110406 +{
1.110407 +   yygotominor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy442, yymsp[-1].minor.yy342.pExpr);
1.110408 +   if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[0].minor.yy0, 1);
1.110409 +   sqlite3ExprListSetSpan(pParse,yygotominor.yy442,&yymsp[-1].minor.yy342);
1.110410 +}
1.110411 +        break;
1.110412 +      case 125: /* selcollist ::= sclp STAR */
1.110413 +{
1.110414 +  Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
1.110415 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy442, p);
1.110416 +}
1.110417 +        break;
1.110418 +      case 126: /* selcollist ::= sclp nm DOT STAR */
1.110419 +{
1.110420 +  Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
1.110421 +  Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
1.110422 +  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
1.110423 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy442, pDot);
1.110424 +}
1.110425 +        break;
1.110426 +      case 129: /* as ::= */
1.110427 +{yygotominor.yy0.n = 0;}
1.110428 +        break;
1.110429 +      case 130: /* from ::= */
1.110430 +{yygotominor.yy347 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy347));}
1.110431 +        break;
1.110432 +      case 131: /* from ::= FROM seltablist */
1.110433 +{
1.110434 +  yygotominor.yy347 = yymsp[0].minor.yy347;
1.110435 +  sqlite3SrcListShiftJoinType(yygotominor.yy347);
1.110436 +}
1.110437 +        break;
1.110438 +      case 132: /* stl_prefix ::= seltablist joinop */
1.110439 +{
1.110440 +   yygotominor.yy347 = yymsp[-1].minor.yy347;
1.110441 +   if( ALWAYS(yygotominor.yy347 && yygotominor.yy347->nSrc>0) ) yygotominor.yy347->a[yygotominor.yy347->nSrc-1].jointype = (u8)yymsp[0].minor.yy392;
1.110442 +}
1.110443 +        break;
1.110444 +      case 133: /* stl_prefix ::= */
1.110445 +{yygotominor.yy347 = 0;}
1.110446 +        break;
1.110447 +      case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
1.110448 +{
1.110449 +  yygotominor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy122,yymsp[0].minor.yy180);
1.110450 +  sqlite3SrcListIndexedBy(pParse, yygotominor.yy347, &yymsp[-2].minor.yy0);
1.110451 +}
1.110452 +        break;
1.110453 +      case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
1.110454 +{
1.110455 +    yygotominor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy159,yymsp[-1].minor.yy122,yymsp[0].minor.yy180);
1.110456 +  }
1.110457 +        break;
1.110458 +      case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
1.110459 +{
1.110460 +    if( yymsp[-6].minor.yy347==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy122==0 && yymsp[0].minor.yy180==0 ){
1.110461 +      yygotominor.yy347 = yymsp[-4].minor.yy347;
1.110462 +    }else{
1.110463 +      Select *pSubquery;
1.110464 +      sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy347);
1.110465 +      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy347,0,0,0,0,0,0,0);
1.110466 +      yygotominor.yy347 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy347,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy122,yymsp[0].minor.yy180);
1.110467 +    }
1.110468 +  }
1.110469 +        break;
1.110470 +      case 137: /* dbnm ::= */
1.110471 +      case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
1.110472 +{yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
1.110473 +        break;
1.110474 +      case 139: /* fullname ::= nm dbnm */
1.110475 +{yygotominor.yy347 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
1.110476 +        break;
1.110477 +      case 140: /* joinop ::= COMMA|JOIN */
1.110478 +{ yygotominor.yy392 = JT_INNER; }
1.110479 +        break;
1.110480 +      case 141: /* joinop ::= JOIN_KW JOIN */
1.110481 +{ yygotominor.yy392 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
1.110482 +        break;
1.110483 +      case 142: /* joinop ::= JOIN_KW nm JOIN */
1.110484 +{ yygotominor.yy392 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
1.110485 +        break;
1.110486 +      case 143: /* joinop ::= JOIN_KW nm nm JOIN */
1.110487 +{ yygotominor.yy392 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
1.110488 +        break;
1.110489 +      case 144: /* on_opt ::= ON expr */
1.110490 +      case 161: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==161);
1.110491 +      case 168: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==168);
1.110492 +      case 234: /* case_else ::= ELSE expr */ yytestcase(yyruleno==234);
1.110493 +      case 236: /* case_operand ::= expr */ yytestcase(yyruleno==236);
1.110494 +{yygotominor.yy122 = yymsp[0].minor.yy342.pExpr;}
1.110495 +        break;
1.110496 +      case 145: /* on_opt ::= */
1.110497 +      case 160: /* having_opt ::= */ yytestcase(yyruleno==160);
1.110498 +      case 167: /* where_opt ::= */ yytestcase(yyruleno==167);
1.110499 +      case 235: /* case_else ::= */ yytestcase(yyruleno==235);
1.110500 +      case 237: /* case_operand ::= */ yytestcase(yyruleno==237);
1.110501 +{yygotominor.yy122 = 0;}
1.110502 +        break;
1.110503 +      case 148: /* indexed_opt ::= NOT INDEXED */
1.110504 +{yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
1.110505 +        break;
1.110506 +      case 149: /* using_opt ::= USING LP inscollist RP */
1.110507 +      case 180: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==180);
1.110508 +{yygotominor.yy180 = yymsp[-1].minor.yy180;}
1.110509 +        break;
1.110510 +      case 150: /* using_opt ::= */
1.110511 +      case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
1.110512 +{yygotominor.yy180 = 0;}
1.110513 +        break;
1.110514 +      case 152: /* orderby_opt ::= ORDER BY sortlist */
1.110515 +      case 159: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==159);
1.110516 +      case 238: /* exprlist ::= nexprlist */ yytestcase(yyruleno==238);
1.110517 +{yygotominor.yy442 = yymsp[0].minor.yy442;}
1.110518 +        break;
1.110519 +      case 153: /* sortlist ::= sortlist COMMA expr sortorder */
1.110520 +{
1.110521 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy442,yymsp[-1].minor.yy342.pExpr);
1.110522 +  if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
1.110523 +}
1.110524 +        break;
1.110525 +      case 154: /* sortlist ::= expr sortorder */
1.110526 +{
1.110527 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy342.pExpr);
1.110528 +  if( yygotominor.yy442 && ALWAYS(yygotominor.yy442->a) ) yygotominor.yy442->a[0].sortOrder = (u8)yymsp[0].minor.yy392;
1.110529 +}
1.110530 +        break;
1.110531 +      case 155: /* sortorder ::= ASC */
1.110532 +      case 157: /* sortorder ::= */ yytestcase(yyruleno==157);
1.110533 +{yygotominor.yy392 = SQLITE_SO_ASC;}
1.110534 +        break;
1.110535 +      case 156: /* sortorder ::= DESC */
1.110536 +{yygotominor.yy392 = SQLITE_SO_DESC;}
1.110537 +        break;
1.110538 +      case 162: /* limit_opt ::= */
1.110539 +{yygotominor.yy64.pLimit = 0; yygotominor.yy64.pOffset = 0;}
1.110540 +        break;
1.110541 +      case 163: /* limit_opt ::= LIMIT expr */
1.110542 +{yygotominor.yy64.pLimit = yymsp[0].minor.yy342.pExpr; yygotominor.yy64.pOffset = 0;}
1.110543 +        break;
1.110544 +      case 164: /* limit_opt ::= LIMIT expr OFFSET expr */
1.110545 +{yygotominor.yy64.pLimit = yymsp[-2].minor.yy342.pExpr; yygotominor.yy64.pOffset = yymsp[0].minor.yy342.pExpr;}
1.110546 +        break;
1.110547 +      case 165: /* limit_opt ::= LIMIT expr COMMA expr */
1.110548 +{yygotominor.yy64.pOffset = yymsp[-2].minor.yy342.pExpr; yygotominor.yy64.pLimit = yymsp[0].minor.yy342.pExpr;}
1.110549 +        break;
1.110550 +      case 166: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
1.110551 +{
1.110552 +  sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy347, &yymsp[-1].minor.yy0);
1.110553 +  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy347,yymsp[0].minor.yy122);
1.110554 +}
1.110555 +        break;
1.110556 +      case 169: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
1.110557 +{
1.110558 +  sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy347, &yymsp[-3].minor.yy0);
1.110559 +  sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy442,"set list"); 
1.110560 +  sqlite3Update(pParse,yymsp[-4].minor.yy347,yymsp[-1].minor.yy442,yymsp[0].minor.yy122,yymsp[-5].minor.yy258);
1.110561 +}
1.110562 +        break;
1.110563 +      case 170: /* setlist ::= setlist COMMA nm EQ expr */
1.110564 +{
1.110565 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy442, yymsp[0].minor.yy342.pExpr);
1.110566 +  sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
1.110567 +}
1.110568 +        break;
1.110569 +      case 171: /* setlist ::= nm EQ expr */
1.110570 +{
1.110571 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy342.pExpr);
1.110572 +  sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
1.110573 +}
1.110574 +        break;
1.110575 +      case 172: /* cmd ::= insert_cmd INTO fullname inscollist_opt valuelist */
1.110576 +{sqlite3Insert(pParse, yymsp[-2].minor.yy347, yymsp[0].minor.yy487.pList, yymsp[0].minor.yy487.pSelect, yymsp[-1].minor.yy180, yymsp[-4].minor.yy258);}
1.110577 +        break;
1.110578 +      case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
1.110579 +{sqlite3Insert(pParse, yymsp[-2].minor.yy347, 0, yymsp[0].minor.yy159, yymsp[-1].minor.yy180, yymsp[-4].minor.yy258);}
1.110580 +        break;
1.110581 +      case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
1.110582 +{sqlite3Insert(pParse, yymsp[-3].minor.yy347, 0, 0, yymsp[-2].minor.yy180, yymsp[-5].minor.yy258);}
1.110583 +        break;
1.110584 +      case 175: /* insert_cmd ::= INSERT orconf */
1.110585 +{yygotominor.yy258 = yymsp[0].minor.yy258;}
1.110586 +        break;
1.110587 +      case 176: /* insert_cmd ::= REPLACE */
1.110588 +{yygotominor.yy258 = OE_Replace;}
1.110589 +        break;
1.110590 +      case 177: /* valuelist ::= VALUES LP nexprlist RP */
1.110591 +{
1.110592 +  yygotominor.yy487.pList = yymsp[-1].minor.yy442;
1.110593 +  yygotominor.yy487.pSelect = 0;
1.110594 +}
1.110595 +        break;
1.110596 +      case 178: /* valuelist ::= valuelist COMMA LP exprlist RP */
1.110597 +{
1.110598 +  Select *pRight = sqlite3SelectNew(pParse, yymsp[-1].minor.yy442, 0, 0, 0, 0, 0, 0, 0, 0);
1.110599 +  if( yymsp[-4].minor.yy487.pList ){
1.110600 +    yymsp[-4].minor.yy487.pSelect = sqlite3SelectNew(pParse, yymsp[-4].minor.yy487.pList, 0, 0, 0, 0, 0, 0, 0, 0);
1.110601 +    yymsp[-4].minor.yy487.pList = 0;
1.110602 +  }
1.110603 +  yygotominor.yy487.pList = 0;
1.110604 +  if( yymsp[-4].minor.yy487.pSelect==0 || pRight==0 ){
1.110605 +    sqlite3SelectDelete(pParse->db, pRight);
1.110606 +    sqlite3SelectDelete(pParse->db, yymsp[-4].minor.yy487.pSelect);
1.110607 +    yygotominor.yy487.pSelect = 0;
1.110608 +  }else{
1.110609 +    pRight->op = TK_ALL;
1.110610 +    pRight->pPrior = yymsp[-4].minor.yy487.pSelect;
1.110611 +    pRight->selFlags |= SF_Values;
1.110612 +    pRight->pPrior->selFlags |= SF_Values;
1.110613 +    yygotominor.yy487.pSelect = pRight;
1.110614 +  }
1.110615 +}
1.110616 +        break;
1.110617 +      case 181: /* inscollist ::= inscollist COMMA nm */
1.110618 +{yygotominor.yy180 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy180,&yymsp[0].minor.yy0);}
1.110619 +        break;
1.110620 +      case 182: /* inscollist ::= nm */
1.110621 +{yygotominor.yy180 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
1.110622 +        break;
1.110623 +      case 183: /* expr ::= term */
1.110624 +{yygotominor.yy342 = yymsp[0].minor.yy342;}
1.110625 +        break;
1.110626 +      case 184: /* expr ::= LP expr RP */
1.110627 +{yygotominor.yy342.pExpr = yymsp[-1].minor.yy342.pExpr; spanSet(&yygotominor.yy342,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
1.110628 +        break;
1.110629 +      case 185: /* term ::= NULL */
1.110630 +      case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
1.110631 +      case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
1.110632 +{spanExpr(&yygotominor.yy342, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
1.110633 +        break;
1.110634 +      case 186: /* expr ::= id */
1.110635 +      case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
1.110636 +{spanExpr(&yygotominor.yy342, pParse, TK_ID, &yymsp[0].minor.yy0);}
1.110637 +        break;
1.110638 +      case 188: /* expr ::= nm DOT nm */
1.110639 +{
1.110640 +  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
1.110641 +  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
1.110642 +  yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
1.110643 +  spanSet(&yygotominor.yy342,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
1.110644 +}
1.110645 +        break;
1.110646 +      case 189: /* expr ::= nm DOT nm DOT nm */
1.110647 +{
1.110648 +  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
1.110649 +  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
1.110650 +  Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
1.110651 +  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
1.110652 +  yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
1.110653 +  spanSet(&yygotominor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
1.110654 +}
1.110655 +        break;
1.110656 +      case 192: /* expr ::= REGISTER */
1.110657 +{
1.110658 +  /* When doing a nested parse, one can include terms in an expression
1.110659 +  ** that look like this:   #1 #2 ...  These terms refer to registers
1.110660 +  ** in the virtual machine.  #N is the N-th register. */
1.110661 +  if( pParse->nested==0 ){
1.110662 +    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
1.110663 +    yygotominor.yy342.pExpr = 0;
1.110664 +  }else{
1.110665 +    yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
1.110666 +    if( yygotominor.yy342.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy342.pExpr->iTable);
1.110667 +  }
1.110668 +  spanSet(&yygotominor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
1.110669 +}
1.110670 +        break;
1.110671 +      case 193: /* expr ::= VARIABLE */
1.110672 +{
1.110673 +  spanExpr(&yygotominor.yy342, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
1.110674 +  sqlite3ExprAssignVarNumber(pParse, yygotominor.yy342.pExpr);
1.110675 +  spanSet(&yygotominor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
1.110676 +}
1.110677 +        break;
1.110678 +      case 194: /* expr ::= expr COLLATE ids */
1.110679 +{
1.110680 +  yygotominor.yy342.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy342.pExpr, &yymsp[0].minor.yy0);
1.110681 +  yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
1.110682 +  yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.110683 +}
1.110684 +        break;
1.110685 +      case 195: /* expr ::= CAST LP expr AS typetoken RP */
1.110686 +{
1.110687 +  yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy342.pExpr, 0, &yymsp[-1].minor.yy0);
1.110688 +  spanSet(&yygotominor.yy342,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
1.110689 +}
1.110690 +        break;
1.110691 +      case 196: /* expr ::= ID LP distinct exprlist RP */
1.110692 +{
1.110693 +  if( yymsp[-1].minor.yy442 && yymsp[-1].minor.yy442->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
1.110694 +    sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
1.110695 +  }
1.110696 +  yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy442, &yymsp[-4].minor.yy0);
1.110697 +  spanSet(&yygotominor.yy342,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
1.110698 +  if( yymsp[-2].minor.yy392 && yygotominor.yy342.pExpr ){
1.110699 +    yygotominor.yy342.pExpr->flags |= EP_Distinct;
1.110700 +  }
1.110701 +}
1.110702 +        break;
1.110703 +      case 197: /* expr ::= ID LP STAR RP */
1.110704 +{
1.110705 +  yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
1.110706 +  spanSet(&yygotominor.yy342,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
1.110707 +}
1.110708 +        break;
1.110709 +      case 198: /* term ::= CTIME_KW */
1.110710 +{
1.110711 +  /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
1.110712 +  ** treated as functions that return constants */
1.110713 +  yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
1.110714 +  if( yygotominor.yy342.pExpr ){
1.110715 +    yygotominor.yy342.pExpr->op = TK_CONST_FUNC;  
1.110716 +  }
1.110717 +  spanSet(&yygotominor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
1.110718 +}
1.110719 +        break;
1.110720 +      case 199: /* expr ::= expr AND expr */
1.110721 +      case 200: /* expr ::= expr OR expr */ yytestcase(yyruleno==200);
1.110722 +      case 201: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==201);
1.110723 +      case 202: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==202);
1.110724 +      case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==203);
1.110725 +      case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
1.110726 +      case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205);
1.110727 +      case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
1.110728 +{spanBinaryExpr(&yygotominor.yy342,pParse,yymsp[-1].major,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342);}
1.110729 +        break;
1.110730 +      case 207: /* likeop ::= LIKE_KW */
1.110731 +      case 209: /* likeop ::= MATCH */ yytestcase(yyruleno==209);
1.110732 +{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 0;}
1.110733 +        break;
1.110734 +      case 208: /* likeop ::= NOT LIKE_KW */
1.110735 +      case 210: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==210);
1.110736 +{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 1;}
1.110737 +        break;
1.110738 +      case 211: /* expr ::= expr likeop expr */
1.110739 +{
1.110740 +  ExprList *pList;
1.110741 +  pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy342.pExpr);
1.110742 +  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy342.pExpr);
1.110743 +  yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy318.eOperator);
1.110744 +  if( yymsp[-1].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
1.110745 +  yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
1.110746 +  yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
1.110747 +  if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
1.110748 +}
1.110749 +        break;
1.110750 +      case 212: /* expr ::= expr likeop expr ESCAPE expr */
1.110751 +{
1.110752 +  ExprList *pList;
1.110753 +  pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
1.110754 +  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy342.pExpr);
1.110755 +  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr);
1.110756 +  yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy318.eOperator);
1.110757 +  if( yymsp[-3].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
1.110758 +  yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
1.110759 +  yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
1.110760 +  if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
1.110761 +}
1.110762 +        break;
1.110763 +      case 213: /* expr ::= expr ISNULL|NOTNULL */
1.110764 +{spanUnaryPostfix(&yygotominor.yy342,pParse,yymsp[0].major,&yymsp[-1].minor.yy342,&yymsp[0].minor.yy0);}
1.110765 +        break;
1.110766 +      case 214: /* expr ::= expr NOT NULL */
1.110767 +{spanUnaryPostfix(&yygotominor.yy342,pParse,TK_NOTNULL,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy0);}
1.110768 +        break;
1.110769 +      case 215: /* expr ::= expr IS expr */
1.110770 +{
1.110771 +  spanBinaryExpr(&yygotominor.yy342,pParse,TK_IS,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342);
1.110772 +  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy342.pExpr, yygotominor.yy342.pExpr, TK_ISNULL);
1.110773 +}
1.110774 +        break;
1.110775 +      case 216: /* expr ::= expr IS NOT expr */
1.110776 +{
1.110777 +  spanBinaryExpr(&yygotominor.yy342,pParse,TK_ISNOT,&yymsp[-3].minor.yy342,&yymsp[0].minor.yy342);
1.110778 +  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy342.pExpr, yygotominor.yy342.pExpr, TK_NOTNULL);
1.110779 +}
1.110780 +        break;
1.110781 +      case 217: /* expr ::= NOT expr */
1.110782 +      case 218: /* expr ::= BITNOT expr */ yytestcase(yyruleno==218);
1.110783 +{spanUnaryPrefix(&yygotominor.yy342,pParse,yymsp[-1].major,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);}
1.110784 +        break;
1.110785 +      case 219: /* expr ::= MINUS expr */
1.110786 +{spanUnaryPrefix(&yygotominor.yy342,pParse,TK_UMINUS,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);}
1.110787 +        break;
1.110788 +      case 220: /* expr ::= PLUS expr */
1.110789 +{spanUnaryPrefix(&yygotominor.yy342,pParse,TK_UPLUS,&yymsp[0].minor.yy342,&yymsp[-1].minor.yy0);}
1.110790 +        break;
1.110791 +      case 223: /* expr ::= expr between_op expr AND expr */
1.110792 +{
1.110793 +  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
1.110794 +  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr);
1.110795 +  yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy342.pExpr, 0, 0);
1.110796 +  if( yygotominor.yy342.pExpr ){
1.110797 +    yygotominor.yy342.pExpr->x.pList = pList;
1.110798 +  }else{
1.110799 +    sqlite3ExprListDelete(pParse->db, pList);
1.110800 +  } 
1.110801 +  if( yymsp[-3].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
1.110802 +  yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
1.110803 +  yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
1.110804 +}
1.110805 +        break;
1.110806 +      case 226: /* expr ::= expr in_op LP exprlist RP */
1.110807 +{
1.110808 +    if( yymsp[-1].minor.yy442==0 ){
1.110809 +      /* Expressions of the form
1.110810 +      **
1.110811 +      **      expr1 IN ()
1.110812 +      **      expr1 NOT IN ()
1.110813 +      **
1.110814 +      ** simplify to constants 0 (false) and 1 (true), respectively,
1.110815 +      ** regardless of the value of expr1.
1.110816 +      */
1.110817 +      yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy392]);
1.110818 +      sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy342.pExpr);
1.110819 +    }else{
1.110820 +      yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy342.pExpr, 0, 0);
1.110821 +      if( yygotominor.yy342.pExpr ){
1.110822 +        yygotominor.yy342.pExpr->x.pList = yymsp[-1].minor.yy442;
1.110823 +        sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
1.110824 +      }else{
1.110825 +        sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy442);
1.110826 +      }
1.110827 +      if( yymsp[-3].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
1.110828 +    }
1.110829 +    yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
1.110830 +    yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.110831 +  }
1.110832 +        break;
1.110833 +      case 227: /* expr ::= LP select RP */
1.110834 +{
1.110835 +    yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
1.110836 +    if( yygotominor.yy342.pExpr ){
1.110837 +      yygotominor.yy342.pExpr->x.pSelect = yymsp[-1].minor.yy159;
1.110838 +      ExprSetProperty(yygotominor.yy342.pExpr, EP_xIsSelect);
1.110839 +      sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
1.110840 +    }else{
1.110841 +      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy159);
1.110842 +    }
1.110843 +    yygotominor.yy342.zStart = yymsp[-2].minor.yy0.z;
1.110844 +    yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.110845 +  }
1.110846 +        break;
1.110847 +      case 228: /* expr ::= expr in_op LP select RP */
1.110848 +{
1.110849 +    yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy342.pExpr, 0, 0);
1.110850 +    if( yygotominor.yy342.pExpr ){
1.110851 +      yygotominor.yy342.pExpr->x.pSelect = yymsp[-1].minor.yy159;
1.110852 +      ExprSetProperty(yygotominor.yy342.pExpr, EP_xIsSelect);
1.110853 +      sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
1.110854 +    }else{
1.110855 +      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy159);
1.110856 +    }
1.110857 +    if( yymsp[-3].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
1.110858 +    yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
1.110859 +    yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.110860 +  }
1.110861 +        break;
1.110862 +      case 229: /* expr ::= expr in_op nm dbnm */
1.110863 +{
1.110864 +    SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
1.110865 +    yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy342.pExpr, 0, 0);
1.110866 +    if( yygotominor.yy342.pExpr ){
1.110867 +      yygotominor.yy342.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
1.110868 +      ExprSetProperty(yygotominor.yy342.pExpr, EP_xIsSelect);
1.110869 +      sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
1.110870 +    }else{
1.110871 +      sqlite3SrcListDelete(pParse->db, pSrc);
1.110872 +    }
1.110873 +    if( yymsp[-2].minor.yy392 ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
1.110874 +    yygotominor.yy342.zStart = yymsp[-3].minor.yy342.zStart;
1.110875 +    yygotominor.yy342.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];
1.110876 +  }
1.110877 +        break;
1.110878 +      case 230: /* expr ::= EXISTS LP select RP */
1.110879 +{
1.110880 +    Expr *p = yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
1.110881 +    if( p ){
1.110882 +      p->x.pSelect = yymsp[-1].minor.yy159;
1.110883 +      ExprSetProperty(p, EP_xIsSelect);
1.110884 +      sqlite3ExprSetHeight(pParse, p);
1.110885 +    }else{
1.110886 +      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy159);
1.110887 +    }
1.110888 +    yygotominor.yy342.zStart = yymsp[-3].minor.yy0.z;
1.110889 +    yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.110890 +  }
1.110891 +        break;
1.110892 +      case 231: /* expr ::= CASE case_operand case_exprlist case_else END */
1.110893 +{
1.110894 +  yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy122, yymsp[-1].minor.yy122, 0);
1.110895 +  if( yygotominor.yy342.pExpr ){
1.110896 +    yygotominor.yy342.pExpr->x.pList = yymsp[-2].minor.yy442;
1.110897 +    sqlite3ExprSetHeight(pParse, yygotominor.yy342.pExpr);
1.110898 +  }else{
1.110899 +    sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy442);
1.110900 +  }
1.110901 +  yygotominor.yy342.zStart = yymsp[-4].minor.yy0.z;
1.110902 +  yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.110903 +}
1.110904 +        break;
1.110905 +      case 232: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
1.110906 +{
1.110907 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, yymsp[-2].minor.yy342.pExpr);
1.110908 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,yygotominor.yy442, yymsp[0].minor.yy342.pExpr);
1.110909 +}
1.110910 +        break;
1.110911 +      case 233: /* case_exprlist ::= WHEN expr THEN expr */
1.110912 +{
1.110913 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
1.110914 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,yygotominor.yy442, yymsp[0].minor.yy342.pExpr);
1.110915 +}
1.110916 +        break;
1.110917 +      case 240: /* nexprlist ::= nexprlist COMMA expr */
1.110918 +{yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy442,yymsp[0].minor.yy342.pExpr);}
1.110919 +        break;
1.110920 +      case 241: /* nexprlist ::= expr */
1.110921 +{yygotominor.yy442 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy342.pExpr);}
1.110922 +        break;
1.110923 +      case 242: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
1.110924 +{
1.110925 +  sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0, 
1.110926 +                     sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy442, yymsp[-9].minor.yy392,
1.110927 +                      &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy392);
1.110928 +}
1.110929 +        break;
1.110930 +      case 243: /* uniqueflag ::= UNIQUE */
1.110931 +      case 296: /* raisetype ::= ABORT */ yytestcase(yyruleno==296);
1.110932 +{yygotominor.yy392 = OE_Abort;}
1.110933 +        break;
1.110934 +      case 244: /* uniqueflag ::= */
1.110935 +{yygotominor.yy392 = OE_None;}
1.110936 +        break;
1.110937 +      case 247: /* idxlist ::= idxlist COMMA nm collate sortorder */
1.110938 +{
1.110939 +  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
1.110940 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, p);
1.110941 +  sqlite3ExprListSetName(pParse,yygotominor.yy442,&yymsp[-2].minor.yy0,1);
1.110942 +  sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
1.110943 +  if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
1.110944 +}
1.110945 +        break;
1.110946 +      case 248: /* idxlist ::= nm collate sortorder */
1.110947 +{
1.110948 +  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
1.110949 +  yygotominor.yy442 = sqlite3ExprListAppend(pParse,0, p);
1.110950 +  sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
1.110951 +  sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
1.110952 +  if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
1.110953 +}
1.110954 +        break;
1.110955 +      case 249: /* collate ::= */
1.110956 +{yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
1.110957 +        break;
1.110958 +      case 251: /* cmd ::= DROP INDEX ifexists fullname */
1.110959 +{sqlite3DropIndex(pParse, yymsp[0].minor.yy347, yymsp[-1].minor.yy392);}
1.110960 +        break;
1.110961 +      case 252: /* cmd ::= VACUUM */
1.110962 +      case 253: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==253);
1.110963 +{sqlite3Vacuum(pParse);}
1.110964 +        break;
1.110965 +      case 254: /* cmd ::= PRAGMA nm dbnm */
1.110966 +{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
1.110967 +        break;
1.110968 +      case 255: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
1.110969 +{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
1.110970 +        break;
1.110971 +      case 256: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
1.110972 +{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
1.110973 +        break;
1.110974 +      case 257: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
1.110975 +{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
1.110976 +        break;
1.110977 +      case 258: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
1.110978 +{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
1.110979 +        break;
1.110980 +      case 268: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
1.110981 +{
1.110982 +  Token all;
1.110983 +  all.z = yymsp[-3].minor.yy0.z;
1.110984 +  all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
1.110985 +  sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy327, &all);
1.110986 +}
1.110987 +        break;
1.110988 +      case 269: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
1.110989 +{
1.110990 +  sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy392, yymsp[-4].minor.yy410.a, yymsp[-4].minor.yy410.b, yymsp[-2].minor.yy347, yymsp[0].minor.yy122, yymsp[-10].minor.yy392, yymsp[-8].minor.yy392);
1.110991 +  yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
1.110992 +}
1.110993 +        break;
1.110994 +      case 270: /* trigger_time ::= BEFORE */
1.110995 +      case 273: /* trigger_time ::= */ yytestcase(yyruleno==273);
1.110996 +{ yygotominor.yy392 = TK_BEFORE; }
1.110997 +        break;
1.110998 +      case 271: /* trigger_time ::= AFTER */
1.110999 +{ yygotominor.yy392 = TK_AFTER;  }
1.111000 +        break;
1.111001 +      case 272: /* trigger_time ::= INSTEAD OF */
1.111002 +{ yygotominor.yy392 = TK_INSTEAD;}
1.111003 +        break;
1.111004 +      case 274: /* trigger_event ::= DELETE|INSERT */
1.111005 +      case 275: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==275);
1.111006 +{yygotominor.yy410.a = yymsp[0].major; yygotominor.yy410.b = 0;}
1.111007 +        break;
1.111008 +      case 276: /* trigger_event ::= UPDATE OF inscollist */
1.111009 +{yygotominor.yy410.a = TK_UPDATE; yygotominor.yy410.b = yymsp[0].minor.yy180;}
1.111010 +        break;
1.111011 +      case 279: /* when_clause ::= */
1.111012 +      case 301: /* key_opt ::= */ yytestcase(yyruleno==301);
1.111013 +{ yygotominor.yy122 = 0; }
1.111014 +        break;
1.111015 +      case 280: /* when_clause ::= WHEN expr */
1.111016 +      case 302: /* key_opt ::= KEY expr */ yytestcase(yyruleno==302);
1.111017 +{ yygotominor.yy122 = yymsp[0].minor.yy342.pExpr; }
1.111018 +        break;
1.111019 +      case 281: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
1.111020 +{
1.111021 +  assert( yymsp[-2].minor.yy327!=0 );
1.111022 +  yymsp[-2].minor.yy327->pLast->pNext = yymsp[-1].minor.yy327;
1.111023 +  yymsp[-2].minor.yy327->pLast = yymsp[-1].minor.yy327;
1.111024 +  yygotominor.yy327 = yymsp[-2].minor.yy327;
1.111025 +}
1.111026 +        break;
1.111027 +      case 282: /* trigger_cmd_list ::= trigger_cmd SEMI */
1.111028 +{ 
1.111029 +  assert( yymsp[-1].minor.yy327!=0 );
1.111030 +  yymsp[-1].minor.yy327->pLast = yymsp[-1].minor.yy327;
1.111031 +  yygotominor.yy327 = yymsp[-1].minor.yy327;
1.111032 +}
1.111033 +        break;
1.111034 +      case 284: /* trnm ::= nm DOT nm */
1.111035 +{
1.111036 +  yygotominor.yy0 = yymsp[0].minor.yy0;
1.111037 +  sqlite3ErrorMsg(pParse, 
1.111038 +        "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1.111039 +        "statements within triggers");
1.111040 +}
1.111041 +        break;
1.111042 +      case 286: /* tridxby ::= INDEXED BY nm */
1.111043 +{
1.111044 +  sqlite3ErrorMsg(pParse,
1.111045 +        "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1.111046 +        "within triggers");
1.111047 +}
1.111048 +        break;
1.111049 +      case 287: /* tridxby ::= NOT INDEXED */
1.111050 +{
1.111051 +  sqlite3ErrorMsg(pParse,
1.111052 +        "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1.111053 +        "within triggers");
1.111054 +}
1.111055 +        break;
1.111056 +      case 288: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
1.111057 +{ yygotominor.yy327 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy442, yymsp[0].minor.yy122, yymsp[-5].minor.yy258); }
1.111058 +        break;
1.111059 +      case 289: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt valuelist */
1.111060 +{yygotominor.yy327 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy180, yymsp[0].minor.yy487.pList, yymsp[0].minor.yy487.pSelect, yymsp[-4].minor.yy258);}
1.111061 +        break;
1.111062 +      case 290: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
1.111063 +{yygotominor.yy327 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy180, 0, yymsp[0].minor.yy159, yymsp[-4].minor.yy258);}
1.111064 +        break;
1.111065 +      case 291: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
1.111066 +{yygotominor.yy327 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy122);}
1.111067 +        break;
1.111068 +      case 292: /* trigger_cmd ::= select */
1.111069 +{yygotominor.yy327 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy159); }
1.111070 +        break;
1.111071 +      case 293: /* expr ::= RAISE LP IGNORE RP */
1.111072 +{
1.111073 +  yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
1.111074 +  if( yygotominor.yy342.pExpr ){
1.111075 +    yygotominor.yy342.pExpr->affinity = OE_Ignore;
1.111076 +  }
1.111077 +  yygotominor.yy342.zStart = yymsp[-3].minor.yy0.z;
1.111078 +  yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.111079 +}
1.111080 +        break;
1.111081 +      case 294: /* expr ::= RAISE LP raisetype COMMA nm RP */
1.111082 +{
1.111083 +  yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); 
1.111084 +  if( yygotominor.yy342.pExpr ) {
1.111085 +    yygotominor.yy342.pExpr->affinity = (char)yymsp[-3].minor.yy392;
1.111086 +  }
1.111087 +  yygotominor.yy342.zStart = yymsp[-5].minor.yy0.z;
1.111088 +  yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
1.111089 +}
1.111090 +        break;
1.111091 +      case 295: /* raisetype ::= ROLLBACK */
1.111092 +{yygotominor.yy392 = OE_Rollback;}
1.111093 +        break;
1.111094 +      case 297: /* raisetype ::= FAIL */
1.111095 +{yygotominor.yy392 = OE_Fail;}
1.111096 +        break;
1.111097 +      case 298: /* cmd ::= DROP TRIGGER ifexists fullname */
1.111098 +{
1.111099 +  sqlite3DropTrigger(pParse,yymsp[0].minor.yy347,yymsp[-1].minor.yy392);
1.111100 +}
1.111101 +        break;
1.111102 +      case 299: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
1.111103 +{
1.111104 +  sqlite3Attach(pParse, yymsp[-3].minor.yy342.pExpr, yymsp[-1].minor.yy342.pExpr, yymsp[0].minor.yy122);
1.111105 +}
1.111106 +        break;
1.111107 +      case 300: /* cmd ::= DETACH database_kw_opt expr */
1.111108 +{
1.111109 +  sqlite3Detach(pParse, yymsp[0].minor.yy342.pExpr);
1.111110 +}
1.111111 +        break;
1.111112 +      case 305: /* cmd ::= REINDEX */
1.111113 +{sqlite3Reindex(pParse, 0, 0);}
1.111114 +        break;
1.111115 +      case 306: /* cmd ::= REINDEX nm dbnm */
1.111116 +{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
1.111117 +        break;
1.111118 +      case 307: /* cmd ::= ANALYZE */
1.111119 +{sqlite3Analyze(pParse, 0, 0);}
1.111120 +        break;
1.111121 +      case 308: /* cmd ::= ANALYZE nm dbnm */
1.111122 +{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
1.111123 +        break;
1.111124 +      case 309: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
1.111125 +{
1.111126 +  sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy347,&yymsp[0].minor.yy0);
1.111127 +}
1.111128 +        break;
1.111129 +      case 310: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
1.111130 +{
1.111131 +  sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
1.111132 +}
1.111133 +        break;
1.111134 +      case 311: /* add_column_fullname ::= fullname */
1.111135 +{
1.111136 +  pParse->db->lookaside.bEnabled = 0;
1.111137 +  sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy347);
1.111138 +}
1.111139 +        break;
1.111140 +      case 314: /* cmd ::= create_vtab */
1.111141 +{sqlite3VtabFinishParse(pParse,0);}
1.111142 +        break;
1.111143 +      case 315: /* cmd ::= create_vtab LP vtabarglist RP */
1.111144 +{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
1.111145 +        break;
1.111146 +      case 316: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
1.111147 +{
1.111148 +    sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy392);
1.111149 +}
1.111150 +        break;
1.111151 +      case 319: /* vtabarg ::= */
1.111152 +{sqlite3VtabArgInit(pParse);}
1.111153 +        break;
1.111154 +      case 321: /* vtabargtoken ::= ANY */
1.111155 +      case 322: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==322);
1.111156 +      case 323: /* lp ::= LP */ yytestcase(yyruleno==323);
1.111157 +{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
1.111158 +        break;
1.111159 +      default:
1.111160 +      /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
1.111161 +      /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
1.111162 +      /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
1.111163 +      /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
1.111164 +      /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
1.111165 +      /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
1.111166 +      /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
1.111167 +      /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
1.111168 +      /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
1.111169 +      /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
1.111170 +      /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
1.111171 +      /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
1.111172 +      /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
1.111173 +      /* (44) type ::= */ yytestcase(yyruleno==44);
1.111174 +      /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
1.111175 +      /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
1.111176 +      /* (53) carglist ::= carglist ccons */ yytestcase(yyruleno==53);
1.111177 +      /* (54) carglist ::= */ yytestcase(yyruleno==54);
1.111178 +      /* (61) ccons ::= NULL onconf */ yytestcase(yyruleno==61);
1.111179 +      /* (89) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==89);
1.111180 +      /* (90) conslist ::= tcons */ yytestcase(yyruleno==90);
1.111181 +      /* (92) tconscomma ::= */ yytestcase(yyruleno==92);
1.111182 +      /* (277) foreach_clause ::= */ yytestcase(yyruleno==277);
1.111183 +      /* (278) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==278);
1.111184 +      /* (285) tridxby ::= */ yytestcase(yyruleno==285);
1.111185 +      /* (303) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==303);
1.111186 +      /* (304) database_kw_opt ::= */ yytestcase(yyruleno==304);
1.111187 +      /* (312) kwcolumn_opt ::= */ yytestcase(yyruleno==312);
1.111188 +      /* (313) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==313);
1.111189 +      /* (317) vtabarglist ::= vtabarg */ yytestcase(yyruleno==317);
1.111190 +      /* (318) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==318);
1.111191 +      /* (320) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==320);
1.111192 +      /* (324) anylist ::= */ yytestcase(yyruleno==324);
1.111193 +      /* (325) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==325);
1.111194 +      /* (326) anylist ::= anylist ANY */ yytestcase(yyruleno==326);
1.111195 +        break;
1.111196 +  };
1.111197 +  assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
1.111198 +  yygoto = yyRuleInfo[yyruleno].lhs;
1.111199 +  yysize = yyRuleInfo[yyruleno].nrhs;
1.111200 +  yypParser->yyidx -= yysize;
1.111201 +  yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
1.111202 +  if( yyact < YYNSTATE ){
1.111203 +#ifdef NDEBUG
1.111204 +    /* If we are not debugging and the reduce action popped at least
1.111205 +    ** one element off the stack, then we can push the new element back
1.111206 +    ** onto the stack here, and skip the stack overflow test in yy_shift().
1.111207 +    ** That gives a significant speed improvement. */
1.111208 +    if( yysize ){
1.111209 +      yypParser->yyidx++;
1.111210 +      yymsp -= yysize-1;
1.111211 +      yymsp->stateno = (YYACTIONTYPE)yyact;
1.111212 +      yymsp->major = (YYCODETYPE)yygoto;
1.111213 +      yymsp->minor = yygotominor;
1.111214 +    }else
1.111215 +#endif
1.111216 +    {
1.111217 +      yy_shift(yypParser,yyact,yygoto,&yygotominor);
1.111218 +    }
1.111219 +  }else{
1.111220 +    assert( yyact == YYNSTATE + YYNRULE + 1 );
1.111221 +    yy_accept(yypParser);
1.111222 +  }
1.111223 +}
1.111224 +
1.111225 +/*
1.111226 +** The following code executes when the parse fails
1.111227 +*/
1.111228 +#ifndef YYNOERRORRECOVERY
1.111229 +static void yy_parse_failed(
1.111230 +  yyParser *yypParser           /* The parser */
1.111231 +){
1.111232 +  sqlite3ParserARG_FETCH;
1.111233 +#ifndef NDEBUG
1.111234 +  if( yyTraceFILE ){
1.111235 +    fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
1.111236 +  }
1.111237 +#endif
1.111238 +  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
1.111239 +  /* Here code is inserted which will be executed whenever the
1.111240 +  ** parser fails */
1.111241 +  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
1.111242 +}
1.111243 +#endif /* YYNOERRORRECOVERY */
1.111244 +
1.111245 +/*
1.111246 +** The following code executes when a syntax error first occurs.
1.111247 +*/
1.111248 +static void yy_syntax_error(
1.111249 +  yyParser *yypParser,           /* The parser */
1.111250 +  int yymajor,                   /* The major type of the error token */
1.111251 +  YYMINORTYPE yyminor            /* The minor type of the error token */
1.111252 +){
1.111253 +  sqlite3ParserARG_FETCH;
1.111254 +#define TOKEN (yyminor.yy0)
1.111255 +
1.111256 +  UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
1.111257 +  assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
1.111258 +  sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
1.111259 +  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
1.111260 +}
1.111261 +
1.111262 +/*
1.111263 +** The following is executed when the parser accepts
1.111264 +*/
1.111265 +static void yy_accept(
1.111266 +  yyParser *yypParser           /* The parser */
1.111267 +){
1.111268 +  sqlite3ParserARG_FETCH;
1.111269 +#ifndef NDEBUG
1.111270 +  if( yyTraceFILE ){
1.111271 +    fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
1.111272 +  }
1.111273 +#endif
1.111274 +  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
1.111275 +  /* Here code is inserted which will be executed whenever the
1.111276 +  ** parser accepts */
1.111277 +  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
1.111278 +}
1.111279 +
1.111280 +/* The main parser program.
1.111281 +** The first argument is a pointer to a structure obtained from
1.111282 +** "sqlite3ParserAlloc" which describes the current state of the parser.
1.111283 +** The second argument is the major token number.  The third is
1.111284 +** the minor token.  The fourth optional argument is whatever the
1.111285 +** user wants (and specified in the grammar) and is available for
1.111286 +** use by the action routines.
1.111287 +**
1.111288 +** Inputs:
1.111289 +** <ul>
1.111290 +** <li> A pointer to the parser (an opaque structure.)
1.111291 +** <li> The major token number.
1.111292 +** <li> The minor token number.
1.111293 +** <li> An option argument of a grammar-specified type.
1.111294 +** </ul>
1.111295 +**
1.111296 +** Outputs:
1.111297 +** None.
1.111298 +*/
1.111299 +SQLITE_PRIVATE void sqlite3Parser(
1.111300 +  void *yyp,                   /* The parser */
1.111301 +  int yymajor,                 /* The major token code number */
1.111302 +  sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
1.111303 +  sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
1.111304 +){
1.111305 +  YYMINORTYPE yyminorunion;
1.111306 +  int yyact;            /* The parser action. */
1.111307 +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
1.111308 +  int yyendofinput;     /* True if we are at the end of input */
1.111309 +#endif
1.111310 +#ifdef YYERRORSYMBOL
1.111311 +  int yyerrorhit = 0;   /* True if yymajor has invoked an error */
1.111312 +#endif
1.111313 +  yyParser *yypParser;  /* The parser */
1.111314 +
1.111315 +  /* (re)initialize the parser, if necessary */
1.111316 +  yypParser = (yyParser*)yyp;
1.111317 +  if( yypParser->yyidx<0 ){
1.111318 +#if YYSTACKDEPTH<=0
1.111319 +    if( yypParser->yystksz <=0 ){
1.111320 +      /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
1.111321 +      yyminorunion = yyzerominor;
1.111322 +      yyStackOverflow(yypParser, &yyminorunion);
1.111323 +      return;
1.111324 +    }
1.111325 +#endif
1.111326 +    yypParser->yyidx = 0;
1.111327 +    yypParser->yyerrcnt = -1;
1.111328 +    yypParser->yystack[0].stateno = 0;
1.111329 +    yypParser->yystack[0].major = 0;
1.111330 +  }
1.111331 +  yyminorunion.yy0 = yyminor;
1.111332 +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
1.111333 +  yyendofinput = (yymajor==0);
1.111334 +#endif
1.111335 +  sqlite3ParserARG_STORE;
1.111336 +
1.111337 +#ifndef NDEBUG
1.111338 +  if( yyTraceFILE ){
1.111339 +    fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
1.111340 +  }
1.111341 +#endif
1.111342 +
1.111343 +  do{
1.111344 +    yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
1.111345 +    if( yyact<YYNSTATE ){
1.111346 +      yy_shift(yypParser,yyact,yymajor,&yyminorunion);
1.111347 +      yypParser->yyerrcnt--;
1.111348 +      yymajor = YYNOCODE;
1.111349 +    }else if( yyact < YYNSTATE + YYNRULE ){
1.111350 +      yy_reduce(yypParser,yyact-YYNSTATE);
1.111351 +    }else{
1.111352 +      assert( yyact == YY_ERROR_ACTION );
1.111353 +#ifdef YYERRORSYMBOL
1.111354 +      int yymx;
1.111355 +#endif
1.111356 +#ifndef NDEBUG
1.111357 +      if( yyTraceFILE ){
1.111358 +        fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
1.111359 +      }
1.111360 +#endif
1.111361 +#ifdef YYERRORSYMBOL
1.111362 +      /* A syntax error has occurred.
1.111363 +      ** The response to an error depends upon whether or not the
1.111364 +      ** grammar defines an error token "ERROR".  
1.111365 +      **
1.111366 +      ** This is what we do if the grammar does define ERROR:
1.111367 +      **
1.111368 +      **  * Call the %syntax_error function.
1.111369 +      **
1.111370 +      **  * Begin popping the stack until we enter a state where
1.111371 +      **    it is legal to shift the error symbol, then shift
1.111372 +      **    the error symbol.
1.111373 +      **
1.111374 +      **  * Set the error count to three.
1.111375 +      **
1.111376 +      **  * Begin accepting and shifting new tokens.  No new error
1.111377 +      **    processing will occur until three tokens have been
1.111378 +      **    shifted successfully.
1.111379 +      **
1.111380 +      */
1.111381 +      if( yypParser->yyerrcnt<0 ){
1.111382 +        yy_syntax_error(yypParser,yymajor,yyminorunion);
1.111383 +      }
1.111384 +      yymx = yypParser->yystack[yypParser->yyidx].major;
1.111385 +      if( yymx==YYERRORSYMBOL || yyerrorhit ){
1.111386 +#ifndef NDEBUG
1.111387 +        if( yyTraceFILE ){
1.111388 +          fprintf(yyTraceFILE,"%sDiscard input token %s\n",
1.111389 +             yyTracePrompt,yyTokenName[yymajor]);
1.111390 +        }
1.111391 +#endif
1.111392 +        yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
1.111393 +        yymajor = YYNOCODE;
1.111394 +      }else{
1.111395 +         while(
1.111396 +          yypParser->yyidx >= 0 &&
1.111397 +          yymx != YYERRORSYMBOL &&
1.111398 +          (yyact = yy_find_reduce_action(
1.111399 +                        yypParser->yystack[yypParser->yyidx].stateno,
1.111400 +                        YYERRORSYMBOL)) >= YYNSTATE
1.111401 +        ){
1.111402 +          yy_pop_parser_stack(yypParser);
1.111403 +        }
1.111404 +        if( yypParser->yyidx < 0 || yymajor==0 ){
1.111405 +          yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1.111406 +          yy_parse_failed(yypParser);
1.111407 +          yymajor = YYNOCODE;
1.111408 +        }else if( yymx!=YYERRORSYMBOL ){
1.111409 +          YYMINORTYPE u2;
1.111410 +          u2.YYERRSYMDT = 0;
1.111411 +          yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
1.111412 +        }
1.111413 +      }
1.111414 +      yypParser->yyerrcnt = 3;
1.111415 +      yyerrorhit = 1;
1.111416 +#elif defined(YYNOERRORRECOVERY)
1.111417 +      /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
1.111418 +      ** do any kind of error recovery.  Instead, simply invoke the syntax
1.111419 +      ** error routine and continue going as if nothing had happened.
1.111420 +      **
1.111421 +      ** Applications can set this macro (for example inside %include) if
1.111422 +      ** they intend to abandon the parse upon the first syntax error seen.
1.111423 +      */
1.111424 +      yy_syntax_error(yypParser,yymajor,yyminorunion);
1.111425 +      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1.111426 +      yymajor = YYNOCODE;
1.111427 +      
1.111428 +#else  /* YYERRORSYMBOL is not defined */
1.111429 +      /* This is what we do if the grammar does not define ERROR:
1.111430 +      **
1.111431 +      **  * Report an error message, and throw away the input token.
1.111432 +      **
1.111433 +      **  * If the input token is $, then fail the parse.
1.111434 +      **
1.111435 +      ** As before, subsequent error messages are suppressed until
1.111436 +      ** three input tokens have been successfully shifted.
1.111437 +      */
1.111438 +      if( yypParser->yyerrcnt<=0 ){
1.111439 +        yy_syntax_error(yypParser,yymajor,yyminorunion);
1.111440 +      }
1.111441 +      yypParser->yyerrcnt = 3;
1.111442 +      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1.111443 +      if( yyendofinput ){
1.111444 +        yy_parse_failed(yypParser);
1.111445 +      }
1.111446 +      yymajor = YYNOCODE;
1.111447 +#endif
1.111448 +    }
1.111449 +  }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
1.111450 +  return;
1.111451 +}
1.111452 +
1.111453 +/************** End of parse.c ***********************************************/
1.111454 +/************** Begin file tokenize.c ****************************************/
1.111455 +/*
1.111456 +** 2001 September 15
1.111457 +**
1.111458 +** The author disclaims copyright to this source code.  In place of
1.111459 +** a legal notice, here is a blessing:
1.111460 +**
1.111461 +**    May you do good and not evil.
1.111462 +**    May you find forgiveness for yourself and forgive others.
1.111463 +**    May you share freely, never taking more than you give.
1.111464 +**
1.111465 +*************************************************************************
1.111466 +** An tokenizer for SQL
1.111467 +**
1.111468 +** This file contains C code that splits an SQL input string up into
1.111469 +** individual tokens and sends those tokens one-by-one over to the
1.111470 +** parser for analysis.
1.111471 +*/
1.111472 +/* #include <stdlib.h> */
1.111473 +
1.111474 +/*
1.111475 +** The charMap() macro maps alphabetic characters into their
1.111476 +** lower-case ASCII equivalent.  On ASCII machines, this is just
1.111477 +** an upper-to-lower case map.  On EBCDIC machines we also need
1.111478 +** to adjust the encoding.  Only alphabetic characters and underscores
1.111479 +** need to be translated.
1.111480 +*/
1.111481 +#ifdef SQLITE_ASCII
1.111482 +# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
1.111483 +#endif
1.111484 +#ifdef SQLITE_EBCDIC
1.111485 +# define charMap(X) ebcdicToAscii[(unsigned char)X]
1.111486 +const unsigned char ebcdicToAscii[] = {
1.111487 +/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
1.111488 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
1.111489 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
1.111490 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
1.111491 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
1.111492 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
1.111493 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
1.111494 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
1.111495 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
1.111496 +   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
1.111497 +   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
1.111498 +   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
1.111499 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
1.111500 +   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
1.111501 +   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
1.111502 +   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
1.111503 +   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
1.111504 +};
1.111505 +#endif
1.111506 +
1.111507 +/*
1.111508 +** The sqlite3KeywordCode function looks up an identifier to determine if
1.111509 +** it is a keyword.  If it is a keyword, the token code of that keyword is 
1.111510 +** returned.  If the input is not a keyword, TK_ID is returned.
1.111511 +**
1.111512 +** The implementation of this routine was generated by a program,
1.111513 +** mkkeywordhash.h, located in the tool subdirectory of the distribution.
1.111514 +** The output of the mkkeywordhash.c program is written into a file
1.111515 +** named keywordhash.h and then included into this source file by
1.111516 +** the #include below.
1.111517 +*/
1.111518 +/************** Include keywordhash.h in the middle of tokenize.c ************/
1.111519 +/************** Begin file keywordhash.h *************************************/
1.111520 +/***** This file contains automatically generated code ******
1.111521 +**
1.111522 +** The code in this file has been automatically generated by
1.111523 +**
1.111524 +**   sqlite/tool/mkkeywordhash.c
1.111525 +**
1.111526 +** The code in this file implements a function that determines whether
1.111527 +** or not a given identifier is really an SQL keyword.  The same thing
1.111528 +** might be implemented more directly using a hand-written hash table.
1.111529 +** But by using this automatically generated code, the size of the code
1.111530 +** is substantially reduced.  This is important for embedded applications
1.111531 +** on platforms with limited memory.
1.111532 +*/
1.111533 +/* Hash score: 175 */
1.111534 +static int keywordCode(const char *z, int n){
1.111535 +  /* zText[] encodes 811 bytes of keywords in 541 bytes */
1.111536 +  /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
1.111537 +  /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
1.111538 +  /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
1.111539 +  /*   UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE          */
1.111540 +  /*   CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN        */
1.111541 +  /*   SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME         */
1.111542 +  /*   AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS     */
1.111543 +  /*   CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF      */
1.111544 +  /*   ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW         */
1.111545 +  /*   INITIALLY                                                          */
1.111546 +  static const char zText[540] = {
1.111547 +    'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
1.111548 +    'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
1.111549 +    'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
1.111550 +    'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
1.111551 +    'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
1.111552 +    'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
1.111553 +    'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
1.111554 +    'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
1.111555 +    'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
1.111556 +    'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
1.111557 +    'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
1.111558 +    'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
1.111559 +    'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
1.111560 +    'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
1.111561 +    'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
1.111562 +    'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
1.111563 +    'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
1.111564 +    'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
1.111565 +    'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
1.111566 +    'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
1.111567 +    'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
1.111568 +    'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
1.111569 +    'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
1.111570 +    'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
1.111571 +    'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
1.111572 +    'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
1.111573 +    'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
1.111574 +    'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
1.111575 +    'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
1.111576 +    'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
1.111577 +  };
1.111578 +  static const unsigned char aHash[127] = {
1.111579 +      72, 101, 114,  70,   0,  45,   0,   0,  78,   0,  73,   0,   0,
1.111580 +      42,  12,  74,  15,   0, 113,  81,  50, 108,   0,  19,   0,   0,
1.111581 +     118,   0, 116, 111,   0,  22,  89,   0,   9,   0,   0,  66,  67,
1.111582 +       0,  65,   6,   0,  48,  86,  98,   0, 115,  97,   0,   0,  44,
1.111583 +       0,  99,  24,   0,  17,   0, 119,  49,  23,   0,   5, 106,  25,
1.111584 +      92,   0,   0, 121, 102,  56, 120,  53,  28,  51,   0,  87,   0,
1.111585 +      96,  26,   0,  95,   0,   0,   0,  91,  88,  93,  84, 105,  14,
1.111586 +      39, 104,   0,  77,   0,  18,  85, 107,  32,   0, 117,  76, 109,
1.111587 +      58,  46,  80,   0,   0,  90,  40,   0, 112,   0,  36,   0,   0,
1.111588 +      29,   0,  82,  59,  60,   0,  20,  57,   0,  52,
1.111589 +  };
1.111590 +  static const unsigned char aNext[121] = {
1.111591 +       0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
1.111592 +       0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
1.111593 +       0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
1.111594 +       0,   0,   0,   0,  33,   0,  21,   0,   0,   0,  43,   3,  47,
1.111595 +       0,   0,   0,   0,  30,   0,  54,   0,  38,   0,   0,   0,   1,
1.111596 +      62,   0,   0,  63,   0,  41,   0,   0,   0,   0,   0,   0,   0,
1.111597 +      61,   0,   0,   0,   0,  31,  55,  16,  34,  10,   0,   0,   0,
1.111598 +       0,   0,   0,   0,  11,  68,  75,   0,   8,   0, 100,  94,   0,
1.111599 +     103,   0,  83,   0,  71,   0,   0, 110,  27,  37,  69,  79,   0,
1.111600 +      35,  64,   0,   0,
1.111601 +  };
1.111602 +  static const unsigned char aLen[121] = {
1.111603 +       7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
1.111604 +       7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
1.111605 +      11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
1.111606 +       4,   6,   2,   3,   9,   4,   2,   6,   5,   6,   6,   5,   6,
1.111607 +       5,   5,   7,   7,   7,   3,   2,   4,   4,   7,   3,   6,   4,
1.111608 +       7,   6,  12,   6,   9,   4,   6,   5,   4,   7,   6,   5,   6,
1.111609 +       7,   5,   4,   5,   6,   5,   7,   3,   7,  13,   2,   2,   4,
1.111610 +       6,   6,   8,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
1.111611 +       4,   4,   2,   2,   6,   5,   8,   5,   5,   8,   3,   5,   5,
1.111612 +       6,   4,   9,   3,
1.111613 +  };
1.111614 +  static const unsigned short int aOffset[121] = {
1.111615 +       0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
1.111616 +      36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
1.111617 +      86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
1.111618 +     159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
1.111619 +     203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
1.111620 +     248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
1.111621 +     326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
1.111622 +     387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
1.111623 +     462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
1.111624 +     521, 527, 531, 536,
1.111625 +  };
1.111626 +  static const unsigned char aCode[121] = {
1.111627 +    TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
1.111628 +    TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
1.111629 +    TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
1.111630 +    TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
1.111631 +    TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
1.111632 +    TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,    
1.111633 +    TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  
1.111634 +    TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,       
1.111635 +    TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
1.111636 +    TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_ATTACH,     TK_HAVING,     
1.111637 +    TK_GROUP,      TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RELEASE,    
1.111638 +    TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       
1.111639 +    TK_LIKE_KW,    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       
1.111640 +    TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  
1.111641 +    TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    
1.111642 +    TK_PRAGMA,     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      
1.111643 +    TK_WHEN,       TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    
1.111644 +    TK_AND,        TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         
1.111645 +    TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    
1.111646 +    TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   
1.111647 +    TK_IS,         TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    
1.111648 +    TK_LIKE_KW,    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      
1.111649 +    TK_RESTRICT,   TK_JOIN_KW,    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        
1.111650 +    TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  
1.111651 +    TK_ALL,        
1.111652 +  };
1.111653 +  int h, i;
1.111654 +  if( n<2 ) return TK_ID;
1.111655 +  h = ((charMap(z[0])*4) ^
1.111656 +      (charMap(z[n-1])*3) ^
1.111657 +      n) % 127;
1.111658 +  for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
1.111659 +    if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
1.111660 +      testcase( i==0 ); /* REINDEX */
1.111661 +      testcase( i==1 ); /* INDEXED */
1.111662 +      testcase( i==2 ); /* INDEX */
1.111663 +      testcase( i==3 ); /* DESC */
1.111664 +      testcase( i==4 ); /* ESCAPE */
1.111665 +      testcase( i==5 ); /* EACH */
1.111666 +      testcase( i==6 ); /* CHECK */
1.111667 +      testcase( i==7 ); /* KEY */
1.111668 +      testcase( i==8 ); /* BEFORE */
1.111669 +      testcase( i==9 ); /* FOREIGN */
1.111670 +      testcase( i==10 ); /* FOR */
1.111671 +      testcase( i==11 ); /* IGNORE */
1.111672 +      testcase( i==12 ); /* REGEXP */
1.111673 +      testcase( i==13 ); /* EXPLAIN */
1.111674 +      testcase( i==14 ); /* INSTEAD */
1.111675 +      testcase( i==15 ); /* ADD */
1.111676 +      testcase( i==16 ); /* DATABASE */
1.111677 +      testcase( i==17 ); /* AS */
1.111678 +      testcase( i==18 ); /* SELECT */
1.111679 +      testcase( i==19 ); /* TABLE */
1.111680 +      testcase( i==20 ); /* LEFT */
1.111681 +      testcase( i==21 ); /* THEN */
1.111682 +      testcase( i==22 ); /* END */
1.111683 +      testcase( i==23 ); /* DEFERRABLE */
1.111684 +      testcase( i==24 ); /* ELSE */
1.111685 +      testcase( i==25 ); /* EXCEPT */
1.111686 +      testcase( i==26 ); /* TRANSACTION */
1.111687 +      testcase( i==27 ); /* ACTION */
1.111688 +      testcase( i==28 ); /* ON */
1.111689 +      testcase( i==29 ); /* NATURAL */
1.111690 +      testcase( i==30 ); /* ALTER */
1.111691 +      testcase( i==31 ); /* RAISE */
1.111692 +      testcase( i==32 ); /* EXCLUSIVE */
1.111693 +      testcase( i==33 ); /* EXISTS */
1.111694 +      testcase( i==34 ); /* SAVEPOINT */
1.111695 +      testcase( i==35 ); /* INTERSECT */
1.111696 +      testcase( i==36 ); /* TRIGGER */
1.111697 +      testcase( i==37 ); /* REFERENCES */
1.111698 +      testcase( i==38 ); /* CONSTRAINT */
1.111699 +      testcase( i==39 ); /* INTO */
1.111700 +      testcase( i==40 ); /* OFFSET */
1.111701 +      testcase( i==41 ); /* OF */
1.111702 +      testcase( i==42 ); /* SET */
1.111703 +      testcase( i==43 ); /* TEMPORARY */
1.111704 +      testcase( i==44 ); /* TEMP */
1.111705 +      testcase( i==45 ); /* OR */
1.111706 +      testcase( i==46 ); /* UNIQUE */
1.111707 +      testcase( i==47 ); /* QUERY */
1.111708 +      testcase( i==48 ); /* ATTACH */
1.111709 +      testcase( i==49 ); /* HAVING */
1.111710 +      testcase( i==50 ); /* GROUP */
1.111711 +      testcase( i==51 ); /* UPDATE */
1.111712 +      testcase( i==52 ); /* BEGIN */
1.111713 +      testcase( i==53 ); /* INNER */
1.111714 +      testcase( i==54 ); /* RELEASE */
1.111715 +      testcase( i==55 ); /* BETWEEN */
1.111716 +      testcase( i==56 ); /* NOTNULL */
1.111717 +      testcase( i==57 ); /* NOT */
1.111718 +      testcase( i==58 ); /* NO */
1.111719 +      testcase( i==59 ); /* NULL */
1.111720 +      testcase( i==60 ); /* LIKE */
1.111721 +      testcase( i==61 ); /* CASCADE */
1.111722 +      testcase( i==62 ); /* ASC */
1.111723 +      testcase( i==63 ); /* DELETE */
1.111724 +      testcase( i==64 ); /* CASE */
1.111725 +      testcase( i==65 ); /* COLLATE */
1.111726 +      testcase( i==66 ); /* CREATE */
1.111727 +      testcase( i==67 ); /* CURRENT_DATE */
1.111728 +      testcase( i==68 ); /* DETACH */
1.111729 +      testcase( i==69 ); /* IMMEDIATE */
1.111730 +      testcase( i==70 ); /* JOIN */
1.111731 +      testcase( i==71 ); /* INSERT */
1.111732 +      testcase( i==72 ); /* MATCH */
1.111733 +      testcase( i==73 ); /* PLAN */
1.111734 +      testcase( i==74 ); /* ANALYZE */
1.111735 +      testcase( i==75 ); /* PRAGMA */
1.111736 +      testcase( i==76 ); /* ABORT */
1.111737 +      testcase( i==77 ); /* VALUES */
1.111738 +      testcase( i==78 ); /* VIRTUAL */
1.111739 +      testcase( i==79 ); /* LIMIT */
1.111740 +      testcase( i==80 ); /* WHEN */
1.111741 +      testcase( i==81 ); /* WHERE */
1.111742 +      testcase( i==82 ); /* RENAME */
1.111743 +      testcase( i==83 ); /* AFTER */
1.111744 +      testcase( i==84 ); /* REPLACE */
1.111745 +      testcase( i==85 ); /* AND */
1.111746 +      testcase( i==86 ); /* DEFAULT */
1.111747 +      testcase( i==87 ); /* AUTOINCREMENT */
1.111748 +      testcase( i==88 ); /* TO */
1.111749 +      testcase( i==89 ); /* IN */
1.111750 +      testcase( i==90 ); /* CAST */
1.111751 +      testcase( i==91 ); /* COLUMN */
1.111752 +      testcase( i==92 ); /* COMMIT */
1.111753 +      testcase( i==93 ); /* CONFLICT */
1.111754 +      testcase( i==94 ); /* CROSS */
1.111755 +      testcase( i==95 ); /* CURRENT_TIMESTAMP */
1.111756 +      testcase( i==96 ); /* CURRENT_TIME */
1.111757 +      testcase( i==97 ); /* PRIMARY */
1.111758 +      testcase( i==98 ); /* DEFERRED */
1.111759 +      testcase( i==99 ); /* DISTINCT */
1.111760 +      testcase( i==100 ); /* IS */
1.111761 +      testcase( i==101 ); /* DROP */
1.111762 +      testcase( i==102 ); /* FAIL */
1.111763 +      testcase( i==103 ); /* FROM */
1.111764 +      testcase( i==104 ); /* FULL */
1.111765 +      testcase( i==105 ); /* GLOB */
1.111766 +      testcase( i==106 ); /* BY */
1.111767 +      testcase( i==107 ); /* IF */
1.111768 +      testcase( i==108 ); /* ISNULL */
1.111769 +      testcase( i==109 ); /* ORDER */
1.111770 +      testcase( i==110 ); /* RESTRICT */
1.111771 +      testcase( i==111 ); /* OUTER */
1.111772 +      testcase( i==112 ); /* RIGHT */
1.111773 +      testcase( i==113 ); /* ROLLBACK */
1.111774 +      testcase( i==114 ); /* ROW */
1.111775 +      testcase( i==115 ); /* UNION */
1.111776 +      testcase( i==116 ); /* USING */
1.111777 +      testcase( i==117 ); /* VACUUM */
1.111778 +      testcase( i==118 ); /* VIEW */
1.111779 +      testcase( i==119 ); /* INITIALLY */
1.111780 +      testcase( i==120 ); /* ALL */
1.111781 +      return aCode[i];
1.111782 +    }
1.111783 +  }
1.111784 +  return TK_ID;
1.111785 +}
1.111786 +SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
1.111787 +  return keywordCode((char*)z, n);
1.111788 +}
1.111789 +#define SQLITE_N_KEYWORD 121
1.111790 +
1.111791 +/************** End of keywordhash.h *****************************************/
1.111792 +/************** Continuing where we left off in tokenize.c *******************/
1.111793 +
1.111794 +
1.111795 +/*
1.111796 +** If X is a character that can be used in an identifier then
1.111797 +** IdChar(X) will be true.  Otherwise it is false.
1.111798 +**
1.111799 +** For ASCII, any character with the high-order bit set is
1.111800 +** allowed in an identifier.  For 7-bit characters, 
1.111801 +** sqlite3IsIdChar[X] must be 1.
1.111802 +**
1.111803 +** For EBCDIC, the rules are more complex but have the same
1.111804 +** end result.
1.111805 +**
1.111806 +** Ticket #1066.  the SQL standard does not allow '$' in the
1.111807 +** middle of identfiers.  But many SQL implementations do. 
1.111808 +** SQLite will allow '$' in identifiers for compatibility.
1.111809 +** But the feature is undocumented.
1.111810 +*/
1.111811 +#ifdef SQLITE_ASCII
1.111812 +#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
1.111813 +#endif
1.111814 +#ifdef SQLITE_EBCDIC
1.111815 +SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
1.111816 +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
1.111817 +    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
1.111818 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
1.111819 +    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
1.111820 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
1.111821 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
1.111822 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
1.111823 +    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
1.111824 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
1.111825 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
1.111826 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
1.111827 +    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
1.111828 +    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
1.111829 +};
1.111830 +#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
1.111831 +#endif
1.111832 +
1.111833 +
1.111834 +/*
1.111835 +** Return the length of the token that begins at z[0]. 
1.111836 +** Store the token type in *tokenType before returning.
1.111837 +*/
1.111838 +SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
1.111839 +  int i, c;
1.111840 +  switch( *z ){
1.111841 +    case ' ': case '\t': case '\n': case '\f': case '\r': {
1.111842 +      testcase( z[0]==' ' );
1.111843 +      testcase( z[0]=='\t' );
1.111844 +      testcase( z[0]=='\n' );
1.111845 +      testcase( z[0]=='\f' );
1.111846 +      testcase( z[0]=='\r' );
1.111847 +      for(i=1; sqlite3Isspace(z[i]); i++){}
1.111848 +      *tokenType = TK_SPACE;
1.111849 +      return i;
1.111850 +    }
1.111851 +    case '-': {
1.111852 +      if( z[1]=='-' ){
1.111853 +        /* IMP: R-50417-27976 -- syntax diagram for comments */
1.111854 +        for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
1.111855 +        *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
1.111856 +        return i;
1.111857 +      }
1.111858 +      *tokenType = TK_MINUS;
1.111859 +      return 1;
1.111860 +    }
1.111861 +    case '(': {
1.111862 +      *tokenType = TK_LP;
1.111863 +      return 1;
1.111864 +    }
1.111865 +    case ')': {
1.111866 +      *tokenType = TK_RP;
1.111867 +      return 1;
1.111868 +    }
1.111869 +    case ';': {
1.111870 +      *tokenType = TK_SEMI;
1.111871 +      return 1;
1.111872 +    }
1.111873 +    case '+': {
1.111874 +      *tokenType = TK_PLUS;
1.111875 +      return 1;
1.111876 +    }
1.111877 +    case '*': {
1.111878 +      *tokenType = TK_STAR;
1.111879 +      return 1;
1.111880 +    }
1.111881 +    case '/': {
1.111882 +      if( z[1]!='*' || z[2]==0 ){
1.111883 +        *tokenType = TK_SLASH;
1.111884 +        return 1;
1.111885 +      }
1.111886 +      /* IMP: R-50417-27976 -- syntax diagram for comments */
1.111887 +      for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
1.111888 +      if( c ) i++;
1.111889 +      *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
1.111890 +      return i;
1.111891 +    }
1.111892 +    case '%': {
1.111893 +      *tokenType = TK_REM;
1.111894 +      return 1;
1.111895 +    }
1.111896 +    case '=': {
1.111897 +      *tokenType = TK_EQ;
1.111898 +      return 1 + (z[1]=='=');
1.111899 +    }
1.111900 +    case '<': {
1.111901 +      if( (c=z[1])=='=' ){
1.111902 +        *tokenType = TK_LE;
1.111903 +        return 2;
1.111904 +      }else if( c=='>' ){
1.111905 +        *tokenType = TK_NE;
1.111906 +        return 2;
1.111907 +      }else if( c=='<' ){
1.111908 +        *tokenType = TK_LSHIFT;
1.111909 +        return 2;
1.111910 +      }else{
1.111911 +        *tokenType = TK_LT;
1.111912 +        return 1;
1.111913 +      }
1.111914 +    }
1.111915 +    case '>': {
1.111916 +      if( (c=z[1])=='=' ){
1.111917 +        *tokenType = TK_GE;
1.111918 +        return 2;
1.111919 +      }else if( c=='>' ){
1.111920 +        *tokenType = TK_RSHIFT;
1.111921 +        return 2;
1.111922 +      }else{
1.111923 +        *tokenType = TK_GT;
1.111924 +        return 1;
1.111925 +      }
1.111926 +    }
1.111927 +    case '!': {
1.111928 +      if( z[1]!='=' ){
1.111929 +        *tokenType = TK_ILLEGAL;
1.111930 +        return 2;
1.111931 +      }else{
1.111932 +        *tokenType = TK_NE;
1.111933 +        return 2;
1.111934 +      }
1.111935 +    }
1.111936 +    case '|': {
1.111937 +      if( z[1]!='|' ){
1.111938 +        *tokenType = TK_BITOR;
1.111939 +        return 1;
1.111940 +      }else{
1.111941 +        *tokenType = TK_CONCAT;
1.111942 +        return 2;
1.111943 +      }
1.111944 +    }
1.111945 +    case ',': {
1.111946 +      *tokenType = TK_COMMA;
1.111947 +      return 1;
1.111948 +    }
1.111949 +    case '&': {
1.111950 +      *tokenType = TK_BITAND;
1.111951 +      return 1;
1.111952 +    }
1.111953 +    case '~': {
1.111954 +      *tokenType = TK_BITNOT;
1.111955 +      return 1;
1.111956 +    }
1.111957 +    case '`':
1.111958 +    case '\'':
1.111959 +    case '"': {
1.111960 +      int delim = z[0];
1.111961 +      testcase( delim=='`' );
1.111962 +      testcase( delim=='\'' );
1.111963 +      testcase( delim=='"' );
1.111964 +      for(i=1; (c=z[i])!=0; i++){
1.111965 +        if( c==delim ){
1.111966 +          if( z[i+1]==delim ){
1.111967 +            i++;
1.111968 +          }else{
1.111969 +            break;
1.111970 +          }
1.111971 +        }
1.111972 +      }
1.111973 +      if( c=='\'' ){
1.111974 +        *tokenType = TK_STRING;
1.111975 +        return i+1;
1.111976 +      }else if( c!=0 ){
1.111977 +        *tokenType = TK_ID;
1.111978 +        return i+1;
1.111979 +      }else{
1.111980 +        *tokenType = TK_ILLEGAL;
1.111981 +        return i;
1.111982 +      }
1.111983 +    }
1.111984 +    case '.': {
1.111985 +#ifndef SQLITE_OMIT_FLOATING_POINT
1.111986 +      if( !sqlite3Isdigit(z[1]) )
1.111987 +#endif
1.111988 +      {
1.111989 +        *tokenType = TK_DOT;
1.111990 +        return 1;
1.111991 +      }
1.111992 +      /* If the next character is a digit, this is a floating point
1.111993 +      ** number that begins with ".".  Fall thru into the next case */
1.111994 +    }
1.111995 +    case '0': case '1': case '2': case '3': case '4':
1.111996 +    case '5': case '6': case '7': case '8': case '9': {
1.111997 +      testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
1.111998 +      testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
1.111999 +      testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
1.112000 +      testcase( z[0]=='9' );
1.112001 +      *tokenType = TK_INTEGER;
1.112002 +      for(i=0; sqlite3Isdigit(z[i]); i++){}
1.112003 +#ifndef SQLITE_OMIT_FLOATING_POINT
1.112004 +      if( z[i]=='.' ){
1.112005 +        i++;
1.112006 +        while( sqlite3Isdigit(z[i]) ){ i++; }
1.112007 +        *tokenType = TK_FLOAT;
1.112008 +      }
1.112009 +      if( (z[i]=='e' || z[i]=='E') &&
1.112010 +           ( sqlite3Isdigit(z[i+1]) 
1.112011 +            || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
1.112012 +           )
1.112013 +      ){
1.112014 +        i += 2;
1.112015 +        while( sqlite3Isdigit(z[i]) ){ i++; }
1.112016 +        *tokenType = TK_FLOAT;
1.112017 +      }
1.112018 +#endif
1.112019 +      while( IdChar(z[i]) ){
1.112020 +        *tokenType = TK_ILLEGAL;
1.112021 +        i++;
1.112022 +      }
1.112023 +      return i;
1.112024 +    }
1.112025 +    case '[': {
1.112026 +      for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
1.112027 +      *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
1.112028 +      return i;
1.112029 +    }
1.112030 +    case '?': {
1.112031 +      *tokenType = TK_VARIABLE;
1.112032 +      for(i=1; sqlite3Isdigit(z[i]); i++){}
1.112033 +      return i;
1.112034 +    }
1.112035 +    case '#': {
1.112036 +      for(i=1; sqlite3Isdigit(z[i]); i++){}
1.112037 +      if( i>1 ){
1.112038 +        /* Parameters of the form #NNN (where NNN is a number) are used
1.112039 +        ** internally by sqlite3NestedParse.  */
1.112040 +        *tokenType = TK_REGISTER;
1.112041 +        return i;
1.112042 +      }
1.112043 +      /* Fall through into the next case if the '#' is not followed by
1.112044 +      ** a digit. Try to match #AAAA where AAAA is a parameter name. */
1.112045 +    }
1.112046 +#ifndef SQLITE_OMIT_TCL_VARIABLE
1.112047 +    case '$':
1.112048 +#endif
1.112049 +    case '@':  /* For compatibility with MS SQL Server */
1.112050 +    case ':': {
1.112051 +      int n = 0;
1.112052 +      testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
1.112053 +      *tokenType = TK_VARIABLE;
1.112054 +      for(i=1; (c=z[i])!=0; i++){
1.112055 +        if( IdChar(c) ){
1.112056 +          n++;
1.112057 +#ifndef SQLITE_OMIT_TCL_VARIABLE
1.112058 +        }else if( c=='(' && n>0 ){
1.112059 +          do{
1.112060 +            i++;
1.112061 +          }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
1.112062 +          if( c==')' ){
1.112063 +            i++;
1.112064 +          }else{
1.112065 +            *tokenType = TK_ILLEGAL;
1.112066 +          }
1.112067 +          break;
1.112068 +        }else if( c==':' && z[i+1]==':' ){
1.112069 +          i++;
1.112070 +#endif
1.112071 +        }else{
1.112072 +          break;
1.112073 +        }
1.112074 +      }
1.112075 +      if( n==0 ) *tokenType = TK_ILLEGAL;
1.112076 +      return i;
1.112077 +    }
1.112078 +#ifndef SQLITE_OMIT_BLOB_LITERAL
1.112079 +    case 'x': case 'X': {
1.112080 +      testcase( z[0]=='x' ); testcase( z[0]=='X' );
1.112081 +      if( z[1]=='\'' ){
1.112082 +        *tokenType = TK_BLOB;
1.112083 +        for(i=2; sqlite3Isxdigit(z[i]); i++){}
1.112084 +        if( z[i]!='\'' || i%2 ){
1.112085 +          *tokenType = TK_ILLEGAL;
1.112086 +          while( z[i] && z[i]!='\'' ){ i++; }
1.112087 +        }
1.112088 +        if( z[i] ) i++;
1.112089 +        return i;
1.112090 +      }
1.112091 +      /* Otherwise fall through to the next case */
1.112092 +    }
1.112093 +#endif
1.112094 +    default: {
1.112095 +      if( !IdChar(*z) ){
1.112096 +        break;
1.112097 +      }
1.112098 +      for(i=1; IdChar(z[i]); i++){}
1.112099 +      *tokenType = keywordCode((char*)z, i);
1.112100 +      return i;
1.112101 +    }
1.112102 +  }
1.112103 +  *tokenType = TK_ILLEGAL;
1.112104 +  return 1;
1.112105 +}
1.112106 +
1.112107 +/*
1.112108 +** Run the parser on the given SQL string.  The parser structure is
1.112109 +** passed in.  An SQLITE_ status code is returned.  If an error occurs
1.112110 +** then an and attempt is made to write an error message into 
1.112111 +** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
1.112112 +** error message.
1.112113 +*/
1.112114 +SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
1.112115 +  int nErr = 0;                   /* Number of errors encountered */
1.112116 +  int i;                          /* Loop counter */
1.112117 +  void *pEngine;                  /* The LEMON-generated LALR(1) parser */
1.112118 +  int tokenType;                  /* type of the next token */
1.112119 +  int lastTokenParsed = -1;       /* type of the previous token */
1.112120 +  u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
1.112121 +  sqlite3 *db = pParse->db;       /* The database connection */
1.112122 +  int mxSqlLen;                   /* Max length of an SQL string */
1.112123 +
1.112124 +
1.112125 +  mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
1.112126 +  if( db->activeVdbeCnt==0 ){
1.112127 +    db->u1.isInterrupted = 0;
1.112128 +  }
1.112129 +  pParse->rc = SQLITE_OK;
1.112130 +  pParse->zTail = zSql;
1.112131 +  i = 0;
1.112132 +  assert( pzErrMsg!=0 );
1.112133 +  pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
1.112134 +  if( pEngine==0 ){
1.112135 +    db->mallocFailed = 1;
1.112136 +    return SQLITE_NOMEM;
1.112137 +  }
1.112138 +  assert( pParse->pNewTable==0 );
1.112139 +  assert( pParse->pNewTrigger==0 );
1.112140 +  assert( pParse->nVar==0 );
1.112141 +  assert( pParse->nzVar==0 );
1.112142 +  assert( pParse->azVar==0 );
1.112143 +  enableLookaside = db->lookaside.bEnabled;
1.112144 +  if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
1.112145 +  while( !db->mallocFailed && zSql[i]!=0 ){
1.112146 +    assert( i>=0 );
1.112147 +    pParse->sLastToken.z = &zSql[i];
1.112148 +    pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
1.112149 +    i += pParse->sLastToken.n;
1.112150 +    if( i>mxSqlLen ){
1.112151 +      pParse->rc = SQLITE_TOOBIG;
1.112152 +      break;
1.112153 +    }
1.112154 +    switch( tokenType ){
1.112155 +      case TK_SPACE: {
1.112156 +        if( db->u1.isInterrupted ){
1.112157 +          sqlite3ErrorMsg(pParse, "interrupt");
1.112158 +          pParse->rc = SQLITE_INTERRUPT;
1.112159 +          goto abort_parse;
1.112160 +        }
1.112161 +        break;
1.112162 +      }
1.112163 +      case TK_ILLEGAL: {
1.112164 +        sqlite3DbFree(db, *pzErrMsg);
1.112165 +        *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
1.112166 +                        &pParse->sLastToken);
1.112167 +        nErr++;
1.112168 +        goto abort_parse;
1.112169 +      }
1.112170 +      case TK_SEMI: {
1.112171 +        pParse->zTail = &zSql[i];
1.112172 +        /* Fall thru into the default case */
1.112173 +      }
1.112174 +      default: {
1.112175 +        sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
1.112176 +        lastTokenParsed = tokenType;
1.112177 +        if( pParse->rc!=SQLITE_OK ){
1.112178 +          goto abort_parse;
1.112179 +        }
1.112180 +        break;
1.112181 +      }
1.112182 +    }
1.112183 +  }
1.112184 +abort_parse:
1.112185 +  if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
1.112186 +    if( lastTokenParsed!=TK_SEMI ){
1.112187 +      sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
1.112188 +      pParse->zTail = &zSql[i];
1.112189 +    }
1.112190 +    sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
1.112191 +  }
1.112192 +#ifdef YYTRACKMAXSTACKDEPTH
1.112193 +  sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
1.112194 +      sqlite3ParserStackPeak(pEngine)
1.112195 +  );
1.112196 +#endif /* YYDEBUG */
1.112197 +  sqlite3ParserFree(pEngine, sqlite3_free);
1.112198 +  db->lookaside.bEnabled = enableLookaside;
1.112199 +  if( db->mallocFailed ){
1.112200 +    pParse->rc = SQLITE_NOMEM;
1.112201 +  }
1.112202 +  if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
1.112203 +    sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
1.112204 +  }
1.112205 +  assert( pzErrMsg!=0 );
1.112206 +  if( pParse->zErrMsg ){
1.112207 +    *pzErrMsg = pParse->zErrMsg;
1.112208 +    sqlite3_log(pParse->rc, "%s", *pzErrMsg);
1.112209 +    pParse->zErrMsg = 0;
1.112210 +    nErr++;
1.112211 +  }
1.112212 +  if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
1.112213 +    sqlite3VdbeDelete(pParse->pVdbe);
1.112214 +    pParse->pVdbe = 0;
1.112215 +  }
1.112216 +#ifndef SQLITE_OMIT_SHARED_CACHE
1.112217 +  if( pParse->nested==0 ){
1.112218 +    sqlite3DbFree(db, pParse->aTableLock);
1.112219 +    pParse->aTableLock = 0;
1.112220 +    pParse->nTableLock = 0;
1.112221 +  }
1.112222 +#endif
1.112223 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.112224 +  sqlite3_free(pParse->apVtabLock);
1.112225 +#endif
1.112226 +
1.112227 +  if( !IN_DECLARE_VTAB ){
1.112228 +    /* If the pParse->declareVtab flag is set, do not delete any table 
1.112229 +    ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
1.112230 +    ** will take responsibility for freeing the Table structure.
1.112231 +    */
1.112232 +    sqlite3DeleteTable(db, pParse->pNewTable);
1.112233 +  }
1.112234 +
1.112235 +  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
1.112236 +  for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
1.112237 +  sqlite3DbFree(db, pParse->azVar);
1.112238 +  sqlite3DbFree(db, pParse->aAlias);
1.112239 +  while( pParse->pAinc ){
1.112240 +    AutoincInfo *p = pParse->pAinc;
1.112241 +    pParse->pAinc = p->pNext;
1.112242 +    sqlite3DbFree(db, p);
1.112243 +  }
1.112244 +  while( pParse->pZombieTab ){
1.112245 +    Table *p = pParse->pZombieTab;
1.112246 +    pParse->pZombieTab = p->pNextZombie;
1.112247 +    sqlite3DeleteTable(db, p);
1.112248 +  }
1.112249 +  if( nErr>0 && pParse->rc==SQLITE_OK ){
1.112250 +    pParse->rc = SQLITE_ERROR;
1.112251 +  }
1.112252 +  return nErr;
1.112253 +}
1.112254 +
1.112255 +/************** End of tokenize.c ********************************************/
1.112256 +/************** Begin file complete.c ****************************************/
1.112257 +/*
1.112258 +** 2001 September 15
1.112259 +**
1.112260 +** The author disclaims copyright to this source code.  In place of
1.112261 +** a legal notice, here is a blessing:
1.112262 +**
1.112263 +**    May you do good and not evil.
1.112264 +**    May you find forgiveness for yourself and forgive others.
1.112265 +**    May you share freely, never taking more than you give.
1.112266 +**
1.112267 +*************************************************************************
1.112268 +** An tokenizer for SQL
1.112269 +**
1.112270 +** This file contains C code that implements the sqlite3_complete() API.
1.112271 +** This code used to be part of the tokenizer.c source file.  But by
1.112272 +** separating it out, the code will be automatically omitted from
1.112273 +** static links that do not use it.
1.112274 +*/
1.112275 +#ifndef SQLITE_OMIT_COMPLETE
1.112276 +
1.112277 +/*
1.112278 +** This is defined in tokenize.c.  We just have to import the definition.
1.112279 +*/
1.112280 +#ifndef SQLITE_AMALGAMATION
1.112281 +#ifdef SQLITE_ASCII
1.112282 +#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
1.112283 +#endif
1.112284 +#ifdef SQLITE_EBCDIC
1.112285 +SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
1.112286 +#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
1.112287 +#endif
1.112288 +#endif /* SQLITE_AMALGAMATION */
1.112289 +
1.112290 +
1.112291 +/*
1.112292 +** Token types used by the sqlite3_complete() routine.  See the header
1.112293 +** comments on that procedure for additional information.
1.112294 +*/
1.112295 +#define tkSEMI    0
1.112296 +#define tkWS      1
1.112297 +#define tkOTHER   2
1.112298 +#ifndef SQLITE_OMIT_TRIGGER
1.112299 +#define tkEXPLAIN 3
1.112300 +#define tkCREATE  4
1.112301 +#define tkTEMP    5
1.112302 +#define tkTRIGGER 6
1.112303 +#define tkEND     7
1.112304 +#endif
1.112305 +
1.112306 +/*
1.112307 +** Return TRUE if the given SQL string ends in a semicolon.
1.112308 +**
1.112309 +** Special handling is require for CREATE TRIGGER statements.
1.112310 +** Whenever the CREATE TRIGGER keywords are seen, the statement
1.112311 +** must end with ";END;".
1.112312 +**
1.112313 +** This implementation uses a state machine with 8 states:
1.112314 +**
1.112315 +**   (0) INVALID   We have not yet seen a non-whitespace character.
1.112316 +**
1.112317 +**   (1) START     At the beginning or end of an SQL statement.  This routine
1.112318 +**                 returns 1 if it ends in the START state and 0 if it ends
1.112319 +**                 in any other state.
1.112320 +**
1.112321 +**   (2) NORMAL    We are in the middle of statement which ends with a single
1.112322 +**                 semicolon.
1.112323 +**
1.112324 +**   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
1.112325 +**                 a statement.
1.112326 +**
1.112327 +**   (4) CREATE    The keyword CREATE has been seen at the beginning of a
1.112328 +**                 statement, possibly preceeded by EXPLAIN and/or followed by
1.112329 +**                 TEMP or TEMPORARY
1.112330 +**
1.112331 +**   (5) TRIGGER   We are in the middle of a trigger definition that must be
1.112332 +**                 ended by a semicolon, the keyword END, and another semicolon.
1.112333 +**
1.112334 +**   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
1.112335 +**                 the end of a trigger definition.
1.112336 +**
1.112337 +**   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
1.112338 +**                 of a trigger difinition.
1.112339 +**
1.112340 +** Transitions between states above are determined by tokens extracted
1.112341 +** from the input.  The following tokens are significant:
1.112342 +**
1.112343 +**   (0) tkSEMI      A semicolon.
1.112344 +**   (1) tkWS        Whitespace.
1.112345 +**   (2) tkOTHER     Any other SQL token.
1.112346 +**   (3) tkEXPLAIN   The "explain" keyword.
1.112347 +**   (4) tkCREATE    The "create" keyword.
1.112348 +**   (5) tkTEMP      The "temp" or "temporary" keyword.
1.112349 +**   (6) tkTRIGGER   The "trigger" keyword.
1.112350 +**   (7) tkEND       The "end" keyword.
1.112351 +**
1.112352 +** Whitespace never causes a state transition and is always ignored.
1.112353 +** This means that a SQL string of all whitespace is invalid.
1.112354 +**
1.112355 +** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
1.112356 +** to recognize the end of a trigger can be omitted.  All we have to do
1.112357 +** is look for a semicolon that is not part of an string or comment.
1.112358 +*/
1.112359 +SQLITE_API int sqlite3_complete(const char *zSql){
1.112360 +  u8 state = 0;   /* Current state, using numbers defined in header comment */
1.112361 +  u8 token;       /* Value of the next token */
1.112362 +
1.112363 +#ifndef SQLITE_OMIT_TRIGGER
1.112364 +  /* A complex statement machine used to detect the end of a CREATE TRIGGER
1.112365 +  ** statement.  This is the normal case.
1.112366 +  */
1.112367 +  static const u8 trans[8][8] = {
1.112368 +                     /* Token:                                                */
1.112369 +     /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
1.112370 +     /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
1.112371 +     /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
1.112372 +     /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
1.112373 +     /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
1.112374 +     /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
1.112375 +     /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
1.112376 +     /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
1.112377 +     /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
1.112378 +  };
1.112379 +#else
1.112380 +  /* If triggers are not supported by this compile then the statement machine
1.112381 +  ** used to detect the end of a statement is much simplier
1.112382 +  */
1.112383 +  static const u8 trans[3][3] = {
1.112384 +                     /* Token:           */
1.112385 +     /* State:       **  SEMI  WS  OTHER */
1.112386 +     /* 0 INVALID: */ {    1,  0,     2, },
1.112387 +     /* 1   START: */ {    1,  1,     2, },
1.112388 +     /* 2  NORMAL: */ {    1,  2,     2, },
1.112389 +  };
1.112390 +#endif /* SQLITE_OMIT_TRIGGER */
1.112391 +
1.112392 +  while( *zSql ){
1.112393 +    switch( *zSql ){
1.112394 +      case ';': {  /* A semicolon */
1.112395 +        token = tkSEMI;
1.112396 +        break;
1.112397 +      }
1.112398 +      case ' ':
1.112399 +      case '\r':
1.112400 +      case '\t':
1.112401 +      case '\n':
1.112402 +      case '\f': {  /* White space is ignored */
1.112403 +        token = tkWS;
1.112404 +        break;
1.112405 +      }
1.112406 +      case '/': {   /* C-style comments */
1.112407 +        if( zSql[1]!='*' ){
1.112408 +          token = tkOTHER;
1.112409 +          break;
1.112410 +        }
1.112411 +        zSql += 2;
1.112412 +        while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
1.112413 +        if( zSql[0]==0 ) return 0;
1.112414 +        zSql++;
1.112415 +        token = tkWS;
1.112416 +        break;
1.112417 +      }
1.112418 +      case '-': {   /* SQL-style comments from "--" to end of line */
1.112419 +        if( zSql[1]!='-' ){
1.112420 +          token = tkOTHER;
1.112421 +          break;
1.112422 +        }
1.112423 +        while( *zSql && *zSql!='\n' ){ zSql++; }
1.112424 +        if( *zSql==0 ) return state==1;
1.112425 +        token = tkWS;
1.112426 +        break;
1.112427 +      }
1.112428 +      case '[': {   /* Microsoft-style identifiers in [...] */
1.112429 +        zSql++;
1.112430 +        while( *zSql && *zSql!=']' ){ zSql++; }
1.112431 +        if( *zSql==0 ) return 0;
1.112432 +        token = tkOTHER;
1.112433 +        break;
1.112434 +      }
1.112435 +      case '`':     /* Grave-accent quoted symbols used by MySQL */
1.112436 +      case '"':     /* single- and double-quoted strings */
1.112437 +      case '\'': {
1.112438 +        int c = *zSql;
1.112439 +        zSql++;
1.112440 +        while( *zSql && *zSql!=c ){ zSql++; }
1.112441 +        if( *zSql==0 ) return 0;
1.112442 +        token = tkOTHER;
1.112443 +        break;
1.112444 +      }
1.112445 +      default: {
1.112446 +#ifdef SQLITE_EBCDIC
1.112447 +        unsigned char c;
1.112448 +#endif
1.112449 +        if( IdChar((u8)*zSql) ){
1.112450 +          /* Keywords and unquoted identifiers */
1.112451 +          int nId;
1.112452 +          for(nId=1; IdChar(zSql[nId]); nId++){}
1.112453 +#ifdef SQLITE_OMIT_TRIGGER
1.112454 +          token = tkOTHER;
1.112455 +#else
1.112456 +          switch( *zSql ){
1.112457 +            case 'c': case 'C': {
1.112458 +              if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
1.112459 +                token = tkCREATE;
1.112460 +              }else{
1.112461 +                token = tkOTHER;
1.112462 +              }
1.112463 +              break;
1.112464 +            }
1.112465 +            case 't': case 'T': {
1.112466 +              if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
1.112467 +                token = tkTRIGGER;
1.112468 +              }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
1.112469 +                token = tkTEMP;
1.112470 +              }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
1.112471 +                token = tkTEMP;
1.112472 +              }else{
1.112473 +                token = tkOTHER;
1.112474 +              }
1.112475 +              break;
1.112476 +            }
1.112477 +            case 'e':  case 'E': {
1.112478 +              if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
1.112479 +                token = tkEND;
1.112480 +              }else
1.112481 +#ifndef SQLITE_OMIT_EXPLAIN
1.112482 +              if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
1.112483 +                token = tkEXPLAIN;
1.112484 +              }else
1.112485 +#endif
1.112486 +              {
1.112487 +                token = tkOTHER;
1.112488 +              }
1.112489 +              break;
1.112490 +            }
1.112491 +            default: {
1.112492 +              token = tkOTHER;
1.112493 +              break;
1.112494 +            }
1.112495 +          }
1.112496 +#endif /* SQLITE_OMIT_TRIGGER */
1.112497 +          zSql += nId-1;
1.112498 +        }else{
1.112499 +          /* Operators and special symbols */
1.112500 +          token = tkOTHER;
1.112501 +        }
1.112502 +        break;
1.112503 +      }
1.112504 +    }
1.112505 +    state = trans[state][token];
1.112506 +    zSql++;
1.112507 +  }
1.112508 +  return state==1;
1.112509 +}
1.112510 +
1.112511 +#ifndef SQLITE_OMIT_UTF16
1.112512 +/*
1.112513 +** This routine is the same as the sqlite3_complete() routine described
1.112514 +** above, except that the parameter is required to be UTF-16 encoded, not
1.112515 +** UTF-8.
1.112516 +*/
1.112517 +SQLITE_API int sqlite3_complete16(const void *zSql){
1.112518 +  sqlite3_value *pVal;
1.112519 +  char const *zSql8;
1.112520 +  int rc = SQLITE_NOMEM;
1.112521 +
1.112522 +#ifndef SQLITE_OMIT_AUTOINIT
1.112523 +  rc = sqlite3_initialize();
1.112524 +  if( rc ) return rc;
1.112525 +#endif
1.112526 +  pVal = sqlite3ValueNew(0);
1.112527 +  sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1.112528 +  zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1.112529 +  if( zSql8 ){
1.112530 +    rc = sqlite3_complete(zSql8);
1.112531 +  }else{
1.112532 +    rc = SQLITE_NOMEM;
1.112533 +  }
1.112534 +  sqlite3ValueFree(pVal);
1.112535 +  return sqlite3ApiExit(0, rc);
1.112536 +}
1.112537 +#endif /* SQLITE_OMIT_UTF16 */
1.112538 +#endif /* SQLITE_OMIT_COMPLETE */
1.112539 +
1.112540 +/************** End of complete.c ********************************************/
1.112541 +/************** Begin file main.c ********************************************/
1.112542 +/*
1.112543 +** 2001 September 15
1.112544 +**
1.112545 +** The author disclaims copyright to this source code.  In place of
1.112546 +** a legal notice, here is a blessing:
1.112547 +**
1.112548 +**    May you do good and not evil.
1.112549 +**    May you find forgiveness for yourself and forgive others.
1.112550 +**    May you share freely, never taking more than you give.
1.112551 +**
1.112552 +*************************************************************************
1.112553 +** Main file for the SQLite library.  The routines in this file
1.112554 +** implement the programmer interface to the library.  Routines in
1.112555 +** other files are for internal use by SQLite and should not be
1.112556 +** accessed by users of the library.
1.112557 +*/
1.112558 +
1.112559 +#ifdef SQLITE_ENABLE_FTS3
1.112560 +/************** Include fts3.h in the middle of main.c ***********************/
1.112561 +/************** Begin file fts3.h ********************************************/
1.112562 +/*
1.112563 +** 2006 Oct 10
1.112564 +**
1.112565 +** The author disclaims copyright to this source code.  In place of
1.112566 +** a legal notice, here is a blessing:
1.112567 +**
1.112568 +**    May you do good and not evil.
1.112569 +**    May you find forgiveness for yourself and forgive others.
1.112570 +**    May you share freely, never taking more than you give.
1.112571 +**
1.112572 +******************************************************************************
1.112573 +**
1.112574 +** This header file is used by programs that want to link against the
1.112575 +** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
1.112576 +*/
1.112577 +
1.112578 +#if 0
1.112579 +extern "C" {
1.112580 +#endif  /* __cplusplus */
1.112581 +
1.112582 +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
1.112583 +
1.112584 +#if 0
1.112585 +}  /* extern "C" */
1.112586 +#endif  /* __cplusplus */
1.112587 +
1.112588 +/************** End of fts3.h ************************************************/
1.112589 +/************** Continuing where we left off in main.c ***********************/
1.112590 +#endif
1.112591 +#ifdef SQLITE_ENABLE_RTREE
1.112592 +/************** Include rtree.h in the middle of main.c **********************/
1.112593 +/************** Begin file rtree.h *******************************************/
1.112594 +/*
1.112595 +** 2008 May 26
1.112596 +**
1.112597 +** The author disclaims copyright to this source code.  In place of
1.112598 +** a legal notice, here is a blessing:
1.112599 +**
1.112600 +**    May you do good and not evil.
1.112601 +**    May you find forgiveness for yourself and forgive others.
1.112602 +**    May you share freely, never taking more than you give.
1.112603 +**
1.112604 +******************************************************************************
1.112605 +**
1.112606 +** This header file is used by programs that want to link against the
1.112607 +** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
1.112608 +*/
1.112609 +
1.112610 +#if 0
1.112611 +extern "C" {
1.112612 +#endif  /* __cplusplus */
1.112613 +
1.112614 +SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
1.112615 +
1.112616 +#if 0
1.112617 +}  /* extern "C" */
1.112618 +#endif  /* __cplusplus */
1.112619 +
1.112620 +/************** End of rtree.h ***********************************************/
1.112621 +/************** Continuing where we left off in main.c ***********************/
1.112622 +#endif
1.112623 +#ifdef SQLITE_ENABLE_ICU
1.112624 +/************** Include sqliteicu.h in the middle of main.c ******************/
1.112625 +/************** Begin file sqliteicu.h ***************************************/
1.112626 +/*
1.112627 +** 2008 May 26
1.112628 +**
1.112629 +** The author disclaims copyright to this source code.  In place of
1.112630 +** a legal notice, here is a blessing:
1.112631 +**
1.112632 +**    May you do good and not evil.
1.112633 +**    May you find forgiveness for yourself and forgive others.
1.112634 +**    May you share freely, never taking more than you give.
1.112635 +**
1.112636 +******************************************************************************
1.112637 +**
1.112638 +** This header file is used by programs that want to link against the
1.112639 +** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
1.112640 +*/
1.112641 +
1.112642 +#if 0
1.112643 +extern "C" {
1.112644 +#endif  /* __cplusplus */
1.112645 +
1.112646 +SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
1.112647 +
1.112648 +#if 0
1.112649 +}  /* extern "C" */
1.112650 +#endif  /* __cplusplus */
1.112651 +
1.112652 +
1.112653 +/************** End of sqliteicu.h *******************************************/
1.112654 +/************** Continuing where we left off in main.c ***********************/
1.112655 +#endif
1.112656 +
1.112657 +#ifndef SQLITE_AMALGAMATION
1.112658 +/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
1.112659 +** contains the text of SQLITE_VERSION macro. 
1.112660 +*/
1.112661 +SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
1.112662 +#endif
1.112663 +
1.112664 +/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
1.112665 +** a pointer to the to the sqlite3_version[] string constant. 
1.112666 +*/
1.112667 +SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
1.112668 +
1.112669 +/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
1.112670 +** pointer to a string constant whose value is the same as the
1.112671 +** SQLITE_SOURCE_ID C preprocessor macro. 
1.112672 +*/
1.112673 +SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
1.112674 +
1.112675 +/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
1.112676 +** returns an integer equal to SQLITE_VERSION_NUMBER.
1.112677 +*/
1.112678 +SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
1.112679 +
1.112680 +/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
1.112681 +** zero if and only if SQLite was compiled with mutexing code omitted due to
1.112682 +** the SQLITE_THREADSAFE compile-time option being set to 0.
1.112683 +*/
1.112684 +SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
1.112685 +
1.112686 +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1.112687 +/*
1.112688 +** If the following function pointer is not NULL and if
1.112689 +** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
1.112690 +** I/O active are written using this function.  These messages
1.112691 +** are intended for debugging activity only.
1.112692 +*/
1.112693 +SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
1.112694 +#endif
1.112695 +
1.112696 +/*
1.112697 +** If the following global variable points to a string which is the
1.112698 +** name of a directory, then that directory will be used to store
1.112699 +** temporary files.
1.112700 +**
1.112701 +** See also the "PRAGMA temp_store_directory" SQL command.
1.112702 +*/
1.112703 +SQLITE_API char *sqlite3_temp_directory = 0;
1.112704 +
1.112705 +/*
1.112706 +** If the following global variable points to a string which is the
1.112707 +** name of a directory, then that directory will be used to store
1.112708 +** all database files specified with a relative pathname.
1.112709 +**
1.112710 +** See also the "PRAGMA data_store_directory" SQL command.
1.112711 +*/
1.112712 +SQLITE_API char *sqlite3_data_directory = 0;
1.112713 +
1.112714 +/*
1.112715 +** Initialize SQLite.  
1.112716 +**
1.112717 +** This routine must be called to initialize the memory allocation,
1.112718 +** VFS, and mutex subsystems prior to doing any serious work with
1.112719 +** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
1.112720 +** this routine will be called automatically by key routines such as
1.112721 +** sqlite3_open().  
1.112722 +**
1.112723 +** This routine is a no-op except on its very first call for the process,
1.112724 +** or for the first call after a call to sqlite3_shutdown.
1.112725 +**
1.112726 +** The first thread to call this routine runs the initialization to
1.112727 +** completion.  If subsequent threads call this routine before the first
1.112728 +** thread has finished the initialization process, then the subsequent
1.112729 +** threads must block until the first thread finishes with the initialization.
1.112730 +**
1.112731 +** The first thread might call this routine recursively.  Recursive
1.112732 +** calls to this routine should not block, of course.  Otherwise the
1.112733 +** initialization process would never complete.
1.112734 +**
1.112735 +** Let X be the first thread to enter this routine.  Let Y be some other
1.112736 +** thread.  Then while the initial invocation of this routine by X is
1.112737 +** incomplete, it is required that:
1.112738 +**
1.112739 +**    *  Calls to this routine from Y must block until the outer-most
1.112740 +**       call by X completes.
1.112741 +**
1.112742 +**    *  Recursive calls to this routine from thread X return immediately
1.112743 +**       without blocking.
1.112744 +*/
1.112745 +SQLITE_API int sqlite3_initialize(void){
1.112746 +  MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
1.112747 +  int rc;                                      /* Result code */
1.112748 +
1.112749 +#ifdef SQLITE_OMIT_WSD
1.112750 +  rc = sqlite3_wsd_init(4096, 24);
1.112751 +  if( rc!=SQLITE_OK ){
1.112752 +    return rc;
1.112753 +  }
1.112754 +#endif
1.112755 +
1.112756 +  /* If SQLite is already completely initialized, then this call
1.112757 +  ** to sqlite3_initialize() should be a no-op.  But the initialization
1.112758 +  ** must be complete.  So isInit must not be set until the very end
1.112759 +  ** of this routine.
1.112760 +  */
1.112761 +  if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
1.112762 +
1.112763 +#ifdef SQLITE_ENABLE_SQLLOG
1.112764 +  {
1.112765 +    extern void sqlite3_init_sqllog(void);
1.112766 +    sqlite3_init_sqllog();
1.112767 +  }
1.112768 +#endif
1.112769 +
1.112770 +  /* Make sure the mutex subsystem is initialized.  If unable to 
1.112771 +  ** initialize the mutex subsystem, return early with the error.
1.112772 +  ** If the system is so sick that we are unable to allocate a mutex,
1.112773 +  ** there is not much SQLite is going to be able to do.
1.112774 +  **
1.112775 +  ** The mutex subsystem must take care of serializing its own
1.112776 +  ** initialization.
1.112777 +  */
1.112778 +  rc = sqlite3MutexInit();
1.112779 +  if( rc ) return rc;
1.112780 +
1.112781 +  /* Initialize the malloc() system and the recursive pInitMutex mutex.
1.112782 +  ** This operation is protected by the STATIC_MASTER mutex.  Note that
1.112783 +  ** MutexAlloc() is called for a static mutex prior to initializing the
1.112784 +  ** malloc subsystem - this implies that the allocation of a static
1.112785 +  ** mutex must not require support from the malloc subsystem.
1.112786 +  */
1.112787 +  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
1.112788 +  sqlite3_mutex_enter(pMaster);
1.112789 +  sqlite3GlobalConfig.isMutexInit = 1;
1.112790 +  if( !sqlite3GlobalConfig.isMallocInit ){
1.112791 +    rc = sqlite3MallocInit();
1.112792 +  }
1.112793 +  if( rc==SQLITE_OK ){
1.112794 +    sqlite3GlobalConfig.isMallocInit = 1;
1.112795 +    if( !sqlite3GlobalConfig.pInitMutex ){
1.112796 +      sqlite3GlobalConfig.pInitMutex =
1.112797 +           sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
1.112798 +      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
1.112799 +        rc = SQLITE_NOMEM;
1.112800 +      }
1.112801 +    }
1.112802 +  }
1.112803 +  if( rc==SQLITE_OK ){
1.112804 +    sqlite3GlobalConfig.nRefInitMutex++;
1.112805 +  }
1.112806 +  sqlite3_mutex_leave(pMaster);
1.112807 +
1.112808 +  /* If rc is not SQLITE_OK at this point, then either the malloc
1.112809 +  ** subsystem could not be initialized or the system failed to allocate
1.112810 +  ** the pInitMutex mutex. Return an error in either case.  */
1.112811 +  if( rc!=SQLITE_OK ){
1.112812 +    return rc;
1.112813 +  }
1.112814 +
1.112815 +  /* Do the rest of the initialization under the recursive mutex so
1.112816 +  ** that we will be able to handle recursive calls into
1.112817 +  ** sqlite3_initialize().  The recursive calls normally come through
1.112818 +  ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
1.112819 +  ** recursive calls might also be possible.
1.112820 +  **
1.112821 +  ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
1.112822 +  ** to the xInit method, so the xInit method need not be threadsafe.
1.112823 +  **
1.112824 +  ** The following mutex is what serializes access to the appdef pcache xInit
1.112825 +  ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
1.112826 +  ** call to sqlite3PcacheInitialize().
1.112827 +  */
1.112828 +  sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
1.112829 +  if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
1.112830 +    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
1.112831 +    sqlite3GlobalConfig.inProgress = 1;
1.112832 +    memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
1.112833 +    sqlite3RegisterGlobalFunctions();
1.112834 +    if( sqlite3GlobalConfig.isPCacheInit==0 ){
1.112835 +      rc = sqlite3PcacheInitialize();
1.112836 +    }
1.112837 +    if( rc==SQLITE_OK ){
1.112838 +      sqlite3GlobalConfig.isPCacheInit = 1;
1.112839 +      rc = sqlite3OsInit();
1.112840 +    }
1.112841 +    if( rc==SQLITE_OK ){
1.112842 +      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
1.112843 +          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
1.112844 +      sqlite3GlobalConfig.isInit = 1;
1.112845 +    }
1.112846 +    sqlite3GlobalConfig.inProgress = 0;
1.112847 +  }
1.112848 +  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
1.112849 +
1.112850 +  /* Go back under the static mutex and clean up the recursive
1.112851 +  ** mutex to prevent a resource leak.
1.112852 +  */
1.112853 +  sqlite3_mutex_enter(pMaster);
1.112854 +  sqlite3GlobalConfig.nRefInitMutex--;
1.112855 +  if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
1.112856 +    assert( sqlite3GlobalConfig.nRefInitMutex==0 );
1.112857 +    sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
1.112858 +    sqlite3GlobalConfig.pInitMutex = 0;
1.112859 +  }
1.112860 +  sqlite3_mutex_leave(pMaster);
1.112861 +
1.112862 +  /* The following is just a sanity check to make sure SQLite has
1.112863 +  ** been compiled correctly.  It is important to run this code, but
1.112864 +  ** we don't want to run it too often and soak up CPU cycles for no
1.112865 +  ** reason.  So we run it once during initialization.
1.112866 +  */
1.112867 +#ifndef NDEBUG
1.112868 +#ifndef SQLITE_OMIT_FLOATING_POINT
1.112869 +  /* This section of code's only "output" is via assert() statements. */
1.112870 +  if ( rc==SQLITE_OK ){
1.112871 +    u64 x = (((u64)1)<<63)-1;
1.112872 +    double y;
1.112873 +    assert(sizeof(x)==8);
1.112874 +    assert(sizeof(x)==sizeof(y));
1.112875 +    memcpy(&y, &x, 8);
1.112876 +    assert( sqlite3IsNaN(y) );
1.112877 +  }
1.112878 +#endif
1.112879 +#endif
1.112880 +
1.112881 +  /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
1.112882 +  ** compile-time option.
1.112883 +  */
1.112884 +#ifdef SQLITE_EXTRA_INIT
1.112885 +  if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
1.112886 +    int SQLITE_EXTRA_INIT(const char*);
1.112887 +    rc = SQLITE_EXTRA_INIT(0);
1.112888 +  }
1.112889 +#endif
1.112890 +
1.112891 +  return rc;
1.112892 +}
1.112893 +
1.112894 +/*
1.112895 +** Undo the effects of sqlite3_initialize().  Must not be called while
1.112896 +** there are outstanding database connections or memory allocations or
1.112897 +** while any part of SQLite is otherwise in use in any thread.  This
1.112898 +** routine is not threadsafe.  But it is safe to invoke this routine
1.112899 +** on when SQLite is already shut down.  If SQLite is already shut down
1.112900 +** when this routine is invoked, then this routine is a harmless no-op.
1.112901 +*/
1.112902 +SQLITE_API int sqlite3_shutdown(void){
1.112903 +  if( sqlite3GlobalConfig.isInit ){
1.112904 +#ifdef SQLITE_EXTRA_SHUTDOWN
1.112905 +    void SQLITE_EXTRA_SHUTDOWN(void);
1.112906 +    SQLITE_EXTRA_SHUTDOWN();
1.112907 +#endif
1.112908 +    sqlite3_os_end();
1.112909 +    sqlite3_reset_auto_extension();
1.112910 +    sqlite3GlobalConfig.isInit = 0;
1.112911 +  }
1.112912 +  if( sqlite3GlobalConfig.isPCacheInit ){
1.112913 +    sqlite3PcacheShutdown();
1.112914 +    sqlite3GlobalConfig.isPCacheInit = 0;
1.112915 +  }
1.112916 +  if( sqlite3GlobalConfig.isMallocInit ){
1.112917 +    sqlite3MallocEnd();
1.112918 +    sqlite3GlobalConfig.isMallocInit = 0;
1.112919 +
1.112920 +#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
1.112921 +    /* The heap subsystem has now been shutdown and these values are supposed
1.112922 +    ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
1.112923 +    ** which would rely on that heap subsystem; therefore, make sure these
1.112924 +    ** values cannot refer to heap memory that was just invalidated when the
1.112925 +    ** heap subsystem was shutdown.  This is only done if the current call to
1.112926 +    ** this function resulted in the heap subsystem actually being shutdown.
1.112927 +    */
1.112928 +    sqlite3_data_directory = 0;
1.112929 +    sqlite3_temp_directory = 0;
1.112930 +#endif
1.112931 +  }
1.112932 +  if( sqlite3GlobalConfig.isMutexInit ){
1.112933 +    sqlite3MutexEnd();
1.112934 +    sqlite3GlobalConfig.isMutexInit = 0;
1.112935 +  }
1.112936 +
1.112937 +  return SQLITE_OK;
1.112938 +}
1.112939 +
1.112940 +/*
1.112941 +** This API allows applications to modify the global configuration of
1.112942 +** the SQLite library at run-time.
1.112943 +**
1.112944 +** This routine should only be called when there are no outstanding
1.112945 +** database connections or memory allocations.  This routine is not
1.112946 +** threadsafe.  Failure to heed these warnings can lead to unpredictable
1.112947 +** behavior.
1.112948 +*/
1.112949 +SQLITE_API int sqlite3_config(int op, ...){
1.112950 +  va_list ap;
1.112951 +  int rc = SQLITE_OK;
1.112952 +
1.112953 +  /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
1.112954 +  ** the SQLite library is in use. */
1.112955 +  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
1.112956 +
1.112957 +  va_start(ap, op);
1.112958 +  switch( op ){
1.112959 +
1.112960 +    /* Mutex configuration options are only available in a threadsafe
1.112961 +    ** compile. 
1.112962 +    */
1.112963 +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
1.112964 +    case SQLITE_CONFIG_SINGLETHREAD: {
1.112965 +      /* Disable all mutexing */
1.112966 +      sqlite3GlobalConfig.bCoreMutex = 0;
1.112967 +      sqlite3GlobalConfig.bFullMutex = 0;
1.112968 +      break;
1.112969 +    }
1.112970 +    case SQLITE_CONFIG_MULTITHREAD: {
1.112971 +      /* Disable mutexing of database connections */
1.112972 +      /* Enable mutexing of core data structures */
1.112973 +      sqlite3GlobalConfig.bCoreMutex = 1;
1.112974 +      sqlite3GlobalConfig.bFullMutex = 0;
1.112975 +      break;
1.112976 +    }
1.112977 +    case SQLITE_CONFIG_SERIALIZED: {
1.112978 +      /* Enable all mutexing */
1.112979 +      sqlite3GlobalConfig.bCoreMutex = 1;
1.112980 +      sqlite3GlobalConfig.bFullMutex = 1;
1.112981 +      break;
1.112982 +    }
1.112983 +    case SQLITE_CONFIG_MUTEX: {
1.112984 +      /* Specify an alternative mutex implementation */
1.112985 +      sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
1.112986 +      break;
1.112987 +    }
1.112988 +    case SQLITE_CONFIG_GETMUTEX: {
1.112989 +      /* Retrieve the current mutex implementation */
1.112990 +      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
1.112991 +      break;
1.112992 +    }
1.112993 +#endif
1.112994 +
1.112995 +
1.112996 +    case SQLITE_CONFIG_MALLOC: {
1.112997 +      /* Specify an alternative malloc implementation */
1.112998 +      sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
1.112999 +      break;
1.113000 +    }
1.113001 +    case SQLITE_CONFIG_GETMALLOC: {
1.113002 +      /* Retrieve the current malloc() implementation */
1.113003 +      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
1.113004 +      *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
1.113005 +      break;
1.113006 +    }
1.113007 +    case SQLITE_CONFIG_MEMSTATUS: {
1.113008 +      /* Enable or disable the malloc status collection */
1.113009 +      sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
1.113010 +      break;
1.113011 +    }
1.113012 +    case SQLITE_CONFIG_SCRATCH: {
1.113013 +      /* Designate a buffer for scratch memory space */
1.113014 +      sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
1.113015 +      sqlite3GlobalConfig.szScratch = va_arg(ap, int);
1.113016 +      sqlite3GlobalConfig.nScratch = va_arg(ap, int);
1.113017 +      break;
1.113018 +    }
1.113019 +    case SQLITE_CONFIG_PAGECACHE: {
1.113020 +      /* Designate a buffer for page cache memory space */
1.113021 +      sqlite3GlobalConfig.pPage = va_arg(ap, void*);
1.113022 +      sqlite3GlobalConfig.szPage = va_arg(ap, int);
1.113023 +      sqlite3GlobalConfig.nPage = va_arg(ap, int);
1.113024 +      break;
1.113025 +    }
1.113026 +
1.113027 +    case SQLITE_CONFIG_PCACHE: {
1.113028 +      /* no-op */
1.113029 +      break;
1.113030 +    }
1.113031 +    case SQLITE_CONFIG_GETPCACHE: {
1.113032 +      /* now an error */
1.113033 +      rc = SQLITE_ERROR;
1.113034 +      break;
1.113035 +    }
1.113036 +
1.113037 +    case SQLITE_CONFIG_PCACHE2: {
1.113038 +      /* Specify an alternative page cache implementation */
1.113039 +      sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
1.113040 +      break;
1.113041 +    }
1.113042 +    case SQLITE_CONFIG_GETPCACHE2: {
1.113043 +      if( sqlite3GlobalConfig.pcache2.xInit==0 ){
1.113044 +        sqlite3PCacheSetDefault();
1.113045 +      }
1.113046 +      *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
1.113047 +      break;
1.113048 +    }
1.113049 +
1.113050 +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
1.113051 +    case SQLITE_CONFIG_HEAP: {
1.113052 +      /* Designate a buffer for heap memory space */
1.113053 +      sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
1.113054 +      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
1.113055 +      sqlite3GlobalConfig.mnReq = va_arg(ap, int);
1.113056 +
1.113057 +      if( sqlite3GlobalConfig.mnReq<1 ){
1.113058 +        sqlite3GlobalConfig.mnReq = 1;
1.113059 +      }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
1.113060 +        /* cap min request size at 2^12 */
1.113061 +        sqlite3GlobalConfig.mnReq = (1<<12);
1.113062 +      }
1.113063 +
1.113064 +      if( sqlite3GlobalConfig.pHeap==0 ){
1.113065 +        /* If the heap pointer is NULL, then restore the malloc implementation
1.113066 +        ** back to NULL pointers too.  This will cause the malloc to go
1.113067 +        ** back to its default implementation when sqlite3_initialize() is
1.113068 +        ** run.
1.113069 +        */
1.113070 +        memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
1.113071 +      }else{
1.113072 +        /* The heap pointer is not NULL, then install one of the
1.113073 +        ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
1.113074 +        ** ENABLE_MEMSYS5 is defined, return an error.
1.113075 +        */
1.113076 +#ifdef SQLITE_ENABLE_MEMSYS3
1.113077 +        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
1.113078 +#endif
1.113079 +#ifdef SQLITE_ENABLE_MEMSYS5
1.113080 +        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
1.113081 +#endif
1.113082 +      }
1.113083 +      break;
1.113084 +    }
1.113085 +#endif
1.113086 +
1.113087 +    case SQLITE_CONFIG_LOOKASIDE: {
1.113088 +      sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
1.113089 +      sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
1.113090 +      break;
1.113091 +    }
1.113092 +    
1.113093 +    /* Record a pointer to the logger funcction and its first argument.
1.113094 +    ** The default is NULL.  Logging is disabled if the function pointer is
1.113095 +    ** NULL.
1.113096 +    */
1.113097 +    case SQLITE_CONFIG_LOG: {
1.113098 +      /* MSVC is picky about pulling func ptrs from va lists.
1.113099 +      ** http://support.microsoft.com/kb/47961
1.113100 +      ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
1.113101 +      */
1.113102 +      typedef void(*LOGFUNC_t)(void*,int,const char*);
1.113103 +      sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
1.113104 +      sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
1.113105 +      break;
1.113106 +    }
1.113107 +
1.113108 +    case SQLITE_CONFIG_URI: {
1.113109 +      sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
1.113110 +      break;
1.113111 +    }
1.113112 +
1.113113 +    case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
1.113114 +      sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
1.113115 +      break;
1.113116 +    }
1.113117 +
1.113118 +#ifdef SQLITE_ENABLE_SQLLOG
1.113119 +    case SQLITE_CONFIG_SQLLOG: {
1.113120 +      typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
1.113121 +      sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
1.113122 +      sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
1.113123 +      break;
1.113124 +    }
1.113125 +#endif
1.113126 +
1.113127 +    default: {
1.113128 +      rc = SQLITE_ERROR;
1.113129 +      break;
1.113130 +    }
1.113131 +  }
1.113132 +  va_end(ap);
1.113133 +  return rc;
1.113134 +}
1.113135 +
1.113136 +/*
1.113137 +** Set up the lookaside buffers for a database connection.
1.113138 +** Return SQLITE_OK on success.  
1.113139 +** If lookaside is already active, return SQLITE_BUSY.
1.113140 +**
1.113141 +** The sz parameter is the number of bytes in each lookaside slot.
1.113142 +** The cnt parameter is the number of slots.  If pStart is NULL the
1.113143 +** space for the lookaside memory is obtained from sqlite3_malloc().
1.113144 +** If pStart is not NULL then it is sz*cnt bytes of memory to use for
1.113145 +** the lookaside memory.
1.113146 +*/
1.113147 +static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
1.113148 +  void *pStart;
1.113149 +  if( db->lookaside.nOut ){
1.113150 +    return SQLITE_BUSY;
1.113151 +  }
1.113152 +  /* Free any existing lookaside buffer for this handle before
1.113153 +  ** allocating a new one so we don't have to have space for 
1.113154 +  ** both at the same time.
1.113155 +  */
1.113156 +  if( db->lookaside.bMalloced ){
1.113157 +    sqlite3_free(db->lookaside.pStart);
1.113158 +  }
1.113159 +  /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
1.113160 +  ** than a pointer to be useful.
1.113161 +  */
1.113162 +  sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
1.113163 +  if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
1.113164 +  if( cnt<0 ) cnt = 0;
1.113165 +  if( sz==0 || cnt==0 ){
1.113166 +    sz = 0;
1.113167 +    pStart = 0;
1.113168 +  }else if( pBuf==0 ){
1.113169 +    sqlite3BeginBenignMalloc();
1.113170 +    pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
1.113171 +    sqlite3EndBenignMalloc();
1.113172 +    if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
1.113173 +  }else{
1.113174 +    pStart = pBuf;
1.113175 +  }
1.113176 +  db->lookaside.pStart = pStart;
1.113177 +  db->lookaside.pFree = 0;
1.113178 +  db->lookaside.sz = (u16)sz;
1.113179 +  if( pStart ){
1.113180 +    int i;
1.113181 +    LookasideSlot *p;
1.113182 +    assert( sz > (int)sizeof(LookasideSlot*) );
1.113183 +    p = (LookasideSlot*)pStart;
1.113184 +    for(i=cnt-1; i>=0; i--){
1.113185 +      p->pNext = db->lookaside.pFree;
1.113186 +      db->lookaside.pFree = p;
1.113187 +      p = (LookasideSlot*)&((u8*)p)[sz];
1.113188 +    }
1.113189 +    db->lookaside.pEnd = p;
1.113190 +    db->lookaside.bEnabled = 1;
1.113191 +    db->lookaside.bMalloced = pBuf==0 ?1:0;
1.113192 +  }else{
1.113193 +    db->lookaside.pEnd = 0;
1.113194 +    db->lookaside.bEnabled = 0;
1.113195 +    db->lookaside.bMalloced = 0;
1.113196 +  }
1.113197 +  return SQLITE_OK;
1.113198 +}
1.113199 +
1.113200 +/*
1.113201 +** Return the mutex associated with a database connection.
1.113202 +*/
1.113203 +SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
1.113204 +  return db->mutex;
1.113205 +}
1.113206 +
1.113207 +/*
1.113208 +** Free up as much memory as we can from the given database
1.113209 +** connection.
1.113210 +*/
1.113211 +SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
1.113212 +  int i;
1.113213 +  sqlite3_mutex_enter(db->mutex);
1.113214 +  sqlite3BtreeEnterAll(db);
1.113215 +  for(i=0; i<db->nDb; i++){
1.113216 +    Btree *pBt = db->aDb[i].pBt;
1.113217 +    if( pBt ){
1.113218 +      Pager *pPager = sqlite3BtreePager(pBt);
1.113219 +      sqlite3PagerShrink(pPager);
1.113220 +    }
1.113221 +  }
1.113222 +  sqlite3BtreeLeaveAll(db);
1.113223 +  sqlite3_mutex_leave(db->mutex);
1.113224 +  return SQLITE_OK;
1.113225 +}
1.113226 +
1.113227 +/*
1.113228 +** Configuration settings for an individual database connection
1.113229 +*/
1.113230 +SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
1.113231 +  va_list ap;
1.113232 +  int rc;
1.113233 +  va_start(ap, op);
1.113234 +  switch( op ){
1.113235 +    case SQLITE_DBCONFIG_LOOKASIDE: {
1.113236 +      void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
1.113237 +      int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
1.113238 +      int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
1.113239 +      rc = setupLookaside(db, pBuf, sz, cnt);
1.113240 +      break;
1.113241 +    }
1.113242 +    default: {
1.113243 +      static const struct {
1.113244 +        int op;      /* The opcode */
1.113245 +        u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
1.113246 +      } aFlagOp[] = {
1.113247 +        { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
1.113248 +        { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
1.113249 +      };
1.113250 +      unsigned int i;
1.113251 +      rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
1.113252 +      for(i=0; i<ArraySize(aFlagOp); i++){
1.113253 +        if( aFlagOp[i].op==op ){
1.113254 +          int onoff = va_arg(ap, int);
1.113255 +          int *pRes = va_arg(ap, int*);
1.113256 +          int oldFlags = db->flags;
1.113257 +          if( onoff>0 ){
1.113258 +            db->flags |= aFlagOp[i].mask;
1.113259 +          }else if( onoff==0 ){
1.113260 +            db->flags &= ~aFlagOp[i].mask;
1.113261 +          }
1.113262 +          if( oldFlags!=db->flags ){
1.113263 +            sqlite3ExpirePreparedStatements(db);
1.113264 +          }
1.113265 +          if( pRes ){
1.113266 +            *pRes = (db->flags & aFlagOp[i].mask)!=0;
1.113267 +          }
1.113268 +          rc = SQLITE_OK;
1.113269 +          break;
1.113270 +        }
1.113271 +      }
1.113272 +      break;
1.113273 +    }
1.113274 +  }
1.113275 +  va_end(ap);
1.113276 +  return rc;
1.113277 +}
1.113278 +
1.113279 +
1.113280 +/*
1.113281 +** Return true if the buffer z[0..n-1] contains all spaces.
1.113282 +*/
1.113283 +static int allSpaces(const char *z, int n){
1.113284 +  while( n>0 && z[n-1]==' ' ){ n--; }
1.113285 +  return n==0;
1.113286 +}
1.113287 +
1.113288 +/*
1.113289 +** This is the default collating function named "BINARY" which is always
1.113290 +** available.
1.113291 +**
1.113292 +** If the padFlag argument is not NULL then space padding at the end
1.113293 +** of strings is ignored.  This implements the RTRIM collation.
1.113294 +*/
1.113295 +static int binCollFunc(
1.113296 +  void *padFlag,
1.113297 +  int nKey1, const void *pKey1,
1.113298 +  int nKey2, const void *pKey2
1.113299 +){
1.113300 +  int rc, n;
1.113301 +  n = nKey1<nKey2 ? nKey1 : nKey2;
1.113302 +  rc = memcmp(pKey1, pKey2, n);
1.113303 +  if( rc==0 ){
1.113304 +    if( padFlag
1.113305 +     && allSpaces(((char*)pKey1)+n, nKey1-n)
1.113306 +     && allSpaces(((char*)pKey2)+n, nKey2-n)
1.113307 +    ){
1.113308 +      /* Leave rc unchanged at 0 */
1.113309 +    }else{
1.113310 +      rc = nKey1 - nKey2;
1.113311 +    }
1.113312 +  }
1.113313 +  return rc;
1.113314 +}
1.113315 +
1.113316 +/*
1.113317 +** Another built-in collating sequence: NOCASE. 
1.113318 +**
1.113319 +** This collating sequence is intended to be used for "case independant
1.113320 +** comparison". SQLite's knowledge of upper and lower case equivalents
1.113321 +** extends only to the 26 characters used in the English language.
1.113322 +**
1.113323 +** At the moment there is only a UTF-8 implementation.
1.113324 +*/
1.113325 +static int nocaseCollatingFunc(
1.113326 +  void *NotUsed,
1.113327 +  int nKey1, const void *pKey1,
1.113328 +  int nKey2, const void *pKey2
1.113329 +){
1.113330 +  int r = sqlite3StrNICmp(
1.113331 +      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
1.113332 +  UNUSED_PARAMETER(NotUsed);
1.113333 +  if( 0==r ){
1.113334 +    r = nKey1-nKey2;
1.113335 +  }
1.113336 +  return r;
1.113337 +}
1.113338 +
1.113339 +/*
1.113340 +** Return the ROWID of the most recent insert
1.113341 +*/
1.113342 +SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
1.113343 +  return db->lastRowid;
1.113344 +}
1.113345 +
1.113346 +/*
1.113347 +** Return the number of changes in the most recent call to sqlite3_exec().
1.113348 +*/
1.113349 +SQLITE_API int sqlite3_changes(sqlite3 *db){
1.113350 +  return db->nChange;
1.113351 +}
1.113352 +
1.113353 +/*
1.113354 +** Return the number of changes since the database handle was opened.
1.113355 +*/
1.113356 +SQLITE_API int sqlite3_total_changes(sqlite3 *db){
1.113357 +  return db->nTotalChange;
1.113358 +}
1.113359 +
1.113360 +/*
1.113361 +** Close all open savepoints. This function only manipulates fields of the
1.113362 +** database handle object, it does not close any savepoints that may be open
1.113363 +** at the b-tree/pager level.
1.113364 +*/
1.113365 +SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
1.113366 +  while( db->pSavepoint ){
1.113367 +    Savepoint *pTmp = db->pSavepoint;
1.113368 +    db->pSavepoint = pTmp->pNext;
1.113369 +    sqlite3DbFree(db, pTmp);
1.113370 +  }
1.113371 +  db->nSavepoint = 0;
1.113372 +  db->nStatement = 0;
1.113373 +  db->isTransactionSavepoint = 0;
1.113374 +}
1.113375 +
1.113376 +/*
1.113377 +** Invoke the destructor function associated with FuncDef p, if any. Except,
1.113378 +** if this is not the last copy of the function, do not invoke it. Multiple
1.113379 +** copies of a single function are created when create_function() is called
1.113380 +** with SQLITE_ANY as the encoding.
1.113381 +*/
1.113382 +static void functionDestroy(sqlite3 *db, FuncDef *p){
1.113383 +  FuncDestructor *pDestructor = p->pDestructor;
1.113384 +  if( pDestructor ){
1.113385 +    pDestructor->nRef--;
1.113386 +    if( pDestructor->nRef==0 ){
1.113387 +      pDestructor->xDestroy(pDestructor->pUserData);
1.113388 +      sqlite3DbFree(db, pDestructor);
1.113389 +    }
1.113390 +  }
1.113391 +}
1.113392 +
1.113393 +/*
1.113394 +** Disconnect all sqlite3_vtab objects that belong to database connection
1.113395 +** db. This is called when db is being closed.
1.113396 +*/
1.113397 +static void disconnectAllVtab(sqlite3 *db){
1.113398 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.113399 +  int i;
1.113400 +  sqlite3BtreeEnterAll(db);
1.113401 +  for(i=0; i<db->nDb; i++){
1.113402 +    Schema *pSchema = db->aDb[i].pSchema;
1.113403 +    if( db->aDb[i].pSchema ){
1.113404 +      HashElem *p;
1.113405 +      for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
1.113406 +        Table *pTab = (Table *)sqliteHashData(p);
1.113407 +        if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
1.113408 +      }
1.113409 +    }
1.113410 +  }
1.113411 +  sqlite3BtreeLeaveAll(db);
1.113412 +#else
1.113413 +  UNUSED_PARAMETER(db);
1.113414 +#endif
1.113415 +}
1.113416 +
1.113417 +/*
1.113418 +** Return TRUE if database connection db has unfinalized prepared
1.113419 +** statements or unfinished sqlite3_backup objects.  
1.113420 +*/
1.113421 +static int connectionIsBusy(sqlite3 *db){
1.113422 +  int j;
1.113423 +  assert( sqlite3_mutex_held(db->mutex) );
1.113424 +  if( db->pVdbe ) return 1;
1.113425 +  for(j=0; j<db->nDb; j++){
1.113426 +    Btree *pBt = db->aDb[j].pBt;
1.113427 +    if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
1.113428 +  }
1.113429 +  return 0;
1.113430 +}
1.113431 +
1.113432 +/*
1.113433 +** Close an existing SQLite database
1.113434 +*/
1.113435 +static int sqlite3Close(sqlite3 *db, int forceZombie){
1.113436 +  if( !db ){
1.113437 +    return SQLITE_OK;
1.113438 +  }
1.113439 +  if( !sqlite3SafetyCheckSickOrOk(db) ){
1.113440 +    return SQLITE_MISUSE_BKPT;
1.113441 +  }
1.113442 +  sqlite3_mutex_enter(db->mutex);
1.113443 +
1.113444 +  /* Force xDisconnect calls on all virtual tables */
1.113445 +  disconnectAllVtab(db);
1.113446 +
1.113447 +  /* If a transaction is open, the disconnectAllVtab() call above
1.113448 +  ** will not have called the xDisconnect() method on any virtual
1.113449 +  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
1.113450 +  ** call will do so. We need to do this before the check for active
1.113451 +  ** SQL statements below, as the v-table implementation may be storing
1.113452 +  ** some prepared statements internally.
1.113453 +  */
1.113454 +  sqlite3VtabRollback(db);
1.113455 +
1.113456 +  /* Legacy behavior (sqlite3_close() behavior) is to return
1.113457 +  ** SQLITE_BUSY if the connection can not be closed immediately.
1.113458 +  */
1.113459 +  if( !forceZombie && connectionIsBusy(db) ){
1.113460 +    sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
1.113461 +       "statements or unfinished backups");
1.113462 +    sqlite3_mutex_leave(db->mutex);
1.113463 +    return SQLITE_BUSY;
1.113464 +  }
1.113465 +
1.113466 +#ifdef SQLITE_ENABLE_SQLLOG
1.113467 +  if( sqlite3GlobalConfig.xSqllog ){
1.113468 +    /* Closing the handle. Fourth parameter is passed the value 2. */
1.113469 +    sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
1.113470 +  }
1.113471 +#endif
1.113472 +
1.113473 +  /* Convert the connection into a zombie and then close it.
1.113474 +  */
1.113475 +  db->magic = SQLITE_MAGIC_ZOMBIE;
1.113476 +  sqlite3LeaveMutexAndCloseZombie(db);
1.113477 +  return SQLITE_OK;
1.113478 +}
1.113479 +
1.113480 +/*
1.113481 +** Two variations on the public interface for closing a database
1.113482 +** connection. The sqlite3_close() version returns SQLITE_BUSY and
1.113483 +** leaves the connection option if there are unfinalized prepared
1.113484 +** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
1.113485 +** version forces the connection to become a zombie if there are
1.113486 +** unclosed resources, and arranges for deallocation when the last
1.113487 +** prepare statement or sqlite3_backup closes.
1.113488 +*/
1.113489 +SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
1.113490 +SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
1.113491 +
1.113492 +
1.113493 +/*
1.113494 +** Close the mutex on database connection db.
1.113495 +**
1.113496 +** Furthermore, if database connection db is a zombie (meaning that there
1.113497 +** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
1.113498 +** every sqlite3_stmt has now been finalized and every sqlite3_backup has
1.113499 +** finished, then free all resources.
1.113500 +*/
1.113501 +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
1.113502 +  HashElem *i;                    /* Hash table iterator */
1.113503 +  int j;
1.113504 +
1.113505 +  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
1.113506 +  ** or if the connection has not yet been closed by sqlite3_close_v2(),
1.113507 +  ** then just leave the mutex and return.
1.113508 +  */
1.113509 +  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
1.113510 +    sqlite3_mutex_leave(db->mutex);
1.113511 +    return;
1.113512 +  }
1.113513 +
1.113514 +  /* If we reach this point, it means that the database connection has
1.113515 +  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
1.113516 +  ** pased to sqlite3_close (meaning that it is a zombie).  Therefore,
1.113517 +  ** go ahead and free all resources.
1.113518 +  */
1.113519 +
1.113520 +  /* Free any outstanding Savepoint structures. */
1.113521 +  sqlite3CloseSavepoints(db);
1.113522 +
1.113523 +  /* Close all database connections */
1.113524 +  for(j=0; j<db->nDb; j++){
1.113525 +    struct Db *pDb = &db->aDb[j];
1.113526 +    if( pDb->pBt ){
1.113527 +      sqlite3BtreeClose(pDb->pBt);
1.113528 +      pDb->pBt = 0;
1.113529 +      if( j!=1 ){
1.113530 +        pDb->pSchema = 0;
1.113531 +      }
1.113532 +    }
1.113533 +  }
1.113534 +  /* Clear the TEMP schema separately and last */
1.113535 +  if( db->aDb[1].pSchema ){
1.113536 +    sqlite3SchemaClear(db->aDb[1].pSchema);
1.113537 +  }
1.113538 +  sqlite3VtabUnlockList(db);
1.113539 +
1.113540 +  /* Free up the array of auxiliary databases */
1.113541 +  sqlite3CollapseDatabaseArray(db);
1.113542 +  assert( db->nDb<=2 );
1.113543 +  assert( db->aDb==db->aDbStatic );
1.113544 +
1.113545 +  /* Tell the code in notify.c that the connection no longer holds any
1.113546 +  ** locks and does not require any further unlock-notify callbacks.
1.113547 +  */
1.113548 +  sqlite3ConnectionClosed(db);
1.113549 +
1.113550 +  for(j=0; j<ArraySize(db->aFunc.a); j++){
1.113551 +    FuncDef *pNext, *pHash, *p;
1.113552 +    for(p=db->aFunc.a[j]; p; p=pHash){
1.113553 +      pHash = p->pHash;
1.113554 +      while( p ){
1.113555 +        functionDestroy(db, p);
1.113556 +        pNext = p->pNext;
1.113557 +        sqlite3DbFree(db, p);
1.113558 +        p = pNext;
1.113559 +      }
1.113560 +    }
1.113561 +  }
1.113562 +  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
1.113563 +    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
1.113564 +    /* Invoke any destructors registered for collation sequence user data. */
1.113565 +    for(j=0; j<3; j++){
1.113566 +      if( pColl[j].xDel ){
1.113567 +        pColl[j].xDel(pColl[j].pUser);
1.113568 +      }
1.113569 +    }
1.113570 +    sqlite3DbFree(db, pColl);
1.113571 +  }
1.113572 +  sqlite3HashClear(&db->aCollSeq);
1.113573 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.113574 +  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
1.113575 +    Module *pMod = (Module *)sqliteHashData(i);
1.113576 +    if( pMod->xDestroy ){
1.113577 +      pMod->xDestroy(pMod->pAux);
1.113578 +    }
1.113579 +    sqlite3DbFree(db, pMod);
1.113580 +  }
1.113581 +  sqlite3HashClear(&db->aModule);
1.113582 +#endif
1.113583 +
1.113584 +  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
1.113585 +  if( db->pErr ){
1.113586 +    sqlite3ValueFree(db->pErr);
1.113587 +  }
1.113588 +  sqlite3CloseExtensions(db);
1.113589 +
1.113590 +  db->magic = SQLITE_MAGIC_ERROR;
1.113591 +
1.113592 +  /* The temp-database schema is allocated differently from the other schema
1.113593 +  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
1.113594 +  ** So it needs to be freed here. Todo: Why not roll the temp schema into
1.113595 +  ** the same sqliteMalloc() as the one that allocates the database 
1.113596 +  ** structure?
1.113597 +  */
1.113598 +  sqlite3DbFree(db, db->aDb[1].pSchema);
1.113599 +  sqlite3_mutex_leave(db->mutex);
1.113600 +  db->magic = SQLITE_MAGIC_CLOSED;
1.113601 +  sqlite3_mutex_free(db->mutex);
1.113602 +  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
1.113603 +  if( db->lookaside.bMalloced ){
1.113604 +    sqlite3_free(db->lookaside.pStart);
1.113605 +  }
1.113606 +  sqlite3_free(db);
1.113607 +}
1.113608 +
1.113609 +/*
1.113610 +** Rollback all database files.  If tripCode is not SQLITE_OK, then
1.113611 +** any open cursors are invalidated ("tripped" - as in "tripping a circuit
1.113612 +** breaker") and made to return tripCode if there are any further
1.113613 +** attempts to use that cursor.
1.113614 +*/
1.113615 +SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
1.113616 +  int i;
1.113617 +  int inTrans = 0;
1.113618 +  assert( sqlite3_mutex_held(db->mutex) );
1.113619 +  sqlite3BeginBenignMalloc();
1.113620 +  for(i=0; i<db->nDb; i++){
1.113621 +    Btree *p = db->aDb[i].pBt;
1.113622 +    if( p ){
1.113623 +      if( sqlite3BtreeIsInTrans(p) ){
1.113624 +        inTrans = 1;
1.113625 +      }
1.113626 +      sqlite3BtreeRollback(p, tripCode);
1.113627 +      db->aDb[i].inTrans = 0;
1.113628 +    }
1.113629 +  }
1.113630 +  sqlite3VtabRollback(db);
1.113631 +  sqlite3EndBenignMalloc();
1.113632 +
1.113633 +  if( db->flags&SQLITE_InternChanges ){
1.113634 +    sqlite3ExpirePreparedStatements(db);
1.113635 +    sqlite3ResetAllSchemasOfConnection(db);
1.113636 +  }
1.113637 +
1.113638 +  /* Any deferred constraint violations have now been resolved. */
1.113639 +  db->nDeferredCons = 0;
1.113640 +
1.113641 +  /* If one has been configured, invoke the rollback-hook callback */
1.113642 +  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
1.113643 +    db->xRollbackCallback(db->pRollbackArg);
1.113644 +  }
1.113645 +}
1.113646 +
1.113647 +/*
1.113648 +** Return a static string that describes the kind of error specified in the
1.113649 +** argument.
1.113650 +*/
1.113651 +SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
1.113652 +  static const char* const aMsg[] = {
1.113653 +    /* SQLITE_OK          */ "not an error",
1.113654 +    /* SQLITE_ERROR       */ "SQL logic error or missing database",
1.113655 +    /* SQLITE_INTERNAL    */ 0,
1.113656 +    /* SQLITE_PERM        */ "access permission denied",
1.113657 +    /* SQLITE_ABORT       */ "callback requested query abort",
1.113658 +    /* SQLITE_BUSY        */ "database is locked",
1.113659 +    /* SQLITE_LOCKED      */ "database table is locked",
1.113660 +    /* SQLITE_NOMEM       */ "out of memory",
1.113661 +    /* SQLITE_READONLY    */ "attempt to write a readonly database",
1.113662 +    /* SQLITE_INTERRUPT   */ "interrupted",
1.113663 +    /* SQLITE_IOERR       */ "disk I/O error",
1.113664 +    /* SQLITE_CORRUPT     */ "database disk image is malformed",
1.113665 +    /* SQLITE_NOTFOUND    */ "unknown operation",
1.113666 +    /* SQLITE_FULL        */ "database or disk is full",
1.113667 +    /* SQLITE_CANTOPEN    */ "unable to open database file",
1.113668 +    /* SQLITE_PROTOCOL    */ "locking protocol",
1.113669 +    /* SQLITE_EMPTY       */ "table contains no data",
1.113670 +    /* SQLITE_SCHEMA      */ "database schema has changed",
1.113671 +    /* SQLITE_TOOBIG      */ "string or blob too big",
1.113672 +    /* SQLITE_CONSTRAINT  */ "constraint failed",
1.113673 +    /* SQLITE_MISMATCH    */ "datatype mismatch",
1.113674 +    /* SQLITE_MISUSE      */ "library routine called out of sequence",
1.113675 +    /* SQLITE_NOLFS       */ "large file support is disabled",
1.113676 +    /* SQLITE_AUTH        */ "authorization denied",
1.113677 +    /* SQLITE_FORMAT      */ "auxiliary database format error",
1.113678 +    /* SQLITE_RANGE       */ "bind or column index out of range",
1.113679 +    /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
1.113680 +  };
1.113681 +  const char *zErr = "unknown error";
1.113682 +  switch( rc ){
1.113683 +    case SQLITE_ABORT_ROLLBACK: {
1.113684 +      zErr = "abort due to ROLLBACK";
1.113685 +      break;
1.113686 +    }
1.113687 +    default: {
1.113688 +      rc &= 0xff;
1.113689 +      if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
1.113690 +        zErr = aMsg[rc];
1.113691 +      }
1.113692 +      break;
1.113693 +    }
1.113694 +  }
1.113695 +  return zErr;
1.113696 +}
1.113697 +
1.113698 +/*
1.113699 +** This routine implements a busy callback that sleeps and tries
1.113700 +** again until a timeout value is reached.  The timeout value is
1.113701 +** an integer number of milliseconds passed in as the first
1.113702 +** argument.
1.113703 +*/
1.113704 +static int sqliteDefaultBusyCallback(
1.113705 + void *ptr,               /* Database connection */
1.113706 + int count                /* Number of times table has been busy */
1.113707 +){
1.113708 +#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
1.113709 +  static const u8 delays[] =
1.113710 +     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
1.113711 +  static const u8 totals[] =
1.113712 +     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
1.113713 +# define NDELAY ArraySize(delays)
1.113714 +  sqlite3 *db = (sqlite3 *)ptr;
1.113715 +  int timeout = db->busyTimeout;
1.113716 +  int delay, prior;
1.113717 +
1.113718 +  assert( count>=0 );
1.113719 +  if( count < NDELAY ){
1.113720 +    delay = delays[count];
1.113721 +    prior = totals[count];
1.113722 +  }else{
1.113723 +    delay = delays[NDELAY-1];
1.113724 +    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
1.113725 +  }
1.113726 +  if( prior + delay > timeout ){
1.113727 +    delay = timeout - prior;
1.113728 +    if( delay<=0 ) return 0;
1.113729 +  }
1.113730 +  sqlite3OsSleep(db->pVfs, delay*1000);
1.113731 +  return 1;
1.113732 +#else
1.113733 +  sqlite3 *db = (sqlite3 *)ptr;
1.113734 +  int timeout = ((sqlite3 *)ptr)->busyTimeout;
1.113735 +  if( (count+1)*1000 > timeout ){
1.113736 +    return 0;
1.113737 +  }
1.113738 +  sqlite3OsSleep(db->pVfs, 1000000);
1.113739 +  return 1;
1.113740 +#endif
1.113741 +}
1.113742 +
1.113743 +/*
1.113744 +** Invoke the given busy handler.
1.113745 +**
1.113746 +** This routine is called when an operation failed with a lock.
1.113747 +** If this routine returns non-zero, the lock is retried.  If it
1.113748 +** returns 0, the operation aborts with an SQLITE_BUSY error.
1.113749 +*/
1.113750 +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
1.113751 +  int rc;
1.113752 +  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
1.113753 +  rc = p->xFunc(p->pArg, p->nBusy);
1.113754 +  if( rc==0 ){
1.113755 +    p->nBusy = -1;
1.113756 +  }else{
1.113757 +    p->nBusy++;
1.113758 +  }
1.113759 +  return rc; 
1.113760 +}
1.113761 +
1.113762 +/*
1.113763 +** This routine sets the busy callback for an Sqlite database to the
1.113764 +** given callback function with the given argument.
1.113765 +*/
1.113766 +SQLITE_API int sqlite3_busy_handler(
1.113767 +  sqlite3 *db,
1.113768 +  int (*xBusy)(void*,int),
1.113769 +  void *pArg
1.113770 +){
1.113771 +  sqlite3_mutex_enter(db->mutex);
1.113772 +  db->busyHandler.xFunc = xBusy;
1.113773 +  db->busyHandler.pArg = pArg;
1.113774 +  db->busyHandler.nBusy = 0;
1.113775 +  db->busyTimeout = 0;
1.113776 +  sqlite3_mutex_leave(db->mutex);
1.113777 +  return SQLITE_OK;
1.113778 +}
1.113779 +
1.113780 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1.113781 +/*
1.113782 +** This routine sets the progress callback for an Sqlite database to the
1.113783 +** given callback function with the given argument. The progress callback will
1.113784 +** be invoked every nOps opcodes.
1.113785 +*/
1.113786 +SQLITE_API void sqlite3_progress_handler(
1.113787 +  sqlite3 *db, 
1.113788 +  int nOps,
1.113789 +  int (*xProgress)(void*), 
1.113790 +  void *pArg
1.113791 +){
1.113792 +  sqlite3_mutex_enter(db->mutex);
1.113793 +  if( nOps>0 ){
1.113794 +    db->xProgress = xProgress;
1.113795 +    db->nProgressOps = nOps;
1.113796 +    db->pProgressArg = pArg;
1.113797 +  }else{
1.113798 +    db->xProgress = 0;
1.113799 +    db->nProgressOps = 0;
1.113800 +    db->pProgressArg = 0;
1.113801 +  }
1.113802 +  sqlite3_mutex_leave(db->mutex);
1.113803 +}
1.113804 +#endif
1.113805 +
1.113806 +
1.113807 +/*
1.113808 +** This routine installs a default busy handler that waits for the
1.113809 +** specified number of milliseconds before returning 0.
1.113810 +*/
1.113811 +SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
1.113812 +  if( ms>0 ){
1.113813 +    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
1.113814 +    db->busyTimeout = ms;
1.113815 +  }else{
1.113816 +    sqlite3_busy_handler(db, 0, 0);
1.113817 +  }
1.113818 +  return SQLITE_OK;
1.113819 +}
1.113820 +
1.113821 +/*
1.113822 +** Cause any pending operation to stop at its earliest opportunity.
1.113823 +*/
1.113824 +SQLITE_API void sqlite3_interrupt(sqlite3 *db){
1.113825 +  db->u1.isInterrupted = 1;
1.113826 +}
1.113827 +
1.113828 +
1.113829 +/*
1.113830 +** This function is exactly the same as sqlite3_create_function(), except
1.113831 +** that it is designed to be called by internal code. The difference is
1.113832 +** that if a malloc() fails in sqlite3_create_function(), an error code
1.113833 +** is returned and the mallocFailed flag cleared. 
1.113834 +*/
1.113835 +SQLITE_PRIVATE int sqlite3CreateFunc(
1.113836 +  sqlite3 *db,
1.113837 +  const char *zFunctionName,
1.113838 +  int nArg,
1.113839 +  int enc,
1.113840 +  void *pUserData,
1.113841 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1.113842 +  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1.113843 +  void (*xFinal)(sqlite3_context*),
1.113844 +  FuncDestructor *pDestructor
1.113845 +){
1.113846 +  FuncDef *p;
1.113847 +  int nName;
1.113848 +
1.113849 +  assert( sqlite3_mutex_held(db->mutex) );
1.113850 +  if( zFunctionName==0 ||
1.113851 +      (xFunc && (xFinal || xStep)) || 
1.113852 +      (!xFunc && (xFinal && !xStep)) ||
1.113853 +      (!xFunc && (!xFinal && xStep)) ||
1.113854 +      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
1.113855 +      (255<(nName = sqlite3Strlen30( zFunctionName))) ){
1.113856 +    return SQLITE_MISUSE_BKPT;
1.113857 +  }
1.113858 +  
1.113859 +#ifndef SQLITE_OMIT_UTF16
1.113860 +  /* If SQLITE_UTF16 is specified as the encoding type, transform this
1.113861 +  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1.113862 +  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1.113863 +  **
1.113864 +  ** If SQLITE_ANY is specified, add three versions of the function
1.113865 +  ** to the hash table.
1.113866 +  */
1.113867 +  if( enc==SQLITE_UTF16 ){
1.113868 +    enc = SQLITE_UTF16NATIVE;
1.113869 +  }else if( enc==SQLITE_ANY ){
1.113870 +    int rc;
1.113871 +    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
1.113872 +         pUserData, xFunc, xStep, xFinal, pDestructor);
1.113873 +    if( rc==SQLITE_OK ){
1.113874 +      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
1.113875 +          pUserData, xFunc, xStep, xFinal, pDestructor);
1.113876 +    }
1.113877 +    if( rc!=SQLITE_OK ){
1.113878 +      return rc;
1.113879 +    }
1.113880 +    enc = SQLITE_UTF16BE;
1.113881 +  }
1.113882 +#else
1.113883 +  enc = SQLITE_UTF8;
1.113884 +#endif
1.113885 +  
1.113886 +  /* Check if an existing function is being overridden or deleted. If so,
1.113887 +  ** and there are active VMs, then return SQLITE_BUSY. If a function
1.113888 +  ** is being overridden/deleted but there are no active VMs, allow the
1.113889 +  ** operation to continue but invalidate all precompiled statements.
1.113890 +  */
1.113891 +  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
1.113892 +  if( p && p->iPrefEnc==enc && p->nArg==nArg ){
1.113893 +    if( db->activeVdbeCnt ){
1.113894 +      sqlite3Error(db, SQLITE_BUSY, 
1.113895 +        "unable to delete/modify user-function due to active statements");
1.113896 +      assert( !db->mallocFailed );
1.113897 +      return SQLITE_BUSY;
1.113898 +    }else{
1.113899 +      sqlite3ExpirePreparedStatements(db);
1.113900 +    }
1.113901 +  }
1.113902 +
1.113903 +  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
1.113904 +  assert(p || db->mallocFailed);
1.113905 +  if( !p ){
1.113906 +    return SQLITE_NOMEM;
1.113907 +  }
1.113908 +
1.113909 +  /* If an older version of the function with a configured destructor is
1.113910 +  ** being replaced invoke the destructor function here. */
1.113911 +  functionDestroy(db, p);
1.113912 +
1.113913 +  if( pDestructor ){
1.113914 +    pDestructor->nRef++;
1.113915 +  }
1.113916 +  p->pDestructor = pDestructor;
1.113917 +  p->flags = 0;
1.113918 +  p->xFunc = xFunc;
1.113919 +  p->xStep = xStep;
1.113920 +  p->xFinalize = xFinal;
1.113921 +  p->pUserData = pUserData;
1.113922 +  p->nArg = (u16)nArg;
1.113923 +  return SQLITE_OK;
1.113924 +}
1.113925 +
1.113926 +/*
1.113927 +** Create new user functions.
1.113928 +*/
1.113929 +SQLITE_API int sqlite3_create_function(
1.113930 +  sqlite3 *db,
1.113931 +  const char *zFunc,
1.113932 +  int nArg,
1.113933 +  int enc,
1.113934 +  void *p,
1.113935 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1.113936 +  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1.113937 +  void (*xFinal)(sqlite3_context*)
1.113938 +){
1.113939 +  return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
1.113940 +                                    xFinal, 0);
1.113941 +}
1.113942 +
1.113943 +SQLITE_API int sqlite3_create_function_v2(
1.113944 +  sqlite3 *db,
1.113945 +  const char *zFunc,
1.113946 +  int nArg,
1.113947 +  int enc,
1.113948 +  void *p,
1.113949 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1.113950 +  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1.113951 +  void (*xFinal)(sqlite3_context*),
1.113952 +  void (*xDestroy)(void *)
1.113953 +){
1.113954 +  int rc = SQLITE_ERROR;
1.113955 +  FuncDestructor *pArg = 0;
1.113956 +  sqlite3_mutex_enter(db->mutex);
1.113957 +  if( xDestroy ){
1.113958 +    pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
1.113959 +    if( !pArg ){
1.113960 +      xDestroy(p);
1.113961 +      goto out;
1.113962 +    }
1.113963 +    pArg->xDestroy = xDestroy;
1.113964 +    pArg->pUserData = p;
1.113965 +  }
1.113966 +  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
1.113967 +  if( pArg && pArg->nRef==0 ){
1.113968 +    assert( rc!=SQLITE_OK );
1.113969 +    xDestroy(p);
1.113970 +    sqlite3DbFree(db, pArg);
1.113971 +  }
1.113972 +
1.113973 + out:
1.113974 +  rc = sqlite3ApiExit(db, rc);
1.113975 +  sqlite3_mutex_leave(db->mutex);
1.113976 +  return rc;
1.113977 +}
1.113978 +
1.113979 +#ifndef SQLITE_OMIT_UTF16
1.113980 +SQLITE_API int sqlite3_create_function16(
1.113981 +  sqlite3 *db,
1.113982 +  const void *zFunctionName,
1.113983 +  int nArg,
1.113984 +  int eTextRep,
1.113985 +  void *p,
1.113986 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
1.113987 +  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
1.113988 +  void (*xFinal)(sqlite3_context*)
1.113989 +){
1.113990 +  int rc;
1.113991 +  char *zFunc8;
1.113992 +  sqlite3_mutex_enter(db->mutex);
1.113993 +  assert( !db->mallocFailed );
1.113994 +  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
1.113995 +  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
1.113996 +  sqlite3DbFree(db, zFunc8);
1.113997 +  rc = sqlite3ApiExit(db, rc);
1.113998 +  sqlite3_mutex_leave(db->mutex);
1.113999 +  return rc;
1.114000 +}
1.114001 +#endif
1.114002 +
1.114003 +
1.114004 +/*
1.114005 +** Declare that a function has been overloaded by a virtual table.
1.114006 +**
1.114007 +** If the function already exists as a regular global function, then
1.114008 +** this routine is a no-op.  If the function does not exist, then create
1.114009 +** a new one that always throws a run-time error.  
1.114010 +**
1.114011 +** When virtual tables intend to provide an overloaded function, they
1.114012 +** should call this routine to make sure the global function exists.
1.114013 +** A global function must exist in order for name resolution to work
1.114014 +** properly.
1.114015 +*/
1.114016 +SQLITE_API int sqlite3_overload_function(
1.114017 +  sqlite3 *db,
1.114018 +  const char *zName,
1.114019 +  int nArg
1.114020 +){
1.114021 +  int nName = sqlite3Strlen30(zName);
1.114022 +  int rc = SQLITE_OK;
1.114023 +  sqlite3_mutex_enter(db->mutex);
1.114024 +  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
1.114025 +    rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
1.114026 +                           0, sqlite3InvalidFunction, 0, 0, 0);
1.114027 +  }
1.114028 +  rc = sqlite3ApiExit(db, rc);
1.114029 +  sqlite3_mutex_leave(db->mutex);
1.114030 +  return rc;
1.114031 +}
1.114032 +
1.114033 +#ifndef SQLITE_OMIT_TRACE
1.114034 +/*
1.114035 +** Register a trace function.  The pArg from the previously registered trace
1.114036 +** is returned.  
1.114037 +**
1.114038 +** A NULL trace function means that no tracing is executes.  A non-NULL
1.114039 +** trace is a pointer to a function that is invoked at the start of each
1.114040 +** SQL statement.
1.114041 +*/
1.114042 +SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
1.114043 +  void *pOld;
1.114044 +  sqlite3_mutex_enter(db->mutex);
1.114045 +  pOld = db->pTraceArg;
1.114046 +  db->xTrace = xTrace;
1.114047 +  db->pTraceArg = pArg;
1.114048 +  sqlite3_mutex_leave(db->mutex);
1.114049 +  return pOld;
1.114050 +}
1.114051 +/*
1.114052 +** Register a profile function.  The pArg from the previously registered 
1.114053 +** profile function is returned.  
1.114054 +**
1.114055 +** A NULL profile function means that no profiling is executes.  A non-NULL
1.114056 +** profile is a pointer to a function that is invoked at the conclusion of
1.114057 +** each SQL statement that is run.
1.114058 +*/
1.114059 +SQLITE_API void *sqlite3_profile(
1.114060 +  sqlite3 *db,
1.114061 +  void (*xProfile)(void*,const char*,sqlite_uint64),
1.114062 +  void *pArg
1.114063 +){
1.114064 +  void *pOld;
1.114065 +  sqlite3_mutex_enter(db->mutex);
1.114066 +  pOld = db->pProfileArg;
1.114067 +  db->xProfile = xProfile;
1.114068 +  db->pProfileArg = pArg;
1.114069 +  sqlite3_mutex_leave(db->mutex);
1.114070 +  return pOld;
1.114071 +}
1.114072 +#endif /* SQLITE_OMIT_TRACE */
1.114073 +
1.114074 +/*
1.114075 +** Register a function to be invoked when a transaction commits.
1.114076 +** If the invoked function returns non-zero, then the commit becomes a
1.114077 +** rollback.
1.114078 +*/
1.114079 +SQLITE_API void *sqlite3_commit_hook(
1.114080 +  sqlite3 *db,              /* Attach the hook to this database */
1.114081 +  int (*xCallback)(void*),  /* Function to invoke on each commit */
1.114082 +  void *pArg                /* Argument to the function */
1.114083 +){
1.114084 +  void *pOld;
1.114085 +  sqlite3_mutex_enter(db->mutex);
1.114086 +  pOld = db->pCommitArg;
1.114087 +  db->xCommitCallback = xCallback;
1.114088 +  db->pCommitArg = pArg;
1.114089 +  sqlite3_mutex_leave(db->mutex);
1.114090 +  return pOld;
1.114091 +}
1.114092 +
1.114093 +/*
1.114094 +** Register a callback to be invoked each time a row is updated,
1.114095 +** inserted or deleted using this database connection.
1.114096 +*/
1.114097 +SQLITE_API void *sqlite3_update_hook(
1.114098 +  sqlite3 *db,              /* Attach the hook to this database */
1.114099 +  void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
1.114100 +  void *pArg                /* Argument to the function */
1.114101 +){
1.114102 +  void *pRet;
1.114103 +  sqlite3_mutex_enter(db->mutex);
1.114104 +  pRet = db->pUpdateArg;
1.114105 +  db->xUpdateCallback = xCallback;
1.114106 +  db->pUpdateArg = pArg;
1.114107 +  sqlite3_mutex_leave(db->mutex);
1.114108 +  return pRet;
1.114109 +}
1.114110 +
1.114111 +/*
1.114112 +** Register a callback to be invoked each time a transaction is rolled
1.114113 +** back by this database connection.
1.114114 +*/
1.114115 +SQLITE_API void *sqlite3_rollback_hook(
1.114116 +  sqlite3 *db,              /* Attach the hook to this database */
1.114117 +  void (*xCallback)(void*), /* Callback function */
1.114118 +  void *pArg                /* Argument to the function */
1.114119 +){
1.114120 +  void *pRet;
1.114121 +  sqlite3_mutex_enter(db->mutex);
1.114122 +  pRet = db->pRollbackArg;
1.114123 +  db->xRollbackCallback = xCallback;
1.114124 +  db->pRollbackArg = pArg;
1.114125 +  sqlite3_mutex_leave(db->mutex);
1.114126 +  return pRet;
1.114127 +}
1.114128 +
1.114129 +#ifndef SQLITE_OMIT_WAL
1.114130 +/*
1.114131 +** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
1.114132 +** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
1.114133 +** is greater than sqlite3.pWalArg cast to an integer (the value configured by
1.114134 +** wal_autocheckpoint()).
1.114135 +*/ 
1.114136 +SQLITE_PRIVATE int sqlite3WalDefaultHook(
1.114137 +  void *pClientData,     /* Argument */
1.114138 +  sqlite3 *db,           /* Connection */
1.114139 +  const char *zDb,       /* Database */
1.114140 +  int nFrame             /* Size of WAL */
1.114141 +){
1.114142 +  if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
1.114143 +    sqlite3BeginBenignMalloc();
1.114144 +    sqlite3_wal_checkpoint(db, zDb);
1.114145 +    sqlite3EndBenignMalloc();
1.114146 +  }
1.114147 +  return SQLITE_OK;
1.114148 +}
1.114149 +#endif /* SQLITE_OMIT_WAL */
1.114150 +
1.114151 +/*
1.114152 +** Configure an sqlite3_wal_hook() callback to automatically checkpoint
1.114153 +** a database after committing a transaction if there are nFrame or
1.114154 +** more frames in the log file. Passing zero or a negative value as the
1.114155 +** nFrame parameter disables automatic checkpoints entirely.
1.114156 +**
1.114157 +** The callback registered by this function replaces any existing callback
1.114158 +** registered using sqlite3_wal_hook(). Likewise, registering a callback
1.114159 +** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
1.114160 +** configured by this function.
1.114161 +*/
1.114162 +SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
1.114163 +#ifdef SQLITE_OMIT_WAL
1.114164 +  UNUSED_PARAMETER(db);
1.114165 +  UNUSED_PARAMETER(nFrame);
1.114166 +#else
1.114167 +  if( nFrame>0 ){
1.114168 +    sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
1.114169 +  }else{
1.114170 +    sqlite3_wal_hook(db, 0, 0);
1.114171 +  }
1.114172 +#endif
1.114173 +  return SQLITE_OK;
1.114174 +}
1.114175 +
1.114176 +/*
1.114177 +** Register a callback to be invoked each time a transaction is written
1.114178 +** into the write-ahead-log by this database connection.
1.114179 +*/
1.114180 +SQLITE_API void *sqlite3_wal_hook(
1.114181 +  sqlite3 *db,                    /* Attach the hook to this db handle */
1.114182 +  int(*xCallback)(void *, sqlite3*, const char*, int),
1.114183 +  void *pArg                      /* First argument passed to xCallback() */
1.114184 +){
1.114185 +#ifndef SQLITE_OMIT_WAL
1.114186 +  void *pRet;
1.114187 +  sqlite3_mutex_enter(db->mutex);
1.114188 +  pRet = db->pWalArg;
1.114189 +  db->xWalCallback = xCallback;
1.114190 +  db->pWalArg = pArg;
1.114191 +  sqlite3_mutex_leave(db->mutex);
1.114192 +  return pRet;
1.114193 +#else
1.114194 +  return 0;
1.114195 +#endif
1.114196 +}
1.114197 +
1.114198 +/*
1.114199 +** Checkpoint database zDb.
1.114200 +*/
1.114201 +SQLITE_API int sqlite3_wal_checkpoint_v2(
1.114202 +  sqlite3 *db,                    /* Database handle */
1.114203 +  const char *zDb,                /* Name of attached database (or NULL) */
1.114204 +  int eMode,                      /* SQLITE_CHECKPOINT_* value */
1.114205 +  int *pnLog,                     /* OUT: Size of WAL log in frames */
1.114206 +  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
1.114207 +){
1.114208 +#ifdef SQLITE_OMIT_WAL
1.114209 +  return SQLITE_OK;
1.114210 +#else
1.114211 +  int rc;                         /* Return code */
1.114212 +  int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
1.114213 +
1.114214 +  /* Initialize the output variables to -1 in case an error occurs. */
1.114215 +  if( pnLog ) *pnLog = -1;
1.114216 +  if( pnCkpt ) *pnCkpt = -1;
1.114217 +
1.114218 +  assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
1.114219 +  assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
1.114220 +  assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
1.114221 +  if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
1.114222 +    return SQLITE_MISUSE;
1.114223 +  }
1.114224 +
1.114225 +  sqlite3_mutex_enter(db->mutex);
1.114226 +  if( zDb && zDb[0] ){
1.114227 +    iDb = sqlite3FindDbName(db, zDb);
1.114228 +  }
1.114229 +  if( iDb<0 ){
1.114230 +    rc = SQLITE_ERROR;
1.114231 +    sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
1.114232 +  }else{
1.114233 +    rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
1.114234 +    sqlite3Error(db, rc, 0);
1.114235 +  }
1.114236 +  rc = sqlite3ApiExit(db, rc);
1.114237 +  sqlite3_mutex_leave(db->mutex);
1.114238 +  return rc;
1.114239 +#endif
1.114240 +}
1.114241 +
1.114242 +
1.114243 +/*
1.114244 +** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
1.114245 +** to contains a zero-length string, all attached databases are 
1.114246 +** checkpointed.
1.114247 +*/
1.114248 +SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
1.114249 +  return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
1.114250 +}
1.114251 +
1.114252 +#ifndef SQLITE_OMIT_WAL
1.114253 +/*
1.114254 +** Run a checkpoint on database iDb. This is a no-op if database iDb is
1.114255 +** not currently open in WAL mode.
1.114256 +**
1.114257 +** If a transaction is open on the database being checkpointed, this 
1.114258 +** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 
1.114259 +** an error occurs while running the checkpoint, an SQLite error code is 
1.114260 +** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
1.114261 +**
1.114262 +** The mutex on database handle db should be held by the caller. The mutex
1.114263 +** associated with the specific b-tree being checkpointed is taken by
1.114264 +** this function while the checkpoint is running.
1.114265 +**
1.114266 +** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
1.114267 +** checkpointed. If an error is encountered it is returned immediately -
1.114268 +** no attempt is made to checkpoint any remaining databases.
1.114269 +**
1.114270 +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
1.114271 +*/
1.114272 +SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
1.114273 +  int rc = SQLITE_OK;             /* Return code */
1.114274 +  int i;                          /* Used to iterate through attached dbs */
1.114275 +  int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
1.114276 +
1.114277 +  assert( sqlite3_mutex_held(db->mutex) );
1.114278 +  assert( !pnLog || *pnLog==-1 );
1.114279 +  assert( !pnCkpt || *pnCkpt==-1 );
1.114280 +
1.114281 +  for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
1.114282 +    if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
1.114283 +      rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
1.114284 +      pnLog = 0;
1.114285 +      pnCkpt = 0;
1.114286 +      if( rc==SQLITE_BUSY ){
1.114287 +        bBusy = 1;
1.114288 +        rc = SQLITE_OK;
1.114289 +      }
1.114290 +    }
1.114291 +  }
1.114292 +
1.114293 +  return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
1.114294 +}
1.114295 +#endif /* SQLITE_OMIT_WAL */
1.114296 +
1.114297 +/*
1.114298 +** This function returns true if main-memory should be used instead of
1.114299 +** a temporary file for transient pager files and statement journals.
1.114300 +** The value returned depends on the value of db->temp_store (runtime
1.114301 +** parameter) and the compile time value of SQLITE_TEMP_STORE. The
1.114302 +** following table describes the relationship between these two values
1.114303 +** and this functions return value.
1.114304 +**
1.114305 +**   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
1.114306 +**   -----------------     --------------     ------------------------------
1.114307 +**   0                     any                file      (return 0)
1.114308 +**   1                     1                  file      (return 0)
1.114309 +**   1                     2                  memory    (return 1)
1.114310 +**   1                     0                  file      (return 0)
1.114311 +**   2                     1                  file      (return 0)
1.114312 +**   2                     2                  memory    (return 1)
1.114313 +**   2                     0                  memory    (return 1)
1.114314 +**   3                     any                memory    (return 1)
1.114315 +*/
1.114316 +SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
1.114317 +#if SQLITE_TEMP_STORE==1
1.114318 +  return ( db->temp_store==2 );
1.114319 +#endif
1.114320 +#if SQLITE_TEMP_STORE==2
1.114321 +  return ( db->temp_store!=1 );
1.114322 +#endif
1.114323 +#if SQLITE_TEMP_STORE==3
1.114324 +  return 1;
1.114325 +#endif
1.114326 +#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
1.114327 +  return 0;
1.114328 +#endif
1.114329 +}
1.114330 +
1.114331 +/*
1.114332 +** Return UTF-8 encoded English language explanation of the most recent
1.114333 +** error.
1.114334 +*/
1.114335 +SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
1.114336 +  const char *z;
1.114337 +  if( !db ){
1.114338 +    return sqlite3ErrStr(SQLITE_NOMEM);
1.114339 +  }
1.114340 +  if( !sqlite3SafetyCheckSickOrOk(db) ){
1.114341 +    return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
1.114342 +  }
1.114343 +  sqlite3_mutex_enter(db->mutex);
1.114344 +  if( db->mallocFailed ){
1.114345 +    z = sqlite3ErrStr(SQLITE_NOMEM);
1.114346 +  }else{
1.114347 +    z = (char*)sqlite3_value_text(db->pErr);
1.114348 +    assert( !db->mallocFailed );
1.114349 +    if( z==0 ){
1.114350 +      z = sqlite3ErrStr(db->errCode);
1.114351 +    }
1.114352 +  }
1.114353 +  sqlite3_mutex_leave(db->mutex);
1.114354 +  return z;
1.114355 +}
1.114356 +
1.114357 +#ifndef SQLITE_OMIT_UTF16
1.114358 +/*
1.114359 +** Return UTF-16 encoded English language explanation of the most recent
1.114360 +** error.
1.114361 +*/
1.114362 +SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
1.114363 +  static const u16 outOfMem[] = {
1.114364 +    'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
1.114365 +  };
1.114366 +  static const u16 misuse[] = {
1.114367 +    'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', 
1.114368 +    'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', 
1.114369 +    'c', 'a', 'l', 'l', 'e', 'd', ' ', 
1.114370 +    'o', 'u', 't', ' ', 
1.114371 +    'o', 'f', ' ', 
1.114372 +    's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
1.114373 +  };
1.114374 +
1.114375 +  const void *z;
1.114376 +  if( !db ){
1.114377 +    return (void *)outOfMem;
1.114378 +  }
1.114379 +  if( !sqlite3SafetyCheckSickOrOk(db) ){
1.114380 +    return (void *)misuse;
1.114381 +  }
1.114382 +  sqlite3_mutex_enter(db->mutex);
1.114383 +  if( db->mallocFailed ){
1.114384 +    z = (void *)outOfMem;
1.114385 +  }else{
1.114386 +    z = sqlite3_value_text16(db->pErr);
1.114387 +    if( z==0 ){
1.114388 +      sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
1.114389 +           SQLITE_UTF8, SQLITE_STATIC);
1.114390 +      z = sqlite3_value_text16(db->pErr);
1.114391 +    }
1.114392 +    /* A malloc() may have failed within the call to sqlite3_value_text16()
1.114393 +    ** above. If this is the case, then the db->mallocFailed flag needs to
1.114394 +    ** be cleared before returning. Do this directly, instead of via
1.114395 +    ** sqlite3ApiExit(), to avoid setting the database handle error message.
1.114396 +    */
1.114397 +    db->mallocFailed = 0;
1.114398 +  }
1.114399 +  sqlite3_mutex_leave(db->mutex);
1.114400 +  return z;
1.114401 +}
1.114402 +#endif /* SQLITE_OMIT_UTF16 */
1.114403 +
1.114404 +/*
1.114405 +** Return the most recent error code generated by an SQLite routine. If NULL is
1.114406 +** passed to this function, we assume a malloc() failed during sqlite3_open().
1.114407 +*/
1.114408 +SQLITE_API int sqlite3_errcode(sqlite3 *db){
1.114409 +  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
1.114410 +    return SQLITE_MISUSE_BKPT;
1.114411 +  }
1.114412 +  if( !db || db->mallocFailed ){
1.114413 +    return SQLITE_NOMEM;
1.114414 +  }
1.114415 +  return db->errCode & db->errMask;
1.114416 +}
1.114417 +SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
1.114418 +  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
1.114419 +    return SQLITE_MISUSE_BKPT;
1.114420 +  }
1.114421 +  if( !db || db->mallocFailed ){
1.114422 +    return SQLITE_NOMEM;
1.114423 +  }
1.114424 +  return db->errCode;
1.114425 +}
1.114426 +
1.114427 +/*
1.114428 +** Return a string that describes the kind of error specified in the
1.114429 +** argument.  For now, this simply calls the internal sqlite3ErrStr()
1.114430 +** function.
1.114431 +*/
1.114432 +SQLITE_API const char *sqlite3_errstr(int rc){
1.114433 +  return sqlite3ErrStr(rc);
1.114434 +}
1.114435 +
1.114436 +/*
1.114437 +** Create a new collating function for database "db".  The name is zName
1.114438 +** and the encoding is enc.
1.114439 +*/
1.114440 +static int createCollation(
1.114441 +  sqlite3* db,
1.114442 +  const char *zName, 
1.114443 +  u8 enc,
1.114444 +  void* pCtx,
1.114445 +  int(*xCompare)(void*,int,const void*,int,const void*),
1.114446 +  void(*xDel)(void*)
1.114447 +){
1.114448 +  CollSeq *pColl;
1.114449 +  int enc2;
1.114450 +  int nName = sqlite3Strlen30(zName);
1.114451 +  
1.114452 +  assert( sqlite3_mutex_held(db->mutex) );
1.114453 +
1.114454 +  /* If SQLITE_UTF16 is specified as the encoding type, transform this
1.114455 +  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1.114456 +  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1.114457 +  */
1.114458 +  enc2 = enc;
1.114459 +  testcase( enc2==SQLITE_UTF16 );
1.114460 +  testcase( enc2==SQLITE_UTF16_ALIGNED );
1.114461 +  if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
1.114462 +    enc2 = SQLITE_UTF16NATIVE;
1.114463 +  }
1.114464 +  if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
1.114465 +    return SQLITE_MISUSE_BKPT;
1.114466 +  }
1.114467 +
1.114468 +  /* Check if this call is removing or replacing an existing collation 
1.114469 +  ** sequence. If so, and there are active VMs, return busy. If there
1.114470 +  ** are no active VMs, invalidate any pre-compiled statements.
1.114471 +  */
1.114472 +  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
1.114473 +  if( pColl && pColl->xCmp ){
1.114474 +    if( db->activeVdbeCnt ){
1.114475 +      sqlite3Error(db, SQLITE_BUSY, 
1.114476 +        "unable to delete/modify collation sequence due to active statements");
1.114477 +      return SQLITE_BUSY;
1.114478 +    }
1.114479 +    sqlite3ExpirePreparedStatements(db);
1.114480 +
1.114481 +    /* If collation sequence pColl was created directly by a call to
1.114482 +    ** sqlite3_create_collation, and not generated by synthCollSeq(),
1.114483 +    ** then any copies made by synthCollSeq() need to be invalidated.
1.114484 +    ** Also, collation destructor - CollSeq.xDel() - function may need
1.114485 +    ** to be called.
1.114486 +    */ 
1.114487 +    if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
1.114488 +      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
1.114489 +      int j;
1.114490 +      for(j=0; j<3; j++){
1.114491 +        CollSeq *p = &aColl[j];
1.114492 +        if( p->enc==pColl->enc ){
1.114493 +          if( p->xDel ){
1.114494 +            p->xDel(p->pUser);
1.114495 +          }
1.114496 +          p->xCmp = 0;
1.114497 +        }
1.114498 +      }
1.114499 +    }
1.114500 +  }
1.114501 +
1.114502 +  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
1.114503 +  if( pColl==0 ) return SQLITE_NOMEM;
1.114504 +  pColl->xCmp = xCompare;
1.114505 +  pColl->pUser = pCtx;
1.114506 +  pColl->xDel = xDel;
1.114507 +  pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
1.114508 +  sqlite3Error(db, SQLITE_OK, 0);
1.114509 +  return SQLITE_OK;
1.114510 +}
1.114511 +
1.114512 +
1.114513 +/*
1.114514 +** This array defines hard upper bounds on limit values.  The
1.114515 +** initializer must be kept in sync with the SQLITE_LIMIT_*
1.114516 +** #defines in sqlite3.h.
1.114517 +*/
1.114518 +static const int aHardLimit[] = {
1.114519 +  SQLITE_MAX_LENGTH,
1.114520 +  SQLITE_MAX_SQL_LENGTH,
1.114521 +  SQLITE_MAX_COLUMN,
1.114522 +  SQLITE_MAX_EXPR_DEPTH,
1.114523 +  SQLITE_MAX_COMPOUND_SELECT,
1.114524 +  SQLITE_MAX_VDBE_OP,
1.114525 +  SQLITE_MAX_FUNCTION_ARG,
1.114526 +  SQLITE_MAX_ATTACHED,
1.114527 +  SQLITE_MAX_LIKE_PATTERN_LENGTH,
1.114528 +  SQLITE_MAX_VARIABLE_NUMBER,
1.114529 +  SQLITE_MAX_TRIGGER_DEPTH,
1.114530 +};
1.114531 +
1.114532 +/*
1.114533 +** Make sure the hard limits are set to reasonable values
1.114534 +*/
1.114535 +#if SQLITE_MAX_LENGTH<100
1.114536 +# error SQLITE_MAX_LENGTH must be at least 100
1.114537 +#endif
1.114538 +#if SQLITE_MAX_SQL_LENGTH<100
1.114539 +# error SQLITE_MAX_SQL_LENGTH must be at least 100
1.114540 +#endif
1.114541 +#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
1.114542 +# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
1.114543 +#endif
1.114544 +#if SQLITE_MAX_COMPOUND_SELECT<2
1.114545 +# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
1.114546 +#endif
1.114547 +#if SQLITE_MAX_VDBE_OP<40
1.114548 +# error SQLITE_MAX_VDBE_OP must be at least 40
1.114549 +#endif
1.114550 +#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
1.114551 +# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
1.114552 +#endif
1.114553 +#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
1.114554 +# error SQLITE_MAX_ATTACHED must be between 0 and 62
1.114555 +#endif
1.114556 +#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
1.114557 +# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
1.114558 +#endif
1.114559 +#if SQLITE_MAX_COLUMN>32767
1.114560 +# error SQLITE_MAX_COLUMN must not exceed 32767
1.114561 +#endif
1.114562 +#if SQLITE_MAX_TRIGGER_DEPTH<1
1.114563 +# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
1.114564 +#endif
1.114565 +
1.114566 +
1.114567 +/*
1.114568 +** Change the value of a limit.  Report the old value.
1.114569 +** If an invalid limit index is supplied, report -1.
1.114570 +** Make no changes but still report the old value if the
1.114571 +** new limit is negative.
1.114572 +**
1.114573 +** A new lower limit does not shrink existing constructs.
1.114574 +** It merely prevents new constructs that exceed the limit
1.114575 +** from forming.
1.114576 +*/
1.114577 +SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
1.114578 +  int oldLimit;
1.114579 +
1.114580 +
1.114581 +  /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
1.114582 +  ** there is a hard upper bound set at compile-time by a C preprocessor
1.114583 +  ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
1.114584 +  ** "_MAX_".)
1.114585 +  */
1.114586 +  assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
1.114587 +  assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
1.114588 +  assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
1.114589 +  assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
1.114590 +  assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
1.114591 +  assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
1.114592 +  assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
1.114593 +  assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
1.114594 +  assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
1.114595 +                                               SQLITE_MAX_LIKE_PATTERN_LENGTH );
1.114596 +  assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
1.114597 +  assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
1.114598 +  assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
1.114599 +
1.114600 +
1.114601 +  if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
1.114602 +    return -1;
1.114603 +  }
1.114604 +  oldLimit = db->aLimit[limitId];
1.114605 +  if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
1.114606 +    if( newLimit>aHardLimit[limitId] ){
1.114607 +      newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
1.114608 +    }
1.114609 +    db->aLimit[limitId] = newLimit;
1.114610 +  }
1.114611 +  return oldLimit;                     /* IMP: R-53341-35419 */
1.114612 +}
1.114613 +
1.114614 +/*
1.114615 +** This function is used to parse both URIs and non-URI filenames passed by the
1.114616 +** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
1.114617 +** URIs specified as part of ATTACH statements.
1.114618 +**
1.114619 +** The first argument to this function is the name of the VFS to use (or
1.114620 +** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
1.114621 +** query parameter. The second argument contains the URI (or non-URI filename)
1.114622 +** itself. When this function is called the *pFlags variable should contain
1.114623 +** the default flags to open the database handle with. The value stored in
1.114624 +** *pFlags may be updated before returning if the URI filename contains 
1.114625 +** "cache=xxx" or "mode=xxx" query parameters.
1.114626 +**
1.114627 +** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
1.114628 +** the VFS that should be used to open the database file. *pzFile is set to
1.114629 +** point to a buffer containing the name of the file to open. It is the 
1.114630 +** responsibility of the caller to eventually call sqlite3_free() to release
1.114631 +** this buffer.
1.114632 +**
1.114633 +** If an error occurs, then an SQLite error code is returned and *pzErrMsg
1.114634 +** may be set to point to a buffer containing an English language error 
1.114635 +** message. It is the responsibility of the caller to eventually release
1.114636 +** this buffer by calling sqlite3_free().
1.114637 +*/
1.114638 +SQLITE_PRIVATE int sqlite3ParseUri(
1.114639 +  const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
1.114640 +  const char *zUri,               /* Nul-terminated URI to parse */
1.114641 +  unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
1.114642 +  sqlite3_vfs **ppVfs,            /* OUT: VFS to use */ 
1.114643 +  char **pzFile,                  /* OUT: Filename component of URI */
1.114644 +  char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
1.114645 +){
1.114646 +  int rc = SQLITE_OK;
1.114647 +  unsigned int flags = *pFlags;
1.114648 +  const char *zVfs = zDefaultVfs;
1.114649 +  char *zFile;
1.114650 +  char c;
1.114651 +  int nUri = sqlite3Strlen30(zUri);
1.114652 +
1.114653 +  assert( *pzErrMsg==0 );
1.114654 +
1.114655 +  if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) 
1.114656 +   && nUri>=5 && memcmp(zUri, "file:", 5)==0 
1.114657 +  ){
1.114658 +    char *zOpt;
1.114659 +    int eState;                   /* Parser state when parsing URI */
1.114660 +    int iIn;                      /* Input character index */
1.114661 +    int iOut = 0;                 /* Output character index */
1.114662 +    int nByte = nUri+2;           /* Bytes of space to allocate */
1.114663 +
1.114664 +    /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 
1.114665 +    ** method that there may be extra parameters following the file-name.  */
1.114666 +    flags |= SQLITE_OPEN_URI;
1.114667 +
1.114668 +    for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
1.114669 +    zFile = sqlite3_malloc(nByte);
1.114670 +    if( !zFile ) return SQLITE_NOMEM;
1.114671 +
1.114672 +    /* Discard the scheme and authority segments of the URI. */
1.114673 +    if( zUri[5]=='/' && zUri[6]=='/' ){
1.114674 +      iIn = 7;
1.114675 +      while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
1.114676 +
1.114677 +      if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
1.114678 +        *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", 
1.114679 +            iIn-7, &zUri[7]);
1.114680 +        rc = SQLITE_ERROR;
1.114681 +        goto parse_uri_out;
1.114682 +      }
1.114683 +    }else{
1.114684 +      iIn = 5;
1.114685 +    }
1.114686 +
1.114687 +    /* Copy the filename and any query parameters into the zFile buffer. 
1.114688 +    ** Decode %HH escape codes along the way. 
1.114689 +    **
1.114690 +    ** Within this loop, variable eState may be set to 0, 1 or 2, depending
1.114691 +    ** on the parsing context. As follows:
1.114692 +    **
1.114693 +    **   0: Parsing file-name.
1.114694 +    **   1: Parsing name section of a name=value query parameter.
1.114695 +    **   2: Parsing value section of a name=value query parameter.
1.114696 +    */
1.114697 +    eState = 0;
1.114698 +    while( (c = zUri[iIn])!=0 && c!='#' ){
1.114699 +      iIn++;
1.114700 +      if( c=='%' 
1.114701 +       && sqlite3Isxdigit(zUri[iIn]) 
1.114702 +       && sqlite3Isxdigit(zUri[iIn+1]) 
1.114703 +      ){
1.114704 +        int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
1.114705 +        octet += sqlite3HexToInt(zUri[iIn++]);
1.114706 +
1.114707 +        assert( octet>=0 && octet<256 );
1.114708 +        if( octet==0 ){
1.114709 +          /* This branch is taken when "%00" appears within the URI. In this
1.114710 +          ** case we ignore all text in the remainder of the path, name or
1.114711 +          ** value currently being parsed. So ignore the current character
1.114712 +          ** and skip to the next "?", "=" or "&", as appropriate. */
1.114713 +          while( (c = zUri[iIn])!=0 && c!='#' 
1.114714 +              && (eState!=0 || c!='?')
1.114715 +              && (eState!=1 || (c!='=' && c!='&'))
1.114716 +              && (eState!=2 || c!='&')
1.114717 +          ){
1.114718 +            iIn++;
1.114719 +          }
1.114720 +          continue;
1.114721 +        }
1.114722 +        c = octet;
1.114723 +      }else if( eState==1 && (c=='&' || c=='=') ){
1.114724 +        if( zFile[iOut-1]==0 ){
1.114725 +          /* An empty option name. Ignore this option altogether. */
1.114726 +          while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
1.114727 +          continue;
1.114728 +        }
1.114729 +        if( c=='&' ){
1.114730 +          zFile[iOut++] = '\0';
1.114731 +        }else{
1.114732 +          eState = 2;
1.114733 +        }
1.114734 +        c = 0;
1.114735 +      }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
1.114736 +        c = 0;
1.114737 +        eState = 1;
1.114738 +      }
1.114739 +      zFile[iOut++] = c;
1.114740 +    }
1.114741 +    if( eState==1 ) zFile[iOut++] = '\0';
1.114742 +    zFile[iOut++] = '\0';
1.114743 +    zFile[iOut++] = '\0';
1.114744 +
1.114745 +    /* Check if there were any options specified that should be interpreted 
1.114746 +    ** here. Options that are interpreted here include "vfs" and those that
1.114747 +    ** correspond to flags that may be passed to the sqlite3_open_v2()
1.114748 +    ** method. */
1.114749 +    zOpt = &zFile[sqlite3Strlen30(zFile)+1];
1.114750 +    while( zOpt[0] ){
1.114751 +      int nOpt = sqlite3Strlen30(zOpt);
1.114752 +      char *zVal = &zOpt[nOpt+1];
1.114753 +      int nVal = sqlite3Strlen30(zVal);
1.114754 +
1.114755 +      if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
1.114756 +        zVfs = zVal;
1.114757 +      }else{
1.114758 +        struct OpenMode {
1.114759 +          const char *z;
1.114760 +          int mode;
1.114761 +        } *aMode = 0;
1.114762 +        char *zModeType = 0;
1.114763 +        int mask = 0;
1.114764 +        int limit = 0;
1.114765 +
1.114766 +        if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
1.114767 +          static struct OpenMode aCacheMode[] = {
1.114768 +            { "shared",  SQLITE_OPEN_SHAREDCACHE },
1.114769 +            { "private", SQLITE_OPEN_PRIVATECACHE },
1.114770 +            { 0, 0 }
1.114771 +          };
1.114772 +
1.114773 +          mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
1.114774 +          aMode = aCacheMode;
1.114775 +          limit = mask;
1.114776 +          zModeType = "cache";
1.114777 +        }
1.114778 +        if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
1.114779 +          static struct OpenMode aOpenMode[] = {
1.114780 +            { "ro",  SQLITE_OPEN_READONLY },
1.114781 +            { "rw",  SQLITE_OPEN_READWRITE }, 
1.114782 +            { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
1.114783 +            { "memory", SQLITE_OPEN_MEMORY },
1.114784 +            { 0, 0 }
1.114785 +          };
1.114786 +
1.114787 +          mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
1.114788 +                   | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
1.114789 +          aMode = aOpenMode;
1.114790 +          limit = mask & flags;
1.114791 +          zModeType = "access";
1.114792 +        }
1.114793 +
1.114794 +        if( aMode ){
1.114795 +          int i;
1.114796 +          int mode = 0;
1.114797 +          for(i=0; aMode[i].z; i++){
1.114798 +            const char *z = aMode[i].z;
1.114799 +            if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
1.114800 +              mode = aMode[i].mode;
1.114801 +              break;
1.114802 +            }
1.114803 +          }
1.114804 +          if( mode==0 ){
1.114805 +            *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
1.114806 +            rc = SQLITE_ERROR;
1.114807 +            goto parse_uri_out;
1.114808 +          }
1.114809 +          if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
1.114810 +            *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
1.114811 +                                        zModeType, zVal);
1.114812 +            rc = SQLITE_PERM;
1.114813 +            goto parse_uri_out;
1.114814 +          }
1.114815 +          flags = (flags & ~mask) | mode;
1.114816 +        }
1.114817 +      }
1.114818 +
1.114819 +      zOpt = &zVal[nVal+1];
1.114820 +    }
1.114821 +
1.114822 +  }else{
1.114823 +    zFile = sqlite3_malloc(nUri+2);
1.114824 +    if( !zFile ) return SQLITE_NOMEM;
1.114825 +    memcpy(zFile, zUri, nUri);
1.114826 +    zFile[nUri] = '\0';
1.114827 +    zFile[nUri+1] = '\0';
1.114828 +    flags &= ~SQLITE_OPEN_URI;
1.114829 +  }
1.114830 +
1.114831 +  *ppVfs = sqlite3_vfs_find(zVfs);
1.114832 +  if( *ppVfs==0 ){
1.114833 +    *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
1.114834 +    rc = SQLITE_ERROR;
1.114835 +  }
1.114836 + parse_uri_out:
1.114837 +  if( rc!=SQLITE_OK ){
1.114838 +    sqlite3_free(zFile);
1.114839 +    zFile = 0;
1.114840 +  }
1.114841 +  *pFlags = flags;
1.114842 +  *pzFile = zFile;
1.114843 +  return rc;
1.114844 +}
1.114845 +
1.114846 +
1.114847 +/*
1.114848 +** This routine does the work of opening a database on behalf of
1.114849 +** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
1.114850 +** is UTF-8 encoded.
1.114851 +*/
1.114852 +static int openDatabase(
1.114853 +  const char *zFilename, /* Database filename UTF-8 encoded */
1.114854 +  sqlite3 **ppDb,        /* OUT: Returned database handle */
1.114855 +  unsigned int flags,    /* Operational flags */
1.114856 +  const char *zVfs       /* Name of the VFS to use */
1.114857 +){
1.114858 +  sqlite3 *db;                    /* Store allocated handle here */
1.114859 +  int rc;                         /* Return code */
1.114860 +  int isThreadsafe;               /* True for threadsafe connections */
1.114861 +  char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
1.114862 +  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
1.114863 +
1.114864 +  *ppDb = 0;
1.114865 +#ifndef SQLITE_OMIT_AUTOINIT
1.114866 +  rc = sqlite3_initialize();
1.114867 +  if( rc ) return rc;
1.114868 +#endif
1.114869 +
1.114870 +  /* Only allow sensible combinations of bits in the flags argument.  
1.114871 +  ** Throw an error if any non-sense combination is used.  If we
1.114872 +  ** do not block illegal combinations here, it could trigger
1.114873 +  ** assert() statements in deeper layers.  Sensible combinations
1.114874 +  ** are:
1.114875 +  **
1.114876 +  **  1:  SQLITE_OPEN_READONLY
1.114877 +  **  2:  SQLITE_OPEN_READWRITE
1.114878 +  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
1.114879 +  */
1.114880 +  assert( SQLITE_OPEN_READONLY  == 0x01 );
1.114881 +  assert( SQLITE_OPEN_READWRITE == 0x02 );
1.114882 +  assert( SQLITE_OPEN_CREATE    == 0x04 );
1.114883 +  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
1.114884 +  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
1.114885 +  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
1.114886 +  if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
1.114887 +
1.114888 +  if( sqlite3GlobalConfig.bCoreMutex==0 ){
1.114889 +    isThreadsafe = 0;
1.114890 +  }else if( flags & SQLITE_OPEN_NOMUTEX ){
1.114891 +    isThreadsafe = 0;
1.114892 +  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
1.114893 +    isThreadsafe = 1;
1.114894 +  }else{
1.114895 +    isThreadsafe = sqlite3GlobalConfig.bFullMutex;
1.114896 +  }
1.114897 +  if( flags & SQLITE_OPEN_PRIVATECACHE ){
1.114898 +    flags &= ~SQLITE_OPEN_SHAREDCACHE;
1.114899 +  }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
1.114900 +    flags |= SQLITE_OPEN_SHAREDCACHE;
1.114901 +  }
1.114902 +
1.114903 +  /* Remove harmful bits from the flags parameter
1.114904 +  **
1.114905 +  ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
1.114906 +  ** dealt with in the previous code block.  Besides these, the only
1.114907 +  ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
1.114908 +  ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
1.114909 +  ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
1.114910 +  ** off all other flags.
1.114911 +  */
1.114912 +  flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
1.114913 +               SQLITE_OPEN_EXCLUSIVE |
1.114914 +               SQLITE_OPEN_MAIN_DB |
1.114915 +               SQLITE_OPEN_TEMP_DB | 
1.114916 +               SQLITE_OPEN_TRANSIENT_DB | 
1.114917 +               SQLITE_OPEN_MAIN_JOURNAL | 
1.114918 +               SQLITE_OPEN_TEMP_JOURNAL | 
1.114919 +               SQLITE_OPEN_SUBJOURNAL | 
1.114920 +               SQLITE_OPEN_MASTER_JOURNAL |
1.114921 +               SQLITE_OPEN_NOMUTEX |
1.114922 +               SQLITE_OPEN_FULLMUTEX |
1.114923 +               SQLITE_OPEN_WAL
1.114924 +             );
1.114925 +
1.114926 +  /* Allocate the sqlite data structure */
1.114927 +  db = sqlite3MallocZero( sizeof(sqlite3) );
1.114928 +  if( db==0 ) goto opendb_out;
1.114929 +  if( isThreadsafe ){
1.114930 +    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
1.114931 +    if( db->mutex==0 ){
1.114932 +      sqlite3_free(db);
1.114933 +      db = 0;
1.114934 +      goto opendb_out;
1.114935 +    }
1.114936 +  }
1.114937 +  sqlite3_mutex_enter(db->mutex);
1.114938 +  db->errMask = 0xff;
1.114939 +  db->nDb = 2;
1.114940 +  db->magic = SQLITE_MAGIC_BUSY;
1.114941 +  db->aDb = db->aDbStatic;
1.114942 +
1.114943 +  assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
1.114944 +  memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
1.114945 +  db->autoCommit = 1;
1.114946 +  db->nextAutovac = -1;
1.114947 +  db->nextPagesize = 0;
1.114948 +  db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
1.114949 +#if SQLITE_DEFAULT_FILE_FORMAT<4
1.114950 +                 | SQLITE_LegacyFileFmt
1.114951 +#endif
1.114952 +#ifdef SQLITE_ENABLE_LOAD_EXTENSION
1.114953 +                 | SQLITE_LoadExtension
1.114954 +#endif
1.114955 +#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
1.114956 +                 | SQLITE_RecTriggers
1.114957 +#endif
1.114958 +#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
1.114959 +                 | SQLITE_ForeignKeys
1.114960 +#endif
1.114961 +      ;
1.114962 +  sqlite3HashInit(&db->aCollSeq);
1.114963 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.114964 +  sqlite3HashInit(&db->aModule);
1.114965 +#endif
1.114966 +
1.114967 +  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1.114968 +  ** and UTF-16, so add a version for each to avoid any unnecessary
1.114969 +  ** conversions. The only error that can occur here is a malloc() failure.
1.114970 +  */
1.114971 +  createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
1.114972 +  createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
1.114973 +  createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
1.114974 +  createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
1.114975 +  if( db->mallocFailed ){
1.114976 +    goto opendb_out;
1.114977 +  }
1.114978 +  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
1.114979 +  assert( db->pDfltColl!=0 );
1.114980 +
1.114981 +  /* Also add a UTF-8 case-insensitive collation sequence. */
1.114982 +  createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
1.114983 +
1.114984 +  /* Parse the filename/URI argument. */
1.114985 +  db->openFlags = flags;
1.114986 +  rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
1.114987 +  if( rc!=SQLITE_OK ){
1.114988 +    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
1.114989 +    sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
1.114990 +    sqlite3_free(zErrMsg);
1.114991 +    goto opendb_out;
1.114992 +  }
1.114993 +
1.114994 +  /* Open the backend database driver */
1.114995 +  rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
1.114996 +                        flags | SQLITE_OPEN_MAIN_DB);
1.114997 +  if( rc!=SQLITE_OK ){
1.114998 +    if( rc==SQLITE_IOERR_NOMEM ){
1.114999 +      rc = SQLITE_NOMEM;
1.115000 +    }
1.115001 +    sqlite3Error(db, rc, 0);
1.115002 +    goto opendb_out;
1.115003 +  }
1.115004 +  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
1.115005 +  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
1.115006 +
1.115007 +
1.115008 +  /* The default safety_level for the main database is 'full'; for the temp
1.115009 +  ** database it is 'NONE'. This matches the pager layer defaults.  
1.115010 +  */
1.115011 +  db->aDb[0].zName = "main";
1.115012 +  db->aDb[0].safety_level = 3;
1.115013 +  db->aDb[1].zName = "temp";
1.115014 +  db->aDb[1].safety_level = 1;
1.115015 +
1.115016 +  db->magic = SQLITE_MAGIC_OPEN;
1.115017 +  if( db->mallocFailed ){
1.115018 +    goto opendb_out;
1.115019 +  }
1.115020 +
1.115021 +  /* Register all built-in functions, but do not attempt to read the
1.115022 +  ** database schema yet. This is delayed until the first time the database
1.115023 +  ** is accessed.
1.115024 +  */
1.115025 +  sqlite3Error(db, SQLITE_OK, 0);
1.115026 +  sqlite3RegisterBuiltinFunctions(db);
1.115027 +
1.115028 +  /* Load automatic extensions - extensions that have been registered
1.115029 +  ** using the sqlite3_automatic_extension() API.
1.115030 +  */
1.115031 +  rc = sqlite3_errcode(db);
1.115032 +  if( rc==SQLITE_OK ){
1.115033 +    sqlite3AutoLoadExtensions(db);
1.115034 +    rc = sqlite3_errcode(db);
1.115035 +    if( rc!=SQLITE_OK ){
1.115036 +      goto opendb_out;
1.115037 +    }
1.115038 +  }
1.115039 +
1.115040 +#ifdef SQLITE_ENABLE_FTS1
1.115041 +  if( !db->mallocFailed ){
1.115042 +    extern int sqlite3Fts1Init(sqlite3*);
1.115043 +    rc = sqlite3Fts1Init(db);
1.115044 +  }
1.115045 +#endif
1.115046 +
1.115047 +#ifdef SQLITE_ENABLE_FTS2
1.115048 +  if( !db->mallocFailed && rc==SQLITE_OK ){
1.115049 +    extern int sqlite3Fts2Init(sqlite3*);
1.115050 +    rc = sqlite3Fts2Init(db);
1.115051 +  }
1.115052 +#endif
1.115053 +
1.115054 +#ifdef SQLITE_ENABLE_FTS3
1.115055 +  if( !db->mallocFailed && rc==SQLITE_OK ){
1.115056 +    rc = sqlite3Fts3Init(db);
1.115057 +  }
1.115058 +#endif
1.115059 +
1.115060 +#ifdef SQLITE_ENABLE_ICU
1.115061 +  if( !db->mallocFailed && rc==SQLITE_OK ){
1.115062 +    rc = sqlite3IcuInit(db);
1.115063 +  }
1.115064 +#endif
1.115065 +
1.115066 +#ifdef SQLITE_ENABLE_RTREE
1.115067 +  if( !db->mallocFailed && rc==SQLITE_OK){
1.115068 +    rc = sqlite3RtreeInit(db);
1.115069 +  }
1.115070 +#endif
1.115071 +
1.115072 +  sqlite3Error(db, rc, 0);
1.115073 +
1.115074 +  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
1.115075 +  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
1.115076 +  ** mode.  Doing nothing at all also makes NORMAL the default.
1.115077 +  */
1.115078 +#ifdef SQLITE_DEFAULT_LOCKING_MODE
1.115079 +  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
1.115080 +  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
1.115081 +                          SQLITE_DEFAULT_LOCKING_MODE);
1.115082 +#endif
1.115083 +
1.115084 +  /* Enable the lookaside-malloc subsystem */
1.115085 +  setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
1.115086 +                        sqlite3GlobalConfig.nLookaside);
1.115087 +
1.115088 +  sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
1.115089 +
1.115090 +opendb_out:
1.115091 +  sqlite3_free(zOpen);
1.115092 +  if( db ){
1.115093 +    assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
1.115094 +    sqlite3_mutex_leave(db->mutex);
1.115095 +  }
1.115096 +  rc = sqlite3_errcode(db);
1.115097 +  assert( db!=0 || rc==SQLITE_NOMEM );
1.115098 +  if( rc==SQLITE_NOMEM ){
1.115099 +    sqlite3_close(db);
1.115100 +    db = 0;
1.115101 +  }else if( rc!=SQLITE_OK ){
1.115102 +    db->magic = SQLITE_MAGIC_SICK;
1.115103 +  }
1.115104 +  *ppDb = db;
1.115105 +#ifdef SQLITE_ENABLE_SQLLOG
1.115106 +  if( sqlite3GlobalConfig.xSqllog ){
1.115107 +    /* Opening a db handle. Fourth parameter is passed 0. */
1.115108 +    void *pArg = sqlite3GlobalConfig.pSqllogArg;
1.115109 +    sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
1.115110 +  }
1.115111 +#endif
1.115112 +  return sqlite3ApiExit(0, rc);
1.115113 +}
1.115114 +
1.115115 +/*
1.115116 +** Open a new database handle.
1.115117 +*/
1.115118 +SQLITE_API int sqlite3_open(
1.115119 +  const char *zFilename, 
1.115120 +  sqlite3 **ppDb 
1.115121 +){
1.115122 +  return openDatabase(zFilename, ppDb,
1.115123 +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
1.115124 +}
1.115125 +SQLITE_API int sqlite3_open_v2(
1.115126 +  const char *filename,   /* Database filename (UTF-8) */
1.115127 +  sqlite3 **ppDb,         /* OUT: SQLite db handle */
1.115128 +  int flags,              /* Flags */
1.115129 +  const char *zVfs        /* Name of VFS module to use */
1.115130 +){
1.115131 +  return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
1.115132 +}
1.115133 +
1.115134 +#ifndef SQLITE_OMIT_UTF16
1.115135 +/*
1.115136 +** Open a new database handle.
1.115137 +*/
1.115138 +SQLITE_API int sqlite3_open16(
1.115139 +  const void *zFilename, 
1.115140 +  sqlite3 **ppDb
1.115141 +){
1.115142 +  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
1.115143 +  sqlite3_value *pVal;
1.115144 +  int rc;
1.115145 +
1.115146 +  assert( zFilename );
1.115147 +  assert( ppDb );
1.115148 +  *ppDb = 0;
1.115149 +#ifndef SQLITE_OMIT_AUTOINIT
1.115150 +  rc = sqlite3_initialize();
1.115151 +  if( rc ) return rc;
1.115152 +#endif
1.115153 +  pVal = sqlite3ValueNew(0);
1.115154 +  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1.115155 +  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1.115156 +  if( zFilename8 ){
1.115157 +    rc = openDatabase(zFilename8, ppDb,
1.115158 +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
1.115159 +    assert( *ppDb || rc==SQLITE_NOMEM );
1.115160 +    if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
1.115161 +      ENC(*ppDb) = SQLITE_UTF16NATIVE;
1.115162 +    }
1.115163 +  }else{
1.115164 +    rc = SQLITE_NOMEM;
1.115165 +  }
1.115166 +  sqlite3ValueFree(pVal);
1.115167 +
1.115168 +  return sqlite3ApiExit(0, rc);
1.115169 +}
1.115170 +#endif /* SQLITE_OMIT_UTF16 */
1.115171 +
1.115172 +/*
1.115173 +** Register a new collation sequence with the database handle db.
1.115174 +*/
1.115175 +SQLITE_API int sqlite3_create_collation(
1.115176 +  sqlite3* db, 
1.115177 +  const char *zName, 
1.115178 +  int enc, 
1.115179 +  void* pCtx,
1.115180 +  int(*xCompare)(void*,int,const void*,int,const void*)
1.115181 +){
1.115182 +  int rc;
1.115183 +  sqlite3_mutex_enter(db->mutex);
1.115184 +  assert( !db->mallocFailed );
1.115185 +  rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
1.115186 +  rc = sqlite3ApiExit(db, rc);
1.115187 +  sqlite3_mutex_leave(db->mutex);
1.115188 +  return rc;
1.115189 +}
1.115190 +
1.115191 +/*
1.115192 +** Register a new collation sequence with the database handle db.
1.115193 +*/
1.115194 +SQLITE_API int sqlite3_create_collation_v2(
1.115195 +  sqlite3* db, 
1.115196 +  const char *zName, 
1.115197 +  int enc, 
1.115198 +  void* pCtx,
1.115199 +  int(*xCompare)(void*,int,const void*,int,const void*),
1.115200 +  void(*xDel)(void*)
1.115201 +){
1.115202 +  int rc;
1.115203 +  sqlite3_mutex_enter(db->mutex);
1.115204 +  assert( !db->mallocFailed );
1.115205 +  rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
1.115206 +  rc = sqlite3ApiExit(db, rc);
1.115207 +  sqlite3_mutex_leave(db->mutex);
1.115208 +  return rc;
1.115209 +}
1.115210 +
1.115211 +#ifndef SQLITE_OMIT_UTF16
1.115212 +/*
1.115213 +** Register a new collation sequence with the database handle db.
1.115214 +*/
1.115215 +SQLITE_API int sqlite3_create_collation16(
1.115216 +  sqlite3* db, 
1.115217 +  const void *zName,
1.115218 +  int enc, 
1.115219 +  void* pCtx,
1.115220 +  int(*xCompare)(void*,int,const void*,int,const void*)
1.115221 +){
1.115222 +  int rc = SQLITE_OK;
1.115223 +  char *zName8;
1.115224 +  sqlite3_mutex_enter(db->mutex);
1.115225 +  assert( !db->mallocFailed );
1.115226 +  zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
1.115227 +  if( zName8 ){
1.115228 +    rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
1.115229 +    sqlite3DbFree(db, zName8);
1.115230 +  }
1.115231 +  rc = sqlite3ApiExit(db, rc);
1.115232 +  sqlite3_mutex_leave(db->mutex);
1.115233 +  return rc;
1.115234 +}
1.115235 +#endif /* SQLITE_OMIT_UTF16 */
1.115236 +
1.115237 +/*
1.115238 +** Register a collation sequence factory callback with the database handle
1.115239 +** db. Replace any previously installed collation sequence factory.
1.115240 +*/
1.115241 +SQLITE_API int sqlite3_collation_needed(
1.115242 +  sqlite3 *db, 
1.115243 +  void *pCollNeededArg, 
1.115244 +  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1.115245 +){
1.115246 +  sqlite3_mutex_enter(db->mutex);
1.115247 +  db->xCollNeeded = xCollNeeded;
1.115248 +  db->xCollNeeded16 = 0;
1.115249 +  db->pCollNeededArg = pCollNeededArg;
1.115250 +  sqlite3_mutex_leave(db->mutex);
1.115251 +  return SQLITE_OK;
1.115252 +}
1.115253 +
1.115254 +#ifndef SQLITE_OMIT_UTF16
1.115255 +/*
1.115256 +** Register a collation sequence factory callback with the database handle
1.115257 +** db. Replace any previously installed collation sequence factory.
1.115258 +*/
1.115259 +SQLITE_API int sqlite3_collation_needed16(
1.115260 +  sqlite3 *db, 
1.115261 +  void *pCollNeededArg, 
1.115262 +  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1.115263 +){
1.115264 +  sqlite3_mutex_enter(db->mutex);
1.115265 +  db->xCollNeeded = 0;
1.115266 +  db->xCollNeeded16 = xCollNeeded16;
1.115267 +  db->pCollNeededArg = pCollNeededArg;
1.115268 +  sqlite3_mutex_leave(db->mutex);
1.115269 +  return SQLITE_OK;
1.115270 +}
1.115271 +#endif /* SQLITE_OMIT_UTF16 */
1.115272 +
1.115273 +#ifndef SQLITE_OMIT_DEPRECATED
1.115274 +/*
1.115275 +** This function is now an anachronism. It used to be used to recover from a
1.115276 +** malloc() failure, but SQLite now does this automatically.
1.115277 +*/
1.115278 +SQLITE_API int sqlite3_global_recover(void){
1.115279 +  return SQLITE_OK;
1.115280 +}
1.115281 +#endif
1.115282 +
1.115283 +/*
1.115284 +** Test to see whether or not the database connection is in autocommit
1.115285 +** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
1.115286 +** by default.  Autocommit is disabled by a BEGIN statement and reenabled
1.115287 +** by the next COMMIT or ROLLBACK.
1.115288 +**
1.115289 +******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
1.115290 +*/
1.115291 +SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
1.115292 +  return db->autoCommit;
1.115293 +}
1.115294 +
1.115295 +/*
1.115296 +** The following routines are subtitutes for constants SQLITE_CORRUPT,
1.115297 +** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
1.115298 +** constants.  They server two purposes:
1.115299 +**
1.115300 +**   1.  Serve as a convenient place to set a breakpoint in a debugger
1.115301 +**       to detect when version error conditions occurs.
1.115302 +**
1.115303 +**   2.  Invoke sqlite3_log() to provide the source code location where
1.115304 +**       a low-level error is first detected.
1.115305 +*/
1.115306 +SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
1.115307 +  testcase( sqlite3GlobalConfig.xLog!=0 );
1.115308 +  sqlite3_log(SQLITE_CORRUPT,
1.115309 +              "database corruption at line %d of [%.10s]",
1.115310 +              lineno, 20+sqlite3_sourceid());
1.115311 +  return SQLITE_CORRUPT;
1.115312 +}
1.115313 +SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
1.115314 +  testcase( sqlite3GlobalConfig.xLog!=0 );
1.115315 +  sqlite3_log(SQLITE_MISUSE, 
1.115316 +              "misuse at line %d of [%.10s]",
1.115317 +              lineno, 20+sqlite3_sourceid());
1.115318 +  return SQLITE_MISUSE;
1.115319 +}
1.115320 +SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
1.115321 +  testcase( sqlite3GlobalConfig.xLog!=0 );
1.115322 +  sqlite3_log(SQLITE_CANTOPEN, 
1.115323 +              "cannot open file at line %d of [%.10s]",
1.115324 +              lineno, 20+sqlite3_sourceid());
1.115325 +  return SQLITE_CANTOPEN;
1.115326 +}
1.115327 +
1.115328 +
1.115329 +#ifndef SQLITE_OMIT_DEPRECATED
1.115330 +/*
1.115331 +** This is a convenience routine that makes sure that all thread-specific
1.115332 +** data for this thread has been deallocated.
1.115333 +**
1.115334 +** SQLite no longer uses thread-specific data so this routine is now a
1.115335 +** no-op.  It is retained for historical compatibility.
1.115336 +*/
1.115337 +SQLITE_API void sqlite3_thread_cleanup(void){
1.115338 +}
1.115339 +#endif
1.115340 +
1.115341 +/*
1.115342 +** Return meta information about a specific column of a database table.
1.115343 +** See comment in sqlite3.h (sqlite.h.in) for details.
1.115344 +*/
1.115345 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
1.115346 +SQLITE_API int sqlite3_table_column_metadata(
1.115347 +  sqlite3 *db,                /* Connection handle */
1.115348 +  const char *zDbName,        /* Database name or NULL */
1.115349 +  const char *zTableName,     /* Table name */
1.115350 +  const char *zColumnName,    /* Column name */
1.115351 +  char const **pzDataType,    /* OUTPUT: Declared data type */
1.115352 +  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
1.115353 +  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
1.115354 +  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
1.115355 +  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
1.115356 +){
1.115357 +  int rc;
1.115358 +  char *zErrMsg = 0;
1.115359 +  Table *pTab = 0;
1.115360 +  Column *pCol = 0;
1.115361 +  int iCol;
1.115362 +
1.115363 +  char const *zDataType = 0;
1.115364 +  char const *zCollSeq = 0;
1.115365 +  int notnull = 0;
1.115366 +  int primarykey = 0;
1.115367 +  int autoinc = 0;
1.115368 +
1.115369 +  /* Ensure the database schema has been loaded */
1.115370 +  sqlite3_mutex_enter(db->mutex);
1.115371 +  sqlite3BtreeEnterAll(db);
1.115372 +  rc = sqlite3Init(db, &zErrMsg);
1.115373 +  if( SQLITE_OK!=rc ){
1.115374 +    goto error_out;
1.115375 +  }
1.115376 +
1.115377 +  /* Locate the table in question */
1.115378 +  pTab = sqlite3FindTable(db, zTableName, zDbName);
1.115379 +  if( !pTab || pTab->pSelect ){
1.115380 +    pTab = 0;
1.115381 +    goto error_out;
1.115382 +  }
1.115383 +
1.115384 +  /* Find the column for which info is requested */
1.115385 +  if( sqlite3IsRowid(zColumnName) ){
1.115386 +    iCol = pTab->iPKey;
1.115387 +    if( iCol>=0 ){
1.115388 +      pCol = &pTab->aCol[iCol];
1.115389 +    }
1.115390 +  }else{
1.115391 +    for(iCol=0; iCol<pTab->nCol; iCol++){
1.115392 +      pCol = &pTab->aCol[iCol];
1.115393 +      if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
1.115394 +        break;
1.115395 +      }
1.115396 +    }
1.115397 +    if( iCol==pTab->nCol ){
1.115398 +      pTab = 0;
1.115399 +      goto error_out;
1.115400 +    }
1.115401 +  }
1.115402 +
1.115403 +  /* The following block stores the meta information that will be returned
1.115404 +  ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
1.115405 +  ** and autoinc. At this point there are two possibilities:
1.115406 +  ** 
1.115407 +  **     1. The specified column name was rowid", "oid" or "_rowid_" 
1.115408 +  **        and there is no explicitly declared IPK column. 
1.115409 +  **
1.115410 +  **     2. The table is not a view and the column name identified an 
1.115411 +  **        explicitly declared column. Copy meta information from *pCol.
1.115412 +  */ 
1.115413 +  if( pCol ){
1.115414 +    zDataType = pCol->zType;
1.115415 +    zCollSeq = pCol->zColl;
1.115416 +    notnull = pCol->notNull!=0;
1.115417 +    primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
1.115418 +    autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
1.115419 +  }else{
1.115420 +    zDataType = "INTEGER";
1.115421 +    primarykey = 1;
1.115422 +  }
1.115423 +  if( !zCollSeq ){
1.115424 +    zCollSeq = "BINARY";
1.115425 +  }
1.115426 +
1.115427 +error_out:
1.115428 +  sqlite3BtreeLeaveAll(db);
1.115429 +
1.115430 +  /* Whether the function call succeeded or failed, set the output parameters
1.115431 +  ** to whatever their local counterparts contain. If an error did occur,
1.115432 +  ** this has the effect of zeroing all output parameters.
1.115433 +  */
1.115434 +  if( pzDataType ) *pzDataType = zDataType;
1.115435 +  if( pzCollSeq ) *pzCollSeq = zCollSeq;
1.115436 +  if( pNotNull ) *pNotNull = notnull;
1.115437 +  if( pPrimaryKey ) *pPrimaryKey = primarykey;
1.115438 +  if( pAutoinc ) *pAutoinc = autoinc;
1.115439 +
1.115440 +  if( SQLITE_OK==rc && !pTab ){
1.115441 +    sqlite3DbFree(db, zErrMsg);
1.115442 +    zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
1.115443 +        zColumnName);
1.115444 +    rc = SQLITE_ERROR;
1.115445 +  }
1.115446 +  sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
1.115447 +  sqlite3DbFree(db, zErrMsg);
1.115448 +  rc = sqlite3ApiExit(db, rc);
1.115449 +  sqlite3_mutex_leave(db->mutex);
1.115450 +  return rc;
1.115451 +}
1.115452 +#endif
1.115453 +
1.115454 +/*
1.115455 +** Sleep for a little while.  Return the amount of time slept.
1.115456 +*/
1.115457 +SQLITE_API int sqlite3_sleep(int ms){
1.115458 +  sqlite3_vfs *pVfs;
1.115459 +  int rc;
1.115460 +  pVfs = sqlite3_vfs_find(0);
1.115461 +  if( pVfs==0 ) return 0;
1.115462 +
1.115463 +  /* This function works in milliseconds, but the underlying OsSleep() 
1.115464 +  ** API uses microseconds. Hence the 1000's.
1.115465 +  */
1.115466 +  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
1.115467 +  return rc;
1.115468 +}
1.115469 +
1.115470 +/*
1.115471 +** Enable or disable the extended result codes.
1.115472 +*/
1.115473 +SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
1.115474 +  sqlite3_mutex_enter(db->mutex);
1.115475 +  db->errMask = onoff ? 0xffffffff : 0xff;
1.115476 +  sqlite3_mutex_leave(db->mutex);
1.115477 +  return SQLITE_OK;
1.115478 +}
1.115479 +
1.115480 +/*
1.115481 +** Invoke the xFileControl method on a particular database.
1.115482 +*/
1.115483 +SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
1.115484 +  int rc = SQLITE_ERROR;
1.115485 +  Btree *pBtree;
1.115486 +
1.115487 +  sqlite3_mutex_enter(db->mutex);
1.115488 +  pBtree = sqlite3DbNameToBtree(db, zDbName);
1.115489 +  if( pBtree ){
1.115490 +    Pager *pPager;
1.115491 +    sqlite3_file *fd;
1.115492 +    sqlite3BtreeEnter(pBtree);
1.115493 +    pPager = sqlite3BtreePager(pBtree);
1.115494 +    assert( pPager!=0 );
1.115495 +    fd = sqlite3PagerFile(pPager);
1.115496 +    assert( fd!=0 );
1.115497 +    if( op==SQLITE_FCNTL_FILE_POINTER ){
1.115498 +      *(sqlite3_file**)pArg = fd;
1.115499 +      rc = SQLITE_OK;
1.115500 +    }else if( fd->pMethods ){
1.115501 +      rc = sqlite3OsFileControl(fd, op, pArg);
1.115502 +    }else{
1.115503 +      rc = SQLITE_NOTFOUND;
1.115504 +    }
1.115505 +    sqlite3BtreeLeave(pBtree);
1.115506 +  }
1.115507 +  sqlite3_mutex_leave(db->mutex);
1.115508 +  return rc;   
1.115509 +}
1.115510 +
1.115511 +/*
1.115512 +** Interface to the testing logic.
1.115513 +*/
1.115514 +SQLITE_API int sqlite3_test_control(int op, ...){
1.115515 +  int rc = 0;
1.115516 +#ifndef SQLITE_OMIT_BUILTIN_TEST
1.115517 +  va_list ap;
1.115518 +  va_start(ap, op);
1.115519 +  switch( op ){
1.115520 +
1.115521 +    /*
1.115522 +    ** Save the current state of the PRNG.
1.115523 +    */
1.115524 +    case SQLITE_TESTCTRL_PRNG_SAVE: {
1.115525 +      sqlite3PrngSaveState();
1.115526 +      break;
1.115527 +    }
1.115528 +
1.115529 +    /*
1.115530 +    ** Restore the state of the PRNG to the last state saved using
1.115531 +    ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
1.115532 +    ** this verb acts like PRNG_RESET.
1.115533 +    */
1.115534 +    case SQLITE_TESTCTRL_PRNG_RESTORE: {
1.115535 +      sqlite3PrngRestoreState();
1.115536 +      break;
1.115537 +    }
1.115538 +
1.115539 +    /*
1.115540 +    ** Reset the PRNG back to its uninitialized state.  The next call
1.115541 +    ** to sqlite3_randomness() will reseed the PRNG using a single call
1.115542 +    ** to the xRandomness method of the default VFS.
1.115543 +    */
1.115544 +    case SQLITE_TESTCTRL_PRNG_RESET: {
1.115545 +      sqlite3PrngResetState();
1.115546 +      break;
1.115547 +    }
1.115548 +
1.115549 +    /*
1.115550 +    **  sqlite3_test_control(BITVEC_TEST, size, program)
1.115551 +    **
1.115552 +    ** Run a test against a Bitvec object of size.  The program argument
1.115553 +    ** is an array of integers that defines the test.  Return -1 on a
1.115554 +    ** memory allocation error, 0 on success, or non-zero for an error.
1.115555 +    ** See the sqlite3BitvecBuiltinTest() for additional information.
1.115556 +    */
1.115557 +    case SQLITE_TESTCTRL_BITVEC_TEST: {
1.115558 +      int sz = va_arg(ap, int);
1.115559 +      int *aProg = va_arg(ap, int*);
1.115560 +      rc = sqlite3BitvecBuiltinTest(sz, aProg);
1.115561 +      break;
1.115562 +    }
1.115563 +
1.115564 +    /*
1.115565 +    **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
1.115566 +    **
1.115567 +    ** Register hooks to call to indicate which malloc() failures 
1.115568 +    ** are benign.
1.115569 +    */
1.115570 +    case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
1.115571 +      typedef void (*void_function)(void);
1.115572 +      void_function xBenignBegin;
1.115573 +      void_function xBenignEnd;
1.115574 +      xBenignBegin = va_arg(ap, void_function);
1.115575 +      xBenignEnd = va_arg(ap, void_function);
1.115576 +      sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
1.115577 +      break;
1.115578 +    }
1.115579 +
1.115580 +    /*
1.115581 +    **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
1.115582 +    **
1.115583 +    ** Set the PENDING byte to the value in the argument, if X>0.
1.115584 +    ** Make no changes if X==0.  Return the value of the pending byte
1.115585 +    ** as it existing before this routine was called.
1.115586 +    **
1.115587 +    ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
1.115588 +    ** an incompatible database file format.  Changing the PENDING byte
1.115589 +    ** while any database connection is open results in undefined and
1.115590 +    ** dileterious behavior.
1.115591 +    */
1.115592 +    case SQLITE_TESTCTRL_PENDING_BYTE: {
1.115593 +      rc = PENDING_BYTE;
1.115594 +#ifndef SQLITE_OMIT_WSD
1.115595 +      {
1.115596 +        unsigned int newVal = va_arg(ap, unsigned int);
1.115597 +        if( newVal ) sqlite3PendingByte = newVal;
1.115598 +      }
1.115599 +#endif
1.115600 +      break;
1.115601 +    }
1.115602 +
1.115603 +    /*
1.115604 +    **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
1.115605 +    **
1.115606 +    ** This action provides a run-time test to see whether or not
1.115607 +    ** assert() was enabled at compile-time.  If X is true and assert()
1.115608 +    ** is enabled, then the return value is true.  If X is true and
1.115609 +    ** assert() is disabled, then the return value is zero.  If X is
1.115610 +    ** false and assert() is enabled, then the assertion fires and the
1.115611 +    ** process aborts.  If X is false and assert() is disabled, then the
1.115612 +    ** return value is zero.
1.115613 +    */
1.115614 +    case SQLITE_TESTCTRL_ASSERT: {
1.115615 +      volatile int x = 0;
1.115616 +      assert( (x = va_arg(ap,int))!=0 );
1.115617 +      rc = x;
1.115618 +      break;
1.115619 +    }
1.115620 +
1.115621 +
1.115622 +    /*
1.115623 +    **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
1.115624 +    **
1.115625 +    ** This action provides a run-time test to see how the ALWAYS and
1.115626 +    ** NEVER macros were defined at compile-time.
1.115627 +    **
1.115628 +    ** The return value is ALWAYS(X).  
1.115629 +    **
1.115630 +    ** The recommended test is X==2.  If the return value is 2, that means
1.115631 +    ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
1.115632 +    ** default setting.  If the return value is 1, then ALWAYS() is either
1.115633 +    ** hard-coded to true or else it asserts if its argument is false.
1.115634 +    ** The first behavior (hard-coded to true) is the case if
1.115635 +    ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
1.115636 +    ** behavior (assert if the argument to ALWAYS() is false) is the case if
1.115637 +    ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
1.115638 +    **
1.115639 +    ** The run-time test procedure might look something like this:
1.115640 +    **
1.115641 +    **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
1.115642 +    **      // ALWAYS() and NEVER() are no-op pass-through macros
1.115643 +    **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
1.115644 +    **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
1.115645 +    **    }else{
1.115646 +    **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
1.115647 +    **    }
1.115648 +    */
1.115649 +    case SQLITE_TESTCTRL_ALWAYS: {
1.115650 +      int x = va_arg(ap,int);
1.115651 +      rc = ALWAYS(x);
1.115652 +      break;
1.115653 +    }
1.115654 +
1.115655 +    /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
1.115656 +    **
1.115657 +    ** Set the nReserve size to N for the main database on the database
1.115658 +    ** connection db.
1.115659 +    */
1.115660 +    case SQLITE_TESTCTRL_RESERVE: {
1.115661 +      sqlite3 *db = va_arg(ap, sqlite3*);
1.115662 +      int x = va_arg(ap,int);
1.115663 +      sqlite3_mutex_enter(db->mutex);
1.115664 +      sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
1.115665 +      sqlite3_mutex_leave(db->mutex);
1.115666 +      break;
1.115667 +    }
1.115668 +
1.115669 +    /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
1.115670 +    **
1.115671 +    ** Enable or disable various optimizations for testing purposes.  The 
1.115672 +    ** argument N is a bitmask of optimizations to be disabled.  For normal
1.115673 +    ** operation N should be 0.  The idea is that a test program (like the
1.115674 +    ** SQL Logic Test or SLT test module) can run the same SQL multiple times
1.115675 +    ** with various optimizations disabled to verify that the same answer
1.115676 +    ** is obtained in every case.
1.115677 +    */
1.115678 +    case SQLITE_TESTCTRL_OPTIMIZATIONS: {
1.115679 +      sqlite3 *db = va_arg(ap, sqlite3*);
1.115680 +      db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
1.115681 +      break;
1.115682 +    }
1.115683 +
1.115684 +#ifdef SQLITE_N_KEYWORD
1.115685 +    /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
1.115686 +    **
1.115687 +    ** If zWord is a keyword recognized by the parser, then return the
1.115688 +    ** number of keywords.  Or if zWord is not a keyword, return 0.
1.115689 +    ** 
1.115690 +    ** This test feature is only available in the amalgamation since
1.115691 +    ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
1.115692 +    ** is built using separate source files.
1.115693 +    */
1.115694 +    case SQLITE_TESTCTRL_ISKEYWORD: {
1.115695 +      const char *zWord = va_arg(ap, const char*);
1.115696 +      int n = sqlite3Strlen30(zWord);
1.115697 +      rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
1.115698 +      break;
1.115699 +    }
1.115700 +#endif 
1.115701 +
1.115702 +    /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
1.115703 +    **
1.115704 +    ** Pass pFree into sqlite3ScratchFree(). 
1.115705 +    ** If sz>0 then allocate a scratch buffer into pNew.  
1.115706 +    */
1.115707 +    case SQLITE_TESTCTRL_SCRATCHMALLOC: {
1.115708 +      void *pFree, **ppNew;
1.115709 +      int sz;
1.115710 +      sz = va_arg(ap, int);
1.115711 +      ppNew = va_arg(ap, void**);
1.115712 +      pFree = va_arg(ap, void*);
1.115713 +      if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
1.115714 +      sqlite3ScratchFree(pFree);
1.115715 +      break;
1.115716 +    }
1.115717 +
1.115718 +    /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
1.115719 +    **
1.115720 +    ** If parameter onoff is non-zero, configure the wrappers so that all
1.115721 +    ** subsequent calls to localtime() and variants fail. If onoff is zero,
1.115722 +    ** undo this setting.
1.115723 +    */
1.115724 +    case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
1.115725 +      sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
1.115726 +      break;
1.115727 +    }
1.115728 +
1.115729 +#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
1.115730 +    /*   sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
1.115731 +    **                        sqlite3_stmt*,const char**);
1.115732 +    **
1.115733 +    ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
1.115734 +    ** a string that describes the optimized parse tree.  This test-control
1.115735 +    ** returns a pointer to that string.
1.115736 +    */
1.115737 +    case SQLITE_TESTCTRL_EXPLAIN_STMT: {
1.115738 +      sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
1.115739 +      const char **pzRet = va_arg(ap, const char**);
1.115740 +      *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
1.115741 +      break;
1.115742 +    }
1.115743 +#endif
1.115744 +
1.115745 +  }
1.115746 +  va_end(ap);
1.115747 +#endif /* SQLITE_OMIT_BUILTIN_TEST */
1.115748 +  return rc;
1.115749 +}
1.115750 +
1.115751 +/*
1.115752 +** This is a utility routine, useful to VFS implementations, that checks
1.115753 +** to see if a database file was a URI that contained a specific query 
1.115754 +** parameter, and if so obtains the value of the query parameter.
1.115755 +**
1.115756 +** The zFilename argument is the filename pointer passed into the xOpen()
1.115757 +** method of a VFS implementation.  The zParam argument is the name of the
1.115758 +** query parameter we seek.  This routine returns the value of the zParam
1.115759 +** parameter if it exists.  If the parameter does not exist, this routine
1.115760 +** returns a NULL pointer.
1.115761 +*/
1.115762 +SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
1.115763 +  if( zFilename==0 ) return 0;
1.115764 +  zFilename += sqlite3Strlen30(zFilename) + 1;
1.115765 +  while( zFilename[0] ){
1.115766 +    int x = strcmp(zFilename, zParam);
1.115767 +    zFilename += sqlite3Strlen30(zFilename) + 1;
1.115768 +    if( x==0 ) return zFilename;
1.115769 +    zFilename += sqlite3Strlen30(zFilename) + 1;
1.115770 +  }
1.115771 +  return 0;
1.115772 +}
1.115773 +
1.115774 +/*
1.115775 +** Return a boolean value for a query parameter.
1.115776 +*/
1.115777 +SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
1.115778 +  const char *z = sqlite3_uri_parameter(zFilename, zParam);
1.115779 +  bDflt = bDflt!=0;
1.115780 +  return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
1.115781 +}
1.115782 +
1.115783 +/*
1.115784 +** Return a 64-bit integer value for a query parameter.
1.115785 +*/
1.115786 +SQLITE_API sqlite3_int64 sqlite3_uri_int64(
1.115787 +  const char *zFilename,    /* Filename as passed to xOpen */
1.115788 +  const char *zParam,       /* URI parameter sought */
1.115789 +  sqlite3_int64 bDflt       /* return if parameter is missing */
1.115790 +){
1.115791 +  const char *z = sqlite3_uri_parameter(zFilename, zParam);
1.115792 +  sqlite3_int64 v;
1.115793 +  if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
1.115794 +    bDflt = v;
1.115795 +  }
1.115796 +  return bDflt;
1.115797 +}
1.115798 +
1.115799 +/*
1.115800 +** Return the Btree pointer identified by zDbName.  Return NULL if not found.
1.115801 +*/
1.115802 +SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
1.115803 +  int i;
1.115804 +  for(i=0; i<db->nDb; i++){
1.115805 +    if( db->aDb[i].pBt
1.115806 +     && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
1.115807 +    ){
1.115808 +      return db->aDb[i].pBt;
1.115809 +    }
1.115810 +  }
1.115811 +  return 0;
1.115812 +}
1.115813 +
1.115814 +/*
1.115815 +** Return the filename of the database associated with a database
1.115816 +** connection.
1.115817 +*/
1.115818 +SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
1.115819 +  Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
1.115820 +  return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
1.115821 +}
1.115822 +
1.115823 +/*
1.115824 +** Return 1 if database is read-only or 0 if read/write.  Return -1 if
1.115825 +** no such database exists.
1.115826 +*/
1.115827 +SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
1.115828 +  Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
1.115829 +  return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
1.115830 +}
1.115831 +
1.115832 +/************** End of main.c ************************************************/
1.115833 +/************** Begin file notify.c ******************************************/
1.115834 +/*
1.115835 +** 2009 March 3
1.115836 +**
1.115837 +** The author disclaims copyright to this source code.  In place of
1.115838 +** a legal notice, here is a blessing:
1.115839 +**
1.115840 +**    May you do good and not evil.
1.115841 +**    May you find forgiveness for yourself and forgive others.
1.115842 +**    May you share freely, never taking more than you give.
1.115843 +**
1.115844 +*************************************************************************
1.115845 +**
1.115846 +** This file contains the implementation of the sqlite3_unlock_notify()
1.115847 +** API method and its associated functionality.
1.115848 +*/
1.115849 +
1.115850 +/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
1.115851 +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1.115852 +
1.115853 +/*
1.115854 +** Public interfaces:
1.115855 +**
1.115856 +**   sqlite3ConnectionBlocked()
1.115857 +**   sqlite3ConnectionUnlocked()
1.115858 +**   sqlite3ConnectionClosed()
1.115859 +**   sqlite3_unlock_notify()
1.115860 +*/
1.115861 +
1.115862 +#define assertMutexHeld() \
1.115863 +  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
1.115864 +
1.115865 +/*
1.115866 +** Head of a linked list of all sqlite3 objects created by this process
1.115867 +** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
1.115868 +** is not NULL. This variable may only accessed while the STATIC_MASTER
1.115869 +** mutex is held.
1.115870 +*/
1.115871 +static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
1.115872 +
1.115873 +#ifndef NDEBUG
1.115874 +/*
1.115875 +** This function is a complex assert() that verifies the following 
1.115876 +** properties of the blocked connections list:
1.115877 +**
1.115878 +**   1) Each entry in the list has a non-NULL value for either 
1.115879 +**      pUnlockConnection or pBlockingConnection, or both.
1.115880 +**
1.115881 +**   2) All entries in the list that share a common value for 
1.115882 +**      xUnlockNotify are grouped together.
1.115883 +**
1.115884 +**   3) If the argument db is not NULL, then none of the entries in the
1.115885 +**      blocked connections list have pUnlockConnection or pBlockingConnection
1.115886 +**      set to db. This is used when closing connection db.
1.115887 +*/
1.115888 +static void checkListProperties(sqlite3 *db){
1.115889 +  sqlite3 *p;
1.115890 +  for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
1.115891 +    int seen = 0;
1.115892 +    sqlite3 *p2;
1.115893 +
1.115894 +    /* Verify property (1) */
1.115895 +    assert( p->pUnlockConnection || p->pBlockingConnection );
1.115896 +
1.115897 +    /* Verify property (2) */
1.115898 +    for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
1.115899 +      if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
1.115900 +      assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
1.115901 +      assert( db==0 || p->pUnlockConnection!=db );
1.115902 +      assert( db==0 || p->pBlockingConnection!=db );
1.115903 +    }
1.115904 +  }
1.115905 +}
1.115906 +#else
1.115907 +# define checkListProperties(x)
1.115908 +#endif
1.115909 +
1.115910 +/*
1.115911 +** Remove connection db from the blocked connections list. If connection
1.115912 +** db is not currently a part of the list, this function is a no-op.
1.115913 +*/
1.115914 +static void removeFromBlockedList(sqlite3 *db){
1.115915 +  sqlite3 **pp;
1.115916 +  assertMutexHeld();
1.115917 +  for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
1.115918 +    if( *pp==db ){
1.115919 +      *pp = (*pp)->pNextBlocked;
1.115920 +      break;
1.115921 +    }
1.115922 +  }
1.115923 +}
1.115924 +
1.115925 +/*
1.115926 +** Add connection db to the blocked connections list. It is assumed
1.115927 +** that it is not already a part of the list.
1.115928 +*/
1.115929 +static void addToBlockedList(sqlite3 *db){
1.115930 +  sqlite3 **pp;
1.115931 +  assertMutexHeld();
1.115932 +  for(
1.115933 +    pp=&sqlite3BlockedList; 
1.115934 +    *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; 
1.115935 +    pp=&(*pp)->pNextBlocked
1.115936 +  );
1.115937 +  db->pNextBlocked = *pp;
1.115938 +  *pp = db;
1.115939 +}
1.115940 +
1.115941 +/*
1.115942 +** Obtain the STATIC_MASTER mutex.
1.115943 +*/
1.115944 +static void enterMutex(void){
1.115945 +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
1.115946 +  checkListProperties(0);
1.115947 +}
1.115948 +
1.115949 +/*
1.115950 +** Release the STATIC_MASTER mutex.
1.115951 +*/
1.115952 +static void leaveMutex(void){
1.115953 +  assertMutexHeld();
1.115954 +  checkListProperties(0);
1.115955 +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
1.115956 +}
1.115957 +
1.115958 +/*
1.115959 +** Register an unlock-notify callback.
1.115960 +**
1.115961 +** This is called after connection "db" has attempted some operation
1.115962 +** but has received an SQLITE_LOCKED error because another connection
1.115963 +** (call it pOther) in the same process was busy using the same shared
1.115964 +** cache.  pOther is found by looking at db->pBlockingConnection.
1.115965 +**
1.115966 +** If there is no blocking connection, the callback is invoked immediately,
1.115967 +** before this routine returns.
1.115968 +**
1.115969 +** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
1.115970 +** a deadlock.
1.115971 +**
1.115972 +** Otherwise, make arrangements to invoke xNotify when pOther drops
1.115973 +** its locks.
1.115974 +**
1.115975 +** Each call to this routine overrides any prior callbacks registered
1.115976 +** on the same "db".  If xNotify==0 then any prior callbacks are immediately
1.115977 +** cancelled.
1.115978 +*/
1.115979 +SQLITE_API int sqlite3_unlock_notify(
1.115980 +  sqlite3 *db,
1.115981 +  void (*xNotify)(void **, int),
1.115982 +  void *pArg
1.115983 +){
1.115984 +  int rc = SQLITE_OK;
1.115985 +
1.115986 +  sqlite3_mutex_enter(db->mutex);
1.115987 +  enterMutex();
1.115988 +
1.115989 +  if( xNotify==0 ){
1.115990 +    removeFromBlockedList(db);
1.115991 +    db->pBlockingConnection = 0;
1.115992 +    db->pUnlockConnection = 0;
1.115993 +    db->xUnlockNotify = 0;
1.115994 +    db->pUnlockArg = 0;
1.115995 +  }else if( 0==db->pBlockingConnection ){
1.115996 +    /* The blocking transaction has been concluded. Or there never was a 
1.115997 +    ** blocking transaction. In either case, invoke the notify callback
1.115998 +    ** immediately. 
1.115999 +    */
1.116000 +    xNotify(&pArg, 1);
1.116001 +  }else{
1.116002 +    sqlite3 *p;
1.116003 +
1.116004 +    for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
1.116005 +    if( p ){
1.116006 +      rc = SQLITE_LOCKED;              /* Deadlock detected. */
1.116007 +    }else{
1.116008 +      db->pUnlockConnection = db->pBlockingConnection;
1.116009 +      db->xUnlockNotify = xNotify;
1.116010 +      db->pUnlockArg = pArg;
1.116011 +      removeFromBlockedList(db);
1.116012 +      addToBlockedList(db);
1.116013 +    }
1.116014 +  }
1.116015 +
1.116016 +  leaveMutex();
1.116017 +  assert( !db->mallocFailed );
1.116018 +  sqlite3Error(db, rc, (rc?"database is deadlocked":0));
1.116019 +  sqlite3_mutex_leave(db->mutex);
1.116020 +  return rc;
1.116021 +}
1.116022 +
1.116023 +/*
1.116024 +** This function is called while stepping or preparing a statement 
1.116025 +** associated with connection db. The operation will return SQLITE_LOCKED
1.116026 +** to the user because it requires a lock that will not be available
1.116027 +** until connection pBlocker concludes its current transaction.
1.116028 +*/
1.116029 +SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
1.116030 +  enterMutex();
1.116031 +  if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
1.116032 +    addToBlockedList(db);
1.116033 +  }
1.116034 +  db->pBlockingConnection = pBlocker;
1.116035 +  leaveMutex();
1.116036 +}
1.116037 +
1.116038 +/*
1.116039 +** This function is called when
1.116040 +** the transaction opened by database db has just finished. Locks held 
1.116041 +** by database connection db have been released.
1.116042 +**
1.116043 +** This function loops through each entry in the blocked connections
1.116044 +** list and does the following:
1.116045 +**
1.116046 +**   1) If the sqlite3.pBlockingConnection member of a list entry is
1.116047 +**      set to db, then set pBlockingConnection=0.
1.116048 +**
1.116049 +**   2) If the sqlite3.pUnlockConnection member of a list entry is
1.116050 +**      set to db, then invoke the configured unlock-notify callback and
1.116051 +**      set pUnlockConnection=0.
1.116052 +**
1.116053 +**   3) If the two steps above mean that pBlockingConnection==0 and
1.116054 +**      pUnlockConnection==0, remove the entry from the blocked connections
1.116055 +**      list.
1.116056 +*/
1.116057 +SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
1.116058 +  void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
1.116059 +  int nArg = 0;                            /* Number of entries in aArg[] */
1.116060 +  sqlite3 **pp;                            /* Iterator variable */
1.116061 +  void **aArg;               /* Arguments to the unlock callback */
1.116062 +  void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
1.116063 +  void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
1.116064 +
1.116065 +  aArg = aStatic;
1.116066 +  enterMutex();         /* Enter STATIC_MASTER mutex */
1.116067 +
1.116068 +  /* This loop runs once for each entry in the blocked-connections list. */
1.116069 +  for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
1.116070 +    sqlite3 *p = *pp;
1.116071 +
1.116072 +    /* Step 1. */
1.116073 +    if( p->pBlockingConnection==db ){
1.116074 +      p->pBlockingConnection = 0;
1.116075 +    }
1.116076 +
1.116077 +    /* Step 2. */
1.116078 +    if( p->pUnlockConnection==db ){
1.116079 +      assert( p->xUnlockNotify );
1.116080 +      if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
1.116081 +        xUnlockNotify(aArg, nArg);
1.116082 +        nArg = 0;
1.116083 +      }
1.116084 +
1.116085 +      sqlite3BeginBenignMalloc();
1.116086 +      assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
1.116087 +      assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
1.116088 +      if( (!aDyn && nArg==(int)ArraySize(aStatic))
1.116089 +       || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
1.116090 +      ){
1.116091 +        /* The aArg[] array needs to grow. */
1.116092 +        void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
1.116093 +        if( pNew ){
1.116094 +          memcpy(pNew, aArg, nArg*sizeof(void *));
1.116095 +          sqlite3_free(aDyn);
1.116096 +          aDyn = aArg = pNew;
1.116097 +        }else{
1.116098 +          /* This occurs when the array of context pointers that need to
1.116099 +          ** be passed to the unlock-notify callback is larger than the
1.116100 +          ** aStatic[] array allocated on the stack and the attempt to 
1.116101 +          ** allocate a larger array from the heap has failed.
1.116102 +          **
1.116103 +          ** This is a difficult situation to handle. Returning an error
1.116104 +          ** code to the caller is insufficient, as even if an error code
1.116105 +          ** is returned the transaction on connection db will still be
1.116106 +          ** closed and the unlock-notify callbacks on blocked connections
1.116107 +          ** will go unissued. This might cause the application to wait
1.116108 +          ** indefinitely for an unlock-notify callback that will never 
1.116109 +          ** arrive.
1.116110 +          **
1.116111 +          ** Instead, invoke the unlock-notify callback with the context
1.116112 +          ** array already accumulated. We can then clear the array and
1.116113 +          ** begin accumulating any further context pointers without 
1.116114 +          ** requiring any dynamic allocation. This is sub-optimal because
1.116115 +          ** it means that instead of one callback with a large array of
1.116116 +          ** context pointers the application will receive two or more
1.116117 +          ** callbacks with smaller arrays of context pointers, which will
1.116118 +          ** reduce the applications ability to prioritize multiple 
1.116119 +          ** connections. But it is the best that can be done under the
1.116120 +          ** circumstances.
1.116121 +          */
1.116122 +          xUnlockNotify(aArg, nArg);
1.116123 +          nArg = 0;
1.116124 +        }
1.116125 +      }
1.116126 +      sqlite3EndBenignMalloc();
1.116127 +
1.116128 +      aArg[nArg++] = p->pUnlockArg;
1.116129 +      xUnlockNotify = p->xUnlockNotify;
1.116130 +      p->pUnlockConnection = 0;
1.116131 +      p->xUnlockNotify = 0;
1.116132 +      p->pUnlockArg = 0;
1.116133 +    }
1.116134 +
1.116135 +    /* Step 3. */
1.116136 +    if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
1.116137 +      /* Remove connection p from the blocked connections list. */
1.116138 +      *pp = p->pNextBlocked;
1.116139 +      p->pNextBlocked = 0;
1.116140 +    }else{
1.116141 +      pp = &p->pNextBlocked;
1.116142 +    }
1.116143 +  }
1.116144 +
1.116145 +  if( nArg!=0 ){
1.116146 +    xUnlockNotify(aArg, nArg);
1.116147 +  }
1.116148 +  sqlite3_free(aDyn);
1.116149 +  leaveMutex();         /* Leave STATIC_MASTER mutex */
1.116150 +}
1.116151 +
1.116152 +/*
1.116153 +** This is called when the database connection passed as an argument is 
1.116154 +** being closed. The connection is removed from the blocked list.
1.116155 +*/
1.116156 +SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
1.116157 +  sqlite3ConnectionUnlocked(db);
1.116158 +  enterMutex();
1.116159 +  removeFromBlockedList(db);
1.116160 +  checkListProperties(db);
1.116161 +  leaveMutex();
1.116162 +}
1.116163 +#endif
1.116164 +
1.116165 +/************** End of notify.c **********************************************/
1.116166 +/************** Begin file fts3.c ********************************************/
1.116167 +/*
1.116168 +** 2006 Oct 10
1.116169 +**
1.116170 +** The author disclaims copyright to this source code.  In place of
1.116171 +** a legal notice, here is a blessing:
1.116172 +**
1.116173 +**    May you do good and not evil.
1.116174 +**    May you find forgiveness for yourself and forgive others.
1.116175 +**    May you share freely, never taking more than you give.
1.116176 +**
1.116177 +******************************************************************************
1.116178 +**
1.116179 +** This is an SQLite module implementing full-text search.
1.116180 +*/
1.116181 +
1.116182 +/*
1.116183 +** The code in this file is only compiled if:
1.116184 +**
1.116185 +**     * The FTS3 module is being built as an extension
1.116186 +**       (in which case SQLITE_CORE is not defined), or
1.116187 +**
1.116188 +**     * The FTS3 module is being built into the core of
1.116189 +**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
1.116190 +*/
1.116191 +
1.116192 +/* The full-text index is stored in a series of b+tree (-like)
1.116193 +** structures called segments which map terms to doclists.  The
1.116194 +** structures are like b+trees in layout, but are constructed from the
1.116195 +** bottom up in optimal fashion and are not updatable.  Since trees
1.116196 +** are built from the bottom up, things will be described from the
1.116197 +** bottom up.
1.116198 +**
1.116199 +**
1.116200 +**** Varints ****
1.116201 +** The basic unit of encoding is a variable-length integer called a
1.116202 +** varint.  We encode variable-length integers in little-endian order
1.116203 +** using seven bits * per byte as follows:
1.116204 +**
1.116205 +** KEY:
1.116206 +**         A = 0xxxxxxx    7 bits of data and one flag bit
1.116207 +**         B = 1xxxxxxx    7 bits of data and one flag bit
1.116208 +**
1.116209 +**  7 bits - A
1.116210 +** 14 bits - BA
1.116211 +** 21 bits - BBA
1.116212 +** and so on.
1.116213 +**
1.116214 +** This is similar in concept to how sqlite encodes "varints" but
1.116215 +** the encoding is not the same.  SQLite varints are big-endian
1.116216 +** are are limited to 9 bytes in length whereas FTS3 varints are
1.116217 +** little-endian and can be up to 10 bytes in length (in theory).
1.116218 +**
1.116219 +** Example encodings:
1.116220 +**
1.116221 +**     1:    0x01
1.116222 +**   127:    0x7f
1.116223 +**   128:    0x81 0x00
1.116224 +**
1.116225 +**
1.116226 +**** Document lists ****
1.116227 +** A doclist (document list) holds a docid-sorted list of hits for a
1.116228 +** given term.  Doclists hold docids and associated token positions.
1.116229 +** A docid is the unique integer identifier for a single document.
1.116230 +** A position is the index of a word within the document.  The first 
1.116231 +** word of the document has a position of 0.
1.116232 +**
1.116233 +** FTS3 used to optionally store character offsets using a compile-time
1.116234 +** option.  But that functionality is no longer supported.
1.116235 +**
1.116236 +** A doclist is stored like this:
1.116237 +**
1.116238 +** array {
1.116239 +**   varint docid;          (delta from previous doclist)
1.116240 +**   array {                (position list for column 0)
1.116241 +**     varint position;     (2 more than the delta from previous position)
1.116242 +**   }
1.116243 +**   array {
1.116244 +**     varint POS_COLUMN;   (marks start of position list for new column)
1.116245 +**     varint column;       (index of new column)
1.116246 +**     array {
1.116247 +**       varint position;   (2 more than the delta from previous position)
1.116248 +**     }
1.116249 +**   }
1.116250 +**   varint POS_END;        (marks end of positions for this document.
1.116251 +** }
1.116252 +**
1.116253 +** Here, array { X } means zero or more occurrences of X, adjacent in
1.116254 +** memory.  A "position" is an index of a token in the token stream
1.116255 +** generated by the tokenizer. Note that POS_END and POS_COLUMN occur 
1.116256 +** in the same logical place as the position element, and act as sentinals
1.116257 +** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
1.116258 +** The positions numbers are not stored literally but rather as two more
1.116259 +** than the difference from the prior position, or the just the position plus
1.116260 +** 2 for the first position.  Example:
1.116261 +**
1.116262 +**   label:       A B C D E  F  G H   I  J K
1.116263 +**   value:     123 5 9 1 1 14 35 0 234 72 0
1.116264 +**
1.116265 +** The 123 value is the first docid.  For column zero in this document
1.116266 +** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
1.116267 +** at D signals the start of a new column; the 1 at E indicates that the
1.116268 +** new column is column number 1.  There are two positions at 12 and 45
1.116269 +** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
1.116270 +** 234 at I is the delta to next docid (357).  It has one position 70
1.116271 +** (72-2) and then terminates with the 0 at K.
1.116272 +**
1.116273 +** A "position-list" is the list of positions for multiple columns for
1.116274 +** a single docid.  A "column-list" is the set of positions for a single
1.116275 +** column.  Hence, a position-list consists of one or more column-lists,
1.116276 +** a document record consists of a docid followed by a position-list and
1.116277 +** a doclist consists of one or more document records.
1.116278 +**
1.116279 +** A bare doclist omits the position information, becoming an 
1.116280 +** array of varint-encoded docids.
1.116281 +**
1.116282 +**** Segment leaf nodes ****
1.116283 +** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
1.116284 +** nodes are written using LeafWriter, and read using LeafReader (to
1.116285 +** iterate through a single leaf node's data) and LeavesReader (to
1.116286 +** iterate through a segment's entire leaf layer).  Leaf nodes have
1.116287 +** the format:
1.116288 +**
1.116289 +** varint iHeight;             (height from leaf level, always 0)
1.116290 +** varint nTerm;               (length of first term)
1.116291 +** char pTerm[nTerm];          (content of first term)
1.116292 +** varint nDoclist;            (length of term's associated doclist)
1.116293 +** char pDoclist[nDoclist];    (content of doclist)
1.116294 +** array {
1.116295 +**                             (further terms are delta-encoded)
1.116296 +**   varint nPrefix;           (length of prefix shared with previous term)
1.116297 +**   varint nSuffix;           (length of unshared suffix)
1.116298 +**   char pTermSuffix[nSuffix];(unshared suffix of next term)
1.116299 +**   varint nDoclist;          (length of term's associated doclist)
1.116300 +**   char pDoclist[nDoclist];  (content of doclist)
1.116301 +** }
1.116302 +**
1.116303 +** Here, array { X } means zero or more occurrences of X, adjacent in
1.116304 +** memory.
1.116305 +**
1.116306 +** Leaf nodes are broken into blocks which are stored contiguously in
1.116307 +** the %_segments table in sorted order.  This means that when the end
1.116308 +** of a node is reached, the next term is in the node with the next
1.116309 +** greater node id.
1.116310 +**
1.116311 +** New data is spilled to a new leaf node when the current node
1.116312 +** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
1.116313 +** larger than STANDALONE_MIN (default 1024) is placed in a standalone
1.116314 +** node (a leaf node with a single term and doclist).  The goal of
1.116315 +** these settings is to pack together groups of small doclists while
1.116316 +** making it efficient to directly access large doclists.  The
1.116317 +** assumption is that large doclists represent terms which are more
1.116318 +** likely to be query targets.
1.116319 +**
1.116320 +** TODO(shess) It may be useful for blocking decisions to be more
1.116321 +** dynamic.  For instance, it may make more sense to have a 2.5k leaf
1.116322 +** node rather than splitting into 2k and .5k nodes.  My intuition is
1.116323 +** that this might extend through 2x or 4x the pagesize.
1.116324 +**
1.116325 +**
1.116326 +**** Segment interior nodes ****
1.116327 +** Segment interior nodes store blockids for subtree nodes and terms
1.116328 +** to describe what data is stored by the each subtree.  Interior
1.116329 +** nodes are written using InteriorWriter, and read using
1.116330 +** InteriorReader.  InteriorWriters are created as needed when
1.116331 +** SegmentWriter creates new leaf nodes, or when an interior node
1.116332 +** itself grows too big and must be split.  The format of interior
1.116333 +** nodes:
1.116334 +**
1.116335 +** varint iHeight;           (height from leaf level, always >0)
1.116336 +** varint iBlockid;          (block id of node's leftmost subtree)
1.116337 +** optional {
1.116338 +**   varint nTerm;           (length of first term)
1.116339 +**   char pTerm[nTerm];      (content of first term)
1.116340 +**   array {
1.116341 +**                                (further terms are delta-encoded)
1.116342 +**     varint nPrefix;            (length of shared prefix with previous term)
1.116343 +**     varint nSuffix;            (length of unshared suffix)
1.116344 +**     char pTermSuffix[nSuffix]; (unshared suffix of next term)
1.116345 +**   }
1.116346 +** }
1.116347 +**
1.116348 +** Here, optional { X } means an optional element, while array { X }
1.116349 +** means zero or more occurrences of X, adjacent in memory.
1.116350 +**
1.116351 +** An interior node encodes n terms separating n+1 subtrees.  The
1.116352 +** subtree blocks are contiguous, so only the first subtree's blockid
1.116353 +** is encoded.  The subtree at iBlockid will contain all terms less
1.116354 +** than the first term encoded (or all terms if no term is encoded).
1.116355 +** Otherwise, for terms greater than or equal to pTerm[i] but less
1.116356 +** than pTerm[i+1], the subtree for that term will be rooted at
1.116357 +** iBlockid+i.  Interior nodes only store enough term data to
1.116358 +** distinguish adjacent children (if the rightmost term of the left
1.116359 +** child is "something", and the leftmost term of the right child is
1.116360 +** "wicked", only "w" is stored).
1.116361 +**
1.116362 +** New data is spilled to a new interior node at the same height when
1.116363 +** the current node exceeds INTERIOR_MAX bytes (default 2048).
1.116364 +** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
1.116365 +** interior nodes and making the tree too skinny.  The interior nodes
1.116366 +** at a given height are naturally tracked by interior nodes at
1.116367 +** height+1, and so on.
1.116368 +**
1.116369 +**
1.116370 +**** Segment directory ****
1.116371 +** The segment directory in table %_segdir stores meta-information for
1.116372 +** merging and deleting segments, and also the root node of the
1.116373 +** segment's tree.
1.116374 +**
1.116375 +** The root node is the top node of the segment's tree after encoding
1.116376 +** the entire segment, restricted to ROOT_MAX bytes (default 1024).
1.116377 +** This could be either a leaf node or an interior node.  If the top
1.116378 +** node requires more than ROOT_MAX bytes, it is flushed to %_segments
1.116379 +** and a new root interior node is generated (which should always fit
1.116380 +** within ROOT_MAX because it only needs space for 2 varints, the
1.116381 +** height and the blockid of the previous root).
1.116382 +**
1.116383 +** The meta-information in the segment directory is:
1.116384 +**   level               - segment level (see below)
1.116385 +**   idx                 - index within level
1.116386 +**                       - (level,idx uniquely identify a segment)
1.116387 +**   start_block         - first leaf node
1.116388 +**   leaves_end_block    - last leaf node
1.116389 +**   end_block           - last block (including interior nodes)
1.116390 +**   root                - contents of root node
1.116391 +**
1.116392 +** If the root node is a leaf node, then start_block,
1.116393 +** leaves_end_block, and end_block are all 0.
1.116394 +**
1.116395 +**
1.116396 +**** Segment merging ****
1.116397 +** To amortize update costs, segments are grouped into levels and
1.116398 +** merged in batches.  Each increase in level represents exponentially
1.116399 +** more documents.
1.116400 +**
1.116401 +** New documents (actually, document updates) are tokenized and
1.116402 +** written individually (using LeafWriter) to a level 0 segment, with
1.116403 +** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
1.116404 +** level 0 segments are merged into a single level 1 segment.  Level 1
1.116405 +** is populated like level 0, and eventually MERGE_COUNT level 1
1.116406 +** segments are merged to a single level 2 segment (representing
1.116407 +** MERGE_COUNT^2 updates), and so on.
1.116408 +**
1.116409 +** A segment merge traverses all segments at a given level in
1.116410 +** parallel, performing a straightforward sorted merge.  Since segment
1.116411 +** leaf nodes are written in to the %_segments table in order, this
1.116412 +** merge traverses the underlying sqlite disk structures efficiently.
1.116413 +** After the merge, all segment blocks from the merged level are
1.116414 +** deleted.
1.116415 +**
1.116416 +** MERGE_COUNT controls how often we merge segments.  16 seems to be
1.116417 +** somewhat of a sweet spot for insertion performance.  32 and 64 show
1.116418 +** very similar performance numbers to 16 on insertion, though they're
1.116419 +** a tiny bit slower (perhaps due to more overhead in merge-time
1.116420 +** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
1.116421 +** 16, 2 about 66% slower than 16.
1.116422 +**
1.116423 +** At query time, high MERGE_COUNT increases the number of segments
1.116424 +** which need to be scanned and merged.  For instance, with 100k docs
1.116425 +** inserted:
1.116426 +**
1.116427 +**    MERGE_COUNT   segments
1.116428 +**       16           25
1.116429 +**        8           12
1.116430 +**        4           10
1.116431 +**        2            6
1.116432 +**
1.116433 +** This appears to have only a moderate impact on queries for very
1.116434 +** frequent terms (which are somewhat dominated by segment merge
1.116435 +** costs), and infrequent and non-existent terms still seem to be fast
1.116436 +** even with many segments.
1.116437 +**
1.116438 +** TODO(shess) That said, it would be nice to have a better query-side
1.116439 +** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
1.116440 +** optimizations to things like doclist merging will swing the sweet
1.116441 +** spot around.
1.116442 +**
1.116443 +**
1.116444 +**
1.116445 +**** Handling of deletions and updates ****
1.116446 +** Since we're using a segmented structure, with no docid-oriented
1.116447 +** index into the term index, we clearly cannot simply update the term
1.116448 +** index when a document is deleted or updated.  For deletions, we
1.116449 +** write an empty doclist (varint(docid) varint(POS_END)), for updates
1.116450 +** we simply write the new doclist.  Segment merges overwrite older
1.116451 +** data for a particular docid with newer data, so deletes or updates
1.116452 +** will eventually overtake the earlier data and knock it out.  The
1.116453 +** query logic likewise merges doclists so that newer data knocks out
1.116454 +** older data.
1.116455 +*/
1.116456 +
1.116457 +/************** Include fts3Int.h in the middle of fts3.c ********************/
1.116458 +/************** Begin file fts3Int.h *****************************************/
1.116459 +/*
1.116460 +** 2009 Nov 12
1.116461 +**
1.116462 +** The author disclaims copyright to this source code.  In place of
1.116463 +** a legal notice, here is a blessing:
1.116464 +**
1.116465 +**    May you do good and not evil.
1.116466 +**    May you find forgiveness for yourself and forgive others.
1.116467 +**    May you share freely, never taking more than you give.
1.116468 +**
1.116469 +******************************************************************************
1.116470 +**
1.116471 +*/
1.116472 +#ifndef _FTSINT_H
1.116473 +#define _FTSINT_H
1.116474 +
1.116475 +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
1.116476 +# define NDEBUG 1
1.116477 +#endif
1.116478 +
1.116479 +/*
1.116480 +** FTS4 is really an extension for FTS3.  It is enabled using the
1.116481 +** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
1.116482 +** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
1.116483 +*/
1.116484 +#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
1.116485 +# define SQLITE_ENABLE_FTS3
1.116486 +#endif
1.116487 +
1.116488 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.116489 +
1.116490 +/* If not building as part of the core, include sqlite3ext.h. */
1.116491 +#ifndef SQLITE_CORE
1.116492 +SQLITE_API extern const sqlite3_api_routines *sqlite3_api;
1.116493 +#endif
1.116494 +
1.116495 +/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
1.116496 +/************** Begin file fts3_tokenizer.h **********************************/
1.116497 +/*
1.116498 +** 2006 July 10
1.116499 +**
1.116500 +** The author disclaims copyright to this source code.
1.116501 +**
1.116502 +*************************************************************************
1.116503 +** Defines the interface to tokenizers used by fulltext-search.  There
1.116504 +** are three basic components:
1.116505 +**
1.116506 +** sqlite3_tokenizer_module is a singleton defining the tokenizer
1.116507 +** interface functions.  This is essentially the class structure for
1.116508 +** tokenizers.
1.116509 +**
1.116510 +** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
1.116511 +** including customization information defined at creation time.
1.116512 +**
1.116513 +** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
1.116514 +** tokens from a particular input.
1.116515 +*/
1.116516 +#ifndef _FTS3_TOKENIZER_H_
1.116517 +#define _FTS3_TOKENIZER_H_
1.116518 +
1.116519 +/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
1.116520 +** If tokenizers are to be allowed to call sqlite3_*() functions, then
1.116521 +** we will need a way to register the API consistently.
1.116522 +*/
1.116523 +
1.116524 +/*
1.116525 +** Structures used by the tokenizer interface. When a new tokenizer
1.116526 +** implementation is registered, the caller provides a pointer to
1.116527 +** an sqlite3_tokenizer_module containing pointers to the callback
1.116528 +** functions that make up an implementation.
1.116529 +**
1.116530 +** When an fts3 table is created, it passes any arguments passed to
1.116531 +** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
1.116532 +** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
1.116533 +** implementation. The xCreate() function in turn returns an 
1.116534 +** sqlite3_tokenizer structure representing the specific tokenizer to
1.116535 +** be used for the fts3 table (customized by the tokenizer clause arguments).
1.116536 +**
1.116537 +** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
1.116538 +** method is called. It returns an sqlite3_tokenizer_cursor object
1.116539 +** that may be used to tokenize a specific input buffer based on
1.116540 +** the tokenization rules supplied by a specific sqlite3_tokenizer
1.116541 +** object.
1.116542 +*/
1.116543 +typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
1.116544 +typedef struct sqlite3_tokenizer sqlite3_tokenizer;
1.116545 +typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
1.116546 +
1.116547 +struct sqlite3_tokenizer_module {
1.116548 +
1.116549 +  /*
1.116550 +  ** Structure version. Should always be set to 0 or 1.
1.116551 +  */
1.116552 +  int iVersion;
1.116553 +
1.116554 +  /*
1.116555 +  ** Create a new tokenizer. The values in the argv[] array are the
1.116556 +  ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
1.116557 +  ** TABLE statement that created the fts3 table. For example, if
1.116558 +  ** the following SQL is executed:
1.116559 +  **
1.116560 +  **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
1.116561 +  **
1.116562 +  ** then argc is set to 2, and the argv[] array contains pointers
1.116563 +  ** to the strings "arg1" and "arg2".
1.116564 +  **
1.116565 +  ** This method should return either SQLITE_OK (0), or an SQLite error 
1.116566 +  ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
1.116567 +  ** to point at the newly created tokenizer structure. The generic
1.116568 +  ** sqlite3_tokenizer.pModule variable should not be initialised by
1.116569 +  ** this callback. The caller will do so.
1.116570 +  */
1.116571 +  int (*xCreate)(
1.116572 +    int argc,                           /* Size of argv array */
1.116573 +    const char *const*argv,             /* Tokenizer argument strings */
1.116574 +    sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
1.116575 +  );
1.116576 +
1.116577 +  /*
1.116578 +  ** Destroy an existing tokenizer. The fts3 module calls this method
1.116579 +  ** exactly once for each successful call to xCreate().
1.116580 +  */
1.116581 +  int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
1.116582 +
1.116583 +  /*
1.116584 +  ** Create a tokenizer cursor to tokenize an input buffer. The caller
1.116585 +  ** is responsible for ensuring that the input buffer remains valid
1.116586 +  ** until the cursor is closed (using the xClose() method). 
1.116587 +  */
1.116588 +  int (*xOpen)(
1.116589 +    sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
1.116590 +    const char *pInput, int nBytes,      /* Input buffer */
1.116591 +    sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
1.116592 +  );
1.116593 +
1.116594 +  /*
1.116595 +  ** Destroy an existing tokenizer cursor. The fts3 module calls this 
1.116596 +  ** method exactly once for each successful call to xOpen().
1.116597 +  */
1.116598 +  int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
1.116599 +
1.116600 +  /*
1.116601 +  ** Retrieve the next token from the tokenizer cursor pCursor. This
1.116602 +  ** method should either return SQLITE_OK and set the values of the
1.116603 +  ** "OUT" variables identified below, or SQLITE_DONE to indicate that
1.116604 +  ** the end of the buffer has been reached, or an SQLite error code.
1.116605 +  **
1.116606 +  ** *ppToken should be set to point at a buffer containing the 
1.116607 +  ** normalized version of the token (i.e. after any case-folding and/or
1.116608 +  ** stemming has been performed). *pnBytes should be set to the length
1.116609 +  ** of this buffer in bytes. The input text that generated the token is
1.116610 +  ** identified by the byte offsets returned in *piStartOffset and
1.116611 +  ** *piEndOffset. *piStartOffset should be set to the index of the first
1.116612 +  ** byte of the token in the input buffer. *piEndOffset should be set
1.116613 +  ** to the index of the first byte just past the end of the token in
1.116614 +  ** the input buffer.
1.116615 +  **
1.116616 +  ** The buffer *ppToken is set to point at is managed by the tokenizer
1.116617 +  ** implementation. It is only required to be valid until the next call
1.116618 +  ** to xNext() or xClose(). 
1.116619 +  */
1.116620 +  /* TODO(shess) current implementation requires pInput to be
1.116621 +  ** nul-terminated.  This should either be fixed, or pInput/nBytes
1.116622 +  ** should be converted to zInput.
1.116623 +  */
1.116624 +  int (*xNext)(
1.116625 +    sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
1.116626 +    const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
1.116627 +    int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
1.116628 +    int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
1.116629 +    int *piPosition      /* OUT: Number of tokens returned before this one */
1.116630 +  );
1.116631 +
1.116632 +  /***********************************************************************
1.116633 +  ** Methods below this point are only available if iVersion>=1.
1.116634 +  */
1.116635 +
1.116636 +  /* 
1.116637 +  ** Configure the language id of a tokenizer cursor.
1.116638 +  */
1.116639 +  int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
1.116640 +};
1.116641 +
1.116642 +struct sqlite3_tokenizer {
1.116643 +  const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
1.116644 +  /* Tokenizer implementations will typically add additional fields */
1.116645 +};
1.116646 +
1.116647 +struct sqlite3_tokenizer_cursor {
1.116648 +  sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
1.116649 +  /* Tokenizer implementations will typically add additional fields */
1.116650 +};
1.116651 +
1.116652 +int fts3_global_term_cnt(int iTerm, int iCol);
1.116653 +int fts3_term_cnt(int iTerm, int iCol);
1.116654 +
1.116655 +
1.116656 +#endif /* _FTS3_TOKENIZER_H_ */
1.116657 +
1.116658 +/************** End of fts3_tokenizer.h **************************************/
1.116659 +/************** Continuing where we left off in fts3Int.h ********************/
1.116660 +/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
1.116661 +/************** Begin file fts3_hash.h ***************************************/
1.116662 +/*
1.116663 +** 2001 September 22
1.116664 +**
1.116665 +** The author disclaims copyright to this source code.  In place of
1.116666 +** a legal notice, here is a blessing:
1.116667 +**
1.116668 +**    May you do good and not evil.
1.116669 +**    May you find forgiveness for yourself and forgive others.
1.116670 +**    May you share freely, never taking more than you give.
1.116671 +**
1.116672 +*************************************************************************
1.116673 +** This is the header file for the generic hash-table implemenation
1.116674 +** used in SQLite.  We've modified it slightly to serve as a standalone
1.116675 +** hash table implementation for the full-text indexing module.
1.116676 +**
1.116677 +*/
1.116678 +#ifndef _FTS3_HASH_H_
1.116679 +#define _FTS3_HASH_H_
1.116680 +
1.116681 +/* Forward declarations of structures. */
1.116682 +typedef struct Fts3Hash Fts3Hash;
1.116683 +typedef struct Fts3HashElem Fts3HashElem;
1.116684 +
1.116685 +/* A complete hash table is an instance of the following structure.
1.116686 +** The internals of this structure are intended to be opaque -- client
1.116687 +** code should not attempt to access or modify the fields of this structure
1.116688 +** directly.  Change this structure only by using the routines below.
1.116689 +** However, many of the "procedures" and "functions" for modifying and
1.116690 +** accessing this structure are really macros, so we can't really make
1.116691 +** this structure opaque.
1.116692 +*/
1.116693 +struct Fts3Hash {
1.116694 +  char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
1.116695 +  char copyKey;           /* True if copy of key made on insert */
1.116696 +  int count;              /* Number of entries in this table */
1.116697 +  Fts3HashElem *first;    /* The first element of the array */
1.116698 +  int htsize;             /* Number of buckets in the hash table */
1.116699 +  struct _fts3ht {        /* the hash table */
1.116700 +    int count;               /* Number of entries with this hash */
1.116701 +    Fts3HashElem *chain;     /* Pointer to first entry with this hash */
1.116702 +  } *ht;
1.116703 +};
1.116704 +
1.116705 +/* Each element in the hash table is an instance of the following 
1.116706 +** structure.  All elements are stored on a single doubly-linked list.
1.116707 +**
1.116708 +** Again, this structure is intended to be opaque, but it can't really
1.116709 +** be opaque because it is used by macros.
1.116710 +*/
1.116711 +struct Fts3HashElem {
1.116712 +  Fts3HashElem *next, *prev; /* Next and previous elements in the table */
1.116713 +  void *data;                /* Data associated with this element */
1.116714 +  void *pKey; int nKey;      /* Key associated with this element */
1.116715 +};
1.116716 +
1.116717 +/*
1.116718 +** There are 2 different modes of operation for a hash table:
1.116719 +**
1.116720 +**   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
1.116721 +**                           (including the null-terminator, if any).  Case
1.116722 +**                           is respected in comparisons.
1.116723 +**
1.116724 +**   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long. 
1.116725 +**                           memcmp() is used to compare keys.
1.116726 +**
1.116727 +** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.  
1.116728 +*/
1.116729 +#define FTS3_HASH_STRING    1
1.116730 +#define FTS3_HASH_BINARY    2
1.116731 +
1.116732 +/*
1.116733 +** Access routines.  To delete, insert a NULL pointer.
1.116734 +*/
1.116735 +SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
1.116736 +SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
1.116737 +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
1.116738 +SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
1.116739 +SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
1.116740 +
1.116741 +/*
1.116742 +** Shorthand for the functions above
1.116743 +*/
1.116744 +#define fts3HashInit     sqlite3Fts3HashInit
1.116745 +#define fts3HashInsert   sqlite3Fts3HashInsert
1.116746 +#define fts3HashFind     sqlite3Fts3HashFind
1.116747 +#define fts3HashClear    sqlite3Fts3HashClear
1.116748 +#define fts3HashFindElem sqlite3Fts3HashFindElem
1.116749 +
1.116750 +/*
1.116751 +** Macros for looping over all elements of a hash table.  The idiom is
1.116752 +** like this:
1.116753 +**
1.116754 +**   Fts3Hash h;
1.116755 +**   Fts3HashElem *p;
1.116756 +**   ...
1.116757 +**   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
1.116758 +**     SomeStructure *pData = fts3HashData(p);
1.116759 +**     // do something with pData
1.116760 +**   }
1.116761 +*/
1.116762 +#define fts3HashFirst(H)  ((H)->first)
1.116763 +#define fts3HashNext(E)   ((E)->next)
1.116764 +#define fts3HashData(E)   ((E)->data)
1.116765 +#define fts3HashKey(E)    ((E)->pKey)
1.116766 +#define fts3HashKeysize(E) ((E)->nKey)
1.116767 +
1.116768 +/*
1.116769 +** Number of entries in a hash table
1.116770 +*/
1.116771 +#define fts3HashCount(H)  ((H)->count)
1.116772 +
1.116773 +#endif /* _FTS3_HASH_H_ */
1.116774 +
1.116775 +/************** End of fts3_hash.h *******************************************/
1.116776 +/************** Continuing where we left off in fts3Int.h ********************/
1.116777 +
1.116778 +/*
1.116779 +** This constant controls how often segments are merged. Once there are
1.116780 +** FTS3_MERGE_COUNT segments of level N, they are merged into a single
1.116781 +** segment of level N+1.
1.116782 +*/
1.116783 +#define FTS3_MERGE_COUNT 16
1.116784 +
1.116785 +/*
1.116786 +** This is the maximum amount of data (in bytes) to store in the 
1.116787 +** Fts3Table.pendingTerms hash table. Normally, the hash table is
1.116788 +** populated as documents are inserted/updated/deleted in a transaction
1.116789 +** and used to create a new segment when the transaction is committed.
1.116790 +** However if this limit is reached midway through a transaction, a new 
1.116791 +** segment is created and the hash table cleared immediately.
1.116792 +*/
1.116793 +#define FTS3_MAX_PENDING_DATA (1*1024*1024)
1.116794 +
1.116795 +/*
1.116796 +** Macro to return the number of elements in an array. SQLite has a
1.116797 +** similar macro called ArraySize(). Use a different name to avoid
1.116798 +** a collision when building an amalgamation with built-in FTS3.
1.116799 +*/
1.116800 +#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
1.116801 +
1.116802 +
1.116803 +#ifndef MIN
1.116804 +# define MIN(x,y) ((x)<(y)?(x):(y))
1.116805 +#endif
1.116806 +#ifndef MAX
1.116807 +# define MAX(x,y) ((x)>(y)?(x):(y))
1.116808 +#endif
1.116809 +
1.116810 +/*
1.116811 +** Maximum length of a varint encoded integer. The varint format is different
1.116812 +** from that used by SQLite, so the maximum length is 10, not 9.
1.116813 +*/
1.116814 +#define FTS3_VARINT_MAX 10
1.116815 +
1.116816 +/*
1.116817 +** FTS4 virtual tables may maintain multiple indexes - one index of all terms
1.116818 +** in the document set and zero or more prefix indexes. All indexes are stored
1.116819 +** as one or more b+-trees in the %_segments and %_segdir tables. 
1.116820 +**
1.116821 +** It is possible to determine which index a b+-tree belongs to based on the
1.116822 +** value stored in the "%_segdir.level" column. Given this value L, the index
1.116823 +** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
1.116824 +** level values between 0 and 1023 (inclusive) belong to index 0, all levels
1.116825 +** between 1024 and 2047 to index 1, and so on.
1.116826 +**
1.116827 +** It is considered impossible for an index to use more than 1024 levels. In 
1.116828 +** theory though this may happen, but only after at least 
1.116829 +** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
1.116830 +*/
1.116831 +#define FTS3_SEGDIR_MAXLEVEL      1024
1.116832 +#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
1.116833 +
1.116834 +/*
1.116835 +** The testcase() macro is only used by the amalgamation.  If undefined,
1.116836 +** make it a no-op.
1.116837 +*/
1.116838 +#ifndef testcase
1.116839 +# define testcase(X)
1.116840 +#endif
1.116841 +
1.116842 +/*
1.116843 +** Terminator values for position-lists and column-lists.
1.116844 +*/
1.116845 +#define POS_COLUMN  (1)     /* Column-list terminator */
1.116846 +#define POS_END     (0)     /* Position-list terminator */ 
1.116847 +
1.116848 +/*
1.116849 +** This section provides definitions to allow the
1.116850 +** FTS3 extension to be compiled outside of the 
1.116851 +** amalgamation.
1.116852 +*/
1.116853 +#ifndef SQLITE_AMALGAMATION
1.116854 +/*
1.116855 +** Macros indicating that conditional expressions are always true or
1.116856 +** false.
1.116857 +*/
1.116858 +#ifdef SQLITE_COVERAGE_TEST
1.116859 +# define ALWAYS(x) (1)
1.116860 +# define NEVER(X)  (0)
1.116861 +#else
1.116862 +# define ALWAYS(x) (x)
1.116863 +# define NEVER(x)  (x)
1.116864 +#endif
1.116865 +
1.116866 +/*
1.116867 +** Internal types used by SQLite.
1.116868 +*/
1.116869 +typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
1.116870 +typedef short int i16;            /* 2-byte (or larger) signed integer */
1.116871 +typedef unsigned int u32;         /* 4-byte unsigned integer */
1.116872 +typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
1.116873 +typedef sqlite3_int64 i64;        /* 8-byte signed integer */
1.116874 +
1.116875 +/*
1.116876 +** Macro used to suppress compiler warnings for unused parameters.
1.116877 +*/
1.116878 +#define UNUSED_PARAMETER(x) (void)(x)
1.116879 +
1.116880 +/*
1.116881 +** Activate assert() only if SQLITE_TEST is enabled.
1.116882 +*/
1.116883 +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
1.116884 +# define NDEBUG 1
1.116885 +#endif
1.116886 +
1.116887 +/*
1.116888 +** The TESTONLY macro is used to enclose variable declarations or
1.116889 +** other bits of code that are needed to support the arguments
1.116890 +** within testcase() and assert() macros.
1.116891 +*/
1.116892 +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
1.116893 +# define TESTONLY(X)  X
1.116894 +#else
1.116895 +# define TESTONLY(X)
1.116896 +#endif
1.116897 +
1.116898 +#endif /* SQLITE_AMALGAMATION */
1.116899 +
1.116900 +#ifdef SQLITE_DEBUG
1.116901 +SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
1.116902 +# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
1.116903 +#else
1.116904 +# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
1.116905 +#endif
1.116906 +
1.116907 +typedef struct Fts3Table Fts3Table;
1.116908 +typedef struct Fts3Cursor Fts3Cursor;
1.116909 +typedef struct Fts3Expr Fts3Expr;
1.116910 +typedef struct Fts3Phrase Fts3Phrase;
1.116911 +typedef struct Fts3PhraseToken Fts3PhraseToken;
1.116912 +
1.116913 +typedef struct Fts3Doclist Fts3Doclist;
1.116914 +typedef struct Fts3SegFilter Fts3SegFilter;
1.116915 +typedef struct Fts3DeferredToken Fts3DeferredToken;
1.116916 +typedef struct Fts3SegReader Fts3SegReader;
1.116917 +typedef struct Fts3MultiSegReader Fts3MultiSegReader;
1.116918 +
1.116919 +/*
1.116920 +** A connection to a fulltext index is an instance of the following
1.116921 +** structure. The xCreate and xConnect methods create an instance
1.116922 +** of this structure and xDestroy and xDisconnect free that instance.
1.116923 +** All other methods receive a pointer to the structure as one of their
1.116924 +** arguments.
1.116925 +*/
1.116926 +struct Fts3Table {
1.116927 +  sqlite3_vtab base;              /* Base class used by SQLite core */
1.116928 +  sqlite3 *db;                    /* The database connection */
1.116929 +  const char *zDb;                /* logical database name */
1.116930 +  const char *zName;              /* virtual table name */
1.116931 +  int nColumn;                    /* number of named columns in virtual table */
1.116932 +  char **azColumn;                /* column names.  malloced */
1.116933 +  sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
1.116934 +  char *zContentTbl;              /* content=xxx option, or NULL */
1.116935 +  char *zLanguageid;              /* languageid=xxx option, or NULL */
1.116936 +  u8 bAutoincrmerge;              /* True if automerge=1 */
1.116937 +  u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
1.116938 +
1.116939 +  /* Precompiled statements used by the implementation. Each of these 
1.116940 +  ** statements is run and reset within a single virtual table API call. 
1.116941 +  */
1.116942 +  sqlite3_stmt *aStmt[37];
1.116943 +
1.116944 +  char *zReadExprlist;
1.116945 +  char *zWriteExprlist;
1.116946 +
1.116947 +  int nNodeSize;                  /* Soft limit for node size */
1.116948 +  u8 bFts4;                       /* True for FTS4, false for FTS3 */
1.116949 +  u8 bHasStat;                    /* True if %_stat table exists */
1.116950 +  u8 bHasDocsize;                 /* True if %_docsize table exists */
1.116951 +  u8 bDescIdx;                    /* True if doclists are in reverse order */
1.116952 +  u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
1.116953 +  int nPgsz;                      /* Page size for host database */
1.116954 +  char *zSegmentsTbl;             /* Name of %_segments table */
1.116955 +  sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
1.116956 +
1.116957 +  /* 
1.116958 +  ** The following array of hash tables is used to buffer pending index 
1.116959 +  ** updates during transactions. All pending updates buffered at any one
1.116960 +  ** time must share a common language-id (see the FTS4 langid= feature).
1.116961 +  ** The current language id is stored in variable iPrevLangid.
1.116962 +  **
1.116963 +  ** A single FTS4 table may have multiple full-text indexes. For each index
1.116964 +  ** there is an entry in the aIndex[] array. Index 0 is an index of all the
1.116965 +  ** terms that appear in the document set. Each subsequent index in aIndex[]
1.116966 +  ** is an index of prefixes of a specific length.
1.116967 +  **
1.116968 +  ** Variable nPendingData contains an estimate the memory consumed by the 
1.116969 +  ** pending data structures, including hash table overhead, but not including
1.116970 +  ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
1.116971 +  ** tables are flushed to disk. Variable iPrevDocid is the docid of the most 
1.116972 +  ** recently inserted record.
1.116973 +  */
1.116974 +  int nIndex;                     /* Size of aIndex[] */
1.116975 +  struct Fts3Index {
1.116976 +    int nPrefix;                  /* Prefix length (0 for main terms index) */
1.116977 +    Fts3Hash hPending;            /* Pending terms table for this index */
1.116978 +  } *aIndex;
1.116979 +  int nMaxPendingData;            /* Max pending data before flush to disk */
1.116980 +  int nPendingData;               /* Current bytes of pending data */
1.116981 +  sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
1.116982 +  int iPrevLangid;                /* Langid of recently inserted document */
1.116983 +
1.116984 +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
1.116985 +  /* State variables used for validating that the transaction control
1.116986 +  ** methods of the virtual table are called at appropriate times.  These
1.116987 +  ** values do not contribute to FTS functionality; they are used for
1.116988 +  ** verifying the operation of the SQLite core.
1.116989 +  */
1.116990 +  int inTransaction;     /* True after xBegin but before xCommit/xRollback */
1.116991 +  int mxSavepoint;       /* Largest valid xSavepoint integer */
1.116992 +#endif
1.116993 +};
1.116994 +
1.116995 +/*
1.116996 +** When the core wants to read from the virtual table, it creates a
1.116997 +** virtual table cursor (an instance of the following structure) using
1.116998 +** the xOpen method. Cursors are destroyed using the xClose method.
1.116999 +*/
1.117000 +struct Fts3Cursor {
1.117001 +  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
1.117002 +  i16 eSearch;                    /* Search strategy (see below) */
1.117003 +  u8 isEof;                       /* True if at End Of Results */
1.117004 +  u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
1.117005 +  sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
1.117006 +  Fts3Expr *pExpr;                /* Parsed MATCH query string */
1.117007 +  int iLangid;                    /* Language being queried for */
1.117008 +  int nPhrase;                    /* Number of matchable phrases in query */
1.117009 +  Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
1.117010 +  sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
1.117011 +  char *pNextId;                  /* Pointer into the body of aDoclist */
1.117012 +  char *aDoclist;                 /* List of docids for full-text queries */
1.117013 +  int nDoclist;                   /* Size of buffer at aDoclist */
1.117014 +  u8 bDesc;                       /* True to sort in descending order */
1.117015 +  int eEvalmode;                  /* An FTS3_EVAL_XX constant */
1.117016 +  int nRowAvg;                    /* Average size of database rows, in pages */
1.117017 +  sqlite3_int64 nDoc;             /* Documents in table */
1.117018 +
1.117019 +  int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
1.117020 +  u32 *aMatchinfo;                /* Information about most recent match */
1.117021 +  int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
1.117022 +  char *zMatchinfo;               /* Matchinfo specification */
1.117023 +};
1.117024 +
1.117025 +#define FTS3_EVAL_FILTER    0
1.117026 +#define FTS3_EVAL_NEXT      1
1.117027 +#define FTS3_EVAL_MATCHINFO 2
1.117028 +
1.117029 +/*
1.117030 +** The Fts3Cursor.eSearch member is always set to one of the following.
1.117031 +** Actualy, Fts3Cursor.eSearch can be greater than or equal to
1.117032 +** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
1.117033 +** of the column to be searched.  For example, in
1.117034 +**
1.117035 +**     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
1.117036 +**     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
1.117037 +** 
1.117038 +** Because the LHS of the MATCH operator is 2nd column "b",
1.117039 +** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
1.117040 +** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1" 
1.117041 +** indicating that all columns should be searched,
1.117042 +** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
1.117043 +*/
1.117044 +#define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
1.117045 +#define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
1.117046 +#define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
1.117047 +
1.117048 +
1.117049 +struct Fts3Doclist {
1.117050 +  char *aAll;                    /* Array containing doclist (or NULL) */
1.117051 +  int nAll;                      /* Size of a[] in bytes */
1.117052 +  char *pNextDocid;              /* Pointer to next docid */
1.117053 +
1.117054 +  sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
1.117055 +  int bFreeList;                 /* True if pList should be sqlite3_free()d */
1.117056 +  char *pList;                   /* Pointer to position list following iDocid */
1.117057 +  int nList;                     /* Length of position list */
1.117058 +};
1.117059 +
1.117060 +/*
1.117061 +** A "phrase" is a sequence of one or more tokens that must match in
1.117062 +** sequence.  A single token is the base case and the most common case.
1.117063 +** For a sequence of tokens contained in double-quotes (i.e. "one two three")
1.117064 +** nToken will be the number of tokens in the string.
1.117065 +*/
1.117066 +struct Fts3PhraseToken {
1.117067 +  char *z;                        /* Text of the token */
1.117068 +  int n;                          /* Number of bytes in buffer z */
1.117069 +  int isPrefix;                   /* True if token ends with a "*" character */
1.117070 +  int bFirst;                     /* True if token must appear at position 0 */
1.117071 +
1.117072 +  /* Variables above this point are populated when the expression is
1.117073 +  ** parsed (by code in fts3_expr.c). Below this point the variables are
1.117074 +  ** used when evaluating the expression. */
1.117075 +  Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
1.117076 +  Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
1.117077 +};
1.117078 +
1.117079 +struct Fts3Phrase {
1.117080 +  /* Cache of doclist for this phrase. */
1.117081 +  Fts3Doclist doclist;
1.117082 +  int bIncr;                 /* True if doclist is loaded incrementally */
1.117083 +  int iDoclistToken;
1.117084 +
1.117085 +  /* Variables below this point are populated by fts3_expr.c when parsing 
1.117086 +  ** a MATCH expression. Everything above is part of the evaluation phase. 
1.117087 +  */
1.117088 +  int nToken;                /* Number of tokens in the phrase */
1.117089 +  int iColumn;               /* Index of column this phrase must match */
1.117090 +  Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
1.117091 +};
1.117092 +
1.117093 +/*
1.117094 +** A tree of these objects forms the RHS of a MATCH operator.
1.117095 +**
1.117096 +** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist 
1.117097 +** points to a malloced buffer, size nDoclist bytes, containing the results 
1.117098 +** of this phrase query in FTS3 doclist format. As usual, the initial 
1.117099 +** "Length" field found in doclists stored on disk is omitted from this 
1.117100 +** buffer.
1.117101 +**
1.117102 +** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
1.117103 +** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
1.117104 +** where nCol is the number of columns in the queried FTS table. The array
1.117105 +** is populated as follows:
1.117106 +**
1.117107 +**   aMI[iCol*3 + 0] = Undefined
1.117108 +**   aMI[iCol*3 + 1] = Number of occurrences
1.117109 +**   aMI[iCol*3 + 2] = Number of rows containing at least one instance
1.117110 +**
1.117111 +** The aMI array is allocated using sqlite3_malloc(). It should be freed 
1.117112 +** when the expression node is.
1.117113 +*/
1.117114 +struct Fts3Expr {
1.117115 +  int eType;                 /* One of the FTSQUERY_XXX values defined below */
1.117116 +  int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
1.117117 +  Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
1.117118 +  Fts3Expr *pLeft;           /* Left operand */
1.117119 +  Fts3Expr *pRight;          /* Right operand */
1.117120 +  Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
1.117121 +
1.117122 +  /* The following are used by the fts3_eval.c module. */
1.117123 +  sqlite3_int64 iDocid;      /* Current docid */
1.117124 +  u8 bEof;                   /* True this expression is at EOF already */
1.117125 +  u8 bStart;                 /* True if iDocid is valid */
1.117126 +  u8 bDeferred;              /* True if this expression is entirely deferred */
1.117127 +
1.117128 +  u32 *aMI;
1.117129 +};
1.117130 +
1.117131 +/*
1.117132 +** Candidate values for Fts3Query.eType. Note that the order of the first
1.117133 +** four values is in order of precedence when parsing expressions. For 
1.117134 +** example, the following:
1.117135 +**
1.117136 +**   "a OR b AND c NOT d NEAR e"
1.117137 +**
1.117138 +** is equivalent to:
1.117139 +**
1.117140 +**   "a OR (b AND (c NOT (d NEAR e)))"
1.117141 +*/
1.117142 +#define FTSQUERY_NEAR   1
1.117143 +#define FTSQUERY_NOT    2
1.117144 +#define FTSQUERY_AND    3
1.117145 +#define FTSQUERY_OR     4
1.117146 +#define FTSQUERY_PHRASE 5
1.117147 +
1.117148 +
1.117149 +/* fts3_write.c */
1.117150 +SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
1.117151 +SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
1.117152 +SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
1.117153 +SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
1.117154 +SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
1.117155 +  sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
1.117156 +SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
1.117157 +  Fts3Table*,int,const char*,int,int,Fts3SegReader**);
1.117158 +SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
1.117159 +SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
1.117160 +SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
1.117161 +SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
1.117162 +
1.117163 +SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
1.117164 +SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
1.117165 +
1.117166 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
1.117167 +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
1.117168 +SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
1.117169 +SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
1.117170 +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
1.117171 +SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
1.117172 +#else
1.117173 +# define sqlite3Fts3FreeDeferredTokens(x)
1.117174 +# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
1.117175 +# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
1.117176 +# define sqlite3Fts3FreeDeferredDoclists(x)
1.117177 +# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
1.117178 +#endif
1.117179 +
1.117180 +SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
1.117181 +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
1.117182 +
1.117183 +/* Special values interpreted by sqlite3SegReaderCursor() */
1.117184 +#define FTS3_SEGCURSOR_PENDING        -1
1.117185 +#define FTS3_SEGCURSOR_ALL            -2
1.117186 +
1.117187 +SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
1.117188 +SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
1.117189 +SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
1.117190 +
1.117191 +SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *, 
1.117192 +    int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
1.117193 +
1.117194 +/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
1.117195 +#define FTS3_SEGMENT_REQUIRE_POS   0x00000001
1.117196 +#define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
1.117197 +#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
1.117198 +#define FTS3_SEGMENT_PREFIX        0x00000008
1.117199 +#define FTS3_SEGMENT_SCAN          0x00000010
1.117200 +#define FTS3_SEGMENT_FIRST         0x00000020
1.117201 +
1.117202 +/* Type passed as 4th argument to SegmentReaderIterate() */
1.117203 +struct Fts3SegFilter {
1.117204 +  const char *zTerm;
1.117205 +  int nTerm;
1.117206 +  int iCol;
1.117207 +  int flags;
1.117208 +};
1.117209 +
1.117210 +struct Fts3MultiSegReader {
1.117211 +  /* Used internally by sqlite3Fts3SegReaderXXX() calls */
1.117212 +  Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
1.117213 +  int nSegment;                   /* Size of apSegment array */
1.117214 +  int nAdvance;                   /* How many seg-readers to advance */
1.117215 +  Fts3SegFilter *pFilter;         /* Pointer to filter object */
1.117216 +  char *aBuffer;                  /* Buffer to merge doclists in */
1.117217 +  int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
1.117218 +
1.117219 +  int iColFilter;                 /* If >=0, filter for this column */
1.117220 +  int bRestart;
1.117221 +
1.117222 +  /* Used by fts3.c only. */
1.117223 +  int nCost;                      /* Cost of running iterator */
1.117224 +  int bLookup;                    /* True if a lookup of a single entry. */
1.117225 +
1.117226 +  /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
1.117227 +  char *zTerm;                    /* Pointer to term buffer */
1.117228 +  int nTerm;                      /* Size of zTerm in bytes */
1.117229 +  char *aDoclist;                 /* Pointer to doclist buffer */
1.117230 +  int nDoclist;                   /* Size of aDoclist[] in bytes */
1.117231 +};
1.117232 +
1.117233 +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
1.117234 +
1.117235 +/* fts3.c */
1.117236 +SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
1.117237 +SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
1.117238 +SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
1.117239 +SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
1.117240 +SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
1.117241 +SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
1.117242 +SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
1.117243 +SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
1.117244 +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
1.117245 +
1.117246 +/* fts3_tokenizer.c */
1.117247 +SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
1.117248 +SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
1.117249 +SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
1.117250 +    sqlite3_tokenizer **, char **
1.117251 +);
1.117252 +SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
1.117253 +
1.117254 +/* fts3_snippet.c */
1.117255 +SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
1.117256 +SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
1.117257 +  const char *, const char *, int, int
1.117258 +);
1.117259 +SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
1.117260 +
1.117261 +/* fts3_expr.c */
1.117262 +SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
1.117263 +  char **, int, int, int, const char *, int, Fts3Expr **
1.117264 +);
1.117265 +SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
1.117266 +#ifdef SQLITE_TEST
1.117267 +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
1.117268 +SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
1.117269 +#endif
1.117270 +
1.117271 +SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
1.117272 +  sqlite3_tokenizer_cursor **
1.117273 +);
1.117274 +
1.117275 +/* fts3_aux.c */
1.117276 +SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
1.117277 +
1.117278 +SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
1.117279 +
1.117280 +SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
1.117281 +    Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
1.117282 +SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
1.117283 +    Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
1.117284 +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); 
1.117285 +SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
1.117286 +SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
1.117287 +
1.117288 +/* fts3_unicode2.c (functions generated by parsing unicode text files) */
1.117289 +#ifdef SQLITE_ENABLE_FTS4_UNICODE61
1.117290 +SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
1.117291 +SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
1.117292 +SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
1.117293 +#endif
1.117294 +
1.117295 +#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
1.117296 +#endif /* _FTSINT_H */
1.117297 +
1.117298 +/************** End of fts3Int.h *********************************************/
1.117299 +/************** Continuing where we left off in fts3.c ***********************/
1.117300 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.117301 +
1.117302 +#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
1.117303 +# define SQLITE_CORE 1
1.117304 +#endif
1.117305 +
1.117306 +/* #include <assert.h> */
1.117307 +/* #include <stdlib.h> */
1.117308 +/* #include <stddef.h> */
1.117309 +/* #include <stdio.h> */
1.117310 +/* #include <string.h> */
1.117311 +/* #include <stdarg.h> */
1.117312 +
1.117313 +#ifndef SQLITE_CORE 
1.117314 +  SQLITE_EXTENSION_INIT1
1.117315 +#endif
1.117316 +
1.117317 +static int fts3EvalNext(Fts3Cursor *pCsr);
1.117318 +static int fts3EvalStart(Fts3Cursor *pCsr);
1.117319 +static int fts3TermSegReaderCursor(
1.117320 +    Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
1.117321 +
1.117322 +/* 
1.117323 +** Write a 64-bit variable-length integer to memory starting at p[0].
1.117324 +** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
1.117325 +** The number of bytes written is returned.
1.117326 +*/
1.117327 +SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
1.117328 +  unsigned char *q = (unsigned char *) p;
1.117329 +  sqlite_uint64 vu = v;
1.117330 +  do{
1.117331 +    *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
1.117332 +    vu >>= 7;
1.117333 +  }while( vu!=0 );
1.117334 +  q[-1] &= 0x7f;  /* turn off high bit in final byte */
1.117335 +  assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
1.117336 +  return (int) (q - (unsigned char *)p);
1.117337 +}
1.117338 +
1.117339 +/* 
1.117340 +** Read a 64-bit variable-length integer from memory starting at p[0].
1.117341 +** Return the number of bytes read, or 0 on error.
1.117342 +** The value is stored in *v.
1.117343 +*/
1.117344 +SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
1.117345 +  const unsigned char *q = (const unsigned char *) p;
1.117346 +  sqlite_uint64 x = 0, y = 1;
1.117347 +  while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
1.117348 +    x += y * (*q++ & 0x7f);
1.117349 +    y <<= 7;
1.117350 +  }
1.117351 +  x += y * (*q++);
1.117352 +  *v = (sqlite_int64) x;
1.117353 +  return (int) (q - (unsigned char *)p);
1.117354 +}
1.117355 +
1.117356 +/*
1.117357 +** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
1.117358 +** 32-bit integer before it is returned.
1.117359 +*/
1.117360 +SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
1.117361 + sqlite_int64 i;
1.117362 + int ret = sqlite3Fts3GetVarint(p, &i);
1.117363 + *pi = (int) i;
1.117364 + return ret;
1.117365 +}
1.117366 +
1.117367 +/*
1.117368 +** Return the number of bytes required to encode v as a varint
1.117369 +*/
1.117370 +SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
1.117371 +  int i = 0;
1.117372 +  do{
1.117373 +    i++;
1.117374 +    v >>= 7;
1.117375 +  }while( v!=0 );
1.117376 +  return i;
1.117377 +}
1.117378 +
1.117379 +/*
1.117380 +** Convert an SQL-style quoted string into a normal string by removing
1.117381 +** the quote characters.  The conversion is done in-place.  If the
1.117382 +** input does not begin with a quote character, then this routine
1.117383 +** is a no-op.
1.117384 +**
1.117385 +** Examples:
1.117386 +**
1.117387 +**     "abc"   becomes   abc
1.117388 +**     'xyz'   becomes   xyz
1.117389 +**     [pqr]   becomes   pqr
1.117390 +**     `mno`   becomes   mno
1.117391 +**
1.117392 +*/
1.117393 +SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
1.117394 +  char quote;                     /* Quote character (if any ) */
1.117395 +
1.117396 +  quote = z[0];
1.117397 +  if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
1.117398 +    int iIn = 1;                  /* Index of next byte to read from input */
1.117399 +    int iOut = 0;                 /* Index of next byte to write to output */
1.117400 +
1.117401 +    /* If the first byte was a '[', then the close-quote character is a ']' */
1.117402 +    if( quote=='[' ) quote = ']';  
1.117403 +
1.117404 +    while( ALWAYS(z[iIn]) ){
1.117405 +      if( z[iIn]==quote ){
1.117406 +        if( z[iIn+1]!=quote ) break;
1.117407 +        z[iOut++] = quote;
1.117408 +        iIn += 2;
1.117409 +      }else{
1.117410 +        z[iOut++] = z[iIn++];
1.117411 +      }
1.117412 +    }
1.117413 +    z[iOut] = '\0';
1.117414 +  }
1.117415 +}
1.117416 +
1.117417 +/*
1.117418 +** Read a single varint from the doclist at *pp and advance *pp to point
1.117419 +** to the first byte past the end of the varint.  Add the value of the varint
1.117420 +** to *pVal.
1.117421 +*/
1.117422 +static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
1.117423 +  sqlite3_int64 iVal;
1.117424 +  *pp += sqlite3Fts3GetVarint(*pp, &iVal);
1.117425 +  *pVal += iVal;
1.117426 +}
1.117427 +
1.117428 +/*
1.117429 +** When this function is called, *pp points to the first byte following a
1.117430 +** varint that is part of a doclist (or position-list, or any other list
1.117431 +** of varints). This function moves *pp to point to the start of that varint,
1.117432 +** and sets *pVal by the varint value.
1.117433 +**
1.117434 +** Argument pStart points to the first byte of the doclist that the
1.117435 +** varint is part of.
1.117436 +*/
1.117437 +static void fts3GetReverseVarint(
1.117438 +  char **pp, 
1.117439 +  char *pStart, 
1.117440 +  sqlite3_int64 *pVal
1.117441 +){
1.117442 +  sqlite3_int64 iVal;
1.117443 +  char *p;
1.117444 +
1.117445 +  /* Pointer p now points at the first byte past the varint we are 
1.117446 +  ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
1.117447 +  ** clear on character p[-1]. */
1.117448 +  for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
1.117449 +  p++;
1.117450 +  *pp = p;
1.117451 +
1.117452 +  sqlite3Fts3GetVarint(p, &iVal);
1.117453 +  *pVal = iVal;
1.117454 +}
1.117455 +
1.117456 +/*
1.117457 +** The xDisconnect() virtual table method.
1.117458 +*/
1.117459 +static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
1.117460 +  Fts3Table *p = (Fts3Table *)pVtab;
1.117461 +  int i;
1.117462 +
1.117463 +  assert( p->nPendingData==0 );
1.117464 +  assert( p->pSegments==0 );
1.117465 +
1.117466 +  /* Free any prepared statements held */
1.117467 +  for(i=0; i<SizeofArray(p->aStmt); i++){
1.117468 +    sqlite3_finalize(p->aStmt[i]);
1.117469 +  }
1.117470 +  sqlite3_free(p->zSegmentsTbl);
1.117471 +  sqlite3_free(p->zReadExprlist);
1.117472 +  sqlite3_free(p->zWriteExprlist);
1.117473 +  sqlite3_free(p->zContentTbl);
1.117474 +  sqlite3_free(p->zLanguageid);
1.117475 +
1.117476 +  /* Invoke the tokenizer destructor to free the tokenizer. */
1.117477 +  p->pTokenizer->pModule->xDestroy(p->pTokenizer);
1.117478 +
1.117479 +  sqlite3_free(p);
1.117480 +  return SQLITE_OK;
1.117481 +}
1.117482 +
1.117483 +/*
1.117484 +** Construct one or more SQL statements from the format string given
1.117485 +** and then evaluate those statements. The success code is written
1.117486 +** into *pRc.
1.117487 +**
1.117488 +** If *pRc is initially non-zero then this routine is a no-op.
1.117489 +*/
1.117490 +static void fts3DbExec(
1.117491 +  int *pRc,              /* Success code */
1.117492 +  sqlite3 *db,           /* Database in which to run SQL */
1.117493 +  const char *zFormat,   /* Format string for SQL */
1.117494 +  ...                    /* Arguments to the format string */
1.117495 +){
1.117496 +  va_list ap;
1.117497 +  char *zSql;
1.117498 +  if( *pRc ) return;
1.117499 +  va_start(ap, zFormat);
1.117500 +  zSql = sqlite3_vmprintf(zFormat, ap);
1.117501 +  va_end(ap);
1.117502 +  if( zSql==0 ){
1.117503 +    *pRc = SQLITE_NOMEM;
1.117504 +  }else{
1.117505 +    *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
1.117506 +    sqlite3_free(zSql);
1.117507 +  }
1.117508 +}
1.117509 +
1.117510 +/*
1.117511 +** The xDestroy() virtual table method.
1.117512 +*/
1.117513 +static int fts3DestroyMethod(sqlite3_vtab *pVtab){
1.117514 +  Fts3Table *p = (Fts3Table *)pVtab;
1.117515 +  int rc = SQLITE_OK;              /* Return code */
1.117516 +  const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
1.117517 +  sqlite3 *db = p->db;             /* Database handle */
1.117518 +
1.117519 +  /* Drop the shadow tables */
1.117520 +  if( p->zContentTbl==0 ){
1.117521 +    fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
1.117522 +  }
1.117523 +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
1.117524 +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
1.117525 +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
1.117526 +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
1.117527 +
1.117528 +  /* If everything has worked, invoke fts3DisconnectMethod() to free the
1.117529 +  ** memory associated with the Fts3Table structure and return SQLITE_OK.
1.117530 +  ** Otherwise, return an SQLite error code.
1.117531 +  */
1.117532 +  return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
1.117533 +}
1.117534 +
1.117535 +
1.117536 +/*
1.117537 +** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
1.117538 +** passed as the first argument. This is done as part of the xConnect()
1.117539 +** and xCreate() methods.
1.117540 +**
1.117541 +** If *pRc is non-zero when this function is called, it is a no-op. 
1.117542 +** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
1.117543 +** before returning.
1.117544 +*/
1.117545 +static void fts3DeclareVtab(int *pRc, Fts3Table *p){
1.117546 +  if( *pRc==SQLITE_OK ){
1.117547 +    int i;                        /* Iterator variable */
1.117548 +    int rc;                       /* Return code */
1.117549 +    char *zSql;                   /* SQL statement passed to declare_vtab() */
1.117550 +    char *zCols;                  /* List of user defined columns */
1.117551 +    const char *zLanguageid;
1.117552 +
1.117553 +    zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
1.117554 +    sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
1.117555 +
1.117556 +    /* Create a list of user columns for the virtual table */
1.117557 +    zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
1.117558 +    for(i=1; zCols && i<p->nColumn; i++){
1.117559 +      zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
1.117560 +    }
1.117561 +
1.117562 +    /* Create the whole "CREATE TABLE" statement to pass to SQLite */
1.117563 +    zSql = sqlite3_mprintf(
1.117564 +        "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)", 
1.117565 +        zCols, p->zName, zLanguageid
1.117566 +    );
1.117567 +    if( !zCols || !zSql ){
1.117568 +      rc = SQLITE_NOMEM;
1.117569 +    }else{
1.117570 +      rc = sqlite3_declare_vtab(p->db, zSql);
1.117571 +    }
1.117572 +
1.117573 +    sqlite3_free(zSql);
1.117574 +    sqlite3_free(zCols);
1.117575 +    *pRc = rc;
1.117576 +  }
1.117577 +}
1.117578 +
1.117579 +/*
1.117580 +** Create the %_stat table if it does not already exist.
1.117581 +*/
1.117582 +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
1.117583 +  fts3DbExec(pRc, p->db, 
1.117584 +      "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
1.117585 +          "(id INTEGER PRIMARY KEY, value BLOB);",
1.117586 +      p->zDb, p->zName
1.117587 +  );
1.117588 +  if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
1.117589 +}
1.117590 +
1.117591 +/*
1.117592 +** Create the backing store tables (%_content, %_segments and %_segdir)
1.117593 +** required by the FTS3 table passed as the only argument. This is done
1.117594 +** as part of the vtab xCreate() method.
1.117595 +**
1.117596 +** If the p->bHasDocsize boolean is true (indicating that this is an
1.117597 +** FTS4 table, not an FTS3 table) then also create the %_docsize and
1.117598 +** %_stat tables required by FTS4.
1.117599 +*/
1.117600 +static int fts3CreateTables(Fts3Table *p){
1.117601 +  int rc = SQLITE_OK;             /* Return code */
1.117602 +  int i;                          /* Iterator variable */
1.117603 +  sqlite3 *db = p->db;            /* The database connection */
1.117604 +
1.117605 +  if( p->zContentTbl==0 ){
1.117606 +    const char *zLanguageid = p->zLanguageid;
1.117607 +    char *zContentCols;           /* Columns of %_content table */
1.117608 +
1.117609 +    /* Create a list of user columns for the content table */
1.117610 +    zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
1.117611 +    for(i=0; zContentCols && i<p->nColumn; i++){
1.117612 +      char *z = p->azColumn[i];
1.117613 +      zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
1.117614 +    }
1.117615 +    if( zLanguageid && zContentCols ){
1.117616 +      zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
1.117617 +    }
1.117618 +    if( zContentCols==0 ) rc = SQLITE_NOMEM;
1.117619 +  
1.117620 +    /* Create the content table */
1.117621 +    fts3DbExec(&rc, db, 
1.117622 +       "CREATE TABLE %Q.'%q_content'(%s)",
1.117623 +       p->zDb, p->zName, zContentCols
1.117624 +    );
1.117625 +    sqlite3_free(zContentCols);
1.117626 +  }
1.117627 +
1.117628 +  /* Create other tables */
1.117629 +  fts3DbExec(&rc, db, 
1.117630 +      "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
1.117631 +      p->zDb, p->zName
1.117632 +  );
1.117633 +  fts3DbExec(&rc, db, 
1.117634 +      "CREATE TABLE %Q.'%q_segdir'("
1.117635 +        "level INTEGER,"
1.117636 +        "idx INTEGER,"
1.117637 +        "start_block INTEGER,"
1.117638 +        "leaves_end_block INTEGER,"
1.117639 +        "end_block INTEGER,"
1.117640 +        "root BLOB,"
1.117641 +        "PRIMARY KEY(level, idx)"
1.117642 +      ");",
1.117643 +      p->zDb, p->zName
1.117644 +  );
1.117645 +  if( p->bHasDocsize ){
1.117646 +    fts3DbExec(&rc, db, 
1.117647 +        "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
1.117648 +        p->zDb, p->zName
1.117649 +    );
1.117650 +  }
1.117651 +  assert( p->bHasStat==p->bFts4 );
1.117652 +  if( p->bHasStat ){
1.117653 +    sqlite3Fts3CreateStatTable(&rc, p);
1.117654 +  }
1.117655 +  return rc;
1.117656 +}
1.117657 +
1.117658 +/*
1.117659 +** Store the current database page-size in bytes in p->nPgsz.
1.117660 +**
1.117661 +** If *pRc is non-zero when this function is called, it is a no-op. 
1.117662 +** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
1.117663 +** before returning.
1.117664 +*/
1.117665 +static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
1.117666 +  if( *pRc==SQLITE_OK ){
1.117667 +    int rc;                       /* Return code */
1.117668 +    char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
1.117669 +    sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
1.117670 +  
1.117671 +    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
1.117672 +    if( !zSql ){
1.117673 +      rc = SQLITE_NOMEM;
1.117674 +    }else{
1.117675 +      rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
1.117676 +      if( rc==SQLITE_OK ){
1.117677 +        sqlite3_step(pStmt);
1.117678 +        p->nPgsz = sqlite3_column_int(pStmt, 0);
1.117679 +        rc = sqlite3_finalize(pStmt);
1.117680 +      }else if( rc==SQLITE_AUTH ){
1.117681 +        p->nPgsz = 1024;
1.117682 +        rc = SQLITE_OK;
1.117683 +      }
1.117684 +    }
1.117685 +    assert( p->nPgsz>0 || rc!=SQLITE_OK );
1.117686 +    sqlite3_free(zSql);
1.117687 +    *pRc = rc;
1.117688 +  }
1.117689 +}
1.117690 +
1.117691 +/*
1.117692 +** "Special" FTS4 arguments are column specifications of the following form:
1.117693 +**
1.117694 +**   <key> = <value>
1.117695 +**
1.117696 +** There may not be whitespace surrounding the "=" character. The <value> 
1.117697 +** term may be quoted, but the <key> may not.
1.117698 +*/
1.117699 +static int fts3IsSpecialColumn(
1.117700 +  const char *z, 
1.117701 +  int *pnKey,
1.117702 +  char **pzValue
1.117703 +){
1.117704 +  char *zValue;
1.117705 +  const char *zCsr = z;
1.117706 +
1.117707 +  while( *zCsr!='=' ){
1.117708 +    if( *zCsr=='\0' ) return 0;
1.117709 +    zCsr++;
1.117710 +  }
1.117711 +
1.117712 +  *pnKey = (int)(zCsr-z);
1.117713 +  zValue = sqlite3_mprintf("%s", &zCsr[1]);
1.117714 +  if( zValue ){
1.117715 +    sqlite3Fts3Dequote(zValue);
1.117716 +  }
1.117717 +  *pzValue = zValue;
1.117718 +  return 1;
1.117719 +}
1.117720 +
1.117721 +/*
1.117722 +** Append the output of a printf() style formatting to an existing string.
1.117723 +*/
1.117724 +static void fts3Appendf(
1.117725 +  int *pRc,                       /* IN/OUT: Error code */
1.117726 +  char **pz,                      /* IN/OUT: Pointer to string buffer */
1.117727 +  const char *zFormat,            /* Printf format string to append */
1.117728 +  ...                             /* Arguments for printf format string */
1.117729 +){
1.117730 +  if( *pRc==SQLITE_OK ){
1.117731 +    va_list ap;
1.117732 +    char *z;
1.117733 +    va_start(ap, zFormat);
1.117734 +    z = sqlite3_vmprintf(zFormat, ap);
1.117735 +    va_end(ap);
1.117736 +    if( z && *pz ){
1.117737 +      char *z2 = sqlite3_mprintf("%s%s", *pz, z);
1.117738 +      sqlite3_free(z);
1.117739 +      z = z2;
1.117740 +    }
1.117741 +    if( z==0 ) *pRc = SQLITE_NOMEM;
1.117742 +    sqlite3_free(*pz);
1.117743 +    *pz = z;
1.117744 +  }
1.117745 +}
1.117746 +
1.117747 +/*
1.117748 +** Return a copy of input string zInput enclosed in double-quotes (") and
1.117749 +** with all double quote characters escaped. For example:
1.117750 +**
1.117751 +**     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
1.117752 +**
1.117753 +** The pointer returned points to memory obtained from sqlite3_malloc(). It
1.117754 +** is the callers responsibility to call sqlite3_free() to release this
1.117755 +** memory.
1.117756 +*/
1.117757 +static char *fts3QuoteId(char const *zInput){
1.117758 +  int nRet;
1.117759 +  char *zRet;
1.117760 +  nRet = 2 + (int)strlen(zInput)*2 + 1;
1.117761 +  zRet = sqlite3_malloc(nRet);
1.117762 +  if( zRet ){
1.117763 +    int i;
1.117764 +    char *z = zRet;
1.117765 +    *(z++) = '"';
1.117766 +    for(i=0; zInput[i]; i++){
1.117767 +      if( zInput[i]=='"' ) *(z++) = '"';
1.117768 +      *(z++) = zInput[i];
1.117769 +    }
1.117770 +    *(z++) = '"';
1.117771 +    *(z++) = '\0';
1.117772 +  }
1.117773 +  return zRet;
1.117774 +}
1.117775 +
1.117776 +/*
1.117777 +** Return a list of comma separated SQL expressions and a FROM clause that 
1.117778 +** could be used in a SELECT statement such as the following:
1.117779 +**
1.117780 +**     SELECT <list of expressions> FROM %_content AS x ...
1.117781 +**
1.117782 +** to return the docid, followed by each column of text data in order
1.117783 +** from left to write. If parameter zFunc is not NULL, then instead of
1.117784 +** being returned directly each column of text data is passed to an SQL
1.117785 +** function named zFunc first. For example, if zFunc is "unzip" and the
1.117786 +** table has the three user-defined columns "a", "b", and "c", the following
1.117787 +** string is returned:
1.117788 +**
1.117789 +**     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
1.117790 +**
1.117791 +** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
1.117792 +** is the responsibility of the caller to eventually free it.
1.117793 +**
1.117794 +** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
1.117795 +** a NULL pointer is returned). Otherwise, if an OOM error is encountered
1.117796 +** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
1.117797 +** no error occurs, *pRc is left unmodified.
1.117798 +*/
1.117799 +static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
1.117800 +  char *zRet = 0;
1.117801 +  char *zFree = 0;
1.117802 +  char *zFunction;
1.117803 +  int i;
1.117804 +
1.117805 +  if( p->zContentTbl==0 ){
1.117806 +    if( !zFunc ){
1.117807 +      zFunction = "";
1.117808 +    }else{
1.117809 +      zFree = zFunction = fts3QuoteId(zFunc);
1.117810 +    }
1.117811 +    fts3Appendf(pRc, &zRet, "docid");
1.117812 +    for(i=0; i<p->nColumn; i++){
1.117813 +      fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
1.117814 +    }
1.117815 +    if( p->zLanguageid ){
1.117816 +      fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
1.117817 +    }
1.117818 +    sqlite3_free(zFree);
1.117819 +  }else{
1.117820 +    fts3Appendf(pRc, &zRet, "rowid");
1.117821 +    for(i=0; i<p->nColumn; i++){
1.117822 +      fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
1.117823 +    }
1.117824 +    if( p->zLanguageid ){
1.117825 +      fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
1.117826 +    }
1.117827 +  }
1.117828 +  fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x", 
1.117829 +      p->zDb,
1.117830 +      (p->zContentTbl ? p->zContentTbl : p->zName),
1.117831 +      (p->zContentTbl ? "" : "_content")
1.117832 +  );
1.117833 +  return zRet;
1.117834 +}
1.117835 +
1.117836 +/*
1.117837 +** Return a list of N comma separated question marks, where N is the number
1.117838 +** of columns in the %_content table (one for the docid plus one for each
1.117839 +** user-defined text column).
1.117840 +**
1.117841 +** If argument zFunc is not NULL, then all but the first question mark
1.117842 +** is preceded by zFunc and an open bracket, and followed by a closed
1.117843 +** bracket. For example, if zFunc is "zip" and the FTS3 table has three 
1.117844 +** user-defined text columns, the following string is returned:
1.117845 +**
1.117846 +**     "?, zip(?), zip(?), zip(?)"
1.117847 +**
1.117848 +** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
1.117849 +** is the responsibility of the caller to eventually free it.
1.117850 +**
1.117851 +** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
1.117852 +** a NULL pointer is returned). Otherwise, if an OOM error is encountered
1.117853 +** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
1.117854 +** no error occurs, *pRc is left unmodified.
1.117855 +*/
1.117856 +static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
1.117857 +  char *zRet = 0;
1.117858 +  char *zFree = 0;
1.117859 +  char *zFunction;
1.117860 +  int i;
1.117861 +
1.117862 +  if( !zFunc ){
1.117863 +    zFunction = "";
1.117864 +  }else{
1.117865 +    zFree = zFunction = fts3QuoteId(zFunc);
1.117866 +  }
1.117867 +  fts3Appendf(pRc, &zRet, "?");
1.117868 +  for(i=0; i<p->nColumn; i++){
1.117869 +    fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
1.117870 +  }
1.117871 +  if( p->zLanguageid ){
1.117872 +    fts3Appendf(pRc, &zRet, ", ?");
1.117873 +  }
1.117874 +  sqlite3_free(zFree);
1.117875 +  return zRet;
1.117876 +}
1.117877 +
1.117878 +/*
1.117879 +** This function interprets the string at (*pp) as a non-negative integer
1.117880 +** value. It reads the integer and sets *pnOut to the value read, then 
1.117881 +** sets *pp to point to the byte immediately following the last byte of
1.117882 +** the integer value.
1.117883 +**
1.117884 +** Only decimal digits ('0'..'9') may be part of an integer value. 
1.117885 +**
1.117886 +** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
1.117887 +** the output value undefined. Otherwise SQLITE_OK is returned.
1.117888 +**
1.117889 +** This function is used when parsing the "prefix=" FTS4 parameter.
1.117890 +*/
1.117891 +static int fts3GobbleInt(const char **pp, int *pnOut){
1.117892 +  const char *p;                  /* Iterator pointer */
1.117893 +  int nInt = 0;                   /* Output value */
1.117894 +
1.117895 +  for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
1.117896 +    nInt = nInt * 10 + (p[0] - '0');
1.117897 +  }
1.117898 +  if( p==*pp ) return SQLITE_ERROR;
1.117899 +  *pnOut = nInt;
1.117900 +  *pp = p;
1.117901 +  return SQLITE_OK;
1.117902 +}
1.117903 +
1.117904 +/*
1.117905 +** This function is called to allocate an array of Fts3Index structures
1.117906 +** representing the indexes maintained by the current FTS table. FTS tables
1.117907 +** always maintain the main "terms" index, but may also maintain one or
1.117908 +** more "prefix" indexes, depending on the value of the "prefix=" parameter
1.117909 +** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
1.117910 +**
1.117911 +** Argument zParam is passed the value of the "prefix=" option if one was
1.117912 +** specified, or NULL otherwise.
1.117913 +**
1.117914 +** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
1.117915 +** the allocated array. *pnIndex is set to the number of elements in the
1.117916 +** array. If an error does occur, an SQLite error code is returned.
1.117917 +**
1.117918 +** Regardless of whether or not an error is returned, it is the responsibility
1.117919 +** of the caller to call sqlite3_free() on the output array to free it.
1.117920 +*/
1.117921 +static int fts3PrefixParameter(
1.117922 +  const char *zParam,             /* ABC in prefix=ABC parameter to parse */
1.117923 +  int *pnIndex,                   /* OUT: size of *apIndex[] array */
1.117924 +  struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
1.117925 +){
1.117926 +  struct Fts3Index *aIndex;       /* Allocated array */
1.117927 +  int nIndex = 1;                 /* Number of entries in array */
1.117928 +
1.117929 +  if( zParam && zParam[0] ){
1.117930 +    const char *p;
1.117931 +    nIndex++;
1.117932 +    for(p=zParam; *p; p++){
1.117933 +      if( *p==',' ) nIndex++;
1.117934 +    }
1.117935 +  }
1.117936 +
1.117937 +  aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
1.117938 +  *apIndex = aIndex;
1.117939 +  *pnIndex = nIndex;
1.117940 +  if( !aIndex ){
1.117941 +    return SQLITE_NOMEM;
1.117942 +  }
1.117943 +
1.117944 +  memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
1.117945 +  if( zParam ){
1.117946 +    const char *p = zParam;
1.117947 +    int i;
1.117948 +    for(i=1; i<nIndex; i++){
1.117949 +      int nPrefix;
1.117950 +      if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
1.117951 +      aIndex[i].nPrefix = nPrefix;
1.117952 +      p++;
1.117953 +    }
1.117954 +  }
1.117955 +
1.117956 +  return SQLITE_OK;
1.117957 +}
1.117958 +
1.117959 +/*
1.117960 +** This function is called when initializing an FTS4 table that uses the
1.117961 +** content=xxx option. It determines the number of and names of the columns
1.117962 +** of the new FTS4 table.
1.117963 +**
1.117964 +** The third argument passed to this function is the value passed to the
1.117965 +** config=xxx option (i.e. "xxx"). This function queries the database for
1.117966 +** a table of that name. If found, the output variables are populated
1.117967 +** as follows:
1.117968 +**
1.117969 +**   *pnCol:   Set to the number of columns table xxx has,
1.117970 +**
1.117971 +**   *pnStr:   Set to the total amount of space required to store a copy
1.117972 +**             of each columns name, including the nul-terminator.
1.117973 +**
1.117974 +**   *pazCol:  Set to point to an array of *pnCol strings. Each string is
1.117975 +**             the name of the corresponding column in table xxx. The array
1.117976 +**             and its contents are allocated using a single allocation. It
1.117977 +**             is the responsibility of the caller to free this allocation
1.117978 +**             by eventually passing the *pazCol value to sqlite3_free().
1.117979 +**
1.117980 +** If the table cannot be found, an error code is returned and the output
1.117981 +** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
1.117982 +** returned (and the output variables are undefined).
1.117983 +*/
1.117984 +static int fts3ContentColumns(
1.117985 +  sqlite3 *db,                    /* Database handle */
1.117986 +  const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
1.117987 +  const char *zTbl,               /* Name of content table */
1.117988 +  const char ***pazCol,           /* OUT: Malloc'd array of column names */
1.117989 +  int *pnCol,                     /* OUT: Size of array *pazCol */
1.117990 +  int *pnStr                      /* OUT: Bytes of string content */
1.117991 +){
1.117992 +  int rc = SQLITE_OK;             /* Return code */
1.117993 +  char *zSql;                     /* "SELECT *" statement on zTbl */  
1.117994 +  sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
1.117995 +
1.117996 +  zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
1.117997 +  if( !zSql ){
1.117998 +    rc = SQLITE_NOMEM;
1.117999 +  }else{
1.118000 +    rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
1.118001 +  }
1.118002 +  sqlite3_free(zSql);
1.118003 +
1.118004 +  if( rc==SQLITE_OK ){
1.118005 +    const char **azCol;           /* Output array */
1.118006 +    int nStr = 0;                 /* Size of all column names (incl. 0x00) */
1.118007 +    int nCol;                     /* Number of table columns */
1.118008 +    int i;                        /* Used to iterate through columns */
1.118009 +
1.118010 +    /* Loop through the returned columns. Set nStr to the number of bytes of
1.118011 +    ** space required to store a copy of each column name, including the
1.118012 +    ** nul-terminator byte.  */
1.118013 +    nCol = sqlite3_column_count(pStmt);
1.118014 +    for(i=0; i<nCol; i++){
1.118015 +      const char *zCol = sqlite3_column_name(pStmt, i);
1.118016 +      nStr += (int)strlen(zCol) + 1;
1.118017 +    }
1.118018 +
1.118019 +    /* Allocate and populate the array to return. */
1.118020 +    azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
1.118021 +    if( azCol==0 ){
1.118022 +      rc = SQLITE_NOMEM;
1.118023 +    }else{
1.118024 +      char *p = (char *)&azCol[nCol];
1.118025 +      for(i=0; i<nCol; i++){
1.118026 +        const char *zCol = sqlite3_column_name(pStmt, i);
1.118027 +        int n = (int)strlen(zCol)+1;
1.118028 +        memcpy(p, zCol, n);
1.118029 +        azCol[i] = p;
1.118030 +        p += n;
1.118031 +      }
1.118032 +    }
1.118033 +    sqlite3_finalize(pStmt);
1.118034 +
1.118035 +    /* Set the output variables. */
1.118036 +    *pnCol = nCol;
1.118037 +    *pnStr = nStr;
1.118038 +    *pazCol = azCol;
1.118039 +  }
1.118040 +
1.118041 +  return rc;
1.118042 +}
1.118043 +
1.118044 +/*
1.118045 +** This function is the implementation of both the xConnect and xCreate
1.118046 +** methods of the FTS3 virtual table.
1.118047 +**
1.118048 +** The argv[] array contains the following:
1.118049 +**
1.118050 +**   argv[0]   -> module name  ("fts3" or "fts4")
1.118051 +**   argv[1]   -> database name
1.118052 +**   argv[2]   -> table name
1.118053 +**   argv[...] -> "column name" and other module argument fields.
1.118054 +*/
1.118055 +static int fts3InitVtab(
1.118056 +  int isCreate,                   /* True for xCreate, false for xConnect */
1.118057 +  sqlite3 *db,                    /* The SQLite database connection */
1.118058 +  void *pAux,                     /* Hash table containing tokenizers */
1.118059 +  int argc,                       /* Number of elements in argv array */
1.118060 +  const char * const *argv,       /* xCreate/xConnect argument array */
1.118061 +  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
1.118062 +  char **pzErr                    /* Write any error message here */
1.118063 +){
1.118064 +  Fts3Hash *pHash = (Fts3Hash *)pAux;
1.118065 +  Fts3Table *p = 0;               /* Pointer to allocated vtab */
1.118066 +  int rc = SQLITE_OK;             /* Return code */
1.118067 +  int i;                          /* Iterator variable */
1.118068 +  int nByte;                      /* Size of allocation used for *p */
1.118069 +  int iCol;                       /* Column index */
1.118070 +  int nString = 0;                /* Bytes required to hold all column names */
1.118071 +  int nCol = 0;                   /* Number of columns in the FTS table */
1.118072 +  char *zCsr;                     /* Space for holding column names */
1.118073 +  int nDb;                        /* Bytes required to hold database name */
1.118074 +  int nName;                      /* Bytes required to hold table name */
1.118075 +  int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
1.118076 +  const char **aCol;              /* Array of column names */
1.118077 +  sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
1.118078 +
1.118079 +  int nIndex;                     /* Size of aIndex[] array */
1.118080 +  struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
1.118081 +
1.118082 +  /* The results of parsing supported FTS4 key=value options: */
1.118083 +  int bNoDocsize = 0;             /* True to omit %_docsize table */
1.118084 +  int bDescIdx = 0;               /* True to store descending indexes */
1.118085 +  char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
1.118086 +  char *zCompress = 0;            /* compress=? parameter (or NULL) */
1.118087 +  char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
1.118088 +  char *zContent = 0;             /* content=? parameter (or NULL) */
1.118089 +  char *zLanguageid = 0;          /* languageid=? parameter (or NULL) */
1.118090 +
1.118091 +  assert( strlen(argv[0])==4 );
1.118092 +  assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
1.118093 +       || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
1.118094 +  );
1.118095 +
1.118096 +  nDb = (int)strlen(argv[1]) + 1;
1.118097 +  nName = (int)strlen(argv[2]) + 1;
1.118098 +
1.118099 +  aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
1.118100 +  if( !aCol ) return SQLITE_NOMEM;
1.118101 +  memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
1.118102 +
1.118103 +  /* Loop through all of the arguments passed by the user to the FTS3/4
1.118104 +  ** module (i.e. all the column names and special arguments). This loop
1.118105 +  ** does the following:
1.118106 +  **
1.118107 +  **   + Figures out the number of columns the FTSX table will have, and
1.118108 +  **     the number of bytes of space that must be allocated to store copies
1.118109 +  **     of the column names.
1.118110 +  **
1.118111 +  **   + If there is a tokenizer specification included in the arguments,
1.118112 +  **     initializes the tokenizer pTokenizer.
1.118113 +  */
1.118114 +  for(i=3; rc==SQLITE_OK && i<argc; i++){
1.118115 +    char const *z = argv[i];
1.118116 +    int nKey;
1.118117 +    char *zVal;
1.118118 +
1.118119 +    /* Check if this is a tokenizer specification */
1.118120 +    if( !pTokenizer 
1.118121 +     && strlen(z)>8
1.118122 +     && 0==sqlite3_strnicmp(z, "tokenize", 8) 
1.118123 +     && 0==sqlite3Fts3IsIdChar(z[8])
1.118124 +    ){
1.118125 +      rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
1.118126 +    }
1.118127 +
1.118128 +    /* Check if it is an FTS4 special argument. */
1.118129 +    else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
1.118130 +      struct Fts4Option {
1.118131 +        const char *zOpt;
1.118132 +        int nOpt;
1.118133 +      } aFts4Opt[] = {
1.118134 +        { "matchinfo",   9 },     /* 0 -> MATCHINFO */
1.118135 +        { "prefix",      6 },     /* 1 -> PREFIX */
1.118136 +        { "compress",    8 },     /* 2 -> COMPRESS */
1.118137 +        { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
1.118138 +        { "order",       5 },     /* 4 -> ORDER */
1.118139 +        { "content",     7 },     /* 5 -> CONTENT */
1.118140 +        { "languageid", 10 }      /* 6 -> LANGUAGEID */
1.118141 +      };
1.118142 +
1.118143 +      int iOpt;
1.118144 +      if( !zVal ){
1.118145 +        rc = SQLITE_NOMEM;
1.118146 +      }else{
1.118147 +        for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
1.118148 +          struct Fts4Option *pOp = &aFts4Opt[iOpt];
1.118149 +          if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
1.118150 +            break;
1.118151 +          }
1.118152 +        }
1.118153 +        if( iOpt==SizeofArray(aFts4Opt) ){
1.118154 +          *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
1.118155 +          rc = SQLITE_ERROR;
1.118156 +        }else{
1.118157 +          switch( iOpt ){
1.118158 +            case 0:               /* MATCHINFO */
1.118159 +              if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
1.118160 +                *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
1.118161 +                rc = SQLITE_ERROR;
1.118162 +              }
1.118163 +              bNoDocsize = 1;
1.118164 +              break;
1.118165 +
1.118166 +            case 1:               /* PREFIX */
1.118167 +              sqlite3_free(zPrefix);
1.118168 +              zPrefix = zVal;
1.118169 +              zVal = 0;
1.118170 +              break;
1.118171 +
1.118172 +            case 2:               /* COMPRESS */
1.118173 +              sqlite3_free(zCompress);
1.118174 +              zCompress = zVal;
1.118175 +              zVal = 0;
1.118176 +              break;
1.118177 +
1.118178 +            case 3:               /* UNCOMPRESS */
1.118179 +              sqlite3_free(zUncompress);
1.118180 +              zUncompress = zVal;
1.118181 +              zVal = 0;
1.118182 +              break;
1.118183 +
1.118184 +            case 4:               /* ORDER */
1.118185 +              if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
1.118186 +               && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
1.118187 +              ){
1.118188 +                *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
1.118189 +                rc = SQLITE_ERROR;
1.118190 +              }
1.118191 +              bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
1.118192 +              break;
1.118193 +
1.118194 +            case 5:              /* CONTENT */
1.118195 +              sqlite3_free(zContent);
1.118196 +              zContent = zVal;
1.118197 +              zVal = 0;
1.118198 +              break;
1.118199 +
1.118200 +            case 6:              /* LANGUAGEID */
1.118201 +              assert( iOpt==6 );
1.118202 +              sqlite3_free(zLanguageid);
1.118203 +              zLanguageid = zVal;
1.118204 +              zVal = 0;
1.118205 +              break;
1.118206 +          }
1.118207 +        }
1.118208 +        sqlite3_free(zVal);
1.118209 +      }
1.118210 +    }
1.118211 +
1.118212 +    /* Otherwise, the argument is a column name. */
1.118213 +    else {
1.118214 +      nString += (int)(strlen(z) + 1);
1.118215 +      aCol[nCol++] = z;
1.118216 +    }
1.118217 +  }
1.118218 +
1.118219 +  /* If a content=xxx option was specified, the following:
1.118220 +  **
1.118221 +  **   1. Ignore any compress= and uncompress= options.
1.118222 +  **
1.118223 +  **   2. If no column names were specified as part of the CREATE VIRTUAL
1.118224 +  **      TABLE statement, use all columns from the content table.
1.118225 +  */
1.118226 +  if( rc==SQLITE_OK && zContent ){
1.118227 +    sqlite3_free(zCompress); 
1.118228 +    sqlite3_free(zUncompress); 
1.118229 +    zCompress = 0;
1.118230 +    zUncompress = 0;
1.118231 +    if( nCol==0 ){
1.118232 +      sqlite3_free((void*)aCol); 
1.118233 +      aCol = 0;
1.118234 +      rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
1.118235 +
1.118236 +      /* If a languageid= option was specified, remove the language id
1.118237 +      ** column from the aCol[] array. */ 
1.118238 +      if( rc==SQLITE_OK && zLanguageid ){
1.118239 +        int j;
1.118240 +        for(j=0; j<nCol; j++){
1.118241 +          if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
1.118242 +            int k;
1.118243 +            for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
1.118244 +            nCol--;
1.118245 +            break;
1.118246 +          }
1.118247 +        }
1.118248 +      }
1.118249 +    }
1.118250 +  }
1.118251 +  if( rc!=SQLITE_OK ) goto fts3_init_out;
1.118252 +
1.118253 +  if( nCol==0 ){
1.118254 +    assert( nString==0 );
1.118255 +    aCol[0] = "content";
1.118256 +    nString = 8;
1.118257 +    nCol = 1;
1.118258 +  }
1.118259 +
1.118260 +  if( pTokenizer==0 ){
1.118261 +    rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
1.118262 +    if( rc!=SQLITE_OK ) goto fts3_init_out;
1.118263 +  }
1.118264 +  assert( pTokenizer );
1.118265 +
1.118266 +  rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
1.118267 +  if( rc==SQLITE_ERROR ){
1.118268 +    assert( zPrefix );
1.118269 +    *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
1.118270 +  }
1.118271 +  if( rc!=SQLITE_OK ) goto fts3_init_out;
1.118272 +
1.118273 +  /* Allocate and populate the Fts3Table structure. */
1.118274 +  nByte = sizeof(Fts3Table) +                  /* Fts3Table */
1.118275 +          nCol * sizeof(char *) +              /* azColumn */
1.118276 +          nIndex * sizeof(struct Fts3Index) +  /* aIndex */
1.118277 +          nName +                              /* zName */
1.118278 +          nDb +                                /* zDb */
1.118279 +          nString;                             /* Space for azColumn strings */
1.118280 +  p = (Fts3Table*)sqlite3_malloc(nByte);
1.118281 +  if( p==0 ){
1.118282 +    rc = SQLITE_NOMEM;
1.118283 +    goto fts3_init_out;
1.118284 +  }
1.118285 +  memset(p, 0, nByte);
1.118286 +  p->db = db;
1.118287 +  p->nColumn = nCol;
1.118288 +  p->nPendingData = 0;
1.118289 +  p->azColumn = (char **)&p[1];
1.118290 +  p->pTokenizer = pTokenizer;
1.118291 +  p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
1.118292 +  p->bHasDocsize = (isFts4 && bNoDocsize==0);
1.118293 +  p->bHasStat = isFts4;
1.118294 +  p->bFts4 = isFts4;
1.118295 +  p->bDescIdx = bDescIdx;
1.118296 +  p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
1.118297 +  p->zContentTbl = zContent;
1.118298 +  p->zLanguageid = zLanguageid;
1.118299 +  zContent = 0;
1.118300 +  zLanguageid = 0;
1.118301 +  TESTONLY( p->inTransaction = -1 );
1.118302 +  TESTONLY( p->mxSavepoint = -1 );
1.118303 +
1.118304 +  p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
1.118305 +  memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
1.118306 +  p->nIndex = nIndex;
1.118307 +  for(i=0; i<nIndex; i++){
1.118308 +    fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
1.118309 +  }
1.118310 +
1.118311 +  /* Fill in the zName and zDb fields of the vtab structure. */
1.118312 +  zCsr = (char *)&p->aIndex[nIndex];
1.118313 +  p->zName = zCsr;
1.118314 +  memcpy(zCsr, argv[2], nName);
1.118315 +  zCsr += nName;
1.118316 +  p->zDb = zCsr;
1.118317 +  memcpy(zCsr, argv[1], nDb);
1.118318 +  zCsr += nDb;
1.118319 +
1.118320 +  /* Fill in the azColumn array */
1.118321 +  for(iCol=0; iCol<nCol; iCol++){
1.118322 +    char *z; 
1.118323 +    int n = 0;
1.118324 +    z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
1.118325 +    memcpy(zCsr, z, n);
1.118326 +    zCsr[n] = '\0';
1.118327 +    sqlite3Fts3Dequote(zCsr);
1.118328 +    p->azColumn[iCol] = zCsr;
1.118329 +    zCsr += n+1;
1.118330 +    assert( zCsr <= &((char *)p)[nByte] );
1.118331 +  }
1.118332 +
1.118333 +  if( (zCompress==0)!=(zUncompress==0) ){
1.118334 +    char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
1.118335 +    rc = SQLITE_ERROR;
1.118336 +    *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
1.118337 +  }
1.118338 +  p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
1.118339 +  p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
1.118340 +  if( rc!=SQLITE_OK ) goto fts3_init_out;
1.118341 +
1.118342 +  /* If this is an xCreate call, create the underlying tables in the 
1.118343 +  ** database. TODO: For xConnect(), it could verify that said tables exist.
1.118344 +  */
1.118345 +  if( isCreate ){
1.118346 +    rc = fts3CreateTables(p);
1.118347 +  }
1.118348 +
1.118349 +  /* Check to see if a legacy fts3 table has been "upgraded" by the
1.118350 +  ** addition of a %_stat table so that it can use incremental merge.
1.118351 +  */
1.118352 +  if( !isFts4 && !isCreate ){
1.118353 +    int rc2 = SQLITE_OK;
1.118354 +    fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
1.118355 +               p->zDb, p->zName);
1.118356 +    if( rc2==SQLITE_OK ) p->bHasStat = 1;
1.118357 +  }
1.118358 +
1.118359 +  /* Figure out the page-size for the database. This is required in order to
1.118360 +  ** estimate the cost of loading large doclists from the database.  */
1.118361 +  fts3DatabasePageSize(&rc, p);
1.118362 +  p->nNodeSize = p->nPgsz-35;
1.118363 +
1.118364 +  /* Declare the table schema to SQLite. */
1.118365 +  fts3DeclareVtab(&rc, p);
1.118366 +
1.118367 +fts3_init_out:
1.118368 +  sqlite3_free(zPrefix);
1.118369 +  sqlite3_free(aIndex);
1.118370 +  sqlite3_free(zCompress);
1.118371 +  sqlite3_free(zUncompress);
1.118372 +  sqlite3_free(zContent);
1.118373 +  sqlite3_free(zLanguageid);
1.118374 +  sqlite3_free((void *)aCol);
1.118375 +  if( rc!=SQLITE_OK ){
1.118376 +    if( p ){
1.118377 +      fts3DisconnectMethod((sqlite3_vtab *)p);
1.118378 +    }else if( pTokenizer ){
1.118379 +      pTokenizer->pModule->xDestroy(pTokenizer);
1.118380 +    }
1.118381 +  }else{
1.118382 +    assert( p->pSegments==0 );
1.118383 +    *ppVTab = &p->base;
1.118384 +  }
1.118385 +  return rc;
1.118386 +}
1.118387 +
1.118388 +/*
1.118389 +** The xConnect() and xCreate() methods for the virtual table. All the
1.118390 +** work is done in function fts3InitVtab().
1.118391 +*/
1.118392 +static int fts3ConnectMethod(
1.118393 +  sqlite3 *db,                    /* Database connection */
1.118394 +  void *pAux,                     /* Pointer to tokenizer hash table */
1.118395 +  int argc,                       /* Number of elements in argv array */
1.118396 +  const char * const *argv,       /* xCreate/xConnect argument array */
1.118397 +  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
1.118398 +  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
1.118399 +){
1.118400 +  return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
1.118401 +}
1.118402 +static int fts3CreateMethod(
1.118403 +  sqlite3 *db,                    /* Database connection */
1.118404 +  void *pAux,                     /* Pointer to tokenizer hash table */
1.118405 +  int argc,                       /* Number of elements in argv array */
1.118406 +  const char * const *argv,       /* xCreate/xConnect argument array */
1.118407 +  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
1.118408 +  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
1.118409 +){
1.118410 +  return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
1.118411 +}
1.118412 +
1.118413 +/* 
1.118414 +** Implementation of the xBestIndex method for FTS3 tables. There
1.118415 +** are three possible strategies, in order of preference:
1.118416 +**
1.118417 +**   1. Direct lookup by rowid or docid. 
1.118418 +**   2. Full-text search using a MATCH operator on a non-docid column.
1.118419 +**   3. Linear scan of %_content table.
1.118420 +*/
1.118421 +static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
1.118422 +  Fts3Table *p = (Fts3Table *)pVTab;
1.118423 +  int i;                          /* Iterator variable */
1.118424 +  int iCons = -1;                 /* Index of constraint to use */
1.118425 +  int iLangidCons = -1;           /* Index of langid=x constraint, if present */
1.118426 +
1.118427 +  /* By default use a full table scan. This is an expensive option,
1.118428 +  ** so search through the constraints to see if a more efficient 
1.118429 +  ** strategy is possible.
1.118430 +  */
1.118431 +  pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
1.118432 +  pInfo->estimatedCost = 500000;
1.118433 +  for(i=0; i<pInfo->nConstraint; i++){
1.118434 +    struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
1.118435 +    if( pCons->usable==0 ) continue;
1.118436 +
1.118437 +    /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
1.118438 +    if( iCons<0 
1.118439 +     && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
1.118440 +     && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
1.118441 +    ){
1.118442 +      pInfo->idxNum = FTS3_DOCID_SEARCH;
1.118443 +      pInfo->estimatedCost = 1.0;
1.118444 +      iCons = i;
1.118445 +    }
1.118446 +
1.118447 +    /* A MATCH constraint. Use a full-text search.
1.118448 +    **
1.118449 +    ** If there is more than one MATCH constraint available, use the first
1.118450 +    ** one encountered. If there is both a MATCH constraint and a direct
1.118451 +    ** rowid/docid lookup, prefer the MATCH strategy. This is done even 
1.118452 +    ** though the rowid/docid lookup is faster than a MATCH query, selecting
1.118453 +    ** it would lead to an "unable to use function MATCH in the requested 
1.118454 +    ** context" error.
1.118455 +    */
1.118456 +    if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH 
1.118457 +     && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
1.118458 +    ){
1.118459 +      pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
1.118460 +      pInfo->estimatedCost = 2.0;
1.118461 +      iCons = i;
1.118462 +    }
1.118463 +
1.118464 +    /* Equality constraint on the langid column */
1.118465 +    if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
1.118466 +     && pCons->iColumn==p->nColumn + 2
1.118467 +    ){
1.118468 +      iLangidCons = i;
1.118469 +    }
1.118470 +  }
1.118471 +
1.118472 +  if( iCons>=0 ){
1.118473 +    pInfo->aConstraintUsage[iCons].argvIndex = 1;
1.118474 +    pInfo->aConstraintUsage[iCons].omit = 1;
1.118475 +  } 
1.118476 +  if( iLangidCons>=0 ){
1.118477 +    pInfo->aConstraintUsage[iLangidCons].argvIndex = 2;
1.118478 +  } 
1.118479 +
1.118480 +  /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
1.118481 +  ** docid) order. Both ascending and descending are possible. 
1.118482 +  */
1.118483 +  if( pInfo->nOrderBy==1 ){
1.118484 +    struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
1.118485 +    if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
1.118486 +      if( pOrder->desc ){
1.118487 +        pInfo->idxStr = "DESC";
1.118488 +      }else{
1.118489 +        pInfo->idxStr = "ASC";
1.118490 +      }
1.118491 +      pInfo->orderByConsumed = 1;
1.118492 +    }
1.118493 +  }
1.118494 +
1.118495 +  assert( p->pSegments==0 );
1.118496 +  return SQLITE_OK;
1.118497 +}
1.118498 +
1.118499 +/*
1.118500 +** Implementation of xOpen method.
1.118501 +*/
1.118502 +static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
1.118503 +  sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
1.118504 +
1.118505 +  UNUSED_PARAMETER(pVTab);
1.118506 +
1.118507 +  /* Allocate a buffer large enough for an Fts3Cursor structure. If the
1.118508 +  ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, 
1.118509 +  ** if the allocation fails, return SQLITE_NOMEM.
1.118510 +  */
1.118511 +  *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
1.118512 +  if( !pCsr ){
1.118513 +    return SQLITE_NOMEM;
1.118514 +  }
1.118515 +  memset(pCsr, 0, sizeof(Fts3Cursor));
1.118516 +  return SQLITE_OK;
1.118517 +}
1.118518 +
1.118519 +/*
1.118520 +** Close the cursor.  For additional information see the documentation
1.118521 +** on the xClose method of the virtual table interface.
1.118522 +*/
1.118523 +static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
1.118524 +  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
1.118525 +  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
1.118526 +  sqlite3_finalize(pCsr->pStmt);
1.118527 +  sqlite3Fts3ExprFree(pCsr->pExpr);
1.118528 +  sqlite3Fts3FreeDeferredTokens(pCsr);
1.118529 +  sqlite3_free(pCsr->aDoclist);
1.118530 +  sqlite3_free(pCsr->aMatchinfo);
1.118531 +  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
1.118532 +  sqlite3_free(pCsr);
1.118533 +  return SQLITE_OK;
1.118534 +}
1.118535 +
1.118536 +/*
1.118537 +** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
1.118538 +** compose and prepare an SQL statement of the form:
1.118539 +**
1.118540 +**    "SELECT <columns> FROM %_content WHERE rowid = ?"
1.118541 +**
1.118542 +** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
1.118543 +** it. If an error occurs, return an SQLite error code.
1.118544 +**
1.118545 +** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
1.118546 +*/
1.118547 +static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
1.118548 +  int rc = SQLITE_OK;
1.118549 +  if( pCsr->pStmt==0 ){
1.118550 +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
1.118551 +    char *zSql;
1.118552 +    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
1.118553 +    if( !zSql ) return SQLITE_NOMEM;
1.118554 +    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
1.118555 +    sqlite3_free(zSql);
1.118556 +  }
1.118557 +  *ppStmt = pCsr->pStmt;
1.118558 +  return rc;
1.118559 +}
1.118560 +
1.118561 +/*
1.118562 +** Position the pCsr->pStmt statement so that it is on the row
1.118563 +** of the %_content table that contains the last match.  Return
1.118564 +** SQLITE_OK on success.  
1.118565 +*/
1.118566 +static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
1.118567 +  int rc = SQLITE_OK;
1.118568 +  if( pCsr->isRequireSeek ){
1.118569 +    sqlite3_stmt *pStmt = 0;
1.118570 +
1.118571 +    rc = fts3CursorSeekStmt(pCsr, &pStmt);
1.118572 +    if( rc==SQLITE_OK ){
1.118573 +      sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
1.118574 +      pCsr->isRequireSeek = 0;
1.118575 +      if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
1.118576 +        return SQLITE_OK;
1.118577 +      }else{
1.118578 +        rc = sqlite3_reset(pCsr->pStmt);
1.118579 +        if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
1.118580 +          /* If no row was found and no error has occured, then the %_content
1.118581 +          ** table is missing a row that is present in the full-text index.
1.118582 +          ** The data structures are corrupt.  */
1.118583 +          rc = FTS_CORRUPT_VTAB;
1.118584 +          pCsr->isEof = 1;
1.118585 +        }
1.118586 +      }
1.118587 +    }
1.118588 +  }
1.118589 +
1.118590 +  if( rc!=SQLITE_OK && pContext ){
1.118591 +    sqlite3_result_error_code(pContext, rc);
1.118592 +  }
1.118593 +  return rc;
1.118594 +}
1.118595 +
1.118596 +/*
1.118597 +** This function is used to process a single interior node when searching
1.118598 +** a b-tree for a term or term prefix. The node data is passed to this 
1.118599 +** function via the zNode/nNode parameters. The term to search for is
1.118600 +** passed in zTerm/nTerm.
1.118601 +**
1.118602 +** If piFirst is not NULL, then this function sets *piFirst to the blockid
1.118603 +** of the child node that heads the sub-tree that may contain the term.
1.118604 +**
1.118605 +** If piLast is not NULL, then *piLast is set to the right-most child node
1.118606 +** that heads a sub-tree that may contain a term for which zTerm/nTerm is
1.118607 +** a prefix.
1.118608 +**
1.118609 +** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
1.118610 +*/
1.118611 +static int fts3ScanInteriorNode(
1.118612 +  const char *zTerm,              /* Term to select leaves for */
1.118613 +  int nTerm,                      /* Size of term zTerm in bytes */
1.118614 +  const char *zNode,              /* Buffer containing segment interior node */
1.118615 +  int nNode,                      /* Size of buffer at zNode */
1.118616 +  sqlite3_int64 *piFirst,         /* OUT: Selected child node */
1.118617 +  sqlite3_int64 *piLast           /* OUT: Selected child node */
1.118618 +){
1.118619 +  int rc = SQLITE_OK;             /* Return code */
1.118620 +  const char *zCsr = zNode;       /* Cursor to iterate through node */
1.118621 +  const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
1.118622 +  char *zBuffer = 0;              /* Buffer to load terms into */
1.118623 +  int nAlloc = 0;                 /* Size of allocated buffer */
1.118624 +  int isFirstTerm = 1;            /* True when processing first term on page */
1.118625 +  sqlite3_int64 iChild;           /* Block id of child node to descend to */
1.118626 +
1.118627 +  /* Skip over the 'height' varint that occurs at the start of every 
1.118628 +  ** interior node. Then load the blockid of the left-child of the b-tree
1.118629 +  ** node into variable iChild.  
1.118630 +  **
1.118631 +  ** Even if the data structure on disk is corrupted, this (reading two
1.118632 +  ** varints from the buffer) does not risk an overread. If zNode is a
1.118633 +  ** root node, then the buffer comes from a SELECT statement. SQLite does
1.118634 +  ** not make this guarantee explicitly, but in practice there are always
1.118635 +  ** either more than 20 bytes of allocated space following the nNode bytes of
1.118636 +  ** contents, or two zero bytes. Or, if the node is read from the %_segments
1.118637 +  ** table, then there are always 20 bytes of zeroed padding following the
1.118638 +  ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
1.118639 +  */
1.118640 +  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
1.118641 +  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
1.118642 +  if( zCsr>zEnd ){
1.118643 +    return FTS_CORRUPT_VTAB;
1.118644 +  }
1.118645 +  
1.118646 +  while( zCsr<zEnd && (piFirst || piLast) ){
1.118647 +    int cmp;                      /* memcmp() result */
1.118648 +    int nSuffix;                  /* Size of term suffix */
1.118649 +    int nPrefix = 0;              /* Size of term prefix */
1.118650 +    int nBuffer;                  /* Total term size */
1.118651 +  
1.118652 +    /* Load the next term on the node into zBuffer. Use realloc() to expand
1.118653 +    ** the size of zBuffer if required.  */
1.118654 +    if( !isFirstTerm ){
1.118655 +      zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
1.118656 +    }
1.118657 +    isFirstTerm = 0;
1.118658 +    zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
1.118659 +    
1.118660 +    if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
1.118661 +      rc = FTS_CORRUPT_VTAB;
1.118662 +      goto finish_scan;
1.118663 +    }
1.118664 +    if( nPrefix+nSuffix>nAlloc ){
1.118665 +      char *zNew;
1.118666 +      nAlloc = (nPrefix+nSuffix) * 2;
1.118667 +      zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
1.118668 +      if( !zNew ){
1.118669 +        rc = SQLITE_NOMEM;
1.118670 +        goto finish_scan;
1.118671 +      }
1.118672 +      zBuffer = zNew;
1.118673 +    }
1.118674 +    assert( zBuffer );
1.118675 +    memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
1.118676 +    nBuffer = nPrefix + nSuffix;
1.118677 +    zCsr += nSuffix;
1.118678 +
1.118679 +    /* Compare the term we are searching for with the term just loaded from
1.118680 +    ** the interior node. If the specified term is greater than or equal
1.118681 +    ** to the term from the interior node, then all terms on the sub-tree 
1.118682 +    ** headed by node iChild are smaller than zTerm. No need to search 
1.118683 +    ** iChild.
1.118684 +    **
1.118685 +    ** If the interior node term is larger than the specified term, then
1.118686 +    ** the tree headed by iChild may contain the specified term.
1.118687 +    */
1.118688 +    cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
1.118689 +    if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
1.118690 +      *piFirst = iChild;
1.118691 +      piFirst = 0;
1.118692 +    }
1.118693 +
1.118694 +    if( piLast && cmp<0 ){
1.118695 +      *piLast = iChild;
1.118696 +      piLast = 0;
1.118697 +    }
1.118698 +
1.118699 +    iChild++;
1.118700 +  };
1.118701 +
1.118702 +  if( piFirst ) *piFirst = iChild;
1.118703 +  if( piLast ) *piLast = iChild;
1.118704 +
1.118705 + finish_scan:
1.118706 +  sqlite3_free(zBuffer);
1.118707 +  return rc;
1.118708 +}
1.118709 +
1.118710 +
1.118711 +/*
1.118712 +** The buffer pointed to by argument zNode (size nNode bytes) contains an
1.118713 +** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
1.118714 +** contains a term. This function searches the sub-tree headed by the zNode
1.118715 +** node for the range of leaf nodes that may contain the specified term
1.118716 +** or terms for which the specified term is a prefix.
1.118717 +**
1.118718 +** If piLeaf is not NULL, then *piLeaf is set to the blockid of the 
1.118719 +** left-most leaf node in the tree that may contain the specified term.
1.118720 +** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
1.118721 +** right-most leaf node that may contain a term for which the specified
1.118722 +** term is a prefix.
1.118723 +**
1.118724 +** It is possible that the range of returned leaf nodes does not contain 
1.118725 +** the specified term or any terms for which it is a prefix. However, if the 
1.118726 +** segment does contain any such terms, they are stored within the identified
1.118727 +** range. Because this function only inspects interior segment nodes (and
1.118728 +** never loads leaf nodes into memory), it is not possible to be sure.
1.118729 +**
1.118730 +** If an error occurs, an error code other than SQLITE_OK is returned.
1.118731 +*/ 
1.118732 +static int fts3SelectLeaf(
1.118733 +  Fts3Table *p,                   /* Virtual table handle */
1.118734 +  const char *zTerm,              /* Term to select leaves for */
1.118735 +  int nTerm,                      /* Size of term zTerm in bytes */
1.118736 +  const char *zNode,              /* Buffer containing segment interior node */
1.118737 +  int nNode,                      /* Size of buffer at zNode */
1.118738 +  sqlite3_int64 *piLeaf,          /* Selected leaf node */
1.118739 +  sqlite3_int64 *piLeaf2          /* Selected leaf node */
1.118740 +){
1.118741 +  int rc;                         /* Return code */
1.118742 +  int iHeight;                    /* Height of this node in tree */
1.118743 +
1.118744 +  assert( piLeaf || piLeaf2 );
1.118745 +
1.118746 +  sqlite3Fts3GetVarint32(zNode, &iHeight);
1.118747 +  rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
1.118748 +  assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
1.118749 +
1.118750 +  if( rc==SQLITE_OK && iHeight>1 ){
1.118751 +    char *zBlob = 0;              /* Blob read from %_segments table */
1.118752 +    int nBlob;                    /* Size of zBlob in bytes */
1.118753 +
1.118754 +    if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
1.118755 +      rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
1.118756 +      if( rc==SQLITE_OK ){
1.118757 +        rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
1.118758 +      }
1.118759 +      sqlite3_free(zBlob);
1.118760 +      piLeaf = 0;
1.118761 +      zBlob = 0;
1.118762 +    }
1.118763 +
1.118764 +    if( rc==SQLITE_OK ){
1.118765 +      rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
1.118766 +    }
1.118767 +    if( rc==SQLITE_OK ){
1.118768 +      rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
1.118769 +    }
1.118770 +    sqlite3_free(zBlob);
1.118771 +  }
1.118772 +
1.118773 +  return rc;
1.118774 +}
1.118775 +
1.118776 +/*
1.118777 +** This function is used to create delta-encoded serialized lists of FTS3 
1.118778 +** varints. Each call to this function appends a single varint to a list.
1.118779 +*/
1.118780 +static void fts3PutDeltaVarint(
1.118781 +  char **pp,                      /* IN/OUT: Output pointer */
1.118782 +  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
1.118783 +  sqlite3_int64 iVal              /* Write this value to the list */
1.118784 +){
1.118785 +  assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
1.118786 +  *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
1.118787 +  *piPrev = iVal;
1.118788 +}
1.118789 +
1.118790 +/*
1.118791 +** When this function is called, *ppPoslist is assumed to point to the 
1.118792 +** start of a position-list. After it returns, *ppPoslist points to the
1.118793 +** first byte after the position-list.
1.118794 +**
1.118795 +** A position list is list of positions (delta encoded) and columns for 
1.118796 +** a single document record of a doclist.  So, in other words, this
1.118797 +** routine advances *ppPoslist so that it points to the next docid in
1.118798 +** the doclist, or to the first byte past the end of the doclist.
1.118799 +**
1.118800 +** If pp is not NULL, then the contents of the position list are copied
1.118801 +** to *pp. *pp is set to point to the first byte past the last byte copied
1.118802 +** before this function returns.
1.118803 +*/
1.118804 +static void fts3PoslistCopy(char **pp, char **ppPoslist){
1.118805 +  char *pEnd = *ppPoslist;
1.118806 +  char c = 0;
1.118807 +
1.118808 +  /* The end of a position list is marked by a zero encoded as an FTS3 
1.118809 +  ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
1.118810 +  ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
1.118811 +  ** of some other, multi-byte, value.
1.118812 +  **
1.118813 +  ** The following while-loop moves pEnd to point to the first byte that is not 
1.118814 +  ** immediately preceded by a byte with the 0x80 bit set. Then increments
1.118815 +  ** pEnd once more so that it points to the byte immediately following the
1.118816 +  ** last byte in the position-list.
1.118817 +  */
1.118818 +  while( *pEnd | c ){
1.118819 +    c = *pEnd++ & 0x80;
1.118820 +    testcase( c!=0 && (*pEnd)==0 );
1.118821 +  }
1.118822 +  pEnd++;  /* Advance past the POS_END terminator byte */
1.118823 +
1.118824 +  if( pp ){
1.118825 +    int n = (int)(pEnd - *ppPoslist);
1.118826 +    char *p = *pp;
1.118827 +    memcpy(p, *ppPoslist, n);
1.118828 +    p += n;
1.118829 +    *pp = p;
1.118830 +  }
1.118831 +  *ppPoslist = pEnd;
1.118832 +}
1.118833 +
1.118834 +/*
1.118835 +** When this function is called, *ppPoslist is assumed to point to the 
1.118836 +** start of a column-list. After it returns, *ppPoslist points to the
1.118837 +** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
1.118838 +**
1.118839 +** A column-list is list of delta-encoded positions for a single column
1.118840 +** within a single document within a doclist.
1.118841 +**
1.118842 +** The column-list is terminated either by a POS_COLUMN varint (1) or
1.118843 +** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
1.118844 +** the POS_COLUMN or POS_END that terminates the column-list.
1.118845 +**
1.118846 +** If pp is not NULL, then the contents of the column-list are copied
1.118847 +** to *pp. *pp is set to point to the first byte past the last byte copied
1.118848 +** before this function returns.  The POS_COLUMN or POS_END terminator
1.118849 +** is not copied into *pp.
1.118850 +*/
1.118851 +static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
1.118852 +  char *pEnd = *ppPoslist;
1.118853 +  char c = 0;
1.118854 +
1.118855 +  /* A column-list is terminated by either a 0x01 or 0x00 byte that is
1.118856 +  ** not part of a multi-byte varint.
1.118857 +  */
1.118858 +  while( 0xFE & (*pEnd | c) ){
1.118859 +    c = *pEnd++ & 0x80;
1.118860 +    testcase( c!=0 && ((*pEnd)&0xfe)==0 );
1.118861 +  }
1.118862 +  if( pp ){
1.118863 +    int n = (int)(pEnd - *ppPoslist);
1.118864 +    char *p = *pp;
1.118865 +    memcpy(p, *ppPoslist, n);
1.118866 +    p += n;
1.118867 +    *pp = p;
1.118868 +  }
1.118869 +  *ppPoslist = pEnd;
1.118870 +}
1.118871 +
1.118872 +/*
1.118873 +** Value used to signify the end of an position-list. This is safe because
1.118874 +** it is not possible to have a document with 2^31 terms.
1.118875 +*/
1.118876 +#define POSITION_LIST_END 0x7fffffff
1.118877 +
1.118878 +/*
1.118879 +** This function is used to help parse position-lists. When this function is
1.118880 +** called, *pp may point to the start of the next varint in the position-list
1.118881 +** being parsed, or it may point to 1 byte past the end of the position-list
1.118882 +** (in which case **pp will be a terminator bytes POS_END (0) or
1.118883 +** (1)).
1.118884 +**
1.118885 +** If *pp points past the end of the current position-list, set *pi to 
1.118886 +** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
1.118887 +** increment the current value of *pi by the value read, and set *pp to
1.118888 +** point to the next value before returning.
1.118889 +**
1.118890 +** Before calling this routine *pi must be initialized to the value of
1.118891 +** the previous position, or zero if we are reading the first position
1.118892 +** in the position-list.  Because positions are delta-encoded, the value
1.118893 +** of the previous position is needed in order to compute the value of
1.118894 +** the next position.
1.118895 +*/
1.118896 +static void fts3ReadNextPos(
1.118897 +  char **pp,                    /* IN/OUT: Pointer into position-list buffer */
1.118898 +  sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
1.118899 +){
1.118900 +  if( (**pp)&0xFE ){
1.118901 +    fts3GetDeltaVarint(pp, pi);
1.118902 +    *pi -= 2;
1.118903 +  }else{
1.118904 +    *pi = POSITION_LIST_END;
1.118905 +  }
1.118906 +}
1.118907 +
1.118908 +/*
1.118909 +** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
1.118910 +** the value of iCol encoded as a varint to *pp.   This will start a new
1.118911 +** column list.
1.118912 +**
1.118913 +** Set *pp to point to the byte just after the last byte written before 
1.118914 +** returning (do not modify it if iCol==0). Return the total number of bytes
1.118915 +** written (0 if iCol==0).
1.118916 +*/
1.118917 +static int fts3PutColNumber(char **pp, int iCol){
1.118918 +  int n = 0;                      /* Number of bytes written */
1.118919 +  if( iCol ){
1.118920 +    char *p = *pp;                /* Output pointer */
1.118921 +    n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
1.118922 +    *p = 0x01;
1.118923 +    *pp = &p[n];
1.118924 +  }
1.118925 +  return n;
1.118926 +}
1.118927 +
1.118928 +/*
1.118929 +** Compute the union of two position lists.  The output written
1.118930 +** into *pp contains all positions of both *pp1 and *pp2 in sorted
1.118931 +** order and with any duplicates removed.  All pointers are
1.118932 +** updated appropriately.   The caller is responsible for insuring
1.118933 +** that there is enough space in *pp to hold the complete output.
1.118934 +*/
1.118935 +static void fts3PoslistMerge(
1.118936 +  char **pp,                      /* Output buffer */
1.118937 +  char **pp1,                     /* Left input list */
1.118938 +  char **pp2                      /* Right input list */
1.118939 +){
1.118940 +  char *p = *pp;
1.118941 +  char *p1 = *pp1;
1.118942 +  char *p2 = *pp2;
1.118943 +
1.118944 +  while( *p1 || *p2 ){
1.118945 +    int iCol1;         /* The current column index in pp1 */
1.118946 +    int iCol2;         /* The current column index in pp2 */
1.118947 +
1.118948 +    if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
1.118949 +    else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
1.118950 +    else iCol1 = 0;
1.118951 +
1.118952 +    if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
1.118953 +    else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
1.118954 +    else iCol2 = 0;
1.118955 +
1.118956 +    if( iCol1==iCol2 ){
1.118957 +      sqlite3_int64 i1 = 0;       /* Last position from pp1 */
1.118958 +      sqlite3_int64 i2 = 0;       /* Last position from pp2 */
1.118959 +      sqlite3_int64 iPrev = 0;
1.118960 +      int n = fts3PutColNumber(&p, iCol1);
1.118961 +      p1 += n;
1.118962 +      p2 += n;
1.118963 +
1.118964 +      /* At this point, both p1 and p2 point to the start of column-lists
1.118965 +      ** for the same column (the column with index iCol1 and iCol2).
1.118966 +      ** A column-list is a list of non-negative delta-encoded varints, each 
1.118967 +      ** incremented by 2 before being stored. Each list is terminated by a
1.118968 +      ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
1.118969 +      ** and writes the results to buffer p. p is left pointing to the byte
1.118970 +      ** after the list written. No terminator (POS_END or POS_COLUMN) is
1.118971 +      ** written to the output.
1.118972 +      */
1.118973 +      fts3GetDeltaVarint(&p1, &i1);
1.118974 +      fts3GetDeltaVarint(&p2, &i2);
1.118975 +      do {
1.118976 +        fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2); 
1.118977 +        iPrev -= 2;
1.118978 +        if( i1==i2 ){
1.118979 +          fts3ReadNextPos(&p1, &i1);
1.118980 +          fts3ReadNextPos(&p2, &i2);
1.118981 +        }else if( i1<i2 ){
1.118982 +          fts3ReadNextPos(&p1, &i1);
1.118983 +        }else{
1.118984 +          fts3ReadNextPos(&p2, &i2);
1.118985 +        }
1.118986 +      }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
1.118987 +    }else if( iCol1<iCol2 ){
1.118988 +      p1 += fts3PutColNumber(&p, iCol1);
1.118989 +      fts3ColumnlistCopy(&p, &p1);
1.118990 +    }else{
1.118991 +      p2 += fts3PutColNumber(&p, iCol2);
1.118992 +      fts3ColumnlistCopy(&p, &p2);
1.118993 +    }
1.118994 +  }
1.118995 +
1.118996 +  *p++ = POS_END;
1.118997 +  *pp = p;
1.118998 +  *pp1 = p1 + 1;
1.118999 +  *pp2 = p2 + 1;
1.119000 +}
1.119001 +
1.119002 +/*
1.119003 +** This function is used to merge two position lists into one. When it is
1.119004 +** called, *pp1 and *pp2 must both point to position lists. A position-list is
1.119005 +** the part of a doclist that follows each document id. For example, if a row
1.119006 +** contains:
1.119007 +**
1.119008 +**     'a b c'|'x y z'|'a b b a'
1.119009 +**
1.119010 +** Then the position list for this row for token 'b' would consist of:
1.119011 +**
1.119012 +**     0x02 0x01 0x02 0x03 0x03 0x00
1.119013 +**
1.119014 +** When this function returns, both *pp1 and *pp2 are left pointing to the
1.119015 +** byte following the 0x00 terminator of their respective position lists.
1.119016 +**
1.119017 +** If isSaveLeft is 0, an entry is added to the output position list for 
1.119018 +** each position in *pp2 for which there exists one or more positions in
1.119019 +** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
1.119020 +** when the *pp1 token appears before the *pp2 token, but not more than nToken
1.119021 +** slots before it.
1.119022 +**
1.119023 +** e.g. nToken==1 searches for adjacent positions.
1.119024 +*/
1.119025 +static int fts3PoslistPhraseMerge(
1.119026 +  char **pp,                      /* IN/OUT: Preallocated output buffer */
1.119027 +  int nToken,                     /* Maximum difference in token positions */
1.119028 +  int isSaveLeft,                 /* Save the left position */
1.119029 +  int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
1.119030 +  char **pp1,                     /* IN/OUT: Left input list */
1.119031 +  char **pp2                      /* IN/OUT: Right input list */
1.119032 +){
1.119033 +  char *p = *pp;
1.119034 +  char *p1 = *pp1;
1.119035 +  char *p2 = *pp2;
1.119036 +  int iCol1 = 0;
1.119037 +  int iCol2 = 0;
1.119038 +
1.119039 +  /* Never set both isSaveLeft and isExact for the same invocation. */
1.119040 +  assert( isSaveLeft==0 || isExact==0 );
1.119041 +
1.119042 +  assert( p!=0 && *p1!=0 && *p2!=0 );
1.119043 +  if( *p1==POS_COLUMN ){ 
1.119044 +    p1++;
1.119045 +    p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
1.119046 +  }
1.119047 +  if( *p2==POS_COLUMN ){ 
1.119048 +    p2++;
1.119049 +    p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
1.119050 +  }
1.119051 +
1.119052 +  while( 1 ){
1.119053 +    if( iCol1==iCol2 ){
1.119054 +      char *pSave = p;
1.119055 +      sqlite3_int64 iPrev = 0;
1.119056 +      sqlite3_int64 iPos1 = 0;
1.119057 +      sqlite3_int64 iPos2 = 0;
1.119058 +
1.119059 +      if( iCol1 ){
1.119060 +        *p++ = POS_COLUMN;
1.119061 +        p += sqlite3Fts3PutVarint(p, iCol1);
1.119062 +      }
1.119063 +
1.119064 +      assert( *p1!=POS_END && *p1!=POS_COLUMN );
1.119065 +      assert( *p2!=POS_END && *p2!=POS_COLUMN );
1.119066 +      fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
1.119067 +      fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
1.119068 +
1.119069 +      while( 1 ){
1.119070 +        if( iPos2==iPos1+nToken 
1.119071 +         || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) 
1.119072 +        ){
1.119073 +          sqlite3_int64 iSave;
1.119074 +          iSave = isSaveLeft ? iPos1 : iPos2;
1.119075 +          fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
1.119076 +          pSave = 0;
1.119077 +          assert( p );
1.119078 +        }
1.119079 +        if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
1.119080 +          if( (*p2&0xFE)==0 ) break;
1.119081 +          fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
1.119082 +        }else{
1.119083 +          if( (*p1&0xFE)==0 ) break;
1.119084 +          fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
1.119085 +        }
1.119086 +      }
1.119087 +
1.119088 +      if( pSave ){
1.119089 +        assert( pp && p );
1.119090 +        p = pSave;
1.119091 +      }
1.119092 +
1.119093 +      fts3ColumnlistCopy(0, &p1);
1.119094 +      fts3ColumnlistCopy(0, &p2);
1.119095 +      assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
1.119096 +      if( 0==*p1 || 0==*p2 ) break;
1.119097 +
1.119098 +      p1++;
1.119099 +      p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
1.119100 +      p2++;
1.119101 +      p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
1.119102 +    }
1.119103 +
1.119104 +    /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
1.119105 +    ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
1.119106 +    ** end of the position list, or the 0x01 that precedes the next 
1.119107 +    ** column-number in the position list. 
1.119108 +    */
1.119109 +    else if( iCol1<iCol2 ){
1.119110 +      fts3ColumnlistCopy(0, &p1);
1.119111 +      if( 0==*p1 ) break;
1.119112 +      p1++;
1.119113 +      p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
1.119114 +    }else{
1.119115 +      fts3ColumnlistCopy(0, &p2);
1.119116 +      if( 0==*p2 ) break;
1.119117 +      p2++;
1.119118 +      p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
1.119119 +    }
1.119120 +  }
1.119121 +
1.119122 +  fts3PoslistCopy(0, &p2);
1.119123 +  fts3PoslistCopy(0, &p1);
1.119124 +  *pp1 = p1;
1.119125 +  *pp2 = p2;
1.119126 +  if( *pp==p ){
1.119127 +    return 0;
1.119128 +  }
1.119129 +  *p++ = 0x00;
1.119130 +  *pp = p;
1.119131 +  return 1;
1.119132 +}
1.119133 +
1.119134 +/*
1.119135 +** Merge two position-lists as required by the NEAR operator. The argument
1.119136 +** position lists correspond to the left and right phrases of an expression 
1.119137 +** like:
1.119138 +**
1.119139 +**     "phrase 1" NEAR "phrase number 2"
1.119140 +**
1.119141 +** Position list *pp1 corresponds to the left-hand side of the NEAR 
1.119142 +** expression and *pp2 to the right. As usual, the indexes in the position 
1.119143 +** lists are the offsets of the last token in each phrase (tokens "1" and "2" 
1.119144 +** in the example above).
1.119145 +**
1.119146 +** The output position list - written to *pp - is a copy of *pp2 with those
1.119147 +** entries that are not sufficiently NEAR entries in *pp1 removed.
1.119148 +*/
1.119149 +static int fts3PoslistNearMerge(
1.119150 +  char **pp,                      /* Output buffer */
1.119151 +  char *aTmp,                     /* Temporary buffer space */
1.119152 +  int nRight,                     /* Maximum difference in token positions */
1.119153 +  int nLeft,                      /* Maximum difference in token positions */
1.119154 +  char **pp1,                     /* IN/OUT: Left input list */
1.119155 +  char **pp2                      /* IN/OUT: Right input list */
1.119156 +){
1.119157 +  char *p1 = *pp1;
1.119158 +  char *p2 = *pp2;
1.119159 +
1.119160 +  char *pTmp1 = aTmp;
1.119161 +  char *pTmp2;
1.119162 +  char *aTmp2;
1.119163 +  int res = 1;
1.119164 +
1.119165 +  fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
1.119166 +  aTmp2 = pTmp2 = pTmp1;
1.119167 +  *pp1 = p1;
1.119168 +  *pp2 = p2;
1.119169 +  fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
1.119170 +  if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
1.119171 +    fts3PoslistMerge(pp, &aTmp, &aTmp2);
1.119172 +  }else if( pTmp1!=aTmp ){
1.119173 +    fts3PoslistCopy(pp, &aTmp);
1.119174 +  }else if( pTmp2!=aTmp2 ){
1.119175 +    fts3PoslistCopy(pp, &aTmp2);
1.119176 +  }else{
1.119177 +    res = 0;
1.119178 +  }
1.119179 +
1.119180 +  return res;
1.119181 +}
1.119182 +
1.119183 +/* 
1.119184 +** An instance of this function is used to merge together the (potentially
1.119185 +** large number of) doclists for each term that matches a prefix query.
1.119186 +** See function fts3TermSelectMerge() for details.
1.119187 +*/
1.119188 +typedef struct TermSelect TermSelect;
1.119189 +struct TermSelect {
1.119190 +  char *aaOutput[16];             /* Malloc'd output buffers */
1.119191 +  int anOutput[16];               /* Size each output buffer in bytes */
1.119192 +};
1.119193 +
1.119194 +/*
1.119195 +** This function is used to read a single varint from a buffer. Parameter
1.119196 +** pEnd points 1 byte past the end of the buffer. When this function is
1.119197 +** called, if *pp points to pEnd or greater, then the end of the buffer
1.119198 +** has been reached. In this case *pp is set to 0 and the function returns.
1.119199 +**
1.119200 +** If *pp does not point to or past pEnd, then a single varint is read
1.119201 +** from *pp. *pp is then set to point 1 byte past the end of the read varint.
1.119202 +**
1.119203 +** If bDescIdx is false, the value read is added to *pVal before returning.
1.119204 +** If it is true, the value read is subtracted from *pVal before this 
1.119205 +** function returns.
1.119206 +*/
1.119207 +static void fts3GetDeltaVarint3(
1.119208 +  char **pp,                      /* IN/OUT: Point to read varint from */
1.119209 +  char *pEnd,                     /* End of buffer */
1.119210 +  int bDescIdx,                   /* True if docids are descending */
1.119211 +  sqlite3_int64 *pVal             /* IN/OUT: Integer value */
1.119212 +){
1.119213 +  if( *pp>=pEnd ){
1.119214 +    *pp = 0;
1.119215 +  }else{
1.119216 +    sqlite3_int64 iVal;
1.119217 +    *pp += sqlite3Fts3GetVarint(*pp, &iVal);
1.119218 +    if( bDescIdx ){
1.119219 +      *pVal -= iVal;
1.119220 +    }else{
1.119221 +      *pVal += iVal;
1.119222 +    }
1.119223 +  }
1.119224 +}
1.119225 +
1.119226 +/*
1.119227 +** This function is used to write a single varint to a buffer. The varint
1.119228 +** is written to *pp. Before returning, *pp is set to point 1 byte past the
1.119229 +** end of the value written.
1.119230 +**
1.119231 +** If *pbFirst is zero when this function is called, the value written to
1.119232 +** the buffer is that of parameter iVal. 
1.119233 +**
1.119234 +** If *pbFirst is non-zero when this function is called, then the value 
1.119235 +** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
1.119236 +** (if bDescIdx is non-zero).
1.119237 +**
1.119238 +** Before returning, this function always sets *pbFirst to 1 and *piPrev
1.119239 +** to the value of parameter iVal.
1.119240 +*/
1.119241 +static void fts3PutDeltaVarint3(
1.119242 +  char **pp,                      /* IN/OUT: Output pointer */
1.119243 +  int bDescIdx,                   /* True for descending docids */
1.119244 +  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
1.119245 +  int *pbFirst,                   /* IN/OUT: True after first int written */
1.119246 +  sqlite3_int64 iVal              /* Write this value to the list */
1.119247 +){
1.119248 +  sqlite3_int64 iWrite;
1.119249 +  if( bDescIdx==0 || *pbFirst==0 ){
1.119250 +    iWrite = iVal - *piPrev;
1.119251 +  }else{
1.119252 +    iWrite = *piPrev - iVal;
1.119253 +  }
1.119254 +  assert( *pbFirst || *piPrev==0 );
1.119255 +  assert( *pbFirst==0 || iWrite>0 );
1.119256 +  *pp += sqlite3Fts3PutVarint(*pp, iWrite);
1.119257 +  *piPrev = iVal;
1.119258 +  *pbFirst = 1;
1.119259 +}
1.119260 +
1.119261 +
1.119262 +/*
1.119263 +** This macro is used by various functions that merge doclists. The two
1.119264 +** arguments are 64-bit docid values. If the value of the stack variable
1.119265 +** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2). 
1.119266 +** Otherwise, (i2-i1).
1.119267 +**
1.119268 +** Using this makes it easier to write code that can merge doclists that are
1.119269 +** sorted in either ascending or descending order.
1.119270 +*/
1.119271 +#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
1.119272 +
1.119273 +/*
1.119274 +** This function does an "OR" merge of two doclists (output contains all
1.119275 +** positions contained in either argument doclist). If the docids in the 
1.119276 +** input doclists are sorted in ascending order, parameter bDescDoclist
1.119277 +** should be false. If they are sorted in ascending order, it should be
1.119278 +** passed a non-zero value.
1.119279 +**
1.119280 +** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
1.119281 +** containing the output doclist and SQLITE_OK is returned. In this case
1.119282 +** *pnOut is set to the number of bytes in the output doclist.
1.119283 +**
1.119284 +** If an error occurs, an SQLite error code is returned. The output values
1.119285 +** are undefined in this case.
1.119286 +*/
1.119287 +static int fts3DoclistOrMerge(
1.119288 +  int bDescDoclist,               /* True if arguments are desc */
1.119289 +  char *a1, int n1,               /* First doclist */
1.119290 +  char *a2, int n2,               /* Second doclist */
1.119291 +  char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
1.119292 +){
1.119293 +  sqlite3_int64 i1 = 0;
1.119294 +  sqlite3_int64 i2 = 0;
1.119295 +  sqlite3_int64 iPrev = 0;
1.119296 +  char *pEnd1 = &a1[n1];
1.119297 +  char *pEnd2 = &a2[n2];
1.119298 +  char *p1 = a1;
1.119299 +  char *p2 = a2;
1.119300 +  char *p;
1.119301 +  char *aOut;
1.119302 +  int bFirstOut = 0;
1.119303 +
1.119304 +  *paOut = 0;
1.119305 +  *pnOut = 0;
1.119306 +
1.119307 +  /* Allocate space for the output. Both the input and output doclists
1.119308 +  ** are delta encoded. If they are in ascending order (bDescDoclist==0),
1.119309 +  ** then the first docid in each list is simply encoded as a varint. For
1.119310 +  ** each subsequent docid, the varint stored is the difference between the
1.119311 +  ** current and previous docid (a positive number - since the list is in
1.119312 +  ** ascending order).
1.119313 +  **
1.119314 +  ** The first docid written to the output is therefore encoded using the 
1.119315 +  ** same number of bytes as it is in whichever of the input lists it is
1.119316 +  ** read from. And each subsequent docid read from the same input list 
1.119317 +  ** consumes either the same or less bytes as it did in the input (since
1.119318 +  ** the difference between it and the previous value in the output must
1.119319 +  ** be a positive value less than or equal to the delta value read from 
1.119320 +  ** the input list). The same argument applies to all but the first docid
1.119321 +  ** read from the 'other' list. And to the contents of all position lists
1.119322 +  ** that will be copied and merged from the input to the output.
1.119323 +  **
1.119324 +  ** However, if the first docid copied to the output is a negative number,
1.119325 +  ** then the encoding of the first docid from the 'other' input list may
1.119326 +  ** be larger in the output than it was in the input (since the delta value
1.119327 +  ** may be a larger positive integer than the actual docid).
1.119328 +  **
1.119329 +  ** The space required to store the output is therefore the sum of the
1.119330 +  ** sizes of the two inputs, plus enough space for exactly one of the input
1.119331 +  ** docids to grow. 
1.119332 +  **
1.119333 +  ** A symetric argument may be made if the doclists are in descending 
1.119334 +  ** order.
1.119335 +  */
1.119336 +  aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
1.119337 +  if( !aOut ) return SQLITE_NOMEM;
1.119338 +
1.119339 +  p = aOut;
1.119340 +  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
1.119341 +  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
1.119342 +  while( p1 || p2 ){
1.119343 +    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
1.119344 +
1.119345 +    if( p2 && p1 && iDiff==0 ){
1.119346 +      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
1.119347 +      fts3PoslistMerge(&p, &p1, &p2);
1.119348 +      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
1.119349 +      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
1.119350 +    }else if( !p2 || (p1 && iDiff<0) ){
1.119351 +      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
1.119352 +      fts3PoslistCopy(&p, &p1);
1.119353 +      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
1.119354 +    }else{
1.119355 +      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
1.119356 +      fts3PoslistCopy(&p, &p2);
1.119357 +      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
1.119358 +    }
1.119359 +  }
1.119360 +
1.119361 +  *paOut = aOut;
1.119362 +  *pnOut = (int)(p-aOut);
1.119363 +  assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
1.119364 +  return SQLITE_OK;
1.119365 +}
1.119366 +
1.119367 +/*
1.119368 +** This function does a "phrase" merge of two doclists. In a phrase merge,
1.119369 +** the output contains a copy of each position from the right-hand input
1.119370 +** doclist for which there is a position in the left-hand input doclist
1.119371 +** exactly nDist tokens before it.
1.119372 +**
1.119373 +** If the docids in the input doclists are sorted in ascending order,
1.119374 +** parameter bDescDoclist should be false. If they are sorted in ascending 
1.119375 +** order, it should be passed a non-zero value.
1.119376 +**
1.119377 +** The right-hand input doclist is overwritten by this function.
1.119378 +*/
1.119379 +static void fts3DoclistPhraseMerge(
1.119380 +  int bDescDoclist,               /* True if arguments are desc */
1.119381 +  int nDist,                      /* Distance from left to right (1=adjacent) */
1.119382 +  char *aLeft, int nLeft,         /* Left doclist */
1.119383 +  char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
1.119384 +){
1.119385 +  sqlite3_int64 i1 = 0;
1.119386 +  sqlite3_int64 i2 = 0;
1.119387 +  sqlite3_int64 iPrev = 0;
1.119388 +  char *pEnd1 = &aLeft[nLeft];
1.119389 +  char *pEnd2 = &aRight[*pnRight];
1.119390 +  char *p1 = aLeft;
1.119391 +  char *p2 = aRight;
1.119392 +  char *p;
1.119393 +  int bFirstOut = 0;
1.119394 +  char *aOut = aRight;
1.119395 +
1.119396 +  assert( nDist>0 );
1.119397 +
1.119398 +  p = aOut;
1.119399 +  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
1.119400 +  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
1.119401 +
1.119402 +  while( p1 && p2 ){
1.119403 +    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
1.119404 +    if( iDiff==0 ){
1.119405 +      char *pSave = p;
1.119406 +      sqlite3_int64 iPrevSave = iPrev;
1.119407 +      int bFirstOutSave = bFirstOut;
1.119408 +
1.119409 +      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
1.119410 +      if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
1.119411 +        p = pSave;
1.119412 +        iPrev = iPrevSave;
1.119413 +        bFirstOut = bFirstOutSave;
1.119414 +      }
1.119415 +      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
1.119416 +      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
1.119417 +    }else if( iDiff<0 ){
1.119418 +      fts3PoslistCopy(0, &p1);
1.119419 +      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
1.119420 +    }else{
1.119421 +      fts3PoslistCopy(0, &p2);
1.119422 +      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
1.119423 +    }
1.119424 +  }
1.119425 +
1.119426 +  *pnRight = (int)(p - aOut);
1.119427 +}
1.119428 +
1.119429 +/*
1.119430 +** Argument pList points to a position list nList bytes in size. This
1.119431 +** function checks to see if the position list contains any entries for
1.119432 +** a token in position 0 (of any column). If so, it writes argument iDelta
1.119433 +** to the output buffer pOut, followed by a position list consisting only
1.119434 +** of the entries from pList at position 0, and terminated by an 0x00 byte.
1.119435 +** The value returned is the number of bytes written to pOut (if any).
1.119436 +*/
1.119437 +SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
1.119438 +  sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
1.119439 +  char *pList,                    /* Position list (no 0x00 term) */
1.119440 +  int nList,                      /* Size of pList in bytes */
1.119441 +  char *pOut                      /* Write output here */
1.119442 +){
1.119443 +  int nOut = 0;
1.119444 +  int bWritten = 0;               /* True once iDelta has been written */
1.119445 +  char *p = pList;
1.119446 +  char *pEnd = &pList[nList];
1.119447 +
1.119448 +  if( *p!=0x01 ){
1.119449 +    if( *p==0x02 ){
1.119450 +      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
1.119451 +      pOut[nOut++] = 0x02;
1.119452 +      bWritten = 1;
1.119453 +    }
1.119454 +    fts3ColumnlistCopy(0, &p);
1.119455 +  }
1.119456 +
1.119457 +  while( p<pEnd && *p==0x01 ){
1.119458 +    sqlite3_int64 iCol;
1.119459 +    p++;
1.119460 +    p += sqlite3Fts3GetVarint(p, &iCol);
1.119461 +    if( *p==0x02 ){
1.119462 +      if( bWritten==0 ){
1.119463 +        nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
1.119464 +        bWritten = 1;
1.119465 +      }
1.119466 +      pOut[nOut++] = 0x01;
1.119467 +      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
1.119468 +      pOut[nOut++] = 0x02;
1.119469 +    }
1.119470 +    fts3ColumnlistCopy(0, &p);
1.119471 +  }
1.119472 +  if( bWritten ){
1.119473 +    pOut[nOut++] = 0x00;
1.119474 +  }
1.119475 +
1.119476 +  return nOut;
1.119477 +}
1.119478 +
1.119479 +
1.119480 +/*
1.119481 +** Merge all doclists in the TermSelect.aaOutput[] array into a single
1.119482 +** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
1.119483 +** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
1.119484 +**
1.119485 +** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
1.119486 +** the responsibility of the caller to free any doclists left in the
1.119487 +** TermSelect.aaOutput[] array.
1.119488 +*/
1.119489 +static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
1.119490 +  char *aOut = 0;
1.119491 +  int nOut = 0;
1.119492 +  int i;
1.119493 +
1.119494 +  /* Loop through the doclists in the aaOutput[] array. Merge them all
1.119495 +  ** into a single doclist.
1.119496 +  */
1.119497 +  for(i=0; i<SizeofArray(pTS->aaOutput); i++){
1.119498 +    if( pTS->aaOutput[i] ){
1.119499 +      if( !aOut ){
1.119500 +        aOut = pTS->aaOutput[i];
1.119501 +        nOut = pTS->anOutput[i];
1.119502 +        pTS->aaOutput[i] = 0;
1.119503 +      }else{
1.119504 +        int nNew;
1.119505 +        char *aNew;
1.119506 +
1.119507 +        int rc = fts3DoclistOrMerge(p->bDescIdx, 
1.119508 +            pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
1.119509 +        );
1.119510 +        if( rc!=SQLITE_OK ){
1.119511 +          sqlite3_free(aOut);
1.119512 +          return rc;
1.119513 +        }
1.119514 +
1.119515 +        sqlite3_free(pTS->aaOutput[i]);
1.119516 +        sqlite3_free(aOut);
1.119517 +        pTS->aaOutput[i] = 0;
1.119518 +        aOut = aNew;
1.119519 +        nOut = nNew;
1.119520 +      }
1.119521 +    }
1.119522 +  }
1.119523 +
1.119524 +  pTS->aaOutput[0] = aOut;
1.119525 +  pTS->anOutput[0] = nOut;
1.119526 +  return SQLITE_OK;
1.119527 +}
1.119528 +
1.119529 +/*
1.119530 +** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
1.119531 +** as the first argument. The merge is an "OR" merge (see function
1.119532 +** fts3DoclistOrMerge() for details).
1.119533 +**
1.119534 +** This function is called with the doclist for each term that matches
1.119535 +** a queried prefix. It merges all these doclists into one, the doclist
1.119536 +** for the specified prefix. Since there can be a very large number of
1.119537 +** doclists to merge, the merging is done pair-wise using the TermSelect
1.119538 +** object.
1.119539 +**
1.119540 +** This function returns SQLITE_OK if the merge is successful, or an
1.119541 +** SQLite error code (SQLITE_NOMEM) if an error occurs.
1.119542 +*/
1.119543 +static int fts3TermSelectMerge(
1.119544 +  Fts3Table *p,                   /* FTS table handle */
1.119545 +  TermSelect *pTS,                /* TermSelect object to merge into */
1.119546 +  char *aDoclist,                 /* Pointer to doclist */
1.119547 +  int nDoclist                    /* Size of aDoclist in bytes */
1.119548 +){
1.119549 +  if( pTS->aaOutput[0]==0 ){
1.119550 +    /* If this is the first term selected, copy the doclist to the output
1.119551 +    ** buffer using memcpy(). */
1.119552 +    pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
1.119553 +    pTS->anOutput[0] = nDoclist;
1.119554 +    if( pTS->aaOutput[0] ){
1.119555 +      memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
1.119556 +    }else{
1.119557 +      return SQLITE_NOMEM;
1.119558 +    }
1.119559 +  }else{
1.119560 +    char *aMerge = aDoclist;
1.119561 +    int nMerge = nDoclist;
1.119562 +    int iOut;
1.119563 +
1.119564 +    for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
1.119565 +      if( pTS->aaOutput[iOut]==0 ){
1.119566 +        assert( iOut>0 );
1.119567 +        pTS->aaOutput[iOut] = aMerge;
1.119568 +        pTS->anOutput[iOut] = nMerge;
1.119569 +        break;
1.119570 +      }else{
1.119571 +        char *aNew;
1.119572 +        int nNew;
1.119573 +
1.119574 +        int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge, 
1.119575 +            pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
1.119576 +        );
1.119577 +        if( rc!=SQLITE_OK ){
1.119578 +          if( aMerge!=aDoclist ) sqlite3_free(aMerge);
1.119579 +          return rc;
1.119580 +        }
1.119581 +
1.119582 +        if( aMerge!=aDoclist ) sqlite3_free(aMerge);
1.119583 +        sqlite3_free(pTS->aaOutput[iOut]);
1.119584 +        pTS->aaOutput[iOut] = 0;
1.119585 +  
1.119586 +        aMerge = aNew;
1.119587 +        nMerge = nNew;
1.119588 +        if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
1.119589 +          pTS->aaOutput[iOut] = aMerge;
1.119590 +          pTS->anOutput[iOut] = nMerge;
1.119591 +        }
1.119592 +      }
1.119593 +    }
1.119594 +  }
1.119595 +  return SQLITE_OK;
1.119596 +}
1.119597 +
1.119598 +/*
1.119599 +** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
1.119600 +*/
1.119601 +static int fts3SegReaderCursorAppend(
1.119602 +  Fts3MultiSegReader *pCsr, 
1.119603 +  Fts3SegReader *pNew
1.119604 +){
1.119605 +  if( (pCsr->nSegment%16)==0 ){
1.119606 +    Fts3SegReader **apNew;
1.119607 +    int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
1.119608 +    apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
1.119609 +    if( !apNew ){
1.119610 +      sqlite3Fts3SegReaderFree(pNew);
1.119611 +      return SQLITE_NOMEM;
1.119612 +    }
1.119613 +    pCsr->apSegment = apNew;
1.119614 +  }
1.119615 +  pCsr->apSegment[pCsr->nSegment++] = pNew;
1.119616 +  return SQLITE_OK;
1.119617 +}
1.119618 +
1.119619 +/*
1.119620 +** Add seg-reader objects to the Fts3MultiSegReader object passed as the
1.119621 +** 8th argument.
1.119622 +**
1.119623 +** This function returns SQLITE_OK if successful, or an SQLite error code
1.119624 +** otherwise.
1.119625 +*/
1.119626 +static int fts3SegReaderCursor(
1.119627 +  Fts3Table *p,                   /* FTS3 table handle */
1.119628 +  int iLangid,                    /* Language id */
1.119629 +  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
1.119630 +  int iLevel,                     /* Level of segments to scan */
1.119631 +  const char *zTerm,              /* Term to query for */
1.119632 +  int nTerm,                      /* Size of zTerm in bytes */
1.119633 +  int isPrefix,                   /* True for a prefix search */
1.119634 +  int isScan,                     /* True to scan from zTerm to EOF */
1.119635 +  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
1.119636 +){
1.119637 +  int rc = SQLITE_OK;             /* Error code */
1.119638 +  sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
1.119639 +  int rc2;                        /* Result of sqlite3_reset() */
1.119640 +
1.119641 +  /* If iLevel is less than 0 and this is not a scan, include a seg-reader 
1.119642 +  ** for the pending-terms. If this is a scan, then this call must be being
1.119643 +  ** made by an fts4aux module, not an FTS table. In this case calling
1.119644 +  ** Fts3SegReaderPending might segfault, as the data structures used by 
1.119645 +  ** fts4aux are not completely populated. So it's easiest to filter these
1.119646 +  ** calls out here.  */
1.119647 +  if( iLevel<0 && p->aIndex ){
1.119648 +    Fts3SegReader *pSeg = 0;
1.119649 +    rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
1.119650 +    if( rc==SQLITE_OK && pSeg ){
1.119651 +      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
1.119652 +    }
1.119653 +  }
1.119654 +
1.119655 +  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
1.119656 +    if( rc==SQLITE_OK ){
1.119657 +      rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
1.119658 +    }
1.119659 +
1.119660 +    while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
1.119661 +      Fts3SegReader *pSeg = 0;
1.119662 +
1.119663 +      /* Read the values returned by the SELECT into local variables. */
1.119664 +      sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
1.119665 +      sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
1.119666 +      sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
1.119667 +      int nRoot = sqlite3_column_bytes(pStmt, 4);
1.119668 +      char const *zRoot = sqlite3_column_blob(pStmt, 4);
1.119669 +
1.119670 +      /* If zTerm is not NULL, and this segment is not stored entirely on its
1.119671 +      ** root node, the range of leaves scanned can be reduced. Do this. */
1.119672 +      if( iStartBlock && zTerm ){
1.119673 +        sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
1.119674 +        rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
1.119675 +        if( rc!=SQLITE_OK ) goto finished;
1.119676 +        if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
1.119677 +      }
1.119678 + 
1.119679 +      rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1, 
1.119680 +          (isPrefix==0 && isScan==0),
1.119681 +          iStartBlock, iLeavesEndBlock, 
1.119682 +          iEndBlock, zRoot, nRoot, &pSeg
1.119683 +      );
1.119684 +      if( rc!=SQLITE_OK ) goto finished;
1.119685 +      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
1.119686 +    }
1.119687 +  }
1.119688 +
1.119689 + finished:
1.119690 +  rc2 = sqlite3_reset(pStmt);
1.119691 +  if( rc==SQLITE_DONE ) rc = rc2;
1.119692 +
1.119693 +  return rc;
1.119694 +}
1.119695 +
1.119696 +/*
1.119697 +** Set up a cursor object for iterating through a full-text index or a 
1.119698 +** single level therein.
1.119699 +*/
1.119700 +SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
1.119701 +  Fts3Table *p,                   /* FTS3 table handle */
1.119702 +  int iLangid,                    /* Language-id to search */
1.119703 +  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
1.119704 +  int iLevel,                     /* Level of segments to scan */
1.119705 +  const char *zTerm,              /* Term to query for */
1.119706 +  int nTerm,                      /* Size of zTerm in bytes */
1.119707 +  int isPrefix,                   /* True for a prefix search */
1.119708 +  int isScan,                     /* True to scan from zTerm to EOF */
1.119709 +  Fts3MultiSegReader *pCsr       /* Cursor object to populate */
1.119710 +){
1.119711 +  assert( iIndex>=0 && iIndex<p->nIndex );
1.119712 +  assert( iLevel==FTS3_SEGCURSOR_ALL
1.119713 +      ||  iLevel==FTS3_SEGCURSOR_PENDING 
1.119714 +      ||  iLevel>=0
1.119715 +  );
1.119716 +  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
1.119717 +  assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
1.119718 +  assert( isPrefix==0 || isScan==0 );
1.119719 +
1.119720 +  memset(pCsr, 0, sizeof(Fts3MultiSegReader));
1.119721 +  return fts3SegReaderCursor(
1.119722 +      p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
1.119723 +  );
1.119724 +}
1.119725 +
1.119726 +/*
1.119727 +** In addition to its current configuration, have the Fts3MultiSegReader
1.119728 +** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
1.119729 +**
1.119730 +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
1.119731 +*/
1.119732 +static int fts3SegReaderCursorAddZero(
1.119733 +  Fts3Table *p,                   /* FTS virtual table handle */
1.119734 +  int iLangid,
1.119735 +  const char *zTerm,              /* Term to scan doclist of */
1.119736 +  int nTerm,                      /* Number of bytes in zTerm */
1.119737 +  Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
1.119738 +){
1.119739 +  return fts3SegReaderCursor(p, 
1.119740 +      iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
1.119741 +  );
1.119742 +}
1.119743 +
1.119744 +/*
1.119745 +** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
1.119746 +** if isPrefix is true, to scan the doclist for all terms for which 
1.119747 +** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
1.119748 +** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
1.119749 +** an SQLite error code.
1.119750 +**
1.119751 +** It is the responsibility of the caller to free this object by eventually
1.119752 +** passing it to fts3SegReaderCursorFree() 
1.119753 +**
1.119754 +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
1.119755 +** Output parameter *ppSegcsr is set to 0 if an error occurs.
1.119756 +*/
1.119757 +static int fts3TermSegReaderCursor(
1.119758 +  Fts3Cursor *pCsr,               /* Virtual table cursor handle */
1.119759 +  const char *zTerm,              /* Term to query for */
1.119760 +  int nTerm,                      /* Size of zTerm in bytes */
1.119761 +  int isPrefix,                   /* True for a prefix search */
1.119762 +  Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
1.119763 +){
1.119764 +  Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
1.119765 +  int rc = SQLITE_NOMEM;          /* Return code */
1.119766 +
1.119767 +  pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
1.119768 +  if( pSegcsr ){
1.119769 +    int i;
1.119770 +    int bFound = 0;               /* True once an index has been found */
1.119771 +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
1.119772 +
1.119773 +    if( isPrefix ){
1.119774 +      for(i=1; bFound==0 && i<p->nIndex; i++){
1.119775 +        if( p->aIndex[i].nPrefix==nTerm ){
1.119776 +          bFound = 1;
1.119777 +          rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
1.119778 +              i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
1.119779 +          );
1.119780 +          pSegcsr->bLookup = 1;
1.119781 +        }
1.119782 +      }
1.119783 +
1.119784 +      for(i=1; bFound==0 && i<p->nIndex; i++){
1.119785 +        if( p->aIndex[i].nPrefix==nTerm+1 ){
1.119786 +          bFound = 1;
1.119787 +          rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
1.119788 +              i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
1.119789 +          );
1.119790 +          if( rc==SQLITE_OK ){
1.119791 +            rc = fts3SegReaderCursorAddZero(
1.119792 +                p, pCsr->iLangid, zTerm, nTerm, pSegcsr
1.119793 +            );
1.119794 +          }
1.119795 +        }
1.119796 +      }
1.119797 +    }
1.119798 +
1.119799 +    if( bFound==0 ){
1.119800 +      rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
1.119801 +          0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
1.119802 +      );
1.119803 +      pSegcsr->bLookup = !isPrefix;
1.119804 +    }
1.119805 +  }
1.119806 +
1.119807 +  *ppSegcsr = pSegcsr;
1.119808 +  return rc;
1.119809 +}
1.119810 +
1.119811 +/*
1.119812 +** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
1.119813 +*/
1.119814 +static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
1.119815 +  sqlite3Fts3SegReaderFinish(pSegcsr);
1.119816 +  sqlite3_free(pSegcsr);
1.119817 +}
1.119818 +
1.119819 +/*
1.119820 +** This function retreives the doclist for the specified term (or term
1.119821 +** prefix) from the database.
1.119822 +*/
1.119823 +static int fts3TermSelect(
1.119824 +  Fts3Table *p,                   /* Virtual table handle */
1.119825 +  Fts3PhraseToken *pTok,          /* Token to query for */
1.119826 +  int iColumn,                    /* Column to query (or -ve for all columns) */
1.119827 +  int *pnOut,                     /* OUT: Size of buffer at *ppOut */
1.119828 +  char **ppOut                    /* OUT: Malloced result buffer */
1.119829 +){
1.119830 +  int rc;                         /* Return code */
1.119831 +  Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
1.119832 +  TermSelect tsc;                 /* Object for pair-wise doclist merging */
1.119833 +  Fts3SegFilter filter;           /* Segment term filter configuration */
1.119834 +
1.119835 +  pSegcsr = pTok->pSegcsr;
1.119836 +  memset(&tsc, 0, sizeof(TermSelect));
1.119837 +
1.119838 +  filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
1.119839 +        | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
1.119840 +        | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
1.119841 +        | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
1.119842 +  filter.iCol = iColumn;
1.119843 +  filter.zTerm = pTok->z;
1.119844 +  filter.nTerm = pTok->n;
1.119845 +
1.119846 +  rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
1.119847 +  while( SQLITE_OK==rc
1.119848 +      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) 
1.119849 +  ){
1.119850 +    rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
1.119851 +  }
1.119852 +
1.119853 +  if( rc==SQLITE_OK ){
1.119854 +    rc = fts3TermSelectFinishMerge(p, &tsc);
1.119855 +  }
1.119856 +  if( rc==SQLITE_OK ){
1.119857 +    *ppOut = tsc.aaOutput[0];
1.119858 +    *pnOut = tsc.anOutput[0];
1.119859 +  }else{
1.119860 +    int i;
1.119861 +    for(i=0; i<SizeofArray(tsc.aaOutput); i++){
1.119862 +      sqlite3_free(tsc.aaOutput[i]);
1.119863 +    }
1.119864 +  }
1.119865 +
1.119866 +  fts3SegReaderCursorFree(pSegcsr);
1.119867 +  pTok->pSegcsr = 0;
1.119868 +  return rc;
1.119869 +}
1.119870 +
1.119871 +/*
1.119872 +** This function counts the total number of docids in the doclist stored
1.119873 +** in buffer aList[], size nList bytes.
1.119874 +**
1.119875 +** If the isPoslist argument is true, then it is assumed that the doclist
1.119876 +** contains a position-list following each docid. Otherwise, it is assumed
1.119877 +** that the doclist is simply a list of docids stored as delta encoded 
1.119878 +** varints.
1.119879 +*/
1.119880 +static int fts3DoclistCountDocids(char *aList, int nList){
1.119881 +  int nDoc = 0;                   /* Return value */
1.119882 +  if( aList ){
1.119883 +    char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
1.119884 +    char *p = aList;              /* Cursor */
1.119885 +    while( p<aEnd ){
1.119886 +      nDoc++;
1.119887 +      while( (*p++)&0x80 );     /* Skip docid varint */
1.119888 +      fts3PoslistCopy(0, &p);   /* Skip over position list */
1.119889 +    }
1.119890 +  }
1.119891 +
1.119892 +  return nDoc;
1.119893 +}
1.119894 +
1.119895 +/*
1.119896 +** Advance the cursor to the next row in the %_content table that
1.119897 +** matches the search criteria.  For a MATCH search, this will be
1.119898 +** the next row that matches. For a full-table scan, this will be
1.119899 +** simply the next row in the %_content table.  For a docid lookup,
1.119900 +** this routine simply sets the EOF flag.
1.119901 +**
1.119902 +** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
1.119903 +** even if we reach end-of-file.  The fts3EofMethod() will be called
1.119904 +** subsequently to determine whether or not an EOF was hit.
1.119905 +*/
1.119906 +static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
1.119907 +  int rc;
1.119908 +  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
1.119909 +  if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
1.119910 +    if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
1.119911 +      pCsr->isEof = 1;
1.119912 +      rc = sqlite3_reset(pCsr->pStmt);
1.119913 +    }else{
1.119914 +      pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
1.119915 +      rc = SQLITE_OK;
1.119916 +    }
1.119917 +  }else{
1.119918 +    rc = fts3EvalNext((Fts3Cursor *)pCursor);
1.119919 +  }
1.119920 +  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
1.119921 +  return rc;
1.119922 +}
1.119923 +
1.119924 +/*
1.119925 +** This is the xFilter interface for the virtual table.  See
1.119926 +** the virtual table xFilter method documentation for additional
1.119927 +** information.
1.119928 +**
1.119929 +** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
1.119930 +** the %_content table.
1.119931 +**
1.119932 +** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
1.119933 +** in the %_content table.
1.119934 +**
1.119935 +** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
1.119936 +** column on the left-hand side of the MATCH operator is column
1.119937 +** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
1.119938 +** side of the MATCH operator.
1.119939 +*/
1.119940 +static int fts3FilterMethod(
1.119941 +  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
1.119942 +  int idxNum,                     /* Strategy index */
1.119943 +  const char *idxStr,             /* Unused */
1.119944 +  int nVal,                       /* Number of elements in apVal */
1.119945 +  sqlite3_value **apVal           /* Arguments for the indexing scheme */
1.119946 +){
1.119947 +  int rc;
1.119948 +  char *zSql;                     /* SQL statement used to access %_content */
1.119949 +  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
1.119950 +  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
1.119951 +
1.119952 +  UNUSED_PARAMETER(idxStr);
1.119953 +  UNUSED_PARAMETER(nVal);
1.119954 +
1.119955 +  assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
1.119956 +  assert( nVal==0 || nVal==1 || nVal==2 );
1.119957 +  assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
1.119958 +  assert( p->pSegments==0 );
1.119959 +
1.119960 +  /* In case the cursor has been used before, clear it now. */
1.119961 +  sqlite3_finalize(pCsr->pStmt);
1.119962 +  sqlite3_free(pCsr->aDoclist);
1.119963 +  sqlite3Fts3ExprFree(pCsr->pExpr);
1.119964 +  memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
1.119965 +
1.119966 +  if( idxStr ){
1.119967 +    pCsr->bDesc = (idxStr[0]=='D');
1.119968 +  }else{
1.119969 +    pCsr->bDesc = p->bDescIdx;
1.119970 +  }
1.119971 +  pCsr->eSearch = (i16)idxNum;
1.119972 +
1.119973 +  if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
1.119974 +    int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
1.119975 +    const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
1.119976 +
1.119977 +    if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
1.119978 +      return SQLITE_NOMEM;
1.119979 +    }
1.119980 +
1.119981 +    pCsr->iLangid = 0;
1.119982 +    if( nVal==2 ) pCsr->iLangid = sqlite3_value_int(apVal[1]);
1.119983 +
1.119984 +    rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
1.119985 +        p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
1.119986 +    );
1.119987 +    if( rc!=SQLITE_OK ){
1.119988 +      if( rc==SQLITE_ERROR ){
1.119989 +        static const char *zErr = "malformed MATCH expression: [%s]";
1.119990 +        p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
1.119991 +      }
1.119992 +      return rc;
1.119993 +    }
1.119994 +
1.119995 +    rc = sqlite3Fts3ReadLock(p);
1.119996 +    if( rc!=SQLITE_OK ) return rc;
1.119997 +
1.119998 +    rc = fts3EvalStart(pCsr);
1.119999 +
1.120000 +    sqlite3Fts3SegmentsClose(p);
1.120001 +    if( rc!=SQLITE_OK ) return rc;
1.120002 +    pCsr->pNextId = pCsr->aDoclist;
1.120003 +    pCsr->iPrevId = 0;
1.120004 +  }
1.120005 +
1.120006 +  /* Compile a SELECT statement for this cursor. For a full-table-scan, the
1.120007 +  ** statement loops through all rows of the %_content table. For a
1.120008 +  ** full-text query or docid lookup, the statement retrieves a single
1.120009 +  ** row by docid.
1.120010 +  */
1.120011 +  if( idxNum==FTS3_FULLSCAN_SEARCH ){
1.120012 +    zSql = sqlite3_mprintf(
1.120013 +        "SELECT %s ORDER BY rowid %s",
1.120014 +        p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
1.120015 +    );
1.120016 +    if( zSql ){
1.120017 +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
1.120018 +      sqlite3_free(zSql);
1.120019 +    }else{
1.120020 +      rc = SQLITE_NOMEM;
1.120021 +    }
1.120022 +  }else if( idxNum==FTS3_DOCID_SEARCH ){
1.120023 +    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
1.120024 +    if( rc==SQLITE_OK ){
1.120025 +      rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
1.120026 +    }
1.120027 +  }
1.120028 +  if( rc!=SQLITE_OK ) return rc;
1.120029 +
1.120030 +  return fts3NextMethod(pCursor);
1.120031 +}
1.120032 +
1.120033 +/* 
1.120034 +** This is the xEof method of the virtual table. SQLite calls this 
1.120035 +** routine to find out if it has reached the end of a result set.
1.120036 +*/
1.120037 +static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
1.120038 +  return ((Fts3Cursor *)pCursor)->isEof;
1.120039 +}
1.120040 +
1.120041 +/* 
1.120042 +** This is the xRowid method. The SQLite core calls this routine to
1.120043 +** retrieve the rowid for the current row of the result set. fts3
1.120044 +** exposes %_content.docid as the rowid for the virtual table. The
1.120045 +** rowid should be written to *pRowid.
1.120046 +*/
1.120047 +static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
1.120048 +  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
1.120049 +  *pRowid = pCsr->iPrevId;
1.120050 +  return SQLITE_OK;
1.120051 +}
1.120052 +
1.120053 +/* 
1.120054 +** This is the xColumn method, called by SQLite to request a value from
1.120055 +** the row that the supplied cursor currently points to.
1.120056 +**
1.120057 +** If:
1.120058 +**
1.120059 +**   (iCol <  p->nColumn)   -> The value of the iCol'th user column.
1.120060 +**   (iCol == p->nColumn)   -> Magic column with the same name as the table.
1.120061 +**   (iCol == p->nColumn+1) -> Docid column
1.120062 +**   (iCol == p->nColumn+2) -> Langid column
1.120063 +*/
1.120064 +static int fts3ColumnMethod(
1.120065 +  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
1.120066 +  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
1.120067 +  int iCol                        /* Index of column to read value from */
1.120068 +){
1.120069 +  int rc = SQLITE_OK;             /* Return Code */
1.120070 +  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
1.120071 +  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
1.120072 +
1.120073 +  /* The column value supplied by SQLite must be in range. */
1.120074 +  assert( iCol>=0 && iCol<=p->nColumn+2 );
1.120075 +
1.120076 +  if( iCol==p->nColumn+1 ){
1.120077 +    /* This call is a request for the "docid" column. Since "docid" is an 
1.120078 +    ** alias for "rowid", use the xRowid() method to obtain the value.
1.120079 +    */
1.120080 +    sqlite3_result_int64(pCtx, pCsr->iPrevId);
1.120081 +  }else if( iCol==p->nColumn ){
1.120082 +    /* The extra column whose name is the same as the table.
1.120083 +    ** Return a blob which is a pointer to the cursor.  */
1.120084 +    sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
1.120085 +  }else if( iCol==p->nColumn+2 && pCsr->pExpr ){
1.120086 +    sqlite3_result_int64(pCtx, pCsr->iLangid);
1.120087 +  }else{
1.120088 +    /* The requested column is either a user column (one that contains 
1.120089 +    ** indexed data), or the language-id column.  */
1.120090 +    rc = fts3CursorSeek(0, pCsr);
1.120091 +
1.120092 +    if( rc==SQLITE_OK ){
1.120093 +      if( iCol==p->nColumn+2 ){
1.120094 +        int iLangid = 0;
1.120095 +        if( p->zLanguageid ){
1.120096 +          iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1);
1.120097 +        }
1.120098 +        sqlite3_result_int(pCtx, iLangid);
1.120099 +      }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
1.120100 +        sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
1.120101 +      }
1.120102 +    }
1.120103 +  }
1.120104 +
1.120105 +  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
1.120106 +  return rc;
1.120107 +}
1.120108 +
1.120109 +/* 
1.120110 +** This function is the implementation of the xUpdate callback used by 
1.120111 +** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
1.120112 +** inserted, updated or deleted.
1.120113 +*/
1.120114 +static int fts3UpdateMethod(
1.120115 +  sqlite3_vtab *pVtab,            /* Virtual table handle */
1.120116 +  int nArg,                       /* Size of argument array */
1.120117 +  sqlite3_value **apVal,          /* Array of arguments */
1.120118 +  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
1.120119 +){
1.120120 +  return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
1.120121 +}
1.120122 +
1.120123 +/*
1.120124 +** Implementation of xSync() method. Flush the contents of the pending-terms
1.120125 +** hash-table to the database.
1.120126 +*/
1.120127 +static int fts3SyncMethod(sqlite3_vtab *pVtab){
1.120128 +
1.120129 +  /* Following an incremental-merge operation, assuming that the input
1.120130 +  ** segments are not completely consumed (the usual case), they are updated
1.120131 +  ** in place to remove the entries that have already been merged. This
1.120132 +  ** involves updating the leaf block that contains the smallest unmerged
1.120133 +  ** entry and each block (if any) between the leaf and the root node. So
1.120134 +  ** if the height of the input segment b-trees is N, and input segments
1.120135 +  ** are merged eight at a time, updating the input segments at the end
1.120136 +  ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
1.120137 +  ** small - often between 0 and 2. So the overhead of the incremental
1.120138 +  ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
1.120139 +  ** dwarfing the actual productive work accomplished, the incremental merge
1.120140 +  ** is only attempted if it will write at least 64 leaf blocks. Hence
1.120141 +  ** nMinMerge.
1.120142 +  **
1.120143 +  ** Of course, updating the input segments also involves deleting a bunch
1.120144 +  ** of blocks from the segments table. But this is not considered overhead
1.120145 +  ** as it would also be required by a crisis-merge that used the same input 
1.120146 +  ** segments.
1.120147 +  */
1.120148 +  const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
1.120149 +
1.120150 +  Fts3Table *p = (Fts3Table*)pVtab;
1.120151 +  int rc = sqlite3Fts3PendingTermsFlush(p);
1.120152 +
1.120153 +  if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
1.120154 +    int mxLevel = 0;              /* Maximum relative level value in db */
1.120155 +    int A;                        /* Incr-merge parameter A */
1.120156 +
1.120157 +    rc = sqlite3Fts3MaxLevel(p, &mxLevel);
1.120158 +    assert( rc==SQLITE_OK || mxLevel==0 );
1.120159 +    A = p->nLeafAdd * mxLevel;
1.120160 +    A += (A/2);
1.120161 +    if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
1.120162 +  }
1.120163 +  sqlite3Fts3SegmentsClose(p);
1.120164 +  return rc;
1.120165 +}
1.120166 +
1.120167 +/*
1.120168 +** Implementation of xBegin() method. This is a no-op.
1.120169 +*/
1.120170 +static int fts3BeginMethod(sqlite3_vtab *pVtab){
1.120171 +  Fts3Table *p = (Fts3Table*)pVtab;
1.120172 +  UNUSED_PARAMETER(pVtab);
1.120173 +  assert( p->pSegments==0 );
1.120174 +  assert( p->nPendingData==0 );
1.120175 +  assert( p->inTransaction!=1 );
1.120176 +  TESTONLY( p->inTransaction = 1 );
1.120177 +  TESTONLY( p->mxSavepoint = -1; );
1.120178 +  p->nLeafAdd = 0;
1.120179 +  return SQLITE_OK;
1.120180 +}
1.120181 +
1.120182 +/*
1.120183 +** Implementation of xCommit() method. This is a no-op. The contents of
1.120184 +** the pending-terms hash-table have already been flushed into the database
1.120185 +** by fts3SyncMethod().
1.120186 +*/
1.120187 +static int fts3CommitMethod(sqlite3_vtab *pVtab){
1.120188 +  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
1.120189 +  UNUSED_PARAMETER(pVtab);
1.120190 +  assert( p->nPendingData==0 );
1.120191 +  assert( p->inTransaction!=0 );
1.120192 +  assert( p->pSegments==0 );
1.120193 +  TESTONLY( p->inTransaction = 0 );
1.120194 +  TESTONLY( p->mxSavepoint = -1; );
1.120195 +  return SQLITE_OK;
1.120196 +}
1.120197 +
1.120198 +/*
1.120199 +** Implementation of xRollback(). Discard the contents of the pending-terms
1.120200 +** hash-table. Any changes made to the database are reverted by SQLite.
1.120201 +*/
1.120202 +static int fts3RollbackMethod(sqlite3_vtab *pVtab){
1.120203 +  Fts3Table *p = (Fts3Table*)pVtab;
1.120204 +  sqlite3Fts3PendingTermsClear(p);
1.120205 +  assert( p->inTransaction!=0 );
1.120206 +  TESTONLY( p->inTransaction = 0 );
1.120207 +  TESTONLY( p->mxSavepoint = -1; );
1.120208 +  return SQLITE_OK;
1.120209 +}
1.120210 +
1.120211 +/*
1.120212 +** When called, *ppPoslist must point to the byte immediately following the
1.120213 +** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
1.120214 +** moves *ppPoslist so that it instead points to the first byte of the
1.120215 +** same position list.
1.120216 +*/
1.120217 +static void fts3ReversePoslist(char *pStart, char **ppPoslist){
1.120218 +  char *p = &(*ppPoslist)[-2];
1.120219 +  char c = 0;
1.120220 +
1.120221 +  while( p>pStart && (c=*p--)==0 );
1.120222 +  while( p>pStart && (*p & 0x80) | c ){ 
1.120223 +    c = *p--; 
1.120224 +  }
1.120225 +  if( p>pStart ){ p = &p[2]; }
1.120226 +  while( *p++&0x80 );
1.120227 +  *ppPoslist = p;
1.120228 +}
1.120229 +
1.120230 +/*
1.120231 +** Helper function used by the implementation of the overloaded snippet(),
1.120232 +** offsets() and optimize() SQL functions.
1.120233 +**
1.120234 +** If the value passed as the third argument is a blob of size
1.120235 +** sizeof(Fts3Cursor*), then the blob contents are copied to the 
1.120236 +** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
1.120237 +** message is written to context pContext and SQLITE_ERROR returned. The
1.120238 +** string passed via zFunc is used as part of the error message.
1.120239 +*/
1.120240 +static int fts3FunctionArg(
1.120241 +  sqlite3_context *pContext,      /* SQL function call context */
1.120242 +  const char *zFunc,              /* Function name */
1.120243 +  sqlite3_value *pVal,            /* argv[0] passed to function */
1.120244 +  Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
1.120245 +){
1.120246 +  Fts3Cursor *pRet;
1.120247 +  if( sqlite3_value_type(pVal)!=SQLITE_BLOB 
1.120248 +   || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
1.120249 +  ){
1.120250 +    char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
1.120251 +    sqlite3_result_error(pContext, zErr, -1);
1.120252 +    sqlite3_free(zErr);
1.120253 +    return SQLITE_ERROR;
1.120254 +  }
1.120255 +  memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
1.120256 +  *ppCsr = pRet;
1.120257 +  return SQLITE_OK;
1.120258 +}
1.120259 +
1.120260 +/*
1.120261 +** Implementation of the snippet() function for FTS3
1.120262 +*/
1.120263 +static void fts3SnippetFunc(
1.120264 +  sqlite3_context *pContext,      /* SQLite function call context */
1.120265 +  int nVal,                       /* Size of apVal[] array */
1.120266 +  sqlite3_value **apVal           /* Array of arguments */
1.120267 +){
1.120268 +  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
1.120269 +  const char *zStart = "<b>";
1.120270 +  const char *zEnd = "</b>";
1.120271 +  const char *zEllipsis = "<b>...</b>";
1.120272 +  int iCol = -1;
1.120273 +  int nToken = 15;                /* Default number of tokens in snippet */
1.120274 +
1.120275 +  /* There must be at least one argument passed to this function (otherwise
1.120276 +  ** the non-overloaded version would have been called instead of this one).
1.120277 +  */
1.120278 +  assert( nVal>=1 );
1.120279 +
1.120280 +  if( nVal>6 ){
1.120281 +    sqlite3_result_error(pContext, 
1.120282 +        "wrong number of arguments to function snippet()", -1);
1.120283 +    return;
1.120284 +  }
1.120285 +  if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
1.120286 +
1.120287 +  switch( nVal ){
1.120288 +    case 6: nToken = sqlite3_value_int(apVal[5]);
1.120289 +    case 5: iCol = sqlite3_value_int(apVal[4]);
1.120290 +    case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
1.120291 +    case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
1.120292 +    case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
1.120293 +  }
1.120294 +  if( !zEllipsis || !zEnd || !zStart ){
1.120295 +    sqlite3_result_error_nomem(pContext);
1.120296 +  }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
1.120297 +    sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
1.120298 +  }
1.120299 +}
1.120300 +
1.120301 +/*
1.120302 +** Implementation of the offsets() function for FTS3
1.120303 +*/
1.120304 +static void fts3OffsetsFunc(
1.120305 +  sqlite3_context *pContext,      /* SQLite function call context */
1.120306 +  int nVal,                       /* Size of argument array */
1.120307 +  sqlite3_value **apVal           /* Array of arguments */
1.120308 +){
1.120309 +  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
1.120310 +
1.120311 +  UNUSED_PARAMETER(nVal);
1.120312 +
1.120313 +  assert( nVal==1 );
1.120314 +  if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
1.120315 +  assert( pCsr );
1.120316 +  if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
1.120317 +    sqlite3Fts3Offsets(pContext, pCsr);
1.120318 +  }
1.120319 +}
1.120320 +
1.120321 +/* 
1.120322 +** Implementation of the special optimize() function for FTS3. This 
1.120323 +** function merges all segments in the database to a single segment.
1.120324 +** Example usage is:
1.120325 +**
1.120326 +**   SELECT optimize(t) FROM t LIMIT 1;
1.120327 +**
1.120328 +** where 't' is the name of an FTS3 table.
1.120329 +*/
1.120330 +static void fts3OptimizeFunc(
1.120331 +  sqlite3_context *pContext,      /* SQLite function call context */
1.120332 +  int nVal,                       /* Size of argument array */
1.120333 +  sqlite3_value **apVal           /* Array of arguments */
1.120334 +){
1.120335 +  int rc;                         /* Return code */
1.120336 +  Fts3Table *p;                   /* Virtual table handle */
1.120337 +  Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
1.120338 +
1.120339 +  UNUSED_PARAMETER(nVal);
1.120340 +
1.120341 +  assert( nVal==1 );
1.120342 +  if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
1.120343 +  p = (Fts3Table *)pCursor->base.pVtab;
1.120344 +  assert( p );
1.120345 +
1.120346 +  rc = sqlite3Fts3Optimize(p);
1.120347 +
1.120348 +  switch( rc ){
1.120349 +    case SQLITE_OK:
1.120350 +      sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
1.120351 +      break;
1.120352 +    case SQLITE_DONE:
1.120353 +      sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
1.120354 +      break;
1.120355 +    default:
1.120356 +      sqlite3_result_error_code(pContext, rc);
1.120357 +      break;
1.120358 +  }
1.120359 +}
1.120360 +
1.120361 +/*
1.120362 +** Implementation of the matchinfo() function for FTS3
1.120363 +*/
1.120364 +static void fts3MatchinfoFunc(
1.120365 +  sqlite3_context *pContext,      /* SQLite function call context */
1.120366 +  int nVal,                       /* Size of argument array */
1.120367 +  sqlite3_value **apVal           /* Array of arguments */
1.120368 +){
1.120369 +  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
1.120370 +  assert( nVal==1 || nVal==2 );
1.120371 +  if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
1.120372 +    const char *zArg = 0;
1.120373 +    if( nVal>1 ){
1.120374 +      zArg = (const char *)sqlite3_value_text(apVal[1]);
1.120375 +    }
1.120376 +    sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
1.120377 +  }
1.120378 +}
1.120379 +
1.120380 +/*
1.120381 +** This routine implements the xFindFunction method for the FTS3
1.120382 +** virtual table.
1.120383 +*/
1.120384 +static int fts3FindFunctionMethod(
1.120385 +  sqlite3_vtab *pVtab,            /* Virtual table handle */
1.120386 +  int nArg,                       /* Number of SQL function arguments */
1.120387 +  const char *zName,              /* Name of SQL function */
1.120388 +  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
1.120389 +  void **ppArg                    /* Unused */
1.120390 +){
1.120391 +  struct Overloaded {
1.120392 +    const char *zName;
1.120393 +    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1.120394 +  } aOverload[] = {
1.120395 +    { "snippet", fts3SnippetFunc },
1.120396 +    { "offsets", fts3OffsetsFunc },
1.120397 +    { "optimize", fts3OptimizeFunc },
1.120398 +    { "matchinfo", fts3MatchinfoFunc },
1.120399 +  };
1.120400 +  int i;                          /* Iterator variable */
1.120401 +
1.120402 +  UNUSED_PARAMETER(pVtab);
1.120403 +  UNUSED_PARAMETER(nArg);
1.120404 +  UNUSED_PARAMETER(ppArg);
1.120405 +
1.120406 +  for(i=0; i<SizeofArray(aOverload); i++){
1.120407 +    if( strcmp(zName, aOverload[i].zName)==0 ){
1.120408 +      *pxFunc = aOverload[i].xFunc;
1.120409 +      return 1;
1.120410 +    }
1.120411 +  }
1.120412 +
1.120413 +  /* No function of the specified name was found. Return 0. */
1.120414 +  return 0;
1.120415 +}
1.120416 +
1.120417 +/*
1.120418 +** Implementation of FTS3 xRename method. Rename an fts3 table.
1.120419 +*/
1.120420 +static int fts3RenameMethod(
1.120421 +  sqlite3_vtab *pVtab,            /* Virtual table handle */
1.120422 +  const char *zName               /* New name of table */
1.120423 +){
1.120424 +  Fts3Table *p = (Fts3Table *)pVtab;
1.120425 +  sqlite3 *db = p->db;            /* Database connection */
1.120426 +  int rc;                         /* Return Code */
1.120427 +
1.120428 +  /* As it happens, the pending terms table is always empty here. This is
1.120429 +  ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction 
1.120430 +  ** always opens a savepoint transaction. And the xSavepoint() method 
1.120431 +  ** flushes the pending terms table. But leave the (no-op) call to
1.120432 +  ** PendingTermsFlush() in in case that changes.
1.120433 +  */
1.120434 +  assert( p->nPendingData==0 );
1.120435 +  rc = sqlite3Fts3PendingTermsFlush(p);
1.120436 +
1.120437 +  if( p->zContentTbl==0 ){
1.120438 +    fts3DbExec(&rc, db,
1.120439 +      "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
1.120440 +      p->zDb, p->zName, zName
1.120441 +    );
1.120442 +  }
1.120443 +
1.120444 +  if( p->bHasDocsize ){
1.120445 +    fts3DbExec(&rc, db,
1.120446 +      "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
1.120447 +      p->zDb, p->zName, zName
1.120448 +    );
1.120449 +  }
1.120450 +  if( p->bHasStat ){
1.120451 +    fts3DbExec(&rc, db,
1.120452 +      "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
1.120453 +      p->zDb, p->zName, zName
1.120454 +    );
1.120455 +  }
1.120456 +  fts3DbExec(&rc, db,
1.120457 +    "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
1.120458 +    p->zDb, p->zName, zName
1.120459 +  );
1.120460 +  fts3DbExec(&rc, db,
1.120461 +    "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
1.120462 +    p->zDb, p->zName, zName
1.120463 +  );
1.120464 +  return rc;
1.120465 +}
1.120466 +
1.120467 +/*
1.120468 +** The xSavepoint() method.
1.120469 +**
1.120470 +** Flush the contents of the pending-terms table to disk.
1.120471 +*/
1.120472 +static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
1.120473 +  int rc = SQLITE_OK;
1.120474 +  UNUSED_PARAMETER(iSavepoint);
1.120475 +  assert( ((Fts3Table *)pVtab)->inTransaction );
1.120476 +  assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
1.120477 +  TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
1.120478 +  if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
1.120479 +    rc = fts3SyncMethod(pVtab);
1.120480 +  }
1.120481 +  return rc;
1.120482 +}
1.120483 +
1.120484 +/*
1.120485 +** The xRelease() method.
1.120486 +**
1.120487 +** This is a no-op.
1.120488 +*/
1.120489 +static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
1.120490 +  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
1.120491 +  UNUSED_PARAMETER(iSavepoint);
1.120492 +  UNUSED_PARAMETER(pVtab);
1.120493 +  assert( p->inTransaction );
1.120494 +  assert( p->mxSavepoint >= iSavepoint );
1.120495 +  TESTONLY( p->mxSavepoint = iSavepoint-1 );
1.120496 +  return SQLITE_OK;
1.120497 +}
1.120498 +
1.120499 +/*
1.120500 +** The xRollbackTo() method.
1.120501 +**
1.120502 +** Discard the contents of the pending terms table.
1.120503 +*/
1.120504 +static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
1.120505 +  Fts3Table *p = (Fts3Table*)pVtab;
1.120506 +  UNUSED_PARAMETER(iSavepoint);
1.120507 +  assert( p->inTransaction );
1.120508 +  assert( p->mxSavepoint >= iSavepoint );
1.120509 +  TESTONLY( p->mxSavepoint = iSavepoint );
1.120510 +  sqlite3Fts3PendingTermsClear(p);
1.120511 +  return SQLITE_OK;
1.120512 +}
1.120513 +
1.120514 +static const sqlite3_module fts3Module = {
1.120515 +  /* iVersion      */ 2,
1.120516 +  /* xCreate       */ fts3CreateMethod,
1.120517 +  /* xConnect      */ fts3ConnectMethod,
1.120518 +  /* xBestIndex    */ fts3BestIndexMethod,
1.120519 +  /* xDisconnect   */ fts3DisconnectMethod,
1.120520 +  /* xDestroy      */ fts3DestroyMethod,
1.120521 +  /* xOpen         */ fts3OpenMethod,
1.120522 +  /* xClose        */ fts3CloseMethod,
1.120523 +  /* xFilter       */ fts3FilterMethod,
1.120524 +  /* xNext         */ fts3NextMethod,
1.120525 +  /* xEof          */ fts3EofMethod,
1.120526 +  /* xColumn       */ fts3ColumnMethod,
1.120527 +  /* xRowid        */ fts3RowidMethod,
1.120528 +  /* xUpdate       */ fts3UpdateMethod,
1.120529 +  /* xBegin        */ fts3BeginMethod,
1.120530 +  /* xSync         */ fts3SyncMethod,
1.120531 +  /* xCommit       */ fts3CommitMethod,
1.120532 +  /* xRollback     */ fts3RollbackMethod,
1.120533 +  /* xFindFunction */ fts3FindFunctionMethod,
1.120534 +  /* xRename */       fts3RenameMethod,
1.120535 +  /* xSavepoint    */ fts3SavepointMethod,
1.120536 +  /* xRelease      */ fts3ReleaseMethod,
1.120537 +  /* xRollbackTo   */ fts3RollbackToMethod,
1.120538 +};
1.120539 +
1.120540 +/*
1.120541 +** This function is registered as the module destructor (called when an
1.120542 +** FTS3 enabled database connection is closed). It frees the memory
1.120543 +** allocated for the tokenizer hash table.
1.120544 +*/
1.120545 +static void hashDestroy(void *p){
1.120546 +  Fts3Hash *pHash = (Fts3Hash *)p;
1.120547 +  sqlite3Fts3HashClear(pHash);
1.120548 +  sqlite3_free(pHash);
1.120549 +}
1.120550 +
1.120551 +/*
1.120552 +** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are 
1.120553 +** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
1.120554 +** respectively. The following three forward declarations are for functions
1.120555 +** declared in these files used to retrieve the respective implementations.
1.120556 +**
1.120557 +** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
1.120558 +** to by the argument to point to the "simple" tokenizer implementation.
1.120559 +** And so on.
1.120560 +*/
1.120561 +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
1.120562 +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
1.120563 +#ifdef SQLITE_ENABLE_FTS4_UNICODE61
1.120564 +SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
1.120565 +#endif
1.120566 +#ifdef SQLITE_ENABLE_ICU
1.120567 +SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
1.120568 +#endif
1.120569 +
1.120570 +/*
1.120571 +** Initialise the fts3 extension. If this extension is built as part
1.120572 +** of the sqlite library, then this function is called directly by
1.120573 +** SQLite. If fts3 is built as a dynamically loadable extension, this
1.120574 +** function is called by the sqlite3_extension_init() entry point.
1.120575 +*/
1.120576 +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
1.120577 +  int rc = SQLITE_OK;
1.120578 +  Fts3Hash *pHash = 0;
1.120579 +  const sqlite3_tokenizer_module *pSimple = 0;
1.120580 +  const sqlite3_tokenizer_module *pPorter = 0;
1.120581 +#ifdef SQLITE_ENABLE_FTS4_UNICODE61
1.120582 +  const sqlite3_tokenizer_module *pUnicode = 0;
1.120583 +#endif
1.120584 +
1.120585 +#ifdef SQLITE_ENABLE_ICU
1.120586 +  const sqlite3_tokenizer_module *pIcu = 0;
1.120587 +  sqlite3Fts3IcuTokenizerModule(&pIcu);
1.120588 +#endif
1.120589 +
1.120590 +#ifdef SQLITE_ENABLE_FTS4_UNICODE61
1.120591 +  sqlite3Fts3UnicodeTokenizer(&pUnicode);
1.120592 +#endif
1.120593 +
1.120594 +#ifdef SQLITE_TEST
1.120595 +  rc = sqlite3Fts3InitTerm(db);
1.120596 +  if( rc!=SQLITE_OK ) return rc;
1.120597 +#endif
1.120598 +
1.120599 +  rc = sqlite3Fts3InitAux(db);
1.120600 +  if( rc!=SQLITE_OK ) return rc;
1.120601 +
1.120602 +  sqlite3Fts3SimpleTokenizerModule(&pSimple);
1.120603 +  sqlite3Fts3PorterTokenizerModule(&pPorter);
1.120604 +
1.120605 +  /* Allocate and initialise the hash-table used to store tokenizers. */
1.120606 +  pHash = sqlite3_malloc(sizeof(Fts3Hash));
1.120607 +  if( !pHash ){
1.120608 +    rc = SQLITE_NOMEM;
1.120609 +  }else{
1.120610 +    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
1.120611 +  }
1.120612 +
1.120613 +  /* Load the built-in tokenizers into the hash table */
1.120614 +  if( rc==SQLITE_OK ){
1.120615 +    if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
1.120616 +     || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) 
1.120617 +
1.120618 +#ifdef SQLITE_ENABLE_FTS4_UNICODE61
1.120619 +     || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) 
1.120620 +#endif
1.120621 +#ifdef SQLITE_ENABLE_ICU
1.120622 +     || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
1.120623 +#endif
1.120624 +    ){
1.120625 +      rc = SQLITE_NOMEM;
1.120626 +    }
1.120627 +  }
1.120628 +
1.120629 +#ifdef SQLITE_TEST
1.120630 +  if( rc==SQLITE_OK ){
1.120631 +    rc = sqlite3Fts3ExprInitTestInterface(db);
1.120632 +  }
1.120633 +#endif
1.120634 +
1.120635 +  /* Create the virtual table wrapper around the hash-table and overload 
1.120636 +  ** the two scalar functions. If this is successful, register the
1.120637 +  ** module with sqlite.
1.120638 +  */
1.120639 +  if( SQLITE_OK==rc 
1.120640 +   && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
1.120641 +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
1.120642 +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
1.120643 +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
1.120644 +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
1.120645 +   && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
1.120646 +  ){
1.120647 +    rc = sqlite3_create_module_v2(
1.120648 +        db, "fts3", &fts3Module, (void *)pHash, hashDestroy
1.120649 +    );
1.120650 +    if( rc==SQLITE_OK ){
1.120651 +      rc = sqlite3_create_module_v2(
1.120652 +          db, "fts4", &fts3Module, (void *)pHash, 0
1.120653 +      );
1.120654 +    }
1.120655 +    return rc;
1.120656 +  }
1.120657 +
1.120658 +  /* An error has occurred. Delete the hash table and return the error code. */
1.120659 +  assert( rc!=SQLITE_OK );
1.120660 +  if( pHash ){
1.120661 +    sqlite3Fts3HashClear(pHash);
1.120662 +    sqlite3_free(pHash);
1.120663 +  }
1.120664 +  return rc;
1.120665 +}
1.120666 +
1.120667 +/*
1.120668 +** Allocate an Fts3MultiSegReader for each token in the expression headed
1.120669 +** by pExpr. 
1.120670 +**
1.120671 +** An Fts3SegReader object is a cursor that can seek or scan a range of
1.120672 +** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
1.120673 +** Fts3SegReader objects internally to provide an interface to seek or scan
1.120674 +** within the union of all segments of a b-tree. Hence the name.
1.120675 +**
1.120676 +** If the allocated Fts3MultiSegReader just seeks to a single entry in a
1.120677 +** segment b-tree (if the term is not a prefix or it is a prefix for which
1.120678 +** there exists prefix b-tree of the right length) then it may be traversed
1.120679 +** and merged incrementally. Otherwise, it has to be merged into an in-memory 
1.120680 +** doclist and then traversed.
1.120681 +*/
1.120682 +static void fts3EvalAllocateReaders(
1.120683 +  Fts3Cursor *pCsr,               /* FTS cursor handle */
1.120684 +  Fts3Expr *pExpr,                /* Allocate readers for this expression */
1.120685 +  int *pnToken,                   /* OUT: Total number of tokens in phrase. */
1.120686 +  int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
1.120687 +  int *pRc                        /* IN/OUT: Error code */
1.120688 +){
1.120689 +  if( pExpr && SQLITE_OK==*pRc ){
1.120690 +    if( pExpr->eType==FTSQUERY_PHRASE ){
1.120691 +      int i;
1.120692 +      int nToken = pExpr->pPhrase->nToken;
1.120693 +      *pnToken += nToken;
1.120694 +      for(i=0; i<nToken; i++){
1.120695 +        Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
1.120696 +        int rc = fts3TermSegReaderCursor(pCsr, 
1.120697 +            pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
1.120698 +        );
1.120699 +        if( rc!=SQLITE_OK ){
1.120700 +          *pRc = rc;
1.120701 +          return;
1.120702 +        }
1.120703 +      }
1.120704 +      assert( pExpr->pPhrase->iDoclistToken==0 );
1.120705 +      pExpr->pPhrase->iDoclistToken = -1;
1.120706 +    }else{
1.120707 +      *pnOr += (pExpr->eType==FTSQUERY_OR);
1.120708 +      fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
1.120709 +      fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
1.120710 +    }
1.120711 +  }
1.120712 +}
1.120713 +
1.120714 +/*
1.120715 +** Arguments pList/nList contain the doclist for token iToken of phrase p.
1.120716 +** It is merged into the main doclist stored in p->doclist.aAll/nAll.
1.120717 +**
1.120718 +** This function assumes that pList points to a buffer allocated using
1.120719 +** sqlite3_malloc(). This function takes responsibility for eventually
1.120720 +** freeing the buffer.
1.120721 +*/
1.120722 +static void fts3EvalPhraseMergeToken(
1.120723 +  Fts3Table *pTab,                /* FTS Table pointer */
1.120724 +  Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
1.120725 +  int iToken,                     /* Token pList/nList corresponds to */
1.120726 +  char *pList,                    /* Pointer to doclist */
1.120727 +  int nList                       /* Number of bytes in pList */
1.120728 +){
1.120729 +  assert( iToken!=p->iDoclistToken );
1.120730 +
1.120731 +  if( pList==0 ){
1.120732 +    sqlite3_free(p->doclist.aAll);
1.120733 +    p->doclist.aAll = 0;
1.120734 +    p->doclist.nAll = 0;
1.120735 +  }
1.120736 +
1.120737 +  else if( p->iDoclistToken<0 ){
1.120738 +    p->doclist.aAll = pList;
1.120739 +    p->doclist.nAll = nList;
1.120740 +  }
1.120741 +
1.120742 +  else if( p->doclist.aAll==0 ){
1.120743 +    sqlite3_free(pList);
1.120744 +  }
1.120745 +
1.120746 +  else {
1.120747 +    char *pLeft;
1.120748 +    char *pRight;
1.120749 +    int nLeft;
1.120750 +    int nRight;
1.120751 +    int nDiff;
1.120752 +
1.120753 +    if( p->iDoclistToken<iToken ){
1.120754 +      pLeft = p->doclist.aAll;
1.120755 +      nLeft = p->doclist.nAll;
1.120756 +      pRight = pList;
1.120757 +      nRight = nList;
1.120758 +      nDiff = iToken - p->iDoclistToken;
1.120759 +    }else{
1.120760 +      pRight = p->doclist.aAll;
1.120761 +      nRight = p->doclist.nAll;
1.120762 +      pLeft = pList;
1.120763 +      nLeft = nList;
1.120764 +      nDiff = p->iDoclistToken - iToken;
1.120765 +    }
1.120766 +
1.120767 +    fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
1.120768 +    sqlite3_free(pLeft);
1.120769 +    p->doclist.aAll = pRight;
1.120770 +    p->doclist.nAll = nRight;
1.120771 +  }
1.120772 +
1.120773 +  if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
1.120774 +}
1.120775 +
1.120776 +/*
1.120777 +** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
1.120778 +** does not take deferred tokens into account.
1.120779 +**
1.120780 +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
1.120781 +*/
1.120782 +static int fts3EvalPhraseLoad(
1.120783 +  Fts3Cursor *pCsr,               /* FTS Cursor handle */
1.120784 +  Fts3Phrase *p                   /* Phrase object */
1.120785 +){
1.120786 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.120787 +  int iToken;
1.120788 +  int rc = SQLITE_OK;
1.120789 +
1.120790 +  for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
1.120791 +    Fts3PhraseToken *pToken = &p->aToken[iToken];
1.120792 +    assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
1.120793 +
1.120794 +    if( pToken->pSegcsr ){
1.120795 +      int nThis = 0;
1.120796 +      char *pThis = 0;
1.120797 +      rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
1.120798 +      if( rc==SQLITE_OK ){
1.120799 +        fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
1.120800 +      }
1.120801 +    }
1.120802 +    assert( pToken->pSegcsr==0 );
1.120803 +  }
1.120804 +
1.120805 +  return rc;
1.120806 +}
1.120807 +
1.120808 +/*
1.120809 +** This function is called on each phrase after the position lists for
1.120810 +** any deferred tokens have been loaded into memory. It updates the phrases
1.120811 +** current position list to include only those positions that are really
1.120812 +** instances of the phrase (after considering deferred tokens). If this
1.120813 +** means that the phrase does not appear in the current row, doclist.pList
1.120814 +** and doclist.nList are both zeroed.
1.120815 +**
1.120816 +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
1.120817 +*/
1.120818 +static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
1.120819 +  int iToken;                     /* Used to iterate through phrase tokens */
1.120820 +  char *aPoslist = 0;             /* Position list for deferred tokens */
1.120821 +  int nPoslist = 0;               /* Number of bytes in aPoslist */
1.120822 +  int iPrev = -1;                 /* Token number of previous deferred token */
1.120823 +
1.120824 +  assert( pPhrase->doclist.bFreeList==0 );
1.120825 +
1.120826 +  for(iToken=0; iToken<pPhrase->nToken; iToken++){
1.120827 +    Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
1.120828 +    Fts3DeferredToken *pDeferred = pToken->pDeferred;
1.120829 +
1.120830 +    if( pDeferred ){
1.120831 +      char *pList;
1.120832 +      int nList;
1.120833 +      int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
1.120834 +      if( rc!=SQLITE_OK ) return rc;
1.120835 +
1.120836 +      if( pList==0 ){
1.120837 +        sqlite3_free(aPoslist);
1.120838 +        pPhrase->doclist.pList = 0;
1.120839 +        pPhrase->doclist.nList = 0;
1.120840 +        return SQLITE_OK;
1.120841 +
1.120842 +      }else if( aPoslist==0 ){
1.120843 +        aPoslist = pList;
1.120844 +        nPoslist = nList;
1.120845 +
1.120846 +      }else{
1.120847 +        char *aOut = pList;
1.120848 +        char *p1 = aPoslist;
1.120849 +        char *p2 = aOut;
1.120850 +
1.120851 +        assert( iPrev>=0 );
1.120852 +        fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
1.120853 +        sqlite3_free(aPoslist);
1.120854 +        aPoslist = pList;
1.120855 +        nPoslist = (int)(aOut - aPoslist);
1.120856 +        if( nPoslist==0 ){
1.120857 +          sqlite3_free(aPoslist);
1.120858 +          pPhrase->doclist.pList = 0;
1.120859 +          pPhrase->doclist.nList = 0;
1.120860 +          return SQLITE_OK;
1.120861 +        }
1.120862 +      }
1.120863 +      iPrev = iToken;
1.120864 +    }
1.120865 +  }
1.120866 +
1.120867 +  if( iPrev>=0 ){
1.120868 +    int nMaxUndeferred = pPhrase->iDoclistToken;
1.120869 +    if( nMaxUndeferred<0 ){
1.120870 +      pPhrase->doclist.pList = aPoslist;
1.120871 +      pPhrase->doclist.nList = nPoslist;
1.120872 +      pPhrase->doclist.iDocid = pCsr->iPrevId;
1.120873 +      pPhrase->doclist.bFreeList = 1;
1.120874 +    }else{
1.120875 +      int nDistance;
1.120876 +      char *p1;
1.120877 +      char *p2;
1.120878 +      char *aOut;
1.120879 +
1.120880 +      if( nMaxUndeferred>iPrev ){
1.120881 +        p1 = aPoslist;
1.120882 +        p2 = pPhrase->doclist.pList;
1.120883 +        nDistance = nMaxUndeferred - iPrev;
1.120884 +      }else{
1.120885 +        p1 = pPhrase->doclist.pList;
1.120886 +        p2 = aPoslist;
1.120887 +        nDistance = iPrev - nMaxUndeferred;
1.120888 +      }
1.120889 +
1.120890 +      aOut = (char *)sqlite3_malloc(nPoslist+8);
1.120891 +      if( !aOut ){
1.120892 +        sqlite3_free(aPoslist);
1.120893 +        return SQLITE_NOMEM;
1.120894 +      }
1.120895 +      
1.120896 +      pPhrase->doclist.pList = aOut;
1.120897 +      if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
1.120898 +        pPhrase->doclist.bFreeList = 1;
1.120899 +        pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
1.120900 +      }else{
1.120901 +        sqlite3_free(aOut);
1.120902 +        pPhrase->doclist.pList = 0;
1.120903 +        pPhrase->doclist.nList = 0;
1.120904 +      }
1.120905 +      sqlite3_free(aPoslist);
1.120906 +    }
1.120907 +  }
1.120908 +
1.120909 +  return SQLITE_OK;
1.120910 +}
1.120911 +
1.120912 +/*
1.120913 +** This function is called for each Fts3Phrase in a full-text query 
1.120914 +** expression to initialize the mechanism for returning rows. Once this
1.120915 +** function has been called successfully on an Fts3Phrase, it may be
1.120916 +** used with fts3EvalPhraseNext() to iterate through the matching docids.
1.120917 +**
1.120918 +** If parameter bOptOk is true, then the phrase may (or may not) use the
1.120919 +** incremental loading strategy. Otherwise, the entire doclist is loaded into
1.120920 +** memory within this call.
1.120921 +**
1.120922 +** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
1.120923 +*/
1.120924 +static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
1.120925 +  int rc;                         /* Error code */
1.120926 +  Fts3PhraseToken *pFirst = &p->aToken[0];
1.120927 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.120928 +
1.120929 +  if( pCsr->bDesc==pTab->bDescIdx 
1.120930 +   && bOptOk==1 
1.120931 +   && p->nToken==1 
1.120932 +   && pFirst->pSegcsr 
1.120933 +   && pFirst->pSegcsr->bLookup 
1.120934 +   && pFirst->bFirst==0
1.120935 +  ){
1.120936 +    /* Use the incremental approach. */
1.120937 +    int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
1.120938 +    rc = sqlite3Fts3MsrIncrStart(
1.120939 +        pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n);
1.120940 +    p->bIncr = 1;
1.120941 +
1.120942 +  }else{
1.120943 +    /* Load the full doclist for the phrase into memory. */
1.120944 +    rc = fts3EvalPhraseLoad(pCsr, p);
1.120945 +    p->bIncr = 0;
1.120946 +  }
1.120947 +
1.120948 +  assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
1.120949 +  return rc;
1.120950 +}
1.120951 +
1.120952 +/*
1.120953 +** This function is used to iterate backwards (from the end to start) 
1.120954 +** through doclists. It is used by this module to iterate through phrase
1.120955 +** doclists in reverse and by the fts3_write.c module to iterate through
1.120956 +** pending-terms lists when writing to databases with "order=desc".
1.120957 +**
1.120958 +** The doclist may be sorted in ascending (parameter bDescIdx==0) or 
1.120959 +** descending (parameter bDescIdx==1) order of docid. Regardless, this
1.120960 +** function iterates from the end of the doclist to the beginning.
1.120961 +*/
1.120962 +SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
1.120963 +  int bDescIdx,                   /* True if the doclist is desc */
1.120964 +  char *aDoclist,                 /* Pointer to entire doclist */
1.120965 +  int nDoclist,                   /* Length of aDoclist in bytes */
1.120966 +  char **ppIter,                  /* IN/OUT: Iterator pointer */
1.120967 +  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
1.120968 +  int *pnList,                    /* OUT: List length pointer */
1.120969 +  u8 *pbEof                       /* OUT: End-of-file flag */
1.120970 +){
1.120971 +  char *p = *ppIter;
1.120972 +
1.120973 +  assert( nDoclist>0 );
1.120974 +  assert( *pbEof==0 );
1.120975 +  assert( p || *piDocid==0 );
1.120976 +  assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
1.120977 +
1.120978 +  if( p==0 ){
1.120979 +    sqlite3_int64 iDocid = 0;
1.120980 +    char *pNext = 0;
1.120981 +    char *pDocid = aDoclist;
1.120982 +    char *pEnd = &aDoclist[nDoclist];
1.120983 +    int iMul = 1;
1.120984 +
1.120985 +    while( pDocid<pEnd ){
1.120986 +      sqlite3_int64 iDelta;
1.120987 +      pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
1.120988 +      iDocid += (iMul * iDelta);
1.120989 +      pNext = pDocid;
1.120990 +      fts3PoslistCopy(0, &pDocid);
1.120991 +      while( pDocid<pEnd && *pDocid==0 ) pDocid++;
1.120992 +      iMul = (bDescIdx ? -1 : 1);
1.120993 +    }
1.120994 +
1.120995 +    *pnList = (int)(pEnd - pNext);
1.120996 +    *ppIter = pNext;
1.120997 +    *piDocid = iDocid;
1.120998 +  }else{
1.120999 +    int iMul = (bDescIdx ? -1 : 1);
1.121000 +    sqlite3_int64 iDelta;
1.121001 +    fts3GetReverseVarint(&p, aDoclist, &iDelta);
1.121002 +    *piDocid -= (iMul * iDelta);
1.121003 +
1.121004 +    if( p==aDoclist ){
1.121005 +      *pbEof = 1;
1.121006 +    }else{
1.121007 +      char *pSave = p;
1.121008 +      fts3ReversePoslist(aDoclist, &p);
1.121009 +      *pnList = (int)(pSave - p);
1.121010 +    }
1.121011 +    *ppIter = p;
1.121012 +  }
1.121013 +}
1.121014 +
1.121015 +/*
1.121016 +** Iterate forwards through a doclist.
1.121017 +*/
1.121018 +SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
1.121019 +  int bDescIdx,                   /* True if the doclist is desc */
1.121020 +  char *aDoclist,                 /* Pointer to entire doclist */
1.121021 +  int nDoclist,                   /* Length of aDoclist in bytes */
1.121022 +  char **ppIter,                  /* IN/OUT: Iterator pointer */
1.121023 +  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
1.121024 +  u8 *pbEof                       /* OUT: End-of-file flag */
1.121025 +){
1.121026 +  char *p = *ppIter;
1.121027 +
1.121028 +  assert( nDoclist>0 );
1.121029 +  assert( *pbEof==0 );
1.121030 +  assert( p || *piDocid==0 );
1.121031 +  assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
1.121032 +
1.121033 +  if( p==0 ){
1.121034 +    p = aDoclist;
1.121035 +    p += sqlite3Fts3GetVarint(p, piDocid);
1.121036 +  }else{
1.121037 +    fts3PoslistCopy(0, &p);
1.121038 +    if( p>=&aDoclist[nDoclist] ){
1.121039 +      *pbEof = 1;
1.121040 +    }else{
1.121041 +      sqlite3_int64 iVar;
1.121042 +      p += sqlite3Fts3GetVarint(p, &iVar);
1.121043 +      *piDocid += ((bDescIdx ? -1 : 1) * iVar);
1.121044 +    }
1.121045 +  }
1.121046 +
1.121047 +  *ppIter = p;
1.121048 +}
1.121049 +
1.121050 +/*
1.121051 +** Attempt to move the phrase iterator to point to the next matching docid. 
1.121052 +** If an error occurs, return an SQLite error code. Otherwise, return 
1.121053 +** SQLITE_OK.
1.121054 +**
1.121055 +** If there is no "next" entry and no error occurs, then *pbEof is set to
1.121056 +** 1 before returning. Otherwise, if no error occurs and the iterator is
1.121057 +** successfully advanced, *pbEof is set to 0.
1.121058 +*/
1.121059 +static int fts3EvalPhraseNext(
1.121060 +  Fts3Cursor *pCsr,               /* FTS Cursor handle */
1.121061 +  Fts3Phrase *p,                  /* Phrase object to advance to next docid */
1.121062 +  u8 *pbEof                       /* OUT: Set to 1 if EOF */
1.121063 +){
1.121064 +  int rc = SQLITE_OK;
1.121065 +  Fts3Doclist *pDL = &p->doclist;
1.121066 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.121067 +
1.121068 +  if( p->bIncr ){
1.121069 +    assert( p->nToken==1 );
1.121070 +    assert( pDL->pNextDocid==0 );
1.121071 +    rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, 
1.121072 +        &pDL->iDocid, &pDL->pList, &pDL->nList
1.121073 +    );
1.121074 +    if( rc==SQLITE_OK && !pDL->pList ){
1.121075 +      *pbEof = 1;
1.121076 +    }
1.121077 +  }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
1.121078 +    sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll, 
1.121079 +        &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
1.121080 +    );
1.121081 +    pDL->pList = pDL->pNextDocid;
1.121082 +  }else{
1.121083 +    char *pIter;                            /* Used to iterate through aAll */
1.121084 +    char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
1.121085 +    if( pDL->pNextDocid ){
1.121086 +      pIter = pDL->pNextDocid;
1.121087 +    }else{
1.121088 +      pIter = pDL->aAll;
1.121089 +    }
1.121090 +
1.121091 +    if( pIter>=pEnd ){
1.121092 +      /* We have already reached the end of this doclist. EOF. */
1.121093 +      *pbEof = 1;
1.121094 +    }else{
1.121095 +      sqlite3_int64 iDelta;
1.121096 +      pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
1.121097 +      if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
1.121098 +        pDL->iDocid += iDelta;
1.121099 +      }else{
1.121100 +        pDL->iDocid -= iDelta;
1.121101 +      }
1.121102 +      pDL->pList = pIter;
1.121103 +      fts3PoslistCopy(0, &pIter);
1.121104 +      pDL->nList = (int)(pIter - pDL->pList);
1.121105 +
1.121106 +      /* pIter now points just past the 0x00 that terminates the position-
1.121107 +      ** list for document pDL->iDocid. However, if this position-list was
1.121108 +      ** edited in place by fts3EvalNearTrim(), then pIter may not actually
1.121109 +      ** point to the start of the next docid value. The following line deals
1.121110 +      ** with this case by advancing pIter past the zero-padding added by
1.121111 +      ** fts3EvalNearTrim().  */
1.121112 +      while( pIter<pEnd && *pIter==0 ) pIter++;
1.121113 +
1.121114 +      pDL->pNextDocid = pIter;
1.121115 +      assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
1.121116 +      *pbEof = 0;
1.121117 +    }
1.121118 +  }
1.121119 +
1.121120 +  return rc;
1.121121 +}
1.121122 +
1.121123 +/*
1.121124 +**
1.121125 +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
1.121126 +** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
1.121127 +** expression. Also the Fts3Expr.bDeferred variable is set to true for any
1.121128 +** expressions for which all descendent tokens are deferred.
1.121129 +**
1.121130 +** If parameter bOptOk is zero, then it is guaranteed that the
1.121131 +** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
1.121132 +** each phrase in the expression (subject to deferred token processing).
1.121133 +** Or, if bOptOk is non-zero, then one or more tokens within the expression
1.121134 +** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
1.121135 +**
1.121136 +** If an error occurs within this function, *pRc is set to an SQLite error
1.121137 +** code before returning.
1.121138 +*/
1.121139 +static void fts3EvalStartReaders(
1.121140 +  Fts3Cursor *pCsr,               /* FTS Cursor handle */
1.121141 +  Fts3Expr *pExpr,                /* Expression to initialize phrases in */
1.121142 +  int bOptOk,                     /* True to enable incremental loading */
1.121143 +  int *pRc                        /* IN/OUT: Error code */
1.121144 +){
1.121145 +  if( pExpr && SQLITE_OK==*pRc ){
1.121146 +    if( pExpr->eType==FTSQUERY_PHRASE ){
1.121147 +      int i;
1.121148 +      int nToken = pExpr->pPhrase->nToken;
1.121149 +      for(i=0; i<nToken; i++){
1.121150 +        if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
1.121151 +      }
1.121152 +      pExpr->bDeferred = (i==nToken);
1.121153 +      *pRc = fts3EvalPhraseStart(pCsr, bOptOk, pExpr->pPhrase);
1.121154 +    }else{
1.121155 +      fts3EvalStartReaders(pCsr, pExpr->pLeft, bOptOk, pRc);
1.121156 +      fts3EvalStartReaders(pCsr, pExpr->pRight, bOptOk, pRc);
1.121157 +      pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
1.121158 +    }
1.121159 +  }
1.121160 +}
1.121161 +
1.121162 +/*
1.121163 +** An array of the following structures is assembled as part of the process
1.121164 +** of selecting tokens to defer before the query starts executing (as part
1.121165 +** of the xFilter() method). There is one element in the array for each
1.121166 +** token in the FTS expression.
1.121167 +**
1.121168 +** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
1.121169 +** to phrases that are connected only by AND and NEAR operators (not OR or
1.121170 +** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
1.121171 +** separately. The root of a tokens AND/NEAR cluster is stored in 
1.121172 +** Fts3TokenAndCost.pRoot.
1.121173 +*/
1.121174 +typedef struct Fts3TokenAndCost Fts3TokenAndCost;
1.121175 +struct Fts3TokenAndCost {
1.121176 +  Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
1.121177 +  int iToken;                     /* Position of token in phrase */
1.121178 +  Fts3PhraseToken *pToken;        /* The token itself */
1.121179 +  Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
1.121180 +  int nOvfl;                      /* Number of overflow pages to load doclist */
1.121181 +  int iCol;                       /* The column the token must match */
1.121182 +};
1.121183 +
1.121184 +/*
1.121185 +** This function is used to populate an allocated Fts3TokenAndCost array.
1.121186 +**
1.121187 +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
1.121188 +** Otherwise, if an error occurs during execution, *pRc is set to an
1.121189 +** SQLite error code.
1.121190 +*/
1.121191 +static void fts3EvalTokenCosts(
1.121192 +  Fts3Cursor *pCsr,               /* FTS Cursor handle */
1.121193 +  Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
1.121194 +  Fts3Expr *pExpr,                /* Expression to consider */
1.121195 +  Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
1.121196 +  Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
1.121197 +  int *pRc                        /* IN/OUT: Error code */
1.121198 +){
1.121199 +  if( *pRc==SQLITE_OK ){
1.121200 +    if( pExpr->eType==FTSQUERY_PHRASE ){
1.121201 +      Fts3Phrase *pPhrase = pExpr->pPhrase;
1.121202 +      int i;
1.121203 +      for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
1.121204 +        Fts3TokenAndCost *pTC = (*ppTC)++;
1.121205 +        pTC->pPhrase = pPhrase;
1.121206 +        pTC->iToken = i;
1.121207 +        pTC->pRoot = pRoot;
1.121208 +        pTC->pToken = &pPhrase->aToken[i];
1.121209 +        pTC->iCol = pPhrase->iColumn;
1.121210 +        *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
1.121211 +      }
1.121212 +    }else if( pExpr->eType!=FTSQUERY_NOT ){
1.121213 +      assert( pExpr->eType==FTSQUERY_OR
1.121214 +           || pExpr->eType==FTSQUERY_AND
1.121215 +           || pExpr->eType==FTSQUERY_NEAR
1.121216 +      );
1.121217 +      assert( pExpr->pLeft && pExpr->pRight );
1.121218 +      if( pExpr->eType==FTSQUERY_OR ){
1.121219 +        pRoot = pExpr->pLeft;
1.121220 +        **ppOr = pRoot;
1.121221 +        (*ppOr)++;
1.121222 +      }
1.121223 +      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
1.121224 +      if( pExpr->eType==FTSQUERY_OR ){
1.121225 +        pRoot = pExpr->pRight;
1.121226 +        **ppOr = pRoot;
1.121227 +        (*ppOr)++;
1.121228 +      }
1.121229 +      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
1.121230 +    }
1.121231 +  }
1.121232 +}
1.121233 +
1.121234 +/*
1.121235 +** Determine the average document (row) size in pages. If successful,
1.121236 +** write this value to *pnPage and return SQLITE_OK. Otherwise, return
1.121237 +** an SQLite error code.
1.121238 +**
1.121239 +** The average document size in pages is calculated by first calculating 
1.121240 +** determining the average size in bytes, B. If B is less than the amount
1.121241 +** of data that will fit on a single leaf page of an intkey table in
1.121242 +** this database, then the average docsize is 1. Otherwise, it is 1 plus
1.121243 +** the number of overflow pages consumed by a record B bytes in size.
1.121244 +*/
1.121245 +static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
1.121246 +  if( pCsr->nRowAvg==0 ){
1.121247 +    /* The average document size, which is required to calculate the cost
1.121248 +    ** of each doclist, has not yet been determined. Read the required 
1.121249 +    ** data from the %_stat table to calculate it.
1.121250 +    **
1.121251 +    ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 
1.121252 +    ** varints, where nCol is the number of columns in the FTS3 table.
1.121253 +    ** The first varint is the number of documents currently stored in
1.121254 +    ** the table. The following nCol varints contain the total amount of
1.121255 +    ** data stored in all rows of each column of the table, from left
1.121256 +    ** to right.
1.121257 +    */
1.121258 +    int rc;
1.121259 +    Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
1.121260 +    sqlite3_stmt *pStmt;
1.121261 +    sqlite3_int64 nDoc = 0;
1.121262 +    sqlite3_int64 nByte = 0;
1.121263 +    const char *pEnd;
1.121264 +    const char *a;
1.121265 +
1.121266 +    rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
1.121267 +    if( rc!=SQLITE_OK ) return rc;
1.121268 +    a = sqlite3_column_blob(pStmt, 0);
1.121269 +    assert( a );
1.121270 +
1.121271 +    pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
1.121272 +    a += sqlite3Fts3GetVarint(a, &nDoc);
1.121273 +    while( a<pEnd ){
1.121274 +      a += sqlite3Fts3GetVarint(a, &nByte);
1.121275 +    }
1.121276 +    if( nDoc==0 || nByte==0 ){
1.121277 +      sqlite3_reset(pStmt);
1.121278 +      return FTS_CORRUPT_VTAB;
1.121279 +    }
1.121280 +
1.121281 +    pCsr->nDoc = nDoc;
1.121282 +    pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
1.121283 +    assert( pCsr->nRowAvg>0 ); 
1.121284 +    rc = sqlite3_reset(pStmt);
1.121285 +    if( rc!=SQLITE_OK ) return rc;
1.121286 +  }
1.121287 +
1.121288 +  *pnPage = pCsr->nRowAvg;
1.121289 +  return SQLITE_OK;
1.121290 +}
1.121291 +
1.121292 +/*
1.121293 +** This function is called to select the tokens (if any) that will be 
1.121294 +** deferred. The array aTC[] has already been populated when this is
1.121295 +** called.
1.121296 +**
1.121297 +** This function is called once for each AND/NEAR cluster in the 
1.121298 +** expression. Each invocation determines which tokens to defer within
1.121299 +** the cluster with root node pRoot. See comments above the definition
1.121300 +** of struct Fts3TokenAndCost for more details.
1.121301 +**
1.121302 +** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
1.121303 +** called on each token to defer. Otherwise, an SQLite error code is
1.121304 +** returned.
1.121305 +*/
1.121306 +static int fts3EvalSelectDeferred(
1.121307 +  Fts3Cursor *pCsr,               /* FTS Cursor handle */
1.121308 +  Fts3Expr *pRoot,                /* Consider tokens with this root node */
1.121309 +  Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
1.121310 +  int nTC                         /* Number of entries in aTC[] */
1.121311 +){
1.121312 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.121313 +  int nDocSize = 0;               /* Number of pages per doc loaded */
1.121314 +  int rc = SQLITE_OK;             /* Return code */
1.121315 +  int ii;                         /* Iterator variable for various purposes */
1.121316 +  int nOvfl = 0;                  /* Total overflow pages used by doclists */
1.121317 +  int nToken = 0;                 /* Total number of tokens in cluster */
1.121318 +
1.121319 +  int nMinEst = 0;                /* The minimum count for any phrase so far. */
1.121320 +  int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
1.121321 +
1.121322 +  /* Tokens are never deferred for FTS tables created using the content=xxx
1.121323 +  ** option. The reason being that it is not guaranteed that the content
1.121324 +  ** table actually contains the same data as the index. To prevent this from
1.121325 +  ** causing any problems, the deferred token optimization is completely
1.121326 +  ** disabled for content=xxx tables. */
1.121327 +  if( pTab->zContentTbl ){
1.121328 +    return SQLITE_OK;
1.121329 +  }
1.121330 +
1.121331 +  /* Count the tokens in this AND/NEAR cluster. If none of the doclists
1.121332 +  ** associated with the tokens spill onto overflow pages, or if there is
1.121333 +  ** only 1 token, exit early. No tokens to defer in this case. */
1.121334 +  for(ii=0; ii<nTC; ii++){
1.121335 +    if( aTC[ii].pRoot==pRoot ){
1.121336 +      nOvfl += aTC[ii].nOvfl;
1.121337 +      nToken++;
1.121338 +    }
1.121339 +  }
1.121340 +  if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
1.121341 +
1.121342 +  /* Obtain the average docsize (in pages). */
1.121343 +  rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
1.121344 +  assert( rc!=SQLITE_OK || nDocSize>0 );
1.121345 +
1.121346 +
1.121347 +  /* Iterate through all tokens in this AND/NEAR cluster, in ascending order 
1.121348 +  ** of the number of overflow pages that will be loaded by the pager layer 
1.121349 +  ** to retrieve the entire doclist for the token from the full-text index.
1.121350 +  ** Load the doclists for tokens that are either:
1.121351 +  **
1.121352 +  **   a. The cheapest token in the entire query (i.e. the one visited by the
1.121353 +  **      first iteration of this loop), or
1.121354 +  **
1.121355 +  **   b. Part of a multi-token phrase.
1.121356 +  **
1.121357 +  ** After each token doclist is loaded, merge it with the others from the
1.121358 +  ** same phrase and count the number of documents that the merged doclist
1.121359 +  ** contains. Set variable "nMinEst" to the smallest number of documents in 
1.121360 +  ** any phrase doclist for which 1 or more token doclists have been loaded.
1.121361 +  ** Let nOther be the number of other phrases for which it is certain that
1.121362 +  ** one or more tokens will not be deferred.
1.121363 +  **
1.121364 +  ** Then, for each token, defer it if loading the doclist would result in
1.121365 +  ** loading N or more overflow pages into memory, where N is computed as:
1.121366 +  **
1.121367 +  **    (nMinEst + 4^nOther - 1) / (4^nOther)
1.121368 +  */
1.121369 +  for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
1.121370 +    int iTC;                      /* Used to iterate through aTC[] array. */
1.121371 +    Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
1.121372 +
1.121373 +    /* Set pTC to point to the cheapest remaining token. */
1.121374 +    for(iTC=0; iTC<nTC; iTC++){
1.121375 +      if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot 
1.121376 +       && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl) 
1.121377 +      ){
1.121378 +        pTC = &aTC[iTC];
1.121379 +      }
1.121380 +    }
1.121381 +    assert( pTC );
1.121382 +
1.121383 +    if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
1.121384 +      /* The number of overflow pages to load for this (and therefore all
1.121385 +      ** subsequent) tokens is greater than the estimated number of pages 
1.121386 +      ** that will be loaded if all subsequent tokens are deferred.
1.121387 +      */
1.121388 +      Fts3PhraseToken *pToken = pTC->pToken;
1.121389 +      rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
1.121390 +      fts3SegReaderCursorFree(pToken->pSegcsr);
1.121391 +      pToken->pSegcsr = 0;
1.121392 +    }else{
1.121393 +      /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
1.121394 +      ** for-loop. Except, limit the value to 2^24 to prevent it from 
1.121395 +      ** overflowing the 32-bit integer it is stored in. */
1.121396 +      if( ii<12 ) nLoad4 = nLoad4*4;
1.121397 +
1.121398 +      if( ii==0 || pTC->pPhrase->nToken>1 ){
1.121399 +        /* Either this is the cheapest token in the entire query, or it is
1.121400 +        ** part of a multi-token phrase. Either way, the entire doclist will
1.121401 +        ** (eventually) be loaded into memory. It may as well be now. */
1.121402 +        Fts3PhraseToken *pToken = pTC->pToken;
1.121403 +        int nList = 0;
1.121404 +        char *pList = 0;
1.121405 +        rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
1.121406 +        assert( rc==SQLITE_OK || pList==0 );
1.121407 +        if( rc==SQLITE_OK ){
1.121408 +          int nCount;
1.121409 +          fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
1.121410 +          nCount = fts3DoclistCountDocids(
1.121411 +              pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
1.121412 +          );
1.121413 +          if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
1.121414 +        }
1.121415 +      }
1.121416 +    }
1.121417 +    pTC->pToken = 0;
1.121418 +  }
1.121419 +
1.121420 +  return rc;
1.121421 +}
1.121422 +
1.121423 +/*
1.121424 +** This function is called from within the xFilter method. It initializes
1.121425 +** the full-text query currently stored in pCsr->pExpr. To iterate through
1.121426 +** the results of a query, the caller does:
1.121427 +**
1.121428 +**    fts3EvalStart(pCsr);
1.121429 +**    while( 1 ){
1.121430 +**      fts3EvalNext(pCsr);
1.121431 +**      if( pCsr->bEof ) break;
1.121432 +**      ... return row pCsr->iPrevId to the caller ...
1.121433 +**    }
1.121434 +*/
1.121435 +static int fts3EvalStart(Fts3Cursor *pCsr){
1.121436 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.121437 +  int rc = SQLITE_OK;
1.121438 +  int nToken = 0;
1.121439 +  int nOr = 0;
1.121440 +
1.121441 +  /* Allocate a MultiSegReader for each token in the expression. */
1.121442 +  fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
1.121443 +
1.121444 +  /* Determine which, if any, tokens in the expression should be deferred. */
1.121445 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
1.121446 +  if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
1.121447 +    Fts3TokenAndCost *aTC;
1.121448 +    Fts3Expr **apOr;
1.121449 +    aTC = (Fts3TokenAndCost *)sqlite3_malloc(
1.121450 +        sizeof(Fts3TokenAndCost) * nToken
1.121451 +      + sizeof(Fts3Expr *) * nOr * 2
1.121452 +    );
1.121453 +    apOr = (Fts3Expr **)&aTC[nToken];
1.121454 +
1.121455 +    if( !aTC ){
1.121456 +      rc = SQLITE_NOMEM;
1.121457 +    }else{
1.121458 +      int ii;
1.121459 +      Fts3TokenAndCost *pTC = aTC;
1.121460 +      Fts3Expr **ppOr = apOr;
1.121461 +
1.121462 +      fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
1.121463 +      nToken = (int)(pTC-aTC);
1.121464 +      nOr = (int)(ppOr-apOr);
1.121465 +
1.121466 +      if( rc==SQLITE_OK ){
1.121467 +        rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
1.121468 +        for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
1.121469 +          rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
1.121470 +        }
1.121471 +      }
1.121472 +
1.121473 +      sqlite3_free(aTC);
1.121474 +    }
1.121475 +  }
1.121476 +#endif
1.121477 +
1.121478 +  fts3EvalStartReaders(pCsr, pCsr->pExpr, 1, &rc);
1.121479 +  return rc;
1.121480 +}
1.121481 +
1.121482 +/*
1.121483 +** Invalidate the current position list for phrase pPhrase.
1.121484 +*/
1.121485 +static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
1.121486 +  if( pPhrase->doclist.bFreeList ){
1.121487 +    sqlite3_free(pPhrase->doclist.pList);
1.121488 +  }
1.121489 +  pPhrase->doclist.pList = 0;
1.121490 +  pPhrase->doclist.nList = 0;
1.121491 +  pPhrase->doclist.bFreeList = 0;
1.121492 +}
1.121493 +
1.121494 +/*
1.121495 +** This function is called to edit the position list associated with
1.121496 +** the phrase object passed as the fifth argument according to a NEAR
1.121497 +** condition. For example:
1.121498 +**
1.121499 +**     abc NEAR/5 "def ghi"
1.121500 +**
1.121501 +** Parameter nNear is passed the NEAR distance of the expression (5 in
1.121502 +** the example above). When this function is called, *paPoslist points to
1.121503 +** the position list, and *pnToken is the number of phrase tokens in, the
1.121504 +** phrase on the other side of the NEAR operator to pPhrase. For example,
1.121505 +** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
1.121506 +** the position list associated with phrase "abc".
1.121507 +**
1.121508 +** All positions in the pPhrase position list that are not sufficiently
1.121509 +** close to a position in the *paPoslist position list are removed. If this
1.121510 +** leaves 0 positions, zero is returned. Otherwise, non-zero.
1.121511 +**
1.121512 +** Before returning, *paPoslist is set to point to the position lsit 
1.121513 +** associated with pPhrase. And *pnToken is set to the number of tokens in
1.121514 +** pPhrase.
1.121515 +*/
1.121516 +static int fts3EvalNearTrim(
1.121517 +  int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
1.121518 +  char *aTmp,                     /* Temporary space to use */
1.121519 +  char **paPoslist,               /* IN/OUT: Position list */
1.121520 +  int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
1.121521 +  Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
1.121522 +){
1.121523 +  int nParam1 = nNear + pPhrase->nToken;
1.121524 +  int nParam2 = nNear + *pnToken;
1.121525 +  int nNew;
1.121526 +  char *p2; 
1.121527 +  char *pOut; 
1.121528 +  int res;
1.121529 +
1.121530 +  assert( pPhrase->doclist.pList );
1.121531 +
1.121532 +  p2 = pOut = pPhrase->doclist.pList;
1.121533 +  res = fts3PoslistNearMerge(
1.121534 +    &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
1.121535 +  );
1.121536 +  if( res ){
1.121537 +    nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
1.121538 +    assert( pPhrase->doclist.pList[nNew]=='\0' );
1.121539 +    assert( nNew<=pPhrase->doclist.nList && nNew>0 );
1.121540 +    memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
1.121541 +    pPhrase->doclist.nList = nNew;
1.121542 +    *paPoslist = pPhrase->doclist.pList;
1.121543 +    *pnToken = pPhrase->nToken;
1.121544 +  }
1.121545 +
1.121546 +  return res;
1.121547 +}
1.121548 +
1.121549 +/*
1.121550 +** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
1.121551 +** Otherwise, it advances the expression passed as the second argument to
1.121552 +** point to the next matching row in the database. Expressions iterate through
1.121553 +** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
1.121554 +** or descending if it is non-zero.
1.121555 +**
1.121556 +** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
1.121557 +** successful, the following variables in pExpr are set:
1.121558 +**
1.121559 +**   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
1.121560 +**   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
1.121561 +**
1.121562 +** If the expression is of type FTSQUERY_PHRASE, and the expression is not
1.121563 +** at EOF, then the following variables are populated with the position list
1.121564 +** for the phrase for the visited row:
1.121565 +**
1.121566 +**   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
1.121567 +**   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
1.121568 +**
1.121569 +** It says above that this function advances the expression to the next
1.121570 +** matching row. This is usually true, but there are the following exceptions:
1.121571 +**
1.121572 +**   1. Deferred tokens are not taken into account. If a phrase consists
1.121573 +**      entirely of deferred tokens, it is assumed to match every row in
1.121574 +**      the db. In this case the position-list is not populated at all. 
1.121575 +**
1.121576 +**      Or, if a phrase contains one or more deferred tokens and one or
1.121577 +**      more non-deferred tokens, then the expression is advanced to the 
1.121578 +**      next possible match, considering only non-deferred tokens. In other
1.121579 +**      words, if the phrase is "A B C", and "B" is deferred, the expression
1.121580 +**      is advanced to the next row that contains an instance of "A * C", 
1.121581 +**      where "*" may match any single token. The position list in this case
1.121582 +**      is populated as for "A * C" before returning.
1.121583 +**
1.121584 +**   2. NEAR is treated as AND. If the expression is "x NEAR y", it is 
1.121585 +**      advanced to point to the next row that matches "x AND y".
1.121586 +** 
1.121587 +** See fts3EvalTestDeferredAndNear() for details on testing if a row is
1.121588 +** really a match, taking into account deferred tokens and NEAR operators.
1.121589 +*/
1.121590 +static void fts3EvalNextRow(
1.121591 +  Fts3Cursor *pCsr,               /* FTS Cursor handle */
1.121592 +  Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
1.121593 +  int *pRc                        /* IN/OUT: Error code */
1.121594 +){
1.121595 +  if( *pRc==SQLITE_OK ){
1.121596 +    int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
1.121597 +    assert( pExpr->bEof==0 );
1.121598 +    pExpr->bStart = 1;
1.121599 +
1.121600 +    switch( pExpr->eType ){
1.121601 +      case FTSQUERY_NEAR:
1.121602 +      case FTSQUERY_AND: {
1.121603 +        Fts3Expr *pLeft = pExpr->pLeft;
1.121604 +        Fts3Expr *pRight = pExpr->pRight;
1.121605 +        assert( !pLeft->bDeferred || !pRight->bDeferred );
1.121606 +
1.121607 +        if( pLeft->bDeferred ){
1.121608 +          /* LHS is entirely deferred. So we assume it matches every row.
1.121609 +          ** Advance the RHS iterator to find the next row visited. */
1.121610 +          fts3EvalNextRow(pCsr, pRight, pRc);
1.121611 +          pExpr->iDocid = pRight->iDocid;
1.121612 +          pExpr->bEof = pRight->bEof;
1.121613 +        }else if( pRight->bDeferred ){
1.121614 +          /* RHS is entirely deferred. So we assume it matches every row.
1.121615 +          ** Advance the LHS iterator to find the next row visited. */
1.121616 +          fts3EvalNextRow(pCsr, pLeft, pRc);
1.121617 +          pExpr->iDocid = pLeft->iDocid;
1.121618 +          pExpr->bEof = pLeft->bEof;
1.121619 +        }else{
1.121620 +          /* Neither the RHS or LHS are deferred. */
1.121621 +          fts3EvalNextRow(pCsr, pLeft, pRc);
1.121622 +          fts3EvalNextRow(pCsr, pRight, pRc);
1.121623 +          while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
1.121624 +            sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
1.121625 +            if( iDiff==0 ) break;
1.121626 +            if( iDiff<0 ){
1.121627 +              fts3EvalNextRow(pCsr, pLeft, pRc);
1.121628 +            }else{
1.121629 +              fts3EvalNextRow(pCsr, pRight, pRc);
1.121630 +            }
1.121631 +          }
1.121632 +          pExpr->iDocid = pLeft->iDocid;
1.121633 +          pExpr->bEof = (pLeft->bEof || pRight->bEof);
1.121634 +        }
1.121635 +        break;
1.121636 +      }
1.121637 +  
1.121638 +      case FTSQUERY_OR: {
1.121639 +        Fts3Expr *pLeft = pExpr->pLeft;
1.121640 +        Fts3Expr *pRight = pExpr->pRight;
1.121641 +        sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
1.121642 +
1.121643 +        assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
1.121644 +        assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
1.121645 +
1.121646 +        if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
1.121647 +          fts3EvalNextRow(pCsr, pLeft, pRc);
1.121648 +        }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
1.121649 +          fts3EvalNextRow(pCsr, pRight, pRc);
1.121650 +        }else{
1.121651 +          fts3EvalNextRow(pCsr, pLeft, pRc);
1.121652 +          fts3EvalNextRow(pCsr, pRight, pRc);
1.121653 +        }
1.121654 +
1.121655 +        pExpr->bEof = (pLeft->bEof && pRight->bEof);
1.121656 +        iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
1.121657 +        if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
1.121658 +          pExpr->iDocid = pLeft->iDocid;
1.121659 +        }else{
1.121660 +          pExpr->iDocid = pRight->iDocid;
1.121661 +        }
1.121662 +
1.121663 +        break;
1.121664 +      }
1.121665 +
1.121666 +      case FTSQUERY_NOT: {
1.121667 +        Fts3Expr *pLeft = pExpr->pLeft;
1.121668 +        Fts3Expr *pRight = pExpr->pRight;
1.121669 +
1.121670 +        if( pRight->bStart==0 ){
1.121671 +          fts3EvalNextRow(pCsr, pRight, pRc);
1.121672 +          assert( *pRc!=SQLITE_OK || pRight->bStart );
1.121673 +        }
1.121674 +
1.121675 +        fts3EvalNextRow(pCsr, pLeft, pRc);
1.121676 +        if( pLeft->bEof==0 ){
1.121677 +          while( !*pRc 
1.121678 +              && !pRight->bEof 
1.121679 +              && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0 
1.121680 +          ){
1.121681 +            fts3EvalNextRow(pCsr, pRight, pRc);
1.121682 +          }
1.121683 +        }
1.121684 +        pExpr->iDocid = pLeft->iDocid;
1.121685 +        pExpr->bEof = pLeft->bEof;
1.121686 +        break;
1.121687 +      }
1.121688 +
1.121689 +      default: {
1.121690 +        Fts3Phrase *pPhrase = pExpr->pPhrase;
1.121691 +        fts3EvalInvalidatePoslist(pPhrase);
1.121692 +        *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
1.121693 +        pExpr->iDocid = pPhrase->doclist.iDocid;
1.121694 +        break;
1.121695 +      }
1.121696 +    }
1.121697 +  }
1.121698 +}
1.121699 +
1.121700 +/*
1.121701 +** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
1.121702 +** cluster, then this function returns 1 immediately.
1.121703 +**
1.121704 +** Otherwise, it checks if the current row really does match the NEAR 
1.121705 +** expression, using the data currently stored in the position lists 
1.121706 +** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression. 
1.121707 +**
1.121708 +** If the current row is a match, the position list associated with each
1.121709 +** phrase in the NEAR expression is edited in place to contain only those
1.121710 +** phrase instances sufficiently close to their peers to satisfy all NEAR
1.121711 +** constraints. In this case it returns 1. If the NEAR expression does not 
1.121712 +** match the current row, 0 is returned. The position lists may or may not
1.121713 +** be edited if 0 is returned.
1.121714 +*/
1.121715 +static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
1.121716 +  int res = 1;
1.121717 +
1.121718 +  /* The following block runs if pExpr is the root of a NEAR query.
1.121719 +  ** For example, the query:
1.121720 +  **
1.121721 +  **         "w" NEAR "x" NEAR "y" NEAR "z"
1.121722 +  **
1.121723 +  ** which is represented in tree form as:
1.121724 +  **
1.121725 +  **                               |
1.121726 +  **                          +--NEAR--+      <-- root of NEAR query
1.121727 +  **                          |        |
1.121728 +  **                     +--NEAR--+   "z"
1.121729 +  **                     |        |
1.121730 +  **                +--NEAR--+   "y"
1.121731 +  **                |        |
1.121732 +  **               "w"      "x"
1.121733 +  **
1.121734 +  ** The right-hand child of a NEAR node is always a phrase. The 
1.121735 +  ** left-hand child may be either a phrase or a NEAR node. There are
1.121736 +  ** no exceptions to this - it's the way the parser in fts3_expr.c works.
1.121737 +  */
1.121738 +  if( *pRc==SQLITE_OK 
1.121739 +   && pExpr->eType==FTSQUERY_NEAR 
1.121740 +   && pExpr->bEof==0
1.121741 +   && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
1.121742 +  ){
1.121743 +    Fts3Expr *p; 
1.121744 +    int nTmp = 0;                 /* Bytes of temp space */
1.121745 +    char *aTmp;                   /* Temp space for PoslistNearMerge() */
1.121746 +
1.121747 +    /* Allocate temporary working space. */
1.121748 +    for(p=pExpr; p->pLeft; p=p->pLeft){
1.121749 +      nTmp += p->pRight->pPhrase->doclist.nList;
1.121750 +    }
1.121751 +    nTmp += p->pPhrase->doclist.nList;
1.121752 +    if( nTmp==0 ){
1.121753 +      res = 0;
1.121754 +    }else{
1.121755 +      aTmp = sqlite3_malloc(nTmp*2);
1.121756 +      if( !aTmp ){
1.121757 +        *pRc = SQLITE_NOMEM;
1.121758 +        res = 0;
1.121759 +      }else{
1.121760 +        char *aPoslist = p->pPhrase->doclist.pList;
1.121761 +        int nToken = p->pPhrase->nToken;
1.121762 +
1.121763 +        for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
1.121764 +          Fts3Phrase *pPhrase = p->pRight->pPhrase;
1.121765 +          int nNear = p->nNear;
1.121766 +          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
1.121767 +        }
1.121768 +
1.121769 +        aPoslist = pExpr->pRight->pPhrase->doclist.pList;
1.121770 +        nToken = pExpr->pRight->pPhrase->nToken;
1.121771 +        for(p=pExpr->pLeft; p && res; p=p->pLeft){
1.121772 +          int nNear;
1.121773 +          Fts3Phrase *pPhrase;
1.121774 +          assert( p->pParent && p->pParent->pLeft==p );
1.121775 +          nNear = p->pParent->nNear;
1.121776 +          pPhrase = (
1.121777 +              p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
1.121778 +              );
1.121779 +          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
1.121780 +        }
1.121781 +      }
1.121782 +
1.121783 +      sqlite3_free(aTmp);
1.121784 +    }
1.121785 +  }
1.121786 +
1.121787 +  return res;
1.121788 +}
1.121789 +
1.121790 +/*
1.121791 +** This function is a helper function for fts3EvalTestDeferredAndNear().
1.121792 +** Assuming no error occurs or has occurred, It returns non-zero if the
1.121793 +** expression passed as the second argument matches the row that pCsr 
1.121794 +** currently points to, or zero if it does not.
1.121795 +**
1.121796 +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
1.121797 +** If an error occurs during execution of this function, *pRc is set to 
1.121798 +** the appropriate SQLite error code. In this case the returned value is 
1.121799 +** undefined.
1.121800 +*/
1.121801 +static int fts3EvalTestExpr(
1.121802 +  Fts3Cursor *pCsr,               /* FTS cursor handle */
1.121803 +  Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
1.121804 +  int *pRc                        /* IN/OUT: Error code */
1.121805 +){
1.121806 +  int bHit = 1;                   /* Return value */
1.121807 +  if( *pRc==SQLITE_OK ){
1.121808 +    switch( pExpr->eType ){
1.121809 +      case FTSQUERY_NEAR:
1.121810 +      case FTSQUERY_AND:
1.121811 +        bHit = (
1.121812 +            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
1.121813 +         && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
1.121814 +         && fts3EvalNearTest(pExpr, pRc)
1.121815 +        );
1.121816 +
1.121817 +        /* If the NEAR expression does not match any rows, zero the doclist for 
1.121818 +        ** all phrases involved in the NEAR. This is because the snippet(),
1.121819 +        ** offsets() and matchinfo() functions are not supposed to recognize 
1.121820 +        ** any instances of phrases that are part of unmatched NEAR queries. 
1.121821 +        ** For example if this expression:
1.121822 +        **
1.121823 +        **    ... MATCH 'a OR (b NEAR c)'
1.121824 +        **
1.121825 +        ** is matched against a row containing:
1.121826 +        **
1.121827 +        **        'a b d e'
1.121828 +        **
1.121829 +        ** then any snippet() should ony highlight the "a" term, not the "b"
1.121830 +        ** (as "b" is part of a non-matching NEAR clause).
1.121831 +        */
1.121832 +        if( bHit==0 
1.121833 +         && pExpr->eType==FTSQUERY_NEAR 
1.121834 +         && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
1.121835 +        ){
1.121836 +          Fts3Expr *p;
1.121837 +          for(p=pExpr; p->pPhrase==0; p=p->pLeft){
1.121838 +            if( p->pRight->iDocid==pCsr->iPrevId ){
1.121839 +              fts3EvalInvalidatePoslist(p->pRight->pPhrase);
1.121840 +            }
1.121841 +          }
1.121842 +          if( p->iDocid==pCsr->iPrevId ){
1.121843 +            fts3EvalInvalidatePoslist(p->pPhrase);
1.121844 +          }
1.121845 +        }
1.121846 +
1.121847 +        break;
1.121848 +
1.121849 +      case FTSQUERY_OR: {
1.121850 +        int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
1.121851 +        int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
1.121852 +        bHit = bHit1 || bHit2;
1.121853 +        break;
1.121854 +      }
1.121855 +
1.121856 +      case FTSQUERY_NOT:
1.121857 +        bHit = (
1.121858 +            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
1.121859 +         && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
1.121860 +        );
1.121861 +        break;
1.121862 +
1.121863 +      default: {
1.121864 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
1.121865 +        if( pCsr->pDeferred 
1.121866 +         && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
1.121867 +        ){
1.121868 +          Fts3Phrase *pPhrase = pExpr->pPhrase;
1.121869 +          assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
1.121870 +          if( pExpr->bDeferred ){
1.121871 +            fts3EvalInvalidatePoslist(pPhrase);
1.121872 +          }
1.121873 +          *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
1.121874 +          bHit = (pPhrase->doclist.pList!=0);
1.121875 +          pExpr->iDocid = pCsr->iPrevId;
1.121876 +        }else
1.121877 +#endif
1.121878 +        {
1.121879 +          bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
1.121880 +        }
1.121881 +        break;
1.121882 +      }
1.121883 +    }
1.121884 +  }
1.121885 +  return bHit;
1.121886 +}
1.121887 +
1.121888 +/*
1.121889 +** This function is called as the second part of each xNext operation when
1.121890 +** iterating through the results of a full-text query. At this point the
1.121891 +** cursor points to a row that matches the query expression, with the
1.121892 +** following caveats:
1.121893 +**
1.121894 +**   * Up until this point, "NEAR" operators in the expression have been
1.121895 +**     treated as "AND".
1.121896 +**
1.121897 +**   * Deferred tokens have not yet been considered.
1.121898 +**
1.121899 +** If *pRc is not SQLITE_OK when this function is called, it immediately
1.121900 +** returns 0. Otherwise, it tests whether or not after considering NEAR
1.121901 +** operators and deferred tokens the current row is still a match for the
1.121902 +** expression. It returns 1 if both of the following are true:
1.121903 +**
1.121904 +**   1. *pRc is SQLITE_OK when this function returns, and
1.121905 +**
1.121906 +**   2. After scanning the current FTS table row for the deferred tokens,
1.121907 +**      it is determined that the row does *not* match the query.
1.121908 +**
1.121909 +** Or, if no error occurs and it seems the current row does match the FTS
1.121910 +** query, return 0.
1.121911 +*/
1.121912 +static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
1.121913 +  int rc = *pRc;
1.121914 +  int bMiss = 0;
1.121915 +  if( rc==SQLITE_OK ){
1.121916 +
1.121917 +    /* If there are one or more deferred tokens, load the current row into
1.121918 +    ** memory and scan it to determine the position list for each deferred
1.121919 +    ** token. Then, see if this row is really a match, considering deferred
1.121920 +    ** tokens and NEAR operators (neither of which were taken into account
1.121921 +    ** earlier, by fts3EvalNextRow()). 
1.121922 +    */
1.121923 +    if( pCsr->pDeferred ){
1.121924 +      rc = fts3CursorSeek(0, pCsr);
1.121925 +      if( rc==SQLITE_OK ){
1.121926 +        rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
1.121927 +      }
1.121928 +    }
1.121929 +    bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
1.121930 +
1.121931 +    /* Free the position-lists accumulated for each deferred token above. */
1.121932 +    sqlite3Fts3FreeDeferredDoclists(pCsr);
1.121933 +    *pRc = rc;
1.121934 +  }
1.121935 +  return (rc==SQLITE_OK && bMiss);
1.121936 +}
1.121937 +
1.121938 +/*
1.121939 +** Advance to the next document that matches the FTS expression in
1.121940 +** Fts3Cursor.pExpr.
1.121941 +*/
1.121942 +static int fts3EvalNext(Fts3Cursor *pCsr){
1.121943 +  int rc = SQLITE_OK;             /* Return Code */
1.121944 +  Fts3Expr *pExpr = pCsr->pExpr;
1.121945 +  assert( pCsr->isEof==0 );
1.121946 +  if( pExpr==0 ){
1.121947 +    pCsr->isEof = 1;
1.121948 +  }else{
1.121949 +    do {
1.121950 +      if( pCsr->isRequireSeek==0 ){
1.121951 +        sqlite3_reset(pCsr->pStmt);
1.121952 +      }
1.121953 +      assert( sqlite3_data_count(pCsr->pStmt)==0 );
1.121954 +      fts3EvalNextRow(pCsr, pExpr, &rc);
1.121955 +      pCsr->isEof = pExpr->bEof;
1.121956 +      pCsr->isRequireSeek = 1;
1.121957 +      pCsr->isMatchinfoNeeded = 1;
1.121958 +      pCsr->iPrevId = pExpr->iDocid;
1.121959 +    }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
1.121960 +  }
1.121961 +  return rc;
1.121962 +}
1.121963 +
1.121964 +/*
1.121965 +** Restart interation for expression pExpr so that the next call to
1.121966 +** fts3EvalNext() visits the first row. Do not allow incremental 
1.121967 +** loading or merging of phrase doclists for this iteration.
1.121968 +**
1.121969 +** If *pRc is other than SQLITE_OK when this function is called, it is
1.121970 +** a no-op. If an error occurs within this function, *pRc is set to an
1.121971 +** SQLite error code before returning.
1.121972 +*/
1.121973 +static void fts3EvalRestart(
1.121974 +  Fts3Cursor *pCsr,
1.121975 +  Fts3Expr *pExpr,
1.121976 +  int *pRc
1.121977 +){
1.121978 +  if( pExpr && *pRc==SQLITE_OK ){
1.121979 +    Fts3Phrase *pPhrase = pExpr->pPhrase;
1.121980 +
1.121981 +    if( pPhrase ){
1.121982 +      fts3EvalInvalidatePoslist(pPhrase);
1.121983 +      if( pPhrase->bIncr ){
1.121984 +        assert( pPhrase->nToken==1 );
1.121985 +        assert( pPhrase->aToken[0].pSegcsr );
1.121986 +        sqlite3Fts3MsrIncrRestart(pPhrase->aToken[0].pSegcsr);
1.121987 +        *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
1.121988 +      }
1.121989 +
1.121990 +      pPhrase->doclist.pNextDocid = 0;
1.121991 +      pPhrase->doclist.iDocid = 0;
1.121992 +    }
1.121993 +
1.121994 +    pExpr->iDocid = 0;
1.121995 +    pExpr->bEof = 0;
1.121996 +    pExpr->bStart = 0;
1.121997 +
1.121998 +    fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
1.121999 +    fts3EvalRestart(pCsr, pExpr->pRight, pRc);
1.122000 +  }
1.122001 +}
1.122002 +
1.122003 +/*
1.122004 +** After allocating the Fts3Expr.aMI[] array for each phrase in the 
1.122005 +** expression rooted at pExpr, the cursor iterates through all rows matched
1.122006 +** by pExpr, calling this function for each row. This function increments
1.122007 +** the values in Fts3Expr.aMI[] according to the position-list currently
1.122008 +** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase 
1.122009 +** expression nodes.
1.122010 +*/
1.122011 +static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
1.122012 +  if( pExpr ){
1.122013 +    Fts3Phrase *pPhrase = pExpr->pPhrase;
1.122014 +    if( pPhrase && pPhrase->doclist.pList ){
1.122015 +      int iCol = 0;
1.122016 +      char *p = pPhrase->doclist.pList;
1.122017 +
1.122018 +      assert( *p );
1.122019 +      while( 1 ){
1.122020 +        u8 c = 0;
1.122021 +        int iCnt = 0;
1.122022 +        while( 0xFE & (*p | c) ){
1.122023 +          if( (c&0x80)==0 ) iCnt++;
1.122024 +          c = *p++ & 0x80;
1.122025 +        }
1.122026 +
1.122027 +        /* aMI[iCol*3 + 1] = Number of occurrences
1.122028 +        ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
1.122029 +        */
1.122030 +        pExpr->aMI[iCol*3 + 1] += iCnt;
1.122031 +        pExpr->aMI[iCol*3 + 2] += (iCnt>0);
1.122032 +        if( *p==0x00 ) break;
1.122033 +        p++;
1.122034 +        p += sqlite3Fts3GetVarint32(p, &iCol);
1.122035 +      }
1.122036 +    }
1.122037 +
1.122038 +    fts3EvalUpdateCounts(pExpr->pLeft);
1.122039 +    fts3EvalUpdateCounts(pExpr->pRight);
1.122040 +  }
1.122041 +}
1.122042 +
1.122043 +/*
1.122044 +** Expression pExpr must be of type FTSQUERY_PHRASE.
1.122045 +**
1.122046 +** If it is not already allocated and populated, this function allocates and
1.122047 +** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
1.122048 +** of a NEAR expression, then it also allocates and populates the same array
1.122049 +** for all other phrases that are part of the NEAR expression.
1.122050 +**
1.122051 +** SQLITE_OK is returned if the aMI[] array is successfully allocated and
1.122052 +** populated. Otherwise, if an error occurs, an SQLite error code is returned.
1.122053 +*/
1.122054 +static int fts3EvalGatherStats(
1.122055 +  Fts3Cursor *pCsr,               /* Cursor object */
1.122056 +  Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
1.122057 +){
1.122058 +  int rc = SQLITE_OK;             /* Return code */
1.122059 +
1.122060 +  assert( pExpr->eType==FTSQUERY_PHRASE );
1.122061 +  if( pExpr->aMI==0 ){
1.122062 +    Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.122063 +    Fts3Expr *pRoot;                /* Root of NEAR expression */
1.122064 +    Fts3Expr *p;                    /* Iterator used for several purposes */
1.122065 +
1.122066 +    sqlite3_int64 iPrevId = pCsr->iPrevId;
1.122067 +    sqlite3_int64 iDocid;
1.122068 +    u8 bEof;
1.122069 +
1.122070 +    /* Find the root of the NEAR expression */
1.122071 +    pRoot = pExpr;
1.122072 +    while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
1.122073 +      pRoot = pRoot->pParent;
1.122074 +    }
1.122075 +    iDocid = pRoot->iDocid;
1.122076 +    bEof = pRoot->bEof;
1.122077 +    assert( pRoot->bStart );
1.122078 +
1.122079 +    /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
1.122080 +    for(p=pRoot; p; p=p->pLeft){
1.122081 +      Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
1.122082 +      assert( pE->aMI==0 );
1.122083 +      pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
1.122084 +      if( !pE->aMI ) return SQLITE_NOMEM;
1.122085 +      memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
1.122086 +    }
1.122087 +
1.122088 +    fts3EvalRestart(pCsr, pRoot, &rc);
1.122089 +
1.122090 +    while( pCsr->isEof==0 && rc==SQLITE_OK ){
1.122091 +
1.122092 +      do {
1.122093 +        /* Ensure the %_content statement is reset. */
1.122094 +        if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
1.122095 +        assert( sqlite3_data_count(pCsr->pStmt)==0 );
1.122096 +
1.122097 +        /* Advance to the next document */
1.122098 +        fts3EvalNextRow(pCsr, pRoot, &rc);
1.122099 +        pCsr->isEof = pRoot->bEof;
1.122100 +        pCsr->isRequireSeek = 1;
1.122101 +        pCsr->isMatchinfoNeeded = 1;
1.122102 +        pCsr->iPrevId = pRoot->iDocid;
1.122103 +      }while( pCsr->isEof==0 
1.122104 +           && pRoot->eType==FTSQUERY_NEAR 
1.122105 +           && fts3EvalTestDeferredAndNear(pCsr, &rc) 
1.122106 +      );
1.122107 +
1.122108 +      if( rc==SQLITE_OK && pCsr->isEof==0 ){
1.122109 +        fts3EvalUpdateCounts(pRoot);
1.122110 +      }
1.122111 +    }
1.122112 +
1.122113 +    pCsr->isEof = 0;
1.122114 +    pCsr->iPrevId = iPrevId;
1.122115 +
1.122116 +    if( bEof ){
1.122117 +      pRoot->bEof = bEof;
1.122118 +    }else{
1.122119 +      /* Caution: pRoot may iterate through docids in ascending or descending
1.122120 +      ** order. For this reason, even though it seems more defensive, the 
1.122121 +      ** do loop can not be written:
1.122122 +      **
1.122123 +      **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
1.122124 +      */
1.122125 +      fts3EvalRestart(pCsr, pRoot, &rc);
1.122126 +      do {
1.122127 +        fts3EvalNextRow(pCsr, pRoot, &rc);
1.122128 +        assert( pRoot->bEof==0 );
1.122129 +      }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
1.122130 +      fts3EvalTestDeferredAndNear(pCsr, &rc);
1.122131 +    }
1.122132 +  }
1.122133 +  return rc;
1.122134 +}
1.122135 +
1.122136 +/*
1.122137 +** This function is used by the matchinfo() module to query a phrase 
1.122138 +** expression node for the following information:
1.122139 +**
1.122140 +**   1. The total number of occurrences of the phrase in each column of 
1.122141 +**      the FTS table (considering all rows), and
1.122142 +**
1.122143 +**   2. For each column, the number of rows in the table for which the
1.122144 +**      column contains at least one instance of the phrase.
1.122145 +**
1.122146 +** If no error occurs, SQLITE_OK is returned and the values for each column
1.122147 +** written into the array aiOut as follows:
1.122148 +**
1.122149 +**   aiOut[iCol*3 + 1] = Number of occurrences
1.122150 +**   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
1.122151 +**
1.122152 +** Caveats:
1.122153 +**
1.122154 +**   * If a phrase consists entirely of deferred tokens, then all output 
1.122155 +**     values are set to the number of documents in the table. In other
1.122156 +**     words we assume that very common tokens occur exactly once in each 
1.122157 +**     column of each row of the table.
1.122158 +**
1.122159 +**   * If a phrase contains some deferred tokens (and some non-deferred 
1.122160 +**     tokens), count the potential occurrence identified by considering
1.122161 +**     the non-deferred tokens instead of actual phrase occurrences.
1.122162 +**
1.122163 +**   * If the phrase is part of a NEAR expression, then only phrase instances
1.122164 +**     that meet the NEAR constraint are included in the counts.
1.122165 +*/
1.122166 +SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
1.122167 +  Fts3Cursor *pCsr,               /* FTS cursor handle */
1.122168 +  Fts3Expr *pExpr,                /* Phrase expression */
1.122169 +  u32 *aiOut                      /* Array to write results into (see above) */
1.122170 +){
1.122171 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.122172 +  int rc = SQLITE_OK;
1.122173 +  int iCol;
1.122174 +
1.122175 +  if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
1.122176 +    assert( pCsr->nDoc>0 );
1.122177 +    for(iCol=0; iCol<pTab->nColumn; iCol++){
1.122178 +      aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
1.122179 +      aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
1.122180 +    }
1.122181 +  }else{
1.122182 +    rc = fts3EvalGatherStats(pCsr, pExpr);
1.122183 +    if( rc==SQLITE_OK ){
1.122184 +      assert( pExpr->aMI );
1.122185 +      for(iCol=0; iCol<pTab->nColumn; iCol++){
1.122186 +        aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
1.122187 +        aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
1.122188 +      }
1.122189 +    }
1.122190 +  }
1.122191 +
1.122192 +  return rc;
1.122193 +}
1.122194 +
1.122195 +/*
1.122196 +** The expression pExpr passed as the second argument to this function
1.122197 +** must be of type FTSQUERY_PHRASE. 
1.122198 +**
1.122199 +** The returned value is either NULL or a pointer to a buffer containing
1.122200 +** a position-list indicating the occurrences of the phrase in column iCol
1.122201 +** of the current row. 
1.122202 +**
1.122203 +** More specifically, the returned buffer contains 1 varint for each 
1.122204 +** occurence of the phrase in the column, stored using the normal (delta+2) 
1.122205 +** compression and is terminated by either an 0x01 or 0x00 byte. For example,
1.122206 +** if the requested column contains "a b X c d X X" and the position-list
1.122207 +** for 'X' is requested, the buffer returned may contain:
1.122208 +**
1.122209 +**     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
1.122210 +**
1.122211 +** This function works regardless of whether or not the phrase is deferred,
1.122212 +** incremental, or neither.
1.122213 +*/
1.122214 +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
1.122215 +  Fts3Cursor *pCsr,               /* FTS3 cursor object */
1.122216 +  Fts3Expr *pExpr,                /* Phrase to return doclist for */
1.122217 +  int iCol,                       /* Column to return position list for */
1.122218 +  char **ppOut                    /* OUT: Pointer to position list */
1.122219 +){
1.122220 +  Fts3Phrase *pPhrase = pExpr->pPhrase;
1.122221 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.122222 +  char *pIter;
1.122223 +  int iThis;
1.122224 +  sqlite3_int64 iDocid;
1.122225 +
1.122226 +  /* If this phrase is applies specifically to some column other than 
1.122227 +  ** column iCol, return a NULL pointer.  */
1.122228 +  *ppOut = 0;
1.122229 +  assert( iCol>=0 && iCol<pTab->nColumn );
1.122230 +  if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
1.122231 +    return SQLITE_OK;
1.122232 +  }
1.122233 +
1.122234 +  iDocid = pExpr->iDocid;
1.122235 +  pIter = pPhrase->doclist.pList;
1.122236 +  if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
1.122237 +    int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
1.122238 +    int bOr = 0;
1.122239 +    u8 bEof = 0;
1.122240 +    Fts3Expr *p;
1.122241 +
1.122242 +    /* Check if this phrase descends from an OR expression node. If not, 
1.122243 +    ** return NULL. Otherwise, the entry that corresponds to docid 
1.122244 +    ** pCsr->iPrevId may lie earlier in the doclist buffer. */
1.122245 +    for(p=pExpr->pParent; p; p=p->pParent){
1.122246 +      if( p->eType==FTSQUERY_OR ) bOr = 1;
1.122247 +    }
1.122248 +    if( bOr==0 ) return SQLITE_OK;
1.122249 +
1.122250 +    /* This is the descendent of an OR node. In this case we cannot use
1.122251 +    ** an incremental phrase. Load the entire doclist for the phrase
1.122252 +    ** into memory in this case.  */
1.122253 +    if( pPhrase->bIncr ){
1.122254 +      int rc = SQLITE_OK;
1.122255 +      int bEofSave = pExpr->bEof;
1.122256 +      fts3EvalRestart(pCsr, pExpr, &rc);
1.122257 +      while( rc==SQLITE_OK && !pExpr->bEof ){
1.122258 +        fts3EvalNextRow(pCsr, pExpr, &rc);
1.122259 +        if( bEofSave==0 && pExpr->iDocid==iDocid ) break;
1.122260 +      }
1.122261 +      pIter = pPhrase->doclist.pList;
1.122262 +      assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
1.122263 +      if( rc!=SQLITE_OK ) return rc;
1.122264 +    }
1.122265 +
1.122266 +    if( pExpr->bEof ){
1.122267 +      pIter = 0;
1.122268 +      iDocid = 0;
1.122269 +    }
1.122270 +    bEof = (pPhrase->doclist.nAll==0);
1.122271 +    assert( bDescDoclist==0 || bDescDoclist==1 );
1.122272 +    assert( pCsr->bDesc==0 || pCsr->bDesc==1 );
1.122273 +
1.122274 +    if( pCsr->bDesc==bDescDoclist ){
1.122275 +      int dummy;
1.122276 +      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
1.122277 +        sqlite3Fts3DoclistPrev(
1.122278 +            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
1.122279 +            &pIter, &iDocid, &dummy, &bEof
1.122280 +        );
1.122281 +      }
1.122282 +    }else{
1.122283 +      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
1.122284 +        sqlite3Fts3DoclistNext(
1.122285 +            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
1.122286 +            &pIter, &iDocid, &bEof
1.122287 +        );
1.122288 +      }
1.122289 +    }
1.122290 +
1.122291 +    if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
1.122292 +  }
1.122293 +  if( pIter==0 ) return SQLITE_OK;
1.122294 +
1.122295 +  if( *pIter==0x01 ){
1.122296 +    pIter++;
1.122297 +    pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
1.122298 +  }else{
1.122299 +    iThis = 0;
1.122300 +  }
1.122301 +  while( iThis<iCol ){
1.122302 +    fts3ColumnlistCopy(0, &pIter);
1.122303 +    if( *pIter==0x00 ) return 0;
1.122304 +    pIter++;
1.122305 +    pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
1.122306 +  }
1.122307 +
1.122308 +  *ppOut = ((iCol==iThis)?pIter:0);
1.122309 +  return SQLITE_OK;
1.122310 +}
1.122311 +
1.122312 +/*
1.122313 +** Free all components of the Fts3Phrase structure that were allocated by
1.122314 +** the eval module. Specifically, this means to free:
1.122315 +**
1.122316 +**   * the contents of pPhrase->doclist, and
1.122317 +**   * any Fts3MultiSegReader objects held by phrase tokens.
1.122318 +*/
1.122319 +SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
1.122320 +  if( pPhrase ){
1.122321 +    int i;
1.122322 +    sqlite3_free(pPhrase->doclist.aAll);
1.122323 +    fts3EvalInvalidatePoslist(pPhrase);
1.122324 +    memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
1.122325 +    for(i=0; i<pPhrase->nToken; i++){
1.122326 +      fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
1.122327 +      pPhrase->aToken[i].pSegcsr = 0;
1.122328 +    }
1.122329 +  }
1.122330 +}
1.122331 +
1.122332 +
1.122333 +/*
1.122334 +** Return SQLITE_CORRUPT_VTAB.
1.122335 +*/
1.122336 +#ifdef SQLITE_DEBUG
1.122337 +SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
1.122338 +  return SQLITE_CORRUPT_VTAB;
1.122339 +}
1.122340 +#endif
1.122341 +
1.122342 +#if !SQLITE_CORE
1.122343 +/*
1.122344 +** Initialize API pointer table, if required.
1.122345 +*/
1.122346 +SQLITE_API int sqlite3_extension_init(
1.122347 +  sqlite3 *db, 
1.122348 +  char **pzErrMsg,
1.122349 +  const sqlite3_api_routines *pApi
1.122350 +){
1.122351 +  SQLITE_EXTENSION_INIT2(pApi)
1.122352 +  return sqlite3Fts3Init(db);
1.122353 +}
1.122354 +#endif
1.122355 +
1.122356 +#endif
1.122357 +
1.122358 +/************** End of fts3.c ************************************************/
1.122359 +/************** Begin file fts3_aux.c ****************************************/
1.122360 +/*
1.122361 +** 2011 Jan 27
1.122362 +**
1.122363 +** The author disclaims copyright to this source code.  In place of
1.122364 +** a legal notice, here is a blessing:
1.122365 +**
1.122366 +**    May you do good and not evil.
1.122367 +**    May you find forgiveness for yourself and forgive others.
1.122368 +**    May you share freely, never taking more than you give.
1.122369 +**
1.122370 +******************************************************************************
1.122371 +**
1.122372 +*/
1.122373 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.122374 +
1.122375 +/* #include <string.h> */
1.122376 +/* #include <assert.h> */
1.122377 +
1.122378 +typedef struct Fts3auxTable Fts3auxTable;
1.122379 +typedef struct Fts3auxCursor Fts3auxCursor;
1.122380 +
1.122381 +struct Fts3auxTable {
1.122382 +  sqlite3_vtab base;              /* Base class used by SQLite core */
1.122383 +  Fts3Table *pFts3Tab;
1.122384 +};
1.122385 +
1.122386 +struct Fts3auxCursor {
1.122387 +  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
1.122388 +  Fts3MultiSegReader csr;        /* Must be right after "base" */
1.122389 +  Fts3SegFilter filter;
1.122390 +  char *zStop;
1.122391 +  int nStop;                      /* Byte-length of string zStop */
1.122392 +  int isEof;                      /* True if cursor is at EOF */
1.122393 +  sqlite3_int64 iRowid;           /* Current rowid */
1.122394 +
1.122395 +  int iCol;                       /* Current value of 'col' column */
1.122396 +  int nStat;                      /* Size of aStat[] array */
1.122397 +  struct Fts3auxColstats {
1.122398 +    sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
1.122399 +    sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
1.122400 +  } *aStat;
1.122401 +};
1.122402 +
1.122403 +/*
1.122404 +** Schema of the terms table.
1.122405 +*/
1.122406 +#define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
1.122407 +
1.122408 +/*
1.122409 +** This function does all the work for both the xConnect and xCreate methods.
1.122410 +** These tables have no persistent representation of their own, so xConnect
1.122411 +** and xCreate are identical operations.
1.122412 +*/
1.122413 +static int fts3auxConnectMethod(
1.122414 +  sqlite3 *db,                    /* Database connection */
1.122415 +  void *pUnused,                  /* Unused */
1.122416 +  int argc,                       /* Number of elements in argv array */
1.122417 +  const char * const *argv,       /* xCreate/xConnect argument array */
1.122418 +  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
1.122419 +  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
1.122420 +){
1.122421 +  char const *zDb;                /* Name of database (e.g. "main") */
1.122422 +  char const *zFts3;              /* Name of fts3 table */
1.122423 +  int nDb;                        /* Result of strlen(zDb) */
1.122424 +  int nFts3;                      /* Result of strlen(zFts3) */
1.122425 +  int nByte;                      /* Bytes of space to allocate here */
1.122426 +  int rc;                         /* value returned by declare_vtab() */
1.122427 +  Fts3auxTable *p;                /* Virtual table object to return */
1.122428 +
1.122429 +  UNUSED_PARAMETER(pUnused);
1.122430 +
1.122431 +  /* The user should specify a single argument - the name of an fts3 table. */
1.122432 +  if( argc!=4 ){
1.122433 +    *pzErr = sqlite3_mprintf(
1.122434 +        "wrong number of arguments to fts4aux constructor"
1.122435 +    );
1.122436 +    return SQLITE_ERROR;
1.122437 +  }
1.122438 +
1.122439 +  zDb = argv[1]; 
1.122440 +  nDb = (int)strlen(zDb);
1.122441 +  zFts3 = argv[3];
1.122442 +  nFts3 = (int)strlen(zFts3);
1.122443 +
1.122444 +  rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
1.122445 +  if( rc!=SQLITE_OK ) return rc;
1.122446 +
1.122447 +  nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
1.122448 +  p = (Fts3auxTable *)sqlite3_malloc(nByte);
1.122449 +  if( !p ) return SQLITE_NOMEM;
1.122450 +  memset(p, 0, nByte);
1.122451 +
1.122452 +  p->pFts3Tab = (Fts3Table *)&p[1];
1.122453 +  p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
1.122454 +  p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
1.122455 +  p->pFts3Tab->db = db;
1.122456 +  p->pFts3Tab->nIndex = 1;
1.122457 +
1.122458 +  memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
1.122459 +  memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
1.122460 +  sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
1.122461 +
1.122462 +  *ppVtab = (sqlite3_vtab *)p;
1.122463 +  return SQLITE_OK;
1.122464 +}
1.122465 +
1.122466 +/*
1.122467 +** This function does the work for both the xDisconnect and xDestroy methods.
1.122468 +** These tables have no persistent representation of their own, so xDisconnect
1.122469 +** and xDestroy are identical operations.
1.122470 +*/
1.122471 +static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
1.122472 +  Fts3auxTable *p = (Fts3auxTable *)pVtab;
1.122473 +  Fts3Table *pFts3 = p->pFts3Tab;
1.122474 +  int i;
1.122475 +
1.122476 +  /* Free any prepared statements held */
1.122477 +  for(i=0; i<SizeofArray(pFts3->aStmt); i++){
1.122478 +    sqlite3_finalize(pFts3->aStmt[i]);
1.122479 +  }
1.122480 +  sqlite3_free(pFts3->zSegmentsTbl);
1.122481 +  sqlite3_free(p);
1.122482 +  return SQLITE_OK;
1.122483 +}
1.122484 +
1.122485 +#define FTS4AUX_EQ_CONSTRAINT 1
1.122486 +#define FTS4AUX_GE_CONSTRAINT 2
1.122487 +#define FTS4AUX_LE_CONSTRAINT 4
1.122488 +
1.122489 +/*
1.122490 +** xBestIndex - Analyze a WHERE and ORDER BY clause.
1.122491 +*/
1.122492 +static int fts3auxBestIndexMethod(
1.122493 +  sqlite3_vtab *pVTab, 
1.122494 +  sqlite3_index_info *pInfo
1.122495 +){
1.122496 +  int i;
1.122497 +  int iEq = -1;
1.122498 +  int iGe = -1;
1.122499 +  int iLe = -1;
1.122500 +
1.122501 +  UNUSED_PARAMETER(pVTab);
1.122502 +
1.122503 +  /* This vtab delivers always results in "ORDER BY term ASC" order. */
1.122504 +  if( pInfo->nOrderBy==1 
1.122505 +   && pInfo->aOrderBy[0].iColumn==0 
1.122506 +   && pInfo->aOrderBy[0].desc==0
1.122507 +  ){
1.122508 +    pInfo->orderByConsumed = 1;
1.122509 +  }
1.122510 +
1.122511 +  /* Search for equality and range constraints on the "term" column. */
1.122512 +  for(i=0; i<pInfo->nConstraint; i++){
1.122513 +    if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
1.122514 +      int op = pInfo->aConstraint[i].op;
1.122515 +      if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
1.122516 +      if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
1.122517 +      if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
1.122518 +      if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
1.122519 +      if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
1.122520 +    }
1.122521 +  }
1.122522 +
1.122523 +  if( iEq>=0 ){
1.122524 +    pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
1.122525 +    pInfo->aConstraintUsage[iEq].argvIndex = 1;
1.122526 +    pInfo->estimatedCost = 5;
1.122527 +  }else{
1.122528 +    pInfo->idxNum = 0;
1.122529 +    pInfo->estimatedCost = 20000;
1.122530 +    if( iGe>=0 ){
1.122531 +      pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
1.122532 +      pInfo->aConstraintUsage[iGe].argvIndex = 1;
1.122533 +      pInfo->estimatedCost /= 2;
1.122534 +    }
1.122535 +    if( iLe>=0 ){
1.122536 +      pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
1.122537 +      pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
1.122538 +      pInfo->estimatedCost /= 2;
1.122539 +    }
1.122540 +  }
1.122541 +
1.122542 +  return SQLITE_OK;
1.122543 +}
1.122544 +
1.122545 +/*
1.122546 +** xOpen - Open a cursor.
1.122547 +*/
1.122548 +static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
1.122549 +  Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
1.122550 +
1.122551 +  UNUSED_PARAMETER(pVTab);
1.122552 +
1.122553 +  pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
1.122554 +  if( !pCsr ) return SQLITE_NOMEM;
1.122555 +  memset(pCsr, 0, sizeof(Fts3auxCursor));
1.122556 +
1.122557 +  *ppCsr = (sqlite3_vtab_cursor *)pCsr;
1.122558 +  return SQLITE_OK;
1.122559 +}
1.122560 +
1.122561 +/*
1.122562 +** xClose - Close a cursor.
1.122563 +*/
1.122564 +static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
1.122565 +  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
1.122566 +  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
1.122567 +
1.122568 +  sqlite3Fts3SegmentsClose(pFts3);
1.122569 +  sqlite3Fts3SegReaderFinish(&pCsr->csr);
1.122570 +  sqlite3_free((void *)pCsr->filter.zTerm);
1.122571 +  sqlite3_free(pCsr->zStop);
1.122572 +  sqlite3_free(pCsr->aStat);
1.122573 +  sqlite3_free(pCsr);
1.122574 +  return SQLITE_OK;
1.122575 +}
1.122576 +
1.122577 +static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
1.122578 +  if( nSize>pCsr->nStat ){
1.122579 +    struct Fts3auxColstats *aNew;
1.122580 +    aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, 
1.122581 +        sizeof(struct Fts3auxColstats) * nSize
1.122582 +    );
1.122583 +    if( aNew==0 ) return SQLITE_NOMEM;
1.122584 +    memset(&aNew[pCsr->nStat], 0, 
1.122585 +        sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
1.122586 +    );
1.122587 +    pCsr->aStat = aNew;
1.122588 +    pCsr->nStat = nSize;
1.122589 +  }
1.122590 +  return SQLITE_OK;
1.122591 +}
1.122592 +
1.122593 +/*
1.122594 +** xNext - Advance the cursor to the next row, if any.
1.122595 +*/
1.122596 +static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
1.122597 +  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
1.122598 +  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
1.122599 +  int rc;
1.122600 +
1.122601 +  /* Increment our pretend rowid value. */
1.122602 +  pCsr->iRowid++;
1.122603 +
1.122604 +  for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
1.122605 +    if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
1.122606 +  }
1.122607 +
1.122608 +  rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
1.122609 +  if( rc==SQLITE_ROW ){
1.122610 +    int i = 0;
1.122611 +    int nDoclist = pCsr->csr.nDoclist;
1.122612 +    char *aDoclist = pCsr->csr.aDoclist;
1.122613 +    int iCol;
1.122614 +
1.122615 +    int eState = 0;
1.122616 +
1.122617 +    if( pCsr->zStop ){
1.122618 +      int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
1.122619 +      int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
1.122620 +      if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
1.122621 +        pCsr->isEof = 1;
1.122622 +        return SQLITE_OK;
1.122623 +      }
1.122624 +    }
1.122625 +
1.122626 +    if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
1.122627 +    memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
1.122628 +    iCol = 0;
1.122629 +
1.122630 +    while( i<nDoclist ){
1.122631 +      sqlite3_int64 v = 0;
1.122632 +
1.122633 +      i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
1.122634 +      switch( eState ){
1.122635 +        /* State 0. In this state the integer just read was a docid. */
1.122636 +        case 0:
1.122637 +          pCsr->aStat[0].nDoc++;
1.122638 +          eState = 1;
1.122639 +          iCol = 0;
1.122640 +          break;
1.122641 +
1.122642 +        /* State 1. In this state we are expecting either a 1, indicating
1.122643 +        ** that the following integer will be a column number, or the
1.122644 +        ** start of a position list for column 0.  
1.122645 +        ** 
1.122646 +        ** The only difference between state 1 and state 2 is that if the
1.122647 +        ** integer encountered in state 1 is not 0 or 1, then we need to
1.122648 +        ** increment the column 0 "nDoc" count for this term.
1.122649 +        */
1.122650 +        case 1:
1.122651 +          assert( iCol==0 );
1.122652 +          if( v>1 ){
1.122653 +            pCsr->aStat[1].nDoc++;
1.122654 +          }
1.122655 +          eState = 2;
1.122656 +          /* fall through */
1.122657 +
1.122658 +        case 2:
1.122659 +          if( v==0 ){       /* 0x00. Next integer will be a docid. */
1.122660 +            eState = 0;
1.122661 +          }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
1.122662 +            eState = 3;
1.122663 +          }else{            /* 2 or greater. A position. */
1.122664 +            pCsr->aStat[iCol+1].nOcc++;
1.122665 +            pCsr->aStat[0].nOcc++;
1.122666 +          }
1.122667 +          break;
1.122668 +
1.122669 +        /* State 3. The integer just read is a column number. */
1.122670 +        default: assert( eState==3 );
1.122671 +          iCol = (int)v;
1.122672 +          if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
1.122673 +          pCsr->aStat[iCol+1].nDoc++;
1.122674 +          eState = 2;
1.122675 +          break;
1.122676 +      }
1.122677 +    }
1.122678 +
1.122679 +    pCsr->iCol = 0;
1.122680 +    rc = SQLITE_OK;
1.122681 +  }else{
1.122682 +    pCsr->isEof = 1;
1.122683 +  }
1.122684 +  return rc;
1.122685 +}
1.122686 +
1.122687 +/*
1.122688 +** xFilter - Initialize a cursor to point at the start of its data.
1.122689 +*/
1.122690 +static int fts3auxFilterMethod(
1.122691 +  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
1.122692 +  int idxNum,                     /* Strategy index */
1.122693 +  const char *idxStr,             /* Unused */
1.122694 +  int nVal,                       /* Number of elements in apVal */
1.122695 +  sqlite3_value **apVal           /* Arguments for the indexing scheme */
1.122696 +){
1.122697 +  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
1.122698 +  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
1.122699 +  int rc;
1.122700 +  int isScan;
1.122701 +
1.122702 +  UNUSED_PARAMETER(nVal);
1.122703 +  UNUSED_PARAMETER(idxStr);
1.122704 +
1.122705 +  assert( idxStr==0 );
1.122706 +  assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
1.122707 +       || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
1.122708 +       || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
1.122709 +  );
1.122710 +  isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
1.122711 +
1.122712 +  /* In case this cursor is being reused, close and zero it. */
1.122713 +  testcase(pCsr->filter.zTerm);
1.122714 +  sqlite3Fts3SegReaderFinish(&pCsr->csr);
1.122715 +  sqlite3_free((void *)pCsr->filter.zTerm);
1.122716 +  sqlite3_free(pCsr->aStat);
1.122717 +  memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
1.122718 +
1.122719 +  pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
1.122720 +  if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
1.122721 +
1.122722 +  if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
1.122723 +    const unsigned char *zStr = sqlite3_value_text(apVal[0]);
1.122724 +    if( zStr ){
1.122725 +      pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
1.122726 +      pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
1.122727 +      if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
1.122728 +    }
1.122729 +  }
1.122730 +  if( idxNum&FTS4AUX_LE_CONSTRAINT ){
1.122731 +    int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
1.122732 +    pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
1.122733 +    pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
1.122734 +    if( pCsr->zStop==0 ) return SQLITE_NOMEM;
1.122735 +  }
1.122736 +
1.122737 +  rc = sqlite3Fts3SegReaderCursor(pFts3, 0, 0, FTS3_SEGCURSOR_ALL,
1.122738 +      pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
1.122739 +  );
1.122740 +  if( rc==SQLITE_OK ){
1.122741 +    rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
1.122742 +  }
1.122743 +
1.122744 +  if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
1.122745 +  return rc;
1.122746 +}
1.122747 +
1.122748 +/*
1.122749 +** xEof - Return true if the cursor is at EOF, or false otherwise.
1.122750 +*/
1.122751 +static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
1.122752 +  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
1.122753 +  return pCsr->isEof;
1.122754 +}
1.122755 +
1.122756 +/*
1.122757 +** xColumn - Return a column value.
1.122758 +*/
1.122759 +static int fts3auxColumnMethod(
1.122760 +  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
1.122761 +  sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
1.122762 +  int iCol                        /* Index of column to read value from */
1.122763 +){
1.122764 +  Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
1.122765 +
1.122766 +  assert( p->isEof==0 );
1.122767 +  if( iCol==0 ){        /* Column "term" */
1.122768 +    sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
1.122769 +  }else if( iCol==1 ){  /* Column "col" */
1.122770 +    if( p->iCol ){
1.122771 +      sqlite3_result_int(pContext, p->iCol-1);
1.122772 +    }else{
1.122773 +      sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
1.122774 +    }
1.122775 +  }else if( iCol==2 ){  /* Column "documents" */
1.122776 +    sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
1.122777 +  }else{                /* Column "occurrences" */
1.122778 +    sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
1.122779 +  }
1.122780 +
1.122781 +  return SQLITE_OK;
1.122782 +}
1.122783 +
1.122784 +/*
1.122785 +** xRowid - Return the current rowid for the cursor.
1.122786 +*/
1.122787 +static int fts3auxRowidMethod(
1.122788 +  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
1.122789 +  sqlite_int64 *pRowid            /* OUT: Rowid value */
1.122790 +){
1.122791 +  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
1.122792 +  *pRowid = pCsr->iRowid;
1.122793 +  return SQLITE_OK;
1.122794 +}
1.122795 +
1.122796 +/*
1.122797 +** Register the fts3aux module with database connection db. Return SQLITE_OK
1.122798 +** if successful or an error code if sqlite3_create_module() fails.
1.122799 +*/
1.122800 +SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
1.122801 +  static const sqlite3_module fts3aux_module = {
1.122802 +     0,                           /* iVersion      */
1.122803 +     fts3auxConnectMethod,        /* xCreate       */
1.122804 +     fts3auxConnectMethod,        /* xConnect      */
1.122805 +     fts3auxBestIndexMethod,      /* xBestIndex    */
1.122806 +     fts3auxDisconnectMethod,     /* xDisconnect   */
1.122807 +     fts3auxDisconnectMethod,     /* xDestroy      */
1.122808 +     fts3auxOpenMethod,           /* xOpen         */
1.122809 +     fts3auxCloseMethod,          /* xClose        */
1.122810 +     fts3auxFilterMethod,         /* xFilter       */
1.122811 +     fts3auxNextMethod,           /* xNext         */
1.122812 +     fts3auxEofMethod,            /* xEof          */
1.122813 +     fts3auxColumnMethod,         /* xColumn       */
1.122814 +     fts3auxRowidMethod,          /* xRowid        */
1.122815 +     0,                           /* xUpdate       */
1.122816 +     0,                           /* xBegin        */
1.122817 +     0,                           /* xSync         */
1.122818 +     0,                           /* xCommit       */
1.122819 +     0,                           /* xRollback     */
1.122820 +     0,                           /* xFindFunction */
1.122821 +     0,                           /* xRename       */
1.122822 +     0,                           /* xSavepoint    */
1.122823 +     0,                           /* xRelease      */
1.122824 +     0                            /* xRollbackTo   */
1.122825 +  };
1.122826 +  int rc;                         /* Return code */
1.122827 +
1.122828 +  rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
1.122829 +  return rc;
1.122830 +}
1.122831 +
1.122832 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.122833 +
1.122834 +/************** End of fts3_aux.c ********************************************/
1.122835 +/************** Begin file fts3_expr.c ***************************************/
1.122836 +/*
1.122837 +** 2008 Nov 28
1.122838 +**
1.122839 +** The author disclaims copyright to this source code.  In place of
1.122840 +** a legal notice, here is a blessing:
1.122841 +**
1.122842 +**    May you do good and not evil.
1.122843 +**    May you find forgiveness for yourself and forgive others.
1.122844 +**    May you share freely, never taking more than you give.
1.122845 +**
1.122846 +******************************************************************************
1.122847 +**
1.122848 +** This module contains code that implements a parser for fts3 query strings
1.122849 +** (the right-hand argument to the MATCH operator). Because the supported 
1.122850 +** syntax is relatively simple, the whole tokenizer/parser system is
1.122851 +** hand-coded. 
1.122852 +*/
1.122853 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.122854 +
1.122855 +/*
1.122856 +** By default, this module parses the legacy syntax that has been 
1.122857 +** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
1.122858 +** is defined, then it uses the new syntax. The differences between
1.122859 +** the new and the old syntaxes are:
1.122860 +**
1.122861 +**  a) The new syntax supports parenthesis. The old does not.
1.122862 +**
1.122863 +**  b) The new syntax supports the AND and NOT operators. The old does not.
1.122864 +**
1.122865 +**  c) The old syntax supports the "-" token qualifier. This is not 
1.122866 +**     supported by the new syntax (it is replaced by the NOT operator).
1.122867 +**
1.122868 +**  d) When using the old syntax, the OR operator has a greater precedence
1.122869 +**     than an implicit AND. When using the new, both implicity and explicit
1.122870 +**     AND operators have a higher precedence than OR.
1.122871 +**
1.122872 +** If compiled with SQLITE_TEST defined, then this module exports the
1.122873 +** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
1.122874 +** to zero causes the module to use the old syntax. If it is set to 
1.122875 +** non-zero the new syntax is activated. This is so both syntaxes can
1.122876 +** be tested using a single build of testfixture.
1.122877 +**
1.122878 +** The following describes the syntax supported by the fts3 MATCH
1.122879 +** operator in a similar format to that used by the lemon parser
1.122880 +** generator. This module does not use actually lemon, it uses a
1.122881 +** custom parser.
1.122882 +**
1.122883 +**   query ::= andexpr (OR andexpr)*.
1.122884 +**
1.122885 +**   andexpr ::= notexpr (AND? notexpr)*.
1.122886 +**
1.122887 +**   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
1.122888 +**   notexpr ::= LP query RP.
1.122889 +**
1.122890 +**   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
1.122891 +**
1.122892 +**   distance_opt ::= .
1.122893 +**   distance_opt ::= / INTEGER.
1.122894 +**
1.122895 +**   phrase ::= TOKEN.
1.122896 +**   phrase ::= COLUMN:TOKEN.
1.122897 +**   phrase ::= "TOKEN TOKEN TOKEN...".
1.122898 +*/
1.122899 +
1.122900 +#ifdef SQLITE_TEST
1.122901 +SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
1.122902 +#else
1.122903 +# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS 
1.122904 +#  define sqlite3_fts3_enable_parentheses 1
1.122905 +# else
1.122906 +#  define sqlite3_fts3_enable_parentheses 0
1.122907 +# endif
1.122908 +#endif
1.122909 +
1.122910 +/*
1.122911 +** Default span for NEAR operators.
1.122912 +*/
1.122913 +#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
1.122914 +
1.122915 +/* #include <string.h> */
1.122916 +/* #include <assert.h> */
1.122917 +
1.122918 +/*
1.122919 +** isNot:
1.122920 +**   This variable is used by function getNextNode(). When getNextNode() is
1.122921 +**   called, it sets ParseContext.isNot to true if the 'next node' is a 
1.122922 +**   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
1.122923 +**   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
1.122924 +**   zero.
1.122925 +*/
1.122926 +typedef struct ParseContext ParseContext;
1.122927 +struct ParseContext {
1.122928 +  sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
1.122929 +  int iLangid;                        /* Language id used with tokenizer */
1.122930 +  const char **azCol;                 /* Array of column names for fts3 table */
1.122931 +  int bFts4;                          /* True to allow FTS4-only syntax */
1.122932 +  int nCol;                           /* Number of entries in azCol[] */
1.122933 +  int iDefaultCol;                    /* Default column to query */
1.122934 +  int isNot;                          /* True if getNextNode() sees a unary - */
1.122935 +  sqlite3_context *pCtx;              /* Write error message here */
1.122936 +  int nNest;                          /* Number of nested brackets */
1.122937 +};
1.122938 +
1.122939 +/*
1.122940 +** This function is equivalent to the standard isspace() function. 
1.122941 +**
1.122942 +** The standard isspace() can be awkward to use safely, because although it
1.122943 +** is defined to accept an argument of type int, its behaviour when passed
1.122944 +** an integer that falls outside of the range of the unsigned char type
1.122945 +** is undefined (and sometimes, "undefined" means segfault). This wrapper
1.122946 +** is defined to accept an argument of type char, and always returns 0 for
1.122947 +** any values that fall outside of the range of the unsigned char type (i.e.
1.122948 +** negative values).
1.122949 +*/
1.122950 +static int fts3isspace(char c){
1.122951 +  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
1.122952 +}
1.122953 +
1.122954 +/*
1.122955 +** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
1.122956 +** zero the memory before returning a pointer to it. If unsuccessful, 
1.122957 +** return NULL.
1.122958 +*/
1.122959 +static void *fts3MallocZero(int nByte){
1.122960 +  void *pRet = sqlite3_malloc(nByte);
1.122961 +  if( pRet ) memset(pRet, 0, nByte);
1.122962 +  return pRet;
1.122963 +}
1.122964 +
1.122965 +SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
1.122966 +  sqlite3_tokenizer *pTokenizer,
1.122967 +  int iLangid,
1.122968 +  const char *z,
1.122969 +  int n,
1.122970 +  sqlite3_tokenizer_cursor **ppCsr
1.122971 +){
1.122972 +  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
1.122973 +  sqlite3_tokenizer_cursor *pCsr = 0;
1.122974 +  int rc;
1.122975 +
1.122976 +  rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
1.122977 +  assert( rc==SQLITE_OK || pCsr==0 );
1.122978 +  if( rc==SQLITE_OK ){
1.122979 +    pCsr->pTokenizer = pTokenizer;
1.122980 +    if( pModule->iVersion>=1 ){
1.122981 +      rc = pModule->xLanguageid(pCsr, iLangid);
1.122982 +      if( rc!=SQLITE_OK ){
1.122983 +        pModule->xClose(pCsr);
1.122984 +        pCsr = 0;
1.122985 +      }
1.122986 +    }
1.122987 +  }
1.122988 +  *ppCsr = pCsr;
1.122989 +  return rc;
1.122990 +}
1.122991 +
1.122992 +
1.122993 +/*
1.122994 +** Extract the next token from buffer z (length n) using the tokenizer
1.122995 +** and other information (column names etc.) in pParse. Create an Fts3Expr
1.122996 +** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
1.122997 +** single token and set *ppExpr to point to it. If the end of the buffer is
1.122998 +** reached before a token is found, set *ppExpr to zero. It is the
1.122999 +** responsibility of the caller to eventually deallocate the allocated 
1.123000 +** Fts3Expr structure (if any) by passing it to sqlite3_free().
1.123001 +**
1.123002 +** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
1.123003 +** fails.
1.123004 +*/
1.123005 +static int getNextToken(
1.123006 +  ParseContext *pParse,                   /* fts3 query parse context */
1.123007 +  int iCol,                               /* Value for Fts3Phrase.iColumn */
1.123008 +  const char *z, int n,                   /* Input string */
1.123009 +  Fts3Expr **ppExpr,                      /* OUT: expression */
1.123010 +  int *pnConsumed                         /* OUT: Number of bytes consumed */
1.123011 +){
1.123012 +  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
1.123013 +  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
1.123014 +  int rc;
1.123015 +  sqlite3_tokenizer_cursor *pCursor;
1.123016 +  Fts3Expr *pRet = 0;
1.123017 +  int nConsumed = 0;
1.123018 +
1.123019 +  rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
1.123020 +  if( rc==SQLITE_OK ){
1.123021 +    const char *zToken;
1.123022 +    int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
1.123023 +    int nByte;                               /* total space to allocate */
1.123024 +
1.123025 +    rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
1.123026 +    if( rc==SQLITE_OK ){
1.123027 +      nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
1.123028 +      pRet = (Fts3Expr *)fts3MallocZero(nByte);
1.123029 +      if( !pRet ){
1.123030 +        rc = SQLITE_NOMEM;
1.123031 +      }else{
1.123032 +        pRet->eType = FTSQUERY_PHRASE;
1.123033 +        pRet->pPhrase = (Fts3Phrase *)&pRet[1];
1.123034 +        pRet->pPhrase->nToken = 1;
1.123035 +        pRet->pPhrase->iColumn = iCol;
1.123036 +        pRet->pPhrase->aToken[0].n = nToken;
1.123037 +        pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
1.123038 +        memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
1.123039 +
1.123040 +        if( iEnd<n && z[iEnd]=='*' ){
1.123041 +          pRet->pPhrase->aToken[0].isPrefix = 1;
1.123042 +          iEnd++;
1.123043 +        }
1.123044 +
1.123045 +        while( 1 ){
1.123046 +          if( !sqlite3_fts3_enable_parentheses 
1.123047 +           && iStart>0 && z[iStart-1]=='-' 
1.123048 +          ){
1.123049 +            pParse->isNot = 1;
1.123050 +            iStart--;
1.123051 +          }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
1.123052 +            pRet->pPhrase->aToken[0].bFirst = 1;
1.123053 +            iStart--;
1.123054 +          }else{
1.123055 +            break;
1.123056 +          }
1.123057 +        }
1.123058 +
1.123059 +      }
1.123060 +      nConsumed = iEnd;
1.123061 +    }
1.123062 +
1.123063 +    pModule->xClose(pCursor);
1.123064 +  }
1.123065 +  
1.123066 +  *pnConsumed = nConsumed;
1.123067 +  *ppExpr = pRet;
1.123068 +  return rc;
1.123069 +}
1.123070 +
1.123071 +
1.123072 +/*
1.123073 +** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
1.123074 +** then free the old allocation.
1.123075 +*/
1.123076 +static void *fts3ReallocOrFree(void *pOrig, int nNew){
1.123077 +  void *pRet = sqlite3_realloc(pOrig, nNew);
1.123078 +  if( !pRet ){
1.123079 +    sqlite3_free(pOrig);
1.123080 +  }
1.123081 +  return pRet;
1.123082 +}
1.123083 +
1.123084 +/*
1.123085 +** Buffer zInput, length nInput, contains the contents of a quoted string
1.123086 +** that appeared as part of an fts3 query expression. Neither quote character
1.123087 +** is included in the buffer. This function attempts to tokenize the entire
1.123088 +** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE 
1.123089 +** containing the results.
1.123090 +**
1.123091 +** If successful, SQLITE_OK is returned and *ppExpr set to point at the
1.123092 +** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
1.123093 +** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
1.123094 +** to 0.
1.123095 +*/
1.123096 +static int getNextString(
1.123097 +  ParseContext *pParse,                   /* fts3 query parse context */
1.123098 +  const char *zInput, int nInput,         /* Input string */
1.123099 +  Fts3Expr **ppExpr                       /* OUT: expression */
1.123100 +){
1.123101 +  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
1.123102 +  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
1.123103 +  int rc;
1.123104 +  Fts3Expr *p = 0;
1.123105 +  sqlite3_tokenizer_cursor *pCursor = 0;
1.123106 +  char *zTemp = 0;
1.123107 +  int nTemp = 0;
1.123108 +
1.123109 +  const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
1.123110 +  int nToken = 0;
1.123111 +
1.123112 +  /* The final Fts3Expr data structure, including the Fts3Phrase,
1.123113 +  ** Fts3PhraseToken structures token buffers are all stored as a single 
1.123114 +  ** allocation so that the expression can be freed with a single call to
1.123115 +  ** sqlite3_free(). Setting this up requires a two pass approach.
1.123116 +  **
1.123117 +  ** The first pass, in the block below, uses a tokenizer cursor to iterate
1.123118 +  ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
1.123119 +  ** to assemble data in two dynamic buffers:
1.123120 +  **
1.123121 +  **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
1.123122 +  **             structure, followed by the array of Fts3PhraseToken 
1.123123 +  **             structures. This pass only populates the Fts3PhraseToken array.
1.123124 +  **
1.123125 +  **   Buffer zTemp: Contains copies of all tokens.
1.123126 +  **
1.123127 +  ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
1.123128 +  ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
1.123129 +  ** structures.
1.123130 +  */
1.123131 +  rc = sqlite3Fts3OpenTokenizer(
1.123132 +      pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
1.123133 +  if( rc==SQLITE_OK ){
1.123134 +    int ii;
1.123135 +    for(ii=0; rc==SQLITE_OK; ii++){
1.123136 +      const char *zByte;
1.123137 +      int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
1.123138 +      rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
1.123139 +      if( rc==SQLITE_OK ){
1.123140 +        Fts3PhraseToken *pToken;
1.123141 +
1.123142 +        p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
1.123143 +        if( !p ) goto no_mem;
1.123144 +
1.123145 +        zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
1.123146 +        if( !zTemp ) goto no_mem;
1.123147 +
1.123148 +        assert( nToken==ii );
1.123149 +        pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
1.123150 +        memset(pToken, 0, sizeof(Fts3PhraseToken));
1.123151 +
1.123152 +        memcpy(&zTemp[nTemp], zByte, nByte);
1.123153 +        nTemp += nByte;
1.123154 +
1.123155 +        pToken->n = nByte;
1.123156 +        pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
1.123157 +        pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
1.123158 +        nToken = ii+1;
1.123159 +      }
1.123160 +    }
1.123161 +
1.123162 +    pModule->xClose(pCursor);
1.123163 +    pCursor = 0;
1.123164 +  }
1.123165 +
1.123166 +  if( rc==SQLITE_DONE ){
1.123167 +    int jj;
1.123168 +    char *zBuf = 0;
1.123169 +
1.123170 +    p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
1.123171 +    if( !p ) goto no_mem;
1.123172 +    memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
1.123173 +    p->eType = FTSQUERY_PHRASE;
1.123174 +    p->pPhrase = (Fts3Phrase *)&p[1];
1.123175 +    p->pPhrase->iColumn = pParse->iDefaultCol;
1.123176 +    p->pPhrase->nToken = nToken;
1.123177 +
1.123178 +    zBuf = (char *)&p->pPhrase->aToken[nToken];
1.123179 +    if( zTemp ){
1.123180 +      memcpy(zBuf, zTemp, nTemp);
1.123181 +      sqlite3_free(zTemp);
1.123182 +    }else{
1.123183 +      assert( nTemp==0 );
1.123184 +    }
1.123185 +
1.123186 +    for(jj=0; jj<p->pPhrase->nToken; jj++){
1.123187 +      p->pPhrase->aToken[jj].z = zBuf;
1.123188 +      zBuf += p->pPhrase->aToken[jj].n;
1.123189 +    }
1.123190 +    rc = SQLITE_OK;
1.123191 +  }
1.123192 +
1.123193 +  *ppExpr = p;
1.123194 +  return rc;
1.123195 +no_mem:
1.123196 +
1.123197 +  if( pCursor ){
1.123198 +    pModule->xClose(pCursor);
1.123199 +  }
1.123200 +  sqlite3_free(zTemp);
1.123201 +  sqlite3_free(p);
1.123202 +  *ppExpr = 0;
1.123203 +  return SQLITE_NOMEM;
1.123204 +}
1.123205 +
1.123206 +/*
1.123207 +** Function getNextNode(), which is called by fts3ExprParse(), may itself
1.123208 +** call fts3ExprParse(). So this forward declaration is required.
1.123209 +*/
1.123210 +static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
1.123211 +
1.123212 +/*
1.123213 +** The output variable *ppExpr is populated with an allocated Fts3Expr 
1.123214 +** structure, or set to 0 if the end of the input buffer is reached.
1.123215 +**
1.123216 +** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
1.123217 +** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
1.123218 +** If SQLITE_ERROR is returned, pContext is populated with an error message.
1.123219 +*/
1.123220 +static int getNextNode(
1.123221 +  ParseContext *pParse,                   /* fts3 query parse context */
1.123222 +  const char *z, int n,                   /* Input string */
1.123223 +  Fts3Expr **ppExpr,                      /* OUT: expression */
1.123224 +  int *pnConsumed                         /* OUT: Number of bytes consumed */
1.123225 +){
1.123226 +  static const struct Fts3Keyword {
1.123227 +    char *z;                              /* Keyword text */
1.123228 +    unsigned char n;                      /* Length of the keyword */
1.123229 +    unsigned char parenOnly;              /* Only valid in paren mode */
1.123230 +    unsigned char eType;                  /* Keyword code */
1.123231 +  } aKeyword[] = {
1.123232 +    { "OR" ,  2, 0, FTSQUERY_OR   },
1.123233 +    { "AND",  3, 1, FTSQUERY_AND  },
1.123234 +    { "NOT",  3, 1, FTSQUERY_NOT  },
1.123235 +    { "NEAR", 4, 0, FTSQUERY_NEAR }
1.123236 +  };
1.123237 +  int ii;
1.123238 +  int iCol;
1.123239 +  int iColLen;
1.123240 +  int rc;
1.123241 +  Fts3Expr *pRet = 0;
1.123242 +
1.123243 +  const char *zInput = z;
1.123244 +  int nInput = n;
1.123245 +
1.123246 +  pParse->isNot = 0;
1.123247 +
1.123248 +  /* Skip over any whitespace before checking for a keyword, an open or
1.123249 +  ** close bracket, or a quoted string. 
1.123250 +  */
1.123251 +  while( nInput>0 && fts3isspace(*zInput) ){
1.123252 +    nInput--;
1.123253 +    zInput++;
1.123254 +  }
1.123255 +  if( nInput==0 ){
1.123256 +    return SQLITE_DONE;
1.123257 +  }
1.123258 +
1.123259 +  /* See if we are dealing with a keyword. */
1.123260 +  for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
1.123261 +    const struct Fts3Keyword *pKey = &aKeyword[ii];
1.123262 +
1.123263 +    if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
1.123264 +      continue;
1.123265 +    }
1.123266 +
1.123267 +    if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
1.123268 +      int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
1.123269 +      int nKey = pKey->n;
1.123270 +      char cNext;
1.123271 +
1.123272 +      /* If this is a "NEAR" keyword, check for an explicit nearness. */
1.123273 +      if( pKey->eType==FTSQUERY_NEAR ){
1.123274 +        assert( nKey==4 );
1.123275 +        if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
1.123276 +          nNear = 0;
1.123277 +          for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
1.123278 +            nNear = nNear * 10 + (zInput[nKey] - '0');
1.123279 +          }
1.123280 +        }
1.123281 +      }
1.123282 +
1.123283 +      /* At this point this is probably a keyword. But for that to be true,
1.123284 +      ** the next byte must contain either whitespace, an open or close
1.123285 +      ** parenthesis, a quote character, or EOF. 
1.123286 +      */
1.123287 +      cNext = zInput[nKey];
1.123288 +      if( fts3isspace(cNext) 
1.123289 +       || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
1.123290 +      ){
1.123291 +        pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
1.123292 +        if( !pRet ){
1.123293 +          return SQLITE_NOMEM;
1.123294 +        }
1.123295 +        pRet->eType = pKey->eType;
1.123296 +        pRet->nNear = nNear;
1.123297 +        *ppExpr = pRet;
1.123298 +        *pnConsumed = (int)((zInput - z) + nKey);
1.123299 +        return SQLITE_OK;
1.123300 +      }
1.123301 +
1.123302 +      /* Turns out that wasn't a keyword after all. This happens if the
1.123303 +      ** user has supplied a token such as "ORacle". Continue.
1.123304 +      */
1.123305 +    }
1.123306 +  }
1.123307 +
1.123308 +  /* Check for an open bracket. */
1.123309 +  if( sqlite3_fts3_enable_parentheses ){
1.123310 +    if( *zInput=='(' ){
1.123311 +      int nConsumed;
1.123312 +      pParse->nNest++;
1.123313 +      rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
1.123314 +      if( rc==SQLITE_OK && !*ppExpr ){
1.123315 +        rc = SQLITE_DONE;
1.123316 +      }
1.123317 +      *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
1.123318 +      return rc;
1.123319 +    }
1.123320 +  
1.123321 +    /* Check for a close bracket. */
1.123322 +    if( *zInput==')' ){
1.123323 +      pParse->nNest--;
1.123324 +      *pnConsumed = (int)((zInput - z) + 1);
1.123325 +      return SQLITE_DONE;
1.123326 +    }
1.123327 +  }
1.123328 +
1.123329 +  /* See if we are dealing with a quoted phrase. If this is the case, then
1.123330 +  ** search for the closing quote and pass the whole string to getNextString()
1.123331 +  ** for processing. This is easy to do, as fts3 has no syntax for escaping
1.123332 +  ** a quote character embedded in a string.
1.123333 +  */
1.123334 +  if( *zInput=='"' ){
1.123335 +    for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
1.123336 +    *pnConsumed = (int)((zInput - z) + ii + 1);
1.123337 +    if( ii==nInput ){
1.123338 +      return SQLITE_ERROR;
1.123339 +    }
1.123340 +    return getNextString(pParse, &zInput[1], ii-1, ppExpr);
1.123341 +  }
1.123342 +
1.123343 +
1.123344 +  /* If control flows to this point, this must be a regular token, or 
1.123345 +  ** the end of the input. Read a regular token using the sqlite3_tokenizer
1.123346 +  ** interface. Before doing so, figure out if there is an explicit
1.123347 +  ** column specifier for the token. 
1.123348 +  **
1.123349 +  ** TODO: Strangely, it is not possible to associate a column specifier
1.123350 +  ** with a quoted phrase, only with a single token. Not sure if this was
1.123351 +  ** an implementation artifact or an intentional decision when fts3 was
1.123352 +  ** first implemented. Whichever it was, this module duplicates the 
1.123353 +  ** limitation.
1.123354 +  */
1.123355 +  iCol = pParse->iDefaultCol;
1.123356 +  iColLen = 0;
1.123357 +  for(ii=0; ii<pParse->nCol; ii++){
1.123358 +    const char *zStr = pParse->azCol[ii];
1.123359 +    int nStr = (int)strlen(zStr);
1.123360 +    if( nInput>nStr && zInput[nStr]==':' 
1.123361 +     && sqlite3_strnicmp(zStr, zInput, nStr)==0 
1.123362 +    ){
1.123363 +      iCol = ii;
1.123364 +      iColLen = (int)((zInput - z) + nStr + 1);
1.123365 +      break;
1.123366 +    }
1.123367 +  }
1.123368 +  rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
1.123369 +  *pnConsumed += iColLen;
1.123370 +  return rc;
1.123371 +}
1.123372 +
1.123373 +/*
1.123374 +** The argument is an Fts3Expr structure for a binary operator (any type
1.123375 +** except an FTSQUERY_PHRASE). Return an integer value representing the
1.123376 +** precedence of the operator. Lower values have a higher precedence (i.e.
1.123377 +** group more tightly). For example, in the C language, the == operator
1.123378 +** groups more tightly than ||, and would therefore have a higher precedence.
1.123379 +**
1.123380 +** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
1.123381 +** is defined), the order of the operators in precedence from highest to
1.123382 +** lowest is:
1.123383 +**
1.123384 +**   NEAR
1.123385 +**   NOT
1.123386 +**   AND (including implicit ANDs)
1.123387 +**   OR
1.123388 +**
1.123389 +** Note that when using the old query syntax, the OR operator has a higher
1.123390 +** precedence than the AND operator.
1.123391 +*/
1.123392 +static int opPrecedence(Fts3Expr *p){
1.123393 +  assert( p->eType!=FTSQUERY_PHRASE );
1.123394 +  if( sqlite3_fts3_enable_parentheses ){
1.123395 +    return p->eType;
1.123396 +  }else if( p->eType==FTSQUERY_NEAR ){
1.123397 +    return 1;
1.123398 +  }else if( p->eType==FTSQUERY_OR ){
1.123399 +    return 2;
1.123400 +  }
1.123401 +  assert( p->eType==FTSQUERY_AND );
1.123402 +  return 3;
1.123403 +}
1.123404 +
1.123405 +/*
1.123406 +** Argument ppHead contains a pointer to the current head of a query 
1.123407 +** expression tree being parsed. pPrev is the expression node most recently
1.123408 +** inserted into the tree. This function adds pNew, which is always a binary
1.123409 +** operator node, into the expression tree based on the relative precedence
1.123410 +** of pNew and the existing nodes of the tree. This may result in the head
1.123411 +** of the tree changing, in which case *ppHead is set to the new root node.
1.123412 +*/
1.123413 +static void insertBinaryOperator(
1.123414 +  Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
1.123415 +  Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
1.123416 +  Fts3Expr *pNew           /* New binary node to insert into expression tree */
1.123417 +){
1.123418 +  Fts3Expr *pSplit = pPrev;
1.123419 +  while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
1.123420 +    pSplit = pSplit->pParent;
1.123421 +  }
1.123422 +
1.123423 +  if( pSplit->pParent ){
1.123424 +    assert( pSplit->pParent->pRight==pSplit );
1.123425 +    pSplit->pParent->pRight = pNew;
1.123426 +    pNew->pParent = pSplit->pParent;
1.123427 +  }else{
1.123428 +    *ppHead = pNew;
1.123429 +  }
1.123430 +  pNew->pLeft = pSplit;
1.123431 +  pSplit->pParent = pNew;
1.123432 +}
1.123433 +
1.123434 +/*
1.123435 +** Parse the fts3 query expression found in buffer z, length n. This function
1.123436 +** returns either when the end of the buffer is reached or an unmatched 
1.123437 +** closing bracket - ')' - is encountered.
1.123438 +**
1.123439 +** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
1.123440 +** parsed form of the expression and *pnConsumed is set to the number of
1.123441 +** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
1.123442 +** (out of memory error) or SQLITE_ERROR (parse error) is returned.
1.123443 +*/
1.123444 +static int fts3ExprParse(
1.123445 +  ParseContext *pParse,                   /* fts3 query parse context */
1.123446 +  const char *z, int n,                   /* Text of MATCH query */
1.123447 +  Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
1.123448 +  int *pnConsumed                         /* OUT: Number of bytes consumed */
1.123449 +){
1.123450 +  Fts3Expr *pRet = 0;
1.123451 +  Fts3Expr *pPrev = 0;
1.123452 +  Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
1.123453 +  int nIn = n;
1.123454 +  const char *zIn = z;
1.123455 +  int rc = SQLITE_OK;
1.123456 +  int isRequirePhrase = 1;
1.123457 +
1.123458 +  while( rc==SQLITE_OK ){
1.123459 +    Fts3Expr *p = 0;
1.123460 +    int nByte = 0;
1.123461 +    rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
1.123462 +    if( rc==SQLITE_OK ){
1.123463 +      int isPhrase;
1.123464 +
1.123465 +      if( !sqlite3_fts3_enable_parentheses 
1.123466 +       && p->eType==FTSQUERY_PHRASE && pParse->isNot 
1.123467 +      ){
1.123468 +        /* Create an implicit NOT operator. */
1.123469 +        Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
1.123470 +        if( !pNot ){
1.123471 +          sqlite3Fts3ExprFree(p);
1.123472 +          rc = SQLITE_NOMEM;
1.123473 +          goto exprparse_out;
1.123474 +        }
1.123475 +        pNot->eType = FTSQUERY_NOT;
1.123476 +        pNot->pRight = p;
1.123477 +        if( pNotBranch ){
1.123478 +          pNot->pLeft = pNotBranch;
1.123479 +        }
1.123480 +        pNotBranch = pNot;
1.123481 +        p = pPrev;
1.123482 +      }else{
1.123483 +        int eType = p->eType;
1.123484 +        isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
1.123485 +
1.123486 +        /* The isRequirePhrase variable is set to true if a phrase or
1.123487 +        ** an expression contained in parenthesis is required. If a
1.123488 +        ** binary operator (AND, OR, NOT or NEAR) is encounted when
1.123489 +        ** isRequirePhrase is set, this is a syntax error.
1.123490 +        */
1.123491 +        if( !isPhrase && isRequirePhrase ){
1.123492 +          sqlite3Fts3ExprFree(p);
1.123493 +          rc = SQLITE_ERROR;
1.123494 +          goto exprparse_out;
1.123495 +        }
1.123496 +  
1.123497 +        if( isPhrase && !isRequirePhrase ){
1.123498 +          /* Insert an implicit AND operator. */
1.123499 +          Fts3Expr *pAnd;
1.123500 +          assert( pRet && pPrev );
1.123501 +          pAnd = fts3MallocZero(sizeof(Fts3Expr));
1.123502 +          if( !pAnd ){
1.123503 +            sqlite3Fts3ExprFree(p);
1.123504 +            rc = SQLITE_NOMEM;
1.123505 +            goto exprparse_out;
1.123506 +          }
1.123507 +          pAnd->eType = FTSQUERY_AND;
1.123508 +          insertBinaryOperator(&pRet, pPrev, pAnd);
1.123509 +          pPrev = pAnd;
1.123510 +        }
1.123511 +
1.123512 +        /* This test catches attempts to make either operand of a NEAR
1.123513 +        ** operator something other than a phrase. For example, either of
1.123514 +        ** the following:
1.123515 +        **
1.123516 +        **    (bracketed expression) NEAR phrase
1.123517 +        **    phrase NEAR (bracketed expression)
1.123518 +        **
1.123519 +        ** Return an error in either case.
1.123520 +        */
1.123521 +        if( pPrev && (
1.123522 +            (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
1.123523 +         || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
1.123524 +        )){
1.123525 +          sqlite3Fts3ExprFree(p);
1.123526 +          rc = SQLITE_ERROR;
1.123527 +          goto exprparse_out;
1.123528 +        }
1.123529 +  
1.123530 +        if( isPhrase ){
1.123531 +          if( pRet ){
1.123532 +            assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
1.123533 +            pPrev->pRight = p;
1.123534 +            p->pParent = pPrev;
1.123535 +          }else{
1.123536 +            pRet = p;
1.123537 +          }
1.123538 +        }else{
1.123539 +          insertBinaryOperator(&pRet, pPrev, p);
1.123540 +        }
1.123541 +        isRequirePhrase = !isPhrase;
1.123542 +      }
1.123543 +      assert( nByte>0 );
1.123544 +    }
1.123545 +    assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
1.123546 +    nIn -= nByte;
1.123547 +    zIn += nByte;
1.123548 +    pPrev = p;
1.123549 +  }
1.123550 +
1.123551 +  if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
1.123552 +    rc = SQLITE_ERROR;
1.123553 +  }
1.123554 +
1.123555 +  if( rc==SQLITE_DONE ){
1.123556 +    rc = SQLITE_OK;
1.123557 +    if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
1.123558 +      if( !pRet ){
1.123559 +        rc = SQLITE_ERROR;
1.123560 +      }else{
1.123561 +        Fts3Expr *pIter = pNotBranch;
1.123562 +        while( pIter->pLeft ){
1.123563 +          pIter = pIter->pLeft;
1.123564 +        }
1.123565 +        pIter->pLeft = pRet;
1.123566 +        pRet = pNotBranch;
1.123567 +      }
1.123568 +    }
1.123569 +  }
1.123570 +  *pnConsumed = n - nIn;
1.123571 +
1.123572 +exprparse_out:
1.123573 +  if( rc!=SQLITE_OK ){
1.123574 +    sqlite3Fts3ExprFree(pRet);
1.123575 +    sqlite3Fts3ExprFree(pNotBranch);
1.123576 +    pRet = 0;
1.123577 +  }
1.123578 +  *ppExpr = pRet;
1.123579 +  return rc;
1.123580 +}
1.123581 +
1.123582 +/*
1.123583 +** Parameters z and n contain a pointer to and length of a buffer containing
1.123584 +** an fts3 query expression, respectively. This function attempts to parse the
1.123585 +** query expression and create a tree of Fts3Expr structures representing the
1.123586 +** parsed expression. If successful, *ppExpr is set to point to the head
1.123587 +** of the parsed expression tree and SQLITE_OK is returned. If an error
1.123588 +** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
1.123589 +** error) is returned and *ppExpr is set to 0.
1.123590 +**
1.123591 +** If parameter n is a negative number, then z is assumed to point to a
1.123592 +** nul-terminated string and the length is determined using strlen().
1.123593 +**
1.123594 +** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
1.123595 +** use to normalize query tokens while parsing the expression. The azCol[]
1.123596 +** array, which is assumed to contain nCol entries, should contain the names
1.123597 +** of each column in the target fts3 table, in order from left to right. 
1.123598 +** Column names must be nul-terminated strings.
1.123599 +**
1.123600 +** The iDefaultCol parameter should be passed the index of the table column
1.123601 +** that appears on the left-hand-side of the MATCH operator (the default
1.123602 +** column to match against for tokens for which a column name is not explicitly
1.123603 +** specified as part of the query string), or -1 if tokens may by default
1.123604 +** match any table column.
1.123605 +*/
1.123606 +SQLITE_PRIVATE int sqlite3Fts3ExprParse(
1.123607 +  sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
1.123608 +  int iLangid,                        /* Language id for tokenizer */
1.123609 +  char **azCol,                       /* Array of column names for fts3 table */
1.123610 +  int bFts4,                          /* True to allow FTS4-only syntax */
1.123611 +  int nCol,                           /* Number of entries in azCol[] */
1.123612 +  int iDefaultCol,                    /* Default column to query */
1.123613 +  const char *z, int n,               /* Text of MATCH query */
1.123614 +  Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
1.123615 +){
1.123616 +  int nParsed;
1.123617 +  int rc;
1.123618 +  ParseContext sParse;
1.123619 +
1.123620 +  memset(&sParse, 0, sizeof(ParseContext));
1.123621 +  sParse.pTokenizer = pTokenizer;
1.123622 +  sParse.iLangid = iLangid;
1.123623 +  sParse.azCol = (const char **)azCol;
1.123624 +  sParse.nCol = nCol;
1.123625 +  sParse.iDefaultCol = iDefaultCol;
1.123626 +  sParse.bFts4 = bFts4;
1.123627 +  if( z==0 ){
1.123628 +    *ppExpr = 0;
1.123629 +    return SQLITE_OK;
1.123630 +  }
1.123631 +  if( n<0 ){
1.123632 +    n = (int)strlen(z);
1.123633 +  }
1.123634 +  rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
1.123635 +
1.123636 +  /* Check for mismatched parenthesis */
1.123637 +  if( rc==SQLITE_OK && sParse.nNest ){
1.123638 +    rc = SQLITE_ERROR;
1.123639 +    sqlite3Fts3ExprFree(*ppExpr);
1.123640 +    *ppExpr = 0;
1.123641 +  }
1.123642 +
1.123643 +  return rc;
1.123644 +}
1.123645 +
1.123646 +/*
1.123647 +** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
1.123648 +*/
1.123649 +SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
1.123650 +  if( p ){
1.123651 +    assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
1.123652 +    sqlite3Fts3ExprFree(p->pLeft);
1.123653 +    sqlite3Fts3ExprFree(p->pRight);
1.123654 +    sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
1.123655 +    sqlite3_free(p->aMI);
1.123656 +    sqlite3_free(p);
1.123657 +  }
1.123658 +}
1.123659 +
1.123660 +/****************************************************************************
1.123661 +*****************************************************************************
1.123662 +** Everything after this point is just test code.
1.123663 +*/
1.123664 +
1.123665 +#ifdef SQLITE_TEST
1.123666 +
1.123667 +/* #include <stdio.h> */
1.123668 +
1.123669 +/*
1.123670 +** Function to query the hash-table of tokenizers (see README.tokenizers).
1.123671 +*/
1.123672 +static int queryTestTokenizer(
1.123673 +  sqlite3 *db, 
1.123674 +  const char *zName,  
1.123675 +  const sqlite3_tokenizer_module **pp
1.123676 +){
1.123677 +  int rc;
1.123678 +  sqlite3_stmt *pStmt;
1.123679 +  const char zSql[] = "SELECT fts3_tokenizer(?)";
1.123680 +
1.123681 +  *pp = 0;
1.123682 +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1.123683 +  if( rc!=SQLITE_OK ){
1.123684 +    return rc;
1.123685 +  }
1.123686 +
1.123687 +  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
1.123688 +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
1.123689 +    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
1.123690 +      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
1.123691 +    }
1.123692 +  }
1.123693 +
1.123694 +  return sqlite3_finalize(pStmt);
1.123695 +}
1.123696 +
1.123697 +/*
1.123698 +** Return a pointer to a buffer containing a text representation of the
1.123699 +** expression passed as the first argument. The buffer is obtained from
1.123700 +** sqlite3_malloc(). It is the responsibility of the caller to use 
1.123701 +** sqlite3_free() to release the memory. If an OOM condition is encountered,
1.123702 +** NULL is returned.
1.123703 +**
1.123704 +** If the second argument is not NULL, then its contents are prepended to 
1.123705 +** the returned expression text and then freed using sqlite3_free().
1.123706 +*/
1.123707 +static char *exprToString(Fts3Expr *pExpr, char *zBuf){
1.123708 +  switch( pExpr->eType ){
1.123709 +    case FTSQUERY_PHRASE: {
1.123710 +      Fts3Phrase *pPhrase = pExpr->pPhrase;
1.123711 +      int i;
1.123712 +      zBuf = sqlite3_mprintf(
1.123713 +          "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
1.123714 +      for(i=0; zBuf && i<pPhrase->nToken; i++){
1.123715 +        zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, 
1.123716 +            pPhrase->aToken[i].n, pPhrase->aToken[i].z,
1.123717 +            (pPhrase->aToken[i].isPrefix?"+":"")
1.123718 +        );
1.123719 +      }
1.123720 +      return zBuf;
1.123721 +    }
1.123722 +
1.123723 +    case FTSQUERY_NEAR:
1.123724 +      zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
1.123725 +      break;
1.123726 +    case FTSQUERY_NOT:
1.123727 +      zBuf = sqlite3_mprintf("%zNOT ", zBuf);
1.123728 +      break;
1.123729 +    case FTSQUERY_AND:
1.123730 +      zBuf = sqlite3_mprintf("%zAND ", zBuf);
1.123731 +      break;
1.123732 +    case FTSQUERY_OR:
1.123733 +      zBuf = sqlite3_mprintf("%zOR ", zBuf);
1.123734 +      break;
1.123735 +  }
1.123736 +
1.123737 +  if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
1.123738 +  if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
1.123739 +  if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
1.123740 +
1.123741 +  if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
1.123742 +  if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
1.123743 +
1.123744 +  return zBuf;
1.123745 +}
1.123746 +
1.123747 +/*
1.123748 +** This is the implementation of a scalar SQL function used to test the 
1.123749 +** expression parser. It should be called as follows:
1.123750 +**
1.123751 +**   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
1.123752 +**
1.123753 +** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
1.123754 +** to parse the query expression (see README.tokenizers). The second argument
1.123755 +** is the query expression to parse. Each subsequent argument is the name
1.123756 +** of a column of the fts3 table that the query expression may refer to.
1.123757 +** For example:
1.123758 +**
1.123759 +**   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
1.123760 +*/
1.123761 +static void fts3ExprTest(
1.123762 +  sqlite3_context *context,
1.123763 +  int argc,
1.123764 +  sqlite3_value **argv
1.123765 +){
1.123766 +  sqlite3_tokenizer_module const *pModule = 0;
1.123767 +  sqlite3_tokenizer *pTokenizer = 0;
1.123768 +  int rc;
1.123769 +  char **azCol = 0;
1.123770 +  const char *zExpr;
1.123771 +  int nExpr;
1.123772 +  int nCol;
1.123773 +  int ii;
1.123774 +  Fts3Expr *pExpr;
1.123775 +  char *zBuf = 0;
1.123776 +  sqlite3 *db = sqlite3_context_db_handle(context);
1.123777 +
1.123778 +  if( argc<3 ){
1.123779 +    sqlite3_result_error(context, 
1.123780 +        "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
1.123781 +    );
1.123782 +    return;
1.123783 +  }
1.123784 +
1.123785 +  rc = queryTestTokenizer(db,
1.123786 +                          (const char *)sqlite3_value_text(argv[0]), &pModule);
1.123787 +  if( rc==SQLITE_NOMEM ){
1.123788 +    sqlite3_result_error_nomem(context);
1.123789 +    goto exprtest_out;
1.123790 +  }else if( !pModule ){
1.123791 +    sqlite3_result_error(context, "No such tokenizer module", -1);
1.123792 +    goto exprtest_out;
1.123793 +  }
1.123794 +
1.123795 +  rc = pModule->xCreate(0, 0, &pTokenizer);
1.123796 +  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
1.123797 +  if( rc==SQLITE_NOMEM ){
1.123798 +    sqlite3_result_error_nomem(context);
1.123799 +    goto exprtest_out;
1.123800 +  }
1.123801 +  pTokenizer->pModule = pModule;
1.123802 +
1.123803 +  zExpr = (const char *)sqlite3_value_text(argv[1]);
1.123804 +  nExpr = sqlite3_value_bytes(argv[1]);
1.123805 +  nCol = argc-2;
1.123806 +  azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
1.123807 +  if( !azCol ){
1.123808 +    sqlite3_result_error_nomem(context);
1.123809 +    goto exprtest_out;
1.123810 +  }
1.123811 +  for(ii=0; ii<nCol; ii++){
1.123812 +    azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
1.123813 +  }
1.123814 +
1.123815 +  rc = sqlite3Fts3ExprParse(
1.123816 +      pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
1.123817 +  );
1.123818 +  if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
1.123819 +    sqlite3_result_error(context, "Error parsing expression", -1);
1.123820 +  }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
1.123821 +    sqlite3_result_error_nomem(context);
1.123822 +  }else{
1.123823 +    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
1.123824 +    sqlite3_free(zBuf);
1.123825 +  }
1.123826 +
1.123827 +  sqlite3Fts3ExprFree(pExpr);
1.123828 +
1.123829 +exprtest_out:
1.123830 +  if( pModule && pTokenizer ){
1.123831 +    rc = pModule->xDestroy(pTokenizer);
1.123832 +  }
1.123833 +  sqlite3_free(azCol);
1.123834 +}
1.123835 +
1.123836 +/*
1.123837 +** Register the query expression parser test function fts3_exprtest() 
1.123838 +** with database connection db. 
1.123839 +*/
1.123840 +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
1.123841 +  return sqlite3_create_function(
1.123842 +      db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
1.123843 +  );
1.123844 +}
1.123845 +
1.123846 +#endif
1.123847 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.123848 +
1.123849 +/************** End of fts3_expr.c *******************************************/
1.123850 +/************** Begin file fts3_hash.c ***************************************/
1.123851 +/*
1.123852 +** 2001 September 22
1.123853 +**
1.123854 +** The author disclaims copyright to this source code.  In place of
1.123855 +** a legal notice, here is a blessing:
1.123856 +**
1.123857 +**    May you do good and not evil.
1.123858 +**    May you find forgiveness for yourself and forgive others.
1.123859 +**    May you share freely, never taking more than you give.
1.123860 +**
1.123861 +*************************************************************************
1.123862 +** This is the implementation of generic hash-tables used in SQLite.
1.123863 +** We've modified it slightly to serve as a standalone hash table
1.123864 +** implementation for the full-text indexing module.
1.123865 +*/
1.123866 +
1.123867 +/*
1.123868 +** The code in this file is only compiled if:
1.123869 +**
1.123870 +**     * The FTS3 module is being built as an extension
1.123871 +**       (in which case SQLITE_CORE is not defined), or
1.123872 +**
1.123873 +**     * The FTS3 module is being built into the core of
1.123874 +**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
1.123875 +*/
1.123876 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.123877 +
1.123878 +/* #include <assert.h> */
1.123879 +/* #include <stdlib.h> */
1.123880 +/* #include <string.h> */
1.123881 +
1.123882 +
1.123883 +/*
1.123884 +** Malloc and Free functions
1.123885 +*/
1.123886 +static void *fts3HashMalloc(int n){
1.123887 +  void *p = sqlite3_malloc(n);
1.123888 +  if( p ){
1.123889 +    memset(p, 0, n);
1.123890 +  }
1.123891 +  return p;
1.123892 +}
1.123893 +static void fts3HashFree(void *p){
1.123894 +  sqlite3_free(p);
1.123895 +}
1.123896 +
1.123897 +/* Turn bulk memory into a hash table object by initializing the
1.123898 +** fields of the Hash structure.
1.123899 +**
1.123900 +** "pNew" is a pointer to the hash table that is to be initialized.
1.123901 +** keyClass is one of the constants 
1.123902 +** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass 
1.123903 +** determines what kind of key the hash table will use.  "copyKey" is
1.123904 +** true if the hash table should make its own private copy of keys and
1.123905 +** false if it should just use the supplied pointer.
1.123906 +*/
1.123907 +SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
1.123908 +  assert( pNew!=0 );
1.123909 +  assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
1.123910 +  pNew->keyClass = keyClass;
1.123911 +  pNew->copyKey = copyKey;
1.123912 +  pNew->first = 0;
1.123913 +  pNew->count = 0;
1.123914 +  pNew->htsize = 0;
1.123915 +  pNew->ht = 0;
1.123916 +}
1.123917 +
1.123918 +/* Remove all entries from a hash table.  Reclaim all memory.
1.123919 +** Call this routine to delete a hash table or to reset a hash table
1.123920 +** to the empty state.
1.123921 +*/
1.123922 +SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
1.123923 +  Fts3HashElem *elem;         /* For looping over all elements of the table */
1.123924 +
1.123925 +  assert( pH!=0 );
1.123926 +  elem = pH->first;
1.123927 +  pH->first = 0;
1.123928 +  fts3HashFree(pH->ht);
1.123929 +  pH->ht = 0;
1.123930 +  pH->htsize = 0;
1.123931 +  while( elem ){
1.123932 +    Fts3HashElem *next_elem = elem->next;
1.123933 +    if( pH->copyKey && elem->pKey ){
1.123934 +      fts3HashFree(elem->pKey);
1.123935 +    }
1.123936 +    fts3HashFree(elem);
1.123937 +    elem = next_elem;
1.123938 +  }
1.123939 +  pH->count = 0;
1.123940 +}
1.123941 +
1.123942 +/*
1.123943 +** Hash and comparison functions when the mode is FTS3_HASH_STRING
1.123944 +*/
1.123945 +static int fts3StrHash(const void *pKey, int nKey){
1.123946 +  const char *z = (const char *)pKey;
1.123947 +  int h = 0;
1.123948 +  if( nKey<=0 ) nKey = (int) strlen(z);
1.123949 +  while( nKey > 0  ){
1.123950 +    h = (h<<3) ^ h ^ *z++;
1.123951 +    nKey--;
1.123952 +  }
1.123953 +  return h & 0x7fffffff;
1.123954 +}
1.123955 +static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
1.123956 +  if( n1!=n2 ) return 1;
1.123957 +  return strncmp((const char*)pKey1,(const char*)pKey2,n1);
1.123958 +}
1.123959 +
1.123960 +/*
1.123961 +** Hash and comparison functions when the mode is FTS3_HASH_BINARY
1.123962 +*/
1.123963 +static int fts3BinHash(const void *pKey, int nKey){
1.123964 +  int h = 0;
1.123965 +  const char *z = (const char *)pKey;
1.123966 +  while( nKey-- > 0 ){
1.123967 +    h = (h<<3) ^ h ^ *(z++);
1.123968 +  }
1.123969 +  return h & 0x7fffffff;
1.123970 +}
1.123971 +static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
1.123972 +  if( n1!=n2 ) return 1;
1.123973 +  return memcmp(pKey1,pKey2,n1);
1.123974 +}
1.123975 +
1.123976 +/*
1.123977 +** Return a pointer to the appropriate hash function given the key class.
1.123978 +**
1.123979 +** The C syntax in this function definition may be unfamilar to some 
1.123980 +** programmers, so we provide the following additional explanation:
1.123981 +**
1.123982 +** The name of the function is "ftsHashFunction".  The function takes a
1.123983 +** single parameter "keyClass".  The return value of ftsHashFunction()
1.123984 +** is a pointer to another function.  Specifically, the return value
1.123985 +** of ftsHashFunction() is a pointer to a function that takes two parameters
1.123986 +** with types "const void*" and "int" and returns an "int".
1.123987 +*/
1.123988 +static int (*ftsHashFunction(int keyClass))(const void*,int){
1.123989 +  if( keyClass==FTS3_HASH_STRING ){
1.123990 +    return &fts3StrHash;
1.123991 +  }else{
1.123992 +    assert( keyClass==FTS3_HASH_BINARY );
1.123993 +    return &fts3BinHash;
1.123994 +  }
1.123995 +}
1.123996 +
1.123997 +/*
1.123998 +** Return a pointer to the appropriate hash function given the key class.
1.123999 +**
1.124000 +** For help in interpreted the obscure C code in the function definition,
1.124001 +** see the header comment on the previous function.
1.124002 +*/
1.124003 +static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
1.124004 +  if( keyClass==FTS3_HASH_STRING ){
1.124005 +    return &fts3StrCompare;
1.124006 +  }else{
1.124007 +    assert( keyClass==FTS3_HASH_BINARY );
1.124008 +    return &fts3BinCompare;
1.124009 +  }
1.124010 +}
1.124011 +
1.124012 +/* Link an element into the hash table
1.124013 +*/
1.124014 +static void fts3HashInsertElement(
1.124015 +  Fts3Hash *pH,            /* The complete hash table */
1.124016 +  struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
1.124017 +  Fts3HashElem *pNew       /* The element to be inserted */
1.124018 +){
1.124019 +  Fts3HashElem *pHead;     /* First element already in pEntry */
1.124020 +  pHead = pEntry->chain;
1.124021 +  if( pHead ){
1.124022 +    pNew->next = pHead;
1.124023 +    pNew->prev = pHead->prev;
1.124024 +    if( pHead->prev ){ pHead->prev->next = pNew; }
1.124025 +    else             { pH->first = pNew; }
1.124026 +    pHead->prev = pNew;
1.124027 +  }else{
1.124028 +    pNew->next = pH->first;
1.124029 +    if( pH->first ){ pH->first->prev = pNew; }
1.124030 +    pNew->prev = 0;
1.124031 +    pH->first = pNew;
1.124032 +  }
1.124033 +  pEntry->count++;
1.124034 +  pEntry->chain = pNew;
1.124035 +}
1.124036 +
1.124037 +
1.124038 +/* Resize the hash table so that it cantains "new_size" buckets.
1.124039 +** "new_size" must be a power of 2.  The hash table might fail 
1.124040 +** to resize if sqliteMalloc() fails.
1.124041 +**
1.124042 +** Return non-zero if a memory allocation error occurs.
1.124043 +*/
1.124044 +static int fts3Rehash(Fts3Hash *pH, int new_size){
1.124045 +  struct _fts3ht *new_ht;          /* The new hash table */
1.124046 +  Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
1.124047 +  int (*xHash)(const void*,int);   /* The hash function */
1.124048 +
1.124049 +  assert( (new_size & (new_size-1))==0 );
1.124050 +  new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
1.124051 +  if( new_ht==0 ) return 1;
1.124052 +  fts3HashFree(pH->ht);
1.124053 +  pH->ht = new_ht;
1.124054 +  pH->htsize = new_size;
1.124055 +  xHash = ftsHashFunction(pH->keyClass);
1.124056 +  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
1.124057 +    int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
1.124058 +    next_elem = elem->next;
1.124059 +    fts3HashInsertElement(pH, &new_ht[h], elem);
1.124060 +  }
1.124061 +  return 0;
1.124062 +}
1.124063 +
1.124064 +/* This function (for internal use only) locates an element in an
1.124065 +** hash table that matches the given key.  The hash for this key has
1.124066 +** already been computed and is passed as the 4th parameter.
1.124067 +*/
1.124068 +static Fts3HashElem *fts3FindElementByHash(
1.124069 +  const Fts3Hash *pH, /* The pH to be searched */
1.124070 +  const void *pKey,   /* The key we are searching for */
1.124071 +  int nKey,
1.124072 +  int h               /* The hash for this key. */
1.124073 +){
1.124074 +  Fts3HashElem *elem;            /* Used to loop thru the element list */
1.124075 +  int count;                     /* Number of elements left to test */
1.124076 +  int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
1.124077 +
1.124078 +  if( pH->ht ){
1.124079 +    struct _fts3ht *pEntry = &pH->ht[h];
1.124080 +    elem = pEntry->chain;
1.124081 +    count = pEntry->count;
1.124082 +    xCompare = ftsCompareFunction(pH->keyClass);
1.124083 +    while( count-- && elem ){
1.124084 +      if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ 
1.124085 +        return elem;
1.124086 +      }
1.124087 +      elem = elem->next;
1.124088 +    }
1.124089 +  }
1.124090 +  return 0;
1.124091 +}
1.124092 +
1.124093 +/* Remove a single entry from the hash table given a pointer to that
1.124094 +** element and a hash on the element's key.
1.124095 +*/
1.124096 +static void fts3RemoveElementByHash(
1.124097 +  Fts3Hash *pH,         /* The pH containing "elem" */
1.124098 +  Fts3HashElem* elem,   /* The element to be removed from the pH */
1.124099 +  int h                 /* Hash value for the element */
1.124100 +){
1.124101 +  struct _fts3ht *pEntry;
1.124102 +  if( elem->prev ){
1.124103 +    elem->prev->next = elem->next; 
1.124104 +  }else{
1.124105 +    pH->first = elem->next;
1.124106 +  }
1.124107 +  if( elem->next ){
1.124108 +    elem->next->prev = elem->prev;
1.124109 +  }
1.124110 +  pEntry = &pH->ht[h];
1.124111 +  if( pEntry->chain==elem ){
1.124112 +    pEntry->chain = elem->next;
1.124113 +  }
1.124114 +  pEntry->count--;
1.124115 +  if( pEntry->count<=0 ){
1.124116 +    pEntry->chain = 0;
1.124117 +  }
1.124118 +  if( pH->copyKey && elem->pKey ){
1.124119 +    fts3HashFree(elem->pKey);
1.124120 +  }
1.124121 +  fts3HashFree( elem );
1.124122 +  pH->count--;
1.124123 +  if( pH->count<=0 ){
1.124124 +    assert( pH->first==0 );
1.124125 +    assert( pH->count==0 );
1.124126 +    fts3HashClear(pH);
1.124127 +  }
1.124128 +}
1.124129 +
1.124130 +SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
1.124131 +  const Fts3Hash *pH, 
1.124132 +  const void *pKey, 
1.124133 +  int nKey
1.124134 +){
1.124135 +  int h;                          /* A hash on key */
1.124136 +  int (*xHash)(const void*,int);  /* The hash function */
1.124137 +
1.124138 +  if( pH==0 || pH->ht==0 ) return 0;
1.124139 +  xHash = ftsHashFunction(pH->keyClass);
1.124140 +  assert( xHash!=0 );
1.124141 +  h = (*xHash)(pKey,nKey);
1.124142 +  assert( (pH->htsize & (pH->htsize-1))==0 );
1.124143 +  return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
1.124144 +}
1.124145 +
1.124146 +/* 
1.124147 +** Attempt to locate an element of the hash table pH with a key
1.124148 +** that matches pKey,nKey.  Return the data for this element if it is
1.124149 +** found, or NULL if there is no match.
1.124150 +*/
1.124151 +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
1.124152 +  Fts3HashElem *pElem;            /* The element that matches key (if any) */
1.124153 +
1.124154 +  pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
1.124155 +  return pElem ? pElem->data : 0;
1.124156 +}
1.124157 +
1.124158 +/* Insert an element into the hash table pH.  The key is pKey,nKey
1.124159 +** and the data is "data".
1.124160 +**
1.124161 +** If no element exists with a matching key, then a new
1.124162 +** element is created.  A copy of the key is made if the copyKey
1.124163 +** flag is set.  NULL is returned.
1.124164 +**
1.124165 +** If another element already exists with the same key, then the
1.124166 +** new data replaces the old data and the old data is returned.
1.124167 +** The key is not copied in this instance.  If a malloc fails, then
1.124168 +** the new data is returned and the hash table is unchanged.
1.124169 +**
1.124170 +** If the "data" parameter to this function is NULL, then the
1.124171 +** element corresponding to "key" is removed from the hash table.
1.124172 +*/
1.124173 +SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
1.124174 +  Fts3Hash *pH,        /* The hash table to insert into */
1.124175 +  const void *pKey,    /* The key */
1.124176 +  int nKey,            /* Number of bytes in the key */
1.124177 +  void *data           /* The data */
1.124178 +){
1.124179 +  int hraw;                 /* Raw hash value of the key */
1.124180 +  int h;                    /* the hash of the key modulo hash table size */
1.124181 +  Fts3HashElem *elem;       /* Used to loop thru the element list */
1.124182 +  Fts3HashElem *new_elem;   /* New element added to the pH */
1.124183 +  int (*xHash)(const void*,int);  /* The hash function */
1.124184 +
1.124185 +  assert( pH!=0 );
1.124186 +  xHash = ftsHashFunction(pH->keyClass);
1.124187 +  assert( xHash!=0 );
1.124188 +  hraw = (*xHash)(pKey, nKey);
1.124189 +  assert( (pH->htsize & (pH->htsize-1))==0 );
1.124190 +  h = hraw & (pH->htsize-1);
1.124191 +  elem = fts3FindElementByHash(pH,pKey,nKey,h);
1.124192 +  if( elem ){
1.124193 +    void *old_data = elem->data;
1.124194 +    if( data==0 ){
1.124195 +      fts3RemoveElementByHash(pH,elem,h);
1.124196 +    }else{
1.124197 +      elem->data = data;
1.124198 +    }
1.124199 +    return old_data;
1.124200 +  }
1.124201 +  if( data==0 ) return 0;
1.124202 +  if( (pH->htsize==0 && fts3Rehash(pH,8))
1.124203 +   || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
1.124204 +  ){
1.124205 +    pH->count = 0;
1.124206 +    return data;
1.124207 +  }
1.124208 +  assert( pH->htsize>0 );
1.124209 +  new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
1.124210 +  if( new_elem==0 ) return data;
1.124211 +  if( pH->copyKey && pKey!=0 ){
1.124212 +    new_elem->pKey = fts3HashMalloc( nKey );
1.124213 +    if( new_elem->pKey==0 ){
1.124214 +      fts3HashFree(new_elem);
1.124215 +      return data;
1.124216 +    }
1.124217 +    memcpy((void*)new_elem->pKey, pKey, nKey);
1.124218 +  }else{
1.124219 +    new_elem->pKey = (void*)pKey;
1.124220 +  }
1.124221 +  new_elem->nKey = nKey;
1.124222 +  pH->count++;
1.124223 +  assert( pH->htsize>0 );
1.124224 +  assert( (pH->htsize & (pH->htsize-1))==0 );
1.124225 +  h = hraw & (pH->htsize-1);
1.124226 +  fts3HashInsertElement(pH, &pH->ht[h], new_elem);
1.124227 +  new_elem->data = data;
1.124228 +  return 0;
1.124229 +}
1.124230 +
1.124231 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.124232 +
1.124233 +/************** End of fts3_hash.c *******************************************/
1.124234 +/************** Begin file fts3_porter.c *************************************/
1.124235 +/*
1.124236 +** 2006 September 30
1.124237 +**
1.124238 +** The author disclaims copyright to this source code.  In place of
1.124239 +** a legal notice, here is a blessing:
1.124240 +**
1.124241 +**    May you do good and not evil.
1.124242 +**    May you find forgiveness for yourself and forgive others.
1.124243 +**    May you share freely, never taking more than you give.
1.124244 +**
1.124245 +*************************************************************************
1.124246 +** Implementation of the full-text-search tokenizer that implements
1.124247 +** a Porter stemmer.
1.124248 +*/
1.124249 +
1.124250 +/*
1.124251 +** The code in this file is only compiled if:
1.124252 +**
1.124253 +**     * The FTS3 module is being built as an extension
1.124254 +**       (in which case SQLITE_CORE is not defined), or
1.124255 +**
1.124256 +**     * The FTS3 module is being built into the core of
1.124257 +**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
1.124258 +*/
1.124259 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.124260 +
1.124261 +/* #include <assert.h> */
1.124262 +/* #include <stdlib.h> */
1.124263 +/* #include <stdio.h> */
1.124264 +/* #include <string.h> */
1.124265 +
1.124266 +
1.124267 +/*
1.124268 +** Class derived from sqlite3_tokenizer
1.124269 +*/
1.124270 +typedef struct porter_tokenizer {
1.124271 +  sqlite3_tokenizer base;      /* Base class */
1.124272 +} porter_tokenizer;
1.124273 +
1.124274 +/*
1.124275 +** Class derived from sqlite3_tokenizer_cursor
1.124276 +*/
1.124277 +typedef struct porter_tokenizer_cursor {
1.124278 +  sqlite3_tokenizer_cursor base;
1.124279 +  const char *zInput;          /* input we are tokenizing */
1.124280 +  int nInput;                  /* size of the input */
1.124281 +  int iOffset;                 /* current position in zInput */
1.124282 +  int iToken;                  /* index of next token to be returned */
1.124283 +  char *zToken;                /* storage for current token */
1.124284 +  int nAllocated;              /* space allocated to zToken buffer */
1.124285 +} porter_tokenizer_cursor;
1.124286 +
1.124287 +
1.124288 +/*
1.124289 +** Create a new tokenizer instance.
1.124290 +*/
1.124291 +static int porterCreate(
1.124292 +  int argc, const char * const *argv,
1.124293 +  sqlite3_tokenizer **ppTokenizer
1.124294 +){
1.124295 +  porter_tokenizer *t;
1.124296 +
1.124297 +  UNUSED_PARAMETER(argc);
1.124298 +  UNUSED_PARAMETER(argv);
1.124299 +
1.124300 +  t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
1.124301 +  if( t==NULL ) return SQLITE_NOMEM;
1.124302 +  memset(t, 0, sizeof(*t));
1.124303 +  *ppTokenizer = &t->base;
1.124304 +  return SQLITE_OK;
1.124305 +}
1.124306 +
1.124307 +/*
1.124308 +** Destroy a tokenizer
1.124309 +*/
1.124310 +static int porterDestroy(sqlite3_tokenizer *pTokenizer){
1.124311 +  sqlite3_free(pTokenizer);
1.124312 +  return SQLITE_OK;
1.124313 +}
1.124314 +
1.124315 +/*
1.124316 +** Prepare to begin tokenizing a particular string.  The input
1.124317 +** string to be tokenized is zInput[0..nInput-1].  A cursor
1.124318 +** used to incrementally tokenize this string is returned in 
1.124319 +** *ppCursor.
1.124320 +*/
1.124321 +static int porterOpen(
1.124322 +  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
1.124323 +  const char *zInput, int nInput,        /* String to be tokenized */
1.124324 +  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
1.124325 +){
1.124326 +  porter_tokenizer_cursor *c;
1.124327 +
1.124328 +  UNUSED_PARAMETER(pTokenizer);
1.124329 +
1.124330 +  c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
1.124331 +  if( c==NULL ) return SQLITE_NOMEM;
1.124332 +
1.124333 +  c->zInput = zInput;
1.124334 +  if( zInput==0 ){
1.124335 +    c->nInput = 0;
1.124336 +  }else if( nInput<0 ){
1.124337 +    c->nInput = (int)strlen(zInput);
1.124338 +  }else{
1.124339 +    c->nInput = nInput;
1.124340 +  }
1.124341 +  c->iOffset = 0;                 /* start tokenizing at the beginning */
1.124342 +  c->iToken = 0;
1.124343 +  c->zToken = NULL;               /* no space allocated, yet. */
1.124344 +  c->nAllocated = 0;
1.124345 +
1.124346 +  *ppCursor = &c->base;
1.124347 +  return SQLITE_OK;
1.124348 +}
1.124349 +
1.124350 +/*
1.124351 +** Close a tokenization cursor previously opened by a call to
1.124352 +** porterOpen() above.
1.124353 +*/
1.124354 +static int porterClose(sqlite3_tokenizer_cursor *pCursor){
1.124355 +  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
1.124356 +  sqlite3_free(c->zToken);
1.124357 +  sqlite3_free(c);
1.124358 +  return SQLITE_OK;
1.124359 +}
1.124360 +/*
1.124361 +** Vowel or consonant
1.124362 +*/
1.124363 +static const char cType[] = {
1.124364 +   0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
1.124365 +   1, 1, 1, 2, 1
1.124366 +};
1.124367 +
1.124368 +/*
1.124369 +** isConsonant() and isVowel() determine if their first character in
1.124370 +** the string they point to is a consonant or a vowel, according
1.124371 +** to Porter ruls.  
1.124372 +**
1.124373 +** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
1.124374 +** 'Y' is a consonant unless it follows another consonant,
1.124375 +** in which case it is a vowel.
1.124376 +**
1.124377 +** In these routine, the letters are in reverse order.  So the 'y' rule
1.124378 +** is that 'y' is a consonant unless it is followed by another
1.124379 +** consonent.
1.124380 +*/
1.124381 +static int isVowel(const char*);
1.124382 +static int isConsonant(const char *z){
1.124383 +  int j;
1.124384 +  char x = *z;
1.124385 +  if( x==0 ) return 0;
1.124386 +  assert( x>='a' && x<='z' );
1.124387 +  j = cType[x-'a'];
1.124388 +  if( j<2 ) return j;
1.124389 +  return z[1]==0 || isVowel(z + 1);
1.124390 +}
1.124391 +static int isVowel(const char *z){
1.124392 +  int j;
1.124393 +  char x = *z;
1.124394 +  if( x==0 ) return 0;
1.124395 +  assert( x>='a' && x<='z' );
1.124396 +  j = cType[x-'a'];
1.124397 +  if( j<2 ) return 1-j;
1.124398 +  return isConsonant(z + 1);
1.124399 +}
1.124400 +
1.124401 +/*
1.124402 +** Let any sequence of one or more vowels be represented by V and let
1.124403 +** C be sequence of one or more consonants.  Then every word can be
1.124404 +** represented as:
1.124405 +**
1.124406 +**           [C] (VC){m} [V]
1.124407 +**
1.124408 +** In prose:  A word is an optional consonant followed by zero or
1.124409 +** vowel-consonant pairs followed by an optional vowel.  "m" is the
1.124410 +** number of vowel consonant pairs.  This routine computes the value
1.124411 +** of m for the first i bytes of a word.
1.124412 +**
1.124413 +** Return true if the m-value for z is 1 or more.  In other words,
1.124414 +** return true if z contains at least one vowel that is followed
1.124415 +** by a consonant.
1.124416 +**
1.124417 +** In this routine z[] is in reverse order.  So we are really looking
1.124418 +** for an instance of of a consonant followed by a vowel.
1.124419 +*/
1.124420 +static int m_gt_0(const char *z){
1.124421 +  while( isVowel(z) ){ z++; }
1.124422 +  if( *z==0 ) return 0;
1.124423 +  while( isConsonant(z) ){ z++; }
1.124424 +  return *z!=0;
1.124425 +}
1.124426 +
1.124427 +/* Like mgt0 above except we are looking for a value of m which is
1.124428 +** exactly 1
1.124429 +*/
1.124430 +static int m_eq_1(const char *z){
1.124431 +  while( isVowel(z) ){ z++; }
1.124432 +  if( *z==0 ) return 0;
1.124433 +  while( isConsonant(z) ){ z++; }
1.124434 +  if( *z==0 ) return 0;
1.124435 +  while( isVowel(z) ){ z++; }
1.124436 +  if( *z==0 ) return 1;
1.124437 +  while( isConsonant(z) ){ z++; }
1.124438 +  return *z==0;
1.124439 +}
1.124440 +
1.124441 +/* Like mgt0 above except we are looking for a value of m>1 instead
1.124442 +** or m>0
1.124443 +*/
1.124444 +static int m_gt_1(const char *z){
1.124445 +  while( isVowel(z) ){ z++; }
1.124446 +  if( *z==0 ) return 0;
1.124447 +  while( isConsonant(z) ){ z++; }
1.124448 +  if( *z==0 ) return 0;
1.124449 +  while( isVowel(z) ){ z++; }
1.124450 +  if( *z==0 ) return 0;
1.124451 +  while( isConsonant(z) ){ z++; }
1.124452 +  return *z!=0;
1.124453 +}
1.124454 +
1.124455 +/*
1.124456 +** Return TRUE if there is a vowel anywhere within z[0..n-1]
1.124457 +*/
1.124458 +static int hasVowel(const char *z){
1.124459 +  while( isConsonant(z) ){ z++; }
1.124460 +  return *z!=0;
1.124461 +}
1.124462 +
1.124463 +/*
1.124464 +** Return TRUE if the word ends in a double consonant.
1.124465 +**
1.124466 +** The text is reversed here. So we are really looking at
1.124467 +** the first two characters of z[].
1.124468 +*/
1.124469 +static int doubleConsonant(const char *z){
1.124470 +  return isConsonant(z) && z[0]==z[1];
1.124471 +}
1.124472 +
1.124473 +/*
1.124474 +** Return TRUE if the word ends with three letters which
1.124475 +** are consonant-vowel-consonent and where the final consonant
1.124476 +** is not 'w', 'x', or 'y'.
1.124477 +**
1.124478 +** The word is reversed here.  So we are really checking the
1.124479 +** first three letters and the first one cannot be in [wxy].
1.124480 +*/
1.124481 +static int star_oh(const char *z){
1.124482 +  return
1.124483 +    isConsonant(z) &&
1.124484 +    z[0]!='w' && z[0]!='x' && z[0]!='y' &&
1.124485 +    isVowel(z+1) &&
1.124486 +    isConsonant(z+2);
1.124487 +}
1.124488 +
1.124489 +/*
1.124490 +** If the word ends with zFrom and xCond() is true for the stem
1.124491 +** of the word that preceeds the zFrom ending, then change the 
1.124492 +** ending to zTo.
1.124493 +**
1.124494 +** The input word *pz and zFrom are both in reverse order.  zTo
1.124495 +** is in normal order. 
1.124496 +**
1.124497 +** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
1.124498 +** match.  Not that TRUE is returned even if xCond() fails and
1.124499 +** no substitution occurs.
1.124500 +*/
1.124501 +static int stem(
1.124502 +  char **pz,             /* The word being stemmed (Reversed) */
1.124503 +  const char *zFrom,     /* If the ending matches this... (Reversed) */
1.124504 +  const char *zTo,       /* ... change the ending to this (not reversed) */
1.124505 +  int (*xCond)(const char*)   /* Condition that must be true */
1.124506 +){
1.124507 +  char *z = *pz;
1.124508 +  while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
1.124509 +  if( *zFrom!=0 ) return 0;
1.124510 +  if( xCond && !xCond(z) ) return 1;
1.124511 +  while( *zTo ){
1.124512 +    *(--z) = *(zTo++);
1.124513 +  }
1.124514 +  *pz = z;
1.124515 +  return 1;
1.124516 +}
1.124517 +
1.124518 +/*
1.124519 +** This is the fallback stemmer used when the porter stemmer is
1.124520 +** inappropriate.  The input word is copied into the output with
1.124521 +** US-ASCII case folding.  If the input word is too long (more
1.124522 +** than 20 bytes if it contains no digits or more than 6 bytes if
1.124523 +** it contains digits) then word is truncated to 20 or 6 bytes
1.124524 +** by taking 10 or 3 bytes from the beginning and end.
1.124525 +*/
1.124526 +static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
1.124527 +  int i, mx, j;
1.124528 +  int hasDigit = 0;
1.124529 +  for(i=0; i<nIn; i++){
1.124530 +    char c = zIn[i];
1.124531 +    if( c>='A' && c<='Z' ){
1.124532 +      zOut[i] = c - 'A' + 'a';
1.124533 +    }else{
1.124534 +      if( c>='0' && c<='9' ) hasDigit = 1;
1.124535 +      zOut[i] = c;
1.124536 +    }
1.124537 +  }
1.124538 +  mx = hasDigit ? 3 : 10;
1.124539 +  if( nIn>mx*2 ){
1.124540 +    for(j=mx, i=nIn-mx; i<nIn; i++, j++){
1.124541 +      zOut[j] = zOut[i];
1.124542 +    }
1.124543 +    i = j;
1.124544 +  }
1.124545 +  zOut[i] = 0;
1.124546 +  *pnOut = i;
1.124547 +}
1.124548 +
1.124549 +
1.124550 +/*
1.124551 +** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
1.124552 +** zOut is at least big enough to hold nIn bytes.  Write the actual
1.124553 +** size of the output word (exclusive of the '\0' terminator) into *pnOut.
1.124554 +**
1.124555 +** Any upper-case characters in the US-ASCII character set ([A-Z])
1.124556 +** are converted to lower case.  Upper-case UTF characters are
1.124557 +** unchanged.
1.124558 +**
1.124559 +** Words that are longer than about 20 bytes are stemmed by retaining
1.124560 +** a few bytes from the beginning and the end of the word.  If the
1.124561 +** word contains digits, 3 bytes are taken from the beginning and
1.124562 +** 3 bytes from the end.  For long words without digits, 10 bytes
1.124563 +** are taken from each end.  US-ASCII case folding still applies.
1.124564 +** 
1.124565 +** If the input word contains not digits but does characters not 
1.124566 +** in [a-zA-Z] then no stemming is attempted and this routine just 
1.124567 +** copies the input into the input into the output with US-ASCII
1.124568 +** case folding.
1.124569 +**
1.124570 +** Stemming never increases the length of the word.  So there is
1.124571 +** no chance of overflowing the zOut buffer.
1.124572 +*/
1.124573 +static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
1.124574 +  int i, j;
1.124575 +  char zReverse[28];
1.124576 +  char *z, *z2;
1.124577 +  if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
1.124578 +    /* The word is too big or too small for the porter stemmer.
1.124579 +    ** Fallback to the copy stemmer */
1.124580 +    copy_stemmer(zIn, nIn, zOut, pnOut);
1.124581 +    return;
1.124582 +  }
1.124583 +  for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
1.124584 +    char c = zIn[i];
1.124585 +    if( c>='A' && c<='Z' ){
1.124586 +      zReverse[j] = c + 'a' - 'A';
1.124587 +    }else if( c>='a' && c<='z' ){
1.124588 +      zReverse[j] = c;
1.124589 +    }else{
1.124590 +      /* The use of a character not in [a-zA-Z] means that we fallback
1.124591 +      ** to the copy stemmer */
1.124592 +      copy_stemmer(zIn, nIn, zOut, pnOut);
1.124593 +      return;
1.124594 +    }
1.124595 +  }
1.124596 +  memset(&zReverse[sizeof(zReverse)-5], 0, 5);
1.124597 +  z = &zReverse[j+1];
1.124598 +
1.124599 +
1.124600 +  /* Step 1a */
1.124601 +  if( z[0]=='s' ){
1.124602 +    if(
1.124603 +     !stem(&z, "sess", "ss", 0) &&
1.124604 +     !stem(&z, "sei", "i", 0)  &&
1.124605 +     !stem(&z, "ss", "ss", 0)
1.124606 +    ){
1.124607 +      z++;
1.124608 +    }
1.124609 +  }
1.124610 +
1.124611 +  /* Step 1b */  
1.124612 +  z2 = z;
1.124613 +  if( stem(&z, "dee", "ee", m_gt_0) ){
1.124614 +    /* Do nothing.  The work was all in the test */
1.124615 +  }else if( 
1.124616 +     (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
1.124617 +      && z!=z2
1.124618 +  ){
1.124619 +     if( stem(&z, "ta", "ate", 0) ||
1.124620 +         stem(&z, "lb", "ble", 0) ||
1.124621 +         stem(&z, "zi", "ize", 0) ){
1.124622 +       /* Do nothing.  The work was all in the test */
1.124623 +     }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
1.124624 +       z++;
1.124625 +     }else if( m_eq_1(z) && star_oh(z) ){
1.124626 +       *(--z) = 'e';
1.124627 +     }
1.124628 +  }
1.124629 +
1.124630 +  /* Step 1c */
1.124631 +  if( z[0]=='y' && hasVowel(z+1) ){
1.124632 +    z[0] = 'i';
1.124633 +  }
1.124634 +
1.124635 +  /* Step 2 */
1.124636 +  switch( z[1] ){
1.124637 +   case 'a':
1.124638 +     stem(&z, "lanoita", "ate", m_gt_0) ||
1.124639 +     stem(&z, "lanoit", "tion", m_gt_0);
1.124640 +     break;
1.124641 +   case 'c':
1.124642 +     stem(&z, "icne", "ence", m_gt_0) ||
1.124643 +     stem(&z, "icna", "ance", m_gt_0);
1.124644 +     break;
1.124645 +   case 'e':
1.124646 +     stem(&z, "rezi", "ize", m_gt_0);
1.124647 +     break;
1.124648 +   case 'g':
1.124649 +     stem(&z, "igol", "log", m_gt_0);
1.124650 +     break;
1.124651 +   case 'l':
1.124652 +     stem(&z, "ilb", "ble", m_gt_0) ||
1.124653 +     stem(&z, "illa", "al", m_gt_0) ||
1.124654 +     stem(&z, "iltne", "ent", m_gt_0) ||
1.124655 +     stem(&z, "ile", "e", m_gt_0) ||
1.124656 +     stem(&z, "ilsuo", "ous", m_gt_0);
1.124657 +     break;
1.124658 +   case 'o':
1.124659 +     stem(&z, "noitazi", "ize", m_gt_0) ||
1.124660 +     stem(&z, "noita", "ate", m_gt_0) ||
1.124661 +     stem(&z, "rota", "ate", m_gt_0);
1.124662 +     break;
1.124663 +   case 's':
1.124664 +     stem(&z, "msila", "al", m_gt_0) ||
1.124665 +     stem(&z, "ssenevi", "ive", m_gt_0) ||
1.124666 +     stem(&z, "ssenluf", "ful", m_gt_0) ||
1.124667 +     stem(&z, "ssensuo", "ous", m_gt_0);
1.124668 +     break;
1.124669 +   case 't':
1.124670 +     stem(&z, "itila", "al", m_gt_0) ||
1.124671 +     stem(&z, "itivi", "ive", m_gt_0) ||
1.124672 +     stem(&z, "itilib", "ble", m_gt_0);
1.124673 +     break;
1.124674 +  }
1.124675 +
1.124676 +  /* Step 3 */
1.124677 +  switch( z[0] ){
1.124678 +   case 'e':
1.124679 +     stem(&z, "etaci", "ic", m_gt_0) ||
1.124680 +     stem(&z, "evita", "", m_gt_0)   ||
1.124681 +     stem(&z, "ezila", "al", m_gt_0);
1.124682 +     break;
1.124683 +   case 'i':
1.124684 +     stem(&z, "itici", "ic", m_gt_0);
1.124685 +     break;
1.124686 +   case 'l':
1.124687 +     stem(&z, "laci", "ic", m_gt_0) ||
1.124688 +     stem(&z, "luf", "", m_gt_0);
1.124689 +     break;
1.124690 +   case 's':
1.124691 +     stem(&z, "ssen", "", m_gt_0);
1.124692 +     break;
1.124693 +  }
1.124694 +
1.124695 +  /* Step 4 */
1.124696 +  switch( z[1] ){
1.124697 +   case 'a':
1.124698 +     if( z[0]=='l' && m_gt_1(z+2) ){
1.124699 +       z += 2;
1.124700 +     }
1.124701 +     break;
1.124702 +   case 'c':
1.124703 +     if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
1.124704 +       z += 4;
1.124705 +     }
1.124706 +     break;
1.124707 +   case 'e':
1.124708 +     if( z[0]=='r' && m_gt_1(z+2) ){
1.124709 +       z += 2;
1.124710 +     }
1.124711 +     break;
1.124712 +   case 'i':
1.124713 +     if( z[0]=='c' && m_gt_1(z+2) ){
1.124714 +       z += 2;
1.124715 +     }
1.124716 +     break;
1.124717 +   case 'l':
1.124718 +     if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
1.124719 +       z += 4;
1.124720 +     }
1.124721 +     break;
1.124722 +   case 'n':
1.124723 +     if( z[0]=='t' ){
1.124724 +       if( z[2]=='a' ){
1.124725 +         if( m_gt_1(z+3) ){
1.124726 +           z += 3;
1.124727 +         }
1.124728 +       }else if( z[2]=='e' ){
1.124729 +         stem(&z, "tneme", "", m_gt_1) ||
1.124730 +         stem(&z, "tnem", "", m_gt_1) ||
1.124731 +         stem(&z, "tne", "", m_gt_1);
1.124732 +       }
1.124733 +     }
1.124734 +     break;
1.124735 +   case 'o':
1.124736 +     if( z[0]=='u' ){
1.124737 +       if( m_gt_1(z+2) ){
1.124738 +         z += 2;
1.124739 +       }
1.124740 +     }else if( z[3]=='s' || z[3]=='t' ){
1.124741 +       stem(&z, "noi", "", m_gt_1);
1.124742 +     }
1.124743 +     break;
1.124744 +   case 's':
1.124745 +     if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
1.124746 +       z += 3;
1.124747 +     }
1.124748 +     break;
1.124749 +   case 't':
1.124750 +     stem(&z, "eta", "", m_gt_1) ||
1.124751 +     stem(&z, "iti", "", m_gt_1);
1.124752 +     break;
1.124753 +   case 'u':
1.124754 +     if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
1.124755 +       z += 3;
1.124756 +     }
1.124757 +     break;
1.124758 +   case 'v':
1.124759 +   case 'z':
1.124760 +     if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
1.124761 +       z += 3;
1.124762 +     }
1.124763 +     break;
1.124764 +  }
1.124765 +
1.124766 +  /* Step 5a */
1.124767 +  if( z[0]=='e' ){
1.124768 +    if( m_gt_1(z+1) ){
1.124769 +      z++;
1.124770 +    }else if( m_eq_1(z+1) && !star_oh(z+1) ){
1.124771 +      z++;
1.124772 +    }
1.124773 +  }
1.124774 +
1.124775 +  /* Step 5b */
1.124776 +  if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
1.124777 +    z++;
1.124778 +  }
1.124779 +
1.124780 +  /* z[] is now the stemmed word in reverse order.  Flip it back
1.124781 +  ** around into forward order and return.
1.124782 +  */
1.124783 +  *pnOut = i = (int)strlen(z);
1.124784 +  zOut[i] = 0;
1.124785 +  while( *z ){
1.124786 +    zOut[--i] = *(z++);
1.124787 +  }
1.124788 +}
1.124789 +
1.124790 +/*
1.124791 +** Characters that can be part of a token.  We assume any character
1.124792 +** whose value is greater than 0x80 (any UTF character) can be
1.124793 +** part of a token.  In other words, delimiters all must have
1.124794 +** values of 0x7f or lower.
1.124795 +*/
1.124796 +static const char porterIdChar[] = {
1.124797 +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
1.124798 +    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
1.124799 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
1.124800 +    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
1.124801 +    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
1.124802 +    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
1.124803 +};
1.124804 +#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
1.124805 +
1.124806 +/*
1.124807 +** Extract the next token from a tokenization cursor.  The cursor must
1.124808 +** have been opened by a prior call to porterOpen().
1.124809 +*/
1.124810 +static int porterNext(
1.124811 +  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
1.124812 +  const char **pzToken,               /* OUT: *pzToken is the token text */
1.124813 +  int *pnBytes,                       /* OUT: Number of bytes in token */
1.124814 +  int *piStartOffset,                 /* OUT: Starting offset of token */
1.124815 +  int *piEndOffset,                   /* OUT: Ending offset of token */
1.124816 +  int *piPosition                     /* OUT: Position integer of token */
1.124817 +){
1.124818 +  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
1.124819 +  const char *z = c->zInput;
1.124820 +
1.124821 +  while( c->iOffset<c->nInput ){
1.124822 +    int iStartOffset, ch;
1.124823 +
1.124824 +    /* Scan past delimiter characters */
1.124825 +    while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
1.124826 +      c->iOffset++;
1.124827 +    }
1.124828 +
1.124829 +    /* Count non-delimiter characters. */
1.124830 +    iStartOffset = c->iOffset;
1.124831 +    while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
1.124832 +      c->iOffset++;
1.124833 +    }
1.124834 +
1.124835 +    if( c->iOffset>iStartOffset ){
1.124836 +      int n = c->iOffset-iStartOffset;
1.124837 +      if( n>c->nAllocated ){
1.124838 +        char *pNew;
1.124839 +        c->nAllocated = n+20;
1.124840 +        pNew = sqlite3_realloc(c->zToken, c->nAllocated);
1.124841 +        if( !pNew ) return SQLITE_NOMEM;
1.124842 +        c->zToken = pNew;
1.124843 +      }
1.124844 +      porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
1.124845 +      *pzToken = c->zToken;
1.124846 +      *piStartOffset = iStartOffset;
1.124847 +      *piEndOffset = c->iOffset;
1.124848 +      *piPosition = c->iToken++;
1.124849 +      return SQLITE_OK;
1.124850 +    }
1.124851 +  }
1.124852 +  return SQLITE_DONE;
1.124853 +}
1.124854 +
1.124855 +/*
1.124856 +** The set of routines that implement the porter-stemmer tokenizer
1.124857 +*/
1.124858 +static const sqlite3_tokenizer_module porterTokenizerModule = {
1.124859 +  0,
1.124860 +  porterCreate,
1.124861 +  porterDestroy,
1.124862 +  porterOpen,
1.124863 +  porterClose,
1.124864 +  porterNext,
1.124865 +  0
1.124866 +};
1.124867 +
1.124868 +/*
1.124869 +** Allocate a new porter tokenizer.  Return a pointer to the new
1.124870 +** tokenizer in *ppModule
1.124871 +*/
1.124872 +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
1.124873 +  sqlite3_tokenizer_module const**ppModule
1.124874 +){
1.124875 +  *ppModule = &porterTokenizerModule;
1.124876 +}
1.124877 +
1.124878 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.124879 +
1.124880 +/************** End of fts3_porter.c *****************************************/
1.124881 +/************** Begin file fts3_tokenizer.c **********************************/
1.124882 +/*
1.124883 +** 2007 June 22
1.124884 +**
1.124885 +** The author disclaims copyright to this source code.  In place of
1.124886 +** a legal notice, here is a blessing:
1.124887 +**
1.124888 +**    May you do good and not evil.
1.124889 +**    May you find forgiveness for yourself and forgive others.
1.124890 +**    May you share freely, never taking more than you give.
1.124891 +**
1.124892 +******************************************************************************
1.124893 +**
1.124894 +** This is part of an SQLite module implementing full-text search.
1.124895 +** This particular file implements the generic tokenizer interface.
1.124896 +*/
1.124897 +
1.124898 +/*
1.124899 +** The code in this file is only compiled if:
1.124900 +**
1.124901 +**     * The FTS3 module is being built as an extension
1.124902 +**       (in which case SQLITE_CORE is not defined), or
1.124903 +**
1.124904 +**     * The FTS3 module is being built into the core of
1.124905 +**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
1.124906 +*/
1.124907 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.124908 +
1.124909 +/* #include <assert.h> */
1.124910 +/* #include <string.h> */
1.124911 +
1.124912 +/*
1.124913 +** Implementation of the SQL scalar function for accessing the underlying 
1.124914 +** hash table. This function may be called as follows:
1.124915 +**
1.124916 +**   SELECT <function-name>(<key-name>);
1.124917 +**   SELECT <function-name>(<key-name>, <pointer>);
1.124918 +**
1.124919 +** where <function-name> is the name passed as the second argument
1.124920 +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
1.124921 +**
1.124922 +** If the <pointer> argument is specified, it must be a blob value
1.124923 +** containing a pointer to be stored as the hash data corresponding
1.124924 +** to the string <key-name>. If <pointer> is not specified, then
1.124925 +** the string <key-name> must already exist in the has table. Otherwise,
1.124926 +** an error is returned.
1.124927 +**
1.124928 +** Whether or not the <pointer> argument is specified, the value returned
1.124929 +** is a blob containing the pointer stored as the hash data corresponding
1.124930 +** to string <key-name> (after the hash-table is updated, if applicable).
1.124931 +*/
1.124932 +static void scalarFunc(
1.124933 +  sqlite3_context *context,
1.124934 +  int argc,
1.124935 +  sqlite3_value **argv
1.124936 +){
1.124937 +  Fts3Hash *pHash;
1.124938 +  void *pPtr = 0;
1.124939 +  const unsigned char *zName;
1.124940 +  int nName;
1.124941 +
1.124942 +  assert( argc==1 || argc==2 );
1.124943 +
1.124944 +  pHash = (Fts3Hash *)sqlite3_user_data(context);
1.124945 +
1.124946 +  zName = sqlite3_value_text(argv[0]);
1.124947 +  nName = sqlite3_value_bytes(argv[0])+1;
1.124948 +
1.124949 +  if( argc==2 ){
1.124950 +    void *pOld;
1.124951 +    int n = sqlite3_value_bytes(argv[1]);
1.124952 +    if( n!=sizeof(pPtr) ){
1.124953 +      sqlite3_result_error(context, "argument type mismatch", -1);
1.124954 +      return;
1.124955 +    }
1.124956 +    pPtr = *(void **)sqlite3_value_blob(argv[1]);
1.124957 +    pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
1.124958 +    if( pOld==pPtr ){
1.124959 +      sqlite3_result_error(context, "out of memory", -1);
1.124960 +      return;
1.124961 +    }
1.124962 +  }else{
1.124963 +    pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
1.124964 +    if( !pPtr ){
1.124965 +      char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
1.124966 +      sqlite3_result_error(context, zErr, -1);
1.124967 +      sqlite3_free(zErr);
1.124968 +      return;
1.124969 +    }
1.124970 +  }
1.124971 +
1.124972 +  sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
1.124973 +}
1.124974 +
1.124975 +SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
1.124976 +  static const char isFtsIdChar[] = {
1.124977 +      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
1.124978 +      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
1.124979 +      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
1.124980 +      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
1.124981 +      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
1.124982 +      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
1.124983 +      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
1.124984 +      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
1.124985 +  };
1.124986 +  return (c&0x80 || isFtsIdChar[(int)(c)]);
1.124987 +}
1.124988 +
1.124989 +SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
1.124990 +  const char *z1;
1.124991 +  const char *z2 = 0;
1.124992 +
1.124993 +  /* Find the start of the next token. */
1.124994 +  z1 = zStr;
1.124995 +  while( z2==0 ){
1.124996 +    char c = *z1;
1.124997 +    switch( c ){
1.124998 +      case '\0': return 0;        /* No more tokens here */
1.124999 +      case '\'':
1.125000 +      case '"':
1.125001 +      case '`': {
1.125002 +        z2 = z1;
1.125003 +        while( *++z2 && (*z2!=c || *++z2==c) );
1.125004 +        break;
1.125005 +      }
1.125006 +      case '[':
1.125007 +        z2 = &z1[1];
1.125008 +        while( *z2 && z2[0]!=']' ) z2++;
1.125009 +        if( *z2 ) z2++;
1.125010 +        break;
1.125011 +
1.125012 +      default:
1.125013 +        if( sqlite3Fts3IsIdChar(*z1) ){
1.125014 +          z2 = &z1[1];
1.125015 +          while( sqlite3Fts3IsIdChar(*z2) ) z2++;
1.125016 +        }else{
1.125017 +          z1++;
1.125018 +        }
1.125019 +    }
1.125020 +  }
1.125021 +
1.125022 +  *pn = (int)(z2-z1);
1.125023 +  return z1;
1.125024 +}
1.125025 +
1.125026 +SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
1.125027 +  Fts3Hash *pHash,                /* Tokenizer hash table */
1.125028 +  const char *zArg,               /* Tokenizer name */
1.125029 +  sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
1.125030 +  char **pzErr                    /* OUT: Set to malloced error message */
1.125031 +){
1.125032 +  int rc;
1.125033 +  char *z = (char *)zArg;
1.125034 +  int n = 0;
1.125035 +  char *zCopy;
1.125036 +  char *zEnd;                     /* Pointer to nul-term of zCopy */
1.125037 +  sqlite3_tokenizer_module *m;
1.125038 +
1.125039 +  zCopy = sqlite3_mprintf("%s", zArg);
1.125040 +  if( !zCopy ) return SQLITE_NOMEM;
1.125041 +  zEnd = &zCopy[strlen(zCopy)];
1.125042 +
1.125043 +  z = (char *)sqlite3Fts3NextToken(zCopy, &n);
1.125044 +  z[n] = '\0';
1.125045 +  sqlite3Fts3Dequote(z);
1.125046 +
1.125047 +  m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
1.125048 +  if( !m ){
1.125049 +    *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
1.125050 +    rc = SQLITE_ERROR;
1.125051 +  }else{
1.125052 +    char const **aArg = 0;
1.125053 +    int iArg = 0;
1.125054 +    z = &z[n+1];
1.125055 +    while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
1.125056 +      int nNew = sizeof(char *)*(iArg+1);
1.125057 +      char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
1.125058 +      if( !aNew ){
1.125059 +        sqlite3_free(zCopy);
1.125060 +        sqlite3_free((void *)aArg);
1.125061 +        return SQLITE_NOMEM;
1.125062 +      }
1.125063 +      aArg = aNew;
1.125064 +      aArg[iArg++] = z;
1.125065 +      z[n] = '\0';
1.125066 +      sqlite3Fts3Dequote(z);
1.125067 +      z = &z[n+1];
1.125068 +    }
1.125069 +    rc = m->xCreate(iArg, aArg, ppTok);
1.125070 +    assert( rc!=SQLITE_OK || *ppTok );
1.125071 +    if( rc!=SQLITE_OK ){
1.125072 +      *pzErr = sqlite3_mprintf("unknown tokenizer");
1.125073 +    }else{
1.125074 +      (*ppTok)->pModule = m; 
1.125075 +    }
1.125076 +    sqlite3_free((void *)aArg);
1.125077 +  }
1.125078 +
1.125079 +  sqlite3_free(zCopy);
1.125080 +  return rc;
1.125081 +}
1.125082 +
1.125083 +
1.125084 +#ifdef SQLITE_TEST
1.125085 +
1.125086 +/* #include <tcl.h> */
1.125087 +/* #include <string.h> */
1.125088 +
1.125089 +/*
1.125090 +** Implementation of a special SQL scalar function for testing tokenizers 
1.125091 +** designed to be used in concert with the Tcl testing framework. This
1.125092 +** function must be called with two or more arguments:
1.125093 +**
1.125094 +**   SELECT <function-name>(<key-name>, ..., <input-string>);
1.125095 +**
1.125096 +** where <function-name> is the name passed as the second argument
1.125097 +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
1.125098 +** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
1.125099 +**
1.125100 +** The return value is a string that may be interpreted as a Tcl
1.125101 +** list. For each token in the <input-string>, three elements are
1.125102 +** added to the returned list. The first is the token position, the 
1.125103 +** second is the token text (folded, stemmed, etc.) and the third is the
1.125104 +** substring of <input-string> associated with the token. For example, 
1.125105 +** using the built-in "simple" tokenizer:
1.125106 +**
1.125107 +**   SELECT fts_tokenizer_test('simple', 'I don't see how');
1.125108 +**
1.125109 +** will return the string:
1.125110 +**
1.125111 +**   "{0 i I 1 dont don't 2 see see 3 how how}"
1.125112 +**   
1.125113 +*/
1.125114 +static void testFunc(
1.125115 +  sqlite3_context *context,
1.125116 +  int argc,
1.125117 +  sqlite3_value **argv
1.125118 +){
1.125119 +  Fts3Hash *pHash;
1.125120 +  sqlite3_tokenizer_module *p;
1.125121 +  sqlite3_tokenizer *pTokenizer = 0;
1.125122 +  sqlite3_tokenizer_cursor *pCsr = 0;
1.125123 +
1.125124 +  const char *zErr = 0;
1.125125 +
1.125126 +  const char *zName;
1.125127 +  int nName;
1.125128 +  const char *zInput;
1.125129 +  int nInput;
1.125130 +
1.125131 +  const char *azArg[64];
1.125132 +
1.125133 +  const char *zToken;
1.125134 +  int nToken = 0;
1.125135 +  int iStart = 0;
1.125136 +  int iEnd = 0;
1.125137 +  int iPos = 0;
1.125138 +  int i;
1.125139 +
1.125140 +  Tcl_Obj *pRet;
1.125141 +
1.125142 +  if( argc<2 ){
1.125143 +    sqlite3_result_error(context, "insufficient arguments", -1);
1.125144 +    return;
1.125145 +  }
1.125146 +
1.125147 +  nName = sqlite3_value_bytes(argv[0]);
1.125148 +  zName = (const char *)sqlite3_value_text(argv[0]);
1.125149 +  nInput = sqlite3_value_bytes(argv[argc-1]);
1.125150 +  zInput = (const char *)sqlite3_value_text(argv[argc-1]);
1.125151 +
1.125152 +  pHash = (Fts3Hash *)sqlite3_user_data(context);
1.125153 +  p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
1.125154 +
1.125155 +  if( !p ){
1.125156 +    char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
1.125157 +    sqlite3_result_error(context, zErr, -1);
1.125158 +    sqlite3_free(zErr);
1.125159 +    return;
1.125160 +  }
1.125161 +
1.125162 +  pRet = Tcl_NewObj();
1.125163 +  Tcl_IncrRefCount(pRet);
1.125164 +
1.125165 +  for(i=1; i<argc-1; i++){
1.125166 +    azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
1.125167 +  }
1.125168 +
1.125169 +  if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
1.125170 +    zErr = "error in xCreate()";
1.125171 +    goto finish;
1.125172 +  }
1.125173 +  pTokenizer->pModule = p;
1.125174 +  if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
1.125175 +    zErr = "error in xOpen()";
1.125176 +    goto finish;
1.125177 +  }
1.125178 +
1.125179 +  while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
1.125180 +    Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
1.125181 +    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
1.125182 +    zToken = &zInput[iStart];
1.125183 +    nToken = iEnd-iStart;
1.125184 +    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
1.125185 +  }
1.125186 +
1.125187 +  if( SQLITE_OK!=p->xClose(pCsr) ){
1.125188 +    zErr = "error in xClose()";
1.125189 +    goto finish;
1.125190 +  }
1.125191 +  if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
1.125192 +    zErr = "error in xDestroy()";
1.125193 +    goto finish;
1.125194 +  }
1.125195 +
1.125196 +finish:
1.125197 +  if( zErr ){
1.125198 +    sqlite3_result_error(context, zErr, -1);
1.125199 +  }else{
1.125200 +    sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
1.125201 +  }
1.125202 +  Tcl_DecrRefCount(pRet);
1.125203 +}
1.125204 +
1.125205 +static
1.125206 +int registerTokenizer(
1.125207 +  sqlite3 *db, 
1.125208 +  char *zName, 
1.125209 +  const sqlite3_tokenizer_module *p
1.125210 +){
1.125211 +  int rc;
1.125212 +  sqlite3_stmt *pStmt;
1.125213 +  const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
1.125214 +
1.125215 +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1.125216 +  if( rc!=SQLITE_OK ){
1.125217 +    return rc;
1.125218 +  }
1.125219 +
1.125220 +  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
1.125221 +  sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
1.125222 +  sqlite3_step(pStmt);
1.125223 +
1.125224 +  return sqlite3_finalize(pStmt);
1.125225 +}
1.125226 +
1.125227 +static
1.125228 +int queryTokenizer(
1.125229 +  sqlite3 *db, 
1.125230 +  char *zName,  
1.125231 +  const sqlite3_tokenizer_module **pp
1.125232 +){
1.125233 +  int rc;
1.125234 +  sqlite3_stmt *pStmt;
1.125235 +  const char zSql[] = "SELECT fts3_tokenizer(?)";
1.125236 +
1.125237 +  *pp = 0;
1.125238 +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1.125239 +  if( rc!=SQLITE_OK ){
1.125240 +    return rc;
1.125241 +  }
1.125242 +
1.125243 +  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
1.125244 +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
1.125245 +    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
1.125246 +      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
1.125247 +    }
1.125248 +  }
1.125249 +
1.125250 +  return sqlite3_finalize(pStmt);
1.125251 +}
1.125252 +
1.125253 +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
1.125254 +
1.125255 +/*
1.125256 +** Implementation of the scalar function fts3_tokenizer_internal_test().
1.125257 +** This function is used for testing only, it is not included in the
1.125258 +** build unless SQLITE_TEST is defined.
1.125259 +**
1.125260 +** The purpose of this is to test that the fts3_tokenizer() function
1.125261 +** can be used as designed by the C-code in the queryTokenizer and
1.125262 +** registerTokenizer() functions above. These two functions are repeated
1.125263 +** in the README.tokenizer file as an example, so it is important to
1.125264 +** test them.
1.125265 +**
1.125266 +** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
1.125267 +** function with no arguments. An assert() will fail if a problem is
1.125268 +** detected. i.e.:
1.125269 +**
1.125270 +**     SELECT fts3_tokenizer_internal_test();
1.125271 +**
1.125272 +*/
1.125273 +static void intTestFunc(
1.125274 +  sqlite3_context *context,
1.125275 +  int argc,
1.125276 +  sqlite3_value **argv
1.125277 +){
1.125278 +  int rc;
1.125279 +  const sqlite3_tokenizer_module *p1;
1.125280 +  const sqlite3_tokenizer_module *p2;
1.125281 +  sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
1.125282 +
1.125283 +  UNUSED_PARAMETER(argc);
1.125284 +  UNUSED_PARAMETER(argv);
1.125285 +
1.125286 +  /* Test the query function */
1.125287 +  sqlite3Fts3SimpleTokenizerModule(&p1);
1.125288 +  rc = queryTokenizer(db, "simple", &p2);
1.125289 +  assert( rc==SQLITE_OK );
1.125290 +  assert( p1==p2 );
1.125291 +  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
1.125292 +  assert( rc==SQLITE_ERROR );
1.125293 +  assert( p2==0 );
1.125294 +  assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
1.125295 +
1.125296 +  /* Test the storage function */
1.125297 +  rc = registerTokenizer(db, "nosuchtokenizer", p1);
1.125298 +  assert( rc==SQLITE_OK );
1.125299 +  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
1.125300 +  assert( rc==SQLITE_OK );
1.125301 +  assert( p2==p1 );
1.125302 +
1.125303 +  sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
1.125304 +}
1.125305 +
1.125306 +#endif
1.125307 +
1.125308 +/*
1.125309 +** Set up SQL objects in database db used to access the contents of
1.125310 +** the hash table pointed to by argument pHash. The hash table must
1.125311 +** been initialised to use string keys, and to take a private copy 
1.125312 +** of the key when a value is inserted. i.e. by a call similar to:
1.125313 +**
1.125314 +**    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
1.125315 +**
1.125316 +** This function adds a scalar function (see header comment above
1.125317 +** scalarFunc() in this file for details) and, if ENABLE_TABLE is
1.125318 +** defined at compilation time, a temporary virtual table (see header 
1.125319 +** comment above struct HashTableVtab) to the database schema. Both 
1.125320 +** provide read/write access to the contents of *pHash.
1.125321 +**
1.125322 +** The third argument to this function, zName, is used as the name
1.125323 +** of both the scalar and, if created, the virtual table.
1.125324 +*/
1.125325 +SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
1.125326 +  sqlite3 *db, 
1.125327 +  Fts3Hash *pHash, 
1.125328 +  const char *zName
1.125329 +){
1.125330 +  int rc = SQLITE_OK;
1.125331 +  void *p = (void *)pHash;
1.125332 +  const int any = SQLITE_ANY;
1.125333 +
1.125334 +#ifdef SQLITE_TEST
1.125335 +  char *zTest = 0;
1.125336 +  char *zTest2 = 0;
1.125337 +  void *pdb = (void *)db;
1.125338 +  zTest = sqlite3_mprintf("%s_test", zName);
1.125339 +  zTest2 = sqlite3_mprintf("%s_internal_test", zName);
1.125340 +  if( !zTest || !zTest2 ){
1.125341 +    rc = SQLITE_NOMEM;
1.125342 +  }
1.125343 +#endif
1.125344 +
1.125345 +  if( SQLITE_OK==rc ){
1.125346 +    rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
1.125347 +  }
1.125348 +  if( SQLITE_OK==rc ){
1.125349 +    rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
1.125350 +  }
1.125351 +#ifdef SQLITE_TEST
1.125352 +  if( SQLITE_OK==rc ){
1.125353 +    rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
1.125354 +  }
1.125355 +  if( SQLITE_OK==rc ){
1.125356 +    rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
1.125357 +  }
1.125358 +#endif
1.125359 +
1.125360 +#ifdef SQLITE_TEST
1.125361 +  sqlite3_free(zTest);
1.125362 +  sqlite3_free(zTest2);
1.125363 +#endif
1.125364 +
1.125365 +  return rc;
1.125366 +}
1.125367 +
1.125368 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.125369 +
1.125370 +/************** End of fts3_tokenizer.c **************************************/
1.125371 +/************** Begin file fts3_tokenizer1.c *********************************/
1.125372 +/*
1.125373 +** 2006 Oct 10
1.125374 +**
1.125375 +** The author disclaims copyright to this source code.  In place of
1.125376 +** a legal notice, here is a blessing:
1.125377 +**
1.125378 +**    May you do good and not evil.
1.125379 +**    May you find forgiveness for yourself and forgive others.
1.125380 +**    May you share freely, never taking more than you give.
1.125381 +**
1.125382 +******************************************************************************
1.125383 +**
1.125384 +** Implementation of the "simple" full-text-search tokenizer.
1.125385 +*/
1.125386 +
1.125387 +/*
1.125388 +** The code in this file is only compiled if:
1.125389 +**
1.125390 +**     * The FTS3 module is being built as an extension
1.125391 +**       (in which case SQLITE_CORE is not defined), or
1.125392 +**
1.125393 +**     * The FTS3 module is being built into the core of
1.125394 +**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
1.125395 +*/
1.125396 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.125397 +
1.125398 +/* #include <assert.h> */
1.125399 +/* #include <stdlib.h> */
1.125400 +/* #include <stdio.h> */
1.125401 +/* #include <string.h> */
1.125402 +
1.125403 +
1.125404 +typedef struct simple_tokenizer {
1.125405 +  sqlite3_tokenizer base;
1.125406 +  char delim[128];             /* flag ASCII delimiters */
1.125407 +} simple_tokenizer;
1.125408 +
1.125409 +typedef struct simple_tokenizer_cursor {
1.125410 +  sqlite3_tokenizer_cursor base;
1.125411 +  const char *pInput;          /* input we are tokenizing */
1.125412 +  int nBytes;                  /* size of the input */
1.125413 +  int iOffset;                 /* current position in pInput */
1.125414 +  int iToken;                  /* index of next token to be returned */
1.125415 +  char *pToken;                /* storage for current token */
1.125416 +  int nTokenAllocated;         /* space allocated to zToken buffer */
1.125417 +} simple_tokenizer_cursor;
1.125418 +
1.125419 +
1.125420 +static int simpleDelim(simple_tokenizer *t, unsigned char c){
1.125421 +  return c<0x80 && t->delim[c];
1.125422 +}
1.125423 +static int fts3_isalnum(int x){
1.125424 +  return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
1.125425 +}
1.125426 +
1.125427 +/*
1.125428 +** Create a new tokenizer instance.
1.125429 +*/
1.125430 +static int simpleCreate(
1.125431 +  int argc, const char * const *argv,
1.125432 +  sqlite3_tokenizer **ppTokenizer
1.125433 +){
1.125434 +  simple_tokenizer *t;
1.125435 +
1.125436 +  t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
1.125437 +  if( t==NULL ) return SQLITE_NOMEM;
1.125438 +  memset(t, 0, sizeof(*t));
1.125439 +
1.125440 +  /* TODO(shess) Delimiters need to remain the same from run to run,
1.125441 +  ** else we need to reindex.  One solution would be a meta-table to
1.125442 +  ** track such information in the database, then we'd only want this
1.125443 +  ** information on the initial create.
1.125444 +  */
1.125445 +  if( argc>1 ){
1.125446 +    int i, n = (int)strlen(argv[1]);
1.125447 +    for(i=0; i<n; i++){
1.125448 +      unsigned char ch = argv[1][i];
1.125449 +      /* We explicitly don't support UTF-8 delimiters for now. */
1.125450 +      if( ch>=0x80 ){
1.125451 +        sqlite3_free(t);
1.125452 +        return SQLITE_ERROR;
1.125453 +      }
1.125454 +      t->delim[ch] = 1;
1.125455 +    }
1.125456 +  } else {
1.125457 +    /* Mark non-alphanumeric ASCII characters as delimiters */
1.125458 +    int i;
1.125459 +    for(i=1; i<0x80; i++){
1.125460 +      t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
1.125461 +    }
1.125462 +  }
1.125463 +
1.125464 +  *ppTokenizer = &t->base;
1.125465 +  return SQLITE_OK;
1.125466 +}
1.125467 +
1.125468 +/*
1.125469 +** Destroy a tokenizer
1.125470 +*/
1.125471 +static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
1.125472 +  sqlite3_free(pTokenizer);
1.125473 +  return SQLITE_OK;
1.125474 +}
1.125475 +
1.125476 +/*
1.125477 +** Prepare to begin tokenizing a particular string.  The input
1.125478 +** string to be tokenized is pInput[0..nBytes-1].  A cursor
1.125479 +** used to incrementally tokenize this string is returned in 
1.125480 +** *ppCursor.
1.125481 +*/
1.125482 +static int simpleOpen(
1.125483 +  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
1.125484 +  const char *pInput, int nBytes,        /* String to be tokenized */
1.125485 +  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
1.125486 +){
1.125487 +  simple_tokenizer_cursor *c;
1.125488 +
1.125489 +  UNUSED_PARAMETER(pTokenizer);
1.125490 +
1.125491 +  c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
1.125492 +  if( c==NULL ) return SQLITE_NOMEM;
1.125493 +
1.125494 +  c->pInput = pInput;
1.125495 +  if( pInput==0 ){
1.125496 +    c->nBytes = 0;
1.125497 +  }else if( nBytes<0 ){
1.125498 +    c->nBytes = (int)strlen(pInput);
1.125499 +  }else{
1.125500 +    c->nBytes = nBytes;
1.125501 +  }
1.125502 +  c->iOffset = 0;                 /* start tokenizing at the beginning */
1.125503 +  c->iToken = 0;
1.125504 +  c->pToken = NULL;               /* no space allocated, yet. */
1.125505 +  c->nTokenAllocated = 0;
1.125506 +
1.125507 +  *ppCursor = &c->base;
1.125508 +  return SQLITE_OK;
1.125509 +}
1.125510 +
1.125511 +/*
1.125512 +** Close a tokenization cursor previously opened by a call to
1.125513 +** simpleOpen() above.
1.125514 +*/
1.125515 +static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
1.125516 +  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
1.125517 +  sqlite3_free(c->pToken);
1.125518 +  sqlite3_free(c);
1.125519 +  return SQLITE_OK;
1.125520 +}
1.125521 +
1.125522 +/*
1.125523 +** Extract the next token from a tokenization cursor.  The cursor must
1.125524 +** have been opened by a prior call to simpleOpen().
1.125525 +*/
1.125526 +static int simpleNext(
1.125527 +  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
1.125528 +  const char **ppToken,               /* OUT: *ppToken is the token text */
1.125529 +  int *pnBytes,                       /* OUT: Number of bytes in token */
1.125530 +  int *piStartOffset,                 /* OUT: Starting offset of token */
1.125531 +  int *piEndOffset,                   /* OUT: Ending offset of token */
1.125532 +  int *piPosition                     /* OUT: Position integer of token */
1.125533 +){
1.125534 +  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
1.125535 +  simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
1.125536 +  unsigned char *p = (unsigned char *)c->pInput;
1.125537 +
1.125538 +  while( c->iOffset<c->nBytes ){
1.125539 +    int iStartOffset;
1.125540 +
1.125541 +    /* Scan past delimiter characters */
1.125542 +    while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
1.125543 +      c->iOffset++;
1.125544 +    }
1.125545 +
1.125546 +    /* Count non-delimiter characters. */
1.125547 +    iStartOffset = c->iOffset;
1.125548 +    while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
1.125549 +      c->iOffset++;
1.125550 +    }
1.125551 +
1.125552 +    if( c->iOffset>iStartOffset ){
1.125553 +      int i, n = c->iOffset-iStartOffset;
1.125554 +      if( n>c->nTokenAllocated ){
1.125555 +        char *pNew;
1.125556 +        c->nTokenAllocated = n+20;
1.125557 +        pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
1.125558 +        if( !pNew ) return SQLITE_NOMEM;
1.125559 +        c->pToken = pNew;
1.125560 +      }
1.125561 +      for(i=0; i<n; i++){
1.125562 +        /* TODO(shess) This needs expansion to handle UTF-8
1.125563 +        ** case-insensitivity.
1.125564 +        */
1.125565 +        unsigned char ch = p[iStartOffset+i];
1.125566 +        c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
1.125567 +      }
1.125568 +      *ppToken = c->pToken;
1.125569 +      *pnBytes = n;
1.125570 +      *piStartOffset = iStartOffset;
1.125571 +      *piEndOffset = c->iOffset;
1.125572 +      *piPosition = c->iToken++;
1.125573 +
1.125574 +      return SQLITE_OK;
1.125575 +    }
1.125576 +  }
1.125577 +  return SQLITE_DONE;
1.125578 +}
1.125579 +
1.125580 +/*
1.125581 +** The set of routines that implement the simple tokenizer
1.125582 +*/
1.125583 +static const sqlite3_tokenizer_module simpleTokenizerModule = {
1.125584 +  0,
1.125585 +  simpleCreate,
1.125586 +  simpleDestroy,
1.125587 +  simpleOpen,
1.125588 +  simpleClose,
1.125589 +  simpleNext,
1.125590 +  0,
1.125591 +};
1.125592 +
1.125593 +/*
1.125594 +** Allocate a new simple tokenizer.  Return a pointer to the new
1.125595 +** tokenizer in *ppModule
1.125596 +*/
1.125597 +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
1.125598 +  sqlite3_tokenizer_module const**ppModule
1.125599 +){
1.125600 +  *ppModule = &simpleTokenizerModule;
1.125601 +}
1.125602 +
1.125603 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.125604 +
1.125605 +/************** End of fts3_tokenizer1.c *************************************/
1.125606 +/************** Begin file fts3_write.c **************************************/
1.125607 +/*
1.125608 +** 2009 Oct 23
1.125609 +**
1.125610 +** The author disclaims copyright to this source code.  In place of
1.125611 +** a legal notice, here is a blessing:
1.125612 +**
1.125613 +**    May you do good and not evil.
1.125614 +**    May you find forgiveness for yourself and forgive others.
1.125615 +**    May you share freely, never taking more than you give.
1.125616 +**
1.125617 +******************************************************************************
1.125618 +**
1.125619 +** This file is part of the SQLite FTS3 extension module. Specifically,
1.125620 +** this file contains code to insert, update and delete rows from FTS3
1.125621 +** tables. It also contains code to merge FTS3 b-tree segments. Some
1.125622 +** of the sub-routines used to merge segments are also used by the query 
1.125623 +** code in fts3.c.
1.125624 +*/
1.125625 +
1.125626 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.125627 +
1.125628 +/* #include <string.h> */
1.125629 +/* #include <assert.h> */
1.125630 +/* #include <stdlib.h> */
1.125631 +
1.125632 +
1.125633 +#define FTS_MAX_APPENDABLE_HEIGHT 16
1.125634 +
1.125635 +/*
1.125636 +** When full-text index nodes are loaded from disk, the buffer that they
1.125637 +** are loaded into has the following number of bytes of padding at the end 
1.125638 +** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
1.125639 +** of 920 bytes is allocated for it.
1.125640 +**
1.125641 +** This means that if we have a pointer into a buffer containing node data,
1.125642 +** it is always safe to read up to two varints from it without risking an
1.125643 +** overread, even if the node data is corrupted.
1.125644 +*/
1.125645 +#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
1.125646 +
1.125647 +/*
1.125648 +** Under certain circumstances, b-tree nodes (doclists) can be loaded into
1.125649 +** memory incrementally instead of all at once. This can be a big performance
1.125650 +** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
1.125651 +** method before retrieving all query results (as may happen, for example,
1.125652 +** if a query has a LIMIT clause).
1.125653 +**
1.125654 +** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD 
1.125655 +** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
1.125656 +** The code is written so that the hard lower-limit for each of these values 
1.125657 +** is 1. Clearly such small values would be inefficient, but can be useful 
1.125658 +** for testing purposes.
1.125659 +**
1.125660 +** If this module is built with SQLITE_TEST defined, these constants may
1.125661 +** be overridden at runtime for testing purposes. File fts3_test.c contains
1.125662 +** a Tcl interface to read and write the values.
1.125663 +*/
1.125664 +#ifdef SQLITE_TEST
1.125665 +int test_fts3_node_chunksize = (4*1024);
1.125666 +int test_fts3_node_chunk_threshold = (4*1024)*4;
1.125667 +# define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
1.125668 +# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
1.125669 +#else
1.125670 +# define FTS3_NODE_CHUNKSIZE (4*1024) 
1.125671 +# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
1.125672 +#endif
1.125673 +
1.125674 +/*
1.125675 +** The two values that may be meaningfully bound to the :1 parameter in
1.125676 +** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
1.125677 +*/
1.125678 +#define FTS_STAT_DOCTOTAL      0
1.125679 +#define FTS_STAT_INCRMERGEHINT 1
1.125680 +#define FTS_STAT_AUTOINCRMERGE 2
1.125681 +
1.125682 +/*
1.125683 +** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
1.125684 +** and incremental merge operation that takes place. This is used for 
1.125685 +** debugging FTS only, it should not usually be turned on in production
1.125686 +** systems.
1.125687 +*/
1.125688 +#ifdef FTS3_LOG_MERGES
1.125689 +static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
1.125690 +  sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
1.125691 +}
1.125692 +#else
1.125693 +#define fts3LogMerge(x, y)
1.125694 +#endif
1.125695 +
1.125696 +
1.125697 +typedef struct PendingList PendingList;
1.125698 +typedef struct SegmentNode SegmentNode;
1.125699 +typedef struct SegmentWriter SegmentWriter;
1.125700 +
1.125701 +/*
1.125702 +** An instance of the following data structure is used to build doclists
1.125703 +** incrementally. See function fts3PendingListAppend() for details.
1.125704 +*/
1.125705 +struct PendingList {
1.125706 +  int nData;
1.125707 +  char *aData;
1.125708 +  int nSpace;
1.125709 +  sqlite3_int64 iLastDocid;
1.125710 +  sqlite3_int64 iLastCol;
1.125711 +  sqlite3_int64 iLastPos;
1.125712 +};
1.125713 +
1.125714 +
1.125715 +/*
1.125716 +** Each cursor has a (possibly empty) linked list of the following objects.
1.125717 +*/
1.125718 +struct Fts3DeferredToken {
1.125719 +  Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
1.125720 +  int iCol;                       /* Column token must occur in */
1.125721 +  Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
1.125722 +  PendingList *pList;             /* Doclist is assembled here */
1.125723 +};
1.125724 +
1.125725 +/*
1.125726 +** An instance of this structure is used to iterate through the terms on
1.125727 +** a contiguous set of segment b-tree leaf nodes. Although the details of
1.125728 +** this structure are only manipulated by code in this file, opaque handles
1.125729 +** of type Fts3SegReader* are also used by code in fts3.c to iterate through
1.125730 +** terms when querying the full-text index. See functions:
1.125731 +**
1.125732 +**   sqlite3Fts3SegReaderNew()
1.125733 +**   sqlite3Fts3SegReaderFree()
1.125734 +**   sqlite3Fts3SegReaderIterate()
1.125735 +**
1.125736 +** Methods used to manipulate Fts3SegReader structures:
1.125737 +**
1.125738 +**   fts3SegReaderNext()
1.125739 +**   fts3SegReaderFirstDocid()
1.125740 +**   fts3SegReaderNextDocid()
1.125741 +*/
1.125742 +struct Fts3SegReader {
1.125743 +  int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
1.125744 +  u8 bLookup;                     /* True for a lookup only */
1.125745 +  u8 rootOnly;                    /* True for a root-only reader */
1.125746 +
1.125747 +  sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
1.125748 +  sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
1.125749 +  sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
1.125750 +  sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
1.125751 +
1.125752 +  char *aNode;                    /* Pointer to node data (or NULL) */
1.125753 +  int nNode;                      /* Size of buffer at aNode (or 0) */
1.125754 +  int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
1.125755 +  sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
1.125756 +
1.125757 +  Fts3HashElem **ppNextElem;
1.125758 +
1.125759 +  /* Variables set by fts3SegReaderNext(). These may be read directly
1.125760 +  ** by the caller. They are valid from the time SegmentReaderNew() returns
1.125761 +  ** until SegmentReaderNext() returns something other than SQLITE_OK
1.125762 +  ** (i.e. SQLITE_DONE).
1.125763 +  */
1.125764 +  int nTerm;                      /* Number of bytes in current term */
1.125765 +  char *zTerm;                    /* Pointer to current term */
1.125766 +  int nTermAlloc;                 /* Allocated size of zTerm buffer */
1.125767 +  char *aDoclist;                 /* Pointer to doclist of current entry */
1.125768 +  int nDoclist;                   /* Size of doclist in current entry */
1.125769 +
1.125770 +  /* The following variables are used by fts3SegReaderNextDocid() to iterate 
1.125771 +  ** through the current doclist (aDoclist/nDoclist).
1.125772 +  */
1.125773 +  char *pOffsetList;
1.125774 +  int nOffsetList;                /* For descending pending seg-readers only */
1.125775 +  sqlite3_int64 iDocid;
1.125776 +};
1.125777 +
1.125778 +#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
1.125779 +#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
1.125780 +
1.125781 +/*
1.125782 +** An instance of this structure is used to create a segment b-tree in the
1.125783 +** database. The internal details of this type are only accessed by the
1.125784 +** following functions:
1.125785 +**
1.125786 +**   fts3SegWriterAdd()
1.125787 +**   fts3SegWriterFlush()
1.125788 +**   fts3SegWriterFree()
1.125789 +*/
1.125790 +struct SegmentWriter {
1.125791 +  SegmentNode *pTree;             /* Pointer to interior tree structure */
1.125792 +  sqlite3_int64 iFirst;           /* First slot in %_segments written */
1.125793 +  sqlite3_int64 iFree;            /* Next free slot in %_segments */
1.125794 +  char *zTerm;                    /* Pointer to previous term buffer */
1.125795 +  int nTerm;                      /* Number of bytes in zTerm */
1.125796 +  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
1.125797 +  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
1.125798 +  int nSize;                      /* Size of allocation at aData */
1.125799 +  int nData;                      /* Bytes of data in aData */
1.125800 +  char *aData;                    /* Pointer to block from malloc() */
1.125801 +};
1.125802 +
1.125803 +/*
1.125804 +** Type SegmentNode is used by the following three functions to create
1.125805 +** the interior part of the segment b+-tree structures (everything except
1.125806 +** the leaf nodes). These functions and type are only ever used by code
1.125807 +** within the fts3SegWriterXXX() family of functions described above.
1.125808 +**
1.125809 +**   fts3NodeAddTerm()
1.125810 +**   fts3NodeWrite()
1.125811 +**   fts3NodeFree()
1.125812 +**
1.125813 +** When a b+tree is written to the database (either as a result of a merge
1.125814 +** or the pending-terms table being flushed), leaves are written into the 
1.125815 +** database file as soon as they are completely populated. The interior of
1.125816 +** the tree is assembled in memory and written out only once all leaves have
1.125817 +** been populated and stored. This is Ok, as the b+-tree fanout is usually
1.125818 +** very large, meaning that the interior of the tree consumes relatively 
1.125819 +** little memory.
1.125820 +*/
1.125821 +struct SegmentNode {
1.125822 +  SegmentNode *pParent;           /* Parent node (or NULL for root node) */
1.125823 +  SegmentNode *pRight;            /* Pointer to right-sibling */
1.125824 +  SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
1.125825 +  int nEntry;                     /* Number of terms written to node so far */
1.125826 +  char *zTerm;                    /* Pointer to previous term buffer */
1.125827 +  int nTerm;                      /* Number of bytes in zTerm */
1.125828 +  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
1.125829 +  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
1.125830 +  int nData;                      /* Bytes of valid data so far */
1.125831 +  char *aData;                    /* Node data */
1.125832 +};
1.125833 +
1.125834 +/*
1.125835 +** Valid values for the second argument to fts3SqlStmt().
1.125836 +*/
1.125837 +#define SQL_DELETE_CONTENT             0
1.125838 +#define SQL_IS_EMPTY                   1
1.125839 +#define SQL_DELETE_ALL_CONTENT         2 
1.125840 +#define SQL_DELETE_ALL_SEGMENTS        3
1.125841 +#define SQL_DELETE_ALL_SEGDIR          4
1.125842 +#define SQL_DELETE_ALL_DOCSIZE         5
1.125843 +#define SQL_DELETE_ALL_STAT            6
1.125844 +#define SQL_SELECT_CONTENT_BY_ROWID    7
1.125845 +#define SQL_NEXT_SEGMENT_INDEX         8
1.125846 +#define SQL_INSERT_SEGMENTS            9
1.125847 +#define SQL_NEXT_SEGMENTS_ID          10
1.125848 +#define SQL_INSERT_SEGDIR             11
1.125849 +#define SQL_SELECT_LEVEL              12
1.125850 +#define SQL_SELECT_LEVEL_RANGE        13
1.125851 +#define SQL_SELECT_LEVEL_COUNT        14
1.125852 +#define SQL_SELECT_SEGDIR_MAX_LEVEL   15
1.125853 +#define SQL_DELETE_SEGDIR_LEVEL       16
1.125854 +#define SQL_DELETE_SEGMENTS_RANGE     17
1.125855 +#define SQL_CONTENT_INSERT            18
1.125856 +#define SQL_DELETE_DOCSIZE            19
1.125857 +#define SQL_REPLACE_DOCSIZE           20
1.125858 +#define SQL_SELECT_DOCSIZE            21
1.125859 +#define SQL_SELECT_STAT               22
1.125860 +#define SQL_REPLACE_STAT              23
1.125861 +
1.125862 +#define SQL_SELECT_ALL_PREFIX_LEVEL   24
1.125863 +#define SQL_DELETE_ALL_TERMS_SEGDIR   25
1.125864 +#define SQL_DELETE_SEGDIR_RANGE       26
1.125865 +#define SQL_SELECT_ALL_LANGID         27
1.125866 +#define SQL_FIND_MERGE_LEVEL          28
1.125867 +#define SQL_MAX_LEAF_NODE_ESTIMATE    29
1.125868 +#define SQL_DELETE_SEGDIR_ENTRY       30
1.125869 +#define SQL_SHIFT_SEGDIR_ENTRY        31
1.125870 +#define SQL_SELECT_SEGDIR             32
1.125871 +#define SQL_CHOMP_SEGDIR              33
1.125872 +#define SQL_SEGMENT_IS_APPENDABLE     34
1.125873 +#define SQL_SELECT_INDEXES            35
1.125874 +#define SQL_SELECT_MXLEVEL            36
1.125875 +
1.125876 +/*
1.125877 +** This function is used to obtain an SQLite prepared statement handle
1.125878 +** for the statement identified by the second argument. If successful,
1.125879 +** *pp is set to the requested statement handle and SQLITE_OK returned.
1.125880 +** Otherwise, an SQLite error code is returned and *pp is set to 0.
1.125881 +**
1.125882 +** If argument apVal is not NULL, then it must point to an array with
1.125883 +** at least as many entries as the requested statement has bound 
1.125884 +** parameters. The values are bound to the statements parameters before
1.125885 +** returning.
1.125886 +*/
1.125887 +static int fts3SqlStmt(
1.125888 +  Fts3Table *p,                   /* Virtual table handle */
1.125889 +  int eStmt,                      /* One of the SQL_XXX constants above */
1.125890 +  sqlite3_stmt **pp,              /* OUT: Statement handle */
1.125891 +  sqlite3_value **apVal           /* Values to bind to statement */
1.125892 +){
1.125893 +  const char *azSql[] = {
1.125894 +/* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
1.125895 +/* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
1.125896 +/* 2  */  "DELETE FROM %Q.'%q_content'",
1.125897 +/* 3  */  "DELETE FROM %Q.'%q_segments'",
1.125898 +/* 4  */  "DELETE FROM %Q.'%q_segdir'",
1.125899 +/* 5  */  "DELETE FROM %Q.'%q_docsize'",
1.125900 +/* 6  */  "DELETE FROM %Q.'%q_stat'",
1.125901 +/* 7  */  "SELECT %s WHERE rowid=?",
1.125902 +/* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
1.125903 +/* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
1.125904 +/* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
1.125905 +/* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
1.125906 +
1.125907 +          /* Return segments in order from oldest to newest.*/ 
1.125908 +/* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
1.125909 +            "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
1.125910 +/* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
1.125911 +            "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
1.125912 +            "ORDER BY level DESC, idx ASC",
1.125913 +
1.125914 +/* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
1.125915 +/* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
1.125916 +
1.125917 +/* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
1.125918 +/* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
1.125919 +/* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
1.125920 +/* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
1.125921 +/* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
1.125922 +/* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
1.125923 +/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
1.125924 +/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
1.125925 +/* 24 */  "",
1.125926 +/* 25 */  "",
1.125927 +
1.125928 +/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
1.125929 +/* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
1.125930 +
1.125931 +/* This statement is used to determine which level to read the input from
1.125932 +** when performing an incremental merge. It returns the absolute level number
1.125933 +** of the oldest level in the db that contains at least ? segments. Or,
1.125934 +** if no level in the FTS index contains more than ? segments, the statement
1.125935 +** returns zero rows.  */
1.125936 +/* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
1.125937 +         "  ORDER BY (level %% 1024) ASC LIMIT 1",
1.125938 +
1.125939 +/* Estimate the upper limit on the number of leaf nodes in a new segment
1.125940 +** created by merging the oldest :2 segments from absolute level :1. See 
1.125941 +** function sqlite3Fts3Incrmerge() for details.  */
1.125942 +/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
1.125943 +         "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
1.125944 +
1.125945 +/* SQL_DELETE_SEGDIR_ENTRY
1.125946 +**   Delete the %_segdir entry on absolute level :1 with index :2.  */
1.125947 +/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
1.125948 +
1.125949 +/* SQL_SHIFT_SEGDIR_ENTRY
1.125950 +**   Modify the idx value for the segment with idx=:3 on absolute level :2
1.125951 +**   to :1.  */
1.125952 +/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
1.125953 +
1.125954 +/* SQL_SELECT_SEGDIR
1.125955 +**   Read a single entry from the %_segdir table. The entry from absolute 
1.125956 +**   level :1 with index value :2.  */
1.125957 +/* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
1.125958 +            "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
1.125959 +
1.125960 +/* SQL_CHOMP_SEGDIR
1.125961 +**   Update the start_block (:1) and root (:2) fields of the %_segdir
1.125962 +**   entry located on absolute level :3 with index :4.  */
1.125963 +/* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
1.125964 +            "WHERE level = ? AND idx = ?",
1.125965 +
1.125966 +/* SQL_SEGMENT_IS_APPENDABLE
1.125967 +**   Return a single row if the segment with end_block=? is appendable. Or
1.125968 +**   no rows otherwise.  */
1.125969 +/* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
1.125970 +
1.125971 +/* SQL_SELECT_INDEXES
1.125972 +**   Return the list of valid segment indexes for absolute level ?  */
1.125973 +/* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
1.125974 +
1.125975 +/* SQL_SELECT_MXLEVEL
1.125976 +**   Return the largest relative level in the FTS index or indexes.  */
1.125977 +/* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
1.125978 +  };
1.125979 +  int rc = SQLITE_OK;
1.125980 +  sqlite3_stmt *pStmt;
1.125981 +
1.125982 +  assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
1.125983 +  assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
1.125984 +  
1.125985 +  pStmt = p->aStmt[eStmt];
1.125986 +  if( !pStmt ){
1.125987 +    char *zSql;
1.125988 +    if( eStmt==SQL_CONTENT_INSERT ){
1.125989 +      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
1.125990 +    }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
1.125991 +      zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
1.125992 +    }else{
1.125993 +      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
1.125994 +    }
1.125995 +    if( !zSql ){
1.125996 +      rc = SQLITE_NOMEM;
1.125997 +    }else{
1.125998 +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
1.125999 +      sqlite3_free(zSql);
1.126000 +      assert( rc==SQLITE_OK || pStmt==0 );
1.126001 +      p->aStmt[eStmt] = pStmt;
1.126002 +    }
1.126003 +  }
1.126004 +  if( apVal ){
1.126005 +    int i;
1.126006 +    int nParam = sqlite3_bind_parameter_count(pStmt);
1.126007 +    for(i=0; rc==SQLITE_OK && i<nParam; i++){
1.126008 +      rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
1.126009 +    }
1.126010 +  }
1.126011 +  *pp = pStmt;
1.126012 +  return rc;
1.126013 +}
1.126014 +
1.126015 +
1.126016 +static int fts3SelectDocsize(
1.126017 +  Fts3Table *pTab,                /* FTS3 table handle */
1.126018 +  sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
1.126019 +  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
1.126020 +){
1.126021 +  sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
1.126022 +  int rc;                         /* Return code */
1.126023 +
1.126024 +  rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
1.126025 +  if( rc==SQLITE_OK ){
1.126026 +    sqlite3_bind_int64(pStmt, 1, iDocid);
1.126027 +    rc = sqlite3_step(pStmt);
1.126028 +    if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
1.126029 +      rc = sqlite3_reset(pStmt);
1.126030 +      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
1.126031 +      pStmt = 0;
1.126032 +    }else{
1.126033 +      rc = SQLITE_OK;
1.126034 +    }
1.126035 +  }
1.126036 +
1.126037 +  *ppStmt = pStmt;
1.126038 +  return rc;
1.126039 +}
1.126040 +
1.126041 +SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
1.126042 +  Fts3Table *pTab,                /* Fts3 table handle */
1.126043 +  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
1.126044 +){
1.126045 +  sqlite3_stmt *pStmt = 0;
1.126046 +  int rc;
1.126047 +  rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
1.126048 +  if( rc==SQLITE_OK ){
1.126049 +    sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
1.126050 +    if( sqlite3_step(pStmt)!=SQLITE_ROW
1.126051 +     || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
1.126052 +    ){
1.126053 +      rc = sqlite3_reset(pStmt);
1.126054 +      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
1.126055 +      pStmt = 0;
1.126056 +    }
1.126057 +  }
1.126058 +  *ppStmt = pStmt;
1.126059 +  return rc;
1.126060 +}
1.126061 +
1.126062 +SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
1.126063 +  Fts3Table *pTab,                /* Fts3 table handle */
1.126064 +  sqlite3_int64 iDocid,           /* Docid to read size data for */
1.126065 +  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
1.126066 +){
1.126067 +  return fts3SelectDocsize(pTab, iDocid, ppStmt);
1.126068 +}
1.126069 +
1.126070 +/*
1.126071 +** Similar to fts3SqlStmt(). Except, after binding the parameters in
1.126072 +** array apVal[] to the SQL statement identified by eStmt, the statement
1.126073 +** is executed.
1.126074 +**
1.126075 +** Returns SQLITE_OK if the statement is successfully executed, or an
1.126076 +** SQLite error code otherwise.
1.126077 +*/
1.126078 +static void fts3SqlExec(
1.126079 +  int *pRC,                /* Result code */
1.126080 +  Fts3Table *p,            /* The FTS3 table */
1.126081 +  int eStmt,               /* Index of statement to evaluate */
1.126082 +  sqlite3_value **apVal    /* Parameters to bind */
1.126083 +){
1.126084 +  sqlite3_stmt *pStmt;
1.126085 +  int rc;
1.126086 +  if( *pRC ) return;
1.126087 +  rc = fts3SqlStmt(p, eStmt, &pStmt, apVal); 
1.126088 +  if( rc==SQLITE_OK ){
1.126089 +    sqlite3_step(pStmt);
1.126090 +    rc = sqlite3_reset(pStmt);
1.126091 +  }
1.126092 +  *pRC = rc;
1.126093 +}
1.126094 +
1.126095 +
1.126096 +/*
1.126097 +** This function ensures that the caller has obtained a shared-cache
1.126098 +** table-lock on the %_content table. This is required before reading
1.126099 +** data from the fts3 table. If this lock is not acquired first, then
1.126100 +** the caller may end up holding read-locks on the %_segments and %_segdir
1.126101 +** tables, but no read-lock on the %_content table. If this happens 
1.126102 +** a second connection will be able to write to the fts3 table, but
1.126103 +** attempting to commit those writes might return SQLITE_LOCKED or
1.126104 +** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain 
1.126105 +** write-locks on the %_segments and %_segdir ** tables). 
1.126106 +**
1.126107 +** We try to avoid this because if FTS3 returns any error when committing
1.126108 +** a transaction, the whole transaction will be rolled back. And this is
1.126109 +** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
1.126110 +** still happen if the user reads data directly from the %_segments or
1.126111 +** %_segdir tables instead of going through FTS3 though.
1.126112 +**
1.126113 +** This reasoning does not apply to a content=xxx table.
1.126114 +*/
1.126115 +SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
1.126116 +  int rc;                         /* Return code */
1.126117 +  sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
1.126118 +
1.126119 +  if( p->zContentTbl==0 ){
1.126120 +    rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
1.126121 +    if( rc==SQLITE_OK ){
1.126122 +      sqlite3_bind_null(pStmt, 1);
1.126123 +      sqlite3_step(pStmt);
1.126124 +      rc = sqlite3_reset(pStmt);
1.126125 +    }
1.126126 +  }else{
1.126127 +    rc = SQLITE_OK;
1.126128 +  }
1.126129 +
1.126130 +  return rc;
1.126131 +}
1.126132 +
1.126133 +/*
1.126134 +** FTS maintains a separate indexes for each language-id (a 32-bit integer).
1.126135 +** Within each language id, a separate index is maintained to store the
1.126136 +** document terms, and each configured prefix size (configured the FTS 
1.126137 +** "prefix=" option). And each index consists of multiple levels ("relative
1.126138 +** levels").
1.126139 +**
1.126140 +** All three of these values (the language id, the specific index and the
1.126141 +** level within the index) are encoded in 64-bit integer values stored
1.126142 +** in the %_segdir table on disk. This function is used to convert three
1.126143 +** separate component values into the single 64-bit integer value that
1.126144 +** can be used to query the %_segdir table.
1.126145 +**
1.126146 +** Specifically, each language-id/index combination is allocated 1024 
1.126147 +** 64-bit integer level values ("absolute levels"). The main terms index
1.126148 +** for language-id 0 is allocate values 0-1023. The first prefix index
1.126149 +** (if any) for language-id 0 is allocated values 1024-2047. And so on.
1.126150 +** Language 1 indexes are allocated immediately following language 0.
1.126151 +**
1.126152 +** So, for a system with nPrefix prefix indexes configured, the block of
1.126153 +** absolute levels that corresponds to language-id iLangid and index 
1.126154 +** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
1.126155 +*/
1.126156 +static sqlite3_int64 getAbsoluteLevel(
1.126157 +  Fts3Table *p,                   /* FTS3 table handle */
1.126158 +  int iLangid,                    /* Language id */
1.126159 +  int iIndex,                     /* Index in p->aIndex[] */
1.126160 +  int iLevel                      /* Level of segments */
1.126161 +){
1.126162 +  sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
1.126163 +  assert( iLangid>=0 );
1.126164 +  assert( p->nIndex>0 );
1.126165 +  assert( iIndex>=0 && iIndex<p->nIndex );
1.126166 +
1.126167 +  iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
1.126168 +  return iBase + iLevel;
1.126169 +}
1.126170 +
1.126171 +/*
1.126172 +** Set *ppStmt to a statement handle that may be used to iterate through
1.126173 +** all rows in the %_segdir table, from oldest to newest. If successful,
1.126174 +** return SQLITE_OK. If an error occurs while preparing the statement, 
1.126175 +** return an SQLite error code.
1.126176 +**
1.126177 +** There is only ever one instance of this SQL statement compiled for
1.126178 +** each FTS3 table.
1.126179 +**
1.126180 +** The statement returns the following columns from the %_segdir table:
1.126181 +**
1.126182 +**   0: idx
1.126183 +**   1: start_block
1.126184 +**   2: leaves_end_block
1.126185 +**   3: end_block
1.126186 +**   4: root
1.126187 +*/
1.126188 +SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
1.126189 +  Fts3Table *p,                   /* FTS3 table */
1.126190 +  int iLangid,                    /* Language being queried */
1.126191 +  int iIndex,                     /* Index for p->aIndex[] */
1.126192 +  int iLevel,                     /* Level to select (relative level) */
1.126193 +  sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
1.126194 +){
1.126195 +  int rc;
1.126196 +  sqlite3_stmt *pStmt = 0;
1.126197 +
1.126198 +  assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
1.126199 +  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
1.126200 +  assert( iIndex>=0 && iIndex<p->nIndex );
1.126201 +
1.126202 +  if( iLevel<0 ){
1.126203 +    /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
1.126204 +    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
1.126205 +    if( rc==SQLITE_OK ){ 
1.126206 +      sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
1.126207 +      sqlite3_bind_int64(pStmt, 2, 
1.126208 +          getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
1.126209 +      );
1.126210 +    }
1.126211 +  }else{
1.126212 +    /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
1.126213 +    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
1.126214 +    if( rc==SQLITE_OK ){ 
1.126215 +      sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
1.126216 +    }
1.126217 +  }
1.126218 +  *ppStmt = pStmt;
1.126219 +  return rc;
1.126220 +}
1.126221 +
1.126222 +
1.126223 +/*
1.126224 +** Append a single varint to a PendingList buffer. SQLITE_OK is returned
1.126225 +** if successful, or an SQLite error code otherwise.
1.126226 +**
1.126227 +** This function also serves to allocate the PendingList structure itself.
1.126228 +** For example, to create a new PendingList structure containing two
1.126229 +** varints:
1.126230 +**
1.126231 +**   PendingList *p = 0;
1.126232 +**   fts3PendingListAppendVarint(&p, 1);
1.126233 +**   fts3PendingListAppendVarint(&p, 2);
1.126234 +*/
1.126235 +static int fts3PendingListAppendVarint(
1.126236 +  PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
1.126237 +  sqlite3_int64 i                 /* Value to append to data */
1.126238 +){
1.126239 +  PendingList *p = *pp;
1.126240 +
1.126241 +  /* Allocate or grow the PendingList as required. */
1.126242 +  if( !p ){
1.126243 +    p = sqlite3_malloc(sizeof(*p) + 100);
1.126244 +    if( !p ){
1.126245 +      return SQLITE_NOMEM;
1.126246 +    }
1.126247 +    p->nSpace = 100;
1.126248 +    p->aData = (char *)&p[1];
1.126249 +    p->nData = 0;
1.126250 +  }
1.126251 +  else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
1.126252 +    int nNew = p->nSpace * 2;
1.126253 +    p = sqlite3_realloc(p, sizeof(*p) + nNew);
1.126254 +    if( !p ){
1.126255 +      sqlite3_free(*pp);
1.126256 +      *pp = 0;
1.126257 +      return SQLITE_NOMEM;
1.126258 +    }
1.126259 +    p->nSpace = nNew;
1.126260 +    p->aData = (char *)&p[1];
1.126261 +  }
1.126262 +
1.126263 +  /* Append the new serialized varint to the end of the list. */
1.126264 +  p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
1.126265 +  p->aData[p->nData] = '\0';
1.126266 +  *pp = p;
1.126267 +  return SQLITE_OK;
1.126268 +}
1.126269 +
1.126270 +/*
1.126271 +** Add a docid/column/position entry to a PendingList structure. Non-zero
1.126272 +** is returned if the structure is sqlite3_realloced as part of adding
1.126273 +** the entry. Otherwise, zero.
1.126274 +**
1.126275 +** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
1.126276 +** Zero is always returned in this case. Otherwise, if no OOM error occurs,
1.126277 +** it is set to SQLITE_OK.
1.126278 +*/
1.126279 +static int fts3PendingListAppend(
1.126280 +  PendingList **pp,               /* IN/OUT: PendingList structure */
1.126281 +  sqlite3_int64 iDocid,           /* Docid for entry to add */
1.126282 +  sqlite3_int64 iCol,             /* Column for entry to add */
1.126283 +  sqlite3_int64 iPos,             /* Position of term for entry to add */
1.126284 +  int *pRc                        /* OUT: Return code */
1.126285 +){
1.126286 +  PendingList *p = *pp;
1.126287 +  int rc = SQLITE_OK;
1.126288 +
1.126289 +  assert( !p || p->iLastDocid<=iDocid );
1.126290 +
1.126291 +  if( !p || p->iLastDocid!=iDocid ){
1.126292 +    sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
1.126293 +    if( p ){
1.126294 +      assert( p->nData<p->nSpace );
1.126295 +      assert( p->aData[p->nData]==0 );
1.126296 +      p->nData++;
1.126297 +    }
1.126298 +    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
1.126299 +      goto pendinglistappend_out;
1.126300 +    }
1.126301 +    p->iLastCol = -1;
1.126302 +    p->iLastPos = 0;
1.126303 +    p->iLastDocid = iDocid;
1.126304 +  }
1.126305 +  if( iCol>0 && p->iLastCol!=iCol ){
1.126306 +    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
1.126307 +     || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
1.126308 +    ){
1.126309 +      goto pendinglistappend_out;
1.126310 +    }
1.126311 +    p->iLastCol = iCol;
1.126312 +    p->iLastPos = 0;
1.126313 +  }
1.126314 +  if( iCol>=0 ){
1.126315 +    assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
1.126316 +    rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
1.126317 +    if( rc==SQLITE_OK ){
1.126318 +      p->iLastPos = iPos;
1.126319 +    }
1.126320 +  }
1.126321 +
1.126322 + pendinglistappend_out:
1.126323 +  *pRc = rc;
1.126324 +  if( p!=*pp ){
1.126325 +    *pp = p;
1.126326 +    return 1;
1.126327 +  }
1.126328 +  return 0;
1.126329 +}
1.126330 +
1.126331 +/*
1.126332 +** Free a PendingList object allocated by fts3PendingListAppend().
1.126333 +*/
1.126334 +static void fts3PendingListDelete(PendingList *pList){
1.126335 +  sqlite3_free(pList);
1.126336 +}
1.126337 +
1.126338 +/*
1.126339 +** Add an entry to one of the pending-terms hash tables.
1.126340 +*/
1.126341 +static int fts3PendingTermsAddOne(
1.126342 +  Fts3Table *p,
1.126343 +  int iCol,
1.126344 +  int iPos,
1.126345 +  Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
1.126346 +  const char *zToken,
1.126347 +  int nToken
1.126348 +){
1.126349 +  PendingList *pList;
1.126350 +  int rc = SQLITE_OK;
1.126351 +
1.126352 +  pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
1.126353 +  if( pList ){
1.126354 +    p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
1.126355 +  }
1.126356 +  if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
1.126357 +    if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
1.126358 +      /* Malloc failed while inserting the new entry. This can only 
1.126359 +      ** happen if there was no previous entry for this token.
1.126360 +      */
1.126361 +      assert( 0==fts3HashFind(pHash, zToken, nToken) );
1.126362 +      sqlite3_free(pList);
1.126363 +      rc = SQLITE_NOMEM;
1.126364 +    }
1.126365 +  }
1.126366 +  if( rc==SQLITE_OK ){
1.126367 +    p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
1.126368 +  }
1.126369 +  return rc;
1.126370 +}
1.126371 +
1.126372 +/*
1.126373 +** Tokenize the nul-terminated string zText and add all tokens to the
1.126374 +** pending-terms hash-table. The docid used is that currently stored in
1.126375 +** p->iPrevDocid, and the column is specified by argument iCol.
1.126376 +**
1.126377 +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
1.126378 +*/
1.126379 +static int fts3PendingTermsAdd(
1.126380 +  Fts3Table *p,                   /* Table into which text will be inserted */
1.126381 +  int iLangid,                    /* Language id to use */
1.126382 +  const char *zText,              /* Text of document to be inserted */
1.126383 +  int iCol,                       /* Column into which text is being inserted */
1.126384 +  u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
1.126385 +){
1.126386 +  int rc;
1.126387 +  int iStart = 0;
1.126388 +  int iEnd = 0;
1.126389 +  int iPos = 0;
1.126390 +  int nWord = 0;
1.126391 +
1.126392 +  char const *zToken;
1.126393 +  int nToken = 0;
1.126394 +
1.126395 +  sqlite3_tokenizer *pTokenizer = p->pTokenizer;
1.126396 +  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
1.126397 +  sqlite3_tokenizer_cursor *pCsr;
1.126398 +  int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
1.126399 +      const char**,int*,int*,int*,int*);
1.126400 +
1.126401 +  assert( pTokenizer && pModule );
1.126402 +
1.126403 +  /* If the user has inserted a NULL value, this function may be called with
1.126404 +  ** zText==0. In this case, add zero token entries to the hash table and 
1.126405 +  ** return early. */
1.126406 +  if( zText==0 ){
1.126407 +    *pnWord = 0;
1.126408 +    return SQLITE_OK;
1.126409 +  }
1.126410 +
1.126411 +  rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
1.126412 +  if( rc!=SQLITE_OK ){
1.126413 +    return rc;
1.126414 +  }
1.126415 +
1.126416 +  xNext = pModule->xNext;
1.126417 +  while( SQLITE_OK==rc
1.126418 +      && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
1.126419 +  ){
1.126420 +    int i;
1.126421 +    if( iPos>=nWord ) nWord = iPos+1;
1.126422 +
1.126423 +    /* Positions cannot be negative; we use -1 as a terminator internally.
1.126424 +    ** Tokens must have a non-zero length.
1.126425 +    */
1.126426 +    if( iPos<0 || !zToken || nToken<=0 ){
1.126427 +      rc = SQLITE_ERROR;
1.126428 +      break;
1.126429 +    }
1.126430 +
1.126431 +    /* Add the term to the terms index */
1.126432 +    rc = fts3PendingTermsAddOne(
1.126433 +        p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
1.126434 +    );
1.126435 +    
1.126436 +    /* Add the term to each of the prefix indexes that it is not too 
1.126437 +    ** short for. */
1.126438 +    for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
1.126439 +      struct Fts3Index *pIndex = &p->aIndex[i];
1.126440 +      if( nToken<pIndex->nPrefix ) continue;
1.126441 +      rc = fts3PendingTermsAddOne(
1.126442 +          p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
1.126443 +      );
1.126444 +    }
1.126445 +  }
1.126446 +
1.126447 +  pModule->xClose(pCsr);
1.126448 +  *pnWord += nWord;
1.126449 +  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
1.126450 +}
1.126451 +
1.126452 +/* 
1.126453 +** Calling this function indicates that subsequent calls to 
1.126454 +** fts3PendingTermsAdd() are to add term/position-list pairs for the
1.126455 +** contents of the document with docid iDocid.
1.126456 +*/
1.126457 +static int fts3PendingTermsDocid(
1.126458 +  Fts3Table *p,                   /* Full-text table handle */
1.126459 +  int iLangid,                    /* Language id of row being written */
1.126460 +  sqlite_int64 iDocid             /* Docid of row being written */
1.126461 +){
1.126462 +  assert( iLangid>=0 );
1.126463 +
1.126464 +  /* TODO(shess) Explore whether partially flushing the buffer on
1.126465 +  ** forced-flush would provide better performance.  I suspect that if
1.126466 +  ** we ordered the doclists by size and flushed the largest until the
1.126467 +  ** buffer was half empty, that would let the less frequent terms
1.126468 +  ** generate longer doclists.
1.126469 +  */
1.126470 +  if( iDocid<=p->iPrevDocid 
1.126471 +   || p->iPrevLangid!=iLangid
1.126472 +   || p->nPendingData>p->nMaxPendingData 
1.126473 +  ){
1.126474 +    int rc = sqlite3Fts3PendingTermsFlush(p);
1.126475 +    if( rc!=SQLITE_OK ) return rc;
1.126476 +  }
1.126477 +  p->iPrevDocid = iDocid;
1.126478 +  p->iPrevLangid = iLangid;
1.126479 +  return SQLITE_OK;
1.126480 +}
1.126481 +
1.126482 +/*
1.126483 +** Discard the contents of the pending-terms hash tables. 
1.126484 +*/
1.126485 +SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
1.126486 +  int i;
1.126487 +  for(i=0; i<p->nIndex; i++){
1.126488 +    Fts3HashElem *pElem;
1.126489 +    Fts3Hash *pHash = &p->aIndex[i].hPending;
1.126490 +    for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
1.126491 +      PendingList *pList = (PendingList *)fts3HashData(pElem);
1.126492 +      fts3PendingListDelete(pList);
1.126493 +    }
1.126494 +    fts3HashClear(pHash);
1.126495 +  }
1.126496 +  p->nPendingData = 0;
1.126497 +}
1.126498 +
1.126499 +/*
1.126500 +** This function is called by the xUpdate() method as part of an INSERT
1.126501 +** operation. It adds entries for each term in the new record to the
1.126502 +** pendingTerms hash table.
1.126503 +**
1.126504 +** Argument apVal is the same as the similarly named argument passed to
1.126505 +** fts3InsertData(). Parameter iDocid is the docid of the new row.
1.126506 +*/
1.126507 +static int fts3InsertTerms(
1.126508 +  Fts3Table *p, 
1.126509 +  int iLangid, 
1.126510 +  sqlite3_value **apVal, 
1.126511 +  u32 *aSz
1.126512 +){
1.126513 +  int i;                          /* Iterator variable */
1.126514 +  for(i=2; i<p->nColumn+2; i++){
1.126515 +    const char *zText = (const char *)sqlite3_value_text(apVal[i]);
1.126516 +    int rc = fts3PendingTermsAdd(p, iLangid, zText, i-2, &aSz[i-2]);
1.126517 +    if( rc!=SQLITE_OK ){
1.126518 +      return rc;
1.126519 +    }
1.126520 +    aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
1.126521 +  }
1.126522 +  return SQLITE_OK;
1.126523 +}
1.126524 +
1.126525 +/*
1.126526 +** This function is called by the xUpdate() method for an INSERT operation.
1.126527 +** The apVal parameter is passed a copy of the apVal argument passed by
1.126528 +** SQLite to the xUpdate() method. i.e:
1.126529 +**
1.126530 +**   apVal[0]                Not used for INSERT.
1.126531 +**   apVal[1]                rowid
1.126532 +**   apVal[2]                Left-most user-defined column
1.126533 +**   ...
1.126534 +**   apVal[p->nColumn+1]     Right-most user-defined column
1.126535 +**   apVal[p->nColumn+2]     Hidden column with same name as table
1.126536 +**   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
1.126537 +**   apVal[p->nColumn+4]     Hidden languageid column
1.126538 +*/
1.126539 +static int fts3InsertData(
1.126540 +  Fts3Table *p,                   /* Full-text table */
1.126541 +  sqlite3_value **apVal,          /* Array of values to insert */
1.126542 +  sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
1.126543 +){
1.126544 +  int rc;                         /* Return code */
1.126545 +  sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
1.126546 +
1.126547 +  if( p->zContentTbl ){
1.126548 +    sqlite3_value *pRowid = apVal[p->nColumn+3];
1.126549 +    if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
1.126550 +      pRowid = apVal[1];
1.126551 +    }
1.126552 +    if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
1.126553 +      return SQLITE_CONSTRAINT;
1.126554 +    }
1.126555 +    *piDocid = sqlite3_value_int64(pRowid);
1.126556 +    return SQLITE_OK;
1.126557 +  }
1.126558 +
1.126559 +  /* Locate the statement handle used to insert data into the %_content
1.126560 +  ** table. The SQL for this statement is:
1.126561 +  **
1.126562 +  **   INSERT INTO %_content VALUES(?, ?, ?, ...)
1.126563 +  **
1.126564 +  ** The statement features N '?' variables, where N is the number of user
1.126565 +  ** defined columns in the FTS3 table, plus one for the docid field.
1.126566 +  */
1.126567 +  rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
1.126568 +  if( rc==SQLITE_OK && p->zLanguageid ){
1.126569 +    rc = sqlite3_bind_int(
1.126570 +        pContentInsert, p->nColumn+2, 
1.126571 +        sqlite3_value_int(apVal[p->nColumn+4])
1.126572 +    );
1.126573 +  }
1.126574 +  if( rc!=SQLITE_OK ) return rc;
1.126575 +
1.126576 +  /* There is a quirk here. The users INSERT statement may have specified
1.126577 +  ** a value for the "rowid" field, for the "docid" field, or for both.
1.126578 +  ** Which is a problem, since "rowid" and "docid" are aliases for the
1.126579 +  ** same value. For example:
1.126580 +  **
1.126581 +  **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
1.126582 +  **
1.126583 +  ** In FTS3, this is an error. It is an error to specify non-NULL values
1.126584 +  ** for both docid and some other rowid alias.
1.126585 +  */
1.126586 +  if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
1.126587 +    if( SQLITE_NULL==sqlite3_value_type(apVal[0])
1.126588 +     && SQLITE_NULL!=sqlite3_value_type(apVal[1])
1.126589 +    ){
1.126590 +      /* A rowid/docid conflict. */
1.126591 +      return SQLITE_ERROR;
1.126592 +    }
1.126593 +    rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
1.126594 +    if( rc!=SQLITE_OK ) return rc;
1.126595 +  }
1.126596 +
1.126597 +  /* Execute the statement to insert the record. Set *piDocid to the 
1.126598 +  ** new docid value. 
1.126599 +  */
1.126600 +  sqlite3_step(pContentInsert);
1.126601 +  rc = sqlite3_reset(pContentInsert);
1.126602 +
1.126603 +  *piDocid = sqlite3_last_insert_rowid(p->db);
1.126604 +  return rc;
1.126605 +}
1.126606 +
1.126607 +
1.126608 +
1.126609 +/*
1.126610 +** Remove all data from the FTS3 table. Clear the hash table containing
1.126611 +** pending terms.
1.126612 +*/
1.126613 +static int fts3DeleteAll(Fts3Table *p, int bContent){
1.126614 +  int rc = SQLITE_OK;             /* Return code */
1.126615 +
1.126616 +  /* Discard the contents of the pending-terms hash table. */
1.126617 +  sqlite3Fts3PendingTermsClear(p);
1.126618 +
1.126619 +  /* Delete everything from the shadow tables. Except, leave %_content as
1.126620 +  ** is if bContent is false.  */
1.126621 +  assert( p->zContentTbl==0 || bContent==0 );
1.126622 +  if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
1.126623 +  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
1.126624 +  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
1.126625 +  if( p->bHasDocsize ){
1.126626 +    fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
1.126627 +  }
1.126628 +  if( p->bHasStat ){
1.126629 +    fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
1.126630 +  }
1.126631 +  return rc;
1.126632 +}
1.126633 +
1.126634 +/*
1.126635 +**
1.126636 +*/
1.126637 +static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
1.126638 +  int iLangid = 0;
1.126639 +  if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
1.126640 +  return iLangid;
1.126641 +}
1.126642 +
1.126643 +/*
1.126644 +** The first element in the apVal[] array is assumed to contain the docid
1.126645 +** (an integer) of a row about to be deleted. Remove all terms from the
1.126646 +** full-text index.
1.126647 +*/
1.126648 +static void fts3DeleteTerms( 
1.126649 +  int *pRC,               /* Result code */
1.126650 +  Fts3Table *p,           /* The FTS table to delete from */
1.126651 +  sqlite3_value *pRowid,  /* The docid to be deleted */
1.126652 +  u32 *aSz,               /* Sizes of deleted document written here */
1.126653 +  int *pbFound            /* OUT: Set to true if row really does exist */
1.126654 +){
1.126655 +  int rc;
1.126656 +  sqlite3_stmt *pSelect;
1.126657 +
1.126658 +  assert( *pbFound==0 );
1.126659 +  if( *pRC ) return;
1.126660 +  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
1.126661 +  if( rc==SQLITE_OK ){
1.126662 +    if( SQLITE_ROW==sqlite3_step(pSelect) ){
1.126663 +      int i;
1.126664 +      int iLangid = langidFromSelect(p, pSelect);
1.126665 +      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
1.126666 +      for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
1.126667 +        const char *zText = (const char *)sqlite3_column_text(pSelect, i);
1.126668 +        rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[i-1]);
1.126669 +        aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
1.126670 +      }
1.126671 +      if( rc!=SQLITE_OK ){
1.126672 +        sqlite3_reset(pSelect);
1.126673 +        *pRC = rc;
1.126674 +        return;
1.126675 +      }
1.126676 +      *pbFound = 1;
1.126677 +    }
1.126678 +    rc = sqlite3_reset(pSelect);
1.126679 +  }else{
1.126680 +    sqlite3_reset(pSelect);
1.126681 +  }
1.126682 +  *pRC = rc;
1.126683 +}
1.126684 +
1.126685 +/*
1.126686 +** Forward declaration to account for the circular dependency between
1.126687 +** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
1.126688 +*/
1.126689 +static int fts3SegmentMerge(Fts3Table *, int, int, int);
1.126690 +
1.126691 +/* 
1.126692 +** This function allocates a new level iLevel index in the segdir table.
1.126693 +** Usually, indexes are allocated within a level sequentially starting
1.126694 +** with 0, so the allocated index is one greater than the value returned
1.126695 +** by:
1.126696 +**
1.126697 +**   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
1.126698 +**
1.126699 +** However, if there are already FTS3_MERGE_COUNT indexes at the requested
1.126700 +** level, they are merged into a single level (iLevel+1) segment and the 
1.126701 +** allocated index is 0.
1.126702 +**
1.126703 +** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
1.126704 +** returned. Otherwise, an SQLite error code is returned.
1.126705 +*/
1.126706 +static int fts3AllocateSegdirIdx(
1.126707 +  Fts3Table *p, 
1.126708 +  int iLangid,                    /* Language id */
1.126709 +  int iIndex,                     /* Index for p->aIndex */
1.126710 +  int iLevel, 
1.126711 +  int *piIdx
1.126712 +){
1.126713 +  int rc;                         /* Return Code */
1.126714 +  sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
1.126715 +  int iNext = 0;                  /* Result of query pNextIdx */
1.126716 +
1.126717 +  assert( iLangid>=0 );
1.126718 +  assert( p->nIndex>=1 );
1.126719 +
1.126720 +  /* Set variable iNext to the next available segdir index at level iLevel. */
1.126721 +  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
1.126722 +  if( rc==SQLITE_OK ){
1.126723 +    sqlite3_bind_int64(
1.126724 +        pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
1.126725 +    );
1.126726 +    if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
1.126727 +      iNext = sqlite3_column_int(pNextIdx, 0);
1.126728 +    }
1.126729 +    rc = sqlite3_reset(pNextIdx);
1.126730 +  }
1.126731 +
1.126732 +  if( rc==SQLITE_OK ){
1.126733 +    /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
1.126734 +    ** full, merge all segments in level iLevel into a single iLevel+1
1.126735 +    ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
1.126736 +    ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
1.126737 +    */
1.126738 +    if( iNext>=FTS3_MERGE_COUNT ){
1.126739 +      fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
1.126740 +      rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
1.126741 +      *piIdx = 0;
1.126742 +    }else{
1.126743 +      *piIdx = iNext;
1.126744 +    }
1.126745 +  }
1.126746 +
1.126747 +  return rc;
1.126748 +}
1.126749 +
1.126750 +/*
1.126751 +** The %_segments table is declared as follows:
1.126752 +**
1.126753 +**   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
1.126754 +**
1.126755 +** This function reads data from a single row of the %_segments table. The
1.126756 +** specific row is identified by the iBlockid parameter. If paBlob is not
1.126757 +** NULL, then a buffer is allocated using sqlite3_malloc() and populated
1.126758 +** with the contents of the blob stored in the "block" column of the 
1.126759 +** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
1.126760 +** to the size of the blob in bytes before returning.
1.126761 +**
1.126762 +** If an error occurs, or the table does not contain the specified row,
1.126763 +** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
1.126764 +** paBlob is non-NULL, then it is the responsibility of the caller to
1.126765 +** eventually free the returned buffer.
1.126766 +**
1.126767 +** This function may leave an open sqlite3_blob* handle in the
1.126768 +** Fts3Table.pSegments variable. This handle is reused by subsequent calls
1.126769 +** to this function. The handle may be closed by calling the
1.126770 +** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
1.126771 +** performance improvement, but the blob handle should always be closed
1.126772 +** before control is returned to the user (to prevent a lock being held
1.126773 +** on the database file for longer than necessary). Thus, any virtual table
1.126774 +** method (xFilter etc.) that may directly or indirectly call this function
1.126775 +** must call sqlite3Fts3SegmentsClose() before returning.
1.126776 +*/
1.126777 +SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
1.126778 +  Fts3Table *p,                   /* FTS3 table handle */
1.126779 +  sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
1.126780 +  char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
1.126781 +  int *pnBlob,                    /* OUT: Size of blob data */
1.126782 +  int *pnLoad                     /* OUT: Bytes actually loaded */
1.126783 +){
1.126784 +  int rc;                         /* Return code */
1.126785 +
1.126786 +  /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
1.126787 +  assert( pnBlob );
1.126788 +
1.126789 +  if( p->pSegments ){
1.126790 +    rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
1.126791 +  }else{
1.126792 +    if( 0==p->zSegmentsTbl ){
1.126793 +      p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
1.126794 +      if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
1.126795 +    }
1.126796 +    rc = sqlite3_blob_open(
1.126797 +       p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
1.126798 +    );
1.126799 +  }
1.126800 +
1.126801 +  if( rc==SQLITE_OK ){
1.126802 +    int nByte = sqlite3_blob_bytes(p->pSegments);
1.126803 +    *pnBlob = nByte;
1.126804 +    if( paBlob ){
1.126805 +      char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
1.126806 +      if( !aByte ){
1.126807 +        rc = SQLITE_NOMEM;
1.126808 +      }else{
1.126809 +        if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
1.126810 +          nByte = FTS3_NODE_CHUNKSIZE;
1.126811 +          *pnLoad = nByte;
1.126812 +        }
1.126813 +        rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
1.126814 +        memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
1.126815 +        if( rc!=SQLITE_OK ){
1.126816 +          sqlite3_free(aByte);
1.126817 +          aByte = 0;
1.126818 +        }
1.126819 +      }
1.126820 +      *paBlob = aByte;
1.126821 +    }
1.126822 +  }
1.126823 +
1.126824 +  return rc;
1.126825 +}
1.126826 +
1.126827 +/*
1.126828 +** Close the blob handle at p->pSegments, if it is open. See comments above
1.126829 +** the sqlite3Fts3ReadBlock() function for details.
1.126830 +*/
1.126831 +SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
1.126832 +  sqlite3_blob_close(p->pSegments);
1.126833 +  p->pSegments = 0;
1.126834 +}
1.126835 +    
1.126836 +static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
1.126837 +  int nRead;                      /* Number of bytes to read */
1.126838 +  int rc;                         /* Return code */
1.126839 +
1.126840 +  nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
1.126841 +  rc = sqlite3_blob_read(
1.126842 +      pReader->pBlob, 
1.126843 +      &pReader->aNode[pReader->nPopulate],
1.126844 +      nRead,
1.126845 +      pReader->nPopulate
1.126846 +  );
1.126847 +
1.126848 +  if( rc==SQLITE_OK ){
1.126849 +    pReader->nPopulate += nRead;
1.126850 +    memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
1.126851 +    if( pReader->nPopulate==pReader->nNode ){
1.126852 +      sqlite3_blob_close(pReader->pBlob);
1.126853 +      pReader->pBlob = 0;
1.126854 +      pReader->nPopulate = 0;
1.126855 +    }
1.126856 +  }
1.126857 +  return rc;
1.126858 +}
1.126859 +
1.126860 +static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
1.126861 +  int rc = SQLITE_OK;
1.126862 +  assert( !pReader->pBlob 
1.126863 +       || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
1.126864 +  );
1.126865 +  while( pReader->pBlob && rc==SQLITE_OK 
1.126866 +     &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
1.126867 +  ){
1.126868 +    rc = fts3SegReaderIncrRead(pReader);
1.126869 +  }
1.126870 +  return rc;
1.126871 +}
1.126872 +
1.126873 +/*
1.126874 +** Set an Fts3SegReader cursor to point at EOF.
1.126875 +*/
1.126876 +static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
1.126877 +  if( !fts3SegReaderIsRootOnly(pSeg) ){
1.126878 +    sqlite3_free(pSeg->aNode);
1.126879 +    sqlite3_blob_close(pSeg->pBlob);
1.126880 +    pSeg->pBlob = 0;
1.126881 +  }
1.126882 +  pSeg->aNode = 0;
1.126883 +}
1.126884 +
1.126885 +/*
1.126886 +** Move the iterator passed as the first argument to the next term in the
1.126887 +** segment. If successful, SQLITE_OK is returned. If there is no next term,
1.126888 +** SQLITE_DONE. Otherwise, an SQLite error code.
1.126889 +*/
1.126890 +static int fts3SegReaderNext(
1.126891 +  Fts3Table *p, 
1.126892 +  Fts3SegReader *pReader,
1.126893 +  int bIncr
1.126894 +){
1.126895 +  int rc;                         /* Return code of various sub-routines */
1.126896 +  char *pNext;                    /* Cursor variable */
1.126897 +  int nPrefix;                    /* Number of bytes in term prefix */
1.126898 +  int nSuffix;                    /* Number of bytes in term suffix */
1.126899 +
1.126900 +  if( !pReader->aDoclist ){
1.126901 +    pNext = pReader->aNode;
1.126902 +  }else{
1.126903 +    pNext = &pReader->aDoclist[pReader->nDoclist];
1.126904 +  }
1.126905 +
1.126906 +  if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
1.126907 +
1.126908 +    if( fts3SegReaderIsPending(pReader) ){
1.126909 +      Fts3HashElem *pElem = *(pReader->ppNextElem);
1.126910 +      if( pElem==0 ){
1.126911 +        pReader->aNode = 0;
1.126912 +      }else{
1.126913 +        PendingList *pList = (PendingList *)fts3HashData(pElem);
1.126914 +        pReader->zTerm = (char *)fts3HashKey(pElem);
1.126915 +        pReader->nTerm = fts3HashKeysize(pElem);
1.126916 +        pReader->nNode = pReader->nDoclist = pList->nData + 1;
1.126917 +        pReader->aNode = pReader->aDoclist = pList->aData;
1.126918 +        pReader->ppNextElem++;
1.126919 +        assert( pReader->aNode );
1.126920 +      }
1.126921 +      return SQLITE_OK;
1.126922 +    }
1.126923 +
1.126924 +    fts3SegReaderSetEof(pReader);
1.126925 +
1.126926 +    /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
1.126927 +    ** blocks have already been traversed.  */
1.126928 +    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
1.126929 +    if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
1.126930 +      return SQLITE_OK;
1.126931 +    }
1.126932 +
1.126933 +    rc = sqlite3Fts3ReadBlock(
1.126934 +        p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode, 
1.126935 +        (bIncr ? &pReader->nPopulate : 0)
1.126936 +    );
1.126937 +    if( rc!=SQLITE_OK ) return rc;
1.126938 +    assert( pReader->pBlob==0 );
1.126939 +    if( bIncr && pReader->nPopulate<pReader->nNode ){
1.126940 +      pReader->pBlob = p->pSegments;
1.126941 +      p->pSegments = 0;
1.126942 +    }
1.126943 +    pNext = pReader->aNode;
1.126944 +  }
1.126945 +
1.126946 +  assert( !fts3SegReaderIsPending(pReader) );
1.126947 +
1.126948 +  rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
1.126949 +  if( rc!=SQLITE_OK ) return rc;
1.126950 +  
1.126951 +  /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
1.126952 +  ** safe (no risk of overread) even if the node data is corrupted. */
1.126953 +  pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
1.126954 +  pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
1.126955 +  if( nPrefix<0 || nSuffix<=0 
1.126956 +   || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
1.126957 +  ){
1.126958 +    return FTS_CORRUPT_VTAB;
1.126959 +  }
1.126960 +
1.126961 +  if( nPrefix+nSuffix>pReader->nTermAlloc ){
1.126962 +    int nNew = (nPrefix+nSuffix)*2;
1.126963 +    char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
1.126964 +    if( !zNew ){
1.126965 +      return SQLITE_NOMEM;
1.126966 +    }
1.126967 +    pReader->zTerm = zNew;
1.126968 +    pReader->nTermAlloc = nNew;
1.126969 +  }
1.126970 +
1.126971 +  rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
1.126972 +  if( rc!=SQLITE_OK ) return rc;
1.126973 +
1.126974 +  memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
1.126975 +  pReader->nTerm = nPrefix+nSuffix;
1.126976 +  pNext += nSuffix;
1.126977 +  pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist);
1.126978 +  pReader->aDoclist = pNext;
1.126979 +  pReader->pOffsetList = 0;
1.126980 +
1.126981 +  /* Check that the doclist does not appear to extend past the end of the
1.126982 +  ** b-tree node. And that the final byte of the doclist is 0x00. If either 
1.126983 +  ** of these statements is untrue, then the data structure is corrupt.
1.126984 +  */
1.126985 +  if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
1.126986 +   || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
1.126987 +  ){
1.126988 +    return FTS_CORRUPT_VTAB;
1.126989 +  }
1.126990 +  return SQLITE_OK;
1.126991 +}
1.126992 +
1.126993 +/*
1.126994 +** Set the SegReader to point to the first docid in the doclist associated
1.126995 +** with the current term.
1.126996 +*/
1.126997 +static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
1.126998 +  int rc = SQLITE_OK;
1.126999 +  assert( pReader->aDoclist );
1.127000 +  assert( !pReader->pOffsetList );
1.127001 +  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
1.127002 +    u8 bEof = 0;
1.127003 +    pReader->iDocid = 0;
1.127004 +    pReader->nOffsetList = 0;
1.127005 +    sqlite3Fts3DoclistPrev(0,
1.127006 +        pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList, 
1.127007 +        &pReader->iDocid, &pReader->nOffsetList, &bEof
1.127008 +    );
1.127009 +  }else{
1.127010 +    rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
1.127011 +    if( rc==SQLITE_OK ){
1.127012 +      int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
1.127013 +      pReader->pOffsetList = &pReader->aDoclist[n];
1.127014 +    }
1.127015 +  }
1.127016 +  return rc;
1.127017 +}
1.127018 +
1.127019 +/*
1.127020 +** Advance the SegReader to point to the next docid in the doclist
1.127021 +** associated with the current term.
1.127022 +** 
1.127023 +** If arguments ppOffsetList and pnOffsetList are not NULL, then 
1.127024 +** *ppOffsetList is set to point to the first column-offset list
1.127025 +** in the doclist entry (i.e. immediately past the docid varint).
1.127026 +** *pnOffsetList is set to the length of the set of column-offset
1.127027 +** lists, not including the nul-terminator byte. For example:
1.127028 +*/
1.127029 +static int fts3SegReaderNextDocid(
1.127030 +  Fts3Table *pTab,
1.127031 +  Fts3SegReader *pReader,         /* Reader to advance to next docid */
1.127032 +  char **ppOffsetList,            /* OUT: Pointer to current position-list */
1.127033 +  int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
1.127034 +){
1.127035 +  int rc = SQLITE_OK;
1.127036 +  char *p = pReader->pOffsetList;
1.127037 +  char c = 0;
1.127038 +
1.127039 +  assert( p );
1.127040 +
1.127041 +  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
1.127042 +    /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
1.127043 +    ** Pending-terms doclists are always built up in ascending order, so
1.127044 +    ** we have to iterate through them backwards here. */
1.127045 +    u8 bEof = 0;
1.127046 +    if( ppOffsetList ){
1.127047 +      *ppOffsetList = pReader->pOffsetList;
1.127048 +      *pnOffsetList = pReader->nOffsetList - 1;
1.127049 +    }
1.127050 +    sqlite3Fts3DoclistPrev(0,
1.127051 +        pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
1.127052 +        &pReader->nOffsetList, &bEof
1.127053 +    );
1.127054 +    if( bEof ){
1.127055 +      pReader->pOffsetList = 0;
1.127056 +    }else{
1.127057 +      pReader->pOffsetList = p;
1.127058 +    }
1.127059 +  }else{
1.127060 +    char *pEnd = &pReader->aDoclist[pReader->nDoclist];
1.127061 +
1.127062 +    /* Pointer p currently points at the first byte of an offset list. The
1.127063 +    ** following block advances it to point one byte past the end of
1.127064 +    ** the same offset list. */
1.127065 +    while( 1 ){
1.127066 +  
1.127067 +      /* The following line of code (and the "p++" below the while() loop) is
1.127068 +      ** normally all that is required to move pointer p to the desired 
1.127069 +      ** position. The exception is if this node is being loaded from disk
1.127070 +      ** incrementally and pointer "p" now points to the first byte passed
1.127071 +      ** the populated part of pReader->aNode[].
1.127072 +      */
1.127073 +      while( *p | c ) c = *p++ & 0x80;
1.127074 +      assert( *p==0 );
1.127075 +  
1.127076 +      if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
1.127077 +      rc = fts3SegReaderIncrRead(pReader);
1.127078 +      if( rc!=SQLITE_OK ) return rc;
1.127079 +    }
1.127080 +    p++;
1.127081 +  
1.127082 +    /* If required, populate the output variables with a pointer to and the
1.127083 +    ** size of the previous offset-list.
1.127084 +    */
1.127085 +    if( ppOffsetList ){
1.127086 +      *ppOffsetList = pReader->pOffsetList;
1.127087 +      *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
1.127088 +    }
1.127089 +
1.127090 +    while( p<pEnd && *p==0 ) p++;
1.127091 +  
1.127092 +    /* If there are no more entries in the doclist, set pOffsetList to
1.127093 +    ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
1.127094 +    ** Fts3SegReader.pOffsetList to point to the next offset list before
1.127095 +    ** returning.
1.127096 +    */
1.127097 +    if( p>=pEnd ){
1.127098 +      pReader->pOffsetList = 0;
1.127099 +    }else{
1.127100 +      rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
1.127101 +      if( rc==SQLITE_OK ){
1.127102 +        sqlite3_int64 iDelta;
1.127103 +        pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
1.127104 +        if( pTab->bDescIdx ){
1.127105 +          pReader->iDocid -= iDelta;
1.127106 +        }else{
1.127107 +          pReader->iDocid += iDelta;
1.127108 +        }
1.127109 +      }
1.127110 +    }
1.127111 +  }
1.127112 +
1.127113 +  return SQLITE_OK;
1.127114 +}
1.127115 +
1.127116 +
1.127117 +SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
1.127118 +  Fts3Cursor *pCsr, 
1.127119 +  Fts3MultiSegReader *pMsr,
1.127120 +  int *pnOvfl
1.127121 +){
1.127122 +  Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
1.127123 +  int nOvfl = 0;
1.127124 +  int ii;
1.127125 +  int rc = SQLITE_OK;
1.127126 +  int pgsz = p->nPgsz;
1.127127 +
1.127128 +  assert( p->bFts4 );
1.127129 +  assert( pgsz>0 );
1.127130 +
1.127131 +  for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
1.127132 +    Fts3SegReader *pReader = pMsr->apSegment[ii];
1.127133 +    if( !fts3SegReaderIsPending(pReader) 
1.127134 +     && !fts3SegReaderIsRootOnly(pReader) 
1.127135 +    ){
1.127136 +      sqlite3_int64 jj;
1.127137 +      for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
1.127138 +        int nBlob;
1.127139 +        rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
1.127140 +        if( rc!=SQLITE_OK ) break;
1.127141 +        if( (nBlob+35)>pgsz ){
1.127142 +          nOvfl += (nBlob + 34)/pgsz;
1.127143 +        }
1.127144 +      }
1.127145 +    }
1.127146 +  }
1.127147 +  *pnOvfl = nOvfl;
1.127148 +  return rc;
1.127149 +}
1.127150 +
1.127151 +/*
1.127152 +** Free all allocations associated with the iterator passed as the 
1.127153 +** second argument.
1.127154 +*/
1.127155 +SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
1.127156 +  if( pReader && !fts3SegReaderIsPending(pReader) ){
1.127157 +    sqlite3_free(pReader->zTerm);
1.127158 +    if( !fts3SegReaderIsRootOnly(pReader) ){
1.127159 +      sqlite3_free(pReader->aNode);
1.127160 +      sqlite3_blob_close(pReader->pBlob);
1.127161 +    }
1.127162 +  }
1.127163 +  sqlite3_free(pReader);
1.127164 +}
1.127165 +
1.127166 +/*
1.127167 +** Allocate a new SegReader object.
1.127168 +*/
1.127169 +SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
1.127170 +  int iAge,                       /* Segment "age". */
1.127171 +  int bLookup,                    /* True for a lookup only */
1.127172 +  sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
1.127173 +  sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
1.127174 +  sqlite3_int64 iEndBlock,        /* Final block of segment */
1.127175 +  const char *zRoot,              /* Buffer containing root node */
1.127176 +  int nRoot,                      /* Size of buffer containing root node */
1.127177 +  Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
1.127178 +){
1.127179 +  Fts3SegReader *pReader;         /* Newly allocated SegReader object */
1.127180 +  int nExtra = 0;                 /* Bytes to allocate segment root node */
1.127181 +
1.127182 +  assert( iStartLeaf<=iEndLeaf );
1.127183 +  if( iStartLeaf==0 ){
1.127184 +    nExtra = nRoot + FTS3_NODE_PADDING;
1.127185 +  }
1.127186 +
1.127187 +  pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
1.127188 +  if( !pReader ){
1.127189 +    return SQLITE_NOMEM;
1.127190 +  }
1.127191 +  memset(pReader, 0, sizeof(Fts3SegReader));
1.127192 +  pReader->iIdx = iAge;
1.127193 +  pReader->bLookup = bLookup!=0;
1.127194 +  pReader->iStartBlock = iStartLeaf;
1.127195 +  pReader->iLeafEndBlock = iEndLeaf;
1.127196 +  pReader->iEndBlock = iEndBlock;
1.127197 +
1.127198 +  if( nExtra ){
1.127199 +    /* The entire segment is stored in the root node. */
1.127200 +    pReader->aNode = (char *)&pReader[1];
1.127201 +    pReader->rootOnly = 1;
1.127202 +    pReader->nNode = nRoot;
1.127203 +    memcpy(pReader->aNode, zRoot, nRoot);
1.127204 +    memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
1.127205 +  }else{
1.127206 +    pReader->iCurrentBlock = iStartLeaf-1;
1.127207 +  }
1.127208 +  *ppReader = pReader;
1.127209 +  return SQLITE_OK;
1.127210 +}
1.127211 +
1.127212 +/*
1.127213 +** This is a comparison function used as a qsort() callback when sorting
1.127214 +** an array of pending terms by term. This occurs as part of flushing
1.127215 +** the contents of the pending-terms hash table to the database.
1.127216 +*/
1.127217 +static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
1.127218 +  char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
1.127219 +  char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
1.127220 +  int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
1.127221 +  int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
1.127222 +
1.127223 +  int n = (n1<n2 ? n1 : n2);
1.127224 +  int c = memcmp(z1, z2, n);
1.127225 +  if( c==0 ){
1.127226 +    c = n1 - n2;
1.127227 +  }
1.127228 +  return c;
1.127229 +}
1.127230 +
1.127231 +/*
1.127232 +** This function is used to allocate an Fts3SegReader that iterates through
1.127233 +** a subset of the terms stored in the Fts3Table.pendingTerms array.
1.127234 +**
1.127235 +** If the isPrefixIter parameter is zero, then the returned SegReader iterates
1.127236 +** through each term in the pending-terms table. Or, if isPrefixIter is
1.127237 +** non-zero, it iterates through each term and its prefixes. For example, if
1.127238 +** the pending terms hash table contains the terms "sqlite", "mysql" and
1.127239 +** "firebird", then the iterator visits the following 'terms' (in the order
1.127240 +** shown):
1.127241 +**
1.127242 +**   f fi fir fire fireb firebi firebir firebird
1.127243 +**   m my mys mysq mysql
1.127244 +**   s sq sql sqli sqlit sqlite
1.127245 +**
1.127246 +** Whereas if isPrefixIter is zero, the terms visited are:
1.127247 +**
1.127248 +**   firebird mysql sqlite
1.127249 +*/
1.127250 +SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
1.127251 +  Fts3Table *p,                   /* Virtual table handle */
1.127252 +  int iIndex,                     /* Index for p->aIndex */
1.127253 +  const char *zTerm,              /* Term to search for */
1.127254 +  int nTerm,                      /* Size of buffer zTerm */
1.127255 +  int bPrefix,                    /* True for a prefix iterator */
1.127256 +  Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
1.127257 +){
1.127258 +  Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
1.127259 +  Fts3HashElem *pE;               /* Iterator variable */
1.127260 +  Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
1.127261 +  int nElem = 0;                  /* Size of array at aElem */
1.127262 +  int rc = SQLITE_OK;             /* Return Code */
1.127263 +  Fts3Hash *pHash;
1.127264 +
1.127265 +  pHash = &p->aIndex[iIndex].hPending;
1.127266 +  if( bPrefix ){
1.127267 +    int nAlloc = 0;               /* Size of allocated array at aElem */
1.127268 +
1.127269 +    for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
1.127270 +      char *zKey = (char *)fts3HashKey(pE);
1.127271 +      int nKey = fts3HashKeysize(pE);
1.127272 +      if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
1.127273 +        if( nElem==nAlloc ){
1.127274 +          Fts3HashElem **aElem2;
1.127275 +          nAlloc += 16;
1.127276 +          aElem2 = (Fts3HashElem **)sqlite3_realloc(
1.127277 +              aElem, nAlloc*sizeof(Fts3HashElem *)
1.127278 +          );
1.127279 +          if( !aElem2 ){
1.127280 +            rc = SQLITE_NOMEM;
1.127281 +            nElem = 0;
1.127282 +            break;
1.127283 +          }
1.127284 +          aElem = aElem2;
1.127285 +        }
1.127286 +
1.127287 +        aElem[nElem++] = pE;
1.127288 +      }
1.127289 +    }
1.127290 +
1.127291 +    /* If more than one term matches the prefix, sort the Fts3HashElem
1.127292 +    ** objects in term order using qsort(). This uses the same comparison
1.127293 +    ** callback as is used when flushing terms to disk.
1.127294 +    */
1.127295 +    if( nElem>1 ){
1.127296 +      qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
1.127297 +    }
1.127298 +
1.127299 +  }else{
1.127300 +    /* The query is a simple term lookup that matches at most one term in
1.127301 +    ** the index. All that is required is a straight hash-lookup. 
1.127302 +    **
1.127303 +    ** Because the stack address of pE may be accessed via the aElem pointer
1.127304 +    ** below, the "Fts3HashElem *pE" must be declared so that it is valid
1.127305 +    ** within this entire function, not just this "else{...}" block.
1.127306 +    */
1.127307 +    pE = fts3HashFindElem(pHash, zTerm, nTerm);
1.127308 +    if( pE ){
1.127309 +      aElem = &pE;
1.127310 +      nElem = 1;
1.127311 +    }
1.127312 +  }
1.127313 +
1.127314 +  if( nElem>0 ){
1.127315 +    int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
1.127316 +    pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
1.127317 +    if( !pReader ){
1.127318 +      rc = SQLITE_NOMEM;
1.127319 +    }else{
1.127320 +      memset(pReader, 0, nByte);
1.127321 +      pReader->iIdx = 0x7FFFFFFF;
1.127322 +      pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
1.127323 +      memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
1.127324 +    }
1.127325 +  }
1.127326 +
1.127327 +  if( bPrefix ){
1.127328 +    sqlite3_free(aElem);
1.127329 +  }
1.127330 +  *ppReader = pReader;
1.127331 +  return rc;
1.127332 +}
1.127333 +
1.127334 +/*
1.127335 +** Compare the entries pointed to by two Fts3SegReader structures. 
1.127336 +** Comparison is as follows:
1.127337 +**
1.127338 +**   1) EOF is greater than not EOF.
1.127339 +**
1.127340 +**   2) The current terms (if any) are compared using memcmp(). If one
1.127341 +**      term is a prefix of another, the longer term is considered the
1.127342 +**      larger.
1.127343 +**
1.127344 +**   3) By segment age. An older segment is considered larger.
1.127345 +*/
1.127346 +static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
1.127347 +  int rc;
1.127348 +  if( pLhs->aNode && pRhs->aNode ){
1.127349 +    int rc2 = pLhs->nTerm - pRhs->nTerm;
1.127350 +    if( rc2<0 ){
1.127351 +      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
1.127352 +    }else{
1.127353 +      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
1.127354 +    }
1.127355 +    if( rc==0 ){
1.127356 +      rc = rc2;
1.127357 +    }
1.127358 +  }else{
1.127359 +    rc = (pLhs->aNode==0) - (pRhs->aNode==0);
1.127360 +  }
1.127361 +  if( rc==0 ){
1.127362 +    rc = pRhs->iIdx - pLhs->iIdx;
1.127363 +  }
1.127364 +  assert( rc!=0 );
1.127365 +  return rc;
1.127366 +}
1.127367 +
1.127368 +/*
1.127369 +** A different comparison function for SegReader structures. In this
1.127370 +** version, it is assumed that each SegReader points to an entry in
1.127371 +** a doclist for identical terms. Comparison is made as follows:
1.127372 +**
1.127373 +**   1) EOF (end of doclist in this case) is greater than not EOF.
1.127374 +**
1.127375 +**   2) By current docid.
1.127376 +**
1.127377 +**   3) By segment age. An older segment is considered larger.
1.127378 +*/
1.127379 +static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
1.127380 +  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
1.127381 +  if( rc==0 ){
1.127382 +    if( pLhs->iDocid==pRhs->iDocid ){
1.127383 +      rc = pRhs->iIdx - pLhs->iIdx;
1.127384 +    }else{
1.127385 +      rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
1.127386 +    }
1.127387 +  }
1.127388 +  assert( pLhs->aNode && pRhs->aNode );
1.127389 +  return rc;
1.127390 +}
1.127391 +static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
1.127392 +  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
1.127393 +  if( rc==0 ){
1.127394 +    if( pLhs->iDocid==pRhs->iDocid ){
1.127395 +      rc = pRhs->iIdx - pLhs->iIdx;
1.127396 +    }else{
1.127397 +      rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
1.127398 +    }
1.127399 +  }
1.127400 +  assert( pLhs->aNode && pRhs->aNode );
1.127401 +  return rc;
1.127402 +}
1.127403 +
1.127404 +/*
1.127405 +** Compare the term that the Fts3SegReader object passed as the first argument
1.127406 +** points to with the term specified by arguments zTerm and nTerm. 
1.127407 +**
1.127408 +** If the pSeg iterator is already at EOF, return 0. Otherwise, return
1.127409 +** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
1.127410 +** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
1.127411 +*/
1.127412 +static int fts3SegReaderTermCmp(
1.127413 +  Fts3SegReader *pSeg,            /* Segment reader object */
1.127414 +  const char *zTerm,              /* Term to compare to */
1.127415 +  int nTerm                       /* Size of term zTerm in bytes */
1.127416 +){
1.127417 +  int res = 0;
1.127418 +  if( pSeg->aNode ){
1.127419 +    if( pSeg->nTerm>nTerm ){
1.127420 +      res = memcmp(pSeg->zTerm, zTerm, nTerm);
1.127421 +    }else{
1.127422 +      res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
1.127423 +    }
1.127424 +    if( res==0 ){
1.127425 +      res = pSeg->nTerm-nTerm;
1.127426 +    }
1.127427 +  }
1.127428 +  return res;
1.127429 +}
1.127430 +
1.127431 +/*
1.127432 +** Argument apSegment is an array of nSegment elements. It is known that
1.127433 +** the final (nSegment-nSuspect) members are already in sorted order
1.127434 +** (according to the comparison function provided). This function shuffles
1.127435 +** the array around until all entries are in sorted order.
1.127436 +*/
1.127437 +static void fts3SegReaderSort(
1.127438 +  Fts3SegReader **apSegment,                     /* Array to sort entries of */
1.127439 +  int nSegment,                                  /* Size of apSegment array */
1.127440 +  int nSuspect,                                  /* Unsorted entry count */
1.127441 +  int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
1.127442 +){
1.127443 +  int i;                          /* Iterator variable */
1.127444 +
1.127445 +  assert( nSuspect<=nSegment );
1.127446 +
1.127447 +  if( nSuspect==nSegment ) nSuspect--;
1.127448 +  for(i=nSuspect-1; i>=0; i--){
1.127449 +    int j;
1.127450 +    for(j=i; j<(nSegment-1); j++){
1.127451 +      Fts3SegReader *pTmp;
1.127452 +      if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
1.127453 +      pTmp = apSegment[j+1];
1.127454 +      apSegment[j+1] = apSegment[j];
1.127455 +      apSegment[j] = pTmp;
1.127456 +    }
1.127457 +  }
1.127458 +
1.127459 +#ifndef NDEBUG
1.127460 +  /* Check that the list really is sorted now. */
1.127461 +  for(i=0; i<(nSuspect-1); i++){
1.127462 +    assert( xCmp(apSegment[i], apSegment[i+1])<0 );
1.127463 +  }
1.127464 +#endif
1.127465 +}
1.127466 +
1.127467 +/* 
1.127468 +** Insert a record into the %_segments table.
1.127469 +*/
1.127470 +static int fts3WriteSegment(
1.127471 +  Fts3Table *p,                   /* Virtual table handle */
1.127472 +  sqlite3_int64 iBlock,           /* Block id for new block */
1.127473 +  char *z,                        /* Pointer to buffer containing block data */
1.127474 +  int n                           /* Size of buffer z in bytes */
1.127475 +){
1.127476 +  sqlite3_stmt *pStmt;
1.127477 +  int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
1.127478 +  if( rc==SQLITE_OK ){
1.127479 +    sqlite3_bind_int64(pStmt, 1, iBlock);
1.127480 +    sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
1.127481 +    sqlite3_step(pStmt);
1.127482 +    rc = sqlite3_reset(pStmt);
1.127483 +  }
1.127484 +  return rc;
1.127485 +}
1.127486 +
1.127487 +/*
1.127488 +** Find the largest relative level number in the table. If successful, set
1.127489 +** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
1.127490 +** set *pnMax to zero and return an SQLite error code.
1.127491 +*/
1.127492 +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
1.127493 +  int rc;
1.127494 +  int mxLevel = 0;
1.127495 +  sqlite3_stmt *pStmt = 0;
1.127496 +
1.127497 +  rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
1.127498 +  if( rc==SQLITE_OK ){
1.127499 +    if( SQLITE_ROW==sqlite3_step(pStmt) ){
1.127500 +      mxLevel = sqlite3_column_int(pStmt, 0);
1.127501 +    }
1.127502 +    rc = sqlite3_reset(pStmt);
1.127503 +  }
1.127504 +  *pnMax = mxLevel;
1.127505 +  return rc;
1.127506 +}
1.127507 +
1.127508 +/* 
1.127509 +** Insert a record into the %_segdir table.
1.127510 +*/
1.127511 +static int fts3WriteSegdir(
1.127512 +  Fts3Table *p,                   /* Virtual table handle */
1.127513 +  sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
1.127514 +  int iIdx,                       /* Value for "idx" field */
1.127515 +  sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
1.127516 +  sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
1.127517 +  sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
1.127518 +  char *zRoot,                    /* Blob value for "root" field */
1.127519 +  int nRoot                       /* Number of bytes in buffer zRoot */
1.127520 +){
1.127521 +  sqlite3_stmt *pStmt;
1.127522 +  int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
1.127523 +  if( rc==SQLITE_OK ){
1.127524 +    sqlite3_bind_int64(pStmt, 1, iLevel);
1.127525 +    sqlite3_bind_int(pStmt, 2, iIdx);
1.127526 +    sqlite3_bind_int64(pStmt, 3, iStartBlock);
1.127527 +    sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
1.127528 +    sqlite3_bind_int64(pStmt, 5, iEndBlock);
1.127529 +    sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
1.127530 +    sqlite3_step(pStmt);
1.127531 +    rc = sqlite3_reset(pStmt);
1.127532 +  }
1.127533 +  return rc;
1.127534 +}
1.127535 +
1.127536 +/*
1.127537 +** Return the size of the common prefix (if any) shared by zPrev and
1.127538 +** zNext, in bytes. For example, 
1.127539 +**
1.127540 +**   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
1.127541 +**   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
1.127542 +**   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
1.127543 +*/
1.127544 +static int fts3PrefixCompress(
1.127545 +  const char *zPrev,              /* Buffer containing previous term */
1.127546 +  int nPrev,                      /* Size of buffer zPrev in bytes */
1.127547 +  const char *zNext,              /* Buffer containing next term */
1.127548 +  int nNext                       /* Size of buffer zNext in bytes */
1.127549 +){
1.127550 +  int n;
1.127551 +  UNUSED_PARAMETER(nNext);
1.127552 +  for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
1.127553 +  return n;
1.127554 +}
1.127555 +
1.127556 +/*
1.127557 +** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
1.127558 +** (according to memcmp) than the previous term.
1.127559 +*/
1.127560 +static int fts3NodeAddTerm(
1.127561 +  Fts3Table *p,                   /* Virtual table handle */
1.127562 +  SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */ 
1.127563 +  int isCopyTerm,                 /* True if zTerm/nTerm is transient */
1.127564 +  const char *zTerm,              /* Pointer to buffer containing term */
1.127565 +  int nTerm                       /* Size of term in bytes */
1.127566 +){
1.127567 +  SegmentNode *pTree = *ppTree;
1.127568 +  int rc;
1.127569 +  SegmentNode *pNew;
1.127570 +
1.127571 +  /* First try to append the term to the current node. Return early if 
1.127572 +  ** this is possible.
1.127573 +  */
1.127574 +  if( pTree ){
1.127575 +    int nData = pTree->nData;     /* Current size of node in bytes */
1.127576 +    int nReq = nData;             /* Required space after adding zTerm */
1.127577 +    int nPrefix;                  /* Number of bytes of prefix compression */
1.127578 +    int nSuffix;                  /* Suffix length */
1.127579 +
1.127580 +    nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
1.127581 +    nSuffix = nTerm-nPrefix;
1.127582 +
1.127583 +    nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
1.127584 +    if( nReq<=p->nNodeSize || !pTree->zTerm ){
1.127585 +
1.127586 +      if( nReq>p->nNodeSize ){
1.127587 +        /* An unusual case: this is the first term to be added to the node
1.127588 +        ** and the static node buffer (p->nNodeSize bytes) is not large
1.127589 +        ** enough. Use a separately malloced buffer instead This wastes
1.127590 +        ** p->nNodeSize bytes, but since this scenario only comes about when
1.127591 +        ** the database contain two terms that share a prefix of almost 2KB, 
1.127592 +        ** this is not expected to be a serious problem. 
1.127593 +        */
1.127594 +        assert( pTree->aData==(char *)&pTree[1] );
1.127595 +        pTree->aData = (char *)sqlite3_malloc(nReq);
1.127596 +        if( !pTree->aData ){
1.127597 +          return SQLITE_NOMEM;
1.127598 +        }
1.127599 +      }
1.127600 +
1.127601 +      if( pTree->zTerm ){
1.127602 +        /* There is no prefix-length field for first term in a node */
1.127603 +        nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
1.127604 +      }
1.127605 +
1.127606 +      nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
1.127607 +      memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
1.127608 +      pTree->nData = nData + nSuffix;
1.127609 +      pTree->nEntry++;
1.127610 +
1.127611 +      if( isCopyTerm ){
1.127612 +        if( pTree->nMalloc<nTerm ){
1.127613 +          char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
1.127614 +          if( !zNew ){
1.127615 +            return SQLITE_NOMEM;
1.127616 +          }
1.127617 +          pTree->nMalloc = nTerm*2;
1.127618 +          pTree->zMalloc = zNew;
1.127619 +        }
1.127620 +        pTree->zTerm = pTree->zMalloc;
1.127621 +        memcpy(pTree->zTerm, zTerm, nTerm);
1.127622 +        pTree->nTerm = nTerm;
1.127623 +      }else{
1.127624 +        pTree->zTerm = (char *)zTerm;
1.127625 +        pTree->nTerm = nTerm;
1.127626 +      }
1.127627 +      return SQLITE_OK;
1.127628 +    }
1.127629 +  }
1.127630 +
1.127631 +  /* If control flows to here, it was not possible to append zTerm to the
1.127632 +  ** current node. Create a new node (a right-sibling of the current node).
1.127633 +  ** If this is the first node in the tree, the term is added to it.
1.127634 +  **
1.127635 +  ** Otherwise, the term is not added to the new node, it is left empty for
1.127636 +  ** now. Instead, the term is inserted into the parent of pTree. If pTree 
1.127637 +  ** has no parent, one is created here.
1.127638 +  */
1.127639 +  pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
1.127640 +  if( !pNew ){
1.127641 +    return SQLITE_NOMEM;
1.127642 +  }
1.127643 +  memset(pNew, 0, sizeof(SegmentNode));
1.127644 +  pNew->nData = 1 + FTS3_VARINT_MAX;
1.127645 +  pNew->aData = (char *)&pNew[1];
1.127646 +
1.127647 +  if( pTree ){
1.127648 +    SegmentNode *pParent = pTree->pParent;
1.127649 +    rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
1.127650 +    if( pTree->pParent==0 ){
1.127651 +      pTree->pParent = pParent;
1.127652 +    }
1.127653 +    pTree->pRight = pNew;
1.127654 +    pNew->pLeftmost = pTree->pLeftmost;
1.127655 +    pNew->pParent = pParent;
1.127656 +    pNew->zMalloc = pTree->zMalloc;
1.127657 +    pNew->nMalloc = pTree->nMalloc;
1.127658 +    pTree->zMalloc = 0;
1.127659 +  }else{
1.127660 +    pNew->pLeftmost = pNew;
1.127661 +    rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); 
1.127662 +  }
1.127663 +
1.127664 +  *ppTree = pNew;
1.127665 +  return rc;
1.127666 +}
1.127667 +
1.127668 +/*
1.127669 +** Helper function for fts3NodeWrite().
1.127670 +*/
1.127671 +static int fts3TreeFinishNode(
1.127672 +  SegmentNode *pTree, 
1.127673 +  int iHeight, 
1.127674 +  sqlite3_int64 iLeftChild
1.127675 +){
1.127676 +  int nStart;
1.127677 +  assert( iHeight>=1 && iHeight<128 );
1.127678 +  nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
1.127679 +  pTree->aData[nStart] = (char)iHeight;
1.127680 +  sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
1.127681 +  return nStart;
1.127682 +}
1.127683 +
1.127684 +/*
1.127685 +** Write the buffer for the segment node pTree and all of its peers to the
1.127686 +** database. Then call this function recursively to write the parent of 
1.127687 +** pTree and its peers to the database. 
1.127688 +**
1.127689 +** Except, if pTree is a root node, do not write it to the database. Instead,
1.127690 +** set output variables *paRoot and *pnRoot to contain the root node.
1.127691 +**
1.127692 +** If successful, SQLITE_OK is returned and output variable *piLast is
1.127693 +** set to the largest blockid written to the database (or zero if no
1.127694 +** blocks were written to the db). Otherwise, an SQLite error code is 
1.127695 +** returned.
1.127696 +*/
1.127697 +static int fts3NodeWrite(
1.127698 +  Fts3Table *p,                   /* Virtual table handle */
1.127699 +  SegmentNode *pTree,             /* SegmentNode handle */
1.127700 +  int iHeight,                    /* Height of this node in tree */
1.127701 +  sqlite3_int64 iLeaf,            /* Block id of first leaf node */
1.127702 +  sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
1.127703 +  sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
1.127704 +  char **paRoot,                  /* OUT: Data for root node */
1.127705 +  int *pnRoot                     /* OUT: Size of root node in bytes */
1.127706 +){
1.127707 +  int rc = SQLITE_OK;
1.127708 +
1.127709 +  if( !pTree->pParent ){
1.127710 +    /* Root node of the tree. */
1.127711 +    int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
1.127712 +    *piLast = iFree-1;
1.127713 +    *pnRoot = pTree->nData - nStart;
1.127714 +    *paRoot = &pTree->aData[nStart];
1.127715 +  }else{
1.127716 +    SegmentNode *pIter;
1.127717 +    sqlite3_int64 iNextFree = iFree;
1.127718 +    sqlite3_int64 iNextLeaf = iLeaf;
1.127719 +    for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
1.127720 +      int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
1.127721 +      int nWrite = pIter->nData - nStart;
1.127722 +  
1.127723 +      rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
1.127724 +      iNextFree++;
1.127725 +      iNextLeaf += (pIter->nEntry+1);
1.127726 +    }
1.127727 +    if( rc==SQLITE_OK ){
1.127728 +      assert( iNextLeaf==iFree );
1.127729 +      rc = fts3NodeWrite(
1.127730 +          p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
1.127731 +      );
1.127732 +    }
1.127733 +  }
1.127734 +
1.127735 +  return rc;
1.127736 +}
1.127737 +
1.127738 +/*
1.127739 +** Free all memory allocations associated with the tree pTree.
1.127740 +*/
1.127741 +static void fts3NodeFree(SegmentNode *pTree){
1.127742 +  if( pTree ){
1.127743 +    SegmentNode *p = pTree->pLeftmost;
1.127744 +    fts3NodeFree(p->pParent);
1.127745 +    while( p ){
1.127746 +      SegmentNode *pRight = p->pRight;
1.127747 +      if( p->aData!=(char *)&p[1] ){
1.127748 +        sqlite3_free(p->aData);
1.127749 +      }
1.127750 +      assert( pRight==0 || p->zMalloc==0 );
1.127751 +      sqlite3_free(p->zMalloc);
1.127752 +      sqlite3_free(p);
1.127753 +      p = pRight;
1.127754 +    }
1.127755 +  }
1.127756 +}
1.127757 +
1.127758 +/*
1.127759 +** Add a term to the segment being constructed by the SegmentWriter object
1.127760 +** *ppWriter. When adding the first term to a segment, *ppWriter should
1.127761 +** be passed NULL. This function will allocate a new SegmentWriter object
1.127762 +** and return it via the input/output variable *ppWriter in this case.
1.127763 +**
1.127764 +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
1.127765 +*/
1.127766 +static int fts3SegWriterAdd(
1.127767 +  Fts3Table *p,                   /* Virtual table handle */
1.127768 +  SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */ 
1.127769 +  int isCopyTerm,                 /* True if buffer zTerm must be copied */
1.127770 +  const char *zTerm,              /* Pointer to buffer containing term */
1.127771 +  int nTerm,                      /* Size of term in bytes */
1.127772 +  const char *aDoclist,           /* Pointer to buffer containing doclist */
1.127773 +  int nDoclist                    /* Size of doclist in bytes */
1.127774 +){
1.127775 +  int nPrefix;                    /* Size of term prefix in bytes */
1.127776 +  int nSuffix;                    /* Size of term suffix in bytes */
1.127777 +  int nReq;                       /* Number of bytes required on leaf page */
1.127778 +  int nData;
1.127779 +  SegmentWriter *pWriter = *ppWriter;
1.127780 +
1.127781 +  if( !pWriter ){
1.127782 +    int rc;
1.127783 +    sqlite3_stmt *pStmt;
1.127784 +
1.127785 +    /* Allocate the SegmentWriter structure */
1.127786 +    pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
1.127787 +    if( !pWriter ) return SQLITE_NOMEM;
1.127788 +    memset(pWriter, 0, sizeof(SegmentWriter));
1.127789 +    *ppWriter = pWriter;
1.127790 +
1.127791 +    /* Allocate a buffer in which to accumulate data */
1.127792 +    pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
1.127793 +    if( !pWriter->aData ) return SQLITE_NOMEM;
1.127794 +    pWriter->nSize = p->nNodeSize;
1.127795 +
1.127796 +    /* Find the next free blockid in the %_segments table */
1.127797 +    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
1.127798 +    if( rc!=SQLITE_OK ) return rc;
1.127799 +    if( SQLITE_ROW==sqlite3_step(pStmt) ){
1.127800 +      pWriter->iFree = sqlite3_column_int64(pStmt, 0);
1.127801 +      pWriter->iFirst = pWriter->iFree;
1.127802 +    }
1.127803 +    rc = sqlite3_reset(pStmt);
1.127804 +    if( rc!=SQLITE_OK ) return rc;
1.127805 +  }
1.127806 +  nData = pWriter->nData;
1.127807 +
1.127808 +  nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
1.127809 +  nSuffix = nTerm-nPrefix;
1.127810 +
1.127811 +  /* Figure out how many bytes are required by this new entry */
1.127812 +  nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
1.127813 +    sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
1.127814 +    nSuffix +                               /* Term suffix */
1.127815 +    sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
1.127816 +    nDoclist;                               /* Doclist data */
1.127817 +
1.127818 +  if( nData>0 && nData+nReq>p->nNodeSize ){
1.127819 +    int rc;
1.127820 +
1.127821 +    /* The current leaf node is full. Write it out to the database. */
1.127822 +    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
1.127823 +    if( rc!=SQLITE_OK ) return rc;
1.127824 +    p->nLeafAdd++;
1.127825 +
1.127826 +    /* Add the current term to the interior node tree. The term added to
1.127827 +    ** the interior tree must:
1.127828 +    **
1.127829 +    **   a) be greater than the largest term on the leaf node just written
1.127830 +    **      to the database (still available in pWriter->zTerm), and
1.127831 +    **
1.127832 +    **   b) be less than or equal to the term about to be added to the new
1.127833 +    **      leaf node (zTerm/nTerm).
1.127834 +    **
1.127835 +    ** In other words, it must be the prefix of zTerm 1 byte longer than
1.127836 +    ** the common prefix (if any) of zTerm and pWriter->zTerm.
1.127837 +    */
1.127838 +    assert( nPrefix<nTerm );
1.127839 +    rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
1.127840 +    if( rc!=SQLITE_OK ) return rc;
1.127841 +
1.127842 +    nData = 0;
1.127843 +    pWriter->nTerm = 0;
1.127844 +
1.127845 +    nPrefix = 0;
1.127846 +    nSuffix = nTerm;
1.127847 +    nReq = 1 +                              /* varint containing prefix size */
1.127848 +      sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
1.127849 +      nTerm +                               /* Term suffix */
1.127850 +      sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
1.127851 +      nDoclist;                             /* Doclist data */
1.127852 +  }
1.127853 +
1.127854 +  /* If the buffer currently allocated is too small for this entry, realloc
1.127855 +  ** the buffer to make it large enough.
1.127856 +  */
1.127857 +  if( nReq>pWriter->nSize ){
1.127858 +    char *aNew = sqlite3_realloc(pWriter->aData, nReq);
1.127859 +    if( !aNew ) return SQLITE_NOMEM;
1.127860 +    pWriter->aData = aNew;
1.127861 +    pWriter->nSize = nReq;
1.127862 +  }
1.127863 +  assert( nData+nReq<=pWriter->nSize );
1.127864 +
1.127865 +  /* Append the prefix-compressed term and doclist to the buffer. */
1.127866 +  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
1.127867 +  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
1.127868 +  memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
1.127869 +  nData += nSuffix;
1.127870 +  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
1.127871 +  memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
1.127872 +  pWriter->nData = nData + nDoclist;
1.127873 +
1.127874 +  /* Save the current term so that it can be used to prefix-compress the next.
1.127875 +  ** If the isCopyTerm parameter is true, then the buffer pointed to by
1.127876 +  ** zTerm is transient, so take a copy of the term data. Otherwise, just
1.127877 +  ** store a copy of the pointer.
1.127878 +  */
1.127879 +  if( isCopyTerm ){
1.127880 +    if( nTerm>pWriter->nMalloc ){
1.127881 +      char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
1.127882 +      if( !zNew ){
1.127883 +        return SQLITE_NOMEM;
1.127884 +      }
1.127885 +      pWriter->nMalloc = nTerm*2;
1.127886 +      pWriter->zMalloc = zNew;
1.127887 +      pWriter->zTerm = zNew;
1.127888 +    }
1.127889 +    assert( pWriter->zTerm==pWriter->zMalloc );
1.127890 +    memcpy(pWriter->zTerm, zTerm, nTerm);
1.127891 +  }else{
1.127892 +    pWriter->zTerm = (char *)zTerm;
1.127893 +  }
1.127894 +  pWriter->nTerm = nTerm;
1.127895 +
1.127896 +  return SQLITE_OK;
1.127897 +}
1.127898 +
1.127899 +/*
1.127900 +** Flush all data associated with the SegmentWriter object pWriter to the
1.127901 +** database. This function must be called after all terms have been added
1.127902 +** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
1.127903 +** returned. Otherwise, an SQLite error code.
1.127904 +*/
1.127905 +static int fts3SegWriterFlush(
1.127906 +  Fts3Table *p,                   /* Virtual table handle */
1.127907 +  SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
1.127908 +  sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
1.127909 +  int iIdx                        /* Value for 'idx' column of %_segdir */
1.127910 +){
1.127911 +  int rc;                         /* Return code */
1.127912 +  if( pWriter->pTree ){
1.127913 +    sqlite3_int64 iLast = 0;      /* Largest block id written to database */
1.127914 +    sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
1.127915 +    char *zRoot = NULL;           /* Pointer to buffer containing root node */
1.127916 +    int nRoot = 0;                /* Size of buffer zRoot */
1.127917 +
1.127918 +    iLastLeaf = pWriter->iFree;
1.127919 +    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
1.127920 +    if( rc==SQLITE_OK ){
1.127921 +      rc = fts3NodeWrite(p, pWriter->pTree, 1,
1.127922 +          pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
1.127923 +    }
1.127924 +    if( rc==SQLITE_OK ){
1.127925 +      rc = fts3WriteSegdir(
1.127926 +          p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
1.127927 +    }
1.127928 +  }else{
1.127929 +    /* The entire tree fits on the root node. Write it to the segdir table. */
1.127930 +    rc = fts3WriteSegdir(
1.127931 +        p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
1.127932 +  }
1.127933 +  p->nLeafAdd++;
1.127934 +  return rc;
1.127935 +}
1.127936 +
1.127937 +/*
1.127938 +** Release all memory held by the SegmentWriter object passed as the 
1.127939 +** first argument.
1.127940 +*/
1.127941 +static void fts3SegWriterFree(SegmentWriter *pWriter){
1.127942 +  if( pWriter ){
1.127943 +    sqlite3_free(pWriter->aData);
1.127944 +    sqlite3_free(pWriter->zMalloc);
1.127945 +    fts3NodeFree(pWriter->pTree);
1.127946 +    sqlite3_free(pWriter);
1.127947 +  }
1.127948 +}
1.127949 +
1.127950 +/*
1.127951 +** The first value in the apVal[] array is assumed to contain an integer.
1.127952 +** This function tests if there exist any documents with docid values that
1.127953 +** are different from that integer. i.e. if deleting the document with docid
1.127954 +** pRowid would mean the FTS3 table were empty.
1.127955 +**
1.127956 +** If successful, *pisEmpty is set to true if the table is empty except for
1.127957 +** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
1.127958 +** error occurs, an SQLite error code is returned.
1.127959 +*/
1.127960 +static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
1.127961 +  sqlite3_stmt *pStmt;
1.127962 +  int rc;
1.127963 +  if( p->zContentTbl ){
1.127964 +    /* If using the content=xxx option, assume the table is never empty */
1.127965 +    *pisEmpty = 0;
1.127966 +    rc = SQLITE_OK;
1.127967 +  }else{
1.127968 +    rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
1.127969 +    if( rc==SQLITE_OK ){
1.127970 +      if( SQLITE_ROW==sqlite3_step(pStmt) ){
1.127971 +        *pisEmpty = sqlite3_column_int(pStmt, 0);
1.127972 +      }
1.127973 +      rc = sqlite3_reset(pStmt);
1.127974 +    }
1.127975 +  }
1.127976 +  return rc;
1.127977 +}
1.127978 +
1.127979 +/*
1.127980 +** Set *pnMax to the largest segment level in the database for the index
1.127981 +** iIndex.
1.127982 +**
1.127983 +** Segment levels are stored in the 'level' column of the %_segdir table.
1.127984 +**
1.127985 +** Return SQLITE_OK if successful, or an SQLite error code if not.
1.127986 +*/
1.127987 +static int fts3SegmentMaxLevel(
1.127988 +  Fts3Table *p, 
1.127989 +  int iLangid,
1.127990 +  int iIndex, 
1.127991 +  sqlite3_int64 *pnMax
1.127992 +){
1.127993 +  sqlite3_stmt *pStmt;
1.127994 +  int rc;
1.127995 +  assert( iIndex>=0 && iIndex<p->nIndex );
1.127996 +
1.127997 +  /* Set pStmt to the compiled version of:
1.127998 +  **
1.127999 +  **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
1.128000 +  **
1.128001 +  ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
1.128002 +  */
1.128003 +  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
1.128004 +  if( rc!=SQLITE_OK ) return rc;
1.128005 +  sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
1.128006 +  sqlite3_bind_int64(pStmt, 2, 
1.128007 +      getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
1.128008 +  );
1.128009 +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
1.128010 +    *pnMax = sqlite3_column_int64(pStmt, 0);
1.128011 +  }
1.128012 +  return sqlite3_reset(pStmt);
1.128013 +}
1.128014 +
1.128015 +/*
1.128016 +** Delete all entries in the %_segments table associated with the segment
1.128017 +** opened with seg-reader pSeg. This function does not affect the contents
1.128018 +** of the %_segdir table.
1.128019 +*/
1.128020 +static int fts3DeleteSegment(
1.128021 +  Fts3Table *p,                   /* FTS table handle */
1.128022 +  Fts3SegReader *pSeg             /* Segment to delete */
1.128023 +){
1.128024 +  int rc = SQLITE_OK;             /* Return code */
1.128025 +  if( pSeg->iStartBlock ){
1.128026 +    sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
1.128027 +    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
1.128028 +    if( rc==SQLITE_OK ){
1.128029 +      sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
1.128030 +      sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
1.128031 +      sqlite3_step(pDelete);
1.128032 +      rc = sqlite3_reset(pDelete);
1.128033 +    }
1.128034 +  }
1.128035 +  return rc;
1.128036 +}
1.128037 +
1.128038 +/*
1.128039 +** This function is used after merging multiple segments into a single large
1.128040 +** segment to delete the old, now redundant, segment b-trees. Specifically,
1.128041 +** it:
1.128042 +** 
1.128043 +**   1) Deletes all %_segments entries for the segments associated with 
1.128044 +**      each of the SegReader objects in the array passed as the third 
1.128045 +**      argument, and
1.128046 +**
1.128047 +**   2) deletes all %_segdir entries with level iLevel, or all %_segdir
1.128048 +**      entries regardless of level if (iLevel<0).
1.128049 +**
1.128050 +** SQLITE_OK is returned if successful, otherwise an SQLite error code.
1.128051 +*/
1.128052 +static int fts3DeleteSegdir(
1.128053 +  Fts3Table *p,                   /* Virtual table handle */
1.128054 +  int iLangid,                    /* Language id */
1.128055 +  int iIndex,                     /* Index for p->aIndex */
1.128056 +  int iLevel,                     /* Level of %_segdir entries to delete */
1.128057 +  Fts3SegReader **apSegment,      /* Array of SegReader objects */
1.128058 +  int nReader                     /* Size of array apSegment */
1.128059 +){
1.128060 +  int rc = SQLITE_OK;             /* Return Code */
1.128061 +  int i;                          /* Iterator variable */
1.128062 +  sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
1.128063 +
1.128064 +  for(i=0; rc==SQLITE_OK && i<nReader; i++){
1.128065 +    rc = fts3DeleteSegment(p, apSegment[i]);
1.128066 +  }
1.128067 +  if( rc!=SQLITE_OK ){
1.128068 +    return rc;
1.128069 +  }
1.128070 +
1.128071 +  assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
1.128072 +  if( iLevel==FTS3_SEGCURSOR_ALL ){
1.128073 +    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
1.128074 +    if( rc==SQLITE_OK ){
1.128075 +      sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
1.128076 +      sqlite3_bind_int64(pDelete, 2, 
1.128077 +          getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
1.128078 +      );
1.128079 +    }
1.128080 +  }else{
1.128081 +    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
1.128082 +    if( rc==SQLITE_OK ){
1.128083 +      sqlite3_bind_int64(
1.128084 +          pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
1.128085 +      );
1.128086 +    }
1.128087 +  }
1.128088 +
1.128089 +  if( rc==SQLITE_OK ){
1.128090 +    sqlite3_step(pDelete);
1.128091 +    rc = sqlite3_reset(pDelete);
1.128092 +  }
1.128093 +
1.128094 +  return rc;
1.128095 +}
1.128096 +
1.128097 +/*
1.128098 +** When this function is called, buffer *ppList (size *pnList bytes) contains 
1.128099 +** a position list that may (or may not) feature multiple columns. This
1.128100 +** function adjusts the pointer *ppList and the length *pnList so that they
1.128101 +** identify the subset of the position list that corresponds to column iCol.
1.128102 +**
1.128103 +** If there are no entries in the input position list for column iCol, then
1.128104 +** *pnList is set to zero before returning.
1.128105 +*/
1.128106 +static void fts3ColumnFilter(
1.128107 +  int iCol,                       /* Column to filter on */
1.128108 +  char **ppList,                  /* IN/OUT: Pointer to position list */
1.128109 +  int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
1.128110 +){
1.128111 +  char *pList = *ppList;
1.128112 +  int nList = *pnList;
1.128113 +  char *pEnd = &pList[nList];
1.128114 +  int iCurrent = 0;
1.128115 +  char *p = pList;
1.128116 +
1.128117 +  assert( iCol>=0 );
1.128118 +  while( 1 ){
1.128119 +    char c = 0;
1.128120 +    while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
1.128121 +  
1.128122 +    if( iCol==iCurrent ){
1.128123 +      nList = (int)(p - pList);
1.128124 +      break;
1.128125 +    }
1.128126 +
1.128127 +    nList -= (int)(p - pList);
1.128128 +    pList = p;
1.128129 +    if( nList==0 ){
1.128130 +      break;
1.128131 +    }
1.128132 +    p = &pList[1];
1.128133 +    p += sqlite3Fts3GetVarint32(p, &iCurrent);
1.128134 +  }
1.128135 +
1.128136 +  *ppList = pList;
1.128137 +  *pnList = nList;
1.128138 +}
1.128139 +
1.128140 +/*
1.128141 +** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
1.128142 +** existing data). Grow the buffer if required.
1.128143 +**
1.128144 +** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
1.128145 +** trying to resize the buffer, return SQLITE_NOMEM.
1.128146 +*/
1.128147 +static int fts3MsrBufferData(
1.128148 +  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
1.128149 +  char *pList,
1.128150 +  int nList
1.128151 +){
1.128152 +  if( nList>pMsr->nBuffer ){
1.128153 +    char *pNew;
1.128154 +    pMsr->nBuffer = nList*2;
1.128155 +    pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
1.128156 +    if( !pNew ) return SQLITE_NOMEM;
1.128157 +    pMsr->aBuffer = pNew;
1.128158 +  }
1.128159 +
1.128160 +  memcpy(pMsr->aBuffer, pList, nList);
1.128161 +  return SQLITE_OK;
1.128162 +}
1.128163 +
1.128164 +SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
1.128165 +  Fts3Table *p,                   /* Virtual table handle */
1.128166 +  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
1.128167 +  sqlite3_int64 *piDocid,         /* OUT: Docid value */
1.128168 +  char **paPoslist,               /* OUT: Pointer to position list */
1.128169 +  int *pnPoslist                  /* OUT: Size of position list in bytes */
1.128170 +){
1.128171 +  int nMerge = pMsr->nAdvance;
1.128172 +  Fts3SegReader **apSegment = pMsr->apSegment;
1.128173 +  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
1.128174 +    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
1.128175 +  );
1.128176 +
1.128177 +  if( nMerge==0 ){
1.128178 +    *paPoslist = 0;
1.128179 +    return SQLITE_OK;
1.128180 +  }
1.128181 +
1.128182 +  while( 1 ){
1.128183 +    Fts3SegReader *pSeg;
1.128184 +    pSeg = pMsr->apSegment[0];
1.128185 +
1.128186 +    if( pSeg->pOffsetList==0 ){
1.128187 +      *paPoslist = 0;
1.128188 +      break;
1.128189 +    }else{
1.128190 +      int rc;
1.128191 +      char *pList;
1.128192 +      int nList;
1.128193 +      int j;
1.128194 +      sqlite3_int64 iDocid = apSegment[0]->iDocid;
1.128195 +
1.128196 +      rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
1.128197 +      j = 1;
1.128198 +      while( rc==SQLITE_OK 
1.128199 +        && j<nMerge
1.128200 +        && apSegment[j]->pOffsetList
1.128201 +        && apSegment[j]->iDocid==iDocid
1.128202 +      ){
1.128203 +        rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
1.128204 +        j++;
1.128205 +      }
1.128206 +      if( rc!=SQLITE_OK ) return rc;
1.128207 +      fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
1.128208 +
1.128209 +      if( pMsr->iColFilter>=0 ){
1.128210 +        fts3ColumnFilter(pMsr->iColFilter, &pList, &nList);
1.128211 +      }
1.128212 +
1.128213 +      if( nList>0 ){
1.128214 +        if( fts3SegReaderIsPending(apSegment[0]) ){
1.128215 +          rc = fts3MsrBufferData(pMsr, pList, nList+1);
1.128216 +          if( rc!=SQLITE_OK ) return rc;
1.128217 +          *paPoslist = pMsr->aBuffer;
1.128218 +          assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
1.128219 +        }else{
1.128220 +          *paPoslist = pList;
1.128221 +        }
1.128222 +        *piDocid = iDocid;
1.128223 +        *pnPoslist = nList;
1.128224 +        break;
1.128225 +      }
1.128226 +    }
1.128227 +  }
1.128228 +
1.128229 +  return SQLITE_OK;
1.128230 +}
1.128231 +
1.128232 +static int fts3SegReaderStart(
1.128233 +  Fts3Table *p,                   /* Virtual table handle */
1.128234 +  Fts3MultiSegReader *pCsr,       /* Cursor object */
1.128235 +  const char *zTerm,              /* Term searched for (or NULL) */
1.128236 +  int nTerm                       /* Length of zTerm in bytes */
1.128237 +){
1.128238 +  int i;
1.128239 +  int nSeg = pCsr->nSegment;
1.128240 +
1.128241 +  /* If the Fts3SegFilter defines a specific term (or term prefix) to search 
1.128242 +  ** for, then advance each segment iterator until it points to a term of
1.128243 +  ** equal or greater value than the specified term. This prevents many
1.128244 +  ** unnecessary merge/sort operations for the case where single segment
1.128245 +  ** b-tree leaf nodes contain more than one term.
1.128246 +  */
1.128247 +  for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
1.128248 +    int res = 0;
1.128249 +    Fts3SegReader *pSeg = pCsr->apSegment[i];
1.128250 +    do {
1.128251 +      int rc = fts3SegReaderNext(p, pSeg, 0);
1.128252 +      if( rc!=SQLITE_OK ) return rc;
1.128253 +    }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
1.128254 +
1.128255 +    if( pSeg->bLookup && res!=0 ){
1.128256 +      fts3SegReaderSetEof(pSeg);
1.128257 +    }
1.128258 +  }
1.128259 +  fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
1.128260 +
1.128261 +  return SQLITE_OK;
1.128262 +}
1.128263 +
1.128264 +SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
1.128265 +  Fts3Table *p,                   /* Virtual table handle */
1.128266 +  Fts3MultiSegReader *pCsr,       /* Cursor object */
1.128267 +  Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
1.128268 +){
1.128269 +  pCsr->pFilter = pFilter;
1.128270 +  return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
1.128271 +}
1.128272 +
1.128273 +SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
1.128274 +  Fts3Table *p,                   /* Virtual table handle */
1.128275 +  Fts3MultiSegReader *pCsr,       /* Cursor object */
1.128276 +  int iCol,                       /* Column to match on. */
1.128277 +  const char *zTerm,              /* Term to iterate through a doclist for */
1.128278 +  int nTerm                       /* Number of bytes in zTerm */
1.128279 +){
1.128280 +  int i;
1.128281 +  int rc;
1.128282 +  int nSegment = pCsr->nSegment;
1.128283 +  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
1.128284 +    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
1.128285 +  );
1.128286 +
1.128287 +  assert( pCsr->pFilter==0 );
1.128288 +  assert( zTerm && nTerm>0 );
1.128289 +
1.128290 +  /* Advance each segment iterator until it points to the term zTerm/nTerm. */
1.128291 +  rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
1.128292 +  if( rc!=SQLITE_OK ) return rc;
1.128293 +
1.128294 +  /* Determine how many of the segments actually point to zTerm/nTerm. */
1.128295 +  for(i=0; i<nSegment; i++){
1.128296 +    Fts3SegReader *pSeg = pCsr->apSegment[i];
1.128297 +    if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
1.128298 +      break;
1.128299 +    }
1.128300 +  }
1.128301 +  pCsr->nAdvance = i;
1.128302 +
1.128303 +  /* Advance each of the segments to point to the first docid. */
1.128304 +  for(i=0; i<pCsr->nAdvance; i++){
1.128305 +    rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
1.128306 +    if( rc!=SQLITE_OK ) return rc;
1.128307 +  }
1.128308 +  fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
1.128309 +
1.128310 +  assert( iCol<0 || iCol<p->nColumn );
1.128311 +  pCsr->iColFilter = iCol;
1.128312 +
1.128313 +  return SQLITE_OK;
1.128314 +}
1.128315 +
1.128316 +/*
1.128317 +** This function is called on a MultiSegReader that has been started using
1.128318 +** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
1.128319 +** have been made. Calling this function puts the MultiSegReader in such
1.128320 +** a state that if the next two calls are:
1.128321 +**
1.128322 +**   sqlite3Fts3SegReaderStart()
1.128323 +**   sqlite3Fts3SegReaderStep()
1.128324 +**
1.128325 +** then the entire doclist for the term is available in 
1.128326 +** MultiSegReader.aDoclist/nDoclist.
1.128327 +*/
1.128328 +SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
1.128329 +  int i;                          /* Used to iterate through segment-readers */
1.128330 +
1.128331 +  assert( pCsr->zTerm==0 );
1.128332 +  assert( pCsr->nTerm==0 );
1.128333 +  assert( pCsr->aDoclist==0 );
1.128334 +  assert( pCsr->nDoclist==0 );
1.128335 +
1.128336 +  pCsr->nAdvance = 0;
1.128337 +  pCsr->bRestart = 1;
1.128338 +  for(i=0; i<pCsr->nSegment; i++){
1.128339 +    pCsr->apSegment[i]->pOffsetList = 0;
1.128340 +    pCsr->apSegment[i]->nOffsetList = 0;
1.128341 +    pCsr->apSegment[i]->iDocid = 0;
1.128342 +  }
1.128343 +
1.128344 +  return SQLITE_OK;
1.128345 +}
1.128346 +
1.128347 +
1.128348 +SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
1.128349 +  Fts3Table *p,                   /* Virtual table handle */
1.128350 +  Fts3MultiSegReader *pCsr        /* Cursor object */
1.128351 +){
1.128352 +  int rc = SQLITE_OK;
1.128353 +
1.128354 +  int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
1.128355 +  int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
1.128356 +  int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
1.128357 +  int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
1.128358 +  int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
1.128359 +  int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
1.128360 +
1.128361 +  Fts3SegReader **apSegment = pCsr->apSegment;
1.128362 +  int nSegment = pCsr->nSegment;
1.128363 +  Fts3SegFilter *pFilter = pCsr->pFilter;
1.128364 +  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
1.128365 +    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
1.128366 +  );
1.128367 +
1.128368 +  if( pCsr->nSegment==0 ) return SQLITE_OK;
1.128369 +
1.128370 +  do {
1.128371 +    int nMerge;
1.128372 +    int i;
1.128373 +  
1.128374 +    /* Advance the first pCsr->nAdvance entries in the apSegment[] array
1.128375 +    ** forward. Then sort the list in order of current term again.  
1.128376 +    */
1.128377 +    for(i=0; i<pCsr->nAdvance; i++){
1.128378 +      Fts3SegReader *pSeg = apSegment[i];
1.128379 +      if( pSeg->bLookup ){
1.128380 +        fts3SegReaderSetEof(pSeg);
1.128381 +      }else{
1.128382 +        rc = fts3SegReaderNext(p, pSeg, 0);
1.128383 +      }
1.128384 +      if( rc!=SQLITE_OK ) return rc;
1.128385 +    }
1.128386 +    fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
1.128387 +    pCsr->nAdvance = 0;
1.128388 +
1.128389 +    /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
1.128390 +    assert( rc==SQLITE_OK );
1.128391 +    if( apSegment[0]->aNode==0 ) break;
1.128392 +
1.128393 +    pCsr->nTerm = apSegment[0]->nTerm;
1.128394 +    pCsr->zTerm = apSegment[0]->zTerm;
1.128395 +
1.128396 +    /* If this is a prefix-search, and if the term that apSegment[0] points
1.128397 +    ** to does not share a suffix with pFilter->zTerm/nTerm, then all 
1.128398 +    ** required callbacks have been made. In this case exit early.
1.128399 +    **
1.128400 +    ** Similarly, if this is a search for an exact match, and the first term
1.128401 +    ** of segment apSegment[0] is not a match, exit early.
1.128402 +    */
1.128403 +    if( pFilter->zTerm && !isScan ){
1.128404 +      if( pCsr->nTerm<pFilter->nTerm 
1.128405 +       || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
1.128406 +       || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) 
1.128407 +      ){
1.128408 +        break;
1.128409 +      }
1.128410 +    }
1.128411 +
1.128412 +    nMerge = 1;
1.128413 +    while( nMerge<nSegment 
1.128414 +        && apSegment[nMerge]->aNode
1.128415 +        && apSegment[nMerge]->nTerm==pCsr->nTerm 
1.128416 +        && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
1.128417 +    ){
1.128418 +      nMerge++;
1.128419 +    }
1.128420 +
1.128421 +    assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
1.128422 +    if( nMerge==1 
1.128423 +     && !isIgnoreEmpty 
1.128424 +     && !isFirst 
1.128425 +     && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
1.128426 +    ){
1.128427 +      pCsr->nDoclist = apSegment[0]->nDoclist;
1.128428 +      if( fts3SegReaderIsPending(apSegment[0]) ){
1.128429 +        rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
1.128430 +        pCsr->aDoclist = pCsr->aBuffer;
1.128431 +      }else{
1.128432 +        pCsr->aDoclist = apSegment[0]->aDoclist;
1.128433 +      }
1.128434 +      if( rc==SQLITE_OK ) rc = SQLITE_ROW;
1.128435 +    }else{
1.128436 +      int nDoclist = 0;           /* Size of doclist */
1.128437 +      sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
1.128438 +
1.128439 +      /* The current term of the first nMerge entries in the array
1.128440 +      ** of Fts3SegReader objects is the same. The doclists must be merged
1.128441 +      ** and a single term returned with the merged doclist.
1.128442 +      */
1.128443 +      for(i=0; i<nMerge; i++){
1.128444 +        fts3SegReaderFirstDocid(p, apSegment[i]);
1.128445 +      }
1.128446 +      fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
1.128447 +      while( apSegment[0]->pOffsetList ){
1.128448 +        int j;                    /* Number of segments that share a docid */
1.128449 +        char *pList;
1.128450 +        int nList;
1.128451 +        int nByte;
1.128452 +        sqlite3_int64 iDocid = apSegment[0]->iDocid;
1.128453 +        fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
1.128454 +        j = 1;
1.128455 +        while( j<nMerge
1.128456 +            && apSegment[j]->pOffsetList
1.128457 +            && apSegment[j]->iDocid==iDocid
1.128458 +        ){
1.128459 +          fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
1.128460 +          j++;
1.128461 +        }
1.128462 +
1.128463 +        if( isColFilter ){
1.128464 +          fts3ColumnFilter(pFilter->iCol, &pList, &nList);
1.128465 +        }
1.128466 +
1.128467 +        if( !isIgnoreEmpty || nList>0 ){
1.128468 +
1.128469 +          /* Calculate the 'docid' delta value to write into the merged 
1.128470 +          ** doclist. */
1.128471 +          sqlite3_int64 iDelta;
1.128472 +          if( p->bDescIdx && nDoclist>0 ){
1.128473 +            iDelta = iPrev - iDocid;
1.128474 +          }else{
1.128475 +            iDelta = iDocid - iPrev;
1.128476 +          }
1.128477 +          assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
1.128478 +          assert( nDoclist>0 || iDelta==iDocid );
1.128479 +
1.128480 +          nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
1.128481 +          if( nDoclist+nByte>pCsr->nBuffer ){
1.128482 +            char *aNew;
1.128483 +            pCsr->nBuffer = (nDoclist+nByte)*2;
1.128484 +            aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
1.128485 +            if( !aNew ){
1.128486 +              return SQLITE_NOMEM;
1.128487 +            }
1.128488 +            pCsr->aBuffer = aNew;
1.128489 +          }
1.128490 +
1.128491 +          if( isFirst ){
1.128492 +            char *a = &pCsr->aBuffer[nDoclist];
1.128493 +            int nWrite;
1.128494 +           
1.128495 +            nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
1.128496 +            if( nWrite ){
1.128497 +              iPrev = iDocid;
1.128498 +              nDoclist += nWrite;
1.128499 +            }
1.128500 +          }else{
1.128501 +            nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
1.128502 +            iPrev = iDocid;
1.128503 +            if( isRequirePos ){
1.128504 +              memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
1.128505 +              nDoclist += nList;
1.128506 +              pCsr->aBuffer[nDoclist++] = '\0';
1.128507 +            }
1.128508 +          }
1.128509 +        }
1.128510 +
1.128511 +        fts3SegReaderSort(apSegment, nMerge, j, xCmp);
1.128512 +      }
1.128513 +      if( nDoclist>0 ){
1.128514 +        pCsr->aDoclist = pCsr->aBuffer;
1.128515 +        pCsr->nDoclist = nDoclist;
1.128516 +        rc = SQLITE_ROW;
1.128517 +      }
1.128518 +    }
1.128519 +    pCsr->nAdvance = nMerge;
1.128520 +  }while( rc==SQLITE_OK );
1.128521 +
1.128522 +  return rc;
1.128523 +}
1.128524 +
1.128525 +
1.128526 +SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
1.128527 +  Fts3MultiSegReader *pCsr       /* Cursor object */
1.128528 +){
1.128529 +  if( pCsr ){
1.128530 +    int i;
1.128531 +    for(i=0; i<pCsr->nSegment; i++){
1.128532 +      sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
1.128533 +    }
1.128534 +    sqlite3_free(pCsr->apSegment);
1.128535 +    sqlite3_free(pCsr->aBuffer);
1.128536 +
1.128537 +    pCsr->nSegment = 0;
1.128538 +    pCsr->apSegment = 0;
1.128539 +    pCsr->aBuffer = 0;
1.128540 +  }
1.128541 +}
1.128542 +
1.128543 +/*
1.128544 +** Merge all level iLevel segments in the database into a single 
1.128545 +** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
1.128546 +** single segment with a level equal to the numerically largest level 
1.128547 +** currently present in the database.
1.128548 +**
1.128549 +** If this function is called with iLevel<0, but there is only one
1.128550 +** segment in the database, SQLITE_DONE is returned immediately. 
1.128551 +** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, 
1.128552 +** an SQLite error code is returned.
1.128553 +*/
1.128554 +static int fts3SegmentMerge(
1.128555 +  Fts3Table *p, 
1.128556 +  int iLangid,                    /* Language id to merge */
1.128557 +  int iIndex,                     /* Index in p->aIndex[] to merge */
1.128558 +  int iLevel                      /* Level to merge */
1.128559 +){
1.128560 +  int rc;                         /* Return code */
1.128561 +  int iIdx = 0;                   /* Index of new segment */
1.128562 +  sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
1.128563 +  SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
1.128564 +  Fts3SegFilter filter;           /* Segment term filter condition */
1.128565 +  Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
1.128566 +  int bIgnoreEmpty = 0;           /* True to ignore empty segments */
1.128567 +
1.128568 +  assert( iLevel==FTS3_SEGCURSOR_ALL
1.128569 +       || iLevel==FTS3_SEGCURSOR_PENDING
1.128570 +       || iLevel>=0
1.128571 +  );
1.128572 +  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
1.128573 +  assert( iIndex>=0 && iIndex<p->nIndex );
1.128574 +
1.128575 +  rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
1.128576 +  if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
1.128577 +
1.128578 +  if( iLevel==FTS3_SEGCURSOR_ALL ){
1.128579 +    /* This call is to merge all segments in the database to a single
1.128580 +    ** segment. The level of the new segment is equal to the numerically
1.128581 +    ** greatest segment level currently present in the database for this
1.128582 +    ** index. The idx of the new segment is always 0.  */
1.128583 +    if( csr.nSegment==1 ){
1.128584 +      rc = SQLITE_DONE;
1.128585 +      goto finished;
1.128586 +    }
1.128587 +    rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
1.128588 +    bIgnoreEmpty = 1;
1.128589 +
1.128590 +  }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
1.128591 +    iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
1.128592 +    rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
1.128593 +  }else{
1.128594 +    /* This call is to merge all segments at level iLevel. find the next
1.128595 +    ** available segment index at level iLevel+1. The call to
1.128596 +    ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to 
1.128597 +    ** a single iLevel+2 segment if necessary.  */
1.128598 +    rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
1.128599 +    iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
1.128600 +  }
1.128601 +  if( rc!=SQLITE_OK ) goto finished;
1.128602 +  assert( csr.nSegment>0 );
1.128603 +  assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
1.128604 +  assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
1.128605 +
1.128606 +  memset(&filter, 0, sizeof(Fts3SegFilter));
1.128607 +  filter.flags = FTS3_SEGMENT_REQUIRE_POS;
1.128608 +  filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
1.128609 +
1.128610 +  rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
1.128611 +  while( SQLITE_OK==rc ){
1.128612 +    rc = sqlite3Fts3SegReaderStep(p, &csr);
1.128613 +    if( rc!=SQLITE_ROW ) break;
1.128614 +    rc = fts3SegWriterAdd(p, &pWriter, 1, 
1.128615 +        csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
1.128616 +  }
1.128617 +  if( rc!=SQLITE_OK ) goto finished;
1.128618 +  assert( pWriter );
1.128619 +
1.128620 +  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
1.128621 +    rc = fts3DeleteSegdir(
1.128622 +        p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
1.128623 +    );
1.128624 +    if( rc!=SQLITE_OK ) goto finished;
1.128625 +  }
1.128626 +  rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
1.128627 +
1.128628 + finished:
1.128629 +  fts3SegWriterFree(pWriter);
1.128630 +  sqlite3Fts3SegReaderFinish(&csr);
1.128631 +  return rc;
1.128632 +}
1.128633 +
1.128634 +
1.128635 +/* 
1.128636 +** Flush the contents of pendingTerms to level 0 segments.
1.128637 +*/
1.128638 +SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
1.128639 +  int rc = SQLITE_OK;
1.128640 +  int i;
1.128641 +        
1.128642 +  for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
1.128643 +    rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
1.128644 +    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
1.128645 +  }
1.128646 +  sqlite3Fts3PendingTermsClear(p);
1.128647 +
1.128648 +  /* Determine the auto-incr-merge setting if unknown.  If enabled,
1.128649 +  ** estimate the number of leaf blocks of content to be written
1.128650 +  */
1.128651 +  if( rc==SQLITE_OK && p->bHasStat
1.128652 +   && p->bAutoincrmerge==0xff && p->nLeafAdd>0
1.128653 +  ){
1.128654 +    sqlite3_stmt *pStmt = 0;
1.128655 +    rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
1.128656 +    if( rc==SQLITE_OK ){
1.128657 +      sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
1.128658 +      rc = sqlite3_step(pStmt);
1.128659 +      p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
1.128660 +      rc = sqlite3_reset(pStmt);
1.128661 +    }
1.128662 +  }
1.128663 +  return rc;
1.128664 +}
1.128665 +
1.128666 +/*
1.128667 +** Encode N integers as varints into a blob.
1.128668 +*/
1.128669 +static void fts3EncodeIntArray(
1.128670 +  int N,             /* The number of integers to encode */
1.128671 +  u32 *a,            /* The integer values */
1.128672 +  char *zBuf,        /* Write the BLOB here */
1.128673 +  int *pNBuf         /* Write number of bytes if zBuf[] used here */
1.128674 +){
1.128675 +  int i, j;
1.128676 +  for(i=j=0; i<N; i++){
1.128677 +    j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
1.128678 +  }
1.128679 +  *pNBuf = j;
1.128680 +}
1.128681 +
1.128682 +/*
1.128683 +** Decode a blob of varints into N integers
1.128684 +*/
1.128685 +static void fts3DecodeIntArray(
1.128686 +  int N,             /* The number of integers to decode */
1.128687 +  u32 *a,            /* Write the integer values */
1.128688 +  const char *zBuf,  /* The BLOB containing the varints */
1.128689 +  int nBuf           /* size of the BLOB */
1.128690 +){
1.128691 +  int i, j;
1.128692 +  UNUSED_PARAMETER(nBuf);
1.128693 +  for(i=j=0; i<N; i++){
1.128694 +    sqlite3_int64 x;
1.128695 +    j += sqlite3Fts3GetVarint(&zBuf[j], &x);
1.128696 +    assert(j<=nBuf);
1.128697 +    a[i] = (u32)(x & 0xffffffff);
1.128698 +  }
1.128699 +}
1.128700 +
1.128701 +/*
1.128702 +** Insert the sizes (in tokens) for each column of the document
1.128703 +** with docid equal to p->iPrevDocid.  The sizes are encoded as
1.128704 +** a blob of varints.
1.128705 +*/
1.128706 +static void fts3InsertDocsize(
1.128707 +  int *pRC,                       /* Result code */
1.128708 +  Fts3Table *p,                   /* Table into which to insert */
1.128709 +  u32 *aSz                        /* Sizes of each column, in tokens */
1.128710 +){
1.128711 +  char *pBlob;             /* The BLOB encoding of the document size */
1.128712 +  int nBlob;               /* Number of bytes in the BLOB */
1.128713 +  sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
1.128714 +  int rc;                  /* Result code from subfunctions */
1.128715 +
1.128716 +  if( *pRC ) return;
1.128717 +  pBlob = sqlite3_malloc( 10*p->nColumn );
1.128718 +  if( pBlob==0 ){
1.128719 +    *pRC = SQLITE_NOMEM;
1.128720 +    return;
1.128721 +  }
1.128722 +  fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
1.128723 +  rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
1.128724 +  if( rc ){
1.128725 +    sqlite3_free(pBlob);
1.128726 +    *pRC = rc;
1.128727 +    return;
1.128728 +  }
1.128729 +  sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
1.128730 +  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
1.128731 +  sqlite3_step(pStmt);
1.128732 +  *pRC = sqlite3_reset(pStmt);
1.128733 +}
1.128734 +
1.128735 +/*
1.128736 +** Record 0 of the %_stat table contains a blob consisting of N varints,
1.128737 +** where N is the number of user defined columns in the fts3 table plus
1.128738 +** two. If nCol is the number of user defined columns, then values of the 
1.128739 +** varints are set as follows:
1.128740 +**
1.128741 +**   Varint 0:       Total number of rows in the table.
1.128742 +**
1.128743 +**   Varint 1..nCol: For each column, the total number of tokens stored in
1.128744 +**                   the column for all rows of the table.
1.128745 +**
1.128746 +**   Varint 1+nCol:  The total size, in bytes, of all text values in all
1.128747 +**                   columns of all rows of the table.
1.128748 +**
1.128749 +*/
1.128750 +static void fts3UpdateDocTotals(
1.128751 +  int *pRC,                       /* The result code */
1.128752 +  Fts3Table *p,                   /* Table being updated */
1.128753 +  u32 *aSzIns,                    /* Size increases */
1.128754 +  u32 *aSzDel,                    /* Size decreases */
1.128755 +  int nChng                       /* Change in the number of documents */
1.128756 +){
1.128757 +  char *pBlob;             /* Storage for BLOB written into %_stat */
1.128758 +  int nBlob;               /* Size of BLOB written into %_stat */
1.128759 +  u32 *a;                  /* Array of integers that becomes the BLOB */
1.128760 +  sqlite3_stmt *pStmt;     /* Statement for reading and writing */
1.128761 +  int i;                   /* Loop counter */
1.128762 +  int rc;                  /* Result code from subfunctions */
1.128763 +
1.128764 +  const int nStat = p->nColumn+2;
1.128765 +
1.128766 +  if( *pRC ) return;
1.128767 +  a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
1.128768 +  if( a==0 ){
1.128769 +    *pRC = SQLITE_NOMEM;
1.128770 +    return;
1.128771 +  }
1.128772 +  pBlob = (char*)&a[nStat];
1.128773 +  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
1.128774 +  if( rc ){
1.128775 +    sqlite3_free(a);
1.128776 +    *pRC = rc;
1.128777 +    return;
1.128778 +  }
1.128779 +  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
1.128780 +  if( sqlite3_step(pStmt)==SQLITE_ROW ){
1.128781 +    fts3DecodeIntArray(nStat, a,
1.128782 +         sqlite3_column_blob(pStmt, 0),
1.128783 +         sqlite3_column_bytes(pStmt, 0));
1.128784 +  }else{
1.128785 +    memset(a, 0, sizeof(u32)*(nStat) );
1.128786 +  }
1.128787 +  rc = sqlite3_reset(pStmt);
1.128788 +  if( rc!=SQLITE_OK ){
1.128789 +    sqlite3_free(a);
1.128790 +    *pRC = rc;
1.128791 +    return;
1.128792 +  }
1.128793 +  if( nChng<0 && a[0]<(u32)(-nChng) ){
1.128794 +    a[0] = 0;
1.128795 +  }else{
1.128796 +    a[0] += nChng;
1.128797 +  }
1.128798 +  for(i=0; i<p->nColumn+1; i++){
1.128799 +    u32 x = a[i+1];
1.128800 +    if( x+aSzIns[i] < aSzDel[i] ){
1.128801 +      x = 0;
1.128802 +    }else{
1.128803 +      x = x + aSzIns[i] - aSzDel[i];
1.128804 +    }
1.128805 +    a[i+1] = x;
1.128806 +  }
1.128807 +  fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
1.128808 +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
1.128809 +  if( rc ){
1.128810 +    sqlite3_free(a);
1.128811 +    *pRC = rc;
1.128812 +    return;
1.128813 +  }
1.128814 +  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
1.128815 +  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
1.128816 +  sqlite3_step(pStmt);
1.128817 +  *pRC = sqlite3_reset(pStmt);
1.128818 +  sqlite3_free(a);
1.128819 +}
1.128820 +
1.128821 +/*
1.128822 +** Merge the entire database so that there is one segment for each 
1.128823 +** iIndex/iLangid combination.
1.128824 +*/
1.128825 +static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
1.128826 +  int bSeenDone = 0;
1.128827 +  int rc;
1.128828 +  sqlite3_stmt *pAllLangid = 0;
1.128829 +
1.128830 +  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
1.128831 +  if( rc==SQLITE_OK ){
1.128832 +    int rc2;
1.128833 +    sqlite3_bind_int(pAllLangid, 1, p->nIndex);
1.128834 +    while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
1.128835 +      int i;
1.128836 +      int iLangid = sqlite3_column_int(pAllLangid, 0);
1.128837 +      for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
1.128838 +        rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
1.128839 +        if( rc==SQLITE_DONE ){
1.128840 +          bSeenDone = 1;
1.128841 +          rc = SQLITE_OK;
1.128842 +        }
1.128843 +      }
1.128844 +    }
1.128845 +    rc2 = sqlite3_reset(pAllLangid);
1.128846 +    if( rc==SQLITE_OK ) rc = rc2;
1.128847 +  }
1.128848 +
1.128849 +  sqlite3Fts3SegmentsClose(p);
1.128850 +  sqlite3Fts3PendingTermsClear(p);
1.128851 +
1.128852 +  return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
1.128853 +}
1.128854 +
1.128855 +/*
1.128856 +** This function is called when the user executes the following statement:
1.128857 +**
1.128858 +**     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
1.128859 +**
1.128860 +** The entire FTS index is discarded and rebuilt. If the table is one 
1.128861 +** created using the content=xxx option, then the new index is based on
1.128862 +** the current contents of the xxx table. Otherwise, it is rebuilt based
1.128863 +** on the contents of the %_content table.
1.128864 +*/
1.128865 +static int fts3DoRebuild(Fts3Table *p){
1.128866 +  int rc;                         /* Return Code */
1.128867 +
1.128868 +  rc = fts3DeleteAll(p, 0);
1.128869 +  if( rc==SQLITE_OK ){
1.128870 +    u32 *aSz = 0;
1.128871 +    u32 *aSzIns = 0;
1.128872 +    u32 *aSzDel = 0;
1.128873 +    sqlite3_stmt *pStmt = 0;
1.128874 +    int nEntry = 0;
1.128875 +
1.128876 +    /* Compose and prepare an SQL statement to loop through the content table */
1.128877 +    char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
1.128878 +    if( !zSql ){
1.128879 +      rc = SQLITE_NOMEM;
1.128880 +    }else{
1.128881 +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
1.128882 +      sqlite3_free(zSql);
1.128883 +    }
1.128884 +
1.128885 +    if( rc==SQLITE_OK ){
1.128886 +      int nByte = sizeof(u32) * (p->nColumn+1)*3;
1.128887 +      aSz = (u32 *)sqlite3_malloc(nByte);
1.128888 +      if( aSz==0 ){
1.128889 +        rc = SQLITE_NOMEM;
1.128890 +      }else{
1.128891 +        memset(aSz, 0, nByte);
1.128892 +        aSzIns = &aSz[p->nColumn+1];
1.128893 +        aSzDel = &aSzIns[p->nColumn+1];
1.128894 +      }
1.128895 +    }
1.128896 +
1.128897 +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
1.128898 +      int iCol;
1.128899 +      int iLangid = langidFromSelect(p, pStmt);
1.128900 +      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
1.128901 +      memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
1.128902 +      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
1.128903 +        const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
1.128904 +        rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
1.128905 +        aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
1.128906 +      }
1.128907 +      if( p->bHasDocsize ){
1.128908 +        fts3InsertDocsize(&rc, p, aSz);
1.128909 +      }
1.128910 +      if( rc!=SQLITE_OK ){
1.128911 +        sqlite3_finalize(pStmt);
1.128912 +        pStmt = 0;
1.128913 +      }else{
1.128914 +        nEntry++;
1.128915 +        for(iCol=0; iCol<=p->nColumn; iCol++){
1.128916 +          aSzIns[iCol] += aSz[iCol];
1.128917 +        }
1.128918 +      }
1.128919 +    }
1.128920 +    if( p->bFts4 ){
1.128921 +      fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
1.128922 +    }
1.128923 +    sqlite3_free(aSz);
1.128924 +
1.128925 +    if( pStmt ){
1.128926 +      int rc2 = sqlite3_finalize(pStmt);
1.128927 +      if( rc==SQLITE_OK ){
1.128928 +        rc = rc2;
1.128929 +      }
1.128930 +    }
1.128931 +  }
1.128932 +
1.128933 +  return rc;
1.128934 +}
1.128935 +
1.128936 +
1.128937 +/*
1.128938 +** This function opens a cursor used to read the input data for an 
1.128939 +** incremental merge operation. Specifically, it opens a cursor to scan
1.128940 +** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute 
1.128941 +** level iAbsLevel.
1.128942 +*/
1.128943 +static int fts3IncrmergeCsr(
1.128944 +  Fts3Table *p,                   /* FTS3 table handle */
1.128945 +  sqlite3_int64 iAbsLevel,        /* Absolute level to open */
1.128946 +  int nSeg,                       /* Number of segments to merge */
1.128947 +  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
1.128948 +){
1.128949 +  int rc;                         /* Return Code */
1.128950 +  sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */  
1.128951 +  int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
1.128952 +
1.128953 +  /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
1.128954 +  memset(pCsr, 0, sizeof(*pCsr));
1.128955 +  nByte = sizeof(Fts3SegReader *) * nSeg;
1.128956 +  pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
1.128957 +
1.128958 +  if( pCsr->apSegment==0 ){
1.128959 +    rc = SQLITE_NOMEM;
1.128960 +  }else{
1.128961 +    memset(pCsr->apSegment, 0, nByte);
1.128962 +    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
1.128963 +  }
1.128964 +  if( rc==SQLITE_OK ){
1.128965 +    int i;
1.128966 +    int rc2;
1.128967 +    sqlite3_bind_int64(pStmt, 1, iAbsLevel);
1.128968 +    assert( pCsr->nSegment==0 );
1.128969 +    for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
1.128970 +      rc = sqlite3Fts3SegReaderNew(i, 0,
1.128971 +          sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
1.128972 +          sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
1.128973 +          sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
1.128974 +          sqlite3_column_blob(pStmt, 4),         /* segdir.root */
1.128975 +          sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
1.128976 +          &pCsr->apSegment[i]
1.128977 +      );
1.128978 +      pCsr->nSegment++;
1.128979 +    }
1.128980 +    rc2 = sqlite3_reset(pStmt);
1.128981 +    if( rc==SQLITE_OK ) rc = rc2;
1.128982 +  }
1.128983 +
1.128984 +  return rc;
1.128985 +}
1.128986 +
1.128987 +typedef struct IncrmergeWriter IncrmergeWriter;
1.128988 +typedef struct NodeWriter NodeWriter;
1.128989 +typedef struct Blob Blob;
1.128990 +typedef struct NodeReader NodeReader;
1.128991 +
1.128992 +/*
1.128993 +** An instance of the following structure is used as a dynamic buffer
1.128994 +** to build up nodes or other blobs of data in.
1.128995 +**
1.128996 +** The function blobGrowBuffer() is used to extend the allocation.
1.128997 +*/
1.128998 +struct Blob {
1.128999 +  char *a;                        /* Pointer to allocation */
1.129000 +  int n;                          /* Number of valid bytes of data in a[] */
1.129001 +  int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
1.129002 +};
1.129003 +
1.129004 +/*
1.129005 +** This structure is used to build up buffers containing segment b-tree 
1.129006 +** nodes (blocks).
1.129007 +*/
1.129008 +struct NodeWriter {
1.129009 +  sqlite3_int64 iBlock;           /* Current block id */
1.129010 +  Blob key;                       /* Last key written to the current block */
1.129011 +  Blob block;                     /* Current block image */
1.129012 +};
1.129013 +
1.129014 +/*
1.129015 +** An object of this type contains the state required to create or append
1.129016 +** to an appendable b-tree segment.
1.129017 +*/
1.129018 +struct IncrmergeWriter {
1.129019 +  int nLeafEst;                   /* Space allocated for leaf blocks */
1.129020 +  int nWork;                      /* Number of leaf pages flushed */
1.129021 +  sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
1.129022 +  int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
1.129023 +  sqlite3_int64 iStart;           /* Block number of first allocated block */
1.129024 +  sqlite3_int64 iEnd;             /* Block number of last allocated block */
1.129025 +  NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
1.129026 +};
1.129027 +
1.129028 +/*
1.129029 +** An object of the following type is used to read data from a single
1.129030 +** FTS segment node. See the following functions:
1.129031 +**
1.129032 +**     nodeReaderInit()
1.129033 +**     nodeReaderNext()
1.129034 +**     nodeReaderRelease()
1.129035 +*/
1.129036 +struct NodeReader {
1.129037 +  const char *aNode;
1.129038 +  int nNode;
1.129039 +  int iOff;                       /* Current offset within aNode[] */
1.129040 +
1.129041 +  /* Output variables. Containing the current node entry. */
1.129042 +  sqlite3_int64 iChild;           /* Pointer to child node */
1.129043 +  Blob term;                      /* Current term */
1.129044 +  const char *aDoclist;           /* Pointer to doclist */
1.129045 +  int nDoclist;                   /* Size of doclist in bytes */
1.129046 +};
1.129047 +
1.129048 +/*
1.129049 +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
1.129050 +** Otherwise, if the allocation at pBlob->a is not already at least nMin
1.129051 +** bytes in size, extend (realloc) it to be so.
1.129052 +**
1.129053 +** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
1.129054 +** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
1.129055 +** to reflect the new size of the pBlob->a[] buffer.
1.129056 +*/
1.129057 +static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
1.129058 +  if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
1.129059 +    int nAlloc = nMin;
1.129060 +    char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
1.129061 +    if( a ){
1.129062 +      pBlob->nAlloc = nAlloc;
1.129063 +      pBlob->a = a;
1.129064 +    }else{
1.129065 +      *pRc = SQLITE_NOMEM;
1.129066 +    }
1.129067 +  }
1.129068 +}
1.129069 +
1.129070 +/*
1.129071 +** Attempt to advance the node-reader object passed as the first argument to
1.129072 +** the next entry on the node. 
1.129073 +**
1.129074 +** Return an error code if an error occurs (SQLITE_NOMEM is possible). 
1.129075 +** Otherwise return SQLITE_OK. If there is no next entry on the node
1.129076 +** (e.g. because the current entry is the last) set NodeReader->aNode to
1.129077 +** NULL to indicate EOF. Otherwise, populate the NodeReader structure output 
1.129078 +** variables for the new entry.
1.129079 +*/
1.129080 +static int nodeReaderNext(NodeReader *p){
1.129081 +  int bFirst = (p->term.n==0);    /* True for first term on the node */
1.129082 +  int nPrefix = 0;                /* Bytes to copy from previous term */
1.129083 +  int nSuffix = 0;                /* Bytes to append to the prefix */
1.129084 +  int rc = SQLITE_OK;             /* Return code */
1.129085 +
1.129086 +  assert( p->aNode );
1.129087 +  if( p->iChild && bFirst==0 ) p->iChild++;
1.129088 +  if( p->iOff>=p->nNode ){
1.129089 +    /* EOF */
1.129090 +    p->aNode = 0;
1.129091 +  }else{
1.129092 +    if( bFirst==0 ){
1.129093 +      p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
1.129094 +    }
1.129095 +    p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
1.129096 +
1.129097 +    blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
1.129098 +    if( rc==SQLITE_OK ){
1.129099 +      memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
1.129100 +      p->term.n = nPrefix+nSuffix;
1.129101 +      p->iOff += nSuffix;
1.129102 +      if( p->iChild==0 ){
1.129103 +        p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
1.129104 +        p->aDoclist = &p->aNode[p->iOff];
1.129105 +        p->iOff += p->nDoclist;
1.129106 +      }
1.129107 +    }
1.129108 +  }
1.129109 +
1.129110 +  assert( p->iOff<=p->nNode );
1.129111 +
1.129112 +  return rc;
1.129113 +}
1.129114 +
1.129115 +/*
1.129116 +** Release all dynamic resources held by node-reader object *p.
1.129117 +*/
1.129118 +static void nodeReaderRelease(NodeReader *p){
1.129119 +  sqlite3_free(p->term.a);
1.129120 +}
1.129121 +
1.129122 +/*
1.129123 +** Initialize a node-reader object to read the node in buffer aNode/nNode.
1.129124 +**
1.129125 +** If successful, SQLITE_OK is returned and the NodeReader object set to 
1.129126 +** point to the first entry on the node (if any). Otherwise, an SQLite
1.129127 +** error code is returned.
1.129128 +*/
1.129129 +static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
1.129130 +  memset(p, 0, sizeof(NodeReader));
1.129131 +  p->aNode = aNode;
1.129132 +  p->nNode = nNode;
1.129133 +
1.129134 +  /* Figure out if this is a leaf or an internal node. */
1.129135 +  if( p->aNode[0] ){
1.129136 +    /* An internal node. */
1.129137 +    p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
1.129138 +  }else{
1.129139 +    p->iOff = 1;
1.129140 +  }
1.129141 +
1.129142 +  return nodeReaderNext(p);
1.129143 +}
1.129144 +
1.129145 +/*
1.129146 +** This function is called while writing an FTS segment each time a leaf o
1.129147 +** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
1.129148 +** to be greater than the largest key on the node just written, but smaller
1.129149 +** than or equal to the first key that will be written to the next leaf
1.129150 +** node.
1.129151 +**
1.129152 +** The block id of the leaf node just written to disk may be found in
1.129153 +** (pWriter->aNodeWriter[0].iBlock) when this function is called.
1.129154 +*/
1.129155 +static int fts3IncrmergePush(
1.129156 +  Fts3Table *p,                   /* Fts3 table handle */
1.129157 +  IncrmergeWriter *pWriter,       /* Writer object */
1.129158 +  const char *zTerm,              /* Term to write to internal node */
1.129159 +  int nTerm                       /* Bytes at zTerm */
1.129160 +){
1.129161 +  sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
1.129162 +  int iLayer;
1.129163 +
1.129164 +  assert( nTerm>0 );
1.129165 +  for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
1.129166 +    sqlite3_int64 iNextPtr = 0;
1.129167 +    NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
1.129168 +    int rc = SQLITE_OK;
1.129169 +    int nPrefix;
1.129170 +    int nSuffix;
1.129171 +    int nSpace;
1.129172 +
1.129173 +    /* Figure out how much space the key will consume if it is written to
1.129174 +    ** the current node of layer iLayer. Due to the prefix compression, 
1.129175 +    ** the space required changes depending on which node the key is to
1.129176 +    ** be added to.  */
1.129177 +    nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
1.129178 +    nSuffix = nTerm - nPrefix;
1.129179 +    nSpace  = sqlite3Fts3VarintLen(nPrefix);
1.129180 +    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
1.129181 +
1.129182 +    if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){ 
1.129183 +      /* If the current node of layer iLayer contains zero keys, or if adding
1.129184 +      ** the key to it will not cause it to grow to larger than nNodeSize 
1.129185 +      ** bytes in size, write the key here.  */
1.129186 +
1.129187 +      Blob *pBlk = &pNode->block;
1.129188 +      if( pBlk->n==0 ){
1.129189 +        blobGrowBuffer(pBlk, p->nNodeSize, &rc);
1.129190 +        if( rc==SQLITE_OK ){
1.129191 +          pBlk->a[0] = (char)iLayer;
1.129192 +          pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
1.129193 +        }
1.129194 +      }
1.129195 +      blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
1.129196 +      blobGrowBuffer(&pNode->key, nTerm, &rc);
1.129197 +
1.129198 +      if( rc==SQLITE_OK ){
1.129199 +        if( pNode->key.n ){
1.129200 +          pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
1.129201 +        }
1.129202 +        pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
1.129203 +        memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
1.129204 +        pBlk->n += nSuffix;
1.129205 +
1.129206 +        memcpy(pNode->key.a, zTerm, nTerm);
1.129207 +        pNode->key.n = nTerm;
1.129208 +      }
1.129209 +    }else{
1.129210 +      /* Otherwise, flush the current node of layer iLayer to disk.
1.129211 +      ** Then allocate a new, empty sibling node. The key will be written
1.129212 +      ** into the parent of this node. */
1.129213 +      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
1.129214 +
1.129215 +      assert( pNode->block.nAlloc>=p->nNodeSize );
1.129216 +      pNode->block.a[0] = (char)iLayer;
1.129217 +      pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
1.129218 +
1.129219 +      iNextPtr = pNode->iBlock;
1.129220 +      pNode->iBlock++;
1.129221 +      pNode->key.n = 0;
1.129222 +    }
1.129223 +
1.129224 +    if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
1.129225 +    iPtr = iNextPtr;
1.129226 +  }
1.129227 +
1.129228 +  assert( 0 );
1.129229 +  return 0;
1.129230 +}
1.129231 +
1.129232 +/*
1.129233 +** Append a term and (optionally) doclist to the FTS segment node currently
1.129234 +** stored in blob *pNode. The node need not contain any terms, but the
1.129235 +** header must be written before this function is called.
1.129236 +**
1.129237 +** A node header is a single 0x00 byte for a leaf node, or a height varint
1.129238 +** followed by the left-hand-child varint for an internal node.
1.129239 +**
1.129240 +** The term to be appended is passed via arguments zTerm/nTerm. For a 
1.129241 +** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
1.129242 +** node, both aDoclist and nDoclist must be passed 0.
1.129243 +**
1.129244 +** If the size of the value in blob pPrev is zero, then this is the first
1.129245 +** term written to the node. Otherwise, pPrev contains a copy of the 
1.129246 +** previous term. Before this function returns, it is updated to contain a
1.129247 +** copy of zTerm/nTerm.
1.129248 +**
1.129249 +** It is assumed that the buffer associated with pNode is already large
1.129250 +** enough to accommodate the new entry. The buffer associated with pPrev
1.129251 +** is extended by this function if requrired.
1.129252 +**
1.129253 +** If an error (i.e. OOM condition) occurs, an SQLite error code is
1.129254 +** returned. Otherwise, SQLITE_OK.
1.129255 +*/
1.129256 +static int fts3AppendToNode(
1.129257 +  Blob *pNode,                    /* Current node image to append to */
1.129258 +  Blob *pPrev,                    /* Buffer containing previous term written */
1.129259 +  const char *zTerm,              /* New term to write */
1.129260 +  int nTerm,                      /* Size of zTerm in bytes */
1.129261 +  const char *aDoclist,           /* Doclist (or NULL) to write */
1.129262 +  int nDoclist                    /* Size of aDoclist in bytes */ 
1.129263 +){
1.129264 +  int rc = SQLITE_OK;             /* Return code */
1.129265 +  int bFirst = (pPrev->n==0);     /* True if this is the first term written */
1.129266 +  int nPrefix;                    /* Size of term prefix in bytes */
1.129267 +  int nSuffix;                    /* Size of term suffix in bytes */
1.129268 +
1.129269 +  /* Node must have already been started. There must be a doclist for a
1.129270 +  ** leaf node, and there must not be a doclist for an internal node.  */
1.129271 +  assert( pNode->n>0 );
1.129272 +  assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
1.129273 +
1.129274 +  blobGrowBuffer(pPrev, nTerm, &rc);
1.129275 +  if( rc!=SQLITE_OK ) return rc;
1.129276 +
1.129277 +  nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
1.129278 +  nSuffix = nTerm - nPrefix;
1.129279 +  memcpy(pPrev->a, zTerm, nTerm);
1.129280 +  pPrev->n = nTerm;
1.129281 +
1.129282 +  if( bFirst==0 ){
1.129283 +    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
1.129284 +  }
1.129285 +  pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
1.129286 +  memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
1.129287 +  pNode->n += nSuffix;
1.129288 +
1.129289 +  if( aDoclist ){
1.129290 +    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
1.129291 +    memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
1.129292 +    pNode->n += nDoclist;
1.129293 +  }
1.129294 +
1.129295 +  assert( pNode->n<=pNode->nAlloc );
1.129296 +
1.129297 +  return SQLITE_OK;
1.129298 +}
1.129299 +
1.129300 +/*
1.129301 +** Append the current term and doclist pointed to by cursor pCsr to the
1.129302 +** appendable b-tree segment opened for writing by pWriter.
1.129303 +**
1.129304 +** Return SQLITE_OK if successful, or an SQLite error code otherwise.
1.129305 +*/
1.129306 +static int fts3IncrmergeAppend(
1.129307 +  Fts3Table *p,                   /* Fts3 table handle */
1.129308 +  IncrmergeWriter *pWriter,       /* Writer object */
1.129309 +  Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
1.129310 +){
1.129311 +  const char *zTerm = pCsr->zTerm;
1.129312 +  int nTerm = pCsr->nTerm;
1.129313 +  const char *aDoclist = pCsr->aDoclist;
1.129314 +  int nDoclist = pCsr->nDoclist;
1.129315 +  int rc = SQLITE_OK;           /* Return code */
1.129316 +  int nSpace;                   /* Total space in bytes required on leaf */
1.129317 +  int nPrefix;                  /* Size of prefix shared with previous term */
1.129318 +  int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
1.129319 +  NodeWriter *pLeaf;            /* Object used to write leaf nodes */
1.129320 +
1.129321 +  pLeaf = &pWriter->aNodeWriter[0];
1.129322 +  nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
1.129323 +  nSuffix = nTerm - nPrefix;
1.129324 +
1.129325 +  nSpace  = sqlite3Fts3VarintLen(nPrefix);
1.129326 +  nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
1.129327 +  nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
1.129328 +
1.129329 +  /* If the current block is not empty, and if adding this term/doclist
1.129330 +  ** to the current block would make it larger than Fts3Table.nNodeSize
1.129331 +  ** bytes, write this block out to the database. */
1.129332 +  if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
1.129333 +    rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
1.129334 +    pWriter->nWork++;
1.129335 +
1.129336 +    /* Add the current term to the parent node. The term added to the 
1.129337 +    ** parent must:
1.129338 +    **
1.129339 +    **   a) be greater than the largest term on the leaf node just written
1.129340 +    **      to the database (still available in pLeaf->key), and
1.129341 +    **
1.129342 +    **   b) be less than or equal to the term about to be added to the new
1.129343 +    **      leaf node (zTerm/nTerm).
1.129344 +    **
1.129345 +    ** In other words, it must be the prefix of zTerm 1 byte longer than
1.129346 +    ** the common prefix (if any) of zTerm and pWriter->zTerm.
1.129347 +    */
1.129348 +    if( rc==SQLITE_OK ){
1.129349 +      rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
1.129350 +    }
1.129351 +
1.129352 +    /* Advance to the next output block */
1.129353 +    pLeaf->iBlock++;
1.129354 +    pLeaf->key.n = 0;
1.129355 +    pLeaf->block.n = 0;
1.129356 +
1.129357 +    nSuffix = nTerm;
1.129358 +    nSpace  = 1;
1.129359 +    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
1.129360 +    nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
1.129361 +  }
1.129362 +
1.129363 +  blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
1.129364 +
1.129365 +  if( rc==SQLITE_OK ){
1.129366 +    if( pLeaf->block.n==0 ){
1.129367 +      pLeaf->block.n = 1;
1.129368 +      pLeaf->block.a[0] = '\0';
1.129369 +    }
1.129370 +    rc = fts3AppendToNode(
1.129371 +        &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
1.129372 +    );
1.129373 +  }
1.129374 +
1.129375 +  return rc;
1.129376 +}
1.129377 +
1.129378 +/*
1.129379 +** This function is called to release all dynamic resources held by the
1.129380 +** merge-writer object pWriter, and if no error has occurred, to flush
1.129381 +** all outstanding node buffers held by pWriter to disk.
1.129382 +**
1.129383 +** If *pRc is not SQLITE_OK when this function is called, then no attempt
1.129384 +** is made to write any data to disk. Instead, this function serves only
1.129385 +** to release outstanding resources.
1.129386 +**
1.129387 +** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
1.129388 +** flushing buffers to disk, *pRc is set to an SQLite error code before
1.129389 +** returning.
1.129390 +*/
1.129391 +static void fts3IncrmergeRelease(
1.129392 +  Fts3Table *p,                   /* FTS3 table handle */
1.129393 +  IncrmergeWriter *pWriter,       /* Merge-writer object */
1.129394 +  int *pRc                        /* IN/OUT: Error code */
1.129395 +){
1.129396 +  int i;                          /* Used to iterate through non-root layers */
1.129397 +  int iRoot;                      /* Index of root in pWriter->aNodeWriter */
1.129398 +  NodeWriter *pRoot;              /* NodeWriter for root node */
1.129399 +  int rc = *pRc;                  /* Error code */
1.129400 +
1.129401 +  /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment 
1.129402 +  ** root node. If the segment fits entirely on a single leaf node, iRoot
1.129403 +  ** will be set to 0. If the root node is the parent of the leaves, iRoot
1.129404 +  ** will be 1. And so on.  */
1.129405 +  for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
1.129406 +    NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
1.129407 +    if( pNode->block.n>0 ) break;
1.129408 +    assert( *pRc || pNode->block.nAlloc==0 );
1.129409 +    assert( *pRc || pNode->key.nAlloc==0 );
1.129410 +    sqlite3_free(pNode->block.a);
1.129411 +    sqlite3_free(pNode->key.a);
1.129412 +  }
1.129413 +
1.129414 +  /* Empty output segment. This is a no-op. */
1.129415 +  if( iRoot<0 ) return;
1.129416 +
1.129417 +  /* The entire output segment fits on a single node. Normally, this means
1.129418 +  ** the node would be stored as a blob in the "root" column of the %_segdir
1.129419 +  ** table. However, this is not permitted in this case. The problem is that 
1.129420 +  ** space has already been reserved in the %_segments table, and so the 
1.129421 +  ** start_block and end_block fields of the %_segdir table must be populated. 
1.129422 +  ** And, by design or by accident, released versions of FTS cannot handle 
1.129423 +  ** segments that fit entirely on the root node with start_block!=0.
1.129424 +  **
1.129425 +  ** Instead, create a synthetic root node that contains nothing but a 
1.129426 +  ** pointer to the single content node. So that the segment consists of a
1.129427 +  ** single leaf and a single interior (root) node.
1.129428 +  **
1.129429 +  ** Todo: Better might be to defer allocating space in the %_segments 
1.129430 +  ** table until we are sure it is needed.
1.129431 +  */
1.129432 +  if( iRoot==0 ){
1.129433 +    Blob *pBlock = &pWriter->aNodeWriter[1].block;
1.129434 +    blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
1.129435 +    if( rc==SQLITE_OK ){
1.129436 +      pBlock->a[0] = 0x01;
1.129437 +      pBlock->n = 1 + sqlite3Fts3PutVarint(
1.129438 +          &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
1.129439 +      );
1.129440 +    }
1.129441 +    iRoot = 1;
1.129442 +  }
1.129443 +  pRoot = &pWriter->aNodeWriter[iRoot];
1.129444 +
1.129445 +  /* Flush all currently outstanding nodes to disk. */
1.129446 +  for(i=0; i<iRoot; i++){
1.129447 +    NodeWriter *pNode = &pWriter->aNodeWriter[i];
1.129448 +    if( pNode->block.n>0 && rc==SQLITE_OK ){
1.129449 +      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
1.129450 +    }
1.129451 +    sqlite3_free(pNode->block.a);
1.129452 +    sqlite3_free(pNode->key.a);
1.129453 +  }
1.129454 +
1.129455 +  /* Write the %_segdir record. */
1.129456 +  if( rc==SQLITE_OK ){
1.129457 +    rc = fts3WriteSegdir(p, 
1.129458 +        pWriter->iAbsLevel+1,               /* level */
1.129459 +        pWriter->iIdx,                      /* idx */
1.129460 +        pWriter->iStart,                    /* start_block */
1.129461 +        pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
1.129462 +        pWriter->iEnd,                      /* end_block */
1.129463 +        pRoot->block.a, pRoot->block.n      /* root */
1.129464 +    );
1.129465 +  }
1.129466 +  sqlite3_free(pRoot->block.a);
1.129467 +  sqlite3_free(pRoot->key.a);
1.129468 +
1.129469 +  *pRc = rc;
1.129470 +}
1.129471 +
1.129472 +/*
1.129473 +** Compare the term in buffer zLhs (size in bytes nLhs) with that in
1.129474 +** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
1.129475 +** the other, it is considered to be smaller than the other.
1.129476 +**
1.129477 +** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
1.129478 +** if it is greater.
1.129479 +*/
1.129480 +static int fts3TermCmp(
1.129481 +  const char *zLhs, int nLhs,     /* LHS of comparison */
1.129482 +  const char *zRhs, int nRhs      /* RHS of comparison */
1.129483 +){
1.129484 +  int nCmp = MIN(nLhs, nRhs);
1.129485 +  int res;
1.129486 +
1.129487 +  res = memcmp(zLhs, zRhs, nCmp);
1.129488 +  if( res==0 ) res = nLhs - nRhs;
1.129489 +
1.129490 +  return res;
1.129491 +}
1.129492 +
1.129493 +
1.129494 +/*
1.129495 +** Query to see if the entry in the %_segments table with blockid iEnd is 
1.129496 +** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
1.129497 +** returning. Otherwise, set *pbRes to 0. 
1.129498 +**
1.129499 +** Or, if an error occurs while querying the database, return an SQLite 
1.129500 +** error code. The final value of *pbRes is undefined in this case.
1.129501 +**
1.129502 +** This is used to test if a segment is an "appendable" segment. If it
1.129503 +** is, then a NULL entry has been inserted into the %_segments table
1.129504 +** with blockid %_segdir.end_block.
1.129505 +*/
1.129506 +static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
1.129507 +  int bRes = 0;                   /* Result to set *pbRes to */
1.129508 +  sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
1.129509 +  int rc;                         /* Return code */
1.129510 +
1.129511 +  rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
1.129512 +  if( rc==SQLITE_OK ){
1.129513 +    sqlite3_bind_int64(pCheck, 1, iEnd);
1.129514 +    if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
1.129515 +    rc = sqlite3_reset(pCheck);
1.129516 +  }
1.129517 +  
1.129518 +  *pbRes = bRes;
1.129519 +  return rc;
1.129520 +}
1.129521 +
1.129522 +/*
1.129523 +** This function is called when initializing an incremental-merge operation.
1.129524 +** It checks if the existing segment with index value iIdx at absolute level 
1.129525 +** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
1.129526 +** merge-writer object *pWriter is initialized to write to it.
1.129527 +**
1.129528 +** An existing segment can be appended to by an incremental merge if:
1.129529 +**
1.129530 +**   * It was initially created as an appendable segment (with all required
1.129531 +**     space pre-allocated), and
1.129532 +**
1.129533 +**   * The first key read from the input (arguments zKey and nKey) is 
1.129534 +**     greater than the largest key currently stored in the potential
1.129535 +**     output segment.
1.129536 +*/
1.129537 +static int fts3IncrmergeLoad(
1.129538 +  Fts3Table *p,                   /* Fts3 table handle */
1.129539 +  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
1.129540 +  int iIdx,                       /* Index of candidate output segment */
1.129541 +  const char *zKey,               /* First key to write */
1.129542 +  int nKey,                       /* Number of bytes in nKey */
1.129543 +  IncrmergeWriter *pWriter        /* Populate this object */
1.129544 +){
1.129545 +  int rc;                         /* Return code */
1.129546 +  sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
1.129547 +
1.129548 +  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
1.129549 +  if( rc==SQLITE_OK ){
1.129550 +    sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
1.129551 +    sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
1.129552 +    sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
1.129553 +    const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
1.129554 +    int nRoot = 0;                /* Size of aRoot[] in bytes */
1.129555 +    int rc2;                      /* Return code from sqlite3_reset() */
1.129556 +    int bAppendable = 0;          /* Set to true if segment is appendable */
1.129557 +
1.129558 +    /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
1.129559 +    sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
1.129560 +    sqlite3_bind_int(pSelect, 2, iIdx);
1.129561 +    if( sqlite3_step(pSelect)==SQLITE_ROW ){
1.129562 +      iStart = sqlite3_column_int64(pSelect, 1);
1.129563 +      iLeafEnd = sqlite3_column_int64(pSelect, 2);
1.129564 +      iEnd = sqlite3_column_int64(pSelect, 3);
1.129565 +      nRoot = sqlite3_column_bytes(pSelect, 4);
1.129566 +      aRoot = sqlite3_column_blob(pSelect, 4);
1.129567 +    }else{
1.129568 +      return sqlite3_reset(pSelect);
1.129569 +    }
1.129570 +
1.129571 +    /* Check for the zero-length marker in the %_segments table */
1.129572 +    rc = fts3IsAppendable(p, iEnd, &bAppendable);
1.129573 +
1.129574 +    /* Check that zKey/nKey is larger than the largest key the candidate */
1.129575 +    if( rc==SQLITE_OK && bAppendable ){
1.129576 +      char *aLeaf = 0;
1.129577 +      int nLeaf = 0;
1.129578 +
1.129579 +      rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
1.129580 +      if( rc==SQLITE_OK ){
1.129581 +        NodeReader reader;
1.129582 +        for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
1.129583 +            rc==SQLITE_OK && reader.aNode;
1.129584 +            rc = nodeReaderNext(&reader)
1.129585 +        ){
1.129586 +          assert( reader.aNode );
1.129587 +        }
1.129588 +        if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
1.129589 +          bAppendable = 0;
1.129590 +        }
1.129591 +        nodeReaderRelease(&reader);
1.129592 +      }
1.129593 +      sqlite3_free(aLeaf);
1.129594 +    }
1.129595 +
1.129596 +    if( rc==SQLITE_OK && bAppendable ){
1.129597 +      /* It is possible to append to this segment. Set up the IncrmergeWriter
1.129598 +      ** object to do so.  */
1.129599 +      int i;
1.129600 +      int nHeight = (int)aRoot[0];
1.129601 +      NodeWriter *pNode;
1.129602 +
1.129603 +      pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
1.129604 +      pWriter->iStart = iStart;
1.129605 +      pWriter->iEnd = iEnd;
1.129606 +      pWriter->iAbsLevel = iAbsLevel;
1.129607 +      pWriter->iIdx = iIdx;
1.129608 +
1.129609 +      for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
1.129610 +        pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
1.129611 +      }
1.129612 +
1.129613 +      pNode = &pWriter->aNodeWriter[nHeight];
1.129614 +      pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
1.129615 +      blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
1.129616 +      if( rc==SQLITE_OK ){
1.129617 +        memcpy(pNode->block.a, aRoot, nRoot);
1.129618 +        pNode->block.n = nRoot;
1.129619 +      }
1.129620 +
1.129621 +      for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
1.129622 +        NodeReader reader;
1.129623 +        pNode = &pWriter->aNodeWriter[i];
1.129624 +
1.129625 +        rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
1.129626 +        while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
1.129627 +        blobGrowBuffer(&pNode->key, reader.term.n, &rc);
1.129628 +        if( rc==SQLITE_OK ){
1.129629 +          memcpy(pNode->key.a, reader.term.a, reader.term.n);
1.129630 +          pNode->key.n = reader.term.n;
1.129631 +          if( i>0 ){
1.129632 +            char *aBlock = 0;
1.129633 +            int nBlock = 0;
1.129634 +            pNode = &pWriter->aNodeWriter[i-1];
1.129635 +            pNode->iBlock = reader.iChild;
1.129636 +            rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
1.129637 +            blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
1.129638 +            if( rc==SQLITE_OK ){
1.129639 +              memcpy(pNode->block.a, aBlock, nBlock);
1.129640 +              pNode->block.n = nBlock;
1.129641 +            }
1.129642 +            sqlite3_free(aBlock);
1.129643 +          }
1.129644 +        }
1.129645 +        nodeReaderRelease(&reader);
1.129646 +      }
1.129647 +    }
1.129648 +
1.129649 +    rc2 = sqlite3_reset(pSelect);
1.129650 +    if( rc==SQLITE_OK ) rc = rc2;
1.129651 +  }
1.129652 +
1.129653 +  return rc;
1.129654 +}
1.129655 +
1.129656 +/*
1.129657 +** Determine the largest segment index value that exists within absolute
1.129658 +** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
1.129659 +** one before returning SQLITE_OK. Or, if there are no segments at all 
1.129660 +** within level iAbsLevel, set *piIdx to zero.
1.129661 +**
1.129662 +** If an error occurs, return an SQLite error code. The final value of
1.129663 +** *piIdx is undefined in this case.
1.129664 +*/
1.129665 +static int fts3IncrmergeOutputIdx( 
1.129666 +  Fts3Table *p,                   /* FTS Table handle */
1.129667 +  sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
1.129668 +  int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
1.129669 +){
1.129670 +  int rc;
1.129671 +  sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
1.129672 +
1.129673 +  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
1.129674 +  if( rc==SQLITE_OK ){
1.129675 +    sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
1.129676 +    sqlite3_step(pOutputIdx);
1.129677 +    *piIdx = sqlite3_column_int(pOutputIdx, 0);
1.129678 +    rc = sqlite3_reset(pOutputIdx);
1.129679 +  }
1.129680 +
1.129681 +  return rc;
1.129682 +}
1.129683 +
1.129684 +/* 
1.129685 +** Allocate an appendable output segment on absolute level iAbsLevel+1
1.129686 +** with idx value iIdx.
1.129687 +**
1.129688 +** In the %_segdir table, a segment is defined by the values in three
1.129689 +** columns:
1.129690 +**
1.129691 +**     start_block
1.129692 +**     leaves_end_block
1.129693 +**     end_block
1.129694 +**
1.129695 +** When an appendable segment is allocated, it is estimated that the
1.129696 +** maximum number of leaf blocks that may be required is the sum of the
1.129697 +** number of leaf blocks consumed by the input segments, plus the number
1.129698 +** of input segments, multiplied by two. This value is stored in stack 
1.129699 +** variable nLeafEst.
1.129700 +**
1.129701 +** A total of 16*nLeafEst blocks are allocated when an appendable segment
1.129702 +** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
1.129703 +** array of leaf nodes starts at the first block allocated. The array
1.129704 +** of interior nodes that are parents of the leaf nodes start at block
1.129705 +** (start_block + (1 + end_block - start_block) / 16). And so on.
1.129706 +**
1.129707 +** In the actual code below, the value "16" is replaced with the 
1.129708 +** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
1.129709 +*/
1.129710 +static int fts3IncrmergeWriter( 
1.129711 +  Fts3Table *p,                   /* Fts3 table handle */
1.129712 +  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
1.129713 +  int iIdx,                       /* Index of new output segment */
1.129714 +  Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
1.129715 +  IncrmergeWriter *pWriter        /* Populate this object */
1.129716 +){
1.129717 +  int rc;                         /* Return Code */
1.129718 +  int i;                          /* Iterator variable */
1.129719 +  int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
1.129720 +  sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
1.129721 +  sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
1.129722 +
1.129723 +  /* Calculate nLeafEst. */
1.129724 +  rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
1.129725 +  if( rc==SQLITE_OK ){
1.129726 +    sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
1.129727 +    sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
1.129728 +    if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
1.129729 +      nLeafEst = sqlite3_column_int(pLeafEst, 0);
1.129730 +    }
1.129731 +    rc = sqlite3_reset(pLeafEst);
1.129732 +  }
1.129733 +  if( rc!=SQLITE_OK ) return rc;
1.129734 +
1.129735 +  /* Calculate the first block to use in the output segment */
1.129736 +  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
1.129737 +  if( rc==SQLITE_OK ){
1.129738 +    if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
1.129739 +      pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
1.129740 +      pWriter->iEnd = pWriter->iStart - 1;
1.129741 +      pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
1.129742 +    }
1.129743 +    rc = sqlite3_reset(pFirstBlock);
1.129744 +  }
1.129745 +  if( rc!=SQLITE_OK ) return rc;
1.129746 +
1.129747 +  /* Insert the marker in the %_segments table to make sure nobody tries
1.129748 +  ** to steal the space just allocated. This is also used to identify 
1.129749 +  ** appendable segments.  */
1.129750 +  rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
1.129751 +  if( rc!=SQLITE_OK ) return rc;
1.129752 +
1.129753 +  pWriter->iAbsLevel = iAbsLevel;
1.129754 +  pWriter->nLeafEst = nLeafEst;
1.129755 +  pWriter->iIdx = iIdx;
1.129756 +
1.129757 +  /* Set up the array of NodeWriter objects */
1.129758 +  for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
1.129759 +    pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
1.129760 +  }
1.129761 +  return SQLITE_OK;
1.129762 +}
1.129763 +
1.129764 +/*
1.129765 +** Remove an entry from the %_segdir table. This involves running the 
1.129766 +** following two statements:
1.129767 +**
1.129768 +**   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
1.129769 +**   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
1.129770 +**
1.129771 +** The DELETE statement removes the specific %_segdir level. The UPDATE 
1.129772 +** statement ensures that the remaining segments have contiguously allocated
1.129773 +** idx values.
1.129774 +*/
1.129775 +static int fts3RemoveSegdirEntry(
1.129776 +  Fts3Table *p,                   /* FTS3 table handle */
1.129777 +  sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
1.129778 +  int iIdx                        /* Index of %_segdir entry to delete */
1.129779 +){
1.129780 +  int rc;                         /* Return code */
1.129781 +  sqlite3_stmt *pDelete = 0;      /* DELETE statement */
1.129782 +
1.129783 +  rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
1.129784 +  if( rc==SQLITE_OK ){
1.129785 +    sqlite3_bind_int64(pDelete, 1, iAbsLevel);
1.129786 +    sqlite3_bind_int(pDelete, 2, iIdx);
1.129787 +    sqlite3_step(pDelete);
1.129788 +    rc = sqlite3_reset(pDelete);
1.129789 +  }
1.129790 +
1.129791 +  return rc;
1.129792 +}
1.129793 +
1.129794 +/*
1.129795 +** One or more segments have just been removed from absolute level iAbsLevel.
1.129796 +** Update the 'idx' values of the remaining segments in the level so that
1.129797 +** the idx values are a contiguous sequence starting from 0.
1.129798 +*/
1.129799 +static int fts3RepackSegdirLevel(
1.129800 +  Fts3Table *p,                   /* FTS3 table handle */
1.129801 +  sqlite3_int64 iAbsLevel         /* Absolute level to repack */
1.129802 +){
1.129803 +  int rc;                         /* Return code */
1.129804 +  int *aIdx = 0;                  /* Array of remaining idx values */
1.129805 +  int nIdx = 0;                   /* Valid entries in aIdx[] */
1.129806 +  int nAlloc = 0;                 /* Allocated size of aIdx[] */
1.129807 +  int i;                          /* Iterator variable */
1.129808 +  sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
1.129809 +  sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
1.129810 +
1.129811 +  rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
1.129812 +  if( rc==SQLITE_OK ){
1.129813 +    int rc2;
1.129814 +    sqlite3_bind_int64(pSelect, 1, iAbsLevel);
1.129815 +    while( SQLITE_ROW==sqlite3_step(pSelect) ){
1.129816 +      if( nIdx>=nAlloc ){
1.129817 +        int *aNew;
1.129818 +        nAlloc += 16;
1.129819 +        aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
1.129820 +        if( !aNew ){
1.129821 +          rc = SQLITE_NOMEM;
1.129822 +          break;
1.129823 +        }
1.129824 +        aIdx = aNew;
1.129825 +      }
1.129826 +      aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
1.129827 +    }
1.129828 +    rc2 = sqlite3_reset(pSelect);
1.129829 +    if( rc==SQLITE_OK ) rc = rc2;
1.129830 +  }
1.129831 +
1.129832 +  if( rc==SQLITE_OK ){
1.129833 +    rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
1.129834 +  }
1.129835 +  if( rc==SQLITE_OK ){
1.129836 +    sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
1.129837 +  }
1.129838 +
1.129839 +  assert( p->bIgnoreSavepoint==0 );
1.129840 +  p->bIgnoreSavepoint = 1;
1.129841 +  for(i=0; rc==SQLITE_OK && i<nIdx; i++){
1.129842 +    if( aIdx[i]!=i ){
1.129843 +      sqlite3_bind_int(pUpdate, 3, aIdx[i]);
1.129844 +      sqlite3_bind_int(pUpdate, 1, i);
1.129845 +      sqlite3_step(pUpdate);
1.129846 +      rc = sqlite3_reset(pUpdate);
1.129847 +    }
1.129848 +  }
1.129849 +  p->bIgnoreSavepoint = 0;
1.129850 +
1.129851 +  sqlite3_free(aIdx);
1.129852 +  return rc;
1.129853 +}
1.129854 +
1.129855 +static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
1.129856 +  pNode->a[0] = (char)iHeight;
1.129857 +  if( iChild ){
1.129858 +    assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
1.129859 +    pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
1.129860 +  }else{
1.129861 +    assert( pNode->nAlloc>=1 );
1.129862 +    pNode->n = 1;
1.129863 +  }
1.129864 +}
1.129865 +
1.129866 +/*
1.129867 +** The first two arguments are a pointer to and the size of a segment b-tree
1.129868 +** node. The node may be a leaf or an internal node.
1.129869 +**
1.129870 +** This function creates a new node image in blob object *pNew by copying
1.129871 +** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
1.129872 +** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
1.129873 +*/
1.129874 +static int fts3TruncateNode(
1.129875 +  const char *aNode,              /* Current node image */
1.129876 +  int nNode,                      /* Size of aNode in bytes */
1.129877 +  Blob *pNew,                     /* OUT: Write new node image here */
1.129878 +  const char *zTerm,              /* Omit all terms smaller than this */
1.129879 +  int nTerm,                      /* Size of zTerm in bytes */
1.129880 +  sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
1.129881 +){
1.129882 +  NodeReader reader;              /* Reader object */
1.129883 +  Blob prev = {0, 0, 0};          /* Previous term written to new node */
1.129884 +  int rc = SQLITE_OK;             /* Return code */
1.129885 +  int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
1.129886 +
1.129887 +  /* Allocate required output space */
1.129888 +  blobGrowBuffer(pNew, nNode, &rc);
1.129889 +  if( rc!=SQLITE_OK ) return rc;
1.129890 +  pNew->n = 0;
1.129891 +
1.129892 +  /* Populate new node buffer */
1.129893 +  for(rc = nodeReaderInit(&reader, aNode, nNode); 
1.129894 +      rc==SQLITE_OK && reader.aNode; 
1.129895 +      rc = nodeReaderNext(&reader)
1.129896 +  ){
1.129897 +    if( pNew->n==0 ){
1.129898 +      int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
1.129899 +      if( res<0 || (bLeaf==0 && res==0) ) continue;
1.129900 +      fts3StartNode(pNew, (int)aNode[0], reader.iChild);
1.129901 +      *piBlock = reader.iChild;
1.129902 +    }
1.129903 +    rc = fts3AppendToNode(
1.129904 +        pNew, &prev, reader.term.a, reader.term.n,
1.129905 +        reader.aDoclist, reader.nDoclist
1.129906 +    );
1.129907 +    if( rc!=SQLITE_OK ) break;
1.129908 +  }
1.129909 +  if( pNew->n==0 ){
1.129910 +    fts3StartNode(pNew, (int)aNode[0], reader.iChild);
1.129911 +    *piBlock = reader.iChild;
1.129912 +  }
1.129913 +  assert( pNew->n<=pNew->nAlloc );
1.129914 +
1.129915 +  nodeReaderRelease(&reader);
1.129916 +  sqlite3_free(prev.a);
1.129917 +  return rc;
1.129918 +}
1.129919 +
1.129920 +/*
1.129921 +** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute 
1.129922 +** level iAbsLevel. This may involve deleting entries from the %_segments
1.129923 +** table, and modifying existing entries in both the %_segments and %_segdir
1.129924 +** tables.
1.129925 +**
1.129926 +** SQLITE_OK is returned if the segment is updated successfully. Or an
1.129927 +** SQLite error code otherwise.
1.129928 +*/
1.129929 +static int fts3TruncateSegment(
1.129930 +  Fts3Table *p,                   /* FTS3 table handle */
1.129931 +  sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
1.129932 +  int iIdx,                       /* Index within level of segment to modify */
1.129933 +  const char *zTerm,              /* Remove terms smaller than this */
1.129934 +  int nTerm                      /* Number of bytes in buffer zTerm */
1.129935 +){
1.129936 +  int rc = SQLITE_OK;             /* Return code */
1.129937 +  Blob root = {0,0,0};            /* New root page image */
1.129938 +  Blob block = {0,0,0};           /* Buffer used for any other block */
1.129939 +  sqlite3_int64 iBlock = 0;       /* Block id */
1.129940 +  sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
1.129941 +  sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
1.129942 +  sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
1.129943 +
1.129944 +  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
1.129945 +  if( rc==SQLITE_OK ){
1.129946 +    int rc2;                      /* sqlite3_reset() return code */
1.129947 +    sqlite3_bind_int64(pFetch, 1, iAbsLevel);
1.129948 +    sqlite3_bind_int(pFetch, 2, iIdx);
1.129949 +    if( SQLITE_ROW==sqlite3_step(pFetch) ){
1.129950 +      const char *aRoot = sqlite3_column_blob(pFetch, 4);
1.129951 +      int nRoot = sqlite3_column_bytes(pFetch, 4);
1.129952 +      iOldStart = sqlite3_column_int64(pFetch, 1);
1.129953 +      rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
1.129954 +    }
1.129955 +    rc2 = sqlite3_reset(pFetch);
1.129956 +    if( rc==SQLITE_OK ) rc = rc2;
1.129957 +  }
1.129958 +
1.129959 +  while( rc==SQLITE_OK && iBlock ){
1.129960 +    char *aBlock = 0;
1.129961 +    int nBlock = 0;
1.129962 +    iNewStart = iBlock;
1.129963 +
1.129964 +    rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
1.129965 +    if( rc==SQLITE_OK ){
1.129966 +      rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
1.129967 +    }
1.129968 +    if( rc==SQLITE_OK ){
1.129969 +      rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
1.129970 +    }
1.129971 +    sqlite3_free(aBlock);
1.129972 +  }
1.129973 +
1.129974 +  /* Variable iNewStart now contains the first valid leaf node. */
1.129975 +  if( rc==SQLITE_OK && iNewStart ){
1.129976 +    sqlite3_stmt *pDel = 0;
1.129977 +    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
1.129978 +    if( rc==SQLITE_OK ){
1.129979 +      sqlite3_bind_int64(pDel, 1, iOldStart);
1.129980 +      sqlite3_bind_int64(pDel, 2, iNewStart-1);
1.129981 +      sqlite3_step(pDel);
1.129982 +      rc = sqlite3_reset(pDel);
1.129983 +    }
1.129984 +  }
1.129985 +
1.129986 +  if( rc==SQLITE_OK ){
1.129987 +    sqlite3_stmt *pChomp = 0;
1.129988 +    rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
1.129989 +    if( rc==SQLITE_OK ){
1.129990 +      sqlite3_bind_int64(pChomp, 1, iNewStart);
1.129991 +      sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
1.129992 +      sqlite3_bind_int64(pChomp, 3, iAbsLevel);
1.129993 +      sqlite3_bind_int(pChomp, 4, iIdx);
1.129994 +      sqlite3_step(pChomp);
1.129995 +      rc = sqlite3_reset(pChomp);
1.129996 +    }
1.129997 +  }
1.129998 +
1.129999 +  sqlite3_free(root.a);
1.130000 +  sqlite3_free(block.a);
1.130001 +  return rc;
1.130002 +}
1.130003 +
1.130004 +/*
1.130005 +** This function is called after an incrmental-merge operation has run to
1.130006 +** merge (or partially merge) two or more segments from absolute level
1.130007 +** iAbsLevel.
1.130008 +**
1.130009 +** Each input segment is either removed from the db completely (if all of
1.130010 +** its data was copied to the output segment by the incrmerge operation)
1.130011 +** or modified in place so that it no longer contains those entries that
1.130012 +** have been duplicated in the output segment.
1.130013 +*/
1.130014 +static int fts3IncrmergeChomp(
1.130015 +  Fts3Table *p,                   /* FTS table handle */
1.130016 +  sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
1.130017 +  Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
1.130018 +  int *pnRem                      /* Number of segments not deleted */
1.130019 +){
1.130020 +  int i;
1.130021 +  int nRem = 0;
1.130022 +  int rc = SQLITE_OK;
1.130023 +
1.130024 +  for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
1.130025 +    Fts3SegReader *pSeg = 0;
1.130026 +    int j;
1.130027 +
1.130028 +    /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
1.130029 +    ** somewhere in the pCsr->apSegment[] array.  */
1.130030 +    for(j=0; ALWAYS(j<pCsr->nSegment); j++){
1.130031 +      pSeg = pCsr->apSegment[j];
1.130032 +      if( pSeg->iIdx==i ) break;
1.130033 +    }
1.130034 +    assert( j<pCsr->nSegment && pSeg->iIdx==i );
1.130035 +
1.130036 +    if( pSeg->aNode==0 ){
1.130037 +      /* Seg-reader is at EOF. Remove the entire input segment. */
1.130038 +      rc = fts3DeleteSegment(p, pSeg);
1.130039 +      if( rc==SQLITE_OK ){
1.130040 +        rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
1.130041 +      }
1.130042 +      *pnRem = 0;
1.130043 +    }else{
1.130044 +      /* The incremental merge did not copy all the data from this 
1.130045 +      ** segment to the upper level. The segment is modified in place
1.130046 +      ** so that it contains no keys smaller than zTerm/nTerm. */ 
1.130047 +      const char *zTerm = pSeg->zTerm;
1.130048 +      int nTerm = pSeg->nTerm;
1.130049 +      rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
1.130050 +      nRem++;
1.130051 +    }
1.130052 +  }
1.130053 +
1.130054 +  if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
1.130055 +    rc = fts3RepackSegdirLevel(p, iAbsLevel);
1.130056 +  }
1.130057 +
1.130058 +  *pnRem = nRem;
1.130059 +  return rc;
1.130060 +}
1.130061 +
1.130062 +/*
1.130063 +** Store an incr-merge hint in the database.
1.130064 +*/
1.130065 +static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
1.130066 +  sqlite3_stmt *pReplace = 0;
1.130067 +  int rc;                         /* Return code */
1.130068 +
1.130069 +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
1.130070 +  if( rc==SQLITE_OK ){
1.130071 +    sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
1.130072 +    sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
1.130073 +    sqlite3_step(pReplace);
1.130074 +    rc = sqlite3_reset(pReplace);
1.130075 +  }
1.130076 +
1.130077 +  return rc;
1.130078 +}
1.130079 +
1.130080 +/*
1.130081 +** Load an incr-merge hint from the database. The incr-merge hint, if one 
1.130082 +** exists, is stored in the rowid==1 row of the %_stat table.
1.130083 +**
1.130084 +** If successful, populate blob *pHint with the value read from the %_stat
1.130085 +** table and return SQLITE_OK. Otherwise, if an error occurs, return an
1.130086 +** SQLite error code.
1.130087 +*/
1.130088 +static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
1.130089 +  sqlite3_stmt *pSelect = 0;
1.130090 +  int rc;
1.130091 +
1.130092 +  pHint->n = 0;
1.130093 +  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
1.130094 +  if( rc==SQLITE_OK ){
1.130095 +    int rc2;
1.130096 +    sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
1.130097 +    if( SQLITE_ROW==sqlite3_step(pSelect) ){
1.130098 +      const char *aHint = sqlite3_column_blob(pSelect, 0);
1.130099 +      int nHint = sqlite3_column_bytes(pSelect, 0);
1.130100 +      if( aHint ){
1.130101 +        blobGrowBuffer(pHint, nHint, &rc);
1.130102 +        if( rc==SQLITE_OK ){
1.130103 +          memcpy(pHint->a, aHint, nHint);
1.130104 +          pHint->n = nHint;
1.130105 +        }
1.130106 +      }
1.130107 +    }
1.130108 +    rc2 = sqlite3_reset(pSelect);
1.130109 +    if( rc==SQLITE_OK ) rc = rc2;
1.130110 +  }
1.130111 +
1.130112 +  return rc;
1.130113 +}
1.130114 +
1.130115 +/*
1.130116 +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
1.130117 +** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
1.130118 +** consists of two varints, the absolute level number of the input segments 
1.130119 +** and the number of input segments.
1.130120 +**
1.130121 +** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
1.130122 +** set *pRc to an SQLite error code before returning.
1.130123 +*/
1.130124 +static void fts3IncrmergeHintPush(
1.130125 +  Blob *pHint,                    /* Hint blob to append to */
1.130126 +  i64 iAbsLevel,                  /* First varint to store in hint */
1.130127 +  int nInput,                     /* Second varint to store in hint */
1.130128 +  int *pRc                        /* IN/OUT: Error code */
1.130129 +){
1.130130 +  blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
1.130131 +  if( *pRc==SQLITE_OK ){
1.130132 +    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
1.130133 +    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
1.130134 +  }
1.130135 +}
1.130136 +
1.130137 +/*
1.130138 +** Read the last entry (most recently pushed) from the hint blob *pHint
1.130139 +** and then remove the entry. Write the two values read to *piAbsLevel and 
1.130140 +** *pnInput before returning.
1.130141 +**
1.130142 +** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
1.130143 +** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
1.130144 +*/
1.130145 +static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
1.130146 +  const int nHint = pHint->n;
1.130147 +  int i;
1.130148 +
1.130149 +  i = pHint->n-2;
1.130150 +  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
1.130151 +  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
1.130152 +
1.130153 +  pHint->n = i;
1.130154 +  i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
1.130155 +  i += sqlite3Fts3GetVarint32(&pHint->a[i], pnInput);
1.130156 +  if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
1.130157 +
1.130158 +  return SQLITE_OK;
1.130159 +}
1.130160 +
1.130161 +
1.130162 +/*
1.130163 +** Attempt an incremental merge that writes nMerge leaf blocks.
1.130164 +**
1.130165 +** Incremental merges happen nMin segments at a time. The two
1.130166 +** segments to be merged are the nMin oldest segments (the ones with
1.130167 +** the smallest indexes) in the highest level that contains at least
1.130168 +** nMin segments. Multiple merges might occur in an attempt to write the 
1.130169 +** quota of nMerge leaf blocks.
1.130170 +*/
1.130171 +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
1.130172 +  int rc;                         /* Return code */
1.130173 +  int nRem = nMerge;              /* Number of leaf pages yet to  be written */
1.130174 +  Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
1.130175 +  Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
1.130176 +  IncrmergeWriter *pWriter;       /* Writer object */
1.130177 +  int nSeg = 0;                   /* Number of input segments */
1.130178 +  sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
1.130179 +  Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
1.130180 +  int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
1.130181 +
1.130182 +  /* Allocate space for the cursor, filter and writer objects */
1.130183 +  const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
1.130184 +  pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
1.130185 +  if( !pWriter ) return SQLITE_NOMEM;
1.130186 +  pFilter = (Fts3SegFilter *)&pWriter[1];
1.130187 +  pCsr = (Fts3MultiSegReader *)&pFilter[1];
1.130188 +
1.130189 +  rc = fts3IncrmergeHintLoad(p, &hint);
1.130190 +  while( rc==SQLITE_OK && nRem>0 ){
1.130191 +    const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
1.130192 +    sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
1.130193 +    int bUseHint = 0;             /* True if attempting to append */
1.130194 +
1.130195 +    /* Search the %_segdir table for the absolute level with the smallest
1.130196 +    ** relative level number that contains at least nMin segments, if any.
1.130197 +    ** If one is found, set iAbsLevel to the absolute level number and
1.130198 +    ** nSeg to nMin. If no level with at least nMin segments can be found, 
1.130199 +    ** set nSeg to -1.
1.130200 +    */
1.130201 +    rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
1.130202 +    sqlite3_bind_int(pFindLevel, 1, nMin);
1.130203 +    if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
1.130204 +      iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
1.130205 +      nSeg = nMin;
1.130206 +    }else{
1.130207 +      nSeg = -1;
1.130208 +    }
1.130209 +    rc = sqlite3_reset(pFindLevel);
1.130210 +
1.130211 +    /* If the hint read from the %_stat table is not empty, check if the
1.130212 +    ** last entry in it specifies a relative level smaller than or equal
1.130213 +    ** to the level identified by the block above (if any). If so, this 
1.130214 +    ** iteration of the loop will work on merging at the hinted level.
1.130215 +    */
1.130216 +    if( rc==SQLITE_OK && hint.n ){
1.130217 +      int nHint = hint.n;
1.130218 +      sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
1.130219 +      int nHintSeg = 0;                     /* Hint number of segments */
1.130220 +
1.130221 +      rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
1.130222 +      if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
1.130223 +        iAbsLevel = iHintAbsLevel;
1.130224 +        nSeg = nHintSeg;
1.130225 +        bUseHint = 1;
1.130226 +        bDirtyHint = 1;
1.130227 +      }else{
1.130228 +        /* This undoes the effect of the HintPop() above - so that no entry
1.130229 +        ** is removed from the hint blob.  */
1.130230 +        hint.n = nHint;
1.130231 +      }
1.130232 +    }
1.130233 +
1.130234 +    /* If nSeg is less that zero, then there is no level with at least
1.130235 +    ** nMin segments and no hint in the %_stat table. No work to do.
1.130236 +    ** Exit early in this case.  */
1.130237 +    if( nSeg<0 ) break;
1.130238 +
1.130239 +    /* Open a cursor to iterate through the contents of the oldest nSeg 
1.130240 +    ** indexes of absolute level iAbsLevel. If this cursor is opened using 
1.130241 +    ** the 'hint' parameters, it is possible that there are less than nSeg
1.130242 +    ** segments available in level iAbsLevel. In this case, no work is
1.130243 +    ** done on iAbsLevel - fall through to the next iteration of the loop 
1.130244 +    ** to start work on some other level.  */
1.130245 +    memset(pWriter, 0, nAlloc);
1.130246 +    pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
1.130247 +    if( rc==SQLITE_OK ){
1.130248 +      rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
1.130249 +    }
1.130250 +    if( SQLITE_OK==rc && pCsr->nSegment==nSeg
1.130251 +     && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
1.130252 +     && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
1.130253 +    ){
1.130254 +      int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
1.130255 +      rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
1.130256 +      if( rc==SQLITE_OK ){
1.130257 +        if( bUseHint && iIdx>0 ){
1.130258 +          const char *zKey = pCsr->zTerm;
1.130259 +          int nKey = pCsr->nTerm;
1.130260 +          rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
1.130261 +        }else{
1.130262 +          rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
1.130263 +        }
1.130264 +      }
1.130265 +
1.130266 +      if( rc==SQLITE_OK && pWriter->nLeafEst ){
1.130267 +        fts3LogMerge(nSeg, iAbsLevel);
1.130268 +        do {
1.130269 +          rc = fts3IncrmergeAppend(p, pWriter, pCsr);
1.130270 +          if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
1.130271 +          if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
1.130272 +        }while( rc==SQLITE_ROW );
1.130273 +
1.130274 +        /* Update or delete the input segments */
1.130275 +        if( rc==SQLITE_OK ){
1.130276 +          nRem -= (1 + pWriter->nWork);
1.130277 +          rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
1.130278 +          if( nSeg!=0 ){
1.130279 +            bDirtyHint = 1;
1.130280 +            fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
1.130281 +          }
1.130282 +        }
1.130283 +      }
1.130284 +
1.130285 +      fts3IncrmergeRelease(p, pWriter, &rc);
1.130286 +    }
1.130287 +
1.130288 +    sqlite3Fts3SegReaderFinish(pCsr);
1.130289 +  }
1.130290 +
1.130291 +  /* Write the hint values into the %_stat table for the next incr-merger */
1.130292 +  if( bDirtyHint && rc==SQLITE_OK ){
1.130293 +    rc = fts3IncrmergeHintStore(p, &hint);
1.130294 +  }
1.130295 +
1.130296 +  sqlite3_free(pWriter);
1.130297 +  sqlite3_free(hint.a);
1.130298 +  return rc;
1.130299 +}
1.130300 +
1.130301 +/*
1.130302 +** Convert the text beginning at *pz into an integer and return
1.130303 +** its value.  Advance *pz to point to the first character past
1.130304 +** the integer.
1.130305 +*/
1.130306 +static int fts3Getint(const char **pz){
1.130307 +  const char *z = *pz;
1.130308 +  int i = 0;
1.130309 +  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
1.130310 +  *pz = z;
1.130311 +  return i;
1.130312 +}
1.130313 +
1.130314 +/*
1.130315 +** Process statements of the form:
1.130316 +**
1.130317 +**    INSERT INTO table(table) VALUES('merge=A,B');
1.130318 +**
1.130319 +** A and B are integers that decode to be the number of leaf pages
1.130320 +** written for the merge, and the minimum number of segments on a level
1.130321 +** before it will be selected for a merge, respectively.
1.130322 +*/
1.130323 +static int fts3DoIncrmerge(
1.130324 +  Fts3Table *p,                   /* FTS3 table handle */
1.130325 +  const char *zParam              /* Nul-terminated string containing "A,B" */
1.130326 +){
1.130327 +  int rc;
1.130328 +  int nMin = (FTS3_MERGE_COUNT / 2);
1.130329 +  int nMerge = 0;
1.130330 +  const char *z = zParam;
1.130331 +
1.130332 +  /* Read the first integer value */
1.130333 +  nMerge = fts3Getint(&z);
1.130334 +
1.130335 +  /* If the first integer value is followed by a ',',  read the second
1.130336 +  ** integer value. */
1.130337 +  if( z[0]==',' && z[1]!='\0' ){
1.130338 +    z++;
1.130339 +    nMin = fts3Getint(&z);
1.130340 +  }
1.130341 +
1.130342 +  if( z[0]!='\0' || nMin<2 ){
1.130343 +    rc = SQLITE_ERROR;
1.130344 +  }else{
1.130345 +    rc = SQLITE_OK;
1.130346 +    if( !p->bHasStat ){
1.130347 +      assert( p->bFts4==0 );
1.130348 +      sqlite3Fts3CreateStatTable(&rc, p);
1.130349 +    }
1.130350 +    if( rc==SQLITE_OK ){
1.130351 +      rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
1.130352 +    }
1.130353 +    sqlite3Fts3SegmentsClose(p);
1.130354 +  }
1.130355 +  return rc;
1.130356 +}
1.130357 +
1.130358 +/*
1.130359 +** Process statements of the form:
1.130360 +**
1.130361 +**    INSERT INTO table(table) VALUES('automerge=X');
1.130362 +**
1.130363 +** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
1.130364 +** turn it on.  The setting is persistent.
1.130365 +*/
1.130366 +static int fts3DoAutoincrmerge(
1.130367 +  Fts3Table *p,                   /* FTS3 table handle */
1.130368 +  const char *zParam              /* Nul-terminated string containing boolean */
1.130369 +){
1.130370 +  int rc = SQLITE_OK;
1.130371 +  sqlite3_stmt *pStmt = 0;
1.130372 +  p->bAutoincrmerge = fts3Getint(&zParam)!=0;
1.130373 +  if( !p->bHasStat ){
1.130374 +    assert( p->bFts4==0 );
1.130375 +    sqlite3Fts3CreateStatTable(&rc, p);
1.130376 +    if( rc ) return rc;
1.130377 +  }
1.130378 +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
1.130379 +  if( rc ) return rc;;
1.130380 +  sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
1.130381 +  sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
1.130382 +  sqlite3_step(pStmt);
1.130383 +  rc = sqlite3_reset(pStmt);
1.130384 +  return rc;
1.130385 +}
1.130386 +
1.130387 +/*
1.130388 +** Return a 64-bit checksum for the FTS index entry specified by the
1.130389 +** arguments to this function.
1.130390 +*/
1.130391 +static u64 fts3ChecksumEntry(
1.130392 +  const char *zTerm,              /* Pointer to buffer containing term */
1.130393 +  int nTerm,                      /* Size of zTerm in bytes */
1.130394 +  int iLangid,                    /* Language id for current row */
1.130395 +  int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
1.130396 +  i64 iDocid,                     /* Docid for current row. */
1.130397 +  int iCol,                       /* Column number */
1.130398 +  int iPos                        /* Position */
1.130399 +){
1.130400 +  int i;
1.130401 +  u64 ret = (u64)iDocid;
1.130402 +
1.130403 +  ret += (ret<<3) + iLangid;
1.130404 +  ret += (ret<<3) + iIndex;
1.130405 +  ret += (ret<<3) + iCol;
1.130406 +  ret += (ret<<3) + iPos;
1.130407 +  for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
1.130408 +
1.130409 +  return ret;
1.130410 +}
1.130411 +
1.130412 +/*
1.130413 +** Return a checksum of all entries in the FTS index that correspond to
1.130414 +** language id iLangid. The checksum is calculated by XORing the checksums
1.130415 +** of each individual entry (see fts3ChecksumEntry()) together.
1.130416 +**
1.130417 +** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
1.130418 +** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
1.130419 +** return value is undefined in this case.
1.130420 +*/
1.130421 +static u64 fts3ChecksumIndex(
1.130422 +  Fts3Table *p,                   /* FTS3 table handle */
1.130423 +  int iLangid,                    /* Language id to return cksum for */
1.130424 +  int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
1.130425 +  int *pRc                        /* OUT: Return code */
1.130426 +){
1.130427 +  Fts3SegFilter filter;
1.130428 +  Fts3MultiSegReader csr;
1.130429 +  int rc;
1.130430 +  u64 cksum = 0;
1.130431 +
1.130432 +  assert( *pRc==SQLITE_OK );
1.130433 +
1.130434 +  memset(&filter, 0, sizeof(filter));
1.130435 +  memset(&csr, 0, sizeof(csr));
1.130436 +  filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
1.130437 +  filter.flags |= FTS3_SEGMENT_SCAN;
1.130438 +
1.130439 +  rc = sqlite3Fts3SegReaderCursor(
1.130440 +      p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
1.130441 +  );
1.130442 +  if( rc==SQLITE_OK ){
1.130443 +    rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
1.130444 +  }
1.130445 +
1.130446 +  if( rc==SQLITE_OK ){
1.130447 +    while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
1.130448 +      char *pCsr = csr.aDoclist;
1.130449 +      char *pEnd = &pCsr[csr.nDoclist];
1.130450 +
1.130451 +      i64 iDocid = 0;
1.130452 +      i64 iCol = 0;
1.130453 +      i64 iPos = 0;
1.130454 +
1.130455 +      pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
1.130456 +      while( pCsr<pEnd ){
1.130457 +        i64 iVal = 0;
1.130458 +        pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
1.130459 +        if( pCsr<pEnd ){
1.130460 +          if( iVal==0 || iVal==1 ){
1.130461 +            iCol = 0;
1.130462 +            iPos = 0;
1.130463 +            if( iVal ){
1.130464 +              pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
1.130465 +            }else{
1.130466 +              pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
1.130467 +              iDocid += iVal;
1.130468 +            }
1.130469 +          }else{
1.130470 +            iPos += (iVal - 2);
1.130471 +            cksum = cksum ^ fts3ChecksumEntry(
1.130472 +                csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
1.130473 +                (int)iCol, (int)iPos
1.130474 +            );
1.130475 +          }
1.130476 +        }
1.130477 +      }
1.130478 +    }
1.130479 +  }
1.130480 +  sqlite3Fts3SegReaderFinish(&csr);
1.130481 +
1.130482 +  *pRc = rc;
1.130483 +  return cksum;
1.130484 +}
1.130485 +
1.130486 +/*
1.130487 +** Check if the contents of the FTS index match the current contents of the
1.130488 +** content table. If no error occurs and the contents do match, set *pbOk
1.130489 +** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
1.130490 +** to false before returning.
1.130491 +**
1.130492 +** If an error occurs (e.g. an OOM or IO error), return an SQLite error 
1.130493 +** code. The final value of *pbOk is undefined in this case.
1.130494 +*/
1.130495 +static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
1.130496 +  int rc = SQLITE_OK;             /* Return code */
1.130497 +  u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
1.130498 +  u64 cksum2 = 0;                 /* Checksum based on %_content contents */
1.130499 +  sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
1.130500 +
1.130501 +  /* This block calculates the checksum according to the FTS index. */
1.130502 +  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
1.130503 +  if( rc==SQLITE_OK ){
1.130504 +    int rc2;
1.130505 +    sqlite3_bind_int(pAllLangid, 1, p->nIndex);
1.130506 +    while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
1.130507 +      int iLangid = sqlite3_column_int(pAllLangid, 0);
1.130508 +      int i;
1.130509 +      for(i=0; i<p->nIndex; i++){
1.130510 +        cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
1.130511 +      }
1.130512 +    }
1.130513 +    rc2 = sqlite3_reset(pAllLangid);
1.130514 +    if( rc==SQLITE_OK ) rc = rc2;
1.130515 +  }
1.130516 +
1.130517 +  /* This block calculates the checksum according to the %_content table */
1.130518 +  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
1.130519 +  if( rc==SQLITE_OK ){
1.130520 +    sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
1.130521 +    sqlite3_stmt *pStmt = 0;
1.130522 +    char *zSql;
1.130523 +   
1.130524 +    zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
1.130525 +    if( !zSql ){
1.130526 +      rc = SQLITE_NOMEM;
1.130527 +    }else{
1.130528 +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
1.130529 +      sqlite3_free(zSql);
1.130530 +    }
1.130531 +
1.130532 +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
1.130533 +      i64 iDocid = sqlite3_column_int64(pStmt, 0);
1.130534 +      int iLang = langidFromSelect(p, pStmt);
1.130535 +      int iCol;
1.130536 +
1.130537 +      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
1.130538 +        const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
1.130539 +        int nText = sqlite3_column_bytes(pStmt, iCol+1);
1.130540 +        sqlite3_tokenizer_cursor *pT = 0;
1.130541 +
1.130542 +        rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
1.130543 +        while( rc==SQLITE_OK ){
1.130544 +          char const *zToken;       /* Buffer containing token */
1.130545 +          int nToken = 0;           /* Number of bytes in token */
1.130546 +          int iDum1 = 0, iDum2 = 0; /* Dummy variables */
1.130547 +          int iPos = 0;             /* Position of token in zText */
1.130548 +
1.130549 +          rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
1.130550 +          if( rc==SQLITE_OK ){
1.130551 +            int i;
1.130552 +            cksum2 = cksum2 ^ fts3ChecksumEntry(
1.130553 +                zToken, nToken, iLang, 0, iDocid, iCol, iPos
1.130554 +            );
1.130555 +            for(i=1; i<p->nIndex; i++){
1.130556 +              if( p->aIndex[i].nPrefix<=nToken ){
1.130557 +                cksum2 = cksum2 ^ fts3ChecksumEntry(
1.130558 +                  zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
1.130559 +                );
1.130560 +              }
1.130561 +            }
1.130562 +          }
1.130563 +        }
1.130564 +        if( pT ) pModule->xClose(pT);
1.130565 +        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
1.130566 +      }
1.130567 +    }
1.130568 +
1.130569 +    sqlite3_finalize(pStmt);
1.130570 +  }
1.130571 +
1.130572 +  *pbOk = (cksum1==cksum2);
1.130573 +  return rc;
1.130574 +}
1.130575 +
1.130576 +/*
1.130577 +** Run the integrity-check. If no error occurs and the current contents of
1.130578 +** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
1.130579 +** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
1.130580 +**
1.130581 +** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite 
1.130582 +** error code.
1.130583 +**
1.130584 +** The integrity-check works as follows. For each token and indexed token
1.130585 +** prefix in the document set, a 64-bit checksum is calculated (by code
1.130586 +** in fts3ChecksumEntry()) based on the following:
1.130587 +**
1.130588 +**     + The index number (0 for the main index, 1 for the first prefix
1.130589 +**       index etc.),
1.130590 +**     + The token (or token prefix) text itself, 
1.130591 +**     + The language-id of the row it appears in,
1.130592 +**     + The docid of the row it appears in,
1.130593 +**     + The column it appears in, and
1.130594 +**     + The tokens position within that column.
1.130595 +**
1.130596 +** The checksums for all entries in the index are XORed together to create
1.130597 +** a single checksum for the entire index.
1.130598 +**
1.130599 +** The integrity-check code calculates the same checksum in two ways:
1.130600 +**
1.130601 +**     1. By scanning the contents of the FTS index, and 
1.130602 +**     2. By scanning and tokenizing the content table.
1.130603 +**
1.130604 +** If the two checksums are identical, the integrity-check is deemed to have
1.130605 +** passed.
1.130606 +*/
1.130607 +static int fts3DoIntegrityCheck(
1.130608 +  Fts3Table *p                    /* FTS3 table handle */
1.130609 +){
1.130610 +  int rc;
1.130611 +  int bOk = 0;
1.130612 +  rc = fts3IntegrityCheck(p, &bOk);
1.130613 +  if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
1.130614 +  return rc;
1.130615 +}
1.130616 +
1.130617 +/*
1.130618 +** Handle a 'special' INSERT of the form:
1.130619 +**
1.130620 +**   "INSERT INTO tbl(tbl) VALUES(<expr>)"
1.130621 +**
1.130622 +** Argument pVal contains the result of <expr>. Currently the only 
1.130623 +** meaningful value to insert is the text 'optimize'.
1.130624 +*/
1.130625 +static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
1.130626 +  int rc;                         /* Return Code */
1.130627 +  const char *zVal = (const char *)sqlite3_value_text(pVal);
1.130628 +  int nVal = sqlite3_value_bytes(pVal);
1.130629 +
1.130630 +  if( !zVal ){
1.130631 +    return SQLITE_NOMEM;
1.130632 +  }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
1.130633 +    rc = fts3DoOptimize(p, 0);
1.130634 +  }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
1.130635 +    rc = fts3DoRebuild(p);
1.130636 +  }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
1.130637 +    rc = fts3DoIntegrityCheck(p);
1.130638 +  }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
1.130639 +    rc = fts3DoIncrmerge(p, &zVal[6]);
1.130640 +  }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
1.130641 +    rc = fts3DoAutoincrmerge(p, &zVal[10]);
1.130642 +#ifdef SQLITE_TEST
1.130643 +  }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
1.130644 +    p->nNodeSize = atoi(&zVal[9]);
1.130645 +    rc = SQLITE_OK;
1.130646 +  }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
1.130647 +    p->nMaxPendingData = atoi(&zVal[11]);
1.130648 +    rc = SQLITE_OK;
1.130649 +#endif
1.130650 +  }else{
1.130651 +    rc = SQLITE_ERROR;
1.130652 +  }
1.130653 +
1.130654 +  return rc;
1.130655 +}
1.130656 +
1.130657 +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
1.130658 +/*
1.130659 +** Delete all cached deferred doclists. Deferred doclists are cached
1.130660 +** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
1.130661 +*/
1.130662 +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
1.130663 +  Fts3DeferredToken *pDef;
1.130664 +  for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
1.130665 +    fts3PendingListDelete(pDef->pList);
1.130666 +    pDef->pList = 0;
1.130667 +  }
1.130668 +}
1.130669 +
1.130670 +/*
1.130671 +** Free all entries in the pCsr->pDeffered list. Entries are added to 
1.130672 +** this list using sqlite3Fts3DeferToken().
1.130673 +*/
1.130674 +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
1.130675 +  Fts3DeferredToken *pDef;
1.130676 +  Fts3DeferredToken *pNext;
1.130677 +  for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
1.130678 +    pNext = pDef->pNext;
1.130679 +    fts3PendingListDelete(pDef->pList);
1.130680 +    sqlite3_free(pDef);
1.130681 +  }
1.130682 +  pCsr->pDeferred = 0;
1.130683 +}
1.130684 +
1.130685 +/*
1.130686 +** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
1.130687 +** based on the row that pCsr currently points to.
1.130688 +**
1.130689 +** A deferred-doclist is like any other doclist with position information
1.130690 +** included, except that it only contains entries for a single row of the
1.130691 +** table, not for all rows.
1.130692 +*/
1.130693 +SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
1.130694 +  int rc = SQLITE_OK;             /* Return code */
1.130695 +  if( pCsr->pDeferred ){
1.130696 +    int i;                        /* Used to iterate through table columns */
1.130697 +    sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
1.130698 +    Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
1.130699 +  
1.130700 +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
1.130701 +    sqlite3_tokenizer *pT = p->pTokenizer;
1.130702 +    sqlite3_tokenizer_module const *pModule = pT->pModule;
1.130703 +   
1.130704 +    assert( pCsr->isRequireSeek==0 );
1.130705 +    iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
1.130706 +  
1.130707 +    for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
1.130708 +      const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
1.130709 +      sqlite3_tokenizer_cursor *pTC = 0;
1.130710 +  
1.130711 +      rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
1.130712 +      while( rc==SQLITE_OK ){
1.130713 +        char const *zToken;       /* Buffer containing token */
1.130714 +        int nToken = 0;           /* Number of bytes in token */
1.130715 +        int iDum1 = 0, iDum2 = 0; /* Dummy variables */
1.130716 +        int iPos = 0;             /* Position of token in zText */
1.130717 +  
1.130718 +        rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
1.130719 +        for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
1.130720 +          Fts3PhraseToken *pPT = pDef->pToken;
1.130721 +          if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
1.130722 +           && (pPT->bFirst==0 || iPos==0)
1.130723 +           && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
1.130724 +           && (0==memcmp(zToken, pPT->z, pPT->n))
1.130725 +          ){
1.130726 +            fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
1.130727 +          }
1.130728 +        }
1.130729 +      }
1.130730 +      if( pTC ) pModule->xClose(pTC);
1.130731 +      if( rc==SQLITE_DONE ) rc = SQLITE_OK;
1.130732 +    }
1.130733 +  
1.130734 +    for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
1.130735 +      if( pDef->pList ){
1.130736 +        rc = fts3PendingListAppendVarint(&pDef->pList, 0);
1.130737 +      }
1.130738 +    }
1.130739 +  }
1.130740 +
1.130741 +  return rc;
1.130742 +}
1.130743 +
1.130744 +SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
1.130745 +  Fts3DeferredToken *p, 
1.130746 +  char **ppData, 
1.130747 +  int *pnData
1.130748 +){
1.130749 +  char *pRet;
1.130750 +  int nSkip;
1.130751 +  sqlite3_int64 dummy;
1.130752 +
1.130753 +  *ppData = 0;
1.130754 +  *pnData = 0;
1.130755 +
1.130756 +  if( p->pList==0 ){
1.130757 +    return SQLITE_OK;
1.130758 +  }
1.130759 +
1.130760 +  pRet = (char *)sqlite3_malloc(p->pList->nData);
1.130761 +  if( !pRet ) return SQLITE_NOMEM;
1.130762 +
1.130763 +  nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
1.130764 +  *pnData = p->pList->nData - nSkip;
1.130765 +  *ppData = pRet;
1.130766 +  
1.130767 +  memcpy(pRet, &p->pList->aData[nSkip], *pnData);
1.130768 +  return SQLITE_OK;
1.130769 +}
1.130770 +
1.130771 +/*
1.130772 +** Add an entry for token pToken to the pCsr->pDeferred list.
1.130773 +*/
1.130774 +SQLITE_PRIVATE int sqlite3Fts3DeferToken(
1.130775 +  Fts3Cursor *pCsr,               /* Fts3 table cursor */
1.130776 +  Fts3PhraseToken *pToken,        /* Token to defer */
1.130777 +  int iCol                        /* Column that token must appear in (or -1) */
1.130778 +){
1.130779 +  Fts3DeferredToken *pDeferred;
1.130780 +  pDeferred = sqlite3_malloc(sizeof(*pDeferred));
1.130781 +  if( !pDeferred ){
1.130782 +    return SQLITE_NOMEM;
1.130783 +  }
1.130784 +  memset(pDeferred, 0, sizeof(*pDeferred));
1.130785 +  pDeferred->pToken = pToken;
1.130786 +  pDeferred->pNext = pCsr->pDeferred; 
1.130787 +  pDeferred->iCol = iCol;
1.130788 +  pCsr->pDeferred = pDeferred;
1.130789 +
1.130790 +  assert( pToken->pDeferred==0 );
1.130791 +  pToken->pDeferred = pDeferred;
1.130792 +
1.130793 +  return SQLITE_OK;
1.130794 +}
1.130795 +#endif
1.130796 +
1.130797 +/*
1.130798 +** SQLite value pRowid contains the rowid of a row that may or may not be
1.130799 +** present in the FTS3 table. If it is, delete it and adjust the contents
1.130800 +** of subsiduary data structures accordingly.
1.130801 +*/
1.130802 +static int fts3DeleteByRowid(
1.130803 +  Fts3Table *p, 
1.130804 +  sqlite3_value *pRowid, 
1.130805 +  int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
1.130806 +  u32 *aSzDel
1.130807 +){
1.130808 +  int rc = SQLITE_OK;             /* Return code */
1.130809 +  int bFound = 0;                 /* True if *pRowid really is in the table */
1.130810 +
1.130811 +  fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
1.130812 +  if( bFound && rc==SQLITE_OK ){
1.130813 +    int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
1.130814 +    rc = fts3IsEmpty(p, pRowid, &isEmpty);
1.130815 +    if( rc==SQLITE_OK ){
1.130816 +      if( isEmpty ){
1.130817 +        /* Deleting this row means the whole table is empty. In this case
1.130818 +        ** delete the contents of all three tables and throw away any
1.130819 +        ** data in the pendingTerms hash table.  */
1.130820 +        rc = fts3DeleteAll(p, 1);
1.130821 +        *pnChng = 0;
1.130822 +        memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
1.130823 +      }else{
1.130824 +        *pnChng = *pnChng - 1;
1.130825 +        if( p->zContentTbl==0 ){
1.130826 +          fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
1.130827 +        }
1.130828 +        if( p->bHasDocsize ){
1.130829 +          fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
1.130830 +        }
1.130831 +      }
1.130832 +    }
1.130833 +  }
1.130834 +
1.130835 +  return rc;
1.130836 +}
1.130837 +
1.130838 +/*
1.130839 +** This function does the work for the xUpdate method of FTS3 virtual
1.130840 +** tables. The schema of the virtual table being:
1.130841 +**
1.130842 +**     CREATE TABLE <table name>( 
1.130843 +**       <user columns>,
1.130844 +**       <table name> HIDDEN, 
1.130845 +**       docid HIDDEN, 
1.130846 +**       <langid> HIDDEN
1.130847 +**     );
1.130848 +**
1.130849 +** 
1.130850 +*/
1.130851 +SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
1.130852 +  sqlite3_vtab *pVtab,            /* FTS3 vtab object */
1.130853 +  int nArg,                       /* Size of argument array */
1.130854 +  sqlite3_value **apVal,          /* Array of arguments */
1.130855 +  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
1.130856 +){
1.130857 +  Fts3Table *p = (Fts3Table *)pVtab;
1.130858 +  int rc = SQLITE_OK;             /* Return Code */
1.130859 +  int isRemove = 0;               /* True for an UPDATE or DELETE */
1.130860 +  u32 *aSzIns = 0;                /* Sizes of inserted documents */
1.130861 +  u32 *aSzDel = 0;                /* Sizes of deleted documents */
1.130862 +  int nChng = 0;                  /* Net change in number of documents */
1.130863 +  int bInsertDone = 0;
1.130864 +
1.130865 +  assert( p->pSegments==0 );
1.130866 +  assert( 
1.130867 +      nArg==1                     /* DELETE operations */
1.130868 +   || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
1.130869 +  );
1.130870 +
1.130871 +  /* Check for a "special" INSERT operation. One of the form:
1.130872 +  **
1.130873 +  **   INSERT INTO xyz(xyz) VALUES('command');
1.130874 +  */
1.130875 +  if( nArg>1 
1.130876 +   && sqlite3_value_type(apVal[0])==SQLITE_NULL 
1.130877 +   && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL 
1.130878 +  ){
1.130879 +    rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
1.130880 +    goto update_out;
1.130881 +  }
1.130882 +
1.130883 +  if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
1.130884 +    rc = SQLITE_CONSTRAINT;
1.130885 +    goto update_out;
1.130886 +  }
1.130887 +
1.130888 +  /* Allocate space to hold the change in document sizes */
1.130889 +  aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
1.130890 +  if( aSzDel==0 ){
1.130891 +    rc = SQLITE_NOMEM;
1.130892 +    goto update_out;
1.130893 +  }
1.130894 +  aSzIns = &aSzDel[p->nColumn+1];
1.130895 +  memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
1.130896 +
1.130897 +  /* If this is an INSERT operation, or an UPDATE that modifies the rowid
1.130898 +  ** value, then this operation requires constraint handling.
1.130899 +  **
1.130900 +  ** If the on-conflict mode is REPLACE, this means that the existing row
1.130901 +  ** should be deleted from the database before inserting the new row. Or,
1.130902 +  ** if the on-conflict mode is other than REPLACE, then this method must
1.130903 +  ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
1.130904 +  ** modify the database file.
1.130905 +  */
1.130906 +  if( nArg>1 && p->zContentTbl==0 ){
1.130907 +    /* Find the value object that holds the new rowid value. */
1.130908 +    sqlite3_value *pNewRowid = apVal[3+p->nColumn];
1.130909 +    if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
1.130910 +      pNewRowid = apVal[1];
1.130911 +    }
1.130912 +
1.130913 +    if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && ( 
1.130914 +        sqlite3_value_type(apVal[0])==SQLITE_NULL
1.130915 +     || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
1.130916 +    )){
1.130917 +      /* The new rowid is not NULL (in this case the rowid will be
1.130918 +      ** automatically assigned and there is no chance of a conflict), and 
1.130919 +      ** the statement is either an INSERT or an UPDATE that modifies the
1.130920 +      ** rowid column. So if the conflict mode is REPLACE, then delete any
1.130921 +      ** existing row with rowid=pNewRowid. 
1.130922 +      **
1.130923 +      ** Or, if the conflict mode is not REPLACE, insert the new record into 
1.130924 +      ** the %_content table. If we hit the duplicate rowid constraint (or any
1.130925 +      ** other error) while doing so, return immediately.
1.130926 +      **
1.130927 +      ** This branch may also run if pNewRowid contains a value that cannot
1.130928 +      ** be losslessly converted to an integer. In this case, the eventual 
1.130929 +      ** call to fts3InsertData() (either just below or further on in this
1.130930 +      ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is 
1.130931 +      ** invoked, it will delete zero rows (since no row will have
1.130932 +      ** docid=$pNewRowid if $pNewRowid is not an integer value).
1.130933 +      */
1.130934 +      if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
1.130935 +        rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
1.130936 +      }else{
1.130937 +        rc = fts3InsertData(p, apVal, pRowid);
1.130938 +        bInsertDone = 1;
1.130939 +      }
1.130940 +    }
1.130941 +  }
1.130942 +  if( rc!=SQLITE_OK ){
1.130943 +    goto update_out;
1.130944 +  }
1.130945 +
1.130946 +  /* If this is a DELETE or UPDATE operation, remove the old record. */
1.130947 +  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
1.130948 +    assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
1.130949 +    rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
1.130950 +    isRemove = 1;
1.130951 +  }
1.130952 +  
1.130953 +  /* If this is an INSERT or UPDATE operation, insert the new record. */
1.130954 +  if( nArg>1 && rc==SQLITE_OK ){
1.130955 +    int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
1.130956 +    if( bInsertDone==0 ){
1.130957 +      rc = fts3InsertData(p, apVal, pRowid);
1.130958 +      if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
1.130959 +        rc = FTS_CORRUPT_VTAB;
1.130960 +      }
1.130961 +    }
1.130962 +    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
1.130963 +      rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
1.130964 +    }
1.130965 +    if( rc==SQLITE_OK ){
1.130966 +      assert( p->iPrevDocid==*pRowid );
1.130967 +      rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
1.130968 +    }
1.130969 +    if( p->bHasDocsize ){
1.130970 +      fts3InsertDocsize(&rc, p, aSzIns);
1.130971 +    }
1.130972 +    nChng++;
1.130973 +  }
1.130974 +
1.130975 +  if( p->bFts4 ){
1.130976 +    fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
1.130977 +  }
1.130978 +
1.130979 + update_out:
1.130980 +  sqlite3_free(aSzDel);
1.130981 +  sqlite3Fts3SegmentsClose(p);
1.130982 +  return rc;
1.130983 +}
1.130984 +
1.130985 +/* 
1.130986 +** Flush any data in the pending-terms hash table to disk. If successful,
1.130987 +** merge all segments in the database (including the new segment, if 
1.130988 +** there was any data to flush) into a single segment. 
1.130989 +*/
1.130990 +SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
1.130991 +  int rc;
1.130992 +  rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
1.130993 +  if( rc==SQLITE_OK ){
1.130994 +    rc = fts3DoOptimize(p, 1);
1.130995 +    if( rc==SQLITE_OK || rc==SQLITE_DONE ){
1.130996 +      int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
1.130997 +      if( rc2!=SQLITE_OK ) rc = rc2;
1.130998 +    }else{
1.130999 +      sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
1.131000 +      sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
1.131001 +    }
1.131002 +  }
1.131003 +  sqlite3Fts3SegmentsClose(p);
1.131004 +  return rc;
1.131005 +}
1.131006 +
1.131007 +#endif
1.131008 +
1.131009 +/************** End of fts3_write.c ******************************************/
1.131010 +/************** Begin file fts3_snippet.c ************************************/
1.131011 +/*
1.131012 +** 2009 Oct 23
1.131013 +**
1.131014 +** The author disclaims copyright to this source code.  In place of
1.131015 +** a legal notice, here is a blessing:
1.131016 +**
1.131017 +**    May you do good and not evil.
1.131018 +**    May you find forgiveness for yourself and forgive others.
1.131019 +**    May you share freely, never taking more than you give.
1.131020 +**
1.131021 +******************************************************************************
1.131022 +*/
1.131023 +
1.131024 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.131025 +
1.131026 +/* #include <string.h> */
1.131027 +/* #include <assert.h> */
1.131028 +
1.131029 +/*
1.131030 +** Characters that may appear in the second argument to matchinfo().
1.131031 +*/
1.131032 +#define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
1.131033 +#define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
1.131034 +#define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
1.131035 +#define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
1.131036 +#define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
1.131037 +#define FTS3_MATCHINFO_LCS       's'        /* nCol values */
1.131038 +#define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
1.131039 +
1.131040 +/*
1.131041 +** The default value for the second argument to matchinfo(). 
1.131042 +*/
1.131043 +#define FTS3_MATCHINFO_DEFAULT   "pcx"
1.131044 +
1.131045 +
1.131046 +/*
1.131047 +** Used as an fts3ExprIterate() context when loading phrase doclists to
1.131048 +** Fts3Expr.aDoclist[]/nDoclist.
1.131049 +*/
1.131050 +typedef struct LoadDoclistCtx LoadDoclistCtx;
1.131051 +struct LoadDoclistCtx {
1.131052 +  Fts3Cursor *pCsr;               /* FTS3 Cursor */
1.131053 +  int nPhrase;                    /* Number of phrases seen so far */
1.131054 +  int nToken;                     /* Number of tokens seen so far */
1.131055 +};
1.131056 +
1.131057 +/*
1.131058 +** The following types are used as part of the implementation of the 
1.131059 +** fts3BestSnippet() routine.
1.131060 +*/
1.131061 +typedef struct SnippetIter SnippetIter;
1.131062 +typedef struct SnippetPhrase SnippetPhrase;
1.131063 +typedef struct SnippetFragment SnippetFragment;
1.131064 +
1.131065 +struct SnippetIter {
1.131066 +  Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
1.131067 +  int iCol;                       /* Extract snippet from this column */
1.131068 +  int nSnippet;                   /* Requested snippet length (in tokens) */
1.131069 +  int nPhrase;                    /* Number of phrases in query */
1.131070 +  SnippetPhrase *aPhrase;         /* Array of size nPhrase */
1.131071 +  int iCurrent;                   /* First token of current snippet */
1.131072 +};
1.131073 +
1.131074 +struct SnippetPhrase {
1.131075 +  int nToken;                     /* Number of tokens in phrase */
1.131076 +  char *pList;                    /* Pointer to start of phrase position list */
1.131077 +  int iHead;                      /* Next value in position list */
1.131078 +  char *pHead;                    /* Position list data following iHead */
1.131079 +  int iTail;                      /* Next value in trailing position list */
1.131080 +  char *pTail;                    /* Position list data following iTail */
1.131081 +};
1.131082 +
1.131083 +struct SnippetFragment {
1.131084 +  int iCol;                       /* Column snippet is extracted from */
1.131085 +  int iPos;                       /* Index of first token in snippet */
1.131086 +  u64 covered;                    /* Mask of query phrases covered */
1.131087 +  u64 hlmask;                     /* Mask of snippet terms to highlight */
1.131088 +};
1.131089 +
1.131090 +/*
1.131091 +** This type is used as an fts3ExprIterate() context object while 
1.131092 +** accumulating the data returned by the matchinfo() function.
1.131093 +*/
1.131094 +typedef struct MatchInfo MatchInfo;
1.131095 +struct MatchInfo {
1.131096 +  Fts3Cursor *pCursor;            /* FTS3 Cursor */
1.131097 +  int nCol;                       /* Number of columns in table */
1.131098 +  int nPhrase;                    /* Number of matchable phrases in query */
1.131099 +  sqlite3_int64 nDoc;             /* Number of docs in database */
1.131100 +  u32 *aMatchinfo;                /* Pre-allocated buffer */
1.131101 +};
1.131102 +
1.131103 +
1.131104 +
1.131105 +/*
1.131106 +** The snippet() and offsets() functions both return text values. An instance
1.131107 +** of the following structure is used to accumulate those values while the
1.131108 +** functions are running. See fts3StringAppend() for details.
1.131109 +*/
1.131110 +typedef struct StrBuffer StrBuffer;
1.131111 +struct StrBuffer {
1.131112 +  char *z;                        /* Pointer to buffer containing string */
1.131113 +  int n;                          /* Length of z in bytes (excl. nul-term) */
1.131114 +  int nAlloc;                     /* Allocated size of buffer z in bytes */
1.131115 +};
1.131116 +
1.131117 +
1.131118 +/*
1.131119 +** This function is used to help iterate through a position-list. A position
1.131120 +** list is a list of unique integers, sorted from smallest to largest. Each
1.131121 +** element of the list is represented by an FTS3 varint that takes the value
1.131122 +** of the difference between the current element and the previous one plus
1.131123 +** two. For example, to store the position-list:
1.131124 +**
1.131125 +**     4 9 113
1.131126 +**
1.131127 +** the three varints:
1.131128 +**
1.131129 +**     6 7 106
1.131130 +**
1.131131 +** are encoded.
1.131132 +**
1.131133 +** When this function is called, *pp points to the start of an element of
1.131134 +** the list. *piPos contains the value of the previous entry in the list.
1.131135 +** After it returns, *piPos contains the value of the next element of the
1.131136 +** list and *pp is advanced to the following varint.
1.131137 +*/
1.131138 +static void fts3GetDeltaPosition(char **pp, int *piPos){
1.131139 +  int iVal;
1.131140 +  *pp += sqlite3Fts3GetVarint32(*pp, &iVal);
1.131141 +  *piPos += (iVal-2);
1.131142 +}
1.131143 +
1.131144 +/*
1.131145 +** Helper function for fts3ExprIterate() (see below).
1.131146 +*/
1.131147 +static int fts3ExprIterate2(
1.131148 +  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
1.131149 +  int *piPhrase,                  /* Pointer to phrase counter */
1.131150 +  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
1.131151 +  void *pCtx                      /* Second argument to pass to callback */
1.131152 +){
1.131153 +  int rc;                         /* Return code */
1.131154 +  int eType = pExpr->eType;       /* Type of expression node pExpr */
1.131155 +
1.131156 +  if( eType!=FTSQUERY_PHRASE ){
1.131157 +    assert( pExpr->pLeft && pExpr->pRight );
1.131158 +    rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
1.131159 +    if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
1.131160 +      rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
1.131161 +    }
1.131162 +  }else{
1.131163 +    rc = x(pExpr, *piPhrase, pCtx);
1.131164 +    (*piPhrase)++;
1.131165 +  }
1.131166 +  return rc;
1.131167 +}
1.131168 +
1.131169 +/*
1.131170 +** Iterate through all phrase nodes in an FTS3 query, except those that
1.131171 +** are part of a sub-tree that is the right-hand-side of a NOT operator.
1.131172 +** For each phrase node found, the supplied callback function is invoked.
1.131173 +**
1.131174 +** If the callback function returns anything other than SQLITE_OK, 
1.131175 +** the iteration is abandoned and the error code returned immediately.
1.131176 +** Otherwise, SQLITE_OK is returned after a callback has been made for
1.131177 +** all eligible phrase nodes.
1.131178 +*/
1.131179 +static int fts3ExprIterate(
1.131180 +  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
1.131181 +  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
1.131182 +  void *pCtx                      /* Second argument to pass to callback */
1.131183 +){
1.131184 +  int iPhrase = 0;                /* Variable used as the phrase counter */
1.131185 +  return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
1.131186 +}
1.131187 +
1.131188 +/*
1.131189 +** This is an fts3ExprIterate() callback used while loading the doclists
1.131190 +** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
1.131191 +** fts3ExprLoadDoclists().
1.131192 +*/
1.131193 +static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
1.131194 +  int rc = SQLITE_OK;
1.131195 +  Fts3Phrase *pPhrase = pExpr->pPhrase;
1.131196 +  LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
1.131197 +
1.131198 +  UNUSED_PARAMETER(iPhrase);
1.131199 +
1.131200 +  p->nPhrase++;
1.131201 +  p->nToken += pPhrase->nToken;
1.131202 +
1.131203 +  return rc;
1.131204 +}
1.131205 +
1.131206 +/*
1.131207 +** Load the doclists for each phrase in the query associated with FTS3 cursor
1.131208 +** pCsr. 
1.131209 +**
1.131210 +** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable 
1.131211 +** phrases in the expression (all phrases except those directly or 
1.131212 +** indirectly descended from the right-hand-side of a NOT operator). If 
1.131213 +** pnToken is not NULL, then it is set to the number of tokens in all
1.131214 +** matchable phrases of the expression.
1.131215 +*/
1.131216 +static int fts3ExprLoadDoclists(
1.131217 +  Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
1.131218 +  int *pnPhrase,                  /* OUT: Number of phrases in query */
1.131219 +  int *pnToken                    /* OUT: Number of tokens in query */
1.131220 +){
1.131221 +  int rc;                         /* Return Code */
1.131222 +  LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
1.131223 +  sCtx.pCsr = pCsr;
1.131224 +  rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
1.131225 +  if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
1.131226 +  if( pnToken ) *pnToken = sCtx.nToken;
1.131227 +  return rc;
1.131228 +}
1.131229 +
1.131230 +static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
1.131231 +  (*(int *)ctx)++;
1.131232 +  UNUSED_PARAMETER(pExpr);
1.131233 +  UNUSED_PARAMETER(iPhrase);
1.131234 +  return SQLITE_OK;
1.131235 +}
1.131236 +static int fts3ExprPhraseCount(Fts3Expr *pExpr){
1.131237 +  int nPhrase = 0;
1.131238 +  (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
1.131239 +  return nPhrase;
1.131240 +}
1.131241 +
1.131242 +/*
1.131243 +** Advance the position list iterator specified by the first two 
1.131244 +** arguments so that it points to the first element with a value greater
1.131245 +** than or equal to parameter iNext.
1.131246 +*/
1.131247 +static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
1.131248 +  char *pIter = *ppIter;
1.131249 +  if( pIter ){
1.131250 +    int iIter = *piIter;
1.131251 +
1.131252 +    while( iIter<iNext ){
1.131253 +      if( 0==(*pIter & 0xFE) ){
1.131254 +        iIter = -1;
1.131255 +        pIter = 0;
1.131256 +        break;
1.131257 +      }
1.131258 +      fts3GetDeltaPosition(&pIter, &iIter);
1.131259 +    }
1.131260 +
1.131261 +    *piIter = iIter;
1.131262 +    *ppIter = pIter;
1.131263 +  }
1.131264 +}
1.131265 +
1.131266 +/*
1.131267 +** Advance the snippet iterator to the next candidate snippet.
1.131268 +*/
1.131269 +static int fts3SnippetNextCandidate(SnippetIter *pIter){
1.131270 +  int i;                          /* Loop counter */
1.131271 +
1.131272 +  if( pIter->iCurrent<0 ){
1.131273 +    /* The SnippetIter object has just been initialized. The first snippet
1.131274 +    ** candidate always starts at offset 0 (even if this candidate has a
1.131275 +    ** score of 0.0).
1.131276 +    */
1.131277 +    pIter->iCurrent = 0;
1.131278 +
1.131279 +    /* Advance the 'head' iterator of each phrase to the first offset that
1.131280 +    ** is greater than or equal to (iNext+nSnippet).
1.131281 +    */
1.131282 +    for(i=0; i<pIter->nPhrase; i++){
1.131283 +      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
1.131284 +      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
1.131285 +    }
1.131286 +  }else{
1.131287 +    int iStart;
1.131288 +    int iEnd = 0x7FFFFFFF;
1.131289 +
1.131290 +    for(i=0; i<pIter->nPhrase; i++){
1.131291 +      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
1.131292 +      if( pPhrase->pHead && pPhrase->iHead<iEnd ){
1.131293 +        iEnd = pPhrase->iHead;
1.131294 +      }
1.131295 +    }
1.131296 +    if( iEnd==0x7FFFFFFF ){
1.131297 +      return 1;
1.131298 +    }
1.131299 +
1.131300 +    pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
1.131301 +    for(i=0; i<pIter->nPhrase; i++){
1.131302 +      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
1.131303 +      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
1.131304 +      fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
1.131305 +    }
1.131306 +  }
1.131307 +
1.131308 +  return 0;
1.131309 +}
1.131310 +
1.131311 +/*
1.131312 +** Retrieve information about the current candidate snippet of snippet 
1.131313 +** iterator pIter.
1.131314 +*/
1.131315 +static void fts3SnippetDetails(
1.131316 +  SnippetIter *pIter,             /* Snippet iterator */
1.131317 +  u64 mCovered,                   /* Bitmask of phrases already covered */
1.131318 +  int *piToken,                   /* OUT: First token of proposed snippet */
1.131319 +  int *piScore,                   /* OUT: "Score" for this snippet */
1.131320 +  u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
1.131321 +  u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
1.131322 +){
1.131323 +  int iStart = pIter->iCurrent;   /* First token of snippet */
1.131324 +  int iScore = 0;                 /* Score of this snippet */
1.131325 +  int i;                          /* Loop counter */
1.131326 +  u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
1.131327 +  u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
1.131328 +
1.131329 +  for(i=0; i<pIter->nPhrase; i++){
1.131330 +    SnippetPhrase *pPhrase = &pIter->aPhrase[i];
1.131331 +    if( pPhrase->pTail ){
1.131332 +      char *pCsr = pPhrase->pTail;
1.131333 +      int iCsr = pPhrase->iTail;
1.131334 +
1.131335 +      while( iCsr<(iStart+pIter->nSnippet) ){
1.131336 +        int j;
1.131337 +        u64 mPhrase = (u64)1 << i;
1.131338 +        u64 mPos = (u64)1 << (iCsr - iStart);
1.131339 +        assert( iCsr>=iStart );
1.131340 +        if( (mCover|mCovered)&mPhrase ){
1.131341 +          iScore++;
1.131342 +        }else{
1.131343 +          iScore += 1000;
1.131344 +        }
1.131345 +        mCover |= mPhrase;
1.131346 +
1.131347 +        for(j=0; j<pPhrase->nToken; j++){
1.131348 +          mHighlight |= (mPos>>j);
1.131349 +        }
1.131350 +
1.131351 +        if( 0==(*pCsr & 0x0FE) ) break;
1.131352 +        fts3GetDeltaPosition(&pCsr, &iCsr);
1.131353 +      }
1.131354 +    }
1.131355 +  }
1.131356 +
1.131357 +  /* Set the output variables before returning. */
1.131358 +  *piToken = iStart;
1.131359 +  *piScore = iScore;
1.131360 +  *pmCover = mCover;
1.131361 +  *pmHighlight = mHighlight;
1.131362 +}
1.131363 +
1.131364 +/*
1.131365 +** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
1.131366 +** Each invocation populates an element of the SnippetIter.aPhrase[] array.
1.131367 +*/
1.131368 +static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
1.131369 +  SnippetIter *p = (SnippetIter *)ctx;
1.131370 +  SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
1.131371 +  char *pCsr;
1.131372 +  int rc;
1.131373 +
1.131374 +  pPhrase->nToken = pExpr->pPhrase->nToken;
1.131375 +  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
1.131376 +  assert( rc==SQLITE_OK || pCsr==0 );
1.131377 +  if( pCsr ){
1.131378 +    int iFirst = 0;
1.131379 +    pPhrase->pList = pCsr;
1.131380 +    fts3GetDeltaPosition(&pCsr, &iFirst);
1.131381 +    assert( iFirst>=0 );
1.131382 +    pPhrase->pHead = pCsr;
1.131383 +    pPhrase->pTail = pCsr;
1.131384 +    pPhrase->iHead = iFirst;
1.131385 +    pPhrase->iTail = iFirst;
1.131386 +  }else{
1.131387 +    assert( rc!=SQLITE_OK || (
1.131388 +       pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 
1.131389 +    ));
1.131390 +  }
1.131391 +
1.131392 +  return rc;
1.131393 +}
1.131394 +
1.131395 +/*
1.131396 +** Select the fragment of text consisting of nFragment contiguous tokens 
1.131397 +** from column iCol that represent the "best" snippet. The best snippet
1.131398 +** is the snippet with the highest score, where scores are calculated
1.131399 +** by adding:
1.131400 +**
1.131401 +**   (a) +1 point for each occurence of a matchable phrase in the snippet.
1.131402 +**
1.131403 +**   (b) +1000 points for the first occurence of each matchable phrase in 
1.131404 +**       the snippet for which the corresponding mCovered bit is not set.
1.131405 +**
1.131406 +** The selected snippet parameters are stored in structure *pFragment before
1.131407 +** returning. The score of the selected snippet is stored in *piScore
1.131408 +** before returning.
1.131409 +*/
1.131410 +static int fts3BestSnippet(
1.131411 +  int nSnippet,                   /* Desired snippet length */
1.131412 +  Fts3Cursor *pCsr,               /* Cursor to create snippet for */
1.131413 +  int iCol,                       /* Index of column to create snippet from */
1.131414 +  u64 mCovered,                   /* Mask of phrases already covered */
1.131415 +  u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
1.131416 +  SnippetFragment *pFragment,     /* OUT: Best snippet found */
1.131417 +  int *piScore                    /* OUT: Score of snippet pFragment */
1.131418 +){
1.131419 +  int rc;                         /* Return Code */
1.131420 +  int nList;                      /* Number of phrases in expression */
1.131421 +  SnippetIter sIter;              /* Iterates through snippet candidates */
1.131422 +  int nByte;                      /* Number of bytes of space to allocate */
1.131423 +  int iBestScore = -1;            /* Best snippet score found so far */
1.131424 +  int i;                          /* Loop counter */
1.131425 +
1.131426 +  memset(&sIter, 0, sizeof(sIter));
1.131427 +
1.131428 +  /* Iterate through the phrases in the expression to count them. The same
1.131429 +  ** callback makes sure the doclists are loaded for each phrase.
1.131430 +  */
1.131431 +  rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
1.131432 +  if( rc!=SQLITE_OK ){
1.131433 +    return rc;
1.131434 +  }
1.131435 +
1.131436 +  /* Now that it is known how many phrases there are, allocate and zero
1.131437 +  ** the required space using malloc().
1.131438 +  */
1.131439 +  nByte = sizeof(SnippetPhrase) * nList;
1.131440 +  sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
1.131441 +  if( !sIter.aPhrase ){
1.131442 +    return SQLITE_NOMEM;
1.131443 +  }
1.131444 +  memset(sIter.aPhrase, 0, nByte);
1.131445 +
1.131446 +  /* Initialize the contents of the SnippetIter object. Then iterate through
1.131447 +  ** the set of phrases in the expression to populate the aPhrase[] array.
1.131448 +  */
1.131449 +  sIter.pCsr = pCsr;
1.131450 +  sIter.iCol = iCol;
1.131451 +  sIter.nSnippet = nSnippet;
1.131452 +  sIter.nPhrase = nList;
1.131453 +  sIter.iCurrent = -1;
1.131454 +  (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
1.131455 +
1.131456 +  /* Set the *pmSeen output variable. */
1.131457 +  for(i=0; i<nList; i++){
1.131458 +    if( sIter.aPhrase[i].pHead ){
1.131459 +      *pmSeen |= (u64)1 << i;
1.131460 +    }
1.131461 +  }
1.131462 +
1.131463 +  /* Loop through all candidate snippets. Store the best snippet in 
1.131464 +  ** *pFragment. Store its associated 'score' in iBestScore.
1.131465 +  */
1.131466 +  pFragment->iCol = iCol;
1.131467 +  while( !fts3SnippetNextCandidate(&sIter) ){
1.131468 +    int iPos;
1.131469 +    int iScore;
1.131470 +    u64 mCover;
1.131471 +    u64 mHighlight;
1.131472 +    fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
1.131473 +    assert( iScore>=0 );
1.131474 +    if( iScore>iBestScore ){
1.131475 +      pFragment->iPos = iPos;
1.131476 +      pFragment->hlmask = mHighlight;
1.131477 +      pFragment->covered = mCover;
1.131478 +      iBestScore = iScore;
1.131479 +    }
1.131480 +  }
1.131481 +
1.131482 +  sqlite3_free(sIter.aPhrase);
1.131483 +  *piScore = iBestScore;
1.131484 +  return SQLITE_OK;
1.131485 +}
1.131486 +
1.131487 +
1.131488 +/*
1.131489 +** Append a string to the string-buffer passed as the first argument.
1.131490 +**
1.131491 +** If nAppend is negative, then the length of the string zAppend is
1.131492 +** determined using strlen().
1.131493 +*/
1.131494 +static int fts3StringAppend(
1.131495 +  StrBuffer *pStr,                /* Buffer to append to */
1.131496 +  const char *zAppend,            /* Pointer to data to append to buffer */
1.131497 +  int nAppend                     /* Size of zAppend in bytes (or -1) */
1.131498 +){
1.131499 +  if( nAppend<0 ){
1.131500 +    nAppend = (int)strlen(zAppend);
1.131501 +  }
1.131502 +
1.131503 +  /* If there is insufficient space allocated at StrBuffer.z, use realloc()
1.131504 +  ** to grow the buffer until so that it is big enough to accomadate the
1.131505 +  ** appended data.
1.131506 +  */
1.131507 +  if( pStr->n+nAppend+1>=pStr->nAlloc ){
1.131508 +    int nAlloc = pStr->nAlloc+nAppend+100;
1.131509 +    char *zNew = sqlite3_realloc(pStr->z, nAlloc);
1.131510 +    if( !zNew ){
1.131511 +      return SQLITE_NOMEM;
1.131512 +    }
1.131513 +    pStr->z = zNew;
1.131514 +    pStr->nAlloc = nAlloc;
1.131515 +  }
1.131516 +
1.131517 +  /* Append the data to the string buffer. */
1.131518 +  memcpy(&pStr->z[pStr->n], zAppend, nAppend);
1.131519 +  pStr->n += nAppend;
1.131520 +  pStr->z[pStr->n] = '\0';
1.131521 +
1.131522 +  return SQLITE_OK;
1.131523 +}
1.131524 +
1.131525 +/*
1.131526 +** The fts3BestSnippet() function often selects snippets that end with a
1.131527 +** query term. That is, the final term of the snippet is always a term
1.131528 +** that requires highlighting. For example, if 'X' is a highlighted term
1.131529 +** and '.' is a non-highlighted term, BestSnippet() may select:
1.131530 +**
1.131531 +**     ........X.....X
1.131532 +**
1.131533 +** This function "shifts" the beginning of the snippet forward in the 
1.131534 +** document so that there are approximately the same number of 
1.131535 +** non-highlighted terms to the right of the final highlighted term as there
1.131536 +** are to the left of the first highlighted term. For example, to this:
1.131537 +**
1.131538 +**     ....X.....X....
1.131539 +**
1.131540 +** This is done as part of extracting the snippet text, not when selecting
1.131541 +** the snippet. Snippet selection is done based on doclists only, so there
1.131542 +** is no way for fts3BestSnippet() to know whether or not the document 
1.131543 +** actually contains terms that follow the final highlighted term. 
1.131544 +*/
1.131545 +static int fts3SnippetShift(
1.131546 +  Fts3Table *pTab,                /* FTS3 table snippet comes from */
1.131547 +  int iLangid,                    /* Language id to use in tokenizing */
1.131548 +  int nSnippet,                   /* Number of tokens desired for snippet */
1.131549 +  const char *zDoc,               /* Document text to extract snippet from */
1.131550 +  int nDoc,                       /* Size of buffer zDoc in bytes */
1.131551 +  int *piPos,                     /* IN/OUT: First token of snippet */
1.131552 +  u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
1.131553 +){
1.131554 +  u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
1.131555 +
1.131556 +  if( hlmask ){
1.131557 +    int nLeft;                    /* Tokens to the left of first highlight */
1.131558 +    int nRight;                   /* Tokens to the right of last highlight */
1.131559 +    int nDesired;                 /* Ideal number of tokens to shift forward */
1.131560 +
1.131561 +    for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
1.131562 +    for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
1.131563 +    nDesired = (nLeft-nRight)/2;
1.131564 +
1.131565 +    /* Ideally, the start of the snippet should be pushed forward in the
1.131566 +    ** document nDesired tokens. This block checks if there are actually
1.131567 +    ** nDesired tokens to the right of the snippet. If so, *piPos and
1.131568 +    ** *pHlMask are updated to shift the snippet nDesired tokens to the
1.131569 +    ** right. Otherwise, the snippet is shifted by the number of tokens
1.131570 +    ** available.
1.131571 +    */
1.131572 +    if( nDesired>0 ){
1.131573 +      int nShift;                 /* Number of tokens to shift snippet by */
1.131574 +      int iCurrent = 0;           /* Token counter */
1.131575 +      int rc;                     /* Return Code */
1.131576 +      sqlite3_tokenizer_module *pMod;
1.131577 +      sqlite3_tokenizer_cursor *pC;
1.131578 +      pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
1.131579 +
1.131580 +      /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
1.131581 +      ** or more tokens in zDoc/nDoc.
1.131582 +      */
1.131583 +      rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
1.131584 +      if( rc!=SQLITE_OK ){
1.131585 +        return rc;
1.131586 +      }
1.131587 +      while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
1.131588 +        const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
1.131589 +        rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
1.131590 +      }
1.131591 +      pMod->xClose(pC);
1.131592 +      if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
1.131593 +
1.131594 +      nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
1.131595 +      assert( nShift<=nDesired );
1.131596 +      if( nShift>0 ){
1.131597 +        *piPos += nShift;
1.131598 +        *pHlmask = hlmask >> nShift;
1.131599 +      }
1.131600 +    }
1.131601 +  }
1.131602 +  return SQLITE_OK;
1.131603 +}
1.131604 +
1.131605 +/*
1.131606 +** Extract the snippet text for fragment pFragment from cursor pCsr and
1.131607 +** append it to string buffer pOut.
1.131608 +*/
1.131609 +static int fts3SnippetText(
1.131610 +  Fts3Cursor *pCsr,               /* FTS3 Cursor */
1.131611 +  SnippetFragment *pFragment,     /* Snippet to extract */
1.131612 +  int iFragment,                  /* Fragment number */
1.131613 +  int isLast,                     /* True for final fragment in snippet */
1.131614 +  int nSnippet,                   /* Number of tokens in extracted snippet */
1.131615 +  const char *zOpen,              /* String inserted before highlighted term */
1.131616 +  const char *zClose,             /* String inserted after highlighted term */
1.131617 +  const char *zEllipsis,          /* String inserted between snippets */
1.131618 +  StrBuffer *pOut                 /* Write output here */
1.131619 +){
1.131620 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.131621 +  int rc;                         /* Return code */
1.131622 +  const char *zDoc;               /* Document text to extract snippet from */
1.131623 +  int nDoc;                       /* Size of zDoc in bytes */
1.131624 +  int iCurrent = 0;               /* Current token number of document */
1.131625 +  int iEnd = 0;                   /* Byte offset of end of current token */
1.131626 +  int isShiftDone = 0;            /* True after snippet is shifted */
1.131627 +  int iPos = pFragment->iPos;     /* First token of snippet */
1.131628 +  u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
1.131629 +  int iCol = pFragment->iCol+1;   /* Query column to extract text from */
1.131630 +  sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
1.131631 +  sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
1.131632 +  
1.131633 +  zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
1.131634 +  if( zDoc==0 ){
1.131635 +    if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
1.131636 +      return SQLITE_NOMEM;
1.131637 +    }
1.131638 +    return SQLITE_OK;
1.131639 +  }
1.131640 +  nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
1.131641 +
1.131642 +  /* Open a token cursor on the document. */
1.131643 +  pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
1.131644 +  rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
1.131645 +  if( rc!=SQLITE_OK ){
1.131646 +    return rc;
1.131647 +  }
1.131648 +
1.131649 +  while( rc==SQLITE_OK ){
1.131650 +    const char *ZDUMMY;           /* Dummy argument used with tokenizer */
1.131651 +    int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
1.131652 +    int iBegin = 0;               /* Offset in zDoc of start of token */
1.131653 +    int iFin = 0;                 /* Offset in zDoc of end of token */
1.131654 +    int isHighlight = 0;          /* True for highlighted terms */
1.131655 +
1.131656 +    /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
1.131657 +    ** in the FTS code the variable that the third argument to xNext points to
1.131658 +    ** is initialized to zero before the first (*but not necessarily
1.131659 +    ** subsequent*) call to xNext(). This is done for a particular application
1.131660 +    ** that needs to know whether or not the tokenizer is being used for
1.131661 +    ** snippet generation or for some other purpose.
1.131662 +    **
1.131663 +    ** Extreme care is required when writing code to depend on this
1.131664 +    ** initialization. It is not a documented part of the tokenizer interface.
1.131665 +    ** If a tokenizer is used directly by any code outside of FTS, this
1.131666 +    ** convention might not be respected.  */
1.131667 +    rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
1.131668 +    if( rc!=SQLITE_OK ){
1.131669 +      if( rc==SQLITE_DONE ){
1.131670 +        /* Special case - the last token of the snippet is also the last token
1.131671 +        ** of the column. Append any punctuation that occurred between the end
1.131672 +        ** of the previous token and the end of the document to the output. 
1.131673 +        ** Then break out of the loop. */
1.131674 +        rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
1.131675 +      }
1.131676 +      break;
1.131677 +    }
1.131678 +    if( iCurrent<iPos ){ continue; }
1.131679 +
1.131680 +    if( !isShiftDone ){
1.131681 +      int n = nDoc - iBegin;
1.131682 +      rc = fts3SnippetShift(
1.131683 +          pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
1.131684 +      );
1.131685 +      isShiftDone = 1;
1.131686 +
1.131687 +      /* Now that the shift has been done, check if the initial "..." are
1.131688 +      ** required. They are required if (a) this is not the first fragment,
1.131689 +      ** or (b) this fragment does not begin at position 0 of its column. 
1.131690 +      */
1.131691 +      if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
1.131692 +        rc = fts3StringAppend(pOut, zEllipsis, -1);
1.131693 +      }
1.131694 +      if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
1.131695 +    }
1.131696 +
1.131697 +    if( iCurrent>=(iPos+nSnippet) ){
1.131698 +      if( isLast ){
1.131699 +        rc = fts3StringAppend(pOut, zEllipsis, -1);
1.131700 +      }
1.131701 +      break;
1.131702 +    }
1.131703 +
1.131704 +    /* Set isHighlight to true if this term should be highlighted. */
1.131705 +    isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
1.131706 +
1.131707 +    if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
1.131708 +    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
1.131709 +    if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
1.131710 +    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
1.131711 +
1.131712 +    iEnd = iFin;
1.131713 +  }
1.131714 +
1.131715 +  pMod->xClose(pC);
1.131716 +  return rc;
1.131717 +}
1.131718 +
1.131719 +
1.131720 +/*
1.131721 +** This function is used to count the entries in a column-list (a 
1.131722 +** delta-encoded list of term offsets within a single column of a single 
1.131723 +** row). When this function is called, *ppCollist should point to the
1.131724 +** beginning of the first varint in the column-list (the varint that
1.131725 +** contains the position of the first matching term in the column data).
1.131726 +** Before returning, *ppCollist is set to point to the first byte after
1.131727 +** the last varint in the column-list (either the 0x00 signifying the end
1.131728 +** of the position-list, or the 0x01 that precedes the column number of
1.131729 +** the next column in the position-list).
1.131730 +**
1.131731 +** The number of elements in the column-list is returned.
1.131732 +*/
1.131733 +static int fts3ColumnlistCount(char **ppCollist){
1.131734 +  char *pEnd = *ppCollist;
1.131735 +  char c = 0;
1.131736 +  int nEntry = 0;
1.131737 +
1.131738 +  /* A column-list is terminated by either a 0x01 or 0x00. */
1.131739 +  while( 0xFE & (*pEnd | c) ){
1.131740 +    c = *pEnd++ & 0x80;
1.131741 +    if( !c ) nEntry++;
1.131742 +  }
1.131743 +
1.131744 +  *ppCollist = pEnd;
1.131745 +  return nEntry;
1.131746 +}
1.131747 +
1.131748 +/*
1.131749 +** fts3ExprIterate() callback used to collect the "global" matchinfo stats
1.131750 +** for a single query. 
1.131751 +**
1.131752 +** fts3ExprIterate() callback to load the 'global' elements of a
1.131753 +** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements 
1.131754 +** of the matchinfo array that are constant for all rows returned by the 
1.131755 +** current query.
1.131756 +**
1.131757 +** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
1.131758 +** function populates Matchinfo.aMatchinfo[] as follows:
1.131759 +**
1.131760 +**   for(iCol=0; iCol<nCol; iCol++){
1.131761 +**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
1.131762 +**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
1.131763 +**   }
1.131764 +**
1.131765 +** where X is the number of matches for phrase iPhrase is column iCol of all
1.131766 +** rows of the table. Y is the number of rows for which column iCol contains
1.131767 +** at least one instance of phrase iPhrase.
1.131768 +**
1.131769 +** If the phrase pExpr consists entirely of deferred tokens, then all X and
1.131770 +** Y values are set to nDoc, where nDoc is the number of documents in the 
1.131771 +** file system. This is done because the full-text index doclist is required
1.131772 +** to calculate these values properly, and the full-text index doclist is
1.131773 +** not available for deferred tokens.
1.131774 +*/
1.131775 +static int fts3ExprGlobalHitsCb(
1.131776 +  Fts3Expr *pExpr,                /* Phrase expression node */
1.131777 +  int iPhrase,                    /* Phrase number (numbered from zero) */
1.131778 +  void *pCtx                      /* Pointer to MatchInfo structure */
1.131779 +){
1.131780 +  MatchInfo *p = (MatchInfo *)pCtx;
1.131781 +  return sqlite3Fts3EvalPhraseStats(
1.131782 +      p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
1.131783 +  );
1.131784 +}
1.131785 +
1.131786 +/*
1.131787 +** fts3ExprIterate() callback used to collect the "local" part of the
1.131788 +** FTS3_MATCHINFO_HITS array. The local stats are those elements of the 
1.131789 +** array that are different for each row returned by the query.
1.131790 +*/
1.131791 +static int fts3ExprLocalHitsCb(
1.131792 +  Fts3Expr *pExpr,                /* Phrase expression node */
1.131793 +  int iPhrase,                    /* Phrase number */
1.131794 +  void *pCtx                      /* Pointer to MatchInfo structure */
1.131795 +){
1.131796 +  int rc = SQLITE_OK;
1.131797 +  MatchInfo *p = (MatchInfo *)pCtx;
1.131798 +  int iStart = iPhrase * p->nCol * 3;
1.131799 +  int i;
1.131800 +
1.131801 +  for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
1.131802 +    char *pCsr;
1.131803 +    rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
1.131804 +    if( pCsr ){
1.131805 +      p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
1.131806 +    }else{
1.131807 +      p->aMatchinfo[iStart+i*3] = 0;
1.131808 +    }
1.131809 +  }
1.131810 +
1.131811 +  return rc;
1.131812 +}
1.131813 +
1.131814 +static int fts3MatchinfoCheck(
1.131815 +  Fts3Table *pTab, 
1.131816 +  char cArg,
1.131817 +  char **pzErr
1.131818 +){
1.131819 +  if( (cArg==FTS3_MATCHINFO_NPHRASE)
1.131820 +   || (cArg==FTS3_MATCHINFO_NCOL)
1.131821 +   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
1.131822 +   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
1.131823 +   || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
1.131824 +   || (cArg==FTS3_MATCHINFO_LCS)
1.131825 +   || (cArg==FTS3_MATCHINFO_HITS)
1.131826 +  ){
1.131827 +    return SQLITE_OK;
1.131828 +  }
1.131829 +  *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
1.131830 +  return SQLITE_ERROR;
1.131831 +}
1.131832 +
1.131833 +static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
1.131834 +  int nVal;                       /* Number of integers output by cArg */
1.131835 +
1.131836 +  switch( cArg ){
1.131837 +    case FTS3_MATCHINFO_NDOC:
1.131838 +    case FTS3_MATCHINFO_NPHRASE: 
1.131839 +    case FTS3_MATCHINFO_NCOL: 
1.131840 +      nVal = 1;
1.131841 +      break;
1.131842 +
1.131843 +    case FTS3_MATCHINFO_AVGLENGTH:
1.131844 +    case FTS3_MATCHINFO_LENGTH:
1.131845 +    case FTS3_MATCHINFO_LCS:
1.131846 +      nVal = pInfo->nCol;
1.131847 +      break;
1.131848 +
1.131849 +    default:
1.131850 +      assert( cArg==FTS3_MATCHINFO_HITS );
1.131851 +      nVal = pInfo->nCol * pInfo->nPhrase * 3;
1.131852 +      break;
1.131853 +  }
1.131854 +
1.131855 +  return nVal;
1.131856 +}
1.131857 +
1.131858 +static int fts3MatchinfoSelectDoctotal(
1.131859 +  Fts3Table *pTab,
1.131860 +  sqlite3_stmt **ppStmt,
1.131861 +  sqlite3_int64 *pnDoc,
1.131862 +  const char **paLen
1.131863 +){
1.131864 +  sqlite3_stmt *pStmt;
1.131865 +  const char *a;
1.131866 +  sqlite3_int64 nDoc;
1.131867 +
1.131868 +  if( !*ppStmt ){
1.131869 +    int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
1.131870 +    if( rc!=SQLITE_OK ) return rc;
1.131871 +  }
1.131872 +  pStmt = *ppStmt;
1.131873 +  assert( sqlite3_data_count(pStmt)==1 );
1.131874 +
1.131875 +  a = sqlite3_column_blob(pStmt, 0);
1.131876 +  a += sqlite3Fts3GetVarint(a, &nDoc);
1.131877 +  if( nDoc==0 ) return FTS_CORRUPT_VTAB;
1.131878 +  *pnDoc = (u32)nDoc;
1.131879 +
1.131880 +  if( paLen ) *paLen = a;
1.131881 +  return SQLITE_OK;
1.131882 +}
1.131883 +
1.131884 +/*
1.131885 +** An instance of the following structure is used to store state while 
1.131886 +** iterating through a multi-column position-list corresponding to the
1.131887 +** hits for a single phrase on a single row in order to calculate the
1.131888 +** values for a matchinfo() FTS3_MATCHINFO_LCS request.
1.131889 +*/
1.131890 +typedef struct LcsIterator LcsIterator;
1.131891 +struct LcsIterator {
1.131892 +  Fts3Expr *pExpr;                /* Pointer to phrase expression */
1.131893 +  int iPosOffset;                 /* Tokens count up to end of this phrase */
1.131894 +  char *pRead;                    /* Cursor used to iterate through aDoclist */
1.131895 +  int iPos;                       /* Current position */
1.131896 +};
1.131897 +
1.131898 +/* 
1.131899 +** If LcsIterator.iCol is set to the following value, the iterator has
1.131900 +** finished iterating through all offsets for all columns.
1.131901 +*/
1.131902 +#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
1.131903 +
1.131904 +static int fts3MatchinfoLcsCb(
1.131905 +  Fts3Expr *pExpr,                /* Phrase expression node */
1.131906 +  int iPhrase,                    /* Phrase number (numbered from zero) */
1.131907 +  void *pCtx                      /* Pointer to MatchInfo structure */
1.131908 +){
1.131909 +  LcsIterator *aIter = (LcsIterator *)pCtx;
1.131910 +  aIter[iPhrase].pExpr = pExpr;
1.131911 +  return SQLITE_OK;
1.131912 +}
1.131913 +
1.131914 +/*
1.131915 +** Advance the iterator passed as an argument to the next position. Return
1.131916 +** 1 if the iterator is at EOF or if it now points to the start of the
1.131917 +** position list for the next column.
1.131918 +*/
1.131919 +static int fts3LcsIteratorAdvance(LcsIterator *pIter){
1.131920 +  char *pRead = pIter->pRead;
1.131921 +  sqlite3_int64 iRead;
1.131922 +  int rc = 0;
1.131923 +
1.131924 +  pRead += sqlite3Fts3GetVarint(pRead, &iRead);
1.131925 +  if( iRead==0 || iRead==1 ){
1.131926 +    pRead = 0;
1.131927 +    rc = 1;
1.131928 +  }else{
1.131929 +    pIter->iPos += (int)(iRead-2);
1.131930 +  }
1.131931 +
1.131932 +  pIter->pRead = pRead;
1.131933 +  return rc;
1.131934 +}
1.131935 +  
1.131936 +/*
1.131937 +** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. 
1.131938 +**
1.131939 +** If the call is successful, the longest-common-substring lengths for each
1.131940 +** column are written into the first nCol elements of the pInfo->aMatchinfo[] 
1.131941 +** array before returning. SQLITE_OK is returned in this case.
1.131942 +**
1.131943 +** Otherwise, if an error occurs, an SQLite error code is returned and the
1.131944 +** data written to the first nCol elements of pInfo->aMatchinfo[] is 
1.131945 +** undefined.
1.131946 +*/
1.131947 +static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
1.131948 +  LcsIterator *aIter;
1.131949 +  int i;
1.131950 +  int iCol;
1.131951 +  int nToken = 0;
1.131952 +
1.131953 +  /* Allocate and populate the array of LcsIterator objects. The array
1.131954 +  ** contains one element for each matchable phrase in the query.
1.131955 +  **/
1.131956 +  aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
1.131957 +  if( !aIter ) return SQLITE_NOMEM;
1.131958 +  memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
1.131959 +  (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
1.131960 +
1.131961 +  for(i=0; i<pInfo->nPhrase; i++){
1.131962 +    LcsIterator *pIter = &aIter[i];
1.131963 +    nToken -= pIter->pExpr->pPhrase->nToken;
1.131964 +    pIter->iPosOffset = nToken;
1.131965 +  }
1.131966 +
1.131967 +  for(iCol=0; iCol<pInfo->nCol; iCol++){
1.131968 +    int nLcs = 0;                 /* LCS value for this column */
1.131969 +    int nLive = 0;                /* Number of iterators in aIter not at EOF */
1.131970 +
1.131971 +    for(i=0; i<pInfo->nPhrase; i++){
1.131972 +      int rc;
1.131973 +      LcsIterator *pIt = &aIter[i];
1.131974 +      rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
1.131975 +      if( rc!=SQLITE_OK ) return rc;
1.131976 +      if( pIt->pRead ){
1.131977 +        pIt->iPos = pIt->iPosOffset;
1.131978 +        fts3LcsIteratorAdvance(&aIter[i]);
1.131979 +        nLive++;
1.131980 +      }
1.131981 +    }
1.131982 +
1.131983 +    while( nLive>0 ){
1.131984 +      LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
1.131985 +      int nThisLcs = 0;           /* LCS for the current iterator positions */
1.131986 +
1.131987 +      for(i=0; i<pInfo->nPhrase; i++){
1.131988 +        LcsIterator *pIter = &aIter[i];
1.131989 +        if( pIter->pRead==0 ){
1.131990 +          /* This iterator is already at EOF for this column. */
1.131991 +          nThisLcs = 0;
1.131992 +        }else{
1.131993 +          if( pAdv==0 || pIter->iPos<pAdv->iPos ){
1.131994 +            pAdv = pIter;
1.131995 +          }
1.131996 +          if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
1.131997 +            nThisLcs++;
1.131998 +          }else{
1.131999 +            nThisLcs = 1;
1.132000 +          }
1.132001 +          if( nThisLcs>nLcs ) nLcs = nThisLcs;
1.132002 +        }
1.132003 +      }
1.132004 +      if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
1.132005 +    }
1.132006 +
1.132007 +    pInfo->aMatchinfo[iCol] = nLcs;
1.132008 +  }
1.132009 +
1.132010 +  sqlite3_free(aIter);
1.132011 +  return SQLITE_OK;
1.132012 +}
1.132013 +
1.132014 +/*
1.132015 +** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
1.132016 +** be returned by the matchinfo() function. Argument zArg contains the 
1.132017 +** format string passed as the second argument to matchinfo (or the
1.132018 +** default value "pcx" if no second argument was specified). The format
1.132019 +** string has already been validated and the pInfo->aMatchinfo[] array
1.132020 +** is guaranteed to be large enough for the output.
1.132021 +**
1.132022 +** If bGlobal is true, then populate all fields of the matchinfo() output.
1.132023 +** If it is false, then assume that those fields that do not change between
1.132024 +** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
1.132025 +** have already been populated.
1.132026 +**
1.132027 +** Return SQLITE_OK if successful, or an SQLite error code if an error 
1.132028 +** occurs. If a value other than SQLITE_OK is returned, the state the
1.132029 +** pInfo->aMatchinfo[] buffer is left in is undefined.
1.132030 +*/
1.132031 +static int fts3MatchinfoValues(
1.132032 +  Fts3Cursor *pCsr,               /* FTS3 cursor object */
1.132033 +  int bGlobal,                    /* True to grab the global stats */
1.132034 +  MatchInfo *pInfo,               /* Matchinfo context object */
1.132035 +  const char *zArg                /* Matchinfo format string */
1.132036 +){
1.132037 +  int rc = SQLITE_OK;
1.132038 +  int i;
1.132039 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.132040 +  sqlite3_stmt *pSelect = 0;
1.132041 +
1.132042 +  for(i=0; rc==SQLITE_OK && zArg[i]; i++){
1.132043 +
1.132044 +    switch( zArg[i] ){
1.132045 +      case FTS3_MATCHINFO_NPHRASE:
1.132046 +        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
1.132047 +        break;
1.132048 +
1.132049 +      case FTS3_MATCHINFO_NCOL:
1.132050 +        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
1.132051 +        break;
1.132052 +        
1.132053 +      case FTS3_MATCHINFO_NDOC:
1.132054 +        if( bGlobal ){
1.132055 +          sqlite3_int64 nDoc = 0;
1.132056 +          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
1.132057 +          pInfo->aMatchinfo[0] = (u32)nDoc;
1.132058 +        }
1.132059 +        break;
1.132060 +
1.132061 +      case FTS3_MATCHINFO_AVGLENGTH: 
1.132062 +        if( bGlobal ){
1.132063 +          sqlite3_int64 nDoc;     /* Number of rows in table */
1.132064 +          const char *a;          /* Aggregate column length array */
1.132065 +
1.132066 +          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
1.132067 +          if( rc==SQLITE_OK ){
1.132068 +            int iCol;
1.132069 +            for(iCol=0; iCol<pInfo->nCol; iCol++){
1.132070 +              u32 iVal;
1.132071 +              sqlite3_int64 nToken;
1.132072 +              a += sqlite3Fts3GetVarint(a, &nToken);
1.132073 +              iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
1.132074 +              pInfo->aMatchinfo[iCol] = iVal;
1.132075 +            }
1.132076 +          }
1.132077 +        }
1.132078 +        break;
1.132079 +
1.132080 +      case FTS3_MATCHINFO_LENGTH: {
1.132081 +        sqlite3_stmt *pSelectDocsize = 0;
1.132082 +        rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
1.132083 +        if( rc==SQLITE_OK ){
1.132084 +          int iCol;
1.132085 +          const char *a = sqlite3_column_blob(pSelectDocsize, 0);
1.132086 +          for(iCol=0; iCol<pInfo->nCol; iCol++){
1.132087 +            sqlite3_int64 nToken;
1.132088 +            a += sqlite3Fts3GetVarint(a, &nToken);
1.132089 +            pInfo->aMatchinfo[iCol] = (u32)nToken;
1.132090 +          }
1.132091 +        }
1.132092 +        sqlite3_reset(pSelectDocsize);
1.132093 +        break;
1.132094 +      }
1.132095 +
1.132096 +      case FTS3_MATCHINFO_LCS:
1.132097 +        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
1.132098 +        if( rc==SQLITE_OK ){
1.132099 +          rc = fts3MatchinfoLcs(pCsr, pInfo);
1.132100 +        }
1.132101 +        break;
1.132102 +
1.132103 +      default: {
1.132104 +        Fts3Expr *pExpr;
1.132105 +        assert( zArg[i]==FTS3_MATCHINFO_HITS );
1.132106 +        pExpr = pCsr->pExpr;
1.132107 +        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
1.132108 +        if( rc!=SQLITE_OK ) break;
1.132109 +        if( bGlobal ){
1.132110 +          if( pCsr->pDeferred ){
1.132111 +            rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
1.132112 +            if( rc!=SQLITE_OK ) break;
1.132113 +          }
1.132114 +          rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
1.132115 +          if( rc!=SQLITE_OK ) break;
1.132116 +        }
1.132117 +        (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
1.132118 +        break;
1.132119 +      }
1.132120 +    }
1.132121 +
1.132122 +    pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
1.132123 +  }
1.132124 +
1.132125 +  sqlite3_reset(pSelect);
1.132126 +  return rc;
1.132127 +}
1.132128 +
1.132129 +
1.132130 +/*
1.132131 +** Populate pCsr->aMatchinfo[] with data for the current row. The 
1.132132 +** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
1.132133 +*/
1.132134 +static int fts3GetMatchinfo(
1.132135 +  Fts3Cursor *pCsr,               /* FTS3 Cursor object */
1.132136 +  const char *zArg                /* Second argument to matchinfo() function */
1.132137 +){
1.132138 +  MatchInfo sInfo;
1.132139 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.132140 +  int rc = SQLITE_OK;
1.132141 +  int bGlobal = 0;                /* Collect 'global' stats as well as local */
1.132142 +
1.132143 +  memset(&sInfo, 0, sizeof(MatchInfo));
1.132144 +  sInfo.pCursor = pCsr;
1.132145 +  sInfo.nCol = pTab->nColumn;
1.132146 +
1.132147 +  /* If there is cached matchinfo() data, but the format string for the 
1.132148 +  ** cache does not match the format string for this request, discard 
1.132149 +  ** the cached data. */
1.132150 +  if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
1.132151 +    assert( pCsr->aMatchinfo );
1.132152 +    sqlite3_free(pCsr->aMatchinfo);
1.132153 +    pCsr->zMatchinfo = 0;
1.132154 +    pCsr->aMatchinfo = 0;
1.132155 +  }
1.132156 +
1.132157 +  /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
1.132158 +  ** matchinfo function has been called for this query. In this case 
1.132159 +  ** allocate the array used to accumulate the matchinfo data and
1.132160 +  ** initialize those elements that are constant for every row.
1.132161 +  */
1.132162 +  if( pCsr->aMatchinfo==0 ){
1.132163 +    int nMatchinfo = 0;           /* Number of u32 elements in match-info */
1.132164 +    int nArg;                     /* Bytes in zArg */
1.132165 +    int i;                        /* Used to iterate through zArg */
1.132166 +
1.132167 +    /* Determine the number of phrases in the query */
1.132168 +    pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
1.132169 +    sInfo.nPhrase = pCsr->nPhrase;
1.132170 +
1.132171 +    /* Determine the number of integers in the buffer returned by this call. */
1.132172 +    for(i=0; zArg[i]; i++){
1.132173 +      nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
1.132174 +    }
1.132175 +
1.132176 +    /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
1.132177 +    nArg = (int)strlen(zArg);
1.132178 +    pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
1.132179 +    if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
1.132180 +
1.132181 +    pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
1.132182 +    pCsr->nMatchinfo = nMatchinfo;
1.132183 +    memcpy(pCsr->zMatchinfo, zArg, nArg+1);
1.132184 +    memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
1.132185 +    pCsr->isMatchinfoNeeded = 1;
1.132186 +    bGlobal = 1;
1.132187 +  }
1.132188 +
1.132189 +  sInfo.aMatchinfo = pCsr->aMatchinfo;
1.132190 +  sInfo.nPhrase = pCsr->nPhrase;
1.132191 +  if( pCsr->isMatchinfoNeeded ){
1.132192 +    rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
1.132193 +    pCsr->isMatchinfoNeeded = 0;
1.132194 +  }
1.132195 +
1.132196 +  return rc;
1.132197 +}
1.132198 +
1.132199 +/*
1.132200 +** Implementation of snippet() function.
1.132201 +*/
1.132202 +SQLITE_PRIVATE void sqlite3Fts3Snippet(
1.132203 +  sqlite3_context *pCtx,          /* SQLite function call context */
1.132204 +  Fts3Cursor *pCsr,               /* Cursor object */
1.132205 +  const char *zStart,             /* Snippet start text - "<b>" */
1.132206 +  const char *zEnd,               /* Snippet end text - "</b>" */
1.132207 +  const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
1.132208 +  int iCol,                       /* Extract snippet from this column */
1.132209 +  int nToken                      /* Approximate number of tokens in snippet */
1.132210 +){
1.132211 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.132212 +  int rc = SQLITE_OK;
1.132213 +  int i;
1.132214 +  StrBuffer res = {0, 0, 0};
1.132215 +
1.132216 +  /* The returned text includes up to four fragments of text extracted from
1.132217 +  ** the data in the current row. The first iteration of the for(...) loop
1.132218 +  ** below attempts to locate a single fragment of text nToken tokens in 
1.132219 +  ** size that contains at least one instance of all phrases in the query
1.132220 +  ** expression that appear in the current row. If such a fragment of text
1.132221 +  ** cannot be found, the second iteration of the loop attempts to locate
1.132222 +  ** a pair of fragments, and so on.
1.132223 +  */
1.132224 +  int nSnippet = 0;               /* Number of fragments in this snippet */
1.132225 +  SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
1.132226 +  int nFToken = -1;               /* Number of tokens in each fragment */
1.132227 +
1.132228 +  if( !pCsr->pExpr ){
1.132229 +    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
1.132230 +    return;
1.132231 +  }
1.132232 +
1.132233 +  for(nSnippet=1; 1; nSnippet++){
1.132234 +
1.132235 +    int iSnip;                    /* Loop counter 0..nSnippet-1 */
1.132236 +    u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
1.132237 +    u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
1.132238 +
1.132239 +    if( nToken>=0 ){
1.132240 +      nFToken = (nToken+nSnippet-1) / nSnippet;
1.132241 +    }else{
1.132242 +      nFToken = -1 * nToken;
1.132243 +    }
1.132244 +
1.132245 +    for(iSnip=0; iSnip<nSnippet; iSnip++){
1.132246 +      int iBestScore = -1;        /* Best score of columns checked so far */
1.132247 +      int iRead;                  /* Used to iterate through columns */
1.132248 +      SnippetFragment *pFragment = &aSnippet[iSnip];
1.132249 +
1.132250 +      memset(pFragment, 0, sizeof(*pFragment));
1.132251 +
1.132252 +      /* Loop through all columns of the table being considered for snippets.
1.132253 +      ** If the iCol argument to this function was negative, this means all
1.132254 +      ** columns of the FTS3 table. Otherwise, only column iCol is considered.
1.132255 +      */
1.132256 +      for(iRead=0; iRead<pTab->nColumn; iRead++){
1.132257 +        SnippetFragment sF = {0, 0, 0, 0};
1.132258 +        int iS;
1.132259 +        if( iCol>=0 && iRead!=iCol ) continue;
1.132260 +
1.132261 +        /* Find the best snippet of nFToken tokens in column iRead. */
1.132262 +        rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
1.132263 +        if( rc!=SQLITE_OK ){
1.132264 +          goto snippet_out;
1.132265 +        }
1.132266 +        if( iS>iBestScore ){
1.132267 +          *pFragment = sF;
1.132268 +          iBestScore = iS;
1.132269 +        }
1.132270 +      }
1.132271 +
1.132272 +      mCovered |= pFragment->covered;
1.132273 +    }
1.132274 +
1.132275 +    /* If all query phrases seen by fts3BestSnippet() are present in at least
1.132276 +    ** one of the nSnippet snippet fragments, break out of the loop.
1.132277 +    */
1.132278 +    assert( (mCovered&mSeen)==mCovered );
1.132279 +    if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
1.132280 +  }
1.132281 +
1.132282 +  assert( nFToken>0 );
1.132283 +
1.132284 +  for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
1.132285 +    rc = fts3SnippetText(pCsr, &aSnippet[i], 
1.132286 +        i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
1.132287 +    );
1.132288 +  }
1.132289 +
1.132290 + snippet_out:
1.132291 +  sqlite3Fts3SegmentsClose(pTab);
1.132292 +  if( rc!=SQLITE_OK ){
1.132293 +    sqlite3_result_error_code(pCtx, rc);
1.132294 +    sqlite3_free(res.z);
1.132295 +  }else{
1.132296 +    sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
1.132297 +  }
1.132298 +}
1.132299 +
1.132300 +
1.132301 +typedef struct TermOffset TermOffset;
1.132302 +typedef struct TermOffsetCtx TermOffsetCtx;
1.132303 +
1.132304 +struct TermOffset {
1.132305 +  char *pList;                    /* Position-list */
1.132306 +  int iPos;                       /* Position just read from pList */
1.132307 +  int iOff;                       /* Offset of this term from read positions */
1.132308 +};
1.132309 +
1.132310 +struct TermOffsetCtx {
1.132311 +  Fts3Cursor *pCsr;
1.132312 +  int iCol;                       /* Column of table to populate aTerm for */
1.132313 +  int iTerm;
1.132314 +  sqlite3_int64 iDocid;
1.132315 +  TermOffset *aTerm;
1.132316 +};
1.132317 +
1.132318 +/*
1.132319 +** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
1.132320 +*/
1.132321 +static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
1.132322 +  TermOffsetCtx *p = (TermOffsetCtx *)ctx;
1.132323 +  int nTerm;                      /* Number of tokens in phrase */
1.132324 +  int iTerm;                      /* For looping through nTerm phrase terms */
1.132325 +  char *pList;                    /* Pointer to position list for phrase */
1.132326 +  int iPos = 0;                   /* First position in position-list */
1.132327 +  int rc;
1.132328 +
1.132329 +  UNUSED_PARAMETER(iPhrase);
1.132330 +  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
1.132331 +  nTerm = pExpr->pPhrase->nToken;
1.132332 +  if( pList ){
1.132333 +    fts3GetDeltaPosition(&pList, &iPos);
1.132334 +    assert( iPos>=0 );
1.132335 +  }
1.132336 +
1.132337 +  for(iTerm=0; iTerm<nTerm; iTerm++){
1.132338 +    TermOffset *pT = &p->aTerm[p->iTerm++];
1.132339 +    pT->iOff = nTerm-iTerm-1;
1.132340 +    pT->pList = pList;
1.132341 +    pT->iPos = iPos;
1.132342 +  }
1.132343 +
1.132344 +  return rc;
1.132345 +}
1.132346 +
1.132347 +/*
1.132348 +** Implementation of offsets() function.
1.132349 +*/
1.132350 +SQLITE_PRIVATE void sqlite3Fts3Offsets(
1.132351 +  sqlite3_context *pCtx,          /* SQLite function call context */
1.132352 +  Fts3Cursor *pCsr                /* Cursor object */
1.132353 +){
1.132354 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.132355 +  sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
1.132356 +  int rc;                         /* Return Code */
1.132357 +  int nToken;                     /* Number of tokens in query */
1.132358 +  int iCol;                       /* Column currently being processed */
1.132359 +  StrBuffer res = {0, 0, 0};      /* Result string */
1.132360 +  TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
1.132361 +
1.132362 +  if( !pCsr->pExpr ){
1.132363 +    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
1.132364 +    return;
1.132365 +  }
1.132366 +
1.132367 +  memset(&sCtx, 0, sizeof(sCtx));
1.132368 +  assert( pCsr->isRequireSeek==0 );
1.132369 +
1.132370 +  /* Count the number of terms in the query */
1.132371 +  rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
1.132372 +  if( rc!=SQLITE_OK ) goto offsets_out;
1.132373 +
1.132374 +  /* Allocate the array of TermOffset iterators. */
1.132375 +  sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
1.132376 +  if( 0==sCtx.aTerm ){
1.132377 +    rc = SQLITE_NOMEM;
1.132378 +    goto offsets_out;
1.132379 +  }
1.132380 +  sCtx.iDocid = pCsr->iPrevId;
1.132381 +  sCtx.pCsr = pCsr;
1.132382 +
1.132383 +  /* Loop through the table columns, appending offset information to 
1.132384 +  ** string-buffer res for each column.
1.132385 +  */
1.132386 +  for(iCol=0; iCol<pTab->nColumn; iCol++){
1.132387 +    sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
1.132388 +    const char *ZDUMMY;           /* Dummy argument used with xNext() */
1.132389 +    int NDUMMY = 0;               /* Dummy argument used with xNext() */
1.132390 +    int iStart = 0;
1.132391 +    int iEnd = 0;
1.132392 +    int iCurrent = 0;
1.132393 +    const char *zDoc;
1.132394 +    int nDoc;
1.132395 +
1.132396 +    /* Initialize the contents of sCtx.aTerm[] for column iCol. There is 
1.132397 +    ** no way that this operation can fail, so the return code from
1.132398 +    ** fts3ExprIterate() can be discarded.
1.132399 +    */
1.132400 +    sCtx.iCol = iCol;
1.132401 +    sCtx.iTerm = 0;
1.132402 +    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
1.132403 +
1.132404 +    /* Retreive the text stored in column iCol. If an SQL NULL is stored 
1.132405 +    ** in column iCol, jump immediately to the next iteration of the loop.
1.132406 +    ** If an OOM occurs while retrieving the data (this can happen if SQLite
1.132407 +    ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM 
1.132408 +    ** to the caller. 
1.132409 +    */
1.132410 +    zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
1.132411 +    nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
1.132412 +    if( zDoc==0 ){
1.132413 +      if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
1.132414 +        continue;
1.132415 +      }
1.132416 +      rc = SQLITE_NOMEM;
1.132417 +      goto offsets_out;
1.132418 +    }
1.132419 +
1.132420 +    /* Initialize a tokenizer iterator to iterate through column iCol. */
1.132421 +    rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
1.132422 +        zDoc, nDoc, &pC
1.132423 +    );
1.132424 +    if( rc!=SQLITE_OK ) goto offsets_out;
1.132425 +
1.132426 +    rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
1.132427 +    while( rc==SQLITE_OK ){
1.132428 +      int i;                      /* Used to loop through terms */
1.132429 +      int iMinPos = 0x7FFFFFFF;   /* Position of next token */
1.132430 +      TermOffset *pTerm = 0;      /* TermOffset associated with next token */
1.132431 +
1.132432 +      for(i=0; i<nToken; i++){
1.132433 +        TermOffset *pT = &sCtx.aTerm[i];
1.132434 +        if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
1.132435 +          iMinPos = pT->iPos-pT->iOff;
1.132436 +          pTerm = pT;
1.132437 +        }
1.132438 +      }
1.132439 +
1.132440 +      if( !pTerm ){
1.132441 +        /* All offsets for this column have been gathered. */
1.132442 +        rc = SQLITE_DONE;
1.132443 +      }else{
1.132444 +        assert( iCurrent<=iMinPos );
1.132445 +        if( 0==(0xFE&*pTerm->pList) ){
1.132446 +          pTerm->pList = 0;
1.132447 +        }else{
1.132448 +          fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
1.132449 +        }
1.132450 +        while( rc==SQLITE_OK && iCurrent<iMinPos ){
1.132451 +          rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
1.132452 +        }
1.132453 +        if( rc==SQLITE_OK ){
1.132454 +          char aBuffer[64];
1.132455 +          sqlite3_snprintf(sizeof(aBuffer), aBuffer, 
1.132456 +              "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
1.132457 +          );
1.132458 +          rc = fts3StringAppend(&res, aBuffer, -1);
1.132459 +        }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
1.132460 +          rc = FTS_CORRUPT_VTAB;
1.132461 +        }
1.132462 +      }
1.132463 +    }
1.132464 +    if( rc==SQLITE_DONE ){
1.132465 +      rc = SQLITE_OK;
1.132466 +    }
1.132467 +
1.132468 +    pMod->xClose(pC);
1.132469 +    if( rc!=SQLITE_OK ) goto offsets_out;
1.132470 +  }
1.132471 +
1.132472 + offsets_out:
1.132473 +  sqlite3_free(sCtx.aTerm);
1.132474 +  assert( rc!=SQLITE_DONE );
1.132475 +  sqlite3Fts3SegmentsClose(pTab);
1.132476 +  if( rc!=SQLITE_OK ){
1.132477 +    sqlite3_result_error_code(pCtx,  rc);
1.132478 +    sqlite3_free(res.z);
1.132479 +  }else{
1.132480 +    sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
1.132481 +  }
1.132482 +  return;
1.132483 +}
1.132484 +
1.132485 +/*
1.132486 +** Implementation of matchinfo() function.
1.132487 +*/
1.132488 +SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
1.132489 +  sqlite3_context *pContext,      /* Function call context */
1.132490 +  Fts3Cursor *pCsr,               /* FTS3 table cursor */
1.132491 +  const char *zArg                /* Second arg to matchinfo() function */
1.132492 +){
1.132493 +  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
1.132494 +  int rc;
1.132495 +  int i;
1.132496 +  const char *zFormat;
1.132497 +
1.132498 +  if( zArg ){
1.132499 +    for(i=0; zArg[i]; i++){
1.132500 +      char *zErr = 0;
1.132501 +      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
1.132502 +        sqlite3_result_error(pContext, zErr, -1);
1.132503 +        sqlite3_free(zErr);
1.132504 +        return;
1.132505 +      }
1.132506 +    }
1.132507 +    zFormat = zArg;
1.132508 +  }else{
1.132509 +    zFormat = FTS3_MATCHINFO_DEFAULT;
1.132510 +  }
1.132511 +
1.132512 +  if( !pCsr->pExpr ){
1.132513 +    sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
1.132514 +    return;
1.132515 +  }
1.132516 +
1.132517 +  /* Retrieve matchinfo() data. */
1.132518 +  rc = fts3GetMatchinfo(pCsr, zFormat);
1.132519 +  sqlite3Fts3SegmentsClose(pTab);
1.132520 +
1.132521 +  if( rc!=SQLITE_OK ){
1.132522 +    sqlite3_result_error_code(pContext, rc);
1.132523 +  }else{
1.132524 +    int n = pCsr->nMatchinfo * sizeof(u32);
1.132525 +    sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
1.132526 +  }
1.132527 +}
1.132528 +
1.132529 +#endif
1.132530 +
1.132531 +/************** End of fts3_snippet.c ****************************************/
1.132532 +/************** Begin file fts3_unicode.c ************************************/
1.132533 +/*
1.132534 +** 2012 May 24
1.132535 +**
1.132536 +** The author disclaims copyright to this source code.  In place of
1.132537 +** a legal notice, here is a blessing:
1.132538 +**
1.132539 +**    May you do good and not evil.
1.132540 +**    May you find forgiveness for yourself and forgive others.
1.132541 +**    May you share freely, never taking more than you give.
1.132542 +**
1.132543 +******************************************************************************
1.132544 +**
1.132545 +** Implementation of the "unicode" full-text-search tokenizer.
1.132546 +*/
1.132547 +
1.132548 +#ifdef SQLITE_ENABLE_FTS4_UNICODE61
1.132549 +
1.132550 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.132551 +
1.132552 +/* #include <assert.h> */
1.132553 +/* #include <stdlib.h> */
1.132554 +/* #include <stdio.h> */
1.132555 +/* #include <string.h> */
1.132556 +
1.132557 +
1.132558 +/*
1.132559 +** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
1.132560 +** from the sqlite3 source file utf.c. If this file is compiled as part
1.132561 +** of the amalgamation, they are not required.
1.132562 +*/
1.132563 +#ifndef SQLITE_AMALGAMATION
1.132564 +
1.132565 +static const unsigned char sqlite3Utf8Trans1[] = {
1.132566 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1.132567 +  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1.132568 +  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1.132569 +  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1.132570 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1.132571 +  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1.132572 +  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1.132573 +  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
1.132574 +};
1.132575 +
1.132576 +#define READ_UTF8(zIn, zTerm, c)                           \
1.132577 +  c = *(zIn++);                                            \
1.132578 +  if( c>=0xc0 ){                                           \
1.132579 +    c = sqlite3Utf8Trans1[c-0xc0];                         \
1.132580 +    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
1.132581 +      c = (c<<6) + (0x3f & *(zIn++));                      \
1.132582 +    }                                                      \
1.132583 +    if( c<0x80                                             \
1.132584 +        || (c&0xFFFFF800)==0xD800                          \
1.132585 +        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
1.132586 +  }
1.132587 +
1.132588 +#define WRITE_UTF8(zOut, c) {                          \
1.132589 +  if( c<0x00080 ){                                     \
1.132590 +    *zOut++ = (u8)(c&0xFF);                            \
1.132591 +  }                                                    \
1.132592 +  else if( c<0x00800 ){                                \
1.132593 +    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
1.132594 +    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
1.132595 +  }                                                    \
1.132596 +  else if( c<0x10000 ){                                \
1.132597 +    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
1.132598 +    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
1.132599 +    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
1.132600 +  }else{                                               \
1.132601 +    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
1.132602 +    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
1.132603 +    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
1.132604 +    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
1.132605 +  }                                                    \
1.132606 +}
1.132607 +
1.132608 +#endif /* ifndef SQLITE_AMALGAMATION */
1.132609 +
1.132610 +typedef struct unicode_tokenizer unicode_tokenizer;
1.132611 +typedef struct unicode_cursor unicode_cursor;
1.132612 +
1.132613 +struct unicode_tokenizer {
1.132614 +  sqlite3_tokenizer base;
1.132615 +  int bRemoveDiacritic;
1.132616 +  int nException;
1.132617 +  int *aiException;
1.132618 +};
1.132619 +
1.132620 +struct unicode_cursor {
1.132621 +  sqlite3_tokenizer_cursor base;
1.132622 +  const unsigned char *aInput;    /* Input text being tokenized */
1.132623 +  int nInput;                     /* Size of aInput[] in bytes */
1.132624 +  int iOff;                       /* Current offset within aInput[] */
1.132625 +  int iToken;                     /* Index of next token to be returned */
1.132626 +  char *zToken;                   /* storage for current token */
1.132627 +  int nAlloc;                     /* space allocated at zToken */
1.132628 +};
1.132629 +
1.132630 +
1.132631 +/*
1.132632 +** Destroy a tokenizer allocated by unicodeCreate().
1.132633 +*/
1.132634 +static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
1.132635 +  if( pTokenizer ){
1.132636 +    unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
1.132637 +    sqlite3_free(p->aiException);
1.132638 +    sqlite3_free(p);
1.132639 +  }
1.132640 +  return SQLITE_OK;
1.132641 +}
1.132642 +
1.132643 +/*
1.132644 +** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
1.132645 +** statement has specified that the tokenizer for this table shall consider
1.132646 +** all characters in string zIn/nIn to be separators (if bAlnum==0) or
1.132647 +** token characters (if bAlnum==1).
1.132648 +**
1.132649 +** For each codepoint in the zIn/nIn string, this function checks if the
1.132650 +** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
1.132651 +** If so, no action is taken. Otherwise, the codepoint is added to the 
1.132652 +** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
1.132653 +** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
1.132654 +** codepoints in the aiException[] array.
1.132655 +**
1.132656 +** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
1.132657 +** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
1.132658 +** It is not possible to change the behaviour of the tokenizer with respect
1.132659 +** to these codepoints.
1.132660 +*/
1.132661 +static int unicodeAddExceptions(
1.132662 +  unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
1.132663 +  int bAlnum,                     /* Replace Isalnum() return value with this */
1.132664 +  const char *zIn,                /* Array of characters to make exceptions */
1.132665 +  int nIn                         /* Length of z in bytes */
1.132666 +){
1.132667 +  const unsigned char *z = (const unsigned char *)zIn;
1.132668 +  const unsigned char *zTerm = &z[nIn];
1.132669 +  int iCode;
1.132670 +  int nEntry = 0;
1.132671 +
1.132672 +  assert( bAlnum==0 || bAlnum==1 );
1.132673 +
1.132674 +  while( z<zTerm ){
1.132675 +    READ_UTF8(z, zTerm, iCode);
1.132676 +    assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
1.132677 +    if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
1.132678 +     && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
1.132679 +    ){
1.132680 +      nEntry++;
1.132681 +    }
1.132682 +  }
1.132683 +
1.132684 +  if( nEntry ){
1.132685 +    int *aNew;                    /* New aiException[] array */
1.132686 +    int nNew;                     /* Number of valid entries in array aNew[] */
1.132687 +
1.132688 +    aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
1.132689 +    if( aNew==0 ) return SQLITE_NOMEM;
1.132690 +    nNew = p->nException;
1.132691 +
1.132692 +    z = (const unsigned char *)zIn;
1.132693 +    while( z<zTerm ){
1.132694 +      READ_UTF8(z, zTerm, iCode);
1.132695 +      if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
1.132696 +       && sqlite3FtsUnicodeIsdiacritic(iCode)==0
1.132697 +      ){
1.132698 +        int i, j;
1.132699 +        for(i=0; i<nNew && aNew[i]<iCode; i++);
1.132700 +        for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
1.132701 +        aNew[i] = iCode;
1.132702 +        nNew++;
1.132703 +      }
1.132704 +    }
1.132705 +    p->aiException = aNew;
1.132706 +    p->nException = nNew;
1.132707 +  }
1.132708 +
1.132709 +  return SQLITE_OK;
1.132710 +}
1.132711 +
1.132712 +/*
1.132713 +** Return true if the p->aiException[] array contains the value iCode.
1.132714 +*/
1.132715 +static int unicodeIsException(unicode_tokenizer *p, int iCode){
1.132716 +  if( p->nException>0 ){
1.132717 +    int *a = p->aiException;
1.132718 +    int iLo = 0;
1.132719 +    int iHi = p->nException-1;
1.132720 +
1.132721 +    while( iHi>=iLo ){
1.132722 +      int iTest = (iHi + iLo) / 2;
1.132723 +      if( iCode==a[iTest] ){
1.132724 +        return 1;
1.132725 +      }else if( iCode>a[iTest] ){
1.132726 +        iLo = iTest+1;
1.132727 +      }else{
1.132728 +        iHi = iTest-1;
1.132729 +      }
1.132730 +    }
1.132731 +  }
1.132732 +
1.132733 +  return 0;
1.132734 +}
1.132735 +
1.132736 +/*
1.132737 +** Return true if, for the purposes of tokenization, codepoint iCode is
1.132738 +** considered a token character (not a separator).
1.132739 +*/
1.132740 +static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
1.132741 +  assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
1.132742 +  return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
1.132743 +}
1.132744 +
1.132745 +/*
1.132746 +** Create a new tokenizer instance.
1.132747 +*/
1.132748 +static int unicodeCreate(
1.132749 +  int nArg,                       /* Size of array argv[] */
1.132750 +  const char * const *azArg,      /* Tokenizer creation arguments */
1.132751 +  sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
1.132752 +){
1.132753 +  unicode_tokenizer *pNew;        /* New tokenizer object */
1.132754 +  int i;
1.132755 +  int rc = SQLITE_OK;
1.132756 +
1.132757 +  pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
1.132758 +  if( pNew==NULL ) return SQLITE_NOMEM;
1.132759 +  memset(pNew, 0, sizeof(unicode_tokenizer));
1.132760 +  pNew->bRemoveDiacritic = 1;
1.132761 +
1.132762 +  for(i=0; rc==SQLITE_OK && i<nArg; i++){
1.132763 +    const char *z = azArg[i];
1.132764 +    int n = strlen(z);
1.132765 +
1.132766 +    if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
1.132767 +      pNew->bRemoveDiacritic = 1;
1.132768 +    }
1.132769 +    else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
1.132770 +      pNew->bRemoveDiacritic = 0;
1.132771 +    }
1.132772 +    else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
1.132773 +      rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
1.132774 +    }
1.132775 +    else if( n>=11 && memcmp("separators=", z, 11)==0 ){
1.132776 +      rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
1.132777 +    }
1.132778 +    else{
1.132779 +      /* Unrecognized argument */
1.132780 +      rc  = SQLITE_ERROR;
1.132781 +    }
1.132782 +  }
1.132783 +
1.132784 +  if( rc!=SQLITE_OK ){
1.132785 +    unicodeDestroy((sqlite3_tokenizer *)pNew);
1.132786 +    pNew = 0;
1.132787 +  }
1.132788 +  *pp = (sqlite3_tokenizer *)pNew;
1.132789 +  return rc;
1.132790 +}
1.132791 +
1.132792 +/*
1.132793 +** Prepare to begin tokenizing a particular string.  The input
1.132794 +** string to be tokenized is pInput[0..nBytes-1].  A cursor
1.132795 +** used to incrementally tokenize this string is returned in 
1.132796 +** *ppCursor.
1.132797 +*/
1.132798 +static int unicodeOpen(
1.132799 +  sqlite3_tokenizer *p,           /* The tokenizer */
1.132800 +  const char *aInput,             /* Input string */
1.132801 +  int nInput,                     /* Size of string aInput in bytes */
1.132802 +  sqlite3_tokenizer_cursor **pp   /* OUT: New cursor object */
1.132803 +){
1.132804 +  unicode_cursor *pCsr;
1.132805 +
1.132806 +  pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
1.132807 +  if( pCsr==0 ){
1.132808 +    return SQLITE_NOMEM;
1.132809 +  }
1.132810 +  memset(pCsr, 0, sizeof(unicode_cursor));
1.132811 +
1.132812 +  pCsr->aInput = (const unsigned char *)aInput;
1.132813 +  if( aInput==0 ){
1.132814 +    pCsr->nInput = 0;
1.132815 +  }else if( nInput<0 ){
1.132816 +    pCsr->nInput = (int)strlen(aInput);
1.132817 +  }else{
1.132818 +    pCsr->nInput = nInput;
1.132819 +  }
1.132820 +
1.132821 +  *pp = &pCsr->base;
1.132822 +  UNUSED_PARAMETER(p);
1.132823 +  return SQLITE_OK;
1.132824 +}
1.132825 +
1.132826 +/*
1.132827 +** Close a tokenization cursor previously opened by a call to
1.132828 +** simpleOpen() above.
1.132829 +*/
1.132830 +static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
1.132831 +  unicode_cursor *pCsr = (unicode_cursor *) pCursor;
1.132832 +  sqlite3_free(pCsr->zToken);
1.132833 +  sqlite3_free(pCsr);
1.132834 +  return SQLITE_OK;
1.132835 +}
1.132836 +
1.132837 +/*
1.132838 +** Extract the next token from a tokenization cursor.  The cursor must
1.132839 +** have been opened by a prior call to simpleOpen().
1.132840 +*/
1.132841 +static int unicodeNext(
1.132842 +  sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
1.132843 +  const char **paToken,           /* OUT: Token text */
1.132844 +  int *pnToken,                   /* OUT: Number of bytes at *paToken */
1.132845 +  int *piStart,                   /* OUT: Starting offset of token */
1.132846 +  int *piEnd,                     /* OUT: Ending offset of token */
1.132847 +  int *piPos                      /* OUT: Position integer of token */
1.132848 +){
1.132849 +  unicode_cursor *pCsr = (unicode_cursor *)pC;
1.132850 +  unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
1.132851 +  int iCode;
1.132852 +  char *zOut;
1.132853 +  const unsigned char *z = &pCsr->aInput[pCsr->iOff];
1.132854 +  const unsigned char *zStart = z;
1.132855 +  const unsigned char *zEnd;
1.132856 +  const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
1.132857 +
1.132858 +  /* Scan past any delimiter characters before the start of the next token.
1.132859 +  ** Return SQLITE_DONE early if this takes us all the way to the end of 
1.132860 +  ** the input.  */
1.132861 +  while( z<zTerm ){
1.132862 +    READ_UTF8(z, zTerm, iCode);
1.132863 +    if( unicodeIsAlnum(p, iCode) ) break;
1.132864 +    zStart = z;
1.132865 +  }
1.132866 +  if( zStart>=zTerm ) return SQLITE_DONE;
1.132867 +
1.132868 +  zOut = pCsr->zToken;
1.132869 +  do {
1.132870 +    int iOut;
1.132871 +
1.132872 +    /* Grow the output buffer if required. */
1.132873 +    if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
1.132874 +      char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
1.132875 +      if( !zNew ) return SQLITE_NOMEM;
1.132876 +      zOut = &zNew[zOut - pCsr->zToken];
1.132877 +      pCsr->zToken = zNew;
1.132878 +      pCsr->nAlloc += 64;
1.132879 +    }
1.132880 +
1.132881 +    /* Write the folded case of the last character read to the output */
1.132882 +    zEnd = z;
1.132883 +    iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
1.132884 +    if( iOut ){
1.132885 +      WRITE_UTF8(zOut, iOut);
1.132886 +    }
1.132887 +
1.132888 +    /* If the cursor is not at EOF, read the next character */
1.132889 +    if( z>=zTerm ) break;
1.132890 +    READ_UTF8(z, zTerm, iCode);
1.132891 +  }while( unicodeIsAlnum(p, iCode) 
1.132892 +       || sqlite3FtsUnicodeIsdiacritic(iCode)
1.132893 +  );
1.132894 +
1.132895 +  /* Set the output variables and return. */
1.132896 +  pCsr->iOff = (z - pCsr->aInput);
1.132897 +  *paToken = pCsr->zToken;
1.132898 +  *pnToken = zOut - pCsr->zToken;
1.132899 +  *piStart = (zStart - pCsr->aInput);
1.132900 +  *piEnd = (zEnd - pCsr->aInput);
1.132901 +  *piPos = pCsr->iToken++;
1.132902 +  return SQLITE_OK;
1.132903 +}
1.132904 +
1.132905 +/*
1.132906 +** Set *ppModule to a pointer to the sqlite3_tokenizer_module 
1.132907 +** structure for the unicode tokenizer.
1.132908 +*/
1.132909 +SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
1.132910 +  static const sqlite3_tokenizer_module module = {
1.132911 +    0,
1.132912 +    unicodeCreate,
1.132913 +    unicodeDestroy,
1.132914 +    unicodeOpen,
1.132915 +    unicodeClose,
1.132916 +    unicodeNext,
1.132917 +    0,
1.132918 +  };
1.132919 +  *ppModule = &module;
1.132920 +}
1.132921 +
1.132922 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.132923 +#endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
1.132924 +
1.132925 +/************** End of fts3_unicode.c ****************************************/
1.132926 +/************** Begin file fts3_unicode2.c ***********************************/
1.132927 +/*
1.132928 +** 2012 May 25
1.132929 +**
1.132930 +** The author disclaims copyright to this source code.  In place of
1.132931 +** a legal notice, here is a blessing:
1.132932 +**
1.132933 +**    May you do good and not evil.
1.132934 +**    May you find forgiveness for yourself and forgive others.
1.132935 +**    May you share freely, never taking more than you give.
1.132936 +**
1.132937 +******************************************************************************
1.132938 +*/
1.132939 +
1.132940 +/*
1.132941 +** DO NOT EDIT THIS MACHINE GENERATED FILE.
1.132942 +*/
1.132943 +
1.132944 +#if defined(SQLITE_ENABLE_FTS4_UNICODE61)
1.132945 +#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
1.132946 +
1.132947 +/* #include <assert.h> */
1.132948 +
1.132949 +/*
1.132950 +** Return true if the argument corresponds to a unicode codepoint
1.132951 +** classified as either a letter or a number. Otherwise false.
1.132952 +**
1.132953 +** The results are undefined if the value passed to this function
1.132954 +** is less than zero.
1.132955 +*/
1.132956 +SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
1.132957 +  /* Each unsigned integer in the following array corresponds to a contiguous
1.132958 +  ** range of unicode codepoints that are not either letters or numbers (i.e.
1.132959 +  ** codepoints for which this function should return 0).
1.132960 +  **
1.132961 +  ** The most significant 22 bits in each 32-bit value contain the first 
1.132962 +  ** codepoint in the range. The least significant 10 bits are used to store
1.132963 +  ** the size of the range (always at least 1). In other words, the value 
1.132964 +  ** ((C<<22) + N) represents a range of N codepoints starting with codepoint 
1.132965 +  ** C. It is not possible to represent a range larger than 1023 codepoints 
1.132966 +  ** using this format.
1.132967 +  */
1.132968 +  const static unsigned int aEntry[] = {
1.132969 +    0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
1.132970 +    0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
1.132971 +    0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
1.132972 +    0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
1.132973 +    0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
1.132974 +    0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
1.132975 +    0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
1.132976 +    0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
1.132977 +    0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
1.132978 +    0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
1.132979 +    0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
1.132980 +    0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
1.132981 +    0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
1.132982 +    0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
1.132983 +    0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
1.132984 +    0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
1.132985 +    0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
1.132986 +    0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
1.132987 +    0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
1.132988 +    0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
1.132989 +    0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
1.132990 +    0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
1.132991 +    0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
1.132992 +    0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
1.132993 +    0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
1.132994 +    0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
1.132995 +    0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
1.132996 +    0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
1.132997 +    0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
1.132998 +    0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
1.132999 +    0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
1.133000 +    0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
1.133001 +    0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
1.133002 +    0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
1.133003 +    0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
1.133004 +    0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
1.133005 +    0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
1.133006 +    0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
1.133007 +    0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
1.133008 +    0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
1.133009 +    0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
1.133010 +    0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
1.133011 +    0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
1.133012 +    0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
1.133013 +    0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
1.133014 +    0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
1.133015 +    0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
1.133016 +    0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
1.133017 +    0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
1.133018 +    0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
1.133019 +    0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
1.133020 +    0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
1.133021 +    0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
1.133022 +    0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
1.133023 +    0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
1.133024 +    0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
1.133025 +    0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
1.133026 +    0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
1.133027 +    0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
1.133028 +    0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
1.133029 +    0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
1.133030 +    0x037FFC02, 0x03E3FC01, 0x03EC7801, 0x03ECA401, 0x03EEC810,
1.133031 +    0x03F4F802, 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023,
1.133032 +    0x03F95013, 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807,
1.133033 +    0x03FCEC06, 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405,
1.133034 +    0x04040003, 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E,
1.133035 +    0x040E7C01, 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01,
1.133036 +    0x04280403, 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01,
1.133037 +    0x04294009, 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016,
1.133038 +    0x04420003, 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004,
1.133039 +    0x04460003, 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004,
1.133040 +    0x05BD442E, 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5,
1.133041 +    0x07480046, 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01,
1.133042 +    0x075C5401, 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401,
1.133043 +    0x075EA401, 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064,
1.133044 +    0x07C2800F, 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F,
1.133045 +    0x07C4C03C, 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009,
1.133046 +    0x07C94002, 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014,
1.133047 +    0x07CE8025, 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001,
1.133048 +    0x07D108B6, 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018,
1.133049 +    0x07D7EC46, 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401,
1.133050 +    0x38008060, 0x380400F0, 0x3C000001, 0x3FFFF401, 0x40000001,
1.133051 +    0x43FFF401,
1.133052 +  };
1.133053 +  static const unsigned int aAscii[4] = {
1.133054 +    0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
1.133055 +  };
1.133056 +
1.133057 +  if( c<128 ){
1.133058 +    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
1.133059 +  }else if( c<(1<<22) ){
1.133060 +    unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
1.133061 +    int iRes;
1.133062 +    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
1.133063 +    int iLo = 0;
1.133064 +    while( iHi>=iLo ){
1.133065 +      int iTest = (iHi + iLo) / 2;
1.133066 +      if( key >= aEntry[iTest] ){
1.133067 +        iRes = iTest;
1.133068 +        iLo = iTest+1;
1.133069 +      }else{
1.133070 +        iHi = iTest-1;
1.133071 +      }
1.133072 +    }
1.133073 +    assert( aEntry[0]<key );
1.133074 +    assert( key>=aEntry[iRes] );
1.133075 +    return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
1.133076 +  }
1.133077 +  return 1;
1.133078 +}
1.133079 +
1.133080 +
1.133081 +/*
1.133082 +** If the argument is a codepoint corresponding to a lowercase letter
1.133083 +** in the ASCII range with a diacritic added, return the codepoint
1.133084 +** of the ASCII letter only. For example, if passed 235 - "LATIN
1.133085 +** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
1.133086 +** E"). The resuls of passing a codepoint that corresponds to an
1.133087 +** uppercase letter are undefined.
1.133088 +*/
1.133089 +static int remove_diacritic(int c){
1.133090 +  unsigned short aDia[] = {
1.133091 +        0,  1797,  1848,  1859,  1891,  1928,  1940,  1995, 
1.133092 +     2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286, 
1.133093 +     2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732, 
1.133094 +     2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336, 
1.133095 +     3456,  3696,  3712,  3728,  3744,  3896,  3912,  3928, 
1.133096 +     3968,  4008,  4040,  4106,  4138,  4170,  4202,  4234, 
1.133097 +     4266,  4296,  4312,  4344,  4408,  4424,  4472,  4504, 
1.133098 +     6148,  6198,  6264,  6280,  6360,  6429,  6505,  6529, 
1.133099 +    61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726, 
1.133100 +    61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122, 
1.133101 +    62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536, 
1.133102 +    62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730, 
1.133103 +    62924, 63050, 63082, 63274, 63390, 
1.133104 +  };
1.133105 +  char aChar[] = {
1.133106 +    '\0', 'a',  'c',  'e',  'i',  'n',  'o',  'u',  'y',  'y',  'a',  'c',  
1.133107 +    'd',  'e',  'e',  'g',  'h',  'i',  'j',  'k',  'l',  'n',  'o',  'r',  
1.133108 +    's',  't',  'u',  'u',  'w',  'y',  'z',  'o',  'u',  'a',  'i',  'o',  
1.133109 +    'u',  'g',  'k',  'o',  'j',  'g',  'n',  'a',  'e',  'i',  'o',  'r',  
1.133110 +    'u',  's',  't',  'h',  'a',  'e',  'o',  'y',  '\0', '\0', '\0', '\0', 
1.133111 +    '\0', '\0', '\0', '\0', 'a',  'b',  'd',  'd',  'e',  'f',  'g',  'h',  
1.133112 +    'h',  'i',  'k',  'l',  'l',  'm',  'n',  'p',  'r',  'r',  's',  't',  
1.133113 +    'u',  'v',  'w',  'w',  'x',  'y',  'z',  'h',  't',  'w',  'y',  'a',  
1.133114 +    'e',  'i',  'o',  'u',  'y',  
1.133115 +  };
1.133116 +
1.133117 +  unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
1.133118 +  int iRes = 0;
1.133119 +  int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
1.133120 +  int iLo = 0;
1.133121 +  while( iHi>=iLo ){
1.133122 +    int iTest = (iHi + iLo) / 2;
1.133123 +    if( key >= aDia[iTest] ){
1.133124 +      iRes = iTest;
1.133125 +      iLo = iTest+1;
1.133126 +    }else{
1.133127 +      iHi = iTest-1;
1.133128 +    }
1.133129 +  }
1.133130 +  assert( key>=aDia[iRes] );
1.133131 +  return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
1.133132 +};
1.133133 +
1.133134 +
1.133135 +/*
1.133136 +** Return true if the argument interpreted as a unicode codepoint
1.133137 +** is a diacritical modifier character.
1.133138 +*/
1.133139 +SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
1.133140 +  unsigned int mask0 = 0x08029FDF;
1.133141 +  unsigned int mask1 = 0x000361F8;
1.133142 +  if( c<768 || c>817 ) return 0;
1.133143 +  return (c < 768+32) ?
1.133144 +      (mask0 & (1 << (c-768))) :
1.133145 +      (mask1 & (1 << (c-768-32)));
1.133146 +}
1.133147 +
1.133148 +
1.133149 +/*
1.133150 +** Interpret the argument as a unicode codepoint. If the codepoint
1.133151 +** is an upper case character that has a lower case equivalent,
1.133152 +** return the codepoint corresponding to the lower case version.
1.133153 +** Otherwise, return a copy of the argument.
1.133154 +**
1.133155 +** The results are undefined if the value passed to this function
1.133156 +** is less than zero.
1.133157 +*/
1.133158 +SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
1.133159 +  /* Each entry in the following array defines a rule for folding a range
1.133160 +  ** of codepoints to lower case. The rule applies to a range of nRange
1.133161 +  ** codepoints starting at codepoint iCode.
1.133162 +  **
1.133163 +  ** If the least significant bit in flags is clear, then the rule applies
1.133164 +  ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
1.133165 +  ** need to be folded). Or, if it is set, then the rule only applies to
1.133166 +  ** every second codepoint in the range, starting with codepoint C.
1.133167 +  **
1.133168 +  ** The 7 most significant bits in flags are an index into the aiOff[]
1.133169 +  ** array. If a specific codepoint C does require folding, then its lower
1.133170 +  ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
1.133171 +  **
1.133172 +  ** The contents of this array are generated by parsing the CaseFolding.txt
1.133173 +  ** file distributed as part of the "Unicode Character Database". See
1.133174 +  ** http://www.unicode.org for details.
1.133175 +  */
1.133176 +  static const struct TableEntry {
1.133177 +    unsigned short iCode;
1.133178 +    unsigned char flags;
1.133179 +    unsigned char nRange;
1.133180 +  } aEntry[] = {
1.133181 +    {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
1.133182 +    {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
1.133183 +    {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
1.133184 +    {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
1.133185 +    {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
1.133186 +    {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
1.133187 +    {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
1.133188 +    {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
1.133189 +    {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
1.133190 +    {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
1.133191 +    {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
1.133192 +    {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
1.133193 +    {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
1.133194 +    {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
1.133195 +    {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
1.133196 +    {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
1.133197 +    {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
1.133198 +    {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
1.133199 +    {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
1.133200 +    {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
1.133201 +    {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
1.133202 +    {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
1.133203 +    {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
1.133204 +    {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
1.133205 +    {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
1.133206 +    {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
1.133207 +    {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
1.133208 +    {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
1.133209 +    {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
1.133210 +    {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
1.133211 +    {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
1.133212 +    {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
1.133213 +    {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
1.133214 +    {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
1.133215 +    {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
1.133216 +    {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
1.133217 +    {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
1.133218 +    {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
1.133219 +    {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
1.133220 +    {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
1.133221 +    {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
1.133222 +    {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
1.133223 +    {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
1.133224 +    {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
1.133225 +    {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
1.133226 +    {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
1.133227 +    {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
1.133228 +    {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
1.133229 +    {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
1.133230 +    {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
1.133231 +    {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
1.133232 +    {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
1.133233 +    {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
1.133234 +    {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
1.133235 +    {65313, 14, 26},       
1.133236 +  };
1.133237 +  static const unsigned short aiOff[] = {
1.133238 +   1,     2,     8,     15,    16,    26,    28,    32,    
1.133239 +   37,    38,    40,    48,    63,    64,    69,    71,    
1.133240 +   79,    80,    116,   202,   203,   205,   206,   207,   
1.133241 +   209,   210,   211,   213,   214,   217,   218,   219,   
1.133242 +   775,   7264,  10792, 10795, 23228, 23256, 30204, 54721, 
1.133243 +   54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274, 
1.133244 +   57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406, 
1.133245 +   65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, 
1.133246 +   65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, 
1.133247 +   65514, 65521, 65527, 65528, 65529, 
1.133248 +  };
1.133249 +
1.133250 +  int ret = c;
1.133251 +
1.133252 +  assert( c>=0 );
1.133253 +  assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
1.133254 +
1.133255 +  if( c<128 ){
1.133256 +    if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
1.133257 +  }else if( c<65536 ){
1.133258 +    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
1.133259 +    int iLo = 0;
1.133260 +    int iRes = -1;
1.133261 +
1.133262 +    while( iHi>=iLo ){
1.133263 +      int iTest = (iHi + iLo) / 2;
1.133264 +      int cmp = (c - aEntry[iTest].iCode);
1.133265 +      if( cmp>=0 ){
1.133266 +        iRes = iTest;
1.133267 +        iLo = iTest+1;
1.133268 +      }else{
1.133269 +        iHi = iTest-1;
1.133270 +      }
1.133271 +    }
1.133272 +    assert( iRes<0 || c>=aEntry[iRes].iCode );
1.133273 +
1.133274 +    if( iRes>=0 ){
1.133275 +      const struct TableEntry *p = &aEntry[iRes];
1.133276 +      if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
1.133277 +        ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
1.133278 +        assert( ret>0 );
1.133279 +      }
1.133280 +    }
1.133281 +
1.133282 +    if( bRemoveDiacritic ) ret = remove_diacritic(ret);
1.133283 +  }
1.133284 +  
1.133285 +  else if( c>=66560 && c<66600 ){
1.133286 +    ret = c + 40;
1.133287 +  }
1.133288 +
1.133289 +  return ret;
1.133290 +}
1.133291 +#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
1.133292 +#endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
1.133293 +
1.133294 +/************** End of fts3_unicode2.c ***************************************/
1.133295 +/************** Begin file rtree.c *******************************************/
1.133296 +/*
1.133297 +** 2001 September 15
1.133298 +**
1.133299 +** The author disclaims copyright to this source code.  In place of
1.133300 +** a legal notice, here is a blessing:
1.133301 +**
1.133302 +**    May you do good and not evil.
1.133303 +**    May you find forgiveness for yourself and forgive others.
1.133304 +**    May you share freely, never taking more than you give.
1.133305 +**
1.133306 +*************************************************************************
1.133307 +** This file contains code for implementations of the r-tree and r*-tree
1.133308 +** algorithms packaged as an SQLite virtual table module.
1.133309 +*/
1.133310 +
1.133311 +/*
1.133312 +** Database Format of R-Tree Tables
1.133313 +** --------------------------------
1.133314 +**
1.133315 +** The data structure for a single virtual r-tree table is stored in three 
1.133316 +** native SQLite tables declared as follows. In each case, the '%' character
1.133317 +** in the table name is replaced with the user-supplied name of the r-tree
1.133318 +** table.
1.133319 +**
1.133320 +**   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
1.133321 +**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
1.133322 +**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
1.133323 +**
1.133324 +** The data for each node of the r-tree structure is stored in the %_node
1.133325 +** table. For each node that is not the root node of the r-tree, there is
1.133326 +** an entry in the %_parent table associating the node with its parent.
1.133327 +** And for each row of data in the table, there is an entry in the %_rowid
1.133328 +** table that maps from the entries rowid to the id of the node that it
1.133329 +** is stored on.
1.133330 +**
1.133331 +** The root node of an r-tree always exists, even if the r-tree table is
1.133332 +** empty. The nodeno of the root node is always 1. All other nodes in the
1.133333 +** table must be the same size as the root node. The content of each node
1.133334 +** is formatted as follows:
1.133335 +**
1.133336 +**   1. If the node is the root node (node 1), then the first 2 bytes
1.133337 +**      of the node contain the tree depth as a big-endian integer.
1.133338 +**      For non-root nodes, the first 2 bytes are left unused.
1.133339 +**
1.133340 +**   2. The next 2 bytes contain the number of entries currently 
1.133341 +**      stored in the node.
1.133342 +**
1.133343 +**   3. The remainder of the node contains the node entries. Each entry
1.133344 +**      consists of a single 8-byte integer followed by an even number
1.133345 +**      of 4-byte coordinates. For leaf nodes the integer is the rowid
1.133346 +**      of a record. For internal nodes it is the node number of a
1.133347 +**      child page.
1.133348 +*/
1.133349 +
1.133350 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
1.133351 +
1.133352 +/*
1.133353 +** This file contains an implementation of a couple of different variants
1.133354 +** of the r-tree algorithm. See the README file for further details. The 
1.133355 +** same data-structure is used for all, but the algorithms for insert and
1.133356 +** delete operations vary. The variants used are selected at compile time 
1.133357 +** by defining the following symbols:
1.133358 +*/
1.133359 +
1.133360 +/* Either, both or none of the following may be set to activate 
1.133361 +** r*tree variant algorithms.
1.133362 +*/
1.133363 +#define VARIANT_RSTARTREE_CHOOSESUBTREE 0
1.133364 +#define VARIANT_RSTARTREE_REINSERT      1
1.133365 +
1.133366 +/* 
1.133367 +** Exactly one of the following must be set to 1.
1.133368 +*/
1.133369 +#define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
1.133370 +#define VARIANT_GUTTMAN_LINEAR_SPLIT    0
1.133371 +#define VARIANT_RSTARTREE_SPLIT         1
1.133372 +
1.133373 +#define VARIANT_GUTTMAN_SPLIT \
1.133374 +        (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
1.133375 +
1.133376 +#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
1.133377 +  #define PickNext QuadraticPickNext
1.133378 +  #define PickSeeds QuadraticPickSeeds
1.133379 +  #define AssignCells splitNodeGuttman
1.133380 +#endif
1.133381 +#if VARIANT_GUTTMAN_LINEAR_SPLIT
1.133382 +  #define PickNext LinearPickNext
1.133383 +  #define PickSeeds LinearPickSeeds
1.133384 +  #define AssignCells splitNodeGuttman
1.133385 +#endif
1.133386 +#if VARIANT_RSTARTREE_SPLIT
1.133387 +  #define AssignCells splitNodeStartree
1.133388 +#endif
1.133389 +
1.133390 +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
1.133391 +# define NDEBUG 1
1.133392 +#endif
1.133393 +
1.133394 +#ifndef SQLITE_CORE
1.133395 +  SQLITE_EXTENSION_INIT1
1.133396 +#else
1.133397 +#endif
1.133398 +
1.133399 +/* #include <string.h> */
1.133400 +/* #include <assert.h> */
1.133401 +
1.133402 +#ifndef SQLITE_AMALGAMATION
1.133403 +#include "sqlite3rtree.h"
1.133404 +typedef sqlite3_int64 i64;
1.133405 +typedef unsigned char u8;
1.133406 +typedef unsigned int u32;
1.133407 +#endif
1.133408 +
1.133409 +/*  The following macro is used to suppress compiler warnings.
1.133410 +*/
1.133411 +#ifndef UNUSED_PARAMETER
1.133412 +# define UNUSED_PARAMETER(x) (void)(x)
1.133413 +#endif
1.133414 +
1.133415 +typedef struct Rtree Rtree;
1.133416 +typedef struct RtreeCursor RtreeCursor;
1.133417 +typedef struct RtreeNode RtreeNode;
1.133418 +typedef struct RtreeCell RtreeCell;
1.133419 +typedef struct RtreeConstraint RtreeConstraint;
1.133420 +typedef struct RtreeMatchArg RtreeMatchArg;
1.133421 +typedef struct RtreeGeomCallback RtreeGeomCallback;
1.133422 +typedef union RtreeCoord RtreeCoord;
1.133423 +
1.133424 +/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
1.133425 +#define RTREE_MAX_DIMENSIONS 5
1.133426 +
1.133427 +/* Size of hash table Rtree.aHash. This hash table is not expected to
1.133428 +** ever contain very many entries, so a fixed number of buckets is 
1.133429 +** used.
1.133430 +*/
1.133431 +#define HASHSIZE 128
1.133432 +
1.133433 +/* 
1.133434 +** An rtree virtual-table object.
1.133435 +*/
1.133436 +struct Rtree {
1.133437 +  sqlite3_vtab base;
1.133438 +  sqlite3 *db;                /* Host database connection */
1.133439 +  int iNodeSize;              /* Size in bytes of each node in the node table */
1.133440 +  int nDim;                   /* Number of dimensions */
1.133441 +  int nBytesPerCell;          /* Bytes consumed per cell */
1.133442 +  int iDepth;                 /* Current depth of the r-tree structure */
1.133443 +  char *zDb;                  /* Name of database containing r-tree table */
1.133444 +  char *zName;                /* Name of r-tree table */ 
1.133445 +  RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
1.133446 +  int nBusy;                  /* Current number of users of this structure */
1.133447 +
1.133448 +  /* List of nodes removed during a CondenseTree operation. List is
1.133449 +  ** linked together via the pointer normally used for hash chains -
1.133450 +  ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
1.133451 +  ** headed by the node (leaf nodes have RtreeNode.iNode==0).
1.133452 +  */
1.133453 +  RtreeNode *pDeleted;
1.133454 +  int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
1.133455 +
1.133456 +  /* Statements to read/write/delete a record from xxx_node */
1.133457 +  sqlite3_stmt *pReadNode;
1.133458 +  sqlite3_stmt *pWriteNode;
1.133459 +  sqlite3_stmt *pDeleteNode;
1.133460 +
1.133461 +  /* Statements to read/write/delete a record from xxx_rowid */
1.133462 +  sqlite3_stmt *pReadRowid;
1.133463 +  sqlite3_stmt *pWriteRowid;
1.133464 +  sqlite3_stmt *pDeleteRowid;
1.133465 +
1.133466 +  /* Statements to read/write/delete a record from xxx_parent */
1.133467 +  sqlite3_stmt *pReadParent;
1.133468 +  sqlite3_stmt *pWriteParent;
1.133469 +  sqlite3_stmt *pDeleteParent;
1.133470 +
1.133471 +  int eCoordType;
1.133472 +};
1.133473 +
1.133474 +/* Possible values for eCoordType: */
1.133475 +#define RTREE_COORD_REAL32 0
1.133476 +#define RTREE_COORD_INT32  1
1.133477 +
1.133478 +/*
1.133479 +** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
1.133480 +** only deal with integer coordinates.  No floating point operations
1.133481 +** will be done.
1.133482 +*/
1.133483 +#ifdef SQLITE_RTREE_INT_ONLY
1.133484 +  typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
1.133485 +  typedef int RtreeValue;                  /* Low accuracy coordinate */
1.133486 +#else
1.133487 +  typedef double RtreeDValue;              /* High accuracy coordinate */
1.133488 +  typedef float RtreeValue;                /* Low accuracy coordinate */
1.133489 +#endif
1.133490 +
1.133491 +/*
1.133492 +** The minimum number of cells allowed for a node is a third of the 
1.133493 +** maximum. In Gutman's notation:
1.133494 +**
1.133495 +**     m = M/3
1.133496 +**
1.133497 +** If an R*-tree "Reinsert" operation is required, the same number of
1.133498 +** cells are removed from the overfull node and reinserted into the tree.
1.133499 +*/
1.133500 +#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
1.133501 +#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
1.133502 +#define RTREE_MAXCELLS 51
1.133503 +
1.133504 +/*
1.133505 +** The smallest possible node-size is (512-64)==448 bytes. And the largest
1.133506 +** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
1.133507 +** Therefore all non-root nodes must contain at least 3 entries. Since 
1.133508 +** 2^40 is greater than 2^64, an r-tree structure always has a depth of
1.133509 +** 40 or less.
1.133510 +*/
1.133511 +#define RTREE_MAX_DEPTH 40
1.133512 +
1.133513 +/* 
1.133514 +** An rtree cursor object.
1.133515 +*/
1.133516 +struct RtreeCursor {
1.133517 +  sqlite3_vtab_cursor base;
1.133518 +  RtreeNode *pNode;                 /* Node cursor is currently pointing at */
1.133519 +  int iCell;                        /* Index of current cell in pNode */
1.133520 +  int iStrategy;                    /* Copy of idxNum search parameter */
1.133521 +  int nConstraint;                  /* Number of entries in aConstraint */
1.133522 +  RtreeConstraint *aConstraint;     /* Search constraints. */
1.133523 +};
1.133524 +
1.133525 +union RtreeCoord {
1.133526 +  RtreeValue f;
1.133527 +  int i;
1.133528 +};
1.133529 +
1.133530 +/*
1.133531 +** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
1.133532 +** formatted as a RtreeDValue (double or int64). This macro assumes that local
1.133533 +** variable pRtree points to the Rtree structure associated with the
1.133534 +** RtreeCoord.
1.133535 +*/
1.133536 +#ifdef SQLITE_RTREE_INT_ONLY
1.133537 +# define DCOORD(coord) ((RtreeDValue)coord.i)
1.133538 +#else
1.133539 +# define DCOORD(coord) (                           \
1.133540 +    (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
1.133541 +      ((double)coord.f) :                           \
1.133542 +      ((double)coord.i)                             \
1.133543 +  )
1.133544 +#endif
1.133545 +
1.133546 +/*
1.133547 +** A search constraint.
1.133548 +*/
1.133549 +struct RtreeConstraint {
1.133550 +  int iCoord;                     /* Index of constrained coordinate */
1.133551 +  int op;                         /* Constraining operation */
1.133552 +  RtreeDValue rValue;             /* Constraint value. */
1.133553 +  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
1.133554 +  sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
1.133555 +};
1.133556 +
1.133557 +/* Possible values for RtreeConstraint.op */
1.133558 +#define RTREE_EQ    0x41
1.133559 +#define RTREE_LE    0x42
1.133560 +#define RTREE_LT    0x43
1.133561 +#define RTREE_GE    0x44
1.133562 +#define RTREE_GT    0x45
1.133563 +#define RTREE_MATCH 0x46
1.133564 +
1.133565 +/* 
1.133566 +** An rtree structure node.
1.133567 +*/
1.133568 +struct RtreeNode {
1.133569 +  RtreeNode *pParent;               /* Parent node */
1.133570 +  i64 iNode;
1.133571 +  int nRef;
1.133572 +  int isDirty;
1.133573 +  u8 *zData;
1.133574 +  RtreeNode *pNext;                 /* Next node in this hash chain */
1.133575 +};
1.133576 +#define NCELL(pNode) readInt16(&(pNode)->zData[2])
1.133577 +
1.133578 +/* 
1.133579 +** Structure to store a deserialized rtree record.
1.133580 +*/
1.133581 +struct RtreeCell {
1.133582 +  i64 iRowid;
1.133583 +  RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
1.133584 +};
1.133585 +
1.133586 +
1.133587 +/*
1.133588 +** Value for the first field of every RtreeMatchArg object. The MATCH
1.133589 +** operator tests that the first field of a blob operand matches this
1.133590 +** value to avoid operating on invalid blobs (which could cause a segfault).
1.133591 +*/
1.133592 +#define RTREE_GEOMETRY_MAGIC 0x891245AB
1.133593 +
1.133594 +/*
1.133595 +** An instance of this structure must be supplied as a blob argument to
1.133596 +** the right-hand-side of an SQL MATCH operator used to constrain an
1.133597 +** r-tree query.
1.133598 +*/
1.133599 +struct RtreeMatchArg {
1.133600 +  u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
1.133601 +  int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
1.133602 +  void *pContext;
1.133603 +  int nParam;
1.133604 +  RtreeDValue aParam[1];
1.133605 +};
1.133606 +
1.133607 +/*
1.133608 +** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
1.133609 +** a single instance of the following structure is allocated. It is used
1.133610 +** as the context for the user-function created by by s_r_g_c(). The object
1.133611 +** is eventually deleted by the destructor mechanism provided by
1.133612 +** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
1.133613 +** the geometry callback function).
1.133614 +*/
1.133615 +struct RtreeGeomCallback {
1.133616 +  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
1.133617 +  void *pContext;
1.133618 +};
1.133619 +
1.133620 +#ifndef MAX
1.133621 +# define MAX(x,y) ((x) < (y) ? (y) : (x))
1.133622 +#endif
1.133623 +#ifndef MIN
1.133624 +# define MIN(x,y) ((x) > (y) ? (y) : (x))
1.133625 +#endif
1.133626 +
1.133627 +/*
1.133628 +** Functions to deserialize a 16 bit integer, 32 bit real number and
1.133629 +** 64 bit integer. The deserialized value is returned.
1.133630 +*/
1.133631 +static int readInt16(u8 *p){
1.133632 +  return (p[0]<<8) + p[1];
1.133633 +}
1.133634 +static void readCoord(u8 *p, RtreeCoord *pCoord){
1.133635 +  u32 i = (
1.133636 +    (((u32)p[0]) << 24) + 
1.133637 +    (((u32)p[1]) << 16) + 
1.133638 +    (((u32)p[2]) <<  8) + 
1.133639 +    (((u32)p[3]) <<  0)
1.133640 +  );
1.133641 +  *(u32 *)pCoord = i;
1.133642 +}
1.133643 +static i64 readInt64(u8 *p){
1.133644 +  return (
1.133645 +    (((i64)p[0]) << 56) + 
1.133646 +    (((i64)p[1]) << 48) + 
1.133647 +    (((i64)p[2]) << 40) + 
1.133648 +    (((i64)p[3]) << 32) + 
1.133649 +    (((i64)p[4]) << 24) + 
1.133650 +    (((i64)p[5]) << 16) + 
1.133651 +    (((i64)p[6]) <<  8) + 
1.133652 +    (((i64)p[7]) <<  0)
1.133653 +  );
1.133654 +}
1.133655 +
1.133656 +/*
1.133657 +** Functions to serialize a 16 bit integer, 32 bit real number and
1.133658 +** 64 bit integer. The value returned is the number of bytes written
1.133659 +** to the argument buffer (always 2, 4 and 8 respectively).
1.133660 +*/
1.133661 +static int writeInt16(u8 *p, int i){
1.133662 +  p[0] = (i>> 8)&0xFF;
1.133663 +  p[1] = (i>> 0)&0xFF;
1.133664 +  return 2;
1.133665 +}
1.133666 +static int writeCoord(u8 *p, RtreeCoord *pCoord){
1.133667 +  u32 i;
1.133668 +  assert( sizeof(RtreeCoord)==4 );
1.133669 +  assert( sizeof(u32)==4 );
1.133670 +  i = *(u32 *)pCoord;
1.133671 +  p[0] = (i>>24)&0xFF;
1.133672 +  p[1] = (i>>16)&0xFF;
1.133673 +  p[2] = (i>> 8)&0xFF;
1.133674 +  p[3] = (i>> 0)&0xFF;
1.133675 +  return 4;
1.133676 +}
1.133677 +static int writeInt64(u8 *p, i64 i){
1.133678 +  p[0] = (i>>56)&0xFF;
1.133679 +  p[1] = (i>>48)&0xFF;
1.133680 +  p[2] = (i>>40)&0xFF;
1.133681 +  p[3] = (i>>32)&0xFF;
1.133682 +  p[4] = (i>>24)&0xFF;
1.133683 +  p[5] = (i>>16)&0xFF;
1.133684 +  p[6] = (i>> 8)&0xFF;
1.133685 +  p[7] = (i>> 0)&0xFF;
1.133686 +  return 8;
1.133687 +}
1.133688 +
1.133689 +/*
1.133690 +** Increment the reference count of node p.
1.133691 +*/
1.133692 +static void nodeReference(RtreeNode *p){
1.133693 +  if( p ){
1.133694 +    p->nRef++;
1.133695 +  }
1.133696 +}
1.133697 +
1.133698 +/*
1.133699 +** Clear the content of node p (set all bytes to 0x00).
1.133700 +*/
1.133701 +static void nodeZero(Rtree *pRtree, RtreeNode *p){
1.133702 +  memset(&p->zData[2], 0, pRtree->iNodeSize-2);
1.133703 +  p->isDirty = 1;
1.133704 +}
1.133705 +
1.133706 +/*
1.133707 +** Given a node number iNode, return the corresponding key to use
1.133708 +** in the Rtree.aHash table.
1.133709 +*/
1.133710 +static int nodeHash(i64 iNode){
1.133711 +  return (
1.133712 +    (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ 
1.133713 +    (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
1.133714 +  ) % HASHSIZE;
1.133715 +}
1.133716 +
1.133717 +/*
1.133718 +** Search the node hash table for node iNode. If found, return a pointer
1.133719 +** to it. Otherwise, return 0.
1.133720 +*/
1.133721 +static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
1.133722 +  RtreeNode *p;
1.133723 +  for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
1.133724 +  return p;
1.133725 +}
1.133726 +
1.133727 +/*
1.133728 +** Add node pNode to the node hash table.
1.133729 +*/
1.133730 +static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
1.133731 +  int iHash;
1.133732 +  assert( pNode->pNext==0 );
1.133733 +  iHash = nodeHash(pNode->iNode);
1.133734 +  pNode->pNext = pRtree->aHash[iHash];
1.133735 +  pRtree->aHash[iHash] = pNode;
1.133736 +}
1.133737 +
1.133738 +/*
1.133739 +** Remove node pNode from the node hash table.
1.133740 +*/
1.133741 +static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
1.133742 +  RtreeNode **pp;
1.133743 +  if( pNode->iNode!=0 ){
1.133744 +    pp = &pRtree->aHash[nodeHash(pNode->iNode)];
1.133745 +    for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
1.133746 +    *pp = pNode->pNext;
1.133747 +    pNode->pNext = 0;
1.133748 +  }
1.133749 +}
1.133750 +
1.133751 +/*
1.133752 +** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
1.133753 +** indicating that node has not yet been assigned a node number. It is
1.133754 +** assigned a node number when nodeWrite() is called to write the
1.133755 +** node contents out to the database.
1.133756 +*/
1.133757 +static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
1.133758 +  RtreeNode *pNode;
1.133759 +  pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
1.133760 +  if( pNode ){
1.133761 +    memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
1.133762 +    pNode->zData = (u8 *)&pNode[1];
1.133763 +    pNode->nRef = 1;
1.133764 +    pNode->pParent = pParent;
1.133765 +    pNode->isDirty = 1;
1.133766 +    nodeReference(pParent);
1.133767 +  }
1.133768 +  return pNode;
1.133769 +}
1.133770 +
1.133771 +/*
1.133772 +** Obtain a reference to an r-tree node.
1.133773 +*/
1.133774 +static int
1.133775 +nodeAcquire(
1.133776 +  Rtree *pRtree,             /* R-tree structure */
1.133777 +  i64 iNode,                 /* Node number to load */
1.133778 +  RtreeNode *pParent,        /* Either the parent node or NULL */
1.133779 +  RtreeNode **ppNode         /* OUT: Acquired node */
1.133780 +){
1.133781 +  int rc;
1.133782 +  int rc2 = SQLITE_OK;
1.133783 +  RtreeNode *pNode;
1.133784 +
1.133785 +  /* Check if the requested node is already in the hash table. If so,
1.133786 +  ** increase its reference count and return it.
1.133787 +  */
1.133788 +  if( (pNode = nodeHashLookup(pRtree, iNode)) ){
1.133789 +    assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
1.133790 +    if( pParent && !pNode->pParent ){
1.133791 +      nodeReference(pParent);
1.133792 +      pNode->pParent = pParent;
1.133793 +    }
1.133794 +    pNode->nRef++;
1.133795 +    *ppNode = pNode;
1.133796 +    return SQLITE_OK;
1.133797 +  }
1.133798 +
1.133799 +  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
1.133800 +  rc = sqlite3_step(pRtree->pReadNode);
1.133801 +  if( rc==SQLITE_ROW ){
1.133802 +    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
1.133803 +    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
1.133804 +      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
1.133805 +      if( !pNode ){
1.133806 +        rc2 = SQLITE_NOMEM;
1.133807 +      }else{
1.133808 +        pNode->pParent = pParent;
1.133809 +        pNode->zData = (u8 *)&pNode[1];
1.133810 +        pNode->nRef = 1;
1.133811 +        pNode->iNode = iNode;
1.133812 +        pNode->isDirty = 0;
1.133813 +        pNode->pNext = 0;
1.133814 +        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
1.133815 +        nodeReference(pParent);
1.133816 +      }
1.133817 +    }
1.133818 +  }
1.133819 +  rc = sqlite3_reset(pRtree->pReadNode);
1.133820 +  if( rc==SQLITE_OK ) rc = rc2;
1.133821 +
1.133822 +  /* If the root node was just loaded, set pRtree->iDepth to the height
1.133823 +  ** of the r-tree structure. A height of zero means all data is stored on
1.133824 +  ** the root node. A height of one means the children of the root node
1.133825 +  ** are the leaves, and so on. If the depth as specified on the root node
1.133826 +  ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
1.133827 +  */
1.133828 +  if( pNode && iNode==1 ){
1.133829 +    pRtree->iDepth = readInt16(pNode->zData);
1.133830 +    if( pRtree->iDepth>RTREE_MAX_DEPTH ){
1.133831 +      rc = SQLITE_CORRUPT_VTAB;
1.133832 +    }
1.133833 +  }
1.133834 +
1.133835 +  /* If no error has occurred so far, check if the "number of entries"
1.133836 +  ** field on the node is too large. If so, set the return code to 
1.133837 +  ** SQLITE_CORRUPT_VTAB.
1.133838 +  */
1.133839 +  if( pNode && rc==SQLITE_OK ){
1.133840 +    if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
1.133841 +      rc = SQLITE_CORRUPT_VTAB;
1.133842 +    }
1.133843 +  }
1.133844 +
1.133845 +  if( rc==SQLITE_OK ){
1.133846 +    if( pNode!=0 ){
1.133847 +      nodeHashInsert(pRtree, pNode);
1.133848 +    }else{
1.133849 +      rc = SQLITE_CORRUPT_VTAB;
1.133850 +    }
1.133851 +    *ppNode = pNode;
1.133852 +  }else{
1.133853 +    sqlite3_free(pNode);
1.133854 +    *ppNode = 0;
1.133855 +  }
1.133856 +
1.133857 +  return rc;
1.133858 +}
1.133859 +
1.133860 +/*
1.133861 +** Overwrite cell iCell of node pNode with the contents of pCell.
1.133862 +*/
1.133863 +static void nodeOverwriteCell(
1.133864 +  Rtree *pRtree, 
1.133865 +  RtreeNode *pNode,  
1.133866 +  RtreeCell *pCell, 
1.133867 +  int iCell
1.133868 +){
1.133869 +  int ii;
1.133870 +  u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
1.133871 +  p += writeInt64(p, pCell->iRowid);
1.133872 +  for(ii=0; ii<(pRtree->nDim*2); ii++){
1.133873 +    p += writeCoord(p, &pCell->aCoord[ii]);
1.133874 +  }
1.133875 +  pNode->isDirty = 1;
1.133876 +}
1.133877 +
1.133878 +/*
1.133879 +** Remove cell the cell with index iCell from node pNode.
1.133880 +*/
1.133881 +static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
1.133882 +  u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
1.133883 +  u8 *pSrc = &pDst[pRtree->nBytesPerCell];
1.133884 +  int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
1.133885 +  memmove(pDst, pSrc, nByte);
1.133886 +  writeInt16(&pNode->zData[2], NCELL(pNode)-1);
1.133887 +  pNode->isDirty = 1;
1.133888 +}
1.133889 +
1.133890 +/*
1.133891 +** Insert the contents of cell pCell into node pNode. If the insert
1.133892 +** is successful, return SQLITE_OK.
1.133893 +**
1.133894 +** If there is not enough free space in pNode, return SQLITE_FULL.
1.133895 +*/
1.133896 +static int
1.133897 +nodeInsertCell(
1.133898 +  Rtree *pRtree, 
1.133899 +  RtreeNode *pNode, 
1.133900 +  RtreeCell *pCell 
1.133901 +){
1.133902 +  int nCell;                    /* Current number of cells in pNode */
1.133903 +  int nMaxCell;                 /* Maximum number of cells for pNode */
1.133904 +
1.133905 +  nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
1.133906 +  nCell = NCELL(pNode);
1.133907 +
1.133908 +  assert( nCell<=nMaxCell );
1.133909 +  if( nCell<nMaxCell ){
1.133910 +    nodeOverwriteCell(pRtree, pNode, pCell, nCell);
1.133911 +    writeInt16(&pNode->zData[2], nCell+1);
1.133912 +    pNode->isDirty = 1;
1.133913 +  }
1.133914 +
1.133915 +  return (nCell==nMaxCell);
1.133916 +}
1.133917 +
1.133918 +/*
1.133919 +** If the node is dirty, write it out to the database.
1.133920 +*/
1.133921 +static int
1.133922 +nodeWrite(Rtree *pRtree, RtreeNode *pNode){
1.133923 +  int rc = SQLITE_OK;
1.133924 +  if( pNode->isDirty ){
1.133925 +    sqlite3_stmt *p = pRtree->pWriteNode;
1.133926 +    if( pNode->iNode ){
1.133927 +      sqlite3_bind_int64(p, 1, pNode->iNode);
1.133928 +    }else{
1.133929 +      sqlite3_bind_null(p, 1);
1.133930 +    }
1.133931 +    sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
1.133932 +    sqlite3_step(p);
1.133933 +    pNode->isDirty = 0;
1.133934 +    rc = sqlite3_reset(p);
1.133935 +    if( pNode->iNode==0 && rc==SQLITE_OK ){
1.133936 +      pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
1.133937 +      nodeHashInsert(pRtree, pNode);
1.133938 +    }
1.133939 +  }
1.133940 +  return rc;
1.133941 +}
1.133942 +
1.133943 +/*
1.133944 +** Release a reference to a node. If the node is dirty and the reference
1.133945 +** count drops to zero, the node data is written to the database.
1.133946 +*/
1.133947 +static int
1.133948 +nodeRelease(Rtree *pRtree, RtreeNode *pNode){
1.133949 +  int rc = SQLITE_OK;
1.133950 +  if( pNode ){
1.133951 +    assert( pNode->nRef>0 );
1.133952 +    pNode->nRef--;
1.133953 +    if( pNode->nRef==0 ){
1.133954 +      if( pNode->iNode==1 ){
1.133955 +        pRtree->iDepth = -1;
1.133956 +      }
1.133957 +      if( pNode->pParent ){
1.133958 +        rc = nodeRelease(pRtree, pNode->pParent);
1.133959 +      }
1.133960 +      if( rc==SQLITE_OK ){
1.133961 +        rc = nodeWrite(pRtree, pNode);
1.133962 +      }
1.133963 +      nodeHashDelete(pRtree, pNode);
1.133964 +      sqlite3_free(pNode);
1.133965 +    }
1.133966 +  }
1.133967 +  return rc;
1.133968 +}
1.133969 +
1.133970 +/*
1.133971 +** Return the 64-bit integer value associated with cell iCell of
1.133972 +** node pNode. If pNode is a leaf node, this is a rowid. If it is
1.133973 +** an internal node, then the 64-bit integer is a child page number.
1.133974 +*/
1.133975 +static i64 nodeGetRowid(
1.133976 +  Rtree *pRtree, 
1.133977 +  RtreeNode *pNode, 
1.133978 +  int iCell
1.133979 +){
1.133980 +  assert( iCell<NCELL(pNode) );
1.133981 +  return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
1.133982 +}
1.133983 +
1.133984 +/*
1.133985 +** Return coordinate iCoord from cell iCell in node pNode.
1.133986 +*/
1.133987 +static void nodeGetCoord(
1.133988 +  Rtree *pRtree, 
1.133989 +  RtreeNode *pNode, 
1.133990 +  int iCell,
1.133991 +  int iCoord,
1.133992 +  RtreeCoord *pCoord           /* Space to write result to */
1.133993 +){
1.133994 +  readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
1.133995 +}
1.133996 +
1.133997 +/*
1.133998 +** Deserialize cell iCell of node pNode. Populate the structure pointed
1.133999 +** to by pCell with the results.
1.134000 +*/
1.134001 +static void nodeGetCell(
1.134002 +  Rtree *pRtree, 
1.134003 +  RtreeNode *pNode, 
1.134004 +  int iCell,
1.134005 +  RtreeCell *pCell
1.134006 +){
1.134007 +  int ii;
1.134008 +  pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
1.134009 +  for(ii=0; ii<pRtree->nDim*2; ii++){
1.134010 +    nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
1.134011 +  }
1.134012 +}
1.134013 +
1.134014 +
1.134015 +/* Forward declaration for the function that does the work of
1.134016 +** the virtual table module xCreate() and xConnect() methods.
1.134017 +*/
1.134018 +static int rtreeInit(
1.134019 +  sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
1.134020 +);
1.134021 +
1.134022 +/* 
1.134023 +** Rtree virtual table module xCreate method.
1.134024 +*/
1.134025 +static int rtreeCreate(
1.134026 +  sqlite3 *db,
1.134027 +  void *pAux,
1.134028 +  int argc, const char *const*argv,
1.134029 +  sqlite3_vtab **ppVtab,
1.134030 +  char **pzErr
1.134031 +){
1.134032 +  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
1.134033 +}
1.134034 +
1.134035 +/* 
1.134036 +** Rtree virtual table module xConnect method.
1.134037 +*/
1.134038 +static int rtreeConnect(
1.134039 +  sqlite3 *db,
1.134040 +  void *pAux,
1.134041 +  int argc, const char *const*argv,
1.134042 +  sqlite3_vtab **ppVtab,
1.134043 +  char **pzErr
1.134044 +){
1.134045 +  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
1.134046 +}
1.134047 +
1.134048 +/*
1.134049 +** Increment the r-tree reference count.
1.134050 +*/
1.134051 +static void rtreeReference(Rtree *pRtree){
1.134052 +  pRtree->nBusy++;
1.134053 +}
1.134054 +
1.134055 +/*
1.134056 +** Decrement the r-tree reference count. When the reference count reaches
1.134057 +** zero the structure is deleted.
1.134058 +*/
1.134059 +static void rtreeRelease(Rtree *pRtree){
1.134060 +  pRtree->nBusy--;
1.134061 +  if( pRtree->nBusy==0 ){
1.134062 +    sqlite3_finalize(pRtree->pReadNode);
1.134063 +    sqlite3_finalize(pRtree->pWriteNode);
1.134064 +    sqlite3_finalize(pRtree->pDeleteNode);
1.134065 +    sqlite3_finalize(pRtree->pReadRowid);
1.134066 +    sqlite3_finalize(pRtree->pWriteRowid);
1.134067 +    sqlite3_finalize(pRtree->pDeleteRowid);
1.134068 +    sqlite3_finalize(pRtree->pReadParent);
1.134069 +    sqlite3_finalize(pRtree->pWriteParent);
1.134070 +    sqlite3_finalize(pRtree->pDeleteParent);
1.134071 +    sqlite3_free(pRtree);
1.134072 +  }
1.134073 +}
1.134074 +
1.134075 +/* 
1.134076 +** Rtree virtual table module xDisconnect method.
1.134077 +*/
1.134078 +static int rtreeDisconnect(sqlite3_vtab *pVtab){
1.134079 +  rtreeRelease((Rtree *)pVtab);
1.134080 +  return SQLITE_OK;
1.134081 +}
1.134082 +
1.134083 +/* 
1.134084 +** Rtree virtual table module xDestroy method.
1.134085 +*/
1.134086 +static int rtreeDestroy(sqlite3_vtab *pVtab){
1.134087 +  Rtree *pRtree = (Rtree *)pVtab;
1.134088 +  int rc;
1.134089 +  char *zCreate = sqlite3_mprintf(
1.134090 +    "DROP TABLE '%q'.'%q_node';"
1.134091 +    "DROP TABLE '%q'.'%q_rowid';"
1.134092 +    "DROP TABLE '%q'.'%q_parent';",
1.134093 +    pRtree->zDb, pRtree->zName, 
1.134094 +    pRtree->zDb, pRtree->zName,
1.134095 +    pRtree->zDb, pRtree->zName
1.134096 +  );
1.134097 +  if( !zCreate ){
1.134098 +    rc = SQLITE_NOMEM;
1.134099 +  }else{
1.134100 +    rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
1.134101 +    sqlite3_free(zCreate);
1.134102 +  }
1.134103 +  if( rc==SQLITE_OK ){
1.134104 +    rtreeRelease(pRtree);
1.134105 +  }
1.134106 +
1.134107 +  return rc;
1.134108 +}
1.134109 +
1.134110 +/* 
1.134111 +** Rtree virtual table module xOpen method.
1.134112 +*/
1.134113 +static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
1.134114 +  int rc = SQLITE_NOMEM;
1.134115 +  RtreeCursor *pCsr;
1.134116 +
1.134117 +  pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
1.134118 +  if( pCsr ){
1.134119 +    memset(pCsr, 0, sizeof(RtreeCursor));
1.134120 +    pCsr->base.pVtab = pVTab;
1.134121 +    rc = SQLITE_OK;
1.134122 +  }
1.134123 +  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
1.134124 +
1.134125 +  return rc;
1.134126 +}
1.134127 +
1.134128 +
1.134129 +/*
1.134130 +** Free the RtreeCursor.aConstraint[] array and its contents.
1.134131 +*/
1.134132 +static void freeCursorConstraints(RtreeCursor *pCsr){
1.134133 +  if( pCsr->aConstraint ){
1.134134 +    int i;                        /* Used to iterate through constraint array */
1.134135 +    for(i=0; i<pCsr->nConstraint; i++){
1.134136 +      sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
1.134137 +      if( pGeom ){
1.134138 +        if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
1.134139 +        sqlite3_free(pGeom);
1.134140 +      }
1.134141 +    }
1.134142 +    sqlite3_free(pCsr->aConstraint);
1.134143 +    pCsr->aConstraint = 0;
1.134144 +  }
1.134145 +}
1.134146 +
1.134147 +/* 
1.134148 +** Rtree virtual table module xClose method.
1.134149 +*/
1.134150 +static int rtreeClose(sqlite3_vtab_cursor *cur){
1.134151 +  Rtree *pRtree = (Rtree *)(cur->pVtab);
1.134152 +  int rc;
1.134153 +  RtreeCursor *pCsr = (RtreeCursor *)cur;
1.134154 +  freeCursorConstraints(pCsr);
1.134155 +  rc = nodeRelease(pRtree, pCsr->pNode);
1.134156 +  sqlite3_free(pCsr);
1.134157 +  return rc;
1.134158 +}
1.134159 +
1.134160 +/*
1.134161 +** Rtree virtual table module xEof method.
1.134162 +**
1.134163 +** Return non-zero if the cursor does not currently point to a valid 
1.134164 +** record (i.e if the scan has finished), or zero otherwise.
1.134165 +*/
1.134166 +static int rtreeEof(sqlite3_vtab_cursor *cur){
1.134167 +  RtreeCursor *pCsr = (RtreeCursor *)cur;
1.134168 +  return (pCsr->pNode==0);
1.134169 +}
1.134170 +
1.134171 +/*
1.134172 +** The r-tree constraint passed as the second argument to this function is
1.134173 +** guaranteed to be a MATCH constraint.
1.134174 +*/
1.134175 +static int testRtreeGeom(
1.134176 +  Rtree *pRtree,                  /* R-Tree object */
1.134177 +  RtreeConstraint *pConstraint,   /* MATCH constraint to test */
1.134178 +  RtreeCell *pCell,               /* Cell to test */
1.134179 +  int *pbRes                      /* OUT: Test result */
1.134180 +){
1.134181 +  int i;
1.134182 +  RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
1.134183 +  int nCoord = pRtree->nDim*2;
1.134184 +
1.134185 +  assert( pConstraint->op==RTREE_MATCH );
1.134186 +  assert( pConstraint->pGeom );
1.134187 +
1.134188 +  for(i=0; i<nCoord; i++){
1.134189 +    aCoord[i] = DCOORD(pCell->aCoord[i]);
1.134190 +  }
1.134191 +  return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
1.134192 +}
1.134193 +
1.134194 +/* 
1.134195 +** Cursor pCursor currently points to a cell in a non-leaf page.
1.134196 +** Set *pbEof to true if the sub-tree headed by the cell is filtered
1.134197 +** (excluded) by the constraints in the pCursor->aConstraint[] 
1.134198 +** array, or false otherwise.
1.134199 +**
1.134200 +** Return SQLITE_OK if successful or an SQLite error code if an error
1.134201 +** occurs within a geometry callback.
1.134202 +*/
1.134203 +static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
1.134204 +  RtreeCell cell;
1.134205 +  int ii;
1.134206 +  int bRes = 0;
1.134207 +  int rc = SQLITE_OK;
1.134208 +
1.134209 +  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
1.134210 +  for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
1.134211 +    RtreeConstraint *p = &pCursor->aConstraint[ii];
1.134212 +    RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
1.134213 +    RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
1.134214 +
1.134215 +    assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
1.134216 +        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
1.134217 +    );
1.134218 +
1.134219 +    switch( p->op ){
1.134220 +      case RTREE_LE: case RTREE_LT: 
1.134221 +        bRes = p->rValue<cell_min; 
1.134222 +        break;
1.134223 +
1.134224 +      case RTREE_GE: case RTREE_GT: 
1.134225 +        bRes = p->rValue>cell_max; 
1.134226 +        break;
1.134227 +
1.134228 +      case RTREE_EQ:
1.134229 +        bRes = (p->rValue>cell_max || p->rValue<cell_min);
1.134230 +        break;
1.134231 +
1.134232 +      default: {
1.134233 +        assert( p->op==RTREE_MATCH );
1.134234 +        rc = testRtreeGeom(pRtree, p, &cell, &bRes);
1.134235 +        bRes = !bRes;
1.134236 +        break;
1.134237 +      }
1.134238 +    }
1.134239 +  }
1.134240 +
1.134241 +  *pbEof = bRes;
1.134242 +  return rc;
1.134243 +}
1.134244 +
1.134245 +/* 
1.134246 +** Test if the cell that cursor pCursor currently points to
1.134247 +** would be filtered (excluded) by the constraints in the 
1.134248 +** pCursor->aConstraint[] array. If so, set *pbEof to true before
1.134249 +** returning. If the cell is not filtered (excluded) by the constraints,
1.134250 +** set pbEof to zero.
1.134251 +**
1.134252 +** Return SQLITE_OK if successful or an SQLite error code if an error
1.134253 +** occurs within a geometry callback.
1.134254 +**
1.134255 +** This function assumes that the cell is part of a leaf node.
1.134256 +*/
1.134257 +static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
1.134258 +  RtreeCell cell;
1.134259 +  int ii;
1.134260 +  *pbEof = 0;
1.134261 +
1.134262 +  nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
1.134263 +  for(ii=0; ii<pCursor->nConstraint; ii++){
1.134264 +    RtreeConstraint *p = &pCursor->aConstraint[ii];
1.134265 +    RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
1.134266 +    int res;
1.134267 +    assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
1.134268 +        || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
1.134269 +    );
1.134270 +    switch( p->op ){
1.134271 +      case RTREE_LE: res = (coord<=p->rValue); break;
1.134272 +      case RTREE_LT: res = (coord<p->rValue);  break;
1.134273 +      case RTREE_GE: res = (coord>=p->rValue); break;
1.134274 +      case RTREE_GT: res = (coord>p->rValue);  break;
1.134275 +      case RTREE_EQ: res = (coord==p->rValue); break;
1.134276 +      default: {
1.134277 +        int rc;
1.134278 +        assert( p->op==RTREE_MATCH );
1.134279 +        rc = testRtreeGeom(pRtree, p, &cell, &res);
1.134280 +        if( rc!=SQLITE_OK ){
1.134281 +          return rc;
1.134282 +        }
1.134283 +        break;
1.134284 +      }
1.134285 +    }
1.134286 +
1.134287 +    if( !res ){
1.134288 +      *pbEof = 1;
1.134289 +      return SQLITE_OK;
1.134290 +    }
1.134291 +  }
1.134292 +
1.134293 +  return SQLITE_OK;
1.134294 +}
1.134295 +
1.134296 +/*
1.134297 +** Cursor pCursor currently points at a node that heads a sub-tree of
1.134298 +** height iHeight (if iHeight==0, then the node is a leaf). Descend
1.134299 +** to point to the left-most cell of the sub-tree that matches the 
1.134300 +** configured constraints.
1.134301 +*/
1.134302 +static int descendToCell(
1.134303 +  Rtree *pRtree, 
1.134304 +  RtreeCursor *pCursor, 
1.134305 +  int iHeight,
1.134306 +  int *pEof                 /* OUT: Set to true if cannot descend */
1.134307 +){
1.134308 +  int isEof;
1.134309 +  int rc;
1.134310 +  int ii;
1.134311 +  RtreeNode *pChild;
1.134312 +  sqlite3_int64 iRowid;
1.134313 +
1.134314 +  RtreeNode *pSavedNode = pCursor->pNode;
1.134315 +  int iSavedCell = pCursor->iCell;
1.134316 +
1.134317 +  assert( iHeight>=0 );
1.134318 +
1.134319 +  if( iHeight==0 ){
1.134320 +    rc = testRtreeEntry(pRtree, pCursor, &isEof);
1.134321 +  }else{
1.134322 +    rc = testRtreeCell(pRtree, pCursor, &isEof);
1.134323 +  }
1.134324 +  if( rc!=SQLITE_OK || isEof || iHeight==0 ){
1.134325 +    goto descend_to_cell_out;
1.134326 +  }
1.134327 +
1.134328 +  iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
1.134329 +  rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
1.134330 +  if( rc!=SQLITE_OK ){
1.134331 +    goto descend_to_cell_out;
1.134332 +  }
1.134333 +
1.134334 +  nodeRelease(pRtree, pCursor->pNode);
1.134335 +  pCursor->pNode = pChild;
1.134336 +  isEof = 1;
1.134337 +  for(ii=0; isEof && ii<NCELL(pChild); ii++){
1.134338 +    pCursor->iCell = ii;
1.134339 +    rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
1.134340 +    if( rc!=SQLITE_OK ){
1.134341 +      goto descend_to_cell_out;
1.134342 +    }
1.134343 +  }
1.134344 +
1.134345 +  if( isEof ){
1.134346 +    assert( pCursor->pNode==pChild );
1.134347 +    nodeReference(pSavedNode);
1.134348 +    nodeRelease(pRtree, pChild);
1.134349 +    pCursor->pNode = pSavedNode;
1.134350 +    pCursor->iCell = iSavedCell;
1.134351 +  }
1.134352 +
1.134353 +descend_to_cell_out:
1.134354 +  *pEof = isEof;
1.134355 +  return rc;
1.134356 +}
1.134357 +
1.134358 +/*
1.134359 +** One of the cells in node pNode is guaranteed to have a 64-bit 
1.134360 +** integer value equal to iRowid. Return the index of this cell.
1.134361 +*/
1.134362 +static int nodeRowidIndex(
1.134363 +  Rtree *pRtree, 
1.134364 +  RtreeNode *pNode, 
1.134365 +  i64 iRowid,
1.134366 +  int *piIndex
1.134367 +){
1.134368 +  int ii;
1.134369 +  int nCell = NCELL(pNode);
1.134370 +  for(ii=0; ii<nCell; ii++){
1.134371 +    if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
1.134372 +      *piIndex = ii;
1.134373 +      return SQLITE_OK;
1.134374 +    }
1.134375 +  }
1.134376 +  return SQLITE_CORRUPT_VTAB;
1.134377 +}
1.134378 +
1.134379 +/*
1.134380 +** Return the index of the cell containing a pointer to node pNode
1.134381 +** in its parent. If pNode is the root node, return -1.
1.134382 +*/
1.134383 +static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
1.134384 +  RtreeNode *pParent = pNode->pParent;
1.134385 +  if( pParent ){
1.134386 +    return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
1.134387 +  }
1.134388 +  *piIndex = -1;
1.134389 +  return SQLITE_OK;
1.134390 +}
1.134391 +
1.134392 +/* 
1.134393 +** Rtree virtual table module xNext method.
1.134394 +*/
1.134395 +static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
1.134396 +  Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
1.134397 +  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
1.134398 +  int rc = SQLITE_OK;
1.134399 +
1.134400 +  /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
1.134401 +  ** already at EOF. It is against the rules to call the xNext() method of
1.134402 +  ** a cursor that has already reached EOF.
1.134403 +  */
1.134404 +  assert( pCsr->pNode );
1.134405 +
1.134406 +  if( pCsr->iStrategy==1 ){
1.134407 +    /* This "scan" is a direct lookup by rowid. There is no next entry. */
1.134408 +    nodeRelease(pRtree, pCsr->pNode);
1.134409 +    pCsr->pNode = 0;
1.134410 +  }else{
1.134411 +    /* Move to the next entry that matches the configured constraints. */
1.134412 +    int iHeight = 0;
1.134413 +    while( pCsr->pNode ){
1.134414 +      RtreeNode *pNode = pCsr->pNode;
1.134415 +      int nCell = NCELL(pNode);
1.134416 +      for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
1.134417 +        int isEof;
1.134418 +        rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
1.134419 +        if( rc!=SQLITE_OK || !isEof ){
1.134420 +          return rc;
1.134421 +        }
1.134422 +      }
1.134423 +      pCsr->pNode = pNode->pParent;
1.134424 +      rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
1.134425 +      if( rc!=SQLITE_OK ){
1.134426 +        return rc;
1.134427 +      }
1.134428 +      nodeReference(pCsr->pNode);
1.134429 +      nodeRelease(pRtree, pNode);
1.134430 +      iHeight++;
1.134431 +    }
1.134432 +  }
1.134433 +
1.134434 +  return rc;
1.134435 +}
1.134436 +
1.134437 +/* 
1.134438 +** Rtree virtual table module xRowid method.
1.134439 +*/
1.134440 +static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
1.134441 +  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
1.134442 +  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
1.134443 +
1.134444 +  assert(pCsr->pNode);
1.134445 +  *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
1.134446 +
1.134447 +  return SQLITE_OK;
1.134448 +}
1.134449 +
1.134450 +/* 
1.134451 +** Rtree virtual table module xColumn method.
1.134452 +*/
1.134453 +static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
1.134454 +  Rtree *pRtree = (Rtree *)cur->pVtab;
1.134455 +  RtreeCursor *pCsr = (RtreeCursor *)cur;
1.134456 +
1.134457 +  if( i==0 ){
1.134458 +    i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
1.134459 +    sqlite3_result_int64(ctx, iRowid);
1.134460 +  }else{
1.134461 +    RtreeCoord c;
1.134462 +    nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
1.134463 +#ifndef SQLITE_RTREE_INT_ONLY
1.134464 +    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
1.134465 +      sqlite3_result_double(ctx, c.f);
1.134466 +    }else
1.134467 +#endif
1.134468 +    {
1.134469 +      assert( pRtree->eCoordType==RTREE_COORD_INT32 );
1.134470 +      sqlite3_result_int(ctx, c.i);
1.134471 +    }
1.134472 +  }
1.134473 +
1.134474 +  return SQLITE_OK;
1.134475 +}
1.134476 +
1.134477 +/* 
1.134478 +** Use nodeAcquire() to obtain the leaf node containing the record with 
1.134479 +** rowid iRowid. If successful, set *ppLeaf to point to the node and
1.134480 +** return SQLITE_OK. If there is no such record in the table, set
1.134481 +** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
1.134482 +** to zero and return an SQLite error code.
1.134483 +*/
1.134484 +static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
1.134485 +  int rc;
1.134486 +  *ppLeaf = 0;
1.134487 +  sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
1.134488 +  if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
1.134489 +    i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
1.134490 +    rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
1.134491 +    sqlite3_reset(pRtree->pReadRowid);
1.134492 +  }else{
1.134493 +    rc = sqlite3_reset(pRtree->pReadRowid);
1.134494 +  }
1.134495 +  return rc;
1.134496 +}
1.134497 +
1.134498 +/*
1.134499 +** This function is called to configure the RtreeConstraint object passed
1.134500 +** as the second argument for a MATCH constraint. The value passed as the
1.134501 +** first argument to this function is the right-hand operand to the MATCH
1.134502 +** operator.
1.134503 +*/
1.134504 +static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
1.134505 +  RtreeMatchArg *p;
1.134506 +  sqlite3_rtree_geometry *pGeom;
1.134507 +  int nBlob;
1.134508 +
1.134509 +  /* Check that value is actually a blob. */
1.134510 +  if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
1.134511 +
1.134512 +  /* Check that the blob is roughly the right size. */
1.134513 +  nBlob = sqlite3_value_bytes(pValue);
1.134514 +  if( nBlob<(int)sizeof(RtreeMatchArg) 
1.134515 +   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
1.134516 +  ){
1.134517 +    return SQLITE_ERROR;
1.134518 +  }
1.134519 +
1.134520 +  pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
1.134521 +      sizeof(sqlite3_rtree_geometry) + nBlob
1.134522 +  );
1.134523 +  if( !pGeom ) return SQLITE_NOMEM;
1.134524 +  memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
1.134525 +  p = (RtreeMatchArg *)&pGeom[1];
1.134526 +
1.134527 +  memcpy(p, sqlite3_value_blob(pValue), nBlob);
1.134528 +  if( p->magic!=RTREE_GEOMETRY_MAGIC 
1.134529 +   || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
1.134530 +  ){
1.134531 +    sqlite3_free(pGeom);
1.134532 +    return SQLITE_ERROR;
1.134533 +  }
1.134534 +
1.134535 +  pGeom->pContext = p->pContext;
1.134536 +  pGeom->nParam = p->nParam;
1.134537 +  pGeom->aParam = p->aParam;
1.134538 +
1.134539 +  pCons->xGeom = p->xGeom;
1.134540 +  pCons->pGeom = pGeom;
1.134541 +  return SQLITE_OK;
1.134542 +}
1.134543 +
1.134544 +/* 
1.134545 +** Rtree virtual table module xFilter method.
1.134546 +*/
1.134547 +static int rtreeFilter(
1.134548 +  sqlite3_vtab_cursor *pVtabCursor, 
1.134549 +  int idxNum, const char *idxStr,
1.134550 +  int argc, sqlite3_value **argv
1.134551 +){
1.134552 +  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
1.134553 +  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
1.134554 +
1.134555 +  RtreeNode *pRoot = 0;
1.134556 +  int ii;
1.134557 +  int rc = SQLITE_OK;
1.134558 +
1.134559 +  rtreeReference(pRtree);
1.134560 +
1.134561 +  freeCursorConstraints(pCsr);
1.134562 +  pCsr->iStrategy = idxNum;
1.134563 +
1.134564 +  if( idxNum==1 ){
1.134565 +    /* Special case - lookup by rowid. */
1.134566 +    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
1.134567 +    i64 iRowid = sqlite3_value_int64(argv[0]);
1.134568 +    rc = findLeafNode(pRtree, iRowid, &pLeaf);
1.134569 +    pCsr->pNode = pLeaf; 
1.134570 +    if( pLeaf ){
1.134571 +      assert( rc==SQLITE_OK );
1.134572 +      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
1.134573 +    }
1.134574 +  }else{
1.134575 +    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
1.134576 +    ** with the configured constraints. 
1.134577 +    */
1.134578 +    if( argc>0 ){
1.134579 +      pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
1.134580 +      pCsr->nConstraint = argc;
1.134581 +      if( !pCsr->aConstraint ){
1.134582 +        rc = SQLITE_NOMEM;
1.134583 +      }else{
1.134584 +        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
1.134585 +        assert( (idxStr==0 && argc==0)
1.134586 +                || (idxStr && (int)strlen(idxStr)==argc*2) );
1.134587 +        for(ii=0; ii<argc; ii++){
1.134588 +          RtreeConstraint *p = &pCsr->aConstraint[ii];
1.134589 +          p->op = idxStr[ii*2];
1.134590 +          p->iCoord = idxStr[ii*2+1]-'a';
1.134591 +          if( p->op==RTREE_MATCH ){
1.134592 +            /* A MATCH operator. The right-hand-side must be a blob that
1.134593 +            ** can be cast into an RtreeMatchArg object. One created using
1.134594 +            ** an sqlite3_rtree_geometry_callback() SQL user function.
1.134595 +            */
1.134596 +            rc = deserializeGeometry(argv[ii], p);
1.134597 +            if( rc!=SQLITE_OK ){
1.134598 +              break;
1.134599 +            }
1.134600 +          }else{
1.134601 +#ifdef SQLITE_RTREE_INT_ONLY
1.134602 +            p->rValue = sqlite3_value_int64(argv[ii]);
1.134603 +#else
1.134604 +            p->rValue = sqlite3_value_double(argv[ii]);
1.134605 +#endif
1.134606 +          }
1.134607 +        }
1.134608 +      }
1.134609 +    }
1.134610 +  
1.134611 +    if( rc==SQLITE_OK ){
1.134612 +      pCsr->pNode = 0;
1.134613 +      rc = nodeAcquire(pRtree, 1, 0, &pRoot);
1.134614 +    }
1.134615 +    if( rc==SQLITE_OK ){
1.134616 +      int isEof = 1;
1.134617 +      int nCell = NCELL(pRoot);
1.134618 +      pCsr->pNode = pRoot;
1.134619 +      for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
1.134620 +        assert( pCsr->pNode==pRoot );
1.134621 +        rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
1.134622 +        if( !isEof ){
1.134623 +          break;
1.134624 +        }
1.134625 +      }
1.134626 +      if( rc==SQLITE_OK && isEof ){
1.134627 +        assert( pCsr->pNode==pRoot );
1.134628 +        nodeRelease(pRtree, pRoot);
1.134629 +        pCsr->pNode = 0;
1.134630 +      }
1.134631 +      assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
1.134632 +    }
1.134633 +  }
1.134634 +
1.134635 +  rtreeRelease(pRtree);
1.134636 +  return rc;
1.134637 +}
1.134638 +
1.134639 +/*
1.134640 +** Rtree virtual table module xBestIndex method. There are three
1.134641 +** table scan strategies to choose from (in order from most to 
1.134642 +** least desirable):
1.134643 +**
1.134644 +**   idxNum     idxStr        Strategy
1.134645 +**   ------------------------------------------------
1.134646 +**     1        Unused        Direct lookup by rowid.
1.134647 +**     2        See below     R-tree query or full-table scan.
1.134648 +**   ------------------------------------------------
1.134649 +**
1.134650 +** If strategy 1 is used, then idxStr is not meaningful. If strategy
1.134651 +** 2 is used, idxStr is formatted to contain 2 bytes for each 
1.134652 +** constraint used. The first two bytes of idxStr correspond to 
1.134653 +** the constraint in sqlite3_index_info.aConstraintUsage[] with
1.134654 +** (argvIndex==1) etc.
1.134655 +**
1.134656 +** The first of each pair of bytes in idxStr identifies the constraint
1.134657 +** operator as follows:
1.134658 +**
1.134659 +**   Operator    Byte Value
1.134660 +**   ----------------------
1.134661 +**      =        0x41 ('A')
1.134662 +**     <=        0x42 ('B')
1.134663 +**      <        0x43 ('C')
1.134664 +**     >=        0x44 ('D')
1.134665 +**      >        0x45 ('E')
1.134666 +**   MATCH       0x46 ('F')
1.134667 +**   ----------------------
1.134668 +**
1.134669 +** The second of each pair of bytes identifies the coordinate column
1.134670 +** to which the constraint applies. The leftmost coordinate column
1.134671 +** is 'a', the second from the left 'b' etc.
1.134672 +*/
1.134673 +static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
1.134674 +  int rc = SQLITE_OK;
1.134675 +  int ii;
1.134676 +
1.134677 +  int iIdx = 0;
1.134678 +  char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
1.134679 +  memset(zIdxStr, 0, sizeof(zIdxStr));
1.134680 +  UNUSED_PARAMETER(tab);
1.134681 +
1.134682 +  assert( pIdxInfo->idxStr==0 );
1.134683 +  for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
1.134684 +    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
1.134685 +
1.134686 +    if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
1.134687 +      /* We have an equality constraint on the rowid. Use strategy 1. */
1.134688 +      int jj;
1.134689 +      for(jj=0; jj<ii; jj++){
1.134690 +        pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
1.134691 +        pIdxInfo->aConstraintUsage[jj].omit = 0;
1.134692 +      }
1.134693 +      pIdxInfo->idxNum = 1;
1.134694 +      pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
1.134695 +      pIdxInfo->aConstraintUsage[jj].omit = 1;
1.134696 +
1.134697 +      /* This strategy involves a two rowid lookups on an B-Tree structures
1.134698 +      ** and then a linear search of an R-Tree node. This should be 
1.134699 +      ** considered almost as quick as a direct rowid lookup (for which 
1.134700 +      ** sqlite uses an internal cost of 0.0).
1.134701 +      */ 
1.134702 +      pIdxInfo->estimatedCost = 10.0;
1.134703 +      return SQLITE_OK;
1.134704 +    }
1.134705 +
1.134706 +    if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
1.134707 +      u8 op;
1.134708 +      switch( p->op ){
1.134709 +        case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
1.134710 +        case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
1.134711 +        case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
1.134712 +        case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
1.134713 +        case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
1.134714 +        default:
1.134715 +          assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
1.134716 +          op = RTREE_MATCH; 
1.134717 +          break;
1.134718 +      }
1.134719 +      zIdxStr[iIdx++] = op;
1.134720 +      zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
1.134721 +      pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
1.134722 +      pIdxInfo->aConstraintUsage[ii].omit = 1;
1.134723 +    }
1.134724 +  }
1.134725 +
1.134726 +  pIdxInfo->idxNum = 2;
1.134727 +  pIdxInfo->needToFreeIdxStr = 1;
1.134728 +  if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
1.134729 +    return SQLITE_NOMEM;
1.134730 +  }
1.134731 +  assert( iIdx>=0 );
1.134732 +  pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
1.134733 +  return rc;
1.134734 +}
1.134735 +
1.134736 +/*
1.134737 +** Return the N-dimensional volumn of the cell stored in *p.
1.134738 +*/
1.134739 +static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
1.134740 +  RtreeDValue area = (RtreeDValue)1;
1.134741 +  int ii;
1.134742 +  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
1.134743 +    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
1.134744 +  }
1.134745 +  return area;
1.134746 +}
1.134747 +
1.134748 +/*
1.134749 +** Return the margin length of cell p. The margin length is the sum
1.134750 +** of the objects size in each dimension.
1.134751 +*/
1.134752 +static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
1.134753 +  RtreeDValue margin = (RtreeDValue)0;
1.134754 +  int ii;
1.134755 +  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
1.134756 +    margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
1.134757 +  }
1.134758 +  return margin;
1.134759 +}
1.134760 +
1.134761 +/*
1.134762 +** Store the union of cells p1 and p2 in p1.
1.134763 +*/
1.134764 +static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
1.134765 +  int ii;
1.134766 +  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
1.134767 +    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
1.134768 +      p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
1.134769 +      p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
1.134770 +    }
1.134771 +  }else{
1.134772 +    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
1.134773 +      p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
1.134774 +      p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
1.134775 +    }
1.134776 +  }
1.134777 +}
1.134778 +
1.134779 +/*
1.134780 +** Return true if the area covered by p2 is a subset of the area covered
1.134781 +** by p1. False otherwise.
1.134782 +*/
1.134783 +static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
1.134784 +  int ii;
1.134785 +  int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
1.134786 +  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
1.134787 +    RtreeCoord *a1 = &p1->aCoord[ii];
1.134788 +    RtreeCoord *a2 = &p2->aCoord[ii];
1.134789 +    if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
1.134790 +     || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
1.134791 +    ){
1.134792 +      return 0;
1.134793 +    }
1.134794 +  }
1.134795 +  return 1;
1.134796 +}
1.134797 +
1.134798 +/*
1.134799 +** Return the amount cell p would grow by if it were unioned with pCell.
1.134800 +*/
1.134801 +static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
1.134802 +  RtreeDValue area;
1.134803 +  RtreeCell cell;
1.134804 +  memcpy(&cell, p, sizeof(RtreeCell));
1.134805 +  area = cellArea(pRtree, &cell);
1.134806 +  cellUnion(pRtree, &cell, pCell);
1.134807 +  return (cellArea(pRtree, &cell)-area);
1.134808 +}
1.134809 +
1.134810 +#if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
1.134811 +static RtreeDValue cellOverlap(
1.134812 +  Rtree *pRtree, 
1.134813 +  RtreeCell *p, 
1.134814 +  RtreeCell *aCell, 
1.134815 +  int nCell, 
1.134816 +  int iExclude
1.134817 +){
1.134818 +  int ii;
1.134819 +  RtreeDValue overlap = 0.0;
1.134820 +  for(ii=0; ii<nCell; ii++){
1.134821 +#if VARIANT_RSTARTREE_CHOOSESUBTREE
1.134822 +    if( ii!=iExclude )
1.134823 +#else
1.134824 +    assert( iExclude==-1 );
1.134825 +    UNUSED_PARAMETER(iExclude);
1.134826 +#endif
1.134827 +    {
1.134828 +      int jj;
1.134829 +      RtreeDValue o = (RtreeDValue)1;
1.134830 +      for(jj=0; jj<(pRtree->nDim*2); jj+=2){
1.134831 +        RtreeDValue x1, x2;
1.134832 +
1.134833 +        x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
1.134834 +        x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
1.134835 +
1.134836 +        if( x2<x1 ){
1.134837 +          o = 0.0;
1.134838 +          break;
1.134839 +        }else{
1.134840 +          o = o * (x2-x1);
1.134841 +        }
1.134842 +      }
1.134843 +      overlap += o;
1.134844 +    }
1.134845 +  }
1.134846 +  return overlap;
1.134847 +}
1.134848 +#endif
1.134849 +
1.134850 +#if VARIANT_RSTARTREE_CHOOSESUBTREE
1.134851 +static RtreeDValue cellOverlapEnlargement(
1.134852 +  Rtree *pRtree, 
1.134853 +  RtreeCell *p, 
1.134854 +  RtreeCell *pInsert, 
1.134855 +  RtreeCell *aCell, 
1.134856 +  int nCell, 
1.134857 +  int iExclude
1.134858 +){
1.134859 +  RtreeDValue before, after;
1.134860 +  before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
1.134861 +  cellUnion(pRtree, p, pInsert);
1.134862 +  after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
1.134863 +  return (after-before);
1.134864 +}
1.134865 +#endif
1.134866 +
1.134867 +
1.134868 +/*
1.134869 +** This function implements the ChooseLeaf algorithm from Gutman[84].
1.134870 +** ChooseSubTree in r*tree terminology.
1.134871 +*/
1.134872 +static int ChooseLeaf(
1.134873 +  Rtree *pRtree,               /* Rtree table */
1.134874 +  RtreeCell *pCell,            /* Cell to insert into rtree */
1.134875 +  int iHeight,                 /* Height of sub-tree rooted at pCell */
1.134876 +  RtreeNode **ppLeaf           /* OUT: Selected leaf page */
1.134877 +){
1.134878 +  int rc;
1.134879 +  int ii;
1.134880 +  RtreeNode *pNode;
1.134881 +  rc = nodeAcquire(pRtree, 1, 0, &pNode);
1.134882 +
1.134883 +  for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
1.134884 +    int iCell;
1.134885 +    sqlite3_int64 iBest = 0;
1.134886 +
1.134887 +    RtreeDValue fMinGrowth = 0.0;
1.134888 +    RtreeDValue fMinArea = 0.0;
1.134889 +#if VARIANT_RSTARTREE_CHOOSESUBTREE
1.134890 +    RtreeDValue fMinOverlap = 0.0;
1.134891 +    RtreeDValue overlap;
1.134892 +#endif
1.134893 +
1.134894 +    int nCell = NCELL(pNode);
1.134895 +    RtreeCell cell;
1.134896 +    RtreeNode *pChild;
1.134897 +
1.134898 +    RtreeCell *aCell = 0;
1.134899 +
1.134900 +#if VARIANT_RSTARTREE_CHOOSESUBTREE
1.134901 +    if( ii==(pRtree->iDepth-1) ){
1.134902 +      int jj;
1.134903 +      aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
1.134904 +      if( !aCell ){
1.134905 +        rc = SQLITE_NOMEM;
1.134906 +        nodeRelease(pRtree, pNode);
1.134907 +        pNode = 0;
1.134908 +        continue;
1.134909 +      }
1.134910 +      for(jj=0; jj<nCell; jj++){
1.134911 +        nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
1.134912 +      }
1.134913 +    }
1.134914 +#endif
1.134915 +
1.134916 +    /* Select the child node which will be enlarged the least if pCell
1.134917 +    ** is inserted into it. Resolve ties by choosing the entry with
1.134918 +    ** the smallest area.
1.134919 +    */
1.134920 +    for(iCell=0; iCell<nCell; iCell++){
1.134921 +      int bBest = 0;
1.134922 +      RtreeDValue growth;
1.134923 +      RtreeDValue area;
1.134924 +      nodeGetCell(pRtree, pNode, iCell, &cell);
1.134925 +      growth = cellGrowth(pRtree, &cell, pCell);
1.134926 +      area = cellArea(pRtree, &cell);
1.134927 +
1.134928 +#if VARIANT_RSTARTREE_CHOOSESUBTREE
1.134929 +      if( ii==(pRtree->iDepth-1) ){
1.134930 +        overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
1.134931 +      }else{
1.134932 +        overlap = 0.0;
1.134933 +      }
1.134934 +      if( (iCell==0) 
1.134935 +       || (overlap<fMinOverlap) 
1.134936 +       || (overlap==fMinOverlap && growth<fMinGrowth)
1.134937 +       || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
1.134938 +      ){
1.134939 +        bBest = 1;
1.134940 +        fMinOverlap = overlap;
1.134941 +      }
1.134942 +#else
1.134943 +      if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
1.134944 +        bBest = 1;
1.134945 +      }
1.134946 +#endif
1.134947 +      if( bBest ){
1.134948 +        fMinGrowth = growth;
1.134949 +        fMinArea = area;
1.134950 +        iBest = cell.iRowid;
1.134951 +      }
1.134952 +    }
1.134953 +
1.134954 +    sqlite3_free(aCell);
1.134955 +    rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
1.134956 +    nodeRelease(pRtree, pNode);
1.134957 +    pNode = pChild;
1.134958 +  }
1.134959 +
1.134960 +  *ppLeaf = pNode;
1.134961 +  return rc;
1.134962 +}
1.134963 +
1.134964 +/*
1.134965 +** A cell with the same content as pCell has just been inserted into
1.134966 +** the node pNode. This function updates the bounding box cells in
1.134967 +** all ancestor elements.
1.134968 +*/
1.134969 +static int AdjustTree(
1.134970 +  Rtree *pRtree,                    /* Rtree table */
1.134971 +  RtreeNode *pNode,                 /* Adjust ancestry of this node. */
1.134972 +  RtreeCell *pCell                  /* This cell was just inserted */
1.134973 +){
1.134974 +  RtreeNode *p = pNode;
1.134975 +  while( p->pParent ){
1.134976 +    RtreeNode *pParent = p->pParent;
1.134977 +    RtreeCell cell;
1.134978 +    int iCell;
1.134979 +
1.134980 +    if( nodeParentIndex(pRtree, p, &iCell) ){
1.134981 +      return SQLITE_CORRUPT_VTAB;
1.134982 +    }
1.134983 +
1.134984 +    nodeGetCell(pRtree, pParent, iCell, &cell);
1.134985 +    if( !cellContains(pRtree, &cell, pCell) ){
1.134986 +      cellUnion(pRtree, &cell, pCell);
1.134987 +      nodeOverwriteCell(pRtree, pParent, &cell, iCell);
1.134988 +    }
1.134989 + 
1.134990 +    p = pParent;
1.134991 +  }
1.134992 +  return SQLITE_OK;
1.134993 +}
1.134994 +
1.134995 +/*
1.134996 +** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
1.134997 +*/
1.134998 +static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
1.134999 +  sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
1.135000 +  sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
1.135001 +  sqlite3_step(pRtree->pWriteRowid);
1.135002 +  return sqlite3_reset(pRtree->pWriteRowid);
1.135003 +}
1.135004 +
1.135005 +/*
1.135006 +** Write mapping (iNode->iPar) to the <rtree>_parent table.
1.135007 +*/
1.135008 +static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
1.135009 +  sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
1.135010 +  sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
1.135011 +  sqlite3_step(pRtree->pWriteParent);
1.135012 +  return sqlite3_reset(pRtree->pWriteParent);
1.135013 +}
1.135014 +
1.135015 +static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
1.135016 +
1.135017 +#if VARIANT_GUTTMAN_LINEAR_SPLIT
1.135018 +/*
1.135019 +** Implementation of the linear variant of the PickNext() function from
1.135020 +** Guttman[84].
1.135021 +*/
1.135022 +static RtreeCell *LinearPickNext(
1.135023 +  Rtree *pRtree,
1.135024 +  RtreeCell *aCell, 
1.135025 +  int nCell, 
1.135026 +  RtreeCell *pLeftBox, 
1.135027 +  RtreeCell *pRightBox,
1.135028 +  int *aiUsed
1.135029 +){
1.135030 +  int ii;
1.135031 +  for(ii=0; aiUsed[ii]; ii++);
1.135032 +  aiUsed[ii] = 1;
1.135033 +  return &aCell[ii];
1.135034 +}
1.135035 +
1.135036 +/*
1.135037 +** Implementation of the linear variant of the PickSeeds() function from
1.135038 +** Guttman[84].
1.135039 +*/
1.135040 +static void LinearPickSeeds(
1.135041 +  Rtree *pRtree,
1.135042 +  RtreeCell *aCell, 
1.135043 +  int nCell, 
1.135044 +  int *piLeftSeed, 
1.135045 +  int *piRightSeed
1.135046 +){
1.135047 +  int i;
1.135048 +  int iLeftSeed = 0;
1.135049 +  int iRightSeed = 1;
1.135050 +  RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
1.135051 +
1.135052 +  /* Pick two "seed" cells from the array of cells. The algorithm used
1.135053 +  ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
1.135054 +  ** indices of the two seed cells in the array are stored in local
1.135055 +  ** variables iLeftSeek and iRightSeed.
1.135056 +  */
1.135057 +  for(i=0; i<pRtree->nDim; i++){
1.135058 +    RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
1.135059 +    RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
1.135060 +    RtreeDValue x3 = x1;
1.135061 +    RtreeDValue x4 = x2;
1.135062 +    int jj;
1.135063 +
1.135064 +    int iCellLeft = 0;
1.135065 +    int iCellRight = 0;
1.135066 +
1.135067 +    for(jj=1; jj<nCell; jj++){
1.135068 +      RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
1.135069 +      RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
1.135070 +
1.135071 +      if( left<x1 ) x1 = left;
1.135072 +      if( right>x4 ) x4 = right;
1.135073 +      if( left>x3 ){
1.135074 +        x3 = left;
1.135075 +        iCellRight = jj;
1.135076 +      }
1.135077 +      if( right<x2 ){
1.135078 +        x2 = right;
1.135079 +        iCellLeft = jj;
1.135080 +      }
1.135081 +    }
1.135082 +
1.135083 +    if( x4!=x1 ){
1.135084 +      RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
1.135085 +      if( normalwidth>maxNormalInnerWidth ){
1.135086 +        iLeftSeed = iCellLeft;
1.135087 +        iRightSeed = iCellRight;
1.135088 +      }
1.135089 +    }
1.135090 +  }
1.135091 +
1.135092 +  *piLeftSeed = iLeftSeed;
1.135093 +  *piRightSeed = iRightSeed;
1.135094 +}
1.135095 +#endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
1.135096 +
1.135097 +#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
1.135098 +/*
1.135099 +** Implementation of the quadratic variant of the PickNext() function from
1.135100 +** Guttman[84].
1.135101 +*/
1.135102 +static RtreeCell *QuadraticPickNext(
1.135103 +  Rtree *pRtree,
1.135104 +  RtreeCell *aCell, 
1.135105 +  int nCell, 
1.135106 +  RtreeCell *pLeftBox, 
1.135107 +  RtreeCell *pRightBox,
1.135108 +  int *aiUsed
1.135109 +){
1.135110 +  #define FABS(a) ((a)<0.0?-1.0*(a):(a))
1.135111 +
1.135112 +  int iSelect = -1;
1.135113 +  RtreeDValue fDiff;
1.135114 +  int ii;
1.135115 +  for(ii=0; ii<nCell; ii++){
1.135116 +    if( aiUsed[ii]==0 ){
1.135117 +      RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
1.135118 +      RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
1.135119 +      RtreeDValue diff = FABS(right-left);
1.135120 +      if( iSelect<0 || diff>fDiff ){
1.135121 +        fDiff = diff;
1.135122 +        iSelect = ii;
1.135123 +      }
1.135124 +    }
1.135125 +  }
1.135126 +  aiUsed[iSelect] = 1;
1.135127 +  return &aCell[iSelect];
1.135128 +}
1.135129 +
1.135130 +/*
1.135131 +** Implementation of the quadratic variant of the PickSeeds() function from
1.135132 +** Guttman[84].
1.135133 +*/
1.135134 +static void QuadraticPickSeeds(
1.135135 +  Rtree *pRtree,
1.135136 +  RtreeCell *aCell, 
1.135137 +  int nCell, 
1.135138 +  int *piLeftSeed, 
1.135139 +  int *piRightSeed
1.135140 +){
1.135141 +  int ii;
1.135142 +  int jj;
1.135143 +
1.135144 +  int iLeftSeed = 0;
1.135145 +  int iRightSeed = 1;
1.135146 +  RtreeDValue fWaste = 0.0;
1.135147 +
1.135148 +  for(ii=0; ii<nCell; ii++){
1.135149 +    for(jj=ii+1; jj<nCell; jj++){
1.135150 +      RtreeDValue right = cellArea(pRtree, &aCell[jj]);
1.135151 +      RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
1.135152 +      RtreeDValue waste = growth - right;
1.135153 +
1.135154 +      if( waste>fWaste ){
1.135155 +        iLeftSeed = ii;
1.135156 +        iRightSeed = jj;
1.135157 +        fWaste = waste;
1.135158 +      }
1.135159 +    }
1.135160 +  }
1.135161 +
1.135162 +  *piLeftSeed = iLeftSeed;
1.135163 +  *piRightSeed = iRightSeed;
1.135164 +}
1.135165 +#endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
1.135166 +
1.135167 +/*
1.135168 +** Arguments aIdx, aDistance and aSpare all point to arrays of size
1.135169 +** nIdx. The aIdx array contains the set of integers from 0 to 
1.135170 +** (nIdx-1) in no particular order. This function sorts the values
1.135171 +** in aIdx according to the indexed values in aDistance. For
1.135172 +** example, assuming the inputs:
1.135173 +**
1.135174 +**   aIdx      = { 0,   1,   2,   3 }
1.135175 +**   aDistance = { 5.0, 2.0, 7.0, 6.0 }
1.135176 +**
1.135177 +** this function sets the aIdx array to contain:
1.135178 +**
1.135179 +**   aIdx      = { 0,   1,   2,   3 }
1.135180 +**
1.135181 +** The aSpare array is used as temporary working space by the
1.135182 +** sorting algorithm.
1.135183 +*/
1.135184 +static void SortByDistance(
1.135185 +  int *aIdx, 
1.135186 +  int nIdx, 
1.135187 +  RtreeDValue *aDistance, 
1.135188 +  int *aSpare
1.135189 +){
1.135190 +  if( nIdx>1 ){
1.135191 +    int iLeft = 0;
1.135192 +    int iRight = 0;
1.135193 +
1.135194 +    int nLeft = nIdx/2;
1.135195 +    int nRight = nIdx-nLeft;
1.135196 +    int *aLeft = aIdx;
1.135197 +    int *aRight = &aIdx[nLeft];
1.135198 +
1.135199 +    SortByDistance(aLeft, nLeft, aDistance, aSpare);
1.135200 +    SortByDistance(aRight, nRight, aDistance, aSpare);
1.135201 +
1.135202 +    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
1.135203 +    aLeft = aSpare;
1.135204 +
1.135205 +    while( iLeft<nLeft || iRight<nRight ){
1.135206 +      if( iLeft==nLeft ){
1.135207 +        aIdx[iLeft+iRight] = aRight[iRight];
1.135208 +        iRight++;
1.135209 +      }else if( iRight==nRight ){
1.135210 +        aIdx[iLeft+iRight] = aLeft[iLeft];
1.135211 +        iLeft++;
1.135212 +      }else{
1.135213 +        RtreeDValue fLeft = aDistance[aLeft[iLeft]];
1.135214 +        RtreeDValue fRight = aDistance[aRight[iRight]];
1.135215 +        if( fLeft<fRight ){
1.135216 +          aIdx[iLeft+iRight] = aLeft[iLeft];
1.135217 +          iLeft++;
1.135218 +        }else{
1.135219 +          aIdx[iLeft+iRight] = aRight[iRight];
1.135220 +          iRight++;
1.135221 +        }
1.135222 +      }
1.135223 +    }
1.135224 +
1.135225 +#if 0
1.135226 +    /* Check that the sort worked */
1.135227 +    {
1.135228 +      int jj;
1.135229 +      for(jj=1; jj<nIdx; jj++){
1.135230 +        RtreeDValue left = aDistance[aIdx[jj-1]];
1.135231 +        RtreeDValue right = aDistance[aIdx[jj]];
1.135232 +        assert( left<=right );
1.135233 +      }
1.135234 +    }
1.135235 +#endif
1.135236 +  }
1.135237 +}
1.135238 +
1.135239 +/*
1.135240 +** Arguments aIdx, aCell and aSpare all point to arrays of size
1.135241 +** nIdx. The aIdx array contains the set of integers from 0 to 
1.135242 +** (nIdx-1) in no particular order. This function sorts the values
1.135243 +** in aIdx according to dimension iDim of the cells in aCell. The
1.135244 +** minimum value of dimension iDim is considered first, the
1.135245 +** maximum used to break ties.
1.135246 +**
1.135247 +** The aSpare array is used as temporary working space by the
1.135248 +** sorting algorithm.
1.135249 +*/
1.135250 +static void SortByDimension(
1.135251 +  Rtree *pRtree,
1.135252 +  int *aIdx, 
1.135253 +  int nIdx, 
1.135254 +  int iDim, 
1.135255 +  RtreeCell *aCell, 
1.135256 +  int *aSpare
1.135257 +){
1.135258 +  if( nIdx>1 ){
1.135259 +
1.135260 +    int iLeft = 0;
1.135261 +    int iRight = 0;
1.135262 +
1.135263 +    int nLeft = nIdx/2;
1.135264 +    int nRight = nIdx-nLeft;
1.135265 +    int *aLeft = aIdx;
1.135266 +    int *aRight = &aIdx[nLeft];
1.135267 +
1.135268 +    SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
1.135269 +    SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
1.135270 +
1.135271 +    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
1.135272 +    aLeft = aSpare;
1.135273 +    while( iLeft<nLeft || iRight<nRight ){
1.135274 +      RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
1.135275 +      RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
1.135276 +      RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
1.135277 +      RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
1.135278 +      if( (iLeft!=nLeft) && ((iRight==nRight)
1.135279 +       || (xleft1<xright1)
1.135280 +       || (xleft1==xright1 && xleft2<xright2)
1.135281 +      )){
1.135282 +        aIdx[iLeft+iRight] = aLeft[iLeft];
1.135283 +        iLeft++;
1.135284 +      }else{
1.135285 +        aIdx[iLeft+iRight] = aRight[iRight];
1.135286 +        iRight++;
1.135287 +      }
1.135288 +    }
1.135289 +
1.135290 +#if 0
1.135291 +    /* Check that the sort worked */
1.135292 +    {
1.135293 +      int jj;
1.135294 +      for(jj=1; jj<nIdx; jj++){
1.135295 +        RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
1.135296 +        RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
1.135297 +        RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
1.135298 +        RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
1.135299 +        assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
1.135300 +      }
1.135301 +    }
1.135302 +#endif
1.135303 +  }
1.135304 +}
1.135305 +
1.135306 +#if VARIANT_RSTARTREE_SPLIT
1.135307 +/*
1.135308 +** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
1.135309 +*/
1.135310 +static int splitNodeStartree(
1.135311 +  Rtree *pRtree,
1.135312 +  RtreeCell *aCell,
1.135313 +  int nCell,
1.135314 +  RtreeNode *pLeft,
1.135315 +  RtreeNode *pRight,
1.135316 +  RtreeCell *pBboxLeft,
1.135317 +  RtreeCell *pBboxRight
1.135318 +){
1.135319 +  int **aaSorted;
1.135320 +  int *aSpare;
1.135321 +  int ii;
1.135322 +
1.135323 +  int iBestDim = 0;
1.135324 +  int iBestSplit = 0;
1.135325 +  RtreeDValue fBestMargin = 0.0;
1.135326 +
1.135327 +  int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
1.135328 +
1.135329 +  aaSorted = (int **)sqlite3_malloc(nByte);
1.135330 +  if( !aaSorted ){
1.135331 +    return SQLITE_NOMEM;
1.135332 +  }
1.135333 +
1.135334 +  aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
1.135335 +  memset(aaSorted, 0, nByte);
1.135336 +  for(ii=0; ii<pRtree->nDim; ii++){
1.135337 +    int jj;
1.135338 +    aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
1.135339 +    for(jj=0; jj<nCell; jj++){
1.135340 +      aaSorted[ii][jj] = jj;
1.135341 +    }
1.135342 +    SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
1.135343 +  }
1.135344 +
1.135345 +  for(ii=0; ii<pRtree->nDim; ii++){
1.135346 +    RtreeDValue margin = 0.0;
1.135347 +    RtreeDValue fBestOverlap = 0.0;
1.135348 +    RtreeDValue fBestArea = 0.0;
1.135349 +    int iBestLeft = 0;
1.135350 +    int nLeft;
1.135351 +
1.135352 +    for(
1.135353 +      nLeft=RTREE_MINCELLS(pRtree); 
1.135354 +      nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
1.135355 +      nLeft++
1.135356 +    ){
1.135357 +      RtreeCell left;
1.135358 +      RtreeCell right;
1.135359 +      int kk;
1.135360 +      RtreeDValue overlap;
1.135361 +      RtreeDValue area;
1.135362 +
1.135363 +      memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
1.135364 +      memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
1.135365 +      for(kk=1; kk<(nCell-1); kk++){
1.135366 +        if( kk<nLeft ){
1.135367 +          cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
1.135368 +        }else{
1.135369 +          cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
1.135370 +        }
1.135371 +      }
1.135372 +      margin += cellMargin(pRtree, &left);
1.135373 +      margin += cellMargin(pRtree, &right);
1.135374 +      overlap = cellOverlap(pRtree, &left, &right, 1, -1);
1.135375 +      area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
1.135376 +      if( (nLeft==RTREE_MINCELLS(pRtree))
1.135377 +       || (overlap<fBestOverlap)
1.135378 +       || (overlap==fBestOverlap && area<fBestArea)
1.135379 +      ){
1.135380 +        iBestLeft = nLeft;
1.135381 +        fBestOverlap = overlap;
1.135382 +        fBestArea = area;
1.135383 +      }
1.135384 +    }
1.135385 +
1.135386 +    if( ii==0 || margin<fBestMargin ){
1.135387 +      iBestDim = ii;
1.135388 +      fBestMargin = margin;
1.135389 +      iBestSplit = iBestLeft;
1.135390 +    }
1.135391 +  }
1.135392 +
1.135393 +  memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
1.135394 +  memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
1.135395 +  for(ii=0; ii<nCell; ii++){
1.135396 +    RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
1.135397 +    RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
1.135398 +    RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
1.135399 +    nodeInsertCell(pRtree, pTarget, pCell);
1.135400 +    cellUnion(pRtree, pBbox, pCell);
1.135401 +  }
1.135402 +
1.135403 +  sqlite3_free(aaSorted);
1.135404 +  return SQLITE_OK;
1.135405 +}
1.135406 +#endif
1.135407 +
1.135408 +#if VARIANT_GUTTMAN_SPLIT
1.135409 +/*
1.135410 +** Implementation of the regular R-tree SplitNode from Guttman[1984].
1.135411 +*/
1.135412 +static int splitNodeGuttman(
1.135413 +  Rtree *pRtree,
1.135414 +  RtreeCell *aCell,
1.135415 +  int nCell,
1.135416 +  RtreeNode *pLeft,
1.135417 +  RtreeNode *pRight,
1.135418 +  RtreeCell *pBboxLeft,
1.135419 +  RtreeCell *pBboxRight
1.135420 +){
1.135421 +  int iLeftSeed = 0;
1.135422 +  int iRightSeed = 1;
1.135423 +  int *aiUsed;
1.135424 +  int i;
1.135425 +
1.135426 +  aiUsed = sqlite3_malloc(sizeof(int)*nCell);
1.135427 +  if( !aiUsed ){
1.135428 +    return SQLITE_NOMEM;
1.135429 +  }
1.135430 +  memset(aiUsed, 0, sizeof(int)*nCell);
1.135431 +
1.135432 +  PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
1.135433 +
1.135434 +  memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
1.135435 +  memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
1.135436 +  nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
1.135437 +  nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
1.135438 +  aiUsed[iLeftSeed] = 1;
1.135439 +  aiUsed[iRightSeed] = 1;
1.135440 +
1.135441 +  for(i=nCell-2; i>0; i--){
1.135442 +    RtreeCell *pNext;
1.135443 +    pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
1.135444 +    RtreeDValue diff =  
1.135445 +      cellGrowth(pRtree, pBboxLeft, pNext) - 
1.135446 +      cellGrowth(pRtree, pBboxRight, pNext)
1.135447 +    ;
1.135448 +    if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
1.135449 +     || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
1.135450 +    ){
1.135451 +      nodeInsertCell(pRtree, pRight, pNext);
1.135452 +      cellUnion(pRtree, pBboxRight, pNext);
1.135453 +    }else{
1.135454 +      nodeInsertCell(pRtree, pLeft, pNext);
1.135455 +      cellUnion(pRtree, pBboxLeft, pNext);
1.135456 +    }
1.135457 +  }
1.135458 +
1.135459 +  sqlite3_free(aiUsed);
1.135460 +  return SQLITE_OK;
1.135461 +}
1.135462 +#endif
1.135463 +
1.135464 +static int updateMapping(
1.135465 +  Rtree *pRtree, 
1.135466 +  i64 iRowid, 
1.135467 +  RtreeNode *pNode, 
1.135468 +  int iHeight
1.135469 +){
1.135470 +  int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
1.135471 +  xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
1.135472 +  if( iHeight>0 ){
1.135473 +    RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
1.135474 +    if( pChild ){
1.135475 +      nodeRelease(pRtree, pChild->pParent);
1.135476 +      nodeReference(pNode);
1.135477 +      pChild->pParent = pNode;
1.135478 +    }
1.135479 +  }
1.135480 +  return xSetMapping(pRtree, iRowid, pNode->iNode);
1.135481 +}
1.135482 +
1.135483 +static int SplitNode(
1.135484 +  Rtree *pRtree,
1.135485 +  RtreeNode *pNode,
1.135486 +  RtreeCell *pCell,
1.135487 +  int iHeight
1.135488 +){
1.135489 +  int i;
1.135490 +  int newCellIsRight = 0;
1.135491 +
1.135492 +  int rc = SQLITE_OK;
1.135493 +  int nCell = NCELL(pNode);
1.135494 +  RtreeCell *aCell;
1.135495 +  int *aiUsed;
1.135496 +
1.135497 +  RtreeNode *pLeft = 0;
1.135498 +  RtreeNode *pRight = 0;
1.135499 +
1.135500 +  RtreeCell leftbbox;
1.135501 +  RtreeCell rightbbox;
1.135502 +
1.135503 +  /* Allocate an array and populate it with a copy of pCell and 
1.135504 +  ** all cells from node pLeft. Then zero the original node.
1.135505 +  */
1.135506 +  aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
1.135507 +  if( !aCell ){
1.135508 +    rc = SQLITE_NOMEM;
1.135509 +    goto splitnode_out;
1.135510 +  }
1.135511 +  aiUsed = (int *)&aCell[nCell+1];
1.135512 +  memset(aiUsed, 0, sizeof(int)*(nCell+1));
1.135513 +  for(i=0; i<nCell; i++){
1.135514 +    nodeGetCell(pRtree, pNode, i, &aCell[i]);
1.135515 +  }
1.135516 +  nodeZero(pRtree, pNode);
1.135517 +  memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
1.135518 +  nCell++;
1.135519 +
1.135520 +  if( pNode->iNode==1 ){
1.135521 +    pRight = nodeNew(pRtree, pNode);
1.135522 +    pLeft = nodeNew(pRtree, pNode);
1.135523 +    pRtree->iDepth++;
1.135524 +    pNode->isDirty = 1;
1.135525 +    writeInt16(pNode->zData, pRtree->iDepth);
1.135526 +  }else{
1.135527 +    pLeft = pNode;
1.135528 +    pRight = nodeNew(pRtree, pLeft->pParent);
1.135529 +    nodeReference(pLeft);
1.135530 +  }
1.135531 +
1.135532 +  if( !pLeft || !pRight ){
1.135533 +    rc = SQLITE_NOMEM;
1.135534 +    goto splitnode_out;
1.135535 +  }
1.135536 +
1.135537 +  memset(pLeft->zData, 0, pRtree->iNodeSize);
1.135538 +  memset(pRight->zData, 0, pRtree->iNodeSize);
1.135539 +
1.135540 +  rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
1.135541 +  if( rc!=SQLITE_OK ){
1.135542 +    goto splitnode_out;
1.135543 +  }
1.135544 +
1.135545 +  /* Ensure both child nodes have node numbers assigned to them by calling
1.135546 +  ** nodeWrite(). Node pRight always needs a node number, as it was created
1.135547 +  ** by nodeNew() above. But node pLeft sometimes already has a node number.
1.135548 +  ** In this case avoid the all to nodeWrite().
1.135549 +  */
1.135550 +  if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
1.135551 +   || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
1.135552 +  ){
1.135553 +    goto splitnode_out;
1.135554 +  }
1.135555 +
1.135556 +  rightbbox.iRowid = pRight->iNode;
1.135557 +  leftbbox.iRowid = pLeft->iNode;
1.135558 +
1.135559 +  if( pNode->iNode==1 ){
1.135560 +    rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
1.135561 +    if( rc!=SQLITE_OK ){
1.135562 +      goto splitnode_out;
1.135563 +    }
1.135564 +  }else{
1.135565 +    RtreeNode *pParent = pLeft->pParent;
1.135566 +    int iCell;
1.135567 +    rc = nodeParentIndex(pRtree, pLeft, &iCell);
1.135568 +    if( rc==SQLITE_OK ){
1.135569 +      nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
1.135570 +      rc = AdjustTree(pRtree, pParent, &leftbbox);
1.135571 +    }
1.135572 +    if( rc!=SQLITE_OK ){
1.135573 +      goto splitnode_out;
1.135574 +    }
1.135575 +  }
1.135576 +  if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
1.135577 +    goto splitnode_out;
1.135578 +  }
1.135579 +
1.135580 +  for(i=0; i<NCELL(pRight); i++){
1.135581 +    i64 iRowid = nodeGetRowid(pRtree, pRight, i);
1.135582 +    rc = updateMapping(pRtree, iRowid, pRight, iHeight);
1.135583 +    if( iRowid==pCell->iRowid ){
1.135584 +      newCellIsRight = 1;
1.135585 +    }
1.135586 +    if( rc!=SQLITE_OK ){
1.135587 +      goto splitnode_out;
1.135588 +    }
1.135589 +  }
1.135590 +  if( pNode->iNode==1 ){
1.135591 +    for(i=0; i<NCELL(pLeft); i++){
1.135592 +      i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
1.135593 +      rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
1.135594 +      if( rc!=SQLITE_OK ){
1.135595 +        goto splitnode_out;
1.135596 +      }
1.135597 +    }
1.135598 +  }else if( newCellIsRight==0 ){
1.135599 +    rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
1.135600 +  }
1.135601 +
1.135602 +  if( rc==SQLITE_OK ){
1.135603 +    rc = nodeRelease(pRtree, pRight);
1.135604 +    pRight = 0;
1.135605 +  }
1.135606 +  if( rc==SQLITE_OK ){
1.135607 +    rc = nodeRelease(pRtree, pLeft);
1.135608 +    pLeft = 0;
1.135609 +  }
1.135610 +
1.135611 +splitnode_out:
1.135612 +  nodeRelease(pRtree, pRight);
1.135613 +  nodeRelease(pRtree, pLeft);
1.135614 +  sqlite3_free(aCell);
1.135615 +  return rc;
1.135616 +}
1.135617 +
1.135618 +/*
1.135619 +** If node pLeaf is not the root of the r-tree and its pParent pointer is 
1.135620 +** still NULL, load all ancestor nodes of pLeaf into memory and populate
1.135621 +** the pLeaf->pParent chain all the way up to the root node.
1.135622 +**
1.135623 +** This operation is required when a row is deleted (or updated - an update
1.135624 +** is implemented as a delete followed by an insert). SQLite provides the
1.135625 +** rowid of the row to delete, which can be used to find the leaf on which
1.135626 +** the entry resides (argument pLeaf). Once the leaf is located, this 
1.135627 +** function is called to determine its ancestry.
1.135628 +*/
1.135629 +static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
1.135630 +  int rc = SQLITE_OK;
1.135631 +  RtreeNode *pChild = pLeaf;
1.135632 +  while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
1.135633 +    int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
1.135634 +    sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
1.135635 +    rc = sqlite3_step(pRtree->pReadParent);
1.135636 +    if( rc==SQLITE_ROW ){
1.135637 +      RtreeNode *pTest;           /* Used to test for reference loops */
1.135638 +      i64 iNode;                  /* Node number of parent node */
1.135639 +
1.135640 +      /* Before setting pChild->pParent, test that we are not creating a
1.135641 +      ** loop of references (as we would if, say, pChild==pParent). We don't
1.135642 +      ** want to do this as it leads to a memory leak when trying to delete
1.135643 +      ** the referenced counted node structures.
1.135644 +      */
1.135645 +      iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
1.135646 +      for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
1.135647 +      if( !pTest ){
1.135648 +        rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
1.135649 +      }
1.135650 +    }
1.135651 +    rc = sqlite3_reset(pRtree->pReadParent);
1.135652 +    if( rc==SQLITE_OK ) rc = rc2;
1.135653 +    if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
1.135654 +    pChild = pChild->pParent;
1.135655 +  }
1.135656 +  return rc;
1.135657 +}
1.135658 +
1.135659 +static int deleteCell(Rtree *, RtreeNode *, int, int);
1.135660 +
1.135661 +static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
1.135662 +  int rc;
1.135663 +  int rc2;
1.135664 +  RtreeNode *pParent = 0;
1.135665 +  int iCell;
1.135666 +
1.135667 +  assert( pNode->nRef==1 );
1.135668 +
1.135669 +  /* Remove the entry in the parent cell. */
1.135670 +  rc = nodeParentIndex(pRtree, pNode, &iCell);
1.135671 +  if( rc==SQLITE_OK ){
1.135672 +    pParent = pNode->pParent;
1.135673 +    pNode->pParent = 0;
1.135674 +    rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
1.135675 +  }
1.135676 +  rc2 = nodeRelease(pRtree, pParent);
1.135677 +  if( rc==SQLITE_OK ){
1.135678 +    rc = rc2;
1.135679 +  }
1.135680 +  if( rc!=SQLITE_OK ){
1.135681 +    return rc;
1.135682 +  }
1.135683 +
1.135684 +  /* Remove the xxx_node entry. */
1.135685 +  sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
1.135686 +  sqlite3_step(pRtree->pDeleteNode);
1.135687 +  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
1.135688 +    return rc;
1.135689 +  }
1.135690 +
1.135691 +  /* Remove the xxx_parent entry. */
1.135692 +  sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
1.135693 +  sqlite3_step(pRtree->pDeleteParent);
1.135694 +  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
1.135695 +    return rc;
1.135696 +  }
1.135697 +  
1.135698 +  /* Remove the node from the in-memory hash table and link it into
1.135699 +  ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
1.135700 +  */
1.135701 +  nodeHashDelete(pRtree, pNode);
1.135702 +  pNode->iNode = iHeight;
1.135703 +  pNode->pNext = pRtree->pDeleted;
1.135704 +  pNode->nRef++;
1.135705 +  pRtree->pDeleted = pNode;
1.135706 +
1.135707 +  return SQLITE_OK;
1.135708 +}
1.135709 +
1.135710 +static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
1.135711 +  RtreeNode *pParent = pNode->pParent;
1.135712 +  int rc = SQLITE_OK; 
1.135713 +  if( pParent ){
1.135714 +    int ii; 
1.135715 +    int nCell = NCELL(pNode);
1.135716 +    RtreeCell box;                            /* Bounding box for pNode */
1.135717 +    nodeGetCell(pRtree, pNode, 0, &box);
1.135718 +    for(ii=1; ii<nCell; ii++){
1.135719 +      RtreeCell cell;
1.135720 +      nodeGetCell(pRtree, pNode, ii, &cell);
1.135721 +      cellUnion(pRtree, &box, &cell);
1.135722 +    }
1.135723 +    box.iRowid = pNode->iNode;
1.135724 +    rc = nodeParentIndex(pRtree, pNode, &ii);
1.135725 +    if( rc==SQLITE_OK ){
1.135726 +      nodeOverwriteCell(pRtree, pParent, &box, ii);
1.135727 +      rc = fixBoundingBox(pRtree, pParent);
1.135728 +    }
1.135729 +  }
1.135730 +  return rc;
1.135731 +}
1.135732 +
1.135733 +/*
1.135734 +** Delete the cell at index iCell of node pNode. After removing the
1.135735 +** cell, adjust the r-tree data structure if required.
1.135736 +*/
1.135737 +static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
1.135738 +  RtreeNode *pParent;
1.135739 +  int rc;
1.135740 +
1.135741 +  if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
1.135742 +    return rc;
1.135743 +  }
1.135744 +
1.135745 +  /* Remove the cell from the node. This call just moves bytes around
1.135746 +  ** the in-memory node image, so it cannot fail.
1.135747 +  */
1.135748 +  nodeDeleteCell(pRtree, pNode, iCell);
1.135749 +
1.135750 +  /* If the node is not the tree root and now has less than the minimum
1.135751 +  ** number of cells, remove it from the tree. Otherwise, update the
1.135752 +  ** cell in the parent node so that it tightly contains the updated
1.135753 +  ** node.
1.135754 +  */
1.135755 +  pParent = pNode->pParent;
1.135756 +  assert( pParent || pNode->iNode==1 );
1.135757 +  if( pParent ){
1.135758 +    if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
1.135759 +      rc = removeNode(pRtree, pNode, iHeight);
1.135760 +    }else{
1.135761 +      rc = fixBoundingBox(pRtree, pNode);
1.135762 +    }
1.135763 +  }
1.135764 +
1.135765 +  return rc;
1.135766 +}
1.135767 +
1.135768 +static int Reinsert(
1.135769 +  Rtree *pRtree, 
1.135770 +  RtreeNode *pNode, 
1.135771 +  RtreeCell *pCell, 
1.135772 +  int iHeight
1.135773 +){
1.135774 +  int *aOrder;
1.135775 +  int *aSpare;
1.135776 +  RtreeCell *aCell;
1.135777 +  RtreeDValue *aDistance;
1.135778 +  int nCell;
1.135779 +  RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
1.135780 +  int iDim;
1.135781 +  int ii;
1.135782 +  int rc = SQLITE_OK;
1.135783 +  int n;
1.135784 +
1.135785 +  memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
1.135786 +
1.135787 +  nCell = NCELL(pNode)+1;
1.135788 +  n = (nCell+1)&(~1);
1.135789 +
1.135790 +  /* Allocate the buffers used by this operation. The allocation is
1.135791 +  ** relinquished before this function returns.
1.135792 +  */
1.135793 +  aCell = (RtreeCell *)sqlite3_malloc(n * (
1.135794 +    sizeof(RtreeCell)     +         /* aCell array */
1.135795 +    sizeof(int)           +         /* aOrder array */
1.135796 +    sizeof(int)           +         /* aSpare array */
1.135797 +    sizeof(RtreeDValue)             /* aDistance array */
1.135798 +  ));
1.135799 +  if( !aCell ){
1.135800 +    return SQLITE_NOMEM;
1.135801 +  }
1.135802 +  aOrder    = (int *)&aCell[n];
1.135803 +  aSpare    = (int *)&aOrder[n];
1.135804 +  aDistance = (RtreeDValue *)&aSpare[n];
1.135805 +
1.135806 +  for(ii=0; ii<nCell; ii++){
1.135807 +    if( ii==(nCell-1) ){
1.135808 +      memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
1.135809 +    }else{
1.135810 +      nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
1.135811 +    }
1.135812 +    aOrder[ii] = ii;
1.135813 +    for(iDim=0; iDim<pRtree->nDim; iDim++){
1.135814 +      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
1.135815 +      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
1.135816 +    }
1.135817 +  }
1.135818 +  for(iDim=0; iDim<pRtree->nDim; iDim++){
1.135819 +    aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
1.135820 +  }
1.135821 +
1.135822 +  for(ii=0; ii<nCell; ii++){
1.135823 +    aDistance[ii] = 0.0;
1.135824 +    for(iDim=0; iDim<pRtree->nDim; iDim++){
1.135825 +      RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
1.135826 +                               DCOORD(aCell[ii].aCoord[iDim*2]));
1.135827 +      aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
1.135828 +    }
1.135829 +  }
1.135830 +
1.135831 +  SortByDistance(aOrder, nCell, aDistance, aSpare);
1.135832 +  nodeZero(pRtree, pNode);
1.135833 +
1.135834 +  for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
1.135835 +    RtreeCell *p = &aCell[aOrder[ii]];
1.135836 +    nodeInsertCell(pRtree, pNode, p);
1.135837 +    if( p->iRowid==pCell->iRowid ){
1.135838 +      if( iHeight==0 ){
1.135839 +        rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
1.135840 +      }else{
1.135841 +        rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
1.135842 +      }
1.135843 +    }
1.135844 +  }
1.135845 +  if( rc==SQLITE_OK ){
1.135846 +    rc = fixBoundingBox(pRtree, pNode);
1.135847 +  }
1.135848 +  for(; rc==SQLITE_OK && ii<nCell; ii++){
1.135849 +    /* Find a node to store this cell in. pNode->iNode currently contains
1.135850 +    ** the height of the sub-tree headed by the cell.
1.135851 +    */
1.135852 +    RtreeNode *pInsert;
1.135853 +    RtreeCell *p = &aCell[aOrder[ii]];
1.135854 +    rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
1.135855 +    if( rc==SQLITE_OK ){
1.135856 +      int rc2;
1.135857 +      rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
1.135858 +      rc2 = nodeRelease(pRtree, pInsert);
1.135859 +      if( rc==SQLITE_OK ){
1.135860 +        rc = rc2;
1.135861 +      }
1.135862 +    }
1.135863 +  }
1.135864 +
1.135865 +  sqlite3_free(aCell);
1.135866 +  return rc;
1.135867 +}
1.135868 +
1.135869 +/*
1.135870 +** Insert cell pCell into node pNode. Node pNode is the head of a 
1.135871 +** subtree iHeight high (leaf nodes have iHeight==0).
1.135872 +*/
1.135873 +static int rtreeInsertCell(
1.135874 +  Rtree *pRtree,
1.135875 +  RtreeNode *pNode,
1.135876 +  RtreeCell *pCell,
1.135877 +  int iHeight
1.135878 +){
1.135879 +  int rc = SQLITE_OK;
1.135880 +  if( iHeight>0 ){
1.135881 +    RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
1.135882 +    if( pChild ){
1.135883 +      nodeRelease(pRtree, pChild->pParent);
1.135884 +      nodeReference(pNode);
1.135885 +      pChild->pParent = pNode;
1.135886 +    }
1.135887 +  }
1.135888 +  if( nodeInsertCell(pRtree, pNode, pCell) ){
1.135889 +#if VARIANT_RSTARTREE_REINSERT
1.135890 +    if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
1.135891 +      rc = SplitNode(pRtree, pNode, pCell, iHeight);
1.135892 +    }else{
1.135893 +      pRtree->iReinsertHeight = iHeight;
1.135894 +      rc = Reinsert(pRtree, pNode, pCell, iHeight);
1.135895 +    }
1.135896 +#else
1.135897 +    rc = SplitNode(pRtree, pNode, pCell, iHeight);
1.135898 +#endif
1.135899 +  }else{
1.135900 +    rc = AdjustTree(pRtree, pNode, pCell);
1.135901 +    if( rc==SQLITE_OK ){
1.135902 +      if( iHeight==0 ){
1.135903 +        rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
1.135904 +      }else{
1.135905 +        rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
1.135906 +      }
1.135907 +    }
1.135908 +  }
1.135909 +  return rc;
1.135910 +}
1.135911 +
1.135912 +static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
1.135913 +  int ii;
1.135914 +  int rc = SQLITE_OK;
1.135915 +  int nCell = NCELL(pNode);
1.135916 +
1.135917 +  for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
1.135918 +    RtreeNode *pInsert;
1.135919 +    RtreeCell cell;
1.135920 +    nodeGetCell(pRtree, pNode, ii, &cell);
1.135921 +
1.135922 +    /* Find a node to store this cell in. pNode->iNode currently contains
1.135923 +    ** the height of the sub-tree headed by the cell.
1.135924 +    */
1.135925 +    rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
1.135926 +    if( rc==SQLITE_OK ){
1.135927 +      int rc2;
1.135928 +      rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
1.135929 +      rc2 = nodeRelease(pRtree, pInsert);
1.135930 +      if( rc==SQLITE_OK ){
1.135931 +        rc = rc2;
1.135932 +      }
1.135933 +    }
1.135934 +  }
1.135935 +  return rc;
1.135936 +}
1.135937 +
1.135938 +/*
1.135939 +** Select a currently unused rowid for a new r-tree record.
1.135940 +*/
1.135941 +static int newRowid(Rtree *pRtree, i64 *piRowid){
1.135942 +  int rc;
1.135943 +  sqlite3_bind_null(pRtree->pWriteRowid, 1);
1.135944 +  sqlite3_bind_null(pRtree->pWriteRowid, 2);
1.135945 +  sqlite3_step(pRtree->pWriteRowid);
1.135946 +  rc = sqlite3_reset(pRtree->pWriteRowid);
1.135947 +  *piRowid = sqlite3_last_insert_rowid(pRtree->db);
1.135948 +  return rc;
1.135949 +}
1.135950 +
1.135951 +/*
1.135952 +** Remove the entry with rowid=iDelete from the r-tree structure.
1.135953 +*/
1.135954 +static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
1.135955 +  int rc;                         /* Return code */
1.135956 +  RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
1.135957 +  int iCell;                      /* Index of iDelete cell in pLeaf */
1.135958 +  RtreeNode *pRoot;               /* Root node of rtree structure */
1.135959 +
1.135960 +
1.135961 +  /* Obtain a reference to the root node to initialise Rtree.iDepth */
1.135962 +  rc = nodeAcquire(pRtree, 1, 0, &pRoot);
1.135963 +
1.135964 +  /* Obtain a reference to the leaf node that contains the entry 
1.135965 +  ** about to be deleted. 
1.135966 +  */
1.135967 +  if( rc==SQLITE_OK ){
1.135968 +    rc = findLeafNode(pRtree, iDelete, &pLeaf);
1.135969 +  }
1.135970 +
1.135971 +  /* Delete the cell in question from the leaf node. */
1.135972 +  if( rc==SQLITE_OK ){
1.135973 +    int rc2;
1.135974 +    rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
1.135975 +    if( rc==SQLITE_OK ){
1.135976 +      rc = deleteCell(pRtree, pLeaf, iCell, 0);
1.135977 +    }
1.135978 +    rc2 = nodeRelease(pRtree, pLeaf);
1.135979 +    if( rc==SQLITE_OK ){
1.135980 +      rc = rc2;
1.135981 +    }
1.135982 +  }
1.135983 +
1.135984 +  /* Delete the corresponding entry in the <rtree>_rowid table. */
1.135985 +  if( rc==SQLITE_OK ){
1.135986 +    sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
1.135987 +    sqlite3_step(pRtree->pDeleteRowid);
1.135988 +    rc = sqlite3_reset(pRtree->pDeleteRowid);
1.135989 +  }
1.135990 +
1.135991 +  /* Check if the root node now has exactly one child. If so, remove
1.135992 +  ** it, schedule the contents of the child for reinsertion and 
1.135993 +  ** reduce the tree height by one.
1.135994 +  **
1.135995 +  ** This is equivalent to copying the contents of the child into
1.135996 +  ** the root node (the operation that Gutman's paper says to perform 
1.135997 +  ** in this scenario).
1.135998 +  */
1.135999 +  if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
1.136000 +    int rc2;
1.136001 +    RtreeNode *pChild;
1.136002 +    i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
1.136003 +    rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
1.136004 +    if( rc==SQLITE_OK ){
1.136005 +      rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
1.136006 +    }
1.136007 +    rc2 = nodeRelease(pRtree, pChild);
1.136008 +    if( rc==SQLITE_OK ) rc = rc2;
1.136009 +    if( rc==SQLITE_OK ){
1.136010 +      pRtree->iDepth--;
1.136011 +      writeInt16(pRoot->zData, pRtree->iDepth);
1.136012 +      pRoot->isDirty = 1;
1.136013 +    }
1.136014 +  }
1.136015 +
1.136016 +  /* Re-insert the contents of any underfull nodes removed from the tree. */
1.136017 +  for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
1.136018 +    if( rc==SQLITE_OK ){
1.136019 +      rc = reinsertNodeContent(pRtree, pLeaf);
1.136020 +    }
1.136021 +    pRtree->pDeleted = pLeaf->pNext;
1.136022 +    sqlite3_free(pLeaf);
1.136023 +  }
1.136024 +
1.136025 +  /* Release the reference to the root node. */
1.136026 +  if( rc==SQLITE_OK ){
1.136027 +    rc = nodeRelease(pRtree, pRoot);
1.136028 +  }else{
1.136029 +    nodeRelease(pRtree, pRoot);
1.136030 +  }
1.136031 +
1.136032 +  return rc;
1.136033 +}
1.136034 +
1.136035 +/*
1.136036 +** Rounding constants for float->double conversion.
1.136037 +*/
1.136038 +#define RNDTOWARDS  (1.0 - 1.0/8388608.0)  /* Round towards zero */
1.136039 +#define RNDAWAY     (1.0 + 1.0/8388608.0)  /* Round away from zero */
1.136040 +
1.136041 +#if !defined(SQLITE_RTREE_INT_ONLY)
1.136042 +/*
1.136043 +** Convert an sqlite3_value into an RtreeValue (presumably a float)
1.136044 +** while taking care to round toward negative or positive, respectively.
1.136045 +*/
1.136046 +static RtreeValue rtreeValueDown(sqlite3_value *v){
1.136047 +  double d = sqlite3_value_double(v);
1.136048 +  float f = (float)d;
1.136049 +  if( f>d ){
1.136050 +    f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
1.136051 +  }
1.136052 +  return f;
1.136053 +}
1.136054 +static RtreeValue rtreeValueUp(sqlite3_value *v){
1.136055 +  double d = sqlite3_value_double(v);
1.136056 +  float f = (float)d;
1.136057 +  if( f<d ){
1.136058 +    f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
1.136059 +  }
1.136060 +  return f;
1.136061 +}
1.136062 +#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
1.136063 +
1.136064 +
1.136065 +/*
1.136066 +** The xUpdate method for rtree module virtual tables.
1.136067 +*/
1.136068 +static int rtreeUpdate(
1.136069 +  sqlite3_vtab *pVtab, 
1.136070 +  int nData, 
1.136071 +  sqlite3_value **azData, 
1.136072 +  sqlite_int64 *pRowid
1.136073 +){
1.136074 +  Rtree *pRtree = (Rtree *)pVtab;
1.136075 +  int rc = SQLITE_OK;
1.136076 +  RtreeCell cell;                 /* New cell to insert if nData>1 */
1.136077 +  int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
1.136078 +
1.136079 +  rtreeReference(pRtree);
1.136080 +  assert(nData>=1);
1.136081 +
1.136082 +  /* Constraint handling. A write operation on an r-tree table may return
1.136083 +  ** SQLITE_CONSTRAINT for two reasons:
1.136084 +  **
1.136085 +  **   1. A duplicate rowid value, or
1.136086 +  **   2. The supplied data violates the "x2>=x1" constraint.
1.136087 +  **
1.136088 +  ** In the first case, if the conflict-handling mode is REPLACE, then
1.136089 +  ** the conflicting row can be removed before proceeding. In the second
1.136090 +  ** case, SQLITE_CONSTRAINT must be returned regardless of the
1.136091 +  ** conflict-handling mode specified by the user.
1.136092 +  */
1.136093 +  if( nData>1 ){
1.136094 +    int ii;
1.136095 +
1.136096 +    /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
1.136097 +    assert( nData==(pRtree->nDim*2 + 3) );
1.136098 +#ifndef SQLITE_RTREE_INT_ONLY
1.136099 +    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
1.136100 +      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
1.136101 +        cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
1.136102 +        cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
1.136103 +        if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
1.136104 +          rc = SQLITE_CONSTRAINT;
1.136105 +          goto constraint;
1.136106 +        }
1.136107 +      }
1.136108 +    }else
1.136109 +#endif
1.136110 +    {
1.136111 +      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
1.136112 +        cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
1.136113 +        cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
1.136114 +        if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
1.136115 +          rc = SQLITE_CONSTRAINT;
1.136116 +          goto constraint;
1.136117 +        }
1.136118 +      }
1.136119 +    }
1.136120 +
1.136121 +    /* If a rowid value was supplied, check if it is already present in 
1.136122 +    ** the table. If so, the constraint has failed. */
1.136123 +    if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
1.136124 +      cell.iRowid = sqlite3_value_int64(azData[2]);
1.136125 +      if( sqlite3_value_type(azData[0])==SQLITE_NULL
1.136126 +       || sqlite3_value_int64(azData[0])!=cell.iRowid
1.136127 +      ){
1.136128 +        int steprc;
1.136129 +        sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
1.136130 +        steprc = sqlite3_step(pRtree->pReadRowid);
1.136131 +        rc = sqlite3_reset(pRtree->pReadRowid);
1.136132 +        if( SQLITE_ROW==steprc ){
1.136133 +          if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
1.136134 +            rc = rtreeDeleteRowid(pRtree, cell.iRowid);
1.136135 +          }else{
1.136136 +            rc = SQLITE_CONSTRAINT;
1.136137 +            goto constraint;
1.136138 +          }
1.136139 +        }
1.136140 +      }
1.136141 +      bHaveRowid = 1;
1.136142 +    }
1.136143 +  }
1.136144 +
1.136145 +  /* If azData[0] is not an SQL NULL value, it is the rowid of a
1.136146 +  ** record to delete from the r-tree table. The following block does
1.136147 +  ** just that.
1.136148 +  */
1.136149 +  if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
1.136150 +    rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
1.136151 +  }
1.136152 +
1.136153 +  /* If the azData[] array contains more than one element, elements
1.136154 +  ** (azData[2]..azData[argc-1]) contain a new record to insert into
1.136155 +  ** the r-tree structure.
1.136156 +  */
1.136157 +  if( rc==SQLITE_OK && nData>1 ){
1.136158 +    /* Insert the new record into the r-tree */
1.136159 +    RtreeNode *pLeaf = 0;
1.136160 +
1.136161 +    /* Figure out the rowid of the new row. */
1.136162 +    if( bHaveRowid==0 ){
1.136163 +      rc = newRowid(pRtree, &cell.iRowid);
1.136164 +    }
1.136165 +    *pRowid = cell.iRowid;
1.136166 +
1.136167 +    if( rc==SQLITE_OK ){
1.136168 +      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
1.136169 +    }
1.136170 +    if( rc==SQLITE_OK ){
1.136171 +      int rc2;
1.136172 +      pRtree->iReinsertHeight = -1;
1.136173 +      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
1.136174 +      rc2 = nodeRelease(pRtree, pLeaf);
1.136175 +      if( rc==SQLITE_OK ){
1.136176 +        rc = rc2;
1.136177 +      }
1.136178 +    }
1.136179 +  }
1.136180 +
1.136181 +constraint:
1.136182 +  rtreeRelease(pRtree);
1.136183 +  return rc;
1.136184 +}
1.136185 +
1.136186 +/*
1.136187 +** The xRename method for rtree module virtual tables.
1.136188 +*/
1.136189 +static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
1.136190 +  Rtree *pRtree = (Rtree *)pVtab;
1.136191 +  int rc = SQLITE_NOMEM;
1.136192 +  char *zSql = sqlite3_mprintf(
1.136193 +    "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
1.136194 +    "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
1.136195 +    "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
1.136196 +    , pRtree->zDb, pRtree->zName, zNewName 
1.136197 +    , pRtree->zDb, pRtree->zName, zNewName 
1.136198 +    , pRtree->zDb, pRtree->zName, zNewName
1.136199 +  );
1.136200 +  if( zSql ){
1.136201 +    rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
1.136202 +    sqlite3_free(zSql);
1.136203 +  }
1.136204 +  return rc;
1.136205 +}
1.136206 +
1.136207 +static sqlite3_module rtreeModule = {
1.136208 +  0,                          /* iVersion */
1.136209 +  rtreeCreate,                /* xCreate - create a table */
1.136210 +  rtreeConnect,               /* xConnect - connect to an existing table */
1.136211 +  rtreeBestIndex,             /* xBestIndex - Determine search strategy */
1.136212 +  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
1.136213 +  rtreeDestroy,               /* xDestroy - Drop a table */
1.136214 +  rtreeOpen,                  /* xOpen - open a cursor */
1.136215 +  rtreeClose,                 /* xClose - close a cursor */
1.136216 +  rtreeFilter,                /* xFilter - configure scan constraints */
1.136217 +  rtreeNext,                  /* xNext - advance a cursor */
1.136218 +  rtreeEof,                   /* xEof */
1.136219 +  rtreeColumn,                /* xColumn - read data */
1.136220 +  rtreeRowid,                 /* xRowid - read data */
1.136221 +  rtreeUpdate,                /* xUpdate - write data */
1.136222 +  0,                          /* xBegin - begin transaction */
1.136223 +  0,                          /* xSync - sync transaction */
1.136224 +  0,                          /* xCommit - commit transaction */
1.136225 +  0,                          /* xRollback - rollback transaction */
1.136226 +  0,                          /* xFindFunction - function overloading */
1.136227 +  rtreeRename,                /* xRename - rename the table */
1.136228 +  0,                          /* xSavepoint */
1.136229 +  0,                          /* xRelease */
1.136230 +  0                           /* xRollbackTo */
1.136231 +};
1.136232 +
1.136233 +static int rtreeSqlInit(
1.136234 +  Rtree *pRtree, 
1.136235 +  sqlite3 *db, 
1.136236 +  const char *zDb, 
1.136237 +  const char *zPrefix, 
1.136238 +  int isCreate
1.136239 +){
1.136240 +  int rc = SQLITE_OK;
1.136241 +
1.136242 +  #define N_STATEMENT 9
1.136243 +  static const char *azSql[N_STATEMENT] = {
1.136244 +    /* Read and write the xxx_node table */
1.136245 +    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
1.136246 +    "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
1.136247 +    "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
1.136248 +
1.136249 +    /* Read and write the xxx_rowid table */
1.136250 +    "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
1.136251 +    "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
1.136252 +    "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
1.136253 +
1.136254 +    /* Read and write the xxx_parent table */
1.136255 +    "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
1.136256 +    "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
1.136257 +    "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
1.136258 +  };
1.136259 +  sqlite3_stmt **appStmt[N_STATEMENT];
1.136260 +  int i;
1.136261 +
1.136262 +  pRtree->db = db;
1.136263 +
1.136264 +  if( isCreate ){
1.136265 +    char *zCreate = sqlite3_mprintf(
1.136266 +"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
1.136267 +"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
1.136268 +"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
1.136269 +"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
1.136270 +      zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
1.136271 +    );
1.136272 +    if( !zCreate ){
1.136273 +      return SQLITE_NOMEM;
1.136274 +    }
1.136275 +    rc = sqlite3_exec(db, zCreate, 0, 0, 0);
1.136276 +    sqlite3_free(zCreate);
1.136277 +    if( rc!=SQLITE_OK ){
1.136278 +      return rc;
1.136279 +    }
1.136280 +  }
1.136281 +
1.136282 +  appStmt[0] = &pRtree->pReadNode;
1.136283 +  appStmt[1] = &pRtree->pWriteNode;
1.136284 +  appStmt[2] = &pRtree->pDeleteNode;
1.136285 +  appStmt[3] = &pRtree->pReadRowid;
1.136286 +  appStmt[4] = &pRtree->pWriteRowid;
1.136287 +  appStmt[5] = &pRtree->pDeleteRowid;
1.136288 +  appStmt[6] = &pRtree->pReadParent;
1.136289 +  appStmt[7] = &pRtree->pWriteParent;
1.136290 +  appStmt[8] = &pRtree->pDeleteParent;
1.136291 +
1.136292 +  for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
1.136293 +    char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
1.136294 +    if( zSql ){
1.136295 +      rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
1.136296 +    }else{
1.136297 +      rc = SQLITE_NOMEM;
1.136298 +    }
1.136299 +    sqlite3_free(zSql);
1.136300 +  }
1.136301 +
1.136302 +  return rc;
1.136303 +}
1.136304 +
1.136305 +/*
1.136306 +** The second argument to this function contains the text of an SQL statement
1.136307 +** that returns a single integer value. The statement is compiled and executed
1.136308 +** using database connection db. If successful, the integer value returned
1.136309 +** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
1.136310 +** code is returned and the value of *piVal after returning is not defined.
1.136311 +*/
1.136312 +static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
1.136313 +  int rc = SQLITE_NOMEM;
1.136314 +  if( zSql ){
1.136315 +    sqlite3_stmt *pStmt = 0;
1.136316 +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1.136317 +    if( rc==SQLITE_OK ){
1.136318 +      if( SQLITE_ROW==sqlite3_step(pStmt) ){
1.136319 +        *piVal = sqlite3_column_int(pStmt, 0);
1.136320 +      }
1.136321 +      rc = sqlite3_finalize(pStmt);
1.136322 +    }
1.136323 +  }
1.136324 +  return rc;
1.136325 +}
1.136326 +
1.136327 +/*
1.136328 +** This function is called from within the xConnect() or xCreate() method to
1.136329 +** determine the node-size used by the rtree table being created or connected
1.136330 +** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
1.136331 +** Otherwise, an SQLite error code is returned.
1.136332 +**
1.136333 +** If this function is being called as part of an xConnect(), then the rtree
1.136334 +** table already exists. In this case the node-size is determined by inspecting
1.136335 +** the root node of the tree.
1.136336 +**
1.136337 +** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. 
1.136338 +** This ensures that each node is stored on a single database page. If the 
1.136339 +** database page-size is so large that more than RTREE_MAXCELLS entries 
1.136340 +** would fit in a single node, use a smaller node-size.
1.136341 +*/
1.136342 +static int getNodeSize(
1.136343 +  sqlite3 *db,                    /* Database handle */
1.136344 +  Rtree *pRtree,                  /* Rtree handle */
1.136345 +  int isCreate                    /* True for xCreate, false for xConnect */
1.136346 +){
1.136347 +  int rc;
1.136348 +  char *zSql;
1.136349 +  if( isCreate ){
1.136350 +    int iPageSize = 0;
1.136351 +    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
1.136352 +    rc = getIntFromStmt(db, zSql, &iPageSize);
1.136353 +    if( rc==SQLITE_OK ){
1.136354 +      pRtree->iNodeSize = iPageSize-64;
1.136355 +      if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
1.136356 +        pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
1.136357 +      }
1.136358 +    }
1.136359 +  }else{
1.136360 +    zSql = sqlite3_mprintf(
1.136361 +        "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
1.136362 +        pRtree->zDb, pRtree->zName
1.136363 +    );
1.136364 +    rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
1.136365 +  }
1.136366 +
1.136367 +  sqlite3_free(zSql);
1.136368 +  return rc;
1.136369 +}
1.136370 +
1.136371 +/* 
1.136372 +** This function is the implementation of both the xConnect and xCreate
1.136373 +** methods of the r-tree virtual table.
1.136374 +**
1.136375 +**   argv[0]   -> module name
1.136376 +**   argv[1]   -> database name
1.136377 +**   argv[2]   -> table name
1.136378 +**   argv[...] -> column names...
1.136379 +*/
1.136380 +static int rtreeInit(
1.136381 +  sqlite3 *db,                        /* Database connection */
1.136382 +  void *pAux,                         /* One of the RTREE_COORD_* constants */
1.136383 +  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
1.136384 +  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
1.136385 +  char **pzErr,                       /* OUT: Error message, if any */
1.136386 +  int isCreate                        /* True for xCreate, false for xConnect */
1.136387 +){
1.136388 +  int rc = SQLITE_OK;
1.136389 +  Rtree *pRtree;
1.136390 +  int nDb;              /* Length of string argv[1] */
1.136391 +  int nName;            /* Length of string argv[2] */
1.136392 +  int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
1.136393 +
1.136394 +  const char *aErrMsg[] = {
1.136395 +    0,                                                    /* 0 */
1.136396 +    "Wrong number of columns for an rtree table",         /* 1 */
1.136397 +    "Too few columns for an rtree table",                 /* 2 */
1.136398 +    "Too many columns for an rtree table"                 /* 3 */
1.136399 +  };
1.136400 +
1.136401 +  int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
1.136402 +  if( aErrMsg[iErr] ){
1.136403 +    *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
1.136404 +    return SQLITE_ERROR;
1.136405 +  }
1.136406 +
1.136407 +  sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
1.136408 +
1.136409 +  /* Allocate the sqlite3_vtab structure */
1.136410 +  nDb = (int)strlen(argv[1]);
1.136411 +  nName = (int)strlen(argv[2]);
1.136412 +  pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
1.136413 +  if( !pRtree ){
1.136414 +    return SQLITE_NOMEM;
1.136415 +  }
1.136416 +  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
1.136417 +  pRtree->nBusy = 1;
1.136418 +  pRtree->base.pModule = &rtreeModule;
1.136419 +  pRtree->zDb = (char *)&pRtree[1];
1.136420 +  pRtree->zName = &pRtree->zDb[nDb+1];
1.136421 +  pRtree->nDim = (argc-4)/2;
1.136422 +  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
1.136423 +  pRtree->eCoordType = eCoordType;
1.136424 +  memcpy(pRtree->zDb, argv[1], nDb);
1.136425 +  memcpy(pRtree->zName, argv[2], nName);
1.136426 +
1.136427 +  /* Figure out the node size to use. */
1.136428 +  rc = getNodeSize(db, pRtree, isCreate);
1.136429 +
1.136430 +  /* Create/Connect to the underlying relational database schema. If
1.136431 +  ** that is successful, call sqlite3_declare_vtab() to configure
1.136432 +  ** the r-tree table schema.
1.136433 +  */
1.136434 +  if( rc==SQLITE_OK ){
1.136435 +    if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
1.136436 +      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
1.136437 +    }else{
1.136438 +      char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
1.136439 +      char *zTmp;
1.136440 +      int ii;
1.136441 +      for(ii=4; zSql && ii<argc; ii++){
1.136442 +        zTmp = zSql;
1.136443 +        zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
1.136444 +        sqlite3_free(zTmp);
1.136445 +      }
1.136446 +      if( zSql ){
1.136447 +        zTmp = zSql;
1.136448 +        zSql = sqlite3_mprintf("%s);", zTmp);
1.136449 +        sqlite3_free(zTmp);
1.136450 +      }
1.136451 +      if( !zSql ){
1.136452 +        rc = SQLITE_NOMEM;
1.136453 +      }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
1.136454 +        *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
1.136455 +      }
1.136456 +      sqlite3_free(zSql);
1.136457 +    }
1.136458 +  }
1.136459 +
1.136460 +  if( rc==SQLITE_OK ){
1.136461 +    *ppVtab = (sqlite3_vtab *)pRtree;
1.136462 +  }else{
1.136463 +    rtreeRelease(pRtree);
1.136464 +  }
1.136465 +  return rc;
1.136466 +}
1.136467 +
1.136468 +
1.136469 +/*
1.136470 +** Implementation of a scalar function that decodes r-tree nodes to
1.136471 +** human readable strings. This can be used for debugging and analysis.
1.136472 +**
1.136473 +** The scalar function takes two arguments, a blob of data containing
1.136474 +** an r-tree node, and the number of dimensions the r-tree indexes.
1.136475 +** For a two-dimensional r-tree structure called "rt", to deserialize
1.136476 +** all nodes, a statement like:
1.136477 +**
1.136478 +**   SELECT rtreenode(2, data) FROM rt_node;
1.136479 +**
1.136480 +** The human readable string takes the form of a Tcl list with one
1.136481 +** entry for each cell in the r-tree node. Each entry is itself a
1.136482 +** list, containing the 8-byte rowid/pageno followed by the 
1.136483 +** <num-dimension>*2 coordinates.
1.136484 +*/
1.136485 +static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
1.136486 +  char *zText = 0;
1.136487 +  RtreeNode node;
1.136488 +  Rtree tree;
1.136489 +  int ii;
1.136490 +
1.136491 +  UNUSED_PARAMETER(nArg);
1.136492 +  memset(&node, 0, sizeof(RtreeNode));
1.136493 +  memset(&tree, 0, sizeof(Rtree));
1.136494 +  tree.nDim = sqlite3_value_int(apArg[0]);
1.136495 +  tree.nBytesPerCell = 8 + 8 * tree.nDim;
1.136496 +  node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
1.136497 +
1.136498 +  for(ii=0; ii<NCELL(&node); ii++){
1.136499 +    char zCell[512];
1.136500 +    int nCell = 0;
1.136501 +    RtreeCell cell;
1.136502 +    int jj;
1.136503 +
1.136504 +    nodeGetCell(&tree, &node, ii, &cell);
1.136505 +    sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
1.136506 +    nCell = (int)strlen(zCell);
1.136507 +    for(jj=0; jj<tree.nDim*2; jj++){
1.136508 +#ifndef SQLITE_RTREE_INT_ONLY
1.136509 +      sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
1.136510 +                       (double)cell.aCoord[jj].f);
1.136511 +#else
1.136512 +      sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
1.136513 +                       cell.aCoord[jj].i);
1.136514 +#endif
1.136515 +      nCell = (int)strlen(zCell);
1.136516 +    }
1.136517 +
1.136518 +    if( zText ){
1.136519 +      char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
1.136520 +      sqlite3_free(zText);
1.136521 +      zText = zTextNew;
1.136522 +    }else{
1.136523 +      zText = sqlite3_mprintf("{%s}", zCell);
1.136524 +    }
1.136525 +  }
1.136526 +  
1.136527 +  sqlite3_result_text(ctx, zText, -1, sqlite3_free);
1.136528 +}
1.136529 +
1.136530 +static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
1.136531 +  UNUSED_PARAMETER(nArg);
1.136532 +  if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB 
1.136533 +   || sqlite3_value_bytes(apArg[0])<2
1.136534 +  ){
1.136535 +    sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 
1.136536 +  }else{
1.136537 +    u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
1.136538 +    sqlite3_result_int(ctx, readInt16(zBlob));
1.136539 +  }
1.136540 +}
1.136541 +
1.136542 +/*
1.136543 +** Register the r-tree module with database handle db. This creates the
1.136544 +** virtual table module "rtree" and the debugging/analysis scalar 
1.136545 +** function "rtreenode".
1.136546 +*/
1.136547 +SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
1.136548 +  const int utf8 = SQLITE_UTF8;
1.136549 +  int rc;
1.136550 +
1.136551 +  rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
1.136552 +  if( rc==SQLITE_OK ){
1.136553 +    rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
1.136554 +  }
1.136555 +  if( rc==SQLITE_OK ){
1.136556 +#ifdef SQLITE_RTREE_INT_ONLY
1.136557 +    void *c = (void *)RTREE_COORD_INT32;
1.136558 +#else
1.136559 +    void *c = (void *)RTREE_COORD_REAL32;
1.136560 +#endif
1.136561 +    rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
1.136562 +  }
1.136563 +  if( rc==SQLITE_OK ){
1.136564 +    void *c = (void *)RTREE_COORD_INT32;
1.136565 +    rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
1.136566 +  }
1.136567 +
1.136568 +  return rc;
1.136569 +}
1.136570 +
1.136571 +/*
1.136572 +** A version of sqlite3_free() that can be used as a callback. This is used
1.136573 +** in two places - as the destructor for the blob value returned by the
1.136574 +** invocation of a geometry function, and as the destructor for the geometry
1.136575 +** functions themselves.
1.136576 +*/
1.136577 +static void doSqlite3Free(void *p){
1.136578 +  sqlite3_free(p);
1.136579 +}
1.136580 +
1.136581 +/*
1.136582 +** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
1.136583 +** scalar user function. This C function is the callback used for all such
1.136584 +** registered SQL functions.
1.136585 +**
1.136586 +** The scalar user functions return a blob that is interpreted by r-tree
1.136587 +** table MATCH operators.
1.136588 +*/
1.136589 +static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
1.136590 +  RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
1.136591 +  RtreeMatchArg *pBlob;
1.136592 +  int nBlob;
1.136593 +
1.136594 +  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
1.136595 +  pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
1.136596 +  if( !pBlob ){
1.136597 +    sqlite3_result_error_nomem(ctx);
1.136598 +  }else{
1.136599 +    int i;
1.136600 +    pBlob->magic = RTREE_GEOMETRY_MAGIC;
1.136601 +    pBlob->xGeom = pGeomCtx->xGeom;
1.136602 +    pBlob->pContext = pGeomCtx->pContext;
1.136603 +    pBlob->nParam = nArg;
1.136604 +    for(i=0; i<nArg; i++){
1.136605 +#ifdef SQLITE_RTREE_INT_ONLY
1.136606 +      pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
1.136607 +#else
1.136608 +      pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
1.136609 +#endif
1.136610 +    }
1.136611 +    sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
1.136612 +  }
1.136613 +}
1.136614 +
1.136615 +/*
1.136616 +** Register a new geometry function for use with the r-tree MATCH operator.
1.136617 +*/
1.136618 +SQLITE_API int sqlite3_rtree_geometry_callback(
1.136619 +  sqlite3 *db,
1.136620 +  const char *zGeom,
1.136621 +  int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
1.136622 +  void *pContext
1.136623 +){
1.136624 +  RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
1.136625 +
1.136626 +  /* Allocate and populate the context object. */
1.136627 +  pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
1.136628 +  if( !pGeomCtx ) return SQLITE_NOMEM;
1.136629 +  pGeomCtx->xGeom = xGeom;
1.136630 +  pGeomCtx->pContext = pContext;
1.136631 +
1.136632 +  /* Create the new user-function. Register a destructor function to delete
1.136633 +  ** the context object when it is no longer required.  */
1.136634 +  return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, 
1.136635 +      (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
1.136636 +  );
1.136637 +}
1.136638 +
1.136639 +#if !SQLITE_CORE
1.136640 +SQLITE_API int sqlite3_extension_init(
1.136641 +  sqlite3 *db,
1.136642 +  char **pzErrMsg,
1.136643 +  const sqlite3_api_routines *pApi
1.136644 +){
1.136645 +  SQLITE_EXTENSION_INIT2(pApi)
1.136646 +  return sqlite3RtreeInit(db);
1.136647 +}
1.136648 +#endif
1.136649 +
1.136650 +#endif
1.136651 +
1.136652 +/************** End of rtree.c ***********************************************/
1.136653 +/************** Begin file icu.c *********************************************/
1.136654 +/*
1.136655 +** 2007 May 6
1.136656 +**
1.136657 +** The author disclaims copyright to this source code.  In place of
1.136658 +** a legal notice, here is a blessing:
1.136659 +**
1.136660 +**    May you do good and not evil.
1.136661 +**    May you find forgiveness for yourself and forgive others.
1.136662 +**    May you share freely, never taking more than you give.
1.136663 +**
1.136664 +*************************************************************************
1.136665 +** $Id$
1.136666 +**
1.136667 +** This file implements an integration between the ICU library 
1.136668 +** ("International Components for Unicode", an open-source library 
1.136669 +** for handling unicode data) and SQLite. The integration uses 
1.136670 +** ICU to provide the following to SQLite:
1.136671 +**
1.136672 +**   * An implementation of the SQL regexp() function (and hence REGEXP
1.136673 +**     operator) using the ICU uregex_XX() APIs.
1.136674 +**
1.136675 +**   * Implementations of the SQL scalar upper() and lower() functions
1.136676 +**     for case mapping.
1.136677 +**
1.136678 +**   * Integration of ICU and SQLite collation seqences.
1.136679 +**
1.136680 +**   * An implementation of the LIKE operator that uses ICU to 
1.136681 +**     provide case-independent matching.
1.136682 +*/
1.136683 +
1.136684 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
1.136685 +
1.136686 +/* Include ICU headers */
1.136687 +#include <unicode/utypes.h>
1.136688 +#include <unicode/uregex.h>
1.136689 +#include <unicode/ustring.h>
1.136690 +#include <unicode/ucol.h>
1.136691 +
1.136692 +/* #include <assert.h> */
1.136693 +
1.136694 +#ifndef SQLITE_CORE
1.136695 +  SQLITE_EXTENSION_INIT1
1.136696 +#else
1.136697 +#endif
1.136698 +
1.136699 +/*
1.136700 +** Maximum length (in bytes) of the pattern in a LIKE or GLOB
1.136701 +** operator.
1.136702 +*/
1.136703 +#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
1.136704 +# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
1.136705 +#endif
1.136706 +
1.136707 +/*
1.136708 +** Version of sqlite3_free() that is always a function, never a macro.
1.136709 +*/
1.136710 +static void xFree(void *p){
1.136711 +  sqlite3_free(p);
1.136712 +}
1.136713 +
1.136714 +/*
1.136715 +** Compare two UTF-8 strings for equality where the first string is
1.136716 +** a "LIKE" expression. Return true (1) if they are the same and 
1.136717 +** false (0) if they are different.
1.136718 +*/
1.136719 +static int icuLikeCompare(
1.136720 +  const uint8_t *zPattern,   /* LIKE pattern */
1.136721 +  const uint8_t *zString,    /* The UTF-8 string to compare against */
1.136722 +  const UChar32 uEsc         /* The escape character */
1.136723 +){
1.136724 +  static const int MATCH_ONE = (UChar32)'_';
1.136725 +  static const int MATCH_ALL = (UChar32)'%';
1.136726 +
1.136727 +  int iPattern = 0;       /* Current byte index in zPattern */
1.136728 +  int iString = 0;        /* Current byte index in zString */
1.136729 +
1.136730 +  int prevEscape = 0;     /* True if the previous character was uEsc */
1.136731 +
1.136732 +  while( zPattern[iPattern]!=0 ){
1.136733 +
1.136734 +    /* Read (and consume) the next character from the input pattern. */
1.136735 +    UChar32 uPattern;
1.136736 +    U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
1.136737 +    assert(uPattern!=0);
1.136738 +
1.136739 +    /* There are now 4 possibilities:
1.136740 +    **
1.136741 +    **     1. uPattern is an unescaped match-all character "%",
1.136742 +    **     2. uPattern is an unescaped match-one character "_",
1.136743 +    **     3. uPattern is an unescaped escape character, or
1.136744 +    **     4. uPattern is to be handled as an ordinary character
1.136745 +    */
1.136746 +    if( !prevEscape && uPattern==MATCH_ALL ){
1.136747 +      /* Case 1. */
1.136748 +      uint8_t c;
1.136749 +
1.136750 +      /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
1.136751 +      ** MATCH_ALL. For each MATCH_ONE, skip one character in the 
1.136752 +      ** test string.
1.136753 +      */
1.136754 +      while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
1.136755 +        if( c==MATCH_ONE ){
1.136756 +          if( zString[iString]==0 ) return 0;
1.136757 +          U8_FWD_1_UNSAFE(zString, iString);
1.136758 +        }
1.136759 +        iPattern++;
1.136760 +      }
1.136761 +
1.136762 +      if( zPattern[iPattern]==0 ) return 1;
1.136763 +
1.136764 +      while( zString[iString] ){
1.136765 +        if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
1.136766 +          return 1;
1.136767 +        }
1.136768 +        U8_FWD_1_UNSAFE(zString, iString);
1.136769 +      }
1.136770 +      return 0;
1.136771 +
1.136772 +    }else if( !prevEscape && uPattern==MATCH_ONE ){
1.136773 +      /* Case 2. */
1.136774 +      if( zString[iString]==0 ) return 0;
1.136775 +      U8_FWD_1_UNSAFE(zString, iString);
1.136776 +
1.136777 +    }else if( !prevEscape && uPattern==uEsc){
1.136778 +      /* Case 3. */
1.136779 +      prevEscape = 1;
1.136780 +
1.136781 +    }else{
1.136782 +      /* Case 4. */
1.136783 +      UChar32 uString;
1.136784 +      U8_NEXT_UNSAFE(zString, iString, uString);
1.136785 +      uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
1.136786 +      uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
1.136787 +      if( uString!=uPattern ){
1.136788 +        return 0;
1.136789 +      }
1.136790 +      prevEscape = 0;
1.136791 +    }
1.136792 +  }
1.136793 +
1.136794 +  return zString[iString]==0;
1.136795 +}
1.136796 +
1.136797 +/*
1.136798 +** Implementation of the like() SQL function.  This function implements
1.136799 +** the build-in LIKE operator.  The first argument to the function is the
1.136800 +** pattern and the second argument is the string.  So, the SQL statements:
1.136801 +**
1.136802 +**       A LIKE B
1.136803 +**
1.136804 +** is implemented as like(B, A). If there is an escape character E, 
1.136805 +**
1.136806 +**       A LIKE B ESCAPE E
1.136807 +**
1.136808 +** is mapped to like(B, A, E).
1.136809 +*/
1.136810 +static void icuLikeFunc(
1.136811 +  sqlite3_context *context, 
1.136812 +  int argc, 
1.136813 +  sqlite3_value **argv
1.136814 +){
1.136815 +  const unsigned char *zA = sqlite3_value_text(argv[0]);
1.136816 +  const unsigned char *zB = sqlite3_value_text(argv[1]);
1.136817 +  UChar32 uEsc = 0;
1.136818 +
1.136819 +  /* Limit the length of the LIKE or GLOB pattern to avoid problems
1.136820 +  ** of deep recursion and N*N behavior in patternCompare().
1.136821 +  */
1.136822 +  if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
1.136823 +    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
1.136824 +    return;
1.136825 +  }
1.136826 +
1.136827 +
1.136828 +  if( argc==3 ){
1.136829 +    /* The escape character string must consist of a single UTF-8 character.
1.136830 +    ** Otherwise, return an error.
1.136831 +    */
1.136832 +    int nE= sqlite3_value_bytes(argv[2]);
1.136833 +    const unsigned char *zE = sqlite3_value_text(argv[2]);
1.136834 +    int i = 0;
1.136835 +    if( zE==0 ) return;
1.136836 +    U8_NEXT(zE, i, nE, uEsc);
1.136837 +    if( i!=nE){
1.136838 +      sqlite3_result_error(context, 
1.136839 +          "ESCAPE expression must be a single character", -1);
1.136840 +      return;
1.136841 +    }
1.136842 +  }
1.136843 +
1.136844 +  if( zA && zB ){
1.136845 +    sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
1.136846 +  }
1.136847 +}
1.136848 +
1.136849 +/*
1.136850 +** This function is called when an ICU function called from within
1.136851 +** the implementation of an SQL scalar function returns an error.
1.136852 +**
1.136853 +** The scalar function context passed as the first argument is 
1.136854 +** loaded with an error message based on the following two args.
1.136855 +*/
1.136856 +static void icuFunctionError(
1.136857 +  sqlite3_context *pCtx,       /* SQLite scalar function context */
1.136858 +  const char *zName,           /* Name of ICU function that failed */
1.136859 +  UErrorCode e                 /* Error code returned by ICU function */
1.136860 +){
1.136861 +  char zBuf[128];
1.136862 +  sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
1.136863 +  zBuf[127] = '\0';
1.136864 +  sqlite3_result_error(pCtx, zBuf, -1);
1.136865 +}
1.136866 +
1.136867 +/*
1.136868 +** Function to delete compiled regexp objects. Registered as
1.136869 +** a destructor function with sqlite3_set_auxdata().
1.136870 +*/
1.136871 +static void icuRegexpDelete(void *p){
1.136872 +  URegularExpression *pExpr = (URegularExpression *)p;
1.136873 +  uregex_close(pExpr);
1.136874 +}
1.136875 +
1.136876 +/*
1.136877 +** Implementation of SQLite REGEXP operator. This scalar function takes
1.136878 +** two arguments. The first is a regular expression pattern to compile
1.136879 +** the second is a string to match against that pattern. If either 
1.136880 +** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
1.136881 +** is 1 if the string matches the pattern, or 0 otherwise.
1.136882 +**
1.136883 +** SQLite maps the regexp() function to the regexp() operator such
1.136884 +** that the following two are equivalent:
1.136885 +**
1.136886 +**     zString REGEXP zPattern
1.136887 +**     regexp(zPattern, zString)
1.136888 +**
1.136889 +** Uses the following ICU regexp APIs:
1.136890 +**
1.136891 +**     uregex_open()
1.136892 +**     uregex_matches()
1.136893 +**     uregex_close()
1.136894 +*/
1.136895 +static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
1.136896 +  UErrorCode status = U_ZERO_ERROR;
1.136897 +  URegularExpression *pExpr;
1.136898 +  UBool res;
1.136899 +  const UChar *zString = sqlite3_value_text16(apArg[1]);
1.136900 +
1.136901 +  (void)nArg;  /* Unused parameter */
1.136902 +
1.136903 +  /* If the left hand side of the regexp operator is NULL, 
1.136904 +  ** then the result is also NULL. 
1.136905 +  */
1.136906 +  if( !zString ){
1.136907 +    return;
1.136908 +  }
1.136909 +
1.136910 +  pExpr = sqlite3_get_auxdata(p, 0);
1.136911 +  if( !pExpr ){
1.136912 +    const UChar *zPattern = sqlite3_value_text16(apArg[0]);
1.136913 +    if( !zPattern ){
1.136914 +      return;
1.136915 +    }
1.136916 +    pExpr = uregex_open(zPattern, -1, 0, 0, &status);
1.136917 +
1.136918 +    if( U_SUCCESS(status) ){
1.136919 +      sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
1.136920 +    }else{
1.136921 +      assert(!pExpr);
1.136922 +      icuFunctionError(p, "uregex_open", status);
1.136923 +      return;
1.136924 +    }
1.136925 +  }
1.136926 +
1.136927 +  /* Configure the text that the regular expression operates on. */
1.136928 +  uregex_setText(pExpr, zString, -1, &status);
1.136929 +  if( !U_SUCCESS(status) ){
1.136930 +    icuFunctionError(p, "uregex_setText", status);
1.136931 +    return;
1.136932 +  }
1.136933 +
1.136934 +  /* Attempt the match */
1.136935 +  res = uregex_matches(pExpr, 0, &status);
1.136936 +  if( !U_SUCCESS(status) ){
1.136937 +    icuFunctionError(p, "uregex_matches", status);
1.136938 +    return;
1.136939 +  }
1.136940 +
1.136941 +  /* Set the text that the regular expression operates on to a NULL
1.136942 +  ** pointer. This is not really necessary, but it is tidier than 
1.136943 +  ** leaving the regular expression object configured with an invalid
1.136944 +  ** pointer after this function returns.
1.136945 +  */
1.136946 +  uregex_setText(pExpr, 0, 0, &status);
1.136947 +
1.136948 +  /* Return 1 or 0. */
1.136949 +  sqlite3_result_int(p, res ? 1 : 0);
1.136950 +}
1.136951 +
1.136952 +/*
1.136953 +** Implementations of scalar functions for case mapping - upper() and 
1.136954 +** lower(). Function upper() converts its input to upper-case (ABC).
1.136955 +** Function lower() converts to lower-case (abc).
1.136956 +**
1.136957 +** ICU provides two types of case mapping, "general" case mapping and
1.136958 +** "language specific". Refer to ICU documentation for the differences
1.136959 +** between the two.
1.136960 +**
1.136961 +** To utilise "general" case mapping, the upper() or lower() scalar 
1.136962 +** functions are invoked with one argument:
1.136963 +**
1.136964 +**     upper('ABC') -> 'abc'
1.136965 +**     lower('abc') -> 'ABC'
1.136966 +**
1.136967 +** To access ICU "language specific" case mapping, upper() or lower()
1.136968 +** should be invoked with two arguments. The second argument is the name
1.136969 +** of the locale to use. Passing an empty string ("") or SQL NULL value
1.136970 +** as the second argument is the same as invoking the 1 argument version
1.136971 +** of upper() or lower().
1.136972 +**
1.136973 +**     lower('I', 'en_us') -> 'i'
1.136974 +**     lower('I', 'tr_tr') -> 'ı' (small dotless i)
1.136975 +**
1.136976 +** http://www.icu-project.org/userguide/posix.html#case_mappings
1.136977 +*/
1.136978 +static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
1.136979 +  const UChar *zInput;
1.136980 +  UChar *zOutput;
1.136981 +  int nInput;
1.136982 +  int nOutput;
1.136983 +
1.136984 +  UErrorCode status = U_ZERO_ERROR;
1.136985 +  const char *zLocale = 0;
1.136986 +
1.136987 +  assert(nArg==1 || nArg==2);
1.136988 +  if( nArg==2 ){
1.136989 +    zLocale = (const char *)sqlite3_value_text(apArg[1]);
1.136990 +  }
1.136991 +
1.136992 +  zInput = sqlite3_value_text16(apArg[0]);
1.136993 +  if( !zInput ){
1.136994 +    return;
1.136995 +  }
1.136996 +  nInput = sqlite3_value_bytes16(apArg[0]);
1.136997 +
1.136998 +  nOutput = nInput * 2 + 2;
1.136999 +  zOutput = sqlite3_malloc(nOutput);
1.137000 +  if( !zOutput ){
1.137001 +    return;
1.137002 +  }
1.137003 +
1.137004 +  if( sqlite3_user_data(p) ){
1.137005 +    u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
1.137006 +  }else{
1.137007 +    u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
1.137008 +  }
1.137009 +
1.137010 +  if( !U_SUCCESS(status) ){
1.137011 +    icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
1.137012 +    return;
1.137013 +  }
1.137014 +
1.137015 +  sqlite3_result_text16(p, zOutput, -1, xFree);
1.137016 +}
1.137017 +
1.137018 +/*
1.137019 +** Collation sequence destructor function. The pCtx argument points to
1.137020 +** a UCollator structure previously allocated using ucol_open().
1.137021 +*/
1.137022 +static void icuCollationDel(void *pCtx){
1.137023 +  UCollator *p = (UCollator *)pCtx;
1.137024 +  ucol_close(p);
1.137025 +}
1.137026 +
1.137027 +/*
1.137028 +** Collation sequence comparison function. The pCtx argument points to
1.137029 +** a UCollator structure previously allocated using ucol_open().
1.137030 +*/
1.137031 +static int icuCollationColl(
1.137032 +  void *pCtx,
1.137033 +  int nLeft,
1.137034 +  const void *zLeft,
1.137035 +  int nRight,
1.137036 +  const void *zRight
1.137037 +){
1.137038 +  UCollationResult res;
1.137039 +  UCollator *p = (UCollator *)pCtx;
1.137040 +  res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
1.137041 +  switch( res ){
1.137042 +    case UCOL_LESS:    return -1;
1.137043 +    case UCOL_GREATER: return +1;
1.137044 +    case UCOL_EQUAL:   return 0;
1.137045 +  }
1.137046 +  assert(!"Unexpected return value from ucol_strcoll()");
1.137047 +  return 0;
1.137048 +}
1.137049 +
1.137050 +/*
1.137051 +** Implementation of the scalar function icu_load_collation().
1.137052 +**
1.137053 +** This scalar function is used to add ICU collation based collation 
1.137054 +** types to an SQLite database connection. It is intended to be called
1.137055 +** as follows:
1.137056 +**
1.137057 +**     SELECT icu_load_collation(<locale>, <collation-name>);
1.137058 +**
1.137059 +** Where <locale> is a string containing an ICU locale identifier (i.e.
1.137060 +** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
1.137061 +** collation sequence to create.
1.137062 +*/
1.137063 +static void icuLoadCollation(
1.137064 +  sqlite3_context *p, 
1.137065 +  int nArg, 
1.137066 +  sqlite3_value **apArg
1.137067 +){
1.137068 +  sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
1.137069 +  UErrorCode status = U_ZERO_ERROR;
1.137070 +  const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
1.137071 +  const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
1.137072 +  UCollator *pUCollator;    /* ICU library collation object */
1.137073 +  int rc;                   /* Return code from sqlite3_create_collation_x() */
1.137074 +
1.137075 +  assert(nArg==2);
1.137076 +  zLocale = (const char *)sqlite3_value_text(apArg[0]);
1.137077 +  zName = (const char *)sqlite3_value_text(apArg[1]);
1.137078 +
1.137079 +  if( !zLocale || !zName ){
1.137080 +    return;
1.137081 +  }
1.137082 +
1.137083 +  pUCollator = ucol_open(zLocale, &status);
1.137084 +  if( !U_SUCCESS(status) ){
1.137085 +    icuFunctionError(p, "ucol_open", status);
1.137086 +    return;
1.137087 +  }
1.137088 +  assert(p);
1.137089 +
1.137090 +  rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, 
1.137091 +      icuCollationColl, icuCollationDel
1.137092 +  );
1.137093 +  if( rc!=SQLITE_OK ){
1.137094 +    ucol_close(pUCollator);
1.137095 +    sqlite3_result_error(p, "Error registering collation function", -1);
1.137096 +  }
1.137097 +}
1.137098 +
1.137099 +/*
1.137100 +** Register the ICU extension functions with database db.
1.137101 +*/
1.137102 +SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
1.137103 +  struct IcuScalar {
1.137104 +    const char *zName;                        /* Function name */
1.137105 +    int nArg;                                 /* Number of arguments */
1.137106 +    int enc;                                  /* Optimal text encoding */
1.137107 +    void *pContext;                           /* sqlite3_user_data() context */
1.137108 +    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1.137109 +  } scalars[] = {
1.137110 +    {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
1.137111 +
1.137112 +    {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
1.137113 +    {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
1.137114 +    {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
1.137115 +    {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
1.137116 +
1.137117 +    {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
1.137118 +    {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
1.137119 +    {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
1.137120 +    {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
1.137121 +
1.137122 +    {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
1.137123 +    {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
1.137124 +
1.137125 +    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
1.137126 +  };
1.137127 +
1.137128 +  int rc = SQLITE_OK;
1.137129 +  int i;
1.137130 +
1.137131 +  for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
1.137132 +    struct IcuScalar *p = &scalars[i];
1.137133 +    rc = sqlite3_create_function(
1.137134 +        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
1.137135 +    );
1.137136 +  }
1.137137 +
1.137138 +  return rc;
1.137139 +}
1.137140 +
1.137141 +#if !SQLITE_CORE
1.137142 +SQLITE_API int sqlite3_extension_init(
1.137143 +  sqlite3 *db, 
1.137144 +  char **pzErrMsg,
1.137145 +  const sqlite3_api_routines *pApi
1.137146 +){
1.137147 +  SQLITE_EXTENSION_INIT2(pApi)
1.137148 +  return sqlite3IcuInit(db);
1.137149 +}
1.137150 +#endif
1.137151 +
1.137152 +#endif
1.137153 +
1.137154 +/************** End of icu.c *************************************************/
1.137155 +/************** Begin file fts3_icu.c ****************************************/
1.137156 +/*
1.137157 +** 2007 June 22
1.137158 +**
1.137159 +** The author disclaims copyright to this source code.  In place of
1.137160 +** a legal notice, here is a blessing:
1.137161 +**
1.137162 +**    May you do good and not evil.
1.137163 +**    May you find forgiveness for yourself and forgive others.
1.137164 +**    May you share freely, never taking more than you give.
1.137165 +**
1.137166 +*************************************************************************
1.137167 +** This file implements a tokenizer for fts3 based on the ICU library.
1.137168 +*/
1.137169 +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
1.137170 +#ifdef SQLITE_ENABLE_ICU
1.137171 +
1.137172 +/* #include <assert.h> */
1.137173 +/* #include <string.h> */
1.137174 +
1.137175 +#include <unicode/ubrk.h>
1.137176 +/* #include <unicode/ucol.h> */
1.137177 +/* #include <unicode/ustring.h> */
1.137178 +#include <unicode/utf16.h>
1.137179 +
1.137180 +typedef struct IcuTokenizer IcuTokenizer;
1.137181 +typedef struct IcuCursor IcuCursor;
1.137182 +
1.137183 +struct IcuTokenizer {
1.137184 +  sqlite3_tokenizer base;
1.137185 +  char *zLocale;
1.137186 +};
1.137187 +
1.137188 +struct IcuCursor {
1.137189 +  sqlite3_tokenizer_cursor base;
1.137190 +
1.137191 +  UBreakIterator *pIter;      /* ICU break-iterator object */
1.137192 +  int nChar;                  /* Number of UChar elements in pInput */
1.137193 +  UChar *aChar;               /* Copy of input using utf-16 encoding */
1.137194 +  int *aOffset;               /* Offsets of each character in utf-8 input */
1.137195 +
1.137196 +  int nBuffer;
1.137197 +  char *zBuffer;
1.137198 +
1.137199 +  int iToken;
1.137200 +};
1.137201 +
1.137202 +/*
1.137203 +** Create a new tokenizer instance.
1.137204 +*/
1.137205 +static int icuCreate(
1.137206 +  int argc,                            /* Number of entries in argv[] */
1.137207 +  const char * const *argv,            /* Tokenizer creation arguments */
1.137208 +  sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
1.137209 +){
1.137210 +  IcuTokenizer *p;
1.137211 +  int n = 0;
1.137212 +
1.137213 +  if( argc>0 ){
1.137214 +    n = strlen(argv[0])+1;
1.137215 +  }
1.137216 +  p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
1.137217 +  if( !p ){
1.137218 +    return SQLITE_NOMEM;
1.137219 +  }
1.137220 +  memset(p, 0, sizeof(IcuTokenizer));
1.137221 +
1.137222 +  if( n ){
1.137223 +    p->zLocale = (char *)&p[1];
1.137224 +    memcpy(p->zLocale, argv[0], n);
1.137225 +  }
1.137226 +
1.137227 +  *ppTokenizer = (sqlite3_tokenizer *)p;
1.137228 +
1.137229 +  return SQLITE_OK;
1.137230 +}
1.137231 +
1.137232 +/*
1.137233 +** Destroy a tokenizer
1.137234 +*/
1.137235 +static int icuDestroy(sqlite3_tokenizer *pTokenizer){
1.137236 +  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
1.137237 +  sqlite3_free(p);
1.137238 +  return SQLITE_OK;
1.137239 +}
1.137240 +
1.137241 +/*
1.137242 +** Prepare to begin tokenizing a particular string.  The input
1.137243 +** string to be tokenized is pInput[0..nBytes-1].  A cursor
1.137244 +** used to incrementally tokenize this string is returned in 
1.137245 +** *ppCursor.
1.137246 +*/
1.137247 +static int icuOpen(
1.137248 +  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
1.137249 +  const char *zInput,                    /* Input string */
1.137250 +  int nInput,                            /* Length of zInput in bytes */
1.137251 +  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
1.137252 +){
1.137253 +  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
1.137254 +  IcuCursor *pCsr;
1.137255 +
1.137256 +  const int32_t opt = U_FOLD_CASE_DEFAULT;
1.137257 +  UErrorCode status = U_ZERO_ERROR;
1.137258 +  int nChar;
1.137259 +
1.137260 +  UChar32 c;
1.137261 +  int iInput = 0;
1.137262 +  int iOut = 0;
1.137263 +
1.137264 +  *ppCursor = 0;
1.137265 +
1.137266 +  if( zInput==0 ){
1.137267 +    nInput = 0;
1.137268 +    zInput = "";
1.137269 +  }else if( nInput<0 ){
1.137270 +    nInput = strlen(zInput);
1.137271 +  }
1.137272 +  nChar = nInput+1;
1.137273 +  pCsr = (IcuCursor *)sqlite3_malloc(
1.137274 +      sizeof(IcuCursor) +                /* IcuCursor */
1.137275 +      ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
1.137276 +      (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
1.137277 +  );
1.137278 +  if( !pCsr ){
1.137279 +    return SQLITE_NOMEM;
1.137280 +  }
1.137281 +  memset(pCsr, 0, sizeof(IcuCursor));
1.137282 +  pCsr->aChar = (UChar *)&pCsr[1];
1.137283 +  pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
1.137284 +
1.137285 +  pCsr->aOffset[iOut] = iInput;
1.137286 +  U8_NEXT(zInput, iInput, nInput, c); 
1.137287 +  while( c>0 ){
1.137288 +    int isError = 0;
1.137289 +    c = u_foldCase(c, opt);
1.137290 +    U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
1.137291 +    if( isError ){
1.137292 +      sqlite3_free(pCsr);
1.137293 +      return SQLITE_ERROR;
1.137294 +    }
1.137295 +    pCsr->aOffset[iOut] = iInput;
1.137296 +
1.137297 +    if( iInput<nInput ){
1.137298 +      U8_NEXT(zInput, iInput, nInput, c);
1.137299 +    }else{
1.137300 +      c = 0;
1.137301 +    }
1.137302 +  }
1.137303 +
1.137304 +  pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
1.137305 +  if( !U_SUCCESS(status) ){
1.137306 +    sqlite3_free(pCsr);
1.137307 +    return SQLITE_ERROR;
1.137308 +  }
1.137309 +  pCsr->nChar = iOut;
1.137310 +
1.137311 +  ubrk_first(pCsr->pIter);
1.137312 +  *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
1.137313 +  return SQLITE_OK;
1.137314 +}
1.137315 +
1.137316 +/*
1.137317 +** Close a tokenization cursor previously opened by a call to icuOpen().
1.137318 +*/
1.137319 +static int icuClose(sqlite3_tokenizer_cursor *pCursor){
1.137320 +  IcuCursor *pCsr = (IcuCursor *)pCursor;
1.137321 +  ubrk_close(pCsr->pIter);
1.137322 +  sqlite3_free(pCsr->zBuffer);
1.137323 +  sqlite3_free(pCsr);
1.137324 +  return SQLITE_OK;
1.137325 +}
1.137326 +
1.137327 +/*
1.137328 +** Extract the next token from a tokenization cursor.
1.137329 +*/
1.137330 +static int icuNext(
1.137331 +  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
1.137332 +  const char **ppToken,               /* OUT: *ppToken is the token text */
1.137333 +  int *pnBytes,                       /* OUT: Number of bytes in token */
1.137334 +  int *piStartOffset,                 /* OUT: Starting offset of token */
1.137335 +  int *piEndOffset,                   /* OUT: Ending offset of token */
1.137336 +  int *piPosition                     /* OUT: Position integer of token */
1.137337 +){
1.137338 +  IcuCursor *pCsr = (IcuCursor *)pCursor;
1.137339 +
1.137340 +  int iStart = 0;
1.137341 +  int iEnd = 0;
1.137342 +  int nByte = 0;
1.137343 +
1.137344 +  while( iStart==iEnd ){
1.137345 +    UChar32 c;
1.137346 +
1.137347 +    iStart = ubrk_current(pCsr->pIter);
1.137348 +    iEnd = ubrk_next(pCsr->pIter);
1.137349 +    if( iEnd==UBRK_DONE ){
1.137350 +      return SQLITE_DONE;
1.137351 +    }
1.137352 +
1.137353 +    while( iStart<iEnd ){
1.137354 +      int iWhite = iStart;
1.137355 +      U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
1.137356 +      if( u_isspace(c) ){
1.137357 +        iStart = iWhite;
1.137358 +      }else{
1.137359 +        break;
1.137360 +      }
1.137361 +    }
1.137362 +    assert(iStart<=iEnd);
1.137363 +  }
1.137364 +
1.137365 +  do {
1.137366 +    UErrorCode status = U_ZERO_ERROR;
1.137367 +    if( nByte ){
1.137368 +      char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
1.137369 +      if( !zNew ){
1.137370 +        return SQLITE_NOMEM;
1.137371 +      }
1.137372 +      pCsr->zBuffer = zNew;
1.137373 +      pCsr->nBuffer = nByte;
1.137374 +    }
1.137375 +
1.137376 +    u_strToUTF8(
1.137377 +        pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
1.137378 +        &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
1.137379 +        &status                                  /* Output success/failure */
1.137380 +    );
1.137381 +  } while( nByte>pCsr->nBuffer );
1.137382 +
1.137383 +  *ppToken = pCsr->zBuffer;
1.137384 +  *pnBytes = nByte;
1.137385 +  *piStartOffset = pCsr->aOffset[iStart];
1.137386 +  *piEndOffset = pCsr->aOffset[iEnd];
1.137387 +  *piPosition = pCsr->iToken++;
1.137388 +
1.137389 +  return SQLITE_OK;
1.137390 +}
1.137391 +
1.137392 +/*
1.137393 +** The set of routines that implement the simple tokenizer
1.137394 +*/
1.137395 +static const sqlite3_tokenizer_module icuTokenizerModule = {
1.137396 +  0,                           /* iVersion */
1.137397 +  icuCreate,                   /* xCreate  */
1.137398 +  icuDestroy,                  /* xCreate  */
1.137399 +  icuOpen,                     /* xOpen    */
1.137400 +  icuClose,                    /* xClose   */
1.137401 +  icuNext,                     /* xNext    */
1.137402 +};
1.137403 +
1.137404 +/*
1.137405 +** Set *ppModule to point at the implementation of the ICU tokenizer.
1.137406 +*/
1.137407 +SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
1.137408 +  sqlite3_tokenizer_module const**ppModule
1.137409 +){
1.137410 +  *ppModule = &icuTokenizerModule;
1.137411 +}
1.137412 +
1.137413 +#endif /* defined(SQLITE_ENABLE_ICU) */
1.137414 +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
1.137415 +
1.137416 +/************** End of fts3_icu.c ********************************************/

mercurial